236
241
/* -------------------------------------------------------------------- */
242
/* BUFFER HANDLING */
243
/* -------------------------------------------------------------------- */
244
/* Python compatibility API */
246
#if PY_VERSION_HEX < 0x02020000
248
int PyImaging_CheckBuffer(PyObject *buffer)
250
PyBufferProcs *procs = buffer->ob_type->tp_as_buffer;
251
if (procs && procs->bf_getreadbuffer && procs->bf_getsegcount &&
252
procs->bf_getsegcount(buffer, NULL) == 1)
257
int PyImaging_ReadBuffer(PyObject* buffer, const void** ptr)
259
PyBufferProcs *procs = buffer->ob_type->tp_as_buffer;
260
return procs->bf_getreadbuffer(buffer, 0, ptr);
265
int PyImaging_CheckBuffer(PyObject* buffer)
267
return PyObject_CheckReadBuffer(buffer);
270
int PyImaging_ReadBuffer(PyObject* buffer, const void** ptr)
272
/* must call check_buffer first! */
273
#if PY_VERSION_HEX < 0x02050000
278
PyObject_AsReadBuffer(buffer, ptr, &n);
284
/* -------------------------------------------------------------------- */
237
285
/* EXCEPTION REROUTING */
238
286
/* -------------------------------------------------------------------- */
413
464
static inline PyObject*
414
getpixel(Imaging im, int x, int y)
465
getpixel(Imaging im, ImagingAccess access, int x, int y)
418
474
if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
419
475
PyErr_SetString(PyExc_IndexError, outside_image);
424
if (im->image8 != NULL) {
425
p = (UINT8*) &im->image8[y][x];
427
case IMAGING_TYPE_UINT8:
428
return PyInt_FromLong(p[0]);
429
case IMAGING_TYPE_SPECIAL:
430
/* FIXME: are 16-bit images signed or unsigned??? */
431
if (strcmp(im->mode, "I;16") == 0) {
432
p = (UINT8*) &im->image8[y][x+x];
433
return PyInt_FromLong(L16(p, 0));
435
if (strcmp(im->mode, "I;16B") == 0) {
436
p = (UINT8*) &im->image8[y][x+x];
437
return PyInt_FromLong(B16(p, 0));
443
if (im->image32 != NULL) {
444
p = (UINT8*) &im->image32[y][x];
446
case IMAGING_TYPE_UINT8:
447
/* unsigned integer */
449
return Py_BuildValue("ii", p[0], p[3]);
451
return Py_BuildValue("iii", p[0], p[1], p[2]);
452
return Py_BuildValue("iiii", p[0], p[1], p[2], p[3]);
453
case IMAGING_TYPE_INT32:
455
return PyInt_FromLong(*(INT32*) p);
456
case IMAGING_TYPE_FLOAT32:
458
return PyFloat_FromDouble(*(FLOAT32*) p);
479
access->get_pixel(im, x, y, &pixel);
482
case IMAGING_TYPE_UINT8:
485
return PyInt_FromLong(pixel.b[0]);
487
return Py_BuildValue("ii", pixel.b[0], pixel.b[1]);
489
return Py_BuildValue("iii", pixel.b[0], pixel.b[1], pixel.b[2]);
491
return Py_BuildValue("iiii", pixel.b[0], pixel.b[1], pixel.b[2], pixel.b[3]);
494
case IMAGING_TYPE_INT32:
495
return PyInt_FromLong(pixel.i);
496
case IMAGING_TYPE_FLOAT32:
497
return PyFloat_FromDouble(pixel.f);
498
case IMAGING_TYPE_SPECIAL:
499
if (strncmp(im->mode, "I;16", 4) == 0)
500
return PyInt_FromLong(pixel.h);
462
504
/* unknown type */
463
505
Py_INCREF(Py_None);
473
515
/* fill ink buffer (four bytes) with something that can
474
516
be cast to either UINT8 or INT32 */
477
/* unsigned integer, single layer */
478
r = PyInt_AsLong(color);
479
if (r == -1 && PyErr_Occurred())
482
ink[1] = ink[2] = ink[3] = 0;
486
case IMAGING_TYPE_UINT8:
487
/* unsigned integer */
519
case IMAGING_TYPE_UINT8:
520
/* unsigned integer */
521
if (im->bands == 1) {
522
/* unsigned integer, single layer */
523
r = PyInt_AsLong(color);
524
if (r == -1 && PyErr_Occurred())
527
ink[1] = ink[2] = ink[3] = 0;
489
530
if (PyInt_Check(color)) {
490
531
r = PyInt_AS_LONG(color);
507
548
ink[1] = CLIP(g);
508
549
ink[2] = CLIP(b);
509
550
ink[3] = CLIP(a);
511
case IMAGING_TYPE_INT32:
553
case IMAGING_TYPE_INT32:
555
r = PyInt_AsLong(color);
556
if (r == -1 && PyErr_Occurred())
560
case IMAGING_TYPE_FLOAT32:
562
f = PyFloat_AsDouble(color);
563
if (f == -1.0 && PyErr_Occurred())
565
*(FLOAT32*) ink = (FLOAT32) f;
567
case IMAGING_TYPE_SPECIAL:
568
if (strncmp(im->mode, "I;16", 4) == 0) {
513
569
r = PyInt_AsLong(color);
514
570
if (r == -1 && PyErr_Occurred())
518
case IMAGING_TYPE_FLOAT32:
520
f = PyFloat_AsDouble(color);
521
if (f == -1.0 && PyErr_Occurred())
523
*(FLOAT32*) ink = (FLOAT32) f;
573
ink[1] = (UINT8) (r >> 8);
856
#ifdef WITH_UNSHARPMASK
858
_gaussian_blur(ImagingObject* self, PyObject* args)
864
if (!PyArg_ParseTuple(args, "f", &radius))
868
imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize);
872
if (!ImagingGaussianBlur(imIn, imOut, radius))
875
return PyImagingNew(imOut);
806
880
_getpalette(ImagingObject* self, PyObject* args)
1170
1249
if (PyList_Check(data)) {
1171
1250
for (i = x = y = 0; i < n; i++) {
1172
1251
PyObject *op = PyList_GET_ITEM(data, i);
1173
image->image8[y][x] = CLIP(PyInt_AsLong(op));
1252
image->image8[y][x] = (UINT8) CLIP(PyInt_AsLong(op));
1174
1253
if (++x >= (int) image->xsize)
1178
1257
for (i = x = y = 0; i < n; i++) {
1179
1258
PyObject *op = PySequence_GetItem(data, i);
1180
image->image8[y][x] = CLIP(PyInt_AsLong(op));
1259
image->image8[y][x] = (UINT8) CLIP(PyInt_AsLong(op));
1181
1260
Py_XDECREF(op);
1182
1261
if (++x >= (int) image->xsize)
1395
1472
imOut = ImagingNew(imIn->mode, xsize, ysize);
1397
ImagingResize(imOut, imIn, filter);
1474
(void) ImagingResize(imOut, imIn, filter);
1399
1476
return PyImagingNew(imOut);
1419
1496
if (filter && imIn->type != IMAGING_TYPE_SPECIAL) {
1420
1497
/* Rotate with resampling filter */
1421
1498
imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize);
1422
ImagingRotate(imOut, imIn, theta, filter);
1499
(void) ImagingRotate(imOut, imIn, theta, filter);
1423
1500
} else if (theta == 90.0 || theta == 270.0) {
1424
1501
/* Use fast version */
1425
1502
imOut = ImagingNew(imIn->mode, imIn->ysize, imIn->xsize);
1427
1504
if (theta == 90.0)
1428
ImagingRotate90(imOut, imIn);
1505
(void) ImagingRotate90(imOut, imIn);
1430
ImagingRotate270(imOut, imIn);
1507
(void) ImagingRotate270(imOut, imIn);
1433
1510
imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize);
1435
1512
if (theta == 0.0)
1436
1513
/* No rotation: simply copy the input image */
1437
ImagingCopy2(imOut, imIn);
1514
(void) ImagingCopy2(imOut, imIn);
1438
1515
else if (theta == 180.0)
1439
1516
/* Use fast version */
1440
ImagingRotate180(imOut, imIn);
1517
(void) ImagingRotate180(imOut, imIn);
1442
1519
/* Use ordinary version */
1443
ImagingRotate(imOut, imIn, theta, 0);
1520
(void) ImagingRotate(imOut, imIn, theta, 0);
1474
1550
strcpy(im->mode, mode);
1475
1551
im->bands = modelen;
1476
1552
if (!strcmp(mode, "RGBA"))
1477
ImagingFillBand(im, 3, 255);
1479
return ImagingError_ModeError();
1553
(void) ImagingFillBand(im, 3, 255);
1555
/* trying doing an in-place conversion */
1556
if (!ImagingConvertInPlace(im, mode))
1561
ImagingAccessDelete(im, self->access);
1562
self->access = ImagingAccessNew(im);
1481
1564
Py_INCREF(Py_None);
1482
1565
return Py_None;
1636
ImagingFlipLeftRight(imOut, imIn);
1719
(void) ImagingFlipLeftRight(imOut, imIn);
1639
ImagingFlipTopBottom(imOut, imIn);
1722
(void) ImagingFlipTopBottom(imOut, imIn);
1642
ImagingRotate90(imOut, imIn);
1725
(void) ImagingRotate90(imOut, imIn);
1645
ImagingRotate180(imOut, imIn);
1728
(void) ImagingRotate180(imOut, imIn);
1648
ImagingRotate270(imOut, imIn);
1731
(void) ImagingRotate270(imOut, imIn);
1652
1735
return PyImagingNew(imOut);
1738
#ifdef WITH_UNSHARPMASK
1740
_unsharp_mask(ImagingObject* self, PyObject* args)
1746
int percent, threshold;
1747
if (!PyArg_ParseTuple(args, "fii", &radius, &percent, &threshold))
1752
imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize);
1756
if (!ImagingUnsharpMask(imIn, imOut, radius, percent, threshold))
1759
return PyImagingNew(imOut);
1655
1763
/* -------------------------------------------------------------------- */
1657
1765
static PyObject*
1695
1803
for (i = 0; i < colors; i++) {
1696
1804
ImagingColorItem* v = &items[i];
1697
1805
PyObject* item = Py_BuildValue(
1698
"iN", v->count, getpixel(self->image, v->x, v->y)
1806
"iN", v->count, getpixel(self->image, self->access, v->x, v->y)
1700
1808
PyList_SetItem(out, i, item);
2716
2820
switch (status) {
2717
2821
case IMAGING_CODEC_OVERRUN:
2718
msg = "buffer overrun."; break;
2822
msg = "buffer overrun"; break;
2719
2823
case IMAGING_CODEC_BROKEN:
2720
msg = "broken data stream."; break;
2824
msg = "broken data stream"; break;
2721
2825
case IMAGING_CODEC_UNKNOWN:
2722
msg = "unrecognized data stream contents."; break;
2826
msg = "unrecognized data stream contents"; break;
2723
2827
case IMAGING_CODEC_CONFIG:
2724
msg = "codec configuration error."; break;
2828
msg = "codec configuration error"; break;
2725
2829
case IMAGING_CODEC_MEMORY:
2726
msg = "out of memory."; break;
2830
msg = "out of memory"; break;
2728
2832
Py_INCREF(Py_None);
2729
2833
return Py_None;
2835
2939
{"chop_xor", (PyCFunction)_chop_xor, 1},
2942
#ifdef WITH_UNSHARPMASK
2943
/* Kevin Cazabon's unsharpmask extension */
2944
{"gaussian_blur", (PyCFunction)_gaussian_blur, 1},
2945
{"unsharp_mask", (PyCFunction)_unsharp_mask, 1},
2838
2948
#ifdef WITH_EFFECTS
2839
2949
/* Special effects */
2840
2950
{"effect_spread", (PyCFunction)_effect_spread, 1},
2901
3011
x = y = 0; /* leave it to getpixel to raise an exception */
2903
return getpixel(im, x, y);
3013
return getpixel(im, self->access, x, y);
2906
3016
static PySequenceMethods image_as_sequence = {
2907
(inquiry)image_length, /*sq_length*/
2908
(binaryfunc)0, /*sq_concat*/
2909
(ssizeargfunc)0, /*sq_repeat*/
2910
(ssizeargfunc)image_item, /*sq_item*/
2911
(ssizessizeargfunc)0, /*sq_slice*/
2912
(ssizeobjargproc)0, /*sq_ass_item*/
2913
(ssizessizeobjargproc)0, /*sq_ass_slice*/
3017
(inquiry) image_length, /*sq_length*/
3018
(binaryfunc) NULL, /*sq_concat*/
3019
(ssizeargfunc) NULL, /*sq_repeat*/
3020
(ssizeargfunc) image_item, /*sq_item*/
3021
(ssizessizeargfunc) NULL, /*sq_slice*/
3022
(ssizeobjargproc) NULL, /*sq_ass_item*/
3023
(ssizessizeobjargproc) NULL, /*sq_ass_slice*/
2966
3076
static PyMappingMethods pixel_access_as_mapping = {
2967
(inquiry)0, /*mp_length*/
2968
(binaryfunc)pixel_access_getitem, /*mp_subscript*/
2969
(objobjargproc)pixel_access_setitem, /*mp_ass_subscript*/
3077
(inquiry) NULL, /*mp_length*/
3078
(binaryfunc) pixel_access_getitem, /*mp_subscript*/
3079
(objobjargproc) pixel_access_setitem, /*mp_ass_subscript*/
2972
3082
/* type description */
2990
3100
/* -------------------------------------------------------------------- */
2992
3102
/* FIXME: this is something of a mess. Should replace this with
2993
pluggable codecs, but not before PIL 1.1 */
3103
pluggable codecs, but not before PIL 1.2 */
2995
3105
/* Decoders (in decode.c) */
2996
3106
extern PyObject* PyImaging_BitDecoderNew(PyObject* self, PyObject* args);
3018
3128
extern PyObject* PyImaging_XbmEncoderNew(PyObject* self, PyObject* args);
3019
3129
extern PyObject* PyImaging_ZipEncoderNew(PyObject* self, PyObject* args);
3021
/* Display support (in display.c) */
3131
/* Display support etc (in display.c) */
3023
3133
extern PyObject* PyImaging_CreateWindowWin32(PyObject* self, PyObject* args);
3024
3134
extern PyObject* PyImaging_DisplayWin32(PyObject* self, PyObject* args);
3027
3137
extern PyObject* PyImaging_GrabClipboardWin32(PyObject* self, PyObject* args);
3028
3138
extern PyObject* PyImaging_ListWindowsWin32(PyObject* self, PyObject* args);
3029
3139
extern PyObject* PyImaging_EventLoopWin32(PyObject* self, PyObject* args);
3140
extern PyObject* PyImaging_DrawWmf(PyObject* self, PyObject* args);
3032
3143
/* Experimental path stuff (in path.c) */
3097
3208
{"createwindow", (PyCFunction)PyImaging_CreateWindowWin32, 1},
3098
3209
{"eventloop", (PyCFunction)PyImaging_EventLoopWin32, 1},
3099
3210
{"listwindows", (PyCFunction)PyImaging_ListWindowsWin32, 1},
3211
{"drawwmf", (PyCFunction)PyImaging_DrawWmf, 1},
3102
3214
/* Utilities */
3146
3261
PixelAccess_Type.ob_type = &PyType_Type;
3148
Py_InitModule("_imaging", functions);
3263
ImagingAccessInit();
3265
m = Py_InitModule("_imaging", functions);
3266
d = PyModule_GetDict(m);
3270
extern const char* ImagingJpegVersion(void);
3271
PyDict_SetItemString(d, "jpeglib_version", PyString_FromString(ImagingJpegVersion()));
3277
extern const char* ImagingZipVersion(void);
3278
PyDict_SetItemString(d, "zlib_version", PyString_FromString(ImagingZipVersion()));