]> Creatis software - clitk.git/blob - common/clitkImageToImageGenericFilterBase.cxx
2c445bc7b2180f4e42e44557b3f75648b09a6b1c
[clitk.git] / common / clitkImageToImageGenericFilterBase.cxx
1 /*=========================================================================
2
3   Program:   vv
4   Module:    $RCSfile: clitkImageToImageGenericFilterBase.cxx,v $
5   Language:  C++
6   Date:      $Date: 2010/03/24 10:48:05 $
7   Version:   $Revision: 1.3 $
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   mReadOnDisk = true;
43 }
44 //--------------------------------------------------------------------
45
46
47 //--------------------------------------------------------------------
48 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames) {
49     mInputFilenames=filenames;
50 }
51 //--------------------------------------------------------------------
52
53
54 //--------------------------------------------------------------------
55 void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b) {
56   mReadOnDisk = b;
57 }
58 //--------------------------------------------------------------------
59
60
61 //--------------------------------------------------------------------
62 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename) {
63   std::vector<std::string> f;
64   f.push_back(filename);
65   SetInputFilenames(f);
66 }
67 //--------------------------------------------------------------------
68
69
70 //--------------------------------------------------------------------
71 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename) {
72   mInputFilenames.push_back(filename);
73 }
74 //--------------------------------------------------------------------
75
76
77 //--------------------------------------------------------------------
78 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename) {
79   mOutputFilenames.clear();
80   mOutputFilenames.push_back(filename);
81 }
82 //--------------------------------------------------------------------
83
84
85 //--------------------------------------------------------------------
86 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string & filename)
87 {
88   mOutputFilenames.push_back(filename);
89 }
90 //--------------------------------------------------------------------
91
92
93 //--------------------------------------------------------------------
94 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
95 {
96     mOutputFilenames.clear();
97     std::copy(filenames.begin(),filenames.end(),mOutputFilenames.begin());
98 }
99 //--------------------------------------------------------------------
100
101
102 //--------------------------------------------------------------------
103 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
104 {
105     assert(mOutputFilenames.size() == 1);
106     return mOutputFilenames.front();
107 }
108 //--------------------------------------------------------------------
109
110
111 //--------------------------------------------------------------------
112 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
113         std::string& pixeltype,unsigned int& components)
114 {
115   if (mReadOnDisk && mInputFilenames.size()) {
116     int comp_temp,dim_temp; //clitkCommonImage takes ints
117     ReadImageDimensionAndPixelType(mInputFilenames[0], dim_temp, pixeltype,comp_temp);
118     components=comp_temp; dim=dim_temp;
119   }
120   else {
121     if (mInputVVImages.size()) {
122       pixeltype=mInputVVImages[0]->GetScalarTypeAsString();
123       dim=mInputVVImages[0]->GetNumberOfDimensions();
124       components=mInputVVImages[0]->GetNumberOfScalarComponents();
125     }
126     else
127     assert(false); //No input image, shouldn't happen
128   }
129   if (mIOVerbose) {
130     std::cout << "Input is " << mDim << "D " << mPixelTypeName << "." << std::endl;
131   }
132 }
133 //--------------------------------------------------------------------
134
135
136 //--------------------------------------------------------------------
137 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
138 {
139     assert(mOutputVVImages.size());
140     return mOutputVVImages[0];
141 }
142 //--------------------------------------------------------------------
143
144
145 //--------------------------------------------------------------------
146 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
147 {
148     return mOutputVVImages;
149 }
150 //--------------------------------------------------------------------
151
152
153 //--------------------------------------------------------------------
154 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
155 {
156     mInputVVImages.clear();
157     mInputVVImages.push_back(input);
158 }
159 //--------------------------------------------------------------------
160
161
162 //--------------------------------------------------------------------
163 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
164 {
165     mInputVVImages.push_back(input);
166 }
167 //--------------------------------------------------------------------
168
169
170 //--------------------------------------------------------------------
171 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
172 {
173     mInputVVImages=input;
174 }
175 //--------------------------------------------------------------------
176
177
178 //--------------------------------------------------------------------
179 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes() {
180   std::cout << GetAvailableImageTypes();
181 }
182 //--------------------------------------------------------------------
183
184
185
186 //--------------------------------------------------------------------
187 void clitk::ImageToImageGenericFilterBase::ImageTypeError() {
188   std::cerr << "**Error** The filter <" << mFilterName << "> is not available for " 
189             << mDim << "D images with pixel=" 
190             << mPixelTypeName << " and "
191             << mNbOfComponents << " component." << std::endl;
192   std::cerr << GetAvailableImageTypes();
193   exit(0);
194 }
195 //--------------------------------------------------------------------
196
197
198 //--------------------------------------------------------------------
199 void clitk::ImageToImageGenericFilterBase::SetImageTypeError() {
200   std::cerr << "TODO ! " << std::endl;
201   exit(0);
202 }
203 //--------------------------------------------------------------------
204
205
206 //--------------------------------------------------------------------
207 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName() { 
208   return mFilterName; 
209 }
210 //--------------------------------------------------------------------
211
212
213 //--------------------------------------------------------------------
214 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n) { 
215   mFilterName = n; 
216 }
217 //--------------------------------------------------------------------
218
219
220 //--------------------------------------------------------------------
221 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b) { 
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   if (mOutputFilenames.size())
277     {
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   if (mReadOnDisk && mInputFilenames.size() > n) {
291     return clitk::readImage<ImageType>(mInputFilenames[n], mIOVerbose);
292   }
293   else {
294     if (mInputVVImages.size() > n)
295       return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(mInputVVImages[n]).GetPointer()));
296     else
297       {
298         assert(false); //No input, this shouldn't happen
299         return typename ImageType::Pointer(NULL);
300       }
301   }
302 }
303 //--------------------------------------------------------------------
304
305
306