DGtal  1.0.0
PGMReader.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 PGMReader.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 PGMReader.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 template <typename TImageContainer, typename TFunctor>
52 inline
53 TImageContainer
54 DGtal::PGMReader<TImageContainer, TFunctor>::importPGM(const std::string & aFilename,
55  const TFunctor &aFunctor,
56  bool topbotomOrder )
57 {
58  std::ifstream infile;
59  DGtal::IOException dgtalio;
60  BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 2));
61  try
62  {
63  infile.open (aFilename.c_str(), std::ifstream::in | std::ifstream::binary);
64  }
65  catch( ... )
66  {
67  trace.error() << "PGMReader : can't open " << aFilename << std::endl;
68  throw dgtalio;
69  }
70  bool isASCIImode = false;
71  std::string str;
72  getline( infile, str );
73  if ( ! infile.good() )
74  {
75  trace.error() << "PGMReader : can't read " << aFilename << std::endl;
76  throw dgtalio;
77  }
78  str = str.substr(0, 2);
79  if ( str != "P5" && str != "P2")
80  {
81  trace.error() << "PGMReader : No P5 or P2 format in " << aFilename << std::endl;
82  throw dgtalio;
83  }
84  if(str== "P2")
85  {
86  isASCIImode=true;
87  }
88  do
89  {
90  getline( infile, str );
91  if ( ! infile.good() )
92  {
93  trace.error() << "PGMReader : 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() << "PGMReader : 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 c;
144  infile >> c;
145  if ( infile.good() )
146  {
147  ++nb_read;
148  image.setValue( pt, aFunctor(c));
149  }
150  }
151  else
152  {
153  int c;
154  infile >> c;
155  if ( infile.good() )
156  {
157  ++nb_read;
158  image.setValue( pt, aFunctor(c));
159  }
160  }
161  }
162  if ( infile.fail() || infile.bad() )
163  {
164  trace.error() << "# nbread=" << nb_read << std::endl;
165  throw dgtalio;
166  }
167  infile >> std::skipws;
168  return image;
169 }
170 
171 
172 
173 template <typename TImageContainer, typename TFunctor>
174 inline
175 TImageContainer
176 DGtal::PGMReader<TImageContainer,TFunctor>::importPGM3D(const std::string & aFilename,
177  const TFunctor &aFunctor)
178 {
179  std::ifstream infile;
180  DGtal::IOException dgtalio;
181  BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 3));
182  try
183  {
184  infile.open (aFilename.c_str(), std::ifstream::in | std::ifstream::binary);
185  }
186  catch( ... )
187  {
188  trace.error() << "PGMReader : can't open " << aFilename << std::endl;
189  throw dgtalio;
190  }
191 
192  std::string str;
193  getline( infile, str );
194  if ( ! infile.good() ) {
195  trace.error() << "PGMReader : can't read " << aFilename << std::endl;
196  throw dgtalio;
197  }
198 
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;
201  throw dgtalio;
202  }
203 
204  bool isASCIImode = ( str.compare( 0, 2, "P2" ) == 0 );
205  if(!isASCIImode)
206  infile >> std::noskipws;
207  else
208  infile >> std::skipws;
209 
210  do
211  {
212  getline( infile, str );
213  if ( ! infile.good() ){
214  trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
215  throw dgtalio;
216  }
217  }
218  while ( str[ 0 ] == '#' || str=="");
219  std::istringstream str_in( str );
220  unsigned int w, h, e;
221  str_in >> w >> h >> e ;
222 
223 
224  typename TImageContainer::Point firstPoint;
225  typename TImageContainer::Point lastPoint;
226 
227  firstPoint = TImageContainer::Point::zero;
228  lastPoint[0] = w-1;
229  lastPoint[1] = h-1;
230  lastPoint[2] = e-1;
231 
232  typename TImageContainer::Domain domain(firstPoint,lastPoint);
233  TImageContainer image(domain);
234 
235  getline( infile, str );
236  std::istringstream str2_in( str );
237  int max_value;
238  str2_in >> max_value;
239 
240  if ( ! infile.good() ){
241  trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
242  throw dgtalio;
243  }
244  unsigned int nb_read = 0;
245 
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;
251 
252  if(!isASCIImode)
253  {
254  unsigned char c;
255  infile >> c;
256  if ( infile.good() )
257  {
258  ++nb_read;
259  image.setValue( pt, aFunctor(c));
260  }
261  }
262  else
263  {
264  int c;
265  infile >> c;
266  if ( infile.good() )
267  {
268  ++nb_read;
269  image.setValue( pt, aFunctor(c));
270  }
271  }
272  }
273  }
274  }
275  if ( infile.fail() || infile.bad() )
276  {
277  trace.error() << "# nbread=" << nb_read << std::endl;
278  throw dgtalio;
279  }
280  infile >> std::skipws;
281  return image;
282 }
283 
284 
285 
286 // //
287 ///////////////////////////////////////////////////////////////////////////////
288 
289