packed_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 
00013 #ifndef GIL_PACKED_PIXEL_H
00014 #define GIL_PACKED_PIXEL_H
00015 
00024 
00025 #include <functional>
00026 #include <boost/utility/enable_if.hpp>
00027 #include <boost/mpl/bool.hpp>
00028 #include <boost/mpl/front.hpp>
00029 #include "gil_config.hpp"
00030 #include "pixel.hpp"
00031 
00032 namespace boost { namespace gil {
00033 
00037 
00056 
00057 
00058 
00059 template <typename BitField,      // A type that holds the bits of the pixel. Typically an integral type, like boost::uint16_t
00060           typename ChannelRefVec, // An MPL vector whose elements are packed channels. They must be constructible from BitField. GIL uses packed_channel_reference
00061           typename Layout>        // Layout defining the color space and ordering of the channels. Example value: rgb_layout_t
00062 struct packed_pixel {
00063     BitField _bitfield;
00064 
00065     typedef Layout                layout_t;
00066     typedef packed_pixel          value_type;
00067     typedef value_type&           reference;
00068     typedef const value_type&     const_reference;
00069 
00070     BOOST_STATIC_CONSTANT(bool, is_mutable = channel_traits<typename mpl::front<ChannelRefVec>::type>::is_mutable);
00071 
00072     packed_pixel(){}
00073     explicit packed_pixel(const BitField& bitfield) : _bitfield(bitfield) {}
00074 
00075     // Construct from another compatible pixel type
00076     packed_pixel(const packed_pixel& p) : _bitfield(p._bitfield) {}
00077     template <typename P> packed_pixel(const P& p, typename enable_if_c<is_pixel<P>::value>::type* d=0)            { check_compatible<P>(); static_copy(p,*this); }   
00078     packed_pixel(int chan0, int chan1) : _bitfield(0) { 
00079         BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==2)); 
00080         at_c<0>(*this)=chan0; at_c<1>(*this)=chan1; 
00081     } 
00082     packed_pixel(int chan0, int chan1, int chan2) : _bitfield(0) { 
00083         BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==3)); 
00084         at_c<0>(*this)=chan0; at_c<1>(*this)=chan1; at_c<2>(*this)=chan2; 
00085     } 
00086     packed_pixel(int chan0, int chan1, int chan2, int chan3) : _bitfield(0) { 
00087         BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==4)); 
00088         at_c<0>(*this)=chan0; at_c<1>(*this)=chan1; at_c<2>(*this)=chan2; at_c<2>(*this)=chan3; 
00089     } 
00090     packed_pixel(int chan0, int chan1, int chan2, int chan3, int chan4) : _bitfield(0) { 
00091         BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==5)); 
00092         at_c<0>(*this)=chan0; at_c<1>(*this)=chan1; at_c<2>(*this)=chan2; at_c<2>(*this)=chan3;  at_c<3>(*this)=chan4;
00093     } 
00094 
00095     packed_pixel& operator=(const packed_pixel& p)     { _bitfield=p._bitfield; return *this; }
00096 
00097     template <typename P> packed_pixel& operator=(const P& p)        { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; } 
00098     template <typename P> bool          operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); } 
00099 
00100     template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
00101 
00102 private:
00103     template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,packed_pixel> >(); }
00104     template <typename Pixel> void assign(const Pixel& p, mpl::true_)       { check_compatible<Pixel>(); static_copy(p,*this); } 
00105     template <typename Pixel> bool  equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); } 
00106 
00107 // Support for assignment/equality comparison of a channel with a grayscale pixel
00108     static void check_gray() {  BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); }
00109     template <typename Channel> void assign(const Channel& chan, mpl::false_)       { check_gray(); at_c<0>(*this)=chan; }
00110     template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return at_c<0>(*this)==chan; }
00111 public:
00112     packed_pixel&  operator= (int chan)       { check_gray(); at_c<0>(*this)=chan; return *this; }
00113     bool           operator==(int chan) const { check_gray(); return at_c<0>(*this)==chan; }
00114 };
00115 
00117 //  ColorBasedConcept
00119 
00120 template <typename BitField, typename ChannelRefVec, typename Layout, int K>  
00121 struct kth_element_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> : public mpl::at_c<ChannelRefVec,K> {};
00122 
00123 template <typename BitField, typename ChannelRefVec, typename Layout, int K>  
00124 struct kth_element_reference_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> : public mpl::at_c<ChannelRefVec,K> {};
00125 
00126 template <typename BitField, typename ChannelRefVec, typename Layout, int K>  
00127 struct kth_element_const_reference_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> {
00128     typedef typename channel_traits<typename mpl::at_c<ChannelRefVec,K>::type>::const_reference type;
00129 };
00130 
00131 template <int K, typename P, typename C, typename L> inline
00132 typename kth_element_reference_type<packed_pixel<P,C,L>, K>::type 
00133 at_c(packed_pixel<P,C,L>& p) { 
00134     return typename kth_element_reference_type<packed_pixel<P,C,L>, K>::type(&p._bitfield); 
00135 }
00136 
00137 template <int K, typename P, typename C, typename L> inline
00138 typename kth_element_const_reference_type<packed_pixel<P,C,L>, K>::type 
00139 at_c(const packed_pixel<P,C,L>& p) { 
00140     return typename kth_element_const_reference_type<packed_pixel<P,C,L>, K>::type(&p._bitfield);
00141 }
00142 
00144 //  PixelConcept
00146 
00147 // Metafunction predicate that flags packed_pixel as a model of PixelConcept. Required by PixelConcept
00148 template <typename BitField, typename ChannelRefVec, typename Layout>  
00149 struct is_pixel<packed_pixel<BitField,ChannelRefVec,Layout> > : public mpl::true_{};
00150 
00152 //  PixelBasedConcept
00154 
00155 template <typename P, typename C, typename Layout>
00156 struct color_space_type<packed_pixel<P,C,Layout> > {
00157     typedef typename Layout::color_space_t type;
00158 }; 
00159 
00160 template <typename P, typename C, typename Layout>
00161 struct channel_mapping_type<packed_pixel<P,C,Layout> > {
00162     typedef typename Layout::channel_mapping_t type;
00163 }; 
00164 
00165 template <typename P, typename C, typename Layout>
00166 struct is_planar<packed_pixel<P,C,Layout> > : mpl::false_ {}; 
00167 
00168 
00174 
00179 
00180 template <typename P, typename C, typename L>  
00181 struct iterator_is_mutable<packed_pixel<P,C,L>*> : public mpl::bool_<packed_pixel<P,C,L>::is_mutable> {};
00182 template <typename P, typename C, typename L>  
00183 struct iterator_is_mutable<const packed_pixel<P,C,L>*> : public mpl::false_ {};
00184 
00185 
00186 
00187 } }  // namespace boost::gil
00188 
00189 namespace boost {
00190     template <typename P, typename C, typename L>
00191     struct has_trivial_constructor<gil::packed_pixel<P,C,L> > : public has_trivial_constructor<P> {};
00192 }
00193 #endif

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google