r91: Made ImageFile inherit from Array2dFile
[ctsim.git] / include / imagefile.h
index 2c145e0ba6b9de2998222a47b0c6cea3fdd75bdc..7c0952c2acc568e8b96897a011637ac77074baa5 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <string>
 #include "kstddef.h"
+#include <sys/types.h>
+#include <unistd.h>
 #include <array2d.h>
 
 using namespace std;
@@ -35,7 +37,7 @@ public:
 
     ~Array2dFileLabel();
 
-    const string& getLabelString (void) const
+    string getLabelString (void) const
        { return label_str; }
 
     kfloat64 getCalcTime (void) const
@@ -44,6 +46,12 @@ public:
     kfloat64 getLabelType (void) const
        { return label_type; }
 
+    string& setLabelString (const char* const str)
+       { label_str = str; return (label_str); }
+
+    string& setLabelString (const string& str)
+       { label_str = str; return (label_str); }
+
     void getDateString (string& str) const;
 };
 
@@ -82,7 +90,7 @@ private:
 
 public:
 
-  Array2d<T> *array;
+  Array2d<T> array;
 
   static const int INT8 = 1;
   static const int UINT8 = 2;
@@ -109,6 +117,8 @@ public:
 
   void labelAdd (int type, const char* const label_str, double calc_time=0.);
 
+  void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
+
   void fileClose (void);
 
   void setPixelType (int type)
@@ -124,15 +134,17 @@ public:
 
   void setAxisExtent (double mMinX, double mMaxX, double mMinY, double mMaxY);
 
-  void getPixelValueRange (T& pvmin, T& pvmax);
+  void getPixelValueRange (T& pvmin, T& pvmax) const;
       
   void doPixelOffsetScale (double offset, double scale);
 
   T** getArray (void) const
-      { return (array == NULL ? NULL : array->getArray()); }
+      { return (array.getArray()); }
 
   bool arrayDataWrite (void);
 
+  void arrayDataClear (void);
+
   bool fileRead (void);
 
   bool fileCreate (void);
@@ -148,7 +160,7 @@ Array2dFile<T>::Array2dFile (unsigned int x, unsigned int y)
     init();
     mNX = x;
     mNY = y;
-    array = new Array2d<T> (mNX, mNY);
+    array.initSetSize(mNX, mNY);
 }
 
 template<class T>
@@ -158,7 +170,7 @@ Array2dFile<T>::Array2dFile (const char * const str, unsigned int x, unsigned in
     init();
     mNX = x;
     mNY = y;
-    array = new Array2d<T> (mNX, mNY);
+    array.initSetSize(mNX, mNY);
 }
 
 template<class T>
@@ -185,7 +197,6 @@ Array2dFile<T>::init (void)
   mMinX = mMaxX = mMinY = mMaxY = 0;
   mOffsetPV = 0;
   mScalePV = 1;
-  array = NULL;
   io = NULL;
 
 #if 0
@@ -228,11 +239,10 @@ template<class T>
 void
 Array2dFile<T>::fileClose (void)
 {
-  headerWrite ();
-  if (file_id >= 0 && array != NULL) {
-    arrayDataWrite ();
-    close (file_id);
-    file_id = -1;
+  if (file_id >= 0) {
+      headerWrite ();
+      close (file_id);
+      file_id = -1;
   }
 }
 
@@ -260,10 +270,8 @@ Array2dFile<T>::fileRead (void)
   }
 
   headerRead();
-  if (array != NULL)
-    delete array;
 
-  array = new Array2d<T> (mNX, mNY);
+  array.initSetSize(mNX, mNY);
 
   arrayDataRead();
 
