-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_gdmodule.c
2253 lines (1808 loc) · 63.3 KB
/
_gdmodule.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
/******************************************************************
Copyright 1995 Richard Jones, Bureau of Meteorology Australia.
Current maintainer is
Chris Gonnerman <[email protected]>
Please direct all questions and problems to me.
This module is a python wrapper for the GD library (version 1.8.3)
version 0.56
Revised 03/10/2005 by Chris Gonnerman
-- added support for GIF files.
-- the minimum gd library version for this gdmodule version is
2.0.23.
version 0.55
Revised 03/10/2005 by Chris Gonnerman
-- corrected error in the gd.image() constructor, pointed out
by Betty Li. maybe this time I got it right.
-- the minimum gd library version for this gdmodule version is
2.0.22.
version 0.54
Revised 03/03/2005 by Chris Gonnerman
-- corrected error in the gd.image() constructor, pointed out
by Betty Li.
-- corrected yet another obvious error reported by Greg Hewgill,
regarding an incorrect call to gdImagePolygon when fillcolor
is not equal to 1.
-- implemented the saveAlpha and alphaBlending features as
suggested by Christopher Stone.
-- fixed memory management issue regarding image deallocation
reported by Matti Jagula.
-- fixed memory management issue in image_filledpolygon
reported by Sadruddin Rejeb.
-- the minimum gd library version for this gdmodule version is
2.0.22.
version 0.53
Revised 06/10/2004 by Chris Gonnerman
-- corrected obvious error in image_settile(), as pointed out
by Dan Mosedale
-- applied some patches provided by John Hunter. Several are
for Windows compatibility, but this should be considered
a "work in progress" in this version.
-- corrected another obvious error (and an ancient one too)
reported by Greg Hewgill, regarding the foreground color
being used rather than the fill color in the image_polygon
function.
-- the minimum gd library version for this gdmodule version is
2.0.22.
version 0.52
Revised 02/03/2004 by Chris Gonnerman
-- incorporated new functions from the matplotlib project
(provided by John Hunter)
-- corrected error in PyObject_HEAD_INIT() call noted by
Stefan R Kuzminski (thanks!)
version 0.51
Revised 09/24/2003 by Chris Gonnerman
-- fixed memory management bug reported by Jack Diederich.
version 0.50
Revised 09/13/2003 by Chris Gonnerman
-- documentation change only: the GD library must be version
2.0.8 or higher (or you must choose version 0.40 or earlier
of this module, which supports GD 1.8.3 or higher).
version 0.42
Revised 06/10/2003 by Chris Gonnerman
-- implemented patch by Gregory P. Smith to support writing to
arbitrary objects, superceding the patch in 0.41 from
Andreas Rottman. gddemo.py contains Rottman's example
(which works with Smith's patched version) as well as an
example (by me) of reading an image from a URL. The
documentation was updated to reflect these changes.
version 0.41
Revised 05/29/2003 by Chris Gonnerman
-- implemented big patch by Andreas Rottmann to support writing
images to memory via StringIO/CStringIO. Currently the only
documentation is an example in gddemo.py.
-- implemented patch by Bob Galloway to remove the "specialness"
of negative coordinates in the string_ttf/string_ft methods.
-- implemented patch by Nathan Robertson to enable MacOSX fink
builds.
-- fixed bugs in the proxy class with regard to passing of
_gd.image types.
version 0.40
Revised 09/18/2002 by Chris Gonnerman
-- updated to deal with incomplete library installs
version 0.30
Revised 06/12/2002 by Chris Gonnerman
-- initial conversion to two-tier design
version 0.26
Revised 12/10/2001 by Chris Gonnerman
-- made unicode optional via define Py_UNICODEOBJECT_H
version 0.25
Revised 09/15/2001 by Chris Gonnerman
-- implemented patch by Mike Romberg:
adds additional public functions, available
only if GD 2.0.1+ is installed.
version 0.24
Revised 08/08/2001 by Chris Gonnerman
-- implemented patch by Mike Romberg:
supports GCC 3.0 and GD 2.0.1
adds public C function makeGDImage()
It allows C or C++ code which uses the gd library
directly to make an imageobject instance.
version 0.23
Revised 11/22/2000 by Chris Gonnerman
-- included the patch from Tanimoto Osamu
-- updated support to GD version 1.8.3
-- cleaned up code, added ANSI prototypes
******************************************************************/
#include <Python.h>
#include <gd.h>
#include <gdfonts.h>
#include <gdfontl.h>
#include <gdfontmb.h>
#include <gdfontt.h>
#include <gdfontg.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_LIBTTF
#define HAVE_LIBFREETYPE
#endif
/* missing from gd.h */
/* gdImagePtr gdImageCreateFromXpm(char *filename); */
#ifdef WIN32
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
#endif
static PyObject *ErrorObject;
/*
** Declarations for objects of type image
*/
typedef struct i_o {
PyObject_HEAD
gdImagePtr imagedata;
int multiplier_x,origin_x;
int multiplier_y,origin_y;
struct i_o *current_brush;
struct i_o *current_tile;
} imageobject;
// 2.0.22 replaces gdFontTinyRep with gdFontGetTiny, and so on
typedef gdFontPtr (*ptrGetFontFunction)();
typedef struct {
char *name;
ptrGetFontFunction func;
} fontstruct;
static fontstruct fonts[] = {
{"gdFontTiny",&gdFontGetTiny},
{"gdFontSmall",&gdFontGetSmall},
{"gdFontMediumBold",&gdFontGetMediumBold},
{"gdFontLarge",&gdFontGetLarge},
{"gdFontGiant",&gdFontGetGiant},
{NULL,NULL}
};
staticforward PyTypeObject Imagetype;
#define is_imageobject(v) ((v)->ob_type == &Imagetype)
#define MIN(x,y) ((x)<(y)?(x):(y))
#define X(x) ((x)*self->multiplier_x+self->origin_x)
#define Y(y) ((y)*self->multiplier_y+self->origin_y)
#define W(x) ((x)*self->multiplier_x)
#define H(y) ((y)*self->multiplier_y)
static imageobject *newimageobject(PyObject *args);
/*
** Support Functions
*/
static PyObject *write_file(imageobject *img, PyObject *args, char fmt)
{
char *filename;
PyObject *fileobj;
FILE *fp = NULL;
int closeme = 0, use_fileobj_write = 0;
int arg1 = -1, arg2 = -1;
int filesize = 0;
void *filedata = NULL;
if(PyArg_ParseTuple(args, "O!|ii", &PyFile_Type, &fileobj, &arg1, &arg2)) {
fp = PyFile_AsFile(fileobj);
} else if(PyErr_Clear(), PyArg_ParseTuple(args, "z|ii", &filename, &arg1, &arg2)) {
if((fp = fopen(filename, "wb"))) {
closeme = 1;
} else {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
} else if (PyErr_Clear(), PyArg_ParseTuple(args, "O|ii", &fileobj, &arg1, &arg2)) {
/* if we're passed a random object that has a write method we will
* attempt to call object.write(filedata) */
if (!PyObject_HasAttrString(fileobj, "write")) {
PyErr_SetString(ErrorObject, "first argument must be a file, string or object with a write method");
return NULL;
}
use_fileobj_write = 1;
}
else
return NULL;
switch(fmt) {
case 'f' : /* gif */
#ifdef HAVE_LIBGIF
if (use_fileobj_write) {
filedata = gdImageGifPtr(img->imagedata, &filesize);
} else {
gdImageGif(img->imagedata, fp);
}
#else
PyErr_SetString(PyExc_NotImplementedError,
"GIF Support Not Available");
return NULL;
#endif
break;
case 'p' : /* png */
#ifdef HAVE_LIBPNG
if (use_fileobj_write) {
filedata = gdImagePngPtr(img->imagedata, &filesize);
} else {
gdImagePng(img->imagedata, fp);
}
#else
PyErr_SetString(PyExc_NotImplementedError,
"PNG Support Not Available");
return NULL;
#endif
break;
case 'j' : /* jpeg */
#ifdef HAVE_LIBJPEG
if (use_fileobj_write) {
filedata = gdImageJpegPtr(img->imagedata, &filesize, arg1);
} else {
gdImageJpeg(img->imagedata, fp, arg1);
}
#else
PyErr_SetString(PyExc_NotImplementedError,
"JPEG Support Not Available");
return NULL;
#endif
break;
case 'g' : /* gd */
if (use_fileobj_write) {
filedata = gdImageGdPtr(img->imagedata, &filesize);
} else {
gdImageGd(img->imagedata, fp);
}
break;
case 'G' : /* gd2 */
if(arg1 == -1) arg1 = 0;
if(arg2 != GD2_FMT_RAW && arg2 != GD2_FMT_COMPRESSED)
arg2 = GD2_FMT_COMPRESSED;
if (use_fileobj_write) {
filedata = gdImageGd2Ptr(img->imagedata, arg1, arg2, &filesize);
} else {
gdImageGd2(img->imagedata, fp, arg1, arg2);
}
break;
case 'w' : /* wbmp */
if(arg1 == -1)
arg1 = 0;
if (use_fileobj_write) {
//filedata = gdImageWBMPPtr(img->imagedata, &filesize, arg1);
} else {
gdImageWBMP(img->imagedata, arg1, fp);
}
break;
}
if (use_fileobj_write || filedata) {
PyObject *noerr;
noerr = PyObject_CallMethod(fileobj, "write", "s#", filedata, filesize);
gdFree(filedata);
if (noerr == NULL)
return NULL;
} else if (closeme) {
fclose(fp);
}
Py_INCREF(Py_None);
return Py_None;
}
/*
** Methods for the image type
*/
imageobject *makeGDImage(gdImagePtr imagedata)
{
gdImagePtr newimg = gdImageCreate(gdImageSX(imagedata),
gdImageSY(imagedata));
imageobject *rval = 0;
gdImageCopy(newimg, imagedata, 0, 0, 0, 0, gdImageSX(imagedata),
gdImageSY(imagedata));
if (!(rval = PyObject_NEW(imageobject, &Imagetype)))
return NULL;
rval->current_tile = rval->current_brush = NULL;
rval->origin_x = rval->origin_y = 0;
rval->multiplier_x = rval->multiplier_y = 1;
rval->imagedata = newimg;
return rval;
}
/*** I/O Methods ***/
static PyObject *image_writegif(imageobject *self, PyObject *args)
{
return write_file(self, args, 'f');
}
static PyObject *image_writepng(imageobject *self, PyObject *args)
{
return write_file(self, args, 'p');
}
static PyObject *image_writejpeg(imageobject *self, PyObject *args)
{
return write_file(self, args, 'j');
}
static PyObject *image_writegd(imageobject *self, PyObject *args)
{
return write_file(self, args, 'g');
}
static PyObject *image_writegd2(imageobject *self, PyObject *args)
{
return write_file(self, args, 'G');
}
static PyObject *image_writewbmp(imageobject *self, PyObject *args)
{
return write_file(self, args, 'w');
}
/*** Drawing Methods ***/
static PyObject *image_setpixel(imageobject *self, PyObject *args)
{
int x,y,color;
if(!PyArg_ParseTuple(args, "(ii)i", &x, &y, &color))
return NULL;
gdImageSetPixel(self->imagedata, X(x), Y(y), color);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_line(imageobject *self, PyObject *args)
{
int sx,sy,ex,ey,color;
if(!PyArg_ParseTuple(args, "(ii)(ii)i", &sx, &sy, &ex, &ey, &color))
return NULL;
gdImageLine(self->imagedata, X(sx), Y(sy), X(ex), Y(ey), color);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_lines(imageobject *self, PyObject *args)
{
int color,i,N;
long sx,sy,ex,ey;
PyObject *seq;
PyObject *lastTup, *thisTup;
if(!PyArg_ParseTuple(args, "Oi", &seq, &color))
return NULL;
seq = PySequence_Fast(seq, NULL);
N = PySequence_Length(seq);
if (N<2) {
PyErr_SetString(PyExc_ValueError,
"lines() requires sequence of len(2) or greater");
return NULL;
}
lastTup = PySequence_GetItem(seq, 0);
sx = X(PyInt_AsLong(PySequence_GetItem(lastTup, 0)));
sy = Y(PyInt_AsLong(PySequence_GetItem(lastTup, 1)));
for (i=0; i<N; ++i) {
thisTup = PySequence_GetItem(seq, i);
ex = X(PyInt_AsLong(PySequence_GetItem(thisTup, 0)));
ey = Y(PyInt_AsLong(PySequence_GetItem(thisTup, 1)));
gdImageLine(self->imagedata, sx, sy, ex, ey, color);
sx = ex;
sy = ey;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_polygon(imageobject *self, PyObject *args)
{
PyObject *point, *points;
gdPointPtr gdpoints;
int size, color, i, fillcolor = -1;
if(!PyArg_ParseTuple(args, "O!i|i", &PyTuple_Type, &points, &color, &fillcolor)) {
PyErr_Clear();
if(PyArg_ParseTuple(args, "O!i|i", &PyList_Type, &points, &color, &fillcolor))
points=PyList_AsTuple(points);
else
return NULL;
}
size = PyTuple_Size(points);
gdpoints = (gdPointPtr)calloc(size,sizeof(gdPoint));
for(i=0; i<size; i++) {
point = PyTuple_GET_ITEM(points,i);
gdpoints[i].x = X(PyInt_AS_LONG((PyIntObject *)PyTuple_GET_ITEM(point,0)));
gdpoints[i].y = Y(PyInt_AS_LONG((PyIntObject *)PyTuple_GET_ITEM(point,1)));
}
if(fillcolor != -1)
gdImageFilledPolygon(self->imagedata, gdpoints, size, fillcolor);
gdImagePolygon(self->imagedata, gdpoints, size, color);
free(gdpoints);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_rectangle(imageobject *self, PyObject *args)
{
int tx,ty,bx,by,t,color,fillcolor,fill=0;
if(PyArg_ParseTuple(args, "(ii)(ii)ii", &tx, &ty, &bx, &by, &color, &fillcolor))
fill=1;
else if(PyErr_Clear(), !PyArg_ParseTuple(args, "(ii)(ii)i", &tx, &ty, &bx, &by, &color))
return NULL;
tx = X(tx); ty = Y(ty);
bx = X(bx); by = Y(by);
if(tx > bx) {
t = tx;
tx = bx;
bx = t;
}
if(ty > by) {
t = ty;
ty = by;
by = t;
}
if(fill)
gdImageFilledRectangle(self->imagedata, tx, ty, bx, by, fillcolor);
gdImageRectangle(self->imagedata, tx, ty, bx, by, color);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_filledpolygon(imageobject *self, PyObject *args)
{
PyObject *point, *points;
gdPointPtr gdpoints;
int size, color, i;
if(!PyArg_ParseTuple(args, "O!i", &PyTuple_Type, &points, &color)) {
PyErr_Clear();
if(PyArg_ParseTuple(args, "O!i", &PyList_Type, &points, &color))
points=PyList_AsTuple(points);
else
return NULL;
}
size = PyTuple_Size(points);
gdpoints = (gdPointPtr)calloc(size,sizeof(gdPoint));
for(i = 0; i < size; i++) {
point = PyTuple_GET_ITEM(points,i);
gdpoints[i].x = X(PyInt_AS_LONG((PyIntObject *)PyTuple_GET_ITEM(point,0)));
gdpoints[i].y = Y(PyInt_AS_LONG((PyIntObject *)PyTuple_GET_ITEM(point,1)));
}
gdImageFilledPolygon(self->imagedata, gdpoints, size, color);
free(gdpoints);
Py_DECREF(points);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_filledrectangle(imageobject *self, PyObject *args)
{
int tx,ty,bx,by,t,color;
if(!PyArg_ParseTuple(args, "(ii)(ii)i", &tx, &ty, &bx, &by, &color))
return NULL;
tx = X(tx); ty = Y(ty);
bx = X(bx); by = Y(by);
if(tx > bx) {t=tx;tx=bx;bx=t;}
if(ty > by) {t=ty;ty=by;by=t;}
gdImageFilledRectangle(self->imagedata, tx, ty, bx, by, color);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_arc(imageobject *self, PyObject *args)
{
int cx, cy, w, h, s, e, color, i;
if(!PyArg_ParseTuple(args, "(ii)(ii)iii", &cx, &cy, &w, &h, &s, &e, &color))
return NULL;
if(e<s) {i=e;e=s;s=i;}
gdImageArc(self->imagedata, X(cx), Y(cy), W(w), H(h), s, e, color);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_filledarc(imageobject *self, PyObject *args)
{
#if GD2_VERS <= 1
PyErr_SetString(PyExc_NotImplementedError,
"filledArc() requires gd 2.0 or later");
return NULL;
#else
int cx, cy, w, h, s, e, color, i, style;
if(!PyArg_ParseTuple(args, "(ii)(ii)iiii", &cx, &cy, &w, &h, &s,
&e, &color, &style))
return NULL;
if(e<s) {i=e;e=s;s=i;}
gdImageFilledArc(self->imagedata, X(cx), Y(cy), W(w), H(h), s, e,
color, style);
Py_INCREF(Py_None);
return Py_None;
#endif
}
static PyObject *image_filledellipse(imageobject *self, PyObject *args)
{
#if GD2_VERS <= 1
PyErr_SetString(PyExc_NotImplementedError,
"filledEllipse() requires gd 2.0 or later");
return NULL;
#else
int cx, cy, w, h, color;
if(!PyArg_ParseTuple(args, "(ii)(ii)i", &cx, &cy, &w, &h, &color))
return NULL;
gdImageFilledEllipse(self->imagedata, X(cx), Y(cy), W(w), H(h), color);
Py_INCREF(Py_None);
return Py_None;
#endif
}
static PyObject *image_filltoborder(imageobject *self, PyObject *args)
{
int x,y,border,color;
if(!PyArg_ParseTuple(args, "(ii)ii", &x,&y,&border,&color))
return NULL;
gdImageFillToBorder(self->imagedata, X(x),Y(y),border,color);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_fill(imageobject *self, PyObject *args)
{
int x,y,color;
if(!PyArg_ParseTuple(args, "(ii)i", &x,&y,&color))
return NULL;
gdImageFill(self->imagedata, X(x),Y(y),color);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_setbrush(imageobject *self, PyObject *args)
{
imageobject *brush;
char *filename, *type; /* dummies */
if(PyArg_ParseTuple(args, "z|z", &filename, &type))
brush = (imageobject *)newimageobject(args);
else if(PyErr_Clear(), PyArg_ParseTuple(args, "O!", &Imagetype, &brush))
Py_INCREF(brush);
else
return NULL;
if(self->current_brush){
Py_DECREF(self->current_brush);
}
self->current_brush = brush;
gdImageSetBrush(self->imagedata, brush->imagedata);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_setantialiased(imageobject *self, PyObject *args)
{
int c;
if(!PyArg_ParseTuple(args, "i", &c))
return NULL;
gdImageSetAntiAliased(self->imagedata, c);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_setclip(imageobject *self, PyObject *args)
{
int tx,ty,bx,by,t;
if(!PyArg_ParseTuple(args, "(ii)(ii)", &tx, &ty, &bx, &by))
return NULL;
tx = X(tx); ty = Y(ty);
bx = X(bx); by = Y(by);
if(tx > bx) {
t = tx;
tx = bx;
bx = t;
}
if(ty > by) {
t = ty;
ty = by;
by = t;
}
gdImageSetClip(self->imagedata, tx, ty, bx, by);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_settile(imageobject *self, PyObject *args)
{
imageobject *tile;
char *filename, *type; /* dummies */
if(PyArg_ParseTuple(args, "z|z", &filename, &type))
tile = (imageobject *)newimageobject(args);
else
if(PyErr_Clear(), PyArg_ParseTuple(args, "O!", &Imagetype, &tile))
Py_INCREF(tile);
else
return NULL;
if(self->current_tile) {
Py_DECREF(self->current_tile);
}
self->current_tile = tile;
gdImageSetTile(self->imagedata, tile->imagedata);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_setstyle(imageobject *self, PyObject *args)
{
PyObject *style;
int size, i, *stylearray;
if(!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &style)) {
PyErr_Clear();
if(PyArg_ParseTuple(args, "O!", &PyList_Type, &style))
style=PyList_AsTuple(style);
else
return NULL;
}
size = PyTuple_Size(style);
stylearray = (int *)calloc(size,sizeof(int));
for(i=0; i<size; i++)
stylearray[i] = PyInt_AS_LONG((PyIntObject *)PyTuple_GET_ITEM(style,i));
gdImageSetStyle(self->imagedata, stylearray, size);
free(stylearray);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_setthickness(imageobject *self, PyObject *args)
{
#if GD2_VERS <= 1
PyErr_SetString(PyExc_NotImplementedError,
"setThickness() requires gd 2.0 or later");
return NULL;
#else
int t;
if(!PyArg_ParseTuple(args, "i", &t))
return NULL;
gdImageSetThickness(self->imagedata, t);
Py_INCREF(Py_None);
return Py_None;
#endif
}
static PyObject *image_alphablending(imageobject *self, PyObject *args)
{
#if GD2_VERS <= 1
PyErr_SetString(PyExc_NotImplementedError,
"alphaBlending() requires gd 2.0 or later");
return NULL;
#else
int blending;
if(!PyArg_ParseTuple(args, "i", &blending))
return NULL;
gdImageAlphaBlending(self->imagedata, blending);
Py_INCREF(Py_None);
return Py_None;
#endif
}
static PyObject *image_alpha(imageobject *self, PyObject *args)
{
#if GD2_VERS <= 1
PyErr_SetString(PyExc_NotImplementedError,
"alpha() requires gd 2.0 or later");
return NULL;
#else
int color;
if(!PyArg_ParseTuple(args, "i", &color))
return NULL;
return Py_BuildValue("i", gdImageAlpha(self->imagedata, color));
#endif
}
static PyObject *image_getpixel(imageobject *self, PyObject *args)
{
int x,y;
if(!PyArg_ParseTuple(args, "(ii)", &x,&y))
return NULL;
return Py_BuildValue("i",gdImageGetPixel(self->imagedata, X(x),Y(y)));
}
static PyObject *image_boundssafe(imageobject *self, PyObject *args)
{
int x,y;
if(!PyArg_ParseTuple(args, "(ii)", &x,&y))
return NULL;
return Py_BuildValue("i",gdImageBoundsSafe(self->imagedata, X(x),Y(y)));
}
static PyObject *image_blue(imageobject *self, PyObject *args)
{
int c;
if(!PyArg_ParseTuple(args, "i", &c))
return NULL;
return Py_BuildValue("i",gdImageBlue(self->imagedata,c));
}
static PyObject *image_green(imageobject *self, PyObject *args)
{
int c;
if(!PyArg_ParseTuple(args, "i", &c))
return NULL;
return Py_BuildValue("i",gdImageGreen(self->imagedata,c));
}
static PyObject *image_red(imageobject *self, PyObject *args)
{
int c;
if(!PyArg_ParseTuple(args, "i", &c))
return NULL;
return Py_BuildValue("i",gdImageRed(self->imagedata,c));
}
static PyObject *image_size(imageobject *self)
{
return Py_BuildValue("(ii)",gdImageSX(self->imagedata),gdImageSY(self->imagedata));
}
static PyObject *image_char(imageobject *self, PyObject *args)
{
int x,y,font,color;
char c;
if(!PyArg_ParseTuple(args, "i(ii)ii", &font,&x,&y,&c,&color))
return NULL;
gdImageChar(self->imagedata, fonts[font].func(), X(x), Y(y), c, color);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_charup(imageobject *self, PyObject *args)
{
int x,y,font,color;
char c;
if(!PyArg_ParseTuple(args, "i(ii)si", &font,&x,&y,&c,&color))
return NULL;
gdImageCharUp(self->imagedata, fonts[font].func(), X(x), Y(y), c, color);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *image_get_bounding_rect(imageobject *self, PyObject *args)
{
#ifndef HAVE_LIBFREETYPE
PyErr_SetString(PyExc_NotImplementedError,
"Freetype Support Not Available");
return NULL;
#else
double ptsize, angle;
char *fontname, *str, *rc;
int x, y, brect[8];
if(!PyArg_ParseTuple(args, "sdd(ii)s",
&fontname, &ptsize, &angle, &x, &y, &str))
return NULL;
rc = gdImageStringTTF(NULL, brect, 0,
fontname, ptsize, angle, x, y, str);
if(rc != NULL){
PyErr_SetString(PyExc_ValueError, rc);
return NULL;
}
return Py_BuildValue("(iiiiiiii)",brect[0],brect[1],brect[2],brect[3],
brect[4],brect[5],brect[6],brect[7]);
#endif
}
static PyObject *image_string_ft(imageobject *self, PyObject *args)
{
#ifndef HAVE_LIBFREETYPE
PyErr_SetString(PyExc_NotImplementedError,
"Freetype Support Not Available");
return NULL;
#else
#if GD2_VERS <= 1
PyErr_SetString(PyExc_NotImplementedError,
"string_ft() requires gd 2.0 or later");
return NULL;
#else
int x,y,fg;
double ptsize,angle;
char *fontname,*str,*rc;
int brect[8];
if(!PyArg_ParseTuple(args, "sdd(ii)si",
&fontname, &ptsize, &angle, &x,&y,&str,&fg))
return NULL;
rc = gdImageStringFT(NULL, brect, 0, fontname, ptsize, angle,
0, 0, str); /* calculate the bounding rect */
if(rc != NULL){
PyErr_SetString(PyExc_ValueError, rc);
return NULL;
}
rc = gdImageStringTTF(self->imagedata, brect, fg, fontname,
ptsize, angle, x, y, str);
if(rc != NULL){
PyErr_SetString(PyExc_ValueError, rc);
return NULL;
}
return Py_BuildValue("(iiiiiiii)",
brect[0], brect[1], brect[2], brect[3],
brect[4], brect[5], brect[6], brect[7]);
#endif
#endif
}
static PyObject *image_string_ttf(imageobject *self, PyObject *args)
{
#ifndef HAVE_LIBFREETYPE
PyErr_SetString(PyExc_NotImplementedError,
"Freetype Support Not Available");
return NULL;
#else
int x,y,fg;
double ptsize,angle;
char *fontname,*str,*rc;
int brect[8];
if(!PyArg_ParseTuple(args, "sdd(ii)si",
&fontname, &ptsize, &angle, &x,&y,&str,&fg))
return NULL;
rc = gdImageStringTTF(NULL, brect, 0, fontname, ptsize, angle,
0, 0, str); /* calculate the bounding rect */
if(rc != NULL){
PyErr_SetString(PyExc_ValueError, rc);
return NULL;
}
rc = gdImageStringTTF(self->imagedata, brect, fg, fontname,
ptsize, angle, x, y, str);
if(rc != NULL){
PyErr_SetString(PyExc_ValueError, rc);
return NULL;
}
return Py_BuildValue("(iiiiiiii)",
brect[0], brect[1], brect[2], brect[3],
brect[4], brect[5], brect[6], brect[7]);
#endif
}
static PyObject *image_string(imageobject *self, PyObject *args)
{
int x, y, font, color;
unsigned char *str;
if(!PyArg_ParseTuple(args, "i(ii)si", &font,&x,&y,&str,&color))
return NULL;
gdImageString(self->imagedata, fonts[font].func(), X(x), Y(y), str, color);
Py_INCREF(Py_None);
return Py_None;
}
#ifdef Py_UNICODEOBJECT_H
static PyObject *image_string16(imageobject *self, PyObject *args)
{