mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 18:06:18 +03:00
Merge pull request #1009 from wiredfool/putdata-1008
Fix for Image.putdata segfault
This commit is contained in:
commit
b5b0b889d3
|
@ -1,4 +1,5 @@
|
||||||
from helper import unittest, PillowTestCase, hopper
|
from helper import unittest, PillowTestCase, hopper
|
||||||
|
from array import array
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
@ -63,6 +64,25 @@ class TestImagePutData(PillowTestCase):
|
||||||
target = [2.0 * float(elt) + 256.0 for elt in data]
|
target = [2.0 * float(elt) + 256.0 for elt in data]
|
||||||
self.assertEqual(list(im.getdata()), target)
|
self.assertEqual(list(im.getdata()), target)
|
||||||
|
|
||||||
|
def test_array_B(self):
|
||||||
|
# shouldn't segfault
|
||||||
|
# see https://github.com/python-pillow/Pillow/issues/1008
|
||||||
|
|
||||||
|
arr = array('B', [0])*15000
|
||||||
|
im = Image.new('L', (150, 100))
|
||||||
|
im.putdata(arr)
|
||||||
|
|
||||||
|
self.assertEqual(len(im.getdata()),len(arr))
|
||||||
|
|
||||||
|
def test_array_F(self):
|
||||||
|
# shouldn't segfault
|
||||||
|
# see https://github.com/python-pillow/Pillow/issues/1008
|
||||||
|
|
||||||
|
im = Image.new('F', (150, 100))
|
||||||
|
arr = array('f', [0.0])*15000
|
||||||
|
im.putdata(arr)
|
||||||
|
|
||||||
|
self.assertEqual(len(im.getdata()),len(arr))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -121,6 +121,16 @@ class TestNumpy(PillowTestCase):
|
||||||
|
|
||||||
im.point(lut)
|
im.point(lut)
|
||||||
|
|
||||||
|
def test_putdata(self):
|
||||||
|
# shouldn't segfault
|
||||||
|
# see https://github.com/python-pillow/Pillow/issues/1008
|
||||||
|
|
||||||
|
im = Image.new('F', (150, 100))
|
||||||
|
arr = numpy.zeros((15000,), numpy.float32)
|
||||||
|
im.putdata(arr)
|
||||||
|
|
||||||
|
self.assertEqual(len(im.getdata()),len(arr))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
10
_imaging.c
10
_imaging.c
|
@ -1269,7 +1269,7 @@ _putdata(ImagingObject* self, PyObject* args)
|
||||||
if (scale == 1.0 && offset == 0.0) {
|
if (scale == 1.0 && offset == 0.0) {
|
||||||
/* Clipped data */
|
/* Clipped data */
|
||||||
for (i = x = y = 0; i < n; i++) {
|
for (i = x = y = 0; i < n; i++) {
|
||||||
op = PySequence_Fast_GET_ITEM(data, i);
|
op = PySequence_Fast_GET_ITEM(seq, 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++;
|
x = 0, y++;
|
||||||
|
@ -1279,7 +1279,7 @@ _putdata(ImagingObject* self, PyObject* args)
|
||||||
} else {
|
} else {
|
||||||
/* 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 = PySequence_Fast_GET_ITEM(data, i);
|
PyObject *op = PySequence_Fast_GET_ITEM(seq, 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){
|
||||||
|
@ -1299,7 +1299,7 @@ _putdata(ImagingObject* self, PyObject* args)
|
||||||
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++) {
|
||||||
op = PySequence_Fast_GET_ITEM(data, i);
|
op = PySequence_Fast_GET_ITEM(seq, 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);
|
||||||
if (++x >= (int) image->xsize){
|
if (++x >= (int) image->xsize){
|
||||||
|
@ -1310,7 +1310,7 @@ _putdata(ImagingObject* self, PyObject* args)
|
||||||
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++) {
|
||||||
op = PySequence_Fast_GET_ITEM(data, i);
|
op = PySequence_Fast_GET_ITEM(seq, 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);
|
||||||
if (++x >= (int) image->xsize){
|
if (++x >= (int) image->xsize){
|
||||||
|
@ -1326,7 +1326,7 @@ _putdata(ImagingObject* self, PyObject* args)
|
||||||
INT32 inkint;
|
INT32 inkint;
|
||||||
} u;
|
} u;
|
||||||
|
|
||||||
op = PySequence_Fast_GET_ITEM(data, i);
|
op = PySequence_Fast_GET_ITEM(seq, i);
|
||||||
if (!op || !getink(op, image, u.ink)) {
|
if (!op || !getink(op, image, u.ink)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user