// // Copyright 2005-2007 Adobe Systems Incorporated // // Distributed under the Boost Software License, Version 1.0 // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt // #ifndef BOOST_GIL_BIT_ALIGNED_PIXEL_ITERATOR_HPP #define BOOST_GIL_BIT_ALIGNED_PIXEL_ITERATOR_HPP #include #include #include #include #include #include namespace boost { namespace gil { /// A model of a heterogeneous pixel that is not byte aligned. /// Examples are bitmap (1-bit pixels) or 6-bit RGB (222). /// \defgroup PixelIteratorNonAlignedPixelIterator bit_aligned_pixel_iterator /// \ingroup PixelIteratorModel /// \brief An iterator over non-byte-aligned pixels. Models PixelIteratorConcept, PixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept //////////////////////////////////////////////////////////////////////////////////////// /// \brief An iterator over non-byte-aligned pixels. Models PixelIteratorConcept, PixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept /// /// 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. /// /// \ingroup PixelIteratorNonAlignedPixelIterator PixelBasedModel template struct bit_aligned_pixel_iterator : public iterator_facade, typename NonAlignedPixelReference::value_type, std::random_access_iterator_tag, const NonAlignedPixelReference, typename NonAlignedPixelReference::bit_range_t::difference_type> { private: using parent_t = iterator_facade, typename NonAlignedPixelReference::value_type, std::random_access_iterator_tag, const NonAlignedPixelReference, typename NonAlignedPixelReference::bit_range_t::difference_type>; template friend struct bit_aligned_pixel_iterator; using bit_range_t = typename NonAlignedPixelReference::bit_range_t; public: using difference_type = typename parent_t::difference_type; using reference = typename parent_t::reference; bit_aligned_pixel_iterator() {} bit_aligned_pixel_iterator(const bit_aligned_pixel_iterator& p) : _bit_range(p._bit_range) {} bit_aligned_pixel_iterator& operator=(const bit_aligned_pixel_iterator& p) { _bit_range=p._bit_range; return *this; } template bit_aligned_pixel_iterator(const bit_aligned_pixel_iterator& p) : _bit_range(p._bit_range) {} bit_aligned_pixel_iterator(reference* ref) : _bit_range(ref->bit_range()) {} explicit bit_aligned_pixel_iterator(typename bit_range_t::byte_t* data, int bit_offset=0) : _bit_range(data,bit_offset) {} /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference /// We require our own reference because it is registered in iterator_traits auto operator[](difference_type d) const -> reference { bit_aligned_pixel_iterator it=*this; it.advance(d); return *it; } auto operator->() const -> reference { return **this; } auto bit_range() const -> bit_range_t const& { return _bit_range; } auto bit_range() -> bit_range_t& { return _bit_range; } private: bit_range_t _bit_range; static constexpr int bit_size = NonAlignedPixelReference::bit_size; friend class boost::iterator_core_access; auto dereference() const -> reference { return NonAlignedPixelReference(_bit_range); } void increment() { ++_bit_range; } void decrement() { --_bit_range; } void advance(difference_type d) { _bit_range.bit_advance(d*bit_size); } auto distance_to(bit_aligned_pixel_iterator const& it) const -> difference_type { return _bit_range.bit_distance_to(it._bit_range) / bit_size; } bool equal(const bit_aligned_pixel_iterator& it) const { return _bit_range==it._bit_range; } }; template struct const_iterator_type> { using type = bit_aligned_pixel_iterator; }; template struct iterator_is_mutable> : std::integral_constant {}; template struct is_iterator_adaptor> : std::false_type {}; ///////////////////////////// // PixelBasedConcept ///////////////////////////// template struct color_space_type > : public color_space_type {}; template struct channel_mapping_type > : public channel_mapping_type {}; template struct is_planar > : public is_planar {}; // == false ///////////////////////////// // MemoryBasedIteratorConcept ///////////////////////////// template struct byte_to_memunit> : std::integral_constant {}; template inline auto memunit_step(const bit_aligned_pixel_iterator&) -> std::ptrdiff_t { return NonAlignedPixelReference::bit_size; } template inline auto memunit_distance(bit_aligned_pixel_iterator const& p1, bit_aligned_pixel_iterator const& p2) -> std::ptrdiff_t { return (p2.bit_range().current_byte() - p1.bit_range().current_byte())*8 + p2.bit_range().bit_offset() - p1.bit_range().bit_offset(); } template inline void memunit_advance(bit_aligned_pixel_iterator& p, std::ptrdiff_t diff) { p.bit_range().bit_advance(diff); } template inline auto memunit_advanced(bit_aligned_pixel_iterator const& p, std::ptrdiff_t diff) -> bit_aligned_pixel_iterator { bit_aligned_pixel_iterator ret=p; memunit_advance(ret, diff); return ret; } template inline auto memunit_advanced_ref(bit_aligned_pixel_iterator it, std::ptrdiff_t diff) -> NonAlignedPixelReference { return *memunit_advanced(it,diff); } ///////////////////////////// // HasDynamicXStepTypeConcept ///////////////////////////// template struct dynamic_x_step_type > { using type = memory_based_step_iterator >; }; ///////////////////////////// // iterator_type_from_pixel ///////////////////////////// template struct iterator_type_from_pixel,false,false,false> { using type = bit_aligned_pixel_iterator> ; }; template struct iterator_type_from_pixel,false,false,true> { using type = bit_aligned_pixel_iterator>; }; template struct iterator_type_from_pixel,IsPlanar,IsStep,IsMutable> : public iterator_type_from_pixel,IsPlanar,IsStep,IsMutable> {}; } } // namespace boost::gil namespace std { // It is important to provide an overload of uninitialized_copy for bit_aligned_pixel_iterator. The default STL implementation calls placement new, // which is not defined for bit_aligned_pixel_iterator. template auto uninitialized_copy(boost::gil::bit_aligned_pixel_iterator first, boost::gil::bit_aligned_pixel_iterator last, boost::gil::bit_aligned_pixel_iterator dst) -> boost::gil::bit_aligned_pixel_iterator { return std::copy(first,last,dst); } } // namespace std #endif