r326: FFTW additions, filter image generation
[ctsim.git] / libctsim / imagefile.cpp
index b6525e1fda69077f252b2994aede0cb997d01d21..df647ce930efad8de0b4a8288f14c99499fc71bb 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: imagefile.cpp,v 1.27 2000/12/29 20:15:37 kevin Exp $
+**  $Id: imagefile.cpp,v 1.28 2001/01/01 10:14:34 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
@@ -36,8 +36,8 @@ F32Image::F32Image (int nx, int ny, int dataType)
 F32Image::F32Image (void)\r
 : Array2dFile()\r
 {\r
-       setPixelFormat (Array2dFile::PIXEL_FLOAT32);\r
-       setPixelSize (sizeof(kfloat32));\r
+  setPixelFormat (Array2dFile::PIXEL_FLOAT32);\r
+  setPixelSize (sizeof(kfloat32));\r
   setDataType (Array2dFile::DATA_TYPE_REAL);\r
 }\r
 \r
@@ -49,78 +49,98 @@ F64Image::F64Image (int nx, int ny, int dataType)
 F64Image::F64Image (void)\r
 : Array2dFile ()\r
 {\r
-       setPixelFormat (PIXEL_FLOAT64);\r
-       setPixelSize (sizeof(kfloat64));\r
+  setPixelFormat (PIXEL_FLOAT64);\r
+  setPixelSize (sizeof(kfloat64));\r
   setDataType (Array2dFile::DATA_TYPE_REAL);\r
 }\r
 
 void 
 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
-{
-       int hx = (m_nx - 1) / 2;
-       int hy = (m_ny - 1) / 2;
-       ImageFileArray v = getArray();
-       SignalFilter filter (filterName, domainName, bw, filt_param);
-       
-       for (int i = -hx; i <= hx; i++) {
-               for (int j = -hy; j <= hy; j++) {
+{\r
+  ImageFileArray v = getArray();\r
+  SignalFilter filter (filterName, domainName, bw, filt_param);\r
+\r
+#if 1\r
+  int iXCenter, iYCenter;\r
+  if (isEven (m_nx))\r
+    iXCenter = m_nx / 2;\r
+  else\r
+    iXCenter = (m_nx - 1) / 2;\r
+  if (isEven (m_ny))\r
+    iYCenter = m_ny / 2;\r
+  else\r
+    iYCenter = (m_ny - 1) / 2;\r
+\r
+  for (int ix = 0; ix < m_nx; ix++)\r
+    for (int iy = 0; iy < m_ny; iy++) {\r
+      long lD2 = (ix - iXCenter) * (ix - iXCenter) + (iy - iYCenter) * (iy - iYCenter);\r
+      double r = ::sqrt (static_cast<double>(lD2));\r
+      v[ix][iy] = filter.response (r);\r
+    }\r
+#else
+  int hx = (m_nx - 1) / 2;
+  int hy = (m_ny - 1) / 2;
+  
+  for (int i = -hx; i <= hx; i++) {
+    for (int j = -hy; j <= hy; j++) {
       double r = ::sqrt (i * i + j * j);
-                       
-                       v[i+hx][j+hy] = filter.response (r);
-               }
-       }
+      
+      v[i+hx][j+hy] = filter.response (r);
+    }
+  }\r
+#endif
 }
 
 int
 ImageFile::display (void) const
 {
-    double pmin, pmax;
-       
-    getMinMax (pmin, pmax);
-       
-    return (displayScaling (1, pmin, pmax));
+  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;
-       
+  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];
-               }
+  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);
+  }
+  
+  g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens);
+  
+  delete pPens;
+  return (id_X11);
 #else
-    return 0;
+  return 0;
 #endif
 }
 
@@ -139,102 +159,102 @@ ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const Ima
 bool
 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
 {
-    if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
-               sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
-               return false;
-    }
-    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++) {
-                       myMean += v[ix][iy];
-               }
+  if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
+    sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
+    return false;
+  }
+  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++) {
+      myMean += v[ix][iy];
     }
