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: #ifndef GIL_PIXEL_H Chris@16: #define GIL_PIXEL_H Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////////////// Chris@16: /// \file Chris@16: /// \brief pixel class and related utilities 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 "gil_config.hpp" Chris@16: #include "color_base.hpp" Chris@16: #include "gil_concept.hpp" Chris@16: #include "channel.hpp" Chris@16: #include "metafunctions.hpp" Chris@16: #include "utilities.hpp" Chris@16: #include "color_base_algorithm.hpp" Chris@16: Chris@16: namespace boost { namespace gil { Chris@16: Chris@16: // Forward-declare gray_t Chris@16: struct gray_color_t; Chris@16: typedef mpl::vector1 gray_t; Chris@16: template struct color_space_type; Chris@16: template struct channel_mapping_type; Chris@16: template struct channel_type; Chris@16: template struct is_planar; Chris@16: Chris@16: template struct color_space_type : public color_space_type {}; Chris@16: template struct channel_mapping_type : public channel_mapping_type {}; Chris@16: template struct channel_type : public channel_type {}; Chris@16: Chris@16: template struct is_planar : mpl::false_ {}; Chris@16: template struct is_planar : public is_planar {}; Chris@16: Chris@16: Chris@16: template struct is_pixel : public mpl::false_{}; Chris@16: template struct is_pixel : public is_pixel {}; Chris@16: Chris@16: /// \ingroup PixelBasedAlgorithm Chris@16: /// \brief Returns the number of channels of a pixel-based GIL construct Chris@16: template Chris@16: struct num_channels : public mpl::size::type> {}; Chris@16: Chris@16: /** Chris@16: \addtogroup PixelBasedAlgorithm Chris@16: Chris@16: Example: Chris@16: \code Chris@16: BOOST_STATIC_ASSERT((num_channels::value==3)); Chris@16: BOOST_STATIC_ASSERT((num_channels::value==4)); Chris@16: Chris@16: BOOST_STATIC_ASSERT((is_planar::value)); Chris@16: BOOST_STATIC_ASSERT((is_same::type, rgb_t>::value)); Chris@16: BOOST_STATIC_ASSERT((is_same::type, Chris@16: channel_mapping_type::type>::value)); Chris@16: BOOST_STATIC_ASSERT((is_same::type, bits8>::value)); Chris@16: \endcode Chris@16: */ Chris@16: Chris@16: /// \defgroup ColorBaseModelPixel pixel Chris@16: /// \ingroup ColorBaseModel Chris@16: /// \brief A homogeneous color base whose element is a channel value. Models HomogeneousColorBaseValueConcept Chris@16: Chris@16: /// \defgroup PixelModelPixel pixel Chris@16: /// \ingroup PixelModel Chris@16: /// \brief A homogeneous pixel value. Models HomogeneousPixelValueConcept Chris@16: Chris@16: /// \ingroup PixelModelPixel ColorBaseModelPixel PixelBasedModel Chris@16: /// \brief Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept, PixelValueConcept, HomogeneousPixelBasedConcept Chris@16: /// Chris@16: /// A pixel is a set of channels defining the color at a given point in an image. Conceptually, a pixel is little more than a color base whose elements Chris@16: /// model \p ChannelConcept. The class \p pixel defines a simple, homogeneous pixel value. It is used to store Chris@16: /// the value of a color. The built-in C++ references to \p pixel, \p pixel& and \p const \p pixel& are used to represent a reference to a pixel Chris@16: /// inside an interleaved image view (a view in which all channels are together in memory). Similarly, built-in pointer types \p pixel* and \p const \p pixel* Chris@16: /// are used as the standard iterator over a row of interleaved homogeneous pixels. Chris@16: /// Chris@16: /// Since \p pixel inherits the properties of color base, assigning, equality comparison and copy-construcion are allowed between compatible pixels. Chris@16: /// This means that an 8-bit RGB pixel may be assigned to an 8-bit BGR pixel, or to an 8-bit planar reference. The channels are properly paired semantically. Chris@16: /// Chris@16: /// The single-channel (grayscale) instantiation of the class pixel, (i.e. \p pixel) is also convertible to/from a channel value. Chris@16: /// This allows grayscale pixels to be used in simpler expressions like *gray_pix1 = *gray_pix2 instead of more complicated at_c<0>(gray_pix1) = at_c<0>(gray_pix2) Chris@16: /// or get_color(gray_pix1) = get_color(gray_pix2) Chris@16: Chris@16: template // = mpl::range_c > Chris@16: struct pixel : public detail::homogeneous_color_base::value> { Chris@16: private: Chris@16: typedef ChannelValue channel_t; Chris@16: typedef detail::homogeneous_color_base::value> parent_t; Chris@16: public: Chris@16: typedef pixel value_type; Chris@16: typedef value_type& reference; Chris@16: typedef const value_type& const_reference; Chris@16: BOOST_STATIC_CONSTANT(bool, is_mutable = channel_traits::is_mutable); Chris@16: Chris@16: pixel(){} Chris@16: explicit pixel(channel_t v) : parent_t(v) {} // sets all channels to v Chris@16: pixel(channel_t v0, channel_t v1) : parent_t(v0,v1) {} Chris@16: pixel(channel_t v0, channel_t v1, channel_t v2) : parent_t(v0,v1,v2) {} Chris@16: pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3) : parent_t(v0,v1,v2,v3) {} Chris@16: pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4) : parent_t(v0,v1,v2,v3,v4) {} Chris@16: pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4, channel_t v5) : parent_t(v0,v1,v2,v3,v4,v5) {} Chris@16: Chris@16: pixel(const pixel& p) : parent_t(p) {} Chris@16: pixel& operator=(const pixel& p) { static_copy(p,*this); return *this; } Chris@16: Chris@16: // Construct from another compatible pixel type Chris@16: template pixel(const Pixel& p, typename enable_if_c::value>::type* dummy = 0) : parent_t(p) { Chris@16: check_compatible(); Chris@16: } Chris@16: Chris@16: template 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: // homogeneous pixels have operator[] Chris@16: typename channel_traits::reference operator[](std::size_t i) { return dynamic_at_c(*this,i); } Chris@16: typename channel_traits::const_reference operator[](std::size_t i) const { return dynamic_at_c(*this,i); } Chris@16: private: 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: template void check_compatible() const { gil_function_requires >(); } Chris@16: Chris@16: // Support for assignment/equality comparison of a channel with a grayscale pixel 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_) { 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: public: Chris@16: pixel& operator= (channel_t chan) { check_gray(); gil::at_c<0>(*this)=chan; return *this; } Chris@16: bool operator==(channel_t chan) 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: typedef ChannelValue type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct kth_element_reference_type, K> { Chris@16: typedef typename channel_traits::reference type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct kth_element_reference_type, K> { Chris@16: typedef typename channel_traits::const_reference type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct kth_element_const_reference_type, K> { Chris@16: typedef typename channel_traits::const_reference type; Chris@16: }; Chris@16: Chris@16: ///////////////////////////// Chris@16: // PixelConcept Chris@16: ///////////////////////////// Chris@16: Chris@16: template Chris@16: struct is_pixel > : public mpl::true_{}; Chris@16: Chris@16: ///////////////////////////// Chris@16: // HomogeneousPixelBasedConcept 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 > : public mpl::false_ {}; Chris@16: Chris@16: template Chris@16: struct channel_type > { Chris@16: typedef ChannelValue type; 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