]> Creatis software - clitk.git/blob - tools/clitkSplitImageGenericFilter.cxx
Debug vvMainWinsow.ui
[clitk.git] / tools / clitkSplitImageGenericFilter.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 /**
19  -------------------------------------------------------------------
20  * @file   clitkSplitImageGenericFilter.cxx
21  * @author Joël Schaerer
22  * @date   20 April 2009
23
24  * @brief
25  -------------------------------------------------------------------*/
26
27 #include "clitkSplitImageGenericFilter.h"
28 #include <itkIntensityWindowingImageFilter.h>
29 //--------------------------------------------------------------------
30 clitk::SplitImageGenericFilter::SplitImageGenericFilter():
31   clitk::ImageToImageGenericFilter<Self>("SplitImage")
32 {
33   mSplitDimension = 0;
34   InitializeImageType<3>();
35   InitializeImageType<4>();
36 }
37 //--------------------------------------------------------------------
38
39 //--------------------------------------------------------------------
40 template<unsigned int Dim>
41 void clitk::SplitImageGenericFilter::InitializeImageType()
42 {
43   ADD_DEFAULT_IMAGE_TYPES(Dim);
44   ADD_VEC_IMAGE_TYPE(Dim, 3,float);
45 }
46 //--------------------------------------------------------------------
47
48 //--------------------------------------------------------------------
49 template <class ImageType>
50 typename clitk::SplitImageGenericFilter::PngConversion<ImageType>::OutputPngImagePointer
51 clitk::SplitImageGenericFilter::PngConversion<ImageType>::Do(double window,
52                                                              double level,
53                                                              ImagePointer input)
54 {
55   static const unsigned int PixelDimension = itk::PixelTraits<typename ImageType::PixelType>::Dimension;
56   return this->Do(window, level, input, static_cast< PixelDimType<PixelDimension> *>(NULL) );
57 }
58 //--------------------------------------------------------------------
59
60 //--------------------------------------------------------------------
61 template <class ImageType>
62 template<unsigned int Dim>
63 typename clitk::SplitImageGenericFilter::PngConversion<ImageType>::OutputPngImagePointer
64 clitk::SplitImageGenericFilter::PngConversion<ImageType>::Do(double window,
65                                                              double level,
66                                                              ImagePointer input,
67                                                              PixelDimType<Dim> *)
68 {
69   clitkExceptionMacro("Png conversion is not implemented for vector fields");
70   return NULL;
71 }
72 //--------------------------------------------------------------------
73
74 //--------------------------------------------------------------------
75 template <class ImageType>
76 typename clitk::SplitImageGenericFilter::PngConversion<ImageType>::OutputPngImagePointer
77 clitk::SplitImageGenericFilter::PngConversion<ImageType>::Do(double window,
78                                                              double level,
79                                                              ImagePointer input,
80                                                              PixelDimType<1> *)
81 {
82   typedef itk::IntensityWindowingImageFilter< ImageType, OutputPngImageType > CastFilterType;
83   typename CastFilterType::Pointer cast = CastFilterType::New();
84   cast->SetWindowLevel(window, level);
85   cast->SetInput(input);
86   cast->Update();
87   return cast->GetOutput();
88 }
89 //--------------------------------------------------------------------
90
91 //--------------------------------------------------------------------
92 template<class ImageType>
93 void clitk::SplitImageGenericFilter::UpdateWithInputImageType()
94 {
95
96   // Read input
97   typedef typename ImageType::PixelType PixelType;
98   typedef itk::Image<PixelType,ImageType::ImageDimension-1> OutputImageType;
99   typename ImageType::Pointer input = this->GetInput<ImageType>(0);
100   typedef itk::ExtractImageFilter<ImageType,OutputImageType> FilterType;
101   typename FilterType::Pointer filter= FilterType::New();
102
103   filter->SetInput(input);
104   typename ImageType::SizeType size=input->GetLargestPossibleRegion().GetSize();
105   size[mSplitDimension]=0;
106   typename ImageType::RegionType extracted_region;
107   extracted_region.SetSize(size);
108   filter->SetDirectionCollapseToIdentity();
109   filter->SetExtractionRegion(extracted_region);
110   filter->Update();
111
112   typename ImageType::IndexType index=input->GetLargestPossibleRegion().GetIndex();
113   std::string base_filename=GetOutputFilename();
114   unsigned int number_of_output_images=input->GetLargestPossibleRegion().GetSize()[mSplitDimension];
115   for (unsigned int i=0; i<number_of_output_images; i++) {
116     std::ostringstream ss;
117     ss << std::setfill('0') << std::setw((int)(log10(double(number_of_output_images))+1)) << i;
118     index[mSplitDimension]=i;
119     extracted_region.SetIndex(index);
120     filter->SetExtractionRegion(extracted_region);
121     filter->Update();
122     if(this->m_Png){
123       PngConversion<OutputImageType> png;
124       SetOutputFilename(base_filename+"_"+ss.str()+".png");
125       typename PngConversion<OutputImageType>::OutputPngImagePointer output;
126       output = png.Do(this->m_Window, this->m_Level, filter->GetOutput());
127       this->template SetNextOutput<typename PngConversion<OutputImageType>::OutputPngImageType>(output);
128     }
129     else {
130       SetOutputFilename(base_filename+"_"+ss.str()+".mhd");
131       SetNextOutput<OutputImageType>(filter->GetOutput());
132     }
133   }
134 }
135 //--------------------------------------------------------------------