00001
00002 #ifndef vil_nearest_interp_h_
00003 #define vil_nearest_interp_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <vcl_cassert.h>
00016 #include <vcl_cstddef.h>
00017 #include <vil/vil_fwd.h>
00018
00019
00020
00021
00022 template<class T>
00023 inline T vil_nearest_interp_unsafe(double x, double y, const T* data,
00024 int , int ,
00025 vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00026 {
00027 int ix = int(x + 0.5);
00028 int iy = int(y + 0.5);
00029 return *(data + ix*xstep + iy*ystep);
00030 }
00031
00032
00033
00034
00035
00036 template<class T>
00037 inline T vil_nearest_interp_unsafe(const vil_image_view<T>& view, double x, double y, unsigned p=0)
00038 {
00039 return vil_nearest_interp_unsafe(x, y, &view(0,0,p), 0, 0, view.istep(), view.jstep());
00040 }
00041
00042
00043
00044
00045 template<class T>
00046 inline T vil_nearest_interp_safe(double x, double y, const T* data,
00047 int nx, int ny,
00048 vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00049 {
00050 int ix = int(x + 0.5);
00051 int iy = int(y + 0.5);
00052 if (ix >= 0 && ix < nx && iy >= 0 && iy < ny)
00053 return *(data + ix*xstep + iy*ystep);
00054 else
00055 return 0;
00056 }
00057
00058
00059
00060
00061
00062
00063 template<class T>
00064 inline T vil_nearest_interp_safe(
00065 const vil_image_view<T> &view, double x, double y, unsigned p=0)
00066 {
00067 return vil_nearest_interp_safe(x, y, &view(0,0,p), view.ni(), view.nj(),
00068 view.istep(), view.jstep());
00069 }
00070
00071
00072
00073
00074
00075
00076
00077 template<class T>
00078 inline T vil_nearest_interp(double x, double y, const T* data,
00079 int nx, int ny,
00080 vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00081 {
00082 int ix = int(x + 0.5);
00083 int iy = int(y + 0.5);
00084 assert (ix>=0);
00085 assert (iy>=0);
00086 assert (ix<nx);
00087 assert (iy<ny);
00088 return *(data + ix*xstep + iy*ystep);
00089 }
00090
00091
00092
00093
00094
00095
00096
00097 template<class T>
00098 inline T vil_nearest_interp(
00099 const vil_image_view<T> &view, double x, double y, unsigned p=0)
00100 {
00101 return vil_nearest_interp(x, y, &view(0,0,p), view.ni(), view.nj(),
00102 view.istep(), view.jstep());
00103 }
00104
00105
00106
00107
00108
00109
00110 template<class T>
00111 inline T vil_nearest_interp_safe_extend(double x, double y, const T* data,
00112 int nx, int ny,
00113 vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep)
00114 {
00115 int ix = int(x + 0.5);
00116 int iy = int(y + 0.5);
00117 if (ix<0)
00118 ix= 0;
00119 else
00120 if (ix>=nx) ix=nx;
00121
00122 if (iy<0)
00123 iy= 0;
00124 else
00125 if (iy>=ny) iy=ny;
00126
00127 return *(data + ix*xstep + iy*ystep);
00128 }
00129
00130
00131
00132
00133
00134
00135 template<class T>
00136 inline T vil_nearest_interp_safe_extend(
00137 const vil_image_view<T> &view, double x, double y, unsigned p=0)
00138 {
00139 int ix = int(x + 0.5);
00140 int iy = int(y + 0.5);
00141 if (ix<0)
00142 ix= 0.0;
00143 else
00144 if (ix>=(int)view.ni()) ix=view.ni()-1;
00145
00146 if (iy<0)
00147 iy= 0.0;
00148 else
00149 if (iy>=(int)view.nj()) iy=view.nj()-1;
00150
00151 return view(ix, iy, p);
00152 }
00153
00154 #endif // vil_nearest_interp_h_