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