[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

interpolating_accessor.hxx VIGRA

1 /************************************************************************/
2 /* */
3 /* Copyright 1998-2002 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35 
36 #ifndef VIGRA_INTERPOLATING_ACCESSOR_HXX
37 #define VIGRA_INTERPOLATING_ACCESSOR_HXX
38 
39 
40 #include "accessor.hxx"
41 #include "diff2d.hxx"
42 
43 namespace vigra {
44 
45 /** \addtogroup DataAccessors
46 */
47 //@{
48 
49 /********************************************************/
50 /* */
51 /* BilinearInterpolatingAccessor */
52 /* */
53 /********************************************************/
54 
55 /** \brief Bilinear interpolation at non-integer positions.
56 
57  This accessor allows an image be accessed at arbitrary non-integer
58  coordinates and performs an bi-linear interpolation to
59  obtain a pixel value.
60  It uses the given ACCESSOR (which is usually the
61  accessor originally associated with the iterator)
62  to access data.
63 
64  <b>\#include</b> <vigra/accessor.hxx>
65  Namespace: vigra
66 
67  <b> Required Interface:</b>
68 
69  \code
70  ITERATOR iter;
71  ACCESSOR a;
72  VALUETYPE destvalue;
73  float s;
74  int x, y;
75 
76  destvalue = s * a(iter, x, y) + s * a(iter, x, y);
77 
78  \endcode
79 */
80 template <class ACCESSOR, class VALUETYPE>
82 {
83  public:
84  /** the iterators' pixel type
85  */
86  typedef VALUETYPE value_type;
87 
88  /** init from given accessor
89  */
91  : a_(a)
92  {}
93 
94  /** Interpolate the data item at a non-integer position.
95  Ensure that no outside pixels are accessed if
96  (x, y) is near the image border (as long as
97  0 <= x <= width-1, 0 <= y <= height-1).
98  */
99  template <class ITERATOR>
100  value_type operator()(ITERATOR const & i, float x, float y) const
101  {
102  int ix = int(x);
103  int iy = int(y);
104  float dx = x - ix;
105  float dy = y - iy;
106 
107  value_type ret;
108 
109  // avoid dereferencing the iterator outside its range
110  if(dx == 0.0)
111  {
112  if(dy == 0.0)
113  {
114  ret = a_(i, Diff2D(ix, iy));
115  }
116  else
117  {
118  ret = detail::RequiresExplicitCast<value_type>::cast(
119  (1.0 - dy) * a_(i, Diff2D(ix, iy)) +
120  dy * a_(i, Diff2D(ix, iy + 1)));
121  }
122  }
123  else
124  {
125  if(dy == 0.0)
126  {
127  ret = detail::RequiresExplicitCast<value_type>::cast(
128  (1.0 - dx) * a_(i, Diff2D(ix, iy)) +
129  dx * a_(i, Diff2D(ix + 1, iy)));
130  }
131  else
132  {
133  ret = detail::RequiresExplicitCast<value_type>::cast(
134  (1.0 - dx) * (1.0 - dy) * a_(i, Diff2D(ix, iy)) +
135  dx * (1.0 - dy) * a_(i, Diff2D(ix + 1, iy)) +
136  (1.0 - dx) * dy * a_(i, Diff2D(ix, iy + 1)) +
137  dx * dy * a_(i, Diff2D(ix + 1, iy + 1)));
138  }
139  }
140 
141  return ret;
142  }
143 
144  /** Interpolate the data item at a non-integer position.
145  This function works as long as 0 <= x < width-1,
146  0 <= y < height-1. It is slightly faster than <TT>operator()</TT>.
147  */
148  template <class ITERATOR>
149  value_type unchecked(ITERATOR const & i, float x, float y) const
150  {
151  int ix = int(x);
152  int iy = int(y);
153  float dx = x - ix;
154  float dy = y - iy;
155  return detail::RequiresExplicitCast<value_type>::cast(
156  (1.0 - dx) * (1.0 - dy) * a_(i, Diff2D(ix, iy)) +
157  dx * (1.0 - dy) * a_(i, Diff2D(ix + 1, iy)) +
158  (1.0 - dx) * dy * a_(i, Diff2D(ix, iy + 1)) +
159  dx * dy * a_(i, Diff2D(ix + 1, iy + 1)));
160  }
161 
162  private:
163  ACCESSOR a_;
164 };
165 
166 //@}
167 
168 } // namespace vigra
169 
170 #endif /* VIGRA_INTERPOLATING_ACCESSOR_HXX */
BilinearInterpolatingAccessor(ACCESSOR a)
Definition: interpolating_accessor.hxx:90
Two dimensional difference vector.
Definition: diff2d.hxx:185
Bilinear interpolation at non-integer positions.
Definition: interpolating_accessor.hxx:81
value_type unchecked(ITERATOR const &i, float x, float y) const
Definition: interpolating_accessor.hxx:149
value_type operator()(ITERATOR const &i, float x, float y) const
Definition: interpolating_accessor.hxx:100
VALUETYPE value_type
Definition: interpolating_accessor.hxx:86

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.11.1 (Fri May 19 2017)