Skip to content

Commit c42b587

Browse files
committed
Use numpy.take instead of C-level manipulations for tostring_argb etc.
1 parent 1151b27 commit c42b587

File tree

4 files changed

+5
-64
lines changed

4 files changed

+5
-64
lines changed

lib/matplotlib/backends/backend_agg.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,14 @@ def points_to_pixels(self, points):
278278
"""
279279
return points*self.dpi/72.0
280280

281-
def tostring_rgb(self):
282-
return self._renderer.tostring_rgb()
281+
def buffer_rgba(self):
282+
return np.asarray(self._renderer)
283283

284284
def tostring_argb(self):
285-
return self._renderer.tostring_argb()
285+
return np.asarray(self._renderer).take([3, 0, 1, 2], axis=2).tobytes()
286286

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

290290
def clear(self):
291291
self._renderer.clear()

src/_backend_agg.cpp

-23
Original file line numberDiff line numberDiff line change
@@ -156,29 +156,6 @@ bool RendererAgg::render_clippath(py::PathIterator &clippath,
156156
return has_clippath;
157157
}
158158

159-
void RendererAgg::tostring_rgb(uint8_t *buf)
160-
{
161-
// "Return the rendered buffer as an RGB string"
162-
163-
int row_len = width * 3;
164-
165-
agg::rendering_buffer renderingBufferTmp;
166-
renderingBufferTmp.attach(buf, width, height, row_len);
167-
168-
agg::color_conv(&renderingBufferTmp, &renderingBuffer, agg::color_conv_rgba32_to_rgb24());
169-
}
170-
171-
void RendererAgg::tostring_argb(uint8_t *buf)
172-
{
173-
//"Return the rendered buffer as an RGB string";
174-
175-
int row_len = width * 4;
176-
177-
agg::rendering_buffer renderingBufferTmp;
178-
renderingBufferTmp.attach(buf, width, height, row_len);
179-
agg::color_conv(&renderingBufferTmp, &renderingBuffer, agg::color_conv_rgba32_to_argb32());
180-
}
181-
182159
void RendererAgg::tostring_bgra(uint8_t *buf)
183160
{
184161
//"Return the rendered buffer as an RGB string";

src/_backend_agg.h

-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ class RendererAgg
203203
ColorArray &colors,
204204
agg::trans_affine &trans);
205205

206-
void tostring_rgb(uint8_t *buf);
207-
void tostring_argb(uint8_t *buf);
208206
void tostring_bgra(uint8_t *buf);
209207
agg::rect_i get_content_extents();
210208
void clear();

src/_backend_agg_wrapper.cpp

-34
Original file line numberDiff line numberDiff line change
@@ -525,38 +525,6 @@ PyRendererAgg_draw_gouraud_triangles(PyRendererAgg *self, PyObject *args, PyObje
525525
Py_RETURN_NONE;
526526
}
527527

528-
static PyObject *PyRendererAgg_tostring_rgb(PyRendererAgg *self, PyObject *args, PyObject *kwds)
529-
{
530-
PyObject *buffobj = NULL;
531-
532-
buffobj = PyBytes_FromStringAndSize(NULL, self->x->get_width() * self->x->get_height() * 3);
533-
if (buffobj == NULL) {
534-
return NULL;
535-
}
536-
537-
CALL_CPP_CLEANUP("tostring_rgb",
538-
(self->x->tostring_rgb((uint8_t *)PyBytes_AS_STRING(buffobj))),
539-
Py_DECREF(buffobj));
540-
541-
return buffobj;
542-
}
543-
544-
static PyObject *PyRendererAgg_tostring_argb(PyRendererAgg *self, PyObject *args, PyObject *kwds)
545-
{
546-
PyObject *buffobj = NULL;
547-
548-
buffobj = PyBytes_FromStringAndSize(NULL, self->x->get_width() * self->x->get_height() * 4);
549-
if (buffobj == NULL) {
550-
return NULL;
551-
}
552-
553-
CALL_CPP_CLEANUP("tostring_argb",
554-
(self->x->tostring_argb((uint8_t *)PyBytes_AS_STRING(buffobj))),
555-
Py_DECREF(buffobj));
556-
557-
return buffobj;
558-
}
559-
560528
static PyObject *PyRendererAgg_tostring_bgra(PyRendererAgg *self, PyObject *args, PyObject *kwds)
561529
{
562530
PyObject *buffobj = NULL;
@@ -680,8 +648,6 @@ static PyTypeObject *PyRendererAgg_init_type(PyObject *m, PyTypeObject *type)
680648
{"draw_gouraud_triangle", (PyCFunction)PyRendererAgg_draw_gouraud_triangle, METH_VARARGS, NULL},
681649
{"draw_gouraud_triangles", (PyCFunction)PyRendererAgg_draw_gouraud_triangles, METH_VARARGS, NULL},
682650

683-
{"tostring_rgb", (PyCFunction)PyRendererAgg_tostring_rgb, METH_NOARGS, NULL},
684-
{"tostring_argb", (PyCFunction)PyRendererAgg_tostring_argb, METH_NOARGS, NULL},
685651
{"tostring_bgra", (PyCFunction)PyRendererAgg_tostring_bgra, METH_NOARGS, NULL},
686652
{"get_content_extents", (PyCFunction)PyRendererAgg_get_content_extents, METH_NOARGS, NULL},
687653
{"buffer_rgba", (PyCFunction)PyRendererAgg_buffer_rgba, METH_NOARGS, NULL},

0 commit comments

Comments
 (0)