fw4spl
JpgImageWriter.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/JpgImageWriter.hpp"
8 
9 #include "fwItkIO/helper/ProgressItkToFw.hpp"
10 #include "fwItkIO/itk.hpp"
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwData/Composite.hpp>
15 #include <fwData/Image.hpp>
16 #include <fwData/Integer.hpp>
17 #include <fwData/TransferFunction.hpp>
18 
19 #include <fwDataIO/writer/registry/macros.hpp>
20 
21 #include <fwDataTools/fieldHelper/Image.hpp>
22 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
23 
24 #include <fwTools/Dispatcher.hpp>
25 #include <fwTools/DynamicTypeKeyTypeMapping.hpp>
26 #include <fwTools/IntrinsicTypes.hpp>
27 
28 #include <boost/filesystem.hpp>
29 
30 #include <itkImageSeriesWriter.h>
31 #include <itkIntensityWindowingImageFilter.h>
32 #include <itkJPEGImageIOFactory.h>
33 #include <itkNumericSeriesFileNames.h>
34 
35 fwDataIOWriterRegisterMacro( ::fwItkIO::JpgImageWriter );
36 
37 namespace fwItkIO
38 {
39 
40 //------------------------------------------------------------------------------
41 
42 JpgImageWriter::JpgImageWriter(::fwDataIO::writer::IObjectWriter::Key key) :
43  ::fwData::location::enableFolder< ::fwDataIO::writer::IObjectWriter >(this)
44 {
46 }
47 
48 //------------------------------------------------------------------------------
49 
50 JpgImageWriter::~JpgImageWriter()
51 {
53 }
54 
55 //------------------------------------------------------------------------------
56 
58 {
60  {
61  // force register/link_with JPEGImageIOFactory
62  ::itk::JPEGImageIOFactory::RegisterOneFactory();
63  }
64  struct Parameter
65  {
66  std::string m_filename;
67  ::fwData::Image::csptr m_dataImage;
68  ::fwItkIO::JpgImageWriter::sptr m_fwWriter;
69  };
70 
71  //------------------------------------------------------------------------------
72 
73  template<class PIXELTYPE>
74  void operator()( const Parameter& param )
75  {
76  OSLM_DEBUG( "itk::ImageSeriesWriter with PIXELTYPE "<< fwTools::DynamicType::string<PIXELTYPE>() );
77 
78  ::fwData::Image::csptr image = param.m_dataImage;
79 
80  // VAG attention : ImageFileReader ne notifie AUCUNE progressEvent mais son ImageIO oui!!!! mais ImageFileReader
81  // ne permet pas de l'atteindre
82  // car soit mis a la mano ou alors construit lors de l'Update donc trop tard
83  // Il faut dont creer une ImageIO a la mano (*1*): affecter l'observation sur IO (*2*) et mettre le IO dans le
84  // reader (voir *3*)
85 
86  // Reader IO (*1*)
87  typename itk::ImageIOBase::Pointer imageIOWrite = itk::ImageIOFactory::CreateImageIO( "image.jpg",
88  itk::ImageIOFactory::WriteMode);
89  assert( imageIOWrite.IsNotNull() );
90 
91  // create writer
92  typedef itk::Image< PIXELTYPE, 3> itkImageType;
93  typedef itk::Image< unsigned char, 2 > Image2DType;
94  typedef typename itk::ImageSeriesWriter< itkImageType, Image2DType > WriterType;
95  typename WriterType::Pointer writer = WriterType::New();
96 
97  // set observation (*2*)
98  itk::LightProcessObject::Pointer castHelper = (itk::LightProcessObject*)(imageIOWrite.GetPointer());
99  assert( castHelper.IsNotNull() );
100  Progressor progress(castHelper, param.m_fwWriter, param.m_filename);
101 
102  // create itk Image
103  typename itkImageType::Pointer itkImage = ::fwItkIO::itkImageFactory<itkImageType>( image );
104 
105  typedef ::itk::IntensityWindowingImageFilter< itkImageType, itkImageType > RescaleFilterType;
106  typename RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
107 
108  double min, max;
109  ::fwData::Composite::sptr poolTF;
110  poolTF =
112  if(poolTF)
113  {
115  if(iter != poolTF->end())
116  {
117  ::fwData::TransferFunction::sptr tf;
118  tf = ::fwData::TransferFunction::dynamicCast(iter->second);
119  min = tf->getWLMinMax().first;
120  max = tf->getWLMinMax().second;
121  }
122  }
123  else
124  {
126  }
127 
128  rescaleFilter->SetWindowMinimum( min );
129  rescaleFilter->SetWindowMaximum( max );
130  rescaleFilter->SetOutputMinimum( 0 );
131  rescaleFilter->SetOutputMaximum( 255 );
132  rescaleFilter->InPlaceOff();
133  rescaleFilter->SetInput( itkImage );
134  rescaleFilter->Update();
135 
136  writer->SetInput( rescaleFilter->GetOutput() );
137 
138  typedef itk::NumericSeriesFileNames NameGeneratorType;
139 
140  NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
141 
142  std::string format = param.m_filename;
143  format += "/%04d.jpg";
144  nameGenerator->SetSeriesFormat( format.c_str() );
145  nameGenerator->SetStartIndex( 1 );
146  nameGenerator->SetEndIndex( image->getSize()[2] );
147  nameGenerator->SetIncrementIndex( 1 );
148 
149  writer->SetFileNames( nameGenerator->GetFileNames() );
150 
151  writer->SetImageIO( imageIOWrite );
152 
153  // save image;
154  writer->Update();
155  }
156 };
157 
158 //------------------------------------------------------------------------------
159 
160 void JpgImageWriter::write()
161 {
162  assert( !m_object.expired() );
163  assert( m_object.lock() );
164 
166  saverParam.m_filename = this->getFolder().string();
167  saverParam.m_dataImage = this->getConcreteObject();
168  saverParam.m_fwWriter = this->getSptr();
169  assert( saverParam.m_dataImage );
170 
172  saverParam.m_dataImage->getPixelType(), saverParam );
173 }
174 
175 //------------------------------------------------------------------------------
176 
177 std::string JpgImageWriter::extension()
178 {
179  return ".jpg";
180 }
181 
182 } // namespace fwItkIO
static FWDATA_API const std::string s_DEFAULT_TF_NAME
Default transfer function name.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
static FWDATATOOLS_API const std::string m_transferFunctionCompositeId
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.
This class defines a composite object.
static void getMinMax(const ::fwData::Image::csptr _img, MINMAXTYPE &_min, MINMAXTYPE &_max)
Return minimum and maximum values contained in image. If image min or max value is out of MINMAXTYPE ...
#define OSLM_DEBUG(message)
Definition: spyLog.hpp:241