]> Creatis software - clitk.git/blob - common/clitkImageToImageGenericFilterBase.cxx
set and get macro like itk
[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://oncora1.lyon.fnclcc.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 #include "clitkImageToImageGenericFilterBase.h"
19 #include <itkImage.h>
20
21 //--------------------------------------------------------------------
22 clitk::ImageToImageGenericFilterBase::~ImageToImageGenericFilterBase() {}
23 //--------------------------------------------------------------------
24
25
26 //--------------------------------------------------------------------
27 clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string n)
28   :m_IOVerbose(false)
29 {
30   m_FilterName = n;
31   m_FailOnImageTypeError = true;
32   m_ReadOnDisk = true;
33   m_LastError = "";
34 }
35 //--------------------------------------------------------------------
36
37
38 //--------------------------------------------------------------------
39 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames)
40 {
41   m_InputFilenames = filenames;
42 }
43 //--------------------------------------------------------------------
44
45
46 //--------------------------------------------------------------------
47 void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b)
48 {
49   m_ReadOnDisk = b;
50 }
51 //--------------------------------------------------------------------
52
53
54 //--------------------------------------------------------------------
55 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename)
56 {
57   std::vector<std::string> f;
58   f.push_back(filename);
59   SetInputFilenames(f);
60 }
61 //--------------------------------------------------------------------
62
63
64 //--------------------------------------------------------------------
65 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename)
66 {
67   m_InputFilenames.push_back(filename);
68 }
69 //--------------------------------------------------------------------
70
71
72 //--------------------------------------------------------------------
73 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename)
74 {
75   m_OutputFilenames.clear();
76   m_OutputFilenames.push_back(filename);
77 }
78 //--------------------------------------------------------------------
79
80
81 //--------------------------------------------------------------------
82 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string & filename)
83 {
84   m_OutputFilenames.push_back(filename);
85 }
86 //--------------------------------------------------------------------
87
88
89 //--------------------------------------------------------------------
90 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
91 {
92   m_OutputFilenames.clear();
93   std::copy(filenames.begin(),filenames.end(),m_OutputFilenames.begin());
94 }
95 //--------------------------------------------------------------------
96
97
98 //--------------------------------------------------------------------
99 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
100 {
101   assert(m_OutputFilenames.size() == 1);
102   return m_OutputFilenames.front();
103 }
104 //--------------------------------------------------------------------
105
106
107 //--------------------------------------------------------------------
108 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
109     std::string& pixeltype,unsigned int& components)
110 {
111   if (m_ReadOnDisk && m_InputFilenames.size()) {
112     int comp_temp,dim_temp; //clitkCommonImage takes ints
113     ReadImageDimensionAndPixelType(m_InputFilenames[0], dim_temp, pixeltype,comp_temp);
114     components=comp_temp;
115     dim=dim_temp;
116   } else {
117     if (m_InputVVImages.size()) {
118       pixeltype=m_InputVVImages[0]->GetScalarTypeAsString();
119       dim=m_InputVVImages[0]->GetNumberOfDimensions();
120       components=m_InputVVImages[0]->GetNumberOfScalarComponents();
121     } else
122       assert(false); //No input image, shouldn't happen
123   }
124   if (m_IOVerbose) {
125     std::cout << "Input is " << m_Dim << "D " << m_PixelTypeName << "." << std::endl;
126   }
127 }
128 //--------------------------------------------------------------------
129
130
131 //--------------------------------------------------------------------
132 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
133 {
134   assert(m_OutputVVImages.size());
135   return m_OutputVVImages[0];
136 }
137 //--------------------------------------------------------------------
138
139
140 //--------------------------------------------------------------------
141 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
142 {
143   return m_OutputVVImages;
144 }
145 //--------------------------------------------------------------------
146
147
148 //--------------------------------------------------------------------
149 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
150 {
151   m_InputVVImages.clear();
152   m_InputVVImages.push_back(input);
153 }
154 //--------------------------------------------------------------------
155
156
157 //--------------------------------------------------------------------
158 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
159 {
160   m_InputVVImages.push_back(input);
161 }
162 //--------------------------------------------------------------------
163
164
165 //--------------------------------------------------------------------
166 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
167 {
168   m_InputVVImages=input;
169 }
170 //--------------------------------------------------------------------
171
172
173 //--------------------------------------------------------------------
174 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes()
175 {
176   std::cout << GetAvailableImageTypes();
177 }
178 //--------------------------------------------------------------------
179
180
181
182 //--------------------------------------------------------------------
183 void clitk::ImageToImageGenericFilterBase::ImageTypeError()
184 {
185   std::cerr << "**Error** The filter <" << m_FilterName << "> is not available for "
186             << m_Dim << "D images with pixel="
187             << m_PixelTypeName << " and "
188             << m_NbOfComponents << " component." << std::endl;
189   std::cerr << GetAvailableImageTypes();
190   exit(0);
191 }
192 //--------------------------------------------------------------------
193
194
195 //--------------------------------------------------------------------
196 void clitk::ImageToImageGenericFilterBase::SetImageTypeError()
197 {
198   std::cerr << "TODO ! " << std::endl;
199   exit(0);
200 }
201 //--------------------------------------------------------------------
202
203
204 //--------------------------------------------------------------------
205 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName()
206 {
207   return m_FilterName;
208 }
209 //--------------------------------------------------------------------
210
211
212 //--------------------------------------------------------------------
213 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n)
214 {
215   m_FilterName = n;
216 }
217 //--------------------------------------------------------------------
218
219
220 //--------------------------------------------------------------------
221 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b)
222 {
223   m_IOVerbose = b;
224 }
225 //--------------------------------------------------------------------
226
227 #define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \
228   template \
229 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, Dim> >(itk::Image<PixelType,Dim>::Pointer output); \
230   template \
231    itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
232
233 #define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim)   \
234   template \
235   void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<float, Compo>, Dim> >(itk::Image<itk::Vector<float, Compo>,Dim>::Pointer output); \
236   template \
237   itk::Image<itk::Vector<float,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<float, Compo>, Dim> >(unsigned int n);
238
239 DEF_SetNextOutput_And_GetInput(char, 2);
240 DEF_SetNextOutput_And_GetInput(unsigned char, 2);
241 DEF_SetNextOutput_And_GetInput(short, 2);
242 DEF_SetNextOutput_And_GetInput(unsigned short, 2);
243 DEF_SetNextOutput_And_GetInput(int, 2);
244 DEF_SetNextOutput_And_GetInput(float, 2);
245 DEF_SetNextOutput_And_GetInput(double, 2);
246
247 DEF_SetNextOutput_And_GetInput(char, 3);
248 DEF_SetNextOutput_And_GetInput(unsigned char, 3);
249 DEF_SetNextOutput_And_GetInput(short, 3);
250 DEF_SetNextOutput_And_GetInput(unsigned short, 3);
251 DEF_SetNextOutput_And_GetInput(int, 3);
252 DEF_SetNextOutput_And_GetInput(float, 3);
253 DEF_SetNextOutput_And_GetInput(double, 3);
254
255 DEF_SetNextOutput_And_GetInput_WithCompo(2, 2);
256 DEF_SetNextOutput_And_GetInput_WithCompo(2, 3);
257 DEF_SetNextOutput_And_GetInput_WithCompo(2, 4);
258 DEF_SetNextOutput_And_GetInput_WithCompo(3, 2);
259 DEF_SetNextOutput_And_GetInput_WithCompo(3, 3);
260 DEF_SetNextOutput_And_GetInput_WithCompo(3, 4);
261 DEF_SetNextOutput_And_GetInput_WithCompo(4, 2);
262 DEF_SetNextOutput_And_GetInput_WithCompo(4, 3);
263 DEF_SetNextOutput_And_GetInput_WithCompo(4, 4);
264
265 DEF_SetNextOutput_And_GetInput(char, 4);
266 DEF_SetNextOutput_And_GetInput(unsigned char, 4);
267 DEF_SetNextOutput_And_GetInput(short, 4);
268 DEF_SetNextOutput_And_GetInput(unsigned short, 4);
269 DEF_SetNextOutput_And_GetInput(int, 4);
270 DEF_SetNextOutput_And_GetInput(float, 4);
271 DEF_SetNextOutput_And_GetInput(double, 4);
272
273
274 //--------------------------------------------------------------------
275 template<class ImageType>
276 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output)
277 {
278   if (m_OutputFilenames.size()) {
279     clitk::writeImage<ImageType>(output, m_OutputFilenames.front(), m_IOVerbose);
280     m_OutputFilenames.pop_front();
281   }
282   if (m_InputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
283     m_OutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
284 }
285 //--------------------------------------------------------------------
286
287
288 //--------------------------------------------------------------------
289 template<class ImageType>
290 typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n)
291 {
292   if (m_ReadOnDisk && m_InputFilenames.size() > n) {
293     return clitk::readImage<ImageType>(m_InputFilenames[n], m_IOVerbose);
294   } else {
295     if (m_InputVVImages.size() > n)
296       return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(m_InputVVImages[n]).GetPointer()));
297     else {
298       assert(false); //No input, this shouldn't happen
299       return typename ImageType::Pointer(NULL);
300     }
301   }
302 }
303 //--------------------------------------------------------------------
304
305
306