Generic Image Library: bit_aligned_pixel_iterator.hpp Source File

###  | #  |

bit_aligned_pixel_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_BIT_ALIGNED_PIXEL_ITERATOR_HPP
9#define BOOST_GIL_BIT_ALIGNED_PIXEL_ITERATOR_HPP
10
11#include <boost/gil/bit_aligned_pixel_reference.hpp>
12#include <boost/gil/pixel_iterator.hpp>
13
14#include <boost/config.hpp>
15#include <boost/iterator/iterator_facade.hpp>
16
17#include <functional>
18#include <type_traits>
19
20namespace boost { namespace gil {
21
24
28
35
36template <typename NonAlignedPixelReference>
37 struct bit_aligned_pixel_iterator : public iterator_facade<bit_aligned_pixel_iterator<NonAlignedPixelReference>,
38 typename NonAlignedPixelReference::value_type,
39 std::random_access_iterator_tag,
40 const NonAlignedPixelReference,
41 typename NonAlignedPixelReference::bit_range_t::difference_type> {
42private:
43 using parent_t = iterator_facade<bit_aligned_pixel_iterator<NonAlignedPixelReference>,
44 typename NonAlignedPixelReference::value_type,
45 std::random_access_iterator_tag,
46 const NonAlignedPixelReference,
47 typename NonAlignedPixelReference::bit_range_t::difference_type>;
48 template <typename Ref> friend struct bit_aligned_pixel_iterator;
49
50 using bit_range_t = typename NonAlignedPixelReference::bit_range_t;
51public:
52 using difference_type = typename parent_t::difference_type;
53 using reference = typename parent_t::reference;
54
55 bit_aligned_pixel_iterator() {}
56 bit_aligned_pixel_iterator(const bit_aligned_pixel_iterator& p) : _bit_range(p._bit_range) {}
57 bit_aligned_pixel_iterator& operator=(const bit_aligned_pixel_iterator& p) { _bit_range=p._bit_range; return *this; }
58
59 template <typename Ref> bit_aligned_pixel_iterator(const bit_aligned_pixel_iterator<Ref>& p) : _bit_range(p._bit_range) {}
60
61 bit_aligned_pixel_iterator(reference* ref) : _bit_range(ref->bit_range()) {}
62 explicit bit_aligned_pixel_iterator(typename bit_range_t::byte_t* data, int bit_offset=0) : _bit_range(data,bit_offset) {}
63
66 auto operator[](difference_type d) const -> reference { bit_aligned_pixel_iterator it=*this; it.advance(d); return *it; }
67
68 auto operator->() const -> reference { return **this; }
69 auto bit_range() const -> bit_range_t const& { return _bit_range; }
70 auto bit_range() -> bit_range_t& { return _bit_range; }
71private:
72 bit_range_t _bit_range;
73 static constexpr int bit_size = NonAlignedPixelReference::bit_size;
74
75 friend class boost::iterator_core_access;
76 auto dereference() const -> reference { return NonAlignedPixelReference(_bit_range); }
77 void increment() { ++_bit_range; }
78 void decrement() { --_bit_range; }
79 void advance(difference_type d) { _bit_range.bit_advance(d*bit_size); }
80
81 auto distance_to(bit_aligned_pixel_iterator const& it) const -> difference_type { return _bit_range.bit_distance_to(it._bit_range) / bit_size; }
82 bool equal(const bit_aligned_pixel_iterator& it) const { return _bit_range==it._bit_range; }
83};
84
85template <typename NonAlignedPixelReference>
86struct const_iterator_type<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
87{
88 using type =
89 bit_aligned_pixel_iterator<typename NonAlignedPixelReference::const_reference>;
90};
91
92template <typename NonAlignedPixelReference>
93struct iterator_is_mutable<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
94 : std::integral_constant<bool, NonAlignedPixelReference::is_mutable>
95{};
96
97template <typename NonAlignedPixelReference>
98struct is_iterator_adaptor<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
99 : std::false_type
100{};
101
103// PixelBasedConcept
105
106template <typename NonAlignedPixelReference>
107struct color_space_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public color_space_type<NonAlignedPixelReference> {};
108
109template <typename NonAlignedPixelReference>
110struct channel_mapping_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public channel_mapping_type<NonAlignedPixelReference> {};
111
112template <typename NonAlignedPixelReference>
113struct is_planar<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public is_planar<NonAlignedPixelReference> {}; // == false
114
116// MemoryBasedIteratorConcept
118
119template <typename NonAlignedPixelReference>
120struct byte_to_memunit<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
121 : std::integral_constant<int, 8>
122{};
123
124template <typename NonAlignedPixelReference>
125inline auto memunit_step(const bit_aligned_pixel_iterator<NonAlignedPixelReference>&) -> std::ptrdiff_t
126{
127 return NonAlignedPixelReference::bit_size;
128}
129
130template <typename NonAlignedPixelReference>
131inline auto memunit_distance(bit_aligned_pixel_iterator<NonAlignedPixelReference> const& p1, bit_aligned_pixel_iterator<NonAlignedPixelReference> const& p2) -> std::ptrdiff_t
132{
133 return (p2.bit_range().current_byte() - p1.bit_range().current_byte())*8 + p2.bit_range().bit_offset() - p1.bit_range().bit_offset();
134}
135
136template <typename NonAlignedPixelReference>
137inline void memunit_advance(bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) {
138 p.bit_range().bit_advance(diff);
139}
140
141template <typename NonAlignedPixelReference>
142inline auto memunit_advanced(bit_aligned_pixel_iterator<NonAlignedPixelReference> const& p, std::ptrdiff_t diff) -> bit_aligned_pixel_iterator<NonAlignedPixelReference> {
143 bit_aligned_pixel_iterator<NonAlignedPixelReference> ret=p;
144 memunit_advance(ret, diff);
145 return ret;
146}
147
148template <typename NonAlignedPixelReference> inline
149auto memunit_advanced_ref(bit_aligned_pixel_iterator<NonAlignedPixelReference> it, std::ptrdiff_t diff) -> NonAlignedPixelReference
150{
151 return *memunit_advanced(it,diff);
152}
154// HasDynamicXStepTypeConcept
156
157template <typename NonAlignedPixelReference>
158struct dynamic_x_step_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > {
159 using type = memory_based_step_iterator<bit_aligned_pixel_iterator<NonAlignedPixelReference> >;
160};
161
163// iterator_type_from_pixel
165
166template <typename B, typename C, typename L, bool M>
167struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,false>
168{
169 using type = bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,false>> ;
170};
171
172template <typename B, typename C, typename L, bool M>
173struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,true>
174{
175 using type = bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,true>>;
176};
177
178template <typename B, typename C, typename L, bool M, bool IsPlanar, bool IsStep, bool IsMutable>
179struct iterator_type_from_pixel<bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable>
180 : public iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable> {};
181
182} } // namespace boost::gil
183
184namespace std {
185
186// It is important to provide an overload of uninitialized_copy for bit_aligned_pixel_iterator. The default STL implementation calls placement new,
187// which is not defined for bit_aligned_pixel_iterator.
188template <typename NonAlignedPixelReference>
189auto uninitialized_copy(boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> first,
190 boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> last,
191 boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> dst)
192 -> boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference>
193{
194 return std::copy(first,last,dst);
195}
196
197} // namespace std
198
199#endif
std::copy
BOOST_FORCEINLINE auto copy(boost::gil::pixel< T, CS > *first, boost::gil::pixel< T, CS > *last, boost::gil::pixel< T, CS > *dst) -> boost::gil::pixel< T, CS > *
Copy when both src and dst are interleaved and of the same type can be just memmove.
Definition algorithm.hpp:145
boost
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition algorithm.hpp:36
boost::gil::bit_aligned_pixel_iterator
An iterator over non-byte-aligned pixels. Models PixelIteratorConcept, PixelBasedConcept,...
Definition bit_aligned_pixel_iterator.hpp:41
boost::gil::bit_aligned_pixel_iterator::operator[]
auto operator[](difference_type d) const -> reference
Definition bit_aligned_pixel_iterator.hpp:66