]>
Commit | Line | Data |
---|---|---|
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 | |
20 | namespace 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 | ||
36 | template <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> { | |
42 | private: | |
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 | 51 | public: |
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; } | |
71 | private: | |
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 |
85 | template <typename NonAlignedPixelReference> |
86 | struct 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 |
92 | template <typename NonAlignedPixelReference> |
93 | struct iterator_is_mutable<bit_aligned_pixel_iterator<NonAlignedPixelReference>> | |
94 | : std::integral_constant<bool, NonAlignedPixelReference::is_mutable> | |
95 | {}; | |
7c673cae | 96 | |
92f5a8d4 TL |
97 | template <typename NonAlignedPixelReference> |
98 | struct is_iterator_adaptor<bit_aligned_pixel_iterator<NonAlignedPixelReference>> | |
99 | : std::false_type | |
100 | {}; | |
7c673cae FG |
101 | |
102 | ///////////////////////////// | |
103 | // PixelBasedConcept | |
104 | ///////////////////////////// | |
105 | ||
106 | template <typename NonAlignedPixelReference> | |
107 | struct color_space_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public color_space_type<NonAlignedPixelReference> {}; | |
108 | ||
109 | template <typename NonAlignedPixelReference> | |
110 | struct channel_mapping_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public channel_mapping_type<NonAlignedPixelReference> {}; | |
111 | ||
112 | template <typename NonAlignedPixelReference> | |
113 | struct is_planar<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public is_planar<NonAlignedPixelReference> {}; // == false | |
114 | ||
115 | ///////////////////////////// | |
116 | // MemoryBasedIteratorConcept | |
117 | ///////////////////////////// | |
118 | ||
119 | template <typename NonAlignedPixelReference> | |
92f5a8d4 TL |
120 | struct byte_to_memunit<bit_aligned_pixel_iterator<NonAlignedPixelReference>> |
121 | : std::integral_constant<int, 8> | |
122 | {}; | |
7c673cae FG |
123 | |
124 | template <typename NonAlignedPixelReference> | |
92f5a8d4 TL |
125 | inline std::ptrdiff_t memunit_step(const bit_aligned_pixel_iterator<NonAlignedPixelReference>&) { |
126 | return NonAlignedPixelReference::bit_size; | |
7c673cae FG |
127 | } |
128 | ||
129 | template <typename NonAlignedPixelReference> | |
92f5a8d4 TL |
130 | inline 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 | ||
134 | template <typename NonAlignedPixelReference> | |
92f5a8d4 | 135 | inline void memunit_advance(bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) { |
7c673cae FG |
136 | p.bit_range().bit_advance(diff); |
137 | } | |
138 | ||
139 | template <typename NonAlignedPixelReference> | |
140 | inline 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 | ||
146 | template <typename NonAlignedPixelReference> inline | |
147 | NonAlignedPixelReference 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 | ||
154 | template <typename NonAlignedPixelReference> | |
155 | struct 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 | ||
163 | template <typename B, typename C, typename L, bool M> | |
92f5a8d4 TL |
164 | struct 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 | ||
169 | template <typename B, typename C, typename L, bool M> | |
92f5a8d4 TL |
170 | struct 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 | ||
175 | template <typename B, typename C, typename L, bool M, bool IsPlanar, bool IsStep, bool IsMutable> | |
176 | struct 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 | ||
181 | namespace 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 | 185 | template <typename NonAlignedPixelReference> |
92f5a8d4 | 186 | boost::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 |