fw4spl
VtiImageWriter.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 "fwVtkIO/VtiImageWriter.hpp"
8 
9 #include "fwVtkIO/helper/vtkLambdaCommand.hpp"
10 #include "fwVtkIO/vtk.hpp"
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwDataIO/writer/registry/macros.hpp>
15 
16 #include <fwJobs/IJob.hpp>
17 #include <fwJobs/Observer.hpp>
18 
19 #include <vtkImageData.h>
20 #include <vtkSmartPointer.h>
21 #include <vtkXMLImageDataWriter.h>
22 #include <vtkZLibDataCompressor.h>
23 
24 fwDataIOWriterRegisterMacro( ::fwVtkIO::VtiImageWriter );
25 
26 namespace fwVtkIO
27 {
28 //------------------------------------------------------------------------------
29 
31  ::fwData::location::enableSingleFile< ::fwDataIO::writer::IObjectWriter >(this),
32  m_job(::fwJobs::Observer::New("VTK Image Writer"))
33 {
35 }
36 
37 //------------------------------------------------------------------------------
38 
40 {
42 }
43 
44 //------------------------------------------------------------------------------
45 
47 {
48  using namespace fwVtkIO::helper;
49 
50  assert( !m_object.expired() );
51  assert( m_object.lock() );
52 
53  ::fwData::Image::csptr pImage = getConcreteObject();
54 
55  vtkSmartPointer< vtkXMLImageDataWriter > writer = vtkSmartPointer< vtkXMLImageDataWriter >::New();
56  vtkSmartPointer< vtkImageData > vtkImage = vtkSmartPointer< vtkImageData >::New();
57  ::fwVtkIO::toVTKImage( pImage, vtkImage );
58  writer->SetInputData( vtkImage );
59  writer->SetFileName( this->getFile().string().c_str() );
60  writer->SetDataModeToAppended();
61 
62  vtkSmartPointer< vtkZLibDataCompressor > compressor = vtkSmartPointer< vtkZLibDataCompressor >::New();
63  compressor->SetCompressionLevel(1);
64  writer->SetCompressor( compressor );
65  writer->EncodeAppendedDataOff();
66 
67  vtkSmartPointer<vtkLambdaCommand> progressCallback;
68  progressCallback = vtkSmartPointer<vtkLambdaCommand>::New();
69  progressCallback->SetCallback([this](vtkObject* caller, long unsigned int, void* )
70  {
71  auto filter = static_cast<vtkXMLImageDataWriter*>(caller);
72  m_job->doneWork( filter->GetProgress()*100 );
73  });
74 
75  writer->AddObserver(vtkCommand::ProgressEvent, progressCallback);
76  m_job->addSimpleCancelHook( [&]()
77  {
78  writer->AbortExecuteOn();
79  });
80  writer->Write();
81  m_job->finish();
82 }
83 
84 //------------------------------------------------------------------------------
85 
87 {
88  return ".vti";
89 }
90 
91 //------------------------------------------------------------------------------
92 
94 {
95  return m_job;
96 }
97 
98 } // namespace fwVtkIO
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
FWVTKIO_API void write() override
Writing operator.
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
::fwTools::Object::cwptr m_object
Object write on filesystem by the process.
virtual std::shared_ptr< const DataType > getConcreteObject() const
m_object getter.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
std::shared_ptr< ::fwJobs::IJob > sptr
Cancel request callback type.
Definition: IJob.hpp:54
FWVTKIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const override
FWVTKIO_API ~VtiImageWriter()
Destructor.
FWVTKIO_API VtiImageWriter(::fwDataIO::writer::IObjectWriter::Key key)
Constructor.
FWVTKIO_API std::string extension() override
Contains the representation of the data objects used in the framework.
This namespace fwJobs provides jobs management.