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 //
51 template <typename TImageContainer, typename TFunctor>
54 DGtal::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 if ( str != "P5" && str != "P2")
80 trace.error() << "PGMReader : No P5 or P2 format in " << aFilename << std::endl;
89 getline( infile, str );
90 if ( ! infile.good() )
92 trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
96 while ( str[ 0 ] == '#' || str=="");
97 std::istringstream str_in( str );
102 typename TImageContainer::Point firstPoint;
103 typename TImageContainer::Point lastPoint;
105 firstPoint = TImageContainer::Point::zero;
109 typename TImageContainer::Domain domain(firstPoint,lastPoint);
110 TImageContainer image(domain);
112 getline( infile, str );
113 std::istringstream str2_in( str );
114 unsigned int max_value;
115 str2_in >> max_value;
117 if ( ! infile.good() )
119 trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
123 unsigned int nb_read = 0;
125 infile >> std::noskipws;
127 infile >> std::skipws;
129 for(unsigned int y=0; y <h; y++)
130 for(unsigned int x=0; x <w; x++)
132 typename TImageContainer::Point pt;
134 pt[0]=x; pt[1]=h-1-y;
147 image.setValue( pt, aFunctor(c));
157 image.setValue( pt, aFunctor(c));
161 if ( infile.fail() || infile.bad() )
163 trace.error() << "# nbread=" << nb_read << std::endl;
166 infile >> std::skipws;
172 template <typename TImageContainer, typename TFunctor>
175 DGtal::PGMReader<TImageContainer,TFunctor>::importPGM3D(const std::string & aFilename,
176 const TFunctor &aFunctor)
178 std::ifstream infile;
179 DGtal::IOException dgtalio;
180 BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 3));
183 infile.open (aFilename.c_str(), std::ifstream::in | std::ifstream::binary);
187 trace.error() << "PGMReader : can't open " << aFilename << std::endl;
192 getline( infile, str );
193 if ( ! infile.good() ) {
194 trace.error() << "PGMReader : can't read " << aFilename << std::endl;
198 if ( str.compare( 0, 2, "P2" ) != 0 && str.compare( 0, 2, "P3" ) != 0 && str.compare( 0, 2, "P5" ) != 0 ){
199 trace.error() << "PGMReader : Wrong format in " << aFilename << std::endl;
203 bool isASCIImode = ( str.compare( 0, 2, "P2" ) == 0 );
205 infile >> std::noskipws;
207 infile >> std::skipws;
211 getline( infile, str );
212 if ( ! infile.good() ){
213 trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
217 while ( str[ 0 ] == '#' || str=="");
218 std::istringstream str_in( str );
219 unsigned int w, h, e;
220 str_in >> w >> h >> e ;
223 typename TImageContainer::Point firstPoint;
224 typename TImageContainer::Point lastPoint;
226 firstPoint = TImageContainer::Point::zero;
231 typename TImageContainer::Domain domain(firstPoint,lastPoint);
232 TImageContainer image(domain);
234 getline( infile, str );
235 std::istringstream str2_in( str );
237 str2_in >> max_value;
239 if ( ! infile.good() ){
240 trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
243 unsigned int nb_read = 0;
245 for(unsigned int z=0; z <e; z++){
246 for(unsigned int y=0; y <h; y++){
247 for(unsigned int x=0; x <w; x++){
248 typename TImageContainer::Point pt;
249 pt[0]=x; pt[1]=y; pt[2]=z;
258 image.setValue( pt, aFunctor(c));
268 image.setValue( pt, aFunctor(c));
274 if ( infile.fail() || infile.bad() )
276 trace.error() << "# nbread=" << nb_read << std::endl;
279 infile >> std::skipws;
286 ///////////////////////////////////////////////////////////////////////////////