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_BIT_ALIGNED_PIXEL_ITERATOR_HPP |
14 | #define GIL_BIT_ALIGNED_PIXEL_ITERATOR_HPP |
15 | |
16 | //////////////////////////////////////////////////////////////////////////////////////// |
17 | /// \file |
18 | /// \brief A model of a heterogeneous pixel that is not byte aligned. Examples are bitmap (1-bit pixels) or 6-bit RGB (222) |
19 | /// \author Lubomir Bourdev and Hailin Jin \n |
20 | /// Adobe Systems Incorporated |
21 | /// \date 2005-2007 \n Last updated on September 28, 2006 |
22 | /// |
23 | //////////////////////////////////////////////////////////////////////////////////////// |
24 | |
25 | #include <functional> |
26 | #include <boost/iterator/iterator_facade.hpp> |
27 | #include "gil_config.hpp" |
28 | #include "bit_aligned_pixel_reference.hpp" |
29 | #include "pixel_iterator.hpp" |
30 | |
31 | namespace boost { namespace gil { |
32 | |
33 | /// \defgroup PixelIteratorNonAlignedPixelIterator bit_aligned_pixel_iterator |
34 | /// \ingroup PixelIteratorModel |
35 | /// \brief An iterator over non-byte-aligned pixels. Models PixelIteratorConcept, PixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept |
36 | |
37 | //////////////////////////////////////////////////////////////////////////////////////// |
38 | /// \brief An iterator over non-byte-aligned pixels. Models PixelIteratorConcept, PixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept |
39 | /// |
40 | /// 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. |
41 | /// |
42 | /// \ingroup PixelIteratorNonAlignedPixelIterator PixelBasedModel |
43 | |
44 | template <typename NonAlignedPixelReference> |
45 | struct bit_aligned_pixel_iterator : public iterator_facade<bit_aligned_pixel_iterator<NonAlignedPixelReference>, |
46 | typename NonAlignedPixelReference::value_type, |
47 | std::random_access_iterator_tag, |
48 | const NonAlignedPixelReference, |
49 | typename NonAlignedPixelReference::bit_range_t::difference_type> { |
50 | private: |
51 | typedef iterator_facade<bit_aligned_pixel_iterator<NonAlignedPixelReference>, |
52 | typename NonAlignedPixelReference::value_type, |
53 | std::random_access_iterator_tag, |
54 | const NonAlignedPixelReference, |
55 | typename NonAlignedPixelReference::bit_range_t::difference_type> parent_t; |
56 | template <typename Ref> friend struct bit_aligned_pixel_iterator; |
57 | |
58 | typedef typename NonAlignedPixelReference::bit_range_t bit_range_t; |
59 | public: |
60 | typedef typename parent_t::difference_type difference_type; |
61 | typedef typename parent_t::reference reference; |
62 | |
63 | bit_aligned_pixel_iterator() {} |
64 | bit_aligned_pixel_iterator(const bit_aligned_pixel_iterator& p) : _bit_range(p._bit_range) {} |
65 | bit_aligned_pixel_iterator& operator=(const bit_aligned_pixel_iterator& p) { _bit_range=p._bit_range; return *this; } |
66 | |
67 | template <typename Ref> bit_aligned_pixel_iterator(const bit_aligned_pixel_iterator<Ref>& p) : _bit_range(p._bit_range) {} |
68 | |
69 | bit_aligned_pixel_iterator(reference* ref) : _bit_range(ref->bit_range()) {} |
70 | explicit bit_aligned_pixel_iterator(typename bit_range_t::byte_t* data, int bit_offset=0) : _bit_range(data,bit_offset) {} |
71 | |
72 | /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference |
73 | /// We require our own reference because it is registered in iterator_traits |
74 | reference operator[](difference_type d) const { bit_aligned_pixel_iterator it=*this; it.advance(d); return *it; } |
75 | |
76 | reference operator->() const { return **this; } |
77 | const bit_range_t& bit_range() const { return _bit_range; } |
78 | bit_range_t& bit_range() { return _bit_range; } |
79 | private: |
80 | bit_range_t _bit_range; |
81 | BOOST_STATIC_CONSTANT(int, bit_size = NonAlignedPixelReference::bit_size); |
82 | |
83 | friend class boost::iterator_core_access; |
84 | reference dereference() const { return NonAlignedPixelReference(_bit_range); } |
85 | void increment() { ++_bit_range; } |
86 | void decrement() { --_bit_range; } |
87 | void advance(difference_type d) { _bit_range.bit_advance(d*bit_size); } |
88 | |
89 | difference_type distance_to(const bit_aligned_pixel_iterator& it) const { return _bit_range.bit_distance_to(it._bit_range) / bit_size; } |
90 | bool equal(const bit_aligned_pixel_iterator& it) const { return _bit_range==it._bit_range; } |
91 | }; |
92 | |
93 | template <typename NonAlignedPixelReference> |
94 | struct const_iterator_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > { |
95 | typedef bit_aligned_pixel_iterator<typename NonAlignedPixelReference::const_reference> type; |
96 | }; |
97 | |
98 | template <typename NonAlignedPixelReference> |
99 | struct iterator_is_mutable<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public mpl::bool_<NonAlignedPixelReference::is_mutable> {}; |
100 | |
101 | template <typename NonAlignedPixelReference> |
102 | struct is_iterator_adaptor<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public mpl::false_ {}; |
103 | |
104 | ///////////////////////////// |
105 | // PixelBasedConcept |
106 | ///////////////////////////// |
107 | |
108 | template <typename NonAlignedPixelReference> |
109 | struct color_space_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public color_space_type<NonAlignedPixelReference> {}; |
110 | |
111 | template <typename NonAlignedPixelReference> |
112 | struct channel_mapping_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public channel_mapping_type<NonAlignedPixelReference> {}; |
113 | |
114 | template <typename NonAlignedPixelReference> |
115 | struct is_planar<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public is_planar<NonAlignedPixelReference> {}; // == false |
116 | |
117 | ///////////////////////////// |
118 | // MemoryBasedIteratorConcept |
119 | ///////////////////////////// |
120 | |
121 | template <typename NonAlignedPixelReference> |
122 | struct byte_to_memunit<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public mpl::int_<8> {}; |
123 | |
124 | template <typename NonAlignedPixelReference> |
125 | inline std::ptrdiff_t memunit_step(const bit_aligned_pixel_iterator<NonAlignedPixelReference>&) { |
126 | return NonAlignedPixelReference::bit_size; |
127 | } |
128 | |
129 | template <typename NonAlignedPixelReference> |
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(); |
132 | } |
133 | |
134 | template <typename NonAlignedPixelReference> |
135 | inline void memunit_advance(bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) { |
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> > { |
156 | typedef memory_based_step_iterator<bit_aligned_pixel_iterator<NonAlignedPixelReference> > type; |
157 | }; |
158 | |
159 | ///////////////////////////// |
160 | // iterator_type_from_pixel |
161 | ///////////////////////////// |
162 | |
163 | template <typename B, typename C, typename L, bool M> |
164 | struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,false> { |
165 | typedef bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,false> > type; |
166 | }; |
167 | |
168 | template <typename B, typename C, typename L, bool M> |
169 | struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,true> { |
170 | typedef bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,true> > type; |
171 | }; |
172 | |
173 | template <typename B, typename C, typename L, bool M, bool IsPlanar, bool IsStep, bool IsMutable> |
174 | struct iterator_type_from_pixel<bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable> |
175 | : public iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable> {}; |
176 | |
177 | } } // namespace boost::gil |
178 | |
179 | namespace std { |
180 | |
181 | // It is important to provide an overload of uninitialized_copy for bit_aligned_pixel_iterator. The default STL implementation calls placement new, |
182 | // which is not defined for bit_aligned_pixel_iterator. |
183 | template <typename NonAlignedPixelReference> |
184 | boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> uninitialized_copy(boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> first, |
185 | boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> last, |
186 | boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> dst) { |
187 | return std::copy(first,last,dst); |
188 | } |
189 | |
190 | } // namespace std |
191 | #endif |
192 | |