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