1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
20 #include "clitkImageToImageGenericFilterBase.h"
21 #include "clitkExceptionObject.h"
26 //--------------------------------------------------------------------
27 clitk::ImageToImageGenericFilterBase::~ImageToImageGenericFilterBase() {}
28 //--------------------------------------------------------------------
31 //--------------------------------------------------------------------
32 clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string n)
36 m_FailOnImageTypeError = true;
39 m_WriteCompression = false;
44 //--------------------------------------------------------------------
47 //--------------------------------------------------------------------
48 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames)
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);
57 //--------------------------------------------------------------------
60 //--------------------------------------------------------------------
61 void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b)
65 //--------------------------------------------------------------------
68 //--------------------------------------------------------------------
69 void clitk::ImageToImageGenericFilterBase::EnableWriteOnDisk(bool b)
73 //--------------------------------------------------------------------
76 //--------------------------------------------------------------------
77 void clitk::ImageToImageGenericFilterBase::EnableWriteCompression(bool b)
79 m_WriteCompression = b;
81 //--------------------------------------------------------------------
84 //--------------------------------------------------------------------
85 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename)
87 std::vector<std::string> f;
88 f.push_back(filename);
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);
96 //--------------------------------------------------------------------
99 //--------------------------------------------------------------------
100 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename)
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);
109 //--------------------------------------------------------------------
112 //--------------------------------------------------------------------
113 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename)
115 m_OutputFilenames.clear();
116 m_OutputFilenames.push_back(filename);
118 //--------------------------------------------------------------------
121 //--------------------------------------------------------------------
122 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string filename)
124 m_OutputFilenames.push_back(filename);
126 //--------------------------------------------------------------------
129 //--------------------------------------------------------------------
130 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
132 m_OutputFilenames.clear();
133 std::copy(filenames.begin(),filenames.end(),m_OutputFilenames.begin());
135 //--------------------------------------------------------------------
138 //--------------------------------------------------------------------
139 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
141 assert(m_OutputFilenames.size() == 1);
142 return m_OutputFilenames.front();
144 //--------------------------------------------------------------------
147 //--------------------------------------------------------------------
148 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
149 std::string& pixeltype,unsigned int& components)
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;
157 if (m_InputVVImages.size()) {
158 pixeltype = m_InputVVImages[0]->GetScalarTypeAsITKString();
159 dim = m_InputVVImages[0]->GetNumberOfDimensions();
160 components = m_InputVVImages[0]->GetNumberOfScalarComponents();
162 clitkExceptionMacro("No input given in this ImageToImageGenericFilter.");
163 assert(false); //No input image, shouldn't happen
167 std::cout << "Input is " << m_Dim << "D " << m_PixelTypeName << "." << std::endl;
170 //--------------------------------------------------------------------
173 //--------------------------------------------------------------------
174 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
176 assert(m_OutputVVImages.size());
177 return m_OutputVVImages[0];
179 //--------------------------------------------------------------------
182 //--------------------------------------------------------------------
183 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
185 return m_OutputVVImages;
187 //--------------------------------------------------------------------
190 //--------------------------------------------------------------------
191 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
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);
201 //--------------------------------------------------------------------
204 //--------------------------------------------------------------------
205 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
207 m_InputVVImages.push_back(input);
209 //--------------------------------------------------------------------
212 //--------------------------------------------------------------------
213 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
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);
222 //--------------------------------------------------------------------
225 //--------------------------------------------------------------------
226 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes()
228 std::cout << GetAvailableImageTypes();
230 //--------------------------------------------------------------------
234 //--------------------------------------------------------------------
235 void clitk::ImageToImageGenericFilterBase::ImageTypeError()
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());
246 //--------------------------------------------------------------------
249 //--------------------------------------------------------------------
250 void clitk::ImageToImageGenericFilterBase::SetImageTypeError()
252 std::cerr << "TODO ! " << std::endl;
255 //--------------------------------------------------------------------
258 //--------------------------------------------------------------------
259 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName()
263 //--------------------------------------------------------------------
266 //--------------------------------------------------------------------
267 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n)
271 //--------------------------------------------------------------------
274 //--------------------------------------------------------------------
275 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b)
279 //--------------------------------------------------------------------
281 #define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \
283 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, Dim> >(itk::Image<PixelType,Dim>::Pointer output); \
285 itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
287 #define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim) \
289 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<float, Compo>, Dim> >(itk::Image<itk::Vector<float, Compo>,Dim>::Pointer output); \
291 itk::Image<itk::Vector<float,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<float, Compo>, Dim> >(unsigned int n);
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);
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);
309 DEF_SetNextOutput_And_GetInput_WithCompo(2, 2);
310 DEF_SetNextOutput_And_GetInput_WithCompo(2, 3);
311 DEF_SetNextOutput_And_GetInput_WithCompo(2, 4);
312 DEF_SetNextOutput_And_GetInput_WithCompo(3, 2);
313 DEF_SetNextOutput_And_GetInput_WithCompo(3, 3);
314 DEF_SetNextOutput_And_GetInput_WithCompo(3, 4);
315 DEF_SetNextOutput_And_GetInput_WithCompo(4, 2);
316 DEF_SetNextOutput_And_GetInput_WithCompo(4, 3);
317 DEF_SetNextOutput_And_GetInput_WithCompo(4, 4);
319 DEF_SetNextOutput_And_GetInput(char, 4);
320 DEF_SetNextOutput_And_GetInput(unsigned char, 4);
321 DEF_SetNextOutput_And_GetInput(short, 4);
322 DEF_SetNextOutput_And_GetInput(unsigned short, 4);
323 DEF_SetNextOutput_And_GetInput(int, 4);
324 DEF_SetNextOutput_And_GetInput(float, 4);
325 DEF_SetNextOutput_And_GetInput(double, 4);
328 //--------------------------------------------------------------------
329 template<class ImageType>
330 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output)
332 if (m_WriteOnDisk && m_OutputFilenames.size()) {
333 clitk::writeImage<ImageType>(output, m_OutputFilenames.front(), m_IOVerbose, m_WriteCompression);
334 m_OutputFilenames.pop_front();
336 if (m_InputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
337 m_OutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
339 //--------------------------------------------------------------------
342 //--------------------------------------------------------------------
343 template<class ImageType>
344 typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n)
346 if (m_ReadOnDisk && m_InputFilenames.size() > n) {
347 return clitk::readImage<ImageType>(m_InputFilenames[n], m_IOVerbose);
349 if (m_InputVVImages.size() > n)
350 return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(m_InputVVImages[n]).GetPointer()));
352 assert(false); //No input, this shouldn't happen
353 return typename ImageType::Pointer(NULL);
357 //--------------------------------------------------------------------
360 //--------------------------------------------------------------------
361 // void clitk::ImageToImageGenericFilterBase::MustStop()
363 // if (m_FilterBase != NULL) {
364 // m_FilterBase->SetMustStop(true);
367 //--------------------------------------------------------------------
370 //--------------------------------------------------------------------
371 void clitk::ImageToImageGenericFilterBase::DeleteLastOutputImage()
373 if (m_OutputVVImages.size()>1) {
374 m_OutputVVImages.pop_back();
377 //--------------------------------------------------------------------
380 //--------------------------------------------------------------------