]> Creatis software - clitk.git/blob - vv/vvToolImageArithm.cxx
- correct "same image name" bug
[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   // Setup the UI
36   Ui_vvToolImageArithm::setupUi(mToolWidget);
37
38   // Main filter 
39   mFilter = new clitk::ImageArithmGenericFilter<args_info_clitkImageArithm>;
40
41   // Set how many inputs are needed for this tool
42   AddInputSelector("Select first image (A)", mFilter);
43   AddInputSelector("Select second image (B)", mFilter, true);
44 }
45 //------------------------------------------------------------------------------
46
47
48 //------------------------------------------------------------------------------
49 vvToolImageArithm::~vvToolImageArithm() {
50 }
51 //------------------------------------------------------------------------------
52
53
54 //------------------------------------------------------------------------------
55 void vvToolImageArithm::Initialize() {
56   SetToolName("ImageArithm");
57   SetToolMenuName("ImageArithm");
58   SetToolIconFilename(":/common/icons/arithm.png");
59   SetToolTip("Perform simple arithmetic operations on one or two images.");
60   SetToolExperimental(true);
61 }
62 //------------------------------------------------------------------------------
63
64
65 //------------------------------------------------------------------------------
66 void vvToolImageArithm::InputIsSelected(std::vector<vvSlicerManager *> & l) {
67   mInput1 = l[0];
68   mInput2 = l[1];  
69   mTwoInputs = true;
70   mGroupBoxOneInput->setEnabled(false);
71   mGroupBoxTwoInputs->setEnabled(true);
72 }
73 //------------------------------------------------------------------------------
74
75
76 //------------------------------------------------------------------------------
77 void vvToolImageArithm::InputIsSelected(vvSlicerManager * l) {
78   mInput1 = l;
79   mTwoInputs = false;
80   // DD("Single input");
81   mGroupBoxTwoInputs->setEnabled(false);
82   mGroupBoxOneInput->setEnabled(true);
83 }
84 //------------------------------------------------------------------------------
85
86
87 //------------------------------------------------------------------------------
88 void vvToolImageArithm::GetArgsInfoFromGUI() {
89   //  DD("GetArgsInfoFromGUI");
90   mArgsInfo.input1_given = false;
91   if (mTwoInputs) {
92     mArgsInfo.input2_given = true;
93     mArgsInfo.input2_arg = new char; // need to indicate that there are two inputs
94     mArgsInfo.scalar_given = false;
95     if (radioButtonSum->isChecked()) mArgsInfo.operation_arg = 0;
96     if (radioButtonMultiply->isChecked()) mArgsInfo.operation_arg = 1;
97     if (radioButtonDivide->isChecked()) mArgsInfo.operation_arg = 2;
98     if (radioButtonMax->isChecked()) mArgsInfo.operation_arg = 3;
99     if (radioButtonMin->isChecked()) mArgsInfo.operation_arg = 4;
100     if (radioButtonAbsDiff->isChecked()) mArgsInfo.operation_arg = 5;
101     if (radioButtonSquaredDiff->isChecked()) mArgsInfo.operation_arg = 6;
102   }
103   else {
104     mArgsInfo.input2_given = false;    
105     mArgsInfo.scalar_given = true;
106     if (radioButtonSumV->isChecked()) mArgsInfo.operation_arg = 0;
107     if (radioButtonMultiplyV->isChecked()) mArgsInfo.operation_arg = 1;
108     if (radioButtonInverseV->isChecked()) mArgsInfo.operation_arg = 2;
109     if (radioButtonMaxV->isChecked()) mArgsInfo.operation_arg = 3;
110     if (radioButtonMinV->isChecked()) mArgsInfo.operation_arg = 4;
111     if (radioButtonAbsDiffV->isChecked()) mArgsInfo.operation_arg = 5;
112     if (radioButtonSquaredDiffV->isChecked()) mArgsInfo.operation_arg = 6;
113     if (radioButtonLogV->isChecked()) mArgsInfo.operation_arg = 7;
114     if (radioButtonExpV->isChecked()) mArgsInfo.operation_arg = 8;
115     if (radioButtonSqrtV->isChecked()) mArgsInfo.operation_arg = 9;
116     mArgsInfo.scalar_given = true;
117     mArgsInfo.scalar_arg = mValueSpinBox->value();
118   }
119   mArgsInfo.output_given = false;
120   mArgsInfo.verbose_flag = false;  
121   mArgsInfo.setFloatOutput_flag = mCheckBoxUseFloatOutputType->isChecked();
122   mArgsInfo.imagetypes_flag = false;
123 }
124 //------------------------------------------------------------------------------
125
126
127 //------------------------------------------------------------------------------
128 void vvToolImageArithm::apply() {
129   if (!mCurrentSlicerManager) close();
130   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
131   GetArgsInfoFromGUI();
132
133   std::vector<vvImage::Pointer> inputs;
134   if (mTwoInputs) {
135     // Input
136     inputs.push_back(mInput1->GetImage());
137     inputs.push_back(mInput2->GetImage());
138     
139     // Check input type
140     if (inputs[0]->GetScalarTypeAsString() != inputs[1]->GetScalarTypeAsString()) {
141       std::cerr << "Sorry inputs should have the same pixeltype." << std::endl;
142       std::cerr << "Input1 = " << inputs[0]->GetScalarTypeAsString() << std::endl;
143       std::cerr << "Input2 = " << inputs[1]->GetScalarTypeAsString() << std::endl;
144       QApplication::restoreOverrideCursor();
145       QMessageBox::information(this, "Wrong image type","Sorry, could not perform operation. Please select inputs with same pixe type.");
146       close();
147       return;
148     }
149   }   
150   else {
151     // Input
152     inputs.push_back(mInput1->GetImage());
153     DD("Single input");
154   }
155    
156   // Main filter
157   clitk::ImageArithmGenericFilter<args_info_clitkImageArithm>::Pointer filter = 
158     clitk::ImageArithmGenericFilter<args_info_clitkImageArithm>::New();
159   filter->SetInputVVImages(inputs);
160   filter->SetArgsInfo(mArgsInfo);
161   filter->EnableReadOnDisk(false);
162   filter->EnableOverwriteInputImage(false);
163   filter->Update();
164     
165   // Output
166   vvImage::Pointer output = filter->GetOutputVVImage();
167   std::ostringstream osstream;
168   osstream << "Arithm_" << mArgsInfo.operation_arg << "_ " 
169            << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
170   AddImage(output,osstream.str()); 
171   QApplication::restoreOverrideCursor();
172   close();
173 }
174 //------------------------------------------------------------------------------