r334: *** empty log message ***
[ctsim.git] / libctsim / imagefile.cpp
index df647ce930efad8de0b4a8288f14c99499fc71bb..c21c17f6858facaa7114cd9a92e90b9667dbc35b 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: imagefile.cpp,v 1.28 2001/01/01 10:14:34 kevin Exp $
+**  $Id: imagefile.cpp,v 1.31 2001/01/02 08:15:02 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
 
 #include "ct.h"
 
+const int ImageFile::FORMAT_INVALID = -1;\r
+const int ImageFile::FORMAT_PGM = 0;\r
+const int ImageFile::FORMAT_PGMASCII = 1;\r
+#ifdef HAVE_PNG\r
+const int ImageFile::FORMAT_PNG = 2;\r
+const int ImageFile::FORMAT_PNG16 = 3;\r
+#endif\r
+\r
+const char* ImageFile::s_aszFormatName[] = \r
+{\r
+  {"pgm"},\r
+  {"pgmascii"},\r
+#ifdef HAVE_PNG\r
+  {"png"},\r
+  {"png16"},\r
+#endif\r
+};\r
+\r
+const char* ImageFile::s_aszFormatTitle[] = \r
+{\r
+  {"PGM"},\r
+  {"PGM ASCII"},\r
+  {"PNG"},\r
+  {"PNG 16-bit"},\r
+};\r
+\r
+const int ImageFile::s_iFormatCount = sizeof(s_aszFormatName) / sizeof(const char*);\r
+\r
+\r
 
 F32Image::F32Image (int nx, int ny, int dataType)\r
 : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32, dataType)\r
@@ -55,11 +84,11 @@ F64Image::F64Image (void)
 }\r
 
 void 
-ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
+ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param, double dInputScale, double dOutputScale)
 {\r
   ImageFileArray v = getArray();\r
   SignalFilter filter (filterName, domainName, bw, filt_param);\r
-\r
+  \r
 #if 1\r
   int iXCenter, iYCenter;\r
   if (isEven (m_nx))\r
@@ -70,24 +99,24 @@ ImageFile::filterResponse (const char* const domainName, double bw, const char*
     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
+  for (unsigned int ix = 0; ix < m_nx; ix++)\r
+    for (unsigned 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)) * dInputScale;\r
+      v[ix][iy] = filter.response (r) * dOutputScale;\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);
-    }
-  }\r
+    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);
+      }
+    }\r
 #endif
 }
 
@@ -356,16 +385,27 @@ ImageFile::subtractImages (const ImageFile& rRHS, ImageFile& result) const
     return false;\r
   }\r
   \r
+  if (isComplex() || rRHS.isComplex() && ! result.isComplex())\r
+    result.convertRealToComplex();\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
+  ImageFileArrayConst vLHSImag = getImaginaryArray();\r
   ImageFileArrayConst vRHS = rRHS.getArray();\r
+  ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();\r
   ImageFileArray vResult = result.getArray();\r
+  ImageFileArray vResultImag = result.getImaginaryArray();\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
+    for (unsigned int iy = 0; iy < m_ny; iy++) {\r
+      vResult[ix][iy] = vLHS[ix][iy] - vRHS[ix][iy];\r
+      if (result.isComplex()) {\r
+        vResultImag[ix][iy] = 0;\r
+        if (isComplex())\r
+          vResultImag[ix][iy] += vLHSImag[ix][iy];\r
+        if (rRHS.isComplex())\r
+          vResultImag[ix][iy] -= vRHSImag[ix][iy];\r
+      }\r
+    }\r
   }\r
   \r
   return true;\r
@@ -379,16 +419,27 @@ ImageFile::addImages (const ImageFile& rRHS, ImageFile& result) const
     return false;\r
   }\r
   \r
+  if (isComplex() || rRHS.isComplex() && ! result.isComplex())\r
+    result.convertRealToComplex();\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
+  ImageFileArrayConst vLHSImag = getImaginaryArray();\r
   ImageFileArrayConst vRHS = rRHS.getArray();\r
+  ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();\r
   ImageFileArray vResult = result.getArray();\r
