1 /*=========================================================================
4 Module: $RCSfile: clitkImageToImageGenericFilterBase.cxx,v $
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>
12 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
13 CREATIS-LRMN http://www.creatis.insa-lyon.fr
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.
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.
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/>.
27 =========================================================================*/
29 #include "clitkImageToImageGenericFilterBase.h"
32 //--------------------------------------------------------------------
33 clitk::ImageToImageGenericFilterBase::~ImageToImageGenericFilterBase() {}
34 //--------------------------------------------------------------------
37 //--------------------------------------------------------------------
38 clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string n)
41 mFailOnImageTypeError = true;
43 //--------------------------------------------------------------------
46 //--------------------------------------------------------------------
47 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames) {
48 mInputFilenames=filenames;
50 //--------------------------------------------------------------------
53 //--------------------------------------------------------------------
54 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename) {
55 std::vector<std::string> f;
56 f.push_back(filename);
59 //--------------------------------------------------------------------
62 //--------------------------------------------------------------------
63 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename) {
64 mInputFilenames.push_back(filename);
66 //--------------------------------------------------------------------
69 //--------------------------------------------------------------------
70 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename) {
71 mOutputFilenames.clear();
72 mOutputFilenames.push_back(filename);
74 //--------------------------------------------------------------------
77 //--------------------------------------------------------------------
78 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string & filename)
80 mOutputFilenames.push_back(filename);
82 //--------------------------------------------------------------------
85 //--------------------------------------------------------------------
86 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
88 mOutputFilenames.clear();
89 std::copy(filenames.begin(),filenames.end(),mOutputFilenames.begin());
91 //--------------------------------------------------------------------
94 //--------------------------------------------------------------------
95 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
97 assert(mOutputFilenames.size() == 1);
98 return mOutputFilenames.front();
100 //--------------------------------------------------------------------
103 //--------------------------------------------------------------------
104 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
105 std::string& pixeltype,unsigned int& components)
107 if (mInputFilenames.size())
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;
113 else if (mInputVVImages.size())
115 pixeltype=mInputVVImages[0]->GetScalarTypeAsString();
116 dim=mInputVVImages[0]->GetNumberOfDimensions();
117 components=mInputVVImages[0]->GetNumberOfScalarComponents();
120 assert(false); //No input image, shouldn't happen
123 std::cout << "Input is " << mDim << "D " << mPixelTypeName << "." << std::endl;
126 //--------------------------------------------------------------------
129 //--------------------------------------------------------------------
130 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
132 assert(mOutputVVImages.size());
133 return mOutputVVImages[0];
135 //--------------------------------------------------------------------
138 //--------------------------------------------------------------------
139 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
141 return mOutputVVImages;
143 //--------------------------------------------------------------------
146 //--------------------------------------------------------------------
147 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
149 mInputVVImages.clear();
150 mInputVVImages.push_back(input);
152 //--------------------------------------------------------------------
155 //--------------------------------------------------------------------
156 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
158 mInputVVImages.push_back(input);
160 //--------------------------------------------------------------------
163 //--------------------------------------------------------------------
164 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
166 mInputVVImages=input;
168 //--------------------------------------------------------------------
171 //--------------------------------------------------------------------
172 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes() {
173 std::cout << GetAvailableImageTypes();
175 //--------------------------------------------------------------------
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();
188 //--------------------------------------------------------------------
191 //--------------------------------------------------------------------
192 void clitk::ImageToImageGenericFilterBase::SetImageTypeError() {
193 std::cerr << "TODO ! " << std::endl;
196 //--------------------------------------------------------------------
199 //--------------------------------------------------------------------
200 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName() {
203 //--------------------------------------------------------------------
206 //--------------------------------------------------------------------
207 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n) {
210 //--------------------------------------------------------------------
213 //--------------------------------------------------------------------
214 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b) {
217 //--------------------------------------------------------------------
219 #define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \
221 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, Dim> >(itk::Image<PixelType,Dim>::Pointer output); \
223 itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
225 #define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim) \
227 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<float, Compo>, Dim> >(itk::Image<itk::Vector<float, Compo>,Dim>::Pointer output); \
229 itk::Image<itk::Vector<float,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<float, Compo>, Dim> >(unsigned int n);
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);
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);
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);
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);
266 //--------------------------------------------------------------------
267 template<class ImageType>
268 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output) {
269 if (mOutputFilenames.size())
271 clitk::writeImage<ImageType>(output, mOutputFilenames.front(), mIOVerbose);
272 mOutputFilenames.pop_front();
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));
277 //--------------------------------------------------------------------
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);
286 else if (mInputVVImages.size() > n)
287 return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(mInputVVImages[n]).GetPointer()));
290 assert(false); //No input, this shouldn't happen
291 return typename ImageType::Pointer(NULL);
294 //--------------------------------------------------------------------