bit_aligned_pixel_iterator.hpp 9.5 KB

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