-    myMean /= (m_nx * m_ny);
-       
-    double sqErrorSum = 0.;
-    double absErrorSum = 0.;
-    double sqDiffFromMeanSum = 0.;
-    double absValueSum = 0.;
-    for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
-               for (unsigned int iy = 0; iy < m_ny; iy++) {
-                       double diff = v[ix2][iy] - vComp[ix2][iy];
-                       sqErrorSum += diff * diff;
-                       absErrorSum += fabs(diff);
-                       double diffFromMean = v[ix2][iy] - myMean;
-                       sqDiffFromMeanSum += diffFromMean * diffFromMean;
-                       absValueSum += fabs(v[ix2][iy]);
-               }
+  }
+  myMean /= (m_nx * m_ny);
+  
+  double sqErrorSum = 0.;
+  double absErrorSum = 0.;
+  double sqDiffFromMeanSum = 0.;
+  double absValueSum = 0.;
+  for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
+    for (unsigned int iy = 0; iy < m_ny; iy++) {
+      double diff = v[ix2][iy] - vComp[ix2][iy];
+      sqErrorSum += diff * diff;
+      absErrorSum += fabs(diff);
+      double diffFromMean = v[ix2][iy] - myMean;
+      sqDiffFromMeanSum += diffFromMean * diffFromMean;
+      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;
-    for (int ix3 = 0; ix3 < hx; ix3++) {
-               for (int iy = 0; iy < hy; iy++) {
-                       double avgPixel = 0.25 * (v[2*ix3][2*iy] + v[2*ix3+1][2*iy] + v[2*ix3][2*iy+1] + v[2*ix3+1][2*iy+1]);
-                       double avgPixelComp = 0.25 * (vComp[2*ix3][2*iy] + vComp[2*ix3+1][2*iy] + vComp[2*ix3][2*iy+1] + vComp[2*ix3+1][2*iy+1]);
-                       double error = fabs (avgPixel - avgPixelComp);
-                       if (error > eMax)
-                               eMax = error;
-               }
+  }
+  
+  d = ::sqrt (sqErrorSum / sqDiffFromMeanSum);
+  r = absErrorSum / absValueSum;
+  
+  int hx = m_nx / 2;
+  int hy = m_ny / 2;
+  double eMax = -1;
+  for (int ix3 = 0; ix3 < hx; ix3++) {
+    for (int iy = 0; iy < hy; iy++) {
+      double avgPixel = 0.25 * (v[2*ix3][2*iy] + v[2*ix3+1][2*iy] + v[2*ix3][2*iy+1] + v[2*ix3+1][2*iy+1]);
+      double avgPixelComp = 0.25 * (vComp[2*ix3][2*iy] + vComp[2*ix3+1][2*iy] + vComp[2*ix3][2*iy+1] + vComp[2*ix3+1][2*iy+1]);
+      double error = fabs (avgPixel - avgPixelComp);
+      if (error > eMax)
+        eMax = error;
     }
-       
-    e = eMax;
-       
-    return true;
+  }
+  
+  e = eMax;
+  
+  return true;
 }
 
 
 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;
