]> Creatis software - clitk.git/blob - common/clitkImageToImageGenericFilterBase.cxx
added the new headers
[clitk.git] / common / clitkImageToImageGenericFilterBase.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 #include "clitkImageToImageGenericFilterBase.h"
19 #include <itkImage.h>
20
21 //--------------------------------------------------------------------
22 clitk::ImageToImageGenericFilterBase::~ImageToImageGenericFilterBase() {}
23 //--------------------------------------------------------------------
24
25
26 //--------------------------------------------------------------------
27 clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string n)
28   :mIOVerbose(false) {
29   mFilterName = n;
30   mFailOnImageTypeError = true;
31   mReadOnDisk = true;
32 }
33 //--------------------------------------------------------------------
34
35
36 //--------------------------------------------------------------------
37 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames) {
38     mInputFilenames=filenames;
39 }
40 //--------------------------------------------------------------------
41
42
43 //--------------------------------------------------------------------
44 void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b) {
45   mReadOnDisk = b;
46 }
47 //--------------------------------------------------------------------
48
49
50 //--------------------------------------------------------------------
51 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename) {
52   std::vector<std::string> f;
53   f.push_back(filename);
54   SetInputFilenames(f);
55 }
56 //--------------------------------------------------------------------
57
58
59 //--------------------------------------------------------------------
60 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename) {
61   mInputFilenames.push_back(filename);
62 }
63 //--------------------------------------------------------------------
64
65
66 //--------------------------------------------------------------------
67 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename) {
68   mOutputFilenames.clear();
69   mOutputFilenames.push_back(filename);
70 }
71 //--------------------------------------------------------------------
72
73
74 //--------------------------------------------------------------------
75 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string & filename)
76 {
77   mOutputFilenames.push_back(filename);
78 }
79 //--------------------------------------------------------------------
80
81
82 //--------------------------------------------------------------------
83 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
84 {
85     mOutputFilenames.clear();
86     std::copy(filenames.begin(),filenames.end(),mOutputFilenames.begin());
87 }
88 //--------------------------------------------------------------------
89
90
91 //--------------------------------------------------------------------
92 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
93 {
94     assert(mOutputFilenames.size() == 1);
95     return mOutputFilenames.front();
96 }
97 //--------------------------------------------------------------------
98
99
100 //--------------------------------------------------------------------
101 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
102         std::string& pixeltype,unsigned int& components)
103 {
104   if (mReadOnDisk && mInputFilenames.size()) {
105     int comp_temp,dim_temp; //clitkCommonImage takes ints
106     ReadImageDimensionAndPixelType(mInputFilenames[0], dim_temp, pixeltype,comp_temp);
107     components=comp_temp; dim=dim_temp;
108   }
109   else {
110     if (mInputVVImages.size()) {
111       pixeltype=mInputVVImages[0]->GetScalarTypeAsString();
112       dim=mInputVVImages[0]->GetNumberOfDimensions();
113       components=mInputVVImages[0]->GetNumberOfScalarComponents();
114     }
115     else
116     assert(false); //No input image, shouldn't happen
117   }
118   if (mIOVerbose) {
119     std::cout << "Input is " << mDim << "D " << mPixelTypeName << "." << std::endl;
120   }
121 }
122 //--------------------------------------------------------------------
123
124
125 //--------------------------------------------------------------------
126 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
127 {
128     assert(mOutputVVImages.size());
129     return mOutputVVImages[0];
130 }
131 //--------------------------------------------------------------------
132
133
134 //--------------------------------------------------------------------
135 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
136 {
137     return mOutputVVImages;
138 }
139 //--------------------------------------------------------------------
140
141
142 //--------------------------------------------------------------------
143 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
144 {
145     mInputVVImages.clear();
146     mInputVVImages.push_back(input);
147 }
148 //--------------------------------------------------------------------
149
150
151 //--------------------------------------------------------------------
152 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
153 {
154     mInputVVImages.push_back(input);
155 }
156 //--------------------------------------------------------------------
157
158
159 //--------------------------------------------------------------------
160 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
161 {
162     mInputVVImages=input;
163 }
164 //--------------------------------------------------------------------
165
166
167 //--------------------------------------------------------------------
168 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes() {
169   std::cout << GetAvailableImageTypes();
170 }
171 //--------------------------------------------------------------------
172
173
174
175 //--------------------------------------------------------------------
176 void clitk::ImageToImageGenericFilterBase::ImageTypeError() {
177   std::cerr << "**Error** The filter <" << mFilterName << "> is not available for " 
178             << mDim << "D images with pixel=" 
179             << mPixelTypeName << " and "
180             << mNbOfComponents << " component." << std::endl;
181   std::cerr << GetAvailableImageTypes();
182   exit(0);
183 }
184 //--------------------------------------------------------------------
185
186
187 //--------------------------------------------------------------------
188 void clitk::ImageToImageGenericFilterBase::SetImageTypeError() {
189   std::cerr << "TODO ! " << std::endl;
190   exit(0);
191 }
192 //--------------------------------------------------------------------
193
194
195 //--------------------------------------------------------------------
196 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName() { 
197   return mFilterName; 
198 }
199 //--------------------------------------------------------------------
200
201
202 //--------------------------------------------------------------------
203 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n) { 
204   mFilterName = n; 
205 }
206 //--------------------------------------------------------------------
207
208
209 //--------------------------------------------------------------------
210 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b) { 
211   mIOVerbose = b; 
212 }
213 //--------------------------------------------------------------------
214
215 #define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \
216   template \
217 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, Dim> >(itk::Image<PixelType,Dim>::Pointer output); \
218   template \
219    itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
220
221 #define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim)   \
222   template \
223   void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<float, Compo>, Dim> >(itk::Image<itk::Vector<float, Compo>,Dim>::Pointer output); \
224   template \
225   itk::Image<itk::Vector<float,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<float, Compo>, Dim> >(unsigned int n);
226
227 DEF_SetNextOutput_And_GetInput(char, 2);
228 DEF_SetNextOutput_And_GetInput(unsigned char, 2);
229 DEF_SetNextOutput_And_GetInput(short, 2);
230 DEF_SetNextOutput_And_GetInput(unsigned short, 2);
231 DEF_SetNextOutput_And_GetInput(int, 2);
232 DEF_SetNextOutput_And_GetInput(float, 2);
233 DEF_SetNextOutput_And_GetInput(double, 2);
234
235 DEF_SetNextOutput_And_GetInput(char, 3);
236 DEF_SetNextOutput_And_GetInput(unsigned char, 3);
237 DEF_SetNextOutput_And_GetInput(short, 3);
238 DEF_SetNextOutput_And_GetInput(unsigned short, 3);
239 DEF_SetNextOutput_And_GetInput(int, 3);
240 DEF_SetNextOutput_And_GetInput(float, 3);
241 DEF_SetNextOutput_And_GetInput(double, 3);
242
243 DEF_SetNextOutput_And_GetInput_WithCompo(2, 2);
244 DEF_SetNextOutput_And_GetInput_WithCompo(2, 3);
245 DEF_SetNextOutput_And_GetInput_WithCompo(2, 4);
246 DEF_SetNextOutput_And_GetInput_WithCompo(3, 2);
247 DEF_SetNextOutput_And_GetInput_WithCompo(3, 3);
248 DEF_SetNextOutput_And_GetInput_WithCompo(3, 4);
249 DEF_SetNextOutput_And_GetInput_WithCompo(4, 2);
250 DEF_SetNextOutput_And_GetInput_WithCompo(4, 3);
251 DEF_SetNextOutput_And_GetInput_WithCompo(4, 4);
252
253 DEF_SetNextOutput_And_GetInput(char, 4);
254 DEF_SetNextOutput_And_GetInput(unsigned char, 4);
255 DEF_SetNextOutput_And_GetInput(short, 4);
256 DEF_SetNextOutput_And_GetInput(unsigned short, 4);
257 DEF_SetNextOutput_And_GetInput(int, 4);
258 DEF_SetNextOutput_And_GetInput(float, 4);
259 DEF_SetNextOutput_And_GetInput(double, 4);
260
261
262 //--------------------------------------------------------------------
263 template<class ImageType> 
264 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output) {
265   if (mOutputFilenames.size())
266     {
267       clitk::writeImage<ImageType>(output, mOutputFilenames.front(), mIOVerbose);
268       mOutputFilenames.pop_front();
269     }
270   if (mInputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
271     mOutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
272 }
273 //--------------------------------------------------------------------
274
275
276 //--------------------------------------------------------------------
277 template<class ImageType> 
278 typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n) {
279   if (mReadOnDisk && mInputFilenames.size() > n) {
280     return clitk::readImage<ImageType>(mInputFilenames[n], mIOVerbose);
281   }
282   else {
283     if (mInputVVImages.size() > n)
284       return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(mInputVVImages[n]).GetPointer()));
285     else
286       {
287         assert(false); //No input, this shouldn't happen
288         return typename ImageType::Pointer(NULL);
289       }
290   }
291 }
292 //--------------------------------------------------------------------
293
294
295