r641: no message
[ctsim.git] / libctsim / imagefile.cpp
index 1c8a063db7c05af76bde6ff2db25df9457bb557c..bab87bdeff5ab0fa4486a66f629089e85efd4d91 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (c) 1983-2001 Kevin Rosenberg
 **
-**  $Id: imagefile.cpp,v 1.41 2001/03/18 18:08:25 kevin Exp $
+**  $Id: imagefile.cpp,v 1.42 2001/03/21 21:45:31 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
@@ -150,7 +150,8 @@ ImageFile::getCenterCoordinates (unsigned int& iXCenter, unsigned int& iYCenter)
 
 
 void 
-ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param, double dInputScale, double dOutputScale)
+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);
@@ -166,60 +167,6 @@ ImageFile::filterResponse (const char* const domainName, double bw, const char*
     }
 }
 
-int
-ImageFile::display (void) const
-{
-  double pmin, pmax;
-  
-  getMinMax (pmin, pmax);
-  
-  return (displayScaling (1, pmin, pmax));
-}
-
-int 
-ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax) const
-{
-  int nx = m_nx;
-  int ny = m_ny;
-  ImageFileArrayConst v = getArray();
-  if (v == NULL || nx == 0 || ny == 0)
-    return 0;
-  
-#if HAVE_G2_H
-  int* pPens = new int [nx * ny * scale * scale ];
-  
-  double view_scale = 255 / (pmax - pmin);
-  int id_X11 = g2_open_X11 (nx * scale, ny * scale);
-  int grayscale[256];
-  for (int i = 0; i < 256; i++) {
-    double cval = i / 255.;
-    grayscale[i] = g2_ink (id_X11, cval, cval, cval);
-  }
-  
-  for (int iy = ny - 1; iy >= 0; iy--) {
-    int iRowPos = ((ny - 1 - iy) * scale) * (nx * scale);
-    for (int ix = 0; ix < nx; ix++) {
-      int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
-      if (cval < 0)  
-        cval = 0;
-      else if (cval > 255) 
-        cval = 255;
-      for (int sy = 0; sy < scale; sy++)
-        for (int sx = 0; sx < scale; sx++)
-          pPens[iRowPos+(sy * nx * scale)+(sx + (ix * scale))] = grayscale[cval];
-    }
-  }
-  
-  g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens);
-  
-  delete pPens;
-  return (id_X11);
-#else
-  return 0;
-#endif
-}
-
-
 
 // ImageFile::comparativeStatistics    Calculate comparative stats
 //
@@ -739,40 +686,22 @@ ImageFile::scaleImage (ImageFile& result) const
   ImageFileArray vResult = result.getArray();
   ImageFileArray vResultImag = result.getImaginaryArray();
   
+  BilinearInterpolator<ImageFileValue> realInterp (vReal, nx, ny);
+  BilinearInterpolator<ImageFileValue> imagInterp (vImag, nx, ny);
+
   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;
-        }
-      }
+      vResult[ix][iy] = realInterp.interpolate (dXPos, dYPos);
+      if (result.isComplex())
+        if (isComplex())
+          vResultImag[ix][iy] = imagInterp.interpolate (dXPos, dYPos);
+        else
+          vResultImag[ix][iy] = 0;
     }
   }
-  
+
   return true;
 }
 
@@ -797,7 +726,7 @@ ImageFile::fft (ImageFile& result) const
   
   unsigned int ix, iy;
   unsigned int iArray = 0;
-  for (ix = 0; ix < m_nx; ix++)
+  for (ix = 0; ix < m_nx; ix++) {
     for (iy = 0; iy < m_ny; iy++) {
       in[iArray].re = vReal[ix][iy];
       if (isComplex())
@@ -806,29 +735,28 @@ ImageFile::fft (ImageFile& result) const
         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;
+  }
+  
+  fftwnd_plan plan = fftw2d_create_plan (m_nx, m_ny, FFTW_FORWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);  
+  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++;
+    }
+  }
+  delete in;
+  fftwnd_destroy_plan (plan);
+  
+  Fourier::shuffleFourierToNaturalOrder (result);
+  
+  return true;
 }
 
 
@@ -850,7 +778,7 @@ ImageFile::ifft (ImageFile& result) const
   ImageFileArray vRealResult = result.getArray();
   ImageFileArray vImagResult = result.getImaginaryArray();
   unsigned int ix, iy;
