]> Creatis software - clitk.git/blob - filters/clitkImageConvertGenericFilter.cxx
c6afba85cb7cb0c6b97129f4baade53a17b7e431
[clitk.git] / filters / clitkImageConvertGenericFilter.cxx
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_CXX
19 #define CLITKIMAGECONVERTGENERICFILTER_CXX
20
21 #include "clitkImageConvertGenericFilter.h"
22
23 //--------------------------------------------------------------------
24 clitk::ImageConvertGenericFilter::ImageConvertGenericFilter():
25   clitk::ImageToImageGenericFilter<Self>("ImageConvert") {
26   mOutputPixelTypeName = "NotSpecified";
27   mWarningOccur = false;
28   mWarning = "";
29   mDisplayWarning = true;
30   InitializeImageType<2>();
31   InitializeImageType<3>();  
32   InitializeImageType<4>();  
33 }
34 //--------------------------------------------------------------------
35
36
37 //--------------------------------------------------------------------
38 template<unsigned int Dim>
39 void clitk::ImageConvertGenericFilter::InitializeImageType() {      
40     ADD_DEFAULT_IMAGE_TYPES(Dim);
41 }
42 //--------------------------------------------------------------------
43
44
45 //--------------------------------------------------------------------
46 template<class InputImageType>
47 void clitk::ImageConvertGenericFilter::UpdateWithInputImageType() {
48
49   // Verbose stuff
50   if (mIOVerbose) {
51     if (mInputFilenames.size() == 1) {
52       std::cout << "Input image <" << mInputFilenames[0] << "> is ";
53       itk::ImageIOBase::Pointer header = clitk::readImageHeader(mInputFilenames[0]);
54       printImageHeader(header, std::cout);
55       std::cout << std::endl;
56     }
57     else {
58       for(unsigned int i=0; i<mInputFilenames.size(); i++) {
59         std::cout << "Input image " << i << " <" << mInputFilenames[i] << "> is ";
60         itk::ImageIOBase::Pointer h = clitk::readImageHeader(mInputFilenames[i]);       
61         printImageHeader(h, std::cout);
62         std::cout << std::endl;
63       }
64     }
65   }
66
67
68   if ((mPixelTypeName == mOutputPixelTypeName) || (mOutputPixelTypeName == "NotSpecified")) {
69     //    typename InputImageType::Pointer input = clitk::readImage<InputImageType>(mInputFilenames);
70     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
71     //clitk::writeImage<InputImageType>(input, mOutputFilename, mIOVerbose);
72     this->SetNextOutput<InputImageType>(input);
73   }
74   else {
75 #define TRY_TYPE(TYPE)                                                  \
76     if (IsSameType<TYPE>(mOutputPixelTypeName)) { UpdateWithOutputType<InputImageType, TYPE>(); return; }
77     TRY_TYPE(char);
78     //    TRY_TYPE(signed char);
79     TRY_TYPE(uchar);
80     TRY_TYPE(short);
81     TRY_TYPE(ushort);
82     TRY_TYPE(int); // no uint ...
83     TRY_TYPE(float);
84     TRY_TYPE(double);
85 #undef TRY_TYPE
86
87     std::string list = CreateListOfTypes<char, uchar, short, ushort, int, float, double>();
88     std::cerr << "Error, I don't know the output type '" << mOutputPixelTypeName 
89               << "'. " << std::endl << "Known types are " << list << "." << std::endl;
90     exit(0);
91   }  
92 }
93 //====================================================================
94
95 //====================================================================
96 template<class InputImageType, class OutputPixelType>
97 void clitk::ImageConvertGenericFilter::UpdateWithOutputType() {
98   // Read
99   typename InputImageType::Pointer input =this->template GetInput<InputImageType>(0);
100
101   // Typedef
102   typedef typename InputImageType::PixelType PixelType;
103
104   // Warning
105   std::ostringstream osstream;
106   if (std::numeric_limits<PixelType>::is_signed) {
107     if (!std::numeric_limits<OutputPixelType>::is_signed) {
108       osstream << "Warning, input type is signed (" << mPixelTypeName << ") while output type is not (" 
109                << mOutputPixelTypeName << "), use at your own responsability." << std::endl;
110       mWarningOccur = true;      
111     }
112   }
113   if (!std::numeric_limits<PixelType>::is_integer) {
114     if (std::numeric_limits<OutputPixelType>::is_integer) {
115       osstream << "Warning, input type is not integer (" << mPixelTypeName << ") while output type is (" 
116                << mOutputPixelTypeName << "), use at your own responsability." << std::endl;
117       mWarningOccur = true;
118     }
119   }
120   //  DD(std::numeric_limits<PixelType>::digits10);
121   // DD(std::numeric_limits<OutputPixelType>::digits10);
122   if (!std::numeric_limits<PixelType>::is_integer) {
123     if (std::numeric_limits<OutputPixelType>::is_integer) {
124       osstream << "Warning, input type is not integer (" << mPixelTypeName << ") while output type is (" 
125                << mOutputPixelTypeName << "), use at your own responsability." << std::endl;
126       mWarningOccur = true;
127     }
128   }
129   if (std::numeric_limits<PixelType>::digits10 > std::numeric_limits<OutputPixelType>::digits10) {
130     osstream << "Warning, possible loss of precision : input type is (" << mPixelTypeName << ") while output type is (" 
131              << mOutputPixelTypeName << "), use at your own responsability." << std::endl;
132     mWarningOccur = true;
133   }
134
135   mWarning = osstream.str();
136   if (mDisplayWarning) {
137     std::cerr << mWarning;
138   }
139
140   // Cast
141   typedef itk::Image<OutputPixelType,InputImageType::ImageDimension> OutputImageType;
142   typedef itk::CastImageFilter<InputImageType, OutputImageType> FilterType;
143   typename FilterType::Pointer filter = FilterType::New();
144   filter->SetInput(input);
145   filter->Update();
146
147   // Write
148   SetNextOutput<OutputImageType>(filter->GetOutput());
149   //clitk::writeImage<OutputImageType>(filter->GetOutput(), mOutputFilename, mIOVerbose);
150 }
151 //====================================================================
152
153
154 #endif /* end #define CLITKIMAGECONVERTGENERICFILTER_CXX */
155