fw4spl
fwItkIO/src/fwItkIO/ImageWriter.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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 "fwItkIO/ImageWriter.hpp"
8 
9 #include "fwItkIO/helper/ProgressItkToFw.hpp"
10 #include "fwItkIO/itk.hpp"
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwDataIO/writer/registry/macros.hpp>
15 
16 #include <fwTools/Dispatcher.hpp>
17 #include <fwTools/DynamicTypeKeyTypeMapping.hpp>
18 #include <fwTools/IntrinsicTypes.hpp>
19 
20 #include <boost/filesystem.hpp>
21 
22 #include <itkImageFileWriter.h>
23 
24 fwDataIOWriterRegisterMacro( ::fwItkIO::ImageWriter );
25 
26 namespace fwItkIO
27 {
28 //------------------------------------------------------------------------------
29 
30 ImageWriter::ImageWriter(::fwDataIO::writer::IObjectWriter::Key key) :
31  ::fwData::location::enableSingleFile< ::fwDataIO::writer::IObjectWriter >(this)
32 {
34 }
35 
36 //------------------------------------------------------------------------------
37 
38 ImageWriter::~ImageWriter()
39 {
41 }
42 
44 {
45 
46  struct Parameter
47  {
48  std::string m_filename;
49  ::fwData::Image::csptr m_dataImage;
50  ::fwItkIO::ImageWriter::sptr m_fwWriter;
51  };
52 
53  //------------------------------------------------------------------------------
54 
55  template<class PIXELTYPE>
56  void operator()( const Parameter& param )
57  {
58  OSLM_DEBUG( "itk::ImageFileWriter with PIXELTYPE "<< fwTools::DynamicType::string<PIXELTYPE>() );
59 
60  // VAG attention : ImageFileReader ne notifie AUCUNE progressEvent mais son ImageIO oui!!!! mais ImageFileReader
61  // ne permet pas de l'atteindre
62  // car soit mis a la mano ou alors construit lors de l'Update donc trop tard
63  // Il faut dont creer une ImageIO a la mano (*1*): affecter l'observation sur IO (*2*) et mettre le IO dans le
64  // reader (voir *3*)
65 
66  // Reader IO (*1*)
67  typename itk::ImageIOBase::Pointer imageIOWrite = itk::ImageIOFactory::CreateImageIO(
68  param.m_filename.c_str(), itk::ImageIOFactory::WriteMode);
69  assert( imageIOWrite.IsNotNull() );
70 
71  // create writer
72  typedef itk::Image< PIXELTYPE, 3> itkImageType;
73  typedef typename itk::ImageFileWriter< itkImageType > WriterType;
74  typename WriterType::Pointer writer = WriterType::New();
75 
76  // set observation (*2*)
77  itk::LightProcessObject::Pointer castHelper = (itk::LightProcessObject*)(imageIOWrite.GetPointer());
78  assert( castHelper.IsNotNull() );
79  Progressor progress(castHelper, param.m_fwWriter, param.m_filename);
80 
81  // create itk Image
82  typename itkImageType::Pointer itkImage = ::fwItkIO::itkImageFactory<itkImageType>( param.m_dataImage );
83 
84  writer->SetFileName( param.m_filename.c_str() );
85  writer->SetInput( itkImage );
86  writer->SetImageIO( imageIOWrite ); // (*3*)
87 
88  // save image;
89  writer->Update();
90  }
91 };
92 
93 //------------------------------------------------------------------------------
94 
95 void ImageWriter::write()
96 {
97  assert( !m_object.expired() );
98  assert( m_object.lock() );
99 
100  ITKSaverFunctor::Parameter saverParam;
101  saverParam.m_filename = this->getFile().string();
102  saverParam.m_dataImage = getConcreteObject();
103  saverParam.m_fwWriter = this->getSptr();
104  assert( saverParam.m_dataImage );
105 
107  saverParam.m_dataImage->getPixelType(), saverParam );
108 }
109 
110 //------------------------------------------------------------------------------
111 
112 std::string ImageWriter::extension()
113 {
114  if ( getFile().empty() || ( getFile().string().find(".inr.gz") != std::string::npos ) )
115  {
116  return ".inr.gz";
117  }
118  else
119  {
120  return ::boost::filesystem::extension( getFile().leaf() );
121  }
122 }
123 
124 //------------------------------------------------------------------------------
125 
126 } // namespace fwItkIO
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
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.
#define OSLM_DEBUG(message)
Definition: spyLog.hpp:241