Generic Image Library: pixel_iterator_adaptor.hpp Source File

###  | #  |

pixel_iterator_adaptor.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_PIXEL_ITERATOR_ADAPTOR_HPP
9#define BOOST_GIL_PIXEL_ITERATOR_ADAPTOR_HPP
10
11#include <boost/gil/concepts.hpp>
12#include <boost/gil/pixel_iterator.hpp>
13
14#include <boost/iterator/iterator_facade.hpp>
15
16#include <iterator>
17
18namespace boost { namespace gil {
19
23
26
27template <typename Iterator, // Models Iterator
28 typename DFn> // Models Returns the result of dereferencing a given iterator of type Iterator
29 class dereference_iterator_adaptor : public iterator_adaptor<dereference_iterator_adaptor<Iterator,DFn>,
30 Iterator,
31 typename DFn::value_type,
32 typename std::iterator_traits<Iterator>::iterator_category,
33 typename DFn::reference,
34 use_default> {
35 DFn _deref_fn;
36public:
37 using parent_t = iterator_adaptor<dereference_iterator_adaptor<Iterator,DFn>,
38 Iterator,
39 typename DFn::value_type,
40 typename std::iterator_traits<Iterator>::iterator_category,
41 typename DFn::reference,
42 use_default>;
43 using reference = typename DFn::result_type;
44 using difference_type = typename std::iterator_traits<Iterator>::difference_type;
45 using dereference_fn = DFn;
46
47 dereference_iterator_adaptor() {}
48 template <typename Iterator1>
49 dereference_iterator_adaptor(const dereference_iterator_adaptor<Iterator1,DFn>& dit) : parent_t(dit.base()), _deref_fn(dit._deref_fn) {}
50 dereference_iterator_adaptor(Iterator it, DFn deref_fn=DFn()) : parent_t(it), _deref_fn(deref_fn) {}
51 template <typename Iterator1, typename DFn1>
52 dereference_iterator_adaptor(const dereference_iterator_adaptor<Iterator1,DFn1>& it) : parent_t(it.base()), _deref_fn(it._deref_fn) {}
55 reference operator[](difference_type d) const { return *(*this+d);}
56
57 // although iterator_adaptor defines these, the default implementation computes distance and compares for zero.
58 // it is often faster to just apply the relation operator to the base
59 bool operator> (const dereference_iterator_adaptor& p) const { return this->base_reference()> p.base_reference(); }
60 bool operator< (const dereference_iterator_adaptor& p) const { return this->base_reference()< p.base_reference(); }
61 bool operator>=(const dereference_iterator_adaptor& p) const { return this->base_reference()>=p.base_reference(); }
62 bool operator<=(const dereference_iterator_adaptor& p) const { return this->base_reference()<=p.base_reference(); }
63 bool operator==(const dereference_iterator_adaptor& p) const { return this->base_reference()==p.base_reference(); }
64 bool operator!=(const dereference_iterator_adaptor& p) const { return this->base_reference()!=p.base_reference(); }
65
66 Iterator& base() { return this->base_reference(); }
67 const Iterator& base() const { return this->base_reference(); }
68 const DFn& deref_fn() const { return _deref_fn; }
69private:
70 template <typename Iterator1, typename DFn1>
71 friend class dereference_iterator_adaptor;
72 friend class boost::iterator_core_access;
73
74 reference dereference() const { return _deref_fn(*(this->base_reference())); }
75};
76
77template <typename I, typename DFn>
78struct const_iterator_type<dereference_iterator_adaptor<I,DFn> > {
79 using type = dereference_iterator_adaptor<typename const_iterator_type<I>::type,typename DFn::const_t>;
80};
81
82template <typename I, typename DFn>
83struct iterator_is_mutable<dereference_iterator_adaptor<I, DFn>>
84 : std::integral_constant<bool, DFn::is_mutable>
85{};
86
87
88template <typename I, typename DFn>
89struct is_iterator_adaptor<dereference_iterator_adaptor<I, DFn>> : std::true_type {};
90
91template <typename I, typename DFn>
92struct iterator_adaptor_get_base<dereference_iterator_adaptor<I, DFn>>
93{
94 using type = I;
95};
96
97template <typename I, typename DFn, typename NewBaseIterator>
98struct iterator_adaptor_rebind<dereference_iterator_adaptor<I,DFn>,NewBaseIterator> {
99 using type = dereference_iterator_adaptor<NewBaseIterator,DFn>;
100};
101
103// PixelBasedConcept
105
106template <typename I, typename DFn>
107struct color_space_type<dereference_iterator_adaptor<I,DFn> > : public color_space_type<typename DFn::value_type> {};
108
109template <typename I, typename DFn>
110struct channel_mapping_type<dereference_iterator_adaptor<I,DFn> > : public channel_mapping_type<typename DFn::value_type> {};
111
112template <typename I, typename DFn>
113struct is_planar<dereference_iterator_adaptor<I,DFn> > : public is_planar<typename DFn::value_type> {};
114
115template <typename I, typename DFn>
116struct channel_type<dereference_iterator_adaptor<I,DFn> > : public channel_type<typename DFn::value_type> {};
117
118
120// MemoryBasedIteratorConcept
122
123template <typename Iterator, typename DFn>
124struct byte_to_memunit<dereference_iterator_adaptor<Iterator,DFn>> : public byte_to_memunit<Iterator> {};
125
126template <typename Iterator, typename DFn>
127inline auto memunit_step(dereference_iterator_adaptor<Iterator,DFn> const& p)
128 -> typename std::iterator_traits<Iterator>::difference_type
129{
130 return memunit_step(p.base());
131}
132
133template <typename Iterator, typename DFn>
134inline auto memunit_distance(dereference_iterator_adaptor<Iterator,DFn> const& p1,
135 dereference_iterator_adaptor<Iterator,DFn> const& p2)
136 -> typename std::iterator_traits<Iterator>::difference_type
137{
138 return memunit_distance(p1.base(),p2.base());
139}
140
141template <typename Iterator, typename DFn>
142inline void memunit_advance(dereference_iterator_adaptor<Iterator,DFn>& p,
143 typename std::iterator_traits<Iterator>::difference_type diff) {
144 memunit_advance(p.base(), diff);
145}
146
147template <typename Iterator, typename DFn>
148inline auto memunit_advanced(dereference_iterator_adaptor<Iterator,DFn> const& p,
149 typename std::iterator_traits<Iterator>::difference_type diff)
150 -> dereference_iterator_adaptor<Iterator,DFn>
151{
152 return dereference_iterator_adaptor<Iterator,DFn>(memunit_advanced(p.base(), diff), p.deref_fn());
153}
154
155
156template <typename Iterator, typename DFn>
157inline auto memunit_advanced_ref(dereference_iterator_adaptor<Iterator,DFn> const& p,
158 typename std::iterator_traits<Iterator>::difference_type diff)
159 -> typename std::iterator_traits<dereference_iterator_adaptor<Iterator,DFn> >::reference
160{
161 return *memunit_advanced(p, diff);
162}
163
165// HasDynamicXStepTypeConcept
167
168template <typename Iterator, typename DFn>
169struct dynamic_x_step_type<dereference_iterator_adaptor<Iterator,DFn> > {
170 using type = dereference_iterator_adaptor<typename dynamic_x_step_type<Iterator>::type,DFn>;
171};
172
175template <typename Iterator, typename Deref>
176 struct iterator_add_deref {
177 BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
178
179 using type = dereference_iterator_adaptor<Iterator, Deref>;
180
181 static type make(const Iterator& it, const Deref& d) { return type(it,d); }
182};
183
186template <typename Iterator, typename PREV_DEREF, typename Deref>
187 struct iterator_add_deref<dereference_iterator_adaptor<Iterator, PREV_DEREF>,Deref> {
188// BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
189
190 using type = dereference_iterator_adaptor<Iterator, deref_compose<Deref,PREV_DEREF>>;
191
192 static type make(const dereference_iterator_adaptor<Iterator, PREV_DEREF>& it, const Deref& d) {
193 return type(it.base(),deref_compose<Deref,PREV_DEREF>(d,it.deref_fn()));
194 }
195};
196
197}} // namespace boost::gil
198
199#endif
boost::gil::deref_compose
Composes two dereference function objects. Similar to std::unary_compose but needs to pull some alias...
Definition: utilities.hpp:131
boost::gil::dereference_iterator_adaptor
An adaptor over an existing iterator that provides for custom filter on dereferencing the object....
Definition: pixel_iterator_adaptor.hpp:34
boost::gil::dereference_iterator_adaptor::operator[]
reference operator[](difference_type d) const
Definition: pixel_iterator_adaptor.hpp:55
boost
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
boost::gil::PixelDereferenceAdaptorConcept
Represents a unary function object that can be invoked upon dereferencing a pixel iterator.
Definition: pixel_dereference.hpp:54
boost::gil::iterator_add_deref
Returns the type (and creates an instance) of an iterator that invokes the given dereference adaptor ...
Definition: pixel_iterator_adaptor.hpp:176