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 PPMReader.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 //
52 template <typename TImageContainer, typename TFunctor>
55 DGtal::PPMReader<TImageContainer, TFunctor>::importPPM(const std::string & aFilename,
56 const TFunctor &aFunctor,
60 DGtal::IOException dgtalio;
61 BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 2));
64 infile.open (aFilename.c_str(), std::ifstream::in | std::ifstream::binary);
68 trace.error() << "PPMReader : can't open " << aFilename << std::endl;
71 bool isASCIImode = false;
73 getline( infile, str );
74 if ( ! infile.good() )
76 trace.error() << "PPMReader : can't read " << aFilename << std::endl;
79 if ( str != "P6" && str != "P3")
81 trace.error() << "PPMReader : No P6 or P3 format in " << aFilename << std::endl;
90 getline( infile, str );
91 if ( ! infile.good() )
93 trace.error() << "PPMReader : 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() << "PPMReader : 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;
144 infile >> r; infile >> g; infile >> b;
149 image.setValue( pt, aFunctor(aColor) );
155 infile >> r; infile >> g; infile >> b;
158 Color aColor((unsigned char)r, (unsigned char)g, (unsigned char)b);
160 image.setValue( pt, aFunctor(aColor) );
164 if ( infile.fail() || infile.bad() )
166 trace.error() << "# nbread=" << nb_read << std::endl;
169 infile >> std::skipws;
176 ///////////////////////////////////////////////////////////////////////////////