1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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"
21 //--------------------------------------------------------------------
22 clitk::ImageToImageGenericFilterBase::~ImageToImageGenericFilterBase() {}
23 //--------------------------------------------------------------------
26 //--------------------------------------------------------------------
27 clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string n)
30 mFailOnImageTypeError = true;
33 //--------------------------------------------------------------------
36 //--------------------------------------------------------------------
37 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames) {
38 mInputFilenames=filenames;
40 //--------------------------------------------------------------------
43 //--------------------------------------------------------------------
44 void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b) {
47 //--------------------------------------------------------------------
50 //--------------------------------------------------------------------
51 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename) {
52 std::vector<std::string> f;
53 f.push_back(filename);
56 //--------------------------------------------------------------------
59 //--------------------------------------------------------------------
60 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename) {
61 mInputFilenames.push_back(filename);
63 //--------------------------------------------------------------------
66 //--------------------------------------------------------------------
67 void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename) {
68 mOutputFilenames.clear();
69 mOutputFilenames.push_back(filename);
71 //--------------------------------------------------------------------
74 //--------------------------------------------------------------------
75 void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string & filename)
77 mOutputFilenames.push_back(filename);
79 //--------------------------------------------------------------------
82 //--------------------------------------------------------------------
83 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
85 mOutputFilenames.clear();
86 std::copy(filenames.begin(),filenames.end(),mOutputFilenames.begin());
88 //--------------------------------------------------------------------
91 //--------------------------------------------------------------------
92 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
94 assert(mOutputFilenames.size() == 1);
95 return mOutputFilenames.front();
97 //--------------------------------------------------------------------
100 //--------------------------------------------------------------------
101 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
102 std::string& pixeltype,unsigned int& components)
104 if (mReadOnDisk && mInputFilenames.size()) {
105 int comp_temp,dim_temp; //clitkCommonImage takes ints
106 ReadImageDimensionAndPixelType(mInputFilenames[0], dim_temp, pixeltype,comp_temp);
107 components=comp_temp; dim=dim_temp;
110 if (mInputVVImages.size()) {
111 pixeltype=mInputVVImages[0]->GetScalarTypeAsString();
112 dim=mInputVVImages[0]->GetNumberOfDimensions();
113 components=mInputVVImages[0]->GetNumberOfScalarComponents();
116 assert(false); //No input image, shouldn't happen
119 std::cout << "Input is " << mDim << "D " << mPixelTypeName << "." << std::endl;
122 //--------------------------------------------------------------------
125 //--------------------------------------------------------------------
126 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
128 assert(mOutputVVImages.size());
129 return mOutputVVImages[0];
131 //--------------------------------------------------------------------
134 //--------------------------------------------------------------------
135 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
137 return mOutputVVImages;
139 //--------------------------------------------------------------------
142 //--------------------------------------------------------------------
143 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
145 mInputVVImages.clear();
146 mInputVVImages.push_back(input);
148 //--------------------------------------------------------------------
151 //--------------------------------------------------------------------
152 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
154 mInputVVImages.push_back(input);
156 //--------------------------------------------------------------------
159 //--------------------------------------------------------------------
160 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
162 mInputVVImages=input;
164 //--------------------------------------------------------------------
167 //--------------------------------------------------------------------
168 void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes() {
169 std::cout << GetAvailableImageTypes();
171 //--------------------------------------------------------------------
175 //--------------------------------------------------------------------
176 void clitk::ImageToImageGenericFilterBase::ImageTypeError() {
177 std::cerr << "**Error** The filter <" << mFilterName << "> is not available for "
178 << mDim << "D images with pixel="
179 << mPixelTypeName << " and "
180 << mNbOfComponents << " component." << std::endl;
181 std::cerr << GetAvailableImageTypes();
184 //--------------------------------------------------------------------
187 //--------------------------------------------------------------------
188 void clitk::ImageToImageGenericFilterBase::SetImageTypeError() {
189 std::cerr << "TODO ! " << std::endl;
192 //--------------------------------------------------------------------
195 //--------------------------------------------------------------------
196 const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName() {
199 //--------------------------------------------------------------------
202 //--------------------------------------------------------------------
203 void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n) {
206 //--------------------------------------------------------------------
209 //--------------------------------------------------------------------
210 void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b) {
213 //--------------------------------------------------------------------
215 #define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \
217 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, Dim> >(itk::Image<PixelType,Dim>::Pointer output); \
219 itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
221 #define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim) \
223 void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<float, Compo>, Dim> >(itk::Image<itk::Vector<float, Compo>,Dim>::Pointer output); \
225 itk::Image<itk::Vector<float,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<float, Compo>, Dim> >(unsigned int n);
227 DEF_SetNextOutput_And_GetInput(char, 2);
228 DEF_SetNextOutput_And_GetInput(unsigned char, 2);
229 DEF_SetNextOutput_And_GetInput(short, 2);
230 DEF_SetNextOutput_And_GetInput(unsigned short, 2);
231 DEF_SetNextOutput_And_GetInput(int, 2);
232 DEF_SetNextOutput_And_GetInput(float, 2);
233 DEF_SetNextOutput_And_GetInput(double, 2);
235 DEF_SetNextOutput_And_GetInput(char, 3);
236 DEF_SetNextOutput_And_GetInput(unsigned char, 3);
237 DEF_SetNextOutput_And_GetInput(short, 3);
238 DEF_SetNextOutput_And_GetInput(unsigned short, 3);
239 DEF_SetNextOutput_And_GetInput(int, 3);
240 DEF_SetNextOutput_And_GetInput(float, 3);
241 DEF_SetNextOutput_And_GetInput(double, 3);
243 DEF_SetNextOutput_And_GetInput_WithCompo(2, 2);
244 DEF_SetNextOutput_And_GetInput_WithCompo(2, 3);
245 DEF_SetNextOutput_And_GetInput_WithCompo(2, 4);
246 DEF_SetNextOutput_And_GetInput_WithCompo(3, 2);
247 DEF_SetNextOutput_And_GetInput_WithCompo(3, 3);
248 DEF_SetNextOutput_And_GetInput_WithCompo(3, 4);
249 DEF_SetNextOutput_And_GetInput_WithCompo(4, 2);
250 DEF_SetNextOutput_And_GetInput_WithCompo(4, 3);
251 DEF_SetNextOutput_And_GetInput_WithCompo(4, 4);
253 DEF_SetNextOutput_And_GetInput(char, 4);
254 DEF_SetNextOutput_And_GetInput(unsigned char, 4);
255 DEF_SetNextOutput_And_GetInput(short, 4);
256 DEF_SetNextOutput_And_GetInput(unsigned short, 4);
257 DEF_SetNextOutput_And_GetInput(int, 4);
258 DEF_SetNextOutput_And_GetInput(float, 4);
259 DEF_SetNextOutput_And_GetInput(double, 4);
262 //--------------------------------------------------------------------
263 template<class ImageType>
264 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output) {
265 if (mOutputFilenames.size())
267 clitk::writeImage<ImageType>(output, mOutputFilenames.front(), mIOVerbose);
268 mOutputFilenames.pop_front();
270 if (mInputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
271 mOutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
273 //--------------------------------------------------------------------
276 //--------------------------------------------------------------------
277 template<class ImageType>
278 typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n) {
279 if (mReadOnDisk && mInputFilenames.size() > n) {
280 return clitk::readImage<ImageType>(mInputFilenames[n], mIOVerbose);
283 if (mInputVVImages.size() > n)
284 return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(mInputVVImages[n]).GetPointer()));
287 assert(false); //No input, this shouldn't happen
288 return typename ImageType::Pointer(NULL);
292 //--------------------------------------------------------------------