DGtal 1.4.0
Loading...
Searching...
No Matches
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#include "DGtal/io/readers/STBReader.h"
53#ifdef WITH_HDF5
54#include "DGtal/io/readers/HDF5Reader.h"
55#endif
56#ifdef WITH_ITK
57#include "DGtal/io/readers/DicomReader.h"
58#include "DGtal/io/readers/ITKReader.h"
59#endif
60#include "DGtal/io/colormaps/BasicColorToScalarFunctors.h"
61
63
64namespace DGtal
65{
66
68 // template class GenericReader
98 template <typename TContainer, int Tdim=TContainer::Point::dimension, typename TValue = typename TContainer::Value>
100 {
102
113 static TContainer import(const std::string &filename,
114 std::vector<unsigned int> dimSpace= std::vector<unsigned int > () );
115
128 template <typename TFunctor>
129 static TContainer importWithValueFunctor(
130 const std::string & filename, const TFunctor & aFunctor,
131 std::vector<unsigned int> dimSpace =
132 std::vector<unsigned int>() )
133 {
136 DGtal::IOException dgtalio;
137 const std::string extension =
138 filename.substr( filename.find_last_of( "." ) + 1 );
139
140 if ( extension == "raw" )
141 {
142 for ( unsigned int i = 0; i < dimSpace.size(); i++ )
143 ASSERT( dimSpace[ i ] != 0 );
144 typename TContainer::Point const pt;
145 for ( unsigned int i = 0; i < dimSpace.size(); i++ )
146 pt[ i ] = dimSpace[ i ];
147
148 return RawReader<TContainer, TFunctor>::template importRaw<TValue>(
149 filename, pt, aFunctor );
150 }
151
152 trace.error() << "Extension " << extension
153 << " not yet implemented in DGtal GenericReader."
154 << std::endl;
155 throw dgtalio;
156 }
157
173 template <typename TFunctor>
174 static TContainer importWithColorFunctor(
175 const std::string & filename, const TFunctor & aFunctor,
176 std::vector<unsigned int> dimSpace =
177 std::vector<unsigned int>() )
178
179 {
182 typename TContainer::Value>));
183
184 DGtal::IOException dgtalio;
185 const std::string extension =
186 filename.substr( filename.find_last_of( "." ) + 1 );
187
188 if ( extension == "raw" )
189 {
190 for ( unsigned int i = 0; i < dimSpace.size(); i++ )
191 ASSERT( dimSpace[ i ] != 0 );
192 typename TContainer::Point const pt;
193 for ( unsigned int i = 0; i < dimSpace.size(); i++ )
194 pt[ i ] = dimSpace[ i ];
196 DGtal::Color>( filename, pt, aFunctor );
197 }
198
199 trace.error() << "Extension " << extension
200 << " not yet implemented in DGtal GenericReader."
201 << std::endl;
202 throw dgtalio;
203 }
204 };
205
206
211 template <typename TContainer, typename TValue>
212 struct GenericReader<TContainer, 3, TValue>
213 {
227 static TContainer import(const std::string &filename, unsigned int x=0,
228 unsigned int y=0, unsigned int z=0);
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)
249 {
251 DGtal::IOException dgtalio;
252 const std::string extension = filename.substr(filename.find_last_of(".") + 1);
253
254 if ( extension == "vol" )
255 {
256 return VolReader<TContainer, TFunctor>::importVol( filename, aFunctor );
257 }
258 else if ( extension == "longvol" )
259 {
260 return LongvolReader<TContainer, TFunctor>::importLongvol( filename, aFunctor );
261 }
262 else if ( extension == "pgm3d" || extension == "pgm3D" || extension == "p3d" || extension == "pgm" )
263 {
264 return PGMReader<TContainer, TFunctor>::importPGM3D(filename, aFunctor);
265 }
266 else if ( extension == "raw" )
267 {
268 ASSERT( x != 0 && y != 0 && z != 0 );
269 typename TContainer::Point const pt (x, y, z);
270 return RawReader< TContainer, TFunctor >::template importRaw<TValue>( filename, pt, aFunctor );
271 }
272
273#ifdef WITH_HDF5
274 if ( extension == "h5" )
275 return HDF5Reader<TContainer, TFunctor>::importHDF5_3D(filename, "UInt8Array3D", aFunctor);
276#endif
277
278#ifdef WITH_ITK
279 if ( extension == "dcm" )
280 {
281 return DicomReader<TContainer, TFunctor>::importDicom(filename, aFunctor);
282 }
283 else if (std::find(ITK_IO_IMAGE_EXT.begin(), ITK_IO_IMAGE_EXT.end(), extension) != ITK_IO_IMAGE_EXT.end() )
284 {
285 return ITKReader<TContainer>::importITK( filename, aFunctor );
286 }
287#endif
288
289 trace.error() << "Extension " << extension<< " in 3D, not yet implemented in DGtal GenericReader." << std::endl;
290 throw dgtalio;
291 }
292 };
293
294
295
300 template <typename TContainer>
301 struct GenericReader<TContainer, 3 , DGtal::uint32_t>
302 {
316 static TContainer import(const std::string &filename, unsigned int x=0,
317 unsigned int y=0, unsigned int z=0);
318
319
320
333 template<typename TFunctor>
334 static TContainer importWithValueFunctor(const std::string &filename,
335 const TFunctor &aFunctor,
336 unsigned int x=0,
337 unsigned int y=0, unsigned int z=0)
338 {
340 DGtal::IOException dgtalio;
341 const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
342
343 if ( extension == "longvol" )
344 {
345 return LongvolReader<TContainer, TFunctor>::importLongvol( filename, aFunctor );
346 }
347 else if ( extension == "raw" )
348 {
349 ASSERT( x != 0 && y != 0 && z != 0 );
350 typename TContainer::Point const pt (x, y, z);
351 return RawReader< TContainer, TFunctor >::importRaw32 ( filename, pt, aFunctor );
352 }
353
354#ifdef WITH_ITK
355 if ( extension == "dcm" )
356 {
357 return DicomReader<TContainer, TFunctor>::importDicom(filename, aFunctor);
358 }
359 else if ( extension == "mha" || extension == "mhd" )
360 {
361 return ITKReader<TContainer>::importITK( filename, aFunctor );
362 }
363#endif
364
365 trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
366 throw dgtalio;
367 }
368 };
369
370
371
372
377 template <typename TContainer>
378 struct GenericReader<TContainer, 3 , DGtal::uint64_t>
379 {
388 static TContainer import(const std::string &filename);
389
390 };
391
392
397 template <typename TContainer, typename TValue>
398 struct GenericReader<TContainer, 2, TValue>
399 {
401
413 static TContainer import(const std::string &filename, unsigned int x=0,
414 unsigned int y=0);
415
416
428 template<typename TFunctor>
429 static TContainer importWithColorFunctor(const std::string &filename,
430 const TFunctor &aFunctor,
431 unsigned int x=0,
432 unsigned int y=0)
433 {
435 DGtal::IOException dgtalio;
436 //Getting image extension
437 const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
438
439 if ( extension == "ppm" )
440 {
441 return PPMReader<TContainer, TFunctor>::importPPM(filename, aFunctor);
442 }
443 else if ( extension == "raw" )
444 {
445 ASSERT( x != 0 && y != 0 );
446 typename TContainer::Point const pt (x,y);
447 return RawReader< TContainer, TFunctor >::template importRaw<DGtal::Color>( filename, pt, aFunctor);
448 }
449 else if ( extension == "gif" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" || extension == "tga" )
450 {
452 return reader.import( filename, aFunctor );
453 }
454
455 trace.error() << "Extension " << extension<< " in 2D, not yet implemented in DGtal GenericReader." << std::endl;
456 throw dgtalio;
457 }
458
459
471 template<typename TFunctor>
472 static TContainer importWithValueFunctor(const std::string &filename,
473 const TFunctor &aFunctor, unsigned int x=0,
474 unsigned int y=0)
475 {
477
478 DGtal::IOException dgtalio;
479 //Getting image extension
480 const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
481
482 if ( extension == "raw" )
483 {
484 ASSERT( x!= 0 && y != 0);
485 typename TContainer::Point const pt (x,y);
486 return RawReader< TContainer, TFunctor >::importRaw8( filename, pt, aFunctor );
487 }
488 else if ( extension == "pgm" )
489 {
490 return PGMReader<TContainer, TFunctor>::importPGM(filename, aFunctor);
491 }
492
493#ifdef WITH_HDF5
494 if (extension=="h5")
495 return HDF5Reader<TContainer, TFunctor>::importHDF5(filename, "image8bit", aFunctor);
496#endif
497
498 trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
499 throw dgtalio;
500
501 }
502
503 };
504
509 template <typename TContainer>
510 struct GenericReader<TContainer, 2, DGtal::uint32_t>
511 {
513
525 static TContainer import(const std::string &filename,unsigned int x=0,
526 unsigned int y=0 );
527
528
540 template<typename TFunctor>
541 static TContainer importWithColorFunctor(const std::string &filename,
542 const TFunctor &aFunctor, unsigned int x=0,
543 unsigned int y=0)
544 {
545
547 DGtal::IOException dgtalio;
548 //Getting image extension
549 const std::string extension = filename.substr(filename.find_last_of(".") + 1);
550
551 if ( extension == "ppm" )
552 {
553 return PPMReader<TContainer, TFunctor>::importPPM(filename, aFunctor);
554 }
555 else if ( extension == "raw" )
556 {
557 ASSERT( x != 0 && y != 0 );
558 typename TContainer::Point const pt (x,y);
559 return RawReader< TContainer, TFunctor >::template importRaw<DGtal::Color>( filename, pt, aFunctor);
560 }
561 else if ( extension == "tga" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
562 {
564 return reader.import( filename, aFunctor );
565 }
566
567 trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
568 throw dgtalio;
569 }
570
571
582 template<typename TFunctor>
583 static TContainer importWithValueFunctor(const std::string &filename,
584 const TFunctor &aFunctor, unsigned int x=0,
585 unsigned int y=0)
586 {
588
589 DGtal::IOException dgtalio;
590 //Getting image extension
591 const std::string extension = filename.substr(filename.find_last_of(".") + 1);
592
593 if ( extension == "raw" )
594 {
595 ASSERT( x != 0 && y != 0);
596 typename TContainer::Point const pt (x,y);
597 return RawReader< TContainer, TFunctor >::importRaw8 ( filename, pt, aFunctor );
598 }
599 else if ( extension == "pgm ")
600 {
601 return PGMReader<TContainer, TFunctor>::importPGM(filename, aFunctor);
602 }
603
604#ifdef WITH_HDF5
605 if (extension=="h5")
606 return HDF5Reader<TContainer, TFunctor>::importHDF5(filename, "image8bit", aFunctor);
607#endif
608
609 trace.error() << "Extension " << extension<< " not yet implemented in DGtal GenericReader." << std::endl;
610 throw dgtalio;
611
612 }
613
614 };
615
616
617} // namespace DGtal
618
619
621// Includes inline functions.
622#include "DGtal/io/readers/GenericReader.ih"
623
624// //
626
627#endif // !defined GenericReader_h
628
629#undef GenericReader_RECURSES
630#endif // else defined(GenericReader_RECURSES)
Structure representing an RGB triple with alpha component.
Definition Color.h:68
Aim: Image reader using the stb_image.h header only code.
Definition STBReader.h:62
static ImageContainer import(const std::string &filename, const Functor &aFunctor=Functor())
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:153
const auto ITK_IO_IMAGE_EXT
Definition ITKIOTrait.h:48
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())
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.