@@ -292,32 +300,32 @@ Array2dFile<T>::setAxisExtent (double minX, double maxX, double minY, double max
 
 template<class T>
 void 
-Array2dFile<T>::getPixelValueRange (T& pvmin, T& pvmax)
+Array2dFile<T>::getPixelValueRange (T& pvmin, T& pvmax) const
 {
-      if (array != NULL) {
-       T** da = array.GetArray();
+    T** da = array.getArray();
+    if (da) {
        pvmax = pvmin = da[0][0];
-         for (int ix = 0; ix < mNX; ix++)
-             for (int iy = 0; iy < mNY; iy++)
-                 if (pvmax < da[ix][iy])
-                     pvmax = da[ix][iy];
-                 else if (pvmin > da[ix][iy])
+       for (int ix = 0; ix < mNX; ix++)
+           for (int iy = 0; iy < mNY; iy++)
+               if (pvmax < da[ix][iy])
+                   pvmax = da[ix][iy];
+               else if (pvmin > da[ix][iy])
                    pvmin = da[ix][iy];
-      }
+    }
 }
 
 template<class T>
 void
 Array2dFile<T>::doPixelOffsetScale (double offset, double scale)
 {
-    if (adf != NULL) {
-      mOffsetPV = offset;
-      mScalePV = scale;
-
-      T** ad = adf->getArray();
-      for (unsigned int ix = 0; ix < mNX; ix++) 
-         for (unsigned int iy = 0; iy < mNY; iy++)
-             ad[ix][iy] = (ad[ix][iy] - offset) * scale;
+    T** ad = array.getArray();
+    if (ad) {
+       mOffsetPV = offset;
+       mScalePV = scale;
+
+       for (unsigned int ix = 0; ix < mNX; ix++) 
+           for (unsigned int iy = 0; iy < mNY; iy++)
+               ad[ix][iy] = (ad[ix][iy] - offset) * scale;
     }
 }
 
@@ -418,12 +426,16 @@ Array2dFile<T>::arrayDataWrite (void)
     return (false);
   }
 
+  T** da = array.getArray();
+  if (! da) 
+      return (false);
+
   lseek (file_id, headersize, SEEK_SET);
   for (unsigned int ix = 0; ix < mNX; ix++)
       for (unsigned int iy = 0; iy < mNY; iy++) {
-         T value = array->array_data[ix][iy];
-         ConvertNetworkOrder (&value, sizeof(T));
-         write (file_id, &array->array_data[ix][iy], mPixelSize);
+         T value = da[ix][iy];
+         ConvertReverseNetworkOrder (&value, sizeof(T));
+             write (file_id, &value, mPixelSize);
       }
 
   return (true);
@@ -438,13 +450,17 @@ Array2dFile<T>::arrayDataRead (void)
     return (false);
   }
 
+  T** da = array.getArray();
+  if (! da) 
+      return (false);
+
   lseek (file_id, headersize, SEEK_SET);
   T pixelBuffer;
   for (int ix = 0; ix < mNX; ix++) 
       for (unsigned int iy = 0; iy < mNY; iy++) {
          read (file_id, &pixelBuffer, mPixelSize);
-         ConvertNetworkOrder (&pixelBuffer, sizeof(T));
-         array->array_data[ix][iy] = pixelBuffer;
+         ConvertReverseNetworkOrder (&pixelBuffer, sizeof(T));
+         da[ix][iy] = pixelBuffer;
       }
 
   return (true);
@@ -459,12 +475,12 @@ Array2dFile<T>::labelSeek (unsigned int label_num)
     return (false);
   }
 
