diff options
Diffstat (limited to 'boost/gil/bit_aligned_pixel_reference.hpp')
-rw-r--r-- | boost/gil/bit_aligned_pixel_reference.hpp | 158 |
1 files changed, 91 insertions, 67 deletions
diff --git a/boost/gil/bit_aligned_pixel_reference.hpp b/boost/gil/bit_aligned_pixel_reference.hpp index 35f85f8043..13e083e988 100644 --- a/boost/gil/bit_aligned_pixel_reference.hpp +++ b/boost/gil/bit_aligned_pixel_reference.hpp @@ -11,6 +11,7 @@ #include <boost/gil/pixel.hpp> #include <boost/gil/channel.hpp> +#include <boost/assert.hpp> #include <boost/config.hpp> #include <boost/mpl/accumulate.hpp> #include <boost/mpl/at.hpp> @@ -21,6 +22,7 @@ #include <boost/mpl/vector.hpp> #include <functional> +#include <type_traits> namespace boost { namespace gil { @@ -36,16 +38,21 @@ namespace boost { namespace gil { template <int RangeSize, bool Mutable> class bit_range { public: - typedef typename mpl::if_c<Mutable,unsigned char,const unsigned char>::type byte_t; - typedef std::ptrdiff_t difference_type; + using byte_t = typename mpl::if_c<Mutable,unsigned char,const unsigned char>::type; + using difference_type = std::ptrdiff_t; template <int RS, bool M> friend class bit_range; private: byte_t* _current_byte; // the starting byte of the bit range int _bit_offset; // offset from the beginning of the current byte. 0<=_bit_offset<=7 public: - bit_range() : _current_byte(NULL), _bit_offset(0) {} - bit_range(byte_t* current_byte, int bit_offset) : _current_byte(current_byte), _bit_offset(bit_offset) { assert(bit_offset>=0 && bit_offset<8); } + bit_range() : _current_byte(nullptr), _bit_offset(0) {} + bit_range(byte_t* current_byte, int bit_offset) + : _current_byte(current_byte) + , _bit_offset(bit_offset) + { + BOOST_ASSERT(bit_offset >= 0 && bit_offset < 8); + } bit_range(const bit_range& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {} template <bool M> bit_range(const bit_range<RangeSize,M>& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {} @@ -76,53 +83,61 @@ public: int bit_offset() const { return _bit_offset; } }; - /// \defgroup ColorBaseModelNonAlignedPixel bit_aligned_pixel_reference /// \ingroup ColorBaseModel /// \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. - -/** -\defgroup PixelModelNonAlignedPixel bit_aligned_pixel_reference -\ingroup PixelModel -\brief A heterogeneous pixel reference used to represent non-byte-aligned pixels. Models PixelConcept - -Example: -\code -unsigned char data=0; - -// A mutable reference to a 6-bit BGR pixel in "123" format (1 bit for red, 2 bits for green, 3 bits for blue) -typedef const bit_aligned_pixel_reference<unsigned char, mpl::vector3_c<int,1,2,3>, rgb_layout_t, true> rgb123_ref_t; - -// create the pixel reference at bit offset 2 -// (i.e. red = [2], green = [3,4], blue = [5,6,7] bits) -rgb123_ref_t ref(&data, 2); -get_color(ref, red_t()) = 1; -assert(data == 0x04); -get_color(ref, green_t()) = 3; -assert(data == 0x1C); -get_color(ref, blue_t()) = 7; -assert(data == 0xFC); -\endcode -*/ +/// +/// \defgroup PixelModelNonAlignedPixel bit_aligned_pixel_reference +/// \ingroup PixelModel +/// \brief A heterogeneous pixel reference used to represent non-byte-aligned pixels. Models PixelConcept +/// +/// Example: +/// \code +/// unsigned char data=0; +/// +/// // A mutable reference to a 6-bit BGR pixel in "123" format (1 bit for red, 2 bits for green, 3 bits for blue) +/// using rgb123_ref_t = bit_aligned_pixel_reference<unsigned char, mpl::vector3_c<int,1,2,3>, rgb_layout_t, true> const; +/// +/// // create the pixel reference at bit offset 2 +/// // (i.e. red = [2], green = [3,4], blue = [5,6,7] bits) +/// rgb123_ref_t ref(&data, 2); +/// get_color(ref, red_t()) = 1; +/// assert(data == 0x04); +/// get_color(ref, green_t()) = 3; +/// assert(data == 0x1C); +/// get_color(ref, blue_t()) = 7; +/// assert(data == 0xFC); +/// \endcode +/// /// \ingroup ColorBaseModelNonAlignedPixel PixelModelNonAlignedPixel PixelBasedModel /// \brief Heterogeneous pixel reference corresponding to non-byte-aligned bit range. Models ColorBaseConcept, PixelConcept, PixelBasedConcept -template <typename BitField, - typename ChannelBitSizes, // MPL integral vector defining the number of bits for each channel. For example, for 565RGB, vector_c<int,5,6,5> - typename Layout, - bool IsMutable> -struct bit_aligned_pixel_reference { - BOOST_STATIC_CONSTANT(int, bit_size = (mpl::accumulate<ChannelBitSizes, mpl::int_<0>, mpl::plus<mpl::_1, mpl::_2> >::type::value)); - typedef boost::gil::bit_range<bit_size,IsMutable> bit_range_t; - typedef BitField bitfield_t; - typedef typename mpl::if_c<IsMutable,unsigned char*,const unsigned char*>::type data_ptr_t; - - typedef Layout layout_t; - - typedef typename packed_pixel_type<bitfield_t,ChannelBitSizes,Layout>::type value_type; - typedef const bit_aligned_pixel_reference reference; - typedef const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,false> const_reference; - - BOOST_STATIC_CONSTANT(bool, is_mutable = IsMutable); +/// +/// \tparam BitField +/// \tparam ChannelBitSizes MPL integral vector defining the number of bits for each channel. For example, for 565RGB, vector_c<int,5,6,5> +/// \tparam Layout +/// \tparam IsMutable +template <typename BitField, typename ChannelBitSizes, typename Layout, bool IsMutable> +struct bit_aligned_pixel_reference +{ + static constexpr int bit_size = + mpl::accumulate + < + ChannelBitSizes, + mpl::int_<0>, + mpl::plus<mpl::_1, mpl::_2> + >::type::value; + + using bit_range_t = boost::gil::bit_range<bit_size,IsMutable>; + using bitfield_t = BitField; + using data_ptr_t =typename mpl::if_c<IsMutable,unsigned char*,const unsigned char*>::type; + + using layout_t = Layout; + + using value_type = typename packed_pixel_type<bitfield_t,ChannelBitSizes,Layout>::type; + using reference = const bit_aligned_pixel_reference<BitField, ChannelBitSizes, Layout, IsMutable>; + using const_reference = bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,false> const; + + static constexpr bool is_mutable = IsMutable; bit_aligned_pixel_reference(){} bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset) : _bit_range(data_ptr, bit_offset) {} @@ -130,8 +145,10 @@ struct bit_aligned_pixel_reference { template <bool IsMutable2> bit_aligned_pixel_reference(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,IsMutable2>& p) : _bit_range(p._bit_range) {} // Grayscale references can be constructed from the channel reference - explicit bit_aligned_pixel_reference(const typename kth_element_type<bit_aligned_pixel_reference,0>::type channel0) : _bit_range(static_cast<data_ptr_t>(&channel0), channel0.first_bit()) { - BOOST_STATIC_ASSERT((num_channels<bit_aligned_pixel_reference>::value==1)); + explicit bit_aligned_pixel_reference(typename kth_element_type<bit_aligned_pixel_reference,0>::type const channel0) + : _bit_range(static_cast<data_ptr_t>(&channel0), channel0.first_bit()) + { + static_assert(num_channels<bit_aligned_pixel_reference>::value == 1, ""); } // Construct from another compatible pixel type @@ -159,7 +176,11 @@ private: template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); } private: - static void check_gray() { BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); } + static void check_gray() + { + static_assert(std::is_same<typename Layout::color_space_t, gray_t>::value, ""); + } + template <typename Channel> void assign(const Channel& chan, mpl::false_) const { check_gray(); gil::at_c<0>(*this)=chan; } template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; } }; @@ -169,9 +190,9 @@ private: ///////////////////////////// template <typename BitField, typename ChannelBitSizes, typename L, bool IsMutable, int K> -struct kth_element_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,IsMutable>, K> { -public: - typedef const packed_dynamic_channel_reference<BitField, mpl::at_c<ChannelBitSizes,K>::type::value, IsMutable> type; +struct kth_element_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,IsMutable>, K> +{ + using type = packed_dynamic_channel_reference<BitField, mpl::at_c<ChannelBitSizes,K>::type::value, IsMutable> const; }; template <typename B, typename C, typename L, bool M, int K> @@ -194,10 +215,11 @@ namespace detail { // at_c required by MutableColorBaseConcept template <int K, typename BitField, typename ChannelBitSizes, typename L, bool Mutable> inline typename kth_element_reference_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>,K>::type -at_c(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>& p) { - typedef bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable> pixel_t; - typedef typename kth_element_reference_type<pixel_t,K>::type channel_t; - typedef typename pixel_t::bit_range_t bit_range_t; +at_c(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>& p) +{ + using pixel_t = bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>; + using channel_t = typename kth_element_reference_type<pixel_t,K>::type; + using bit_range_t = typename pixel_t::bit_range_t; bit_range_t bit_range(p.bit_range()); bit_range.bit_advance(detail::sum_k<ChannelBitSizes,K>::value); @@ -219,12 +241,12 @@ struct is_pixel<bit_aligned_pixel_reference<B,C,L,M> > : public mpl::true_{}; template <typename B, typename C, typename L, bool M> struct color_space_type<bit_aligned_pixel_reference<B,C,L,M> > { - typedef typename L::color_space_t type; + using type = typename L::color_space_t; }; template <typename B, typename C, typename L, bool M> struct channel_mapping_type<bit_aligned_pixel_reference<B,C,L,M> > { - typedef typename L::channel_mapping_t type; + using type = typename L::channel_mapping_t; }; template <typename B, typename C, typename L, bool M> @@ -238,29 +260,31 @@ namespace detail { // returns a vector containing K copies of the type T template <unsigned K, typename T> struct k_copies; template <typename T> struct k_copies<0,T> { - typedef mpl::vector0<> type; + using type = mpl::vector0<>; }; template <unsigned K, typename T> struct k_copies : public mpl::push_back<typename k_copies<K-1,T>::type, T> {}; } // Constructs a homogeneous bit_aligned_pixel_reference given a channel reference template <typename BitField, int NumBits, typename Layout> -struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,false>, Layout, false, false> { +struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,false>, Layout, false, false> +{ private: - typedef typename mpl::size<typename Layout::color_space_t>::type size_t; - typedef typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type channel_bit_sizes_t; + using size_t = typename mpl::size<typename Layout::color_space_t>::type; + using channel_bit_sizes_t = typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type; public: - typedef bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false> type; + using type = bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false>; }; // Same but for the mutable case. We cannot combine the mutable and read-only cases because this triggers ambiguity template <typename BitField, int NumBits, typename Layout> -struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,true>, Layout, false, true> { +struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,true>, Layout, false, true> +{ private: - typedef typename mpl::size<typename Layout::color_space_t>::type size_t; - typedef typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type channel_bit_sizes_t; + using size_t = typename mpl::size<typename Layout::color_space_t>::type; + using channel_bit_sizes_t = typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits>>::type; public: - typedef bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true> type; + using type = bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true>; }; } } // namespace boost::gil |