+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::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;
+}
+
+void
+ImageFile::skipSpacePPM (FILE* fp)
+{
+ int c = fgetc (fp);
+ while (isspace (c) || c == '#') {
+ if (c == '#') { // comment until end of line
+ c = fgetc(fp);
+ while (c != 13 && c != 10)
+ c = fgetc(fp);
+ }
+ else
+ c = fgetc(fp);
+ }
+
+ ungetc (c, fp);
+}
+
+bool
+ImageFile::readImagePPM (const char* const pszFile)
+{
+ FILE* fp = fopen (pszFile, "r");
+ if ((fp = fopen (pszFile, "r")) == NULL)
+ return false;
+ char cSignature = toupper(fgetc(fp));
+ if (cSignature != 'P') {
+ fclose(fp);
+ return false;
+ }
+ cSignature = fgetc(fp);
+ if (cSignature == '5' || cSignature == '6') { // binary modes
+ fclose(fp);
+ fp = fopen(pszFile, "rb"); // reopen in binary mode
+ fgetc(fp);
+ fgetc(fp);
+ } else if (cSignature != '2' && cSignature != '3') {
+ fclose(fp);
+ return false;
+ }
+
+ int nRows, nCols, iMaxValue;
+ skipSpacePPM (fp);
+ if (fscanf (fp, "%d", &nCols) != 1) {
+ fclose(fp);
+ return false;
+ }
+ skipSpacePPM (fp);
+ if (fscanf (fp, "%d", &nRows) != 1) {
+ fclose(fp);
+ return false;
+ }
+ skipSpacePPM (fp);
+ if (fscanf (fp, "%d", &iMaxValue) != 1) {
+ fclose(fp);
+ return false;
+ }
+ setArraySize (nRows, nCols);
+
+ if (cSignature == '5' || cSignature == '6') { // binary modes
+ int c = fgetc(fp);
+ if (c == 13) {
+ c = fgetc(fp);
+ if (c != 10) // read msdos 13-10 newline
+ ungetc(c, fp);
+ }
+ } else
+ skipSpacePPM (fp); // ascii may have comments
+
+ double dMaxValue = iMaxValue;
+ ImageFileArray v = getArray();
+ for (int iy = nRows - 1; iy >= 0; iy--) {
+ for (int ix = 0; ix < nCols; ix++) {
+ int iGS, iR, iG, iB;
+ double dR, dG, dB;
+ switch (cSignature) {
+ case '2':
+ if (fscanf(fp, "%d ", &iGS) != 1) {
+ fclose(fp);
+ return false;
+ }
+ v[ix][iy] = iGS / dMaxValue;
+ break;
+ case '5':
+ iGS = fgetc(fp);
+ if (iGS == EOF) {
+ fclose(fp);
+ return false;
+ }
+ v[ix][iy] = iGS / dMaxValue;
+ break;
+ case '3':
+ if (fscanf (fp, "%d %d %d ", &iR, &iG, &iB) != 3) {
+ fclose(fp);
+ return false;
+ }
+ dR = iR / dMaxValue;
+ dG = iG / dMaxValue;
+ dB = iB / dMaxValue;
+ v[ix][iy] = colorToGrayscale (dR, dG, dB);
+ break;
+ case '6':
+ iR = fgetc(fp);
+ iG = fgetc(fp);
+ iB = fgetc(fp);
+ if (iB == EOF) {
+ fclose(fp);
+ return false;
+ }
+ dR = iR / dMaxValue;
+ dG = iG / dMaxValue;
+ dB = iB / dMaxValue;
+ v[ix][iy] = colorToGrayscale (dR, dG, dB);
+ break;
+ }