Pass bytearray into C layer instead of castin g to string in the Python layer.

This commit is contained in:
wiredfool 2014-06-19 14:54:39 -07:00
parent a422a4ff4e
commit b95eb3d3d1
2 changed files with 23 additions and 6 deletions

View File

@ -195,7 +195,7 @@ class MorphOp:
raise Exception('No operator loaded') raise Exception('No operator loaded')
outimage = Image.new(image.mode, image.size, None) outimage = Image.new(image.mode, image.size, None)
count = _imagingmorph.apply(str(self.lut), image.im.id, outimage.im.id) count = _imagingmorph.apply(self.lut, image.im.id, outimage.im.id)
return count, outimage return count, outimage
def match(self, image): def match(self, image):
@ -205,7 +205,7 @@ class MorphOp:
if self.lut is None: if self.lut is None:
raise Exception('No operator loaded') raise Exception('No operator loaded')
return _imagingmorph.match(str(self.lut), image.im.id) return _imagingmorph.match(self.lut, image.im.id)
def get_on_pixels(self, image): def get_on_pixels(self, image):
"""Get a list of all turned on pixels in a binary image """Get a list of all turned on pixels in a binary image

View File

@ -32,6 +32,7 @@ static PyObject*
apply(PyObject *self, PyObject* args) apply(PyObject *self, PyObject* args)
{ {
const char *lut; const char *lut;
PyObject *py_lut;
Py_ssize_t lut_len, i0, i1; Py_ssize_t lut_len, i0, i1;
Imaging imgin, imgout; Imaging imgin, imgout;
int width, height; int width, height;
@ -39,17 +40,25 @@ apply(PyObject *self, PyObject* args)
UINT8 **inrows, **outrows; UINT8 **inrows, **outrows;
int num_changed_pixels = 0; int num_changed_pixels = 0;
if (!PyArg_ParseTuple(args, "s#nn", &lut, &lut_len, &i0, &i1)) { if (!PyArg_ParseTuple(args, "Onn", &py_lut, &i0, &i1)) {
PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem"); PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
return NULL; return NULL;
} }
if (!PyByteArray_Check(py_lut)) {
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT is not a byte array");
return NULL;
}
lut_len = PyByteArray_Size(py_lut);
if (lut_len < LUT_SIZE) { if (lut_len < LUT_SIZE) {
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size"); PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size");
return NULL; return NULL;
} }
lut = PyByteArray_AsString(py_lut);
imgin = (Imaging) i0; imgin = (Imaging) i0;
imgout = (Imaging) i1; imgout = (Imaging) i1;
width = imgin->xsize; width = imgin->xsize;
@ -130,6 +139,7 @@ static PyObject*
match(PyObject *self, PyObject* args) match(PyObject *self, PyObject* args)
{ {
const char *lut; const char *lut;
PyObject *py_lut;
Py_ssize_t lut_len, i0; Py_ssize_t lut_len, i0;
Imaging imgin; Imaging imgin;
int width, height; int width, height;
@ -137,17 +147,24 @@ match(PyObject *self, PyObject* args)
UINT8 **inrows; UINT8 **inrows;
PyObject *ret = PyList_New(0); PyObject *ret = PyList_New(0);
if (!PyArg_ParseTuple(args, "s#n", &lut, &lut_len, &i0)) { if (!PyArg_ParseTuple(args, "On", &py_lut, &i0)) {
PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem"); PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
return NULL; return NULL;
} }
if (!PyByteArray_Check(py_lut)) {
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT is not a byte array");
return NULL;
}
lut_len = PyByteArray_Size(py_lut);
if (lut_len < LUT_SIZE) { if (lut_len < LUT_SIZE) {
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size"); PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size");
return NULL; return NULL;
} }
lut = PyByteArray_AsString(py_lut);
imgin = (Imaging) i0; imgin = (Imaging) i0;
if (imgin->type != IMAGING_TYPE_UINT8 && if (imgin->type != IMAGING_TYPE_UINT8 &&