00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include <sstream>
00057
00058 #include <qgarlib/AbstractPbmPlusFile.H>
00059 #include <qgarlib/QgarErrorInvalidArg.H>
00060 #include <qgarlib/QgarErrorIO.H>
00061
00062
00063
00064 namespace qgar
00065 {
00066
00067
00068
00069
00070
00071
00072 AbstractPbmPlusFile::AbstractPbmPlusFile(const char* aFileName,
00073 int aRowCnt,
00074 int aColCnt,
00075 QGEpbmFormat aFormat)
00076 throw(QgarErrorInvalidArg)
00077
00078 : AbstractFile(aFileName),
00079 _format(aFormat),
00080 _rowCnt(aRowCnt),
00081 _colCnt(aColCnt),
00082 _pRow(0)
00083
00084 {
00085
00086 if ((aRowCnt < 0) || (aColCnt < 0))
00087 {
00088 std::ostringstream os;
00089 os << "Cannot create file "
00090 << _fileName
00091 << ": Bad image size ("
00092 << aRowCnt
00093 << " rows, "
00094 << aColCnt
00095 << " columns)";
00096 throw QgarErrorInvalidArg(__FILE__, __LINE__,
00097 "qgar::AbstractPbmPlusFile::AbstractPbmPlusFile(const char*, int, int, qgar::QGEpbmFormat)",
00098 os.str());
00099 }
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 AbstractPbmPlusFile::~AbstractPbmPlusFile()
00109 {
00110 if (_pRow != 0)
00111 {
00112 delete [] _pRow;
00113 }
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 char
00125 AbstractPbmPlusFile::getChar()
00126
00127 throw(QgarErrorIO)
00128
00129 {
00130 int intC = _fileStream.get();
00131 if (intC == EOF)
00132 {
00133 std::ostringstream os;
00134 os << "EOF while reading in file "
00135 << _fileName;
00136 throw QgarErrorIO(__FILE__, __LINE__,
00137 "char qgar::AbstractPbmPlusFile::getChar()",
00138 os.str());
00139 }
00140
00141 char c = (char) intC;
00142
00143 if (c == '#')
00144 {
00145 do
00146 {
00147 intC = _fileStream.get();
00148 if (intC == EOF)
00149 {
00150 std::ostringstream os;
00151 os << "EOF while reading in file "
00152 << _fileName;
00153 throw QgarErrorIO(__FILE__, __LINE__,
00154 "char qgar::AbstractPbmPlusFile::getChar()",
00155 os.str());
00156 }
00157 c = (char) intC;
00158 }
00159 while ((c != '\n') && (c != '\r'));
00160 }
00161
00162 return c;
00163 }
00164
00165
00166
00167
00168 int
00169 AbstractPbmPlusFile::getInt()
00170
00171 throw(QgarErrorIO)
00172
00173 {
00174 char c;
00175 do
00176 {
00177 c = getChar();
00178 }
00179 while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
00180
00181 if ((c < '0') || (c > '9'))
00182 {
00183 std::ostringstream os;
00184 os << "Error while reading in file "
00185 << _fileName
00186 << ": Integer in [0,9] expected";
00187 throw QgarErrorIO(__FILE__, __LINE__,
00188 "int qgar::AbstractPbmPlusFile::getInt()",
00189 os.str());
00190 }
00191
00192 int i = 0;
00193 do
00194 {
00195 i = (i * 10) + c - '0';
00196 c = getChar();
00197 }
00198 while ((c >= '0') && (c <= '9'));
00199
00200 return i;
00201 }
00202
00203
00204
00205
00206 unsigned char
00207 AbstractPbmPlusFile::getRawByte()
00208
00209 throw(QgarErrorIO)
00210
00211 {
00212 int iByte = _fileStream.get();
00213
00214 if (iByte == EOF)
00215 {
00216 std::ostringstream os;
00217 os << "EOF while reading in file "
00218 << _fileName;
00219 throw QgarErrorIO(__FILE__, __LINE__,
00220 "unsigned char qgar::AbstractPbmPlusFile::getRawByte()",
00221 os.str());
00222 }
00223
00224 return (unsigned char)iByte;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233 void
00234 AbstractPbmPlusFile::newRow()
00235 {
00236 if (_pRow != 0)
00237 {
00238 delete [] _pRow;
00239 }
00240 _pRow = new unsigned char[_colCnt];
00241 }
00242
00243
00244
00245
00246 }