fw4spl
fwItkIO/src/fwItkIO/ImageReader.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include <boost/filesystem.hpp>
8 
9 #include <fwCore/base.hpp>
10 
11 #include <itkImageFileReader.h>
12 #include <itkImageIOFactory.h>
13 
14 #include <fwData/Image.hpp>
15 
16 #include <fwTools/IntrinsicTypes.hpp>
17 #include <fwTools/Dispatcher.hpp>
18 #include <fwTools/TypeInfoKeyTypeMapping.hpp>
19 
20 #include <fwDataIO/reader/registry/macros.hpp>
21 
22 #include "inr2itk/itkInrImageIOFactory.hpp"
23 
24 #include "fwItkIO/itk.hpp"
25 #include "fwItkIO/ImageReader.hpp"
26 #include "fwItkIO/helper/ProgressItkToFw.hpp"
27 
28 fwDataIOReaderRegisterMacro( ::fwItkIO::ImageReader );
29 
30 
31 namespace fwItkIO
32 {
33 
34 //------------------------------------------------------------------------------
35 
36 ImageReader::ImageReader(::fwDataIO::reader::IObjectReader::Key key) : ::fwData::location::enableSingleFile<
37  IObjectReader >(this)
38 {
40 }
41 
42 //------------------------------------------------------------------------------
43 
44 ImageReader::~ImageReader()
45 {
47 }
48 
49 //------------------------------------------------------------------------------
50 
52 {
53  struct Parameter
54  {
55  ::fwData::Image::sptr m_dataImage;
56  std::string m_filename;
57  ::fwItkIO::ImageReader::sptr m_fwReader;
58  };
59 
60  template<class PIXELTYPE>
61  void operator()(Parameter &param)
62  {
63  OSLM_INFO( "::fwItkIO::ImageReader::ITKLoaderFunctor with PIXELTYPE "<<
64  ::fwTools::DynamicType::string<PIXELTYPE>() );
65 
66  // VAG attention : ImageFileReader ne notifie AUCUNE progressEvent mais son ImageIO oui!!!! mais ImageFileReader ne permet pas de l'atteindre
67  // car soit mis a la mano ou alors construit lors de l'Update donc trop tard
68  // Il faut dont creer une ImageIO a la mano (*1*): affecter l'observation sur IO (*2*) et mettre le IO dans le reader (voir *3*)
69 
70  // Reader IO (*1*)
71  typename itk::ImageIOBase::Pointer imageIORead = itk::ImageIOFactory::CreateImageIO(
72  param.m_filename.c_str(), itk::ImageIOFactory::ReadMode);
73 
74  // set observation (*2*)
75  Progressor progress(imageIORead, param.m_fwReader, param.m_filename);
76 
77  // the reader
78  typedef itk::Image< PIXELTYPE, 3 > ImageType;
79  typedef itk::ImageFileReader< ImageType > ReaderType;
80  typename ReaderType::Pointer reader = ReaderType::New();
81  reader->SetFileName( param.m_filename.c_str() );
82  // attach its IO (*3*)
83  reader->SetImageIO( imageIORead );
84 
85  reader->Update();
86  typename ImageType::Pointer itkimage = reader->GetOutput();
87  ::fwItkIO::dataImageFactory< ImageType>( itkimage, param.m_dataImage );
88  }
89 
91  static const std::type_info& getImageType( const std::string &imageFileName )
92  {
93  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(
94  imageFileName.c_str(), itk::ImageIOFactory::ReadMode);
95 
96  if( !imageIO )
97  {
98  std::string errMsg;
99  errMsg = "no ImageIOFactory found to read header of file : ";
100  errMsg.append( imageFileName );
101 
102  throw( std::ios_base::failure( errMsg ) );
103  }
104 
105  imageIO->SetFileName( imageFileName.c_str() );
106  imageIO->ReadImageInformation();
107  return imageIO->GetComponentTypeInfo();
108  }
109 };
110 
111 //------------------------------------------------------------------------------
112 
113 void ImageReader::read()
114 {
115  ::boost::filesystem::path file = getFile();
116  OSLM_ASSERT("File: "<<file<<" doesn't exist", ::boost::filesystem::exists( file ) );
117  assert( !m_object.expired() );
118  assert( m_object.lock() );
119 
120  const std::type_info& ti = ITKLoaderFunctor::getImageType( file.string() );
121 
123  param.m_filename = file.string();
124  param.m_dataImage = this->getConcreteObject();
125  param.m_fwReader = this->getSptr();
126 
128 
129  assert( m_object.lock() ); // verify that ::fwData::Image is well produced
130  // Post Condition image with a pixel type
131  SLM_ASSERT("Image has an unspecified type", getConcreteObject()->getPixelType() != ::fwTools::DynamicType() );
132 }
133 
134 //------------------------------------------------------------------------------
135 
136 } // namespace fwItkIO
#define OSLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:310
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
Class defining an elementary C++ type aka unsigned char, signed char, .... signed long...
Definition: DynamicType.hpp:31
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
static void invoke()
Instanciate and invoke all functors.
Definition: Dispatcher.hpp:99
The namespace fwItkIO contains reader, writer and helper for itk image.
Contains the representation of the data objects used in the framework.