]> Creatis software - clitk.git/commitdiff
Prompt transform backup when saving image in vv.
authorSimon Rit <simon.rit@creatis.insa-lyon.fr>
Fri, 23 Mar 2012 09:33:03 +0000 (10:33 +0100)
committerSimon Rit <simon.rit@creatis.insa-lyon.fr>
Fri, 23 Mar 2012 09:33:41 +0000 (10:33 +0100)
common/vvImageWriter.cxx
common/vvImageWriter.h
common/vvImageWriter.txx
vv/vvMainWindow.cxx

index 18123f6959906ff75ad9aa93afe7a228e294be3c..c2d2a9162a429db3d5ffb7a0077164861167a9db 100644 (file)
@@ -30,6 +30,7 @@ vvImageWriter::vvImageWriter()
   mOutputFilename = "";
   mLastError = "";
   mUseAnObserver = false;
+  mSaveTransform = false;
 }
 //------------------------------------------------------------------------------
 
index 62815490fba4306bfd1d15bbad9212d8c1a474d7..ae192b5e797aed7d9f4fa7254de452c895bd90cc 100644 (file)
@@ -48,6 +48,9 @@ public:
     return mLastError;
   }
 
+  bool GetSaveTransform() { return mSaveTransform; }
+  void SetSaveTransform(bool b) { mSaveTransform = b; }
+
   //====================================================================
   // Main function
   void Update() {
@@ -66,6 +69,7 @@ protected:
 
   std::string mLastError;
   bool mUseAnObserver;
+  bool mSaveTransform;
 
   //====================================================================
   template<unsigned int VImageDimension>
index 35192f065de1426205bfc9b17d92af3e7b4ba29e..0640f0cd3645bd3d036facd374f6949225718d5d 100644 (file)
@@ -19,8 +19,8 @@
 #define vvImageWriter_TXX
 
 #include <itkImageFileWriter.h>
+#include <itkChangeInformationImageFilter.h>
 #include "vvToITK.h"
-#include "clitkDD.h"
 
 //====================================================================
 template<unsigned int VImageDimension>
@@ -52,12 +52,50 @@ void vvImageWriter::UpdateWithDim(std::string OutputPixelType)
 template<class OutputPixelType, unsigned int VImageDimension>
 void vvImageWriter::UpdateWithDimAndOutputPixelType()
 {
-  //Create the writer
+  // The ITK image
   typedef itk::Image< OutputPixelType, VImageDimension > OutputImageType;
+  typename OutputImageType::ConstPointer itkimg = vvImageToITK<OutputImageType>(mImage);
+
+  //Create the writer
   typedef itk::ImageFileWriter<OutputImageType> WriterType;
   typename WriterType::Pointer writer = WriterType::New();
   writer->SetFileName(mOutputFilename);
-  writer->SetInput(vvImageToITK<OutputImageType>(mImage));
+
+  //Change information if it must transformation must be saved
+  typedef itk::ChangeInformationImageFilter<OutputImageType> ChangeInfoType;
+  typename ChangeInfoType::Pointer info = ChangeInfoType::New();
+  if(mSaveTransform) {
+    // Set pipeline
+    info->SetInput(itkimg);
+    writer->SetInput(info->GetOutput());
+
+    // Inverse vv matrix
+    itk::Matrix<double, 4, 4> trans;
+    for(int i=0; i<4; i++)
+      for(int j=0; j<4; j++)
+        trans[i][j] = mImage->GetTransform()->GetMatrix()->GetElement(i,j);
+    trans = trans.GetInverse();
+
+    // Direction
+    typename ChangeInfoType::DirectionType direction;
+    for(unsigned int i=0; i<VImageDimension; i++)
+      for(unsigned int j=0; j<VImageDimension; j++)
+        direction[i][j] = trans[i][j];
+    info->SetOutputDirection(direction);
+    info->ChangeDirectionOn();
+
+    // Origin
+    typename ChangeInfoType::PointType origin = itkimg->GetOrigin();
+    origin = direction * origin;
+    for(unsigned int i=0; i<VImageDimension; i++)
+      origin[i] += trans[i][3];
+    info->SetOutputOrigin(origin);
+    info->ChangeOriginOn();
+  }
+  else
+    writer->SetInput(itkimg);
+
+
   if (mUseAnObserver) {
     writer->AddObserver(itk::ProgressEvent(), mObserver);
   }
index 9dbd407e4168aee728bc700f638ca0695bc0bd42..4c55a5c536313b0e096f553360798f12ab84da1a 100644 (file)
@@ -2297,6 +2297,27 @@ void vvMainWindow::SaveAs()
       vvImageWriter::Pointer writer = vvImageWriter::New();
       writer->SetOutputFileName(fileName.toStdString());
       writer->SetInput(mSlicerManagers[index]->GetImage());
+
+      // Check on transform and prompt user
+      writer->SetSaveTransform(false);
+      bool bId = true;
+      for(int i=0; i<4; i++)
+        for(int j=0; j<4; j++) {
+          double elt = mSlicerManagers[index]->GetImage()->GetTransform()->GetMatrix()->GetElement(i,j);
+          if(i==j && elt!=1.)
+            bId = false;
+          if(i!=j && elt!=0.)
+            bId = false;
+        }
+      if( !bId ) {
+        QString warning = "The image has an associated linear transform. Do you want to save it along?";
+        QMessageBox msgBox(QMessageBox::Warning, tr("Save transform"), warning, 0, this);
+        msgBox.addButton(tr("Yes"), QMessageBox::AcceptRole);
+        msgBox.addButton(tr("No"), QMessageBox::RejectRole);
+        if (msgBox.exec() == QMessageBox::AcceptRole)
+          writer->SetSaveTransform(true);
+      }
+
       writer->Update();
       QApplication::restoreOverrideCursor();
       if (writer->GetLastError().size()) {