r256: *** empty log message ***
[ctsim.git] / libctsim / imagefile.cpp
index 1b1660dc0375653a4665d990612a11fc89e76140..a751573aef3e35ab0ee60b80c7c37d603228966b 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: imagefile.cpp,v 1.9 2000/07/15 08:36:13 kevin Exp $
+**  $Id: imagefile.cpp,v 1.19 2000/12/16 02:44:26 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"
 
 
+F32Image::F32Image (int nx, int ny)\r
+      : Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32)\r
+  {\r
+  }\r
+\r
+F32Image::F32Image (void)\r
+      : Array2dFile()\r
+  {\r
+      setPixelFormat (Array2dFile::PIXEL_FLOAT32);\r
+      setPixelSize (sizeof(kfloat32));\r
+  }\r
+\r
+F64Image::F64Image (int nx, int ny)\r
+      : Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64)\r
+  {\r
+  }\r
+\r
+F64Image::F64Image (void)\r
+      : Array2dFile ()\r
+  {\r
+      setPixelFormat (PIXEL_FLOAT64);\r
+      setPixelSize (sizeof(kfloat64));\r
+  }\r
 
 void 
 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
@@ -66,7 +89,7 @@ ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const Ima
       return 0;
 
 #if HAVE_G2_H
-    int pens [nx * ny * scale * scale ];
+    int* pPens = new int [nx * ny * scale * scale ];
 
     double view_scale = 255 / (pmax - pmin);
     int id_X11 = g2_open_X11 (nx * scale, ny * scale);
@@ -76,21 +99,26 @@ ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const Ima
        grayscale[i] = g2_ink (id_X11, cval, cval, cval);
     }
 
-    for (int i= 0, iy = ny - 1; iy >= 0; iy--) {
-       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;
-           pens[i++] = grayscale[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, ny, pens);
+    g2_image (id_X11, 0., 0., nx * scale, ny * scale, pPens);
 
+    delete pPens;
     return (id_X11);
-
+#else
+    return 0;
 #endif
 }
 
@@ -131,14 +159,14 @@ ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r,
     double absErrorSum = 0.;
     double sqDiffFromMeanSum = 0.;
     double absValueSum = 0.;
-    for (unsigned int ix = 0; ix < m_nx; ix++) {
+    for (unsigned int ix2 = 0; ix2 < m_nx; ix2++) {
        for (unsigned int iy = 0; iy < m_ny; iy++) {
-           double diff = v[ix][iy] - vComp[ix][iy];
+           double diff = v[ix2][iy] - vComp[ix2][iy];
            sqErrorSum += diff * diff;
            absErrorSum += fabs(diff);
-           double diffFromMean = v[ix][iy] - myMean;
+           double diffFromMean = v[ix2][iy] - myMean;
            sqDiffFromMeanSum += diffFromMean * diffFromMean;
-           absValueSum += fabs(v[ix][iy]);
+           absValueSum += fabs(v[ix2][iy]);
        }
     }
 
@@ -148,10 +176,10 @@ ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r,
     int hx = m_nx / 2;
     int hy = m_ny / 2;
     double eMax = -1;
-    for (int ix = 0; ix < hx; ix++) {
+    for (int ix3 = 0; ix3 < hx; ix3++) {
       for (int iy = 0; iy < hy; iy++) {
-       double avgPixel = 0.25 * (v[2*ix][2*iy] + v[2*ix+1][2*iy] + v[2*ix][2*iy+1] + v[2*ix+1][2*iy+1]);
-       double avgPixelComp = 0.25 * (vComp[2*ix][2*iy] + vComp[2*ix+1][2*iy] + vComp[2*ix][2*iy+1] + vComp[2*ix+1][2*iy+1]);
+       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;
@@ -219,16 +247,16 @@ ImageFile::statistics (double& min, double& max, double& mean, double& mode, dou
     }
     mean /= (nx * ny);
 
-    static const int nbin = 256;
+    static const int nbin = 1024;
     int hist[ nbin ] = {0};
     double spread = max - min;
     mode = 0;
     stddev = 0;
-    for (int ix = 0; ix < nx; ix++) {
+    for (int ix4 = 0; ix4 < nx; ix4++) {
       for (int iy = 0; iy < ny; iy++) {
-       int b = static_cast<int>((((v[ix][iy] - min) / spread) * (nbin - 1)) + 0.5);
+       int b = static_cast<int>((((v[ix4][iy] - min) / spread) * (nbin - 1)) + 0.5);
        hist[b]++;
-       double diff = (v[ix][iy] - mean);
+       double diff = (v[ix4][iy] - mean);
        stddev += diff * diff;
        }
     }
@@ -244,8 +272,22 @@ ImageFile::statistics (double& min, double& max, double& mean, double& mode, dou
     }
 
     mode = (max_binindex * spread / (nbin - 1)) + min;
-      
-    median = 0.;
+
+    int nPixels = nx * ny;
+    list<double> vecImage;
+    for (int ix5 = 0; ix5 < nx; ix5++)
+      for (int iy = 0; iy < ny; iy++)
+       vecImage.push_front (v[ix5][iy]);
+    vecImage.sort();
+    list<double>::const_iterator iter = vecImage.begin();
+    for (int i = 0; i < (nPixels / 2) - 1; i++)
+      iter++; // Advance iterator to (nPixels / 2) - 1;
+
+    if (nPixels % 2) {  // Odd
+      iter++;
+      median = *iter;
+    } else                // Even
+      median = (*iter++ + *iter) / 2;
 }
 
 
@@ -279,7 +321,7 @@ ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double de
   int ny = m_ny;
   ImageFileArray v = getArray();
 
-  unsigned char rowp [nx * nxcell];
+  unsigned char* rowp = new unsigned char [nx * nxcell];
 
   if ((fp = fopen (outfile, "wb")) == NULL)
      return;
@@ -299,10 +341,11 @@ ImageFile::writeImagePGM (const char *outfile, int nxcell, int nycell, double de
     }
     for (int ir = 0; ir < nycell; ir++) {
       for (int ic = 0; ic < nx * nxcell; ic++) 
-       fprintf(fp, "%c ", rowp[ic]);
+       fputc( rowp[ic], fp );
     }
   }
-
+\r
+  delete rowp;
   fclose(fp);
 }
 
@@ -314,9 +357,7 @@ ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, doub
   int ny = m_ny;
   ImageFileArray v = getArray();
 
-  unsigned char rowp [nx * nxcell];
-  if (rowp == NULL)
-    return;
+  unsigned char* rowp = new unsigned char [nx * nxcell];
 
   if ((fp = fopen (outfile, "wb")) == NULL)
      return;
@@ -340,7 +381,8 @@ ImageFile::writeImagePGMASCII (const char *outfile, int nxcell, int nycell, doub
       fprintf(fp, "\n");
     }
   }
-
+\r
+  delete rowp;
   fclose(fp);
 }
 
@@ -357,7 +399,7 @@ ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nyc
   int ny = m_ny;
   ImageFileArray v = getArray();
 
-  unsigned char rowp [nx * nxcell * (bitdepth / 8)];
+  unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
 
   if ((fp = fopen (outfile, "wb")) == NULL)
      return;
@@ -409,6 +451,7 @@ ImageFile::writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nyc
 
   png_write_end(png_ptr, info_ptr);
   png_destroy_write_struct(&png_ptr, &info_ptr);
+  delete rowp;\r
 
   fclose(fp);
 }