+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 | FFTW_ESTIMATE | FFTW_USE_WISDOM);
+
+ 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;
+ }
+
+ ImageFileArrayConst vReal = getArray();
+ ImageFileArrayConst vImag = getImaginaryArray();
+
+ fftw_plan plan = fftw_create_plan (m_nx, FFTW_FORWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
+
+ fftw_complex* in = new fftw_complex [m_nx];
+ std::complex<double>* pcRow = new std::complex<double> [m_nx];
+ for (unsigned int iy = 0; iy < m_ny; iy++) {
+ unsigned int ix;
+ 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() / m_nx;
+ vImag[ix][iy] = pcRow[ix].imag() / m_nx;
+ }
+ }
+ 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 | FFTW_ESTIMATE | FFTW_USE_WISDOM);
+ 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
+{
+ 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;
+ }
+
+ ImageFileArrayConst vReal = getArray();
+ ImageFileArrayConst vImag = getImaginaryArray();
+
+ fftw_plan plan = fftw_create_plan (m_ny, FFTW_FORWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
+
+ std::complex<double>* pcCol = new std::complex<double> [m_ny];
+ fftw_complex* in = new fftw_complex [m_ny];
+ for (unsigned int ix = 0; ix < m_nx; ix++) {
+ unsigned int iy;
+ for (iy = 0; iy < m_ny; iy++) {
+ in[iy].re = vReal[ix][iy];
+ if (isComplex())
+ in[iy].im = vImag[ix][iy];
+ else
+ in[iy].im = 0;
+ }
+
+ fftw_one (plan, in, NULL);
+
+ for (iy = 0; iy < m_ny; iy++)
+ pcCol[iy] = std::complex<double>(in[iy].re, in[iy].im);
+
+ Fourier::shuffleFourierToNaturalOrder (pcCol, m_ny);
+ for (iy = 0; iy < m_ny; iy++) {
+ vReal[ix][iy] = pcCol[iy].real() / m_ny;
+ vImag[ix][iy] = pcCol[iy].imag() / m_ny;
+ }
+ }
+ delete [] pcCol;
+
+ fftw_destroy_plan (plan);
+ delete in;
+
+ return true;
+}
+
+bool
+ImageFile::ifftCols (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_ny];
+
+ ImageFileArrayConst vReal = getArray();
+ ImageFileArrayConst vImag = getImaginaryArray();
+
+ fftw_plan plan = fftw_create_plan (m_ny, FFTW_BACKWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
+ std::complex<double>* pcCol = new std::complex<double> [m_ny];
+
+ unsigned int ix, iy;
+ // unsigned int iArray = 0;
+ for (ix = 0; ix < m_nx; ix++) {
+ for (iy = 0; iy < m_ny; iy++) {
+ double dImag = 0;
+ if (isComplex())
+ dImag = vImag[ix][iy];
+ pcCol[iy] = std::complex<double> (vReal[ix][iy], dImag);
+ }
+
+ Fourier::shuffleNaturalToFourierOrder (pcCol, m_ny);
+
+ for (iy = 0; iy < m_ny; iy++) {
+ in[iy].re = pcCol[iy].real();
+ in[iy].im = pcCol[iy].imag();
+ }
+
+ fftw_one (plan, in, NULL);
+
+ for (iy = 0; iy < m_ny; iy++) {
+ vReal[ix][iy] = in[iy].re;
+ vImag[ix][iy] = in[iy].im;
+ }
+ }
+ delete [] pcCol;
+
+ fftw_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<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] = ::fabs(vReal[ix][iy]);
+ }
+
+ if (result.isComplex())
+ result.reallocComplexToReal();
+
+ 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.reallocComplexToReal();
+
+ return true;
+}
+
+bool
+ImageFile::real (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 vRealResult = result.getArray();
+
+ for (unsigned int ix = 0; ix < m_nx; ix++) {
+ for (unsigned int iy = 0; iy < m_ny; iy++) {
+ vRealResult[ix][iy] = vReal[ix][iy];
+ }
+ }
+
+ if (result.isComplex())
+ result.reallocComplexToReal();
+
+ return true;
+}
+
+bool
+ImageFile::imaginary (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 vImag = getArray();
+ 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] = vImag[ix][iy];
+ else
+ vRealResult[ix][iy] = 0;
+ }
+ }
+
+ if (result.isComplex())
+ result.reallocComplexToReal();
+
+ 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::convertExportFormatNameToID (const char* const formatName)
+{
+ int formatID = EXPORT_FORMAT_INVALID;
+
+ for (int i = 0; i < s_iExportFormatCount; i++)
+ if (strcasecmp (formatName, s_aszExportFormatName[i]) == 0) {
+ formatID = i;
+ break;
+ }
+
+ return (formatID);
+}
+
+const char*
+ImageFile::convertExportFormatIDToName (int formatID)
+{
+ static const char *formatName = "";
+
+ if (formatID >= 0 && formatID < s_iExportFormatCount)
+ return (s_aszExportFormatName[formatID]);
+
+ return (formatName);
+}
+
+const char*
+ImageFile::convertExportFormatIDToTitle (const int formatID)
+{
+ static const char *formatTitle = "";
+
+ if (formatID >= 0 && formatID < s_iExportFormatCount)
+ return (s_aszExportFormatTitle[formatID]);
+
+ return (formatTitle);
+}
+
+int
+ImageFile::convertImportFormatNameToID (const char* const formatName)
+{
+ int formatID = IMPORT_FORMAT_INVALID;
+
+ for (int i = 0; i < s_iImportFormatCount; i++)
+ if (strcasecmp (formatName, s_aszImportFormatName[i]) == 0) {
+ formatID = i;
+ break;
+ }
+
+ return (formatID);
+}
+
+const char*
+ImageFile::convertImportFormatIDToName (int formatID)
+{
+ static const char *formatName = "";
+
+ if (formatID >= 0 && formatID < s_iImportFormatCount)
+ return (s_aszImportFormatName[formatID]);
+
+ return (formatName);
+}
+
+const char*
+ImageFile::convertImportFormatIDToTitle (const int formatID)
+{
+ static const char *formatTitle = "";
+
+ if (formatID >= 0 && formatID < s_iImportFormatCount)
+ return (s_aszImportFormatTitle[formatID]);
+
+ return (formatTitle);
+}
+
+bool
+ImageFile::importImage (const char* const pszFormat, const char* const pszFilename)
+{
+ int iFormatID = convertImportFormatNameToID (pszFormat);
+
+ if (iFormatID == IMPORT_FORMAT_PPM)
+ return readImagePPM (pszFilename);
+#ifdef HAVE_PNG
+ else if (iFormatID == IMPORT_FORMAT_PNG)
+ return readImagePNG (pszFilename);
+#endif
+
+ sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::importImage]", pszFormat);
+ return false;
+}
+