DGtal  0.9.2
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 ) throw(DGtal::IOException)
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  if ( str != "P5" && str != "P2")
79  {
80  trace.error() << "PGMReader : No P5 or P2 format in " << aFilename << std::endl;
81  throw dgtalio;
82  }
83  if(str== "P2")
84  {
85  isASCIImode=true;
86  }
87  do
88  {
89  getline( infile, str );
90  if ( ! infile.good() )
91  {
92  trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
93  throw dgtalio;
94  }
95  }
96  while ( str[ 0 ] == '#' || str=="");
97  std::istringstream str_in( str );
98  unsigned int w, h;
99  str_in >> w >> h;
100 
101 
102  typename TImageContainer::Point firstPoint;
103  typename TImageContainer::Point lastPoint;
104 
105  firstPoint = TImageContainer::Point::zero;
106  lastPoint[0] = w-1;
107  lastPoint[1] = h-1;
108 
109  typename TImageContainer::Domain domain(firstPoint,lastPoint);
110  TImageContainer image(domain);
111 
112  getline( infile, str );
113  std::istringstream str2_in( str );
114  unsigned int max_value;
115  str2_in >> max_value;
116 
117  if ( ! infile.good() )
118  {
119  trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
120  throw dgtalio;
121  }
122 
123  unsigned int nb_read = 0;
124  if(!isASCIImode)
125  infile >> std::noskipws;
126  else
127  infile >> std::skipws;
128 
129  for(unsigned int y=0; y <h; y++)
130  for(unsigned int x=0; x <w; x++)
131  {
132  typename TImageContainer::Point pt;
133  if (topbotomOrder){
134  pt[0]=x; pt[1]=h-1-y;
135  }else{
136  pt[0]=x; pt[1]=y;
137  }
138 
139 
140  if(!isASCIImode)
141  {
142  unsigned char c;
143  infile >> c;
144  if ( infile.good() )
145  {
146  ++nb_read;
147  image.setValue( pt, aFunctor(c));
148  }
149  }
150  else
151  {
152  int c;
153  infile >> c;
154  if ( infile.good() )
155  {
156  ++nb_read;
157  image.setValue( pt, aFunctor(c));
158  }
159  }
160  }
161  if ( infile.fail() || infile.bad() )
162  {
163  trace.error() << "# nbread=" << nb_read << std::endl;
164  throw dgtalio;
165  }
166  infile >> std::skipws;
167  return image;
168 }
169 
170 
171 
172 template <typename TImageContainer, typename TFunctor>
173 inline
174 TImageContainer
175 DGtal::PGMReader<TImageContainer,TFunctor>::importPGM3D(const std::string & aFilename,
176  const TFunctor &aFunctor) throw(DGtal::IOException)
177 {
178  std::ifstream infile;
179  DGtal::IOException dgtalio;
180  BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 3));
181  try
182  {
183  infile.open (aFilename.c_str(), std::ifstream::in | std::ifstream::binary);
184  }
185  catch( ... )
186  {
187  trace.error() << "PGMReader : can't open " << aFilename << std::endl;
188  throw dgtalio;
189  }
190 
191  std::string str;
192  getline( infile, str );
193  if ( ! infile.good() ) {
194  trace.error() << "PGMReader : can't read " << aFilename << std::endl;
195  throw dgtalio;
196  }
197 
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;
200  throw dgtalio;
201  }
202 
203  bool isASCIImode = ( str.compare( 0, 2, "P2" ) == 0 );
204  if(!isASCIImode)
205  infile >> std::noskipws;
206  else
207  infile >> std::skipws;
208 
209  do
210  {
211  getline( infile, str );
212  if ( ! infile.good() ){
213  trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
214  throw dgtalio;
215  }
216  }
217  while ( str[ 0 ] == '#' || str=="");
218  std::istringstream str_in( str );
219  unsigned int w, h, e;
220  str_in >> w >> h >> e ;
221 
222 
223  typename TImageContainer::Point firstPoint;
224  typename TImageContainer::Point lastPoint;
225 
226  firstPoint = TImageContainer::Point::zero;
227  lastPoint[0] = w-1;
228  lastPoint[1] = h-1;
229  lastPoint[2] = e-1;
230 
231  typename TImageContainer::Domain domain(firstPoint,lastPoint);
232  TImageContainer image(domain);
233 
234  getline( infile, str );
235  std::istringstream str2_in( str );
236  int max_value;
237  str2_in >> max_value;
238 
239  if ( ! infile.good() ){
240  trace.error() << "PGMReader : Invalid format in " << aFilename << std::endl;
241  throw dgtalio;
242  }
243  unsigned int nb_read = 0;
244 
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;
250 
251  if(!isASCIImode)
252  {
253  unsigned char c;
254  infile >> c;
255  if ( infile.good() )
256  {
257  ++nb_read;
258  image.setValue( pt, aFunctor(c));
259  }
260  }
261  else
262  {
263  int c;
264  infile >> c;
265  if ( infile.good() )
266  {
267  ++nb_read;
268  image.setValue( pt, aFunctor(c));
269  }
270  }
271  }
272  }
273  }
274  if ( infile.fail() || infile.bad() )
275  {
276  trace.error() << "# nbread=" << nb_read << std::endl;
277  throw dgtalio;
278  }
279  infile >> std::skipws;
280  return image;
281 }
282 
283 
284 
285 // //
286 ///////////////////////////////////////////////////////////////////////////////
287 
288