+  ImageFileArray vResultImag = result.getImaginaryArray();\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
+    for (unsigned int iy = 0; iy < m_ny; iy++) {\r
+      vResult[ix][iy] = vLHS[ix][iy] + vRHS[ix][iy];\r
+      if (result.isComplex()) {\r
+        vResultImag[ix][iy] = 0;\r
+        if (isComplex())\r
+          vResultImag[ix][iy] += vLHSImag[ix][iy];\r
+        if (rRHS.isComplex())\r
+          vResultImag[ix][iy] += vRHSImag[ix][iy];\r
+      }\r
+    }\r
   }\r
   \r
   return true;\r
@@ -402,18 +453,36 @@ ImageFile::multiplyImages (const ImageFile& rRHS, ImageFile& result) const
     return false;\r
   }\r
   \r
+  if (isComplex() || rRHS.isComplex() && ! result.isComplex())\r
+    result.convertRealToComplex();\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
+  ImageFileArrayConst vLHSImag = getImaginaryArray();\r
   ImageFileArrayConst vRHS = rRHS.getArray();\r
+  ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();\r
   ImageFileArray vResult = result.getArray();\r
+  ImageFileArray vResultImag = result.getImaginaryArray();\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
+    for (unsigned int iy = 0; iy < m_ny; iy++) {\r
+      if (result.isComplex()) {\r
+        double dImag = 0;\r
+        if (isComplex())\r
+          dImag = vLHSImag[ix][iy];\r
+        std::complex<double> cLHS (vLHS[ix][iy], dImag);\r
+        dImag = 0;\r
+        if (rRHS.isComplex())\r
+          dImag = vRHSImag[ix][iy];\r
+        std::complex<double> cRHS (vRHS[ix][iy], dImag);\r
+        std::complex<double> cResult = cLHS * cRHS;\r
+        vResult[ix][iy] = cResult.real();\r
+        vResultImag[ix][iy] = cResult.imag();\r
+      } else\r
+        vResult[ix][iy] = vLHS[ix][iy] * vRHS[ix][iy];\r
+    }\r
   }\r
   \r
+  \r
   return true;\r
 }\r
 \r
@@ -425,19 +494,36 @@ ImageFile::divideImages (const ImageFile& rRHS, ImageFile& result) const
     return false;\r
   }\r
   \r
+  if (isComplex() || rRHS.isComplex() && ! result.isComplex())\r
+    result.convertRealToComplex();\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
+  ImageFileArrayConst vLHSImag = getImaginaryArray();\r
   ImageFileArrayConst vRHS = rRHS.getArray();\r
+  ImageFileArrayConst vRHSImag = rRHS.getImaginaryArray();\r
   ImageFileArray vResult = result.getArray();\r
+  ImageFileArray vResultImag = result.getImaginaryArray();\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
-      if (*in2 != 0.)\r
-        *out++ = *in1++ / *in2++;\r
-      else\r
-        *out++ = 0;\r
+      if (result.isComplex()) {\r
+        double dImag = 0;\r
+        if (isComplex())\r
+          dImag = vLHSImag[ix][iy];\r
+        std::complex<double> cLHS (vLHS[ix][iy], dImag);\r
+        dImag = 0;\r
+        if (rRHS.isComplex())\r
+          dImag = vRHSImag[ix][iy];\r
+        std::complex<double> cRHS (vRHS[ix][iy], dImag);\r
+        std::complex<double> cResult = cLHS / cRHS;\r
+        vResult[ix][iy] = cResult.real();\r
+        vResultImag[ix][iy] = cResult.imag();\r
+      } else {\r
+        if (vRHS != 0)\r
+          vResult[ix][iy] = vLHS[ix][iy] / vRHS[ix][iy];\r
+        else\r
+          vResult[ix][iy] = 0;\r
+      }\r
     }\r
   }\r
   \r
@@ -453,6 +539,9 @@ ImageFile::invertPixelValues (ImageFile& result) const
     return false;\r
   }\r
   \r
+  if (isComplex() && ! result.isComplex())\r
+    result.convertRealToComplex();\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
   ImageFileArray vResult = result.getArray();\r
   \r
@@ -474,20 +563,41 @@ ImageFile::sqrt (ImageFile& result) const
     return false;\r
   }\r
   \r
