]> Creatis software - clitk.git/blob - tools/clitkImageConvertGenericFilter.h
kept only the "most relevant" vector conversions
[clitk.git] / tools / clitkImageConvertGenericFilter.h
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 CLITKIMAGECONVERTGENERICFILTER_H
19 #define CLITKIMAGECONVERTGENERICFILTER_H
20 /**
21  ===================================================================
22  * @file   clitkImageConvertGenericFilter.h
23  * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24  * @date   05 May 2008 10:40:24
25
26  * @brief  
27
28  ===================================================================*/
29
30 // clitk include
31 #include "clitkImageToImageGenericFilter.h"
32
33 // itk include
34 #include "itkCastImageFilter.h"
35 #include "itkVectorCastImageFilter.h"
36
37
38 namespace clitk {
39   
40   template <class TPixel>
41   class ImageConvertTraits
42   {
43   public:
44     enum { IS_VECTOR = false };
45     ImageConvertTraits() {
46       TPixel p = "SCALAR";
47     }
48   };
49
50   template < class TPixel, unsigned int Comp > 
51   class ImageConvertTraits< itk::Vector<TPixel, Comp> > 
52   { 
53   public: 
54     enum { IS_VECTOR = true }; 
55   };
56
57   class ImageConvertGenericFilter: 
58     public clitk::ImageToImageGenericFilter<ImageConvertGenericFilter> {
59     
60   public: 
61     // constructor - destructor
62     ImageConvertGenericFilter();
63
64     // Types
65     typedef ImageConvertGenericFilter     Self;
66     typedef itk::SmartPointer<Self>       Pointer;
67     typedef itk::SmartPointer<const Self> ConstPointer;
68
69     // New
70     itkNewMacro(Self);
71     
72     // Members functions
73     std::string GetInputPixelTypeName() { return m_PixelTypeName; }
74     std::string GetOutputPixelTypeName() { return mOutputPixelTypeName; }
75     void SetOutputPixelType(std::string p) { mOutputPixelTypeName = p; }
76     bool IsWarningOccur() { return mWarningOccur; }
77     std::string & GetWarning() { return mWarning; }
78     void EnableDisplayWarning(bool b) { mDisplayWarning = b; }
79
80     //--------------------------------------------------------------------
81     // Main function called each time the filter is updated
82     template<class InputImageType>  
83     void UpdateWithInputImageType();
84
85     template<class PixelType, class OutputPixelType>
86     void CheckTypes(std::string inType, std::string outType);
87     
88   protected:
89
90     template<unsigned int Dim> void InitializeImageType();
91     std::string mOutputPixelTypeName;
92     std::string mWarning;
93     bool mWarningOccur;
94     bool mDisplayWarning;
95
96   private:
97     template <class InputImageType, bool isVector>
98     class UpdateWithSelectiveOutputType
99     {
100     public:
101       static bool Run(ImageConvertGenericFilter& filter, std::string outputPixelType)
102       {
103           if (IsSameType<char>(outputPixelType))
104             UpdateWithOutputType<char>(filter);
105           else if (IsSameType<uchar>(outputPixelType))
106             UpdateWithOutputType<uchar>(filter);
107           else if (IsSameType<short>(outputPixelType))
108             UpdateWithOutputType<short>(filter);
109           else if (IsSameType<ushort>(outputPixelType))
110             UpdateWithOutputType<ushort>(filter);
111           else if (IsSameType<int>(outputPixelType))
112             UpdateWithOutputType<int>(filter);
113           else if (IsSameType<float>(outputPixelType))
114             UpdateWithOutputType<float>(filter);
115           else if (IsSameType<double>(outputPixelType))
116             UpdateWithOutputType<double>(filter);
117           else
118           {
119             std::string list = CreateListOfTypes<float, double>();
120             std::cerr << "Error, I don't know the vector output type '" << outputPixelType 
121                       << "'. " << std::endl << "Known types are " << list << "." << std::endl;
122             return false;
123           }
124           
125           return true;
126       }
127
128     private:
129       
130       template <class OutputPixelType> 
131       static void UpdateWithOutputType(ImageConvertGenericFilter& filter)
132       {
133         // Read
134         typename InputImageType::Pointer input =filter.template GetInput<InputImageType>(0);
135
136         // Typedef
137         typedef typename InputImageType::PixelType PixelType;
138
139         // Warning
140         filter.CheckTypes<PixelType, OutputPixelType>(filter.GetInputPixelTypeName(), filter.GetOutputPixelTypeName());
141
142         // Cast
143         typedef itk::Image<OutputPixelType,InputImageType::ImageDimension> OutputImageType;
144         typedef itk::CastImageFilter<InputImageType, OutputImageType> FilterType;
145         typename FilterType::Pointer cast_filter = FilterType::New();
146         cast_filter->SetInput(input);
147         cast_filter->Update();
148
149         // Write
150         filter.SetNextOutput<OutputImageType>(cast_filter->GetOutput());
151       }
152     };
153     
154     template <class InputImageType>
155     class UpdateWithSelectiveOutputType<InputImageType, true>
156     {
157     public:
158       static bool Run(ImageConvertGenericFilter& filter, std::string outputPixelType)
159       {
160         /*
161         // RP: future conversions? 
162         if (IsSameType<char>(outputPixelType))
163           UpdateWithOutputVectorType<char>();
164         else if (IsSameType<uchar>(outputPixelType))
165           UpdateWithOutputVectorType<uchar>();
166         else if (IsSameType<short>(outputPixelType))
167           UpdateWithOutputVectorType<short>();
168         else if (IsSameType<ushort>(outputPixelType))
169           UpdateWithOutputVectorType<ushort>();
170         else if (IsSameType<int>(outputPixelType))
171           UpdateWithOutputVectorType<int>();
172         else 
173           */
174         if (IsSameType<float>(outputPixelType))
175           UpdateWithOutputVectorType<float>(filter);
176         else if (IsSameType<double>(outputPixelType))
177           UpdateWithOutputVectorType<double>(filter);
178         else
179         {
180           std::string list = CreateListOfTypes<float, double>();
181           std::cerr << "Error, I don't know the vector output type '" << outputPixelType  
182                     << "'. " << std::endl << "Known types are " << list << "." << std::endl;
183           return false;
184         }
185         
186         return true;
187       }
188       
189     private:
190       
191       template <class OutputPixelType> 
192       static void UpdateWithOutputVectorType(ImageConvertGenericFilter& filter)
193       {
194         // Read
195         typename InputImageType::Pointer input =filter.template GetInput<InputImageType>(0);
196
197         // Typedef
198         typedef typename InputImageType::PixelType::ValueType PixelType;
199
200         // Warning
201         filter.CheckTypes<PixelType, OutputPixelType>(filter.GetInputPixelTypeName(), filter.GetOutputPixelTypeName());
202         
203         // Cast
204         typedef itk::Image<itk::Vector<OutputPixelType, InputImageType::PixelType::Dimension>, InputImageType::ImageDimension> OutputImageType;
205         typedef itk::VectorCastImageFilter<InputImageType, OutputImageType> FilterType;
206         typename FilterType::Pointer cast_filter = FilterType::New();
207         cast_filter->SetInput(input);
208         cast_filter->Update();
209
210         // Write
211         filter.SetNextOutput<OutputImageType>(cast_filter->GetOutput());
212       }
213     };
214   }; // end class ImageConvertGenericFilter
215
216 //#include "clitkImageConvertGenericFilter.txx"
217
218 } // end namespace
219
220 #endif /* end #define CLITKIMAGECONVERTGENERICFILTER_H */
221