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