core/vil/vil_bicub_interp.h
Go to the documentation of this file.
00001 // This is core/vil/vil_bicub_interp.h
00002 #ifndef vil_bicub_interp_h_
00003 #define vil_bicub_interp_h_
00004 //:
00005 // \file
00006 // \brief Bicubic interpolation functions for 2D images
00007 //
00008 // The vil bicub source files were derived from the corresponding
00009 // vil bilin files, thus the vil bilin/bicub source files are very
00010 // similar.  If you modify something in this file, there is a
00011 // corresponding bilin file that would likely also benefit from
00012 // the same change.
00013 
00014 #include <vcl_cassert.h>
00015 #include <vcl_cstddef.h>
00016 #include <vil/vil_image_view.h>
00017 
00018 //: Compute bicubic interpolation at (x,y), no bound checks. Requires 1<x<ni-3, 1<y<nj-3
00019 //  Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
00020 //  No bound checks are done.
00021 template<class T>
00022 double vil_bicub_interp_unsafe(double x, double y, const T* data,
00023                                vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep);
00024 
00025 //: Compute bicubic interpolation at (x,y), no bound checks. Requires 1<x<ni-3, 1<y<nj-3
00026 //  Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
00027 //  No bound checks are done.
00028 //  This is a version of vil_bicub_interp_unsafe with the same function
00029 //  signature as vil_bicub_interp_safe.
00030 template<class T>
00031 inline double vil_bicub_interp_unsafe(double x, double y, const T* data,
00032                                       int /*nx*/, int /*ny*/,
00033                                       vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00034 {
00035   return vil_bicub_interp_unsafe(x, y, data, xstep, ystep);
00036 }
00037 
00038 
00039 
00040 //: Compute bicubic interpolation at (x,y), no bound checks
00041 //  Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
00042 //  No bound checks are done.
00043 template<class T>
00044 double vil_bicub_interp_raw(double x, double y, const T* data,
00045                             vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep);
00046 
00047                             
00048 //: Compute bicubic interpolation at (x,y), no bound checks
00049 //  Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
00050 //  No bound checks are done.
00051 //  This is a version of vil_bicub_interp_raw with the same function
00052 //  signature as vil_bicub_interp_safe.
00053 template<class T>
00054 inline double vil_bicub_interp_raw(double x, double y, const T* data,
00055                                    int /*nx*/, int /*ny*/,
00056                                    vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00057 {
00058   return vil_bicub_interp_raw(x, y, data, xstep, ystep);
00059 }
00060                             
00061 //: Compute bicubic interpolation at (x,y), with bound checks
00062 //  Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
00063 //  If (x,y) is outside interpolatable image region, zero is returned.
00064 //  The safe interpolatable region is [1,nx-2]*[1,ny-2].
00065 template<class T>
00066 inline double vil_bicub_interp_safe(double x, double y, const T* data,
00067                                     int nx, int ny,
00068                                     vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00069 {
00070     if (x<1) return 0.0;
00071     if (y<1) return 0.0;
00072     if (x>nx-2) return 0.0;
00073     if (y>ny-2) return 0.0;
00074     return vil_bicub_interp_raw(x,y,data,xstep,ystep);
00075 }
00076 
00077 //: Compute bicubic interpolation at (x,y), with bound checks
00078 //  If (x,y) is outside interpolatable image region, zero is returned.
00079 //  The safe interpolatable region is [1,view.ni()-2]*[1,view.nj()-2].
00080 // \relatesalso vil_image_view
00081 template<class T>
00082 inline double vil_bicub_interp_safe(const vil_image_view<T> &view,
00083                                     double x, double y, unsigned p=0)
00084 {
00085     return vil_bicub_interp_safe(x, y, &view(0,0,p),
00086                                  view.ni(), view.nj(),
00087                                  view.istep(), view.jstep());
00088 }
00089 
00090 //: Compute bicubic interpolation at (x,y), with minimal bound checks
00091 //  Image is nx * ny array of Ts. x,y element is data[ystep*y+xstep*x]
00092 //  If (x,y) is outside interpolatable image region and NDEBUG is not defined
00093 //  the code will fail an ASSERT.
00094 //  The safe interpolatable region is [1,nx-2]*[1,ny-2].
00095 template<class T>
00096 inline double vil_bicub_interp(double x, double y, const T* data,
00097                                int nx, int ny,
00098                                vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00099 {
00100     assert (x>=1);
00101     assert (y>=1);
00102     assert (x<=nx-2);
00103     assert (y<=ny-2);
00104     return vil_bicub_interp_raw(x,y,data,xstep,ystep);
00105 }
00106 
00107 //: Compute bicubic interpolation at (x,y), with minimal bound checks
00108 //  If (x,y) is outside interpolatable image region and NDEBUG is not defined
00109 //  the code will fail an ASSERT.
00110 //  The safe interpolatable region is [1,view.ni()-2]*[1,view.nj()-2].
00111 // \relatesalso vil_image_view
00112 template<class T>
00113 inline double vil_bicub_interp(const vil_image_view<T> &view,
00114                                double x, double y, unsigned p=0)
00115 {
00116     return vil_bicub_interp(x, y, &view(0,0,p),
00117                             view.ni(), view.nj(),
00118                             view.istep(), view.jstep());
00119 }
00120 
00121 //: Compute bicubic interpolation at (x,y), with bound checks
00122 //  Image is nx * ny array of Ts. x,y element is data[nx*y+x]
00123 //  If (x,y) is outside safe interpolatable image region, nearest pixel value is returned.
00124 //  The safe interpolatable region is [1,nx-2]*[1,ny-2].
00125 template<class T>
00126 inline double vil_bicub_interp_safe_extend(double x, double y, const T* data,
00127                                            int nx, int ny,
00128                                            vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00129 {
00130     if (x<1) x= 0.0;
00131     if (y<1) y= 0.0;
00132     if (x>nx-2) x=nx-1.0;
00133     if (y>ny-2) y=ny-1.0;
00134     return vil_bicub_interp_raw(x,y,data,xstep,ystep);
00135 }
00136 
00137 //: Compute bicubic interpolation at (x,y), with bound checks
00138 //  If (x,y) is outside safe interpolatable image region, nearest pixel value is returned.
00139 //  The safe interpolatable region is [1,view.ni()-2]*[1,view.nj()-2].
00140 // \relatesalso vil_image_view
00141 template<class T>
00142 inline double vil_bicub_interp_safe_extend(const vil_image_view<T> &view,
00143                                            double x, double y, unsigned p=0)
00144 {
00145     return vil_bicub_interp_safe_extend(x, y, &view(0,0,p),
00146                                         view.ni(), view.nj(),
00147                                         view.istep(), view.jstep());
00148 }
00149 
00150 #endif // vil_bicub_interp_h_