bit_aligned_pixel_iterator.hpp 9.5 KB

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