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;
41 //--------------------------------------------------------------------
44 //--------------------------------------------------------------------
45 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames)
47 m_InputFilenames = filenames;
49 //--------------------------------------------------------------------
52 //--------------------------------------------------------------------
53 void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b)
57 //--------------------------------------------------------------------
60 //--------------------------------------------------------------------
61 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename)
63 std::vector<std::string> f;
64 f.push_back(filename);
67 //--------------------------------------------------------------------
70 //--------------------------------------------------------------------
71 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename)
73 m_InputFilenames.push_back(filename);
75 //--------------------------------------------------------------------
78 //--------------------------------------------------------------------
79 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename)
81 m_OutputFilenames.clear();
82 m_OutputFilenames.push_back(filename);
84 //--------------------------------------------------------------------
87 //--------------------------------------------------------------------
88 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string & filename)
90 m_OutputFilenames.push_back(filename);
92 //--------------------------------------------------------------------
95 //--------------------------------------------------------------------
96 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
98 m_OutputFilenames.clear();
99 std::copy(filenames.begin(),filenames.end(),m_OutputFilenames.begin());
101 //--------------------------------------------------------------------
104 //--------------------------------------------------------------------
105 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
107 assert(m_OutputFilenames.size() == 1);
108 return m_OutputFilenames.front();
110 //--------------------------------------------------------------------
113 //--------------------------------------------------------------------
114 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
115 std::string& pixeltype,unsigned int& components)
117 if (m_ReadOnDisk && m_InputFilenames.size()) {
118 int comp_temp,dim_temp; //clitkCommonImage takes ints
119 ReadImageDimensionAndPixelType(m_InputFilenames[0], dim_temp, pixeltype,comp_temp);
120 components=comp_temp;
123 if (m_InputVVImages.size()) {
124 pixeltype = m_InputVVImages[0]->GetScalarTypeAsITKString();
125 dim = m_InputVVImages[0]->GetNumberOfDimensions();
126 components = m_InputVVImages[0]->GetNumberOfScalarComponents();
128 assert(false); //No input image, shouldn't happen
131 std::cout << "Input is " << m_Dim << "D " << m_PixelTypeName << "." << std::endl;
134 //--------------------------------------------------------------------
137 //--------------------------------------------------------------------
138 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
140 assert(m_OutputVVImages.size());
141 return m_OutputVVImages[0];
143 //--------------------------------------------------------------------
146 //--------------------------------------------------------------------
147 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
149 return m_OutputVVImages;
151 //--------------------------------------------------------------------
154 //--------------------------------------------------------------------
155 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
157 m_InputVVImages.clear();
158 m_InputVVImages.push_back(input);
160 //--------------------------------------------------------------------
163 //--------------------------------------------------------------------
164 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
166 m_InputVVImages.push_back(input);
168 //--------------------------------------------------------------------
171 //--------------------------------------------------------------------
172 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
174 m_InputVVImages=input;
176 //--------------------------------------------------------------------
179 //--------------------------------------------------------------------
180 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes()
182 std::cout << GetAvailableImageTypes();
184 //--------------------------------------------------------------------
188 //--------------------------------------------------------------------
189 void clitk::ImageToImageGenericFilterBase::ImageTypeError()
191 std::cerr << "**Error** The filter <" << m_FilterName << "> is not available for "
192 << m_Dim << "D images with pixel="
193 << m_PixelTypeName << " and "
194 << m_NbOfComponents << " component." << std::endl;
195 std::cerr << GetAvailableImageTypes();
198 //--------------------------------------------------------------------
201 //--------------------------------------------------------------------
202 void clitk::ImageToImageGenericFilterBase::SetImageTypeError()
204 std::cerr << "TODO ! " << std::endl;
207 //--------------------------------------------------------------------
210 //--------------------------------------------------------------------
211 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName()
215 //--------------------------------------------------------------------
218 //--------------------------------------------------------------------
219 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n)
223 //--------------------------------------------------------------------
226 //--------------------------------------------------------------------
227 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b)
231 //--------------------------------------------------------------------
233 #define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \
235 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, Dim> >(itk::Image<PixelType,Dim>::Pointer output); \
237 itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
239 #define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim) \
241 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<float, Compo>, Dim> >(itk::Image<itk::Vector<float, Compo>,Dim>::Pointer output); \
243 itk::Image<itk::Vector<float,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<float, Compo>, Dim> >(unsigned int n);
245 DEF_SetNextOutput_And_GetInput(char, 2);
246 DEF_SetNextOutput_And_GetInput(unsigned char, 2);
247 DEF_SetNextOutput_And_GetInput(short, 2);
248 DEF_SetNextOutput_And_GetInput(unsigned short, 2);
249 DEF_SetNextOutput_And_GetInput(int, 2);
250 DEF_SetNextOutput_And_GetInput(float, 2);
251 DEF_SetNextOutput_And_GetInput(double, 2);
253 DEF_SetNextOutput_And_GetInput(char, 3);
254 DEF_SetNextOutput_And_GetInput(unsigned char, 3);
255 DEF_SetNextOutput_And_GetInput(short, 3);
256 DEF_SetNextOutput_And_GetInput(unsigned short, 3);
257 DEF_SetNextOutput_And_GetInput(int, 3);
258 DEF_SetNextOutput_And_GetInput(float, 3);
259 DEF_SetNextOutput_And_GetInput(double, 3);
261 DEF_SetNextOutput_And_GetInput_WithCompo(2, 2);
262 DEF_SetNextOutput_And_GetInput_WithCompo(2, 3);
263 DEF_SetNextOutput_And_GetInput_WithCompo(2, 4);
264 DEF_SetNextOutput_And_GetInput_WithCompo(3, 2);
265 DEF_SetNextOutput_And_GetInput_WithCompo(3, 3);
266 DEF_SetNextOutput_And_GetInput_WithCompo(3, 4);
267 DEF_SetNextOutput_And_GetInput_WithCompo(4, 2);
268 DEF_SetNextOutput_And_GetInput_WithCompo(4, 3);
269 DEF_SetNextOutput_And_GetInput_WithCompo(4, 4);
271 DEF_SetNextOutput_And_GetInput(char, 4);
272 DEF_SetNextOutput_And_GetInput(unsigned char, 4);
273 DEF_SetNextOutput_And_GetInput(short, 4);
274 DEF_SetNextOutput_And_GetInput(unsigned short, 4);
275 DEF_SetNextOutput_And_GetInput(int, 4);
276 DEF_SetNextOutput_And_GetInput(float, 4);
277 DEF_SetNextOutput_And_GetInput(double, 4);
280 //--------------------------------------------------------------------
281 template<class ImageType>
282 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output)
284 if (m_OutputFilenames.size()) {
285 clitk::writeImage<ImageType>(output, m_OutputFilenames.front(), m_IOVerbose);
286 m_OutputFilenames.pop_front();
288 if (m_InputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
289 m_OutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
291 //--------------------------------------------------------------------
294 //--------------------------------------------------------------------
295 template<class ImageType>
296 typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n)
298 if (m_ReadOnDisk && m_InputFilenames.size() > n) {
299 return clitk::readImage<ImageType>(m_InputFilenames[n], m_IOVerbose);
301 if (m_InputVVImages.size() > n)
302 return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(m_InputVVImages[n]).GetPointer()));
304 assert(false); //No input, this shouldn't happen
305 return typename ImageType::Pointer(NULL);
309 //--------------------------------------------------------------------
312 //--------------------------------------------------------------------
313 void clitk::ImageToImageGenericFilterBase::MustStop()
315 if (m_FilterBase != NULL) {
316 m_FilterBase->SetMustStop(true);
319 //--------------------------------------------------------------------