Skip to content

Commit

Permalink
Agg backend shouldn't do its own image interp
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Feb 17, 2016
1 parent cf11aea commit 3b7d1bb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 69 deletions.
12 changes: 3 additions & 9 deletions lib/matplotlib/backends/backend_agg.py
Expand Up @@ -321,7 +321,7 @@ def option_scale_image(self):
"""
agg backend support arbitrary scaling of image.
"""
return True
return False

def restore_region(self, region, bbox=None, xy=None):
"""
Expand Down Expand Up @@ -389,28 +389,22 @@ def post_processing(image, dpi):
# For agg_filter to work, the rendere's method need
# to overridden in the class. See draw_markers, and draw_path_collections

from matplotlib._image import fromarray

width, height = int(self.width), int(self.height)

buffer, bounds = self.tostring_rgba_minimized()

l, b, w, h = bounds


self._renderer = self._filter_renderers.pop()
self._update_methods()

if w > 0 and h > 0:
img = np.fromstring(buffer, np.uint8)
img, ox, oy = post_processing(img.reshape((h, w, 4)) / 255.,
self.dpi)
image = fromarray(img, 1)

gc = self.new_gc()
self._renderer.draw_image(gc,
l+ox, height - b - h +oy,
image)
self._renderer.draw_image(
gc, l + ox, height - b - h + oy, img)


def new_figure_manager(num, *args, **kwargs):
Expand Down
55 changes: 12 additions & 43 deletions src/_backend_agg.h
Expand Up @@ -158,11 +158,7 @@ class RendererAgg
void draw_image(GCAgg &gc,
double x,
double y,
ImageArray &image,
double w,
double h,
agg::trans_affine trans,
bool resize);
ImageArray &image);

template <class PathGenerator,
class TransformArray,
Expand Down Expand Up @@ -832,11 +828,7 @@ template <class ImageArray>
inline void RendererAgg::draw_image(GCAgg &gc,
double x,
double y,
ImageArray &image,
double w,
double h,
agg::trans_affine trans,
bool resize)
ImageArray &image)
{
double alpha = gc.alpha;

Expand All @@ -850,21 +842,11 @@ inline void RendererAgg::draw_image(GCAgg &gc,
image.data(), (unsigned)image.dim(1), (unsigned)image.dim(0), -(int)image.dim(1) * 4);
pixfmt pixf(buffer);

if (resize | has_clippath) {
if (has_clippath) {
agg::trans_affine mtx;
agg::path_storage rect;

if (resize) {
mtx *= agg::trans_affine_scaling(1, -1);
mtx *= agg::trans_affine_translation(0, image.dim(0));
mtx *= agg::trans_affine_scaling(w / (image.dim(1)), h / (image.dim(0)));
mtx *= agg::trans_affine_translation(x, y);
mtx *= trans;
mtx *= agg::trans_affine_scaling(1.0, -1.0);
mtx *= agg::trans_affine_translation(0.0, (double)height);
} else {
mtx *= agg::trans_affine_translation((int)x, (int)(height - (y + image.dim(0))));
}
mtx *= agg::trans_affine_translation((int)x, (int)(height - (y + image.dim(0))));

rect.move_to(0, 0);
rect.line_to(image.dim(1), 0);
Expand All @@ -891,30 +873,17 @@ inline void RendererAgg::draw_image(GCAgg &gc,
span_conv_alpha conv_alpha(alpha);
span_conv spans(image_span_generator, conv_alpha);

if (has_clippath) {
typedef agg::pixfmt_amask_adaptor<pixfmt, alpha_mask_type> pixfmt_amask_type;
typedef agg::renderer_base<pixfmt_amask_type> amask_ren_type;
typedef agg::renderer_scanline_aa<amask_ren_type, color_span_alloc_type, span_conv>
typedef agg::pixfmt_amask_adaptor<pixfmt, alpha_mask_type> pixfmt_amask_type;
typedef agg::renderer_base<pixfmt_amask_type> amask_ren_type;
typedef agg::renderer_scanline_aa<amask_ren_type, color_span_alloc_type, span_conv>
renderer_type_alpha;

pixfmt_amask_type pfa(pixFmt, alphaMask);
amask_ren_type r(pfa);
renderer_type_alpha ri(r, sa, spans);

theRasterizer.add_path(rect2);
agg::render_scanlines(theRasterizer, scanlineAlphaMask, ri);
} else {
typedef agg::renderer_base<pixfmt> ren_type;
typedef agg::renderer_scanline_aa<ren_type, color_span_alloc_type, span_conv>
renderer_type;

ren_type r(pixFmt);
renderer_type ri(r, sa, spans);

theRasterizer.add_path(rect2);
agg::render_scanlines(theRasterizer, slineP8, ri);
}
pixfmt_amask_type pfa(pixFmt, alphaMask);
amask_ren_type r(pfa);
renderer_type_alpha ri(r, sa, spans);

theRasterizer.add_path(rect2);
agg::render_scanlines(theRasterizer, scanlineAlphaMask, ri);
} else {
set_clipbox(gc.cliprect, rendererBase);
rendererBase.blend_from(
Expand Down
22 changes: 5 additions & 17 deletions src/_backend_agg_wrapper.cpp
Expand Up @@ -284,34 +284,22 @@ static PyObject *PyRendererAgg_draw_image(PyRendererAgg *self, PyObject *args, P
double x;
double y;
numpy::array_view<agg::int8u, 3> image;
double w = 0;
double h = 0;
agg::trans_affine trans;
bool resize = false;

if (!PyArg_ParseTuple(args,
"O&ddO&|ddO&:draw_image",
"O&ddO&:draw_image",
&convert_gcagg,
&gc,
&x,
&y,
&image.converter_contiguous,
&image,
&w,
&h,
&convert_trans_affine,
&trans)) {
&image)) {
return NULL;
}

if (PyTuple_Size(args) == 4) {
x = mpl_round(x);
y = mpl_round(y);
} else {
resize = true;
}
x = mpl_round(x);
y = mpl_round(y);

CALL_CPP("draw_image", (self->x->draw_image(gc, x, y, image, w, h, trans, resize)));
CALL_CPP("draw_image", (self->x->draw_image(gc, x, y, image)));

Py_RETURN_NONE;
}
Expand Down

0 comments on commit 3b7d1bb

Please sign in to comment.