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