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