-  ImageFileArrayConst vLHS = getArray();\r
-  ImageFileArray vResult = result.getArray();\r
+  if (isComplex() && ! result.isComplex())\r
+    result.convertRealToComplex();\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
-      if (*in < 0)\r
-        *out++ = -::sqrt(-*in++);\r
-      else\r
-        *out++ = ::sqrt(*in++);\r
-  }\r
-  \r
-  return true;\r
+  bool bComplexOutput = result.isComplex();\r
+  ImageFileArrayConst vLHS = getArray();\r
+  if (! bComplexOutput)   // check if should convert to complex output\r
+    for (unsigned int ix = 0; ix < m_nx; ix++)\r
+      for (unsigned int iy = 0; iy < m_ny; iy++)\r
+        if (! bComplexOutput && vLHS[ix][iy] < 0) {\r
+          result.convertRealToComplex();\r
+          bComplexOutput = true;\r
+          break;\r
+        }\r
+        \r
+        ImageFileArrayConst vLHSImag = getImaginaryArray();\r
+        ImageFileArray vResult = result.getArray();\r
+        ImageFileArray vResultImag = result.getImaginaryArray();\r
+        \r
+        for (unsigned int ix = 0; ix < m_nx; ix++) {\r
+          for (unsigned int iy = 0; iy < m_ny; iy++) {\r
+            if (result.isComplex()) {\r
+              double dImag = 0;\r
+              if (isComplex())\r
+                dImag = vLHSImag[ix][iy];\r
+              std::complex<double> cLHS (vLHS[ix][iy], dImag);\r
+              std::complex<double> cResult = std::sqrt(cLHS);\r
+              vResult[ix][iy] = cResult.real();\r
+              vResultImag[ix][iy] = cResult.imag();\r
+            } else\r
+              vResult[ix][iy] = ::sqrt (vLHS[ix][iy]);\r
+          }\r
+        }\r
+        \r
+        \r
+        return true;\r
 }\r
 \r
 bool\r
@@ -498,19 +608,30 @@ ImageFile::log (ImageFile& result) const
     return false;\r
   }\r
   \r
+  if (isComplex() && ! result.isComplex())\r
+    result.convertRealToComplex();\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
+  ImageFileArrayConst vLHSImag = getImaginaryArray();\r
   ImageFileArray vResult = result.getArray();\r
+  ImageFileArray vResultImag = result.getImaginaryArray();\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
-      if (*in <= 0)\r
-        *out++ = 0;\r
-      else\r
-        *out++ = ::log(*in++);\r
+    for (unsigned int iy = 0; iy < m_ny; iy++) {\r
+      if (result.isComplex()) {\r
+        double dImag = 0;\r
+        if (isComplex())\r
+          dImag = vLHSImag[ix][iy];\r
+        std::complex<double> cLHS (vLHS[ix][iy], dImag);\r
+        std::complex<double> cResult = std::log (cLHS);\r
+        vResult[ix][iy] = cResult.real();\r
+        vResultImag[ix][iy] = cResult.imag();\r
+      } else\r
+        vResult[ix][iy] = ::log (vLHS[ix][iy]);\r
+    }\r
   }\r
   \r
+  \r
   return true;\r
 }\r
 \r
@@ -522,94 +643,87 @@ ImageFile::exp (ImageFile& result) const
     return false;\r
   }\r
   \r
+  if (isComplex() && ! result.isComplex())\r
+    result.convertRealToComplex();\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
+  ImageFileArrayConst vLHSImag = getImaginaryArray();\r
   ImageFileArray vResult = result.getArray();\r
