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"
25 //--------------------------------------------------------------------
26 clitk::ImageToImageGenericFilterBase::~ImageToImageGenericFilterBase() {}
27 //--------------------------------------------------------------------
30 //--------------------------------------------------------------------
31 clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string n)
35 m_FailOnImageTypeError = true;
40 //--------------------------------------------------------------------
43 //--------------------------------------------------------------------
44 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames)
46 m_InputFilenames = filenames;
48 //--------------------------------------------------------------------
51 //--------------------------------------------------------------------
52 void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b)
56 //--------------------------------------------------------------------
59 //--------------------------------------------------------------------
60 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename)
62 std::vector<std::string> f;
63 f.push_back(filename);
66 //--------------------------------------------------------------------
69 //--------------------------------------------------------------------
70 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename)
72 m_InputFilenames.push_back(filename);
74 //--------------------------------------------------------------------
77 //--------------------------------------------------------------------
78 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename)
80 m_OutputFilenames.clear();
81 m_OutputFilenames.push_back(filename);
83 //--------------------------------------------------------------------
86 //--------------------------------------------------------------------
87 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string & filename)
89 m_OutputFilenames.push_back(filename);
91 //--------------------------------------------------------------------
94 //--------------------------------------------------------------------
95 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
97 m_OutputFilenames.clear();
98 std::copy(filenames.begin(),filenames.end(),m_OutputFilenames.begin());
100 //--------------------------------------------------------------------
103 //--------------------------------------------------------------------
104 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
106 assert(m_OutputFilenames.size() == 1);
107 return m_OutputFilenames.front();
109 //--------------------------------------------------------------------
112 //--------------------------------------------------------------------
113 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
114 std::string& pixeltype,unsigned int& components)
116 if (m_ReadOnDisk && m_InputFilenames.size()) {
117 int comp_temp,dim_temp; //clitkCommonImage takes ints
118 ReadImageDimensionAndPixelType(m_InputFilenames[0], dim_temp, pixeltype,comp_temp);
119 components=comp_temp;
122 if (m_InputVVImages.size()) {
123 pixeltype = m_InputVVImages[0]->GetScalarTypeAsITKString();
124 dim = m_InputVVImages[0]->GetNumberOfDimensions();
125 components = m_InputVVImages[0]->GetNumberOfScalarComponents();
127 assert(false); //No input image, shouldn't happen
130 std::cout << "Input is " << m_Dim << "D " << m_PixelTypeName << "." << std::endl;
133 //--------------------------------------------------------------------
136 //--------------------------------------------------------------------
137 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
139 assert(m_OutputVVImages.size());
140 return m_OutputVVImages[0];
142 //--------------------------------------------------------------------
145 //--------------------------------------------------------------------
146 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
148 return m_OutputVVImages;
150 //--------------------------------------------------------------------
153 //--------------------------------------------------------------------
154 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
156 m_InputVVImages.clear();
157 m_InputVVImages.push_back(input);
159 //--------------------------------------------------------------------
162 //--------------------------------------------------------------------
163 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
165 m_InputVVImages.push_back(input);
167 //--------------------------------------------------------------------
170 //--------------------------------------------------------------------
171 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
173 m_InputVVImages=input;
175 //--------------------------------------------------------------------
178 //--------------------------------------------------------------------
179 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes()
181 std::cout << GetAvailableImageTypes();
183 //--------------------------------------------------------------------
187 //--------------------------------------------------------------------
188 void clitk::ImageToImageGenericFilterBase::ImageTypeError()
190 std::cerr << "**Error** The filter <" << m_FilterName << "> is not available for "
191 << m_Dim << "D images with pixel="
192 << m_PixelTypeName << " and "
193 << m_NbOfComponents << " component." << std::endl;
194 std::cerr << GetAvailableImageTypes();
197 //--------------------------------------------------------------------
200 //--------------------------------------------------------------------
201 void clitk::ImageToImageGenericFilterBase::SetImageTypeError()
203 std::cerr << "TODO ! " << std::endl;
206 //--------------------------------------------------------------------
209 //--------------------------------------------------------------------
210 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName()
214 //--------------------------------------------------------------------
217 //--------------------------------------------------------------------
218 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n)
222 //--------------------------------------------------------------------
225 //--------------------------------------------------------------------
226 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b)
230 //--------------------------------------------------------------------
232 #define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \
234 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, Dim> >(itk::Image<PixelType,Dim>::Pointer output); \
236 itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
238 #define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim) \
240 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<float, Compo>, Dim> >(itk::Image<itk::Vector<float, Compo>,Dim>::Pointer output); \
242 itk::Image<itk::Vector<float,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<float, Compo>, Dim> >(unsigned int n);
244 DEF_SetNextOutput_And_GetInput(char, 2);
245 DEF_SetNextOutput_And_GetInput(unsigned char, 2);
246 DEF_SetNextOutput_And_GetInput(short, 2);
247 DEF_SetNextOutput_And_GetInput(unsigned short, 2);
248 DEF_SetNextOutput_And_GetInput(int, 2);
249 DEF_SetNextOutput_And_GetInput(float, 2);
250 DEF_SetNextOutput_And_GetInput(double, 2);
252 DEF_SetNextOutput_And_GetInput(char, 3);
253 DEF_SetNextOutput_And_GetInput(unsigned char, 3);
254 DEF_SetNextOutput_And_GetInput(short, 3);
255 DEF_SetNextOutput_And_GetInput(unsigned short, 3);
256 DEF_SetNextOutput_And_GetInput(int, 3);
257 DEF_SetNextOutput_And_GetInput(float, 3);
258 DEF_SetNextOutput_And_GetInput(double, 3);
260 DEF_SetNextOutput_And_GetInput_WithCompo(2, 2);
261 DEF_SetNextOutput_And_GetInput_WithCompo(2, 3);
262 DEF_SetNextOutput_And_GetInput_WithCompo(2, 4);
263 DEF_SetNextOutput_And_GetInput_WithCompo(3, 2);
264 DEF_SetNextOutput_And_GetInput_WithCompo(3, 3);
265 DEF_SetNextOutput_And_GetInput_WithCompo(3, 4);
266 DEF_SetNextOutput_And_GetInput_WithCompo(4, 2);
267 DEF_SetNextOutput_And_GetInput_WithCompo(4, 3);
268 DEF_SetNextOutput_And_GetInput_WithCompo(4, 4);
270 DEF_SetNextOutput_And_GetInput(char, 4);
271 DEF_SetNextOutput_And_GetInput(unsigned char, 4);
272 DEF_SetNextOutput_And_GetInput(short, 4);
273 DEF_SetNextOutput_And_GetInput(unsigned short, 4);
274 DEF_SetNextOutput_And_GetInput(int, 4);
275 DEF_SetNextOutput_And_GetInput(float, 4);
276 DEF_SetNextOutput_And_GetInput(double, 4);
279 //--------------------------------------------------------------------
280 template<class ImageType>
281 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output)
283 if (m_OutputFilenames.size()) {
284 clitk::writeImage<ImageType>(output, m_OutputFilenames.front(), m_IOVerbose);
285 m_OutputFilenames.pop_front();
287 if (m_InputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
288 m_OutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
290 //--------------------------------------------------------------------
293 //--------------------------------------------------------------------
294 template<class ImageType>
295 typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n)
297 if (m_ReadOnDisk && m_InputFilenames.size() > n) {
298 return clitk::readImage<ImageType>(m_InputFilenames[n], m_IOVerbose);
300 if (m_InputVVImages.size() > n)
301 return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(m_InputVVImages[n]).GetPointer()));
303 assert(false); //No input, this shouldn't happen
304 return typename ImageType::Pointer(NULL);
308 //--------------------------------------------------------------------
311 //--------------------------------------------------------------------
312 void clitk::ImageToImageGenericFilterBase::MustStop()
314 if (m_FilterBase != NULL) {
315 m_FilterBase->SetMustStop(true);
318 //--------------------------------------------------------------------