-  for (ix = 0; ix < m_nx; ix++)
+  for (ix = 0; ix < m_nx; ix++) {
     for (iy = 0; iy < m_ny; iy++) {
       vRealResult[ix][iy] = vReal[ix][iy];
       if (isComplex()) 
@@ -858,36 +786,38 @@ ImageFile::ifft (ImageFile& result) const
       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;
+  }
+  
+  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
@@ -902,18 +832,16 @@ ImageFile::fftRows (ImageFile& result) const
     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++) {
+  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 (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())
@@ -959,7 +887,7 @@ ImageFile::ifftRows (ImageFile& result) const
   ImageFileArrayConst vReal = getArray();
   ImageFileArrayConst vImag = getImaginaryArray();
   
-  fftw_plan plan = fftw_create_plan (m_nx, FFTW_BACKWARD, FFTW_IN_PLACE);
+  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;
@@ -1006,18 +934,16 @@ ImageFile::fftCols (ImageFile& result) const
     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_FORWARD, FFTW_IN_PLACE);
-  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++) {
+  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 (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())
@@ -1063,7 +989,7 @@ ImageFile::ifftCols (ImageFile& result) const
   ImageFileArrayConst vReal = getArray();
   ImageFileArrayConst vImag = getImaginaryArray();
   
-  fftw_plan plan = fftw_create_plan (m_ny, FFTW_BACKWARD, FFTW_IN_PLACE);
+  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;
@@ -1263,7 +1189,7 @@ ImageFile::magnitude (ImageFile& result) const
     }
     
     if (result.isComplex())
-      result.convertComplexToReal();
+      result.reallocComplexToReal();
     
     return true;
 }
@@ -1280,18 +1206,67 @@ ImageFile::phase (ImageFile& result) const
   ImageFileArray vImag = getImaginaryArray();
   ImageFileArray vRealResult = result.getArray();
   
-  for (unsigned int ix = 0; ix < m_nx; ix++)
+  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();
     
-    if (result.isComplex())
-      result.convertComplexToReal();
+  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;
+  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
@@ -1324,8 +1299,7 @@ ImageFile::square (ImageFile& result) const
         vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
     }
   }
-  
-  
+    
   return true;
 }
 
@@ -1484,7 +1458,10 @@ ImageFile::readImagePPM (const char* const pszFile)
   } else
     skipSpacePPM (fp); // ascii may have comments
   
+  bool bMonochromeImage = false;
   double dMaxValue = iMaxValue;
+  double dMaxValue3 = iMaxValue * 3;
+
   ImageFileArray v = getArray();
   for (int iy = nRows - 1; iy >= 0; iy--) {
     for (int ix = 0; ix < nCols; ix++) {
@@ -1511,23 +1488,37 @@ ImageFile::readImagePPM (const char* const pszFile)
           fclose(fp);
           return false;
         }
-        dR = iR / dMaxValue;
-        dG = iG / dMaxValue;
-        dB = iB / dMaxValue;
-        v[ix][iy] = colorToGrayscale (dR, dG, dB);
+        if (ix == 0 && iy == 0 && (iR == iG && iG == iB))
+          bMonochromeImage = true;
+        if (bMonochromeImage)
+          v[ix][iy] = (iR + iG + iB) / dMaxValue3;
+        else {
+          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);
+        if (ix == 0 && iy == 0 && (iR == iG && iG == iB))
+          bMonochromeImage = true;
+
+        if (bMonochromeImage)
+          v[ix][iy] = (iR + iG + iB) / dMaxValue3;
+        else {
+          dR = iR / dMaxValue;
+          dG = iG / dMaxValue;
+          dB = iB / dMaxValue;
+          v[ix][iy] = colorToGrayscale (dR, dG, dB);
+        }
         break;
       }
     }
@@ -1676,7 +1667,7 @@ ImageFile::exportImage (const char* const pszFormat, const char* const pszFilena
     return bSuccess;
   }
 #endif
-
+  
   sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
   return false;
 }
@@ -1767,7 +1758,7 @@ ImageFile::writeImageText (const char* const outfile)
   int ny = m_ny;
   ImageFileArray v = getArray();
   ImageFileArray vImag = getImaginaryArray();
-
+  
   if ((fp = fopen (outfile, "w")) == NULL)
     return false;