X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsim%2Farray2dfile.cpp;h=88e11e16c236a7a7a68e228978e7f7094a4c82ca;hp=ca45d8b820eaea485272a6e6f824243c01ac0b89;hb=c0f892798de8f89715266150f7d8e413f2cf29fe;hpb=67a6c34b5a6f38d34e8cbe66091853f453fd2d7a diff --git a/libctsim/array2dfile.cpp b/libctsim/array2dfile.cpp index ca45d8b..88e11e1 100644 --- a/libctsim/array2dfile.cpp +++ b/libctsim/array2dfile.cpp @@ -7,9 +7,9 @@ ** Date Started: June 2000 ** ** This is part of the CTSim program -** Copyright (C) 1983-2000 Kevin Rosenberg +** Copyright (c) 1983-2001 Kevin Rosenberg ** -** $Id: array2dfile.cpp,v 1.22 2000/12/29 19:30:08 kevin Exp $ +** $Id: array2dfile.cpp,v 1.29 2002/05/28 18:43:16 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 @@ -26,9 +26,9 @@ ******************************************************************************/ #include "array2dfile.h" -#include -#ifdef MSVC -typedef long off_t; +#include +#ifdef MSVC +typedef long off_t; #endif @@ -108,8 +108,8 @@ Array2dFileLabel::getDateTime (int& year, int& month, int& day, int& hour, int& const std::string& Array2dFileLabel::getDateString (void) const { - char szDate [128]; - snprintf (szDate, sizeof(szDate), "%2d/%02d/%4d %02d:%02d:%02d", + char szDate [128]; + snprintf (szDate, sizeof(szDate), "%2d/%02d/%4d %02d:%02d:%02d", m_month + 1, m_day, m_year + 1900, m_hour, m_minute, m_second); m_strDate = szDate; return m_strDate; @@ -154,6 +154,36 @@ Array2dFileLabel::print (std::ostream& os) const } } +void +Array2dFileLabel::printBrief (std::ostream& os) const +{ + if (m_labelType == L_HISTORY) { + os << "History ("; + if (m_calcTime > 0) + os << m_calcTime << " secs, "; + os << getDateString() << "): " << m_strLabel << std::endl; + } else if (m_labelType == L_USER) { + os << "Note (" << getDateString() << "): " << m_strLabel << std::endl; + } else { + os << "Unknown (" << getDateString() << "): " << m_strLabel << std::endl; + } +} + +void +Array2dFileLabel::printBrief (std::ostringstream& os) const +{ + if (m_labelType == L_HISTORY) { + os << "History ("; + if (m_calcTime > 0) + os << m_calcTime << " secs, "; + os << getDateString().c_str() << "): " << m_strLabel.c_str() << "\n"; + } else if (m_labelType == L_USER) { + os << "Note (" << getDateString() << "): " << m_strLabel << "\n"; + } else { + os << "Unknown (" << getDateString() << "): " << m_strLabel << "\n"; + } +} + /////////////////////////////////////////////////////////////////////////// // CLASS IMPLEMENTATION @@ -186,8 +216,8 @@ Array2dFile::init (void) { m_pixelSize = 0; m_pixelFormat = PIXEL_INVALID; - m_arrayData = NULL; - m_imaginaryArrayData = NULL; + m_arrayData = NULL; + m_imaginaryArrayData = NULL; m_dataType = DATA_TYPE_INVALID; m_nx = 0; m_ny = 0; @@ -205,7 +235,7 @@ void Array2dFile::setArraySize (int x, int y, int pixelSize, int pixelFormat, int dataType) { m_pixelSize = pixelSize; - m_pixelFormat = pixelFormat; + m_pixelFormat = pixelFormat; m_dataType = dataType; setArraySize (x, y); } @@ -216,78 +246,78 @@ Array2dFile::setArraySize (int x, int y) m_nx = x; m_ny = y; allocArrays (); -} - -bool -Array2dFile::reallocComplexToReal () -{ - if (m_dataType != DATA_TYPE_COMPLEX) - return false; - - freeArray (m_imaginaryArrayData); - m_dataType = DATA_TYPE_REAL; - - return true; -} - - -bool -Array2dFile::reallocRealToComplex () -{ - if (m_dataType != DATA_TYPE_REAL) - return false; - - allocArray (m_imaginaryArrayData); - m_dataType = DATA_TYPE_COMPLEX; - - return true; -} - +} + +bool +Array2dFile::reallocComplexToReal () +{ + if (m_dataType != DATA_TYPE_COMPLEX) + return false; + + freeArray (m_imaginaryArrayData); + m_dataType = DATA_TYPE_REAL; + + return true; +} + + +bool +Array2dFile::reallocRealToComplex () +{ + if (m_dataType != DATA_TYPE_REAL) + return false; + + allocArray (m_imaginaryArrayData); + m_dataType = DATA_TYPE_COMPLEX; + + return true; +} + void Array2dFile::allocArrays () { - if (m_arrayData) - freeArray (m_arrayData); - if (m_imaginaryArrayData) - freeArray (m_imaginaryArrayData); - - allocArray (m_arrayData); - if (m_dataType == DATA_TYPE_COMPLEX) - allocArray (m_imaginaryArrayData); -} - -void -Array2dFile::allocArray (unsigned char**& rppData) -{ + if (m_arrayData) + freeArray (m_arrayData); + if (m_imaginaryArrayData) + freeArray (m_imaginaryArrayData); + + allocArray (m_arrayData); + if (m_dataType == DATA_TYPE_COMPLEX) + allocArray (m_imaginaryArrayData); +} + +void +Array2dFile::allocArray (unsigned char**& rppData) +{ m_arraySize = m_nx * m_ny * m_pixelSize; - rppData = new unsigned char* [m_nx]; + rppData = new unsigned char* [m_nx]; int columnBytes = m_ny * m_pixelSize; for (unsigned int i = 0; i < m_nx; i++) - rppData[i] = new unsigned char [columnBytes]; + rppData[i] = new unsigned char [columnBytes]; } void Array2dFile::freeArrays () { - if (m_arrayData) - freeArray (m_arrayData); - - if (m_imaginaryArrayData) - freeArray (m_imaginaryArrayData); - -} - -void -Array2dFile::freeArray (unsigned char**& rppData) -{ - for (unsigned int i = 0; i < m_nx; i++) - delete rppData[i]; - delete rppData; - rppData = NULL; -} - + if (m_arrayData) + freeArray (m_arrayData); + + if (m_imaginaryArrayData) + freeArray (m_imaginaryArrayData); + +} + +void +Array2dFile::freeArray (unsigned char**& rppData) +{ + for (unsigned int i = 0; i < m_nx; i++) + delete rppData[i]; + delete rppData; + rppData = NULL; +} + bool Array2dFile::fileWrite (const std::string& filename) @@ -328,11 +358,8 @@ Array2dFile::fileRead (const char* const filename) { m_filename = filename; -#ifdef MSVC - frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary); -#else - frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary | std::ios::nocreate); -#endif + frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary); + if (fs.fail()) { sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_filename.c_str()); return false; @@ -387,7 +414,7 @@ Array2dFile::headerRead (frnetorderstream& fs) fs.readInt16 (m_pixelSize); fs.readInt16 (m_numFileLabels); fs.readInt32 (m_nx); - fs.readInt32 (m_ny); + fs.readInt32 (m_ny); fs.readInt16 (m_dataType); fs.readInt16 (m_axisIncrementKnown); fs.readFloat64 (m_axisIncrementX); @@ -410,7 +437,7 @@ Array2dFile::headerRead (frnetorderstream& fs) return false; } - return true; + return ! fs.fail(); } @@ -431,7 +458,7 @@ Array2dFile::headerWrite (frnetorderstream& fs) fs.writeInt16 (m_pixelSize); fs.writeInt16 (m_numFileLabels); fs.writeInt32 (m_nx); - fs.writeInt32 (m_ny); + fs.writeInt32 (m_ny); fs.writeInt16 (m_dataType); fs.writeInt16 (m_axisIncrementKnown); fs.writeFloat64 (m_axisIncrementX); @@ -448,7 +475,7 @@ Array2dFile::headerWrite (frnetorderstream& fs) fs.seekp (0); fs.writeInt16 (m_headersize); - return true; + return ! fs.fail(); } @@ -465,30 +492,30 @@ Array2dFile::arrayDataWrite (frnetorderstream& fs) fs.seekp (m_headersize); int columnSize = m_ny * m_pixelSize; - for (unsigned int ix = 0; ix < m_nx; ix++) { - unsigned char* ptrColumn = m_arrayData[ix]; - if (NativeBigEndian()) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - ConvertReverseNetworkOrder (ptrColumn, m_pixelSize); - fs.write (reinterpret_cast(ptrColumn), m_pixelSize); - ptrColumn += m_pixelSize; - } - } else - fs.write (reinterpret_cast(ptrColumn), columnSize); - } - if (m_dataType == DATA_TYPE_COMPLEX) { - for (unsigned int ix = 0; ix < m_nx; ix++) { - unsigned char* ptrColumn = m_imaginaryArrayData[ix]; - if (NativeBigEndian()) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - ConvertReverseNetworkOrder (ptrColumn, m_pixelSize); - fs.write (reinterpret_cast(ptrColumn), m_pixelSize); - ptrColumn += m_pixelSize; - } - } else - fs.write (reinterpret_cast(ptrColumn), columnSize); - } - } + for (unsigned int ix = 0; ix < m_nx; ix++) { + unsigned char* ptrColumn = m_arrayData[ix]; + if (NativeBigEndian()) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + ConvertReverseNetworkOrder (ptrColumn, m_pixelSize); + fs.write (reinterpret_cast(ptrColumn), m_pixelSize); + ptrColumn += m_pixelSize; + } + } else + fs.write (reinterpret_cast(ptrColumn), columnSize); + } + if (m_dataType == DATA_TYPE_COMPLEX) { + for (unsigned int ix = 0; ix < m_nx; ix++) { + unsigned char* ptrColumn = m_imaginaryArrayData[ix]; + if (NativeBigEndian()) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + ConvertReverseNetworkOrder (ptrColumn, m_pixelSize); + fs.write (reinterpret_cast(ptrColumn), m_pixelSize); + ptrColumn += m_pixelSize; + } + } else + fs.write (reinterpret_cast(ptrColumn), columnSize); + } + } return true; } @@ -507,30 +534,30 @@ Array2dFile::arrayDataRead (frnetorderstream& fs) fs.seekg (m_headersize); int columnSize = m_ny * m_pixelSize; - for (unsigned int ix = 0; ix < m_nx; ix++) { - unsigned char* ptrColumn = m_arrayData[ix]; - if (NativeBigEndian()) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - fs.read (reinterpret_cast(ptrColumn), m_pixelSize); - ConvertReverseNetworkOrder (ptrColumn, m_pixelSize); - ptrColumn += m_pixelSize; - } - } else - fs.read (reinterpret_cast(ptrColumn), columnSize); - } - if (m_dataType == DATA_TYPE_COMPLEX) { - for (unsigned int ix = 0; ix < m_nx; ix++) { - unsigned char* ptrColumn = m_imaginaryArrayData[ix]; - if (NativeBigEndian()) { - for (unsigned int iy = 0; iy < m_ny; iy++) { - fs.read (reinterpret_cast(ptrColumn), m_pixelSize); - ConvertReverseNetworkOrder (ptrColumn, m_pixelSize); - ptrColumn += m_pixelSize; - } - } else - fs.read (reinterpret_cast(ptrColumn), columnSize); - } - } + for (unsigned int ix = 0; ix < m_nx; ix++) { + unsigned char* ptrColumn = m_arrayData[ix]; + if (NativeBigEndian()) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + fs.read (reinterpret_cast(ptrColumn), m_pixelSize); + ConvertReverseNetworkOrder (ptrColumn, m_pixelSize); + ptrColumn += m_pixelSize; + } + } else + fs.read (reinterpret_cast(ptrColumn), columnSize); + } + if (m_dataType == DATA_TYPE_COMPLEX) { + for (unsigned int ix = 0; ix < m_nx; ix++) { + unsigned char* ptrColumn = m_imaginaryArrayData[ix]; + if (NativeBigEndian()) { + for (unsigned int iy = 0; iy < m_ny; iy++) { + fs.read (reinterpret_cast(ptrColumn), m_pixelSize); + ConvertReverseNetworkOrder (ptrColumn, m_pixelSize); + ptrColumn += m_pixelSize; + } + } else + fs.read (reinterpret_cast(ptrColumn), columnSize); + } + } return true; } @@ -563,10 +590,10 @@ Array2dFile::labelsRead (frnetorderstream& fs) pszLabelStr[strLength] = 0; Array2dFileLabel* pLabel = new Array2dFileLabel (labelType, pszLabelStr, calcTime); - delete pszLabelStr; - + delete pszLabelStr; + pLabel->setDateTime (year, month, day, hour, minute, second); - m_labels.push_back (pLabel); + m_labels.push_back (pLabel); } return true; @@ -631,7 +658,7 @@ Array2dFile::labelAdd (const Array2dFileLabel& label) } void -Array2dFile::labelsCopy (Array2dFile& copyFile, const char* const pszId) +Array2dFile::labelsCopy (const Array2dFile& copyFile, const char* const pszId) { std::string id; if (pszId) @@ -648,16 +675,16 @@ Array2dFile::labelsCopy (Array2dFile& copyFile, const char* const pszId) void Array2dFile::arrayDataClear (void) { - if (m_arrayData) { - int columnSize = m_ny * m_pixelSize; - for (unsigned int ix = 0; ix < m_nx; ix++) - memset (m_arrayData[ix], 0, columnSize); - } - if (m_imaginaryArrayData) { - int columnSize = m_ny * m_pixelSize; - for (unsigned int ix = 0; ix < m_nx; ix++) - memset (m_arrayData[ix], 0, columnSize); - } + if (m_arrayData) { + int columnSize = m_ny * m_pixelSize; + for (unsigned int ix = 0; ix < m_nx; ix++) + memset (m_arrayData[ix], 0, columnSize); + } + if (m_imaginaryArrayData) { + int columnSize = m_ny * m_pixelSize; + for (unsigned int ix = 0; ix < m_nx; ix++) + memset (m_arrayData[ix], 0, columnSize); + } } void @@ -671,6 +698,26 @@ Array2dFile::printLabels (std::ostream& os) const } } +void +Array2dFile::printLabelsBrief (std::ostream& os) const +{ + for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) { + const Array2dFileLabel& label = **l; + + label.printBrief (os); + } +} + +void +Array2dFile::printLabelsBrief (std::ostringstream& os) const +{ + for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) { + const Array2dFileLabel& label = **l; + + label.printBrief (os); + } +} + const Array2dFileLabel& Array2dFile::labelGet (int i) const