]> Creatis software - clitk.git/blob - common/clitkImageToImageGenericFilterBase.cxx
cosmetic for .ggo
[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://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
19 // clitk
20 #include "clitkImageToImageGenericFilterBase.h"
21 #include "clitkExceptionObject.h"
22
23 // itk
24 #include <itkImage.h>
25
26 //--------------------------------------------------------------------
27 clitk::ImageToImageGenericFilterBase::~ImageToImageGenericFilterBase() {}
28 //--------------------------------------------------------------------
29
30
31 //--------------------------------------------------------------------
32 clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string n)
33   :m_IOVerbose(false)
34 {
35   m_FilterName = n;
36   m_FailOnImageTypeError = true;
37   m_ReadOnDisk = true;
38   m_WriteOnDisk = true;
39   m_WriteCompression = false;
40   // m_LastError = "";
41   // StopOnErrorOn();
42   SetFilterBase(NULL);
43 }
44 //--------------------------------------------------------------------
45
46
47 //--------------------------------------------------------------------
48 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames)
49 {
50   m_InputFilenames = filenames;
51   // in this case, assume by default that we DO want to write/read on
52   // disk (rather a command line tool, but not a vvTool. Can be
53   // changed with EnableReadOnDisk and EnableWriteOnDisk)
54   EnableReadOnDisk(true);
55   EnableWriteOnDisk(true);
56 }
57 //--------------------------------------------------------------------
58
59
60 //--------------------------------------------------------------------
61 void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b)
62 {
63   m_ReadOnDisk = b;
64 }
65 //--------------------------------------------------------------------
66
67
68 //--------------------------------------------------------------------
69 void clitk::ImageToImageGenericFilterBase::EnableWriteOnDisk(bool b)
70 {
71   m_WriteOnDisk = b;
72 }
73 //--------------------------------------------------------------------
74
75
76 //--------------------------------------------------------------------
77 void clitk::ImageToImageGenericFilterBase::EnableWriteCompression(bool b)
78 {
79   m_WriteCompression = b;
80 }
81 //--------------------------------------------------------------------
82
83
84 //--------------------------------------------------------------------
85 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename)
86 {
87   std::vector<std::string> f;
88   f.push_back(filename);
89   SetInputFilenames(f);
90   // in this case, assume by default that we DO want to write/read on
91   // disk (rather a command line tool, but not a vvTool. Can be
92   // changed with EnableReadOnDisk and EnableWriteOnDisk)
93   EnableReadOnDisk(true);
94   EnableWriteOnDisk(true);
95 }
96 //--------------------------------------------------------------------
97
98
99 //--------------------------------------------------------------------
100 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename)
101 {
102   m_InputFilenames.push_back(filename);
103   // in this case, assume by default that we DO want to write/read on
104   // disk (rather a command line tool, but not a vvTool. Can be
105   // changed with EnableReadOnDisk and EnableWriteOnDisk)
106   EnableReadOnDisk(true);
107   EnableWriteOnDisk(true);
108 }
109 //--------------------------------------------------------------------
110
111
112 //--------------------------------------------------------------------
113 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename)
114 {
115   m_OutputFilenames.clear();
116   m_OutputFilenames.push_back(filename);
117 }
118 //--------------------------------------------------------------------
119
120
121 //--------------------------------------------------------------------
122 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string filename)
123 {
124   m_OutputFilenames.push_back(filename);
125 }
126 //--------------------------------------------------------------------
127
128
129 //--------------------------------------------------------------------
130 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
131 {
132   m_OutputFilenames.clear();
133   std::copy(filenames.begin(),filenames.end(),m_OutputFilenames.begin());
134 }
135 //--------------------------------------------------------------------
136
137
138 //--------------------------------------------------------------------
139 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
140 {
141   assert(m_OutputFilenames.size() == 1);
142   return m_OutputFilenames.front();
143 }
144 //--------------------------------------------------------------------
145
146
147 //--------------------------------------------------------------------
148 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
149     std::string& pixeltype,unsigned int& components)
150 {
151   if (m_ReadOnDisk && m_InputFilenames.size()) {
152     int comp_temp,dim_temp; //clitkCommonImage takes ints
153     ReadImageDimensionAndPixelType(m_InputFilenames[0], dim_temp, pixeltype,comp_temp);
154     components=comp_temp;
155     dim=dim_temp;
156   } else {
157     if (m_InputVVImages.size()) {
158       pixeltype = m_InputVVImages[0]->GetScalarTypeAsITKString();
159       dim = m_InputVVImages[0]->GetNumberOfDimensions();
160       components = m_InputVVImages[0]->GetNumberOfScalarComponents();
161     } else {
162       clitkExceptionMacro("No input given in this ImageToImageGenericFilter.");
163       assert(false); //No input image, shouldn't happen
164     }
165   }
166   if (m_IOVerbose) {
167     std::cout << "Input is " << m_Dim << "D " << m_PixelTypeName << "." << std::endl;
168   }
169 }
170 //--------------------------------------------------------------------
171
172
173 //--------------------------------------------------------------------
174 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
175 {
176   assert(m_OutputVVImages.size());
177   return m_OutputVVImages[0];
178 }
179 //--------------------------------------------------------------------
180
181
182 //--------------------------------------------------------------------
183 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
184 {
185   return m_OutputVVImages;
186 }
187 //--------------------------------------------------------------------
188
189
190 //--------------------------------------------------------------------
191 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
192 {
193   m_InputVVImages.clear();
194   m_InputVVImages.push_back(input);
195   // in this case, assume by default that we do not want to write/read
196   // on disk (not a command line tool, but rather a vvTool. Can be
197   // changed with EnableReadOnDisk and EnableWriteOnDisk)
198   EnableReadOnDisk(false);
199   EnableWriteOnDisk(false);
200 }
201 //--------------------------------------------------------------------
202
203
204 //--------------------------------------------------------------------
205 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
206 {
207   m_InputVVImages.push_back(input);
208 }
209 //--------------------------------------------------------------------
210
211
212 //--------------------------------------------------------------------
213 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
214 {
215   m_InputVVImages=input;
216   // in this case, assume by default that we do not want to write/read
217   // on disk (not a command line tool, but rather a vvTool. Can be
218   // changed with EnableReadOnDisk and EnableWriteOnDisk)
219   EnableReadOnDisk(false);
220   EnableWriteOnDisk(false);
221 }
222 //--------------------------------------------------------------------
223
224
225 //--------------------------------------------------------------------
226 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes()
227 {
228   std::cout << GetAvailableImageTypes();
229 }
230 //--------------------------------------------------------------------
231
232
233
234 //--------------------------------------------------------------------
235 void clitk::ImageToImageGenericFilterBase::ImageTypeError()
236 {
237   std::ostringstream os;
238   os << "**Error** The filter <" << m_FilterName << "> is not available for "
239      << m_Dim << "D images with pixel="
240      << m_PixelTypeName << " and "
241      << m_NbOfComponents << " component." << std::endl;
242   os << GetAvailableImageTypes();
243   clitkExceptionMacro(os.str());
244   //  exit(0);
245 }
246 //--------------------------------------------------------------------
247
248
249 //--------------------------------------------------------------------
250 void clitk::ImageToImageGenericFilterBase::SetImageTypeError()
251 {
252   std::cerr << "TODO ! " << std::endl;
253   exit(0);
254 }
255 //--------------------------------------------------------------------
256
257
258 //--------------------------------------------------------------------
259 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName()
260 {
261   return m_FilterName;
262 }
263 //--------------------------------------------------------------------
264
265
266 //--------------------------------------------------------------------
267 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n)
268 {
269   m_FilterName = n;
270 }
271 //--------------------------------------------------------------------
272
273
274 //--------------------------------------------------------------------
275 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b)
276 {
277   m_IOVerbose = b;
278 }
279 //--------------------------------------------------------------------
280
281 #define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \
282   template \
283 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, Dim> >(itk::Image<PixelType,Dim>::Pointer output); \
284   template \
285    itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
286
287 #define DEF_SetNextOutput_And_GetInput_WithCompo(PixelType, Compo, Dim)   \
288   template \
289   void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<PixelType, Compo>, Dim> >(itk::Image<itk::Vector<PixelType, Compo>,Dim>::Pointer output); \
290   template \
291   itk::Image<itk::Vector<PixelType,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<PixelType, Compo>, Dim> >(unsigned int n);
292
293 DEF_SetNextOutput_And_GetInput(char, 2);
294 DEF_SetNextOutput_And_GetInput(unsigned char, 2);
295 DEF_SetNextOutput_And_GetInput(short, 2);
296 DEF_SetNextOutput_And_GetInput(unsigned short, 2);
297 DEF_SetNextOutput_And_GetInput(int, 2);
298 DEF_SetNextOutput_And_GetInput(float, 2);
299 DEF_SetNextOutput_And_GetInput(double, 2);
300
301 DEF_SetNextOutput_And_GetInput(char, 3);
302 DEF_SetNextOutput_And_GetInput(unsigned char, 3);
303 DEF_SetNextOutput_And_GetInput(short, 3);
304 DEF_SetNextOutput_And_GetInput(unsigned short, 3);
305 DEF_SetNextOutput_And_GetInput(int, 3);
306 DEF_SetNextOutput_And_GetInput(float, 3);
307 DEF_SetNextOutput_And_GetInput(double, 3);
308
309 /*
310 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 2, 2);
311 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 2, 3);
312 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 2, 4);
313 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 3, 2);
314 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 3, 3);
315 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 3, 4);
316 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 4, 2);
317 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 4, 3);
318 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 4, 4);
319
320 DEF_SetNextOutput_And_GetInput_WithCompo(char, 2, 2);
321 DEF_SetNextOutput_And_GetInput_WithCompo(char, 2, 3);
322 DEF_SetNextOutput_And_GetInput_WithCompo(char, 2, 4);
323 DEF_SetNextOutput_And_GetInput_WithCompo(char, 3, 2);
324 DEF_SetNextOutput_And_GetInput_WithCompo(char, 3, 3);
325 DEF_SetNextOutput_And_GetInput_WithCompo(char, 3, 4);
326 DEF_SetNextOutput_And_GetInput_WithCompo(char, 4, 2);
327 DEF_SetNextOutput_And_GetInput_WithCompo(char, 4, 3);
328 DEF_SetNextOutput_And_GetInput_WithCompo(char, 4, 4);
329
330 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 2, 2);
331 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 2, 3);
332 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 2, 4);
333 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 3, 2);
334 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 3, 3);
335 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 3, 4);
336 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 4, 2);
337 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 4, 3);
338 DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 4, 4);
339
340 DEF_SetNextOutput_And_GetInput_WithCompo(short, 2, 2);
341 DEF_SetNextOutput_And_GetInput_WithCompo(short, 2, 3);
342 DEF_SetNextOutput_And_GetInput_WithCompo(short, 2, 4);
343 DEF_SetNextOutput_And_GetInput_WithCompo(short, 3, 2);
344 DEF_SetNextOutput_And_GetInput_WithCompo(short, 3, 3);
345 DEF_SetNextOutput_And_GetInput_WithCompo(short, 3, 4);
346 DEF_SetNextOutput_And_GetInput_WithCompo(short, 4, 2);
347 DEF_SetNextOutput_And_GetInput_WithCompo(short, 4, 3);
348 DEF_SetNextOutput_And_GetInput_WithCompo(short, 4, 4);
349
350 DEF_SetNextOutput_And_GetInput_WithCompo(int, 2, 2);
351 DEF_SetNextOutput_And_GetInput_WithCompo(int, 2, 3);
352 DEF_SetNextOutput_And_GetInput_WithCompo(int, 2, 4);
353 DEF_SetNextOutput_And_GetInput_WithCompo(int, 3, 2);
354 DEF_SetNextOutput_And_GetInput_WithCompo(int, 3, 3);
355 DEF_SetNextOutput_And_GetInput_WithCompo(int, 3, 4);
356 DEF_SetNextOutput_And_GetInput_WithCompo(int, 4, 2);
357 DEF_SetNextOutput_And_GetInput_WithCompo(int, 4, 3);
358 DEF_SetNextOutput_And_GetInput_WithCompo(int, 4, 4);
359 */
360
361 DEF_SetNextOutput_And_GetInput_WithCompo(float, 2, 2);
362 DEF_SetNextOutput_And_GetInput_WithCompo(float, 2, 3);
363 DEF_SetNextOutput_And_GetInput_WithCompo(float, 2, 4);
364 DEF_SetNextOutput_And_GetInput_WithCompo(float, 3, 2);
365 DEF_SetNextOutput_And_GetInput_WithCompo(float, 3, 3);
366 DEF_SetNextOutput_And_GetInput_WithCompo(float, 3, 4);
367 DEF_SetNextOutput_And_GetInput_WithCompo(float, 4, 2);
368 DEF_SetNextOutput_And_GetInput_WithCompo(float, 4, 3);
369 DEF_SetNextOutput_And_GetInput_WithCompo(float, 4, 4);
370
371 DEF_SetNextOutput_And_GetInput_WithCompo(double, 2, 2);
372 DEF_SetNextOutput_And_GetInput_WithCompo(double, 2, 3);
373 DEF_SetNextOutput_And_GetInput_WithCompo(double, 2, 4);
374 DEF_SetNextOutput_And_GetInput_WithCompo(double, 3, 2);
375 DEF_SetNextOutput_And_GetInput_WithCompo(double, 3, 3);
376 DEF_SetNextOutput_And_GetInput_WithCompo(double, 3, 4);
377 DEF_SetNextOutput_And_GetInput_WithCompo(double, 4, 2);
378 DEF_SetNextOutput_And_GetInput_WithCompo(double, 4, 3);
379 DEF_SetNextOutput_And_GetInput_WithCompo(double, 4, 4);
380
381 DEF_SetNextOutput_And_GetInput(char, 4);
382 DEF_SetNextOutput_And_GetInput(unsigned char, 4);
383 DEF_SetNextOutput_And_GetInput(short, 4);
384 DEF_SetNextOutput_And_GetInput(unsigned short, 4);
385 DEF_SetNextOutput_And_GetInput(int, 4);
386 DEF_SetNextOutput_And_GetInput(float, 4);
387 DEF_SetNextOutput_And_GetInput(double, 4);
388
389
390 //--------------------------------------------------------------------
391 template<class ImageType>
392 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output)
393 {
394   if (m_WriteOnDisk && m_OutputFilenames.size()) {
395     clitk::writeImage<ImageType>(output, m_OutputFilenames.front(), m_IOVerbose, m_WriteCompression);
396     m_OutputFilenames.pop_front();
397   }
398   if (m_InputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
399     m_OutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
400 }
401 //--------------------------------------------------------------------
402
403
404 //--------------------------------------------------------------------
405 template<class ImageType>
406 typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n)
407 {
408   if (m_ReadOnDisk && m_InputFilenames.size() > n) {
409     return clitk::readImage<ImageType>(m_InputFilenames[n], m_IOVerbose);
410   } else {
411     if (m_InputVVImages.size() > n)
412       return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(m_InputVVImages[n]).GetPointer()));
413     else {
414       assert(false); //No input, this shouldn't happen
415       return typename ImageType::Pointer((ImageType*)ITK_NULLPTR);
416     }
417   }
418 }
419 //--------------------------------------------------------------------
420
421
422 //--------------------------------------------------------------------
423 // void clitk::ImageToImageGenericFilterBase::MustStop()
424 // {
425 //   if (m_FilterBase != NULL) {
426 //     m_FilterBase->SetMustStop(true);
427 //   }
428 // }
429 //--------------------------------------------------------------------
430
431
432 //--------------------------------------------------------------------
433 void clitk::ImageToImageGenericFilterBase::DeleteLastOutputImage()
434 {
435   if (m_OutputVVImages.size()>1) {
436    m_OutputVVImages.pop_back();
437   }
438 }
439 //--------------------------------------------------------------------
440
441
442 //--------------------------------------------------------------------
443
444