1 | /* |
2 | Copyright 2005-2007 Adobe Systems Incorporated |
3 | |
4 | Use, modification and distribution are subject to the Boost Software License, |
5 | Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
6 | http://www.boost.org/LICENSE_1_0.txt). |
7 | |
8 | See http://opensource.adobe.com/gil for most recent version including documentation. |
9 | */ |
10 | |
11 | /*************************************************************************************************/ |
12 | |
13 | #ifndef GIL_POSITION_ITERATOR_HPP |
14 | #define GIL_POSITION_ITERATOR_HPP |
15 | |
16 | //////////////////////////////////////////////////////////////////////////////////////// |
17 | /// \file |
18 | /// \brief Locator for virtual image views |
19 | /// \author Lubomir Bourdev and Hailin Jin \n |
20 | /// Adobe Systems Incorporated |
21 | /// \date 2005-2007 \n Last updated on February 12, 2007 |
22 | /// |
23 | //////////////////////////////////////////////////////////////////////////////////////// |
24 | |
25 | #include <boost/iterator/iterator_facade.hpp> |
26 | #include "locator.hpp" |
27 | |
28 | namespace boost { namespace gil { |
29 | |
30 | /// \defgroup PixelIteratorModelVirtual position_iterator |
31 | /// \ingroup PixelIteratorModel |
32 | /// \brief An iterator that remembers its current X,Y position and invokes a function object with it upon dereferencing. Models PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept. Used to create virtual image views. |
33 | |
34 | |
35 | /// \brief An iterator that remembers its current X,Y position and invokes a function object with it upon dereferencing. Models PixelIteratorConcept. Used to create virtual image views. |
36 | /// Models: StepIteratorConcept, PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept |
37 | /// \ingroup PixelIteratorModelVirtual PixelBasedModel |
38 | template <typename Deref, // A function object that given a point returns a pixel reference. Models PixelDereferenceAdaptorConcept |
39 | int Dim> // the dimension to advance along |
40 | struct position_iterator : public iterator_facade<position_iterator<Deref,Dim>, |
41 | typename Deref::value_type, |
42 | std::random_access_iterator_tag, |
43 | typename Deref::reference, |
44 | typename Deref::argument_type::template axis<Dim>::coord_t> { |
45 | typedef iterator_facade<position_iterator<Deref,Dim>, |
46 | typename Deref::value_type, |
47 | std::random_access_iterator_tag, |
48 | typename Deref::reference, |
49 | typename Deref::argument_type::template axis<Dim>::coord_t> parent_t; |
50 | typedef typename parent_t::difference_type difference_type; |
51 | typedef typename parent_t::reference reference; |
52 | typedef typename Deref::argument_type point_t; |
53 | |
54 | position_iterator() {} |
55 | position_iterator(const point_t& p, const point_t& step, const Deref& d) : _p(p), _step(step), _d(d) {} |
56 | |
57 | position_iterator(const position_iterator& p) : _p(p._p), _step(p._step), _d(p._d) {} |
58 | template <typename D> position_iterator(const position_iterator<D,Dim>& p) : _p(p._p), _step(p._step), _d(p._d) {} |
59 | position_iterator& operator=(const position_iterator& p) { _p=p._p; _d=p._d; _step=p._step; return *this; } |
60 | |
61 | const point_t& pos() const { return _p; } |
62 | const point_t& step() const { return _step; } |
63 | const Deref& deref_fn() const { return _d; } |
64 | |
65 | void set_step(difference_type s) { _step[Dim]=s; } |
66 | /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference |
67 | /// We require our own reference because it is registered in iterator_traits |
68 | reference operator[](difference_type d) const { point_t p=_p; p[Dim]+=d*_step[Dim]; return _d(p); } |
69 | |
70 | private: |
71 | point_t _p, _step; |
72 | Deref _d; |
73 | |
74 | template <typename DE, int DI> friend struct position_iterator; |
75 | friend class boost::iterator_core_access; |
76 | reference dereference() const { return _d(_p); } |
77 | void increment() { _p[Dim]+=_step[Dim]; } |
78 | void decrement() { _p[Dim]-=_step[Dim]; } |
79 | void advance(difference_type d) { _p[Dim]+=d*_step[Dim]; } |
80 | |
81 | difference_type distance_to(const position_iterator& it) const { return (it._p[Dim]-_p[Dim])/_step[Dim]; } |
82 | bool equal(const position_iterator& it) const { return _p==it._p; } |
83 | }; |
84 | |
85 | template <typename Deref,int Dim> |
86 | struct const_iterator_type<position_iterator<Deref,Dim> > { |
87 | typedef position_iterator<typename Deref::const_t,Dim> type; |
88 | }; |
89 | |
90 | template <typename Deref,int Dim> |
91 | struct iterator_is_mutable<position_iterator<Deref,Dim> > : public mpl::bool_<Deref::is_mutable> { |
92 | }; |
93 | |
94 | ///////////////////////////// |
95 | // PixelBasedConcept |
96 | ///////////////////////////// |
97 | |
98 | template <typename Deref,int Dim> |
99 | struct color_space_type<position_iterator<Deref,Dim> > : public color_space_type<typename Deref::value_type> {}; |
100 | |
101 | template <typename Deref,int Dim> |
102 | struct channel_mapping_type<position_iterator<Deref,Dim> > : public channel_mapping_type<typename Deref::value_type> {}; |
103 | |
104 | template <typename Deref,int Dim> |
105 | struct is_planar<position_iterator<Deref,Dim> > : public mpl::false_ {}; |
106 | |
107 | template <typename Deref,int Dim> |
108 | struct channel_type<position_iterator<Deref,Dim> > : public channel_type<typename Deref::value_type> {}; |
109 | |
110 | ///////////////////////////// |
111 | // HasDynamicXStepTypeConcept |
112 | ///////////////////////////// |
113 | |
114 | template <typename Deref,int Dim> |
115 | struct dynamic_x_step_type<position_iterator<Deref,Dim> > { |
116 | typedef position_iterator<Deref,Dim> type; |
117 | }; |
118 | |
119 | } } // namespace boost::gil |
120 | |
121 | #endif |
122 | |