Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

PgmFile.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------+
00002  | Library QgarLib, graphics analysis and recognition                  |
00003  | Copyright (C) 2002  Qgar Project, LORIA                             |
00004  |                                                                     |
00005  | This library is free software; you can redistribute it and/or       |
00006  | modify it under the terms of the GNU Lesser General Public          |
00007  | License version 2.1, as published by the Free Software Foundation.  |
00008  |                                                                     |
00009  | This library is distributed in the hope that it will be useful,     |
00010  | but WITHOUT ANY WARRANTY; without even the implied warranty of      |
00011  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                |
00012  | See the GNU Lesser General Public License for more details.         |
00013  |                                                                     |
00014  | The GNU Lesser General Public License is included in the file       |
00015  | LICENSE.LGPL, in the root directory of the Qgar packaging. See      |
00016  | http://www.gnu.org/licenses/lgpl.html for the terms of the licence. |
00017  | To receive a paper copy, write to the Free Software Foundation,     |
00018  | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.       |
00019  |                                                                     |
00020  | Contact Project Qgar for any information:                           |
00021  |   LORIA - équipe Qgar                                               |
00022  |   B.P. 239, 54506 Vandoeuvre-lès-Nancy Cedex, France                |
00023  |   email: qgar-contact@loria.fr                                      |
00024  |   http://www.qgar.org/                                              |
00025  *---------------------------------------------------------------------*/
00026 
00027 
00028 /**
00029  * @file   PgmFile.C
00030  * @brief  Implementation of class qgar::PgmFile.
00031  *
00032  *         See file PgmFile.H for the interface.
00033  *
00034  * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Karl Tombre">Karl Tombre</a>
00035  * @date   Jul 3, 2001  16:57
00036  * @since  Qgar 1.0
00037  */
00038 
00039 
00040 // Most of the code documented here is an adaptation of code from the PBM
00041 // software package. Here is the copyright notice of this package:
00042 //
00043 // +------------------------------------------------------------------------+
00044 // | Copyright (C) 1988 by Jef Poskanzer                                    |
00045 // |                                                                        |
00046 // | Permission to use, copy, modify, and distribute this software and its  |
00047 // | documentation for any purpose and without fee is hereby granted,       |
00048 // | provided that the above copyright notice appear in all copies and that |
00049 // | both that copyright notice and this permission notice appear in        |
00050 // | supporting documentation.  This software is provided "as is" without   |
00051 // | express or implied warranty.                                           |
00052 // +------------------------------------------------------------------------+
00053 
00054 
00055 
00056 // STD
00057 #include <sstream>
00058 // QGAR
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 // I N I T I A L I Z A T I O N S   O F   S T A T I C   D A T A 
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 // C O N S T R U C T O R S 
00090 // -------------------------------------------------------------------
00091 
00092 
00093 // INITIALIZE FROM FILE NAME AND FORMAT
00094 
00095 PgmFile::PgmFile(const char* aFileName, QGEpbmFormat aFormat)
00096 
00097     : AbstractPbmPlusFile(aFileName, 0, 0, aFormat),
00098       _maxPix(0)
00099 
00100 {
00101   // VOID
00102 }
00103 
00104 
00105 // INITIALIZE FROM FULL DATA
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   // VOID
00118 }
00119 
00120 
00121 // -------------------------------------------------------------------
00122 // I N P U T 
00123 // -------------------------------------------------------------------
00124 
00125 
00126 // READ FILE HEADER
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 // READ A ROW FROM THE FILE
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 (/* VOID */ ; 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     } // END switch
00217 }
00218 
00219 
00220 // -------------------------------------------------------------------
00221 // O U T P U T 
00222 // -------------------------------------------------------------------
00223 
00224 
00225 // WRITE FILE HEADER
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 // WRITE A ROW INTO THE FILE
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 // WRITE FILE FOOTER
00303 
00304 void
00305 PgmFile::writeFooter()
00306 {
00307   // VOID
00308 }
00309 
00310 
00311 // PUT AN unsigned char INTO THE FILE
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 } // namespace qgar