+  ImageFileArray vResultImag = result.getImaginaryArray();\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
+    for (unsigned int iy = 0; iy < m_ny; iy++) {\r
+      if (result.isComplex()) {\r
+        double dImag = 0;\r
+        if (isComplex())\r
+          dImag = vLHSImag[ix][iy];\r
+        std::complex<double> cLHS (vLHS[ix][iy], dImag);\r
+        std::complex<double> cResult = std::exp (cLHS);\r
+        vResult[ix][iy] = cResult.real();\r
+        vResultImag[ix][iy] = cResult.imag();\r
+      } else\r
+        vResult[ix][iy] = ::exp (vLHS[ix][iy]);\r
+    }\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
+bool\r
+ImageFile::scaleImage (ImageFile& result) const\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
+  unsigned int nx = m_nx;\r
+  unsigned int ny = m_ny;\r
+  unsigned int newNX = result.nx();\r
+  unsigned int newNY = result.ny();\r
+  \r
+  double dXScale = static_cast<double>(newNX) / static_cast<double>(nx);\r
+  double dYScale = static_cast<double>(newNY) / static_cast<double>(ny);\r
+  \r
+  if (isComplex() && ! result.isComplex())\r
+    result.convertRealToComplex();\r
+  \r
+  ImageFileArrayConst vReal = getArray();\r
+  ImageFileArrayConst vImag = getImaginaryArray();\r
+  ImageFileArray vResult = result.getArray();\r
+  ImageFileArray vResultImag = result.getImaginaryArray();\r
+  \r
+  for (unsigned int ix = 0; ix < newNX; ix++) {\r
+    for (unsigned int iy = 0; iy < newNY; iy++) {\r
+      double dXPos = ix / dXScale;\r
+      double dYPos = iy / dYScale;\r
+      unsigned int scaleNX = static_cast<unsigned int> (dXPos);\r
+      unsigned int scaleNY = static_cast<unsigned int> (dYPos);\r
+      double dXFrac = dXPos - scaleNX;\r
+      double dYFrac = dYPos - scaleNY;\r
+      if (scaleNX >= nx - 1 || scaleNY >= ny - 1) {\r
+        vResult[ix][iy] = vReal[scaleNX][scaleNY];\r
+        if (result.isComplex()) {\r
+          if (isComplex())\r
+            vResultImag[ix][iy] = vImag[scaleNX][scaleNY];\r
+          else\r
+            vResultImag[ix][iy] = 0;\r
+        }\r
+      } else {\r
+        vResult[ix][iy] = vReal[scaleNX][scaleNY] + \r
+          dXFrac * (vReal[scaleNX+1][scaleNY] - vReal[scaleNX][scaleNY]) + \r
+          dYFrac * (vReal[scaleNX][scaleNY+1] - vReal[scaleNX][scaleNY]);\r
+        if (result.isComplex()) {\r
+          if (isComplex())\r
+            vResultImag[ix][iy] = vImag[scaleNX][scaleNY] + \r
+            dXFrac * (vImag[scaleNX+1][scaleNY] - vImag[scaleNX][scaleNY]) + \r
+            dYFrac * (vImag[scaleNX][scaleNY+1] - vImag[scaleNX][scaleNY]);\r
+          else\r
+            vResultImag[ix][iy] = 0;\r
+        }\r
+      }\r
     }\r
   }\r
-  delete [] pRow;\r
+  \r
+  return true;\r
 }\r
 \r
\r
-}; // namespace ProcessIamge\r
-\r
 #ifdef HAVE_FFTW\r
 bool\r
 ImageFile::fft (ImageFile& result) const\r
@@ -618,17 +732,17 @@ ImageFile::fft (ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
     return false;\r
   }\r
-\r
+  \r
   if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {\r
-    if (! result.reallocRealToComplex ())\r
+    if (! result.convertRealToComplex ())\r
       return false;\r
   }\r
-\r
+  \r
   fftw_complex* in = new fftw_complex [m_nx * m_ny];\r
-\r
+  \r
   ImageFileArrayConst vReal = getArray();\r
   ImageFileArrayConst vImag = getImaginaryArray();\r
-\r
+  \r
   unsigned int ix, iy;\r
   unsigned int iArray = 0;\r
   for (ix = 0; ix < m_nx; ix++)\r
@@ -640,29 +754,29 @@ ImageFile::fft (ImageFile& result) const
         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
+    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
+      Fourier::shuffleFourierToNaturalOrder (result);\r
+      \r
+      return true;\r
 }\r
 \r
 \r
@@ -673,12 +787,12 @@ ImageFile::ifft (ImageFile& result) const
     sys_error (ERR_WARNING, "Difference sizes of images [ImageFile::invertPixelValues]");\r
     return false;\r
   }\r
-\r
+  \r
   if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {\r
-    if (! result.reallocRealToComplex ())\r
+    if (! result.convertRealToComplex ())\r
       return false;\r
   }\r
-\r
+  \r
   ImageFileArrayConst vReal = getArray();\r
   ImageFileArrayConst vImag = getImaginaryArray();\r
   ImageFileArray vRealResult = result.getArray();\r
@@ -692,41 +806,41 @@ ImageFile::ifft (ImageFile& result) const
       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
+    Fourier::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
+\r
 bool\r
 ImageFile::fourier (ImageFile& result) const\r
 {\r
@@ -735,85 +849,62 @@ ImageFile::fourier (ImageFile& result) const
     return false;\r
   }\r
   \r
