]> Creatis software - clitk.git/blob - tools/clitkPadImageGenericFilter.cxx
Increase number of decimal for fusion and overlay
[clitk.git] / tools / clitkPadImageGenericFilter.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://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 #ifndef clitkPadImageGenericFilter_cxx
19 #define clitkPadImageGenericFilter_cxx
20
21 /* =================================================
22  * @file   clitkPadImageGenericFilter.cxx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30 #include "clitkPadImageGenericFilter.h"
31
32 // itk
33 #include <itkConstantPadImageFilter.h>
34
35
36 namespace clitk
37 {
38
39
40   //-----------------------------------------------------------
41   // Constructor
42   //-----------------------------------------------------------
43   PadImageGenericFilter::PadImageGenericFilter():
44     ImageToImageGenericFilter<Self>("PadImage")
45   {
46     cmdline_parser_clitkPadImage_init(&mArgsInfo);
47     InitializeImageType<2>();
48     InitializeImageType<3>();
49     //InitializeImageType<4>();
50   }
51
52   //--------------------------------------------------------------------
53   template<unsigned int Dim>
54   void PadImageGenericFilter::InitializeImageType()
55   {
56     ADD_DEFAULT_IMAGE_TYPES(Dim);
57     //ADD_IMAGE_TYPE(Dim, uchar);
58     //ADD_IMAGE_TYPE(Dim, short);
59     // ADD_IMAGE_TYPE(Dim, uint);
60     //  ADD_IMAGE_TYPE(Dim, ulong);
61     // ADD_IMAGE_TYPE(Dim, int);
62     // ADD_IMAGE_TYPE(Dim, float);
63   }
64   //--------------------------------------------------------------------
65
66   //--------------------------------------------------------------------
67   void PadImageGenericFilter::SetArgsInfo(const args_info_type& a) 
68   {
69     mArgsInfo=a;
70     SetIOVerbose(mArgsInfo.verbose_flag);
71     if (mArgsInfo.input_given)   AddInputFilename(mArgsInfo.input_arg);
72     if (mArgsInfo.output_given)  AddOutputFilename(mArgsInfo.output_arg);
73   }
74   //--------------------------------------------------------------------
75
76   //--------------------------------------------------------------------
77   // Update with the number of dimensions and the pixeltype
78   //--------------------------------------------------------------------
79   template<class ImageType>
80   void PadImageGenericFilter::UpdateWithInputImageType() 
81   { 
82     typedef itk::ConstantPadImageFilter<ImageType, ImageType> PadFilterType;
83     typedef typename PadFilterType::SizeValueType PadBoundType;
84
85     // Reading input
86     typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
87
88     const unsigned int dim = ImageType::ImageDimension;
89     PadBoundType pad_lower[dim], pad_upper[dim];
90     if (mArgsInfo.like_given) {
91       int err = PadLike<ImageType, PadBoundType, ImageType::ImageDimension>(input, pad_lower, pad_upper);
92       if (err) {
93         std::cerr << "Error processing like image." << std::endl;
94         return;
95       }
96     }
97     else {
98       if (mArgsInfo.lower_given != dim || mArgsInfo.upper_given != dim)
99       {
100         std::cerr << "The number of lower and upper padding parameters must be equal to the image dimension." << std::endl;
101         return;
102       }
103       
104       for (unsigned int i = 0; i < dim; i++) {
105         pad_lower[i] = mArgsInfo.lower_arg[i];
106         pad_upper[i] = mArgsInfo.upper_arg[i];
107       }
108     }
109     
110     typename PadFilterType::Pointer filter = PadFilterType::New();
111     filter->SetPadLowerBound(pad_lower);
112     filter->SetPadUpperBound(pad_upper);
113     filter->SetInput(input);
114     filter->SetConstant(mArgsInfo.value_arg);
115     filter->Update();
116
117     // Prepare output
118     typename ImageType::Pointer output;
119     output = filter->GetOutput();
120       
121     // Write/Save results
122     this->template SetNextOutput<ImageType>(output); 
123   }
124   //--------------------------------------------------------------------
125
126   //--------------------------------------------------------------------
127   // Update with the number of dimensions and the pixeltype
128   //--------------------------------------------------------------------
129   template <class ImageType, class PadBoundType, unsigned int dim>
130   int PadImageGenericFilter::PadLike(typename ImageType::Pointer input, PadBoundType* padLower, PadBoundType* padUpper) 
131   { 
132     if (mArgsInfo.verbose_flag)
133       std::cout << "PadLike - IN" << std::endl;
134     
135     typedef typename ImageType::SpacingType SpacingType;
136     typedef typename ImageType::RegionType RegionType;
137     typedef typename ImageType::SizeType SizeType;
138     typedef typename ImageType::IndexType IndexType;
139     typedef typename ImageType::PointType PointType;
140     typedef typename ImageType::PointValueType PointValueType;
141
142     if (mArgsInfo.verbose_flag)
143       std::cout << "Reading like image: " << mArgsInfo.like_arg << ::endl;
144     
145     typedef itk::ImageFileReader<ImageType> ImageReaderType;
146     typename ImageReaderType::Pointer reader = ImageReaderType::New();
147     reader->SetFileName(mArgsInfo.like_arg);
148     reader->Update();
149     
150     typename ImageType::Pointer like_image = reader->GetOutput();
151
152     if (mArgsInfo.verbose_flag)
153       std::cout << "Calculating padding." << std::endl;
154     
155     SpacingType spacing = input->GetSpacing(), like_spacing = like_image->GetSpacing(); 
156     if (spacing != like_spacing) {
157       std::cerr << "Like-image must have same spacing as input: " << spacing << " " << like_spacing << std::endl;
158       return PAD_ERR_NOT_SAME_SPACING;
159     }
160     
161     SizeType size = input->GetLargestPossibleRegion().GetSize(), like_size = like_image->GetLargestPossibleRegion().GetSize();
162     PointType origin = input->GetOrigin(), like_origin = like_image->GetOrigin();
163     
164     PointType lower_bound, like_lower_bound;
165     PointType upper_bound, like_upper_bound;
166     PointValueType auxl = 0, auxu = 0;
167     for (unsigned int i = 0; i < dim; i++) {
168       lower_bound[i] = origin[i];
169       like_lower_bound[i] = like_origin[i];
170       auxl = itk::Math::Round<PointValueType>(((lower_bound[i] - like_lower_bound[i])/spacing[i]));
171       
172       upper_bound[i] = (lower_bound[i] + size[i]*spacing[i]);
173       like_upper_bound[i] = (like_lower_bound[i] + like_size[i]*spacing[i]);
174       auxu = itk::Math::Round<PointValueType>(((like_upper_bound[i] - upper_bound[i])/spacing[i]));
175
176       if (auxl < 0 || auxu < 0) {
177         std::cerr << "Like-image's bounding box must be larger than input's" << std::endl;
178         return PAD_ERR_NOT_LIKE_LARGER;
179       }
180
181       padLower[i] = (PadBoundType)auxl;
182       padUpper[i] = (PadBoundType)auxu;
183     }
184     
185     if (mArgsInfo.verbose_flag)
186       std::cout << "PadLike - OUT" << std::endl;
187     return PAD_ERR_SUCCESS;
188   }
189 } //end clitk
190
191 #endif  //#define clitkPadImageGenericFilter_cxx