]> Creatis software - clitk.git/blob - vv/vvToolImageArithm.cxx
- new vv tool
[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:02:59 $
7   Version:   $Revision: 1.1 $
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 }
70 //------------------------------------------------------------------------------
71
72
73 //------------------------------------------------------------------------------
74 void vvToolImageArithm::InputIsSelected(std::vector<vvSlicerManager *> & l) {
75   mInput1 = l[0];
76   mInput2 = l[1];  
77   mTwoInputs = true;
78   mGroupBoxOneInput->setEnabled(false);
79   mGroupBoxTwoInputs->setEnabled(true);
80 }
81 //------------------------------------------------------------------------------
82
83
84 //------------------------------------------------------------------------------
85 void vvToolImageArithm::InputIsSelected(vvSlicerManager * l) {
86   mInput1 = l;
87   mTwoInputs = false;
88   // DD("Single input");
89   mGroupBoxTwoInputs->setEnabled(false);
90   mGroupBoxOneInput->setEnabled(true);
91 }
92 //------------------------------------------------------------------------------
93
94
95 //------------------------------------------------------------------------------
96 void vvToolImageArithm::GetArgsInfoFromGUI() {
97   //  DD("GetArgsInfoFromGUI");
98   mArgsInfo.input1_given = false;
99   if (mTwoInputs) {
100     mArgsInfo.input2_given = true;
101     mArgsInfo.input2_arg = new char; // need to indicate that there are two inputs
102     mArgsInfo.scalar_given = false;
103     if (radioButtonSum->isChecked()) mArgsInfo.operation_arg = 0;
104     if (radioButtonMultiply->isChecked()) mArgsInfo.operation_arg = 1;
105     if (radioButtonDivide->isChecked()) mArgsInfo.operation_arg = 2;
106     if (radioButtonMax->isChecked()) mArgsInfo.operation_arg = 3;
107     if (radioButtonMin->isChecked()) mArgsInfo.operation_arg = 4;
108     if (radioButtonAbsDiff->isChecked()) mArgsInfo.operation_arg = 5;
109     if (radioButtonSquaredDiff->isChecked()) mArgsInfo.operation_arg = 6;
110   }
111   else {
112     mArgsInfo.input2_given = false;    
113     mArgsInfo.scalar_given = true;
114     if (radioButtonSumV->isChecked()) mArgsInfo.operation_arg = 0;
115     if (radioButtonMultiplyV->isChecked()) mArgsInfo.operation_arg = 1;
116     if (radioButtonInverseV->isChecked()) mArgsInfo.operation_arg = 2;
117     if (radioButtonMaxV->isChecked()) mArgsInfo.operation_arg = 3;
118     if (radioButtonMinV->isChecked()) mArgsInfo.operation_arg = 4;
119     if (radioButtonAbsDiffV->isChecked()) mArgsInfo.operation_arg = 5;
120     if (radioButtonSquaredDiffV->isChecked()) mArgsInfo.operation_arg = 6;
121     if (radioButtonLogV->isChecked()) mArgsInfo.operation_arg = 7;
122     if (radioButtonExpV->isChecked()) mArgsInfo.operation_arg = 8;
123     if (radioButtonSqrtV->isChecked()) mArgsInfo.operation_arg = 9;
124     mArgsInfo.scalar_given = true;
125     mArgsInfo.scalar_arg = mValueSpinBox->value();
126   }
127   mArgsInfo.output_given = false;
128   mArgsInfo.verbose_flag = false;  
129   mArgsInfo.setFloatOutput_flag = mCheckBoxUseFloatOutputType->isChecked();
130   mArgsInfo.imagetypes_flag = false;
131 }
132 //------------------------------------------------------------------------------
133
134
135 //------------------------------------------------------------------------------
136 void vvToolImageArithm::apply() {
137   if (!mCurrentSlicerManager) close();
138   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
139   GetArgsInfoFromGUI();
140
141   std::vector<vvImage::Pointer> inputs;
142   if (mTwoInputs) {
143     // Input
144     inputs.push_back(mInput1->GetImage());
145     inputs.push_back(mInput2->GetImage());
146     
147     // Check input type
148     if (inputs[0]->GetScalarTypeAsString() != inputs[1]->GetScalarTypeAsString()) {
149       std::cerr << "Sorry inputs should have the same pixeltype." << std::endl;
150       std::cerr << "Input1 = " << inputs[0]->GetScalarTypeAsString() << std::endl;
151       std::cerr << "Input2 = " << inputs[1]->GetScalarTypeAsString() << std::endl;
152       QApplication::restoreOverrideCursor();
153       QMessageBox::information(this, "Wrong image type","Sorry, could not perform operation. Please select inputs with same pixe type.");
154       close();
155       return;
156     }
157   }   
158   else {
159     // Input
160     inputs.push_back(mInput1->GetImage());
161     DD("Single input");
162   }
163    
164   // Main filter
165   clitk::ImageArithmGenericFilter<args_info_clitkImageArithm>::Pointer filter = 
166     clitk::ImageArithmGenericFilter<args_info_clitkImageArithm>::New();
167   filter->SetInputVVImages(inputs);
168   filter->SetArgsInfo(mArgsInfo);
169   filter->EnableReadOnDisk(false);
170   filter->EnableOverwriteInputImage(false);
171   filter->Update();
172     
173   // Output
174   vvImage::Pointer output = filter->GetOutputVVImage();
175   std::ostringstream osstream;
176   osstream << "Arithm_" << mArgsInfo.operation_arg << "_ " 
177            << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
178   AddImage(output,osstream.str()); 
179   QApplication::restoreOverrideCursor();
180   close();
181 }
182 //------------------------------------------------------------------------------