-               os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
-               return true;
-       }
-       return false;
+  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;
+    os << "Worst case distance (2x2 pixel average) (e): " << e << std::endl;
+    return true;
+  }
+  return false;
 }
 
 
 void
 ImageFile::printStatistics (std::ostream& os) const
 {
-    double min, max, mean, mode, median, stddev;
-       
-    statistics (min, max, mean, mode, median, stddev);\r
-    if (dataType() == Array2dFile::DATA_TYPE_COMPLEX)\r
-      os << "Real Component Statistics" << std::endl;\r
-
-    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;\r
-\r
-    if (dataType() == Array2dFile::DATA_TYPE_COMPLEX) {\r
-      statistics (getImaginaryArray(), min, max, mean, mode, median, stddev);\r
-      os << std::endl << "Imaginary Component Statistics" << std::endl;
+  double min, max, mean, mode, median, stddev;
+  
+  statistics (min, max, mean, mode, median, stddev);\r
+  if (isComplex())\r
+    os << "Real Component Statistics" << std::endl;\r
+  
+  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;\r
+  \r
+  if (isComplex()) {\r
+    statistics (getImaginaryArray(), min, max, mean, mode, median, stddev);\r
+    os << std::endl << "Imaginary Component Statistics" << std::endl;
     os << "   min: " << min << std::endl;\r
     os << "   max: " << max << std::endl;\r
     os << "  mean: " << mean << std::endl;\r
     os << "  mode: " << mode << std::endl;\r
     os << "median: " << median << std::endl;\r
     os << "stddev: " << stddev << std::endl;\r
-    }\r
+  }\r
 }
 
 
@@ -247,45 +267,45 @@ ImageFile::statistics (double& min, double& max, double& mean, double& mode, dou
 
 
 void\r
-ImageFile::statistics (ImageFileArrayConst& v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const\r
+ImageFile::statistics (ImageFileArrayConst v, double& min, double& max, double& mean, double& mode, double& median, double& stddev) const\r
 {\r
-    int nx = m_nx;\r
-    int ny = m_ny;\r
-    \r
-    if (v == NULL || nx == 0 || ny == 0)\r
-               return;\r
-\r
-       std::vector<double> vecImage;\r
-       int iVec = 0;\r
-       vecImage.resize (nx * ny);\r
-    for (int ix = 0; ix < nx; ix++) {\r
-               for (int iy = 0; iy < ny; iy++)\r
-                       vecImage[iVec++] = v[ix][iy];\r
-       }\r
-\r
-       vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);\r
+  int nx = m_nx;\r
+  int ny = m_ny;\r
+  \r
+  if (v == NULL || nx == 0 || ny == 0)\r
+    return;\r
+  \r
+  std::vector<double> vecImage;\r
+  int iVec = 0;\r
+  vecImage.resize (nx * ny);\r
+  for (int ix = 0; ix < nx; ix++) {\r
+    for (int iy = 0; iy < ny; iy++)\r
+      vecImage[iVec++] = v[ix][iy];\r
+  }\r
+  \r
+  vectorNumericStatistics (vecImage, nx * ny, min, max, mean, mode, median, stddev);\r
 }\r
 \r
 void
 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++) {
-               for (int iy = 0; iy < ny; iy++) {
-                       if (v[ix][iy] > max)
-                               max = v[ix][iy];
-                       if (v[ix][iy] < min)
-                               min = v[ix][iy];
-               }
+  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++) {
+    for (int iy = 0; iy < ny; iy++) {
+      if (v[ix][iy] > max)
+        max = v[ix][iy];
+      if (v[ix][iy] < min)
+        min = v[ix][iy];
     }
+  }
 }
 \r
 bool\r
@@ -293,17 +313,17 @@ ImageFile::convertRealToComplex ()
 {\r
   if (dataType() != Array2dFile::DATA_TYPE_REAL)\r
     return false;\r
-\r
+  \r
   if (! reallocRealToComplex())\r
     return false;\r
-\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
+  \r
   return true;\r
 }\r
 \r
@@ -312,19 +332,19 @@ ImageFile::convertComplexToReal ()
 {\r
   if (dataType() != Array2dFile::DATA_TYPE_COMPLEX)\r
     return false;\r
-\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
-      std::complex<double> c (*vRealCol, *vImagCol);\r
+      CTSimComplex c (*vRealCol, *vImagCol);\r
       *vRealCol++ = std::abs (c);\r
       vImagCol++;\r
     }\r
   }\r
-\r
+  \r
   return reallocComplexToReal();\r
 }\r
 \r
