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://oncora1.lyon.fnclcc.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;
43 //--------------------------------------------------------------------
46 //--------------------------------------------------------------------
47 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames)
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);
56 //--------------------------------------------------------------------
59 //--------------------------------------------------------------------
60 void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b)
64 //--------------------------------------------------------------------
67 //--------------------------------------------------------------------
68 void clitk::ImageToImageGenericFilterBase::EnableWriteOnDisk(bool b)
72 //--------------------------------------------------------------------
75 //--------------------------------------------------------------------
76 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename)
78 std::vector<std::string> f;
79 f.push_back(filename);
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);
87 //--------------------------------------------------------------------
90 //--------------------------------------------------------------------
91 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename)
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);
100 //--------------------------------------------------------------------
103 //--------------------------------------------------------------------
104 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename)
106 m_OutputFilenames.clear();
107 m_OutputFilenames.push_back(filename);
109 //--------------------------------------------------------------------
112 //--------------------------------------------------------------------
113 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string filename)
115 m_OutputFilenames.push_back(filename);
117 //--------------------------------------------------------------------
120 //--------------------------------------------------------------------
121 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
123 m_OutputFilenames.clear();
124 std::copy(filenames.begin(),filenames.end(),m_OutputFilenames.begin());
126 //--------------------------------------------------------------------
129 //--------------------------------------------------------------------
130 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
132 assert(m_OutputFilenames.size() == 1);
133 return m_OutputFilenames.front();
135 //--------------------------------------------------------------------
138 //--------------------------------------------------------------------
139 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
140 std::string& pixeltype,unsigned int& components)
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;
148 if (m_InputVVImages.size()) {
149 pixeltype = m_InputVVImages[0]->GetScalarTypeAsITKString();
150 dim = m_InputVVImages[0]->GetNumberOfDimensions();
151 components = m_InputVVImages[0]->GetNumberOfScalarComponents();
153 assert(false); //No input image, shouldn't happen
156 std::cout << "Input is " << m_Dim << "D " << m_PixelTypeName << "." << std::endl;
159 //--------------------------------------------------------------------
162 //--------------------------------------------------------------------
163 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
165 assert(m_OutputVVImages.size());
166 return m_OutputVVImages[0];
168 //--------------------------------------------------------------------
171 //--------------------------------------------------------------------
172 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
174 return m_OutputVVImages;
176 //--------------------------------------------------------------------
179 //--------------------------------------------------------------------
180 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
182 m_InputVVImages.clear();
183 m_InputVVImages.push_back(input);
184 // in this case, assume by default that we do not want to write/read
185 // on disk (not a command line tool, but rather a vvTool. Can be
186 // changed with EnableReadOnDisk and EnableWriteOnDisk)
187 EnableReadOnDisk(false);
188 EnableWriteOnDisk(false);
190 //--------------------------------------------------------------------
193 //--------------------------------------------------------------------
194 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
196 m_InputVVImages.push_back(input);
198 //--------------------------------------------------------------------
201 //--------------------------------------------------------------------
202 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
204 m_InputVVImages=input;
205 // in this case, assume by default that we do not want to write/read
206 // on disk (not a command line tool, but rather a vvTool. Can be
207 // changed with EnableReadOnDisk and EnableWriteOnDisk)
208 EnableReadOnDisk(false);
209 EnableWriteOnDisk(false);
211 //--------------------------------------------------------------------
214 //--------------------------------------------------------------------
215 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes()
217 std::cout << GetAvailableImageTypes();
219 //--------------------------------------------------------------------
223 //--------------------------------------------------------------------
224 void clitk::ImageToImageGenericFilterBase::ImageTypeError()
226 std::ostringstream os;
227 os << "**Error** The filter <" << m_FilterName << "> is not available for "
228 << m_Dim << "D images with pixel="
229 << m_PixelTypeName << " and "
230 << m_NbOfComponents << " component." << std::endl;
231 os << GetAvailableImageTypes();
232 clitkExceptionMacro(os.str());
235 //--------------------------------------------------------------------
238 //--------------------------------------------------------------------
239 void clitk::ImageToImageGenericFilterBase::SetImageTypeError()
241 std::cerr << "TODO ! " << std::endl;
244 //--------------------------------------------------------------------
247 //--------------------------------------------------------------------
248 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName()
252 //--------------------------------------------------------------------
255 //--------------------------------------------------------------------
256 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n)
260 //--------------------------------------------------------------------
263 //--------------------------------------------------------------------
264 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b)
268 //--------------------------------------------------------------------
270 #define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \
272 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, Dim> >(itk::Image<PixelType,Dim>::Pointer output); \
274 itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
276 #define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim) \
278 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<float, Compo>, Dim> >(itk::Image<itk::Vector<float, Compo>,Dim>::Pointer output); \
280 itk::Image<itk::Vector<float,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<float, Compo>, Dim> >(unsigned int n);
282 DEF_SetNextOutput_And_GetInput(char, 2);
283 DEF_SetNextOutput_And_GetInput(unsigned char, 2);
284 DEF_SetNextOutput_And_GetInput(short, 2);
285 DEF_SetNextOutput_And_GetInput(unsigned short, 2);
286 DEF_SetNextOutput_And_GetInput(int, 2);
287 DEF_SetNextOutput_And_GetInput(float, 2);
288 DEF_SetNextOutput_And_GetInput(double, 2);
290 DEF_SetNextOutput_And_GetInput(char, 3);
291 DEF_SetNextOutput_And_GetInput(unsigned char, 3);
292 DEF_SetNextOutput_And_GetInput(short, 3);
293 DEF_SetNextOutput_And_GetInput(unsigned short, 3);
294 DEF_SetNextOutput_And_GetInput(int, 3);
295 DEF_SetNextOutput_And_GetInput(float, 3);
296 DEF_SetNextOutput_And_GetInput(double, 3);
298 DEF_SetNextOutput_And_GetInput_WithCompo(2, 2);
299 DEF_SetNextOutput_And_GetInput_WithCompo(2, 3);
300 DEF_SetNextOutput_And_GetInput_WithCompo(2, 4);
301 DEF_SetNextOutput_And_GetInput_WithCompo(3, 2);
302 DEF_SetNextOutput_And_GetInput_WithCompo(3, 3);
303 DEF_SetNextOutput_And_GetInput_WithCompo(3, 4);
304 DEF_SetNextOutput_And_GetInput_WithCompo(4, 2);
305 DEF_SetNextOutput_And_GetInput_WithCompo(4, 3);
306 DEF_SetNextOutput_And_GetInput_WithCompo(4, 4);
308 DEF_SetNextOutput_And_GetInput(char, 4);
309 DEF_SetNextOutput_And_GetInput(unsigned char, 4);
310 DEF_SetNextOutput_And_GetInput(short, 4);
311 DEF_SetNextOutput_And_GetInput(unsigned short, 4);
312 DEF_SetNextOutput_And_GetInput(int, 4);
313 DEF_SetNextOutput_And_GetInput(float, 4);
314 DEF_SetNextOutput_And_GetInput(double, 4);
317 //--------------------------------------------------------------------
318 template<class ImageType>
319 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output)
321 if (m_WriteOnDisk && m_OutputFilenames.size()) {
322 clitk::writeImage<ImageType>(output, m_OutputFilenames.front(), m_IOVerbose);
323 m_OutputFilenames.pop_front();
325 if (m_InputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
326 m_OutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
328 //--------------------------------------------------------------------
331 //--------------------------------------------------------------------
332 template<class ImageType>
333 typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n)
335 if (m_ReadOnDisk && m_InputFilenames.size() > n) {
336 return clitk::readImage<ImageType>(m_InputFilenames[n], m_IOVerbose);
338 if (m_InputVVImages.size() > n)
339 return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(m_InputVVImages[n]).GetPointer()));
341 assert(false); //No input, this shouldn't happen
342 return typename ImageType::Pointer(NULL);
346 //--------------------------------------------------------------------
349 //--------------------------------------------------------------------
350 // void clitk::ImageToImageGenericFilterBase::MustStop()
352 // if (m_FilterBase != NULL) {
353 // m_FilterBase->SetMustStop(true);
356 //--------------------------------------------------------------------
359 //--------------------------------------------------------------------
360 void clitk::ImageToImageGenericFilterBase::DeleteLastOutputImage()
362 if (m_OutputVVImages.size()>1) {
363 m_OutputVVImages.pop_back();
366 //--------------------------------------------------------------------
369 //--------------------------------------------------------------------