template scale_image_agg on image_data_type to squash code duplication

This commit is contained in:
artemp 2014-12-11 18:54:32 +01:00
parent 6b7ae6d700
commit 5dd87de782
2 changed files with 90 additions and 251 deletions

View file

@ -60,42 +60,13 @@ enum scaling_method_e
MAPNIK_DECL boost::optional<scaling_method_e> scaling_method_from_string(std::string const& name); MAPNIK_DECL boost::optional<scaling_method_e> scaling_method_from_string(std::string const& name);
MAPNIK_DECL boost::optional<std::string> scaling_method_to_string(scaling_method_e scaling_method); MAPNIK_DECL boost::optional<std::string> scaling_method_to_string(scaling_method_e scaling_method);
MAPNIK_DECL void scale_image_agg(image_data_gray8 & target, template <typename T> MAPNIK_DECL void scale_image_agg(T & target, T const& source,
image_data_gray8 const& source,
scaling_method_e scaling_method, scaling_method_e scaling_method,
double image_ratio_x, double image_ratio_x,
double image_ratio_y, double image_ratio_y,
double x_off_f, double x_off_f,
double y_off_f, double y_off_f,
double filter_factor); double filter_factor);
MAPNIK_DECL void scale_image_agg(image_data_gray32f & target,
image_data_gray32f const& source,
scaling_method_e scaling_method,
double image_ratio_x,
double image_ratio_y,
double x_off_f,
double y_off_f,
double filter_factor);
MAPNIK_DECL void scale_image_agg(image_data_gray16 & target,
image_data_gray16 const& source,
scaling_method_e scaling_method,
double image_ratio_x,
double image_ratio_y,
double x_off_f,
double y_off_f,
double filter_factor);
MAPNIK_DECL void scale_image_agg(image_data_rgba8 & target,
image_data_rgba8 const& source,
scaling_method_e scaling_method,
double image_ratio_x,
double image_ratio_y,
double x_off_f,
double y_off_f,
double filter_factor);
} }
#endif // MAPNIK_IMAGE_SCALING_HPP #endif // MAPNIK_IMAGE_SCALING_HPP

View file

