Boost GIL


position_iterator.hpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_POSITION_ITERATOR_HPP
9 #define BOOST_GIL_POSITION_ITERATOR_HPP
10 
11 #include <boost/gil/locator.hpp>
12 
13 #include <boost/iterator/iterator_facade.hpp>
14 
15 namespace boost { namespace gil {
16 
20 
21 
25 template <typename Deref, // A function object that given a point returns a pixel reference. Models PixelDereferenceAdaptorConcept
26  int Dim> // the dimension to advance along
27 struct position_iterator : public iterator_facade<position_iterator<Deref,Dim>,
28  typename Deref::value_type,
29  std::random_access_iterator_tag,
30  typename Deref::reference,
31  typename Deref::argument_type::template axis<Dim>::coord_t> {
32  using parent_t = iterator_facade<position_iterator<Deref,Dim>,
33  typename Deref::value_type,
34  std::random_access_iterator_tag,
35  typename Deref::reference,
36  typename Deref::argument_type::template axis<Dim>::coord_t>;
37  using difference_type = typename parent_t::difference_type;
38  using reference = typename parent_t::reference;
39  using point_t = typename Deref::argument_type;
40 
42  position_iterator(const point_t& p, const point_t& step, const Deref& d) : _p(p), _step(step), _d(d) {}
43 
44  position_iterator(const position_iterator& p) : _p(p._p), _step(p._step), _d(p._d) {}
45  template <typename D> position_iterator(const position_iterator<D,Dim>& p) : _p(p._p), _step(p._step), _d(p._d) {}
46  position_iterator& operator=(const position_iterator& p) { _p=p._p; _d=p._d; _step=p._step; return *this; }
47 
48  const point_t& pos() const { return _p; }
49  const point_t& step() const { return _step; }
50  const Deref& deref_fn() const { return _d; }
51 
52  void set_step(difference_type s) { _step[Dim]=s; }
55  reference operator[](difference_type d) const { point_t p=_p; p[Dim]+=d*_step[Dim]; return _d(p); }
56 
57 private:
58  point_t _p, _step;
59  Deref _d;
60 
61  template <typename DE, int DI> friend struct position_iterator;
62  friend class boost::iterator_core_access;
63  reference dereference() const { return _d(_p); }
64  void increment() { _p[Dim]+=_step[Dim]; }
65  void decrement() { _p[Dim]-=_step[Dim]; }
66  void advance(difference_type d) { _p[Dim]+=d*_step[Dim]; }
67 
68  difference_type distance_to(const position_iterator& it) const { return (it._p[Dim]-_p[Dim])/_step[Dim]; }
69  bool equal(const position_iterator& it) const { return _p==it._p; }
70 };
71 
72 template <typename Deref,int Dim>
73 struct const_iterator_type<position_iterator<Deref,Dim> > {
75 };
76 
77 template <typename Deref,int Dim>
78 struct iterator_is_mutable<position_iterator<Deref,Dim> > : public mpl::bool_<Deref::is_mutable> {
79 };
80 
82 // PixelBasedConcept
84 
85 template <typename Deref,int Dim>
86 struct color_space_type<position_iterator<Deref,Dim> > : public color_space_type<typename Deref::value_type> {};
87 
88 template <typename Deref,int Dim>
89 struct channel_mapping_type<position_iterator<Deref,Dim> > : public channel_mapping_type<typename Deref::value_type> {};
90 
91 template <typename Deref,int Dim>
92 struct is_planar<position_iterator<Deref,Dim> > : public mpl::false_ {};
93 
94 template <typename Deref,int Dim>
95 struct channel_type<position_iterator<Deref,Dim> > : public channel_type<typename Deref::value_type> {};
96 
98 // HasDynamicXStepTypeConcept
100 
101 template <typename Deref,int Dim>
102 struct dynamic_x_step_type<position_iterator<Deref,Dim> > {
103  using type = position_iterator<Deref,Dim>;
104 };
105 
106 } } // namespace boost::gil
107 
108 #endif
Definition: algorithm.hpp:30
An iterator that remembers its current X,Y position and invokes a function object with it upon derefe...
Definition: position_iterator.hpp:27
reference operator[](difference_type d) const
Definition: position_iterator.hpp:55
BOOST_FORCEINLINE bool equal(boost::gil::iterator_from_2d< Loc1 > first, boost::gil::iterator_from_2d< Loc1 > last, boost::gil::iterator_from_2d< Loc2 > first2)
std::equal(I1,I1,I2) with I1 and I2 being a iterator_from_2d
Definition: algorithm.hpp:929
Definition: color_convert.hpp:30
Metafunction predicate returning whether the given iterator allows for changing its values...
Definition: pixel_iterator.hpp:48
Returns the type of an iterator just like the input iterator, except operating over immutable values...
Definition: pixel_iterator.hpp:39
Base template for types that model HasDynamicXStepTypeConcept.
Definition: dynamic_step.hpp:17