vil_sample_grid_bicub.hxx
Go to the documentation of this file.
1 // This is core/vil/vil_sample_grid_bicub.hxx
2 #ifndef vil_sample_grid_bicub_hxx_
3 #define vil_sample_grid_bicub_hxx_
4 //:
5 // \file
6 // \brief Bicubic profile sampling 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 "vil_sample_grid_bicub.h"
15 #include <vil/vil_bicub_interp.h>
16 
17 //: This function should not be the same in bicub and bilin
18 inline bool vil_grid_bicub_corner_in_image(double x0, double y0,
19  const vil_image_view_base& image)
20 {
21  return x0 >= 2
22  && y0 >= 2
23  && x0+3 <= image.ni()
24  && y0+3 <= image.nj();
25 }
26 
27 //: Sample along profile, using safe bicubic interpolation
28 // Profile points are along the line between p0 and p1 (in image co-ordinates).
29 // Vector v is resized to n*np elements, where np=image.n_planes().
30 // v[0]..v[np-1] are the values from point p
31 // Points outside image return zero.
32 template <class imType, class vecType>
33 void vil_sample_grid_bicub(vecType* v,
34  const vil_image_view<imType>& image,
35  double x0, double y0, double dx1, double dy1,
36  double dx2, double dy2, int n1, int n2)
37 {
38  bool all_in_image = vil_grid_bicub_corner_in_image(x0,y0,image)
39  && vil_grid_bicub_corner_in_image(x0+(n1-1)*dx1,y0+(n1-1)*dy1,image)
40  && vil_grid_bicub_corner_in_image(x0+(n2-1)*dx2,y0+(n2-1)*dy2,image)
41  && vil_grid_bicub_corner_in_image(x0+(n1-1)*dx1+(n2-1)*dx2,
42  y0+(n1-1)*dy1+(n2-1)*dy2,image);
43 
44  const unsigned ni = image.ni();
45  const unsigned nj = image.nj();
46  const unsigned np = image.nplanes();
47  const std::ptrdiff_t istep = image.istep();
48  const std::ptrdiff_t jstep = image.jstep();
49  const std::ptrdiff_t pstep = image.planestep();
50  double x1=x0;
51  double y1=y0;
52  const imType* plane0 = image.top_left_ptr();
53 
54  if (all_in_image)
55  {
56  if (np==1)
57  {
58  for (int i=0;i<n1;++i,x1+=dx1,y1+=dy1)
59  {
60  double x=x1, y=y1; // Start of j-th row
61  for (int j=0;j<n2;++j,x+=dx2,y+=dy2,++v)
62  *v = (vecType) vil_bicub_interp(x,y,plane0,ni,nj,istep,jstep);
63  }
64  }
65  else
66  {
67  for (int i=0;i<n1;++i,x1+=dx1,y1+=dy1)
68  {
69  double x=x1, y=y1; // Start of j-th row
70  for (int j=0;j<n2;++j,x+=dx2,y+=dy2)
71  {
72  for (unsigned p=0;p<np;++p,++v)
73  *v = (vecType) vil_bicub_interp(x,y,plane0+p*pstep,ni,nj,istep,jstep);
74  }
75  }
76  }
77  }
78  else
79  {
80  // Use safe interpolation
81  if (np==1)
82  {
83  for (int i=0;i<n1;++i,x1+=dx1,y1+=dy1)
84  {
85  double x=x1, y=y1; // Start of j-th row
86  for (int j=0;j<n2;++j,x+=dx2,y+=dy2,++v)
87  *v = (vecType) vil_bicub_interp_safe(x,y,plane0,ni,nj,istep,jstep);
88  }
89  }
90  else
91  {
92  for (int i=0;i<n1;++i,x1+=dx1,y1+=dy1)
93  {
94  double x=x1, y=y1; // Start of j-th row
95  for (int j=0;j<n2;++j,x+=dx2,y+=dy2)
96  {
97  for (unsigned p=0;p<np;++p,++v)
98  *v = (vecType) vil_bicub_interp_safe(x,y,plane0+p*pstep,ni,nj,istep,jstep);
99  }
100  }
101  }
102  }
103 }
104 
105 #define VIL_SAMPLE_GRID_BICUB_INSTANTIATE( imType, vecType ) \
106 template void vil_sample_grid_bicub(vecType* v, \
107  const vil_image_view<imType >& image, \
108  double x0, double y0, double dx1, double dy1, \
109  double dx2, double dy2, int n1, int n2)
110 
111 #endif // vil_sample_grid_bicub_hxx_
An abstract base class of smart pointers to actual image data in memory.
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
void vil_sample_grid_bicub(vecType *v, const vil_image_view< imType > &image, double x0, double y0, double dx1, double dy1, double dx2, double dy2, int n1, int n2)
Sample grid from image, using bicubic interpolation.
Bicubic interpolation functions for 2D images.
bool vil_grid_bicub_corner_in_image(double x0, double y0, const vil_image_view_base &image)
This function should not be the same in bicub and bilin.
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.
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.
T * top_left_ptr()
Pointer to the first (top left in plane 0) pixel.
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.
unsigned nplanes() const
Number of planes.
Bicubic grid sampling function for 2D images.
std::ptrdiff_t istep() const
Add this to your pixel pointer to get next i pixel.