vil_bicub_interp.h
Go to the documentation of this file.
1 // This is core/vil/vil_bicub_interp.h
2 #ifndef vil_bicub_interp_h_
3 #define vil_bicub_interp_h_
4 //:
5 // \file
6 // \brief Bicubic interpolation functions for 2D images
7 //
8 // The vil bicub source files were derived from the corresponding
9 // vil bilin files, thus the vil bilin/bicub source files are very
10 // similar. If you modify something in this file, there is a
11 // corresponding bilin file that would likely also benefit from
12 // the same change.
13 
14 #include <cstddef>
15 #include <cassert>
16 #ifdef _MSC_VER
17 # include <vcl_msvc_warnings.h>
18 #endif
19 #include <vil/vil_image_view.h>
20 
21 //: Compute bicubic interpolation at (x,y), no bound checks. Requires 1<x<ni-3, 1<y<nj-3
22 // Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
23 // No bound checks are done.
24 template<class T>
25 double vil_bicub_interp_unsafe(double x, double y, const T* data,
26  std::ptrdiff_t xstep, std::ptrdiff_t ystep);
27 
28 //: Compute bicubic interpolation at (x,y), no bound checks. Requires 1<x<ni-3, 1<y<nj-3
29 // Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
30 // No bound checks are done.
31 // This is a version of vil_bicub_interp_unsafe with the same function
32 // signature as vil_bicub_interp_safe.
33 template<class T>
34 inline double vil_bicub_interp_unsafe(double x, double y, const T* data,
35  int /*nx*/, int /*ny*/,
36  std::ptrdiff_t xstep, std::ptrdiff_t ystep)
37 {
38  return vil_bicub_interp_unsafe(x, y, data, xstep, ystep);
39 }
40 
41 
42 
43 //: Compute bicubic interpolation at (x,y), no bound checks
44 // Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
45 // No bound checks are done.
46 template<class T>
47 double vil_bicub_interp_raw(double x, double y, const T* data,
48  std::ptrdiff_t xstep, std::ptrdiff_t ystep);
49 
50 
51 //: Compute bicubic interpolation at (x,y), no bound checks
52 // Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
53 // No bound checks are done.
54 // This is a version of vil_bicub_interp_raw with the same function
55 // signature as vil_bicub_interp_safe.
56 template<class T>
57 inline double vil_bicub_interp_raw(double x, double y, const T* data,
58  int /*nx*/, int /*ny*/,
59  std::ptrdiff_t xstep, std::ptrdiff_t ystep)
60 {
61  return vil_bicub_interp_raw(x, y, data, xstep, ystep);
62 }
63 
64 //: Compute bicubic interpolation at (x,y), with bound checks
65 // Image is nx * ny array of Ts. x,y element is data[xstep*x+ystep*y]
66 // If (x,y) is outside interpolatable image region, zero is returned.
67 // The safe interpolatable region is [1,nx-2]*[1,ny-2].
68 template<class T>
69 inline double vil_bicub_interp_safe(double x, double y, const T* data,
70  int nx, int ny,
71  std::ptrdiff_t xstep, std::ptrdiff_t ystep)
72 {
73  if (x<1) return 0.0;
74  if (y<1) return 0.0;
75  if (x>nx-2) return 0.0;
76  if (y>ny-2) return 0.0;
77  return vil_bicub_interp_raw(x,y,data,xstep,ystep);
78 }
79 
80 //: Compute bicubic interpolation at (x,y), with bound checks
81 // If (x,y) is outside interpolatable image region, zero is returned.
82 // The safe interpolatable region is [1,view.ni()-2]*[1,view.nj()-2].
83 // \relatesalso vil_image_view
84 template<class T>
85 inline double vil_bicub_interp_safe(const vil_image_view<T> &view,
86  double x, double y, unsigned p=0)
87 {
88  return vil_bicub_interp_safe(x, y, &view(0,0,p),
89  view.ni(), view.nj(),
90  view.istep(), view.jstep());
91 }
92 
93 //: Compute bicubic interpolation at (x,y), with minimal bound checks
94 // Image is nx * ny array of Ts. x,y element is data[ystep*y+xstep*x]
95 // If (x,y) is outside interpolatable image region and NDEBUG is not defined
96 // the code will fail an ASSERT.
97 // The safe interpolatable region is [1,nx-2]*[1,ny-2].
98 template<class T>
99 inline double vil_bicub_interp(double x, double y, const T* data,
100  int nx, int ny,
101  std::ptrdiff_t xstep, std::ptrdiff_t ystep)
102 {
103  assert (x>=1);
104  assert (y>=1);
105  assert (x<=nx-2);
106  assert (y<=ny-2);
107  return vil_bicub_interp_raw(x,y,data,xstep,ystep);
108 }
109 
110 //: Compute bicubic interpolation at (x,y), with minimal bound checks
111 // If (x,y) is outside interpolatable image region and NDEBUG is not defined
112 // the code will fail an ASSERT.
113 // The safe interpolatable region is [1,view.ni()-2]*[1,view.nj()-2].
114 // \relatesalso vil_image_view
115 template<class T>
116 inline double vil_bicub_interp(const vil_image_view<T> &view,
117  double x, double y, unsigned p=0)
118 {
119  return vil_bicub_interp(x, y, &view(0,0,p),
120  view.ni(), view.nj(),
121  view.istep(), view.jstep());
122 }
123 
124 //: Compute bicubic interpolation at (x,y), with bound checks
125 // Image is nx * ny array of Ts. x,y element is data[nx*y+x]
126 // If (x,y) is outside safe interpolatable image region, nearest pixel value is returned.
127 // The safe interpolatable region is [1,nx-2]*[1,ny-2].
128 template<class T>
129 inline double vil_bicub_interp_safe_extend(double x, double y, const T* data,
130  int nx, int ny,
131  std::ptrdiff_t xstep, std::ptrdiff_t ystep)
132 {
133  if (x<1) x= 0.0;
134  if (y<1) y= 0.0;
135  if (x>nx-2) x=nx-1.0;
136  if (y>ny-2) y=ny-1.0;
137  return vil_bicub_interp_raw(x,y,data,xstep,ystep);
138 }
139 
140 //: Compute bicubic interpolation at (x,y), with bound checks
141 // If (x,y) is outside safe interpolatable image region, nearest pixel value is returned.
142 // The safe interpolatable region is [1,view.ni()-2]*[1,view.nj()-2].
143 // \relatesalso vil_image_view
144 template<class T>
146  double x, double y, unsigned p=0)
147 {
148  return vil_bicub_interp_safe_extend(x, y, &view(0,0,p),
149  view.ni(), view.nj(),
150  view.istep(), view.jstep());
151 }
152 
153 #endif // vil_bicub_interp_h_
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
double vil_bicub_interp_unsafe(double x, double y, const T *data, std::ptrdiff_t xstep, std::ptrdiff_t ystep)
Compute bicubic interpolation at (x,y), no bound checks. Requires 1<x<ni-3, 1<y<nj-3.
std::ptrdiff_t jstep() const
Add this to your pixel pointer to get next j pixel.
unsigned ni() const
Width.
unsigned nj() const
Height.
double vil_bicub_interp_raw(double x, double y, const T *data, std::ptrdiff_t xstep, std::ptrdiff_t ystep)
Compute bicubic interpolation at (x,y), no bound checks.
A base class reference-counting view of some image data.
double vil_bicub_interp(const vil_image_view< T > &view, double x, double y, unsigned p=0)
Compute bicubic interpolation at (x,y), with minimal bound checks.
double vil_bicub_interp_safe(const vil_image_view< T > &view, double x, double y, unsigned p=0)
Compute bicubic interpolation at (x,y), with bound checks.
double vil_bicub_interp_safe_extend(const vil_image_view< T > &view, double x, double y, unsigned p=0)
Compute bicubic interpolation at (x,y), with bound checks.
std::ptrdiff_t istep() const
Add this to your pixel pointer to get next i pixel.