Skip to content

Commit

Permalink
Fix matplotlib#4154: Return a writable buffer from conv_color
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Feb 25, 2015
1 parent f529297 commit bbdaf40
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/_image_wrapper.cpp
Expand Up @@ -165,14 +165,21 @@ static PyObject *PyImage_color_conv(PyImage *self, PyObject *args, PyObject *kwd
return NULL;
}

PyObject *result = PyBytes_FromStringAndSize(NULL, self->x->rowsOut * self->x->colsOut * 4);
if (result == NULL) {
return NULL;
Py_ssize_t size = self->x->rowsOut * self->x->colsOut * 4;
agg::int8u *buff = (agg::int8u *)malloc(size);
if (buff == NULL) {
PyErr_SetString(PyExc_MemoryError, "Out of memory");
}

CALL_CPP_CLEANUP("color_conv",
(self->x->color_conv(format, (agg::int8u *)PyBytes_AsString(result))),
Py_DECREF(result));
(self->x->color_conv(format, buff)),
free(buff));

PyObject *result = PyByteArray_FromStringAndSize((const char *)buff, size);
if (result == NULL) {
free(buff);
return NULL;
}

return Py_BuildValue("nnN", self->x->rowsOut, self->x->colsOut, result);
}
Expand Down

0 comments on commit bbdaf40

Please sign in to comment.