bit_aligned_pixel_iterator.hpp 8.9 KB

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