@@ -335,20 +355,20 @@ ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
   ImageFileArrayConst vRHS = rRHS.getArray();\r
   ImageFileArray vResult = result.getArray();\r
-\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
+      *out++ = *in1++ - *in2++;\r
   }\r
-\r
-    return true;\r
+  \r
+  return true;\r
 }\r
 
 bool\r
@@ -358,20 +378,20 @@ ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
   ImageFileArrayConst vRHS = rRHS.getArray();\r
   ImageFileArray vResult = result.getArray();\r
-\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
+      *out++ = *in1++ + *in2++;\r
   }\r
-\r
-    return true;\r
+  \r
+  return true;\r
 }\r
 \r
 bool\r
@@ -381,20 +401,20 @@ ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
   ImageFileArrayConst vRHS = rRHS.getArray();\r
   ImageFileArray vResult = result.getArray();\r
-\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
+      *out++ = *in1++ * *in2++;\r
   }\r
-\r
-    return true;\r
+  \r
+  return true;\r
 }\r
 \r
 bool\r
@@ -404,11 +424,11 @@ ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::subtractImage]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
   ImageFileArrayConst vRHS = rRHS.getArray();\r
   ImageFileArray vResult = result.getArray();\r
-\r
+  \r
   for (unsigned int ix = 0; ix < m_nx; ix++) {\r
     ImageFileColumnConst in1 = vLHS[ix];\r
     ImageFileColumnConst in2 = vRHS[ix];\r
@@ -420,8 +440,8 @@ ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
         *out++ = 0;\r
     }\r
   }\r
-\r
-    return true;\r
+  \r
+  return true;\r
 }\r
 \r
 \r
@@ -432,17 +452,17 @@ ImageFile::invertPixelValues (ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
   ImageFileArray vResult = result.getArray();\r
-\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
+      *out++ = - *in++;\r
   }\r
-\r
+  \r
   return true;\r
 }\r
 \r
@@ -453,10 +473,10 @@ ImageFile::sqrt (ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
   ImageFileArray vResult = result.getArray();\r
-\r
+  \r
   for (unsigned int ix = 0; ix < m_nx; ix++) {\r
     ImageFileColumnConst in = vLHS[ix];\r
     ImageFileColumn out = vResult[ix];\r
@@ -466,7 +486,7 @@ ImageFile::sqrt (ImageFile& result) const
       else\r
         *out++ = ::sqrt(*in++);\r
   }\r
-\r
+  \r
   return true;\r
 }\r
 \r
@@ -477,10 +497,10 @@ ImageFile::log (ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
   ImageFileArray vResult = result.getArray();\r
-\r
+  \r
   for (unsigned int ix = 0; ix < m_nx; ix++) {\r
     ImageFileColumnConst in = vLHS[ix];\r
     ImageFileColumn out = vResult[ix];\r
@@ -490,7 +510,7 @@ ImageFile::log (ImageFile& result) const
       else\r
         *out++ = ::log(*in++);\r
   }\r
-\r
+  \r
   return true;\r
 }\r
 \r
@@ -501,33 +521,153 @@ ImageFile::exp (ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
   ImageFileArray vResult = result.getArray();\r
-\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
+  \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::inverseFourier (ImageFile& result) const\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::fourier (ImageFile& result) const\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
@@ -539,62 +679,230 @@ ImageFile::fourier (ImageFile& result) const
       return false;\r
   }\r
 \r
-  ImageFileArrayConst vLHS = getArray();\r
+  ImageFileArrayConst vReal = getArray();\r
+  ImageFileArrayConst vImag = getImaginaryArray();\r
   ImageFileArray vRealResult = result.getArray();\r
   ImageFileArray vImagResult = result.getImaginaryArray();\r
-\r
   unsigned int ix, iy;\r
-  double* pY = new double [m_ny];\r
-  std::complex<double>** complexOut = new std::complex<double>* [m_nx];\r
   for (ix = 0; ix < m_nx; ix++)\r
