Modules | Alphabetical List | Class List | Directories | File List | GIL Home Page

# pixel.hpp

Go to the documentation of this file.

    00001 /*
    00002     Copyright 2005-2007 Adobe Systems Incorporated
    00003
    00004     Use, modification and distribution are subject to the Boost Software License,
    00005     Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    00006     http://www.boost.org/LICENSE_1_0.txt).
    00007
    00008     See http://opensource.adobe.com/gil for most recent version including documentation.
    00009 */
    00010 /*************************************************************************************************/
    00011
    00012 #ifndef GIL_PIXEL_H
    00013 #define GIL_PIXEL_H
    00014
    00023
    00024 #include <functional>
    00025 #include <boost/utility/enable_if.hpp>
    00026 #include <boost/mpl/bool.hpp>
    00027 #include <boost/mpl/front.hpp>
    00028 #include <boost/type_traits.hpp>
    00029 #include "gil_config.hpp"
    00030 #include "color_base.hpp"
    00031 #include "gil_concept.hpp"
    00032 #include "channel.hpp"
    00033 #include "metafunctions.hpp"
    00034 #include "utilities.hpp"
    00035 #include "color_base_algorithm.hpp"
    00036
    00037 namespace boost { namespace gil {
    00038
    00039 // Forward-declare gray_t
    00040 struct gray_color_t;
    00041 typedef mpl::vector1<gray_color_t> gray_t;
    00042 template <typename PixelBased> struct color_space_type;
    00043 template <typename PixelBased> struct channel_mapping_type;
    00044 template <typename PixelBased> struct channel_type;
    00045 template <typename PixelBased> struct is_planar;
    00046
    00047 template <typename PixelBased> struct color_space_type<const PixelBased> : public color_space_type<PixelBased> {};
    00048 template <typename PixelBased> struct channel_mapping_type<const PixelBased> : public channel_mapping_type<PixelBased> {};
    00049 template <typename PixelBased> struct channel_type<const PixelBased> : public channel_type<PixelBased> {};
    00050 template <typename PixelBased> struct is_planar<const PixelBased> : public is_planar<PixelBased> {};
    00051
    00052
    00053 template <typename T> struct is_pixel : public mpl::false_{};
    00054 template <typename T> struct is_pixel<const T> : public is_pixel<T> {};
    00055
    00058 template <typename PixelBased>
    00059 struct num_channels : public mpl::size<typename color_space_type<PixelBased>::type> {};
    00060
    00077
    00078
    00079
    00080
    00084
    00100
    00101 template <typename ChannelValue, typename Layout> // = mpl::range_c<int,0,ColorSpace::size> >
    00102 struct pixel : public detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> {
    00103 private:
    00104     typedef ChannelValue channel_t;
    00105     typedef detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> parent_t;
    00106 public:
    00107     typedef pixel                               value_type;
    00108     typedef value_type&                         reference;
    00109     typedef const value_type&                   const_reference;
    00110     BOOST_STATIC_CONSTANT(bool,                 is_mutable = channel_traits<channel_t>::is_mutable);
    00111
    00112     pixel(){}
    00113     explicit pixel(channel_t v) : parent_t(v) {}  // sets all channels to v
    00114     pixel(channel_t v0, channel_t v1)                                                         : parent_t(v0,v1) {}
    00115     pixel(channel_t v0, channel_t v1, channel_t v2)                                           : parent_t(v0,v1,v2) {}
    00116     pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3)                             : parent_t(v0,v1,v2,v3) {}
    00117     pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4)               : parent_t(v0,v1,v2,v3,v4) {}
    00118     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) {}
    00119
    00120     pixel(const pixel& p) : parent_t(p) {}
    00121     pixel&                       operator=(const pixel& p)       { static_copy(p,*this); return *this; }
    00122
    00123     // Construct from another compatible pixel type
    00124     template <typename Pixel>    pixel(const Pixel& p, typename enable_if_c<is_pixel<Pixel>::value>::type* dummy = 0) : parent_t(p) {
    00125         check_compatible<Pixel>();
    00126     }
    00127
    00128     template <typename P> pixel& operator=(const P& p)           { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
    00129     template <typename P> bool   operator==(const P& p)    const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
    00130
    00131     template <typename P> bool   operator!=(const P& p)    const { return !(*this==p); }
    00132
    00133     // homogeneous pixels have operator[]
    00134     typename channel_traits<channel_t>::reference       operator[](std::size_t i)       { return dynamic_at_c(*this,i); }
    00135     typename channel_traits<channel_t>::const_reference operator[](std::size_t i) const { return dynamic_at_c(*this,i); }
    00136 private:
    00137     template <typename Pixel> void assign(const Pixel& p, mpl::true_)       { check_compatible<Pixel>(); static_copy(p,*this); }
    00138     template <typename Pixel> bool  equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
    00139
    00140     template <typename Pixel> void check_compatible() const { gil_function_requires<PixelsCompatibleConcept<Pixel,pixel> >(); }
    00141
    00142 // Support for assignment/equality comparison of a channel with a grayscale pixel
    00143
    00144 private:
    00145     static void check_gray() {  BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); }
    00146     template <typename Channel> void assign(const Channel& chan, mpl::false_)       { check_gray(); at_c<0>(*this)=chan; }
    00147     template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return at_c<0>(*this)==chan; }
    00148 public:
    00149     pixel&  operator= (channel_t chan)       { check_gray(); at_c<0>(*this)=chan; return *this; }
    00150     bool    operator==(channel_t chan) const { check_gray(); return at_c<0>(*this)==chan; }
    00151 };
    00152
    00154 //  ColorBasedConcept
    00156
    00157 template <typename ChannelValue, typename Layout, int K>
    00158 struct kth_element_type<pixel<ChannelValue,Layout>, K> {
    00159     typedef ChannelValue type;
    00160 };
    00161
    00162 template <typename ChannelValue, typename Layout, int K>
    00163 struct kth_element_reference_type<pixel<ChannelValue,Layout>, K> {
    00164     typedef typename channel_traits<ChannelValue>::reference type;
    00165 };
    00166
    00167 template <typename ChannelValue, typename Layout, int K>
    00168 struct kth_element_reference_type<const pixel<ChannelValue,Layout>, K> {
    00169     typedef typename channel_traits<ChannelValue>::const_reference type;
    00170 };
    00171
    00172 template <typename ChannelValue, typename Layout, int K>
    00173 struct kth_element_const_reference_type<pixel<ChannelValue,Layout>, K> {
    00174     typedef typename channel_traits<ChannelValue>::const_reference type;
    00175 };
    00176
    00178 //  PixelConcept
    00180
    00181 template <typename ChannelValue, typename Layout>
    00182 struct is_pixel<pixel<ChannelValue,Layout> > : public mpl::true_{};
    00183
    00185 //  HomogeneousPixelBasedConcept
    00187
    00188 template <typename ChannelValue, typename Layout>
    00189 struct color_space_type<pixel<ChannelValue,Layout> > {
    00190     typedef typename Layout::color_space_t type;
    00191 };
    00192
    00193 template <typename ChannelValue, typename Layout>
    00194 struct channel_mapping_type<pixel<ChannelValue,Layout> > {
    00195     typedef typename Layout::channel_mapping_t type;
    00196 };
    00197
    00198 template <typename ChannelValue, typename Layout>
    00199 struct is_planar<pixel<ChannelValue,Layout> > : public mpl::false_ {};
    00200
    00201 template <typename ChannelValue, typename Layout>
    00202 struct channel_type<pixel<ChannelValue,Layout> > {
    00203     typedef ChannelValue type;
    00204 };
    00205
    00206 } }  // namespace boost::gil
    00207
    00208 namespace boost {
    00209     template <typename ChannelValue, typename Layout>
    00210     struct has_trivial_constructor<gil::pixel<ChannelValue,Layout> > : public has_trivial_constructor<ChannelValue> {};
    00211 }
    00212 #endif