]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/gil/bit_aligned_pixel_iterator.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / gil / bit_aligned_pixel_iterator.hpp
CommitLineData
92f5a8d4
TL
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>
7c673cae
FG
16
17#include <functional>
92f5a8d4 18#include <type_traits>
7c673cae
FG
19
20namespace boost { namespace gil {
21
92f5a8d4
TL
22/// A model of a heterogeneous pixel that is not byte aligned.
23/// Examples are bitmap (1-bit pixels) or 6-bit RGB (222).
24
7c673cae
FG
25/// \defgroup PixelIteratorNonAlignedPixelIterator bit_aligned_pixel_iterator
26/// \ingroup PixelIteratorModel
27/// \brief An iterator over non-byte-aligned pixels. Models PixelIteratorConcept, PixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
28
29////////////////////////////////////////////////////////////////////////////////////////
30/// \brief An iterator over non-byte-aligned pixels. Models PixelIteratorConcept, PixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
31///
32/// An iterator over pixels that correspond to non-byte-aligned bit ranges. Examples of such pixels are single bit grayscale pixel, or a 6-bit RGB 222 pixel.
92f5a8d4 33///
7c673cae
FG
34/// \ingroup PixelIteratorNonAlignedPixelIterator PixelBasedModel
35
36template <typename NonAlignedPixelReference>
37struct 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:
92f5a8d4 43 using parent_t = iterator_facade<bit_aligned_pixel_iterator<NonAlignedPixelReference>,
7c673cae
FG
44 typename NonAlignedPixelReference::value_type,
45 std::random_access_iterator_tag,
46 const NonAlignedPixelReference,
92f5a8d4 47 typename NonAlignedPixelReference::bit_range_t::difference_type>;
7c673cae
FG
48 template <typename Ref> friend struct bit_aligned_pixel_iterator;
49
92f5a8d4 50 using bit_range_t = typename NonAlignedPixelReference::bit_range_t;
7c673cae 51public:
92f5a8d4
TL
52 using difference_type = typename parent_t::difference_type;
53 using reference = typename parent_t::reference;
7c673cae
FG
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
64 /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference
65 /// We require our own reference because it is registered in iterator_traits
66 reference operator[](difference_type d) const { bit_aligned_pixel_iterator it=*this; it.advance(d); return *it; }
67
68 reference operator->() const { return **this; }
69 const bit_range_t& bit_range() const { return _bit_range; }
70 bit_range_t& bit_range() { return _bit_range; }
71private:
72 bit_range_t _bit_range;
92f5a8d4 73 static constexpr int bit_size = NonAlignedPixelReference::bit_size;
7c673cae
FG
74
75 friend class boost::iterator_core_access;
76 reference dereference() const { 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 difference_type distance_to(const bit_aligned_pixel_iterator& it) const { 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
92f5a8d4
TL
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>;
7c673cae
FG
90};
91
92f5a8d4
TL
92template <typename NonAlignedPixelReference>
93struct iterator_is_mutable<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
94 : std::integral_constant<bool, NonAlignedPixelReference::is_mutable>
95{};
7c673cae 96
92f5a8d4
TL
97template <typename NonAlignedPixelReference>
98struct is_iterator_adaptor<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
99 : std::false_type
100{};
7c673cae
FG
101
102/////////////////////////////
103// PixelBasedConcept
104/////////////////////////////
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
115/////////////////////////////
116// MemoryBasedIteratorConcept
117/////////////////////////////
118
119template <typename NonAlignedPixelReference>
92f5a8d4
TL
120struct byte_to_memunit<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
121 : std::integral_constant<int, 8>
122{};
7c673cae
FG
123
124template <typename NonAlignedPixelReference>
92f5a8d4
TL
125inline std::ptrdiff_t memunit_step(const bit_aligned_pixel_iterator<NonAlignedPixelReference>&) {
126 return NonAlignedPixelReference::bit_size;
7c673cae
FG
127}
128
129template <typename NonAlignedPixelReference>
92f5a8d4
TL
130inline std::ptrdiff_t memunit_distance(const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p1, const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p2) {
131 return (p2.bit_range().current_byte() - p1.bit_range().current_byte())*8 + p2.bit_range().bit_offset() - p1.bit_range().bit_offset();
7c673cae
FG
132}
133
134template <typename NonAlignedPixelReference>
92f5a8d4 135inline void memunit_advance(bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) {
7c673cae
FG
136 p.bit_range().bit_advance(diff);
137}
138
139template <typename NonAlignedPixelReference>
140inline bit_aligned_pixel_iterator<NonAlignedPixelReference> memunit_advanced(const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) {
141 bit_aligned_pixel_iterator<NonAlignedPixelReference> ret=p;
142 memunit_advance(ret, diff);
143 return ret;
144}
145
146template <typename NonAlignedPixelReference> inline
147NonAlignedPixelReference memunit_advanced_ref(bit_aligned_pixel_iterator<NonAlignedPixelReference> it, std::ptrdiff_t diff) {
148 return *memunit_advanced(it,diff);
149}
150/////////////////////////////
151// HasDynamicXStepTypeConcept
152/////////////////////////////
153
154template <typename NonAlignedPixelReference>
155struct dynamic_x_step_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > {
92f5a8d4 156 using type = memory_based_step_iterator<bit_aligned_pixel_iterator<NonAlignedPixelReference> >;
7c673cae
FG
157};
158
159/////////////////////////////
160// iterator_type_from_pixel
161/////////////////////////////
162
163template <typename B, typename C, typename L, bool M>
92f5a8d4
TL
164struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,false>
165{
166 using type = bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,false>> ;
7c673cae
FG
167};
168
169template <typename B, typename C, typename L, bool M>
92f5a8d4
TL
170struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,true>
171{
172 using type = bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,true>>;
7c673cae
FG
173};
174
175template <typename B, typename C, typename L, bool M, bool IsPlanar, bool IsStep, bool IsMutable>
176struct iterator_type_from_pixel<bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable>
177 : public iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable> {};
178
179} } // namespace boost::gil
180
181namespace std {
182
183// It is important to provide an overload of uninitialized_copy for bit_aligned_pixel_iterator. The default STL implementation calls placement new,
92f5a8d4 184// which is not defined for bit_aligned_pixel_iterator.
7c673cae 185template <typename NonAlignedPixelReference>
92f5a8d4 186boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> uninitialized_copy(boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> first,
7c673cae
FG
187 boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> last,
188 boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> dst) {
189 return std::copy(first,last,dst);
190}
191
192} // namespace std
193#endif