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