]> Creatis software - clitk.git/blob - common/clitkImageToImageGenericFilterBase.cxx
1a3782a62dca975e23ff8ae8ddf05a1ba38bc14a
[clitk.git] / common / clitkImageToImageGenericFilterBase.cxx
1 /*=========================================================================
2
3   Program:   vv
4   Module:    $RCSfile: clitkImageToImageGenericFilterBase.cxx,v $
5   Language:  C++
6   Date:      $Date: 2010/03/03 13:00:36 $
7   Version:   $Revision: 1.2 $
8   Author :   Joel Schaerer <joel.schaerer@creatis.insa-lyon.fr>
9   David Sarrut <david.sarrut@creatis.insa-lyon.fr>
10
11   Copyright (C) 2008
12   Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
13   CREATIS-LRMN http://www.creatis.insa-lyon.fr
14
15   This program is free software: you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, version 3 of the License.
18
19   This program is distributed in the hope that it will be useful,
20   but WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   GNU General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
27   =========================================================================*/
28
29 #include "clitkImageToImageGenericFilterBase.h"
30 #include <itkImage.h>
31
32 //--------------------------------------------------------------------
33 clitk::ImageToImageGenericFilterBase::~ImageToImageGenericFilterBase() {}
34 //--------------------------------------------------------------------
35
36
37 //--------------------------------------------------------------------
38 clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string n)
39   :mIOVerbose(false) {
40   mFilterName = n;
41   mFailOnImageTypeError = true;
42 }
43 //--------------------------------------------------------------------
44
45
46 //--------------------------------------------------------------------
47 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames) {
48     mInputFilenames=filenames;
49 }
50 //--------------------------------------------------------------------
51
52
53 //--------------------------------------------------------------------
54 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename) {
55   std::vector<std::string> f;
56   f.push_back(filename);
57   SetInputFilenames(f);
58 }
59 //--------------------------------------------------------------------
60
61
62 //--------------------------------------------------------------------
63 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename) {
64   mInputFilenames.push_back(filename);
65 }
66 //--------------------------------------------------------------------
67
68
69 //--------------------------------------------------------------------
70 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename) {
71   mOutputFilenames.clear();
72   mOutputFilenames.push_back(filename);
73 }
74 //--------------------------------------------------------------------
75
76
77 //--------------------------------------------------------------------
78 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string & filename)
79 {
80   mOutputFilenames.push_back(filename);
81 }
82 //--------------------------------------------------------------------
83
84
85 //--------------------------------------------------------------------
86 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
87 {
88     mOutputFilenames.clear();
89     std::copy(filenames.begin(),filenames.end(),mOutputFilenames.begin());
90 }
91 //--------------------------------------------------------------------
92
93
94 //--------------------------------------------------------------------
95 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
96 {
97     assert(mOutputFilenames.size() == 1);
98     return mOutputFilenames.front();
99 }
100 //--------------------------------------------------------------------
101
102
103 //--------------------------------------------------------------------
104 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
105         std::string& pixeltype,unsigned int& components)
106 {
107   if (mInputFilenames.size())
108     {
109       int comp_temp,dim_temp; //clitkCommonImage takes ints
110       ReadImageDimensionAndPixelType(mInputFilenames[0], dim_temp, pixeltype,comp_temp);
111       components=comp_temp; dim=dim_temp;
112     }
113   else if (mInputVVImages.size())
114     {
115       pixeltype=mInputVVImages[0]->GetScalarTypeAsString();
116       dim=mInputVVImages[0]->GetNumberOfDimensions();
117       components=mInputVVImages[0]->GetNumberOfScalarComponents();
118     }
119   else
120     assert(false); //No input image, shouldn't happen
121   
122   if (mIOVerbose) {
123     std::cout << "Input is " << mDim << "D " << mPixelTypeName << "." << std::endl;
124   }
125 }
126 //--------------------------------------------------------------------
127
128
129 //--------------------------------------------------------------------
130 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
131 {
132     assert(mOutputVVImages.size());
133     return mOutputVVImages[0];
134 }
135 //--------------------------------------------------------------------
136
137
138 //--------------------------------------------------------------------
139 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
140 {
141     return mOutputVVImages;
142 }
143 //--------------------------------------------------------------------
144
145
146 //--------------------------------------------------------------------
147 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
148 {
149     mInputVVImages.clear();
150     mInputVVImages.push_back(input);
151 }
152 //--------------------------------------------------------------------
153
154
155 //--------------------------------------------------------------------
156 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
157 {
158     mInputVVImages.push_back(input);
159 }
160 //--------------------------------------------------------------------
161
162
163 //--------------------------------------------------------------------
164 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
165 {
166     mInputVVImages=input;
167 }
168 //--------------------------------------------------------------------
169
170
171 //--------------------------------------------------------------------
172 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes() {
173   std::cout << GetAvailableImageTypes();
174 }
175 //--------------------------------------------------------------------
176
177
178
179 //--------------------------------------------------------------------
180 void clitk::ImageToImageGenericFilterBase::ImageTypeError() {
181   std::cerr << "**Error** The filter <" << mFilterName << "> is not available for " 
182             << mDim << "D images with pixel=" 
183             << mPixelTypeName << " and "
184             << mNbOfComponents << " component." << std::endl;
185   std::cerr << GetAvailableImageTypes();
186   exit(0);
187 }
188 //--------------------------------------------------------------------
189
190
191 //--------------------------------------------------------------------
192 void clitk::ImageToImageGenericFilterBase::SetImageTypeError() {
193   std::cerr << "TODO ! " << std::endl;
194   exit(0);
195 }
196 //--------------------------------------------------------------------
197
198
199 //--------------------------------------------------------------------
200 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName() { 
201   return mFilterName; 
202 }
203 //--------------------------------------------------------------------
204
205
206 //--------------------------------------------------------------------
207 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n) { 
208   mFilterName = n; 
209 }
210 //--------------------------------------------------------------------
211
212
213 //--------------------------------------------------------------------
214 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b) { 
215   mIOVerbose = b; 
216 }
217 //--------------------------------------------------------------------
218
219 #define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \
220   template \
221 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, Dim> >(itk::Image<PixelType,Dim>::Pointer output); \
222   template \
223    itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
224
225 #define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim)   \
226   template \
227   void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<float, Compo>, Dim> >(itk::Image<itk::Vector<float, Compo>,Dim>::Pointer output); \
228   template \
229   itk::Image<itk::Vector<float,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<float, Compo>, Dim> >(unsigned int n);
230
231 DEF_SetNextOutput_And_GetInput(char, 2);
232 DEF_SetNextOutput_And_GetInput(unsigned char, 2);
233 DEF_SetNextOutput_And_GetInput(short, 2);
234 DEF_SetNextOutput_And_GetInput(unsigned short, 2);
235 DEF_SetNextOutput_And_GetInput(int, 2);
236 DEF_SetNextOutput_And_GetInput(float, 2);
237 DEF_SetNextOutput_And_GetInput(double, 2);
238
239 DEF_SetNextOutput_And_GetInput(char, 3);
240 DEF_SetNextOutput_And_GetInput(unsigned char, 3);
241 DEF_SetNextOutput_And_GetInput(short, 3);
242 DEF_SetNextOutput_And_GetInput(unsigned short, 3);
243 DEF_SetNextOutput_And_GetInput(int, 3);
244 DEF_SetNextOutput_And_GetInput(float, 3);
245 DEF_SetNextOutput_And_GetInput(double, 3);
246
247 DEF_SetNextOutput_And_GetInput_WithCompo(2, 2);
248 DEF_SetNextOutput_And_GetInput_WithCompo(2, 3);
249 DEF_SetNextOutput_And_GetInput_WithCompo(2, 4);
250 DEF_SetNextOutput_And_GetInput_WithCompo(3, 2);
251 DEF_SetNextOutput_And_GetInput_WithCompo(3, 3);
252 DEF_SetNextOutput_And_GetInput_WithCompo(3, 4);
253 DEF_SetNextOutput_And_GetInput_WithCompo(4, 2);
254 DEF_SetNextOutput_And_GetInput_WithCompo(4, 3);
255 DEF_SetNextOutput_And_GetInput_WithCompo(4, 4);
256
257 DEF_SetNextOutput_And_GetInput(char, 4);
258 DEF_SetNextOutput_And_GetInput(unsigned char, 4);
259 DEF_SetNextOutput_And_GetInput(short, 4);
260 DEF_SetNextOutput_And_GetInput(unsigned short, 4);
261 DEF_SetNextOutput_And_GetInput(int, 4);
262 DEF_SetNextOutput_And_GetInput(float, 4);
263 DEF_SetNextOutput_And_GetInput(double, 4);
264
265
266 //--------------------------------------------------------------------
267 template<class ImageType> 
268 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output) {
269   if (mOutputFilenames.size())
270     {
271       clitk::writeImage<ImageType>(output, mOutputFilenames.front(), mIOVerbose);
272       mOutputFilenames.pop_front();
273     }
274   if (mInputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
275     mOutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
276 }
277 //--------------------------------------------------------------------
278
279
280 //--------------------------------------------------------------------
281 template<class ImageType> 
282 typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n) {
283   if (mInputFilenames.size() > n) {
284     return clitk::readImage<ImageType>(mInputFilenames[n], mIOVerbose);
285   }
286   else if (mInputVVImages.size() > n)
287     return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(mInputVVImages[n]).GetPointer()));
288   else
289     {
290       assert(false); //No input, this shouldn't happen
291       return typename ImageType::Pointer(NULL);
292     }
293 }
294 //--------------------------------------------------------------------
295
296
297