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 ======================================================================-====*/
20 #include "vvToolResample.h"
22 #include "vvToolCreator.h"
26 #include <QMessageBox>
28 #define COLUMN_IMAGE_NAME 7
30 ADD_TOOL(vvToolResample);
32 //------------------------------------------------------------------------------
33 vvToolResample::vvToolResample(vvMainWindowBase * parent, Qt::WindowFlags f):
34 vvToolWidgetBase(parent,f),
35 vvToolBase<vvToolResample>(parent),
38 Ui_vvToolResample::setupUi(mToolWidget);
40 // mFilter = clitk::ResampleImageGenericFilter::New();
41 mFilter = clitk::ImageResampleGenericFilter::New();
44 mInputFileFormat = "";
47 mCurrentSlicerManager=NULL;
50 mInputSpacing.resize(0);
51 mInputOrigin.resize(0);
52 mOutputSize.resize(0);
53 mOutputSpacing.resize(0);
55 xSizeLineEdit->setText("");
56 ySizeLineEdit->setText("");
57 zSizeLineEdit->setText("");
58 xSpacingLineEdit->setText("");
59 ySpacingLineEdit->setText("");
60 zSpacingLineEdit->setText("");
61 scaleSizeLineEdit->setText("");
62 scaleSpacingLineEdit->setText("");
64 sizeRadioButton->setChecked(0);
65 scaleSizeRadioButton->setChecked(0);
66 isoSizeRadioButton->setChecked(0);
67 spacingRadioButton->setChecked(0);
68 scaleSpacingRadioButton->setChecked(0);
69 isoSpacingRadioButton->setChecked(0);
71 gaussianFilterCheckBox->setCheckState(Qt::Unchecked);
73 xGaussianLineEdit->hide();
74 yGaussianLineEdit->hide();
75 zGaussianLineEdit->hide();
76 gaussianFilterLabel->hide();
79 bSplineOrderSpinBox->hide();
80 bLUTFactorLabel->hide();
85 qPalette.setColor(QPalette::Foreground, QColor(Qt::blue));
86 mInputFormatLabel->setPalette(qPalette);
87 mInputDimLabel->setPalette(qPalette);
88 mInputPixelTypeLabel->setPalette(qPalette);
89 mInputSizeLabel->setPalette(qPalette);
90 mInputSpacingLabel->setPalette(qPalette);
92 // Set how many inputs are needed for this tool
93 AddInputSelector("Select an image to resample", mFilter);
95 // Connect signals & slots
97 connect(sizeRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
98 connect(scaleSizeRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
99 connect(isoSizeRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
100 connect(spacingRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
101 connect(scaleSpacingRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
102 connect(isoSpacingRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
104 connect(xSizeLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSpacingFromSize()));
105 connect(ySizeLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSpacingFromSize()));
106 connect(zSizeLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSpacingFromSize()));
107 connect(xSpacingLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSizeFromSpacing()));
108 connect(ySpacingLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSizeFromSpacing()));
109 connect(zSpacingLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSizeFromSpacing()));
110 connect(scaleSizeLineEdit,SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSizeFromScale()));
111 connect(scaleSpacingLineEdit,SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSpacingFromScale()));
112 connect(isoSizeLineEdit,SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSizeFromIso()));
113 connect(isoSpacingLineEdit,SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSpacingFromIso()));
115 connect(gaussianFilterCheckBox,SIGNAL(stateChanged(int)),this,SLOT(UpdateGaussianFilter()));
116 connect(interpolationComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(UpdateInterpolation()));
118 //------------------------------------------------------------------------------
121 //------------------------------------------------------------------------------
122 void vvToolResample::Initialize()
124 SetToolName("Resample Image");
125 SetToolMenuName("Resample");
126 SetToolIconFilename(":/common/icons/resample.png");
127 SetToolTip("Resample image with various interpolation methods.");
129 //------------------------------------------------------------------------------
132 //------------------------------------------------------------------------------
133 void vvToolResample::InputIsSelected(vvSlicerManager* m)
136 mCurrentSlicerManager = m;
137 mCurrentImage = mCurrentSlicerManager->GetSlicer(0)->GetImage();
138 if (mCurrentImage.IsNull()) return;
139 mInputFileName = mCurrentSlicerManager->GetFileName().c_str();
141 // Set current information
142 mPixelType = mCurrentImage->GetScalarTypeAsITKString().c_str();
143 //ds ComponentType = mCurrentImageGetNumberOfScalarComponents();
144 mDimension = mCurrentImage->GetNumberOfDimensions();
146 // Copy size, spacing ...
147 mInputOrigin.resize(mDimension);
148 mInputSpacing.resize(mDimension);
149 mInputSize.resize(mDimension);
150 for (int i = 0; i < mDimension; i++) {
151 mInputOrigin[i] = mCurrentImage->GetOrigin()[i];
152 mInputSpacing[i] = mCurrentImage->GetSpacing()[i];
153 mInputSize[i] = mCurrentImage->GetSize()[i];
157 mInputFileFormat = itksys::SystemTools::GetFilenameLastExtension(mInputFileName.toStdString()).c_str();
160 mInputFormatLabel->setText(mInputFileFormat);
161 mInputSizeLabel->setText(GetVectorIntAsString(mInputSize));
162 mInputDimLabel->setText(QString::number(mDimension)+"D");
163 mInputSpacingLabel->setText(GetVectorDoubleAsString(mInputSpacing));
164 mInputPixelTypeLabel->setText(mPixelType);
165 mInputMemoryLabel->setText(GetSizeInBytes(mInputSize));
168 scaleSizeRadioButton->setChecked(true);
169 UpdateControlSizeAndSpacing();
170 scaleSizeLineEdit->setText("100");
171 ComputeNewSizeFromScale();
176 //------------------------------------------------------------------------------
179 //------------------------------------------------------------------------------
180 void vvToolResample::UpdateOutputInfo()
182 mOutputSizeLabel->setText(GetVectorIntAsString(mOutputSize));
183 mOutputSpacingLabel->setText(GetVectorDoubleAsString(mOutputSpacing));
184 mOutputMemoryLabel->setText(GetSizeInBytes(mOutputSize));
186 //------------------------------------------------------------------------------
189 //------------------------------------------------------------------------------
190 QString vvToolResample::GetSizeInBytes(std::vector<int> & size)
193 for (unsigned int i=0; i<size.size(); i++) t *= size[i];
194 t *= mCurrentImage->GetScalarSize()*mCurrentImage->GetNumberOfScalarComponents();
195 QString result = QString::number(t);
196 result += " bytes (";
197 if (t > 1000000000) {
199 result += QString::number(t);
201 } else if (t > 1000000) {
203 result += QString::number(t);
205 } else if (t > 1000) {
207 result += QString::number(t);
209 } else result += ")";
212 //------------------------------------------------------------------------------
215 //------------------------------------------------------------------------------
216 QString vvToolResample::GetVectorDoubleAsString(std::vector<double> vectorDouble)
219 for (unsigned int i= 0; i<vectorDouble.size(); i++) {
222 result += QString::number(vectorDouble[i]);
226 //------------------------------------------------------------------------------
229 //------------------------------------------------------------------------------
230 QString vvToolResample::GetVectorIntAsString(std::vector<int> vectorInt)
233 for (unsigned int i= 0; i<vectorInt.size(); i++) {
236 result += QString::number(vectorInt[i]);
240 //------------------------------------------------------------------------------
243 //------------------------------------------------------------------------------
244 void vvToolResample::FillSizeEdit(std::vector<int> size)
246 xSizeLineEdit->setText(QString::number(size[0]));
247 ySizeLineEdit->setText(QString::number(size[1]));
249 zSizeLineEdit->setText(QString::number(size[2]));
251 //------------------------------------------------------------------------------
254 //------------------------------------------------------------------------------
255 void vvToolResample::FillSpacingEdit(std::vector<double> spacing)
257 xSpacingLineEdit->setText(QString::number(spacing[0]));
258 ySpacingLineEdit->setText(QString::number(spacing[1]));
259 if (spacing.size() > 2)
260 zSpacingLineEdit->setText(QString::number(spacing[2]));
262 //------------------------------------------------------------------------------
265 //------------------------------------------------------------------------------
266 void vvToolResample::UpdateOutputSizeAndSpacing()
268 mOutputSize.resize(mDimension);
269 mOutputSize = mInputSize;
270 mOutputSpacing.resize(mDimension);
271 mOutputSpacing = mInputSpacing;
272 mOutputSize[0] = (int)xSizeLineEdit->text().toDouble();
273 mOutputSize[1] = (int)ySizeLineEdit->text().toDouble();
275 mOutputSize[2] = (int)zSizeLineEdit->text().toDouble();
277 mOutputSpacing[0] = xSpacingLineEdit->text().toDouble();
278 mOutputSpacing[1] = ySpacingLineEdit->text().toDouble();
280 mOutputSpacing[2] = zSpacingLineEdit->text().toDouble();
284 //------------------------------------------------------------------------------
287 //------------------------------------------------------------------------------
288 void vvToolResample::UpdateControlSizeAndSpacing()
290 scaleSizeLineEdit->setText("");
291 scaleSpacingLineEdit->setText("");
292 isoSizeLineEdit->setText("");
293 isoSpacingLineEdit->setText("");
295 xSizeLineEdit->setReadOnly(1);
296 ySizeLineEdit->setReadOnly(1);
297 zSizeLineEdit->setReadOnly(1);
298 scaleSizeLineEdit->setReadOnly(1);
299 isoSizeLineEdit->setReadOnly(1);
301 xSpacingLineEdit->setReadOnly(1);
302 ySpacingLineEdit->setReadOnly(1);
303 zSpacingLineEdit->setReadOnly(1);
304 scaleSpacingLineEdit->setReadOnly(1);
305 isoSpacingLineEdit->setReadOnly(1);
307 if (sizeRadioButton->isChecked()) {
308 xSizeLineEdit->setReadOnly(0);
309 ySizeLineEdit->setReadOnly(0);
311 zSizeLineEdit->setReadOnly(0);
313 if (spacingRadioButton->isChecked()) {
314 xSpacingLineEdit->setReadOnly(0);
315 ySpacingLineEdit->setReadOnly(0);
317 zSpacingLineEdit->setReadOnly(0);
318 } else if (scaleSizeRadioButton->isChecked())
319 scaleSizeLineEdit->setReadOnly(0);
320 else if (scaleSpacingRadioButton->isChecked())
321 scaleSpacingLineEdit->setReadOnly(0);
322 else if (isoSizeRadioButton->isChecked())
323 isoSizeLineEdit->setReadOnly(0);
324 else if (isoSpacingRadioButton->isChecked())
325 isoSpacingLineEdit->setReadOnly(0);
328 //------------------------------------------------------------------------------
331 //------------------------------------------------------------------------------
332 void vvToolResample::ComputeNewSpacingFromSize()
334 double newSpacing = mInputSpacing[0]*mInputSize[0];
335 xSpacingLineEdit->setText(QString::number(newSpacing/xSizeLineEdit->text().toDouble()));
336 newSpacing = mInputSpacing[1]*mInputSize[1];
337 ySpacingLineEdit->setText(QString::number(newSpacing/ySizeLineEdit->text().toDouble()));
338 if (mDimension > 2) {
339 newSpacing = mInputSpacing[2]*mInputSize[2];
340 zSpacingLineEdit->setText(QString::number(newSpacing/zSizeLineEdit->text().toDouble()));
342 UpdateOutputSizeAndSpacing();
344 //------------------------------------------------------------------------------
347 //------------------------------------------------------------------------------
348 void vvToolResample::ComputeNewSizeFromSpacing()
350 double newSize = mInputSpacing[0]*mInputSize[0];
351 xSizeLineEdit->setText(QString::number(newSize/xSpacingLineEdit->text().toDouble()));
352 newSize = mInputSpacing[1]*mInputSize[1];
353 ySizeLineEdit->setText(QString::number(newSize/ySpacingLineEdit->text().toDouble()));
354 if (mDimension > 2) {
355 newSize = mInputSpacing[2]*mInputSize[2];
356 zSizeLineEdit->setText(QString::number(newSize/zSpacingLineEdit->text().toDouble()));
358 UpdateOutputSizeAndSpacing();
360 //------------------------------------------------------------------------------
363 //------------------------------------------------------------------------------
364 void vvToolResample::ComputeNewSpacingFromScale()
366 xSpacingLineEdit->setText(QString::number(mInputSpacing[0]*scaleSpacingLineEdit->text().toDouble()/100));
367 ySpacingLineEdit->setText(QString::number(mInputSpacing[1]*scaleSpacingLineEdit->text().toDouble()/100));
369 zSpacingLineEdit->setText(QString::number(mInputSpacing[2]*scaleSpacingLineEdit->text().toDouble()/100));
370 ComputeNewSizeFromSpacing();
372 //------------------------------------------------------------------------------
375 //------------------------------------------------------------------------------
376 void vvToolResample::ComputeNewSizeFromScale()
378 xSizeLineEdit->setText(QString::number(mInputSize[0]*scaleSizeLineEdit->text().toDouble()/100));
379 ySizeLineEdit->setText(QString::number(mInputSize[1]*scaleSizeLineEdit->text().toDouble()/100));
381 zSizeLineEdit->setText(QString::number(mInputSize[2]*scaleSizeLineEdit->text().toDouble()/100));
382 ComputeNewSpacingFromSize();
384 //------------------------------------------------------------------------------
387 //------------------------------------------------------------------------------
388 void vvToolResample::ComputeNewSpacingFromIso()
390 xSpacingLineEdit->setText(QString::number(isoSpacingLineEdit->text().toDouble()));
391 ySpacingLineEdit->setText(QString::number(isoSpacingLineEdit->text().toDouble()));
393 zSpacingLineEdit->setText(QString::number(isoSpacingLineEdit->text().toDouble()));
394 ComputeNewSizeFromSpacing();
396 //------------------------------------------------------------------------------
399 //------------------------------------------------------------------------------
400 void vvToolResample::ComputeNewSizeFromIso()
402 xSizeLineEdit->setText(QString::number(isoSizeLineEdit->text().toDouble()));
403 ySizeLineEdit->setText(QString::number(isoSizeLineEdit->text().toDouble()));
405 zSizeLineEdit->setText(QString::number(isoSizeLineEdit->text().toDouble()));
406 ComputeNewSpacingFromSize();
408 //------------------------------------------------------------------------------
411 //------------------------------------------------------------------------------
412 void vvToolResample::UpdateInterpolation()
414 if (interpolationComboBox->currentText() == "BSpline") {
415 bSplineLabel->show();
416 bSplineOrderSpinBox->show();
417 bLUTFactorLabel->hide();
419 } else if (interpolationComboBox->currentText() == "Blut (faster BSpline)") {
420 bSplineLabel->show();
421 bSplineOrderSpinBox->show();
422 bLUTFactorLabel->show();
425 bSplineLabel->hide();
426 bSplineOrderSpinBox->hide();
427 bLUTFactorLabel->hide();
431 //------------------------------------------------------------------------------
434 //------------------------------------------------------------------------------
435 void vvToolResample::UpdateGaussianFilter()
437 if (gaussianFilterCheckBox->isChecked()) {
438 gaussianFilterLabel->show();
439 xGaussianLineEdit->show();
440 yGaussianLineEdit->show();
442 zGaussianLineEdit->show();
444 gaussianFilterLabel->hide();
445 xGaussianLineEdit->hide();
446 yGaussianLineEdit->hide();
447 zGaussianLineEdit->hide();
450 //------------------------------------------------------------------------------
453 //------------------------------------------------------------------------------
454 void vvToolResample::apply()
457 // Get resampler options
458 std::vector<double> sigma;
459 sigma.push_back(xGaussianLineEdit->text().toDouble());
460 sigma.push_back(yGaussianLineEdit->text().toDouble());
461 if (mDimension > 2) sigma.push_back(zGaussianLineEdit->text().toDouble());
462 if (mDimension == 4) sigma.push_back(0.01); //FIXME Don't filter along the temporal direction
466 typename clitk::ResampleImageGenericFilter::ArgsInfoType mArgsInfo;
467 mArgsInfo.size_given = mDimension;
468 mArgsInfo.size_arg = mDimension;
469 for(int i=0; i<mDimension; i++) {
470 mArgsInfo.size = mOutputSize;
473 mFilter->SetOutputSize(mOutputSize);
474 mFilter->SetOutputSpacing(mOutputSpacing);
475 mFilter->SetInterpolationName(interpolationComboBox->currentText().toLower().toStdString());
477 if (interpolationComboBox->currentText() == "BSpline")
478 mFilter->SetBSplineOrder(bSplineOrderSpinBox->value());
479 else if (interpolationComboBox->currentText() == "Blut (faster BSpline)") {
480 mFilter->SetInterpolationName("blut");
481 mFilter->SetBSplineOrder(bSplineOrderSpinBox->value());
482 mFilter->SetBLUTSampling(bLUTSpinBox->value());
484 if (gaussianFilterCheckBox->isChecked())
485 mFilter->SetGaussianSigma(sigma);
486 // mFilter->SetOutputFileName(OutputFileName.toStdString());
487 mFilter->SetDefaultPixelValue(defaultPixelValueLineEdit->text().toDouble());
488 mFilter->SetInputVVImage(mCurrentImage);
492 mOutput = mFilter->GetOutputVVImage();
493 AddImage(mOutput,GetOutputFileName());
496 //------------------------------------------------------------------------------
499 //------------------------------------------------------------------------------
500 std::string vvToolResample::GetOutputFileName()
502 QFileInfo info(QString(mCurrentSlicerManager->GetFileName().c_str()));
503 return (info.path().toStdString() + "/resampled_" + info.fileName().toStdString());
505 //------------------------------------------------------------------------------