X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=include%2Fimagefile.h;h=607f7166cd1cef4bd2f323c273830c0b38bb23e7;hp=a6988d8e76b909c77f395909894203901f257a5b;hb=44ba9ce559d2d52cbd7bbea6bcd76242840fd3eb;hpb=dc41bc610efe1cfb7b29f715dcc036b879db1f44 diff --git a/include/imagefile.h b/include/imagefile.h index a6988d8..607f716 100644 --- a/include/imagefile.h +++ b/include/imagefile.h @@ -2,26 +2,35 @@ #define IDF_H #include -#include "kstddef.h" -#include +#include +#include +#include +#include +#include "ctsupport.h" +#include "fnetorderstream.h" +#include "array2d.h" using namespace std; + class Array2dFileLabel { private: void init (void); + Array2dFileLabel (const Array2dFileLabel&); + Array2dFileLabel& operator= (const Array2dFileLabel&); public: - kfloat64 calc_time; - kuint16 label_type; - kuint16 year; - kuint16 month; - kuint16 day; - kuint16 hour; - kuint16 minute; - kuint16 second; - string label_str; + kfloat64 m_calcTime; + kuint16 m_labelType; + kuint16 m_year; + kuint16 m_month; + kuint16 m_day; + kuint16 m_hour; + kuint16 m_minute; + kuint16 m_second; + string m_strLabel; + mutable string m_strDate; static const int L_EMPTY = 0; static const int L_HISTORY = 1; @@ -36,15 +45,21 @@ public: ~Array2dFileLabel(); const string& getLabelString (void) const - { return label_str; } + { return m_strLabel; } kfloat64 getCalcTime (void) const - { return calc_time; } + { return m_calcTime; } kfloat64 getLabelType (void) const - { return label_type; } + { return m_labelType; } + + string& setLabelString (const char* const str) + { m_strLabel = str; return (m_strLabel); } + + string& setLabelString (const string& str) + { m_strLabel = str; return (m_strLabel); } - void getDateString (string& str) const; + const string& getDateString () const; }; @@ -58,7 +73,7 @@ private: kuint16 headersize; string filename; int file_id; - iostream *io; + frnetorderstream *m_pFS; bool bHeaderWritten; bool bDataWritten; @@ -80,9 +95,12 @@ private: kuint32 mNX; kuint32 mNY; + Array2dFile (const Array2dFile&); + Array2dFile& operator= (const Array2dFile&); + public: - Array2d *array; + Array2d array; static const int INT8 = 1; static const int UINT8 = 2; @@ -105,9 +123,11 @@ public: void labelAdd (const Array2dFileLabel& label); - void labelAdd (const char* const label_str, double calc_time=0.); + void labelAdd (const char* const m_strLabel, double calc_time=0.); + + void labelAdd (int type, const char* const m_strLabel, double calc_time=0.); - 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); @@ -124,15 +144,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 +170,7 @@ Array2dFile::Array2dFile (unsigned int x, unsigned int y) init(); mNX = x; mNY = y; - array = new Array2d (mNX, mNY); + array.initSetSize(mNX, mNY); } template @@ -158,7 +180,7 @@ Array2dFile::Array2dFile (const char * const str, unsigned int x, unsigned in init(); mNX = x; mNY = y; - array = new Array2d (mNX, mNY); + array.initSetSize(mNX, mNY); } template @@ -174,7 +196,6 @@ Array2dFile::init (void) { mPixelSize = sizeof(T); signature = ('I' * 256 + 'F'); - file_id = -1; mNX = 0; mNY = 0; headersize = 0; @@ -185,8 +206,7 @@ Array2dFile::init (void) mMinX = mMaxX = mMinY = mMaxY = 0; mOffsetPV = 0; mScalePV = 1; - array = NULL; - io = NULL; + m_pFS = NULL; #if 0 const type_info& t_id = typeid(T); @@ -228,11 +248,10 @@ template void Array2dFile::fileClose (void) { - headerWrite (); - if (file_id >= 0 && array != NULL) { - arrayDataWrite (); - close (file_id); - file_id = -1; + if (m_pFS) { + headerWrite (); + m_pFS->close (); + m_pFS = NULL; } } @@ -240,11 +259,11 @@ template bool Array2dFile::fileCreate (void) { - // io = new iostream(filename, ios::out | ios::in | ios::trunc | io::binary); - if ((file_id = open (filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { - sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", filename.c_str()); - return (false); - } + m_pFS = new frnetorderstream (filename.c_str(), ios::out | ios::in | ios::trunc | ios::binary); + if (! m_pFS) { + sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", filename.c_str()); + return (false); + } headerWrite(); return (true); } @@ -253,17 +272,16 @@ template bool Array2dFile::fileRead (void) { - // io = new iostream(filename, ios::out | ios::in | io::binary); - if ((file_id = open (filename.c_str(), O_RDONLY | O_BINARY)) < 0) { + m_pFS = new frnetorderstream (filename.c_str(), ios::out | ios::in | ios::binary | ios::nocreate); + if (m_pFS->fail()) { sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", filename.c_str()); return (false); } - headerRead(); - if (array != NULL) - delete array; + if (! headerRead()) + return false; - array = new Array2d (mNX, mNY); + array.initSetSize(mNX, mNY); arrayDataRead(); @@ -292,32 +310,32 @@ Array2dFile::setAxisExtent (double minX, double maxX, double minY, double max template void -Array2dFile::getPixelValueRange (T& pvmin, T& pvmax) +Array2dFile::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 void Array2dFile::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; } } @@ -325,35 +343,35 @@ template bool Array2dFile::headerRead (void) { - if (file_id < 0) { + if (! m_pFS) { sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]"); return (false); } - lseek (file_id, 0, SEEK_SET); + m_pFS->seekg (0); kuint16 file_signature; kuint16 file_mPixelSize; kuint16 file_mPixelType; - read_nint16 (&headersize, file_id); - read_nint16 (&file_signature, file_id); - read_nint16 (&num_labels, file_id); - read_nint16 (&file_mPixelType, file_id); - read_nint16 (&file_mPixelSize, file_id); - read_nint32 (&mNX, file_id); - read_nint32 (&mNY, file_id); - read_nint16 (&axis_increment_known, file_id); - read_nfloat64 (&mIncX, file_id); - read_nfloat64 (&mIncY, file_id); - read_nint16 (&axis_extent_known, file_id); - read_nfloat64 (&mMinX, file_id); - read_nfloat64 (&mMaxX, file_id); - read_nfloat64 (&mMinY, file_id); - read_nfloat64 (&mMaxY, file_id); - read_nfloat64 (&mOffsetPV, file_id); - read_nfloat64 (&mScalePV, file_id); - - int read_headersize = lseek (file_id, 0, SEEK_CUR); + m_pFS->readInt16 (headersize); + m_pFS->readInt16 (file_signature); + m_pFS->readInt16 (num_labels); + m_pFS->readInt16 (file_mPixelType); + m_pFS->readInt16 (file_mPixelSize); + m_pFS->readInt32 (mNX); + m_pFS->readInt32 (mNY); + m_pFS->readInt16 (axis_increment_known); + m_pFS->readFloat64 (mIncX); + m_pFS->readFloat64 (mIncY); + m_pFS->readInt16 (axis_extent_known); + m_pFS->readFloat64 (mMinX); + m_pFS->readFloat64 (mMaxX); + m_pFS->readFloat64 (mMinY); + m_pFS->readFloat64 (mMaxY); + m_pFS->readFloat64 (mOffsetPV); + m_pFS->readFloat64 (mScalePV); + + int read_headersize = m_pFS->tellg(); if (read_headersize != headersize) { sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize); return (false); @@ -378,33 +396,32 @@ template bool Array2dFile::headerWrite (void) { - if (file_id < 0) { + if (! m_pFS) { sys_error (ERR_WARNING, "Tried to write header with file closed"); return (false); } - lseek (file_id, 0, SEEK_SET); - write_nint16 (&headersize, file_id); - write_nint16 (&signature, file_id); - write_nint16 (&num_labels, file_id); - write_nint16 (&mPixelType, file_id); - write_nint16 (&mPixelSize, file_id); - write_nint32 (&mNX, file_id); - write_nint32 (&mNY, file_id); - write_nint16 (&axis_increment_known, file_id); - write_nfloat64 (&mIncX, file_id); - write_nfloat64 (&mIncY, file_id); - write_nint16 (&axis_extent_known, file_id); - write_nfloat64 (&mMinX, file_id); - write_nfloat64 (&mMaxX, file_id); - write_nfloat64 (&mMinY, file_id); - write_nfloat64 (&mMaxY, file_id); - write_nfloat64 (&mOffsetPV, file_id); - write_nfloat64 (&mScalePV, file_id); - - headersize = lseek (file_id, 0, SEEK_CUR); - lseek (file_id, 0, SEEK_SET); - write_nint16 (&headersize, file_id); + m_pFS->seekp (0); + m_pFS->writeInt16 (headersize); + m_pFS->writeInt16 (signature); + m_pFS->writeInt16 (num_labels); + m_pFS->writeInt16 (mPixelType); + m_pFS->writeInt16 (mPixelSize); + m_pFS->writeInt32 (mNX); + m_pFS->writeInt32 (mNY); + m_pFS->writeInt16 (axis_increment_known); + m_pFS->writeFloat64 (mIncX); + m_pFS->writeFloat64 (mIncY); + m_pFS->writeInt16 (axis_extent_known); + m_pFS->writeFloat64 (mMinX); + m_pFS->writeFloat64 (mMaxX); + m_pFS->writeFloat64 (mMinY); + m_pFS->writeFloat64 (mMaxY); + m_pFS->writeFloat64 (mOffsetPV); + m_pFS->writeFloat64 (mScalePV); + + headersize = m_pFS->tellp(); + m_pFS->writeInt16 (headersize); return (true); } @@ -413,18 +430,26 @@ template bool Array2dFile::arrayDataWrite (void) { - if (file_id < 0) { - sys_error (ERR_WARNING, "Tried to arrayDataWrite with file_id < 0"); + if (! m_pFS) { + sys_error (ERR_WARNING, "Tried to arrayDataWrite with !m_pFS"); 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, &value, mPixelSize); - } + T** da = array.getArray(); + if (! da) + return (false); + + m_pFS->seekp (headersize); + for (unsigned int ix = 0; ix < mNX; ix++) { + if (NativeBigEndian()) { + for (unsigned int iy = 0; iy < mNY; iy++) { + T value = da[ix][iy]; + ConvertReverseNetworkOrder (&value, sizeof(T)); + m_pFS->write (&value, mPixelSize); + } + } else + m_pFS->write (da[ix], sizeof(T) * mNY); + } return (true); } @@ -433,19 +458,28 @@ template bool Array2dFile::arrayDataRead (void) { - if (file_id < 0) { - sys_error (ERR_WARNING, "Tried to arrayDataRead with file_id < 0"); + if (! m_pFS) { + sys_error (ERR_WARNING, "Tried to arrayDataRead with !m_pFS"); return (false); } - lseek (file_id, headersize, SEEK_SET); + T** da = array.getArray(); + if (! da) + return (false); + + m_pFS->seekg (headersize); 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; - } + for (int ix = 0; ix < mNX; ix++) { + if (NativeBigEndian()) { + for (unsigned int iy = 0; iy < mNY; iy++) { + m_pFS->read (&pixelBuffer, mPixelSize); + ConvertReverseNetworkOrder (&pixelBuffer, sizeof(T)); + da[ix][iy] = pixelBuffer; + } + } else + m_pFS->read (da[ix], sizeof(T) * mNY); + } + return (true); } @@ -459,32 +493,27 @@ Array2dFile::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(); - if (lseek (file_id, pos, SEEK_SET) != pos) { - sys_error (ERR_WARNING, "Can't seek to end of data array"); - return (false); - } + off_t pos = headersize + array.sizeofArray(); + m_pFS->seekg (pos); for (int i = 0; i < label_num; i++) { pos += 22; // Skip to string length - if (lseek (file_id, pos, SEEK_SET) != pos) { - sys_error (ERR_WARNING, "Can't seek to string length"); - return (false); - } + m_pFS->seekg (pos); + kuint16 strlength; - read_nint16 (&strlength, file_id); + m_pFS->readInt16 (strlength); pos += 2 + strlength; // Skip label string length + data - if (lseek (file_id, pos, SEEK_SET) != pos) { - sys_error (ERR_WARNING, "Can't seek past label string"); - return (false); - } + m_pFS->seekg (pos); } + + if (! m_pFS) + return false; return (true); } @@ -503,20 +532,21 @@ Array2dFile::labelRead (Array2dFileLabel& label, unsigned int label_num) return (false); } - read_nint16 (&label.label_type, file_id); - read_nint16 (&label.year, file_id); - read_nint16 (&label.month, file_id); - read_nint16 (&label.day, file_id); - read_nint16 (&label.hour, file_id); - read_nint16 (&label.minute, file_id); - read_nint16 (&label.second, file_id); - read_nfloat64 (&label.calc_time, file_id); + m_pFS->readInt16 (label.m_labelType); + m_pFS->readInt16 (label.m_year); + m_pFS->readInt16 (label.m_month); + m_pFS->readInt16 (label.m_day); + m_pFS->readInt16 (label.m_hour); + m_pFS->readInt16 (label.m_minute); + m_pFS->readInt16 (label.m_second); + m_pFS->readFloat64 (label.m_calcTime); kuint16 strlength; - read_nint16 (&strlength, file_id); - char *str = new char [strlength+1]; - read (file_id, str, strlength); - label.label_str = str; + m_pFS->readInt16 (strlength); + char str [strlength+1]; + m_pFS->read (str, strlength); + str[strlength] = 0; + label.m_strLabel = str; return (true); } @@ -544,87 +574,125 @@ Array2dFile::labelAdd (const Array2dFileLabel& label) { labelSeek (num_labels); - write_nint16 (&label.label_type, file_id); - write_nint16 (&label.year, file_id); - write_nint16 (&label.month, file_id); - write_nint16 (&label.day, file_id); - write_nint16 (&label.hour, file_id); - write_nint16 (&label.minute, file_id); - write_nint16 (&label.second, file_id); - write_nfloat64 (&label.calc_time, file_id); - kuint16 strlength = label.label_str.length(); - write_nint16 (&strlength, file_id); - write (file_id, static_cast(label.label_str.c_str()), strlength); + m_pFS->writeInt16 (label.m_labelType); + m_pFS->writeInt16 (label.m_year); + m_pFS->writeInt16 (label.m_month); + m_pFS->writeInt16 (label.m_day); + m_pFS->writeInt16 (label.m_hour); + m_pFS->writeInt16 (label.m_minute); + m_pFS->writeInt16 (label.m_second); + m_pFS->writeFloat64 (label.m_calcTime); + kuint16 strlength = label.m_strLabel.length(); + m_pFS->writeInt16 (strlength); + m_pFS->write (label.m_strLabel.c_str(), strlength); num_labels++; headerWrite(); - fsync(file_id); } +template +void +Array2dFile::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 +void +Array2dFile::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 + +#ifdef HAVE_MPI +#include #endif -class F32Image +class F32Image : public Array2dFile { public: - Array2dFile 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::Array2dFile (fname, nx, ny) { - adf.setPixelType (Array2dFile::FLOAT32); + setPixelType (FLOAT32); } - F32Image (unsigned int nx, unsigned int ny) : adf (nx, ny) + F32Image (unsigned int nx, unsigned int ny) + : Array2dFile::Array2dFile (nx, ny) { - adf.setPixelType (Array2dFile::FLOAT32); + setPixelType (FLOAT32); } - F32Image (const char* const fname) : adf (fname) + F32Image (const char* const fname) + : Array2dFile::Array2dFile (fname) { - adf.setPixelType (Array2dFile::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 adf; -#ifdef MPI_CT - MPI_Datatype getMPIDataType (void) const - { return MPI_DOUBLE; } -#endif +class F64Image : public Array2dFile +{ + 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::Array2dFile (fname, nx, ny) { - adf.setPixelType (Array2dFile::FLOAT64); + setPixelType (FLOAT64); } - F64Image (unsigned int nx, unsigned int ny) : adf (nx, ny) + F64Image (unsigned int nx, unsigned int ny) + : Array2dFile::Array2dFile (nx, ny) { - adf.setPixelType (Array2dFile::FLOAT64); + setPixelType (FLOAT64); } - F64Image (const char* const fname) : adf (fname) + F64Image (const char* const fname) + : Array2dFile::Array2dFile (fname) { - adf.setPixelType (Array2dFile::FLOAT64); + setPixelType (FLOAT64); } + +#ifdef HAVE_MPI + MPI::Datatype getMPIDataType (void) const + { return MPI::DOUBLE; } +#endif }; +#undef IMAGEFILE_64_BITS +#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