From c9c80f9da5fe120181deb970e43efef903156bda Mon Sep 17 00:00:00 2001 From: wiredfool Date: Wed, 23 Jul 2014 15:38:11 -0700 Subject: [PATCH] Use PySequence_Fast for Image.putdata Pre-Commit: $ NOSE_PROCESSES=0 time ./test-installed.py Tests/test_image_putdata.py ... ---------------------------------------------------------------------- Ran 3 tests in 131.623s OK 131.77user 0.18system 2:14.04elapsed 98%CPU (0avgtext+0avgdata 325632maxresident)k 87376inputs+8outputs (314major+47333minor)pagefaults 0swaps Post: $ NOSE_PROCESSES=0 time ./test-installed.py Tests/test_image_putdata.py ... ---------------------------------------------------------------------- Ran 3 tests in 0.534s OK 0.77user 0.05system 0:00.83elapsed 99%CPU (0avgtext+0avgdata 296352maxresident)k 0inputs+0outputs (0major+21462minor)pagefaults 0swaps --- _imaging.c | 91 +++++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/_imaging.c b/_imaging.c index 7c55f633b..c28bd4d93 100644 --- a/_imaging.c +++ b/_imaging.c @@ -1220,6 +1220,8 @@ _putdata(ImagingObject* self, PyObject* args) Py_ssize_t n, i, x, y; PyObject* data; + PyObject* seq; + PyObject* op; double scale = 1.0; double offset = 0.0; @@ -1259,69 +1261,61 @@ _putdata(ImagingObject* self, PyObject* args) x = 0, y++; } } else { - if (scale == 1.0 && offset == 0.0) { - /* Clipped data */ - if (PyList_Check(data)) { - for (i = x = y = 0; i < n; i++) { - PyObject *op = PyList_GET_ITEM(data, i); - image->image8[y][x] = (UINT8) CLIP(PyInt_AsLong(op)); - if (++x >= (int) image->xsize) - x = 0, y++; - } - } else { - for (i = x = y = 0; i < n; i++) { - PyObject *op = PySequence_GetItem(data, i); - image->image8[y][x] = (UINT8) CLIP(PyInt_AsLong(op)); - Py_XDECREF(op); - if (++x >= (int) image->xsize) - x = 0, y++; - } - } + seq = PySequence_Fast(data, must_be_sequence); + if (!seq) { + PyErr_SetString(PyExc_TypeError, must_be_sequence); + return NULL; + } + if (scale == 1.0 && offset == 0.0) { + /* Clipped data */ + for (i = x = y = 0; i < n; i++) { + op = PySequence_Fast_GET_ITEM(data, i); + image->image8[y][x] = (UINT8) CLIP(PyInt_AsLong(op)); + if (++x >= (int) image->xsize){ + x = 0, y++; + } + } + } else { - if (PyList_Check(data)) { - /* Scaled and clipped data */ - for (i = x = y = 0; i < n; i++) { - PyObject *op = PyList_GET_ITEM(data, i); - image->image8[y][x] = CLIP( - (int) (PyFloat_AsDouble(op) * scale + offset)); - if (++x >= (int) image->xsize) - x = 0, y++; - } - } else { - for (i = x = y = 0; i < n; i++) { - PyObject *op = PySequence_GetItem(data, i); - image->image8[y][x] = CLIP( - (int) (PyFloat_AsDouble(op) * scale + offset)); - Py_XDECREF(op); - if (++x >= (int) image->xsize) - x = 0, y++; - } - } - } - PyErr_Clear(); /* Avoid weird exceptions */ + /* Scaled and clipped data */ + for (i = x = y = 0; i < n; i++) { + PyObject *op = PySequence_Fast_GET_ITEM(data, i); + image->image8[y][x] = CLIP( + (int) (PyFloat_AsDouble(op) * scale + offset)); + if (++x >= (int) image->xsize){ + x = 0, y++; + } + } + } + PyErr_Clear(); /* Avoid weird exceptions */ } } else { /* 32-bit images */ + seq = PySequence_Fast(data, must_be_sequence); + if (!seq) { + PyErr_SetString(PyExc_TypeError, must_be_sequence); + return NULL; + } switch (image->type) { case IMAGING_TYPE_INT32: for (i = x = y = 0; i < n; i++) { - PyObject *op = PySequence_GetItem(data, i); + op = PySequence_Fast_GET_ITEM(data, i); IMAGING_PIXEL_INT32(image, x, y) = (INT32) (PyFloat_AsDouble(op) * scale + offset); - Py_XDECREF(op); - if (++x >= (int) image->xsize) + if (++x >= (int) image->xsize){ x = 0, y++; + } } PyErr_Clear(); /* Avoid weird exceptions */ break; case IMAGING_TYPE_FLOAT32: for (i = x = y = 0; i < n; i++) { - PyObject *op = PySequence_GetItem(data, i); + op = PySequence_Fast_GET_ITEM(data, i); IMAGING_PIXEL_FLOAT32(image, x, y) = (FLOAT32) (PyFloat_AsDouble(op) * scale + offset); - Py_XDECREF(op); - if (++x >= (int) image->xsize) + if (++x >= (int) image->xsize){ x = 0, y++; + } } PyErr_Clear(); /* Avoid weird exceptions */ break; @@ -1332,16 +1326,15 @@ _putdata(ImagingObject* self, PyObject* args) INT32 inkint; } u; - PyObject *op = PySequence_GetItem(data, i); + op = PySequence_Fast_GET_ITEM(data, i); if (!op || !getink(op, image, u.ink)) { - Py_DECREF(op); return NULL; } /* FIXME: what about scale and offset? */ image->image32[y][x] = u.inkint; - Py_XDECREF(op); - if (++x >= (int) image->xsize) + if (++x >= (int) image->xsize){ x = 0, y++; + } } PyErr_Clear(); /* Avoid weird exceptions */ break;