]> Creatis software - clitk.git/blob - tools/clitkCropImageGenericFilter.cxx
b8d77445eaa2dbd3f89703d18ca50167eb31a5c9
[clitk.git] / tools / clitkCropImageGenericFilter.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 clitkCropImageGenericFilter_cxx
19 #define clitkCropImageGenericFilter_cxx
20
21 /* =================================================
22  * @file   clitkCropImageGenericFilter.cxx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30 #include "clitkCropImageGenericFilter.h"
31
32
33 namespace clitk
34 {
35
36
37   //-----------------------------------------------------------
38   // Constructor
39   //-----------------------------------------------------------
40   CropImageGenericFilter::CropImageGenericFilter():
41     ImageToImageGenericFilter<Self>("CropImage")
42   {
43     cmdline_parser_clitkCropImage_init(&mArgsInfo);
44     InitializeImageType<2>();
45     InitializeImageType<3>();
46     InitializeImageType<4>();
47   }
48
49   //--------------------------------------------------------------------
50   template<unsigned int Dim>
51   void clitk::CropImageGenericFilter::InitializeImageType()
52   {
53     ADD_DEFAULT_IMAGE_TYPES(Dim);
54     //ADD_IMAGE_TYPE(Dim, uchar);
55     //ADD_IMAGE_TYPE(Dim, short);
56     // ADD_IMAGE_TYPE(Dim, uint);
57     //  ADD_IMAGE_TYPE(Dim, ulong);
58     // ADD_IMAGE_TYPE(Dim, int);
59     // ADD_IMAGE_TYPE(Dim, float);
60   }
61   //--------------------------------------------------------------------
62
63   //--------------------------------------------------------------------
64   void clitk::CropImageGenericFilter::SetArgsInfo(const args_info_type& a) 
65   {
66     mArgsInfo=a;
67     SetIOVerbose(mArgsInfo.verbose_flag);
68     if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
69     if (mArgsInfo.input_given)   AddInputFilename(mArgsInfo.input_arg);
70     if (mArgsInfo.output_given)  AddOutputFilename(mArgsInfo.output_arg);
71   }
72   //--------------------------------------------------------------------
73
74   //--------------------------------------------------------------------
75   // Update with the number of dimensions and the pixeltype
76   //--------------------------------------------------------------------
77   template<class ImageType>
78   void clitk::CropImageGenericFilter::UpdateWithInputImageType() 
79   { 
80     // Reading input
81     typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
82
83     // Check options
84     if (mArgsInfo.BG_given && mArgsInfo.like_given)
85       clitkExceptionMacro("Do not use --BG and --like at the same time");    
86
87     // Prepare output
88     typename ImageType::Pointer output;
89     
90     // ------------------------------------------------
91     if (mArgsInfo.BG_given) { // AutoCrop filter
92       if (mArgsInfo.boundingBox_given) 
93         clitkExceptionMacro("Do not use --BG and --boundingBox at the same time");    
94       if (mArgsInfo.lower_given) 
95         clitkExceptionMacro("Do not use --BG and --lower at the same time");    
96       if (mArgsInfo.upper_given) 
97         clitkExceptionMacro("Do not use --BG and --upper at the same time");    
98       typedef clitk::AutoCropFilter<ImageType> FilterType;
99       typename FilterType::Pointer filter = FilterType::New();
100       filter->SetInput(input);
101       filter->SetBackgroundValue(mArgsInfo.BG_arg);
102       filter->Update();
103       output = filter->GetOutput();
104     }
105     else {
106       // ------------------------------------------------
107       if (mArgsInfo.like_given) { // CropLike filter
108       if (mArgsInfo.boundingBox_given) 
109         clitkExceptionMacro("Do not use --like and --boundingBox at the same time");    
110       if (mArgsInfo.lower_given) 
111         clitkExceptionMacro("Do not use --like and --lower at the same time");    
112       if (mArgsInfo.upper_given) 
113         clitkExceptionMacro("Do not use --like and --upper at the same time");    
114         typedef clitk::CropLikeImageFilter<ImageType> FilterType;
115         typename FilterType::Pointer filter = FilterType::New();
116         filter->SetInput(input);
117         filter->SetCropLikeFilename(mArgsInfo.like_arg);
118         filter->SetBackgroundValue(mArgsInfo.BGLike_arg);
119         filter->Update();
120         output = filter->GetOutput();
121       }
122       else {
123         // ------------------------------------------------
124         typename ImageType::SizeType lSize;
125         typename ImageType::SizeType uSize;
126         if (mArgsInfo.boundingBox_given) {
127           for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
128             lSize[i] = mArgsInfo.boundingBox_arg[2*i];
129             uSize[i] = input->GetLargestPossibleRegion().GetSize()[i]-mArgsInfo.boundingBox_arg[2*i+1]-1;
130           }
131         }
132         else {
133           if (mArgsInfo.lower_given) {
134             for(unsigned int i=0; i<ImageType::ImageDimension; i++) 
135               lSize[i]=static_cast<unsigned int >(mArgsInfo.lower_arg[i]);
136           }
137           else lSize.Fill(0);
138           if (mArgsInfo.upper_given) {
139             for(unsigned int i=0; i<ImageType::ImageDimension; i++)
140               uSize[i]=static_cast<unsigned int >(mArgsInfo.upper_arg[i]);
141           }
142           else uSize.Fill(0);
143         }
144         typedef  itk::CropImageFilter<ImageType, ImageType> CropImageFilterType;
145         typename CropImageFilterType::Pointer filter=CropImageFilterType::New();
146         filter->SetInput(input);
147         filter->SetLowerBoundaryCropSize(lSize);
148         filter->SetUpperBoundaryCropSize(uSize);
149         filter->Update();
150         output = filter->GetOutput();    
151       }
152     }
153
154     // Force origin if needed
155     if (mArgsInfo.origin_flag) {
156       typename ImageType::PointType origin;
157       origin.Fill(itk::NumericTraits<double>::Zero);
158       output->SetOrigin(origin);
159     }
160
161     // Write/Save results
162     this->template SetNextOutput<ImageType>(output); 
163   }
164   //--------------------------------------------------------------------
165
166 } //end clitk
167
168 #endif  //#define clitkCropImageGenericFilter_cxx