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