2012-03-19 23:42:44 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2012-03-19 23:42:44 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// mapnik
|
|
|
|
#include <mapnik/image_compositing.hpp>
|
2015-01-22 18:39:37 +01:00
|
|
|
#include <mapnik/image.hpp>
|
2015-01-22 02:40:12 +01:00
|
|
|
#include <mapnik/image_any.hpp>
|
2015-06-16 04:32:22 +02:00
|
|
|
#include <mapnik/safe_cast.hpp>
|
|
|
|
#include <mapnik/util/const_rendering_buffer.hpp>
|
2012-03-19 23:42:44 +01:00
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 02:53:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2012-03-21 16:45:23 +01:00
|
|
|
#include <boost/assign/list_of.hpp>
|
|
|
|
#include <boost/bimap.hpp>
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2012-03-21 16:45:23 +01:00
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2016-03-10 01:42:15 +01:00
|
|
|
#include <mapnik/warning_ignore_agg.hpp>
|
2012-03-19 23:42:44 +01:00
|
|
|
#include "agg_rendering_buffer.h"
|
|
|
|
#include "agg_rasterizer_scanline_aa.h"
|
|
|
|
#include "agg_scanline_u.h"
|
|
|
|
#include "agg_renderer_scanline.h"
|
|
|
|
#include "agg_pixfmt_rgba.h"
|
2014-12-02 15:56:40 +01:00
|
|
|
#include "agg_pixfmt_gray.h"
|
2013-07-24 00:45:25 +02:00
|
|
|
#include "agg_color_rgba.h"
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2014-12-02 15:56:40 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
namespace mapnik {
|
2012-03-19 23:42:44 +01:00
|
|
|
|
2014-07-07 19:23:15 +02:00
|
|
|
using comp_op_lookup_type = boost::bimap<composite_mode_e, std::string>;
|
2022-01-26 10:43:31 +01:00
|
|
|
static const comp_op_lookup_type comp_lookup =
|
|
|
|
boost::assign::list_of<comp_op_lookup_type::relation>(clear, "clear")(src, "src")(dst, "dst")(src_over, "src-over")(
|
|
|
|
dst_over,
|
|
|
|
"dst-over")(src_in, "src-in")(dst_in, "dst-in")(src_out, "src-out")(dst_out, "dst-out")(src_atop, "src-atop")(
|
|
|
|
dst_atop,
|
|
|
|
"dst-atop")(_xor, "xor")(plus, "plus")(minus, "minus")(multiply, "multiply")(screen, "screen")(overlay, "overlay")(
|
|
|
|
darken,
|
|
|
|
"darken")(lighten, "lighten")(color_dodge, "color-dodge")(color_burn, "color-burn")(hard_light, "hard-light")(
|
|
|
|
soft_light,
|
|
|
|
"soft-light")(difference, "difference")(exclusion, "exclusion")(contrast, "contrast")(invert, "invert")(
|
|
|
|
invert_rgb,
|
|
|
|
"invert-rgb")(grain_merge, "grain-merge")(grain_extract, "grain-extract")(hue, "hue")(saturation, "saturation")(
|
|
|
|
_color,
|
|
|
|
"color")(_value, "value")(linear_dodge, "linear-dodge")(linear_burn, "linear-burn")(divide, "divide");
|
2012-03-21 16:45:23 +01:00
|
|
|
|
2024-04-19 20:45:24 +02:00
|
|
|
std::optional<composite_mode_e> comp_op_from_string(std::string const& name)
|
2012-03-21 16:45:23 +01:00
|
|
|
{
|
2024-04-19 20:45:24 +02:00
|
|
|
std::optional<composite_mode_e> mode;
|
2012-03-21 16:45:23 +01:00
|
|
|
comp_op_lookup_type::right_const_iterator right_iter = comp_lookup.right.find(name);
|
|
|
|
if (right_iter != comp_lookup.right.end())
|
|
|
|
{
|
2024-04-19 20:45:24 +02:00
|
|
|
mode = right_iter->second;
|
2012-03-21 16:45:23 +01:00
|
|
|
}
|
2012-04-18 16:37:14 +02:00
|
|
|
return mode;
|
2012-03-21 16:45:23 +01:00
|
|
|
}
|
|
|
|
|
2024-04-19 20:45:24 +02:00
|
|
|
std::optional<std::string> comp_op_to_string(composite_mode_e comp_op)
|
2012-07-06 02:06:41 +02:00
|
|
|
{
|
2024-04-19 20:45:24 +02:00
|
|
|
std::optional<std::string> mode;
|
2012-07-06 02:06:41 +02:00
|
|
|
comp_op_lookup_type::left_const_iterator left_iter = comp_lookup.left.find(comp_op);
|
|
|
|
if (left_iter != comp_lookup.left.end())
|
|
|
|
{
|
2024-04-19 20:45:24 +02:00
|
|
|
mode = left_iter->second;
|
2012-07-06 02:06:41 +02:00
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2012-10-01 23:13:54 +02:00
|
|
|
/*
|
|
|
|
Note: the difference between agg::pixfmt_rgba32 and agg:pixfmt_rgba32_pre is subtle.
|
|
|
|
|
|
|
|
From http://www.antigrain.com/news/release_notes/v22.agdoc.html:
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
Format agg::pixfmt_rgba32 is the main and the fastest pixel format and it's supposed to be used in most cases. But it
|
|
|
|
always uses plain colors as input and produces pre-multiplied result on the canvas. It has even less number of
|
|
|
|
calculations than agg::pixfmt_rgba32_pre. Format agg::pixfmt_rgba32_plain is slow because of division operations. APIs
|
|
|
|
allowing for alpha-blending require premultiplied colors. Besides, if you display RGBA with RGB API (that is, without
|
|
|
|
alpha, like WinAPI BitBlt), the colors still must be premultiplied. Note that the formulas in agg::pixfmt_rgba32 and
|
|
|
|
agg::pixfmt_rgb24 are exactly the same! So, premultiplied colors are more natural and agg::pixfmt_rgba32_plain is rather
|
|
|
|
useless.
|
|
|
|
|
|
|
|
Format agg::pixfmt_rgba32_pre is a bit slower than agg::pixfmt_rgba32 because of additional "cover" values, i.e.
|
|
|
|
secondary alphas, that are to be mixed with the source premultiplied color. That spoils the beauty of the premultiplied
|
|
|
|
colors idea. But the "cover" values are important because there can be other color spaces and color types that don't
|
|
|
|
have any "alpha" at all, or the alpha is incompatible with integral types. So, the "cover" is a secondary, uniform alpha
|
|
|
|
in range of 0…255, used specifically for anti-aliasing purposes. One needs to consider this issue when transforming
|
|
|
|
images. Actually, all RGBA images are supposed to be in the premultiplied color space and the result of filtering is
|
|
|
|
also premultiplied. Since the resulting colors of the filtered images are the source for the renderers, one should use
|
|
|
|
the premultiplied renderers, that is, agg::pixfmt_rgba32_pre, or the new one, agg::pixfmt_rgb24_pre. But it's important
|
|
|
|
only if images are translucent, that is, have actual alpha channel.
|
|
|
|
|
|
|
|
For example, if you generate some pattern with AGG (premultiplied) and would like to use it for filling, you'll need to
|
|
|
|
use agg::pixfmt_rgba32_pre. If you use agg::span_image_filter_rgb24_gamma_bilinear (that is, RGB for input) and draw it
|
|
|
|
on the RGBA canvas, you still need to use agg::pixfmt_rgba32_pre as the destination canvas. The only thing you need is
|
|
|
|
to premultiply the background color used out of bounds.
|
2012-10-01 23:13:54 +02:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<>
|
|
|
|
MAPNIK_DECL void
|
|
|
|
composite(image_rgba8& dst, image_rgba8 const& src, composite_mode_e mode, float opacity, int dx, int dy)
|
2012-03-19 23:42:44 +01:00
|
|
|
{
|
2014-07-07 19:23:15 +02:00
|
|
|
using color = agg::rgba8;
|
|
|
|
using order = agg::order_rgba;
|
2015-06-16 04:32:22 +02:00
|
|
|
using const_rendering_buffer = util::rendering_buffer<image_rgba8>;
|
2014-07-07 19:23:15 +02:00
|
|
|
using blender_type = agg::comp_op_adaptor_rgba_pre<color, order>;
|
|
|
|
using pixfmt_type = agg::pixfmt_custom_blend_rgba<blender_type, agg::rendering_buffer>;
|
|
|
|
using renderer_type = agg::renderer_base<pixfmt_type>;
|
2012-05-07 13:15:04 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
agg::rendering_buffer dst_buffer(dst.bytes(),
|
|
|
|
safe_cast<unsigned>(dst.width()),
|
|
|
|
safe_cast<unsigned>(dst.height()),
|
|
|
|
safe_cast<int>(dst.row_size()));
|
2015-01-08 12:42:38 +01:00
|
|
|
const_rendering_buffer src_buffer(src);
|
2012-06-19 00:50:32 +02:00
|
|
|
pixfmt_type pixf(dst_buffer);
|
2012-03-21 16:45:23 +01:00
|
|
|
pixf.comp_op(static_cast<agg::comp_op_e>(mode));
|
2022-01-26 10:43:31 +01:00
|
|
|
agg::pixfmt_alpha_blend_rgba<agg::blender_rgba32_pre, const_rendering_buffer, agg::pixel32_type> pixf_mask(
|
|
|
|
src_buffer);
|
2015-01-17 01:26:56 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2015-05-04 10:57:57 +02:00
|
|
|
if (!src.get_premultiplied())
|
2015-01-15 01:29:00 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("SOURCE MUST BE PREMULTIPLIED FOR COMPOSITING!");
|
|
|
|
}
|
|
|
|
if (!dst.get_premultiplied())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("DESTINATION MUST BE PREMULTIPLIED FOR COMPOSITING!");
|
2015-05-04 10:57:57 +02:00
|
|
|
}
|
2015-01-17 01:26:56 +01:00
|
|
|
#endif
|
2012-03-19 23:42:44 +01:00
|
|
|
renderer_type ren(pixf);
|
2022-01-26 10:43:31 +01:00
|
|
|
ren.blend_from(pixf_mask, 0, dx, dy, safe_cast<agg::cover_type>(255 * opacity));
|
2012-05-11 11:55:58 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<>
|
|
|
|
MAPNIK_DECL void
|
|
|
|
composite(image_gray32f& dst, image_gray32f const& src, composite_mode_e /*mode*/, float /*opacity*/, int dx, int dy)
|
2014-12-02 15:56:40 +01:00
|
|
|
{
|
2015-06-16 04:32:22 +02:00
|
|
|
using const_rendering_buffer = util::rendering_buffer<image_gray32f>;
|
2015-01-08 12:42:38 +01:00
|
|
|
using src_pixfmt_type = agg::pixfmt_alpha_blend_gray<agg::blender_gray<agg::gray32>, const_rendering_buffer, 1, 0>;
|
|
|
|
using dst_pixfmt_type = agg::pixfmt_alpha_blend_gray<agg::blender_gray<agg::gray32>, agg::rendering_buffer, 1, 0>;
|
|
|
|
using renderer_type = agg::renderer_base<dst_pixfmt_type>;
|
2014-12-02 15:56:40 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
agg::rendering_buffer dst_buffer(dst.bytes(),
|
|
|
|
safe_cast<unsigned>(dst.width()),
|
|
|
|
safe_cast<unsigned>(dst.height()),
|
|
|
|
safe_cast<int>(dst.width()));
|
2015-01-08 12:42:38 +01:00
|
|
|
const_rendering_buffer src_buffer(src);
|
|
|
|
dst_pixfmt_type pixf(dst_buffer);
|
|
|
|
src_pixfmt_type pixf_mask(src_buffer);
|
2014-12-02 15:56:40 +01:00
|
|
|
renderer_type ren(pixf);
|
2022-01-26 10:43:31 +01:00
|
|
|
ren.copy_from(pixf_mask, 0, dx, dy);
|
2014-12-02 15:56:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-14 19:42:30 +01:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
struct composite_visitor
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
composite_visitor(image_any const& src, composite_mode_e mode, float opacity, int dx, int dy)
|
|
|
|
: src_(src)
|
|
|
|
, mode_(mode)
|
|
|
|
, opacity_(opacity)
|
|
|
|
, dx_(dx)
|
|
|
|
, dy_(dy)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void operator()(T& dst) const
|
2016-07-25 14:49:52 +02:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Error: Composite with " + std::string(typeid(dst).name()) + " is not supported");
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
void operator()(image_rgba8& dst) const { composite(dst, util::get<image_rgba8>(src_), mode_, opacity_, dx_, dy_); }
|
2016-07-25 14:49:52 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
void operator()(image_gray32f& dst) const
|
2016-07-25 14:49:52 +02:00
|
|
|
{
|
|
|
|
composite(dst, util::get<image_gray32f>(src_), mode_, opacity_, dx_, dy_);
|
|
|
|
}
|
2015-01-14 19:42:30 +01:00
|
|
|
|
|
|
|
private:
|
2015-01-22 02:40:12 +01:00
|
|
|
image_any const& src_;
|
2015-01-14 19:42:30 +01:00
|
|
|
composite_mode_e mode_;
|
|
|
|
float opacity_;
|
|
|
|
int dx_;
|
|
|
|
int dy_;
|
2016-07-25 14:49:52 +02:00
|
|
|
};
|
2015-01-14 19:42:30 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
} // namespace detail
|
2015-01-14 19:42:30 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<>
|
|
|
|
MAPNIK_DECL void composite(image_any& dst, image_any const& src, composite_mode_e mode, float opacity, int dx, int dy)
|
2015-01-14 19:42:30 +01:00
|
|
|
{
|
|
|
|
util::apply_visitor(detail::composite_visitor(src, mode, opacity, dx, dy), dst);
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
} // namespace mapnik
|