]> Creatis software - clitk.git/blob - vv/vvToolImageArithm.cxx
Reformatted using new coding style
[clitk.git] / vv / vvToolImageArithm.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18
19 #include "vvToolImageArithm.h"
20 #include "vvSlicer.h"
21 #include "clitkImageArithmGenericFilter.h"
22 #include <QMessageBox>
23
24 //------------------------------------------------------------------------------
25 // Create the tool and automagically (I like this word) insert it in
26 // the main window menu.
27 ADD_TOOL(vvToolImageArithm);
28 //------------------------------------------------------------------------------
29
30 //------------------------------------------------------------------------------
31 vvToolImageArithm::vvToolImageArithm(vvMainWindowBase * parent, Qt::WindowFlags f)
32   :vvToolWidgetBase(parent, f),
33    vvToolBase<vvToolImageArithm>(parent),
34    Ui::vvToolImageArithm()
35 {
36   // Setup the UI
37   Ui_vvToolImageArithm::setupUi(mToolWidget);
38
39   // Main filter
40   mFilter = new clitk::ImageArithmGenericFilter<args_info_clitkImageArithm>;
41
42   // Set how many inputs are needed for this tool
43   AddInputSelector("Select first image (A)", mFilter);
44   AddInputSelector("Select second image (B)", mFilter, true);
45 }
46 //------------------------------------------------------------------------------
47
48
49 //------------------------------------------------------------------------------
50 vvToolImageArithm::~vvToolImageArithm()
51 {
52 }
53 //------------------------------------------------------------------------------
54
55
56 //------------------------------------------------------------------------------
57 void vvToolImageArithm::Initialize()
58 {
59   SetToolName("ImageArithm");
60   SetToolMenuName("ImageArithm");
61   SetToolIconFilename(":/common/icons/arithm.png");
62   SetToolTip("Perform simple arithmetic operations on one or two images.");
63   SetToolExperimental(false);
64 }
65 //------------------------------------------------------------------------------
66
67
68 //------------------------------------------------------------------------------
69 void vvToolImageArithm::InputIsSelected(std::vector<vvSlicerManager *> & l)
70 {
71   mInput1 = l[0];
72   mInput2 = l[1];
73   mTwoInputs = true;
74   mGroupBoxOneInput->setEnabled(false);
75   mGroupBoxTwoInputs->setEnabled(true);
76 }
77 //------------------------------------------------------------------------------
78
79
80 //------------------------------------------------------------------------------
81 void vvToolImageArithm::InputIsSelected(vvSlicerManager * l)
82 {
83   mInput1 = l;
84   mTwoInputs = false;
85   // DD("Single input");
86   mGroupBoxTwoInputs->setEnabled(false);
87   mGroupBoxOneInput->setEnabled(true);
88 }
89 //------------------------------------------------------------------------------
90
91
92 //------------------------------------------------------------------------------
93 void vvToolImageArithm::GetArgsInfoFromGUI()
94 {
95   //  DD("GetArgsInfoFromGUI");
96   mArgsInfo.input1_given = false;
97   if (mTwoInputs) {
98     mArgsInfo.input2_given = true;
99     mArgsInfo.input2_arg = new char; // need to indicate that there are two inputs
100     mArgsInfo.scalar_given = false;
101     if (radioButtonSum->isChecked()) mArgsInfo.operation_arg = 0;
102     if (radioButtonMultiply->isChecked()) mArgsInfo.operation_arg = 1;
103     if (radioButtonDivide->isChecked()) mArgsInfo.operation_arg = 2;
104     if (radioButtonMax->isChecked()) mArgsInfo.operation_arg = 3;
105     if (radioButtonMin->isChecked()) mArgsInfo.operation_arg = 4;
106     if (radioButtonAbsDiff->isChecked()) mArgsInfo.operation_arg = 5;
107     if (radioButtonSquaredDiff->isChecked()) mArgsInfo.operation_arg = 6;
108   } else {
109     mArgsInfo.input2_given = false;
110     mArgsInfo.scalar_given = true;
111     if (radioButtonSumV->isChecked()) mArgsInfo.operation_arg = 0;
112     if (radioButtonMultiplyV->isChecked()) mArgsInfo.operation_arg = 1;
113     if (radioButtonInverseV->isChecked()) mArgsInfo.operation_arg = 2;
114     if (radioButtonMaxV->isChecked()) mArgsInfo.operation_arg = 3;
115     if (radioButtonMinV->isChecked()) mArgsInfo.operation_arg = 4;
116     if (radioButtonAbsDiffV->isChecked()) mArgsInfo.operation_arg = 5;
117     if (radioButtonSquaredDiffV->isChecked()) mArgsInfo.operation_arg = 6;
118     if (radioButtonLogV->isChecked()) mArgsInfo.operation_arg = 7;
119     if (radioButtonExpV->isChecked()) mArgsInfo.operation_arg = 8;
120     if (radioButtonSqrtV->isChecked()) mArgsInfo.operation_arg = 9;
121     mArgsInfo.scalar_given = true;
122     mArgsInfo.scalar_arg = mValueSpinBox->value();
123   }
124   mArgsInfo.output_given = false;
125   mArgsInfo.verbose_flag = false;
126   mArgsInfo.setFloatOutput_flag = mCheckBoxUseFloatOutputType->isChecked();
127   mArgsInfo.imagetypes_flag = false;
128 }
129 //------------------------------------------------------------------------------
130
131
132 //------------------------------------------------------------------------------
133 void vvToolImageArithm::apply()
134 {
135   if (!mCurrentSlicerManager) close();
136   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
137   GetArgsInfoFromGUI();
138
139   std::vector<vvImage::Pointer> inputs;
140   if (mTwoInputs) {
141     // Input
142     inputs.push_back(mInput1->GetImage());
143     inputs.push_back(mInput2->GetImage());
144
145     // Check input type
146     if (inputs[0]->GetScalarTypeAsString() != inputs[1]->GetScalarTypeAsString()) {
147       std::cerr << "Sorry inputs should have the same pixeltype." << std::endl;
148       std::cerr << "Input1 = " << inputs[0]->GetScalarTypeAsString() << std::endl;
149       std::cerr << "Input2 = " << inputs[1]->GetScalarTypeAsString() << std::endl;
150       QApplication::restoreOverrideCursor();
151       QMessageBox::information(this, "Wrong image type","Sorry, could not perform operation. Please select inputs with same pixe type.");
152       close();
153       return;
154     }
155   } else {
156     // Input
157     inputs.push_back(mInput1->GetImage());
158     DD("Single input");
159   }
160
161   // Main filter
162   clitk::ImageArithmGenericFilter<args_info_clitkImageArithm>::Pointer filter =
163     clitk::ImageArithmGenericFilter<args_info_clitkImageArithm>::New();
164   filter->SetInputVVImages(inputs);
165   filter->SetArgsInfo(mArgsInfo);
166   filter->EnableReadOnDisk(false);
167   filter->EnableOverwriteInputImage(false);
168   filter->Update();
169
170   // Output
171   vvImage::Pointer output = filter->GetOutputVVImage();
172   std::ostringstream osstream;
173   osstream << "Arithm_" << mArgsInfo.operation_arg << "_ "
174            << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
175   AddImage(output,osstream.str());
176   QApplication::restoreOverrideCursor();
177   close();
178 }
179 //------------------------------------------------------------------------------