core/vil/vil_bilin_interp.h

Go to the documentation of this file.
00001 // This is core/vil/vil_bilin_interp.h
00002 #ifndef vil_bilin_interp_h_
00003 #define vil_bilin_interp_h_
00004 //:
00005 // \file
00006 // \brief Bilinear interpolation functions for 2D images
00007 // \author Tim Cootes
00008 //
00009 // The vil bicub source files were derived from the corresponding
00010 // vil bilin files, thus the vil bilin/bicub source files are very
00011 // similar.  If you modify something in this file, there is a
00012 // corresponding bicub file that would likely also benefit from
00013 // the same change.
00014 
00015 #include <vcl_cassert.h>
00016 #include <vcl_cstddef.h>
00017 #include <vil/vil_image_view.h>
00018 
00019 //: Compute bilinear interpolation at (x,y), no bound checks. Requires 0<x<ni-2, 0<y<nj-2
00020 //  Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
00021 //  No bound checks are done.
00022 template<class T>
00023 inline double vil_bilin_interp_unsafe(double x, double y, const T* data,
00024                                       vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00025 {
00026   int p1x=int(x);
00027   double normx = x-p1x;
00028   int p1y=int(y);
00029   double normy = y-p1y;
00030 
00031   const T* pix1 = data + p1y*ystep + p1x*xstep;
00032 
00033   double i1 = pix1[0    ]+(pix1[      ystep]-pix1[0    ])*normy;
00034   double i2 = pix1[xstep]+(pix1[xstep+ystep]-pix1[xstep])*normy;
00035 
00036   return i1+(i2-i1)*normx;
00037 }
00038 
00039 
00040 //: Compute bilinear interpolation at (x,y), no bound checks. Requires 0<x<ni-2, 0<y<nj-2
00041 //  Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
00042 //  No bound checks are done.
00043 //  This is a version of vil_bilin_interp_unsafe with the same function
00044 //  signature as vil_bilin_interp_safe.
00045 template<class T>
00046 inline double vil_bilin_interp_unsafe(double x, double y, const T* data,
00047                                       int /*nx*/, int /*ny*/,
00048                                       vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00049 {
00050   return vil_bilin_interp_unsafe(x, y, data, xstep, ystep);
00051 }
00052 
00053 //: Compute bilinear interpolation at (x,y), no bound checks
00054 //  Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
00055 //  No bound checks are done.
00056 template<class T>
00057 inline double vil_bilin_interp_raw(double x, double y, const T* data,
00058                                    vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00059 {
00060   int p1x=int(x);
00061   double normx = x-p1x;
00062   int p1y=int(y);
00063   double normy = y-p1y;
00064 
00065   const T* pix1 = data + p1y*ystep + p1x*xstep;
00066 
00067   // special boundary cases can be handled more quickly first;
00068   // also avoids accessing an invalid pix1[t] which is going to have weight 0.
00069   if (normx == 0 && normy == 0) return pix1[0];
00070   if (normx == 0) return pix1[0]+(pix1[ystep]-pix1[0])*normy;
00071   if (normy == 0) return pix1[0]+(pix1[xstep]-pix1[0])*normx;
00072 
00073   double i1 = pix1[0    ]+(pix1[      ystep]-pix1[0    ])*normy;
00074   double i2 = pix1[xstep]+(pix1[xstep+ystep]-pix1[xstep])*normy;
00075 
00076   return i1+(i2-i1)*normx;
00077 }
00078 
00079 //: Compute bilinear interpolation at (x,y), no bound checks.
00080 //  Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
00081 //  No bound checks are done.
00082 //  This is a version of vil_bilin_interp_raw with the same function
00083 //  signature as vil_bilin_interp_safe.
00084 template<class T>
00085 inline double vil_bilin_interp_raw(double x, double y, const T* data,
00086                                    int /*nx*/, int /*ny*/,
00087                                    vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00088 {
00089   return vil_bilin_interp_raw(x, y, data, xstep, ystep);
00090 }
00091 
00092 //: Compute bilinear interpolation at (x,y), with bound checks
00093 //  Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
00094 //  If (x,y) is outside interpolatable image region, zero is returned.
00095 //  The safe interpolatable region is [0,nx-1]*[0,ny-1].
00096 template<class T>
00097 inline double vil_bilin_interp_safe(double x, double y, const T* data,
00098                                     int nx, int ny,
00099                                     vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00100 {
00101   if (x<0) return 0.0;
00102   if (y<0) return 0.0;
00103   if (x>nx-1) return 0.0;
00104   if (y>ny-1) return 0.0;
00105   return vil_bilin_interp_raw(x,y,data,xstep,ystep);
00106 }
00107 
00108 //: Compute bilinear interpolation at (x,y), with bound checks
00109 //  If (x,y) is outside interpolatable image region, zero is returned.
00110 //  The safe interpolatable region is [0,view.ni()-1]*[0,view.nj()-1].
00111 // \relates vil_image_view
00112 template<class T>
00113 inline double vil_bilin_interp_safe(const vil_image_view<T> &view,
00114                                     double x, double y, unsigned p=0)
00115 {
00116   return vil_bilin_interp_safe(x, y, &view(0,0,p),
00117                                view.ni(), view.nj(),
00118                                view.istep(), view.jstep());
00119 }
00120 
00121 
00122 //: Compute bilinear interpolation at (x,y), with minimal bound checks
00123 //  Image is nx * ny array of Ts. x,y element is data[ystep*y+xstep*x]
00124 //  If (x,y) is outside interpolatable image region and NDEBUG is not defined
00125 //  the code will fail an ASSERT.
00126 //  The safe interpolatable region is [0,nx-1]*[0,ny-1].
00127 template<class T>
00128 inline double vil_bilin_interp(double x, double y, const T* data,
00129                                int nx, int ny,
00130                                vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00131 {
00132   assert (x>=0);
00133   assert (y>=0);
00134   assert (x<=nx-1);
00135   assert (y<=ny-1);
00136   return vil_bilin_interp_raw(x,y,data,xstep,ystep);
00137 }
00138 
00139 //: Compute bilinear interpolation at (x,y), with minimal bound checks
00140 //  If (x,y) is outside interpolatable image region and NDEBUG is not defined
00141 //  the code will fail an ASSERT.
00142 //  The safe interpolatable region is [0,view.ni()-1]*[0,view.nj()-1].
00143 // \relates vil_image_view
00144 template<class T>
00145 inline double vil_bilin_interp(const vil_image_view<T> &view,
00146                                double x, double y, unsigned p=0)
00147 {
00148   return vil_bilin_interp(x, y, &view(0,0,p),
00149                           view.ni(), view.nj(),
00150                           view.istep(), view.jstep());
00151 }
00152 
00153 
00154 //: Compute bilinear interpolation at (x,y), with bound checks
00155 //  Image is nx * ny array of Ts. x,y element is data[nx*y+x]
00156 //  If (x,y) is outside safe interpolatable image region, nearest pixel value is returned.
00157 //  The safe interpolatable region is [0,nx-1]*[0,ny-1].
00158 template<class T>
00159 inline double vil_bilin_interp_safe_extend(double x, double y, const T* data,
00160                                            int nx, int ny,
00161                                            vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00162 {
00163   if (x<0) x= 0.0;
00164   if (y<0) y= 0.0;
00165   if (x>nx-1) x=nx-1.0;
00166   if (y>ny-1) y=ny-1.0;
00167   return vil_bilin_interp_raw(x,y,data,xstep,ystep);
00168 }
00169 
00170 //: Compute bilinear interpolation at (x,y), with bound checks
00171 //  If (x,y) is outside safe interpolatable image region, nearest pixel value is returned.
00172 //  The safe interpolatable region is [0,view.ni()-1]*[0,view.nj()-1].
00173 // \relates vil_image_view
00174 template<class T>
00175 inline double vil_bilin_interp_safe_extend(const vil_image_view<T> &view,
00176                                            double x, double y, unsigned p=0)
00177 {
00178   return vil_bilin_interp_safe_extend(x, y, &view(0,0,p),
00179                                       view.ni(), view.nj(),
00180                                       view.istep(), view.jstep());
00181 }
00182 
00183 #endif // vil_bilin_interp_h_

Generated on Thu Aug 28 05:08:06 2008 for core/vil by  doxygen 1.5.1