-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Draw.c
1947 lines (1734 loc) · 50.7 KB
/
Draw.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.
* $Id$
*
* a simple drawing package for the Imaging library
*
* history:
* 1996-04-13 fl Created.
* 1996-04-30 fl Added transforms and polygon support.
* 1996-08-12 fl Added filled polygons.
* 1996-11-05 fl Fixed float/int confusion in polygon filler
* 1997-07-04 fl Support 32-bit images (C++ would have been nice)
* 1998-09-09 fl Eliminated qsort casts; improved rectangle clipping
* 1998-09-10 fl Fixed fill rectangle to include lower edge (!)
* 1998-12-29 fl Added arc, chord, and pieslice primitives
* 1999-01-10 fl Added some level 2 ("arrow") stuff (experimental)
* 1999-02-06 fl Added bitmap primitive
* 1999-07-26 fl Eliminated a compiler warning
* 1999-07-31 fl Pass ink as void* instead of int
* 2002-12-10 fl Added experimental RGBA-on-RGB drawing
* 2004-09-04 fl Support simple wide lines (no joins)
* 2005-05-25 fl Fixed line width calculation
*
* Copyright (c) 1996-2006 by Fredrik Lundh
* Copyright (c) 1997-2006 by Secret Labs AB.
*
* See the README file for information on usage and redistribution.
*/
/* FIXME: support fill/outline attribute for all filled shapes */
/* FIXME: support zero-winding fill */
/* FIXME: add drawing context, support affine transforms */
/* FIXME: support clip window (and mask?) */
#include "Imaging.h"
#include <math.h>
#include <stdint.h>
#define CEIL(v) (int)ceil(v)
#define FLOOR(v) ((v) >= 0.0 ? (int)(v) : (int)floor(v))
#define INK8(ink) (*(UINT8 *)ink)
/*
* Rounds around zero (up=away from zero, down=towards zero)
* This guarantees that ROUND_UP|DOWN(f) == -ROUND_UP|DOWN(-f)
*/
#define ROUND_UP(f) ((int)((f) >= 0.0 ? floor((f) + 0.5F) : -floor(fabs(f) + 0.5F)))
#define ROUND_DOWN(f) ((int)((f) >= 0.0 ? ceil((f)-0.5F) : -ceil(fabs(f) - 0.5F)))
/* -------------------------------------------------------------------- */
/* Primitives */
/* -------------------------------------------------------------------- */
typedef struct {
/* edge descriptor for polygon engine */
int d;
int x0, y0;
int xmin, ymin, xmax, ymax;
float dx;
} Edge;
/* Type used in "polygon*" functions */
typedef void (*hline_handler)(Imaging, int, int, int, int);
static inline void
point8(Imaging im, int x, int y, int ink) {
if (x >= 0 && x < im->xsize && y >= 0 && y < im->ysize) {
if (strncmp(im->mode, "I;16", 4) == 0) {
im->image8[y][x * 2] = (UINT8)ink;
im->image8[y][x * 2 + 1] = (UINT8)ink;
} else {
im->image8[y][x] = (UINT8)ink;
}
}
}
static inline void
point32(Imaging im, int x, int y, int ink) {
if (x >= 0 && x < im->xsize && y >= 0 && y < im->ysize) {
im->image32[y][x] = ink;
}
}
static inline void
point32rgba(Imaging im, int x, int y, int ink) {
unsigned int tmp1;
if (x >= 0 && x < im->xsize && y >= 0 && y < im->ysize) {
UINT8 *out = (UINT8 *)im->image[y] + x * 4;
UINT8 *in = (UINT8 *)&ink;
out[0] = BLEND(in[3], out[0], in[0], tmp1);
out[1] = BLEND(in[3], out[1], in[1], tmp1);
out[2] = BLEND(in[3], out[2], in[2], tmp1);
}
}
static inline void
hline8(Imaging im, int x0, int y0, int x1, int ink) {
int tmp, pixelwidth;
if (y0 >= 0 && y0 < im->ysize) {
if (x0 > x1) {
tmp = x0, x0 = x1, x1 = tmp;
}
if (x0 < 0) {
x0 = 0;
} else if (x0 >= im->xsize) {
return;
}
if (x1 < 0) {
return;
} else if (x1 >= im->xsize) {
x1 = im->xsize - 1;
}
if (x0 <= x1) {
pixelwidth = strncmp(im->mode, "I;16", 4) == 0 ? 2 : 1;
memset(
im->image8[y0] + x0 * pixelwidth,
(UINT8)ink,
(x1 - x0 + 1) * pixelwidth);
}
}
}
static inline void
hline32(Imaging im, int x0, int y0, int x1, int ink) {
int tmp;
INT32 *p;
if (y0 >= 0 && y0 < im->ysize) {
if (x0 > x1) {
tmp = x0, x0 = x1, x1 = tmp;
}
if (x0 < 0) {
x0 = 0;
} else if (x0 >= im->xsize) {
return;
}
if (x1 < 0) {
return;
} else if (x1 >= im->xsize) {
x1 = im->xsize - 1;
}
p = im->image32[y0];
while (x0 <= x1) {
p[x0++] = ink;
}
}
}
static inline void
hline32rgba(Imaging im, int x0, int y0, int x1, int ink) {
int tmp;
unsigned int tmp1;
if (y0 >= 0 && y0 < im->ysize) {
if (x0 > x1) {
tmp = x0, x0 = x1, x1 = tmp;
}
if (x0 < 0) {
x0 = 0;
} else if (x0 >= im->xsize) {
return;
}
if (x1 < 0) {
return;
} else if (x1 >= im->xsize) {
x1 = im->xsize - 1;
}
if (x0 <= x1) {
UINT8 *out = (UINT8 *)im->image[y0] + x0 * 4;
UINT8 *in = (UINT8 *)&ink;
while (x0 <= x1) {
out[0] = BLEND(in[3], out[0], in[0], tmp1);
out[1] = BLEND(in[3], out[1], in[1], tmp1);
out[2] = BLEND(in[3], out[2], in[2], tmp1);
x0++;
out += 4;
}
}
}
}
static inline void
line8(Imaging im, int x0, int y0, int x1, int y1, int ink) {
int i, n, e;
int dx, dy;
int xs, ys;
/* normalize coordinates */
dx = x1 - x0;
if (dx < 0) {
dx = -dx, xs = -1;
} else {
xs = 1;
}
dy = y1 - y0;
if (dy < 0) {
dy = -dy, ys = -1;
} else {
ys = 1;
}
n = (dx > dy) ? dx : dy;
if (dx == 0) {
/* vertical */
for (i = 0; i < dy; i++) {
point8(im, x0, y0, ink);
y0 += ys;
}
} else if (dy == 0) {
/* horizontal */
for (i = 0; i < dx; i++) {
point8(im, x0, y0, ink);
x0 += xs;
}
} else if (dx > dy) {
/* bresenham, horizontal slope */
n = dx;
dy += dy;
e = dy - dx;
dx += dx;
for (i = 0; i < n; i++) {
point8(im, x0, y0, ink);
if (e >= 0) {
y0 += ys;
e -= dx;
}
e += dy;
x0 += xs;
}
} else {
/* bresenham, vertical slope */
n = dy;
dx += dx;
e = dx - dy;
dy += dy;
for (i = 0; i < n; i++) {
point8(im, x0, y0, ink);
if (e >= 0) {
x0 += xs;
e -= dy;
}
e += dx;
y0 += ys;
}
}
}
static inline void
line32(Imaging im, int x0, int y0, int x1, int y1, int ink) {
int i, n, e;
int dx, dy;
int xs, ys;
/* normalize coordinates */
dx = x1 - x0;
if (dx < 0) {
dx = -dx, xs = -1;
} else {
xs = 1;
}
dy = y1 - y0;
if (dy < 0) {
dy = -dy, ys = -1;
} else {
ys = 1;
}
n = (dx > dy) ? dx : dy;
if (dx == 0) {
/* vertical */
for (i = 0; i < dy; i++) {
point32(im, x0, y0, ink);
y0 += ys;
}
} else if (dy == 0) {
/* horizontal */
for (i = 0; i < dx; i++) {
point32(im, x0, y0, ink);
x0 += xs;
}
} else if (dx > dy) {
/* bresenham, horizontal slope */
n = dx;
dy += dy;
e = dy - dx;
dx += dx;
for (i = 0; i < n; i++) {
point32(im, x0, y0, ink);
if (e >= 0) {
y0 += ys;
e -= dx;
}
e += dy;
x0 += xs;
}
} else {
/* bresenham, vertical slope */
n = dy;
dx += dx;
e = dx - dy;
dy += dy;
for (i = 0; i < n; i++) {
point32(im, x0, y0, ink);
if (e >= 0) {
x0 += xs;
e -= dy;
}
e += dx;
y0 += ys;
}
}
}
static inline void
line32rgba(Imaging im, int x0, int y0, int x1, int y1, int ink) {
int i, n, e;
int dx, dy;
int xs, ys;
/* normalize coordinates */
dx = x1 - x0;
if (dx < 0) {
dx = -dx, xs = -1;
} else {
xs = 1;
}
dy = y1 - y0;
if (dy < 0) {
dy = -dy, ys = -1;
} else {
ys = 1;
}
n = (dx > dy) ? dx : dy;
if (dx == 0) {
/* vertical */
for (i = 0; i < dy; i++) {
point32rgba(im, x0, y0, ink);
y0 += ys;
}
} else if (dy == 0) {
/* horizontal */
for (i = 0; i < dx; i++) {
point32rgba(im, x0, y0, ink);
x0 += xs;
}
} else if (dx > dy) {
/* bresenham, horizontal slope */
n = dx;
dy += dy;
e = dy - dx;
dx += dx;
for (i = 0; i < n; i++) {
point32rgba(im, x0, y0, ink);
if (e >= 0) {
y0 += ys;
e -= dx;
}
e += dy;
x0 += xs;
}
} else {
/* bresenham, vertical slope */
n = dy;
dx += dx;
e = dx - dy;
dy += dy;
for (i = 0; i < n; i++) {
point32rgba(im, x0, y0, ink);
if (e >= 0) {
x0 += xs;
e -= dy;
}
e += dx;
y0 += ys;
}
}
}
static int
x_cmp(const void *x0, const void *x1) {
float diff = *((float *)x0) - *((float *)x1);
if (diff < 0) {
return -1;
} else if (diff > 0) {
return 1;
} else {
return 0;
}
}
static void
draw_horizontal_lines(
Imaging im, int n, Edge *e, int ink, int *x_pos, int y, hline_handler hline) {
int i;
for (i = 0; i < n; i++) {
if (e[i].ymin == y && e[i].ymin == e[i].ymax) {
int xmax;
int xmin = e[i].xmin;
if (*x_pos < xmin) {
// Line would be after the current position
continue;
}
xmax = e[i].xmax;
if (*x_pos > xmin) {
// Line would be partway through x_pos, so increase the starting point
xmin = *x_pos;
if (xmax < xmin) {
// Line would now end before it started
continue;
}
}
(*hline)(im, xmin, e[i].ymin, xmax, ink);
*x_pos = xmax + 1;
}
}
}
/*
* Filled polygon draw function using scan line algorithm.
*/
static inline int
polygon_generic(Imaging im, int n, Edge *e, int ink, int eofill, hline_handler hline, int hasAlpha) {
Edge **edge_table;
float *xx;
int edge_count = 0;
int ymin = im->ysize - 1;
int ymax = 0;
int i, j, k;
float adjacent_line_x, adjacent_line_x_other_edge;
if (n <= 0) {
return 0;
}
/* Initialize the edge table and find polygon boundaries */
/* malloc check ok, using calloc */
edge_table = calloc(n, sizeof(Edge *));
if (!edge_table) {
return -1;
}
for (i = 0; i < n; i++) {
if (ymin > e[i].ymin) {
ymin = e[i].ymin;
}
if (ymax < e[i].ymax) {
ymax = e[i].ymax;
}
if (e[i].ymin == e[i].ymax) {
if (hasAlpha != 1) {
(*hline)(im, e[i].xmin, e[i].ymin, e[i].xmax, ink);
}
continue;
}
edge_table[edge_count++] = (e + i);
}
if (ymin < 0) {
ymin = 0;
}
if (ymax > im->ysize) {
ymax = im->ysize;
}
/* Process the edge table with a scan line searching for intersections */
/* malloc check ok, using calloc */
xx = calloc(edge_count * 2, sizeof(float));
if (!xx) {
free(edge_table);
return -1;
}
for (; ymin <= ymax; ymin++) {
j = 0;
for (i = 0; i < edge_count; i++) {
Edge *current = edge_table[i];
if (ymin >= current->ymin && ymin <= current->ymax) {
xx[j++] = (ymin - current->y0) * current->dx + current->x0;
if (ymin == current->ymax && ymin < ymax) {
// Needed to draw consistent polygons
xx[j] = xx[j - 1];
j++;
} else if (current->dx != 0 && roundf(xx[j-1]) == xx[j-1]) {
// Connect discontiguous corners
for (k = 0; k < i; k++) {
Edge *other_edge = edge_table[k];
if ((current->dx > 0 && other_edge->dx <= 0) ||
(current->dx < 0 && other_edge->dx >= 0)) {
continue;
}
// Check if the two edges join to make a corner
if (xx[j-1] == (ymin - other_edge->y0) * other_edge->dx + other_edge->x0) {
// Determine points from the edges on the next row
// Or if this is the last row, check the previous row
int offset = ymin == ymax ? -1 : 1;
adjacent_line_x = (ymin + offset - current->y0) * current->dx + current->x0;
adjacent_line_x_other_edge = (ymin + offset - other_edge->y0) * other_edge->dx + other_edge->x0;
if (ymin == current->ymax) {
if (current->dx > 0) {
xx[k] = fmax(adjacent_line_x, adjacent_line_x_other_edge) + 1;
} else {
xx[k] = fmin(adjacent_line_x, adjacent_line_x_other_edge) - 1;
}
} else {
if (current->dx > 0) {
xx[k] = fmin(adjacent_line_x, adjacent_line_x_other_edge);
} else {
xx[k] = fmax(adjacent_line_x, adjacent_line_x_other_edge) + 1;
}
}
break;
}
}
}
}
}
qsort(xx, j, sizeof(float), x_cmp);
if (hasAlpha == 1) {
int x_pos = 0;
for (i = 1; i < j; i += 2) {
int x_end = ROUND_DOWN(xx[i]);
if (x_end < x_pos) {
// Line would be before the current position
continue;
}
draw_horizontal_lines(im, n, e, ink, &x_pos, ymin, hline);
if (x_end < x_pos) {
// Line would be before the current position
continue;
}
int x_start = ROUND_UP(xx[i - 1]);
if (x_pos > x_start) {
// Line would be partway through x_pos, so increase the starting point
x_start = x_pos;
if (x_end < x_start) {
// Line would now end before it started
continue;
}
}
(*hline)(im, x_start, ymin, x_end, ink);
x_pos = x_end + 1;
}
draw_horizontal_lines(im, n, e, ink, &x_pos, ymin, hline);
} else {
for (i = 1; i < j; i += 2) {
(*hline)(im, ROUND_UP(xx[i - 1]), ymin, ROUND_DOWN(xx[i]), ink);
}
}
}
free(xx);
free(edge_table);
return 0;
}
static inline int
polygon8(Imaging im, int n, Edge *e, int ink, int eofill) {
return polygon_generic(im, n, e, ink, eofill, hline8, 0);
}
static inline int
polygon32(Imaging im, int n, Edge *e, int ink, int eofill) {
return polygon_generic(im, n, e, ink, eofill, hline32, 0);
}
static inline int
polygon32rgba(Imaging im, int n, Edge *e, int ink, int eofill) {
return polygon_generic(im, n, e, ink, eofill, hline32rgba, 1);
}
static inline void
add_edge(Edge *e, int x0, int y0, int x1, int y1) {
/* printf("edge %d %d %d %d\n", x0, y0, x1, y1); */
if (x0 <= x1) {
e->xmin = x0, e->xmax = x1;
} else {
e->xmin = x1, e->xmax = x0;
}
if (y0 <= y1) {
e->ymin = y0, e->ymax = y1;
} else {
e->ymin = y1, e->ymax = y0;
}
if (y0 == y1) {
e->d = 0;
e->dx = 0.0;
} else {
e->dx = ((float)(x1 - x0)) / (y1 - y0);
if (y0 == e->ymin) {
e->d = 1;
} else {
e->d = -1;
}
}
e->x0 = x0;
e->y0 = y0;
}
typedef struct {
void (*point)(Imaging im, int x, int y, int ink);
void (*hline)(Imaging im, int x0, int y0, int x1, int ink);
void (*line)(Imaging im, int x0, int y0, int x1, int y1, int ink);
int (*polygon)(Imaging im, int n, Edge *e, int ink, int eofill);
} DRAW;
DRAW draw8 = {point8, hline8, line8, polygon8};
DRAW draw32 = {point32, hline32, line32, polygon32};
DRAW draw32rgba = {point32rgba, hline32rgba, line32rgba, polygon32rgba};
/* -------------------------------------------------------------------- */
/* Interface */
/* -------------------------------------------------------------------- */
#define DRAWINIT() \
if (im->image8) { \
draw = &draw8; \
ink = INK8(ink_); \
} else { \
draw = (op) ? &draw32rgba : &draw32; \
memcpy(&ink, ink_, sizeof(ink)); \
}
int
ImagingDrawPoint(Imaging im, int x0, int y0, const void *ink_, int op) {
DRAW *draw;
INT32 ink;
DRAWINIT();
draw->point(im, x0, y0, ink);
return 0;
}
int
ImagingDrawLine(Imaging im, int x0, int y0, int x1, int y1, const void *ink_, int op) {
DRAW *draw;
INT32 ink;
DRAWINIT();
draw->line(im, x0, y0, x1, y1, ink);
return 0;
}
int
ImagingDrawWideLine(
Imaging im, int x0, int y0, int x1, int y1, const void *ink_, int width, int op) {
DRAW *draw;
INT32 ink;
int dx, dy;
double big_hypotenuse, small_hypotenuse, ratio_max, ratio_min;
int dxmin, dxmax, dymin, dymax;
Edge e[4];
DRAWINIT();
dx = x1 - x0;
dy = y1 - y0;
if (dx == 0 && dy == 0) {
draw->point(im, x0, y0, ink);
return 0;
}
big_hypotenuse = hypot(dx, dy);
small_hypotenuse = (width - 1) / 2.0;
ratio_max = ROUND_UP(small_hypotenuse) / big_hypotenuse;
ratio_min = ROUND_DOWN(small_hypotenuse) / big_hypotenuse;
dxmin = ROUND_DOWN(ratio_min * dy);
dxmax = ROUND_DOWN(ratio_max * dy);
dymin = ROUND_DOWN(ratio_min * dx);
dymax = ROUND_DOWN(ratio_max * dx);
{
int vertices[4][2] = {
{x0 - dxmin, y0 + dymax},
{x1 - dxmin, y1 + dymax},
{x1 + dxmax, y1 - dymin},
{x0 + dxmax, y0 - dymin}};
add_edge(e + 0, vertices[0][0], vertices[0][1], vertices[1][0], vertices[1][1]);
add_edge(e + 1, vertices[1][0], vertices[1][1], vertices[2][0], vertices[2][1]);
add_edge(e + 2, vertices[2][0], vertices[2][1], vertices[3][0], vertices[3][1]);
add_edge(e + 3, vertices[3][0], vertices[3][1], vertices[0][0], vertices[0][1]);
draw->polygon(im, 4, e, ink, 0);
}
return 0;
}
int
ImagingDrawRectangle(
Imaging im,
int x0,
int y0,
int x1,
int y1,
const void *ink_,
int fill,
int width,
int op) {
int i;
int y;
int tmp;
DRAW *draw;
INT32 ink;
DRAWINIT();
if (y0 > y1) {
tmp = y0, y0 = y1, y1 = tmp;
}
if (fill) {
if (y0 < 0) {
y0 = 0;
} else if (y0 >= im->ysize) {
return 0;
}
if (y1 < 0) {
return 0;
} else if (y1 > im->ysize) {
y1 = im->ysize;
}
for (y = y0; y <= y1; y++) {
draw->hline(im, x0, y, x1, ink);
}
} else {
/* outline */
if (width == 0) {
width = 1;
}
for (i = 0; i < width; i++) {
draw->hline(im, x0, y0 + i, x1, ink);
draw->hline(im, x0, y1 - i, x1, ink);
draw->line(im, x1 - i, y0 + width, x1 - i, y1 - width + 1, ink);
draw->line(im, x0 + i, y0 + width, x0 + i, y1 - width + 1, ink);
}
}
return 0;
}
int
ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, int width, int op) {
int i, n, x0, y0, x1, y1;
DRAW *draw;
INT32 ink;
if (count <= 0) {
return 0;
}
DRAWINIT();
if (fill) {
/* Build edge list */
/* malloc check ok, using calloc */
Edge *e = calloc(count, sizeof(Edge));
if (!e) {
(void)ImagingError_MemoryError();
return -1;
}
for (i = n = 0; i < count - 1; i++) {
x0 = xy[i * 2];
y0 = xy[i * 2 + 1];
x1 = xy[i * 2 + 2];
y1 = xy[i * 2 + 3];
if (y0 == y1 && i != 0 && y0 == xy[i * 2 - 1]) {
// This is a horizontal line,
// that immediately follows another horizontal line
Edge *last_e = &e[n-1];
if (x1 > x0 && x0 > xy[i * 2 - 2]) {
// They are both increasing in x
last_e->xmax = x1;
continue;
} else if (x1 < x0 && x0 < xy[i * 2 - 2]) {
// They are both decreasing in x
last_e->xmin = x1;
continue;
}
}
add_edge(&e[n++], x0, y0, x1, y1);
}
if (xy[i * 2] != xy[0] || xy[i * 2 + 1] != xy[1]) {
add_edge(&e[n++], xy[i * 2], xy[i * 2 + 1], xy[0], xy[1]);
}
draw->polygon(im, n, e, ink, 0);
free(e);
} else {
/* Outline */
if (width == 1) {
for (i = 0; i < count - 1; i++) {
draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], ink);
}
draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink);
} else {
for (i = 0; i < count - 1; i++) {
ImagingDrawWideLine(im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], ink_, width, op);
}
ImagingDrawWideLine(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink_, width, op);
}
}
return 0;
}
int
ImagingDrawBitmap(Imaging im, int x0, int y0, Imaging bitmap, const void *ink, int op) {
return ImagingFill2(
im, ink, bitmap, x0, y0, x0 + bitmap->xsize, y0 + bitmap->ysize);
}
/* -------------------------------------------------------------------- */
/* standard shapes */
// Imagine 2D plane and ellipse with center in (0, 0) and semi-major axes a and b.
// Then quarter_* stuff approximates its top right quarter (x, y >= 0) with integer
// points from set {(2x+x0, 2y+y0) | x,y in Z} where x0, y0 are from {0, 1} and
// are such that point (a, b) is in the set.
typedef struct {
int32_t a, b, cx, cy, ex, ey;
int64_t a2, b2, a2b2;
int8_t finished;
} quarter_state;
void
quarter_init(quarter_state *s, int32_t a, int32_t b) {
if (a < 0 || b < 0) {
s->finished = 1;
} else {
s->a = a;
s->b = b;
s->cx = a;
s->cy = b % 2;
s->ex = a % 2;
s->ey = b;
s->a2 = a * a;
s->b2 = b * b;
s->a2b2 = s->a2 * s->b2;
s->finished = 0;
}
}
// deviation of the point from ellipse curve, basically a substitution
// of the point into the ellipse equation
int64_t
quarter_delta(quarter_state *s, int64_t x, int64_t y) {
return llabs(s->a2 * y * y + s->b2 * x * x - s->a2b2);
}
int8_t
quarter_next(quarter_state *s, int32_t *ret_x, int32_t *ret_y) {
if (s->finished) {
return -1;
}
*ret_x = s->cx;
*ret_y = s->cy;
if (s->cx == s->ex && s->cy == s->ey) {
s->finished = 1;
} else {
// Bresenham's algorithm, possible optimization: only consider 2 of 3
// next points depending on current slope
int32_t nx = s->cx;
int32_t ny = s->cy + 2;
int64_t ndelta = quarter_delta(s, nx, ny);
if (nx > 1) {
int64_t newdelta = quarter_delta(s, s->cx - 2, s->cy + 2);
if (ndelta > newdelta) {
nx = s->cx - 2;
ny = s->cy + 2;
ndelta = newdelta;
}
newdelta = quarter_delta(s, s->cx - 2, s->cy);
if (ndelta > newdelta) {
nx = s->cx - 2;
ny = s->cy;
}
}
s->cx = nx;
s->cy = ny;
}
return 0;
}
// quarter_* stuff can "draw" a quarter of an ellipse with thickness 1, great.
// Now we use ellipse_* stuff to join all four quarters of two different sized
// ellipses and receive horizontal segments of a complete ellipse with
// specified thickness.
//
// Still using integer grid with step 2 at this point (like in quarter_*)
// to ease angle clipping in future.
typedef struct {
quarter_state st_o, st_i;
int32_t py, pl, pr;
int32_t cy[4], cl[4], cr[4];
int8_t bufcnt;
int8_t finished;
int8_t leftmost;
} ellipse_state;
void
ellipse_init(ellipse_state *s, int32_t a, int32_t b, int32_t w) {
s->bufcnt = 0;
s->leftmost = a % 2;
quarter_init(&s->st_o, a, b);
if (w < 1 || quarter_next(&s->st_o, &s->pr, &s->py) == -1) {
s->finished = 1;
} else {
s->finished = 0;
quarter_init(&s->st_i, a - 2 * (w - 1), b - 2 * (w - 1));
s->pl = s->leftmost;
}
}
int8_t
ellipse_next(ellipse_state *s, int32_t *ret_x0, int32_t *ret_y, int32_t *ret_x1) {
if (s->bufcnt == 0) {
if (s->finished) {
return -1;
}
int32_t y = s->py;
int32_t l = s->pl;
int32_t r = s->pr;
int32_t cx = 0, cy = 0;
int8_t next_ret;
while ((next_ret = quarter_next(&s->st_o, &cx, &cy)) != -1 && cy <= y) {
}
if (next_ret == -1) {
s->finished = 1;
} else {
s->pr = cx;
s->py = cy;
}
while ((next_ret = quarter_next(&s->st_i, &cx, &cy)) != -1 && cy <= y) {
l = cx;
}
s->pl = next_ret == -1 ? s->leftmost : cx;
if ((l > 0 || l < r) && y > 0) {
s->cl[s->bufcnt] = l == 0 ? 2 : l;
s->cy[s->bufcnt] = y;
s->cr[s->bufcnt] = r;
++s->bufcnt;
}
if (y > 0) {
s->cl[s->bufcnt] = -r;
s->cy[s->bufcnt] = y;
s->cr[s->bufcnt] = -l;
++s->bufcnt;
}
if (l > 0 || l < r) {
s->cl[s->bufcnt] = l == 0 ? 2 : l;
s->cy[s->bufcnt] = -y;
s->cr[s->bufcnt] = r;
++s->bufcnt;
}
s->cl[s->bufcnt] = -r;
s->cy[s->bufcnt] = -y;
s->cr[s->bufcnt] = -l;
++s->bufcnt;
}
--s->bufcnt;
*ret_x0 = s->cl[s->bufcnt];