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://opensource.adobe.com/gil for most recent version including documentation. Chris@16: */ Chris@16: Chris@16: /*************************************************************************************************/ Chris@16: Chris@16: #ifndef GIL_PACKED_PIXEL_H Chris@16: #define GIL_PACKED_PIXEL_H Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////////////// Chris@16: /// \file Chris@16: /// \brief A model of a heterogeneous pixel whose channels are bit ranges. For example 16-bit RGB in '565' format Chris@16: /// \author Lubomir Bourdev and Hailin Jin \n Chris@16: /// Adobe Systems Incorporated Chris@16: /// \date 2005-2009 \n Last updated on February 20, 2009 Chris@16: /// Chris@16: //////////////////////////////////////////////////////////////////////////////////////// Chris@16: 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: Chris@16: namespace boost { namespace gil { Chris@16: Chris@16: /// \defgroup ColorBaseModelPackedPixel packed_pixel Chris@16: /// \ingroup ColorBaseModel Chris@16: /// \brief A heterogeneous color base whose elements are reference proxies to channels in a pixel. Models ColorBaseValueConcept. This class is used to model packed pixels, such as 16-bit packed RGB. Chris@16: Chris@16: /** Chris@16: \defgroup PixelModelPackedPixel packed_pixel Chris@16: \ingroup PixelModel Chris@16: \brief A heterogeneous pixel used to represent packed pixels with non-byte-aligned channels. Models PixelValueConcept Chris@16: Chris@16: Example: Chris@16: \code Chris@16: typedef packed_pixel_type, rgb_layout_t>::type rgb565_pixel_t; Chris@16: BOOST_STATIC_ASSERT((sizeof(rgb565_pixel_t)==2)); Chris@16: Chris@16: rgb565_pixel_t r565; Chris@16: get_color(r565,red_t()) = 31; Chris@16: get_color(r565,green_t()) = 63; Chris@16: get_color(r565,blue_t()) = 31; Chris@16: assert(r565 == rgb565_pixel_t((uint16_t)0xFFFF)); Chris@16: \endcode Chris@16: */ Chris@16: Chris@16: /// \ingroup ColorBaseModelPackedPixel PixelModelPackedPixel PixelBasedModel Chris@16: /// \brief Heterogeneous pixel value whose channel references can be constructed from the pixel bitfield and their index. Models ColorBaseValueConcept, PixelValueConcept, PixelBasedConcept Chris@16: /// Typical use for this is a model of a packed pixel (like 565 RGB) Chris@16: template // Layout defining the color space and ordering of the channels. Example value: rgb_layout_t Chris@16: struct packed_pixel { Chris@16: BitField _bitfield; Chris@16: Chris@16: typedef Layout layout_t; Chris@16: typedef packed_pixel value_type; Chris@16: typedef value_type& reference; Chris@16: typedef const value_type& const_reference; Chris@16: Chris@16: BOOST_STATIC_CONSTANT(bool, is_mutable = channel_traits::type>::is_mutable); Chris@16: Chris@16: packed_pixel(){} Chris@16: explicit packed_pixel(const BitField& bitfield) : _bitfield(bitfield) {} Chris@16: Chris@16: // Construct from another compatible pixel type Chris@16: packed_pixel(const packed_pixel& p) : _bitfield(p._bitfield) {} Chris@16: template packed_pixel(const P& p, typename enable_if_c::value>::type* d=0) { check_compatible

