1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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
21 ===================================================================
22 * @file clitkImageConvertGenericFilter.h
23 * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24 * @date 05 May 2008 10:40:24
28 ===================================================================*/
31 #include "clitkImageToImageGenericFilter.h"
34 #include "itkCastImageFilter.h"
35 #include "itkVectorCastImageFilter.h"
40 template <class TPixel>
41 class ImageConvertTraits
44 enum { IS_VECTOR = false };
45 ImageConvertTraits() {
50 template < class TPixel, unsigned int Comp >
51 class ImageConvertTraits< itk::Vector<TPixel, Comp> >
54 enum { IS_VECTOR = true };
57 class ImageConvertGenericFilter:
58 public clitk::ImageToImageGenericFilter<ImageConvertGenericFilter> {
61 // constructor - destructor
62 ImageConvertGenericFilter();
65 typedef ImageConvertGenericFilter Self;
66 typedef itk::SmartPointer<Self> Pointer;
67 typedef itk::SmartPointer<const Self> ConstPointer;
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 void SetCorrectNegativeSpacingFlag(bool b) { mCorrectNegativeSpacingFlag = b; }
82 //--------------------------------------------------------------------
83 // Main function called each time the filter is updated
84 template<class InputImageType>
85 void UpdateWithInputImageType();
87 template<class PixelType, class OutputPixelType>
88 void CheckTypes(std::string inType, std::string outType);
92 template<unsigned int Dim> void InitializeImageType();
93 std::string mOutputPixelTypeName;
98 bool mCorrectNegativeSpacingFlag;
101 template <class InputImageType, bool isVector>
102 class UpdateWithSelectiveOutputType
105 static bool Run(ImageConvertGenericFilter& filter, std::string outputPixelType)
107 if (IsSameType<char>(outputPixelType))
108 UpdateWithOutputType<char>(filter);
109 else if (IsSameType<uchar>(outputPixelType))
110 UpdateWithOutputType<uchar>(filter);
111 else if (IsSameType<short>(outputPixelType))
112 UpdateWithOutputType<short>(filter);
113 else if (IsSameType<ushort>(outputPixelType))
114 UpdateWithOutputType<ushort>(filter);
115 else if (IsSameType<int>(outputPixelType))
116 UpdateWithOutputType<int>(filter);
117 else if (IsSameType<float>(outputPixelType))
118 UpdateWithOutputType<float>(filter);
119 else if (IsSameType<double>(outputPixelType))
120 UpdateWithOutputType<double>(filter);
123 std::string list = CreateListOfTypes<float, double>();
124 std::cerr << "Error, I don't know the vector output type '" << outputPixelType
125 << "'. " << std::endl << "Known types are " << list << "." << std::endl;
134 template <class OutputPixelType>
135 static void UpdateWithOutputType(ImageConvertGenericFilter& filter)
138 typename InputImageType::Pointer input =filter.template GetInput<InputImageType>(0);
141 typedef typename InputImageType::PixelType PixelType;
144 filter.CheckTypes<PixelType, OutputPixelType>(filter.GetInputPixelTypeName(), filter.GetOutputPixelTypeName());
147 typedef itk::Image<OutputPixelType,InputImageType::ImageDimension> OutputImageType;
148 typedef itk::CastImageFilter<InputImageType, OutputImageType> FilterType;
149 typename FilterType::Pointer cast_filter = FilterType::New();
150 cast_filter->SetInput(input);
151 cast_filter->Update();
154 filter.SetNextOutput<OutputImageType>(cast_filter->GetOutput());
158 template <class InputImageType>
159 class UpdateWithSelectiveOutputType<InputImageType, true>
162 static bool Run(ImageConvertGenericFilter& filter, std::string outputPixelType)
165 // RP: future conversions?
166 if (IsSameType<char>(outputPixelType))
167 UpdateWithOutputVectorType<char>();
168 else if (IsSameType<uchar>(outputPixelType))
169 UpdateWithOutputVectorType<uchar>();
170 else if (IsSameType<short>(outputPixelType))
171 UpdateWithOutputVectorType<short>();
172 else if (IsSameType<ushort>(outputPixelType))
173 UpdateWithOutputVectorType<ushort>();
174 else if (IsSameType<int>(outputPixelType))
175 UpdateWithOutputVectorType<int>();
178 if (IsSameType<float>(outputPixelType))
179 UpdateWithOutputVectorType<float>(filter);
180 else if (IsSameType<double>(outputPixelType))
181 UpdateWithOutputVectorType<double>(filter);
184 std::string list = CreateListOfTypes<float, double>();
185 std::cerr << "Error, I don't know the vector output type '" << outputPixelType
186 << "'. " << std::endl << "Known types are " << list << "." << std::endl;
195 template <class OutputPixelType>
196 static void UpdateWithOutputVectorType(ImageConvertGenericFilter& filter)
199 typename InputImageType::Pointer input =filter.template GetInput<InputImageType>(0);
202 typedef typename InputImageType::PixelType::ValueType PixelType;
205 filter.CheckTypes<PixelType, OutputPixelType>(filter.GetInputPixelTypeName(), filter.GetOutputPixelTypeName());
208 typedef itk::Image<itk::Vector<OutputPixelType, InputImageType::PixelType::Dimension>, InputImageType::ImageDimension> OutputImageType;
209 typedef itk::VectorCastImageFilter<InputImageType, OutputImageType> FilterType;
210 typename FilterType::Pointer cast_filter = FilterType::New();
211 cast_filter->SetInput(input);
212 cast_filter->Update();
215 filter.SetNextOutput<OutputImageType>(cast_filter->GetOutput());
218 }; // end class ImageConvertGenericFilter
220 //#include "clitkImageConvertGenericFilter.txx"
224 #endif /* end #define CLITKIMAGECONVERTGENERICFILTER_H */