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