/* Copyright 2014 Use, modification and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). */ /*************************************************************************************************/ #ifndef BOOST_GIL_PREMULTIPLY_HPP #define BOOST_GIL_PREMULTIPLY_HPP #include #include #include namespace boost { namespace gil { template struct channel_premultiply { channel_premultiply (SrcP const & src, DstP & dst) : src_ (src), dst_ (dst) {} template void operator () (Channel c) const { // @todo: need to do a “channel_convert” too, in case the channel types aren't the same? get_color (dst_, Channel()) = channel_multiply(get_color(src_,Channel()), alpha_or_max(src_)); } SrcP const & src_; DstP & dst_; }; namespace detail { template void assign_alpha_if(mpl::true_, SrcP const &src, DstP &dst) { get_color (dst,alpha_t()) = alpha_or_max (src); }; template void assign_alpha_if(mpl::false_, SrcP const &src, DstP &dst) { // nothing to do }; } struct premultiply { template void operator()(const SrcP& src, DstP& dst) const { typedef typename color_space_type::type src_colour_space_t; typedef typename color_space_type::type dst_colour_space_t; typedef typename mpl:: remove :: type src_colour_channels; typedef mpl::bool_::value> has_alpha_t; mpl:: for_each ( channel_premultiply (src, dst) ); detail::assign_alpha_if(has_alpha_t(), src, dst); } }; template // Destination pixel value (models PixelValueConcept) class premultiply_deref_fn { public: typedef premultiply_deref_fn const_t; typedef DstP value_type; typedef value_type reference; // read-only dereferencing typedef const value_type& const_reference; typedef SrcConstRefP argument_type; typedef reference result_type; BOOST_STATIC_CONSTANT(bool, is_mutable=false); result_type operator()(argument_type srcP) const { result_type dstP; premultiply () (srcP,dstP); return dstP; } }; template struct premultiplied_view_type { private: typedef typename SrcView::const_t::reference src_pix_ref; // const reference to pixel in SrcView typedef premultiply_deref_fn deref_t; // the dereference adaptor that performs color conversion typedef typename SrcView::template add_deref add_ref_t; public: typedef typename add_ref_t::type type; // the color converted view type static type make(const SrcView& sv) { return add_ref_t::make(sv, deref_t()); } }; template inline typename premultiplied_view_type::type premultiply_view(const View& src) { return premultiplied_view_type::make(src); } } } #endif // BOOST_GIL_PREMULTIPLY_HPP