bit_aligned_pixel_iterator.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include <boost/gil/bit_aligned_pixel_reference.hpp>
  11. #include <boost/gil/pixel_iterator.hpp>
  12. #include <boost/config.hpp>
  13. #include <boost/iterator/iterator_facade.hpp>
  14. #include <functional>
  15. #include <type_traits>
  16. namespace boost { namespace gil {
  17. /// A model of a heterogeneous pixel that is not byte aligned.
  18. /// Examples are bitmap (1-bit pixels) or 6-bit RGB (222).
  19. /// \defgroup PixelIteratorNonAlignedPixelIterator bit_aligned_pixel_iterator
  20. /// \ingroup PixelIteratorModel
  21. /// \brief An iterator over non-byte-aligned pixels. Models PixelIteratorConcept, PixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
  22. ////////////////////////////////////////////////////////////////////////////////////////
  23. /// \brief An iterator over non-byte-aligned pixels. Models PixelIteratorConcept, PixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
  24. ///
  25. /// 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.
  26. ///
  27. /// \ingroup PixelIteratorNonAlignedPixelIterator PixelBasedModel
  28. template <typename NonAlignedPixelReference>
  29. struct bit_aligned_pixel_iterator : public iterator_facade<bit_aligned_pixel_iterator<NonAlignedPixelReference>,
  30. typename NonAlignedPixelReference::value_type,
  31. std::random_access_iterator_tag,
  32. const NonAlignedPixelReference,
  33. typename NonAlignedPixelReference::bit_range_t::difference_type> {
  34. private:
  35. using parent_t = iterator_facade<bit_aligned_pixel_iterator<NonAlignedPixelReference>,
  36. typename NonAlignedPixelReference::value_type,
  37. std::random_access_iterator_tag,
  38. const NonAlignedPixelReference,
  39. typename NonAlignedPixelReference::bit_range_t::difference_type>;
  40. template <typename Ref> friend struct bit_aligned_pixel_iterator;
  41. using bit_range_t = typename NonAlignedPixelReference::bit_range_t;
  42. public:
  43. using difference_type = typename parent_t::difference_type;
  44. using reference = typename parent_t::reference;
  45. bit_aligned_pixel_iterator() {}
  46. bit_aligned_pixel_iterator(const bit_aligned_pixel_iterator& p) : _bit_range(p._bit_range) {}
  47. bit_aligned_pixel_iterator& operator=(const bit_aligned_pixel_iterator& p) { _bit_range=p._bit_range; return *this; }
  48. template <typename Ref> bit_aligned_pixel_iterator(const bit_aligned_pixel_iterator<Ref>& p) : _bit_range(p._bit_range) {}
  49. bit_aligned_pixel_iterator(reference* ref) : _bit_range(ref->bit_range()) {}
  50. explicit bit_aligned_pixel_iterator(typename bit_range_t::byte_t* data, int bit_offset=0) : _bit_range(data,bit_offset) {}
  51. /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference
  52. /// We require our own reference because it is registered in iterator_traits
  53. auto operator[](difference_type d) const -> reference { bit_aligned_pixel_iterator it=*this; it.advance(d); return *it; }
  54. auto operator->() const -> reference { return **this; }
  55. auto bit_range() const -> bit_range_t const& { return _bit_range; }
  56. auto bit_range() -> bit_range_t& { return _bit_range; }
  57. private:
  58. bit_range_t _bit_range;
  59. static constexpr int bit_size = NonAlignedPixelReference::bit_size;
  60. friend class boost::iterator_core_access;
  61. auto dereference() const -> reference { return NonAlignedPixelReference(_bit_range); }
  62. void increment() { ++_bit_range; }
  63. void decrement() { --_bit_range; }
  64. void advance(difference_type d) { _bit_range.bit_advance(d*bit_size); }
  65. auto distance_to(bit_aligned_pixel_iterator const& it) const -> difference_type { return _bit_range.bit_distance_to(it._bit_range) / bit_size; }
  66. bool equal(const bit_aligned_pixel_iterator& it) const { return _bit_range==it._bit_range; }
  67. };
  68. template <typename NonAlignedPixelReference>
  69. struct const_iterator_type<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
  70. {
  71. using type =
  72. bit_aligned_pixel_iterator<typename NonAlignedPixelReference::const_reference>;
  73. };
  74. template <typename NonAlignedPixelReference>
  75. struct iterator_is_mutable<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
  76. : std::integral_constant<bool, NonAlignedPixelReference::is_mutable>
  77. {};
  78. template <typename NonAlignedPixelReference>
  79. struct is_iterator_adaptor<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
  80. : std::false_type
  81. {};
  82. /////////////////////////////
  83. // PixelBasedConcept
  84. /////////////////////////////
  85. template <typename NonAlignedPixelReference>
  86. struct color_space_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public color_space_type<NonAlignedPixelReference> {};
  87. template <typename NonAlignedPixelReference>
  88. struct channel_mapping_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public channel_mapping_type<NonAlignedPixelReference> {};
  89. template <typename NonAlignedPixelReference>
  90. struct is_planar<bit_aligned_pixel_iterator<NonAlignedPixelReference> > : public is_planar<NonAlignedPixelReference> {}; // == false
  91. /////////////////////////////
  92. // MemoryBasedIteratorConcept
  93. /////////////////////////////
  94. template <typename NonAlignedPixelReference>
  95. struct byte_to_memunit<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
  96. : std::integral_constant<int, 8>
  97. {};
  98. template <typename NonAlignedPixelReference>
  99. inline auto memunit_step(const bit_aligned_pixel_iterator<NonAlignedPixelReference>&) -> std::ptrdiff_t
  100. {
  101. return NonAlignedPixelReference::bit_size;
  102. }
  103. template <typename NonAlignedPixelReference>
  104. inline auto memunit_distance(bit_aligned_pixel_iterator<NonAlignedPixelReference> const& p1, bit_aligned_pixel_iterator<NonAlignedPixelReference> const& p2) -> std::ptrdiff_t
  105. {
  106. return (p2.bit_range().current_byte() - p1.bit_range().current_byte())*8 + p2.bit_range().bit_offset() - p1.bit_range().bit_offset();
  107. }
  108. template <typename NonAlignedPixelReference>
  109. inline void memunit_advance(bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) {
  110. p.bit_range().bit_advance(diff);
  111. }
  112. template <typename NonAlignedPixelReference>
  113. inline auto memunit_advanced(bit_aligned_pixel_iterator<NonAlignedPixelReference> const& p, std::ptrdiff_t diff) -> bit_aligned_pixel_iterator<NonAlignedPixelReference> {
  114. bit_aligned_pixel_iterator<NonAlignedPixelReference> ret=p;
  115. memunit_advance(ret, diff);
  116. return ret;
  117. }
  118. template <typename NonAlignedPixelReference> inline
  119. auto memunit_advanced_ref(bit_aligned_pixel_iterator<NonAlignedPixelReference> it, std::ptrdiff_t diff) -> NonAlignedPixelReference
  120. {
  121. return *memunit_advanced(it,diff);
  122. }
  123. /////////////////////////////
  124. // HasDynamicXStepTypeConcept
  125. /////////////////////////////
  126. template <typename NonAlignedPixelReference>
  127. struct dynamic_x_step_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > {
  128. using type = memory_based_step_iterator<bit_aligned_pixel_iterator<NonAlignedPixelReference> >;
  129. };
  130. /////////////////////////////
  131. // iterator_type_from_pixel
  132. /////////////////////////////
  133. template <typename B, typename C, typename L, bool M>
  134. struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,false>
  135. {
  136. using type = bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,false>> ;
  137. };
  138. template <typename B, typename C, typename L, bool M>
  139. struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,true>
  140. {
  141. using type = bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,true>>;
  142. };
  143. template <typename B, typename C, typename L, bool M, bool IsPlanar, bool IsStep, bool IsMutable>
  144. struct iterator_type_from_pixel<bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable>
  145. : public iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable> {};
  146. } } // namespace boost::gil
  147. namespace std {
  148. // It is important to provide an overload of uninitialized_copy for bit_aligned_pixel_iterator. The default STL implementation calls placement new,
  149. // which is not defined for bit_aligned_pixel_iterator.
  150. template <typename NonAlignedPixelReference>
  151. auto uninitialized_copy(boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> first,
  152. boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> last,
  153. boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> dst)
  154. -> boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference>
  155. {
  156. return std::copy(first,last,dst);
  157. }
  158. } // namespace std
  159. #endif