Boost GIL


bit_aligned_pixel_iterator.hpp
Go to the documentation of this file.
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 
24 
25 #include <functional>
26 
27 #include <boost/config.hpp>
28 #include <boost/iterator/iterator_facade.hpp>
29 
30 #include "gil_config.hpp"
32 #include "pixel_iterator.hpp"
33 
34 namespace boost { namespace gil {
35 
39 
46 
47 template <typename NonAlignedPixelReference>
48 struct bit_aligned_pixel_iterator : public iterator_facade<bit_aligned_pixel_iterator<NonAlignedPixelReference>,
49  typename NonAlignedPixelReference::value_type,
50  std::random_access_iterator_tag,
51  const NonAlignedPixelReference,
52  typename NonAlignedPixelReference::bit_range_t::difference_type> {
53 private:
54  typedef iterator_facade<bit_aligned_pixel_iterator<NonAlignedPixelReference>,
55  typename NonAlignedPixelReference::value_type,
56  std::random_access_iterator_tag,
57  const NonAlignedPixelReference,
58  typename NonAlignedPixelReference::bit_range_t::difference_type> parent_t;
59  template <typename Ref> friend struct bit_aligned_pixel_iterator;
60 
61  typedef typename NonAlignedPixelReference::bit_range_t bit_range_t;
62 public:
63  typedef typename parent_t::difference_type difference_type;
64  typedef typename parent_t::reference reference;
65 
67  bit_aligned_pixel_iterator(const bit_aligned_pixel_iterator& p) : _bit_range(p._bit_range) {}
68  bit_aligned_pixel_iterator& operator=(const bit_aligned_pixel_iterator& p) { _bit_range=p._bit_range; return *this; }
69 
70  template <typename Ref> bit_aligned_pixel_iterator(const bit_aligned_pixel_iterator<Ref>& p) : _bit_range(p._bit_range) {}
71 
72  bit_aligned_pixel_iterator(reference* ref) : _bit_range(ref->bit_range()) {}
73  explicit bit_aligned_pixel_iterator(typename bit_range_t::byte_t* data, int bit_offset=0) : _bit_range(data,bit_offset) {}
74 
77  reference operator[](difference_type d) const { bit_aligned_pixel_iterator it=*this; it.advance(d); return *it; }
78 
79  reference operator->() const { return **this; }
80  const bit_range_t& bit_range() const { return _bit_range; }
81  bit_range_t& bit_range() { return _bit_range; }
82 private:
83  bit_range_t _bit_range;
84  BOOST_STATIC_CONSTANT(int, bit_size = NonAlignedPixelReference::bit_size);
85 
86  friend class boost::iterator_core_access;
87  reference dereference() const { return NonAlignedPixelReference(_bit_range); }
88  void increment() { ++_bit_range; }
89  void decrement() { --_bit_range; }
90  void advance(difference_type d) { _bit_range.bit_advance(d*bit_size); }
91 
92  difference_type distance_to(const bit_aligned_pixel_iterator& it) const { return _bit_range.bit_distance_to(it._bit_range) / bit_size; }
93  bool equal(const bit_aligned_pixel_iterator& it) const { return _bit_range==it._bit_range; }
94 };
95 
96 template <typename NonAlignedPixelReference>
97 struct const_iterator_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > {
98  typedef bit_aligned_pixel_iterator<typename NonAlignedPixelReference::const_reference> type;
99 };
100 
101 template <typename NonAlignedPixelReference>
102 struct iterator_is_mutable<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public mpl::bool_<NonAlignedPixelReference::is_mutable> {};
103 
104 template <typename NonAlignedPixelReference>
105 struct is_iterator_adaptor<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public mpl::false_ {};
106 
108 // PixelBasedConcept
110 
111 template <typename NonAlignedPixelReference>
112 struct color_space_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public color_space_type<NonAlignedPixelReference> {};
113 
114 template <typename NonAlignedPixelReference>
115 struct channel_mapping_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public channel_mapping_type<NonAlignedPixelReference> {};
116 
117 template <typename NonAlignedPixelReference>
118 struct is_planar<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public is_planar<NonAlignedPixelReference> {}; // == false
119 
121 // MemoryBasedIteratorConcept
123 
124 template <typename NonAlignedPixelReference>
125 struct byte_to_memunit<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public mpl::int_<8> {};
126 
127 template <typename NonAlignedPixelReference>
128 inline std::ptrdiff_t memunit_step(const bit_aligned_pixel_iterator<NonAlignedPixelReference>&) {
129  return NonAlignedPixelReference::bit_size;
130 }
131 
132 template <typename NonAlignedPixelReference>
133 inline std::ptrdiff_t memunit_distance(const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p1, const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p2) {
134  return (p2.bit_range().current_byte() - p1.bit_range().current_byte())*8 + p2.bit_range().bit_offset() - p1.bit_range().bit_offset();
135 }
136 
137 template <typename NonAlignedPixelReference>
138 inline void memunit_advance(bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) {
139  p.bit_range().bit_advance(diff);
140 }
141 
142 template <typename NonAlignedPixelReference>
143 inline bit_aligned_pixel_iterator<NonAlignedPixelReference> memunit_advanced(const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) {
144  bit_aligned_pixel_iterator<NonAlignedPixelReference> ret=p;
145  memunit_advance(ret, diff);
146  return ret;
147 }
148 
149 template <typename NonAlignedPixelReference> inline
150 NonAlignedPixelReference memunit_advanced_ref(bit_aligned_pixel_iterator<NonAlignedPixelReference> it, std::ptrdiff_t diff) {
151  return *memunit_advanced(it,diff);
152 }
154 // HasDynamicXStepTypeConcept
156 
157 template <typename NonAlignedPixelReference>
158 struct dynamic_x_step_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > {
159  typedef memory_based_step_iterator<bit_aligned_pixel_iterator<NonAlignedPixelReference> > type;
160 };
161 
163 // iterator_type_from_pixel
165 
166 template <typename B, typename C, typename L, bool M>
167 struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,false> {
168  typedef bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,false> > type;
169 };
170 
171 template <typename B, typename C, typename L, bool M>
172 struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,true> {
173  typedef bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,true> > type;
174 };
175 
176 template <typename B, typename C, typename L, bool M, bool IsPlanar, bool IsStep, bool IsMutable>
177 struct iterator_type_from_pixel<bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable>
178  : public iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable> {};
179 
180 } } // namespace boost::gil
181 
182 namespace std {
183 
184 // It is important to provide an overload of uninitialized_copy for bit_aligned_pixel_iterator. The default STL implementation calls placement new,
185 // which is not defined for bit_aligned_pixel_iterator.
186 template <typename NonAlignedPixelReference>
190  return std::copy(first,last,dst);
191 }
192 
193 } // namespace std
194 #endif
BOOST_FORCEINLINE boost::gil::pixel< T, Cs > * copy(boost::gil::pixel< T, Cs > *first, boost::gil::pixel< T, Cs > *last, boost::gil::pixel< T, Cs > *dst)
Copy when both src and dst are interleaved and of the same type can be just memmove.
Definition: algorithm.hpp:151
An iterator over non-byte-aligned pixels. Models PixelIteratorConcept, PixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept.
Definition: bit_aligned_pixel_iterator.hpp:48
reference operator[](difference_type d) const
Definition: bit_aligned_pixel_iterator.hpp:77
pixel iterator support
BOOST_FORCEINLINE bool equal(boost::gil::iterator_from_2d< Loc1 > first, boost::gil::iterator_from_2d< Loc1 > last, boost::gil::iterator_from_2d< Loc2 > first2)
std::equal(I1,I1,I2) with I1 and I2 being a iterator_from_2d
Definition: algorithm.hpp:934
GIL configuration file.
A model of a heterogeneous pixel that is not byte aligned. Examples are bitmap (1-bit pixels) or 6-bi...