DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PNMReader.ih
1 
29 
30 // IMPLEMENTATION of inline methods.
32 
34 #include <cstdlib>
35 #include <iostream>
36 #include <fstream>
37 #include <sstream>
39 
40 
41 
43 // Implementation of inline methods //
44 
45 
47 // Implementation of inline functions and external operators //
48 
49 template <typename TImageContainer>
50 inline
51 TImageContainer
52 DGtal::PNMReader<TImageContainer>::importPGM(const std::string & aFilename, bool topbotomOrder ) throw(DGtal::IOException)
53 {
54  ifstream infile;
55  DGtal::IOException dgtalio;
56  BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 2));
57  try
58  {
59  infile.open (aFilename.c_str(), ifstream::in);
60  }
61  catch( ... )
62  {
63  trace.error() << "PNMReader : can't open " << aFilename << endl;
64  throw dgtalio;
65  }
66  bool isASCIImode = false;
67  string str;
68  getline( infile, str );
69  if ( ! infile.good() )
70  {
71  trace.error() << "PNMReader : can't read " << aFilename << endl;
72  throw dgtalio;
73  }
74  if ( str != "P5" && str != "P2")
75  {
76  trace.error() << "PNMReader : No P5 or P2 format in " << aFilename << endl;
77  throw dgtalio;
78  }
79  if(str== "P2")
80  {
81  isASCIImode=true;
82  }
83  do
84  {
85  getline( infile, str );
86  if ( ! infile.good() )
87  {
88  trace.error() << "PNMReader : Invalid format in " << aFilename << endl;
89  throw dgtalio;
90  }
91  }
92  while ( str[ 0 ] == '#' || str=="");
93  istringstream str_in( str );
94  unsigned int w, h;
95  str_in >> w >> h;
96 
97 
98  typename TImageContainer::Point firstPoint;
99  typename TImageContainer::Point lastPoint;
100 
101  firstPoint = TImageContainer::Point::zero;
102  lastPoint[0] = w-1;
103  lastPoint[1] = h-1;
104 
105  typename TImageContainer::Domain domain(firstPoint,lastPoint);
106  TImageContainer image(domain);
107 
108  getline( infile, str );
109  istringstream str2_in( str );
110  unsigned int max_value;
111  str2_in >> max_value;
112 
113  if ( ! infile.good() )
114  {
115  trace.error() << "PNMReader : Invalid format in " << aFilename << endl;
116  throw dgtalio;
117  }
118 
119  unsigned int nb_read = 0;
120  if(!isASCIImode)
121  infile >> noskipws;
122  else
123  infile >> skipws;
124 
125  for(unsigned int y=0; y <h; y++)
126  for(unsigned int x=0; x <w; x++)
127  {
128  typename TImageContainer::Point pt;
129  if (topbotomOrder){
130  pt[0]=x; pt[1]=h-1-y;
131  }else{
132  pt[0]=x; pt[1]=y;
133  }
134 
135 
136  if(!isASCIImode)
137  {
138  unsigned char c;
139  infile >> c;
140  if ( infile.good() )
141  {
142  ++nb_read;
143  image.setValue( pt, c);
144  }
145  }
146  else
147  {
148  int c;
149  infile >> c;
150  if ( infile.good() )
151  {
152  ++nb_read;
153  image.setValue( pt, c);
154  }
155  }
156  }
157  if ( infile.fail() || infile.bad() )
158  {
159  trace.error() << "# nbread=" << nb_read << endl;
160  throw dgtalio;
161  }
162  infile >> skipws;
163  return image;
164 }
165 
166 
167 
168 template <typename TImageContainer>
169 inline
170 TImageContainer
172 {
173  ifstream infile;
174  DGtal::IOException dgtalio;
175  BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 3));
176  try
177  {
178  infile.open (aFilename.c_str(), ifstream::in);
179  }
180  catch( ... )
181  {
182  trace.error() << "PNMReader : can't open " << aFilename << endl;
183  throw dgtalio;
184  }
185 
186  string str;
187  getline( infile, str );
188  if ( ! infile.good() ) {
189  trace.error() << "PNMReader : can't read " << aFilename << endl;
190  throw dgtalio;
191  }
192  if ( str != "P3d" && str != "P3D"){
193  trace.error() << "PNMReader : No P3d format in " << aFilename << endl;
194  throw dgtalio;
195  }
196 
197  do
198  {
199  getline( infile, str );
200  if ( ! infile.good() ){
201  trace.error() << "PNMReader : Invalid format in " << aFilename << endl;
202  throw dgtalio;
203  }
204  }
205  while ( str[ 0 ] == '#' || str=="");
206  istringstream str_in( str );
207  unsigned int w, h, e;
208  str_in >> w >> h >> e ;
209 
210 
211  typename TImageContainer::Point firstPoint;
212  typename TImageContainer::Point lastPoint;
213 
214  firstPoint = TImageContainer::Point::zero;
215  lastPoint[0] = w-1;
216  lastPoint[1] = h-1;
217  lastPoint[2] = e-1;
218 
219  typename TImageContainer::Domain domain(firstPoint,lastPoint);
220  TImageContainer image(domain);
221 
222  getline( infile, str );
223  istringstream str2_in( str );
224  int max_value;
225  str2_in >> max_value;
226 
227  if ( ! infile.good() ){
228  trace.error() << "PNMReader : Invalid format in " << aFilename << endl;
229  throw dgtalio;
230  }
231  unsigned int nb_read = 0;
232 
233  infile >> noskipws;
234  for(unsigned int z=0; z <e; z++){
235  for(unsigned int y=0; y <h; y++){
236  for(unsigned int x=0; x <w; x++){
237  typename TImageContainer::Point pt;
238  pt[0]=x; pt[1]=y; pt[2]=z;
239  char c;
240  infile >> c;
241  if ( infile.good() ){
242  ++nb_read;
243  image.setValue( pt, c);
244  }
245  }
246  }
247  }
248  if ( infile.fail() || infile.bad() )
249  {
250  trace.error() << "# nbread=" << nb_read << endl;
251  throw dgtalio;
252  }
253  infile >> skipws;
254  return image;
255 }
256 
257 
258 
259 // //
261 
262