DGtal  0.9.3beta
GenericReader.h
1 
17 #pragma once
18 
31 #if defined(GenericReader_RECURSES)
32 #error Recursive header files inclusion detected in GenericReader.h
33 #else // defined(GenericReader_RECURSES)
34 
35 #define GenericReader_RECURSES
36 
37 #if !defined GenericReader_h
38 
39 #define GenericReader_h
40 
42 // Inclusions
43 #include <iostream>
44 #include <vector>
45 #include "DGtal/images/CImage.h"
46 #include "DGtal/base/Common.h"
47 #include "DGtal/io/readers/VolReader.h"
48 #include "DGtal/io/readers/LongvolReader.h"
49 #include "DGtal/io/readers/PPMReader.h"
50 #include "DGtal/io/readers/PGMReader.h"
51 #include "DGtal/io/readers/RawReader.h"
52 #ifdef WITH_HDF5
53 #include "DGtal/io/readers/HDF5Reader.h"
54 #endif
55 #ifdef WITH_MAGICK
56 #include "DGtal/io/readers/MagickReader.h"
57 #endif
58 #ifdef WITH_ITK
59 #include "DGtal/io/readers/DicomReader.h"
60 #include "DGtal/io/readers/ITKReader.h"
61 #endif
62 #include "DGtal/io/colormaps/BasicColorToScalarFunctors.h"
63 
65 
66 namespace DGtal
67 {
68 
70  // template class GenericReader
100  template <typename TContainer, int Tdim=TContainer::Point::dimension, typename TValue = typename TContainer::Value>
102  {
104 
113  static TContainer import(const std::string &filename,
114  std::vector<unsigned int> dimSpace= std::vector<unsigned int > () ) throw(DGtal::IOException);
115  };
116 
117 
122  template <typename TContainer, typename TValue>
123  struct GenericReader<TContainer, 3, TValue>
124  {
138  static TContainer import(const std::string &filename, unsigned int x=0,
139  unsigned int y=0, unsigned int z=0) throw(DGtal::IOException);
140 
141 
142 
155  template<typename TFunctor>
156  static TContainer importWithValueFunctor(const std::string &filename,
157  const TFunctor &aFunctor,
158  unsigned int x=0,
159  unsigned int y=0, unsigned int z=0) throw( DGtal::IOException )
160  {
162  DGtal::IOException dgtalio;
163  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
164 
165  if ( extension == "vol" )
166  {
167  return VolReader<TContainer, TFunctor>::importVol( filename, aFunctor );
168  }
169  else if ( extension == "longvol" )
170  {
171  return LongvolReader<TContainer, TFunctor>::importLongvol( filename, aFunctor );
172  }
173  else if ( extension == "pgm3d" || extension == "pgm3D" || extension == "p3d" || extension == "pgm" )
174  {
175  return PGMReader<TContainer, TFunctor>::importPGM3D(filename, aFunctor);
176  }
177  else if ( extension == "raw" )
178  {
179  ASSERT( x != 0 && y != 0 && z != 0 );
180  typename TContainer::Point const pt (x, y, z);
181  return RawReader< TContainer, TFunctor >::template importRaw<TValue>( filename, pt, aFunctor );
182  }
183 
184 #ifdef WITH_HDF5
185  if ( extension == "h5" )
186  return HDF5Reader<TContainer, TFunctor>::importHDF5_3D(filename, "UInt8Array3D", aFunctor);
187 #endif
188 
189 #ifdef WITH_ITK
190  if ( extension == "dcm" )
191  {
192  return DicomReader<TContainer, TFunctor>::importDicom(filename, aFunctor);
193  }
194  else if ( extension == "mha" || extension == "mhd" )
195  {
196  return ITKReader<TContainer, TFunctor>::importITK(filename, aFunctor);
197  }
198 #endif
199 
200  trace.error() << "Extension " << extension<< " in 3D, not yet implemented in DGtal GenericReader." << std::endl;
201  throw dgtalio;
202  }
203  };
204 
205 
206 
211  template <typename TContainer>
212  struct GenericReader<TContainer, 3 , DGtal::uint32_t>
213  {
227  static TContainer import(const std::string &filename, unsigned int x=0,
228  unsigned int y=0, unsigned int z=0) throw(DGtal::IOException);
229 
230 
231 
244  template<typename TFunctor>
245  static TContainer importWithValueFunctor(const std::string &filename,
246  const TFunctor &aFunctor,
247  unsigned int x=0,
248  unsigned int y=0, unsigned int z=0) throw( DGtal::IOException )
249  {
251  DGtal::IOException dgtalio;
252  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
253 
254  if ( extension == "longvol" )
255  {
256  return LongvolReader<TContainer, TFunctor>::importLongvol( filename, aFunctor );
257  }
258  else if ( extension == "raw" )
259  {
260  ASSERT( x != 0 && y != 0 && z != 0 );
261  typename TContainer::Point const pt (x, y, z);
262  return RawReader< TContainer, TFunctor >::importRaw32 ( filename, pt, aFunctor );
263  }
264 
265 #ifdef WITH_ITK
266  if ( extension == "dcm" )
267  {
268  return DicomReader<TContainer, TFunctor>::importDicom(filename, aFunctor);
269  }
270  else if ( extension == "mha" || extension == "mhd" )
271  {
272  return ITKReader<TContainer, TFunctor>::importITK(filename, aFunctor);
273  }
274 #endif
275 
276  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
277  throw dgtalio;
278  }
279  };
280 
281 
282 
283 
288  template <typename TContainer>
289  struct GenericReader<TContainer, 3 , DGtal::uint64_t>
290  {
299  static TContainer import(const std::string &filename) throw(DGtal::IOException);
300 
301  };
302 
303 
308  template <typename TContainer, typename TValue>
309  struct GenericReader<TContainer, 2, TValue>
310  {
312 
324  static TContainer import(const std::string &filename, unsigned int x=0,
325  unsigned int y=0) throw(DGtal::IOException);
326 
327 
340  template<typename TFunctor>
341  static TContainer importWithColorFunctor(const std::string &filename,
342  const TFunctor &aFunctor,
343  unsigned int x=0,
344  unsigned int y=0) throw( DGtal::IOException )
345  {
347  DGtal::IOException dgtalio;
348  //Getting image extension
349  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
350 
351  if ( extension == "ppm" )
352  {
353  return PPMReader<TContainer, TFunctor>::importPPM(filename, aFunctor);
354  }
355  else if ( extension == "raw" )
356  {
357  ASSERT( x != 0 && y != 0 );
358  typename TContainer::Point const pt (x,y);
359  return RawReader< TContainer, TFunctor >::template importRaw<DGtal::Color>( filename, pt, aFunctor);
360  }
361  else if ( extension == "gif" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
362  {
363 #ifdef WITH_MAGICK
365  return reader.importImage( filename, aFunctor );
366 #else
367  trace.error() << "Extension " << extension<< " not yet implemented in DGtal but you can add Magick option to deal with this image type." << std::endl;
368  throw dgtalio;
369 #endif
370  }
371 
372  trace.error() << "Extension " << extension<< " in 2D, not yet implemented in DGtal GenericReader." << std::endl;
373  throw dgtalio;
374  }
375 
376 
388  template<typename TFunctor>
389  static TContainer importWithValueFunctor(const std::string &filename,
390  const TFunctor &aFunctor, unsigned int x=0,
391  unsigned int y=0) throw( DGtal::IOException )
392  {
394 
395  DGtal::IOException dgtalio;
396  //Getting image extension
397  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
398 
399  if ( extension == "raw" )
400  {
401  ASSERT( x!= 0 && y != 0);
402  typename TContainer::Point const pt (x,y);
403  return RawReader< TContainer, TFunctor >::importRaw8( filename, pt, aFunctor );
404  }
405  else if ( extension == "pgm" )
406  {
407  return PGMReader<TContainer, TFunctor>::importPGM(filename, aFunctor);
408  }
409 
410 #ifdef WITH_HDF5
411  if (extension=="h5")
412  return HDF5Reader<TContainer, TFunctor>::importHDF5(filename, "image8bit", aFunctor);
413 #endif
414 
415  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
416  throw dgtalio;
417 
418  }
419 
420  };
421 
426  template <typename TContainer>
427  struct GenericReader<TContainer, 2, DGtal::uint32_t>
428  {
430 
442  static TContainer import(const std::string &filename,unsigned int x=0,
443  unsigned int y=0 ) throw(DGtal::IOException);
444 
445 
458  template<typename TFunctor>
459  static TContainer importWithColorFunctor(const std::string &filename,
460  const TFunctor &aFunctor, unsigned int x=0,
461  unsigned int y=0) throw(DGtal::IOException)
462  {
463 
465  DGtal::IOException dgtalio;
466  //Getting image extension
467  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
468 
469  if ( extension == "ppm" )
470  {
471  return PPMReader<TContainer, TFunctor>::importPPM(filename, aFunctor);
472  }
473  else if ( extension == "raw" )
474  {
475  ASSERT( x != 0 && y != 0 );
476  typename TContainer::Point const pt (x,y);
477  return RawReader< TContainer, TFunctor >::template importRaw<DGtal::Color>( filename, pt, aFunctor);
478  }
479  else if ( extension == "gif" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
480  {
481 #ifdef WITH_MAGICK
483  return reader.importImage( filename, aFunctor );
484 #else
485  trace.error() << "Extension " << extension<< " not yet implemented in DGtal but you can add Magick option to deal with this image type." << std::endl;
486  throw dgtalio;
487 #endif
488  }
489 
490  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
491  throw dgtalio;
492  }
493 
494 
505  template<typename TFunctor>
506  static TContainer importWithValueFunctor(const std::string &filename,
507  const TFunctor &aFunctor, unsigned int x=0,
508  unsigned int y=0) throw(DGtal::IOException)
509  {
511 
512  DGtal::IOException dgtalio;
513  //Getting image extension
514  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
515 
516  if ( extension == "raw" )
517  {
518  ASSERT( x != 0 && y != 0);
519  typename TContainer::Point const pt (x,y);
520  return RawReader< TContainer, TFunctor >::importRaw8 ( filename, pt, aFunctor );
521  }
522  else if ( extension == "pgm ")
523  {
524  return PGMReader<TContainer, TFunctor>::importPGM(filename, aFunctor);
525  }
526 
527 #ifdef WITH_HDF5
528  if (extension=="h5")
529  return HDF5Reader<TContainer, TFunctor>::importHDF5(filename, "image8bit", aFunctor);
530 #endif
531 
532  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
533  throw dgtalio;
534 
535  }
536 
537  };
538 
539 
540 } // namespace DGtal
541 
542 
544 // Includes inline functions.
545 #include "DGtal/io/readers/GenericReader.ih"
546 
547 // //
549 
550 #endif // !defined GenericReader_h
551 
552 #undef GenericReader_RECURSES
553 #endif // else defined(GenericReader_RECURSES)
BOOST_CONCEPT_ASSERT((concepts::CImage< TContainer >))
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
Trace trace
Definition: Common.h:137
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0, unsigned int z=0)
static ImageContainer importDicom(const std::string &aFilename, const Functor &aFunctor=Functor())
static ImageContainer importRaw8(const std::string &filename, const Vector &extent, const Functor &aFunctor=Functor())
static TContainer importWithColorFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0)
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:89
static ImageContainer importPGM(const std::string &aFilename, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
static ImageContainer importVol(const std::string &filename, const Functor &aFunctor=Functor())
static ImageContainer importHDF5(const std::string &aFilename, const std::string &aDataset, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
Aim: Provide a mechanism to load with the bestloader according to an image (2D or 3D) filename (by pa...
static TContainer importWithColorFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0)
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0, unsigned int z=0)
Aim: implements methods to read a 2D image using the ImageMagick library.
Definition: MagickReader.h:65
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0)
static ImageContainer importImage(const std::string &filename, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
DGtal is the top-level namespace which contains all DGtal functions and types.
static ImageContainer importLongvol(const std::string &filename, const Functor &aFunctor=Functor())
Aim: Raw binary import of an Image.
Definition: RawReader.h:93
boost::uint64_t uint64_t
unsigned 64-bit integer.
Definition: BasicTypes.h:65
static ImageContainer importRaw32(const std::string &filename, const Vector &extent, const Functor &aFunctor=Functor())
static Image importITK(const std::string &filename, const Functor &aFunctor=Functor())
static ImageContainer importHDF5_3D(const std::string &aFilename, const std::string &aDataset, const Functor &aFunctor=Functor())
static ImageContainer importPGM3D(const std::string &aFilename, const Functor &aFunctor=Functor())
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102
std::ostream & error()
static ImageContainer importPPM(const std::string &aFilename, const Functor &aFunctor=functors::ColorRGBEncoder< Value >(), bool topbotomOrder=true)
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0)