1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #include "vvToolCropImage.h"
22 #include <QApplication>
23 #include <vtkImageClip.h>
24 #include <vtkImageTranslateExtent.h>
25 #include <vtkImageData.h>
26 #include <vtkSmartPointer.h>
29 //------------------------------------------------------------------------------
30 // Create the tool and automagically (I like this word) insert it in
31 // the main window menu.
32 ADD_TOOL(vvToolCropImage);
33 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 vvToolCropImage::vvToolCropImage(vvMainWindowBase * parent, Qt::WindowFlags f):
38 vvToolWidgetBase(parent, f),
39 vvToolBase<vvToolCropImage>(parent),
44 Ui_vvToolCropImage::setupUi(mToolWidget);
46 // Set how many inputs are needed for this tool
47 AddInputSelector("Select one image");
49 //------------------------------------------------------------------------------
52 //------------------------------------------------------------------------------
53 vvToolCropImage::~vvToolCropImage()
57 //------------------------------------------------------------------------------
60 //------------------------------------------------------------------------------
61 void vvToolCropImage::closeEvent(QCloseEvent *event)
63 for(int i=0; i<mExtentSize; i++) mReducedExtent[i] = mInitialExtent[i];
67 //------------------------------------------------------------------------------
70 //------------------------------------------------------------------------------
71 bool vvToolCropImage::close()
73 // for(int i=0; i<mExtentSize; i++) mReducedExtent[i] = mInitialExtent[i];
75 return vvToolWidgetBase::close();
77 //------------------------------------------------------------------------------
80 //------------------------------------------------------------------------------
81 void vvToolCropImage::reject()
83 for(int i=0; i<mExtentSize; i++) mReducedExtent[i] = mInitialExtent[i];
85 return vvToolWidgetBase::reject();
87 //------------------------------------------------------------------------------
90 //------------------------------------------------------------------------------
91 void vvToolCropImage::sliderXMinValueChanged(int s)
93 xmaxSlider->setMinimum(xminSlider->value());
94 mReducedExtent[0] = xminSlider->value();
97 //------------------------------------------------------------------------------
100 //------------------------------------------------------------------------------
101 void vvToolCropImage::sliderXMaxValueChanged(int s)
103 xminSlider->setMaximum(xmaxSlider->value());
104 mReducedExtent[1] = xmaxSlider->value();
107 //------------------------------------------------------------------------------
110 //------------------------------------------------------------------------------
111 void vvToolCropImage::sliderYMinValueChanged(int s)
113 ymaxSlider->setMinimum(yminSlider->value());
114 mReducedExtent[2] = yminSlider->value();
117 //------------------------------------------------------------------------------
120 //------------------------------------------------------------------------------
121 void vvToolCropImage::sliderYMaxValueChanged(int s)
123 yminSlider->setMaximum(ymaxSlider->value());
124 mReducedExtent[3] = ymaxSlider->value();
127 //------------------------------------------------------------------------------
130 //------------------------------------------------------------------------------
131 void vvToolCropImage::sliderZMinValueChanged(int s)
133 zmaxSlider->setMinimum(zminSlider->value());
134 mReducedExtent[4] = zminSlider->value();
137 //------------------------------------------------------------------------------
140 //------------------------------------------------------------------------------
141 void vvToolCropImage::sliderZMaxValueChanged(int s)
143 zminSlider->setMaximum(zmaxSlider->value());
144 mReducedExtent[5] = zmaxSlider->value();
147 //------------------------------------------------------------------------------
150 //------------------------------------------------------------------------------
151 // void vvToolCropImage::sliderTMinValueChanged(int s) {
152 // tmaxSlider->setMinimum(tminSlider->value());
153 // mReducedExtent[6] = tminSlider->value();
156 //------------------------------------------------------------------------------
159 //------------------------------------------------------------------------------
160 // void vvToolCropImage::sliderTMaxValueChanged(int s) {
161 // tminSlider->setMaximum(tmaxSlider->value());
162 // mReducedExtent[7] = tmaxSlider->value();
165 //------------------------------------------------------------------------------
168 //------------------------------------------------------------------------------
169 void vvToolCropImage::UpdateExtent()
171 for(int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
172 mCurrentSlicerManager->GetSlicer(i)->SetReducedExtent(mReducedExtent);
173 mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent();
174 mCurrentSlicerManager->GetSlicer(i)->Render();
177 //------------------------------------------------------------------------------
180 //------------------------------------------------------------------------------
181 void vvToolCropImage::InputIsSelected(vvSlicerManager * slicer)
184 // Change interface according to number of dimension
185 mExtentSize = 2*slicer->GetDimension();
186 // if (slicer->GetDimension()<4) {
187 // mTLabel1->setHidden(true);
188 // mTLabel2->setHidden(true);
189 // tminSlider->setHidden(true);
190 // tmaxSlider->setHidden(true);
191 // spin_tmin->setHidden(true);
192 // spin_tmax->setHidden(true);
194 if (slicer->GetDimension()<3) {
195 mZLabel1->setHidden(true);
196 mZLabel2->setHidden(true);
197 zminSlider->setHidden(true);
198 zmaxSlider->setHidden(true);
199 spin_zmin->setHidden(true);
200 spin_zmax->setHidden(true);
203 // Record initial extend
204 mReducedExtent = new int[mExtentSize];
205 mInitialExtent = new int[mExtentSize];
206 mReducedExtent = mCurrentSlicerManager->GetImage()->GetFirstVTKImageData()->GetWholeExtent();
207 for(int i=0; i<mExtentSize; i++) mInitialExtent[i] = mReducedExtent[i];
208 for(int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
210 //DD(mReducedExtent[i]);
211 //DD(mInitialExtent[i]);
212 mCurrentSlicerManager->GetSlicer(i)->EnableReducedExtent(true);
216 // mIntensitySlider->SetImage(mCurrentImage);
217 //mIntensitySlider->setEnabled(false);
219 // Set initial sliders values
220 std::vector<int> imsize = mCurrentSlicerManager->GetImage()->GetSize();
221 xminSlider->setMaximum(imsize[0]-1);
222 xmaxSlider->setMaximum(imsize[0]-1);
223 xmaxSlider->setValue(imsize[0]-1);
225 yminSlider->setMaximum(imsize[1]-1);
226 ymaxSlider->setMaximum(imsize[1]-1);
227 ymaxSlider->setValue(imsize[1]-1);
229 if (slicer->GetDimension() >2) {
230 zminSlider->setMaximum(imsize[2]-1);
231 zmaxSlider->setMaximum(imsize[2]-1);
232 zmaxSlider->setValue(imsize[2]-1);
235 // if (slicer->GetDimension() >3) {
236 // tminSlider->setMaximum(imsize[3]-1);
237 // tmaxSlider->setMaximum(imsize[3]-1);
238 // tmaxSlider->setValue(imsize[3]-1);
241 spin_xmin->setMaximum(imsize[0]-1);
242 spin_xmax->setMaximum(imsize[0]-1);
243 spin_xmax->setValue(imsize[0]-1);
245 spin_ymin->setMaximum(imsize[1]-1);
246 spin_ymax->setMaximum(imsize[1]-1);
247 spin_ymax->setValue(imsize[1]-1);
249 if (slicer->GetDimension() >2) {
250 spin_zmin->setMaximum(imsize[2]-1);
251 spin_zmax->setMaximum(imsize[2]-1);
252 spin_zmax->setValue(imsize[2]-1);
255 // if (slicer->GetDimension() >3) {
256 // spin_tmin->setMaximum(imsize[3]-1);
257 // spin_tmax->setMaximum(imsize[3]-1);
258 // spin_tmax->setValue(imsize[3]-1);
262 connect(xminSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderXMinValueChanged(int)));
263 connect(xmaxSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderXMaxValueChanged(int)));
264 connect(yminSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderYMinValueChanged(int)));
265 connect(ymaxSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderYMaxValueChanged(int)));
266 connect(zminSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderZMinValueChanged(int)));
267 connect(zmaxSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderZMaxValueChanged(int)));
268 // connect(tminSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderTMinValueChanged(int)));
269 // connect(tmaxSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderTMaxValueChanged(int)));
271 // connect(mCurrentSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateExtent()));
272 //connect(mCurrentSlicerManager,SIGNAL(UpdateTSlice(int,int)),this,SLOT(UpdateExtent()));
274 // connect(mIntensitySlider, SIGNAL(valueChanged(double)), this, SLOT(autoCropValueChanged(double)));
277 //------------------------------------------------------------------------------
280 //------------------------------------------------------------------------------
281 void vvToolCropImage::autoCropValueChanged(double v)
284 // vvImageToITKImageVector -> TODO a generic method
285 // then sliceriterator on each dimension from in to max
286 // ==> make a clitkGenericFilter even for the regular apply ...
288 //------------------------------------------------------------------------------
291 //------------------------------------------------------------------------------
292 void vvToolCropImage::apply()
295 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
296 vvImage::Pointer mResult=vvImage::New();
297 vvImage::Pointer image= mCurrentSlicerManager->GetImage();
298 for (std::vector<vtkImageData*>::const_iterator i=image->GetVTKImages().begin();
299 i!=image->GetVTKImages().end(); i++) {
300 vtkSmartPointer<vtkImageClip> filter=vtkSmartPointer<vtkImageClip>::New();
301 ///Vtk is very weird, you need to "translate the extent" to get the correct origin
302 //http://markmail.org/message/vndc2tr6kcabiakp#query:vtkImageClip%20origin+page:1+mid:6na7y57floutklvz+state:results
303 vtkSmartPointer<vtkImageTranslateExtent> translate=vtkSmartPointer<vtkImageTranslateExtent>::New();
304 filter->SetInput(*i);
305 filter->SetOutputWholeExtent(xminSlider->value(),xmaxSlider->value(),
306 yminSlider->value(),ymaxSlider->value(),
307 zminSlider->value(),zmaxSlider->value());
308 translate->SetTranslation(-xminSlider->value(),-yminSlider->value(),-zminSlider->value());
309 translate->SetInput(filter->GetOutput());
310 filter->ClipDataOn(); //Really create a cropped copy of the image
312 vtkImageData* output=vtkImageData::New();
313 output->ShallowCopy(translate->GetOutput());
314 mResult->AddImage(output);
316 QApplication::restoreOverrideCursor();
317 std::ostringstream osstream;
318 osstream << "Crop_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
319 AddImage(mResult, osstream.str());
322 //------------------------------------------------------------------------------