2011-09-16 16:34:14 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2011-09-16 16:34:14 +02: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/warp.hpp>
|
|
|
|
#include <mapnik/config.hpp>
|
2015-01-22 18:39:37 +01:00
|
|
|
#include <mapnik/image.hpp>
|
2014-12-16 12:04:18 +01:00
|
|
|
#include <mapnik/image_scaling_traits.hpp>
|
2011-09-16 16:34:14 +02:00
|
|
|
#include <mapnik/image_util.hpp>
|
2017-01-26 09:51:37 +01:00
|
|
|
#include <mapnik/geometry/box2d.hpp>
|
2014-08-28 11:29:04 +02:00
|
|
|
#include <mapnik/view_transform.hpp>
|
2013-01-28 05:53:37 +01:00
|
|
|
#include <mapnik/raster.hpp>
|
|
|
|
#include <mapnik/proj_transform.hpp>
|
2018-02-14 16:46:24 +01:00
|
|
|
#include <mapnik/safe_cast.hpp>
|
2011-09-16 16:34:14 +02: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>
|
2011-09-16 16:34:14 +02:00
|
|
|
#include "agg_image_filters.h"
|
|
|
|
#include "agg_trans_bilinear.h"
|
|
|
|
#include "agg_span_interpolator_linear.h"
|
|
|
|
#include "agg_span_image_filter_rgba.h"
|
|
|
|
#include "agg_rendering_buffer.h"
|
|
|
|
#include "agg_pixfmt_rgba.h"
|
|
|
|
#include "agg_rasterizer_scanline_aa.h"
|
|
|
|
#include "agg_basics.h"
|
2014-09-11 17:49:00 +02:00
|
|
|
#include "agg_scanline_bin.h"
|
2011-09-16 16:34:14 +02:00
|
|
|
#include "agg_renderer_scanline.h"
|
|
|
|
#include "agg_span_allocator.h"
|
|
|
|
#include "agg_image_accessors.h"
|
|
|
|
#include "agg_renderer_scanline.h"
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2011-09-16 16:34:14 +02:00
|
|
|
|
2011-09-16 16:57:40 +02:00
|
|
|
namespace mapnik {
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename T>
|
2018-08-06 18:14:07 +02:00
|
|
|
struct pixel_format
|
|
|
|
{
|
|
|
|
using type = typename detail::agg_scaling_traits<T>::pixfmt_pre;
|
|
|
|
};
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<>
|
2018-08-06 18:14:07 +02:00
|
|
|
struct pixel_format<image_rgba8>
|
|
|
|
{
|
|
|
|
struct src_blender
|
|
|
|
{
|
|
|
|
using color_type = agg::rgba8;
|
|
|
|
using order_type = agg::order_rgba;
|
|
|
|
using value_type = typename color_type::value_type;
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
static inline void
|
|
|
|
blend_pix(unsigned /*op*/, value_type* p, unsigned cr, unsigned cg, unsigned cb, unsigned ca, unsigned cover)
|
2018-08-06 18:14:07 +02:00
|
|
|
{
|
|
|
|
agg::comp_op_rgba_src<color_type, order_type>::blend_pix(p, cr, cg, cb, ca, cover);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use comp_op_src to fix seams between faces of the mesh
|
|
|
|
using type = agg::pixfmt_custom_blend_rgba<src_blender, agg::rendering_buffer>;
|
|
|
|
};
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename T>
|
|
|
|
MAPNIK_DECL void warp_image(T& target,
|
|
|
|
T const& source,
|
|
|
|
proj_transform const& prj_trans,
|
|
|
|
box2d<double> const& target_ext,
|
|
|
|
box2d<double> const& source_ext,
|
|
|
|
double offset_x,
|
|
|
|
double offset_y,
|
|
|
|
unsigned mesh_size,
|
|
|
|
scaling_method_e scaling_method,
|
|
|
|
double filter_factor,
|
|
|
|
boost::optional<double> const& nodata_value)
|
2011-09-16 16:34:14 +02:00
|
|
|
{
|
2015-01-22 18:39:37 +01:00
|
|
|
using image_type = T;
|
|
|
|
using pixel_type = typename image_type::pixel_type;
|
|
|
|
using pixfmt_pre = typename detail::agg_scaling_traits<image_type>::pixfmt_pre;
|
|
|
|
using color_type = typename detail::agg_scaling_traits<image_type>::color_type;
|
2018-08-06 18:14:07 +02:00
|
|
|
using output_pixfmt_type = typename pixel_format<T>::type;
|
|
|
|
using renderer_base = agg::renderer_base<output_pixfmt_type>;
|
2015-01-22 18:39:37 +01:00
|
|
|
using interpolator_type = typename detail::agg_scaling_traits<image_type>::interpolator_type;
|
2014-12-16 12:04:18 +01:00
|
|
|
|
|
|
|
constexpr std::size_t pixel_size = sizeof(pixel_type);
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
view_transform ts(source.width(), source.height(), source_ext);
|
|
|
|
view_transform tt(target.width(), target.height(), target_ext, offset_x, offset_y);
|
2014-07-07 19:23:15 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
std::size_t mesh_nx = std::ceil(source.width() / double(mesh_size) + 1);
|
|
|
|
std::size_t mesh_ny = std::ceil(source.height() / double(mesh_size) + 1);
|
2012-07-07 01:45:58 +02:00
|
|
|
|
2015-02-04 22:41:58 +01:00
|
|
|
image_gray64f xs(mesh_nx, mesh_ny, false);
|
|
|
|
image_gray64f ys(mesh_nx, mesh_ny, false);
|
2012-07-07 01:45:58 +02:00
|
|
|
|
|
|
|
// Precalculate reprojected mesh
|
2022-01-26 10:43:31 +01:00
|
|
|
for (std::size_t j = 0; j < mesh_ny; ++j)
|
2012-09-19 14:53:56 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
for (std::size_t i = 0; i < mesh_nx; ++i)
|
2012-09-19 14:53:56 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
xs(i, j) = std::min(i * mesh_size, source.width());
|
|
|
|
ys(i, j) = std::min(j * mesh_size, source.height());
|
|
|
|
ts.backward(&xs(i, j), &ys(i, j));
|
2011-09-16 16:34:14 +02:00
|
|
|
}
|
2012-07-07 01:45:58 +02:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
prj_trans.backward(xs.data(), ys.data(), nullptr, mesh_nx * mesh_ny);
|
2012-07-07 01:45:58 +02:00
|
|
|
|
|
|
|
agg::rasterizer_scanline_aa<> rasterizer;
|
2014-09-11 17:49:00 +02:00
|
|
|
agg::scanline_bin scanline;
|
2022-01-26 10:43:31 +01:00
|
|
|
agg::rendering_buffer buf(target.bytes(), target.width(), target.height(), target.width() * pixel_size);
|
2018-08-06 18:14:07 +02:00
|
|
|
output_pixfmt_type pixf(buf);
|
2013-07-24 00:46:40 +02:00
|
|
|
renderer_base rb(pixf);
|
2014-12-16 12:04:18 +01:00
|
|
|
rasterizer.clip_box(0, 0, target.width(), target.height());
|
2022-01-26 10:43:31 +01:00
|
|
|
agg::rendering_buffer buf_tile(const_cast<unsigned char*>(source.bytes()),
|
|
|
|
source.width(),
|
|
|
|
source.height(),
|
|
|
|
source.width() * pixel_size);
|
2012-07-07 01:45:58 +02:00
|
|
|
|
2014-12-16 12:04:18 +01:00
|
|
|
pixfmt_pre pixf_tile(buf_tile);
|
2012-07-07 01:45:58 +02:00
|
|
|
|
2014-12-16 12:04:18 +01:00
|
|
|
using img_accessor_type = agg::image_accessor_clone<pixfmt_pre>;
|
2012-07-07 01:45:58 +02:00
|
|
|
img_accessor_type ia(pixf_tile);
|
|
|
|
|
|
|
|
agg::span_allocator<color_type> sa;
|
|
|
|
// Project mesh cells into target interpolating raster inside each one
|
2014-12-16 12:04:18 +01:00
|
|
|
for (std::size_t j = 0; j < mesh_ny - 1; ++j)
|
2012-09-19 14:53:56 +02:00
|
|
|
{
|
2014-11-20 12:46:16 +01:00
|
|
|
for (std::size_t i = 0; i < mesh_nx - 1; ++i)
|
2012-09-19 14:53:56 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
double polygon[8] = {xs(i, j),
|
|
|
|
ys(i, j),
|
|
|
|
xs(i + 1, j),
|
|
|
|
ys(i + 1, j),
|
|
|
|
xs(i + 1, j + 1),
|
|
|
|
ys(i + 1, j + 1),
|
|
|
|
xs(i, j + 1),
|
|
|
|
ys(i, j + 1)};
|
|
|
|
tt.forward(polygon + 0, polygon + 1);
|
|
|
|
tt.forward(polygon + 2, polygon + 3);
|
|
|
|
tt.forward(polygon + 4, polygon + 5);
|
|
|
|
tt.forward(polygon + 6, polygon + 7);
|
2012-07-07 01:45:58 +02:00
|
|
|
|
|
|
|
rasterizer.reset();
|
2012-09-26 15:27:42 +02:00
|
|
|
rasterizer.move_to_d(std::floor(polygon[0]), std::floor(polygon[1]));
|
|
|
|
rasterizer.line_to_d(std::floor(polygon[2]), std::floor(polygon[3]));
|
|
|
|
rasterizer.line_to_d(std::floor(polygon[4]), std::floor(polygon[5]));
|
|
|
|
rasterizer.line_to_d(std::floor(polygon[6]), std::floor(polygon[7]));
|
2012-07-07 01:45:58 +02:00
|
|
|
|
2014-11-20 12:46:16 +01:00
|
|
|
std::size_t x0 = i * mesh_size;
|
|
|
|
std::size_t y0 = j * mesh_size;
|
2022-01-26 10:43:31 +01:00
|
|
|
std::size_t x1 = (i + 1) * mesh_size;
|
|
|
|
std::size_t y1 = (j + 1) * mesh_size;
|
2014-12-16 12:04:18 +01:00
|
|
|
x1 = std::min(x1, source.width());
|
|
|
|
y1 = std::min(y1, source.height());
|
2012-07-07 01:45:58 +02:00
|
|
|
agg::trans_affine tr(polygon, x0, y0, x1, y1);
|
|
|
|
if (tr.is_valid())
|
|
|
|
{
|
|
|
|
interpolator_type interpolator(tr);
|
2014-07-07 19:23:15 +02:00
|
|
|
if (scaling_method == SCALING_NEAR)
|
|
|
|
{
|
2015-01-22 18:39:37 +01:00
|
|
|
using span_gen_type = typename detail::agg_scaling_traits<image_type>::span_image_filter;
|
2012-07-07 01:45:58 +02:00
|
|
|
span_gen_type sg(ia, interpolator);
|
2014-12-16 12:04:18 +01:00
|
|
|
agg::render_scanlines_bin(rasterizer, scanline, rb, sa, sg);
|
2014-07-07 19:23:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-22 18:39:37 +01:00
|
|
|
using span_gen_type = typename detail::agg_scaling_traits<image_type>::span_image_resample_affine;
|
2014-12-16 12:04:18 +01:00
|
|
|
agg::image_filter_lut filter;
|
2014-12-17 14:24:58 +01:00
|
|
|
detail::set_scaling_method(filter, scaling_method, filter_factor);
|
2015-10-27 13:45:06 +01:00
|
|
|
boost::optional<typename span_gen_type::value_type> nodata;
|
|
|
|
if (nodata_value)
|
|
|
|
{
|
2018-02-14 16:46:24 +01:00
|
|
|
nodata = safe_cast<typename span_gen_type::value_type>(*nodata_value);
|
2015-10-27 13:45:06 +01:00
|
|
|
}
|
|
|
|
span_gen_type sg(ia, interpolator, filter, nodata);
|
2014-12-16 12:04:18 +01:00
|
|
|
agg::render_scanlines_bin(rasterizer, scanline, rb, sa, sg);
|
2011-09-16 16:34:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 12:04:18 +01:00
|
|
|
|
2014-12-17 14:24:58 +01:00
|
|
|
namespace detail {
|
|
|
|
|
2015-01-07 11:35:21 +01:00
|
|
|
struct warp_image_visitor
|
2014-12-16 12:04:18 +01:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
warp_image_visitor(raster& target_raster,
|
|
|
|
proj_transform const& prj_trans,
|
|
|
|
box2d<double> const& source_ext,
|
|
|
|
double offset_x,
|
|
|
|
double offset_y,
|
|
|
|
unsigned mesh_size,
|
|
|
|
scaling_method_e scaling_method,
|
|
|
|
double filter_factor,
|
|
|
|
boost::optional<double> const& nodata_value)
|
|
|
|
: target_raster_(target_raster)
|
|
|
|
, prj_trans_(prj_trans)
|
|
|
|
, source_ext_(source_ext)
|
|
|
|
, offset_x_(offset_x)
|
|
|
|
, offset_y_(offset_y)
|
|
|
|
, mesh_size_(mesh_size)
|
|
|
|
, scaling_method_(scaling_method)
|
|
|
|
, filter_factor_(filter_factor)
|
|
|
|
, nodata_value_(nodata_value)
|
2015-10-27 13:45:06 +01:00
|
|
|
{}
|
2014-12-16 12:04:18 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
void operator()(image_null const&) const {}
|
2014-12-16 12:04:18 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename T>
|
|
|
|
void operator()(T const& source) const
|
2014-12-16 12:04:18 +01:00
|
|
|
{
|
2015-01-22 18:39:37 +01:00
|
|
|
using image_type = T;
|
2022-01-26 10:43:31 +01:00
|
|
|
// source and target image data types must match
|
2015-01-22 18:39:37 +01:00
|
|
|
if (target_raster_.data_.template is<image_type>())
|
2014-12-16 12:04:18 +01:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
image_type& target = util::get<image_type>(target_raster_.data_);
|
|
|
|
warp_image(target,
|
|
|
|
source,
|
|
|
|
prj_trans_,
|
|
|
|
target_raster_.ext_,
|
|
|
|
source_ext_,
|
|
|
|
offset_x_,
|
|
|
|
offset_y_,
|
|
|
|
mesh_size_,
|
|
|
|
scaling_method_,
|
|
|
|
filter_factor_,
|
|
|
|
nodata_value_);
|
2014-12-16 12:04:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
raster& target_raster_;
|
2014-12-16 12:04:18 +01:00
|
|
|
proj_transform const& prj_trans_;
|
|
|
|
box2d<double> const& source_ext_;
|
|
|
|
double offset_x_;
|
|
|
|
double offset_y_;
|
|
|
|
unsigned mesh_size_;
|
|
|
|
scaling_method_e scaling_method_;
|
|
|
|
double filter_factor_;
|
2022-01-26 10:43:31 +01:00
|
|
|
boost::optional<double> const& nodata_value_;
|
2014-12-16 12:04:18 +01:00
|
|
|
};
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
} // namespace detail
|
2014-12-16 12:04:18 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
void reproject_and_scale_raster(raster& target,
|
|
|
|
raster const& source,
|
2014-12-16 12:04:18 +01:00
|
|
|
proj_transform const& prj_trans,
|
2022-01-26 10:43:31 +01:00
|
|
|
double offset_x,
|
|
|
|
double offset_y,
|
2014-12-16 12:04:18 +01:00
|
|
|
unsigned mesh_size,
|
2015-10-27 13:45:06 +01:00
|
|
|
scaling_method_e scaling_method,
|
2022-01-26 10:43:31 +01:00
|
|
|
boost::optional<double> const& nodata_value)
|
2014-12-16 12:04:18 +01:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
detail::warp_image_visitor warper(target,
|
|
|
|
prj_trans,
|
|
|
|
source.ext_,
|
|
|
|
offset_x,
|
|
|
|
offset_y,
|
|
|
|
mesh_size,
|
|
|
|
scaling_method,
|
|
|
|
source.get_filter_factor(),
|
|
|
|
nodata_value);
|
2014-12-16 12:04:18 +01:00
|
|
|
util::apply_visitor(warper, source.data_);
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
void reproject_and_scale_raster(raster& target,
|
|
|
|
raster const& source,
|
|
|
|
proj_transform const& prj_trans,
|
|
|
|
double offset_x,
|
|
|
|
double offset_y,
|
|
|
|
unsigned mesh_size,
|
|
|
|
scaling_method_e scaling_method)
|
2015-11-19 13:26:16 +01:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
reproject_and_scale_raster(target,
|
|
|
|
source,
|
|
|
|
prj_trans,
|
|
|
|
offset_x,
|
|
|
|
offset_y,
|
2015-11-19 13:26:16 +01:00
|
|
|
mesh_size,
|
|
|
|
scaling_method,
|
|
|
|
boost::optional<double>());
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template MAPNIK_DECL void warp_image(image_rgba8&,
|
|
|
|
image_rgba8 const&,
|
|
|
|
proj_transform const&,
|
|
|
|
box2d<double> const&,
|
|
|
|
box2d<double> const&,
|
|
|
|
double,
|
|
|
|
double,
|
|
|
|
unsigned,
|
|
|
|
scaling_method_e,
|
|
|
|
double,
|
|
|
|
boost::optional<double> const&);
|
|
|
|
|
|
|
|
template MAPNIK_DECL void warp_image(image_gray8&,
|
|
|
|
image_gray8 const&,
|
|
|
|
proj_transform const&,
|
|
|
|
box2d<double> const&,
|
|
|
|
box2d<double> const&,
|
|
|
|
double,
|
|
|
|
double,
|
|
|
|
unsigned,
|
|
|
|
scaling_method_e,
|
|
|
|
double,
|
|
|
|
boost::optional<double> const&);
|
|
|
|
|
|
|
|
template MAPNIK_DECL void warp_image(image_gray16&,
|
|
|
|
image_gray16 const&,
|
|
|
|
proj_transform const&,
|
|
|
|
box2d<double> const&,
|
|
|
|
box2d<double> const&,
|
|
|
|
double,
|
|
|
|
double,
|
|
|
|
unsigned,
|
|
|
|
scaling_method_e,
|
|
|
|
double,
|
|
|
|
boost::optional<double> const&);
|
|
|
|
|
|
|
|
template MAPNIK_DECL void warp_image(image_gray32f&,
|
|
|
|
image_gray32f const&,
|
|
|
|
proj_transform const&,
|
|
|
|
box2d<double> const&,
|
|
|
|
box2d<double> const&,
|
|
|
|
double,
|
|
|
|
double,
|
|
|
|
unsigned,
|
|
|
|
scaling_method_e,
|
|
|
|
double,
|
|
|
|
boost::optional<double> const&);
|
|
|
|
|
|
|
|
} // namespace mapnik
|