]> Creatis software - clitk.git/blob - tools/clitkImageConvertGenericFilter.h
8ffb360596833d0225c6b022cc9dd04711dc544d
[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     void SetVV(bool b) { mVV = b; }
77     bool IsWarningOccur() { return mWarningOccur; }
78     std::string & GetWarning() { return mWarning; }
79     void EnableDisplayWarning(bool b) { mDisplayWarning = b; }
80
81     //--------------------------------------------------------------------
82     // Main function called each time the filter is updated
83     template<class InputImageType>  
84     void UpdateWithInputImageType();
85
86     template<class PixelType, class OutputPixelType>
87     void CheckTypes(std::string inType, std::string outType);
88     
89   protected:
90
91     template<unsigned int Dim> void InitializeImageType();
92     std::string mOutputPixelTypeName;
93     std::string mWarning;
94     bool mWarningOccur;
95     bool mDisplayWarning;
96     bool mVV;
97
98   private:
99     template <class InputImageType, bool isVector>
100     class UpdateWithSelectiveOutputType
101     {
102     public:
103       static bool Run(ImageConvertGenericFilter& filter, std::string outputPixelType)
104       {
105           if (IsSameType<char>(outputPixelType))
106             UpdateWithOutputType<char>(filter);
107           else if (IsSameType<uchar>(outputPixelType))
108             UpdateWithOutputType<uchar>(filter);
109           else if (IsSameType<short>(outputPixelType))
110             UpdateWithOutputType<short>(filter);
111           else if (IsSameType<ushort>(outputPixelType))
112             UpdateWithOutputType<ushort>(filter);
113           else if (IsSameType<int>(outputPixelType))
114             UpdateWithOutputType<int>(filter);
115           else if (IsSameType<float>(outputPixelType))
116             UpdateWithOutputType<float>(filter);
117           else if (IsSameType<double>(outputPixelType))
118             UpdateWithOutputType<double>(filter);
119           else
120           {
121             std::string list = CreateListOfTypes<float, double>();
122             std::cerr << "Error, I don't know the vector output type '" << outputPixelType 
123                       << "'. " << std::endl << "Known types are " << list << "." << std::endl;
124             return false;
125           }
126           
127           return true;
128       }
129
130     private:
131       
132       template <class OutputPixelType> 
133       static void UpdateWithOutputType(ImageConvertGenericFilter& filter)
134       {
135         // Read
136         typename InputImageType::Pointer input =filter.template GetInput<InputImageType>(0);
137
138         // Typedef
139         typedef typename InputImageType::PixelType PixelType;
140
141         // Warning
142         filter.CheckTypes<PixelType, OutputPixelType>(filter.GetInputPixelTypeName(), filter.GetOutputPixelTypeName());
143
144         // Cast
145         typedef itk::Image<OutputPixelType,InputImageType::ImageDimension> OutputImageType;
146         typedef itk::CastImageFilter<InputImageType, OutputImageType> FilterType;
147         typename FilterType::Pointer cast_filter = FilterType::New();
148         cast_filter->SetInput(input);
149         cast_filter->Update();
150
151         // Write
152         filter.SetNextOutput<OutputImageType>(cast_filter->GetOutput());
153       }
154     };
155     
156     template <class InputImageType>
157     class UpdateWithSelectiveOutputType<InputImageType, true>
158     {
159     public:
160       static bool Run(ImageConvertGenericFilter& filter, std::string outputPixelType)
161       {
162         /*
163         // RP: future conversions? 
164         if (IsSameType<char>(outputPixelType))
165           UpdateWithOutputVectorType<char>();
166         else if (IsSameType<uchar>(outputPixelType))
167           UpdateWithOutputVectorType<uchar>();
168         else if (IsSameType<short>(outputPixelType))
169           UpdateWithOutputVectorType<short>();
170         else if (IsSameType<ushort>(outputPixelType))
171           UpdateWithOutputVectorType<ushort>();
172         else if (IsSameType<int>(outputPixelType))
173           UpdateWithOutputVectorType<int>();
174         else 
175           */
176         if (IsSameType<float>(outputPixelType))
177           UpdateWithOutputVectorType<float>(filter);
178         else if (IsSameType<double>(outputPixelType))
179           UpdateWithOutputVectorType<double>(filter);
180         else
181         {
182           std::string list = CreateListOfTypes<float, double>();
183           std::cerr << "Error, I don't know the vector output type '" << outputPixelType  
184                     << "'. " << std::endl << "Known types are " << list << "." << std::endl;
185           return false;
186         }
187         
188         return true;
189       }
190       
191     private:
192       
193       template <class OutputPixelType> 
194       static void UpdateWithOutputVectorType(ImageConvertGenericFilter& filter)
195       {
196         // Read
197         typename InputImageType::Pointer input =filter.template GetInput<InputImageType>(0);
198
199         // Typedef
200         typedef typename InputImageType::PixelType::ValueType PixelType;
201
202         // Warning
203         filter.CheckTypes<PixelType, OutputPixelType>(filter.GetInputPixelTypeName(), filter.GetOutputPixelTypeName());
204         
205         // Cast
206         typedef itk::Image<itk::Vector<OutputPixelType, InputImageType::PixelType::Dimension>, InputImageType::ImageDimension> OutputImageType;
207         typedef itk::VectorCastImageFilter<InputImageType, OutputImageType> FilterType;
208         typename FilterType::Pointer cast_filter = FilterType::New();
209         cast_filter->SetInput(input);
210         cast_filter->Update();
211
212         // Write
213         filter.SetNextOutput<OutputImageType>(cast_filter->GetOutput());
214       }
215     };
216   }; // end class ImageConvertGenericFilter
217
218 //#include "clitkImageConvertGenericFilter.txx"
219
220 } // end namespace
221
222 #endif /* end #define CLITKIMAGECONVERTGENERICFILTER_H */
223