fw4spl
visu/visuVTKAdaptor/src/visuVTKAdaptor/SSnapshot.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 "visuVTKAdaptor/SSnapshot.hpp"
8 
9 #include <fwCom/Slots.hxx>
10 
11 #include <fwData/Composite.hpp>
12 #include <fwData/String.hpp>
13 
14 #include <fwDataTools/fieldHelper/Image.hpp>
15 
16 #include <fwServices/macros.hpp>
17 
18 #include <fwVtkIO/vtk.hpp>
19 
20 #include <boost/filesystem/path.hpp>
21 
22 #include <vtkActor.h>
23 #include <vtkBMPWriter.h>
24 #include <vtkImageData.h>
25 #include <vtkImageWriter.h>
26 #include <vtkJPEGWriter.h>
27 #include <vtkPNGWriter.h>
28 #include <vtkRenderer.h>
29 #include <vtkRenderWindow.h>
30 #include <vtkTIFFWriter.h>
31 #include <vtkWindowToImageFilter.h>
32 
33 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SSnapshot);
34 
35 namespace visuVTKAdaptor
36 {
37 
38 const ::fwCom::Slots::SlotKeyType SSnapshot::s_SNAP_SLOT = "snap";
39 const ::fwCom::Slots::SlotKeyType SSnapshot::s_SNAPTOIMAGE_SLOT = "snapToImage";
40 
41 SSnapshot::SSnapshot() noexcept
42 {
43  newSlot(s_SNAP_SLOT, &SSnapshot::snap, this);
44  newSlot(s_SNAPTOIMAGE_SLOT, &SSnapshot::snapToImage, this);
45 }
46 
47 //------------------------------------------------------------------------------
48 
49 SSnapshot::~SSnapshot() noexcept
50 {
51 }
52 
53 //------------------------------------------------------------------------------
54 
56 {
57  this->configureParams();
58 }
59 
60 //------------------------------------------------------------------------------
61 
63 {
64  this->initialize();
65 }
66 
67 //------------------------------------------------------------------------------
68 
70 {
71 }
72 
73 //------------------------------------------------------------------------------
74 
76 {
77 }
78 
79 //------------------------------------------------------------------------------
80 
81 void SSnapshot::snapToImage()
82 {
83  if ( !m_imageUid.empty() )
84  {
85  ::fwData::Image::sptr imageToSnap = ::fwData::Image::New();
86 
87  vtkWindowToImageFilter* snapper = vtkWindowToImageFilter::New();
88  snapper->SetMagnification( 1 );
89  snapper->SetInput( this->getRenderer()->GetRenderWindow() );
90  snapper->Update();
91 
92  vtkImageData* vtkImage = snapper->GetOutput();
93  ::fwVtkIO::fromVTKImage(vtkImage, imageToSnap);
94 
95  this->setOutput("image", imageToSnap);
96 
97  snapper->Delete();
98  }
99 }
100 
101 //------------------------------------------------------------------------------
102 
103 void SSnapshot::snap(std::string filePath)
104 {
105  SLM_ASSERT("filePath is empty", !filePath.empty());
106  namespace fs = ::boost::filesystem;
107  fs::path pathImageSnap(filePath);
108 
109  std::string ext = ".jpg";
110  ext = pathImageSnap.extension().string();
111  vtkImageWriter* writer = 0;
112 
113  if( ext == ".jpg" || ext == ".jpeg" )
114  {
115  writer = vtkJPEGWriter::New();
116  }
117  else if ( ext == ".bmp" )
118  {
119  writer = vtkBMPWriter::New();
120  }
121  else if ( ext == ".tiff" )
122  {
123  writer = vtkTIFFWriter::New();
124  }
125  else if ( ext == ".png" )
126  {
127  writer = vtkPNGWriter::New();
128  }
129  else
130  {
131  SLM_FATAL("Error: Format is not supported.");
132  }
133 
134  vtkWindowToImageFilter* snapper = vtkWindowToImageFilter::New();
135  snapper->SetMagnification( 1 );
136  snapper->SetInput( this->getRenderer()->GetRenderWindow() );
137 
138  writer->SetInputConnection( snapper->GetOutputPort() );
139  writer->SetFileName( pathImageSnap.string().c_str() );
140  writer->Write();
141 
142  snapper->Delete();
143  writer->Delete();
144 }
145 
146 } //namespace visuVTKAdaptor
FWRENDERVTK_API vtkRenderer * getRenderer()
Returns the renderer.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
FWRENDERVTK_API void configureParams()
Parse the xml configuration for renderer, picker and transform.
FWSERVICES_API void setOutput(const ::fwServices::IService::KeyType &key, const ::fwData::Object::sptr &object, size_t index=0)
Register an output object at a given key in the OSR, replacing it if it already exists.
Definition: IService.cpp:80
#define SLM_FATAL(message)
Definition: spyLog.hpp:283
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
#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
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
FWRENDERVTK_API void initialize()
Initialize the adaptor with the associated render service. (must be call in starting).
This service will snapshot the current generic scene.