Correct __getitem__ return type by radarhere · Pull Request #9264 · python-pillow/Pillow
| getpixel(Imaging im, ImagingAccess access, int x, int y) { | |
| union { | |
| UINT8 b[4]; | |
| UINT16 h; | |
| INT32 i; | |
| FLOAT32 f; | |
| } pixel; | |
| if (x < 0) { | |
| x = im->xsize + x; | |
| } | |
| if (y < 0) { | |
| y = im->ysize + y; | |
| } | |
| if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { | |
| PyErr_SetString(PyExc_IndexError, outside_image); | |
| return NULL; | |
| } | |
| access->get_pixel(im, x, y, &pixel); | |
| switch (im->type) { | |
| case IMAGING_TYPE_UINT8: | |
| switch (im->bands) { | |
| case 1: | |
| return PyLong_FromLong(pixel.b[0]); | |
| case 2: | |
| return Py_BuildValue("BB", pixel.b[0], pixel.b[1]); | |
| case 3: | |
| return Py_BuildValue("BBB", pixel.b[0], pixel.b[1], pixel.b[2]); | |
| case 4: | |
| return Py_BuildValue( | |
| "BBBB", pixel.b[0], pixel.b[1], pixel.b[2], pixel.b[3] | |
| ); | |
| } | |
| break; | |
| case IMAGING_TYPE_INT32: | |
| return PyLong_FromLong(pixel.i); | |
| case IMAGING_TYPE_FLOAT32: | |
| return PyFloat_FromDouble(pixel.f); | |
| case IMAGING_TYPE_SPECIAL: | |
| if (im->bands == 1) { | |
| return PyLong_FromLong(pixel.h); | |
| } else { | |
| return Py_BuildValue("BBB", pixel.b[0], pixel.b[1], pixel.b[2]); | |
| } | |
| break; | |
| } | |
| /* unknown type */ | |
| Py_RETURN_NONE; |