-    complexOut[ix] = new std::complex<double> [m_ny];\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
+    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
-  delete pY;\r
-\r
-  std::complex<double>* pX = new std::complex<double> [m_nx];\r
-  std::complex<double>* complexOutCol = new std::complex<double> [m_nx];\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, complexOutCol, m_nx, ProcessSignal::FORWARD);\r
+    ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);\r
     for (ix = 0; ix < m_nx; ix++)\r
-      complexOut[ix][iy] = complexOutCol[ix];\r
+      complexOut[ix][iy] = complexOutRow[ix];\r
   }\r
   delete [] pX;\r
-\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
-      complexOutCol[ix] = complexOut[ix][iy];\r
-    ProcessSignal::shuffleFourierToNaturalOrder (complexOutCol, m_nx);;\r
+      pComplexRow[ix] = complexOut[ix][iy];\r
+    ProcessSignal::shuffleNaturalToFourierOrder (pComplexRow, m_nx);\r
     for (ix = 0; ix < m_nx; ix++)\r
-      complexOut[ix][iy] = complexOutCol[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 [] complexOutCol;\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
+    \r
+  // delete complexOut matrix\r
   for (ix = 0; ix < m_nx; ix++)\r
     delete [] complexOut[ix];\r
-\r
   delete [] complexOut;\r
-\r
+    \r
   return true;\r
 }\r
 \r
+\r
 bool\r
 ImageFile::magnitude (ImageFile& result) const\r
 {\r
@@ -602,29 +910,23 @@ ImageFile::magnitude (ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArray vReal = getArray();\r
-  ImageFileArray vImag = NULL;\r
-  if (dataType() == Array2dFile::DATA_TYPE_COMPLEX)\r
-    vImag = getImaginaryArray();\r
-\r
+  ImageFileArray vImag = getImaginaryArray();\r
+  \r
   ImageFileArray vRealResult = result.getArray();\r
-  if (result.dataType() == Array2dFile::DATA_TYPE_COMPLEX) {\r
-    ImageFileArray vImagResult = result.getImaginaryArray();\r
-    for (unsigned int ix = 0; ix < m_nx; ix++)\r
-      for (unsigned int iy = 0; iy < m_ny; iy++)\r
-        vImagResult[ix][iy] = 0;\r
-  }\r
-\r
   for (unsigned int ix = 0; ix < m_nx; ix++)\r
     for (unsigned int iy = 0; iy < m_ny; iy++) {\r
-      if (dataType() == Array2dFile::DATA_TYPE_COMPLEX\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
-  return true;\r
+  if (result.isComplex())\r
+    result.reallocComplexToReal();\r
+  \r
+    return true;\r
 }\r
 \r
 bool\r
@@ -634,28 +936,22 @@ ImageFile::phase (ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArray vReal = getArray();\r
-  ImageFileArray vImag = NULL;\r
-  if (dataType() == Array2dFile::DATA_TYPE_COMPLEX)\r
-    vImag = getImaginaryArray();\r
-\r
+  ImageFileArray vImag = getImaginaryArray();\r
+  \r
   ImageFileArray vRealResult = result.getArray();\r
-  if (result.dataType() == Array2dFile::DATA_TYPE_COMPLEX) {\r
-    ImageFileArray vImagResult = result.getImaginaryArray();\r
-    for (unsigned int ix = 0; ix < m_nx; ix++)\r
-      for (unsigned int iy = 0; iy < m_ny; iy++)\r
-        vImagResult[ix][iy] = 0;\r
-  }\r
-\r
   for (unsigned int ix = 0; ix < m_nx; ix++)\r
     for (unsigned int iy = 0; iy < m_ny; iy++) {\r
-      if (dataType() == Array2dFile::DATA_TYPE_COMPLEX) \r
-        vRealResult[ix][iy] = ::atan (vImag[ix][iy] / vReal[ix][iy]);\r
+      if (isComplex())\r
+        vRealResult[ix][iy] = ::atan2 (vImag[ix][iy], vReal[ix][iy]);\r
       else\r
-        vRealResult[ix][iy] = vReal[ix][iy];\r
+        vRealResult[ix][iy] = 0;\r
     }\r
-\r
+    \r
+  if (result.isComplex())\r
+    result.reallocComplexToReal();\r
+  \r
   return true;\r
 }\r
 \r
@@ -666,19 +962,19 @@ ImageFile::square (ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
     return false;\r
   }\r
-\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
   ImageFileArray vResult = result.getArray();\r
-\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
+      *out++ = *in * *in;\r
+      in++;\r
     }\r
   }\r
-\r
+  \r
   return true;\r
 }\r
 \r
