]> Creatis software - clitk.git/blob - tools/clitkResampleImageGenericFilter.txx
7ecf84298ba01aad9a1a63b749ed326857681309
[clitk.git] / tools / clitkResampleImageGenericFilter.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19 #ifndef CLITKRESAMPLEIMAGEGENERICFILTER_TXX
20 #define CLITKRESAMPLEIMAGEGENERICFILTER_TXX
21
22 // clitk
23 #include "clitkResampleImageWithOptionsFilter.h"
24
25 //--------------------------------------------------------------------
26 // Update with the number of dimensions and the pixeltype
27 //--------------------------------------------------------------------
28 template<class InputImageType>
29 void
30 clitk::ResampleImageGenericFilter::UpdateWithInputImageType()
31 {
32
33   // Reading input
34   typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
35
36   // Main filter
37   typedef typename InputImageType::PixelType PixelType;
38   typedef InputImageType OutputImageType; // to change to float is user ask it (?)
39
40   // Filter
41   typedef clitk::ResampleImageWithOptionsFilter<InputImageType, OutputImageType> ResampleImageFilterType;
42   typename ResampleImageFilterType::Pointer filter = ResampleImageFilterType::New();
43   filter->SetInput(input);
44
45   // Set Verbose
46   filter->SetVerboseOptions(mArgsInfo.verbose_flag);
47
48   // Set size / spacing
49   static const unsigned int dim = OutputImageType::ImageDimension;
50   typename OutputImageType::SpacingType spacing;
51   typename OutputImageType::SizeType size;
52   typename OutputImageType::PointType origin;
53   typename OutputImageType::DirectionType direction;
54
55   if (mArgsInfo.like_given) {
56     itk::ImageIOBase::Pointer header = clitk::readImageHeader(mArgsInfo.like_arg);
57     if (header) {
58       for(unsigned int i=0; i<dim; i++){
59         spacing[i] = header->GetSpacing(i);
60         size[i] = header->GetDimensions(i);
61         origin[i] = header->GetOrigin(i);
62       }
63       for(unsigned int i=0; i<dim; i++) {
64         for(unsigned int j=0;j<dim;j++) {
65             direction(i,j) = header->GetDirection(i)[j];
66         }
67       }
68       filter->SetOutputSpacing(spacing);
69       filter->SetOutputSize(size);
70       filter->SetOutputOrigin(origin);
71       filter->SetOutputDirection(direction);
72     }
73     else {
74       std::cerr << "*** Warning : I could not read '" << mArgsInfo.like_arg << "' ***" << std::endl;
75       exit(0);
76     }
77   }
78   else {
79     if (mArgsInfo.spacing_given == 1) {
80       filter->SetOutputIsoSpacing(mArgsInfo.spacing_arg[0]);
81     }
82     else if ((mArgsInfo.spacing_given != 0) && (mArgsInfo.size_given != 0)) {
83       std::cerr << "Error: use spacing or size, not both." << std::endl;
84       exit(0);
85     }
86     else if (mArgsInfo.spacing_given) {
87       if ((mArgsInfo.spacing_given != 0) && (mArgsInfo.spacing_given != dim)) {
88         std::cerr << "Error: spacing should have one or " << dim << " values." << std::endl;
89         exit(0);
90       }
91       for(unsigned int i=0; i<dim; i++)
92         spacing[i] = mArgsInfo.spacing_arg[i];
93       filter->SetOutputSpacing(spacing);
94     }
95     else if (mArgsInfo.size_given) {
96       if ((mArgsInfo.size_given != 0) && (mArgsInfo.size_given != dim)) {
97         std::cerr << "Error: size should have " << dim << " values." << std::endl;
98         exit(0);
99       }
100       for(unsigned int i=0; i<dim; i++)
101         size[i] = mArgsInfo.size_arg[i];
102       filter->SetOutputSize(size);
103     }
104     for(unsigned int i=0; i<dim; i++){
105       origin[i] = input->GetOrigin()[i];
106     }
107     for(unsigned int i=0; i<dim; i++) {
108       for(unsigned int j=0;j<dim;j++) {
109           direction(i,j) = input->GetDirection()[i][j];
110       }
111     }
112     filter->SetOutputOrigin(origin);
113     filter->SetOutputDirection(direction);
114   }
115
116   // Set temporal dimension
117   filter->SetLastDimensionIsTime(mArgsInfo.time_flag);
118
119   // Set Gauss
120   filter->SetGaussianFilteringEnabled(mArgsInfo.autogauss_flag);
121   if (mArgsInfo.gauss_given != 0) {
122     typename ResampleImageFilterType::GaussianSigmaType g;
123     for(unsigned int i=0; i<dim; i++) {
124       g[i] = mArgsInfo.gauss_arg[i];
125     }
126     filter->SetGaussianSigma(g);
127   }
128
129   // Set Interpolation
130   std::string interp = std::string(mArgsInfo.interp_arg);
131   if (interp == "nn") {
132     filter->SetInterpolationType(ResampleImageFilterType::NearestNeighbor);
133   } else {
134     if (interp == "linear") {
135       filter->SetInterpolationType(ResampleImageFilterType::Linear);
136     } else {
137       if (interp == "bspline") {
138         filter->SetInterpolationType(ResampleImageFilterType::BSpline);
139       } else {
140         if (interp == "blut") {
141           filter->SetInterpolationType(ResampleImageFilterType::B_LUT);
142         } else {
143           if (interp == "windowed sinc") {
144             filter->SetInterpolationType(ResampleImageFilterType::WSINC);
145           } else {
146             std::cerr << "Error. I do not know interpolation '" << mArgsInfo.interp_arg
147                       << "'. Choose among: nn, linear, bspline, blut, windowed sinc" << std::endl;
148             exit(0);
149           }
150         }
151       }
152     }
153   }
154
155   // Set default pixel value
156   filter->SetDefaultPixelValue(mArgsInfo.default_arg);
157
158   // Set thread
159   if (mArgsInfo.thread_given) {
160     filter->SetNumberOfThreads(mArgsInfo.thread_arg);
161   }
162
163   // Go !
164   filter->Update();
165   typename OutputImageType::Pointer outputImage = filter->GetOutput();
166   this->template SetNextOutput<OutputImageType>(outputImage);
167 }
168 //--------------------------------------------------------------------
169
170 #endif /* end #define CLITKRESAMPLEIMAGEGENERICFILTER_TXX */
171