@ -96,36 +96,89 @@ boost::optional<std::string> scaling_method_to_string(scaling_method_e scaling_m
return mode; return mode;
} }
void scale_image_agg(image_data_rgba8 & target,
image_data_rgba8 const& source, namespace detail {
scaling_method_e scaling_method,
double image_ratio_x, template <typename T>
double image_ratio_y, struct agg_scaling_traits {};
double x_off_f,
double y_off_f, template <>
struct agg_scaling_traits<image_data_rgba8>
{
using pixfmt_pre = agg::pixfmt_rgba32_pre;
using color_type = agg::rgba8;
using interpolator_type = agg::span_interpolator_linear<>;
using img_src_type = agg::image_accessor_clone<pixfmt_pre>;
using span_image_filter = agg::span_image_filter_rgba_nn<img_src_type,interpolator_type>;
using span_image_resample_affine = agg::span_image_resample_rgba_affine<img_src_type>;
};
template <>
struct agg_scaling_traits<image_data_gray8>
{
using pixfmt_pre = agg::pixfmt_gray8_pre;
using color_type = agg::gray8;
using interpolator_type = agg::span_interpolator_linear<>;
using img_src_type = agg::image_accessor_clone<pixfmt_pre>;
using span_image_filter = agg::span_image_filter_gray_nn<img_src_type,interpolator_type>;
using span_image_resample_affine = agg::span_image_resample_gray_affine<img_src_type>;
};
template <>
struct agg_scaling_traits<image_data_gray16>
{
using pixfmt_pre = agg::pixfmt_gray16_pre;
using color_type = agg::gray16;
using interpolator_type = agg::span_interpolator_linear<>;
using img_src_type = agg::image_accessor_clone<pixfmt_pre>;
using span_image_filter = agg::span_image_filter_gray_nn<img_src_type,interpolator_type>;
using span_image_resample_affine = agg::span_image_resample_gray_affine<img_src_type>;
};
template <>
struct agg_scaling_traits<image_data_gray32f>
{
using pixfmt_pre = agg::pixfmt_gray32_pre;
using color_type = agg::gray32;
using interpolator_type = agg::span_interpolator_linear<>;
using img_src_type = agg::image_accessor_clone<pixfmt_pre>;
using span_image_filter = agg::span_image_filter_gray_nn<img_src_type,interpolator_type>;
using span_image_resample_affine = agg::span_image_resample_gray_affine<img_src_type>;
};
}
template <typename T>
void scale_image_agg(T & target, T const& source, scaling_method_e scaling_method,
double image_ratio_x, double image_ratio_y, double x_off_f, double y_off_f,
double filter_factor) double filter_factor)
{ {
// "the image filters should work namely in the premultiplied color space" // "the image filters should work namely in the premultiplied color space"
// http://old.nabble.com/Re:--AGG--Basic-image-transformations-p1110665.html // http://old.nabble.com/Re:--AGG--Basic-image-transformations-p1110665.html
// "Yes, you need to use premultiplied images only. Only in this case the simple weighted averaging works correctly in the image fitering." // "Yes, you need to use premultiplied images only. Only in this case the simple weighted averaging works correctly in the image fitering."
// http://permalink.gmane.org/gmane.comp.graphics.agg/3443 // http://permalink.gmane.org/gmane.comp.graphics.agg/3443
using pixfmt_pre = agg::pixfmt_rgba32_pre; using image_data_type = T;
using pixel_type = typename image_data_type::pixel_type;
using pixfmt_pre = typename detail::agg_scaling_traits<image_data_type>::pixfmt_pre;
using color_type = typename detail::agg_scaling_traits<image_data_type>::color_type;
using img_src_type = typename detail::agg_scaling_traits<image_data_type>::img_src_type;
using interpolator_type = typename detail::agg_scaling_traits<image_data_type>::interpolator_type;
using renderer_base_pre = agg::renderer_base<pixfmt_pre>; using renderer_base_pre = agg::renderer_base<pixfmt_pre>;
// define some stuff we'll use soon // define some stuff we'll use soon
agg::rasterizer_scanline_aa<> ras; agg::rasterizer_scanline_aa<> ras;
agg::scanline_u8 sl; agg::scanline_u8 sl;
agg::span_allocator<agg::rgba8> sa; agg::span_allocator<color_type> sa;
agg::image_filter_lut filter; agg::image_filter_lut filter;
// initialize source AGG buffer // initialize source AGG buffer
agg::rendering_buffer rbuf_src(const_cast<unsigned char*>(source.getBytes()), source.width(), source.height(), source.width() * 4); agg::rendering_buffer rbuf_src(const_cast<unsigned char*>(source.getBytes()), source.width(), source.height(), source.width() * sizeof(pixel_type));
pixfmt_pre pixf_src(rbuf_src); pixfmt_pre pixf_src(rbuf_src);
using img_src_type = agg::image_accessor_clone<pixfmt_pre>;
img_src_type img_src(pixf_src); img_src_type img_src(pixf_src);
// initialize destination AGG buffer (with transparency) // initialize destination AGG buffer (with transparency)
agg::rendering_buffer rbuf_dst(target.getBytes(), target.width(), target.height(), target.width() * 4); agg::rendering_buffer rbuf_dst(target.getBytes(), target.width(), target.height(), target.width() * sizeof(pixel_type));
pixfmt_pre pixf_dst(rbuf_dst); pixfmt_pre pixf_dst(rbuf_dst);
renderer_base_pre rb_dst_pre(pixf_dst); renderer_base_pre rb_dst_pre(pixf_dst);
@ -134,7 +187,7 @@ void scale_image_agg(image_data_rgba8 & target,
img_mtx /= agg::trans_affine_scaling(image_ratio_x, image_ratio_y); img_mtx /= agg::trans_affine_scaling(image_ratio_x, image_ratio_y);
// create a linear interpolator for our scaling matrix // create a linear interpolator for our scaling matrix
using interpolator_type = agg::span_interpolator_linear<>;
interpolator_type interpolator(img_mtx); interpolator_type interpolator(img_mtx);
// draw an anticlockwise polygon to render our image into // draw an anticlockwise polygon to render our image into
@ -150,7 +203,7 @@ void scale_image_agg(image_data_rgba8 & target,
{ {
case SCALING_NEAR: case SCALING_NEAR:
{ {
using span_gen_type = agg::span_image_filter_rgba_nn<img_src_type, interpolator_type>; using span_gen_type = typename detail::agg_scaling_traits<image_data_type>::span_image_filter;
span_gen_type sg(img_src, interpolator); span_gen_type sg(img_src, interpolator);
agg::render_scanlines_aa(ras, sl, rb_dst_pre, sa, sg); agg::render_scanlines_aa(ras, sl, rb_dst_pre, sa, sg);
return; return;
@ -192,7 +245,7 @@ void scale_image_agg(image_data_rgba8 & target,
// http://old.nabble.com/Re%3A-Newbie---texture-p5057255.html // http://old.nabble.com/Re%3A-Newbie---texture-p5057255.html
// high quality resampler // high quality resampler
using span_gen_type = agg::span_image_resample_rgba_affine<img_src_type>; using span_gen_type = typename detail::agg_scaling_traits<image_data_type>::span_image_resample_affine;
// faster, lower quality // faster, lower quality
//using span_gen_type = agg::span_image_filter_rgba<img_src_type,interpolator_type>; //using span_gen_type = agg::span_image_filter_rgba<img_src_type,interpolator_type>;
@ -204,204 +257,19 @@ void scale_image_agg(image_data_rgba8 & target,
//using span_gen_type = mapnik::span_image_resample_rgba_affine<img_src_type>; //using span_gen_type = mapnik::span_image_resample_rgba_affine<img_src_type>;
span_gen_type sg(img_src, interpolator, filter); span_gen_type sg(img_src, interpolator, filter);
agg::render_scanlines_aa(ras, sl, rb_dst_pre, sa, sg); agg::render_scanlines_aa(ras, sl, rb_dst_pre, sa, sg);
}
}
void scale_image_agg(image_data_gray8 & target,
image_data_gray8 const& source, template void scale_image_agg(image_data_rgba8 &, image_data_rgba8 const&, scaling_method_e,
scaling_method_e scaling_method, double, double , double, double , double);
double image_ratio_x,
double image_ratio_y, template void scale_image_agg(image_data_gray8 &, image_data_gray8 const&, scaling_method_e,
double x_off_f, double, double , double, double , double);
double y_off_f,
double filter_factor) template void scale_image_agg(image_data_gray16 &, image_data_gray16 const&, scaling_method_e,
{ double, double , double, double , double);
// TODO
} template void scale_image_agg(image_data_gray32f &, image_data_gray32f const&, scaling_method_e,
double, double , double, double , double);
void scale_image_agg(image_data_gray32f & target,
image_data_gray32f const& source,
scaling_method_e scaling_method,
double image_ratio_x,
double image_ratio_y,
double x_off_f,
double y_off_f,
double filter_factor)
{
using pixfmt_pre = agg::pixfmt_gray32_pre;
using renderer_base_pre = agg::renderer_base<pixfmt_pre>;
// define some stuff we'll use soon
agg::rasterizer_scanline_aa<> ras;
agg::scanline_u8 sl;
agg::span_allocator<agg::gray32> sa;
agg::image_filter_lut filter;
// initialize source AGG buffer
agg::rendering_buffer rbuf_src(const_cast<unsigned char*>(source.getBytes()), source.width(), source.height(), source.width() * 4);
pixfmt_pre pixf_src(rbuf_src);
using img_src_type = agg::image_accessor_clone<pixfmt_pre>;
img_src_type img_src(pixf_src);
// initialize destination AGG buffer (with transparency)
agg::rendering_buffer rbuf_dst(target.getBytes(), target.width(), target.height(), target.width() * 4);
pixfmt_pre pixf_dst(rbuf_dst);
renderer_base_pre rb_dst_pre(pixf_dst);
// create a scaling matrix
agg::trans_affine img_mtx;
img_mtx /= agg::trans_affine_scaling(image_ratio_x, image_ratio_y);
// create a linear interpolator for our scaling matrix
using interpolator_type = agg::span_interpolator_linear<>;
interpolator_type interpolator(img_mtx);
// draw an anticlockwise polygon to render our image into
double scaled_width = target.width();
double scaled_height = target.height();
ras.reset();
ras.move_to_d(x_off_f, y_off_f);
ras.line_to_d(x_off_f + scaled_width, y_off_f);
ras.line_to_d(x_off_f + scaled_width, y_off_f + scaled_height);
ras.line_to_d(x_off_f, y_off_f + scaled_height);
switch(scaling_method)
{
case SCALING_NEAR:
{
using span_gen_type = agg::span_image_filter_gray_nn<img_src_type, interpolator_type>;
span_gen_type sg(img_src, interpolator);
agg::render_scanlines_aa(ras, sl, rb_dst_pre, sa, sg);
return;
}
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:
filter.calculate(agg::image_filter_sinc(filter_factor), true); break;
case SCALING_LANCZOS:
filter.calculate(agg::image_filter_lanczos(filter_factor), true); break;
case SCALING_BLACKMAN:
filter.calculate(agg::image_filter_blackman(filter_factor), true); break;
}
using span_gen_type = agg::span_image_resample_gray_affine<img_src_type>;
span_gen_type sg(img_src, interpolator, filter);
agg::render_scanlines_aa(ras, sl, rb_dst_pre, sa, sg);
}
void scale_image_agg(image_data_gray16 & target,
image_data_gray16 const& source,
scaling_method_e scaling_method,
double image_ratio_x,
double image_ratio_y,
double x_off_f,
double y_off_f,
double filter_factor)
{
using pixfmt_pre = agg::pixfmt_gray16_pre;
using renderer_base_pre = agg::renderer_base<pixfmt_pre>;
// define some stuff we'll use soon
agg::rasterizer_scanline_aa<> ras;
agg::scanline_u8 sl;
agg::span_allocator<agg::gray16> sa;
agg::image_filter_lut filter;
// initialize source AGG buffer
agg::rendering_buffer rbuf_src(const_cast<unsigned char*>(source.getBytes()), source.width(), source.height(), source.width() * 2);
pixfmt_pre pixf_src(rbuf_src);
using img_src_type = agg::image_accessor_clone<pixfmt_pre>;
img_src_type img_src(pixf_src);
// initialize destination AGG buffer (with transparency)
agg::rendering_buffer rbuf_dst(target.getBytes(), target.width(), target.height(), target.width() * 2);
pixfmt_pre pixf_dst(rbuf_dst);
renderer_base_pre rb_dst_pre(pixf_dst);
// create a scaling matrix
agg::trans_affine img_mtx;
img_mtx /= agg::trans_affine_scaling(image_ratio_x, image_ratio_y);
// create a linear interpolator for our scaling matrix
using interpolator_type = agg::span_interpolator_linear<>;
interpolator_type interpolator(img_mtx);
// draw an anticlockwise polygon to render our image into
double scaled_width = target.width();
double scaled_height = target.height();
ras.reset();
ras.move_to_d(x_off_f, y_off_f);
ras.line_to_d(x_off_f + scaled_width, y_off_f);
ras.line_to_d(x_off_f + scaled_width, y_off_f + scaled_height);
ras.line_to_d(x_off_f, y_off_f + scaled_height);
switch(scaling_method)
{
case SCALING_NEAR:
{
using span_gen_type = agg::span_image_filter_gray_nn<img_src_type, interpolator_type>;
span_gen_type sg(img_src, interpolator);
agg::render_scanlines_aa(ras, sl, rb_dst_pre, sa, sg);
return;
}
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:
filter.calculate(agg::image_filter_sinc(filter_factor), true); break;
case SCALING_LANCZOS:
filter.calculate(agg::image_filter_lanczos(filter_factor), true); break;
case SCALING_BLACKMAN:
filter.calculate(agg::image_filter_blackman(filter_factor), true); break;
}
using span_gen_type = agg::span_image_resample_gray_affine<img_src_type>;
span_gen_type sg(img_src, interpolator, filter);
agg::render_scanlines_aa(ras, sl, rb_dst_pre, sa, sg);
}
} }