]> Creatis software - clitk.git/blobdiff - vv/vvImageReader.cxx
Command line options --window --level. Both are required to be accounted for.
[clitk.git] / vv / vvImageReader.cxx
index 7cc0e569856b160276385b1f7c81d7e9ecba03ec..1f33fc72b2abe997ff119adf7f90b156edc7b0e7 100644 (file)
@@ -1,7 +1,7 @@
 /*=========================================================================
   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
 
-  Authors belong to: 
+  Authors belong to:
   - University of LYON              http://www.universite-lyon.fr/
   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
@@ -22,6 +22,7 @@
 #include <itkImageFileReader.h>
 #include "vvImageReader.h"
 #include "vvImageReader.txx"
+#include "clitkTransformUtilities.h"
 
 //------------------------------------------------------------------------------
 vvImageReader::vvImageReader()
@@ -30,6 +31,7 @@ vvImageReader::vvImageReader()
   mInputFilenames.resize(0);
   mLastError = "";
   mType = UNDEFINEDIMAGETYPE;
+  mSlice = 0;
 }
 //------------------------------------------------------------------------------
 
@@ -53,8 +55,7 @@ void vvImageReader::Update(LoadedImageType type)
   itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
   if (!reader) {
     mLastError="Unable to read file.";
-  }
-  else {
+  } else {
     reader->SetFileName(mInputFilenames[0]);
     reader->ReadImageInformation();
     if (mInputFilenames.size() > 1)
@@ -67,17 +68,17 @@ void vvImageReader::Update(LoadedImageType type)
 
 
 //------------------------------------------------------------------------------
-void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type) {
+void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
+{
   //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
   mType = type;
   mDim = dim;
   mInputPixelType=inputPixelType;
   this->start(); //Start heavy read operation in a separate thread
-  while (this->isRunning())
-    {
-      qApp->processEvents();
-      this->wait(50);
-    }
+  while (this->isRunning()) {
+    qApp->processEvents();
+    this->wait(50);
+  }
 }
 //------------------------------------------------------------------------------
 
@@ -112,10 +113,66 @@ void vvImageReader::SetInputFilename(const std::string & filename)
 
 
 //------------------------------------------------------------------------------
-void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames) {
+void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
+{
   mInputFilenames = filenames;
 }
 //------------------------------------------------------------------------------
 
+
+//------------------------------------------------------------------------------
+//Read transformation in NKI format (Xdr, transposed, cm)
+void vvImageReader::ReadNkiImageTransform()
+{
+  bool bRead=false;
+  std::string filename = mInputFilenames[0]+".MACHINEORIENTATION";
+  if(itksys::SystemTools::FileExists(filename.c_str())){
+    typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
+    MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
+    readerTransfo->SetFileName(filename);
+    try {
+      bRead = true;
+      readerTransfo->Update();
+    } catch( itk::ExceptionObject & err ) {
+      bRead=false;
+      std::cerr << "Cannot read " << filename << std::endl
+                << "The error is: " << err << std::endl;
+    }
+
+    if (bRead) {
+      //Transpose matrix (NKI format)
+      for(int j=0; j<4; j++)
+        for(int i=0; i<4; i++)
+          mImage->GetTransform()->GetMatrix()->SetElement(j,i,readerTransfo->GetOutput()->GetBufferPointer()[4*i+j]);
+
+      //From cm to mm
+      for(int i=0; i<3; i++)
+        mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3));
+
+      mImage->GetTransform()->Inverse();
+      mImage->UpdateReslice();
+    }
+  }
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+//Read transformation in ASCII format
+void vvImageReader::ReadMatImageTransform()
+{
+  std::string filename(mInputFilenames[0]+".mat");
+  std::ifstream f(filename.c_str());
+  if(f.is_open()) {
+    f.close();
+    
+    itk::Matrix<double, 4, 4> itkMat = clitk::ReadMatrix3D(filename);
+    for(int j=0; j<4; j++)
+      for(int i=0; i<4; i++)
+        mImage->GetTransform()->GetMatrix()->SetElement(j,i,itkMat[j][i]);
+    mImage->UpdateReslice();
+  }
+}
+//------------------------------------------------------------------------------
 #endif