-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
_imaging.c
4373 lines (3680 loc) · 116 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.
*/
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#ifdef HAVE_LIBJPEG
#include "jconfig.h"
#endif
#ifdef HAVE_LIBZ
#include "zlib.h"
#endif
#ifdef HAVE_LIBTIFF
#ifndef _TIFFIO_
#include <tiffio.h>
#endif
#endif
#include "libImaging/Imaging.h"
#define _USE_MATH_DEFINES
#include <math.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 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) {
return PyObject_CheckBuffer(buffer);
}
int
PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view) {
/* must call check_buffer first! */
return PyObject_GetBuffer(buffer, view, PyBUF_SIMPLE);
}
/* -------------------------------------------------------------------- */
/* 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 *incorrectly_ordered_x_coordinate =
"x1 must be greater than or equal to x0";
static const char *incorrectly_ordered_y_coordinate =
"y1 must be greater than or equal to y0";
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_OSError(void) {
PyErr_SetString(PyExc_OSError, "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_FLOAT16 (0x500 | sizeof(FLOAT16))
#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;
FLOAT32 ftemp;
UINT8 *list;
PyObject *seq;
PyObject *op;
if (!PySequence_Check(arg)) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
n = PySequence_Size(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 ImagingError_MemoryError();
}
seq = PySequence_Fast(arg, must_be_sequence);
if (!seq) {
free(list);
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 = PyLong_AsLong(op);
list[i] = CLIP8(itemp);
break;
case TYPE_INT32:
itemp = PyLong_AsLong(op);
memcpy(list + i * sizeof(INT32), &itemp, sizeof(itemp));
break;
case TYPE_FLOAT32:
ftemp = (FLOAT32)PyFloat_AsDouble(op);
memcpy(list + i * sizeof(ftemp), &ftemp, sizeof(ftemp));
break;
case TYPE_DOUBLE:
dtemp = PyFloat_AsDouble(op);
memcpy(list + i * sizeof(dtemp), &dtemp, sizeof(dtemp));
break;
}
}
Py_DECREF(seq);
if (PyErr_Occurred()) {
free(list);
return NULL;
}
if (length) {
*length = n;
}
return list;
}
FLOAT32
float16tofloat32(const FLOAT16 in) {
UINT32 t1;
UINT32 t2;
UINT32 t3;
FLOAT32 out[1] = {0};
t1 = in & 0x7fff; // Non-sign bits
t2 = in & 0x8000; // Sign bit
t3 = in & 0x7c00; // Exponent
t1 <<= 13; // Align mantissa on MSB
t2 <<= 16; // Shift sign bit into position
t1 += 0x38000000; // Adjust bias
t1 = (t3 == 0 ? 0 : t1); // Denormals-as-zero
t1 |= t2; // Re-insert sign bit
memcpy(out, &t1, 4);
return out[0];
}
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 + 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 (strncmp(im->mode, "I;16", 4) == 0) {
return PyLong_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;
FLOAT32 ftmp;
INT32 itmp;
/* fill ink buffer (four bytes) with something that can
be cast to either UINT8 or INT32 */
int rIsInt = 0;
int tupleSize = PyTuple_Check(color) ? PyTuple_GET_SIZE(color) : -1;
if (tupleSize == 1) {
color = PyTuple_GetItem(color, 0);
}
if (im->type == IMAGING_TYPE_UINT8 || im->type == IMAGING_TYPE_INT32 ||
im->type == IMAGING_TYPE_SPECIAL) {
if (PyLong_Check(color)) {
r = PyLong_AsLongLong(color);
if (r == -1 && PyErr_Occurred()) {
return NULL;
}
rIsInt = 1;
} else if (im->bands == 1) {
PyErr_SetString(
PyExc_TypeError, "color must be int or single-element tuple");
return NULL;
} else if (tupleSize == -1) {
PyErr_SetString(PyExc_TypeError, "color must be int or tuple");
return NULL;
}
}
switch (im->type) {
case IMAGING_TYPE_UINT8:
/* unsigned integer */
if (im->bands == 1) {
/* unsigned integer, single layer */
if (rIsInt != 1) {
if (tupleSize != 1) {
PyErr_SetString(PyExc_TypeError, "color must be int or single-element tuple");
return NULL;
} else if (!PyArg_ParseTuple(color, "L", &r)) {
return NULL;
}
}
ink[0] = (char)CLIP8(r);
ink[1] = ink[2] = ink[3] = 0;
} else {
if (rIsInt) {
/* compatibility: ABGR */
a = (UINT8)(r >> 24);
b = (UINT8)(r >> 16);
g = (UINT8)(r >> 8);
r = (UINT8)r;
} else {
a = 255;
if (im->bands == 2) {
if (tupleSize != 1 && tupleSize != 2) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or two elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "L|i", &r, &a)) {
return NULL;
}
g = b = r;
} else {
if (tupleSize != 3 && tupleSize != 4) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one, three or four elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a)) {
return NULL;
}
}
}
ink[0] = (char)CLIP8(r);
ink[1] = (char)CLIP8(g);
ink[2] = (char)CLIP8(b);
ink[3] = (char)CLIP8(a);
}
return ink;
case IMAGING_TYPE_INT32:
/* signed integer */
itmp = r;
memcpy(ink, &itmp, sizeof(itmp));
return ink;
case IMAGING_TYPE_FLOAT32:
/* floating point */
f = PyFloat_AsDouble(color);
if (f == -1.0 && PyErr_Occurred()) {
return NULL;
}
ftmp = f;
memcpy(ink, &ftmp, sizeof(ftmp));
return ink;
case IMAGING_TYPE_SPECIAL:
if (strncmp(im->mode, "I;16", 4) == 0) {
ink[0] = (UINT8)r;
ink[1] = (UINT8)(r >> 8);
ink[2] = ink[3] = 0;
return ink;
} else {
if (rIsInt) {
b = (UINT8)(r >> 16);
g = (UINT8)(r >> 8);
r = (UINT8)r;
} else if (tupleSize != 3) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or three elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "Lii", &r, &g, &b)) {
return NULL;
}
if (!strcmp(im->mode, "BGR;15")) {
UINT16 v = ((((UINT16)r) << 7) & 0x7c00) +
((((UINT16)g) << 2) & 0x03e0) +
((((UINT16)b) >> 3) & 0x001f);
ink[0] = (UINT8)v;
ink[1] = (UINT8)(v >> 8);
ink[2] = ink[3] = 0;
return ink;
} else if (!strcmp(im->mode, "BGR;16")) {
UINT16 v = ((((UINT16)r) << 8) & 0xf800) +
((((UINT16)g) << 3) & 0x07e0) +
((((UINT16)b) >> 3) & 0x001f);
ink[0] = (UINT8)v;
ink[1] = (UINT8)(v >> 8);
ink[2] = ink[3] = 0;
return ink;
} else if (!strcmp(im->mode, "BGR;24")) {
ink[0] = (UINT8)b;
ink[1] = (UINT8)g;
ink[2] = (UINT8)r;
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 *
_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 INT16 *
_prepare_lut_table(PyObject *table, Py_ssize_t table_size) {
int i;
Py_buffer buffer_info;
INT32 data_type = TYPE_FLOAT32;
float item = 0;
void *table_data = NULL;
int free_table_data = 0;
INT16 *prepared;
/* NOTE: This value should be the same as in ColorLUT.c */
#define PRECISION_BITS (16 - 8 - 2)
const char *wrong_size =
("The table should have table_channels * "
"size1D * size2D * size3D float items.");
if (PyObject_CheckBuffer(table)) {
if (!PyObject_GetBuffer(table, &buffer_info, PyBUF_CONTIG_RO | PyBUF_FORMAT)) {
if (buffer_info.ndim == 1 && buffer_info.shape[0] == table_size) {
if (strlen(buffer_info.format) == 1) {
switch (buffer_info.format[0]) {
case 'e':
data_type = TYPE_FLOAT16;
table_data = buffer_info.buf;
break;
case 'f':
data_type = TYPE_FLOAT32;
table_data = buffer_info.buf;
break;
case 'd':
data_type = TYPE_DOUBLE;
table_data = buffer_info.buf;
break;
}
}
}
PyBuffer_Release(&buffer_info);
}
}
if (!table_data) {
free_table_data = 1;
table_data = getlist(table, &table_size, wrong_size, TYPE_FLOAT32);
if (!table_data) {
return NULL;
}
}
/* malloc check ok, max is 2 * 4 * 65**3 = 2197000 */
prepared = (INT16 *)malloc(sizeof(INT16) * table_size);
if (!prepared) {
if (free_table_data) {
free(table_data);
}
return (INT16 *)ImagingError_MemoryError();
}
for (i = 0; i < table_size; i++) {
FLOAT16 htmp;
double dtmp;
switch (data_type) {
case TYPE_FLOAT16:
memcpy(&htmp, ((char *)table_data) + i * sizeof(htmp), sizeof(htmp));
item = float16tofloat32(htmp);
break;
case TYPE_FLOAT32:
memcpy(
&item, ((char *)table_data) + i * sizeof(FLOAT32), sizeof(FLOAT32));
break;
case TYPE_DOUBLE:
memcpy(&dtmp, ((char *)table_data) + i * sizeof(dtmp), sizeof(dtmp));
item = (FLOAT32)dtmp;
break;
}
/* Max value for INT16 */
if (item >= (0x7fff - 0.5) / (255 << PRECISION_BITS)) {
prepared[i] = 0x7fff;
continue;
}
/* Min value for INT16 */
if (item <= (-0x8000 + 0.5) / (255 << PRECISION_BITS)) {
prepared[i] = -0x8000;
continue;
}
if (item < 0) {
prepared[i] = item * (255 << PRECISION_BITS) - 0.5;
} else {
prepared[i] = item * (255 << PRECISION_BITS) + 0.5;
}
}
#undef PRECISION_BITS
if (free_table_data) {
free(table_data);
}
return prepared;
}
static PyObject *
_color_lut_3d(ImagingObject *self, PyObject *args) {
char *mode;
int filter;
int table_channels;
int size1D, size2D, size3D;
PyObject *table;
INT16 *prepared_table;
Imaging imOut;
if (!PyArg_ParseTuple(
args,
"siiiiiO:color_lut_3d",
&mode,
&filter,
&table_channels,
&size1D,
&size2D,
&size3D,
&table)) {
return NULL;
}
/* actually, it is trilinear */
if (filter != IMAGING_TRANSFORM_BILINEAR) {
PyErr_SetString(PyExc_ValueError, "Only LINEAR filter is supported.");
return NULL;
}
if (1 > table_channels || table_channels > 4) {
PyErr_SetString(PyExc_ValueError, "table_channels should be from 1 to 4");
return NULL;
}
if (2 > size1D || size1D > 65 || 2 > size2D || size2D > 65 || 2 > size3D ||
size3D > 65) {
PyErr_SetString(
PyExc_ValueError, "Table size in any dimension should be from 2 to 65");
return NULL;
}
prepared_table =
_prepare_lut_table(table, table_channels * size1D * size2D * size3D);
if (!prepared_table) {
return NULL;
}
imOut = ImagingNewDirty(mode, self->image->xsize, self->image->ysize);
if (!imOut) {
free(prepared_table);
return NULL;
}
if (!ImagingColorLUT3D_linear(
imOut,
self->image,
table_channels,
size1D,
size2D,
size3D,
prepared_table)) {
free(prepared_table);
ImagingDelete(imOut);
return NULL;
}
free(prepared_table);
return PyImagingNew(imOut);
}
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));
}