vil_sample_profile_bilin.hxx
Go to the documentation of this file.
1 // This is core/vil/vil_sample_profile_bilin.hxx
2 #ifndef vil_sample_profile_bilin_hxx_
3 #define vil_sample_profile_bilin_hxx_
4 //:
5 // \file
6 // \brief Bilinear profile sampling functions for 2D images
7 // \author Tim Cootes
8 //
9 // The vil bicub source files were derived from the corresponding
10 // vil bilin files, thus the vil bilin/bicub source files are very
11 // similar. If you modify something in this file, there is a
12 // corresponding bicub file that would likely also benefit from
13 // the same change.
14 
16 #include <vil/vil_bilin_interp.h>
17 
18 //: This function should not be the same in bicub and bilin
19 inline bool vil_profile_bilin_in_image(double x0, double y0,
20  double x1, double y1,
21  const vil_image_view_base& image)
22 {
23  return x0 >= 1
24  && y0 >= 1
25  && x1 >= 1
26  && y1 >= 1
27  && x0+2 <= image.ni()
28  && y0+2 <= image.nj()
29  && x1+2 <= image.ni()
30  && y1+2 <= image.nj();
31 }
32 
33 //: Sample along profile, using safe bilinear interpolation
34 // Profile points are along the line between p0 and p1 (in image co-ordinates).
35 // Vector v is resized to n*np elements, where np=image.n_planes().
36 // v[0]..v[np-1] are the values from point p
37 // Points outside image return zero.
38 template <class imType, class vecType>
39 void vil_sample_profile_bilin(vecType* v,
40  const vil_image_view<imType>& image,
41  double x0, double y0, double dx, double dy,
42  int n)
43 {
44  bool all_in_image = vil_profile_bilin_in_image(x0,y0,x0+(n-1)*dx,y0+(n-1)*dy,image);
45 
46  const unsigned ni = image.ni();
47  const unsigned nj = image.nj();
48  const unsigned np = image.nplanes();
49  const std::ptrdiff_t istep = image.istep();
50  const std::ptrdiff_t jstep = image.jstep();
51  const std::ptrdiff_t pstep = image.planestep();
52  double x=x0;
53  double y=y0;
54  const imType* plane0 = image.top_left_ptr();
55 
56  if (all_in_image)
57  {
58  if (np==1)
59  {
60  for (int k=0;k<n;++k,x+=dx,y+=dy)
61  v[k] = vil_bilin_interp(x,y,plane0,ni,nj,istep,jstep);
62  }
63  else
64  {
65  for (int k=0;k<n;++k,x+=dx,y+=dy)
66  {
67  for (unsigned int p=0;p<np;++p,++v)
68  *v = vil_bilin_interp(x,y,plane0+p*pstep,ni,nj,istep,jstep);
69  }
70  }
71  }
72  else
73  {
74  // Use safe interpolation
75  if (np==1)
76  {
77  for (int k=0;k<n;++k,x+=dx,y+=dy)
78  v[k] = vil_bilin_interp_safe(x,y,plane0,ni,nj,istep,jstep);
79  }
80  else
81  {
82  for (int k=0;k<n;++k,x+=dx,y+=dy)
83  {
84  for (unsigned int p=0;p<np;++p,++v)
85  *v = vil_bilin_interp_safe(x,y,plane0+p*pstep,ni,nj,istep,jstep);
86  }
87  }
88  }
89 }
90 
91 //: Sample along profile, using bilinear interpolation
92 // Profile points are along the line between p0 and p1 (in image co-ordinates).
93 // Vector v is resized to n*np elements, where np=image.n_planes().
94 // v[0]..v[np-1] are the values from point p
95 // Points outside image return NA.
96 template <class imType, class vecType>
98  const vil_image_view<imType>& image,
99  double x0, double y0, double dx, double dy,
100  int n)
101 {
102  bool all_in_image = vil_profile_bilin_in_image(x0,y0,x0+(n-1)*dx,y0+(n-1)*dy,image);
103 
104  const unsigned ni = image.ni();
105  const unsigned nj = image.nj();
106  const unsigned np = image.nplanes();
107  const std::ptrdiff_t istep = image.istep();
108  const std::ptrdiff_t jstep = image.jstep();
109  const std::ptrdiff_t pstep = image.planestep();
110  double x=x0;
111  double y=y0;
112  const imType* plane0 = image.top_left_ptr();
113 
114  if (all_in_image)
115  {
116  if (np==1)
117  {
118  for (int k=0;k<n;++k,x+=dx,y+=dy)
119  v[k] = vil_bilin_interp(x,y,plane0,ni,nj,istep,jstep);
120  }
121  else
122  {
123  for (int k=0;k<n;++k,x+=dx,y+=dy)
124  {
125  for (unsigned int p=0;p<np;++p,++v)
126  *v = vil_bilin_interp(x,y,plane0+p*pstep,ni,nj,istep,jstep);
127  }
128  }
129  }
130  else
131  {
132  // Use safe interpolation
133  if (np==1)
134  {
135  for (int k=0;k<n;++k,x+=dx,y+=dy)
136  v[k] = vil_bilin_interp_safe_edgena(x,y,plane0,ni,nj,istep,jstep);
137  }
138  else
139  {
140  for (int k=0;k<n;++k,x+=dx,y+=dy)
141  {
142  for (unsigned int p=0;p<np;++p,++v)
143  *v = vil_bilin_interp_safe_edgena(x,y,plane0+p*pstep,ni,nj,istep,jstep);
144  }
145  }
146  }
147 }
148 
149 #define VIL_SAMPLE_PROFILE_BILIN_INSTANTIATE( imType, vecType ) \
150 template void vil_sample_profile_bilin(vecType* v, \
151  const vil_image_view<imType >& image, \
152  double x0, double y0, \
153  double dx, double dy, \
154  int n); \
155 template void vil_sample_profile_bilin_edgena(vecType* v, \
156  const vil_image_view<imType >& image, \
157  double x0, double y0, \
158  double dx, double dy, \
159  int n)
160 #endif // vil_sample_profile_bilin_hxx_
An abstract base class of smart pointers to actual image data in memory.
double vil_bilin_interp(const vil_image_view< T > &view, double x, double y, unsigned p=0)
Compute bilinear interpolation at (x,y), with minimal bound checks.
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
bool vil_profile_bilin_in_image(double x0, double y0, double x1, double y1, const vil_image_view_base &image)
This function should not be the same in bicub and bilin.
double vil_bilin_interp_safe(const vil_image_view< T > &view, double x, double y, unsigned p=0)
Compute bilinear interpolation at (x,y), with bound checks.
double vil_bilin_interp_safe_edgena(const vil_image_view< T > &view, double x, double y, unsigned p=0)
Compute bilinear interpolation at (x,y), with bound checks.
std::ptrdiff_t jstep() const
Add this to your pixel pointer to get next j pixel.
unsigned ni() const
Width.
unsigned nj() const
Height.
#define v
std::ptrdiff_t planestep() const
Add this to your pixel pointer to get pixel on next plane.
Bilinear profile sampling functions for 2D images.
T * top_left_ptr()
Pointer to the first (top left in plane 0) pixel.
unsigned nplanes() const
Number of planes.
void vil_sample_profile_bilin_edgena(vecType *v, const vil_image_view< imType > &image, double x0, double y0, double dx, double dy, int n)
Sample along profile, using bilinear interpolation.
std::ptrdiff_t istep() const
Add this to your pixel pointer to get next i pixel.
void vil_sample_profile_bilin(vecType *v, const vil_image_view< imType > &image, double x0, double y0, double dx, double dy, int n)
Sample along profile, using bilinear interpolation.
Bilinear interpolation functions for 2D images.