DGtal  0.9.2
PPMReader.ih
1 /**
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.
6  *
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.
11  *
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/>.
14  *
15  **/
16 
17 /**
18  * @file PPMReader.ih
19  * @author Bertrand Kerautret (\c kerautre@loria.fr )
20  * LORIA (CNRS, UMR 7503), University of Nancy, France
21  *
22  * @date 2011/04/29
23  *
24  * Implementation of inline methods defined in PPMReader.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 //////////////////////////////////////////////////////////////////////////////
34 #include <cstdlib>
35 #include <iostream>
36 #include <fstream>
37 #include <sstream>
38 
39 #include "DGtal/io/Color.h"
40 //////////////////////////////////////////////////////////////////////////////
41 
42 
43 
44 ///////////////////////////////////////////////////////////////////////////////
45 // Implementation of inline methods //
46 
47 
48 ///////////////////////////////////////////////////////////////////////////////
49 // Implementation of inline functions and external operators //
50 
51 
52 template <typename TImageContainer, typename TFunctor>
53 inline
54 TImageContainer
55 DGtal::PPMReader<TImageContainer, TFunctor>::importPPM(const std::string & aFilename,
56  const TFunctor &aFunctor,
57  bool topbotomOrder ) throw(DGtal::IOException)
58 {
59  std::ifstream infile;
60  DGtal::IOException dgtalio;
61  BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 2));
62  try
63  {
64  infile.open (aFilename.c_str(), std::ifstream::in);
65  }
66  catch( ... )
67  {
68  trace.error() << "PPMReader : can't open " << aFilename << std::endl;
69  throw dgtalio;
70  }
71  bool isASCIImode = false;
72  std::string str;
73  getline( infile, str );
74  if ( ! infile.good() )
75  {
76  trace.error() << "PPMReader : can't read " << aFilename << std::endl;
77  throw dgtalio;
78  }
79  if ( str != "P6" && str != "P3")
80  {
81  trace.error() << "PPMReader : No P6 or P3 format in " << aFilename << std::endl;
82  throw dgtalio;
83  }
84  if(str== "P3")
85  {
86  isASCIImode=true;
87  }
88  do
89  {
90  getline( infile, str );
91  if ( ! infile.good() )
92  {
93  trace.error() << "PPMReader : Invalid format in " << aFilename << std::endl;
94  throw dgtalio;
95  }
96  }
97  while ( str[ 0 ] == '#' || str=="");
98  std::istringstream str_in( str );
99  unsigned int w, h;
100  str_in >> w >> h;
101 
102 
103  typename TImageContainer::Point firstPoint;
104  typename TImageContainer::Point lastPoint;
105 
106  firstPoint = TImageContainer::Point::zero;
107  lastPoint[0] = w-1;
108  lastPoint[1] = h-1;
109 
110  typename TImageContainer::Domain domain(firstPoint,lastPoint);
111  TImageContainer image(domain);
112 
113  getline( infile, str );
114  std::istringstream str2_in( str );
115  unsigned int max_value;
116  str2_in >> max_value;
117 
118  if ( ! infile.good() )
119  {
120  trace.error() << "PPMReader : Invalid format in " << aFilename << std::endl;
121  throw dgtalio;
122  }
123 
124  unsigned int nb_read = 0;
125  if(!isASCIImode)
126  infile >> std::noskipws;
127  else
128  infile >> std::skipws;
129 
130  for(unsigned int y=0; y <h; y++)
131  for(unsigned int x=0; x <w; x++)
132  {
133  typename TImageContainer::Point pt;
134  if (topbotomOrder){
135  pt[0]=x; pt[1]=h-1-y;
136  }else{
137  pt[0]=x; pt[1]=y;
138  }
139 
140 
141  if(!isASCIImode)
142  {
143  unsigned char r,g,b;
144  infile >> r; infile >> g; infile >> b;
145  if ( infile.good() )
146  {
147  Color aColor(r,g,b);
148  nb_read+=3;
149  image.setValue( pt, aFunctor(aColor) );
150  }
151  }
152  else
153  {
154  int r,g,b;
155  infile >> r; infile >> g; infile >> b;
156  if ( infile.good() )
157  {
158  Color aColor((unsigned char)r, (unsigned char)g, (unsigned char)b);
159  nb_read+=3;
160  image.setValue( pt, aFunctor(aColor) );
161  }
162  }
163  }
164  if ( infile.fail() || infile.bad() )
165  {
166  trace.error() << "# nbread=" << nb_read << std::endl;
167  throw dgtalio;
168  }
169  infile >> std::skipws;
170  return image;
171 }
172 
173 
174 
175 // //
176 ///////////////////////////////////////////////////////////////////////////////
177 
178