X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsim%2Fimagefile.cpp;h=df0e51ec073981d312bdf265891a909d7125cc3a;hp=cc7e23489af837a9b99ef30f9f42369abce3b090;hb=c00c639073653fac7463a88f2b000f263236550d;hpb=23b7ef994fc5d95fcca6d4ae69abbd5971101262 diff --git a/libctsim/imagefile.cpp b/libctsim/imagefile.cpp index cc7e234..df0e51e 100644 --- a/libctsim/imagefile.cpp +++ b/libctsim/imagefile.cpp @@ -9,7 +9,7 @@ ** This is part of the CTSim program ** Copyright (C) 1983-2000 Kevin Rosenberg ** -** $Id: imagefile.cpp,v 1.32 2001/01/02 10:23:46 kevin Exp $ +** $Id: imagefile.cpp,v 1.33 2001/01/02 16:02:13 kevin Exp $ ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License (version 2) as @@ -27,84 +27,84 @@ #include "ct.h" -const int ImageFile::FORMAT_INVALID = -1; -const int ImageFile::FORMAT_PGM = 0; -const int ImageFile::FORMAT_PGMASCII = 1; -#ifdef HAVE_PNG -const int ImageFile::FORMAT_PNG = 2; -const int ImageFile::FORMAT_PNG16 = 3; -#endif - -const char* ImageFile::s_aszFormatName[] = -{ - {"pgm"}, - {"pgmascii"}, -#ifdef HAVE_PNG - {"png"}, - {"png16"}, -#endif -}; - -const char* ImageFile::s_aszFormatTitle[] = -{ - {"PGM"}, - {"PGM ASCII"}, - {"PNG"}, - {"PNG 16-bit"}, -}; - -const int ImageFile::s_iFormatCount = sizeof(s_aszFormatName) / sizeof(const char*); - - +const int ImageFile::FORMAT_INVALID = -1; +const int ImageFile::FORMAT_PGM = 0; +const int ImageFile::FORMAT_PGMASCII = 1; +#ifdef HAVE_PNG +const int ImageFile::FORMAT_PNG = 2; +const int ImageFile::FORMAT_PNG16 = 3; +#endif + +const char* ImageFile::s_aszFormatName[] = +{ + {"pgm"}, + {"pgmascii"}, +#ifdef HAVE_PNG + {"png"}, + {"png16"}, +#endif +}; + +const char* ImageFile::s_aszFormatTitle[] = +{ + {"PGM"}, + {"PGM ASCII"}, + {"PNG"}, + {"PNG 16-bit"}, +}; + +const int ImageFile::s_iFormatCount = sizeof(s_aszFormatName) / sizeof(const char*); -F32Image::F32Image (int nx, int ny, int dataType) -: Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType) -{ -} - -F32Image::F32Image (void) -: Array2dFile() -{ - setPixelFormat (Array2dFile::PIXEL_FLOAT32); - setPixelSize (sizeof(kfloat32)); - setDataType (Array2dFile::DATA_TYPE_REAL); -} - -F64Image::F64Image (int nx, int ny, int dataType) -: Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64, dataType) -{ -} - -F64Image::F64Image (void) -: Array2dFile () -{ - setPixelFormat (PIXEL_FLOAT64); - setPixelSize (sizeof(kfloat64)); - setDataType (Array2dFile::DATA_TYPE_REAL); -} + + +F32Image::F32Image (int nx, int ny, int dataType) +: Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType) +{ +} + +F32Image::F32Image (void) +: Array2dFile() +{ + setPixelFormat (Array2dFile::PIXEL_FLOAT32); + setPixelSize (sizeof(kfloat32)); + setDataType (Array2dFile::DATA_TYPE_REAL); +} + +F64Image::F64Image (int nx, int ny, int dataType) +: Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64, dataType) +{ +} + +F64Image::F64Image (void) +: Array2dFile () +{ + setPixelFormat (PIXEL_FLOAT64); + setPixelSize (sizeof(kfloat64)); + setDataType (Array2dFile::DATA_TYPE_REAL); +} void ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param, double dInputScale, double dOutputScale) -{ - ImageFileArray v = getArray(); - SignalFilter filter (filterName, domainName, bw, filt_param); - - int iXCenter, iYCenter; - if (isEven (m_nx)) - iXCenter = m_nx / 2; - else - iXCenter = (m_nx - 1) / 2; - if (isEven (m_ny)) - iYCenter = m_ny / 2; - else - iYCenter = (m_ny - 1) / 2; - - for (unsigned int ix = 0; ix < m_nx; ix++) - for (unsigned int iy = 0; iy < m_ny; iy++) { - long lD2 = ((ix - iXCenter) * (ix - iXCenter)) + ((iy - iYCenter) * (iy - iYCenter)); - double r = ::sqrt (static_cast(lD2)) * dInputScale; - v[ix][iy] = filter.response (r) * dOutputScale; - } +{ + ImageFileArray v = getArray(); + SignalFilter filter (filterName, domainName, bw, filt_param); + + int iXCenter, iYCenter; + if (isEven (m_nx)) + iXCenter = m_nx / 2; + else + iXCenter = (m_nx - 1) / 2; + if (isEven (m_ny)) + iYCenter = m_ny / 2; + else + iYCenter = (m_ny - 1) / 2; + + for (unsigned int ix = 0; ix < m_nx; ix++) + for (unsigned int iy = 0; iy < m_ny; iy++) { + long lD2 = ((ix - iXCenter) * (ix - iXCenter)) + ((iy - iYCenter) * (iy - iYCenter)); + double r = ::sqrt (static_cast(lD2)) * dInputScale; + v[ix][iy] = filter.response (r) * dOutputScale; + } } int @@ -250,58 +250,58 @@ ImageFile::printStatistics (std::ostream& os) const { double min, max, mean, mode, median, stddev; - statistics (min, max, mean, mode, median, stddev); - if (isComplex()) - os << "Real Component Statistics" << std::endl; + statistics (min, max, mean, mode, median, stddev); + if (isComplex()) + os << "Real Component Statistics" << std::endl; os << " min: " << min << std::endl; os << " max: " << max << std::endl; os << " mean: " << mean << std::endl; os << " mode: " << mode << std::endl; os << "median: " << median << std::endl; - os << "stddev: " << stddev << std::endl; - - if (isComplex()) { - statistics (getImaginaryArray(), min, max, mean, mode, median, stddev); + os << "stddev: " << stddev << std::endl; + + if (isComplex()) { + statistics (getImaginaryArray(), min, max, mean, mode, median, stddev); os << std::endl << "Imaginary Component Statistics" << std::endl; - os << " min: " << min << std::endl; - os << " max: " << max << std::endl; - os << " mean: " << mean << std::endl; - os << " mode: " << mode << std::endl; - os << "median: " << median << std::endl; - os << "stddev: " << stddev << std::endl; - } + os << " min: " << min << std::endl; + os << " max: " << max << std::endl; + os << " mean: " << mean << std::endl; + os << " mode: " << mode << std::endl; + os << "median: " << median << std::endl; + os << "stddev: " << stddev << std::endl; + } } void ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const -{ - ImageFileArrayConst v = getArray(); - statistics (v, min, max, mean, mode, median, stddev); +{ + ImageFileArrayConst v = getArray(); + statistics (v, min, max, mean, mode, median, stddev); } -void -ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const -{ - int nx = m_nx; - int ny = m_ny; - - if (v == NULL || nx == 0 || ny == 0) - return; - - std::vector vecImage; - int iVec = 0; - vecImage.resize (nx * ny); - for (int ix = 0; ix < nx; ix++) { - for (int iy = 0; iy < ny; iy++) - vecImage[iVec++] = v[ix][iy]; - } - - vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev); -} - +void +ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const +{ + int nx = m_nx; + int ny = m_ny; + + if (v == NULL || nx == 0 || ny == 0) + return; + + std::vector vecImage; + int iVec = 0; + vecImage.resize (nx * ny); + for (int ix = 0; ix < nx; ix++) { + for (int iy = 0; iy < ny; iy++) + vecImage[iVec++] = v[ix][iy]; + } + + vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev); +} + void ImageFile::getMinMax (double& min, double& max) const { @@ -323,796 +323,796 @@ ImageFile::getMinMax (double& min, double& max) const } } } - -bool -ImageFile::convertRealToComplex () -{ - if (dataType() != Array2dFile::DATA_TYPE_REAL) - return false; - - if (! reallocRealToComplex()) - return false; - - ImageFileArray vImag = getImaginaryArray(); - for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumn vCol = vImag[ix]; - for (unsigned int iy = 0; iy < m_ny; iy++) - *vCol++ = 0; - } - - return true; -} - -bool -ImageFile::convertComplexToReal () -{ - if (dataType() != Array2dFile::DATA_TYPE_COMPLEX) - return false; - - ImageFileArray vReal = getArray(); - ImageFileArray vImag = getImaginaryArray(); - for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumn vRealCol = vReal[ix]; - ImageFileColumn vImagCol = vImag[ix]; - for (unsigned int iy = 0; iy < m_ny; iy++) { - CTSimComplex c (*vRealCol, *vImagCol); - *vRealCol++ = std::abs (c); - vImagCol++; - } - } - - return reallocComplexToReal(); -} - -bool -ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const -{ - if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]"); - return false; - } - - if (isComplex() || rRHS.isComplex() && ! result.isComplex()) - result.convertRealToComplex(); - - ImageFileArrayConst vLHS = getArray(); - ImageFileArrayConst vLHSImag = getImaginaryArray(); - ImageFileArrayConst vRHS = rRHS.getArray(); - ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); - ImageFileArray vResult = result.getArray(); - ImageFileArray vResultImag = result.getImaginaryArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy]; - if (result.isComplex()) { - vResultImag[ix][iy] = 0; - if (isComplex()) - vResultImag[ix][iy] += vLHSImag[ix][iy]; - if (rRHS.isComplex()) - vResultImag[ix][iy] -= vRHSImag[ix][iy]; - } - } - } - - return true; -} -bool -ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const -{ - if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]"); - return false; - } - - if (isComplex() || rRHS.isComplex() && ! result.isComplex()) - result.convertRealToComplex(); - - ImageFileArrayConst vLHS = getArray(); - ImageFileArrayConst vLHSImag = getImaginaryArray(); - ImageFileArrayConst vRHS = rRHS.getArray(); - ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); - ImageFileArray vResult = result.getArray(); - ImageFileArray vResultImag = result.getImaginaryArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy]; - if (result.isComplex()) { - vResultImag[ix][iy] = 0; - if (isComplex()) - vResultImag[ix][iy] += vLHSImag[ix][iy]; - if (rRHS.isComplex()) - vResultImag[ix][iy] += vRHSImag[ix][iy]; - } - } - } - - return true; -} - -bool -ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const -{ - if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]"); - return false; - } - - if (isComplex() || rRHS.isComplex() && ! result.isComplex()) - result.convertRealToComplex(); - - ImageFileArrayConst vLHS = getArray(); - ImageFileArrayConst vLHSImag = getImaginaryArray(); - ImageFileArrayConst vRHS = rRHS.getArray(); - ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); - ImageFileArray vResult = result.getArray(); - ImageFileArray vResultImag = result.getImaginaryArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - if (result.isComplex()) { - double dImag = 0; - if (isComplex()) - dImag = vLHSImag[ix][iy]; - std::complex cLHS (vLHS[ix][iy], dImag); - dImag = 0; - if (rRHS.isComplex()) - dImag = vRHSImag[ix][iy]; - std::complex cRHS (vRHS[ix][iy], dImag); - std::complex cResult = cLHS * cRHS; - vResult[ix][iy] = cResult.real(); - vResultImag[ix][iy] = cResult.imag(); - } else - vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy]; - } - } - - - return true; -} - -bool -ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const -{ - if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]"); - return false; - } - - if (isComplex() || rRHS.isComplex() && ! result.isComplex()) - result.convertRealToComplex(); - - ImageFileArrayConst vLHS = getArray(); - ImageFileArrayConst vLHSImag = getImaginaryArray(); - ImageFileArrayConst vRHS = rRHS.getArray(); - ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); - ImageFileArray vResult = result.getArray(); - ImageFileArray vResultImag = result.getImaginaryArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - if (result.isComplex()) { - double dImag = 0; - if (isComplex()) - dImag = vLHSImag[ix][iy]; - std::complex cLHS (vLHS[ix][iy], dImag); - dImag = 0; - if (rRHS.isComplex()) - dImag = vRHSImag[ix][iy]; - std::complex cRHS (vRHS[ix][iy], dImag); - std::complex cResult = cLHS / cRHS; - vResult[ix][iy] = cResult.real(); - vResultImag[ix][iy] = cResult.imag(); - } else { - if (vRHS != 0) - vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy]; - else - vResult[ix][iy] = 0; - } - } - } - - return true; -} - - -bool -ImageFile::invertPixelValues (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - if (isComplex() && ! result.isComplex()) - result.convertRealToComplex(); - - ImageFileArrayConst vLHS = getArray(); - ImageFileArray vResult = result.getArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) { - ImageFileColumnConst in = vLHS[ix]; - ImageFileColumn out = vResult[ix]; - for (unsigned int iy = 0; iy < m_ny; iy++) - *out++ = - *in++; - } - - return true; -} - -bool -ImageFile::sqrt (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - if (isComplex() && ! result.isComplex()) - result.convertRealToComplex(); - - bool bComplexOutput = result.isComplex(); - ImageFileArrayConst vLHS = getArray(); - if (! bComplexOutput) // check if should convert to complex output - for (unsigned int ix = 0; ix < m_nx; ix++) - for (unsigned int iy = 0; iy < m_ny; iy++) - if (! bComplexOutput && vLHS[ix][iy] < 0) { - result.convertRealToComplex(); - bComplexOutput = true; - break; - } - - ImageFileArrayConst vLHSImag = getImaginaryArray(); - ImageFileArray vResult = result.getArray(); - ImageFileArray vResultImag = result.getImaginaryArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - if (result.isComplex()) { - double dImag = 0; - if (isComplex()) - dImag = vLHSImag[ix][iy]; - std::complex cLHS (vLHS[ix][iy], dImag); - std::complex cResult = std::sqrt(cLHS); - vResult[ix][iy] = cResult.real(); - vResultImag[ix][iy] = cResult.imag(); - } else - vResult[ix][iy] = ::sqrt (vLHS[ix][iy]); - } - } - - - return true; -} - -bool -ImageFile::log (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - if (isComplex() && ! result.isComplex()) - result.convertRealToComplex(); - - ImageFileArrayConst vLHS = getArray(); - ImageFileArrayConst vLHSImag = getImaginaryArray(); - ImageFileArray vResult = result.getArray(); - ImageFileArray vResultImag = result.getImaginaryArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - if (result.isComplex()) { - double dImag = 0; - if (isComplex()) - dImag = vLHSImag[ix][iy]; - std::complex cLHS (vLHS[ix][iy], dImag); - std::complex cResult = std::log (cLHS); - vResult[ix][iy] = cResult.real(); - vResultImag[ix][iy] = cResult.imag(); - } else - vResult[ix][iy] = ::log (vLHS[ix][iy]); - } - } - - - return true; -} - -bool -ImageFile::exp (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - if (isComplex() && ! result.isComplex()) - result.convertRealToComplex(); - - ImageFileArrayConst vLHS = getArray(); - ImageFileArrayConst vLHSImag = getImaginaryArray(); - ImageFileArray vResult = result.getArray(); - ImageFileArray vResultImag = result.getImaginaryArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - if (result.isComplex()) { - double dImag = 0; - if (isComplex()) - dImag = vLHSImag[ix][iy]; - std::complex cLHS (vLHS[ix][iy], dImag); - std::complex cResult = std::exp (cLHS); - vResult[ix][iy] = cResult.real(); - vResultImag[ix][iy] = cResult.imag(); - } else - vResult[ix][iy] = ::exp (vLHS[ix][iy]); - } - } - - - return true; -} - -bool -ImageFile::scaleImage (ImageFile& result) const -{ - unsigned int nx = m_nx; - unsigned int ny = m_ny; - unsigned int newNX = result.nx(); - unsigned int newNY = result.ny(); - - double dXScale = static_cast(newNX) / static_cast(nx); - double dYScale = static_cast(newNY) / static_cast(ny); - - if (isComplex() && ! result.isComplex()) - result.convertRealToComplex(); - - ImageFileArrayConst vReal = getArray(); - ImageFileArrayConst vImag = getImaginaryArray(); - ImageFileArray vResult = result.getArray(); - ImageFileArray vResultImag = result.getImaginaryArray(); - - for (unsigned int ix = 0; ix < newNX; ix++) { - for (unsigned int iy = 0; iy < newNY; iy++) { - double dXPos = ix / dXScale; - double dYPos = iy / dYScale; - unsigned int scaleNX = static_cast (dXPos); - unsigned int scaleNY = static_cast (dYPos); - double dXFrac = dXPos - scaleNX; - double dYFrac = dYPos - scaleNY; - if (scaleNX >= nx - 1 || scaleNY >= ny - 1) { - vResult[ix][iy] = vReal[scaleNX][scaleNY]; - if (result.isComplex()) { - if (isComplex()) - vResultImag[ix][iy] = vImag[scaleNX][scaleNY]; - else - vResultImag[ix][iy] = 0; - } - } else { - vResult[ix][iy] = vReal[scaleNX][scaleNY] + - dXFrac * (vReal[scaleNX+1][scaleNY] - vReal[scaleNX][scaleNY]) + - dYFrac * (vReal[scaleNX][scaleNY+1] - vReal[scaleNX][scaleNY]); - if (result.isComplex()) { - if (isComplex()) - vResultImag[ix][iy] = vImag[scaleNX][scaleNY] + - dXFrac * (vImag[scaleNX+1][scaleNY] - vImag[scaleNX][scaleNY]) + - dYFrac * (vImag[scaleNX][scaleNY+1] - vImag[scaleNX][scaleNY]); - else - vResultImag[ix][iy] = 0; - } - } - } - } - - return true; -} - -#ifdef HAVE_FFTW -bool -ImageFile::fft (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - if (result.dataType() == Array2dFile::DATA_TYPE_REAL) { - if (! result.convertRealToComplex ()) - return false; - } - - fftw_complex* in = new fftw_complex [m_nx * m_ny]; - - ImageFileArrayConst vReal = getArray(); - ImageFileArrayConst vImag = getImaginaryArray(); - - unsigned int ix, iy; - unsigned int iArray = 0; - for (ix = 0; ix < m_nx; ix++) - for (iy = 0; iy < m_ny; iy++) { - in[iArray].re = vReal[ix][iy]; - if (isComplex()) - in[iArray].im = vImag[ix][iy]; - else - in[iArray].im = 0; - iArray++; - } - - fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE); - - fftwnd_one (plan, in, NULL); - - ImageFileArray vRealResult = result.getArray(); - ImageFileArray vImagResult = result.getImaginaryArray(); - iArray = 0; - unsigned int iScale = m_nx * m_ny; - for (ix = 0; ix < m_nx; ix++) - for (iy = 0; iy < m_ny; iy++) { - vRealResult[ix][iy] = in[iArray].re / iScale; - vImagResult[ix][iy] = in[iArray].im / iScale; - iArray++; - } - - fftwnd_destroy_plan (plan); - delete in; - - - Fourier::shuffleFourierToNaturalOrder (result); - - return true; -} - - -bool -ImageFile::ifft (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - if (result.dataType() == Array2dFile::DATA_TYPE_REAL) { - if (! result.convertRealToComplex ()) - return false; - } - - ImageFileArrayConst vReal = getArray(); - ImageFileArrayConst vImag = getImaginaryArray(); - ImageFileArray vRealResult = result.getArray(); - ImageFileArray vImagResult = result.getImaginaryArray(); - unsigned int ix, iy; - for (ix = 0; ix < m_nx; ix++) - for (iy = 0; iy < m_ny; iy++) { - vRealResult[ix][iy] = vReal[ix][iy]; - if (isComplex()) - vImagResult[ix][iy] = vImag[ix][iy]; - else - vImagResult[ix][iy] = 0; - } - - Fourier::shuffleNaturalToFourierOrder (result); - - fftw_complex* in = new fftw_complex [m_nx * m_ny]; - - unsigned int iArray = 0; - for (ix = 0; ix < m_nx; ix++) - for (iy = 0; iy < m_ny; iy++) { - in[iArray].re = vRealResult[ix][iy]; - in[iArray].im = vImagResult[ix][iy]; - iArray++; - } - - fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_BACKWARD, FFTW_IN_PLACE); - - fftwnd_one (plan, in, NULL); - - iArray = 0; - for (ix = 0; ix < m_nx; ix++) - for (iy = 0; iy < m_ny; iy++) { - vRealResult[ix][iy] = in[iArray].re; - vImagResult[ix][iy] = in[iArray].im; - iArray++; - } - - fftwnd_destroy_plan (plan); - - delete in; - - return true; -} -#endif // HAVE_FFTW - - - -bool -ImageFile::fourier (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - if (! result.isComplex()) - if (! result.convertRealToComplex ()) - return false; - - ImageFileArrayConst vLHS = getArray(); - ImageFileArrayConst vLHSImag = getImaginaryArray(); - ImageFileArray vRealResult = result.getArray(); - ImageFileArray vImagResult = result.getImaginaryArray(); - - unsigned int ix, iy; - - // alloc output matrix - CTSimComplex** complexOut = new CTSimComplex* [m_nx]; - for (ix = 0; ix < m_nx; ix++) - complexOut[ix] = new CTSimComplex [m_ny]; - - // fourier each x column - CTSimComplex* pY = new CTSimComplex [m_ny]; - for (ix = 0; ix < m_nx; ix++) { - for (iy = 0; iy < m_ny; iy++) { - double dImag = 0; - if (isComplex()) - dImag = vLHSImag[ix][iy]; - pY[iy] = std::complex(vLHS[ix][iy], dImag); - } - ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD); - } - delete [] pY; - - // fourier each y row - CTSimComplex* pX = new CTSimComplex [m_nx]; - CTSimComplex* complexOutRow = new CTSimComplex [m_nx]; - for (iy = 0; iy < m_ny; iy++) { - for (ix = 0; ix < m_nx; ix++) - pX[ix] = complexOut[ix][iy]; - ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD); - for (ix = 0; ix < m_nx; ix++) - complexOut[ix][iy] = complexOutRow[ix]; - } - delete [] pX; - delete [] complexOutRow; - - for (ix = 0; ix < m_nx; ix++) - for (iy = 0; iy < m_ny; iy++) { - vRealResult[ix][iy] = complexOut[ix][iy].real(); - vImagResult[ix][iy] = complexOut[ix][iy].imag(); - } - - Fourier::shuffleFourierToNaturalOrder (result); - - // delete complexOut matrix - for (ix = 0; ix < m_nx; ix++) - delete [] complexOut[ix]; - delete [] complexOut; - - return true; -} - -bool -ImageFile::inverseFourier (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - if (result.dataType() == Array2dFile::DATA_TYPE_REAL) { - if (! result.convertRealToComplex ()) - return false; - } - - ImageFileArrayConst vLHSReal = getArray(); - ImageFileArrayConst vLHSImag = getImaginaryArray(); - ImageFileArray vRealResult = result.getArray(); - ImageFileArray vImagResult = result.getImaginaryArray(); - - unsigned int ix, iy; - // alloc 2d complex output matrix - CTSimComplex** complexOut = new CTSimComplex* [m_nx]; - for (ix = 0; ix < m_nx; ix++) - complexOut[ix] = new CTSimComplex [m_ny]; - - // put input image into result - for (ix = 0; ix < m_nx; ix++) - for (iy = 0; iy < m_ny; iy++) { - vRealResult[ix][iy] = vLHSReal[ix][iy]; - if (isComplex()) - vImagResult[ix][iy] = vLHSImag[ix][iy]; - else - vImagResult[ix][iy] = 0; - } - - Fourier::shuffleNaturalToFourierOrder (result); - - // ifourier each x column - CTSimComplex* pCol = new CTSimComplex [m_ny]; - for (ix = 0; ix < m_nx; ix++) { - for (iy = 0; iy < m_ny; iy++) { - pCol[iy] = std::complex (vRealResult[ix][iy], vImagResult[ix][iy]); - } - ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD); - } - delete [] pCol; - - // ifourier each y row - CTSimComplex* complexInRow = new CTSimComplex [m_nx]; - CTSimComplex* complexOutRow = new CTSimComplex [m_nx]; - for (iy = 0; iy < m_ny; iy++) { - for (ix = 0; ix < m_nx; ix++) - complexInRow[ix] = complexOut[ix][iy]; - ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD); - for (ix = 0; ix < m_nx; ix++) - complexOut[ix][iy] = complexOutRow[ix]; - } - delete [] complexInRow; - delete [] complexOutRow; - - for (ix = 0; ix < m_nx; ix++) - for (iy = 0; iy < m_ny; iy++) { - vRealResult[ix][iy] = complexOut[ix][iy].real(); - vImagResult[ix][iy] = complexOut[ix][iy].imag(); - } - - // delete complexOut matrix - for (ix = 0; ix < m_nx; ix++) - delete [] complexOut[ix]; - delete [] complexOut; - - return true; -} - - -bool -ImageFile::magnitude (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - ImageFileArray vReal = getArray(); - ImageFileArray vImag = getImaginaryArray(); - ImageFileArray vRealResult = result.getArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) - for (unsigned int iy = 0; iy < m_ny; iy++) { - if (isComplex()) - vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]); - else - vRealResult[ix][iy] = vReal[ix][iy]; - } - - if (result.isComplex()) - result.convertComplexToReal(); - - return true; -} - -bool -ImageFile::phase (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - ImageFileArray vReal = getArray(); - ImageFileArray vImag = getImaginaryArray(); - ImageFileArray vRealResult = result.getArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) - for (unsigned int iy = 0; iy < m_ny; iy++) { - if (isComplex()) - vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]); - else - vRealResult[ix][iy] = 0; - } - - if (result.isComplex()) - result.convertComplexToReal(); - - return true; -} - -bool -ImageFile::square (ImageFile& result) const -{ - if (m_nx != result.nx() || m_ny != result.ny()) { - sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); - return false; - } - - if (isComplex() && ! result.isComplex()) - result.convertRealToComplex(); - - ImageFileArrayConst vLHS = getArray(); - ImageFileArrayConst vLHSImag = getImaginaryArray(); - ImageFileArray vResult = result.getArray(); - ImageFileArray vResultImag = result.getImaginaryArray(); - - for (unsigned int ix = 0; ix < m_nx; ix++) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - if (result.isComplex()) { - double dImag = 0; - if (isComplex()) - dImag = vLHSImag[ix][iy]; - std::complex cLHS (vLHS[ix][iy], dImag); - std::complex cResult = cLHS * cLHS; - vResult[ix][iy] = cResult.real(); - vResultImag[ix][iy] = cResult.imag(); - } else - vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy]; - } - } - - - return true; -} - -int -ImageFile::convertFormatNameToID (const char* const formatName) -{ - int formatID = FORMAT_INVALID; - - for (int i = 0; i < s_iFormatCount; i++) - if (strcasecmp (formatName, s_aszFormatName[i]) == 0) { - formatID = i; - break; - } - - return (formatID); -} - -const char* -ImageFile::convertFormatIDToName (int formatID) -{ - static const char *formatName = ""; - - if (formatID >= 0 && formatID < s_iFormatCount) - return (s_aszFormatName[formatID]); - - return (formatName); -} - -const char* -ImageFile::convertFormatIDToTitle (const int formatID) -{ - static const char *formatTitle = ""; - - if (formatID >= 0 && formatID < s_iFormatCount) - return (s_aszFormatTitle[formatID]); - - return (formatTitle); -} - -bool -ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax) -{ - int iFormatID = convertFormatNameToID (pszFormat); - if (iFormatID == FORMAT_INVALID) { - sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat); - return false; - } - - if (iFormatID == FORMAT_PGM) - return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax); - else if (iFormatID == FORMAT_PGMASCII) - return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax); - else if (iFormatID == FORMAT_PNG) - return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax); - else if (iFormatID == FORMAT_PNG16) - return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax); - - sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat); - return false; -} - +bool +ImageFile::convertRealToComplex () +{ + if (dataType() != Array2dFile::DATA_TYPE_REAL) + return false; + + if (! reallocRealToComplex()) + return false; + + ImageFileArray vImag = getImaginaryArray(); + for (unsigned int ix = 0; ix < m_nx; ix++) { + ImageFileColumn vCol = vImag[ix]; + for (unsigned int iy = 0; iy < m_ny; iy++) + *vCol++ = 0; + } + + return true; +} + +bool +ImageFile::convertComplexToReal () +{ + if (dataType() != Array2dFile::DATA_TYPE_COMPLEX) + return false; + + ImageFileArray vReal = getArray(); + ImageFileArray vImag = getImaginaryArray(); + for (unsigned int ix = 0; ix < m_nx; ix++) { + ImageFileColumn vRealCol = vReal[ix]; + ImageFileColumn vImagCol = vImag[ix]; + for (unsigned int iy = 0; iy < m_ny; iy++) { + CTSimComplex c (*vRealCol, *vImagCol); + *vRealCol++ = std::abs (c); + vImagCol++; + } + } + + return reallocComplexToReal(); +} + +bool +ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const +{ + if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]"); + return false; + } + + if (isComplex() || rRHS.isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArrayConst vRHS = rRHS.getArray(); + ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); + ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy]; + if (result.isComplex()) { + vResultImag[ix][iy] = 0; + if (isComplex()) + vResultImag[ix][iy] += vLHSImag[ix][iy]; + if (rRHS.isComplex()) + vResultImag[ix][iy] -= vRHSImag[ix][iy]; + } + } + } + + return true; +} + +bool +ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const +{ + if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]"); + return false; + } + + if (isComplex() || rRHS.isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArrayConst vRHS = rRHS.getArray(); + ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); + ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy]; + if (result.isComplex()) { + vResultImag[ix][iy] = 0; + if (isComplex()) + vResultImag[ix][iy] += vLHSImag[ix][iy]; + if (rRHS.isComplex()) + vResultImag[ix][iy] += vRHSImag[ix][iy]; + } + } + } + + return true; +} + +bool +ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const +{ + if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]"); + return false; + } + + if (isComplex() || rRHS.isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArrayConst vRHS = rRHS.getArray(); + ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); + ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (result.isComplex()) { + double dImag = 0; + if (isComplex()) + dImag = vLHSImag[ix][iy]; + std::complex cLHS (vLHS[ix][iy], dImag); + dImag = 0; + if (rRHS.isComplex()) + dImag = vRHSImag[ix][iy]; + std::complex cRHS (vRHS[ix][iy], dImag); + std::complex cResult = cLHS * cRHS; + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else + vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy]; + } + } + + + return true; +} + +bool +ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const +{ + if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]"); + return false; + } + + if (isComplex() || rRHS.isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArrayConst vRHS = rRHS.getArray(); + ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray(); + ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (result.isComplex()) { + double dImag = 0; + if (isComplex()) + dImag = vLHSImag[ix][iy]; + std::complex cLHS (vLHS[ix][iy], dImag); + dImag = 0; + if (rRHS.isComplex()) + dImag = vRHSImag[ix][iy]; + std::complex cRHS (vRHS[ix][iy], dImag); + std::complex cResult = cLHS / cRHS; + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else { + if (vRHS != 0) + vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy]; + else + vResult[ix][iy] = 0; + } + } + } + + return true; +} + + +bool +ImageFile::invertPixelValues (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + + ImageFileArrayConst vLHS = getArray(); + ImageFileArray vResult = result.getArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) { + ImageFileColumnConst in = vLHS[ix]; + ImageFileColumn out = vResult[ix]; + for (unsigned int iy = 0; iy < m_ny; iy++) + *out++ = - *in++; + } + + return true; +} + +bool +ImageFile::sqrt (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + + bool bComplexOutput = result.isComplex(); + ImageFileArrayConst vLHS = getArray(); + if (! bComplexOutput) // check if should convert to complex output + for (unsigned int ix = 0; ix < m_nx; ix++) + for (unsigned int iy = 0; iy < m_ny; iy++) + if (! bComplexOutput && vLHS[ix][iy] < 0) { + result.convertRealToComplex(); + bComplexOutput = true; + break; + } + + ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (result.isComplex()) { + double dImag = 0; + if (isComplex()) + dImag = vLHSImag[ix][iy]; + std::complex cLHS (vLHS[ix][iy], dImag); + std::complex cResult = std::sqrt(cLHS); + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else + vResult[ix][iy] = ::sqrt (vLHS[ix][iy]); + } + } + + + return true; +} + +bool +ImageFile::log (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (result.isComplex()) { + double dImag = 0; + if (isComplex()) + dImag = vLHSImag[ix][iy]; + std::complex cLHS (vLHS[ix][iy], dImag); + std::complex cResult = std::log (cLHS); + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else + vResult[ix][iy] = ::log (vLHS[ix][iy]); + } + } + + + return true; +} + +bool +ImageFile::exp (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (result.isComplex()) { + double dImag = 0; + if (isComplex()) + dImag = vLHSImag[ix][iy]; + std::complex cLHS (vLHS[ix][iy], dImag); + std::complex cResult = std::exp (cLHS); + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else + vResult[ix][iy] = ::exp (vLHS[ix][iy]); + } + } + + + return true; +} + +bool +ImageFile::scaleImage (ImageFile& result) const +{ + unsigned int nx = m_nx; + unsigned int ny = m_ny; + unsigned int newNX = result.nx(); + unsigned int newNY = result.ny(); + + double dXScale = static_cast(newNX) / static_cast(nx); + double dYScale = static_cast(newNY) / static_cast(ny); + + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + + ImageFileArrayConst vReal = getArray(); + ImageFileArrayConst vImag = getImaginaryArray(); + ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); + + for (unsigned int ix = 0; ix < newNX; ix++) { + for (unsigned int iy = 0; iy < newNY; iy++) { + double dXPos = ix / dXScale; + double dYPos = iy / dYScale; + unsigned int scaleNX = static_cast (dXPos); + unsigned int scaleNY = static_cast (dYPos); + double dXFrac = dXPos - scaleNX; + double dYFrac = dYPos - scaleNY; + if (scaleNX >= nx - 1 || scaleNY >= ny - 1) { + vResult[ix][iy] = vReal[scaleNX][scaleNY]; + if (result.isComplex()) { + if (isComplex()) + vResultImag[ix][iy] = vImag[scaleNX][scaleNY]; + else + vResultImag[ix][iy] = 0; + } + } else { + vResult[ix][iy] = vReal[scaleNX][scaleNY] + + dXFrac * (vReal[scaleNX+1][scaleNY] - vReal[scaleNX][scaleNY]) + + dYFrac * (vReal[scaleNX][scaleNY+1] - vReal[scaleNX][scaleNY]); + if (result.isComplex()) { + if (isComplex()) + vResultImag[ix][iy] = vImag[scaleNX][scaleNY] + + dXFrac * (vImag[scaleNX+1][scaleNY] - vImag[scaleNX][scaleNY]) + + dYFrac * (vImag[scaleNX][scaleNY+1] - vImag[scaleNX][scaleNY]); + else + vResultImag[ix][iy] = 0; + } + } + } + } + + return true; +} + +#ifdef HAVE_FFTW +bool +ImageFile::fft (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + if (result.dataType() == Array2dFile::DATA_TYPE_REAL) { + if (! result.convertRealToComplex ()) + return false; + } + + fftw_complex* in = new fftw_complex [m_nx * m_ny]; + + ImageFileArrayConst vReal = getArray(); + ImageFileArrayConst vImag = getImaginaryArray(); + + unsigned int ix, iy; + unsigned int iArray = 0; + for (ix = 0; ix < m_nx; ix++) + for (iy = 0; iy < m_ny; iy++) { + in[iArray].re = vReal[ix][iy]; + if (isComplex()) + in[iArray].im = vImag[ix][iy]; + else + in[iArray].im = 0; + iArray++; + } + + fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE); + + fftwnd_one (plan, in, NULL); + + ImageFileArray vRealResult = result.getArray(); + ImageFileArray vImagResult = result.getImaginaryArray(); + iArray = 0; + unsigned int iScale = m_nx * m_ny; + for (ix = 0; ix < m_nx; ix++) + for (iy = 0; iy < m_ny; iy++) { + vRealResult[ix][iy] = in[iArray].re / iScale; + vImagResult[ix][iy] = in[iArray].im / iScale; + iArray++; + } + + fftwnd_destroy_plan (plan); + delete in; + + + Fourier::shuffleFourierToNaturalOrder (result); + + return true; +} + + +bool +ImageFile::ifft (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + if (result.dataType() == Array2dFile::DATA_TYPE_REAL) { + if (! result.convertRealToComplex ()) + return false; + } + + ImageFileArrayConst vReal = getArray(); + ImageFileArrayConst vImag = getImaginaryArray(); + ImageFileArray vRealResult = result.getArray(); + ImageFileArray vImagResult = result.getImaginaryArray(); + unsigned int ix, iy; + for (ix = 0; ix < m_nx; ix++) + for (iy = 0; iy < m_ny; iy++) { + vRealResult[ix][iy] = vReal[ix][iy]; + if (isComplex()) + vImagResult[ix][iy] = vImag[ix][iy]; + else + vImagResult[ix][iy] = 0; + } + + Fourier::shuffleNaturalToFourierOrder (result); + + fftw_complex* in = new fftw_complex [m_nx * m_ny]; + + unsigned int iArray = 0; + for (ix = 0; ix < m_nx; ix++) + for (iy = 0; iy < m_ny; iy++) { + in[iArray].re = vRealResult[ix][iy]; + in[iArray].im = vImagResult[ix][iy]; + iArray++; + } + + fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_BACKWARD, FFTW_IN_PLACE); + + fftwnd_one (plan, in, NULL); + + iArray = 0; + for (ix = 0; ix < m_nx; ix++) + for (iy = 0; iy < m_ny; iy++) { + vRealResult[ix][iy] = in[iArray].re; + vImagResult[ix][iy] = in[iArray].im; + iArray++; + } + + fftwnd_destroy_plan (plan); + + delete in; + + return true; +} +#endif // HAVE_FFTW + + + +bool +ImageFile::fourier (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + if (! result.isComplex()) + if (! result.convertRealToComplex ()) + return false; + + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArray vRealResult = result.getArray(); + ImageFileArray vImagResult = result.getImaginaryArray(); + + unsigned int ix, iy; + + // alloc output matrix + CTSimComplex** complexOut = new CTSimComplex* [m_nx]; + for (ix = 0; ix < m_nx; ix++) + complexOut[ix] = new CTSimComplex [m_ny]; + + // fourier each x column + CTSimComplex* pY = new CTSimComplex [m_ny]; + for (ix = 0; ix < m_nx; ix++) { + for (iy = 0; iy < m_ny; iy++) { + double dImag = 0; + if (isComplex()) + dImag = vLHSImag[ix][iy]; + pY[iy] = std::complex(vLHS[ix][iy], dImag); + } + ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD); + } + delete [] pY; + + // fourier each y row + CTSimComplex* pX = new CTSimComplex [m_nx]; + CTSimComplex* complexOutRow = new CTSimComplex [m_nx]; + for (iy = 0; iy < m_ny; iy++) { + for (ix = 0; ix < m_nx; ix++) + pX[ix] = complexOut[ix][iy]; + ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD); + for (ix = 0; ix < m_nx; ix++) + complexOut[ix][iy] = complexOutRow[ix]; + } + delete [] pX; + delete [] complexOutRow; + + for (ix = 0; ix < m_nx; ix++) + for (iy = 0; iy < m_ny; iy++) { + vRealResult[ix][iy] = complexOut[ix][iy].real(); + vImagResult[ix][iy] = complexOut[ix][iy].imag(); + } + + Fourier::shuffleFourierToNaturalOrder (result); + + // delete complexOut matrix + for (ix = 0; ix < m_nx; ix++) + delete [] complexOut[ix]; + delete [] complexOut; + + return true; +} + +bool +ImageFile::inverseFourier (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + if (result.dataType() == Array2dFile::DATA_TYPE_REAL) { + if (! result.convertRealToComplex ()) + return false; + } + + ImageFileArrayConst vLHSReal = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArray vRealResult = result.getArray(); + ImageFileArray vImagResult = result.getImaginaryArray(); + + unsigned int ix, iy; + // alloc 2d complex output matrix + CTSimComplex** complexOut = new CTSimComplex* [m_nx]; + for (ix = 0; ix < m_nx; ix++) + complexOut[ix] = new CTSimComplex [m_ny]; + + // put input image into result + for (ix = 0; ix < m_nx; ix++) + for (iy = 0; iy < m_ny; iy++) { + vRealResult[ix][iy] = vLHSReal[ix][iy]; + if (isComplex()) + vImagResult[ix][iy] = vLHSImag[ix][iy]; + else + vImagResult[ix][iy] = 0; + } + + Fourier::shuffleNaturalToFourierOrder (result); + + // ifourier each x column + CTSimComplex* pCol = new CTSimComplex [m_ny]; + for (ix = 0; ix < m_nx; ix++) { + for (iy = 0; iy < m_ny; iy++) { + pCol[iy] = std::complex (vRealResult[ix][iy], vImagResult[ix][iy]); + } + ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD); + } + delete [] pCol; + + // ifourier each y row + CTSimComplex* complexInRow = new CTSimComplex [m_nx]; + CTSimComplex* complexOutRow = new CTSimComplex [m_nx]; + for (iy = 0; iy < m_ny; iy++) { + for (ix = 0; ix < m_nx; ix++) + complexInRow[ix] = complexOut[ix][iy]; + ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD); + for (ix = 0; ix < m_nx; ix++) + complexOut[ix][iy] = complexOutRow[ix]; + } + delete [] complexInRow; + delete [] complexOutRow; + + for (ix = 0; ix < m_nx; ix++) + for (iy = 0; iy < m_ny; iy++) { + vRealResult[ix][iy] = complexOut[ix][iy].real(); + vImagResult[ix][iy] = complexOut[ix][iy].imag(); + } + + // delete complexOut matrix + for (ix = 0; ix < m_nx; ix++) + delete [] complexOut[ix]; + delete [] complexOut; + + return true; +} + + +bool +ImageFile::magnitude (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + ImageFileArray vReal = getArray(); + ImageFileArray vImag = getImaginaryArray(); + ImageFileArray vRealResult = result.getArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (isComplex()) + vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]); + else + vRealResult[ix][iy] = vReal[ix][iy]; + } + + if (result.isComplex()) + result.convertComplexToReal(); + + return true; +} + +bool +ImageFile::phase (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + ImageFileArray vReal = getArray(); + ImageFileArray vImag = getImaginaryArray(); + ImageFileArray vRealResult = result.getArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (isComplex()) + vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]); + else + vRealResult[ix][iy] = 0; + } + + if (result.isComplex()) + result.convertComplexToReal(); + + return true; +} + +bool +ImageFile::square (ImageFile& result) const +{ + if (m_nx != result.nx() || m_ny != result.ny()) { + sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]"); + return false; + } + + if (isComplex() && ! result.isComplex()) + result.convertRealToComplex(); + + ImageFileArrayConst vLHS = getArray(); + ImageFileArrayConst vLHSImag = getImaginaryArray(); + ImageFileArray vResult = result.getArray(); + ImageFileArray vResultImag = result.getImaginaryArray(); + + for (unsigned int ix = 0; ix < m_nx; ix++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + if (result.isComplex()) { + double dImag = 0; + if (isComplex()) + dImag = vLHSImag[ix][iy]; + std::complex cLHS (vLHS[ix][iy], dImag); + std::complex cResult = cLHS * cLHS; + vResult[ix][iy] = cResult.real(); + vResultImag[ix][iy] = cResult.imag(); + } else + vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy]; + } + } + + + return true; +} + +int +ImageFile::convertFormatNameToID (const char* const formatName) +{ + int formatID = FORMAT_INVALID; + + for (int i = 0; i < s_iFormatCount; i++) + if (strcasecmp (formatName, s_aszFormatName[i]) == 0) { + formatID = i; + break; + } + + return (formatID); +} + +const char* +ImageFile::convertFormatIDToName (int formatID) +{ + static const char *formatName = ""; + + if (formatID >= 0 && formatID < s_iFormatCount) + return (s_aszFormatName[formatID]); + + return (formatName); +} + +const char* +ImageFile::convertFormatIDToTitle (const int formatID) +{ + static const char *formatTitle = ""; + + if (formatID >= 0 && formatID < s_iFormatCount) + return (s_aszFormatTitle[formatID]); + + return (formatTitle); +} + +bool +ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax) +{ + int iFormatID = convertFormatNameToID (pszFormat); + if (iFormatID == FORMAT_INVALID) { + sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat); + return false; + } + + if (iFormatID == FORMAT_PGM) + return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax); + else if (iFormatID == FORMAT_PGMASCII) + return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax); + else if (iFormatID == FORMAT_PNG) + return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax); + else if (iFormatID == FORMAT_PNG16) + return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax); + + sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat); + return false; +} + bool ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax) { @@ -1144,10 +1144,10 @@ ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, dou fputc( rowp[ic], fp ); } } - + delete rowp; - fclose(fp); - + fclose(fp); + return true; } @@ -1183,10 +1183,10 @@ ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell fprintf(fp, "\n"); } } - + delete rowp; - fclose(fp); - + fclose(fp); + return true; } @@ -1202,7 +1202,7 @@ ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, i unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)]; - FILE *fp = fopen (outfile, "wb"); + FILE *fp = fopen (outfile, "wb"); if (! fp) return false; @@ -1253,10 +1253,10 @@ ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, i png_write_end (png_ptr, info_ptr); png_destroy_write_struct (&png_ptr, &info_ptr); - delete rowp; + delete rowp; + + fclose(fp); - fclose(fp); - return true; } #endif @@ -1295,17 +1295,17 @@ ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, dou } } - FILE *out; + FILE *out; if ((out = fopen (outfile,"w")) == NULL) { sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile); return false; } gdImageGif(gif,out); fclose(out); - gdImageDestroy(gif); - - delete rowp; - + gdImageDestroy(gif); + + delete rowp; + return true; } #endif