fw4spl
SImageWriter.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 "ioVTK/SImageWriter.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwData/Image.hpp>
12 #include <fwData/location/Folder.hpp>
13 #include <fwData/location/SingleFile.hpp>
14 
15 #include <fwDataIO/reader/IObjectReader.hpp>
16 
17 #include <fwGui/Cursor.hpp>
18 #include <fwGui/dialog/LocationDialog.hpp>
19 #include <fwGui/dialog/MessageDialog.hpp>
20 #include <fwGui/dialog/ProgressDialog.hpp>
21 
22 #include <fwIO/IWriter.hpp>
23 
24 #include <fwJobs/IJob.hpp>
25 #include <fwJobs/Job.hpp>
26 
27 #include <fwServices/macros.hpp>
28 
29 #include <fwTools/Failed.hpp>
30 
31 #include <fwVtkIO/ImageWriter.hpp>
32 #include <fwVtkIO/MetaImageWriter.hpp>
33 #include <fwVtkIO/VtiImageWriter.hpp>
34 
35 #include <boost/algorithm/string.hpp>
36 
37 namespace ioVTK
38 {
39 
40 fwServicesRegisterMacro( ::fwIO::IWriter, ::ioVTK::SImageWriter, ::fwData::Image );
41 
42 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
43 
44 //------------------------------------------------------------------------------
45 
47 {
48  m_sigJobCreated = newSignal< JobCreatedSignalType >( JOB_CREATED_SIGNAL );
49 }
50 
51 //------------------------------------------------------------------------------
52 
54 {
55  return ::fwIO::FILE;
56 }
57 
58 //------------------------------------------------------------------------------
59 
61 {
63  static ::boost::filesystem::path _sDefaultPath("");
64 
66  dialogFile.setTitle(m_windowTitle.empty() ? "Choose an file to save an image" : m_windowTitle);
67  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
68  dialogFile.addFilter("Vtk", "*.vtk");
69  dialogFile.addFilter("Vti", "*.vti");
70  dialogFile.addFilter("MetaImage", "*.mhd");
71  dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);
72 
73  ::fwData::location::SingleFile::sptr result;
74  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
75  if (result)
76  {
77  _sDefaultPath = result->getPath().parent_path();
78  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
79  this->setFile(result->getPath());
80  }
81  else
82  {
83  this->clearLocations();
84  }
85 
86 }
87 
88 //------------------------------------------------------------------------------
89 
91 {
93 }
94 
95 //------------------------------------------------------------------------------
96 
98 {
100 }
101 
102 //------------------------------------------------------------------------------
103 
105 {
107 }
108 
109 //------------------------------------------------------------------------------
110 
111 void SImageWriter::info(std::ostream& _sstream )
112 {
113  _sstream << "SImageWriter::info";
114 }
115 
116 //------------------------------------------------------------------------------
117 
118 bool SImageWriter::saveImage( const ::boost::filesystem::path& imgFile,
119  const CSPTR(::fwData::Image)& image,
120  const SPTR(JobCreatedSignalType)& sigJobCreated )
121 {
122  SLM_TRACE_FUNC();
123  bool bValue = true;
124 
125  ::fwDataIO::writer::IObjectWriter::sptr myWriter;
126  fwGui::dialog::ProgressDialog progressMeterGUI("Saving images... ");
127  std::string ext = ::boost::filesystem::extension(imgFile);
128  ::boost::algorithm::to_lower(ext);
129 
130  if(ext == ".vtk")
131  {
132  ::fwVtkIO::ImageWriter::sptr vtkWriter = ::fwVtkIO::ImageWriter::New();
133  // Set the file system path
134  vtkWriter->setFile(imgFile);
135  myWriter = vtkWriter;
136  }
137  else if(ext == ".vti")
138  {
139  ::fwVtkIO::VtiImageWriter::sptr vtiWriter = ::fwVtkIO::VtiImageWriter::New();
140  vtiWriter->setFile(imgFile);
141  myWriter = vtiWriter;
142  }
143  else if(ext == ".mhd")
144  {
145  ::fwVtkIO::MetaImageWriter::sptr mhdWriter = ::fwVtkIO::MetaImageWriter::New();
146  mhdWriter->setFile(imgFile);
147  myWriter = mhdWriter;
148  }
149  else
150  {
151  FW_RAISE_EXCEPTION(::fwTools::Failed("Only .vtk, .vti and .mhd are supported."));
152  }
153 
154  myWriter->setObject(image);
155 
156  sigJobCreated->emit(myWriter->getJob());
157 
158  try
159  {
160  // Launch writing process
161  myWriter->write();
162  }
163  catch (const std::exception& e)
164  {
165  std::stringstream ss;
166  ss << "Warning during saving : " << e.what();
167 
169  "Warning",
170  ss.str(),
171  ::fwGui::dialog::IMessageDialog::WARNING);
172  bValue = false;
173  }
174  catch( ... )
175  {
177  "Warning",
178  "Warning during saving.",
179  ::fwGui::dialog::IMessageDialog::WARNING);
180  bValue = false;
181  }
182  return bValue;
183 }
184 
185 //------------------------------------------------------------------------------
186 
188 {
189  SLM_TRACE_FUNC();
190 
191  if( this->hasLocationDefined() )
192  {
193  // Retrieve dataStruct associated with this service
194  ::fwData::Image::csptr pImage = this->getInput< ::fwData::Image >(::fwIO::s_DATA_KEY);
195  if (!pImage)
196  {
197  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
198  pImage = this->getObject< ::fwData::Image >();
199  }
200  SLM_ASSERT("Image not instanced", pImage);
201 
202  ::fwGui::Cursor cursor;
203  cursor.setCursor(::fwGui::ICursor::BUSY);
204 
205  try
206  {
207  this->saveImage(this->getFile(), pImage, m_sigJobCreated);
208  }
209  catch(::fwTools::Failed& e)
210  {
211  OSLM_TRACE("Error : " << e.what());
212  FW_RAISE_EXCEPTION(e);
213  }
214  cursor.setDefaultCursor();
215  }
216 }
217 
218 //------------------------------------------------------------------------------
219 
220 } // namespace ioVtk
virtual IOVTK_API void configureWithIHM() override
Configure the image path.
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
virtual FWGUI_API void setCursor(::fwGui::ICursor::CursorType cursor) override
Set the cursor.
#define SPTR(_cls_)
#define CSPTR(_cls_)
VTK Image Writer.
IOVTK_API void updating() override
Updating method.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
virtual IOVTK_API void configuring() override
Configuring method.
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
FWGUI_API::fwGui::dialog::ILocationDialog & setOption(::fwGui::dialog::ILocationDialog::Options option) override
allow to set option to the file dialog mode=READ/WRITE, check=FILE_MUST_EXIST
FWGUI_API::fwData::location::ILocation::sptr show() override
Display the dialog.
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
virtual IOVTK_API void starting() override
Starting method.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
Defines the generic progress dialog for IHM. Use the Delegate design pattern. The specific implementa...
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
Definition: IWriter.cpp:49
virtual IOVTK_API::fwIO::IOPathType getIOPathType() const override
This method must be implemented by concrete service writers that use path file system to write data...
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IWriter.cpp:122
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IWriter.cpp:40
static IOVTK_API bool saveImage(const ::boost::filesystem::path &imgFile, const std::shared_ptr< const ::fwData::Image > &image, const std::shared_ptr< JobCreatedSignalType > &sigJobCreated)
Save a VTK image.
IOVTK_API SImageWriter() noexcept
Constructor. Do nothing.
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IWriter.cpp:115
#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
IOVTK_API void info(std::ostream &_sstream) override
Info method.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
Implements a failed exception class.
Writer service API. It manages extension points definition and extension configuration.
Definition: IWriter.hpp:33
FWGUI_API void setTitle(const std::string &title) override
Set the title for the dialog.
Defines the generic file/folder selector dialog for IHM.
Defines the generic cursor for IHM. Use the Delegate design pattern.
This class defines an image.
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
The namespace ioVTK contains reader, writer and helper using the fwVtkIO lib for output and input act...
virtual IOVTK_API void stopping() override
Stopping method.
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
Definition: IWriter.cpp:184
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IWriter.hpp:171