-  if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {\r
-    if (! result.reallocRealToComplex ())\r
+  if (! result.isComplex())\r
+    if (! result.convertRealToComplex ())\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
-      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
-#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, complexOutRow, m_nx, ProcessSignal::FORWARD);\r
-    for (ix = 0; ix < m_nx; ix++)\r
-      complexOut[ix][iy] = complexOutRow[ix];\r
-  }\r
-  delete [] pX;\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
+    \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][iy] = complexOutRow[ix];\r
+      complexOut[ix] = new CTSimComplex [m_ny];\r
     \r
-  }\r
-  delete [] complexOutRow;\r
-  \r
-  for (ix = 0; ix < m_nx; ix++)\r
+    // fourier each x column\r
+    CTSimComplex* pY = new CTSimComplex [m_ny];\r
+    for (ix = 0; ix < m_nx; ix++) {\r
+      for (iy = 0; iy < m_ny; iy++) {\r
+        double dImag = 0;\r
+        if (isComplex())\r
+          dImag = vLHSImag[ix][iy];\r
+        pY[iy] = std::complex<double>(vLHS[ix][iy], dImag);\r
+      } \r
+      ProcessSignal::finiteFourierTransform (pY, complexOut[ix], m_ny,  ProcessSignal::FORWARD);\r
+    }\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
-      vRealResult[ix][iy] = complexOut[ix][iy].real();\r
-      vImagResult[ix][iy] = complexOut[ix][iy].imag();\r
+      for (ix = 0; ix < m_nx; ix++)\r
+        pX[ix] = complexOut[ix][iy];\r
+      ProcessSignal::finiteFourierTransform (pX, complexOutRow, m_nx, ProcessSignal::FORWARD);\r
+      for (ix = 0; ix < m_nx; ix++)\r
+        complexOut[ix][iy] = complexOutRow[ix];\r
     }\r
+    delete [] pX;\r
+    delete [] complexOutRow;\r
     \r
     for (ix = 0; ix < m_nx; ix++)\r
-      delete [] complexOut[ix];\r
-    delete [] complexOut;\r
-    \r
-    return true;\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
+      Fourier::shuffleFourierToNaturalOrder (result);\r
+      \r
+      // delete complexOut matrix\r
+      for (ix = 0; ix < m_nx; ix++)\r
+        delete [] complexOut[ix];\r
+      delete [] complexOut;\r
+      \r
+      return true;\r
 }\r
 \r
 bool\r
@@ -825,81 +916,68 @@ ImageFile::inverseFourier (ImageFile& result) const
   }\r
   \r
   if (result.dataType() == Array2dFile::DATA_TYPE_REAL) {\r
-    if (! result.reallocRealToComplex ())\r
+    if (! result.convertRealToComplex ())\r
       return false;\r
   }\r
   \r
   ImageFileArrayConst vLHSReal = getArray();\r
   ImageFileArrayConst vLHSImag = getImaginaryArray();\r
+  ImageFileArray vRealResult = result.getArray();\r
+  ImageFileArray vImagResult = result.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
+  \r
+  // put input image into result\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
+      vRealResult[ix][iy] = vLHSReal[ix][iy];\r
       if (isComplex())\r
-        complexOut[ix][iy].imag (vLHSImag[ix][iy]);\r
+        vImagResult[ix][iy] = vLHSImag[ix][iy];\r
       else\r
-        complexOut[ix][iy].imag (0);\r
+        vImagResult[ix][iy] = 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
-      pComplexRow[ix] = complexOut[ix][iy];\r
-    ProcessSignal::shuffleNaturalToFourierOrder (pComplexRow, m_nx);\r
-    for (ix = 0; ix < m_nx; 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 [] 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
+    Fourier::shuffleNaturalToFourierOrder (result);\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] = std::complex<double> (vRealResult[ix][iy], vImagResult[ix][iy]);\r
+      }\r
+      ProcessSignal::finiteFourierTransform (pCol, complexOut[ix], m_ny,  ProcessSignal::BACKWARD);\r
     }\r
+    delete [] pCol;\r
     \r
-  // delete complexOut matrix\r
-  for (ix = 0; ix < m_nx; ix++)\r
-    delete [] complexOut[ix];\r
-  delete [] complexOut;\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
-  return true;\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
+      // delete complexOut matrix\r
+      for (ix = 0; ix < m_nx; ix++)\r
+        delete [] complexOut[ix];\r
+      delete [] complexOut;\r
+      \r
+      return true;\r
 }\r
 \r
 \r