-  if (array == NULL) {    // Could not have written data if array not allocated
+  if (array.getArray() == NULL) {    // Could not have written data if array not allocated
     sys_error (ERR_WARNING, "array == NULL [labelSeek]");
     return (false);
   }
 
-  off_t pos = headersize + array->sizeofArray();
+  off_t pos = headersize + array.sizeofArray();
   if (lseek (file_id, pos, SEEK_SET) != pos) {
     sys_error (ERR_WARNING, "Can't seek to end of data array");
     return (false);
@@ -562,69 +578,108 @@ Array2dFile<T>::labelAdd (const Array2dFileLabel& label)
   fsync(file_id);
 }
 
+template<class T>
+void
+Array2dFile<T>::labelsCopy (Array2dFile& copyFile, const char* const idStr)
+{
+    string id = idStr;
+    for (int i = 0; i < copyFile.getNumLabels(); i++) {
+      Array2dFileLabel l;
+      copyFile.labelRead (l, i);
+      string lstr = l.getLabelString();
+      lstr = idStr + lstr;
+      l.setLabelString (lstr);
+      labelAdd (l);
+    }
+}
 
-#endif
+template<class T>
+void 
+Array2dFile<T>::arrayDataClear (void)
+{
+    T** v = array.getArray();
+    if (v) {
+       for (unsigned int ix = 0; ix < mNX; ix++)
+           for (unsigned int iy = 0; iy < mNY; iy++)
+               v[ix][iy] = 0;
+    }
+}
 
-#ifdef MPI_CT
-#include <mpi.h>
+
+#ifdef HAVE_MPI
+#include <mpi++.h>
 #endif
 
-class F32Image
+class F32Image : public Array2dFile<kfloat32>
 {
 public:
-  Array2dFile<kfloat32> adf;
-
-  F32Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny)
+  F32Image (const char* const fname, unsigned int nx, unsigned int ny)
+      : Array2dFile<kfloat32>::Array2dFile (fname, nx, ny)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
+      setPixelType (FLOAT32);
   }
 
-  F32Image (unsigned int nx, unsigned int ny) : adf (nx, ny)
+  F32Image (unsigned int nx, unsigned int ny)
+      : Array2dFile<kfloat32>::Array2dFile (nx, ny)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
+      setPixelType (FLOAT32);
   }
 
-  F32Image (const char* const fname) : adf (fname)
+  F32Image (const char* const fname)
+      : Array2dFile<kfloat32>::Array2dFile (fname)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
+      setPixelType (FLOAT32);
   }
 
-#ifdef MPI_CT
-  MPI_Datatype getMPIDataType (void) const
-      { return MPI_FLOAT; }
+#ifdef HAVE_MPI
+  MPI::Datatype getMPIDataType (void) const
+      { return MPI::FLOAT; }
 #endif
 };
 
-class F64Image 
-{
-public:
-  Array2dFile<kfloat64> adf;
 
-#ifdef MPI_CT
-  MPI_Datatype getMPIDataType (void) const
-      { return MPI_DOUBLE; }
-#endif
+class F64Image : public Array2dFile<kfloat64>
+{
+ public:
 
-  F64Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny)
+  F64Image (const char* const fname, unsigned int nx, unsigned int ny)
+      : Array2dFile<kfloat64>::Array2dFile (fname, nx, ny)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
+      setPixelType (FLOAT64);
   }
 
-  F64Image (unsigned int nx, unsigned int ny) : adf (nx, ny)
+  F64Image (unsigned int nx, unsigned int ny)
+      : Array2dFile<kfloat64>::Array2dFile (nx, ny)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
+      setPixelType (FLOAT64);
   }
 
-  F64Image (const char* const fname) : adf (fname)
+  F64Image (const char* const fname)
+      : Array2dFile<kfloat64>::Array2dFile (fname)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
+      setPixelType (FLOAT64);
   }
+
+#ifdef HAVE_MPI
+  MPI::Datatype getMPIDataType (void) const
+      { return MPI::DOUBLE; }
+#endif
 };
 
+#define IMAGEFILE_64_BITS 1
+#ifdef IMAGEFILE_64_BITS
 typedef F64Image   ImageFile;
 typedef kfloat64   ImageFileValue;
 typedef kfloat64*  ImageFileColumn;
 typedef kfloat64** ImageFileArray;
+#else
+typedef F32Image   ImageFile;
+typedef kfloat32   ImageFileValue;
+typedef kfloat32*  ImageFileColumn;
+typedef kfloat32** ImageFileArray;
+#endif
+
+#endif