DGtal  1.3.beta
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 
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 (std::find(ITK_IO_IMAGE_EXT.begin(), ITK_IO_IMAGE_EXT.end(), extension) != ITK_IO_IMAGE_EXT.end() )
286  {
287  return ITKReader<TContainer>::importITK( filename, aFunctor );
288  }
289 #endif
290 
291  trace.error() << "Extension " << extension<< " in 3D, not yet implemented in DGtal GenericReader." << std::endl;
292  throw dgtalio;
293  }
294  };
295 
296 
297 
302  template <typename TContainer>
303  struct GenericReader<TContainer, 3 , DGtal::uint32_t>
304  {
318  static TContainer import(const std::string &filename, unsigned int x=0,
319  unsigned int y=0, unsigned int z=0);
320 
321 
322 
335  template<typename TFunctor>
336  static TContainer importWithValueFunctor(const std::string &filename,
337  const TFunctor &aFunctor,
338  unsigned int x=0,
339  unsigned int y=0, unsigned int z=0)
340  {
342  DGtal::IOException dgtalio;
343  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
344 
345  if ( extension == "longvol" )
346  {
347  return LongvolReader<TContainer, TFunctor>::importLongvol( filename, aFunctor );
348  }
349  else if ( extension == "raw" )
350  {
351  ASSERT( x != 0 && y != 0 && z != 0 );
352  typename TContainer::Point const pt (x, y, z);
353  return RawReader< TContainer, TFunctor >::importRaw32 ( filename, pt, aFunctor );
354  }
355 
356 #ifdef WITH_ITK
357  if ( extension == "dcm" )
358  {
359  return DicomReader<TContainer, TFunctor>::importDicom(filename, aFunctor);
360  }
361  else if ( extension == "mha" || extension == "mhd" )
362  {
363  return ITKReader<TContainer>::importITK( filename, aFunctor );
364  }
365 #endif
366 
367  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
368  throw dgtalio;
369  }
370  };
371 
372 
373 
374 
379  template <typename TContainer>
380  struct GenericReader<TContainer, 3 , DGtal::uint64_t>
381  {
390  static TContainer import(const std::string &filename);
391 
392  };
393 
394 
399  template <typename TContainer, typename TValue>
400  struct GenericReader<TContainer, 2, TValue>
401  {
403 
415  static TContainer import(const std::string &filename, unsigned int x=0,
416  unsigned int y=0);
417 
418 
431  template<typename TFunctor>
432  static TContainer importWithColorFunctor(const std::string &filename,
433  const TFunctor &aFunctor,
434  unsigned int x=0,
435  unsigned int y=0)
436  {
438  DGtal::IOException dgtalio;
439  //Getting image extension
440  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
441 
442  if ( extension == "ppm" )
443  {
444  return PPMReader<TContainer, TFunctor>::importPPM(filename, aFunctor);
445  }
446  else if ( extension == "raw" )
447  {
448  ASSERT( x != 0 && y != 0 );
449  typename TContainer::Point const pt (x,y);
450  return RawReader< TContainer, TFunctor >::template importRaw<DGtal::Color>( filename, pt, aFunctor);
451  }
452  else if ( extension == "gif" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
453  {
454 #ifdef WITH_MAGICK
456  return reader.importImage( filename, aFunctor );
457 #else
458  trace.error() << "Extension " << extension<< " not yet implemented in DGtal but you can add Magick option to deal with this image type." << std::endl;
459  throw dgtalio;
460 #endif
461  }
462 
463  trace.error() << "Extension " << extension<< " in 2D, not yet implemented in DGtal GenericReader." << std::endl;
464  throw dgtalio;
465  }
466 
467 
479  template<typename TFunctor>
480  static TContainer importWithValueFunctor(const std::string &filename,
481  const TFunctor &aFunctor, unsigned int x=0,
482  unsigned int y=0)
483  {
485 
486  DGtal::IOException dgtalio;
487  //Getting image extension
488  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
489 
490  if ( extension == "raw" )
491  {
492  ASSERT( x!= 0 && y != 0);
493  typename TContainer::Point const pt (x,y);
494  return RawReader< TContainer, TFunctor >::importRaw8( filename, pt, aFunctor );
495  }
496  else if ( extension == "pgm" )
497  {
498  return PGMReader<TContainer, TFunctor>::importPGM(filename, aFunctor);
499  }
500 
501 #ifdef WITH_HDF5
502  if (extension=="h5")
503  return HDF5Reader<TContainer, TFunctor>::importHDF5(filename, "image8bit", aFunctor);
504 #endif
505 
506  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
507  throw dgtalio;
508 
509  }
510 
511  };
512 
517  template <typename TContainer>
518  struct GenericReader<TContainer, 2, DGtal::uint32_t>
519  {
521 
533  static TContainer import(const std::string &filename,unsigned int x=0,
534  unsigned int y=0 );
535 
536 
549  template<typename TFunctor>
550  static TContainer importWithColorFunctor(const std::string &filename,
551  const TFunctor &aFunctor, unsigned int x=0,
552  unsigned int y=0)
553  {
554 
556  DGtal::IOException dgtalio;
557  //Getting image extension
558  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
559 
560  if ( extension == "ppm" )
561  {
562  return PPMReader<TContainer, TFunctor>::importPPM(filename, aFunctor);
563  }
564  else if ( extension == "raw" )
565  {
566  ASSERT( x != 0 && y != 0 );
567  typename TContainer::Point const pt (x,y);
568  return RawReader< TContainer, TFunctor >::template importRaw<DGtal::Color>( filename, pt, aFunctor);
569  }
570  else if ( extension == "gif" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
571  {
572 #ifdef WITH_MAGICK
574  return reader.importImage( filename, aFunctor );
575 #else
576  trace.error() << "Extension " << extension<< " not yet implemented in DGtal but you can add Magick option to deal with this image type." << std::endl;
577  throw dgtalio;
578 #endif
579  }
580 
581  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
582  throw dgtalio;
583  }
584 
585 
596  template<typename TFunctor>
597  static TContainer importWithValueFunctor(const std::string &filename,
598  const TFunctor &aFunctor, unsigned int x=0,
599  unsigned int y=0)
600  {
602 
603  DGtal::IOException dgtalio;
604  //Getting image extension
605  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
606 
607  if ( extension == "raw" )
608  {
609  ASSERT( x != 0 && y != 0);
610  typename TContainer::Point const pt (x,y);
611  return RawReader< TContainer, TFunctor >::importRaw8 ( filename, pt, aFunctor );
612  }
613  else if ( extension == "pgm ")
614  {
615  return PGMReader<TContainer, TFunctor>::importPGM(filename, aFunctor);
616  }
617 
618 #ifdef WITH_HDF5
619  if (extension=="h5")
620  return HDF5Reader<TContainer, TFunctor>::importHDF5(filename, "image8bit", aFunctor);
621 #endif
622 
623  trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
624  throw dgtalio;
625 
626  }
627 
628  };
629 
630 
631 } // namespace DGtal
632 
633 
635 // Includes inline functions.
636 #include "DGtal/io/readers/GenericReader.ih"
637 
638 // //
640 
641 #endif // !defined GenericReader_h
642 
643 #undef GenericReader_RECURSES
644 #endif // else defined(GenericReader_RECURSES)
DGtal::HDF5Reader::importHDF5
static ImageContainer importHDF5(const std::string &aFilename, const std::string &aDataset, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
DGtal::ITK_IO_IMAGE_EXT
const auto ITK_IO_IMAGE_EXT
Definition: ITKIOTrait.h:48
DGtal::VolReader::importVol
static ImageContainer importVol(const std::string &filename, const Functor &aFunctor=Functor())
DGtal::GenericReader< TContainer, 3, DGtal::uint32_t >::importWithValueFunctor
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0, unsigned int z=0)
Definition: GenericReader.h:336
DGtal::uint32_t
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
DGtal::Trace::error
std::ostream & error()
DGtal::Color
Structure representing an RGB triple with alpha component.
Definition: Color.h:67
DGtal::GenericReader::importWithValueFunctor
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
Definition: GenericReader.h:131
DGtal::RawReader
Aim: Raw binary import of an Image.
Definition: RawReader.h:93
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::GenericReader< TContainer, 2, TValue >::importWithValueFunctor
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0)
Definition: GenericReader.h:480
DGtal::GenericReader
Aim: Provide a mechanism to load with the bestloader according to an image (2D or 3D) filename (by pa...
Definition: GenericReader.h:101
DGtal::GenericReader< TContainer, 2, DGtal::uint32_t >::importWithColorFunctor
static TContainer importWithColorFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0)
Definition: GenericReader.h:550
DGtal::GenericReader< TContainer, 3, TValue >::importWithValueFunctor
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0, unsigned int z=0)
Definition: GenericReader.h:247
DGtal::MagickReader
Aim: implements methods to read a 2D image using the ImageMagick library.
Definition: MagickReader.h:111
DGtal::PPMReader::importPPM
static ImageContainer importPPM(const std::string &aFilename, const Functor &aFunctor=functors::ColorRGBEncoder< Value >(), bool topbotomOrder=true)
DGtal::RawReader::importRaw8
static ImageContainer importRaw8(const std::string &filename, const Vector &extent, const Functor &aFunctor=Functor())
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::PGMReader::importPGM
static ImageContainer importPGM(const std::string &aFilename, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
DGtal::GenericReader::importWithColorFunctor
static TContainer importWithColorFunctor(const std::string &filename, const TFunctor &aFunctor, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
Definition: GenericReader.h:176
DGtal::DicomReader::importDicom
static ImageContainer importDicom(const std::string &aFilename, const Functor &aFunctor=Functor())
DGtal::uint64_t
boost::uint64_t uint64_t
unsigned 64-bit integer.
Definition: BasicTypes.h:65
DGtal::MagickReader::importImage
static ImageContainer importImage(const std::string &filename, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
DGtal::RawReader::importRaw32
static ImageContainer importRaw32(const std::string &filename, const Vector &extent, const Functor &aFunctor=Functor())
DGtal::GenericReader::BOOST_CONCEPT_ASSERT
BOOST_CONCEPT_ASSERT((concepts::CImage< TContainer >))
DGtal::PGMReader::importPGM3D
static ImageContainer importPGM3D(const std::string &aFilename, const Functor &aFunctor=Functor())
DGtal::LongvolReader::importLongvol
static ImageContainer importLongvol(const std::string &filename, const Functor &aFunctor=Functor())
DGtal::GenericReader< TContainer, 2, DGtal::uint32_t >::importWithValueFunctor
static TContainer importWithValueFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0)
Definition: GenericReader.h:597
DGtal::GenericReader< TContainer, 2, TValue >::importWithColorFunctor
static TContainer importWithColorFunctor(const std::string &filename, const TFunctor &aFunctor, unsigned int x=0, unsigned int y=0)
Definition: GenericReader.h:432
DGtal::IOException
Definition: Exceptions.h:52
Point
MyPointD Point
Definition: testClone2.cpp:383
DGtal::concepts::CUnaryFunctor
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:89
Value
double Value
Definition: testSimpleRandomAccessRangeFromPoint.cpp:38
DGtal::HDF5Reader::importHDF5_3D
static ImageContainer importHDF5_3D(const std::string &aFilename, const std::string &aDataset, const Functor &aFunctor=Functor())
DGtal::ITKReader::importITK
static Image importITK(const std::string &filename, const TFunctor &aFunctor=TFunctor(), bool shiftDomainUsingOrigin=true)
DGtal::concepts::CImage
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102