-\r
-bool\r
-ImageFile::convertRealToComplex ()\r
-{\r
- if (dataType() != Array2dFile::DATA_TYPE_REAL)\r
- return false;\r
- \r
- if (! reallocRealToComplex())\r
- return false;\r
- \r
- ImageFileArray vImag = getImaginaryArray();\r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumn vCol = vImag[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++)\r
- *vCol++ = 0;\r
- }\r
- \r
- return true;\r
-}\r
-\r
-bool\r
-ImageFile::convertComplexToReal ()\r
-{\r
- if (dataType() != Array2dFile::DATA_TYPE_COMPLEX)\r
- return false;\r
- \r
- ImageFileArray vReal = getArray();\r
- ImageFileArray vImag = getImaginaryArray();\r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumn vRealCol = vReal[ix];\r
- ImageFileColumn vImagCol = vImag[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++) {\r
- CTSimComplex c (*vRealCol, *vImagCol);\r
- *vRealCol++ = std::abs (c);\r
- vImagCol++;\r
- }\r
- }\r
- \r
- return reallocComplexToReal();\r
-}\r
-\r
-bool\r
-ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const\r
-{\r
- if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHS = getArray();\r
- ImageFileArrayConst vRHS = rRHS.getArray();\r
- ImageFileArray vResult = result.getArray();\r
- \r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumnConst in1 = vLHS[ix];\r
- ImageFileColumnConst in2 = vRHS[ix];\r
- ImageFileColumn out = vResult[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++)\r
- *out++ = *in1++ - *in2++;\r
- }\r
- \r
- return true;\r
-}\r
-
-bool\r
-ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const\r
-{\r
- if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHS = getArray();\r
- ImageFileArrayConst vRHS = rRHS.getArray();\r
- ImageFileArray vResult = result.getArray();\r
- \r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumnConst in1 = vLHS[ix];\r
- ImageFileColumnConst in2 = vRHS[ix];\r
- ImageFileColumn out = vResult[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++)\r
- *out++ = *in1++ + *in2++;\r
- }\r
- \r
- return true;\r
-}\r
-\r
-bool\r
-ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const\r
-{\r
- if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHS = getArray();\r
- ImageFileArrayConst vRHS = rRHS.getArray();\r
- ImageFileArray vResult = result.getArray();\r
- \r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumnConst in1 = vLHS[ix];\r
- ImageFileColumnConst in2 = vRHS[ix];\r
- ImageFileColumn out = vResult[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++)\r
- *out++ = *in1++ * *in2++;\r
- }\r
- \r
- return true;\r
-}\r
-\r
-bool\r
-ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const\r
-{\r
- if (m_nx != rRHS.nx() || m_ny != rRHS.ny() || m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHS = getArray();\r
- ImageFileArrayConst vRHS = rRHS.getArray();\r
- ImageFileArray vResult = result.getArray();\r
- \r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumnConst in1 = vLHS[ix];\r
- ImageFileColumnConst in2 = vRHS[ix];\r
- ImageFileColumn out = vResult[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++) {\r
- if (*in2 != 0.)\r
- *out++ = *in1++ / *in2++;\r
- else\r
- *out++ = 0;\r
- }\r
- }\r
- \r
- return true;\r
-}\r
-\r
-\r
-bool\r
-ImageFile::invertPixelValues (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHS = getArray();\r
- ImageFileArray vResult = result.getArray();\r
- \r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumnConst in = vLHS[ix];\r
- ImageFileColumn out = vResult[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++)\r
- *out++ = - *in++;\r
- }\r
- \r
- return true;\r
-}\r
-\r
-bool\r
-ImageFile::sqrt (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHS = getArray();\r
- ImageFileArray vResult = result.getArray();\r
- \r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumnConst in = vLHS[ix];\r
- ImageFileColumn out = vResult[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++)\r
- if (*in < 0)\r
- *out++ = -::sqrt(-*in++);\r
- else\r
- *out++ = ::sqrt(*in++);\r
- }\r
- \r
- return true;\r
-}\r
-\r
-bool\r
-ImageFile::log (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHS = getArray();\r
- ImageFileArray vResult = result.getArray();\r
- \r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumnConst in = vLHS[ix];\r
- ImageFileColumn out = vResult[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++)\r
- if (*in <= 0)\r
- *out++ = 0;\r
- else\r
- *out++ = ::log(*in++);\r
- }\r
- \r
- return true;\r
-}\r
-\r
-bool\r
-ImageFile::exp (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHS = getArray();\r
- ImageFileArray vResult = result.getArray();\r
- \r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumnConst in = vLHS[ix];\r
- ImageFileColumn out = vResult[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++)\r
- *out++ = ::exp (*in++);\r
- }\r
- \r
- return true;\r
-}\r
-\r
-namespace ProcessImage {\r
-\r
-void\r
-shuffleFourierToNaturalOrder (ImageFile& im)\r
-{\r
- ImageFileArray vReal = im.getArray();\r
- ImageFileArray vImag = im.getImaginaryArray();\r
- unsigned int ix, iy;\r
- unsigned int nx = im.nx();\r
- unsigned int ny = im.ny();\r
-\r
- // shuffle each column\r
- for (ix = 0; ix < nx; ix++) {\r
- ProcessSignal::shuffleFourierToNaturalOrder (vReal[ix], ny);\r
- if (im.isComplex())\r
- ProcessSignal::shuffleFourierToNaturalOrder (vImag[ix], ny);\r
- }\r
-\r
- // shuffle each row\r
- float* pRow = new float [nx];\r
- for (iy = 0; iy < ny; iy++) {\r
- for (ix = 0; ix < nx; ix++)\r
- pRow[ix] = vReal[ix][iy];\r
- ProcessSignal::shuffleFourierToNaturalOrder (pRow, nx);\r
- for (ix = 0; ix < nx; ix++)\r
- vReal[ix][iy] = pRow[ix];\r
- if (im.isComplex()) {\r
- for (ix = 0; ix < nx; ix++)\r
- pRow[ix] = vImag[ix][iy];\r
- ProcessSignal::shuffleFourierToNaturalOrder (pRow, nx);;\r
- for (ix = 0; ix < nx; ix++)\r
- vImag[ix][iy] = pRow[ix];\r
- }\r
- }\r
- delete pRow;\r
-}\r
- \r
-void\r
-shuffleNaturalToFourierOrder (ImageFile& im)\r
-{\r
- ImageFileArray vReal = im.getArray();\r
- ImageFileArray vImag = im.getImaginaryArray();\r
- unsigned int ix, iy;\r
- unsigned int nx = im.nx();\r
- unsigned int ny = im.ny();\r
-\r
- // shuffle each x column\r
- for (ix = 0; ix < nx; ix++) {\r
- ProcessSignal::shuffleNaturalToFourierOrder (vReal[ix], ny);\r
- if (im.isComplex())\r
- ProcessSignal::shuffleNaturalToFourierOrder (vImag[ix], ny);\r
- }\r
-\r
- // shuffle each y row\r
- float* pRow = new float [nx];\r
- for (iy = 0; iy < ny; iy++) {\r
- for (ix = 0; ix < nx; ix++)\r
- pRow[ix] = vReal[ix][iy];\r
- ProcessSignal::shuffleNaturalToFourierOrder (pRow, nx);\r
- for (ix = 0; ix < nx; ix++)\r
- vReal[ix][iy] = pRow[ix];\r
- if (im.isComplex()) {\r
- for (ix = 0; ix < nx; ix++)\r
- pRow[ix] = vImag[ix][iy];\r
- ProcessSignal::shuffleNaturalToFourierOrder (pRow, nx);\r
- for (ix = 0; ix < nx; ix++)\r
- vImag[ix][iy] = pRow[ix];\r
- }\r
- }\r
- delete [] pRow;\r
-}\r
-\r
- \r
-}; // namespace ProcessIamge\r
-\r
-#ifdef HAVE_FFTW\r
-bool\r
-ImageFile::fft (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
-\r
- if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {\r
- if (! result.reallocRealToComplex ())\r
- return false;\r
- }\r
-\r
- fftw_complex* in = new fftw_complex [m_nx * m_ny];\r
-\r
- ImageFileArrayConst vReal = getArray();\r
- ImageFileArrayConst vImag = getImaginaryArray();\r
-\r
- unsigned int ix, iy;\r
- unsigned int iArray = 0;\r
- for (ix = 0; ix < m_nx; ix++)\r
- for (iy = 0; iy < m_ny; iy++) {\r
- in[iArray].re = vReal[ix][iy];\r
- if (isComplex())\r
- in[iArray].im = vImag[ix][iy];\r
- else\r
- in[iArray].im = 0;\r
- iArray++;\r
- }\r
-\r
- fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE);\r
-\r
- fftwnd_one (plan, in, NULL);\r
-\r
- ImageFileArray vRealResult = result.getArray();\r
- ImageFileArray vImagResult = result.getImaginaryArray();\r
- iArray = 0;\r
- unsigned int iScale = m_nx * m_ny;\r
- for (ix = 0; ix < m_nx; ix++)\r
- for (iy = 0; iy < m_ny; iy++) {\r
- vRealResult[ix][iy] = in[iArray].re / iScale;\r
- vImagResult[ix][iy] = in[iArray].im / iScale;\r
- iArray++;\r
- }\r
-\r
- fftwnd_destroy_plan (plan);\r
- delete in;\r
-\r
-\r
- ProcessImage::shuffleFourierToNaturalOrder (result);\r
-\r
- return true;\r
-}\r
-\r
-\r
-bool\r
-ImageFile::ifft (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
-\r
- if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {\r
- if (! result.reallocRealToComplex ())\r
- return false;\r
- }\r
-\r
- ImageFileArrayConst vReal = getArray();\r
- ImageFileArrayConst vImag = getImaginaryArray();\r
- ImageFileArray vRealResult = result.getArray();\r
- ImageFileArray vImagResult = result.getImaginaryArray();\r
- unsigned int ix, iy;\r
- for (ix = 0; ix < m_nx; ix++)\r
- for (iy = 0; iy < m_ny; iy++) {\r
- vRealResult[ix][iy] = vReal[ix][iy];\r
- if (isComplex()) \r
- vImagResult[ix][iy] = vImag[ix][iy];\r
- else\r
- vImagResult[ix][iy] = 0;\r
- }\r
-\r
- ProcessImage::shuffleNaturalToFourierOrder (result);\r
-\r
- fftw_complex* in = new fftw_complex [m_nx * m_ny];\r
-\r
- unsigned int iArray = 0;\r
- for (ix = 0; ix < m_nx; ix++)\r
- for (iy = 0; iy < m_ny; iy++) {\r
- in[iArray].re = vRealResult[ix][iy];\r
- in[iArray].im = vImagResult[ix][iy];\r
- iArray++;\r
- }\r
-\r
- fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_BACKWARD, FFTW_IN_PLACE);\r
-\r
- fftwnd_one (plan, in, NULL);\r
-\r
- iArray = 0;\r
- for (ix = 0; ix < m_nx; ix++)\r
- for (iy = 0; iy < m_ny; iy++) {\r
- vRealResult[ix][iy] = in[iArray].re;\r
- vImagResult[ix][iy] = in[iArray].im;\r
- iArray++;\r
- }\r
-\r
- fftwnd_destroy_plan (plan);\r
-\r
- delete in;\r
-\r
- return true;\r
-}\r
-#endif // HAVE_FFTW\r
-\r
-\r
- \r
-bool\r
-ImageFile::fourier (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
- \r
- if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {\r
- if (! result.reallocRealToComplex ())\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHS = getArray();\r
- ImageFileArrayConst vLHSImag = getImaginaryArray();\r
- ImageFileArray vRealResult = result.getArray();\r
- ImageFileArray vImagResult = result.getImaginaryArray();\r
- \r
- unsigned int ix, iy;\r
-\r
- // alloc output matrix\r
- CTSimComplex** complexOut = new CTSimComplex* [m_nx];\r
- for (ix = 0; ix < m_nx; ix++)\r
- complexOut[ix] = new CTSimComplex [m_ny];\r
- \r
- // fourier each x column\r
-#if 1\r
- CTSimComplex* pY = new CTSimComplex [m_ny];\r
- for (ix = 0; ix < m_nx; ix++) {\r
- for (iy = 0; iy < m_ny; iy++) {\r
- pY[iy].real (vLHS[ix][iy]);\r
- if (isComplex())\r
- pY[iy].imag (vLHSImag[ix][iy]);\r
- else\r
- pY[iy].imag (0);\r
- } \r
- ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);\r
- }\r
-#else\r
- double* pY = new double [m_ny];\r
- for (ix = 0; ix < m_nx; ix++) {\r
- for (iy = 0; iy < m_ny; iy++) {\r
- pY[iy] = vLHS[ix][iy];\r
- } \r
- ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny, ProcessSignal::FORWARD);\r
- }\r
-#endif\r
- delete [] pY;\r
- \r
- // fourier each y row\r
- CTSimComplex* pX = new CTSimComplex [m_nx];\r
- CTSimComplex* complexOutRow = new CTSimComplex [m_nx];\r
- for (iy = 0; iy < m_ny; iy++) {\r
- for (ix = 0; ix < m_nx; ix++)\r
- pX[ix] = complexOut[ix][iy];\r
- ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);\r
- for (ix = 0; ix < m_nx; ix++)\r
- complexOut[ix][iy] = complexOutRow[ix];\r
- }\r
- delete [] pX;\r
- \r
- // shuffle each column\r
- for (ix = 0; ix < m_nx; ix++)\r
- ProcessSignal::shuffleFourierToNaturalOrder (complexOut[ix], m_ny);\r
- \r
- // shuffle each row\r
- for (iy = 0; iy < m_ny; iy++) {\r
- for (ix = 0; ix < m_nx; ix++)\r
- complexOutRow[ix] = complexOut[ix][iy];\r
- ProcessSignal::shuffleFourierToNaturalOrder (complexOutRow, m_nx);;\r
- for (ix = 0; ix < m_nx; ix++)\r
- complexOut[ix][iy] = complexOutRow[ix];\r
- \r
- }\r
- delete [] complexOutRow;\r
- \r
- for (ix = 0; ix < m_nx; ix++)\r
- for (iy = 0; iy < m_ny; iy++) {\r
- vRealResult[ix][iy] = complexOut[ix][iy].real();\r
- vImagResult[ix][iy] = complexOut[ix][iy].imag();\r
- }\r
- \r
- for (ix = 0; ix < m_nx; ix++)\r
- delete [] complexOut[ix];\r
- delete [] complexOut;\r
- \r
- return true;\r
-}\r
-\r
-bool\r
-ImageFile::inverseFourier (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
- \r
- if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {\r
- if (! result.reallocRealToComplex ())\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHSReal = getArray();\r
- ImageFileArrayConst vLHSImag = getImaginaryArray();\r
- \r
- unsigned int ix, iy;\r
-\r
- // alloc 2d complex output matrix\r
- CTSimComplex** complexOut = new CTSimComplex* [m_nx];\r
- for (ix = 0; ix < m_nx; ix++)\r
- complexOut[ix] = new CTSimComplex [m_ny];\r
-\r
- // put input image into complexOut\r
- for (ix = 0; ix < m_nx; ix++)\r
- for (iy = 0; iy < m_ny; iy++) {\r
- complexOut[ix][iy].real (vLHSReal[ix][iy]);\r
- if (isComplex())\r
- complexOut[ix][iy].imag (vLHSImag[ix][iy]);\r
- else\r
- complexOut[ix][iy].imag (0);\r
- }\r
-\r
- // shuffle each x column\r
- for (ix = 0; ix < m_nx; ix++)\r
- ProcessSignal::shuffleNaturalToFourierOrder (complexOut[ix], m_ny);\r
-\r
- // shuffle each y row\r
- CTSimComplex* pComplexRow = new CTSimComplex [m_nx];\r
- for (iy = 0; iy < m_ny; iy++) {\r
- for (ix = 0; ix < m_nx; ix++)\r
- pComplexRow[ix] = complexOut[ix][iy];\r
- ProcessSignal::shuffleNaturalToFourierOrder (pComplexRow, m_nx);\r
- for (ix = 0; ix < m_nx; ix++)\r
- complexOut[ix][iy] = pComplexRow[ix];\r
- }\r
- delete [] pComplexRow;\r
-\r
- // ifourier each x column\r
- CTSimComplex* pCol = new CTSimComplex [m_ny];\r
- for (ix = 0; ix < m_nx; ix++) {\r
- for (iy = 0; iy < m_ny; iy++)\r
- pCol[iy] = complexOut[ix][iy];\r
- ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny, ProcessSignal::BACKWARD);\r
- }\r
- delete [] pCol;\r
-\r
- // ifourier each y row\r
- CTSimComplex* complexInRow = new CTSimComplex [m_nx];\r
- CTSimComplex* complexOutRow = new CTSimComplex [m_nx];\r
- for (iy = 0; iy < m_ny; iy++) {\r
- for (ix = 0; ix < m_nx; ix++)\r
- complexInRow[ix] = complexOut[ix][iy];\r
- ProcessSignal::finiteFourierTransform (complexInRow, complexOutRow, m_nx, ProcessSignal::BACKWARD);\r
- for (ix = 0; ix < m_nx; ix++)\r
- complexOut[ix][iy] = complexOutRow[ix];\r
- }\r
- delete [] complexInRow;\r
- delete [] complexOutRow;\r
-\r
- ImageFileArray vRealResult = result.getArray();\r
- ImageFileArray vImagResult = result.getImaginaryArray();\r
- for (ix = 0; ix < m_nx; ix++)\r
- for (iy = 0; iy < m_ny; iy++) {\r
- vRealResult[ix][iy] = complexOut[ix][iy].real();\r
- vImagResult[ix][iy] = complexOut[ix][iy].imag();\r
- }\r
- \r
- // delete complexOut matrix\r
- for (ix = 0; ix < m_nx; ix++)\r
- delete [] complexOut[ix];\r
- delete [] complexOut;\r
- \r
- return true;\r
-}\r
-\r
-\r
-bool\r
-ImageFile::magnitude (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
- \r
- ImageFileArray vReal = getArray();\r
- ImageFileArray vImag = getImaginaryArray();\r
- \r
- ImageFileArray vRealResult = result.getArray();\r
- for (unsigned int ix = 0; ix < m_nx; ix++)\r
- for (unsigned int iy = 0; iy < m_ny; iy++) {\r
- if (isComplex()) \r
- vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]);\r
- else\r
- vRealResult[ix][iy] = vReal[ix][iy];\r
- }\r
-\r
- if (result.isComplex())\r
- result.reallocComplexToReal();\r
- \r
- return true;\r
-}\r
-\r
-bool\r
-ImageFile::phase (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
- \r
- ImageFileArray vReal = getArray();\r
- ImageFileArray vImag = getImaginaryArray();\r
- \r
- ImageFileArray vRealResult = result.getArray();\r
- for (unsigned int ix = 0; ix < m_nx; ix++)\r
- for (unsigned int iy = 0; iy < m_ny; iy++) {\r
- if (isComplex())\r
- vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);\r
- else\r
- vRealResult[ix][iy] = 0;\r
- }\r
- \r
- if (result.isComplex())\r
- result.reallocComplexToReal();\r
- \r
- return true;\r
-}\r
-\r
-bool\r
-ImageFile::square (ImageFile& result) const\r
-{\r
- if (m_nx != result.nx() || m_ny != result.ny()) {\r
- sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
- return false;\r
- }\r
- \r
- ImageFileArrayConst vLHS = getArray();\r
- ImageFileArray vResult = result.getArray();\r
- \r
- for (unsigned int ix = 0; ix < m_nx; ix++) {\r
- ImageFileColumnConst in = vLHS[ix];\r
- ImageFileColumn out = vResult[ix];\r
- for (unsigned int iy = 0; iy < m_ny; iy++) {\r
- *out++ = *in * *in;\r
- in++;\r
- }\r
- }\r
- \r
- return true;\r
-}\r
-\r
-\r
-void
-ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
+
+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<double> cLHS (vLHS[ix][iy], dImag);
+ dImag = 0;
+ if (rRHS.isComplex())
+ dImag = vRHSImag[ix][iy];
+ std::complex<double> cRHS (vRHS[ix][iy], dImag);
+ std::complex<double> 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<double> cLHS (vLHS[ix][iy], dImag);
+ dImag = 0;
+ if (rRHS.isComplex())
+ dImag = vRHSImag[ix][iy];
+ std::complex<double> cRHS (vRHS[ix][iy], dImag);
+ std::complex<double> 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<double> cLHS (vLHS[ix][iy], dImag);
+ std::complex<double> 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<double> cLHS (vLHS[ix][iy], dImag);
+ std::complex<double> 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<double> cLHS (vLHS[ix][iy], dImag);
+ std::complex<double> 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<double>(newNX) / static_cast<double>(nx);
+ double dYScale = static_cast<double>(newNY) / static_cast<double>(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<unsigned int> (dXPos);
+ unsigned int scaleNY = static_cast<unsigned int> (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] = (1 - dXFrac) * (1 - dYFrac) * vReal[scaleNX][scaleNY] +
+ dXFrac * (1 - dYFrac) * vReal[scaleNX+1][scaleNY] +
+ dYFrac * (1 - dXFrac) * vReal[scaleNX][scaleNY+1] +
+ dXFrac * dYFrac * vReal[scaleNX+1][scaleNY+1];
+ if (result.isComplex()) {
+ if (isComplex())
+ vResultImag[ix][iy] = (1 - dXFrac) * (1 - dYFrac) * vImag[scaleNX][scaleNY] +
+ dXFrac * (1 - dYFrac) * vImag[scaleNX+1][scaleNY] +
+ dYFrac * (1 - dXFrac) * vImag[scaleNX][scaleNY+1] +
+ dXFrac * dYFrac * vImag[scaleNX+1][scaleNY+1];
+ 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;
+}
+
+bool
+ImageFile::fftRows (ImageFile& result) const
+{
+ if (m_nx != result.nx() || m_ny != result.ny()) {
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
+ return false;
+ }
+
+ if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
+ if (! result.convertRealToComplex ())
+ return false;
+ }
+
+ fftw_complex* in = new fftw_complex [m_nx];
+
+ ImageFileArrayConst vReal = getArray();
+ ImageFileArrayConst vImag = getImaginaryArray();
+
+ fftw_plan plan = fftw_create_plan (m_nx, FFTW_FORWARD, FFTW_IN_PLACE);
+ std::complex<double>* pcRow = new std::complex<double> [m_nx];
+
+ unsigned int ix, iy;
+ unsigned int iArray = 0;
+ for (iy = 0; iy < m_ny; iy++) {
+ for (ix = 0; ix < m_nx; ix++) {
+ in[ix].re = vReal[ix][iy];
+ if (isComplex())
+ in[ix].im = vImag[ix][iy];
+ else
+ in[ix].im = 0;
+ }
+
+ fftw_one (plan, in, NULL);
+
+ for (ix = 0; ix < m_nx; ix++)
+ pcRow[ix] = std::complex<double>(in[ix].re, in[ix].im);
+
+ Fourier::shuffleFourierToNaturalOrder (pcRow, m_nx);
+ for (ix = 0; ix < m_nx; ix++) {
+ vReal[ix][iy] = pcRow[ix].real();
+ vImag[ix][iy] = pcRow[ix].imag();
+ }
+ }
+ delete [] pcRow;
+
+ fftw_destroy_plan (plan);
+ delete in;
+
+ return true;
+}
+
+bool
+ImageFile::ifftRows (ImageFile& result) const
+{
+ if (m_nx != result.nx() || m_ny != result.ny()) {
+ sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::fftRows]");
+ return false;
+ }
+
+ if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {
+ if (! result.convertRealToComplex ())
+ return false;
+ }
+
+ fftw_complex* in = new fftw_complex [m_nx];
+
+ ImageFileArrayConst vReal = getArray();
+ ImageFileArrayConst vImag = getImaginaryArray();
+
+ fftw_plan plan = fftw_create_plan (m_nx, FFTW_BACKWARD, FFTW_IN_PLACE);
+ std::complex<double>* pcRow = new std::complex<double> [m_nx];
+
+ unsigned int ix, iy;
+ unsigned int iArray = 0;
+ for (iy = 0; iy < m_ny; iy++) {
+ for (ix = 0; ix < m_nx; ix++) {
+ double dImag = 0;
+ if (isComplex())
+ dImag = vImag[ix][iy];
+ pcRow[ix] = std::complex<double> (vReal[ix][iy], dImag);
+ }
+
+ Fourier::shuffleNaturalToFourierOrder (pcRow, m_nx);
+
+ for (ix = 0; ix < m_nx; ix++) {
+ in[ix].re = pcRow[ix].real();
+ in[ix].im = pcRow[ix].imag();
+ }
+
+ fftw_one (plan, in, NULL);
+
+ for (ix = 0; ix < m_nx; ix++) {
+ vReal[ix][iy] = in[ix].re;
+ vImag[ix][iy] = in[ix].im;
+ }
+ }
+ delete [] pcRow;
+
+ fftw_destroy_plan (plan);
+ delete in;
+
+ return true;
+}
+
+bool
+ImageFile::fftCols (ImageFile& result) const
+{
+ return false;
+}
+
+bool
+ImageFile::ifftCols (ImageFile& result) const
+{
+ return false;
+}
+
+#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<double>(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<double> (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<double> cLHS (vLHS[ix][iy], dImag);
+ std::complex<double> 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)