@@ -686,74 +982,74 @@ ImageFile::square (ImageFile& result) const
 void 
 ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
 {
-       FILE *fp;
-       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;
-       
-       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;
-                       double dens = (v[icol][irow] - densmin) / (densmax - densmin);
-                       dens = clamp (dens, 0., 1.);
-                       for (int p = pos; p < pos + nxcell; p++) {
-                               rowp[p] = static_cast<unsigned int> (dens * 255.);
-                       }
-               }
-               for (int ir = 0; ir < nycell; ir++) {
-                       for (int ic = 0; ic < nx * nxcell; ic++) 
-                               fputc( rowp[ic], fp );
-               }
-       }
-       \r
-       delete rowp;
-       fclose(fp);
+  FILE *fp;
+  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;
+  
+  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;
+      double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+      dens = clamp (dens, 0., 1.);
+      for (int p = pos; p < pos + nxcell; p++) {
+        rowp[p] = static_cast<unsigned int> (dens * 255.);
+      }
+    }
+    for (int ir = 0; ir < nycell; ir++) {
+      for (int ic = 0; ic < nx * nxcell; ic++) 
+        fputc( rowp[ic], fp );
+    }
+  }
+  \r
+  delete rowp;
+  fclose(fp);
 }
 
 void 
 ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
 {
-       FILE *fp;
-       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;
-       
-       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;
-                       double dens = (v[icol][irow] - densmin) / (densmax - densmin);
-                       dens = clamp (dens, 0., 1.);
-                       for (int p = pos; p < pos + nxcell; p++) {
-                               rowp[p] = static_cast<unsigned int> (dens * 255.);
-                       }
-               }
-               for (int ir = 0; ir < nycell; ir++) {
-                       for (int ic = 0; ic < nx * nxcell; ic++) 
-                               fprintf(fp, "%d ", rowp[ic]);
-                       fprintf(fp, "\n");
-               }
-       }
-       \r
-       delete rowp;
-       fclose(fp);
+  FILE *fp;
+  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;
+  
+  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;
+      double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+      dens = clamp (dens, 0., 1.);
+      for (int p = pos; p < pos + nxcell; p++) {
+        rowp[p] = static_cast<unsigned int> (dens * 255.);
+      }
+    }
+    for (int ir = 0; ir < nycell; ir++) {
+      for (int ic = 0; ic < nx * nxcell; ic++) 
+        fprintf(fp, "%d ", rowp[ic]);
+      fprintf(fp, "\n");
+    }
+  }
+  \r
+  delete rowp;
+  fclose(fp);
 }
 
 