(); static_copy(p,*this); } Chris@16: packed_pixel(int chan0, int chan1) : _bitfield(0) { Chris@16: BOOST_STATIC_ASSERT((num_channels::value==2)); Chris@16: at_c<0>(*this)=chan0; at_c<1>(*this)=chan1; Chris@16: } Chris@16: packed_pixel(int chan0, int chan1, int chan2) : _bitfield(0) { Chris@16: BOOST_STATIC_ASSERT((num_channels::value==3)); Chris@16: gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1; gil::at_c<2>(*this)=chan2; Chris@16: } Chris@16: packed_pixel(int chan0, int chan1, int chan2, int chan3) : _bitfield(0) { Chris@16: BOOST_STATIC_ASSERT((num_channels::value==4)); Chris@16: gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1; gil::at_c<2>(*this)=chan2; gil::at_c<3>(*this)=chan3; Chris@16: } Chris@16: packed_pixel(int chan0, int chan1, int chan2, int chan3, int chan4) : _bitfield(0) { Chris@16: BOOST_STATIC_ASSERT((num_channels::value==5)); Chris@16: gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1; gil::at_c<2>(*this)=chan2; gil::at_c<3>(*this)=chan3; gil::at_c<4>(*this)=chan4; Chris@16: } Chris@16: Chris@16: packed_pixel& operator=(const packed_pixel& p) { _bitfield=p._bitfield; return *this; } Chris@16: Chris@16: template packed_pixel& operator=(const P& p) { assign(p, mpl::bool_::value>()); return *this; } Chris@16: template bool operator==(const P& p) const { return equal(p, mpl::bool_::value>()); } Chris@16: Chris@16: template bool operator!=(const P& p) const { return !(*this==p); } Chris@16: Chris@16: private: Chris@16: template static void check_compatible() { gil_function_requires >(); } Chris@16: template void assign(const Pixel& p, mpl::true_) { 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: // Support for assignment/equality comparison of a channel with a grayscale pixel Chris@16: static void check_gray() { BOOST_STATIC_ASSERT((is_same::value)); } Chris@16: template void assign(const Channel& chan, mpl::false_) { check_gray(); at_c<0>(*this)=chan; } Chris@16: template bool equal (const Channel& chan, mpl::false_) const { check_gray(); return at_c<0>(*this)==chan; } Chris@16: public: Chris@16: packed_pixel& operator= (int chan) { check_gray(); at_c<0>(*this)=chan; return *this; } Chris@16: bool operator==(int chan) const { check_gray(); return 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> : public mpl::at_c {}; Chris@16: Chris@16: template Chris@16: struct kth_element_reference_type,K> : public mpl::at_c {}; Chris@16: Chris@16: template Chris@16: struct kth_element_const_reference_type,K> { Chris@16: typedef typename channel_traits::type>::const_reference type; Chris@16: }; Chris@16: Chris@16: template inline Chris@16: typename kth_element_reference_type, K>::type Chris@16: at_c(packed_pixel& p) { Chris@16: return typename kth_element_reference_type, K>::type(&p._bitfield); Chris@16: } Chris@16: Chris@16: template inline Chris@16: typename kth_element_const_reference_type, K>::type Chris@16: at_c(const packed_pixel& p) { Chris@16: return typename kth_element_const_reference_type, K>::type(&p._bitfield); Chris@16: } Chris@16: Chris@16: ///////////////////////////// Chris@16: // PixelConcept Chris@16: ///////////////////////////// Chris@16: Chris@16: // Metafunction predicate that flags packed_pixel 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 Layout::color_space_t type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct channel_mapping_type > { Chris@16: typedef typename Layout::channel_mapping_t type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct is_planar > : mpl::false_ {}; Chris@16: Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////// Chris@16: /// Chris@16: /// Support for interleaved iterators over packed pixel Chris@16: /// Chris@16: //////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: /// \defgroup PixelIteratorModelPackedInterleavedPtr Pointer to packed_pixel Chris@16: /// \ingroup PixelIteratorModel Chris@16: /// \brief Iterators over interleaved pixels. Chris@16: /// The pointer packed_pixel* is used as an iterator over interleaved pixels of packed format. Models PixelIteratorConcept, HasDynamicXStepTypeConcept, MemoryBasedIteratorConcept Chris@16: Chris@16: template Chris@16: struct iterator_is_mutable*> : public mpl::bool_::is_mutable> {}; Chris@16: template Chris@16: struct iterator_is_mutable*> : public mpl::false_ {}; Chris@16: Chris@16: Chris@16: Chris@16: } } // namespace boost::gil Chris@16: Chris@16: namespace boost { Chris@16: template Chris@16: struct has_trivial_constructor > : public has_trivial_constructor

{}; Chris@16: } Chris@16: #endif