]> Creatis software - clitk.git/blobdiff - common/vvImageReader.txx
Add define to avoid vtk warning on mac
[clitk.git] / common / vvImageReader.txx
index aac9979fc330d265d9f14e7d7b346860cd6ffa5f..3ef04c6e0487f9b6bcec51e294e4f3075a6032e0 100644 (file)
@@ -23,7 +23,7 @@
 #include <itkImageFileReader.h>
 #include <itkImageSeriesReader.h>
 #include <itkImageToVTKImageFilter.h>
-#include <itkAnalyzeImageIO.h>
+#include <itkFlexibleVectorCastImageFilter.h>
 
 #include <vtkTransform.h>
 
 template<unsigned int VImageDimension>
 void vvImageReader::UpdateWithDim(std::string InputPixelType)
 {
-  if (mType == VECTORFIELD)
-    UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
+  if (mType == VECTORFIELD || mType == VECTORFIELDWITHTIME)
+  {
+    if (VImageDimension == 4)
+      UpdateWithDimAndInputVectorPixelType<itk::Vector<float,3>,VImageDimension>();
+    else
+      UpdateWithDimAndInputVectorPixelType<itk::Vector<float,VImageDimension>,VImageDimension>();
+  }
   else if (InputPixelType == "short")
     UpdateWithDimAndInputPixelType<short,VImageDimension>();
   else if (InputPixelType == "unsigned_short")
@@ -67,8 +72,6 @@ void vvImageReader::UpdateWithDim(std::string InputPixelType)
 template<class InputPixelType, unsigned int VImageDimension>
 void vvImageReader::UpdateWithDimAndInputPixelType()
 {
-  itk::AnalyzeImageIO *analyzeImageIO = NULL;
-
   if (mType == MERGEDWITHTIME)   // In this case we can load the images
     // one at the time to avoid excessive
     // memory use
@@ -91,7 +94,6 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
         mLastError = error.str();
         return;
       }
-      analyzeImageIO = dynamic_cast<itk::AnalyzeImageIO*>( reader->GetImageIO() );
     }
   } else if (mType == SLICED) {
     mImage=vvImage::New();
@@ -118,6 +120,9 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
     filter->SetExtractionRegion(extractedRegion);
     filter->SetInput(reader->GetOutput());
     filter->ReleaseDataFlagOn();
+#if ITK_VERSION_MAJOR == 4
+    filter->SetDirectionCollapseToSubmatrix();
+#endif
     try {
       mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
     } catch ( itk::ExceptionObject & err ) {
@@ -125,7 +130,6 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
                 << "(slice #" << mSlice << ") " << err << std::endl;
       return;
     }
-    analyzeImageIO = dynamic_cast<itk::AnalyzeImageIO*>( reader->GetImageIO() );
   } else {
     if (mInputFilenames.size() > 1) {
       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
@@ -136,7 +140,12 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
 
       try {
         if (mType == IMAGEWITHTIME)
-          mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
+        {
+          std::cerr << "We should never come here:" << std::endl
+            << "  Calling vvImageReader with multiple images and IMAGEWITHTIME is undefined." << std::endl
+            << "  You are probably looking for MERGEDWITHTIME Type." << std::endl;
+          return;
+        }
         else
           mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
       } catch ( itk::ExceptionObject & err ) {
@@ -154,10 +163,7 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
       reader->ReleaseDataFlagOn();
 
       try {
-        if (mType == IMAGEWITHTIME)
-          mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
-        else
-          mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
+        mImage = vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(), mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
       } catch ( itk::ExceptionObject & err ) {
         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
                   << " " << err << std::endl;
@@ -166,27 +172,62 @@ void vvImageReader::UpdateWithDimAndInputPixelType()
         mLastError = error.str();
         return;
       }
-      analyzeImageIO = dynamic_cast<itk::AnalyzeImageIO*>( reader->GetImageIO() );
     }
   }
+}
+//----------------------------------------------------------------------------
+
+//----------------------------------------------------------------------------
+template<class InputPixelType, unsigned int VImageDimension>
+void vvImageReader::UpdateWithDimAndInputVectorPixelType()
+{
+  typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
+  typename InputImageType::Pointer input;
 
-  // For unknown analyze orientations, we set identity
-  if(analyzeImageIO) {
-    const double m[16] = {1.,0.,0.,0.,
-                          0.,0.,1.,0.,
-                          0.,-1.,0.,0.,
-                          0.,0.,0.,1.};
-    int i;
-    for(i=0; i<16 && m[i]==mImage->GetTransform()->GetMatrix()->GetElement(i%4, i/4); i++);
-    if(i==16) {
-      itkWarningMacro(<< "Analyze image file format detected with unknown orientation. "
-                      << "Forcing identity orientation, use other file format if not ok.");
-      mImage->GetTransform()->Identity();
+  if (mInputFilenames.size() > 1) {
+    typedef itk::ImageSeriesReader<InputImageType> ReaderType;
+    typename ReaderType::Pointer reader = ReaderType::New();
+    reader->SetFileNames(mInputFilenames);
+    reader->ReleaseDataFlagOn();
+    try {
+      reader->Update();
+      input = reader->GetOutput();
+    } catch ( itk::ExceptionObject & err ) {
+      std::cerr << "Error while reading image series:" << err << std::endl;
+      std::stringstream error;
+      error << err;
+      mLastError = error.str();
+      return;
+    }
+  } else {
+    typedef itk::ImageFileReader<InputImageType> ReaderType;
+    typename ReaderType::Pointer reader = ReaderType::New();
+    reader->SetFileName(mInputFilenames[0]);
+    reader->ReleaseDataFlagOn();
+    try {
+      reader->Update();
+      input = reader->GetOutput();
+    } catch ( itk::ExceptionObject & err ) {
+      std::cerr << "Error while reading " << mInputFilenames[0].c_str()
+        << " " << err << std::endl;
+      std::stringstream error;
+      error << err;
+      mLastError = error.str();
+      return;
     }
   }
+  
+  typedef itk::Image< itk::Vector<float , 3>, VImageDimension > VectorImageType;
+  typedef itk::FlexibleVectorCastImageFilter<InputImageType, VectorImageType> CasterType;
+  typename VectorImageType::Pointer casted_input;
+  typename CasterType::Pointer caster = CasterType::New();
+  caster->SetInput(input);
+  caster->Update();
+  casted_input = caster->GetOutput();
+  
+  mImage = vvImageFromITK<VImageDimension, itk::Vector<float, 3> >(casted_input, mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
 }
 //----------------------------------------------------------------------------
 
-
 #endif /* end #define vvImageReader_TXX */