]> Creatis software - clitk.git/blob - vv/vvCropDialog.cxx
af089dcb6f1dc8e2584a8800652b39cef0bfd01b
[clitk.git] / vv / vvCropDialog.cxx
1 /*=========================================================================
2
3  Program:   vv
4  Language:  C++
5  Author :   Joel Schaerer (joel.schaerer@insa-lyon.fr)
6
7 Copyright (C) 2008
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 =========================================================================*/
24
25 #include <QComboBox>
26 #include <QCursor>
27 #include <QApplication>
28 #include <vtkImageClip.h>
29 #include <vtkImageTranslateExtent.h>
30 #include <vtkImageData.h>
31 #include <vtkSmartPointer.h>
32 #include "vvCropDialog.h"
33 #include "vvSlicerManager.h"
34 #include "clitkCommon.h"
35
36 vvCropDialog::vvCropDialog(std::vector<vvSlicerManager*> sms,int current) :
37     mSlicerManagers(sms)
38 {
39     setupUi(this);
40     for (unsigned int i=0;i<mSlicerManagers.size();i++)
41         inputSequenceBox->addItem(vtksys::SystemTools::GetFilenameName(mSlicerManagers[i]->GetFileName()).c_str());
42     connect(inputSequenceBox,SIGNAL(currentIndexChanged(int)),this,SLOT(ImageChanged(int)));
43     inputSequenceBox->setCurrentIndex(current);
44     ImageChanged(current);
45     connect(this,SIGNAL(accepted()),this,SLOT(ComputeCroppedImage()));
46 }
47
48 void vvCropDialog::ImageChanged(int newindex)
49 {
50     std::vector<int> imsize=mSlicerManagers[newindex]->GetImage()->GetSize();
51     xminSlider->setMaximum(imsize[0]-1);
52     xmaxSlider->setMaximum(imsize[0]-1);
53     xmaxSlider->setValue(imsize[0]-1);
54     yminSlider->setMaximum(imsize[1]-1);
55     ymaxSlider->setMaximum(imsize[1]-1);
56     ymaxSlider->setValue(imsize[1]-1);
57     zminSlider->setMaximum(imsize[2]-1);
58     zmaxSlider->setMaximum(imsize[2]-1);
59     zmaxSlider->setValue(imsize[2]-1);
60     spin_xmin->setMaximum(imsize[0]-1);
61     spin_xmax->setMaximum(imsize[0]-1);
62     spin_xmax->setValue(imsize[0]-1);
63     spin_ymin->setMaximum(imsize[1]-1);
64     spin_ymax->setMaximum(imsize[1]-1);
65     spin_ymax->setValue(imsize[1]-1);
66     spin_zmin->setMaximum(imsize[2]-1);
67     spin_zmax->setMaximum(imsize[2]-1);
68     spin_zmax->setValue(imsize[2]-1);
69 }
70
71 void vvCropDialog::ComputeCroppedImage()
72 {
73     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
74     mResult=vvImage::New();
75     vvSlicerManager * current=mSlicerManagers[inputSequenceBox->currentIndex()];
76     vvImage::Pointer image=current->GetImage();
77     for (std::vector<vtkImageData*>::const_iterator i=image->GetVTKImages().begin();
78             i!=image->GetVTKImages().end();i++)
79     {
80         vtkSmartPointer<vtkImageClip> filter=vtkSmartPointer<vtkImageClip>::New();
81         ///Vtk is very weird, you need to "translate the extent" to get the correct origin
82         //http://markmail.org/message/vndc2tr6kcabiakp#query:vtkImageClip%20origin+page:1+mid:6na7y57floutklvz+state:results
83         vtkSmartPointer<vtkImageTranslateExtent> translate=vtkSmartPointer<vtkImageTranslateExtent>::New();
84         filter->SetInput(*i);
85         filter->SetOutputWholeExtent(xminSlider->value(),xmaxSlider->value(),
86                 yminSlider->value(),ymaxSlider->value(),
87                 zminSlider->value(),zmaxSlider->value());
88         translate->SetTranslation(-xminSlider->value(),-yminSlider->value(),-zminSlider->value());
89         translate->SetInput(filter->GetOutput());
90         filter->ClipDataOn(); //Really create a cropped copy of the image
91         translate->Update();
92         vtkImageData* output=vtkImageData::New();
93         output->ShallowCopy(translate->GetOutput());
94         mResult->AddImage(output);
95     }
96     QApplication::restoreOverrideCursor();
97 }