]> Creatis software - clitk.git/blob - tools/clitkNVectorImageTo4DImageGenericFilter.txx
932b9acba309a223107b3595f686ca9ab317760b
[clitk.git] / tools / clitkNVectorImageTo4DImageGenericFilter.txx
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 #ifndef clitkNVectorImageTo4DImageGenericFilter_txx
19 #define clitkNVectorImageTo4DImageGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkNVectorImageTo4DImageGenericFilter.txx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30 #include "itkVectorImageToImageAdaptor.h"
31 #include <sstream>
32
33 namespace clitk
34 {
35
36   //-------------------------------------------------------------------
37   // Update with the number of dimensions
38   //-------------------------------------------------------------------
39   template<unsigned int Dimension>
40   void 
41   NVectorImageTo4DImageGenericFilter::UpdateWithDim(std::string PixelType, unsigned int Components)
42   {
43     if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
44
45     if (PixelType == "short") {
46       UpdateWithDimAndPixelType<Dimension, short>();
47     } else if (PixelType == "unsigned short") {
48       UpdateWithDimAndPixelType<Dimension, unsigned short>();
49     } else if (PixelType == "unsigned_short") {
50       UpdateWithDimAndPixelType<Dimension, unsigned short>();
51     } else if (PixelType == "char") {
52       UpdateWithDimAndPixelType<Dimension, char>();
53     } else if (PixelType == "unsigned_char") {
54       UpdateWithDimAndPixelType<Dimension, unsigned char>();
55     } else if (PixelType == "int") {
56       UpdateWithDimAndPixelType<Dimension, int>();
57     } else if (PixelType == "unsigned_int") {
58       UpdateWithDimAndPixelType<Dimension, unsigned int>();
59     } else if (PixelType == "double") {
60       UpdateWithDimAndPixelType<Dimension, double>();
61     } else if (PixelType == "float") {
62       UpdateWithDimAndPixelType<Dimension, float>();
63     } else {
64       std::cerr << "Error, pixel type : \"" << PixelType << "\" unknown !" << std::endl;
65     }
66   }
67
68
69   //-------------------------------------------------------------------
70   // Update with the number of dimensions and the pixeltype
71   //-------------------------------------------------------------------
72   template <unsigned int Dimension, class  PixelType> 
73   void 
74   NVectorImageTo4DImageGenericFilter::UpdateWithDimAndPixelType()
75   {
76     // ImageTypes
77     typedef itk::VectorImage<PixelType, Dimension> InputImageType;
78     typedef itk::Image<PixelType, Dimension+1> OutputImageType;
79     
80     // Read the input
81     typedef itk::ImageFileReader<InputImageType> InputReaderType;
82     typename InputReaderType::Pointer reader = InputReaderType::New();
83     reader->SetFileName( m_InputFileName);
84     reader->Update();
85     typename InputImageType::Pointer input= reader->GetOutput();
86     
87     //Filter
88     typedef itk::VectorImageToImageAdaptor<PixelType, Dimension> ImageAdaptorType;
89     typedef itk::ImageFileWriter<OutputImageType> WriterType;
90     typename ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New();
91     typename OutputImageType::Pointer output = OutputImageType::New();
92     typename WriterType::Pointer writer = WriterType::New();
93
94     adaptor->SetExtractComponentIndex(0);
95     adaptor->SetImage(input);
96     std::string fileName=m_ArgsInfo.output_arg;
97     
98     //Create the output
99     typename OutputImageType::IndexType index;
100     index.Fill(0);
101     typename OutputImageType::SizeType size;
102     size.Fill(input->GetNumberOfComponentsPerPixel());
103     typename OutputImageType::SpacingType spacing;
104     spacing.Fill(1);
105     typename OutputImageType::PointType origin;
106     origin.Fill(0);
107     for (unsigned int pixelDim=0; pixelDim<Dimension; ++pixelDim)
108     {
109       size[pixelDim]=adaptor->GetLargestPossibleRegion().GetSize(pixelDim);
110       spacing[pixelDim]=input->GetSpacing()[pixelDim];
111       origin[pixelDim]=input->GetOrigin()[pixelDim];
112     }
113     typename OutputImageType::RegionType region;
114     region.SetSize(size);
115     region.SetIndex(index);    
116     output->SetRegions(region);
117     output->SetOrigin(origin);
118     output->SetSpacing(spacing);
119     output->Allocate();
120     writer->SetInput(output);
121     
122     //Copy each channel
123     for (unsigned int pixelDim=0; pixelDim<input->GetNumberOfComponentsPerPixel(); ++pixelDim)
124     {
125       adaptor->SetExtractComponentIndex(pixelDim);
126       
127       itk::ImageRegionIterator<InputImageType> imageIterator(input,input->GetLargestPossibleRegion());
128
129       while(!imageIterator.IsAtEnd())
130       {
131         typename OutputImageType::IndexType indexVector;
132         indexVector.Fill(0);
133         for (unsigned int indexDim=0; indexDim<Dimension; ++indexDim)
134         {
135           indexVector[indexDim]=imageIterator.GetIndex().GetElement(indexDim);
136         }
137         indexVector[Dimension]=pixelDim;
138         
139         output->SetPixel(indexVector, adaptor->GetPixel(imageIterator.GetIndex()));
140         ++imageIterator;
141       }
142     }
143     // Output
144     writer->SetFileName(fileName);
145     writer->Update();
146   }
147 }//end clitk
148  
149 #endif //#define clitkNVectorImageTo4DImageGenericFilter_txx