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