fw4spl
MetaImageReader.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 "fwVtkIO/vtk.hpp"
8 #include "fwVtkIO/MetaImageReader.hpp"
9 #include "fwVtkIO/helper/vtkLambdaCommand.hpp"
10 
11 #include <fwCore/base.hpp>
12 
13 #include <fwDataIO/reader/registry/macros.hpp>
14 
15 #include <fwJobs/IJob.hpp>
16 #include <fwJobs/Observer.hpp>
17 
18 #include <vtkSmartPointer.h>
19 #include <vtkMetaImageReader.h>
20 #include <vtkImageData.h>
21 
22 
23 fwDataIOReaderRegisterMacro( ::fwVtkIO::MetaImageReader );
24 
25 
26 namespace fwVtkIO
27 {
28 //------------------------------------------------------------------------------
29 
31  ::fwData::location::enableSingleFile< ::fwDataIO::reader::IObjectReader >(this),
32  m_job(::fwJobs::Observer::New("Meta image reader"))
33 {
35 }
36 
37 //------------------------------------------------------------------------------
38 
40 {
42 }
43 
44 //------------------------------------------------------------------------------
45 
47 {
48  using namespace fwVtkIO::helper;
49  assert( !m_object.expired() );
50  assert( m_object.lock() );
51 
52  ::fwData::Image::sptr pImage = this->getConcreteObject();
53 
54  vtkSmartPointer< vtkMetaImageReader > reader = vtkSmartPointer< vtkMetaImageReader >::New();
55  reader->SetFileName(this->getFile().string().c_str());
56 
57  vtkSmartPointer<vtkLambdaCommand> progressCallback;
58 
59  progressCallback = vtkSmartPointer<vtkLambdaCommand>::New();
60  progressCallback->SetCallback(
61  [&](vtkObject* caller, long unsigned int, void* )
62  {
63  auto filter = static_cast<vtkMetaImageReader*>(caller);
64  m_job->doneWork( filter->GetProgress()*100 );
65  }
66  );
67  reader->AddObserver(vtkCommand::ProgressEvent, progressCallback);
68 
69  m_job->addSimpleCancelHook([&] { reader->AbortExecuteOn(); });
70 
71  reader->Update();
72  reader->UpdateInformation();
73  reader->PropagateUpdateExtent();
74 
75  vtkDataObject *obj = reader->GetOutput();
76  vtkImageData* img = vtkImageData::SafeDownCast(obj);
77 
78  m_job->finish();
79 
80  FW_RAISE_IF("MetaImageReader cannot read mhd image file :"<<this->getFile().string(), !img);
81  try
82  {
83  ::fwVtkIO::fromVTKImage( img, pImage);
84  }
85  catch( std::exception &e)
86  {
87  FW_RAISE("MetaImage to fwData::Image failed : "<<e.what());
88  }
89 }
90 
91 //------------------------------------------------------------------------------
92 
94 {
95  return ".mhd";
96 }
97 
98 //------------------------------------------------------------------------------
99 
101 {
102  return m_job;
103 }
104 
105 //------------------------------------------------------------------------------
106 
107 } // namespace fwVtkIO
FWVTKIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const override
#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.
::fwTools::Object::wptr m_object
Object result of reading process.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
FWVTKIO_API std::string extension() override
std::shared_ptr< ::fwJobs::IJob > sptr
Cancel request callback type.
Definition: IJob.hpp:54
FWVTKIO_API MetaImageReader(::fwDataIO::reader::IObjectReader::Key key)
Constructor.
virtual std::shared_ptr< DataType > getConcreteObject()
m_object getter.
Contains the representation of the data objects used in the framework.
FWVTKIO_API void read() override
Reading operator.
FWVTKIO_API ~MetaImageReader()
Destructor.
This namespace fwJobs provides jobs management.