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