2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @author Bertrand Kerautret (\c kerautre@loria.fr )
20 * LORIA (CNRS, UMR 7503), University of Nancy, France
24 * Implementation of inline methods defined in PGMReader.h
26 * This file is part of the DGtal library.
29///////////////////////////////////////////////////////////////////////////////
30// IMPLEMENTATION of inline methods.
31///////////////////////////////////////////////////////////////////////////////
33//////////////////////////////////////////////////////////////////////////////
39#include "DGtal/io/Color.h"
40//////////////////////////////////////////////////////////////////////////////
44///////////////////////////////////////////////////////////////////////////////
45// Implementation of inline methods //
48///////////////////////////////////////////////////////////////////////////////
49// Implementation of inline functions and external operators //
51template <typename TImageContainer, typename TFunctor>
54DGtal::PGMReader<TImageContainer, TFunctor>::importPGM(const std::string & aFilename,
55 const TFunctor &aFunctor,
59 DGtal::IOException dgtalio;
60 BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 2));
63 infile.open (aFilename.c_str(), std::ifstream::in | std::ifstream::binary);
67 trace.error() << "PGMReader : can't open " << aFilename << std::endl;
70 bool isASCIImode = false;
72 getline( infile, str );
73 if ( ! infile.good() )
75 trace.error() << "PGMReader : can't read " << aFilename << std::endl;
78 str = str.substr(0, 2);
79 if ( str != "P5" && str != "P2")
81 trace.error() << "PGMReader : No P5 or P2 format in " << aFilename << std::endl;
90 getline( infile, str );
91 if ( ! infile.good() )
93 trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
97 while ( str[ 0 ] == '#' || str=="");
98 std::istringstream str_in( str );
103 typename TImageContainer::Point firstPoint;
104 typename TImageContainer::Point lastPoint;
106 firstPoint = TImageContainer::Point::zero;
110 typename TImageContainer::Domain domain(firstPoint,lastPoint);
111 TImageContainer image(domain);
113 getline( infile, str );
114 std::istringstream str2_in( str );
115 unsigned int max_value;
116 str2_in >> max_value;
118 if ( ! infile.good() )
120 trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
124 unsigned int nb_read = 0;
126 infile >> std::noskipws;
128 infile >> std::skipws;
130 for(unsigned int y=0; y <h; y++)
131 for(unsigned int x=0; x <w; x++)
133 typename TImageContainer::Point pt;
135 pt[0]=x; pt[1]=h-1-y;
148 image.setValue( pt, aFunctor(c));
158 image.setValue( pt, aFunctor(c));
162 if ( infile.fail() || infile.bad() )
164 trace.error() << "# nbread=" << nb_read << std::endl;
167 infile >> std::skipws;
173template <typename TImageContainer, typename TFunctor>
176DGtal::PGMReader<TImageContainer,TFunctor>::importPGM3D(const std::string & aFilename,
177 const TFunctor &aFunctor)
179 std::ifstream infile;
180 DGtal::IOException dgtalio;
181 BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 3));
184 infile.open (aFilename.c_str(), std::ifstream::in | std::ifstream::binary);
188 trace.error() << "PGMReader : can't open " << aFilename << std::endl;
193 getline( infile, str );
194 if ( ! infile.good() ) {
195 trace.error() << "PGMReader : can't read " << aFilename << std::endl;
199 if ( str.compare( 0, 2, "P2" ) != 0 && str.compare( 0, 2, "P3" ) != 0 && str.compare( 0, 2, "P5" ) != 0 ){
200 trace.error() << "PGMReader : Wrong format in " << aFilename << std::endl;
204 bool isASCIImode = ( str.compare( 0, 2, "P2" ) == 0 );
206 infile >> std::noskipws;
208 infile >> std::skipws;
212 getline( infile, str );
213 if ( ! infile.good() ){
214 trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
218 while ( str[ 0 ] == '#' || str=="");
219 std::istringstream str_in( str );
220 unsigned int w, h, e;
221 str_in >> w >> h >> e ;
224 typename TImageContainer::Point firstPoint;
225 typename TImageContainer::Point lastPoint;
227 firstPoint = TImageContainer::Point::zero;
232 typename TImageContainer::Domain domain(firstPoint,lastPoint);
233 TImageContainer image(domain);
235 getline( infile, str );
236 std::istringstream str2_in( str );
238 str2_in >> max_value;
240 if ( ! infile.good() ){
241 trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
244 unsigned int nb_read = 0;
246 for(unsigned int z=0; z <e; z++){
247 for(unsigned int y=0; y <h; y++){
248 for(unsigned int x=0; x <w; x++){
249 typename TImageContainer::Point pt;
250 pt[0]=x; pt[1]=y; pt[2]=z;
259 image.setValue( pt, aFunctor(c));
269 image.setValue( pt, aFunctor(c));
275 if ( infile.fail() || infile.bad() )
277 trace.error() << "# nbread=" << nb_read << std::endl;
280 infile >> std::skipws;
287///////////////////////////////////////////////////////////////////////////////