]> Creatis software - clitk.git/blob - filters/clitkImageConvertGenericFilter.txx
removed headers
[clitk.git] / filters / clitkImageConvertGenericFilter.txx
1 #ifndef CLITKIMAGECONVERTGENERICFILTER_TXX
2 #define CLITKIMAGECONVERTGENERICFILTER_TXX
3 /**
4    =================================================
5    * @file   clitkImageConvertGenericFilter.txx
6    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
7    * @date   05 May 2008 11:14:20
8    * 
9    * @brief  
10    * 
11    * 
12    =================================================*/
13
14 #include <limits>
15
16 //====================================================================
17 template<unsigned int Dim>
18 void ImageConvertGenericFilter::Update_WithDim() {  
19 #define TRY_TYPE(TYPE) \
20   if (IsSameType<TYPE>(mPixelTypeName)) { Update_WithDimAndPixelType<Dim, TYPE>(); return; } 
21   // TRY_TYPE(signed char);
22   TRY_TYPE(char);
23   TRY_TYPE(uchar);
24   TRY_TYPE(short);
25   TRY_TYPE(ushort);
26   TRY_TYPE(int); 
27   TRY_TYPE(unsigned int); 
28   TRY_TYPE(float);
29   TRY_TYPE(double);
30 #undef TRY_TYPE
31
32   std::string list = CreateListOfTypes<uchar, short, ushort, int, uint, float, double>();
33   std::cerr << "Error, I don't know the type '" << mPixelTypeName << "' for the input image '"
34             << mInputFilenames[0] << "'." << std::endl << "Known types are " << list << std::endl;
35   exit(0);
36 }
37 //====================================================================
38
39 //====================================================================
40 template<unsigned int Dim, class PixelType>
41 void ImageConvertGenericFilter::Update_WithDimAndPixelType() {
42   if ((mPixelTypeName == mOutputPixelTypeName) || (mOutputPixelTypeName == "NotSpecified")) {
43     typedef itk::Image<PixelType,Dim> InputImageType;
44     typename InputImageType::Pointer input = clitk::readImage<InputImageType>(mInputFilenames);
45     //clitk::writeImage<InputImageType>(input, mOutputFilename, mIOVerbose);
46     this->SetNextOutput<InputImageType>(input);
47   }
48   else {
49 #define TRY_TYPE(TYPE) \
50     if (IsSameType<TYPE>(mOutputPixelTypeName)) { Update_WithDimAndPixelTypeAndOutputType<Dim, PixelType, TYPE>(); return; }
51     TRY_TYPE(char);
52     // TRY_TYPE(signed char);
53     TRY_TYPE(uchar);
54     TRY_TYPE(short);
55     TRY_TYPE(ushort);
56     TRY_TYPE(int); // no uint ...
57     TRY_TYPE(float);
58     TRY_TYPE(double);
59 #undef TRY_TYPE
60
61     std::string list = CreateListOfTypes<char, uchar, short, ushort, int, float, double>();
62     std::cerr << "Error, I don't know the output type '" << mOutputPixelTypeName 
63               << "'. " << std::endl << "Known types are " << list << "." << std::endl;
64     exit(0);
65   }  
66 }
67 //====================================================================
68
69 //====================================================================
70 template<unsigned int Dim, class PixelType, class OutputPixelType>
71 void ImageConvertGenericFilter::Update_WithDimAndPixelTypeAndOutputType() {
72   // Read
73   typedef itk::Image<PixelType,Dim> InputImageType;
74   typename InputImageType::Pointer input = clitk::readImage<InputImageType>(mInputFilenames);
75
76   // Warning
77   if (std::numeric_limits<PixelType>::is_signed) {
78     if (!std::numeric_limits<OutputPixelType>::is_signed) {
79       std::cerr << "Warning, input type is signed (" << mPixelTypeName << ") while output type is not (" 
80                 << mOutputPixelTypeName << "), use at your own responsability." << std::endl;
81     }
82   }
83   if (!std::numeric_limits<PixelType>::is_integer) {
84     if (std::numeric_limits<OutputPixelType>::is_integer) {
85       std::cerr << "Warning, input type is not integer (" << mPixelTypeName << ") while output type is (" 
86                 << mOutputPixelTypeName << "), use at your own responsability." << std::endl;
87     }
88   }
89   //  DD(std::numeric_limits<PixelType>::digits10);
90   // DD(std::numeric_limits<OutputPixelType>::digits10);
91   if (!std::numeric_limits<PixelType>::is_integer) {
92     if (std::numeric_limits<OutputPixelType>::is_integer) {
93       std::cerr << "Warning, input type is not integer (" << mPixelTypeName << ") while output type is (" 
94                 << mOutputPixelTypeName << "), use at your own responsability." << std::endl;
95     }
96   }
97   if (std::numeric_limits<PixelType>::digits10 > std::numeric_limits<OutputPixelType>::digits10) {
98     std::cerr << "Warning, possible loss of precision : input type is (" << mPixelTypeName << ") while output type is (" 
99                 << mOutputPixelTypeName << "), use at your own responsability." << std::endl;
100   }
101
102   // Cast
103   typedef itk::Image<OutputPixelType,Dim> OutputImageType;
104   typedef itk::CastImageFilter<InputImageType, OutputImageType> FilterType;
105   typename FilterType::Pointer filter = FilterType::New();
106   filter->SetInput(input);
107   filter->Update();
108
109   // Write
110   SetNextOutput<OutputImageType>(filter->GetOutput());
111   //clitk::writeImage<OutputImageType>(filter->GetOutput(), mOutputFilename, mIOVerbose);
112 }
113 //====================================================================
114
115 #endif /* end #define CLITKIMAGECONVERTGENERICFILTER_TXX */
116