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
This commit is contained in:
wiredfool 2014-07-23 15:38:11 -07:00
parent 06d21bc709
commit c9c80f9da5

View File

@ -1220,6 +1220,8 @@ _putdata(ImagingObject* self, PyObject* args)
Py_ssize_t n, i, x, y; Py_ssize_t n, i, x, y;
PyObject* data; PyObject* data;
PyObject* seq;
PyObject* op;
double scale = 1.0; double scale = 1.0;
double offset = 0.0; double offset = 0.0;
@ -1259,41 +1261,28 @@ _putdata(ImagingObject* self, PyObject* args)
x = 0, y++; x = 0, y++;
} }
} else { } else {
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) { if (scale == 1.0 && offset == 0.0) {
/* Clipped data */ /* Clipped data */
if (PyList_Check(data)) {
for (i = x = y = 0; i < n; i++) { for (i = x = y = 0; i < n; i++) {
PyObject *op = PyList_GET_ITEM(data, i); op = PySequence_Fast_GET_ITEM(data, i);
image->image8[y][x] = (UINT8) CLIP(PyInt_AsLong(op)); image->image8[y][x] = (UINT8) CLIP(PyInt_AsLong(op));
if (++x >= (int) image->xsize) 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++; x = 0, y++;
} }
} }
} else { } else {
if (PyList_Check(data)) {
/* Scaled and clipped data */ /* Scaled and clipped data */
for (i = x = y = 0; i < n; i++) { for (i = x = y = 0; i < n; i++) {
PyObject *op = PyList_GET_ITEM(data, i); PyObject *op = PySequence_Fast_GET_ITEM(data, i);
image->image8[y][x] = CLIP( image->image8[y][x] = CLIP(
(int) (PyFloat_AsDouble(op) * scale + offset)); (int) (PyFloat_AsDouble(op) * scale + offset));
if (++x >= (int) image->xsize) 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++; x = 0, y++;
} }
} }
@ -1302,27 +1291,32 @@ _putdata(ImagingObject* self, PyObject* args)
} }
} else { } else {
/* 32-bit images */ /* 32-bit images */
seq = PySequence_Fast(data, must_be_sequence);
if (!seq) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
switch (image->type) { switch (image->type) {
case IMAGING_TYPE_INT32: case IMAGING_TYPE_INT32:
for (i = x = y = 0; i < n; i++) { 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) = IMAGING_PIXEL_INT32(image, x, y) =
(INT32) (PyFloat_AsDouble(op) * scale + offset); (INT32) (PyFloat_AsDouble(op) * scale + offset);
Py_XDECREF(op); if (++x >= (int) image->xsize){
if (++x >= (int) image->xsize)
x = 0, y++; x = 0, y++;
} }
}
PyErr_Clear(); /* Avoid weird exceptions */ PyErr_Clear(); /* Avoid weird exceptions */
break; break;
case IMAGING_TYPE_FLOAT32: case IMAGING_TYPE_FLOAT32:
for (i = x = y = 0; i < n; i++) { 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) = IMAGING_PIXEL_FLOAT32(image, x, y) =
(FLOAT32) (PyFloat_AsDouble(op) * scale + offset); (FLOAT32) (PyFloat_AsDouble(op) * scale + offset);
Py_XDECREF(op); if (++x >= (int) image->xsize){
if (++x >= (int) image->xsize)
x = 0, y++; x = 0, y++;
} }
}
PyErr_Clear(); /* Avoid weird exceptions */ PyErr_Clear(); /* Avoid weird exceptions */
break; break;
default: default:
@ -1332,17 +1326,16 @@ _putdata(ImagingObject* self, PyObject* args)
INT32 inkint; INT32 inkint;
} u; } u;
PyObject *op = PySequence_GetItem(data, i); op = PySequence_Fast_GET_ITEM(data, i);
if (!op || !getink(op, image, u.ink)) { if (!op || !getink(op, image, u.ink)) {
Py_DECREF(op);
return NULL; return NULL;
} }
/* FIXME: what about scale and offset? */ /* FIXME: what about scale and offset? */
image->image32[y][x] = u.inkint; image->image32[y][x] = u.inkint;
Py_XDECREF(op); if (++x >= (int) image->xsize){
if (++x >= (int) image->xsize)
x = 0, y++; x = 0, y++;
} }
}
PyErr_Clear(); /* Avoid weird exceptions */ PyErr_Clear(); /* Avoid weird exceptions */
break; break;
} }