Skip to content

Change {FigureCanvasAgg,RendererAgg}.buffer_rgba to return a memoryview. #11735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/2018-07-22-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
`FigureCanvasAgg.buffer_rgba` and `RendererAgg.buffer_rgba` now return a memoryview
```````````````````````````````````````````````````````````````````````````````````

The ``buffer_rgba`` method now allows direct access to the renderer's
underlying buffer (as a ``(m, n, 4)``-shape memoryview) rather than copying the
data to a new bytestring. This is consistent with the behavior on Py2, where a
buffer object was returned.
2 changes: 1 addition & 1 deletion examples/misc/agg_buffer_to_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
fig.canvas.draw()

# grab the pixel buffer and dump it into a numpy array
X = np.array(fig.canvas.renderer._renderer)
X = np.array(fig.canvas.renderer.buffer_rgba())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wowzers. Good catch.


# now display the array X as an Axes in a new figure
fig2 = plt.figure()
Expand Down
25 changes: 12 additions & 13 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ def points_to_pixels(self, points):
"""
return points * self.dpi / 72

def tostring_rgb(self):
return self._renderer.tostring_rgb()
def buffer_rgba(self):
return memoryview(self._renderer)

def tostring_argb(self):
return self._renderer.tostring_argb()
return np.asarray(self._renderer).take([3, 0, 1, 2], axis=2).tobytes()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you take a look at uses of this and tostring_rgb on things like StackOverflow to confirm that the type change would continue to work as expected in those use-cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here there is no change to the return type, it was a bytes, it's still a bytes.

There's a change in the return type of buffer_rgba() but that's sort of the whole point of the PR (and it goes back to the old Py2 behavior, and is also clearly better for performance).


def buffer_rgba(self):
return self._renderer.buffer_rgba()
def tostring_rgb(self):
return np.asarray(self._renderer).take([0, 1, 2], axis=2).tobytes()

def clear(self):
self._renderer.clear()
Expand Down Expand Up @@ -421,40 +421,39 @@ def get_renderer(self, cleared=False):
return self.renderer

def tostring_rgb(self):
'''Get the image as an RGB byte string.
"""Get the image as an RGB byte string.

`draw` must be called at least once before this function will work and
to update the renderer for any subsequent changes to the Figure.

Returns
-------
bytes
'''
"""
return self.renderer.tostring_rgb()

def tostring_argb(self):
'''Get the image as an ARGB byte string
"""Get the image as an ARGB byte string.

`draw` must be called at least once before this function will work and
to update the renderer for any subsequent changes to the Figure.

Returns
-------
bytes

'''
"""
return self.renderer.tostring_argb()

def buffer_rgba(self):
'''Get the image as an RGBA byte string.
"""Get the image as a memoryview to the renderer's buffer.

`draw` must be called at least once before this function will work and
to update the renderer for any subsequent changes to the Figure.

Returns
-------
bytes
'''
memoryview
"""
return self.renderer.buffer_rgba()

def print_raw(self, filename_or_obj, *args, **kwargs):
Expand Down
23 changes: 0 additions & 23 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,6 @@ bool RendererAgg::render_clippath(py::PathIterator &clippath,
return has_clippath;
}

void RendererAgg::tostring_rgb(uint8_t *buf)
{
// "Return the rendered buffer as an RGB string"

int row_len = width * 3;

agg::rendering_buffer renderingBufferTmp;
renderingBufferTmp.attach(buf, width, height, row_len);

agg::color_conv(&renderingBufferTmp, &renderingBuffer, agg::color_conv_rgba32_to_rgb24());
}

void RendererAgg::tostring_argb(uint8_t *buf)
{
//"Return the rendered buffer as an RGB string";

int row_len = width * 4;

agg::rendering_buffer renderingBufferTmp;
renderingBufferTmp.attach(buf, width, height, row_len);
agg::color_conv(&renderingBufferTmp, &renderingBuffer, agg::color_conv_rgba32_to_argb32());
}

agg::rect_i RendererAgg::get_content_extents()
{
agg::rect_i r(width, height, 0, 0);
Expand Down
2 changes: 0 additions & 2 deletions src/_backend_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ class RendererAgg
ColorArray &colors,
agg::trans_affine &trans);

void tostring_rgb(uint8_t *buf);
void tostring_argb(uint8_t *buf);
agg::rect_i get_content_extents();
void clear();

Expand Down
34 changes: 0 additions & 34 deletions src/_backend_agg_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,38 +525,6 @@ PyRendererAgg_draw_gouraud_triangles(PyRendererAgg *self, PyObject *args, PyObje
Py_RETURN_NONE;
}

static PyObject *PyRendererAgg_tostring_rgb(PyRendererAgg *self, PyObject *args, PyObject *kwds)
{
PyObject *buffobj = NULL;

buffobj = PyBytes_FromStringAndSize(NULL, self->x->get_width() * self->x->get_height() * 3);
if (buffobj == NULL) {
return NULL;
}

CALL_CPP_CLEANUP("tostring_rgb",
(self->x->tostring_rgb((uint8_t *)PyBytes_AS_STRING(buffobj))),
Py_DECREF(buffobj));

return buffobj;
}

static PyObject *PyRendererAgg_tostring_argb(PyRendererAgg *self, PyObject *args, PyObject *kwds)
{
PyObject *buffobj = NULL;

buffobj = PyBytes_FromStringAndSize(NULL, self->x->get_width() * self->x->get_height() * 4);
if (buffobj == NULL) {
return NULL;
}

CALL_CPP_CLEANUP("tostring_argb",
(self->x->tostring_argb((uint8_t *)PyBytes_AS_STRING(buffobj))),
Py_DECREF(buffobj));

return buffobj;
}

static PyObject *
PyRendererAgg_get_content_extents(PyRendererAgg *self, PyObject *args, PyObject *kwds)
{
Expand Down Expand Up @@ -664,8 +632,6 @@ static PyTypeObject *PyRendererAgg_init_type(PyObject *m, PyTypeObject *type)
{"draw_gouraud_triangle", (PyCFunction)PyRendererAgg_draw_gouraud_triangle, METH_VARARGS, NULL},
{"draw_gouraud_triangles", (PyCFunction)PyRendererAgg_draw_gouraud_triangles, METH_VARARGS, NULL},

{"tostring_rgb", (PyCFunction)PyRendererAgg_tostring_rgb, METH_NOARGS, NULL},
{"tostring_argb", (PyCFunction)PyRendererAgg_tostring_argb, METH_NOARGS, NULL},
{"get_content_extents", (PyCFunction)PyRendererAgg_get_content_extents, METH_NOARGS, NULL},
{"buffer_rgba", (PyCFunction)PyRendererAgg_buffer_rgba, METH_NOARGS, NULL},
{"clear", (PyCFunction)PyRendererAgg_clear, METH_NOARGS, NULL},
Expand Down