forked from python-pillow/Pillow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_imaging.c
3547 lines (2927 loc) · 92.8 KB
/
_imaging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The Python Imaging Library.
*
* the imaging library bindings
*
* history:
* 1995-09-24 fl Created
* 1996-03-24 fl Ready for first public release (release 0.0)
* 1996-03-25 fl Added fromstring (for Jack's "img" library)
* 1996-03-28 fl Added channel operations
* 1996-03-31 fl Added point operation
* 1996-04-08 fl Added new/new_block/new_array factories
* 1996-04-13 fl Added decoders
* 1996-05-04 fl Added palette hack
* 1996-05-12 fl Compile cleanly as C++
* 1996-05-19 fl Added matrix conversions, gradient fills
* 1996-05-27 fl Added display_mode
* 1996-07-22 fl Added getbbox, offset
* 1996-07-23 fl Added sequence semantics
* 1996-08-13 fl Added logical operators, point mode
* 1996-08-16 fl Modified paste interface
* 1996-09-06 fl Added putdata methods, use abstract interface
* 1996-11-01 fl Added xbm encoder
* 1996-11-04 fl Added experimental path stuff, draw_lines, etc
* 1996-12-10 fl Added zip decoder, crc32 interface
* 1996-12-14 fl Added modulo arithmetics
* 1996-12-29 fl Added zip encoder
* 1997-01-03 fl Added fli and msp decoders
* 1997-01-04 fl Added experimental sun_rle and tga_rle decoders
* 1997-01-05 fl Added gif encoder, getpalette hack
* 1997-02-23 fl Added histogram mask
* 1997-05-12 fl Minor tweaks to match the IFUNC95 interface
* 1997-05-21 fl Added noise generator, spread effect
* 1997-06-05 fl Added mandelbrot generator
* 1997-08-02 fl Modified putpalette to coerce image mode if necessary
* 1998-01-11 fl Added INT32 support
* 1998-01-22 fl Fixed draw_points to draw the last point too
* 1998-06-28 fl Added getpixel, getink, draw_ink
* 1998-07-12 fl Added getextrema
* 1998-07-17 fl Added point conversion to arbitrary formats
* 1998-09-21 fl Added support for resampling filters
* 1998-09-22 fl Added support for quad transform
* 1998-12-29 fl Added support for arcs, chords, and pieslices
* 1999-01-10 fl Added some experimental arrow graphics stuff
* 1999-02-06 fl Added draw_bitmap, font acceleration stuff
* 2001-04-17 fl Fixed some egcs compiler nits
* 2001-09-17 fl Added screen grab primitives (win32)
* 2002-03-09 fl Added stretch primitive
* 2002-03-10 fl Fixed filter handling in rotate
* 2002-06-06 fl Added I, F, and RGB support to putdata
* 2002-06-08 fl Added rankfilter
* 2002-06-09 fl Added support for user-defined filter kernels
* 2002-11-19 fl Added clipboard grab primitives (win32)
* 2002-12-11 fl Added draw context
* 2003-04-26 fl Tweaks for Python 2.3 beta 1
* 2003-05-21 fl Added createwindow primitive (win32)
* 2003-09-13 fl Added thread section hooks
* 2003-09-15 fl Added expand helper
* 2003-09-26 fl Added experimental LA support
* 2004-02-21 fl Handle zero-size images in quantize
* 2004-06-05 fl Added ptr attribute (used to access Imaging objects)
* 2004-06-05 fl Don't crash when fetching pixels from zero-wide images
* 2004-09-17 fl Added getcolors
* 2004-10-04 fl Added modefilter
* 2005-10-02 fl Added access proxy
* 2006-06-18 fl Always draw last point in polyline
*
* Copyright (c) 1997-2006 by Secret Labs AB
* Copyright (c) 1995-2006 by Fredrik Lundh
*
* See the README file for information on usage and redistribution.
*/
#include "Python.h"
#ifdef HAVE_LIBZ
#include "zlib.h"
#endif
#include "Imaging.h"
#include "py3.h"
/* Configuration stuff. Feel free to undef things you don't need. */
#define WITH_IMAGECHOPS /* ImageChops support */
#define WITH_IMAGEDRAW /* ImageDraw support */
#define WITH_MAPPING /* use memory mapping to read some file formats */
#define WITH_IMAGEPATH /* ImagePath stuff */
#define WITH_ARROW /* arrow graphics stuff (experimental) */
#define WITH_EFFECTS /* special effects */
#define WITH_QUANTIZE /* quantization support */
#define WITH_RANKFILTER /* rank filter */
#define WITH_MODEFILTER /* mode filter */
#define WITH_THREADING /* "friendly" threading support */
#define WITH_UNSHARPMASK /* Kevin Cazabon's unsharpmask module */
#undef VERBOSE
#define CLIP(x) ((x) <= 0 ? 0 : (x) < 256 ? (x) : 255)
#define B16(p, i) ((((int)p[(i)]) << 8) + p[(i)+1])
#define L16(p, i) ((((int)p[(i)+1]) << 8) + p[(i)])
#define S16(v) ((v) < 32768 ? (v) : ((v) - 65536))
/* -------------------------------------------------------------------- */
/* OBJECT ADMINISTRATION */
/* -------------------------------------------------------------------- */
typedef struct {
PyObject_HEAD
Imaging image;
ImagingAccess access;
} ImagingObject;
static PyTypeObject Imaging_Type;
#ifdef WITH_IMAGEDRAW
typedef struct
{
/* to write a character, cut out sxy from glyph data, place
at current position plus dxy, and advance by (dx, dy) */
int dx, dy;
int dx0, dy0, dx1, dy1;
int sx0, sy0, sx1, sy1;
} Glyph;
typedef struct {
PyObject_HEAD
ImagingObject* ref;
Imaging bitmap;
int ysize;
int baseline;
Glyph glyphs[256];
} ImagingFontObject;
static PyTypeObject ImagingFont_Type;
typedef struct {
PyObject_HEAD
ImagingObject* image;
UINT8 ink[4];
int blend;
} ImagingDrawObject;
static PyTypeObject ImagingDraw_Type;
#endif
typedef struct {
PyObject_HEAD
ImagingObject* image;
int readonly;
} PixelAccessObject;
static PyTypeObject PixelAccess_Type;
PyObject*
PyImagingNew(Imaging imOut)
{
ImagingObject* imagep;
if (!imOut)
return NULL;
imagep = PyObject_New(ImagingObject, &Imaging_Type);
if (imagep == NULL) {
ImagingDelete(imOut);
return NULL;
}
#ifdef VERBOSE
printf("imaging %p allocated\n", imagep);
#endif
imagep->image = imOut;
imagep->access = ImagingAccessNew(imOut);
return (PyObject*) imagep;
}
static void
_dealloc(ImagingObject* imagep)
{
#ifdef VERBOSE
printf("imaging %p deleted\n", imagep);
#endif
if (imagep->access)
ImagingAccessDelete(imagep->image, imagep->access);
ImagingDelete(imagep->image);
PyObject_Del(imagep);
}
#define PyImaging_Check(op) (Py_TYPE(op) == &Imaging_Type)
Imaging PyImaging_AsImaging(PyObject *op)
{
if (!PyImaging_Check(op)) {
PyErr_BadInternalCall();
return NULL;
}
return ((ImagingObject *)op)->image;
}
/* -------------------------------------------------------------------- */
/* THREAD HANDLING */
/* -------------------------------------------------------------------- */
void ImagingSectionEnter(ImagingSectionCookie* cookie)
{
#ifdef WITH_THREADING
*cookie = (PyThreadState *) PyEval_SaveThread();
#endif
}
void ImagingSectionLeave(ImagingSectionCookie* cookie)
{
#ifdef WITH_THREADING
PyEval_RestoreThread((PyThreadState*) *cookie);
#endif
}
/* -------------------------------------------------------------------- */
/* BUFFER HANDLING */
/* -------------------------------------------------------------------- */
/* Python compatibility API */
int PyImaging_CheckBuffer(PyObject* buffer)
{
#if PY_VERSION_HEX >= 0x03000000
return PyObject_CheckBuffer(buffer);
#else
return PyObject_CheckBuffer(buffer) || PyObject_CheckReadBuffer(buffer);
#endif
}
int PyImaging_GetBuffer(PyObject* buffer, Py_buffer *view)
{
/* must call check_buffer first! */
#if PY_VERSION_HEX >= 0x03000000
return PyObject_GetBuffer(buffer, view, PyBUF_SIMPLE);
#else
/* Use new buffer protocol if available
(mmap doesn't support this in 2.7, go figure) */
if (PyObject_CheckBuffer(buffer)) {
int success = PyObject_GetBuffer(buffer, view, PyBUF_SIMPLE);
if (!success) { return success; }
PyErr_Clear();
}
/* Pretend we support the new protocol; PyBuffer_Release happily ignores
calling bf_releasebuffer on objects that don't support it */
view->buf = NULL;
view->len = 0;
view->readonly = 1;
view->format = NULL;
view->ndim = 0;
view->shape = NULL;
view->strides = NULL;
view->suboffsets = NULL;
view->itemsize = 0;
view->internal = NULL;
Py_INCREF(buffer);
view->obj = buffer;
return PyObject_AsReadBuffer(buffer, (void *) &view->buf, &view->len);
#endif
}
/* -------------------------------------------------------------------- */
/* EXCEPTION REROUTING */
/* -------------------------------------------------------------------- */
/* error messages */
static const char* must_be_sequence = "argument must be a sequence";
static const char* must_be_two_coordinates =
"coordinate list must contain exactly 2 coordinates";
static const char* wrong_mode = "unrecognized image mode";
static const char* wrong_raw_mode = "unrecognized raw mode";
static const char* outside_image = "image index out of range";
static const char* outside_palette = "palette index out of range";
static const char* wrong_palette_size = "invalid palette size";
static const char* no_palette = "image has no palette";
static const char* readonly = "image is readonly";
/* static const char* no_content = "image has no content"; */
void *
ImagingError_IOError(void)
{
PyErr_SetString(PyExc_IOError, "error when accessing file");
return NULL;
}
void *
ImagingError_MemoryError(void)
{
return PyErr_NoMemory();
}
void *
ImagingError_Mismatch(void)
{
PyErr_SetString(PyExc_ValueError, "images do not match");
return NULL;
}
void *
ImagingError_ModeError(void)
{
PyErr_SetString(PyExc_ValueError, "image has wrong mode");
return NULL;
}
void *
ImagingError_ValueError(const char *message)
{
PyErr_SetString(
PyExc_ValueError,
(message) ? (char*) message : "unrecognized argument value"
);
return NULL;
}
void
ImagingError_Clear(void)
{
PyErr_Clear();
}
/* -------------------------------------------------------------------- */
/* HELPERS */
/* -------------------------------------------------------------------- */
static int
getbands(const char* mode)
{
Imaging im;
int bands;
/* FIXME: add primitive to libImaging to avoid extra allocation */
im = ImagingNew(mode, 0, 0);
if (!im)
return -1;
bands = im->bands;
ImagingDelete(im);
return bands;
}
#define TYPE_UINT8 (0x100|sizeof(UINT8))
#define TYPE_INT32 (0x200|sizeof(INT32))
#define TYPE_FLOAT32 (0x300|sizeof(FLOAT32))
#define TYPE_DOUBLE (0x400|sizeof(double))
static void*
getlist(PyObject* arg, Py_ssize_t* length, const char* wrong_length, int type)
{
/* - allocates and returns a c array of the items in the
python sequence arg.
- the size of the returned array is in length
- all of the arg items must be numeric items of the type
specified in type
- sequence length is checked against the length parameter IF
an error parameter is passed in wrong_length
- caller is responsible for freeing the memory
*/
Py_ssize_t i, n;
int itemp;
double dtemp;
void* list;
PyObject* seq;
PyObject* op;
if (!PySequence_Check(arg)) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
n = PyObject_Length(arg);
if (length && wrong_length && n != *length) {
PyErr_SetString(PyExc_ValueError, wrong_length);
return NULL;
}
/* malloc check ok, type & ff is just a sizeof(something)
calloc checks for overflow */
list = calloc(n, type & 0xff);
if (!list)
return PyErr_NoMemory();
seq = PySequence_Fast(arg, must_be_sequence);
if (!seq) {
free(list);
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
for (i = 0; i < n; i++) {
op = PySequence_Fast_GET_ITEM(seq, i);
// DRY, branch prediction is going to work _really_ well
// on this switch. And 3 fewer loops to copy/paste.
switch (type) {
case TYPE_UINT8:
itemp = PyInt_AsLong(op);
((UINT8*)list)[i] = CLIP(itemp);
break;
case TYPE_INT32:
itemp = PyInt_AsLong(op);
((INT32*)list)[i] = itemp;
break;
case TYPE_FLOAT32:
dtemp = PyFloat_AsDouble(op);
((FLOAT32*)list)[i] = (FLOAT32) dtemp;
break;
case TYPE_DOUBLE:
dtemp = PyFloat_AsDouble(op);
((double*)list)[i] = (double) dtemp;
break;
}
}
if (length)
*length = n;
PyErr_Clear();
Py_DECREF(seq);
return list;
}
static inline PyObject*
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 || 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 PyInt_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 PyInt_FromLong(pixel.i);
case IMAGING_TYPE_FLOAT32:
return PyFloat_FromDouble(pixel.f);
case IMAGING_TYPE_SPECIAL:
if (strncmp(im->mode, "I;16", 4) == 0)
return PyInt_FromLong(pixel.h);
break;
}
/* unknown type */
Py_INCREF(Py_None);
return Py_None;
}
static char*
getink(PyObject* color, Imaging im, char* ink)
{
int g=0, b=0, a=0;
double f=0;
/* Windows 64 bit longs are 32 bits, and 0xFFFFFFFF (white) is a
python long (not int) that raises an overflow error when trying
to return it into a 32 bit C long
*/
PY_LONG_LONG r = 0;
/* fill ink buffer (four bytes) with something that can
be cast to either UINT8 or INT32 */
int rIsInt = 0;
if (im->type == IMAGING_TYPE_UINT8 ||
im->type == IMAGING_TYPE_INT32 ||
im->type == IMAGING_TYPE_SPECIAL) {
#if PY_VERSION_HEX >= 0x03000000
if (PyLong_Check(color)) {
r = PyLong_AsLongLong(color);
#else
if (PyInt_Check(color) || PyLong_Check(color)) {
if (PyInt_Check(color))
r = PyInt_AS_LONG(color);
else
r = PyLong_AsLongLong(color);
#endif
rIsInt = 1;
}
if (r == -1 && PyErr_Occurred()) {
rIsInt = 0;
}
}
switch (im->type) {
case IMAGING_TYPE_UINT8:
/* unsigned integer */
if (im->bands == 1) {
/* unsigned integer, single layer */
if (rIsInt != 1) {
if (!PyArg_ParseTuple(color, "L", &r)) {
return NULL;
}
}
ink[0] = CLIP(r);
ink[1] = ink[2] = ink[3] = 0;
} else {
a = 255;
if (rIsInt) {
/* compatibility: ABGR */
a = (UINT8) (r >> 24);
b = (UINT8) (r >> 16);
g = (UINT8) (r >> 8);
r = (UINT8) r;
} else {
if (im->bands == 2) {
if (!PyArg_ParseTuple(color, "L|i", &r, &a))
return NULL;
g = b = r;
} else {
if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a))
return NULL;
}
}
ink[0] = CLIP(r);
ink[1] = CLIP(g);
ink[2] = CLIP(b);
ink[3] = CLIP(a);
}
return ink;
case IMAGING_TYPE_INT32:
/* signed integer */
if (rIsInt != 1)
return NULL;
*(INT32*) ink = r;
return ink;
case IMAGING_TYPE_FLOAT32:
/* floating point */
f = PyFloat_AsDouble(color);
if (f == -1.0 && PyErr_Occurred())
return NULL;
*(FLOAT32*) ink = (FLOAT32) f;
return ink;
case IMAGING_TYPE_SPECIAL:
if (strncmp(im->mode, "I;16", 4) == 0) {
if (rIsInt != 1)
return NULL;
ink[0] = (UINT8) r;
ink[1] = (UINT8) (r >> 8);
ink[2] = ink[3] = 0;
return ink;
}
}
PyErr_SetString(PyExc_ValueError, wrong_mode);
return NULL;
}
/* -------------------------------------------------------------------- */
/* FACTORIES */
/* -------------------------------------------------------------------- */
static PyObject*
_fill(PyObject* self, PyObject* args)
{
char* mode;
int xsize, ysize;
PyObject* color;
char buffer[4];
Imaging im;
xsize = ysize = 256;
color = NULL;
if (!PyArg_ParseTuple(args, "s|(ii)O", &mode, &xsize, &ysize, &color))
return NULL;
im = ImagingNewDirty(mode, xsize, ysize);
if (!im)
return NULL;
buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0;
if (color) {
if (!getink(color, im, buffer)) {
ImagingDelete(im);
return NULL;
}
}
(void) ImagingFill(im, buffer);
return PyImagingNew(im);
}
static PyObject*
_new(PyObject* self, PyObject* args)
{
char* mode;
int xsize, ysize;
if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize))
return NULL;
return PyImagingNew(ImagingNew(mode, xsize, ysize));
}
static PyObject*
_new_block(PyObject* self, PyObject* args)
{
char* mode;
int xsize, ysize;
if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize))
return NULL;
return PyImagingNew(ImagingNewBlock(mode, xsize, ysize));
}
static PyObject*
_getcount(PyObject* self, PyObject* args)
{
if (!PyArg_ParseTuple(args, ":getcount"))
return NULL;
return PyInt_FromLong(ImagingNewCount);
}
static PyObject*
_linear_gradient(PyObject* self, PyObject* args)
{
char* mode;
if (!PyArg_ParseTuple(args, "s", &mode))
return NULL;
return PyImagingNew(ImagingFillLinearGradient(mode));
}
static PyObject*
_radial_gradient(PyObject* self, PyObject* args)
{
char* mode;
if (!PyArg_ParseTuple(args, "s", &mode))
return NULL;
return PyImagingNew(ImagingFillRadialGradient(mode));
}
static PyObject*
_alpha_composite(ImagingObject* self, PyObject* args)
{
ImagingObject* imagep1;
ImagingObject* imagep2;
if (!PyArg_ParseTuple(args, "O!O!",
&Imaging_Type, &imagep1,
&Imaging_Type, &imagep2))
return NULL;
return PyImagingNew(ImagingAlphaComposite(imagep1->image, imagep2->image));
}
static PyObject*
_blend(ImagingObject* self, PyObject* args)
{
ImagingObject* imagep1;
ImagingObject* imagep2;
double alpha;
alpha = 0.5;
if (!PyArg_ParseTuple(args, "O!O!|d",
&Imaging_Type, &imagep1,
&Imaging_Type, &imagep2,
&alpha))
return NULL;
return PyImagingNew(ImagingBlend(imagep1->image, imagep2->image,
(float) alpha));
}
/* -------------------------------------------------------------------- */
/* METHODS */
/* -------------------------------------------------------------------- */
static PyObject*
_convert(ImagingObject* self, PyObject* args)
{
char* mode;
int dither = 0;
ImagingObject *paletteimage = NULL;
if (!PyArg_ParseTuple(args, "s|iO", &mode, &dither, &paletteimage))
return NULL;
if (paletteimage != NULL) {
if (!PyImaging_Check(paletteimage)) {
PyObject_Print((PyObject *)paletteimage, stderr, 0);
PyErr_SetString(PyExc_ValueError, "palette argument must be image with mode 'P'");
return NULL;
}
if (paletteimage->image->palette == NULL) {
PyErr_SetString(PyExc_ValueError, "null palette");
return NULL;
}
}
return PyImagingNew(ImagingConvert(self->image, mode, paletteimage ? paletteimage->image->palette : NULL, dither));
}
static PyObject*
_convert2(ImagingObject* self, PyObject* args)
{
ImagingObject* imagep1;
ImagingObject* imagep2;
if (!PyArg_ParseTuple(args, "O!O!",
&Imaging_Type, &imagep1,
&Imaging_Type, &imagep2))
return NULL;
if (!ImagingConvert2(imagep1->image, imagep2->image))
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
_convert_matrix(ImagingObject* self, PyObject* args)
{
char* mode;
float m[12];
if (!PyArg_ParseTuple(args, "s(ffff)", &mode, m+0, m+1, m+2, m+3)) {
PyErr_Clear();
if (!PyArg_ParseTuple(args, "s(ffffffffffff)", &mode,
m+0, m+1, m+2, m+3,
m+4, m+5, m+6, m+7,
m+8, m+9, m+10, m+11)){
return NULL;
}
}
return PyImagingNew(ImagingConvertMatrix(self->image, mode, m));
}
static PyObject*
_convert_transparent(ImagingObject* self, PyObject* args)
{
char* mode;
int r,g,b;
if (PyArg_ParseTuple(args, "s(iii)", &mode, &r, &g, &b)) {
return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, g, b));
}
PyErr_Clear();
if (PyArg_ParseTuple(args, "si", &mode, &r)) {
return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, 0, 0));
}
return NULL;
}
static PyObject*
_copy(ImagingObject* self, PyObject* args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
return PyImagingNew(ImagingCopy(self->image));
}
static PyObject*
_crop(ImagingObject* self, PyObject* args)
{
int x0, y0, x1, y1;
if (!PyArg_ParseTuple(args, "(iiii)", &x0, &y0, &x1, &y1))
return NULL;
return PyImagingNew(ImagingCrop(self->image, x0, y0, x1, y1));
}
static PyObject*
_expand_image(ImagingObject* self, PyObject* args)
{
int x, y;
int mode = 0;
if (!PyArg_ParseTuple(args, "ii|i", &x, &y, &mode))
return NULL;
return PyImagingNew(ImagingExpand(self->image, x, y, mode));
}
static PyObject*
_filter(ImagingObject* self, PyObject* args)
{
PyObject* imOut;
Py_ssize_t kernelsize;
FLOAT32* kerneldata;
int xsize, ysize;
float divisor, offset;
PyObject* kernel = NULL;
if (!PyArg_ParseTuple(args, "(ii)ffO", &xsize, &ysize,
&divisor, &offset, &kernel))
return NULL;
/* get user-defined kernel */
kerneldata = getlist(kernel, &kernelsize, NULL, TYPE_FLOAT32);
if (!kerneldata)
return NULL;
if (kernelsize != (Py_ssize_t) xsize * (Py_ssize_t) ysize) {
free(kerneldata);
return ImagingError_ValueError("bad kernel size");
}
imOut = PyImagingNew(
ImagingFilter(self->image, xsize, ysize, kerneldata, offset, divisor)
);
free(kerneldata);
return imOut;
}
#ifdef WITH_UNSHARPMASK
static PyObject*
_gaussian_blur(ImagingObject* self, PyObject* args)
{
Imaging imIn;
Imaging imOut;
float radius = 0;
int passes = 3;
if (!PyArg_ParseTuple(args, "f|i", &radius, &passes))
return NULL;
imIn = self->image;
imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize);
if (!imOut)
return NULL;
if (!ImagingGaussianBlur(imOut, imIn, radius, passes))
return NULL;
return PyImagingNew(imOut);
}
#endif
static PyObject*
_getpalette(ImagingObject* self, PyObject* args)
{
PyObject* palette;
int palettesize = 256;
int bits;
ImagingShuffler pack;
char* mode = "RGB";
char* rawmode = "RGB";
if (!PyArg_ParseTuple(args, "|ss", &mode, &rawmode))
return NULL;
if (!self->image->palette) {
PyErr_SetString(PyExc_ValueError, no_palette);
return NULL;
}
pack = ImagingFindPacker(mode, rawmode, &bits);
if (!pack) {
PyErr_SetString(PyExc_ValueError, wrong_raw_mode);
return NULL;
}
palette = PyBytes_FromStringAndSize(NULL, palettesize * bits / 8);
if (!palette)
return NULL;
pack((UINT8*) PyBytes_AsString(palette),
self->image->palette->palette, palettesize);
return palette;
}
static PyObject*
_getpalettemode(ImagingObject* self, PyObject* args)
{
if (!self->image->palette) {
PyErr_SetString(PyExc_ValueError, no_palette);
return NULL;
}
return PyUnicode_FromString(self->image->palette->mode);
}
static inline int
_getxy(PyObject* xy, int* x, int *y)
{
PyObject* value;
if (!PyTuple_Check(xy) || PyTuple_GET_SIZE(xy) != 2)
goto badarg;
value = PyTuple_GET_ITEM(xy, 0);
if (PyInt_Check(value))
*x = PyInt_AS_LONG(value);
else if (PyFloat_Check(value))
*x = (int) PyFloat_AS_DOUBLE(value);
else
goto badval;
value = PyTuple_GET_ITEM(xy, 1);
if (PyInt_Check(value))
*y = PyInt_AS_LONG(value);
else if (PyFloat_Check(value))
*y = (int) PyFloat_AS_DOUBLE(value);
else
goto badval;
return 0;
badarg:
PyErr_SetString(
PyExc_TypeError,
"argument must be sequence of length 2"
);
return -1;
badval:
PyErr_SetString(
PyExc_TypeError,
"an integer is required"
);
return -1;
}
static PyObject*
_getpixel(ImagingObject* self, PyObject* args)
{
PyObject* xy;
int x, y;
if (PyTuple_GET_SIZE(args) != 1) {
PyErr_SetString(
PyExc_TypeError,
"argument 1 must be sequence of length 2"
);
return NULL;
}
xy = PyTuple_GET_ITEM(args, 0);
if (_getxy(xy, &x, &y))
return NULL;
if (self->access == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
return getpixel(self->image, self->access, x, y);
}
static PyObject*
_histogram(ImagingObject* self, PyObject* args)
{
ImagingHistogram h;
PyObject* list;
int i;
union {
UINT8 u[2];
INT32 i[2];
FLOAT32 f[2];
} extrema;
void* ep;
int i0, i1;
double f0, f1;
PyObject* extremap = NULL;
ImagingObject* maskp = NULL;