]> Creatis software - clitk.git/blob - tools/clitkImageResample.cxx
Reformatted using new coding style
[clitk.git] / tools / clitkImageResample.cxx
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://oncora1.lyon.fnclcc.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 #ifndef CLITKIMAGERESAMPLE_CXX
19 #define CLITKIMAGERESAMPLE_CXX
20 /**
21    ------------------------------------------------=
22    * @file   clitkImageResample.cxx
23    * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24    * @date   23 Feb 2008 08:37:53
25    ------------------------------------------------=*/
26
27 // clitk
28 #include "clitkImageResample_ggo.h"
29 #include "clitkIO.h"
30 #include "clitkImageResampleGenericFilter.h"
31
32 //--------------------------------------------------------------------
33 int main(int argc, char * argv[])
34 {
35
36   // Init command line
37   GGO(clitkImageResample, args_info);
38   CLITK_INIT;
39
40   // Read input image header to check image dimension
41   itk::ImageIOBase::Pointer header = clitk::readImageHeader(args_info.input_arg);
42   unsigned int dim = header->GetNumberOfDimensions();
43   std::string pixelTypeName = header->GetComponentTypeAsString(header->GetComponentType());
44
45   // Print image info if verbose
46   if (args_info.verbose_flag) {
47     std::cout << "Input image <" << args_info.input_arg << "> is ";
48     clitk::printImageHeader(header, std::cout);
49     std::cout << std::endl;
50   }
51
52   // Get input size/spacing
53   std::vector<int>    inputSize;
54   std::vector<double> inputSpacing;
55   inputSize.resize(dim);
56   inputSpacing.resize(dim);
57   for(unsigned int i=0; i<dim; i++) {
58     inputSpacing[i] = header->GetSpacing(i);
59     inputSize[i] = header->GetDimensions(i);
60   }
61
62   // Get options
63   std::vector<int>    outputSize;
64   std::vector<double> outputSpacing;
65   outputSize.resize(dim);
66   outputSpacing.resize(dim);
67
68   // Check options
69   if (!args_info.size_given && !args_info.spacing_given) {
70     std::cerr << "Please indicate output size or spacing." << std::endl;
71     exit(0);
72   }
73
74   // Check options
75   if (args_info.size_given && args_info.spacing_given) {
76     std::cerr << "Please indicate only output size or spacing, not both." << std::endl;
77     exit(0);
78   }
79
80   // Size is given and not spacing
81   if (args_info.size_given && !args_info.spacing_given) {
82     if (args_info.size_given != dim) {
83       std::cerr << "Input image is " << dim << "D, please give " << dim << " size numbers." << std::endl;
84       exit(0);
85     }
86     for(unsigned int i=0; i<dim; i++) {
87       if ((args_info.size_arg[i] == -1) || (args_info.size_arg[i]==0))
88         outputSize[i] = inputSize[i];
89       else
90         outputSize[i] = args_info.size_arg[i];
91       outputSpacing[i] = inputSize[i]*inputSpacing[i]/outputSize[i];
92     }
93   }
94
95   // Spacing is given and not size
96   if (!args_info.size_given && args_info.spacing_given) {
97     if (args_info.spacing_given != dim) {
98       if (args_info.spacing_given == 1) {
99         for(unsigned int i=0; i<dim; i++) outputSpacing[i] = args_info.spacing_arg[0];
100       } else {
101         std::cerr << "Input image is " << dim << "D, please give " << dim << " spacing numbers." << std::endl;
102         exit(0);
103       }
104     } else {
105       for(unsigned int i=0; i<dim; i++) {
106         if ((args_info.spacing_arg[i] == -1) || (args_info.spacing_arg[i]==0))
107           outputSpacing[i] = inputSpacing[i];
108         else
109           outputSpacing[i] = args_info.spacing_arg[i];
110
111       }
112     }
113     for(unsigned int i=0; i<dim; i++)
114       outputSize[i] = (int)lrint(inputSize[i]*inputSpacing[i]/outputSpacing[i]);
115   }
116
117   if (args_info.verbose_flag) {
118     std::cout << "Output image will be : " << std::endl;
119     DDV(outputSize,dim);
120     DDV(outputSpacing,dim);
121   }
122
123   // Get sigma option for Gaussian filter
124   std::vector<double> sigma;
125   sigma.resize(args_info.gauss_given);
126   std::copy(args_info.gauss_arg, args_info.gauss_arg+args_info.gauss_given, sigma.begin());
127   if (args_info.gauss_given) {
128     if (args_info.gauss_given != dim) {
129       if (args_info.gauss_given == 1) {
130         sigma.resize(dim);
131         for(unsigned int i=0; i<dim; i++) sigma[i] = args_info.gauss_arg[0];
132       } else {
133         std::cerr << "Input image is " << dim << "D, please give " << dim << " sigma numbers for gaussian filter." << std::endl;
134         exit(0);
135       }
136     }
137   }
138
139   // Create a filter
140   clitk::ImageResampleGenericFilter::Pointer filter = clitk::ImageResampleGenericFilter::New();
141   filter->SetInputFilename(args_info.input_arg);
142   filter->SetOutputSize(outputSize);
143   filter->SetOutputSpacing(outputSpacing);
144   filter->SetInterpolationName(args_info.interp_arg);
145   filter->SetBSplineOrder(args_info.order_arg);
146   filter->SetBLUTSampling(args_info.sampling_arg);
147   if (args_info.gauss_given)
148     filter->SetGaussianSigma(sigma);
149   filter->SetOutputFilename(args_info.output_arg);
150
151   // Go !
152   filter->Update();
153
154   // this is the end my friend
155   return 0;
156 }// end main
157 //--------------------------------------------------------------------
158
159 #endif  /* end #define CLITKIMAGERESAMPLE_CXX */