DGtal  1.2.0
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)
35 #define GenericReader_RECURSES
36 
37 #if !defined GenericReader_h
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 
115  static TContainer import(const std::string &filename,
116  std::vector<unsigned int> dimSpace= std::vector<unsigned int > () );
117 
130  template <typename TFunctor>
131  static TContainer importWithValueFunctor(
132  const std::string & filename, const TFunctor & aFunctor,
133  std::vector<unsigned int> dimSpace =
134  std::vector<unsigned int>() )
135  {
138  DGtal::IOException dgtalio;
139  const std::string extension =
140  filename.substr( filename.find_last_of( "." ) + 1 );
141 
142  if ( extension == "raw" )
143  {
144  for ( unsigned int i = 0; i < dimSpace.size(); i++ )
145  ASSERT( dimSpace[ i ] != 0 );
146  typename TContainer::Point const pt;
147  for ( unsigned int i = 0; i < dimSpace.size(); i++ )
148  pt[ i ] = dimSpace[ i ];
149 
150  return RawReader<TContainer, TFunctor>::template importRaw<TValue>(
151  filename, pt, aFunctor );
152  }
153 
154  trace.error() << "Extension " << extension
155  << " not yet implemented in DGtal GenericReader."
156  << std::endl;
157  throw dgtalio;
158  }
159 
175  template <typename TFunctor>
176  static TContainer importWithColorFunctor(
177  const std::string & filename, const TFunctor & aFunctor,
178  std::vector<unsigned int> dimSpace =
179  std::vector<unsigned int>() )
180 
181  {
184  typename TContainer::Value>));
185 
186  DGtal::IOException dgtalio;
187  const std::string extension =
188  filename.substr( filename.find_last_of( "." ) + 1 );
189 
190  if ( extension == "raw" )
191  {
192  for ( unsigned int i = 0; i < dimSpace.size(); i++ )
193  ASSERT( dimSpace[ i ] != 0 );
194  typename TContainer::Point const pt;
195  for ( unsigned int i = 0; i < dimSpace.size(); i++ )
196  pt[ i ] = dimSpace[ i ];
198  DGtal::Color>( filename, pt, aFunctor );
199  }
200 
201  trace.error() << "Extension " << extension
202  << " not yet implemented in DGtal GenericReader."
203  << std::endl;
204  throw dgtalio;
205  }
206  };
207 
208 
213  template <typename TContainer, typename TValue>
214  struct GenericReader<TContainer, 3, TValue>
215  {
229  static TContainer import(const std::string &filename, unsigned int x=0,
230  unsigned int y=0, unsigned int z=0);
231 
232 
233 
246  template<typename TFunctor>
247  static TContainer importWithValueFunctor(const std::string &filename,
248  const TFunctor &aFunctor,
249  unsigned int x=0,
250  unsigned int y=0, unsigned int z=0)
251  {
253  DGtal::IOException dgtalio;
254  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
255 
256  if ( extension == "vol" )
257  {
258  return VolReader<TContainer, TFunctor>::importVol( filename, aFunctor );
259  }
260  else if ( extension == "longvol" )
261  {
262  return LongvolReader<TContainer, TFunctor>::importLongvol( filename, aFunctor );
263  }
264  else if ( extension == "pgm3d" || extension == "pgm3D" || extension == "p3d" || extension == "pgm" )
265  {
266  return PGMReader<TContainer, TFunctor>::importPGM3D(filename, aFunctor);
267  }
268  else if ( extension == "raw" )
269  {
270  ASSERT( x != 0 && y != 0 && z != 0 );
271  typename TContainer::Point const pt (x, y, z);
272  return RawReader< TContainer, TFunctor >::template importRaw<TValue>( filename, pt, aFunctor );
273  }
274 
275 #ifdef WITH_HDF5
276  if ( extension == "h5" )
277  return HDF5Reader<TContainer, TFunctor>::importHDF5_3D(filename, "UInt8Array3D", aFunctor);
278 #endif
279 
280 #ifdef WITH_ITK
281  if ( extension == "dcm" )
282  {
283  return DicomReader<TContainer, TFunctor>::importDicom(filename, aFunctor);
284  }
285  else if ( extension == "nii" || extension == "mha" ||
286  extension == "mhd" || extension == "tiff" ||
287  extension == "tif" )
288  {
289  return ITKReader<TContainer>::importITK( filename, aFunctor );
290  }
291 #endif
292 
293  trace.error() << "Extension " << extension<< " in 3D, not yet implemented in DGtal GenericReader." << std::endl;
294  throw dgtalio;
295  }
296  };
297 
298 
299 
304  template <typename TContainer>
305  struct GenericReader<TContainer, 3 , DGtal::uint32_t>
306  {
320  static TContainer import(const std::string &filename, unsigned int x=0,
321  unsigned int y=0, unsigned int z=0);
322 
323 
324 
337  template<typename TFunctor>
338  static TContainer importWithValueFunctor(const std::string &filename,
339  const TFunctor &aFunctor,
340  unsigned int x=0,
341  unsigned int y=0, unsigned int z=0)
342  {
344  DGtal::IOException dgtalio;
345  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
346 
347  if ( extension == "longvol" )
348  {
349  return LongvolReader<TContainer, TFunctor>::importLongvol( filename, aFunctor );
350  }
351  else if ( extension == "raw" )
352  {
353  ASSERT( x != 0 && y != 0 && z != 0 );
354  typename TContainer::Point const pt (x, y, z);
355  return RawReader< TContainer, TFunctor >::importRaw32 ( filename, pt, aFunctor );
356  }
357 
358 #ifdef WITH_ITK
359  if ( extension == "dcm" )
360  {
361  return DicomReader<TContainer, TFunctor>::importDicom(filename, aFunctor);
362  }
363  else if ( extension == "mha" || extension == "mhd" )
364  {
365  return ITKReader<TContainer>::importITK( filename, aFunctor );
366  }
367 #endif
368 
369  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
370  throw dgtalio;
371  }
372  };
373 
374 
375 
376 
381  template <typename TContainer>
382  struct GenericReader<TContainer, 3 , DGtal::uint64_t>
383  {
392  static TContainer import(const std::string &filename);
393 
394  };
395 
396 
401  template <typename TContainer, typename TValue>
402  struct GenericReader<TContainer, 2, TValue>
403  {
405 
417  static TContainer import(const std::string &filename, unsigned int x=0,
418  unsigned int y=0);
419 
420 
433  template<typename TFunctor>
434  static TContainer importWithColorFunctor(const std::string &filename,
435  const TFunctor &aFunctor,
436  unsigned int x=0,
437  unsigned int y=0)
438  {
440  DGtal::IOException dgtalio;
441  //Getting image extension
442  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
443 
444  if ( extension == "ppm" )
445  {
446  return PPMReader<TContainer, TFunctor>::importPPM(filename, aFunctor);
447  }
448  else if ( extension == "raw" )
449  {
450  ASSERT( x != 0 && y != 0 );
451  typename TContainer::Point const pt (x,y);
452  return RawReader< TContainer, TFunctor >::template importRaw<DGtal::Color>( filename, pt, aFunctor);
453  }
454  else if ( extension == "gif" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
455  {
456 #ifdef WITH_MAGICK
458  return reader.importImage( filename, aFunctor );
459 #else
460  trace.error() << "Extension " << extension<< " not yet implemented in DGtal but you can add Magick option to deal with this image type." << std::endl;
461  throw dgtalio;
462 #endif
463  }
464 
465  trace.error() << "Extension " << extension<< " in 2D, not yet implemented in DGtal GenericReader." << std::endl;
466  throw dgtalio;
467  }
468 
469 
481  template<typename TFunctor>
482  static TContainer importWithValueFunctor(const std::string &filename,
483  const TFunctor &aFunctor, unsigned int x=0,
484  unsigned int y=0)
485  {
487 
488  DGtal::IOException dgtalio;
489  //Getting image extension
490  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
491 
492  if ( extension == "raw" )
493  {
494  ASSERT( x!= 0 && y != 0);
495  typename TContainer::Point const pt (x,y);
496  return RawReader< TContainer, TFunctor >::importRaw8( filename, pt, aFunctor );
497  }
498  else if ( extension == "pgm" )
499  {
500  return PGMReader<TContainer, TFunctor>::importPGM(filename, aFunctor);
501  }
502 
503 #ifdef WITH_HDF5
504  if (extension=="h5")
505  return HDF5Reader<TContainer, TFunctor>::importHDF5(filename, "image8bit", aFunctor);
506 #endif
507 
508  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
509  throw dgtalio;
510 
511  }
512 
513  };
514 
519  template <typename TContainer>
520  struct GenericReader<TContainer, 2, DGtal::uint32_t>
521  {
523 
535  static TContainer import(const std::string &filename,unsigned int x=0,
536  unsigned int y=0 );
537 
538 
551  template<typename TFunctor>
552  static TContainer importWithColorFunctor(const std::string &filename,
553  const TFunctor &aFunctor, unsigned int x=0,
554  unsigned int y=0)
555  {
556 
558  DGtal::IOException dgtalio;
559  //Getting image extension
560  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
561 
562  if ( extension == "ppm" )
563  {
564  return PPMReader<TContainer, TFunctor>::importPPM(filename, aFunctor);
565  }
566  else if ( extension == "raw" )
567  {
568  ASSERT( x != 0 && y != 0 );
569  typename TContainer::Point const pt (x,y);
570  return RawReader< TContainer, TFunctor >::template importRaw<DGtal::Color>( filename, pt, aFunctor);
571  }
572  else if ( extension == "gif" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
573  {
574 #ifdef WITH_MAGICK
576  return reader.importImage( filename, aFunctor );
577 #else
578  trace.error() << "Extension " << extension<< " not yet implemented in DGtal but you can add Magick option to deal with this image type." << std::endl;
579  throw dgtalio;
580 #endif
581  }
582 
583  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
584  throw dgtalio;
585  }
586 
587 
598  template<typename TFunctor>
599  static TContainer importWithValueFunctor(const std::string &filename,
600  const TFunctor &aFunctor, unsigned int x=0,
601  unsigned int y=0)
602  {
604 
605  DGtal::IOException dgtalio;
606  //Getting image extension
607  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
608 
609  if ( extension == "raw" )
610  {
611  ASSERT( x != 0 && y != 0);
612  typename TContainer::Point const pt (x,y);
613  return RawReader< TContainer, TFunctor >::importRaw8 ( filename, pt, aFunctor );
614  }
615  else if ( extension == "pgm ")
616  {
617  return PGMReader<TContainer, TFunctor>::importPGM(filename, aFunctor);
618  }
619 
620 #ifdef WITH_HDF5
621  if (extension=="h5")
622  return HDF5Reader<TContainer, TFunctor>::importHDF5(filename, "image8bit", aFunctor);
623 #endif
624 
625  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
626  throw dgtalio;
627 
628  }
629 
630  };
631 
632 
633 } // namespace DGtal
634 
635 
637 // Includes inline functions.
638 #include "DGtal/io/readers/GenericReader.ih"
639 
640 // //
642 
643 #endif // !defined GenericReader_h
644 
645 #undef GenericReader_RECURSES
646 #endif // else defined(GenericReader_RECURSES)
Structure representing an RGB triple with alpha component.
Definition: Color.h:67
std::ostream & error()
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
Trace trace
Definition: Common.h:154
boost::uint64_t uint64_t
unsigned 64-bit integer.
Definition: BasicTypes.h:65
static ImageContainer importDicom(const std::string &aFilename, const Functor &aFunctor=Functor())
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0)
BOOST_CONCEPT_ASSERT((concepts::CImage< TContainer >))
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)
BOOST_CONCEPT_ASSERT((concepts::CImage< TContainer >))
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)
BOOST_CONCEPT_ASSERT((concepts::CImage< TContainer >))
BOOST_CONCEPT_ASSERT((concepts::CImage< TContainer >))
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0, unsigned int z=0)
BOOST_CONCEPT_ASSERT((concepts::CImage< TContainer >))
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, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
BOOST_CONCEPT_ASSERT((concepts::CImage< TContainer >))
static ImageContainer importHDF5_3D(const std::string &aFilename, const std::string &aDataset, const Functor &aFunctor=Functor())
static ImageContainer importHDF5(const std::string &aFilename, const std::string &aDataset, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
static Image importITK(const std::string &filename, const TFunctor &aFunctor=TFunctor(), bool shiftDomainUsingOrigin=true)
static ImageContainer importLongvol(const std::string &filename, const Functor &aFunctor=Functor())
Aim: implements methods to read a 2D image using the ImageMagick library.
Definition: MagickReader.h:112
static ImageContainer importImage(const std::string &filename, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
static ImageContainer importPGM3D(const std::string &aFilename, const Functor &aFunctor=Functor())
static ImageContainer importPGM(const std::string &aFilename, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
static ImageContainer importPPM(const std::string &aFilename, const Functor &aFunctor=functors::ColorRGBEncoder< Value >(), bool topbotomOrder=true)
Aim: Raw binary import of an Image.
Definition: RawReader.h:94
static ImageContainer importRaw8(const std::string &filename, const Vector &extent, const Functor &aFunctor=Functor())
static ImageContainer importRaw32(const std::string &filename, const Vector &extent, const Functor &aFunctor=Functor())
static ImageContainer importVol(const std::string &filename, const Functor &aFunctor=Functor())
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:103
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:90
MyPointD Point
Definition: testClone2.cpp:383