bit_aligned_pixel_iterator.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. reference operator[](difference_type d) const { bit_aligned_pixel_iterator it=*this; it.advance(d); return *it; }
  54. reference operator->() const { return **this; }
  55. const bit_range_t& bit_range() const { return _bit_range; }
  56. bit_range_t& bit_range() { 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. reference dereference() const { 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. difference_type distance_to(const bit_aligned_pixel_iterator& it) const { 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 std::ptrdiff_t memunit_step(const bit_aligned_pixel_iterator<NonAlignedPixelReference>&) {
  100. return NonAlignedPixelReference::bit_size;
  101. }
  102. template <typename NonAlignedPixelReference>
  103. inline std::ptrdiff_t memunit_distance(const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p1, const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p2) {
  104. return (p2.bit_range().current_byte() - p1.bit_range().current_byte())*8 + p2.bit_range().bit_offset() - p1.bit_range().bit_offset();
  105. }
  106. template <typename NonAlignedPixelReference>
  107. inline void memunit_advance(bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) {
  108. p.bit_range().bit_advance(diff);
  109. }
  110. template <typename NonAlignedPixelReference>
  111. inline bit_aligned_pixel_iterator<NonAlignedPixelReference> memunit_advanced(const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) {
  112. bit_aligned_pixel_iterator<NonAlignedPixelReference> ret=p;
  113. memunit_advance(ret, diff);
  114. return ret;
  115. }
  116. template <typename NonAlignedPixelReference> inline
  117. NonAlignedPixelReference memunit_advanced_ref(bit_aligned_pixel_iterator<NonAlignedPixelReference> it, std::ptrdiff_t diff) {
  118. return *memunit_advanced(it,diff);
  119. }
  120. /////////////////////////////
  121. // HasDynamicXStepTypeConcept
  122. /////////////////////////////
  123. template <typename NonAlignedPixelReference>
  124. struct dynamic_x_step_type<bit_aligned_pixel_iterator<NonAlignedPixelReference> > {
  125. using type = memory_based_step_iterator<bit_aligned_pixel_iterator<NonAlignedPixelReference> >;
  126. };
  127. /////////////////////////////
  128. // iterator_type_from_pixel
  129. /////////////////////////////
  130. template <typename B, typename C, typename L, bool M>
  131. struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,false>
  132. {
  133. using type = bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,false>> ;
  134. };
  135. template <typename B, typename C, typename L, bool M>
  136. struct iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,false,false,true>
  137. {
  138. using type = bit_aligned_pixel_iterator<bit_aligned_pixel_reference<B,C,L,true>>;
  139. };
  140. template <typename B, typename C, typename L, bool M, bool IsPlanar, bool IsStep, bool IsMutable>
  141. struct iterator_type_from_pixel<bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable>
  142. : public iterator_type_from_pixel<const bit_aligned_pixel_reference<B,C,L,M>,IsPlanar,IsStep,IsMutable> {};
  143. } } // namespace boost::gil
  144. namespace std {
  145. // It is important to provide an overload of uninitialized_copy for bit_aligned_pixel_iterator. The default STL implementation calls placement new,
  146. // which is not defined for bit_aligned_pixel_iterator.
  147. template <typename NonAlignedPixelReference>
  148. boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> uninitialized_copy(boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> first,
  149. boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> last,
  150. boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> dst) {
  151. return std::copy(first,last,dst);
  152. }
  153. } // namespace std
  154. #endif