1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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
21 /* =================================================
22 * @file clitkCropImageGenericFilter.cxx
28 ===================================================*/
30 #include "clitkCropImageGenericFilter.h"
37 //-----------------------------------------------------------
39 //-----------------------------------------------------------
40 CropImageGenericFilter::CropImageGenericFilter():
41 ImageToImageGenericFilter<Self>("CropImage")
43 cmdline_parser_clitkCropImage_init(&mArgsInfo);
44 InitializeImageType<2>();
45 InitializeImageType<3>();
46 InitializeImageType<4>();
49 //--------------------------------------------------------------------
50 template<unsigned int Dim>
51 void clitk::CropImageGenericFilter::InitializeImageType()
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);
61 //--------------------------------------------------------------------
63 //--------------------------------------------------------------------
64 void clitk::CropImageGenericFilter::SetArgsInfo(const args_info_type& 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);
72 //--------------------------------------------------------------------
74 //--------------------------------------------------------------------
75 // Update with the number of dimensions and the pixeltype
76 //--------------------------------------------------------------------
77 template<class ImageType>
78 void clitk::CropImageGenericFilter::UpdateWithInputImageType()
81 typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
84 if (mArgsInfo.BG_given && mArgsInfo.like_given)
85 clitkExceptionMacro("Do not use --BG and --like at the same time");
88 typename ImageType::Pointer output;
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);
103 output = filter->GetOutput();
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);
120 output = filter->GetOutput();
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;
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]);
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]);
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);
150 output = filter->GetOutput();
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);
161 // Write/Save results
162 this->template SetNextOutput<ImageType>(output);
164 //--------------------------------------------------------------------
168 #endif //#define clitkCropImageGenericFilter_cxx