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
00057 #include <sstream>
00058
00059 #include <qgarlib/AbstractPbmPlusFile.H>
00060 #include <qgarlib/PgmFile.H>
00061 #include <qgarlib/QgarErrorIO.H>
00062
00063
00064
00065 namespace qgar
00066 {
00067
00068
00069
00070
00071
00072
00073 const unsigned char PgmFile::_s_pgm_magic1 = 'P';
00074
00075 const unsigned char PgmFile::_s_pgm_magic2 = '2';
00076
00077 const unsigned short PgmFile::_s_pgm_format =
00078 (((unsigned short) PgmFile::_s_pgm_magic1) * 256)
00079 + (unsigned short) PgmFile::_s_pgm_magic2;
00080
00081 const unsigned char PgmFile::_s_rpgm_magic2 = '5';
00082
00083 const unsigned short PgmFile::_s_rpgm_format =
00084 (((unsigned short) PgmFile::_s_pgm_magic1) * 256)
00085 + (unsigned short) PgmFile::_s_rpgm_magic2;
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 PgmFile::PgmFile(const char* aFileName, QGEpbmFormat aFormat)
00096
00097 : AbstractPbmPlusFile(aFileName, 0, 0, aFormat),
00098 _maxPix(0)
00099
00100 {
00101
00102 }
00103
00104
00105
00106
00107 PgmFile::PgmFile(const char* aFileName,
00108 unsigned int aRowCnt,
00109 unsigned int aColCnt,
00110 unsigned int aMax,
00111 QGEpbmFormat aFormat)
00112
00113 : AbstractPbmPlusFile(aFileName, aRowCnt, aColCnt, aFormat),
00114 _maxPix(aMax)
00115
00116 {
00117
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 void
00129 PgmFile::readHeader()
00130
00131 throw(QgarErrorIO)
00132
00133 {
00134 int c1 = _fileStream.get();
00135 if (c1 == EOF)
00136 {
00137 std::ostringstream os;
00138 os << "EOF while reading in file "
00139 << _fileName;
00140 throw QgarErrorIO(__FILE__, __LINE__,
00141 "void qgar::PgmFile::readHeader()",
00142 os.str());
00143 }
00144
00145 int c2 = _fileStream.get();
00146 if (c2 == EOF)
00147 {
00148 std::ostringstream os;
00149 os << "EOF while reading in file "
00150 << _fileName;
00151 throw QgarErrorIO(__FILE__, __LINE__,
00152 "void qgar::PgmFile::readHeader()",
00153 os.str());
00154 }
00155
00156 unsigned short magicNb = (c1 * 256) + c2;
00157 if (magicNb == _s_pgm_format)
00158 {
00159 _format = QGE_PBM_PLAIN;
00160 }
00161 else if (magicNb == _s_rpgm_format)
00162 {
00163 _format = QGE_PBM_RAW;
00164 }
00165 else
00166 {
00167 std::ostringstream os;
00168 os << "Bad magic number in file "
00169 << _fileName;
00170 throw QgarErrorIO(__FILE__, __LINE__,
00171 "void qgar::PgmFile::readHeader()",
00172 os.str());
00173 }
00174
00175 _colCnt = getInt();
00176 _rowCnt = getInt();
00177 _maxPix = getInt();
00178 if (_maxPix > 255)
00179 {
00180 std::ostringstream os;
00181 os << "Bad max pixel value in file "
00182 << _fileName;
00183 throw QgarErrorIO(__FILE__, __LINE__,
00184 "void qgar::PgmFile::readHeader()",
00185 os.str());
00186 }
00187
00188 newRow();
00189 }
00190
00191
00192
00193
00194 void
00195 PgmFile::readRow()
00196 {
00197 isOpenR();
00198
00199 int col = 0;
00200 unsigned char* pRow = _pRow;
00201
00202 switch(_format)
00203 {
00204 case QGE_PBM_PLAIN:
00205 for ( ; col < _colCnt; ++col, ++pRow)
00206 {
00207 *pRow = (unsigned char) getInt();
00208 }
00209 break;
00210
00211 case QGE_PBM_RAW:
00212 for ( ; col < _colCnt; ++col, ++pRow )
00213 {
00214 *pRow = getRawByte();
00215 }
00216 }
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 void
00228 PgmFile::writeHeader()
00229 {
00230 if (_format == QGE_PBM_RAW)
00231 {
00232 _fileStream << _s_pgm_magic1
00233 << _s_rpgm_magic2
00234 << std::endl;
00235 }
00236 else
00237 {
00238 _fileStream << _s_pgm_magic1
00239 << _s_pgm_magic2
00240 << std::endl;
00241 }
00242
00243 _fileStream << _colCnt
00244 << ' '
00245 << _rowCnt
00246 << std::endl
00247 << _maxPix
00248 << std::endl;
00249
00250 newRow();
00251 }
00252
00253
00254
00255
00256 void
00257 PgmFile::writeRow()
00258 {
00259 isOpenW();
00260
00261 if (_format == QGE_PBM_RAW)
00262 {
00263 int col = 0;
00264 unsigned char* pRow = _pRow;
00265
00266 for ( ; col < _colCnt ; ++col, ++pRow)
00267 {
00268 _fileStream.put(*pRow);
00269 }
00270 }
00271 else
00272 {
00273 int charCnt = 0;
00274 int col = 0;
00275 unsigned char* pRow = _pRow;
00276
00277 for ( ; col < _colCnt ; ++col, ++pRow)
00278 {
00279 if (charCnt >= 65)
00280 {
00281 _fileStream << std::endl;
00282 charCnt = 0;
00283 }
00284 else if (charCnt > 0)
00285 {
00286 _fileStream << ' ';
00287 ++charCnt;
00288 }
00289
00290 putUchar(*pRow);
00291 charCnt += 3;
00292 }
00293
00294 if (charCnt > 0)
00295 {
00296 _fileStream << std::endl;
00297 }
00298 }
00299 }
00300
00301
00302
00303
00304 void
00305 PgmFile::writeFooter()
00306 {
00307
00308 }
00309
00310
00311
00312
00313 void
00314 PgmFile::putUchar(unsigned char anUchar)
00315 {
00316 if (anUchar >= 10)
00317 {
00318 putUchar(anUchar / 10);
00319 }
00320
00321 _fileStream << (char) (anUchar % 10 + '0');
00322 }
00323
00324
00325
00326
00327
00328 }