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