Update copyright date; remove old CVS keyword
[ctsim.git] / libctsim / imagefile.cpp
index 4a49465065257ba2f02dd5c4df6c20e2b1cd4ebc..51a8dea82e94f2924671f0a9998119b055963990 100644 (file)
@@ -1,15 +1,13 @@
 /*****************************************************************************
 ** FILE IDENTIFICATION
 **
-**     Name:           imagefile.cpp
+**      Name:           imagefile.cpp
 **  Purpose:      Imagefile classes
-**     Programmer:   Kevin Rosenberg
-**     Date Started: June 2000
+**      Programmer:   Kevin Rosenberg
+**      Date Started: June 2000
 **
 **  This is part of the CTSim program
-**  Copyright (c) 1983-2001 Kevin Rosenberg
-**
-**  $Id: imagefile.cpp,v 1.46 2002/06/27 03:19:23 kevin Exp $
+**  Copyright (c) 1983-2009 Kevin Rosenberg
 **
 **  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
@@ -29,6 +27,7 @@
 #ifdef HAVE_CTN_DICOM
 #include "ctndicom.h"
 #endif
+#include "interpolator.h"
 
 const double ImageFile::s_dRedGrayscaleFactor = 0.299;
 const double ImageFile::s_dGreenGrayscaleFactor = 0.587;
@@ -48,29 +47,31 @@ const int ImageFile::EXPORT_FORMAT_DICOM = 5;
 #endif
 const int ImageFile::EXPORT_FORMAT_RAW = 6;
 
-const char* ImageFile::s_aszExportFormatName[] = 
+const char* ImageFile::s_aszExportFormatName[] =
 {
-  {"text"},
-  {"pgm"},
-  {"pgmascii"},
+  "text",
+  "pgm",
+  "pgmascii",
 #ifdef HAVE_PNG
-  {"png"},
-  {"png16"},
+  "png",
+  "png16",
 #endif
 #ifdef HAVE_CTN_DICOM
-  {"dicom"},
+  "dicom",
 #endif
 };
 
-const char* ImageFile::s_aszExportFormatTitle[] = 
+const char* ImageFile::s_aszExportFormatTitle[] =
 {
-  {"Text"},
-  {"PGM"},
-  {"PGM ASCII"},
-  {"PNG"},
-  {"PNG 16-bit"},
+  "Text",
+  "PGM",
+  "PGM ASCII",
+#ifdef HAVE_PNG
+  "PNG",
+  "PNG 16-bit",
+#endif
 #ifdef HAVE_CTN_DICOM
-  {"Dicom"},
+  "Dicom",
 #endif
 };
 const int ImageFile::s_iExportFormatCount = sizeof(s_aszExportFormatName) / sizeof(const char*);
@@ -86,23 +87,25 @@ const int ImageFile::IMPORT_FORMAT_DICOM = 2;
 #endif
 
 
-const char* ImageFile::s_aszImportFormatName[] = 
+const char* ImageFile::s_aszImportFormatName[] =
 {
-  {"ppm"},
+  "ppm",
 #ifdef HAVE_PNG
-  {"png"},
+  "png",
 #endif
 #ifdef HAVE_CTN_DICOM
-  {"dicom"},
+  "dicom",
 #endif
 };
 
-const char* ImageFile::s_aszImportFormatTitle[] = 
+const char* ImageFile::s_aszImportFormatTitle[] =
 {
-  {"PPM"},
-  {"PNG"},
+  "PPM",
+#ifdef HAVE_PNG
+  "PNG",
+#endif
 #ifdef HAVE_CTN_DICOM
-  {"Dicom"},
+  "Dicom",
 #endif
 };
 const int ImageFile::s_iImportFormatCount = sizeof(s_aszImportFormatName) / sizeof(const char*);
@@ -135,14 +138,14 @@ F64Image::F64Image (void)
   setDataType (Array2dFile::DATA_TYPE_REAL);
 }
 
-void 
+void
 ImageFile::getCenterCoordinates (unsigned int& iXCenter, unsigned int& iYCenter)
 {
   if (isEven (m_nx))
     iXCenter = m_nx / 2;
   else
     iXCenter = (m_nx - 1) / 2;
-  
+
   if (isEven (m_ny))
     iYCenter = m_ny / 2;
   else
@@ -150,16 +153,16 @@ ImageFile::getCenterCoordinates (unsigned int& iXCenter, unsigned int& iYCenter)
 }
 
 
-void 
-ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, 
+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);
-  
+
   unsigned int iXCenter, iYCenter;
   getCenterCoordinates (iXCenter, iYCenter);
-  
+
   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));
@@ -189,9 +192,9 @@ ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r,
   ImageFileArrayConst v = getArray();
   if (v == NULL || m_nx == 0 || m_ny == 0)
     return false;
-  
+
   ImageFileArrayConst vComp = imComp.getArray();
-  
+
   double myMean = 0.;
   for (unsigned int ix = 0; ix < m_nx; ix++) {
     for (unsigned int iy = 0; iy < m_ny; iy++) {
@@ -199,7 +202,7 @@ ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r,
     }
   }
   myMean /= (m_nx * m_ny);
-  
+
   double sqErrorSum = 0.;
   double absErrorSum = 0.;
   double sqDiffFromMeanSum = 0.;
@@ -214,10 +217,10 @@ ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r,
       absValueSum += fabs(v[ix2][iy]);
     }
   }
-  
+
   d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
   r = absErrorSum / absValueSum;
-  
+
   int hx = m_nx / 2;
   int hy = m_ny / 2;
   double eMax = -1;
@@ -230,9 +233,9 @@ ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r,
         eMax = error;
     }
   }
-  
+
   e = eMax;
-  
+
   return true;
 }
 
@@ -241,7 +244,7 @@ bool
 ImageFile::printComparativeStatistics (const ImageFile& imComp, std::ostream& os) const
 {
   double d, r, e;
-  
+
   if (comparativeStatistics (imComp, d, r, e)) {
     os << "  Normalized root mean squared distance (d): " << d << std::endl;
     os << "      Normalized mean absolute distance (r): " << r << std::endl;
@@ -256,18 +259,18 @@ void
 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;
-  
+
   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 << std::endl << "Imaginary Component Statistics" << std::endl;
@@ -294,10 +297,10 @@ ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double&
 {
   int nx = m_nx;
   int ny = m_ny;
-  
+
   if (v == NULL || nx == 0 || ny == 0)
     return;
-  
+
   std::vector<double> vecImage;
   int iVec = 0;
   vecImage.resize (nx * ny);
@@ -305,7 +308,7 @@ ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double&
     for (int iy = 0; iy < ny; iy++)
       vecImage[iVec++] = v[ix][iy];
   }
-  
+
   vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);
 }
 
@@ -315,10 +318,10 @@ ImageFile::getMinMax (double& min, double& max) const
   int nx = m_nx;
   int ny = m_ny;
   ImageFileArrayConst v = getArray();
-  
+
   if (v == NULL || nx == 0 || ny == 0)
     return;
-  
+
   min = v[0][0];
   max = v[0][0];
   for (int ix = 0; ix < nx; ix++) {
@@ -336,17 +339,17 @@ 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;
 }
 
@@ -355,7 +358,7 @@ 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++) {
@@ -367,7 +370,7 @@ ImageFile::convertComplexToReal ()
       vImagCol++;
     }
   }
-  
+
   return reallocComplexToReal();
 }
 
@@ -378,17 +381,17 @@ ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
     return false;
   }
-  
-  if (isComplex() || rRHS.isComplex() && ! result.isComplex())
+
+  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];
@@ -401,7 +404,7 @@ ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
       }
     }
   }
-  
+
   return true;
 }
 
@@ -412,17 +415,17 @@ ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
     return false;
   }
-  
-  if (isComplex() || rRHS.isComplex() && ! result.isComplex())
+
+  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];
@@ -435,7 +438,7 @@ ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
       }
     }
   }
-  
+
   return true;
 }
 
@@ -446,17 +449,17 @@ ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
     return false;
   }
-  
-  if (isComplex() || rRHS.isComplex() && ! result.isComplex())
+
+  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()) {
@@ -475,8 +478,8 @@ ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
         vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy];
     }
   }
-  
-  
+
+
   return true;
 }
 
@@ -487,17 +490,17 @@ ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");
     return false;
   }
-  
-  if (isComplex() || rRHS.isComplex() && ! result.isComplex())
+
+  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()) {
@@ -520,7 +523,7 @@ ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
       }
     }
   }
-  
+
   return true;
 }
 
@@ -532,20 +535,20 @@ ImageFile::invertPixelValues (ImageFile& result) const
     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;
 }
 
@@ -556,10 +559,10 @@ ImageFile::sqrt (ImageFile& result) const
     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
@@ -570,11 +573,11 @@ ImageFile::sqrt (ImageFile& result) const
           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()) {
@@ -589,8 +592,8 @@ ImageFile::sqrt (ImageFile& result) const
               vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);
           }
         }
-        
-        
+
+
         return true;
 }
 
@@ -601,15 +604,15 @@ ImageFile::log (ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::log]");
     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()) {
@@ -628,8 +631,8 @@ ImageFile::log (ImageFile& result) const
       }
     }
   }
-  
-  
+
+
   return true;
 }
 
@@ -640,15 +643,15 @@ ImageFile::exp (ImageFile& result) const
     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()) {
@@ -663,8 +666,8 @@ ImageFile::exp (ImageFile& result) const
         vResult[ix][iy] = ::exp (vLHS[ix][iy]);
     }
   }
-  
-  
+
+
   return true;
 }
 
@@ -675,18 +678,18 @@ ImageFile::scaleImage (ImageFile& result) const
   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();
-  
+
   BilinearInterpolator<ImageFileValue> realInterp (vReal, nx, ny);
   BilinearInterpolator<ImageFileValue> imagInterp (vImag, nx, ny);
 
@@ -695,11 +698,13 @@ ImageFile::scaleImage (ImageFile& result) const
       double dXPos = ix / dXScale;
       double dYPos = iy / dYScale;
       vResult[ix][iy] = realInterp.interpolate (dXPos, dYPos);
-      if (result.isComplex())
-        if (isComplex())
+      if (result.isComplex()) {
+        if (isComplex()) {
           vResultImag[ix][iy] = imagInterp.interpolate (dXPos, dYPos);
-        else
+        } else {
           vResultImag[ix][iy] = 0;
+        }
+      }
     }
   }
 
@@ -714,49 +719,49 @@ ImageFile::fft (ImageFile& result) const
     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];
-  
+
+  fftw_complex* in = static_cast<fftw_complex*> (fftw_malloc (sizeof(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];
+      in[iArray][0] = vReal[ix][iy];
       if (isComplex())
-        in[iArray].im = vImag[ix][iy];
+        in[iArray][1] = vImag[ix][iy];
       else
-        in[iArray].im = 0;
+        in[iArray][1] = 0;
       iArray++;
     }
   }
-  
-  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);
-  
+
+  fftw_plan plan = fftw_plan_dft_2d (m_nx, m_ny, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
+  fftw_execute (plan);
+
   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;
+      vRealResult[ix][iy] = in[iArray][0] / iScale;
+      vImagResult[ix][iy] = in[iArray][1] / iScale;
       iArray++;
     }
   }
-  delete in;
-  fftwnd_destroy_plan (plan);
-  
+  fftw_free(in);
+  fftw_destroy_plan (plan);
+
   Fourier::shuffleFourierToNaturalOrder (result);
-  
+
   return true;
 }
 
@@ -768,12 +773,12 @@ ImageFile::ifft (ImageFile& result) const
     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();
@@ -782,42 +787,41 @@ ImageFile::ifft (ImageFile& result) const
   for (ix = 0; ix < m_nx; ix++) {
     for (iy = 0; iy < m_ny; iy++) {
       vRealResult[ix][iy] = vReal[ix][iy];
-      if (isComplex()) 
+      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];
-  
+
+  fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(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];
+      in[iArray][0] = vRealResult[ix][iy];
+      in[iArray][1] = 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);
-  
+
+  fftw_plan plan = fftw_plan_dft_2d (m_nx, m_ny, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
+
+  fftw_execute (plan);
+
   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;
+      vRealResult[ix][iy] = in[iArray][0];
+      vImagResult[ix][iy] = in[iArray][1];
       iArray++;
     }
   }
-  fftwnd_destroy_plan (plan);
-  
-  delete in;
-  
+  fftw_destroy_plan (plan);
+  fftw_free(in);
+
   return true;
 }
 
@@ -828,34 +832,34 @@ ImageFile::fftRows (ImageFile& result) const
     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];  
+  fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx));
+  fftw_plan plan = fftw_plan_dft_1d (m_nx, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
+
+  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];
+      in[ix][0] = vReal[ix][iy];
       if (isComplex())
-        in[ix].im = vImag[ix][iy];
+        in[ix][1] = vImag[ix][iy];
       else
-        in[ix].im = 0;
+        in[ix][1] = 0;
     }
-    
-    fftw_one (plan, in, NULL);
-    
+
+    fftw_execute (plan);
+
     for (ix = 0; ix < m_nx; ix++)
-      pcRow[ix] = std::complex<double>(in[ix].re, in[ix].im);
-    
+      pcRow[ix] = std::complex<double>(in[ix][0], in[ix][1]);
+
     Fourier::shuffleFourierToNaturalOrder (pcRow, m_nx);
     for (ix = 0; ix < m_nx; ix++) {
       vReal[ix][iy] = pcRow[ix].real() / m_nx;
@@ -863,12 +867,12 @@ ImageFile::fftRows (ImageFile& result) const
     }
   }
   delete [] pcRow;
-  
+
   fftw_destroy_plan (plan);
-  delete in;
-  
+  fftw_free(in);
+
   return true;
-}     
+}
 
 bool
 ImageFile::ifftRows (ImageFile& result) const
@@ -877,20 +881,19 @@ ImageFile::ifftRows (ImageFile& result) const
     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);
+
+  fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_nx));
+  fftw_plan plan = fftw_plan_dft_1d (m_nx, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
   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++) {
@@ -900,26 +903,26 @@ ImageFile::ifftRows (ImageFile& result) const
         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();
+      in[ix][0] = pcRow[ix].real();
+      in[ix][1] = pcRow[ix].imag();
     }
-    
-    fftw_one (plan, in, NULL);
-    
+
+    fftw_execute (plan);
+
     for (ix = 0; ix < m_nx; ix++) {
-      vReal[ix][iy] = in[ix].re;
-      vImag[ix][iy] = in[ix].im;
+      vReal[ix][iy] = in[ix][0];
+      vImag[ix][iy] = in[ix][1];
     }
   }
   delete [] pcRow;
-  
+
   fftw_destroy_plan (plan);
-  delete in;
-  
+  fftw_free(in);
+
   return true;
 }
 
@@ -930,34 +933,34 @@ ImageFile::fftCols (ImageFile& result) const
     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];
+  fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_ny));
+  fftw_plan plan = fftw_plan_dft_1d (m_ny, in, in, FFTW_FORWARD, FFTW_ESTIMATE);
+
+  std::complex<double>* pcCol = new std::complex<double> [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];
+      in[iy][0] = vReal[ix][iy];
       if (isComplex())
-        in[iy].im = vImag[ix][iy];
+        in[iy][1] = vImag[ix][iy];
       else
-        in[iy].im = 0;
+        in[iy][1] = 0;
     }
-    
-    fftw_one (plan, in, NULL);
-    
+
+    fftw_execute (plan);
+
     for (iy = 0; iy < m_ny; iy++)
-      pcCol[iy] = std::complex<double>(in[iy].re, in[iy].im);
-    
+      pcCol[iy] = std::complex<double>(in[iy][0], in[iy][1]);
+
     Fourier::shuffleFourierToNaturalOrder (pcCol, m_ny);
     for (iy = 0; iy < m_ny; iy++) {
       vReal[ix][iy] = pcCol[iy].real() / m_ny;
@@ -965,10 +968,10 @@ ImageFile::fftCols (ImageFile& result) const
     }
   }
   delete [] pcCol;
-  
+
   fftw_destroy_plan (plan);
-  delete in;
-  
+  fftw_free(in);
+
   return true;
 }
 
@@ -979,20 +982,19 @@ ImageFile::ifftCols (ImageFile& result) const
     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);
+
+  fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * m_ny));
+  fftw_plan plan = fftw_plan_dft_1d (m_ny, in, in, FFTW_BACKWARD, FFTW_ESTIMATE);
   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++) {
@@ -1002,26 +1004,26 @@ ImageFile::ifftCols (ImageFile& result) const
         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();
+      in[iy][0] = pcCol[iy].real();
+      in[iy][1] = pcCol[iy].imag();
     }
-    
-    fftw_one (plan, in, NULL);
-    
+
+    fftw_execute (plan);
+
     for (iy = 0; iy < m_ny; iy++) {
-      vReal[ix][iy] = in[iy].re;
-      vImag[ix][iy] = in[iy].im;
+      vReal[ix][iy] = in[iy][0];
+      vImag[ix][iy] = in[iy][1];
     }
   }
   delete [] pcCol;
-  
+
   fftw_destroy_plan (plan);
-  delete in;
-  
+  fftw_free(in);
+
   return true;
 }
 
@@ -1036,23 +1038,23 @@ ImageFile::fourier (ImageFile& result) const
     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++) {
@@ -1061,11 +1063,11 @@ ImageFile::fourier (ImageFile& result) const
         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];
@@ -1078,20 +1080,20 @@ ImageFile::fourier (ImageFile& result) const
     }
     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;
 }
 
@@ -1102,23 +1104,23 @@ ImageFile::inverseFourier (ImageFile& result) const
     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++) {
@@ -1128,9 +1130,9 @@ ImageFile::inverseFourier (ImageFile& result) const
       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++) {
@@ -1140,7 +1142,7 @@ ImageFile::inverseFourier (ImageFile& result) const
       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];
@@ -1153,18 +1155,18 @@ ImageFile::inverseFourier (ImageFile& result) const
     }
     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;
 }
 
@@ -1176,22 +1178,22 @@ ImageFile::magnitude (ImageFile& result) const
     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()) 
+      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;
 }
 
@@ -1202,11 +1204,11 @@ ImageFile::phase (ImageFile& result) const
     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())
@@ -1217,7 +1219,7 @@ ImageFile::phase (ImageFile& result) const
   }
   if (result.isComplex())
     result.reallocComplexToReal();
-    
+
   return true;
 }
 
@@ -1228,10 +1230,10 @@ ImageFile::real (ImageFile& result) const
     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];
@@ -1240,7 +1242,7 @@ ImageFile::real (ImageFile& result) const
 
   if (result.isComplex())
     result.reallocComplexToReal();
-    
+
   return true;
 }
 
@@ -1251,10 +1253,10 @@ ImageFile::imaginary (ImageFile& result) const
     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())
@@ -1266,7 +1268,7 @@ ImageFile::imaginary (ImageFile& result) const
 
   if (result.isComplex())
       result.reallocComplexToReal();
-    
+
   return true;
 }
 
@@ -1277,15 +1279,15 @@ ImageFile::square (ImageFile& result) const
     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()) {
@@ -1300,7 +1302,7 @@ ImageFile::square (ImageFile& result) const
         vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];
     }
   }
-    
+
   return true;
 }
 
@@ -1308,13 +1310,13 @@ 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);
 }
 
@@ -1322,10 +1324,10 @@ const char*
 ImageFile::convertExportFormatIDToName (int formatID)
 {
   static const char *formatName = "";
-  
+
   if (formatID >= 0 && formatID < s_iExportFormatCount)
     return (s_aszExportFormatName[formatID]);
-  
+
   return (formatName);
 }
 
@@ -1333,10 +1335,10 @@ const char*
 ImageFile::convertExportFormatIDToTitle (const int formatID)
 {
   static const char *formatTitle = "";
-  
+
   if (formatID >= 0 && formatID < s_iExportFormatCount)
     return (s_aszExportFormatTitle[formatID]);
-  
+
   return (formatTitle);
 }
 
@@ -1344,13 +1346,13 @@ 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);
 }
 
@@ -1358,10 +1360,10 @@ const char*
 ImageFile::convertImportFormatIDToName (int formatID)
 {
   static const char *formatName = "";
-  
+
   if (formatID >= 0 && formatID < s_iImportFormatCount)
     return (s_aszImportFormatName[formatID]);
-  
+
   return (formatName);
 }
 
@@ -1369,10 +1371,10 @@ const char*
 ImageFile::convertImportFormatIDToTitle (const int formatID)
 {
   static const char *formatTitle = "";
-  
+
   if (formatID >= 0 && formatID < s_iImportFormatCount)
     return (s_aszImportFormatTitle[formatID]);
-  
+
   return (formatTitle);
 }
 
@@ -1380,14 +1382,14 @@ 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;
 }
@@ -1405,7 +1407,7 @@ ImageFile::skipSpacePPM (FILE* fp)
     else
       c = fgetc(fp);
   }
-  
+
   ungetc (c, fp);
 }
 
@@ -1430,9 +1432,9 @@ ImageFile::readImagePPM (const char* const pszFile)
     fclose(fp);
     return false;
   }
-  
+
   int nRows, nCols, iMaxValue;
-  skipSpacePPM (fp); 
+  skipSpacePPM (fp);
   if (fscanf (fp, "%d", &nCols) != 1) {
     fclose(fp);
     return false;
@@ -1448,7 +1450,7 @@ ImageFile::readImagePPM (const char* const pszFile)
     return false;
   }
   setArraySize (nRows, nCols);
-  
+
   if (cSignature == '5' || cSignature == '6') { // binary modes
     int c = fgetc(fp);
     if (c == 13) {
@@ -1458,7 +1460,7 @@ ImageFile::readImagePPM (const char* const pszFile)
     }
   } else
     skipSpacePPM (fp); // ascii may have comments
-  
+
   bool bMonochromeImage = false;
   double dMaxValue = iMaxValue;
   double dMaxValue3 = iMaxValue * 3;
@@ -1524,7 +1526,7 @@ ImageFile::readImagePPM (const char* const pszFile)
       }
     }
   }
-  
+
   fclose(fp);
   return true;
 }
@@ -1534,7 +1536,7 @@ bool
 ImageFile::readImagePNG (const char* const pszFile)
 {
   FILE* fp = fopen(pszFile, "rb");
-  if (!fp) 
+  if (!fp)
     return false;
   unsigned char header[8];
   fread (header, 1, 8, fp);
@@ -1542,69 +1544,69 @@ ImageFile::readImagePNG (const char* const pszFile)
     fclose (fp);
     return false;
   }
-  
+
   png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
   if (!png_ptr) {
     fclose(fp);
     return false;
   }
-  
+
   png_infop info_ptr = png_create_info_struct(png_ptr);
   if (!info_ptr) {
     png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
     fclose(fp);
     return false;
   }
-  
+
   png_infop end_info = png_create_info_struct(png_ptr);
   if (!end_info) {
     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
     fclose(fp);
     return false;
   }
-  
+
   if (setjmp(png_ptr->jmpbuf)) {
     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
     fclose(fp);
     return false;
   }
-  
+
   png_init_io(png_ptr, fp);
   png_set_sig_bytes(png_ptr, 8);
   png_read_info(png_ptr, info_ptr);
-  
+
   int width = png_get_image_width (png_ptr, info_ptr);
   int height = png_get_image_height (png_ptr, info_ptr);
   int bit_depth = png_get_bit_depth (png_ptr, info_ptr);
   int color_type = png_get_color_type (png_ptr, info_ptr);
-  
-  if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8) 
+
+  if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
     png_set_expand(png_ptr);
-  
-  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) 
+
+  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
     png_set_expand(png_ptr);
-  
+
   if (bit_depth < 8)
     png_set_packing(png_ptr);
-  
+
   if (color_type & PNG_COLOR_MASK_ALPHA)
     png_set_strip_alpha(png_ptr);
-  
+
   if (bit_depth == 16)
     png_set_swap(png_ptr); // convert to little-endian format
-  
+
   png_read_update_info(png_ptr, info_ptr); // update with transformations
   int rowbytes = png_get_rowbytes (png_ptr, info_ptr);
   bit_depth = png_get_bit_depth (png_ptr, info_ptr);
   color_type = png_get_color_type (png_ptr, info_ptr);
-  
+
   png_bytep* row_pointers = new png_bytep [height];
   int i;
   for (i = 0; i < height; i++)
     row_pointers[i] = new unsigned char [rowbytes];
-  
+
   png_read_image(png_ptr, row_pointers);
-  
+
   setArraySize (width, height);
   ImageFileArray v = getArray();
   for (int iy = 0; iy < height; iy++) {
@@ -1617,7 +1619,7 @@ ImageFile::readImagePNG (const char* const pszFile)
           int iBase = ix * 2;
           dV = (row_pointers[iy][iBase] + (row_pointers[iy][iBase+1] << 8)) / 65536.;
         } else
-         dV = 0;
+          dV = 0;
       } else if (color_type == PNG_COLOR_TYPE_RGB) {
         if (bit_depth == 8) {
           int iBase = ix * 3;
@@ -1626,19 +1628,19 @@ ImageFile::readImagePNG (const char* const pszFile)
           double dB = row_pointers[iy][iBase+2] / 255.;
           dV = colorToGrayscale (dR, dG, dB);
         } else
-         dV = 0;
+          dV = 0;
       }
       v[ix][height-iy-1] = dV;
     }
   }
-  
+
   png_read_end(png_ptr, end_info);
   png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
-  
+
   for (i = 0; i < height; i++)
     delete row_pointers[i];
   delete row_pointers;
-  
+
   fclose (fp);
   return true;
 }
@@ -1648,7 +1650,7 @@ bool
 ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)
 {
   int iFormatID = convertExportFormatNameToID (pszFormat);
-  
+
   if (iFormatID == EXPORT_FORMAT_PGM)
     return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);
   else if (iFormatID == EXPORT_FORMAT_PGMASCII)
@@ -1665,15 +1667,15 @@ ImageFile::exportImage (const char* const pszFormat, const char* const pszFilena
   else if (iFormatID == EXPORT_FORMAT_DICOM) {
     DicomExporter dicomExport (this);
     bool bSuccess = dicomExport.writeFile (pszFilename);
-    if (! bSuccess) 
+    if (! bSuccess)
       sys_error (ERR_SEVERE, dicomExport.failMessage().c_str());
     return bSuccess;
   }
 #endif
-  else if (iFormatID == EXPORT_FORMAT_RAW)  
-        return writeImageRaw(pszFilename, nxcell, nycell);
-       
-  
+  else if (iFormatID == EXPORT_FORMAT_RAW)
+         return writeImageRaw(pszFilename, nxcell, nycell);
+
+
   sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);
   return false;
 }
@@ -1686,16 +1688,16 @@ ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, dou
   int nx = m_nx;
   int ny = m_ny;
   ImageFileArray v = getArray();
-  
+
   unsigned char* rowp = new unsigned char [nx * nxcell];
-  
+
   if ((fp = fopen (outfile, "wb")) == NULL)
     return false;
-  
+
   fprintf(fp, "P5\n");
   fprintf(fp, "%d %d\n", nx, ny);
   fprintf(fp, "255\n");
-  
+
   for (int irow = ny - 1; irow >= 0; irow--) {
     for (int icol = 0; icol < nx; icol++) {
       int pos = icol * nxcell;
@@ -1706,14 +1708,14 @@ ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, dou
       }
     }
     for (int ir = 0; ir < nycell; ir++) {
-      for (int ic = 0; ic < nx * nxcell; ic++) 
+      for (int ic = 0; ic < nx * nxcell; ic++)
         fputc( rowp[ic], fp );
     }
   }
-  
+
   delete rowp;
   fclose(fp);
-  
+
   return true;
 }
 
@@ -1724,16 +1726,16 @@ ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell
   int nx = m_nx;
   int ny = m_ny;
   ImageFileArray v = getArray();
-  
+
   unsigned char* rowp = new unsigned char [nx * nxcell];
-  
+
   if ((fp = fopen (outfile, "wb")) == NULL)
     return false;
-  
+
   fprintf(fp, "P2\n");
   fprintf(fp, "%d %d\n", nx, ny);
   fprintf(fp, "255\n");
-  
+
   for (int irow = ny - 1; irow >= 0; irow--) {
     for (int icol = 0; icol < nx; icol++) {
       int pos = icol * nxcell;
@@ -1744,15 +1746,15 @@ ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell
       }
     }
     for (int ir = 0; ir < nycell; ir++) {
-      for (int ic = 0; ic < nx * nxcell; ic++) 
+      for (int ic = 0; ic < nx * nxcell; ic++)
         fprintf(fp, "%d ", rowp[ic]);
       fprintf(fp, "\n");
     }
   }
-  
+
   delete rowp;
   fclose(fp);
-  
+
   return true;
 }
 
@@ -1764,10 +1766,10 @@ ImageFile::writeImageText (const char* const outfile)
   int ny = m_ny;
   ImageFileArray v = getArray();
   ImageFileArray vImag = getImaginaryArray();
-  
+
   if ((fp = fopen (outfile, "w")) == NULL)
     return false;
-  
+
   for (int irow = ny - 1; irow >= 0; irow--) {
     for (int icol = 0; icol < nx; icol++) {
       if (isComplex()) {
@@ -1780,9 +1782,9 @@ ImageFile::writeImageText (const char* const outfile)
     }
     fprintf(fp, "\n");
   }
-  
+
   fclose(fp);
-  
+
   return true;
 }
 
@@ -1795,44 +1797,44 @@ ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, i
   int nx = m_nx;
   int ny = m_ny;
   ImageFileArray v = getArray();
-  
+
   unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
-  
+
   FILE *fp = fopen (outfile, "wb");
   if (! fp)
     return false;
-  
+
   png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
   if (! png_ptr)
     return false;
-  
+
   png_infop info_ptr = png_create_info_struct (png_ptr);
   if (! info_ptr) {
     png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
     fclose (fp);
     return false;
   }
-  
+
   if (setjmp (png_ptr->jmpbuf)) {
     png_destroy_write_struct (&png_ptr, &info_ptr);
     fclose (fp);
     return false;
   }
-  
+
   png_init_io(png_ptr, fp);
-  
+
   png_set_IHDR (png_ptr, info_ptr, nx * nxcell, ny * nycell, bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
-  
+
   png_write_info(png_ptr, info_ptr);
   for (int irow = ny - 1; irow >= 0; irow--) {
     png_bytep row_pointer = rowp;
-    
+
     for (int icol = 0; icol < nx; icol++) {
       int pos = icol * nxcell;
       double dens = (v[icol][irow] - densmin) / (densmax - densmin);
       dens = clamp (dens, 0., 1.);
       unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
-      
+
       for (int p = pos; p < pos + nxcell; p++) {
         if (bitdepth == 8)
           rowp[p] = outval;
@@ -1846,13 +1848,13 @@ ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, i
     for (int ir = 0; ir < nycell; ir++)
       png_write_rows (png_ptr, &row_pointer, 1);
   }
-  
+
   png_write_end (png_ptr, info_ptr);
   png_destroy_write_struct (&png_ptr, &info_ptr);
   delete rowp;
-  
+
   fclose(fp);
-  
+
   return true;
 }
 #endif
@@ -1868,13 +1870,13 @@ ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, dou
   int nx = m_nx;
   int ny = m_ny;
   ImageFileArray v = getArray();
-  
+
   unsigned char* rowp = new unsigned char [nx * nxcell];
-  
+
   gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
   for (int i = 0; i < N_GRAYSCALE; i++)
     gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
-  
+
   int lastrow = ny * nycell - 1;
   for (int irow = 0; irow < ny; irow++) {
     int rpos = irow * nycell;
@@ -1890,7 +1892,7 @@ ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, dou
       }
     }
   }
-  
+
   FILE *out;
   if ((out = fopen (outfile,"w")) == NULL) {
     sys_error(ERR_SEVERE, "Error opening output file %s for writing", outfile);
@@ -1899,9 +1901,9 @@ ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, dou
   gdImageGif(gif,out);
   fclose(out);
   gdImageDestroy(gif);
-  
+
   delete rowp;
-  
+
   return true;
 }
 #endif
@@ -1913,17 +1915,17 @@ ImageFile::writeImageRaw (const char* const outfile, int nxcell, int nycell)
   int nx = m_nx;
   int ny = m_ny;
   ImageFileArray v = getArray();
-  
+
   if ((fp = fopen (outfile, "wb")) == NULL)
     return false;
-  
+
   for (int irow = ny - 1; irow >= 0; irow--) {
     for (int icol = 0; icol < nx; icol++) {
       float dens = v[icol][irow];
      fwrite(&dens, sizeof(float), 1, fp);
     }
   }
-  
+
   fclose(fp);
   return true;
 }