]> Creatis software - clitk.git/blobdiff - tools/clitkSplitImageGenericFilter.cxx
cosmetic for .ggo
[clitk.git] / tools / clitkSplitImageGenericFilter.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 4fd6eac..58732ab
@@ -3,7 +3,7 @@
 
   Authors belong to:
   - University of LYON              http://www.universite-lyon.fr/
-  - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
 
   This software is distributed WITHOUT ANY WARRANTY; without even
@@ -14,7 +14,7 @@
 
   - BSD        See included LICENSE.txt file
   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
-======================================================================-====*/
+===========================================================================**/
 /**
  -------------------------------------------------------------------
  * @file   clitkSplitImageGenericFilter.cxx
@@ -25,7 +25,7 @@
  -------------------------------------------------------------------*/
 
 #include "clitkSplitImageGenericFilter.h"
-#include "clitkSplitImageGenericFilter.txx"
+#include "itkChangeInformationImageFilter.h"
 #include <itkIntensityWindowingImageFilter.h>
 //--------------------------------------------------------------------
 clitk::SplitImageGenericFilter::SplitImageGenericFilter():
@@ -37,13 +37,55 @@ clitk::SplitImageGenericFilter::SplitImageGenericFilter():
 }
 //--------------------------------------------------------------------
 
-
 //--------------------------------------------------------------------
 template<unsigned int Dim>
 void clitk::SplitImageGenericFilter::InitializeImageType()
 {
   ADD_DEFAULT_IMAGE_TYPES(Dim);
-  //ADD_VEC_IMAGE_TYPE(Dim, 3,float);
+  ADD_VEC_IMAGE_TYPE(Dim, 3,float);
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+template <class ImageType>
+typename clitk::SplitImageGenericFilter::PngConversion<ImageType>::OutputPngImagePointer
+clitk::SplitImageGenericFilter::PngConversion<ImageType>::Do(double window,
+                                                             double level,
+                                                             ImagePointer input)
+{
+  static const unsigned int PixelDimension = itk::PixelTraits<typename ImageType::PixelType>::Dimension;
+  return this->Do(window, level, input, static_cast< PixelDimType<PixelDimension> *>(ITK_NULLPTR) );
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+template <class ImageType>
+template<unsigned int Dim>
+typename clitk::SplitImageGenericFilter::PngConversion<ImageType>::OutputPngImagePointer
+clitk::SplitImageGenericFilter::PngConversion<ImageType>::Do(double window,
+                                                             double level,
+                                                             ImagePointer input,
+                                                             PixelDimType<Dim> *)
+{
+  clitkExceptionMacro("Png conversion is not implemented for vector fields");
+  return ITK_NULLPTR;
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+template <class ImageType>
+typename clitk::SplitImageGenericFilter::PngConversion<ImageType>::OutputPngImagePointer
+clitk::SplitImageGenericFilter::PngConversion<ImageType>::Do(double window,
+                                                             double level,
+                                                             ImagePointer input,
+                                                             PixelDimType<1> *)
+{
+  typedef itk::IntensityWindowingImageFilter< ImageType, OutputPngImageType > CastFilterType;
+  typename CastFilterType::Pointer cast = CastFilterType::New();
+  cast->SetWindowLevel(window, level);
+  cast->SetInput(input);
+  cast->Update();
+  return cast->GetOutput();
 }
 //--------------------------------------------------------------------
 
@@ -64,9 +106,12 @@ void clitk::SplitImageGenericFilter::UpdateWithInputImageType()
   size[mSplitDimension]=0;
   typename ImageType::RegionType extracted_region;
   extracted_region.SetSize(size);
+  filter->SetDirectionCollapseToIdentity();
   filter->SetExtractionRegion(extracted_region);
   filter->Update();
 
+  typedef itk::ChangeInformationImageFilter< OutputImageType > InformationFilterType;
+
   typename ImageType::IndexType index=input->GetLargestPossibleRegion().GetIndex();
   std::string base_filename=GetOutputFilename();
   unsigned int number_of_output_images=input->GetLargestPossibleRegion().GetSize()[mSplitDimension];
@@ -77,18 +122,44 @@ void clitk::SplitImageGenericFilter::UpdateWithInputImageType()
     extracted_region.SetIndex(index);
     filter->SetExtractionRegion(extracted_region);
     filter->Update();
-    if(this->m_Png){
-      typedef itk::Image< unsigned char, ImageType::ImageDimension-1 > OutputPngImageType;
-      typedef itk::IntensityWindowingImageFilter< OutputImageType, OutputPngImageType > CastFilterType;
-      typename CastFilterType::Pointer cast = CastFilterType::New();
-      cast->SetWindowLevel(this->m_Window, this->m_Level);
-      cast->SetInput(filter->GetOutput());
-      SetOutputFilename(base_filename+"_"+ss.str()+".png");
-      SetNextOutput<OutputPngImageType>(cast->GetOutput());
+
+    if (ImageType::ImageDimension == 4 && mSplitDimension == 3) //Copy the transformation matrix if the original image is a 4D image splitted along the time dimension
+    {
+      typename InformationFilterType::Pointer informationFilter = InformationFilterType::New();
+      informationFilter->SetInput( filter->GetOutput() );
+      typename OutputImageType::DirectionType directionOutput;
+      for (unsigned j=0; j<3; ++j)
+        for (unsigned k=0; k<3; ++k)
+          directionOutput[j][k] = input->GetDirection()[j][k];
+      informationFilter->SetOutputDirection( directionOutput );
+      informationFilter->ChangeDirectionOn();
+      informationFilter->UpdateOutputInformation();
+
+      if(this->m_Png){
+        PngConversion<OutputImageType> png;
+        SetOutputFilename(base_filename+"_"+ss.str()+".png");
+        typename PngConversion<OutputImageType>::OutputPngImagePointer output;
+        output = png.Do(this->m_Window, this->m_Level, informationFilter->GetOutput());
+        this->template SetNextOutput<typename PngConversion<OutputImageType>::OutputPngImageType>(output);
+      }
+      else {
+        SetOutputFilename(base_filename+"_"+ss.str()+".mhd");
+        SetNextOutput<OutputImageType>(informationFilter->GetOutput());
+      }
     }
-    else {
-      SetOutputFilename(base_filename+"_"+ss.str()+".mhd");
-      SetNextOutput<OutputImageType>(filter->GetOutput());
+    else
+    {
+      if(this->m_Png){
+        PngConversion<OutputImageType> png;
+        SetOutputFilename(base_filename+"_"+ss.str()+".png");
+        typename PngConversion<OutputImageType>::OutputPngImagePointer output;
+        output = png.Do(this->m_Window, this->m_Level, filter->GetOutput());
+        this->template SetNextOutput<typename PngConversion<OutputImageType>::OutputPngImageType>(output);
+      }
+      else {
+        SetOutputFilename(base_filename+"_"+ss.str()+".mhd");
+        SetNextOutput<OutputImageType>(filter->GetOutput());
+      }
     }
   }
 }