fw4spl
SImagesSubstract.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 <QVBoxLayout>
8 #include <QPushButton>
9 
10 #include <fwItkIO/itk.hpp>
11 
12 #include <fwCore/spyLog.hpp>
13 
14 // Service associated data
15 #include <fwData/Image.hpp>
16 
17 #include <fwDataTools/fieldHelper/Image.hpp>
18 
19 #include <fwGuiQt/container/QtContainer.hpp>
20 #include <fwGui/dialog/MessageDialog.hpp>
21 
22 // Services tools
23 #include <fwServices/macros.hpp>
24 
25 #include "basicRegistration/SImagesSubstract.hpp"
26 
27 #include <itkSubtractImageFilter.h>
28 
29 fwServicesRegisterMacro( ::fwGui::editor::IEditor, ::basicRegistration::SImagesSubstract );
30 
31 namespace basicRegistration
32 {
33 
34 SImagesSubstract::SImagesSubstract() noexcept :
35  ::fwGui::editor::IEditor(),
36  mpComputeButton(0)
37 {
38 
39 }
40 
41 SImagesSubstract::~SImagesSubstract() noexcept
42 {
43 }
44 
45 //------------------------------------------------------------------------------
46 
48 {
49  this->initialize();
50 }
51 
52 //------------------------------------------------------------------------------
53 
55 {
56  this->create();
57  ::fwGuiQt::container::QtContainer::sptr qtContainer = ::fwGuiQt::container::QtContainer::dynamicCast(
58  this->getContainer() );
59  QWidget* const container = qtContainer->getQtContainer();
60  SLM_ASSERT("container not instanced", container);
61 
62  QVBoxLayout* layout = new QVBoxLayout(container);
63  mpComputeButton = new QPushButton(tr("Compute"), container );
64  QObject::connect(mpComputeButton, SIGNAL(clicked()), this, SLOT(OnCompute()));
65 
66  layout->addWidget(mpComputeButton, 0);
67  container->setLayout( layout );
68 }
69 
70 //------------------------------------------------------------------------------
71 
73 {
74  this->destroy();
75 
76 }
77 
78 //------------------------------------------------------------------------------
79 
81 {
82  ::fwTools::Type REQUESTED_TYPE = ::fwTools::Type::create("int16");
83 
84  ::fwData::Image::csptr image1 = this->getInput< ::fwData::Image>("image1");
85  ::fwData::Image::csptr image2 = this->getInput< ::fwData::Image>("image2");
86  ::fwData::Image::sptr imageResult = this->getInOut< ::fwData::Image>("result");
87 
88  // Test if the both images have the same type and it is signed short.
89  bool isSameType =
90  ( ((image1->getDataArray())->getType() == (image2->getDataArray())->getType())&&
91  ((image1->getDataArray())->getType() == REQUESTED_TYPE));
92 
93  if(isSameType)
94  {
95  // test if the both images have the same size.
96  bool isSameSize = (image1->getSize() == image2->getSize());
97  if(isSameSize)
98  {
99  typedef itk::Image< std::int16_t, 3 > ImageType;
100 
101  ImageType::Pointer itkImage1 = ::fwItkIO::itkImageFactory< ImageType >( image1 );
102  SLM_ASSERT("Unable to convert fwData::Image to itkImage", itkImage1);
103 
104  ImageType::Pointer itkImage2 = ::fwItkIO::itkImageFactory< ImageType >( image2 );
105  SLM_ASSERT("Unable to convert fwData::Image to itkImage", itkImage2);
106 
107  ImageType::Pointer output;
108 
109  //Create filter
110  typedef ::itk::SubtractImageFilter< ImageType, ImageType, ImageType > SubtractImageFilterType;
111  SubtractImageFilterType::Pointer filter;
112  filter = SubtractImageFilterType::New();
113  assert(filter);
114 
115  filter->SetInput1( itkImage1 );
116  filter->SetInput2( itkImage2 );
117  filter->Update();
118  output = filter->GetOutput();
119  assert(output->GetSource());
120  ::fwItkIO::dataImageFactory< ImageType >( output, imageResult, true );
121 
123  sig->asyncEmit();
124  }
125  else
126  {
128  "Both images must have the same size.",
129  ::fwGui::dialog::IMessageDialog::WARNING);
130  }
131  }
132  else
133  {
135  "Both Images must have signed short as type.",
136  ::fwGui::dialog::IMessageDialog::WARNING);
137  }
138 }
139 
140 //------------------------------------------------------------------------------
141 
143 {
144  // Classic default approach to update service when oject change
145  this->updating();
146 }
147 
148 //------------------------------------------------------------------------------
149 
150 void SImagesSubstract::OnCompute()
151 {
152  this->updating();
153 }
154 // -----------------------------------------------------------------------------
155 } // namespace basicRegistration
virtual BASICREGISTRATION_API void stopping() override
Overrides.
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
Defines the service interface managing the editor service for object.
Definition: IEditor.hpp:25
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
FWGUI_API void destroy()
Stops sub-views and toobar services. Destroys view, sub-views and toolbar containers.
virtual BASICREGISTRATION_API void starting() override
Overrides.
The namespace basicRegistration contains services to perfom a basic registration between images and m...
virtual BASICREGISTRATION_API void swapping() override
Overrides.
virtual BASICREGISTRATION_API void configuring() override
Configure the service before starting. Apply the configuration to service.
#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
Class describing an elementary C++ type aka unsigned char, signed char, .... int, float...
Definition: Type.hpp:32
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
virtual BASICREGISTRATION_API void updating() override
Overrides.
Compute the substraction of two images.
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...
FWGUI_API void initialize()
Initialize managers.