@@ -913,8 +991,8 @@ ImageFile::magnitude (ImageFile& result) const
   \r
   ImageFileArray vReal = getArray();\r
   ImageFileArray vImag = getImaginaryArray();\r
-  \r
   ImageFileArray vRealResult = result.getArray();\r
+  \r
   for (unsigned int ix = 0; ix < m_nx; ix++)\r
     for (unsigned int iy = 0; iy < m_ny; iy++) {\r
       if (isComplex()) \r
@@ -922,10 +1000,10 @@ ImageFile::magnitude (ImageFile& result) const
       else\r
         vRealResult[ix][iy] = vReal[ix][iy];\r
     }\r
-\r
-  if (result.isComplex())\r
-    result.reallocComplexToReal();\r
-  \r
+    \r
+    if (result.isComplex())\r
+      result.convertComplexToReal();\r
+    \r
     return true;\r
 }\r
 \r
@@ -939,8 +1017,8 @@ ImageFile::phase (ImageFile& result) const
   \r
   ImageFileArray vReal = getArray();\r
   ImageFileArray vImag = getImaginaryArray();\r
-  \r
   ImageFileArray vRealResult = result.getArray();\r
+  \r
   for (unsigned int ix = 0; ix < m_nx; ix++)\r
     for (unsigned int iy = 0; iy < m_ny; iy++) {\r
       if (isComplex())\r
@@ -949,10 +1027,10 @@ ImageFile::phase (ImageFile& result) const
         vRealResult[ix][iy] = 0;\r
     }\r
     \r
-  if (result.isComplex())\r
-    result.reallocComplexToReal();\r
-  \r
-  return true;\r
+    if (result.isComplex())\r
+      result.convertComplexToReal();\r
+    \r
+    return true;\r
 }\r
 \r
 bool\r
@@ -963,24 +1041,93 @@ ImageFile::square (ImageFile& result) const
     return false;\r
   }\r
   \r
+  if (isComplex() && ! result.isComplex())\r
+    result.convertRealToComplex();\r
+  \r
   ImageFileArrayConst vLHS = getArray();\r
+  ImageFileArrayConst vLHSImag = getImaginaryArray();\r
   ImageFileArray vResult = result.getArray();\r
+  ImageFileArray vResultImag = result.getImaginaryArray();\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
+      if (result.isComplex()) {\r
+        double dImag = 0;\r
+        if (isComplex())\r
+          dImag = vLHSImag[ix][iy];\r
+        std::complex<double> cLHS (vLHS[ix][iy], dImag);\r
+        std::complex<double> cResult = cLHS * cLHS;\r
+        vResult[ix][iy] = cResult.real();\r
+        vResultImag[ix][iy] = cResult.imag();\r
+      } else\r
+        vResult[ix][iy] = vLHS[ix][iy] * vLHS[ix][iy];\r
     }\r
   }\r
   \r
+  \r
   return true;\r
 }\r
 \r
+int\r
+ImageFile::convertFormatNameToID (const char* const formatName)\r
+{\r
+  int formatID = FORMAT_INVALID;\r
+  \r
+  for (int i = 0; i < s_iFormatCount; i++)\r
+    if (strcasecmp (formatName, s_aszFormatName[i]) == 0) {\r
+      formatID = i;\r
+      break;\r
+    }\r
+    \r
+    return (formatID);\r
+}\r
+\r
+const char*\r
+ImageFile::convertFormatIDToName (int formatID)\r
+{\r
+  static const char *formatName = "";\r
+  \r
+  if (formatID >= 0 && formatID < s_iFormatCount)\r
+    return (s_aszFormatName[formatID]);\r
+  \r
+  return (formatName);\r
+}\r
 \r
