Chris@16: /* Chris@16: Copyright 2005-2007 Adobe Systems Incorporated Chris@16: Chris@16: Use, modification and distribution are subject to the Boost Software License, Chris@16: Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: http://www.boost.org/LICENSE_1_0.txt). Chris@16: Chris@16: See http://stlab.adobe.com/gil for most recent version including documentation. Chris@16: */ Chris@16: Chris@16: /*************************************************************************************************/ Chris@16: Chris@16: #ifndef GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP Chris@16: #define GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////////////// Chris@16: /// \file Chris@16: /// \brief A model of a heterogeneous pixel that is not byte aligned. Examples are bitmap (1-bit pixels) or 6-bit RGB (222) Chris@16: /// \author Lubomir Bourdev and Hailin Jin \n Chris@16: /// Adobe Systems Incorporated Chris@16: /// \date 2005-2007 \n Last updated on September 28, 2006 Chris@16: /// Chris@16: //////////////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include "gil_config.hpp" Chris@16: #include "pixel.hpp" Chris@16: #include "channel.hpp" Chris@16: Chris@16: namespace boost { namespace gil { Chris@16: Chris@16: ///////////////////////////// Chris@16: // bit_range Chris@16: // Chris@16: // Represents a range of bits that can span multiple consecutive bytes. The range has a size fixed at compile time, but the offset is specified at run time. Chris@16: ///////////////////////////// Chris@16: Chris@16: template Chris@16: class bit_range { Chris@16: public: Chris@16: typedef typename mpl::if_c::type byte_t; Chris@16: typedef std::ptrdiff_t difference_type; Chris@16: template friend class bit_range; Chris@16: private: Chris@16: byte_t* _current_byte; // the starting byte of the bit range Chris@16: int _bit_offset; // offset from the beginning of the current byte. 0<=_bit_offset<=7 Chris@16: Chris@16: public: Chris@16: bit_range() : _current_byte(NULL), _bit_offset(0) {} Chris@16: bit_range(byte_t* current_byte, int bit_offset) : _current_byte(current_byte), _bit_offset(bit_offset) { assert(bit_offset>=0 && bit_offset<8); } Chris@16: Chris@16: bit_range(const bit_range& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {} Chris@16: template bit_range(const bit_range& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {} Chris@16: Chris@16: bit_range& operator=(const bit_range& br) { _current_byte = br._current_byte; _bit_offset=br._bit_offset; return *this; } Chris@16: bool operator==(const bit_range& br) const { return _current_byte==br._current_byte && _bit_offset==br._bit_offset; } Chris@16: Chris@16: bit_range& operator++() { Chris@16: _current_byte += (_bit_offset+RangeSize) / 8; Chris@16: _bit_offset = (_bit_offset+RangeSize) % 8; Chris@16: return *this; Chris@16: } Chris@16: bit_range& operator--() { bit_advance(-RangeSize); return *this; } Chris@16: Chris@16: void bit_advance(difference_type num_bits) { Chris@16: int new_offset = int(_bit_offset+num_bits); Chris@16: _current_byte += new_offset / 8; Chris@16: _bit_offset = new_offset % 8; Chris@16: if (_bit_offset<0) { Chris@16: _bit_offset+=8; Chris@16: --_current_byte; Chris@16: } Chris@16: } Chris@16: difference_type bit_distance_to(const bit_range& b) const { Chris@16: return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset(); Chris@16: } Chris@16: byte_t* current_byte() const { return _current_byte; } Chris@16: int bit_offset() const { return _bit_offset; } Chris@16: }; Chris@16: Chris@16: Chris@16: /// \defgroup ColorBaseModelNonAlignedPixel bit_aligned_pixel_reference Chris@16: /// \ingroup ColorBaseModel Chris@16: /// \brief A heterogeneous color base representing pixel that may not be byte aligned, i.e. it may correspond to a bit range that does not start/end at a byte boundary. Models ColorBaseConcept. Chris@16: Chris@16: /** Chris@16: \defgroup PixelModelNonAlignedPixel bit_aligned_pixel_reference Chris@16: \ingroup PixelModel Chris@16: \brief A heterogeneous pixel reference used to represent non-byte-aligned pixels. Models PixelConcept Chris@16: Chris@16: Example: Chris@16: \code Chris@16: unsigned char data=0; Chris@16: Chris@16: // A mutable reference to a 6-bit BGR pixel in "123" format (1 bit for red, 2 bits for green, 3 bits for blue) Chris@16: typedef const bit_aligned_pixel_reference, rgb_layout_t, true> rgb123_ref_t; Chris@16: Chris@16: // create the pixel reference at bit offset 2 Chris@16: // (i.e. red = [2], green = [3,4], blue = [5,6,7] bits) Chris@16: rgb123_ref_t ref(&data, 2); Chris@16: get_color(ref, red_t()) = 1; Chris@16: assert(data == 0x04); Chris@16: get_color(ref, green_t()) = 3; Chris@16: assert(data == 0x1C); Chris@16: get_color(ref, blue_t()) = 7; Chris@16: assert(data == 0xFC); Chris@16: \endcode Chris@16: */ Chris@16: /// \ingroup ColorBaseModelNonAlignedPixel PixelModelNonAlignedPixel PixelBasedModel Chris@16: /// \brief Heterogeneous pixel reference corresponding to non-byte-aligned bit range. Models ColorBaseConcept, PixelConcept, PixelBasedConcept Chris@16: template Chris@16: typename Layout, Chris@16: bool IsMutable> Chris@16: struct bit_aligned_pixel_reference { Chris@16: BOOST_STATIC_CONSTANT(int, bit_size = (mpl::accumulate, mpl::plus >::type::value)); Chris@16: typedef boost::gil::bit_range bit_range_t; Chris@16: typedef BitField bitfield_t; Chris@16: typedef typename mpl::if_c::type data_ptr_t; Chris@16: Chris@16: typedef Layout layout_t; Chris@16: Chris@16: typedef typename packed_pixel_type::type value_type; Chris@16: typedef const bit_aligned_pixel_reference reference; Chris@16: typedef const bit_aligned_pixel_reference const_reference; Chris@16: Chris@16: BOOST_STATIC_CONSTANT(bool, is_mutable = IsMutable); Chris@16: Chris@16: bit_aligned_pixel_reference(){} Chris@16: bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset) : _bit_range(data_ptr, bit_offset) {} Chris@16: explicit bit_aligned_pixel_reference(const bit_range_t& bit_range) : _bit_range(bit_range) {} Chris@16: template bit_aligned_pixel_reference(const bit_aligned_pixel_reference& p) : _bit_range(p._bit_range) {} Chris@16: Chris@16: // Grayscale references can be constructed from the channel reference Chris@16: explicit bit_aligned_pixel_reference(const typename kth_element_type::type channel0) : _bit_range(static_cast(&channel0), channel0.first_bit()) { Chris@16: BOOST_STATIC_ASSERT((num_channels::value==1)); Chris@16: } Chris@16: Chris@16: // Construct from another compatible pixel type Chris@16: bit_aligned_pixel_reference(const bit_aligned_pixel_reference& p) : _bit_range(p._bit_range) {} Chris@16: template bit_aligned_pixel_reference(packed_pixel& p) : _bit_range(static_cast(&gil::at_c<0>(p)), gil::at_c<0>(p).first_bit()) { Chris@16: check_compatible >(); Chris@16: } Chris@16: Chris@16: const bit_aligned_pixel_reference& operator=(const bit_aligned_pixel_reference& p) const { static_copy(p,*this); return *this; } Chris@16: template const bit_aligned_pixel_reference& operator=(const P& p) const { assign(p, mpl::bool_::value>()); return *this; } Chris@16: Chris@16: template bool operator==(const P& p) const { return equal(p, mpl::bool_::value>()); } Chris@16: template bool operator!=(const P& p) const { return !(*this==p); } Chris@16: Chris@16: const bit_aligned_pixel_reference* operator->() const { return this; } Chris@16: Chris@16: const bit_range_t& bit_range() const { return _bit_range; } Chris@16: private: Chris@16: mutable bit_range_t _bit_range; Chris@16: template friend struct bit_aligned_pixel_reference; Chris@16: Chris@16: template static void check_compatible() { gil_function_requires >(); } Chris@16: Chris@16: template void assign(const Pixel& p, mpl::true_) const { check_compatible(); static_copy(p,*this); } Chris@16: template bool equal(const Pixel& p, mpl::true_) const { check_compatible(); return static_equal(*this,p); } Chris@16: Chris@16: private: Chris@16: static void check_gray() { BOOST_STATIC_ASSERT((is_same::value)); } Chris@16: template void assign(const Channel& chan, mpl::false_) const { check_gray(); gil::at_c<0>(*this)=chan; } Chris@16: template bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; } Chris@16: }; Chris@16: Chris@16: ///////////////////////////// Chris@16: // ColorBasedConcept Chris@16: ///////////////////////////// Chris@16: Chris@16: template Chris@16: struct kth_element_type, K> { Chris@16: public: Chris@16: typedef const packed_dynamic_channel_reference::type::value, IsMutable> type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct kth_element_reference_type, K> Chris@16: : public kth_element_type, K> {}; Chris@16: Chris@16: template Chris@16: struct kth_element_const_reference_type, K> Chris@16: : public kth_element_type, K> {}; Chris@16: Chris@16: Chris@16: namespace detail { Chris@16: // returns sum of IntegralVector[0] ... IntegralVector[K-1] Chris@16: template Chris@16: struct sum_k : public mpl::plus, typename mpl::at_c::type > {}; Chris@16: Chris@16: template struct sum_k : public mpl::int_<0> {}; Chris@16: } Chris@16: Chris@16: // at_c required by MutableColorBaseConcept Chris@16: template inline Chris@16: typename kth_element_reference_type,K>::type Chris@16: at_c(const bit_aligned_pixel_reference& p) { Chris@16: typedef bit_aligned_pixel_reference pixel_t; Chris@16: typedef typename kth_element_reference_type::type channel_t; Chris@16: typedef typename pixel_t::bit_range_t bit_range_t; Chris@16: Chris@16: bit_range_t bit_range(p.bit_range()); Chris@16: bit_range.bit_advance(detail::sum_k::value); Chris@16: Chris@16: return channel_t(bit_range.current_byte(), bit_range.bit_offset()); Chris@16: } Chris@16: Chris@16: ///////////////////////////// Chris@16: // PixelConcept Chris@16: ///////////////////////////// Chris@16: Chris@16: /// Metafunction predicate that flags bit_aligned_pixel_reference as a model of PixelConcept. Required by PixelConcept Chris@16: template Chris@16: struct is_pixel > : public mpl::true_{}; Chris@16: Chris@16: ///////////////////////////// Chris@16: // PixelBasedConcept Chris@16: ///////////////////////////// Chris@16: Chris@16: template Chris@16: struct color_space_type > { Chris@16: typedef typename L::color_space_t type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct channel_mapping_type > { Chris@16: typedef typename L::channel_mapping_t type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct is_planar > : mpl::false_ {}; Chris@16: Chris@16: ///////////////////////////// Chris@16: // pixel_reference_type Chris@16: ///////////////////////////// Chris@16: Chris@16: namespace detail { Chris@16: // returns a vector containing K copies of the type T Chris@16: template struct k_copies; Chris@16: template struct k_copies<0,T> { Chris@16: typedef mpl::vector0<> type; Chris@16: }; Chris@16: template struct k_copies : public mpl::push_back::type, T> {}; Chris@16: } Chris@16: Chris@16: // Constructs a homogeneous bit_aligned_pixel_reference given a channel reference Chris@16: template Chris@16: struct pixel_reference_type, Layout, false, false> { Chris@16: private: Chris@16: typedef typename mpl::size::type size_t; Chris@16: typedef typename detail::k_copies >::type channel_bit_sizes_t; Chris@16: public: Chris@16: typedef bit_aligned_pixel_reference type; Chris@16: }; Chris@16: Chris@16: // Same but for the mutable case. We cannot combine the mutable and read-only cases because this triggers ambiguity Chris@16: template Chris@16: struct pixel_reference_type, Layout, false, true> { Chris@16: private: Chris@16: typedef typename mpl::size::type size_t; Chris@16: typedef typename detail::k_copies >::type channel_bit_sizes_t; Chris@16: public: Chris@16: typedef bit_aligned_pixel_reference type; Chris@16: }; Chris@16: Chris@16: } } // namespace boost::gil Chris@16: Chris@16: namespace std { Chris@16: // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified. Chris@16: // swap with 'left bias': Chris@16: // - swap between proxy and anything Chris@16: // - swap between value type and proxy Chris@16: // - swap between proxy and proxy Chris@16: // Having three overloads allows us to swap between different (but compatible) models of PixelConcept Chris@16: Chris@16: template inline Chris@16: void swap(const boost::gil::bit_aligned_pixel_reference x, R& y) { Chris@16: boost::gil::swap_proxy::value_type>(x,y); Chris@16: } Chris@16: Chris@16: Chris@16: template inline Chris@16: void swap(typename boost::gil::bit_aligned_pixel_reference::value_type& x, const boost::gil::bit_aligned_pixel_reference y) { Chris@16: boost::gil::swap_proxy::value_type>(x,y); Chris@16: } Chris@16: Chris@16: Chris@16: template inline Chris@16: void swap(const boost::gil::bit_aligned_pixel_reference x, const boost::gil::bit_aligned_pixel_reference y) { Chris@16: boost::gil::swap_proxy::value_type>(x,y); Chris@16: } Chris@16: } // namespace std Chris@16: #endif