Generic Image Library: premultiply.hpp Source File

###  | #  |

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

premultiply.hpp
1 /*
2 Copyright 2014
3 Use, modification and distribution are subject to the Boost Software License,
4 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 http://www.boost.org/LICENSE_1_0.txt).
6 */
7
8 /*************************************************************************************************/
9
10 #ifndef BOOST_GIL_PREMULTIPLY_HPP
11 #define BOOST_GIL_PREMULTIPLY_HPP
12
13 #include <iostream>
14
15 #include <boost/gil/rgba.hpp>
16 #include <boost/mpl/remove.hpp>
17
18 namespace boost { namespace gil {
19
20 template <typename SrcP, typename DstP>
21 struct channel_premultiply {
22 channel_premultiply (SrcP const & src, DstP & dst)
23 : src_ (src), dst_ (dst)
24 {}
25
26 template <typename Channel>
27 void operator () (Channel c) const {
28 // @todo: need to do a “channel_convert” too, in case the channel types aren't the same?
29 get_color (dst_, Channel()) = channel_multiply(get_color(src_,Channel()), alpha_or_max(src_));
30 }
31 SrcP const & src_;
32 DstP & dst_;
33 };
34
35
36 namespace detail
37 {
38 template <typename SrcP, typename DstP>
39 void assign_alpha_if(mpl::true_, SrcP const &src, DstP &dst)
40 {
41 get_color (dst,alpha_t()) = alpha_or_max (src);
42 };
43
44 template <typename SrcP, typename DstP>
45 void assign_alpha_if(mpl::false_, SrcP const &src, DstP &dst)
46 {
47 // nothing to do
48 };
49 }
50
51 struct premultiply {
52 template <typename SrcP, typename DstP>
53 void operator()(const SrcP& src, DstP& dst) const {
54 typedef typename color_space_type<SrcP>::type src_colour_space_t;
55 typedef typename color_space_type<DstP>::type dst_colour_space_t;
56 typedef typename mpl:: remove <src_colour_space_t, alpha_t>:: type src_colour_channels;
57
58 typedef mpl::bool_<mpl::contains<dst_colour_space_t, alpha_t>::value> has_alpha_t;
59 mpl:: for_each <src_colour_channels> ( channel_premultiply <SrcP, DstP> (src, dst) );
60 detail::assign_alpha_if(has_alpha_t(), src, dst);
61 }
62 };
63
64 template <typename SrcConstRefP, // const reference to the source pixel
65 typename DstP> // Destination pixel value (models PixelValueConcept)
66 class premultiply_deref_fn {
67 public:
68 typedef premultiply_deref_fn const_t;
69 typedef DstP value_type;
70 typedef value_type reference; // read-only dereferencing
71 typedef const value_type& const_reference;
72 typedef SrcConstRefP argument_type;
73 typedef reference result_type;
74 BOOST_STATIC_CONSTANT(bool, is_mutable=false);
75
76 result_type operator()(argument_type srcP) const {
77 result_type dstP;
78 premultiply () (srcP,dstP);
79 return dstP;
80 }
81 };
82
83 template <typename SrcView, typename DstP>
84 struct premultiplied_view_type {
85 private:
86 typedef typename SrcView::const_t::reference src_pix_ref; // const reference to pixel in SrcView
87 typedef premultiply_deref_fn<src_pix_ref, DstP> deref_t; // the dereference adaptor that performs color conversion
88 typedef typename SrcView::template add_deref<deref_t> add_ref_t;
89 public:
90 typedef typename add_ref_t::type type; // the color converted view type
91 static type make(const SrcView& sv) { return add_ref_t::make(sv, deref_t()); }
92 };
93
94 template <typename DstP, typename View> inline
95 typename premultiplied_view_type<View,DstP>::type premultiply_view(const View& src) {
96 return premultiplied_view_type<View,DstP>::make(src);
97 }
98
99 }
100 }
101
102 #endif // BOOST_GIL_PREMULTIPLY_HPP
boost::gil::channel_multiply
channel_traits< Channel >::value_type channel_multiply(Channel a, Channel b)
A function multiplying two channels. result = a * b / max_value.
Definition: channel_algorithm.hpp:510
boost::gil::get_color
color_element_reference_type< ColorBase, Color >::type get_color(ColorBase &cb, Color=Color())
Mutable accessor to the element associated with a given color name.
Definition: color_base_algorithm.hpp:185
rgba.hpp
Support for RGBA color space and variants.