]> Creatis software - clitk.git/blob - tools/clitkImageConvertGenericFilter.cxx
kept only the "most relevant" vector conversions
[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   mDisplayWarning = true;
29   mWarning = "";
30   mWarningOccur = false;
31   
32   InitializeImageType<2>();
33   InitializeImageType<3>();
34   InitializeImageType<4>();
35 }
36 //--------------------------------------------------------------------
37
38
39 //--------------------------------------------------------------------
40 template<unsigned int Dim>
41 void clitk::ImageConvertGenericFilter::InitializeImageType()
42 {
43   ADD_DEFAULT_IMAGE_TYPES(Dim);
44   ADD_VEC_IMAGE_TYPE(Dim, 2, float);
45   ADD_VEC_IMAGE_TYPE(Dim, 3, float);
46   ADD_VEC_IMAGE_TYPE(Dim, 2, double);
47   ADD_VEC_IMAGE_TYPE(Dim, 3, double);
48 }
49 //--------------------------------------------------------------------
50
51
52 //--------------------------------------------------------------------
53 template<class InputImageType>
54 void clitk::ImageConvertGenericFilter::UpdateWithInputImageType()
55 {
56   // Verbose stuff
57   if (m_IOVerbose) {
58     if (m_InputFilenames.size() == 1) {
59       std::cout << "Input image <" << m_InputFilenames[0] << "> is ";
60       itk::ImageIOBase::Pointer header = clitk::readImageHeader(m_InputFilenames[0]);
61       printImageHeader(header, std::cout);
62       std::cout << std::endl;
63     } else {
64       for(unsigned int i=0; i<m_InputFilenames.size(); i++) {
65         std::cout << "Input image " << i << " <" << m_InputFilenames[i] << "> is ";
66         itk::ImageIOBase::Pointer h = clitk::readImageHeader(m_InputFilenames[i]);
67         printImageHeader(h, std::cout);
68         std::cout << std::endl;
69       }
70     }
71   }
72
73   if ((m_PixelTypeName == mOutputPixelTypeName) || (mOutputPixelTypeName == "NotSpecified")) {
74     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
75     this->SetNextOutput<InputImageType>(input);
76   } else {
77     // "trick" to call independent versions of update according to the 
78     // pixel type (vector or scalar), using partial specializations
79     if (!UpdateWithSelectiveOutputType<InputImageType, ImageConvertTraits<typename InputImageType::PixelType>::IS_VECTOR>::Run(*this, mOutputPixelTypeName))    
80       exit(-1);
81   }
82 }
83 //====================================================================
84
85 //====================================================================
86
87 template<class PixelType, class OutputPixelType>
88 void clitk::ImageConvertGenericFilter::CheckTypes(
89   std::string inType, std::string outType
90 )
91 {
92   std::ostringstream osstream;
93   if (std::numeric_limits<PixelType>::is_signed) {
94     if (!std::numeric_limits<OutputPixelType>::is_signed) {
95       osstream << "Warning, input type is signed (";
96     }
97   }
98   if (!std::numeric_limits<PixelType>::is_integer) {
99     if (std::numeric_limits<OutputPixelType>::is_integer) {
100       osstream << "Warning, input type is not integer (";
101     }
102   }
103   //  DD(std::numeric_limits<PixelType>::digits10);
104   // DD(std::numeric_limits<OutputPixelType>::digits10);
105   if (!std::numeric_limits<PixelType>::is_integer) {
106     if (std::numeric_limits<OutputPixelType>::is_integer) {
107       osstream << "Warning, input type is not integer (";
108     }
109   }
110   if (std::numeric_limits<PixelType>::digits10 > std::numeric_limits<OutputPixelType>::digits10) {
111     osstream << "Warning, possible loss of precision : input type is (" ;
112   }
113
114   if (!osstream.str().empty())
115   {
116     mWarningOccur = true;
117     osstream << inType << ") while output type is (" << outType << "), use at your own responsability." << std::endl;
118     mWarning = osstream.str();
119     if (mDisplayWarning) {
120       std::cerr << mWarning;
121     }
122   }
123 }
124
125
126 #endif /* end #define CLITKIMAGECONVERTGENERICFILTER_CXX */
127