]> Creatis software - clitk.git/blob - common/clitkImageToImageGenericFilter.txx
93f486f22d9ac86eb18730978f985469ebe8a84d
[clitk.git] / common / clitkImageToImageGenericFilter.txx
1 /*=========================================================================
2
3   Program:   vv
4   Module:    $RCSfile: clitkImageToImageGenericFilter.txx,v $
5   Language:  C++
6   Date:      $Date: 2010/02/09 14:40:55 $
7   Version:   $Revision: 1.4 $
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 //--------------------------------------------------------------------
30 template<class ImageType> 
31 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output) {
32   if (mOutputFilenames.size())
33     {
34       clitk::writeImage<ImageType>(output, mOutputFilenames.front(), mIOVerbose);
35       mOutputFilenames.pop_front();
36     }
37   if (mInputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
38     mOutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
39 }
40 //--------------------------------------------------------------------
41
42
43 //--------------------------------------------------------------------
44 template<class ImageType> 
45 typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n) {
46   if (mInputFilenames.size() > n) {
47     return clitk::readImage<ImageType>(mInputFilenames[n], mIOVerbose);
48   }
49   else if (mInputVVImages.size() > n)
50     return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(mInputVVImages[n]).GetPointer()));
51   else
52     assert(false); //No input, this shouldn't happen
53 }
54 //--------------------------------------------------------------------
55
56
57 //--------------------------------------------------------------------
58 template<class FilterType>
59 clitk::ImageToImageGenericFilter<FilterType>::ImageToImageGenericFilter(std::string filterName) :
60     ImageToImageGenericFilterBase(filterName), 
61     mImageTypesManager(static_cast<FilterType*>(this))
62 {
63 }
64 //--------------------------------------------------------------------
65
66
67 //--------------------------------------------------------------------
68 template<class FilterType>
69 bool clitk::ImageToImageGenericFilter<FilterType>::Update() {
70   GetInputImageDimensionAndPixelType(mDim,mPixelTypeName,mNbOfComponents);    
71   
72   // Check ImageType
73   if (!CheckImageType()) {
74     if (mFailOnImageTypeError) ImageTypeError();
75     else SetImageTypeError();
76     return false;
77   }
78
79   // Go ! Call the right templatized function
80   mImageTypesManager.DoIt(mDim, mNbOfComponents, mPixelTypeName);
81   return true;
82 }
83 //--------------------------------------------------------------------
84 template<class FilterType>
85 bool clitk::ImageToImageGenericFilter<FilterType>::CheckImageType(unsigned int dim,unsigned int ncomp, std::string pixeltype)
86 {
87     return static_cast<bool>(mImageTypesManager.mMapOfImageTypeToFunction[dim][ncomp][pixeltype]);
88 }
89
90 template<class FilterType>
91 bool clitk::ImageToImageGenericFilter<FilterType>::CheckImageType()
92 {
93     return static_cast<bool>(mImageTypesManager.mMapOfImageTypeToFunction[mDim][mNbOfComponents][mPixelTypeName]);
94 }
95
96 template<class FilterType>
97 std::string clitk::ImageToImageGenericFilter<FilterType>::GetAvailableImageTypes() {
98     std::ostringstream oss;
99     oss << "The filter <" << mFilterName << "> manages:" << std::endl;
100     
101     typedef typename ImageTypesManager<FilterType>::MapOfImageComponentsToFunctionType::const_iterator MCompItType;
102     typedef typename ImageTypesManager<FilterType>::MapOfImageDimensionToFunctionType::const_iterator MDimItType;
103     typedef typename ImageTypesManager<FilterType>::MapOfPixelTypeToFunctionType::const_iterator MPixelItType;
104     for (MDimItType i=mImageTypesManager.mMapOfImageTypeToFunction.begin();
105             i!=mImageTypesManager.mMapOfImageTypeToFunction.end();
106             i++) {
107         for (MCompItType j=(*i).second.begin(); j!= (*i).second.end(); j++) {
108             for (MPixelItType k=(*j).second.begin(); k!= (*j).second.end(); k++) {
109                 oss << "Dim: " << (*i).first 
110                     << ", Components: " << (*j).first 
111                     << ", Type: " << (*k).first << std::endl;
112             }
113         }
114     }
115     return oss.str();
116 }
117 //--------------------------------------------------------------------
118