2011-09-16 16:34:14 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#include <mapnik/image_data.hpp>
|
|
|
|
#include <mapnik/image_util.hpp>
|
|
|
|
#include <mapnik/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>
|
2011-09-16 16:34:14 +02:00
|
|
|
|
|
|
|
// agg
|
|
|
|
#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"
|
|
|
|
|
2011-09-16 16:57:40 +02:00
|
|
|
namespace mapnik {
|
|
|
|
|
2012-07-07 01:45:58 +02:00
|
|
|
void reproject_and_scale_raster(raster & target, raster const& source,
|
2012-09-26 15:27:42 +02:00
|
|
|
proj_transform const& prj_trans,
|
|
|
|
double offset_x, double offset_y,
|
|
|
|
unsigned mesh_size,
|
|
|
|
scaling_method_e scaling_method)
|
2011-09-16 16:34:14 +02:00
|
|
|
{
|
2014-08-28 11:17:15 +02:00
|
|
|
view_transform ts(source.data_.width(), source.data_.height(),
|
2012-07-07 01:45:58 +02:00
|
|
|
source.ext_);
|
2014-08-28 11:17:15 +02:00
|
|
|
view_transform tt(target.data_.width(), target.data_.height(),
|
2012-07-07 01:45:58 +02:00
|
|
|
target.ext_, offset_x, offset_y);
|
2014-07-07 19:23:15 +02:00
|
|
|
|
2013-01-17 22:53:48 +01:00
|
|
|
unsigned mesh_nx = std::ceil(source.data_.width()/double(mesh_size) + 1);
|
|
|
|
unsigned mesh_ny = std::ceil(source.data_.height()/double(mesh_size) + 1);
|
2012-07-07 01:45:58 +02:00
|
|
|
|
2014-10-21 17:06:27 +02:00
|
|
|
image_data<double> xs(mesh_nx, mesh_ny);
|
|
|
|
image_data<double> ys(mesh_nx, mesh_ny);
|
2012-07-07 01:45:58 +02:00
|
|
|
|
|
|
|
// Precalculate reprojected mesh
|
2014-07-07 19:23:15 +02:00
|
|
|
for(unsigned j=0; j<mesh_ny; ++j)
|
2012-09-19 14:53:56 +02:00
|
|
|
{
|
2014-07-07 19:23:15 +02:00
|
|
|
for (unsigned i=0; i<mesh_nx; ++i)
|
2012-09-19 14:53:56 +02:00
|
|
|
{
|
|
|
|
xs(i,j) = std::min(i*mesh_size,source.data_.width());
|
|
|
|
ys(i,j) = std::min(j*mesh_size,source.data_.height());
|
2012-07-07 01:45:58 +02:00
|
|
|
ts.backward(&xs(i,j), &ys(i,j));
|
2011-09-16 16:34:14 +02:00
|
|
|
}
|
2012-07-07 01:45:58 +02:00
|
|
|
}
|
2013-11-27 16:52:47 +01:00
|
|
|
prj_trans.backward(xs.getData(), ys.getData(), nullptr, mesh_nx*mesh_ny);
|
2012-07-07 01:45:58 +02:00
|
|
|
|
|
|
|
// Initialize AGG objects
|
2014-07-07 19:23:15 +02:00
|
|
|
using pixfmt = agg::pixfmt_rgba32_pre;
|
|
|
|
using color_type = pixfmt::color_type;
|
|
|
|
using renderer_base = agg::renderer_base<pixfmt>;
|
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;
|
2012-07-07 01:45:58 +02:00
|
|
|
agg::rendering_buffer buf((unsigned char*)target.data_.getData(),
|
|
|
|
target.data_.width(),
|
|
|
|
target.data_.height(),
|
|
|
|
target.data_.width()*4);
|
2013-07-24 00:46:40 +02:00
|
|
|
pixfmt pixf(buf);
|
|
|
|
renderer_base rb(pixf);
|
2012-07-07 01:45:58 +02:00
|
|
|
rasterizer.clip_box(0, 0, target.data_.width(), target.data_.height());
|
|
|
|
agg::rendering_buffer buf_tile(
|
|
|
|
(unsigned char*)source.data_.getData(),
|
|
|
|
source.data_.width(),
|
|
|
|
source.data_.height(),
|
|
|
|
source.data_.width() * 4);
|
|
|
|
|
|
|
|
pixfmt pixf_tile(buf_tile);
|
|
|
|
|
2014-07-07 19:23:15 +02:00
|
|
|
using img_accessor_type = agg::image_accessor_clone<pixfmt>;
|
2012-07-07 01:45:58 +02:00
|
|
|
img_accessor_type ia(pixf_tile);
|
|
|
|
|
|
|
|
agg::span_allocator<color_type> sa;
|
|
|
|
|
|
|
|
// Initialize filter
|
|
|
|
agg::image_filter_lut filter;
|
|
|
|
switch(scaling_method)
|
|
|
|
{
|
|
|
|
case SCALING_NEAR: break;
|
|
|
|
case SCALING_BILINEAR:
|
|
|
|
filter.calculate(agg::image_filter_bilinear(), true); break;
|
|
|
|
case SCALING_BICUBIC:
|
|
|
|
filter.calculate(agg::image_filter_bicubic(), true); break;
|
|
|
|
case SCALING_SPLINE16:
|
|
|
|
filter.calculate(agg::image_filter_spline16(), true); break;
|
|
|
|
case SCALING_SPLINE36:
|
|
|
|
filter.calculate(agg::image_filter_spline36(), true); break;
|
|
|
|
case SCALING_HANNING:
|
|
|
|
filter.calculate(agg::image_filter_hanning(), true); break;
|
|
|
|
case SCALING_HAMMING:
|
|
|
|
filter.calculate(agg::image_filter_hamming(), true); break;
|
|
|
|
case SCALING_HERMITE:
|
|
|
|
filter.calculate(agg::image_filter_hermite(), true); break;
|
|
|
|
case SCALING_KAISER:
|
|
|
|
filter.calculate(agg::image_filter_kaiser(), true); break;
|
|
|
|
case SCALING_QUADRIC:
|
|
|
|
filter.calculate(agg::image_filter_quadric(), true); break;
|
|
|
|
case SCALING_CATROM:
|
|
|
|
filter.calculate(agg::image_filter_catrom(), true); break;
|
|
|
|
case SCALING_GAUSSIAN:
|
|
|
|
filter.calculate(agg::image_filter_gaussian(), true); break;
|
|
|
|
case SCALING_BESSEL:
|
|
|
|
filter.calculate(agg::image_filter_bessel(), true); break;
|
|
|
|
case SCALING_MITCHELL:
|
|
|
|
filter.calculate(agg::image_filter_mitchell(), true); break;
|
|
|
|
case SCALING_SINC:
|
2013-11-28 07:50:15 +01:00
|
|
|
filter.calculate(agg::image_filter_sinc(source.get_filter_factor()), true); break;
|
2012-07-07 01:45:58 +02:00
|
|
|
case SCALING_LANCZOS:
|
2013-11-28 07:50:15 +01:00
|
|
|
filter.calculate(agg::image_filter_lanczos(source.get_filter_factor()), true); break;
|
2012-07-07 01:45:58 +02:00
|
|
|
case SCALING_BLACKMAN:
|
2013-11-28 07:50:15 +01:00
|
|
|
filter.calculate(agg::image_filter_blackman(source.get_filter_factor()), true); break;
|
2012-07-07 01:45:58 +02:00
|
|
|
}
|
2011-09-16 16:34:14 +02:00
|
|
|
|
2012-07-07 01:45:58 +02:00
|
|
|
// Project mesh cells into target interpolating raster inside each one
|
2014-07-07 19:23:15 +02:00
|
|
|
for(unsigned j=0; j<mesh_ny-1; ++j)
|
2012-09-19 14:53:56 +02:00
|
|
|
{
|
2014-07-07 19:23:15 +02:00
|
|
|
for (unsigned i=0; i<mesh_nx-1; ++i)
|
2012-09-19 14:53:56 +02:00
|
|
|
{
|
2012-07-07 01:45:58 +02: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);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
unsigned x0 = i * mesh_size;
|
|
|
|
unsigned y0 = j * mesh_size;
|
|
|
|
unsigned x1 = (i+1) * mesh_size;
|
|
|
|
unsigned y1 = (j+1) * mesh_size;
|
2012-09-19 14:53:56 +02:00
|
|
|
x1 = std::min(x1, source.data_.width());
|
|
|
|
y1 = std::min(y1, source.data_.height());
|
2012-07-07 01:45:58 +02:00
|
|
|
agg::trans_affine tr(polygon, x0, y0, x1, y1);
|
|
|
|
if (tr.is_valid())
|
|
|
|
{
|
2014-07-07 19:23:15 +02:00
|
|
|
using interpolator_type = agg::span_interpolator_linear<agg::trans_affine>;
|
2012-07-07 01:45:58 +02:00
|
|
|
interpolator_type interpolator(tr);
|
|
|
|
|
2014-07-07 19:23:15 +02:00
|
|
|
if (scaling_method == SCALING_NEAR)
|
|
|
|
{
|
|
|
|
using span_gen_type = agg::span_image_filter_rgba_nn
|
|
|
|
<img_accessor_type, interpolator_type>;
|
2012-07-07 01:45:58 +02:00
|
|
|
span_gen_type sg(ia, interpolator);
|
2014-09-11 17:49:00 +02:00
|
|
|
agg::render_scanlines_bin(rasterizer, scanline, rb,
|
2012-07-07 01:45:58 +02:00
|
|
|
sa, sg);
|
2014-07-07 19:23:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
using span_gen_type = agg::span_image_resample_rgba_affine
|
|
|
|
<img_accessor_type>;
|
2012-07-07 01:45:58 +02:00
|
|
|
span_gen_type sg(ia, interpolator, filter);
|
2014-09-11 17:49:00 +02:00
|
|
|
agg::render_scanlines_bin(rasterizer, scanline, rb,
|
2012-07-07 01:45:58 +02:00
|
|
|
sa, sg);
|
2011-09-16 16:34:14 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-07 01:45:58 +02:00
|
|
|
|
2011-09-16 16:34:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-16 16:57:40 +02:00
|
|
|
}// namespace mapnik
|