fw4spl
MetaImageWriter.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/MetaImageWriter.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 <vtkMetaImageWriter.h>
21 #include <vtkSmartPointer.h>
22 
23 fwDataIOWriterRegisterMacro( ::fwVtkIO::MetaImageWriter );
24 
25 namespace fwVtkIO
26 {
27 //------------------------------------------------------------------------------
28 
30  ::fwData::location::enableSingleFile< ::fwDataIO::writer::IObjectWriter >(this),
31  m_job(::fwJobs::Observer::New("MetaImage writer"))
32 {
34 }
35 
36 //------------------------------------------------------------------------------
37 
39 {
41 }
42 
43 //------------------------------------------------------------------------------
44 
46 {
47  using namespace fwVtkIO::helper;
48 
49  assert( !m_object.expired() );
50  assert( m_object.lock() );
51 
52  ::fwData::Image::csptr pImage = getConcreteObject();
53 
54  vtkSmartPointer< vtkMetaImageWriter > writer = vtkSmartPointer< vtkMetaImageWriter >::New();
55  vtkSmartPointer< vtkImageData > vtkImage = vtkSmartPointer< vtkImageData >::New();
56  ::fwVtkIO::toVTKImage( pImage, vtkImage );
57  writer->SetInputData( vtkImage );
58  writer->SetFileName( this->getFile().string().c_str() );
59  writer->SetCompression(true);
60 
61  vtkSmartPointer<vtkLambdaCommand> progressCallback;
62  progressCallback = vtkSmartPointer<vtkLambdaCommand>::New();
63  progressCallback->SetCallback([this](vtkObject* caller, long unsigned int, void* )
64  {
65  auto filter = static_cast<vtkMetaImageWriter*>(caller);
66  m_job->doneWork( filter->GetProgress()*100 );
67  });
68 
69  writer->AddObserver(vtkCommand::ProgressEvent, progressCallback);
70  m_job->addSimpleCancelHook( [&]()
71  {
72  writer->AbortExecuteOn();
73  });
74  writer->Write();
75  m_job->finish();
76 }
77 
78 //------------------------------------------------------------------------------
79 
81 {
82  return ".mhd";
83 }
84 
85 //------------------------------------------------------------------------------
86 
88 {
89  return m_job;
90 }
91 
92 } // namespace fwVtkIO
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
Write a MetaImage.
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 MetaImageWriter(::fwDataIO::writer::IObjectWriter::Key key)
Constructor.
FWVTKIO_API std::string extension() override
FWVTKIO_API void write() override
Writing operator.
FWVTKIO_API ~MetaImageWriter()
Destructor.
Contains the representation of the data objects used in the framework.
FWVTKIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const override
This namespace fwJobs provides jobs management.