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