1 | /* |
2 | Copyright 2005-2007 Adobe Systems Incorporated |
3 | |
4 | Use, modification and distribution are subject to the Boost Software License, |
5 | Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
6 | http://www.boost.org/LICENSE_1_0.txt). |
7 | |
8 | See http://opensource.adobe.com/gil for most recent version including documentation. |
9 | */ |
10 | /*************************************************************************************************/ |
11 | |
12 | #ifndef GIL_PIXEL_H |
13 | #define GIL_PIXEL_H |
14 | |
15 | //////////////////////////////////////////////////////////////////////////////////////// |
16 | /// \file |
17 | /// \brief pixel class and related utilities |
18 | /// \author Lubomir Bourdev and Hailin Jin \n |
19 | /// Adobe Systems Incorporated |
20 | /// \date 2005-2007 \n Last updated on September 28, 2006 |
21 | /// |
22 | //////////////////////////////////////////////////////////////////////////////////////// |
23 | |
24 | #include <functional> |
25 | #include <boost/utility/enable_if.hpp> |
26 | #include <boost/mpl/bool.hpp> |
27 | #include <boost/mpl/front.hpp> |
28 | #include <boost/type_traits.hpp> |
29 | #include "gil_config.hpp" |
30 | #include "color_base.hpp" |
31 | #include "gil_concept.hpp" |
32 | #include "channel.hpp" |
33 | #include "metafunctions.hpp" |
34 | #include "utilities.hpp" |
35 | #include "color_base_algorithm.hpp" |
36 | |
37 | namespace boost { namespace gil { |
38 | |
39 | // Forward-declare gray_t |
40 | struct gray_color_t; |
41 | typedef mpl::vector1<gray_color_t> gray_t; |
42 | template <typename PixelBased> struct color_space_type; |
43 | template <typename PixelBased> struct channel_mapping_type; |
44 | template <typename PixelBased> struct channel_type; |
45 | template <typename PixelBased> struct is_planar; |
46 | |
47 | template <typename PixelBased> struct color_space_type<const PixelBased> : public color_space_type<PixelBased> {}; |
48 | template <typename PixelBased> struct channel_mapping_type<const PixelBased> : public channel_mapping_type<PixelBased> {}; |
49 | template <typename PixelBased> struct channel_type<const PixelBased> : public channel_type<PixelBased> {}; |
50 | |
51 | template <typename PixelBased> struct is_planar : mpl::false_ {}; |
52 | template <typename PixelBased> struct is_planar<const PixelBased> : public is_planar<PixelBased> {}; |
53 | |
54 | |
55 | template <typename T> struct is_pixel : public mpl::false_{}; |
56 | template <typename T> struct is_pixel<const T> : public is_pixel<T> {}; |
57 | |
58 | /// \ingroup PixelBasedAlgorithm |
59 | /// \brief Returns the number of channels of a pixel-based GIL construct |
60 | template <typename PixelBased> |
61 | struct num_channels : public mpl::size<typename color_space_type<PixelBased>::type> {}; |
62 | |
63 | /** |
64 | \addtogroup PixelBasedAlgorithm |
65 | |
66 | Example: |
67 | \code |
68 | BOOST_STATIC_ASSERT((num_channels<rgb8_view_t>::value==3)); |
69 | BOOST_STATIC_ASSERT((num_channels<cmyk16_planar_ptr_t>::value==4)); |
70 | |
71 | BOOST_STATIC_ASSERT((is_planar<rgb16_planar_image_t>::value)); |
72 | BOOST_STATIC_ASSERT((is_same<color_space_type<rgb8_planar_ref_t>::type, rgb_t>::value)); |
73 | BOOST_STATIC_ASSERT((is_same<channel_mapping_type<cmyk8_pixel_t>::type, |
74 | channel_mapping_type<rgba8_pixel_t>::type>::value)); |
75 | BOOST_STATIC_ASSERT((is_same<channel_type<bgr8_pixel_t>::type, bits8>::value)); |
76 | \endcode |
77 | */ |
78 | |
79 | /// \defgroup ColorBaseModelPixel pixel |
80 | /// \ingroup ColorBaseModel |
81 | /// \brief A homogeneous color base whose element is a channel value. Models HomogeneousColorBaseValueConcept |
82 | |
83 | /// \defgroup PixelModelPixel pixel |
84 | /// \ingroup PixelModel |
85 | /// \brief A homogeneous pixel value. Models HomogeneousPixelValueConcept |
86 | |
87 | /// \ingroup PixelModelPixel ColorBaseModelPixel PixelBasedModel |
88 | /// \brief Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept, PixelValueConcept, HomogeneousPixelBasedConcept |
89 | /// |
90 | /// 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 |
91 | /// model \p ChannelConcept. The class \p pixel defines a simple, homogeneous pixel value. It is used to store |
92 | /// 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 |
93 | /// 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* |
94 | /// are used as the standard iterator over a row of interleaved homogeneous pixels. |
95 | /// |
96 | /// Since \p pixel inherits the properties of color base, assigning, equality comparison and copy-construcion are allowed between compatible pixels. |
97 | /// 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. |
98 | /// |
99 | /// The single-channel (grayscale) instantiation of the class pixel, (i.e. \p pixel<T,gray_layout_t>) is also convertible to/from a channel value. |
100 | /// 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) |
101 | /// or get_color<gray_color_t>(gray_pix1) = get_color<gray_color_t>(gray_pix2) |
102 | |
103 | template <typename ChannelValue, typename Layout> // = mpl::range_c<int,0,ColorSpace::size> > |
104 | struct pixel : public detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> { |
105 | private: |
106 | typedef ChannelValue channel_t; |
107 | typedef detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> parent_t; |
108 | public: |
109 | typedef pixel value_type; |
110 | typedef value_type& reference; |
111 | typedef const value_type& const_reference; |
112 | BOOST_STATIC_CONSTANT(bool, is_mutable = channel_traits<channel_t>::is_mutable); |
113 | |
114 | pixel(){} |
115 | explicit pixel(channel_t v) : parent_t(v) {} // sets all channels to v |
116 | pixel(channel_t v0, channel_t v1) : parent_t(v0,v1) {} |
117 | pixel(channel_t v0, channel_t v1, channel_t v2) : parent_t(v0,v1,v2) {} |
118 | pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3) : parent_t(v0,v1,v2,v3) {} |
119 | pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4) : parent_t(v0,v1,v2,v3,v4) {} |
120 | 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) {} |
121 | |
122 | pixel(const pixel& p) : parent_t(p) {} |
123 | pixel& operator=(const pixel& p) { static_copy(p,*this); return *this; } |
124 | |
125 | // Construct from another compatible pixel type |
126 | template <typename Pixel> pixel(const Pixel& p, typename enable_if_c<is_pixel<Pixel>::value>::type* dummy = 0) : parent_t(p) { |
127 | check_compatible<Pixel>(); |
128 | } |
129 | |
130 | template <typename P> pixel& operator=(const P& p) { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; } |
131 | template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); } |
132 | |
133 | template <typename P> bool operator!=(const P& p) const { return !(*this==p); } |
134 | |
135 | // homogeneous pixels have operator[] |
136 | typename channel_traits<channel_t>::reference operator[](std::size_t i) { return dynamic_at_c(*this,i); } |
137 | typename channel_traits<channel_t>::const_reference operator[](std::size_t i) const { return dynamic_at_c(*this,i); } |
138 | private: |
139 | template <typename Pixel> void assign(const Pixel& p, mpl::true_) { check_compatible<Pixel>(); static_copy(p,*this); } |
140 | template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); } |
141 | |
142 | template <typename Pixel> void check_compatible() const { gil_function_requires<PixelsCompatibleConcept<Pixel,pixel> >(); } |
143 | |
144 | // Support for assignment/equality comparison of a channel with a grayscale pixel |
145 | |
146 | private: |
147 | static void check_gray() { BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); } |
148 | template <typename Channel> void assign(const Channel& chan, mpl::false_) { check_gray(); gil::at_c<0>(*this)=chan; } |
149 | template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; } |
150 | public: |
151 | pixel& operator= (channel_t chan) { check_gray(); gil::at_c<0>(*this)=chan; return *this; } |
152 | bool operator==(channel_t chan) const { check_gray(); return gil::at_c<0>(*this)==chan; } |
153 | }; |
154 | |
155 | ///////////////////////////// |
156 | // ColorBasedConcept |
157 | ///////////////////////////// |
158 | |
159 | template <typename ChannelValue, typename Layout, int K> |
160 | struct kth_element_type<pixel<ChannelValue,Layout>, K> { |
161 | typedef ChannelValue type; |
162 | }; |
163 | |
164 | template <typename ChannelValue, typename Layout, int K> |
165 | struct kth_element_reference_type<pixel<ChannelValue,Layout>, K> { |
166 | typedef typename channel_traits<ChannelValue>::reference type; |
167 | }; |
168 | |
169 | template <typename ChannelValue, typename Layout, int K> |
170 | struct kth_element_reference_type<const pixel<ChannelValue,Layout>, K> { |
171 | typedef typename channel_traits<ChannelValue>::const_reference type; |
172 | }; |
173 | |
174 | template <typename ChannelValue, typename Layout, int K> |
175 | struct kth_element_const_reference_type<pixel<ChannelValue,Layout>, K> { |
176 | typedef typename channel_traits<ChannelValue>::const_reference type; |
177 | }; |
178 | |
179 | ///////////////////////////// |
180 | // PixelConcept |
181 | ///////////////////////////// |
182 | |
183 | template <typename ChannelValue, typename Layout> |
184 | struct is_pixel<pixel<ChannelValue,Layout> > : public mpl::true_{}; |
185 | |
186 | ///////////////////////////// |
187 | // HomogeneousPixelBasedConcept |
188 | ///////////////////////////// |
189 | |
190 | template <typename ChannelValue, typename Layout> |
191 | struct color_space_type<pixel<ChannelValue,Layout> > { |
192 | typedef typename Layout::color_space_t type; |
193 | }; |
194 | |
195 | template <typename ChannelValue, typename Layout> |
196 | struct channel_mapping_type<pixel<ChannelValue,Layout> > { |
197 | typedef typename Layout::channel_mapping_t type; |
198 | }; |
199 | |
200 | template <typename ChannelValue, typename Layout> |
201 | struct is_planar<pixel<ChannelValue,Layout> > : public mpl::false_ {}; |
202 | |
203 | template <typename ChannelValue, typename Layout> |
204 | struct channel_type<pixel<ChannelValue,Layout> > { |
205 | typedef ChannelValue type; |
206 | }; |
207 | |
208 | } } // namespace boost::gil |
209 | |
210 | namespace boost { |
211 | template <typename ChannelValue, typename Layout> |
212 | struct has_trivial_constructor<gil::pixel<ChannelValue,Layout> > : public has_trivial_constructor<ChannelValue> {}; |
213 | } |
214 | #endif |
215 | |