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

AbstractPbmPlusFile.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   AbstractPbmPlusFile.C
00030  * @brief  Implementation of class qgar::AbstractPbmPlusFile.
00031  *
00032  * See file AbstractPbmPlusFile.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:32
00036  * @since  qgar 1.0
00037  */
00038 
00039 
00040 // Most of the code in this module is an adaptation of code from the PBM
00041 // software package. Here is the copyright notice of this package:
00042 // +------------------------------------------------------------------------+
00043 // | Copyright (C) 1988 by Jef Poskanzer                                    |
00044 // |                                                                        |
00045 // | Permission to use, copy, modify, and distribute this software and its  |
00046 // | documentation for any purpose and without fee is hereby granted,       |
00047 // | provided that the above copyright notice appear in all copies and that |
00048 // | both that copyright notice and this permission notice appear in        |
00049 // | supporting documentation.  This software is provided "as is" without   |
00050 // | express or implied warranty.                                           |
00051 // +------------------------------------------------------------------------+
00052 
00053 
00054 
00055 // STL
00056 #include <sstream>
00057 // QGAR
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 // C O N S T R U C T O R 
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   // WARNING: Tests should be '<=' to be quite correct!
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 // D E S T R U C T O R 
00105 // -------------------------------------------------------------------
00106 
00107 
00108 AbstractPbmPlusFile::~AbstractPbmPlusFile()
00109 {
00110   if (_pRow != 0)
00111     {
00112       delete [] _pRow;
00113     }
00114 }
00115 
00116 
00117 // -------------------------------------------------------------------
00118 // I N P U T 
00119 // -------------------------------------------------------------------
00120 
00121 
00122 // READ A CHARACTER FROM THE FILE
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 // READ AN INTEGER FROM THE FILE
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 // READ A RAW BYTE FROM THE FILE
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 // M E M O R Y   S P A C E   A L L O C A T I O N 
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 } // namespace qgar