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