@@ -761,69 +1057,69 @@ ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, doub
 void 
 ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
 {
-       FILE *fp;
-       png_structp png_ptr;
-       png_infop info_ptr;
-       double max_out_level = (1 << bitdepth) - 1;
-       int nx = m_nx;
-       int ny = m_ny;
-       ImageFileArray v = getArray();
-       
-       unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
-       
-       if ((fp = fopen (outfile, "wb")) == NULL)
-               return;
-       
-       png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-       if (! png_ptr)
-               return;
-       
-       info_ptr = png_create_info_struct(png_ptr);
-       if (! info_ptr) {
-               png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
-               fclose(fp);
-               return;
-       }
-       
-       if (setjmp(png_ptr->jmpbuf)) {
-               png_destroy_write_struct(&png_ptr, &info_ptr);
-               fclose(fp);
-               return;
-       }
-       
-       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_BASE, 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;
-                               else {
-                                       int rowpos = p * 2;
-                                       rowp[rowpos] = (outval >> 8) & 0xFF;
-                                       rowp[rowpos+1] = (outval & 0xFF);
-                               }
-                       }
-               }
-               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;\r
-       
-       fclose(fp);
+  FILE *fp;
+  png_structp png_ptr;
+  png_infop info_ptr;
+  double max_out_level = (1 << bitdepth) - 1;
+  int nx = m_nx;
+  int ny = m_ny;
+  ImageFileArray v = getArray();
+  
+  unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
+  
+  if ((fp = fopen (outfile, "wb")) == NULL)
+    return;
+  
+  png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+  if (! png_ptr)
+    return;
+  
+  info_ptr = png_create_info_struct(png_ptr);
+  if (! info_ptr) {
+    png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
+    fclose(fp);
+    return;
+  }
+  
+  if (setjmp(png_ptr->jmpbuf)) {
+    png_destroy_write_struct(&png_ptr, &info_ptr);
+    fclose(fp);
+    return;
+  }
+  
+  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_BASE, 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;
+        else {
+          int rowpos = p * 2;
+          rowp[rowpos] = (outval >> 8) & 0xFF;
+          rowp[rowpos+1] = (outval & 0xFF);
+        }
+      }
+    }
+    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;\r
+  
+  fclose(fp);
 }
 #endif
 
@@ -834,44 +1130,44 @@ static const int N_GRAYSCALE=256;
 void
 ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
 {
-       gdImagePtr gif;
-       FILE *out;
-       int gs_indices[N_GRAYSCALE];
-       int nx = m_nx;
-       int ny = m_ny;
-       ImageFileArray v = getArray();
-       
-       unsigned char rowp [nx * nxcell];
-       if (rowp == NULL)
-               return;
-       
-       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;
-               for (int ir = rpos; ir < rpos + nycell; ir++) {
-                       for (int icol = 0; icol < nx; icol++) {
-                               int cpos = icol * nxcell;
-                               double dens = (v[icol][irow] - densmin) / (densmax - densmin);
-                               dens = clamp(dens, 0., 1.);
-                               for (int ic = cpos; ic < cpos + nxcell; ic++) {
-                                       rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
-                                       gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
-                               }
-                       }
-               }
-       }
-       
-       if ((out = fopen(outfile,"w")) == NULL) {
-               sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);
-               return (1);
-       }
-       gdImageGif(gif,out);
-       fclose(out);
-       gdImageDestroy(gif);
+  gdImagePtr gif;
+  FILE *out;
+  int gs_indices[N_GRAYSCALE];
+  int nx = m_nx;
+  int ny = m_ny;
+  ImageFileArray v = getArray();
+  
+  unsigned char rowp [nx * nxcell];
+  if (rowp == NULL)
+    return;
+  
+  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;
+    for (int ir = rpos; ir < rpos + nycell; ir++) {
+      for (int icol = 0; icol < nx; icol++) {
+        int cpos = icol * nxcell;
+        double dens = (v[icol][irow] - densmin) / (densmax - densmin);
+        dens = clamp(dens, 0., 1.);
+        for (int ic = cpos; ic < cpos + nxcell; ic++) {
+          rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
+          gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
+        }
+      }
+    }
+  }
+  
+  if ((out = fopen(outfile,"w")) == NULL) {
+    sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);
+    return (1);
+  }
+  gdImageGif(gif,out);
+  fclose(out);
+  gdImageDestroy(gif);
 }
 #endif