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