1 /*=========================================================================
5 Author : Joel Schaerer (joel.schaerer@insa-lyon.fr)
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
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.
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.
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/>.
23 =========================================================================*/
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"
36 vvCropDialog::vvCropDialog(std::vector<vvSlicerManager*> sms,int current) :
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()));
48 void vvCropDialog::ImageChanged(int newindex)
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);
71 void vvCropDialog::ComputeCroppedImage()
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++)
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();
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
92 vtkImageData* output=vtkImageData::New();
93 output->ShallowCopy(translate->GetOutput());
94 mResult->AddImage(output);
96 QApplication::restoreOverrideCursor();