X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=include%2Fimagefile.h;h=a7600dcce5cc33b9c563e5ead21c0ee9103f0d99;hp=a6988d8e76b909c77f395909894203901f257a5b;hb=3fba6928127cd65870bdcd96c8114ad5894247ae;hpb=dc41bc610efe1cfb7b29f715dcc036b879db1f44 diff --git a/include/imagefile.h b/include/imagefile.h index a6988d8..a7600dc 100644 --- a/include/imagefile.h +++ b/include/imagefile.h @@ -1,630 +1,163 @@ -#ifndef IDF_H -#define IDF_H +/***************************************************************************** +** FILE IDENTIFICATION +** +** Name: imagefile.h +** Purpose: imagefile class header +** Programmer: Kevin Rosenberg +** Date Started: June 2000 +** +** This is part of the CTSim program +** Copyright (C) 1983-2000 Kevin Rosenberg +** +** $Id: imagefile.h,v 1.18 2000/07/13 07:03:21 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 +** published by the Free Software Foundation. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +******************************************************************************/ + +#ifndef IMAGEFILE_H +#define IMAGEFILE_H #include -#include "kstddef.h" -#include +#include +#include +#include +#include +#include "ctsupport.h" +#include "fnetorderstream.h" +#include "array2dfile.h" using namespace std; -class Array2dFileLabel -{ -private: - void init (void); - -public: - kfloat64 calc_time; - kuint16 label_type; - kuint16 year; - kuint16 month; - kuint16 day; - kuint16 hour; - kuint16 minute; - kuint16 second; - string label_str; - - static const int L_EMPTY = 0; - static const int L_HISTORY = 1; - static const int L_USER = 2; - - Array2dFileLabel(); - - Array2dFileLabel(const char* const str, double ctime = 0.); - - Array2dFileLabel(const int type, const char* const str, double ctime = 0.); - - ~Array2dFileLabel(); - - const string& getLabelString (void) const - { return label_str; } - - kfloat64 getCalcTime (void) const - { return calc_time; } - - kfloat64 getLabelType (void) const - { return label_type; } - - void getDateString (string& str) const; -}; - - -template -class Array2dFile -{ -private: - void init (void); - kuint16 signature; - kuint16 num_labels; - kuint16 headersize; - string filename; - int file_id; - iostream *io; - bool bHeaderWritten; - bool bDataWritten; - - bool headerWrite (void); - - bool headerRead (void); - - bool arrayDataRead (void); - - bool labelSeek (unsigned int label_num); - - kuint16 mPixelSize; - kuint16 axis_increment_known; - kfloat64 mIncX, mIncY; - kuint16 axis_extent_known; - kfloat64 mMinX, mMaxX, mMinY, mMaxY; - kfloat64 mOffsetPV, mScalePV; - kuint16 mPixelType; - kuint32 mNX; - kuint32 mNY; - -public: - - Array2d *array; - - static const int INT8 = 1; - static const int UINT8 = 2; - static const int INT16 = 3; - static const int UINT16 = 4; - static const int INT32 = 5; - static const int UINT32 = 6; - static const int FLOAT32 = 7; - static const int FLOAT64 = 8; - - Array2dFile (unsigned int nx, unsigned int ny); - Array2dFile (const char* const filename); - Array2dFile (const char* const filename, unsigned int nx, unsigned int ny); - ~Array2dFile (); - - unsigned int getNumLabels (void) const - { return num_labels; } - - bool labelRead (Array2dFileLabel& label, unsigned int label_num); - - void labelAdd (const Array2dFileLabel& label); - - void labelAdd (const char* const label_str, double calc_time=0.); - - void labelAdd (int type, const char* const label_str, double calc_time=0.); - - void fileClose (void); - - void setPixelType (int type) - { mPixelType = type; } - - kuint32 nx (void) const - { return mNX; } - - kuint32 ny (void) const - { return mNY; } - - void setAxisIncrement (double mIncX, double mIncY); - - void setAxisExtent (double mMinX, double mMaxX, double mMinY, double mMaxY); - - void getPixelValueRange (T& pvmin, T& pvmax); - - void doPixelOffsetScale (double offset, double scale); - - T** getArray (void) const - { return (array == NULL ? NULL : array->getArray()); } - - bool arrayDataWrite (void); - - bool fileRead (void); - - bool fileCreate (void); - - const string& GetFilename (void) const - { return filename; } -}; - - -template -Array2dFile::Array2dFile (unsigned int x, unsigned int y) -{ - init(); - mNX = x; - mNY = y; - array = new Array2d (mNX, mNY); -} - -template -Array2dFile::Array2dFile (const char * const str, unsigned int x, unsigned int y) - : filename(str) -{ - init(); - mNX = x; - mNY = y; - array = new Array2d (mNX, mNY); -} - -template -Array2dFile::Array2dFile (const char * const str) - : filename(str) -{ - init(); -} - -template -void -Array2dFile::init (void) -{ - mPixelSize = sizeof(T); - signature = ('I' * 256 + 'F'); - file_id = -1; - mNX = 0; - mNY = 0; - headersize = 0; - num_labels = 0; - axis_increment_known = false; - axis_extent_known = false; - mIncX = mMinY = 0; - mMinX = mMaxX = mMinY = mMaxY = 0; - mOffsetPV = 0; - mScalePV = 1; - array = NULL; - io = NULL; - -#if 0 - const type_info& t_id = typeid(T); - cout << t_id.name() << endl; - const type_info& comp_id = typeid(T); - - if (t_id == comp_id) - mPixelType = FLOAT64; - else if (t_id == typeid(kfloat32)) - mPixelType = FLOAT32; - else if (t_id == typeid(kint32)) - mPixelType = INT32; - else if (t_id == typeid(kuint32)) - mPixelType = UINT32; - else if (t_id == typeid(kint16)) - mPixelType = INT16; - else if (t_id == typeid(kuint16)) - mPixelType = UINT16; - else if (t_id == typeid(kint8)) - mPixelType = INT8; - else if (t_id == typeid(kuint8)) - mPixelType = UINT8; - else +#ifdef HAVE_MPI +#include #endif - mPixelType = 0; - bHeaderWritten = false; - bDataWritten = false; -} - -template -Array2dFile::~Array2dFile (void) -{ - fileClose (); -} - - -template -void -Array2dFile::fileClose (void) -{ - headerWrite (); - if (file_id >= 0 && array != NULL) { - arrayDataWrite (); - close (file_id); - file_id = -1; - } -} - -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); - } - headerWrite(); - return (true); -} - -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) { - sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", filename.c_str()); - return (false); - } - - headerRead(); - if (array != NULL) - delete array; - - array = new Array2d (mNX, mNY); - - arrayDataRead(); - - return (true); -} - -template -void -Array2dFile::setAxisIncrement (double incX, double incY) -{ - axis_increment_known = true; - mIncX = incX; - mIncY = incY; -} - -template -void -Array2dFile::setAxisExtent (double minX, double maxX, double minY, double maxY) -{ - axis_extent_known = true; - mMinX = minX; - mMaxY = maxX; - mMinX = minX; - mMaxY = maxY; -} - -template -void -Array2dFile::getPixelValueRange (T& pvmin, T& pvmax) +class F32Image : public Array2dFile { - if (array != NULL) { - T** da = array.GetArray(); - 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]) - 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; - } -} - -template -bool -Array2dFile::headerRead (void) -{ - if (file_id < 0) { - sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]"); - return (false); +public: + F32Image (int nx, int ny) + : Array2dFile::Array2dFile (nx, ny, sizeof(kfloat32), Array2dFile::PIXEL_FLOAT32) + { } - lseek (file_id, 0, SEEK_SET); - 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); - if (read_headersize != headersize) { - sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize); - return (false); - } - if (file_signature != signature) { - sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, signature); - return (false); - } - if (file_mPixelType != mPixelType) { - sys_error (ERR_WARNING, "File pixel type %d != class pixel type %d", file_mPixelType, mPixelType); - return (false); - } - if (file_mPixelSize != mPixelSize) { - sys_error (ERR_WARNING, "File pixel size %d != class pixel size %d", file_mPixelSize, mPixelSize); - return (false); + F32Image (void) + : Array2dFile::Array2dFile() + { + setPixelFormat (Array2dFile::PIXEL_FLOAT32); + setPixelSize (sizeof(kfloat32)); } - return (true); -} + kfloat32** getArray (void) + { return (kfloat32**) (m_arrayData); } -template -bool -Array2dFile::headerWrite (void) -{ - if (file_id < 0) { - 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); - - return (true); -} - -template -bool -Array2dFile::arrayDataWrite (void) -{ - if (file_id < 0) { - sys_error (ERR_WARNING, "Tried to arrayDataWrite with file_id < 0"); - return (false); - } + const kfloat32* const * getArray (void) const + { return (const kfloat32**) (m_arrayData); } - 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); - } +#ifdef HAVE_MPI + MPI::Datatype getMPIDataType (void) const + { return MPI::FLOAT; } +#endif - return (true); -} + private: + F32Image (const F32Image& rhs); //copy constructor + F32Image& operator= (const F32Image& rhs); // assignment operator +}; -template -bool -Array2dFile::arrayDataRead (void) -{ - if (file_id < 0) { - sys_error (ERR_WARNING, "Tried to arrayDataRead with file_id < 0"); - 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; - } - - return (true); -} - -template -bool -Array2dFile::labelSeek (unsigned int label_num) +class F64Image : public Array2dFile { - if (label_num > num_labels) { - sys_error (ERR_WARNING, "label_num %d > num_labels %d [labelSeek]"); - return (false); - } - - if (array == NULL) { // Could not have written data if array not allocated - sys_error (ERR_WARNING, "array == NULL [labelSeek]"); - return (false); - } + public: - 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); + F64Image (int nx, int ny) + : Array2dFile::Array2dFile (nx, ny, sizeof(kfloat64), Array2dFile::PIXEL_FLOAT64) + { } - 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); - } - kuint16 strlength; - read_nint16 (&strlength, file_id); - 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); - } - } - - return (true); -} - -template -bool -Array2dFile::labelRead (Array2dFileLabel& label, unsigned int label_num) -{ - if (label_num >= num_labels) { - sys_error (ERR_WARNING, "Trying to read past number of labels [labelRead]"); - return (false); + F64Image (void) + : Array2dFile::Array2dFile () + { + setPixelFormat (PIXEL_FLOAT64); + setPixelSize (sizeof(kfloat64)); } - if (! labelSeek (label_num)) { - sys_error (ERR_WARNING, "Error calling labelSeek"); - return (false); - } + kfloat64** getArray (void) + { return (kfloat64**) (m_arrayData); } - 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); + const kfloat64* const * getArray (void) const + { return (const kfloat64**) (m_arrayData); } - kuint16 strlength; - read_nint16 (&strlength, file_id); - char *str = new char [strlength+1]; - read (file_id, str, strlength); - label.label_str = str; +#ifdef HAVE_MPI + MPI::Datatype getMPIDataType (void) const + { return MPI::DOUBLE; } +#endif + private: + F64Image (const F64Image& rhs); //copy constructor + F64Image& operator= (const F64Image& rhs); // assignment operator +}; - return (true); -} +#undef IMAGEFILE_64_BITS +#ifdef IMAGEFILE_64_BITS +typedef F64Image ImageFileBase; +typedef kfloat64 ImageFileValue; +typedef kfloat64* ImageFileColumn; +typedef kfloat64** ImageFileArray; +typedef const kfloat64* const * ImageFileArrayConst; +#else +typedef F32Image ImageFileBase; +typedef kfloat32 ImageFileValue; +typedef kfloat32* ImageFileColumn; +typedef kfloat32** ImageFileArray; +typedef const kfloat32* const * ImageFileArrayConst; +#endif -template -void -Array2dFile::labelAdd (const char* const lstr, double calc_time=0.) +class ImageFile : public ImageFileBase { - labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time); -} + public: + ImageFile (int nx, int ny) + : ImageFileBase (nx, ny) + {} -template -void -Array2dFile::labelAdd (int type, const char* const lstr, double calc_time=0.) -{ - Array2dFileLabel label (type, lstr, calc_time); + ImageFile (void) + : ImageFileBase () + {} - labelAdd (label); -} + void filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param); -template -void -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); - - num_labels++; - - headerWrite(); - fsync(file_id); -} + void statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const; + void getMinMax (double& min, double& max) const; -#endif - -#ifdef MPI_CT -#include -#endif + void printStatistics (ostream& os) const; -class F32Image -{ -public: - Array2dFile adf; + bool comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const; - F32Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny) - { - adf.setPixelType (Array2dFile::FLOAT32); - } + bool printComparativeStatistics (const ImageFile& imComp, ostream& os) const; - F32Image (unsigned int nx, unsigned int ny) : adf (nx, ny) - { - adf.setPixelType (Array2dFile::FLOAT32); - } + int display (void) const; - F32Image (const char* const fname) : adf (fname) - { - adf.setPixelType (Array2dFile::FLOAT32); - } + int displayScaling (const int scaleFactor, ImageFileValue pmin, ImageFileValue pmax) const; -#ifdef MPI_CT - MPI_Datatype getMPIDataType (void) const - { return MPI_FLOAT; } +#if HAVE_PNG + void writeImagePNG (const char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax); #endif -}; - -class F64Image -{ -public: - Array2dFile adf; - -#ifdef MPI_CT - MPI_Datatype getMPIDataType (void) const - { return MPI_DOUBLE; } +#if HAVE_GD + void writeImageGIF (const char *outfile, int nxcell, int nycell, double densmin, double densmax); #endif - - F64Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny) - { - adf.setPixelType (Array2dFile::FLOAT64); - } - - F64Image (unsigned int nx, unsigned int ny) : adf (nx, ny) - { - adf.setPixelType (Array2dFile::FLOAT64); - } - - F64Image (const char* const fname) : adf (fname) - { - adf.setPixelType (Array2dFile::FLOAT64); - } + void writeImagePGM (const char *outfile, int nxcell, int nycell, double densmin, double densmax); + void writeImagePGMASCII (const char *outfile, int nxcell, int nycell, double densmin, double densmax); }; -typedef F64Image ImageFile; -typedef kfloat64 ImageFileValue; -typedef kfloat64* ImageFileColumn; -typedef kfloat64** ImageFileArray; - - +#endif