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