]> Creatis software - bbtk.git/blob - packages/itk/src/bbitkResampleImageFilter.h
#3480 Bug typeid in Macos
[bbtk.git] / packages / itk / src / bbitkResampleImageFilter.h
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbitkResampleImageFilter.h,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:50:39 $
33   Version:   $Revision: 1.13 $
34 =========================================================================*/
35
36
37
38
39 #ifdef _USE_ITK_
40
41 #include <math.h>
42 #include "bbtkAtomicBlackBox.h"
43 #include "itkResampleImageFilter.h"
44 #include "bbitkImage.h"
45 #include "itkNearestNeighborInterpolateImageFunction.h"
46 #include "itkLinearInterpolateImageFunction.h"
47 #include "itkBSplineInterpolateImageFunction.h"
48
49 // For ITK4
50 #include <itkAffineTransform.h>
51
52 namespace bbitk
53 {
54
55  
56  //===================================================
57    class /*BBTK_EXPORT*/ ResampleImageFilter
58     : 
59     public bbtk::AtomicBlackBox
60   {
61     BBTK_BLACK_BOX_INTERFACE(ResampleImageFilter, bbtk::AtomicBlackBox);
62         BBTK_DECLARE_INPUT(In,anyImagePointer);
63         BBTK_DECLARE_INPUT(Spacing,std::vector<double>);
64         BBTK_DECLARE_INPUT(Interpolation,std::string);
65         BBTK_DECLARE_OUTPUT(Out,anyImagePointer);
66     BBTK_PROCESS(ProcessSwitch);
67   private :
68     inline void ProcessSwitch();
69     template <class T> void Process();
70     itk::Object* mOutput;
71   };
72  //===================================================
73    
74  //===================================================
75    BBTK_BEGIN_DESCRIBE_BLACK_BOX(ResampleImageFilter,
76                                 bbtk::AtomicBlackBox);
77   BBTK_NAME("ResampleImageFilter");
78   BBTK_AUTHOR("laurent.guigues at creatis.insa-lyon.fr");
79   BBTK_DESCRIPTION("Resamples an image");
80   BBTK_CATEGORY("image;filter");
81   BBTK_INPUT(ResampleImageFilter,In,"Input image. Can be any itk::Image<T,D>*",anyImagePointer,"");
82   BBTK_INPUT(ResampleImageFilter,Spacing,"Spacing",std::vector<double>,"spacing");
83   BBTK_INPUT(ResampleImageFilter,Interpolation,"Interpolation: Linear (default) || BSpline || NearestNeighbor)  ",std::string,"");
84   BBTK_OUTPUT(ResampleImageFilter,Out,"Output image. Of the same type and dimension than the input image",anyImagePointer,"");
85   BBTK_END_DESCRIBE_BLACK_BOX(ResampleImageFilter);
86  //===================================================
87  
88
89
90  //===================================================
91    void ResampleImageFilter::ProcessSwitch()
92   {
93        printf("EED ResampleImageFilter::ProcessSwitch Start \n");
94     bbtk::TypeInfo t = bbGetInputIn().type();
95     BBTK_TEMPLATE_ITK_IMAGE_SWITCH(t, this->Process);
96        printf("EED ResampleImageFilter::ProcessSwitch End \n");
97   }
98  //===================================================
99  
100  //===================================================
101    template <class T> 
102   void ResampleImageFilter::Process()
103   {
104       printf("EED ResampleImageFilter::Process Start \n");
105     bbtkDebugMessageInc("Core",9,
106                         "bbitk::ResampleImageFilter::Process<"
107                         <<bbtk::TypeName<T>()<<">()"<<std::endl);
108  
109     typedef T ImageType;
110       printf("EED ResampleImageFilter::Process 1 \n");
111     typedef itk::ResampleImageFilter<ImageType,ImageType> FilterType;
112       printf("EED ResampleImageFilter::Process 2 \n");
113     typename FilterType::Pointer filter = FilterType::New();
114     const unsigned int Dimension = ImageType::ImageDimension;
115
116     // Input
117       printf("EED ResampleImageFilter::Process 3 \n");
118     T* in = this->bbGetInputIn().get<T*>();
119       printf("EED ResampleImageFilter::Process 4 \n");
120     filter->SetInput( in );
121       printf("EED ResampleImageFilter::Process 5 \n");
122
123     // Size, Spacing, Origin and DefaultPixelVal
124     typename ImageType::SizeType size;
125     typename ImageType::SpacingType spacing;
126     typename ImageType::PointType origin;
127     typename ImageType::RegionType LPR;
128     LPR = in->GetLargestPossibleRegion();
129     size = LPR.GetSize();
130     //    origin = LPR.GetIndex(); //in->GetOrigin();
131      for (unsigned int i=0;i<Dimension;++i) 
132      {
133                 origin[i]       = LPR.GetIndex()[i]*in->GetSpacing()[i];
134                 spacing[i] = bbGetInputSpacing()[i];
135                 double tmp = (LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i] ) + 0.5;
136                 size[i] = (long)floor(tmp);
137 //      size[i] = (long)lrint(LPR.GetSize()[i]*in->GetSpacing()[i]/spacing[i]);
138        }
139    
140     filter->SetOutputOrigin (origin);
141     filter->SetSize (size);
142     filter->SetOutputSpacing( spacing );
143
144     filter->SetDefaultPixelValue (0);
145     filter->SetOutputDirection( in->GetDirection() );
146
147
148
149     // Transform
150     typedef itk::AffineTransform < double, Dimension> TransformType;
151     
152     // Instance of the transform object to be passed to the resample filter
153     // By default, identity transform is applied
154     typename TransformType::Pointer transform =  TransformType::New();
155     filter->SetTransform ( transform );
156
157     
158  
159     if  ( bbGetInputInterpolation() == "NearestNeighbor" ) {
160       typedef itk::NearestNeighborInterpolateImageFunction < ImageType, double > InterpolatorType;     
161       // We create an interpolator of the found type 
162       typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
163       filter->SetInterpolator( interpolator );
164     }
165     else if  ( bbGetInputInterpolation() == "BSpline") { 
166       typedef itk::BSplineInterpolateImageFunction < ImageType, double > InterpolatorType; 
167       // We create an interpolator of the found type 
168       typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
169       filter->SetInterpolator(interpolator);    
170       // When handling unsigned data, it is possible that the interpolated value is negative
171       // if ( (m_InputImage->GetComponentTypeAsString() == "uchar")   || 
172       //           (m_InputImage->GetComponentTypeAsString() == "ushort")  ||
173       //           (m_InputImage->GetComponentTypeAsString() == "uint") ) {   
174       //        std::cout << "Warning! you're using unsigned data ! The interpolated value may result negative! "<< std::endl;
175       // }
176     } //end else if
177     // Interpolation 
178     else { // if ( bbGetInputInterpolation() == "Linear" ) {
179       typedef itk::LinearInterpolateImageFunction < ImageType, double > InterpolatorType;     
180       // We create an interpolator of the found type 
181       typename InterpolatorType::Pointer interpolator =  InterpolatorType::New();
182       filter->SetInterpolator( interpolator );
183     }
184
185     filter->Update();
186     filter->GetOutput()->Register();
187     if (mOutput) mOutput->UnRegister();
188     this->bbSetOutputOut( filter->GetOutput() );
189     mOutput = filter->GetOutput();
190
191     bbtkDebugDecTab("Core",9);
192       
193       printf("EED ResampleImageFilter::Process End \n");
194
195   }
196    //===================================================
197  
198
199         //-----------------------------------------------------------------     
200         void ResampleImageFilter::bbUserSetDefaultValues()
201         {
202                 std::vector<double> res;
203                 res.push_back(1);
204                 res.push_back(1);
205                 res.push_back(1);
206                 bbSetInputSpacing(res);
207                 mOutput = 0;
208         }
209         
210         //-----------------------------------------------------------------     
211         void ResampleImageFilter::bbUserInitializeProcessing()
212         {
213         }
214         
215         //-----------------------------------------------------------------     
216         void ResampleImageFilter::bbUserFinalizeProcessing()
217         {
218         }       
219         
220
221 } // EO namespace bbitk
222
223 #endif   // _USE_ITK_