-void 
-ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
+const char*\r
+ImageFile::convertFormatIDToTitle (const int formatID)\r
+{\r
+  static const char *formatTitle = "";\r
+  \r
+  if (formatID >= 0 && formatID < s_iFormatCount)\r
+    return (s_aszFormatTitle[formatID]);\r
+  \r
+  return (formatTitle);\r
+}\r
+\r
+bool\r
+ImageFile::exportImage (const char* const pszFormat, const char* const pszFilename, int nxcell, int nycell, double densmin, double densmax)\r
+{\r
+  int iFormatID = convertFormatNameToID (pszFormat);\r
+  if (iFormatID == FORMAT_INVALID) {\r
+    sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);\r
+    return false;\r
+  }\r
+  \r
+  if (iFormatID == FORMAT_PGM)\r
+    return writeImagePGM (pszFilename, nxcell, nycell, densmin, densmax);\r
+  else if (iFormatID == FORMAT_PGMASCII)\r
+    return writeImagePGMASCII (pszFilename, nxcell, nycell, densmin, densmax);\r
+  else if (iFormatID == FORMAT_PNG)\r
+    return writeImagePNG (pszFilename, 8, nxcell, nycell, densmin, densmax);\r
+  else if (iFormatID == FORMAT_PNG16)\r
+    return writeImagePNG (pszFilename, 16, nxcell, nycell, densmin, densmax);\r
+  \r
+  sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat);\r
+  return false;\r
+}\r
+\r
+bool
+ImageFile::writeImagePGM (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
 {
   FILE *fp;
   int nx = m_nx;
@@ -990,7 +1137,7 @@ ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double de
   unsigned char* rowp = new unsigned char [nx * nxcell];
   
   if ((fp = fopen (outfile, "wb")) == NULL)
-    return;
+    return false;
   
   fprintf(fp, "P5\n");
   fprintf(fp, "%d %d\n", nx, ny);
@@ -1012,11 +1159,13 @@ ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double de
   }
   \r
   delete rowp;
-  fclose(fp);
+  fclose(fp);\r
+  \r
+  return true;
 }
 
-void 
-ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
+bool
+ImageFile::writeImagePGMASCII (const char* const outfile, int nxcell, int nycell, double densmin, double densmax)
 {
   FILE *fp;
   int nx = m_nx;
@@ -1026,7 +1175,7 @@ ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, doub
   unsigned char* rowp = new unsigned char [nx * nxcell];
   
   if ((fp = fopen (outfile, "wb")) == NULL)
-    return;
+    return false;
   
   fprintf(fp, "P2\n");
   fprintf(fp, "%d %d\n", nx, ny);
@@ -1049,17 +1198,16 @@ ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, doub
   }
   \r
   delete rowp;
-  fclose(fp);
+  fclose(fp);\r
+  \r
+  return true;
 }
 
 
 #ifdef HAVE_PNG
-void 
-ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
+bool
+ImageFile::writeImagePNG (const char* const 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;
@@ -1067,29 +1215,30 @@ ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nyc
   
   unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
   
-  if ((fp = fopen (outfile, "wb")) == NULL)
-    return;
+  FILE *fp = fopen (outfile, "wb");\r
+  if (! fp)
+    return false;
   
-  png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+  png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
   if (! png_ptr)
-    return;
+    return false;
   
-  info_ptr = png_create_info_struct(png_ptr);
+  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;
+    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;
+  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_BASE, PNG_FILTER_TYPE_DEFAULT);
+  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--) {
@@ -1115,11 +1264,13 @@ ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nyc
       png_write_rows (png_ptr, &row_pointer, 1);
   }
   
-  png_write_end(png_ptr, info_ptr);
-  png_destroy_write_struct(&png_ptr, &info_ptr);
+  png_write_end (png_ptr, info_ptr);
+  png_destroy_write_struct (&png_ptr, &info_ptr);
   delete rowp;\r
   
-  fclose(fp);
+  fclose(fp);\r
+  \r
+  return true;
 }
 #endif
 
@@ -1127,21 +1278,17 @@ ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nyc
 #include "gd.h"
 static const int N_GRAYSCALE=256;
 
-void
-ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax)
+bool
+ImageFile::writeImageGIF (const char* const 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;
+  unsigned char* rowp = new unsigned char [nx * nxcell];
   
-  gif = gdImageCreate(nx * nxcell, ny * nycell);
+  gdImagePtr gif = gdImageCreate(nx * nxcell, ny * nycell);
   for (int i = 0; i < N_GRAYSCALE; i++)
     gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
   
@@ -1161,13 +1308,18 @@ ImageFile::writeImageGIF (const char *outfile, int nxcell, int nycell, double de
     }
   }
   
-  if ((out = fopen(outfile,"w")) == NULL) {
+  FILE *out;\r
+  if ((out = fopen (outfile,"w")) == NULL) {
     sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);
-    return (1);
+    return false;
   }
   gdImageGif(gif,out);
   fclose(out);
-  gdImageDestroy(gif);
+  gdImageDestroy(gif);\r
+  \r
+  delete rowp;\r
+  \r
+  return true;
 }
 #endif