]> Creatis software - clitk.git/blob - vv/vvCropDialog.cxx
Fusion windows level is now 4 decimals
[clitk.git] / vv / vvCropDialog.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 <QComboBox>
19 #include <QCursor>
20 #include <QApplication>
21 #include <vtkImageClip.h>
22 #include <vtkImageTranslateExtent.h>
23 #include <vtkImageData.h>
24 #include <vtkSmartPointer.h>
25 #include "vvCropDialog.h"
26 #include "vvSlicerManager.h"
27 #include "clitkCommon.h"
28
29 vvCropDialog::vvCropDialog(std::vector<vvSlicerManager*> sms,int current) :
30   mSlicerManagers(sms)
31 {
32   setupUi(this);
33   for (unsigned int i=0; i<mSlicerManagers.size(); i++)
34     inputSequenceBox->addItem(vtksys::SystemTools::GetFilenameName(mSlicerManagers[i]->GetFileName()).c_str());
35   connect(inputSequenceBox,SIGNAL(currentIndexChanged(int)),this,SLOT(ImageChanged(int)));
36   inputSequenceBox->setCurrentIndex(current);
37   ImageChanged(current);
38   connect(this,SIGNAL(accepted()),this,SLOT(ComputeCroppedImage()));
39 }
40
41 void vvCropDialog::ImageChanged(int newindex)
42 {
43   std::vector<int> imsize=mSlicerManagers[newindex]->GetImage()->GetSize();
44   xminSlider->setMaximum(imsize[0]-1);
45   xmaxSlider->setMaximum(imsize[0]-1);
46   xmaxSlider->setValue(imsize[0]-1);
47   yminSlider->setMaximum(imsize[1]-1);
48   ymaxSlider->setMaximum(imsize[1]-1);
49   ymaxSlider->setValue(imsize[1]-1);
50   zminSlider->setMaximum(imsize[2]-1);
51   zmaxSlider->setMaximum(imsize[2]-1);
52   zmaxSlider->setValue(imsize[2]-1);
53   spin_xmin->setMaximum(imsize[0]-1);
54   spin_xmax->setMaximum(imsize[0]-1);
55   spin_xmax->setValue(imsize[0]-1);
56   spin_ymin->setMaximum(imsize[1]-1);
57   spin_ymax->setMaximum(imsize[1]-1);
58   spin_ymax->setValue(imsize[1]-1);
59   spin_zmin->setMaximum(imsize[2]-1);
60   spin_zmax->setMaximum(imsize[2]-1);
61   spin_zmax->setValue(imsize[2]-1);
62 }
63
64 void vvCropDialog::ComputeCroppedImage()
65 {
66   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
67   mResult=vvImage::New();
68   vvSlicerManager * current=mSlicerManagers[inputSequenceBox->currentIndex()];
69   vvImage::Pointer image=current->GetImage();
70   for (std::vector<vtkImageData*>::const_iterator i=image->GetVTKImages().begin();
71        i!=image->GetVTKImages().end(); i++) {
72     vtkSmartPointer<vtkImageClip> filter=vtkSmartPointer<vtkImageClip>::New();
73     ///Vtk is very weird, you need to "translate the extent" to get the correct origin
74     //http://markmail.org/message/vndc2tr6kcabiakp#query:vtkImageClip%20origin+page:1+mid:6na7y57floutklvz+state:results
75     vtkSmartPointer<vtkImageTranslateExtent> translate=vtkSmartPointer<vtkImageTranslateExtent>::New();
76     filter->SetInput(*i);
77     filter->SetOutputWholeExtent(xminSlider->value(),xmaxSlider->value(),
78                                  yminSlider->value(),ymaxSlider->value(),
79                                  zminSlider->value(),zmaxSlider->value());
80     translate->SetTranslation(-xminSlider->value(),-yminSlider->value(),-zminSlider->value());
81     translate->SetInput(filter->GetOutput());
82     filter->ClipDataOn(); //Really create a cropped copy of the image
83     translate->Update();
84     vtkImageData* output=vtkImageData::New();
85     output->ShallowCopy(translate->GetOutput());
86     mResult->AddImage(output);
87   }
88   QApplication::restoreOverrideCursor();
89 }