fw4spl
BitmapImageReader.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2017.
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/BitmapImageReader.hpp"
8 
9 #include "fwVtkIO/helper/vtkLambdaCommand.hpp"
10 #include "fwVtkIO/vtk.hpp"
11 
12 #include <fwDataIO/reader/registry/macros.hpp>
13 
14 #include <fwJobs/IJob.hpp>
15 #include <fwJobs/Observer.hpp>
16 
17 #include <boost/tokenizer.hpp>
18 
19 #include <vtkGenericDataObjectReader.h>
20 #include <vtkImageData.h>
21 #include <vtkImageReader2.h>
22 #include <vtkImageReader2Collection.h>
23 #include <vtkImageReader2Factory.h>
24 #include <vtkSmartPointer.h>
25 
26 fwDataIOReaderRegisterMacro( ::fwVtkIO::BitmapImageReader );
27 
28 namespace fwVtkIO
29 {
30 //------------------------------------------------------------------------------
31 
33  ::fwData::location::enableSingleFile< ::fwDataIO::reader::IObjectReader >(this),
34  m_job(::fwJobs::Observer::New("Bitmap image reader"))
35 {
36  /* Initialize the available extensions */
37  std::vector<std::string> ext;
39 
40  if(ext.size() > 0)
41  {
42  m_availableExtensions = ext.at(0);
43  for(std::vector<std::string>::size_type i = 1; i < ext.size(); i++)
44  {
45  m_availableExtensions = m_availableExtensions + " " + ext.at(i);
46  }
47  }
48 }
49 
50 //------------------------------------------------------------------------------
51 
53 {
54 }
55 
56 //------------------------------------------------------------------------------
57 
59 {
60  SLM_ASSERT("The current object has expired.", !m_object.expired() );
61  SLM_ASSERT("Unable to lock object", m_object.lock() );
62 
63  ::fwData::Image::sptr pImage = getConcreteObject();
64 
65  // Use a vtkImageReader2Factory to automatically detect the type of the input file
66  // And select the right reader for the file
67  vtkSmartPointer<vtkImageReader2Factory> factory =
68  vtkSmartPointer< vtkImageReader2Factory >::New();
69  vtkSmartPointer<vtkImageReader2> reader =
70  factory->CreateImageReader2( this->getFile().string().c_str() );
71 
72  FW_RAISE_IF("BitmapImageReader cannot read Bitmap image file :" << this->getFile().string(), !reader);
73 
74  reader->SetFileName(this->getFile().string().c_str());
75 
76  using namespace fwVtkIO::helper;
77  vtkSmartPointer< vtkLambdaCommand > progressCallback;
78 
79  progressCallback = vtkSmartPointer< vtkLambdaCommand >::New();
80  progressCallback->SetCallback(
81  [&](vtkObject* caller, long unsigned int, void*)
82  {
83  auto filter = static_cast<vtkGenericDataObjectReader*>(caller);
84  m_job->doneWork(static_cast<uint64_t>(filter->GetProgress() * 100.0));
85  }
86  );
87  reader->AddObserver(vtkCommand::ProgressEvent, progressCallback);
88 
89  m_job->addSimpleCancelHook([&] { reader->AbortExecuteOn(); });
90 
91  reader->Update();
92 
93  vtkDataObject* obj = reader->GetOutput();
94  vtkImageData* img = vtkImageData::SafeDownCast(obj);
95 
96  m_job->finish();
97 
98  FW_RAISE_IF("BitmapImageReader cannot read Bitmap image file :"<<this->getFile().string(), !img);
99  try
100  {
101  ::fwVtkIO::fromVTKImage( img, pImage);
102  }
103  catch( std::exception& e)
104  {
105  FW_RAISE("BitmapImage to fwData::Image failed "<<e.what());
106  }
107 }
108 
109 //------------------------------------------------------------------------------
110 
112 {
113  return m_availableExtensions;
114 }
115 
116 //------------------------------------------------------------------------------
117 
119 {
120  return m_job;
121 }
122 
123 //------------------------------------------------------------------------------
124 
125 void BitmapImageReader::getAvailableExtensions(std::vector<std::string>& ext)
126 {
127  /* Get the collection of available bitmap image readers */
128  vtkSmartPointer<vtkImageReader2Collection> ir2c = vtkSmartPointer<vtkImageReader2Collection>::New();
129  vtkImageReader2Factory::GetRegisteredReaders(ir2c);
130 
131  const ::boost::char_separator<char> sep(" ");
132 
133  /* Iterate over the elements of the collection */
134  ir2c->InitTraversal();
135  for(int i = 0; i < ir2c->GetNumberOfItems(); i++)
136  {
137  vtkImageReader2* ir2 = ir2c->GetNextItem();
138 
139  /* Split the string returned by GetFileExtensions() (several extensions can be available for the same type) */
140  const std::string s = ir2->GetFileExtensions();
141  const ::boost::tokenizer< ::boost::char_separator<char> > tokens {s, sep};
142 
143  for(const auto& token : tokens)
144  {
145  ext.push_back(token);
146  }
147  }
148 }
149 
150 //------------------------------------------------------------------------------
151 
152 } // namespace fwVtkIO
static FWVTKIO_API void getAvailableExtensions(std::vector< std::string > &ext)
FWVTKIO_API ~BitmapImageReader()
Destructor.
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
::fwTools::Object::wptr m_object
Object result of reading process.
FWVTKIO_API BitmapImageReader(::fwDataIO::reader::IObjectReader::Key key)
Constructor.
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
Contains fwAtomsFilter::factory utilities.
FWVTKIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const override
FWVTKIO_API std::string extension() override
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
virtual std::shared_ptr< DataType > getConcreteObject()
m_object getter.
Contains the representation of the data objects used in the framework.
This namespace fwJobs provides jobs management.
FWVTKIO_API void read() override
Reading operator.