]> Creatis software - clitk.git/blob - tools/clitkImageConvertGenericFilter.cxx
Option to save VV transform (mhd + .mat) in the new mhd
[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 #include "vvImageReader.h"
23 #include "vvImageWriter.h"
24
25 //--------------------------------------------------------------------
26 clitk::ImageConvertGenericFilter::ImageConvertGenericFilter():
27   clitk::ImageToImageGenericFilter<Self>("ImageConvert")
28 {
29   mOutputPixelTypeName = "NotSpecified";
30   mDisplayWarning = true;
31   mWarning = "";
32   mWarningOccur = false;
33   
34   InitializeImageType<2>();
35   InitializeImageType<3>();
36   InitializeImageType<4>();
37 }
38 //--------------------------------------------------------------------
39
40
41 //--------------------------------------------------------------------
42 template<unsigned int Dim>
43 void clitk::ImageConvertGenericFilter::InitializeImageType()
44 {
45   ADD_DEFAULT_IMAGE_TYPES(Dim);
46   ADD_VEC_IMAGE_TYPE(Dim, 2, float);
47   ADD_VEC_IMAGE_TYPE(Dim, 3, float);
48   ADD_VEC_IMAGE_TYPE(Dim, 2, double);
49   ADD_VEC_IMAGE_TYPE(Dim, 3, double);
50 }
51 //--------------------------------------------------------------------
52
53
54 //--------------------------------------------------------------------
55 template<class InputImageType>
56 void clitk::ImageConvertGenericFilter::UpdateWithInputImageType()
57 {
58   // Verbose stuff
59   if (m_IOVerbose) {
60     if (m_InputFilenames.size() == 1) {
61       std::cout << "Input image <" << m_InputFilenames[0] << "> is ";
62       itk::ImageIOBase::Pointer header = clitk::readImageHeader(m_InputFilenames[0]);
63       printImageHeader(header, std::cout);
64       std::cout << std::endl;
65     } else {
66       for(unsigned int i=0; i<m_InputFilenames.size(); i++) {
67         std::cout << "Input image " << i << " <" << m_InputFilenames[i] << "> is ";
68         itk::ImageIOBase::Pointer h = clitk::readImageHeader(m_InputFilenames[i]);
69         printImageHeader(h, std::cout);
70         std::cout << std::endl;
71       }
72     }
73   }
74
75   if(mVV) {
76     if (mOutputPixelTypeName != "NotSpecified" || m_WriteCompression) {
77       std::cerr << "--vv is not compatible with --compression and --type options." << std::endl;
78       exit(-1);
79     }
80
81     vvImageReader::Pointer reader = vvImageReader::New();
82     reader->SetInputFilenames(m_InputFilenames);
83     reader->Update(vvImageReader::IMAGE);
84
85     vvImageWriter::Pointer writer = vvImageWriter::New();
86     writer->SetOutputFileName(m_OutputFilenames.front());
87     writer->SetSaveTransform(true);
88     writer->SetInput(reader->GetOutput());
89     writer->Update();
90     return;
91   }
92   else if ((m_PixelTypeName == mOutputPixelTypeName) || (mOutputPixelTypeName == "NotSpecified")) {
93     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
94     this->SetNextOutput<InputImageType>(input);
95   } else {
96     // "trick" to call independent versions of update according to the 
97     // pixel type (vector or scalar), using partial specializations
98     if (!UpdateWithSelectiveOutputType<InputImageType, ImageConvertTraits<typename InputImageType::PixelType>::IS_VECTOR>::Run(*this, mOutputPixelTypeName))    
99       exit(-1);
100   }
101 }
102 //====================================================================
103
104 //====================================================================
105
106 template<class PixelType, class OutputPixelType>
107 void clitk::ImageConvertGenericFilter::CheckTypes(
108   std::string inType, std::string outType
109 )
110 {
111   std::ostringstream osstream;
112   if (std::numeric_limits<PixelType>::is_signed) {
113     if (!std::numeric_limits<OutputPixelType>::is_signed) {
114       osstream << "Warning, input type is signed (";
115     }
116   }
117   if (!std::numeric_limits<PixelType>::is_integer) {
118     if (std::numeric_limits<OutputPixelType>::is_integer) {
119       osstream << "Warning, input type is not integer (";
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 (";
127     }
128   }
129   if (std::numeric_limits<PixelType>::digits10 > std::numeric_limits<OutputPixelType>::digits10) {
130     osstream << "Warning, possible loss of precision : input type is (" ;
131   }
132
133   if (!osstream.str().empty())
134   {
135     mWarningOccur = true;
136     osstream << inType << ") while output type is (" << outType << "), use at your own responsability." << std::endl;
137     mWarning = osstream.str();
138     if (mDisplayWarning) {
139       std::cerr << mWarning;
140     }
141   }
142 }
143
144
145 #endif /* end #define CLITKIMAGECONVERTGENERICFILTER_CXX */
146