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 #if ( ITK_VERSION_MAJOR < 5 )
36 #include "itkVectorCastImageFilter.h"
42 template <class TPixel>
43 class ImageConvertTraits
46 enum { IS_VECTOR = false };
47 ImageConvertTraits() {
52 template < class TPixel, unsigned int Comp >
53 class ImageConvertTraits< itk::Vector<TPixel, Comp> >
56 enum { IS_VECTOR = true };
59 class ImageConvertGenericFilter:
60 public clitk::ImageToImageGenericFilter<ImageConvertGenericFilter> {
63 // constructor - destructor
64 ImageConvertGenericFilter();
67 typedef ImageConvertGenericFilter Self;
68 typedef itk::SmartPointer<Self> Pointer;
69 typedef itk::SmartPointer<const Self> ConstPointer;
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; }
85 //--------------------------------------------------------------------
86 // Main function called each time the filter is updated
87 template<class InputImageType>
88 void UpdateWithInputImageType();
90 template<class PixelType, class OutputPixelType>
91 void CheckTypes(std::string inType, std::string outType);
95 template<unsigned int Dim> void InitializeImageType();
96 std::string mOutputPixelTypeName;
102 bool mCorrectNegativeSpacingFlag;
105 template <class InputImageType, bool isVector>
106 class UpdateWithSelectiveOutputType
109 static bool Run(ImageConvertGenericFilter& filter, std::string outputPixelType)
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);
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;
138 template <class OutputPixelType>
139 static void UpdateWithOutputType(ImageConvertGenericFilter& filter)
142 typename InputImageType::Pointer input =filter.template GetInput<InputImageType>(0);
145 typedef typename InputImageType::PixelType PixelType;
148 filter.CheckTypes<PixelType, OutputPixelType>(filter.GetInputPixelTypeName(), filter.GetOutputPixelTypeName());
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();
158 filter.SetNextOutput<OutputImageType>(cast_filter->GetOutput());
162 template <class InputImageType>
163 class UpdateWithSelectiveOutputType<InputImageType, true>
166 static bool Run(ImageConvertGenericFilter& filter, std::string outputPixelType)
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>();
182 if (IsSameType<float>(outputPixelType))
183 UpdateWithOutputVectorType<float>(filter);
184 else if (IsSameType<double>(outputPixelType))
185 UpdateWithOutputVectorType<double>(filter);
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;
199 template <class OutputPixelType>
200 static void UpdateWithOutputVectorType(ImageConvertGenericFilter& filter)
203 typename InputImageType::Pointer input =filter.template GetInput<InputImageType>(0);
206 typedef typename InputImageType::PixelType::ValueType PixelType;
209 filter.CheckTypes<PixelType, OutputPixelType>(filter.GetInputPixelTypeName(), filter.GetOutputPixelTypeName());
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;
216 typedef itk::CastImageFilter<InputImageType, OutputImageType> FilterType;
218 typename FilterType::Pointer cast_filter = FilterType::New();
219 cast_filter->SetInput(input);
220 cast_filter->Update();
223 filter.SetNextOutput<OutputImageType>(cast_filter->GetOutput());
226 }; // end class ImageConvertGenericFilter
228 //#include "clitkImageConvertGenericFilter.txx"
232 #endif /* end #define CLITKIMAGECONVERTGENERICFILTER_H */