-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdraw.c
2162 lines (1717 loc) · 50 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
#define IMAGER_NO_CONTEXT
#include "imager.h"
#include "draw.h"
#include "log.h"
#include "imageri.h"
#include "imrender.h"
#include <limits.h>
#define NDEBUG
#include <assert.h>
int
i_ppix_norm(i_img *im, i_img_dim x, i_img_dim y, i_color const *col) {
i_color src;
i_color work;
int dest_alpha;
int remains;
if (!col->channel[3])
return 0;
switch (im->channels) {
case 1:
work = *col;
i_adapt_colors(2, 4, &work, 1);
i_gpix(im, x, y, &src);
remains = 255 - work.channel[1];
src.channel[0] = (src.channel[0] * remains
+ work.channel[0] * work.channel[1]) / 255;
return i_ppix(im, x, y, &src);
case 2:
work = *col;
i_adapt_colors(2, 4, &work, 1);
i_gpix(im, x, y, &src);
remains = 255 - work.channel[1];
dest_alpha = work.channel[1] + remains * src.channel[1] / 255;
if (work.channel[1] == 255) {
return i_ppix(im, x, y, &work);
}
else {
src.channel[0] = (work.channel[1] * work.channel[0]
+ remains * src.channel[0] * src.channel[1] / 255) / dest_alpha;
src.channel[1] = dest_alpha;
return i_ppix(im, x, y, &src);
}
case 3:
work = *col;
i_gpix(im, x, y, &src);
remains = 255 - work.channel[3];
src.channel[0] = (src.channel[0] * remains
+ work.channel[0] * work.channel[3]) / 255;
src.channel[1] = (src.channel[1] * remains
+ work.channel[1] * work.channel[3]) / 255;
src.channel[2] = (src.channel[2] * remains
+ work.channel[2] * work.channel[3]) / 255;
return i_ppix(im, x, y, &src);
case 4:
work = *col;
i_gpix(im, x, y, &src);
remains = 255 - work.channel[3];
dest_alpha = work.channel[3] + remains * src.channel[3] / 255;
if (work.channel[3] == 255) {
return i_ppix(im, x, y, &work);
}
else {
src.channel[0] = (work.channel[3] * work.channel[0]
+ remains * src.channel[0] * src.channel[3] / 255) / dest_alpha;
src.channel[1] = (work.channel[3] * work.channel[1]
+ remains * src.channel[1] * src.channel[3] / 255) / dest_alpha;
src.channel[2] = (work.channel[3] * work.channel[2]
+ remains * src.channel[2] * src.channel[3] / 255) / dest_alpha;
src.channel[3] = dest_alpha;
return i_ppix(im, x, y, &src);
}
}
return 0;
}
static void
cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax);
void
i_mmarray_cr(i_mmarray *ar,i_img_dim l) {
i_img_dim i;
size_t alloc_size;
ar->lines=l;
alloc_size = sizeof(minmax) * l;
/* check for overflow */
if (alloc_size / l != sizeof(minmax)) {
fprintf(stderr, "overflow calculating memory allocation");
exit(3);
}
ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
for(i=0;i<l;i++) {
ar->data[i].max = -1;
ar->data[i].min = i_img_dim_MAX;
}
}
void
i_mmarray_dst(i_mmarray *ar) {
ar->lines=0;
if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
}
void
i_mmarray_add(i_mmarray *ar,i_img_dim x,i_img_dim y) {
if (y>-1 && y<ar->lines)
{
if (x<ar->data[y].min) ar->data[y].min=x;
if (x>ar->data[y].max) ar->data[y].max=x;
}
}
i_img_dim
i_mmarray_gmin(i_mmarray *ar,i_img_dim y) {
if (y>-1 && y<ar->lines) return ar->data[y].min;
else return -1;
}
i_img_dim
i_mmarray_getm(i_mmarray *ar,i_img_dim y) {
if (y>-1 && y<ar->lines)
return ar->data[y].max;
else
return i_img_dim_MAX;
}
#if 0
/* unused? */
void
i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
i_img_dim i,x;
for(i=0;i<ar->lines;i++) if (ar->data[i].max!=-1) for(x=ar->data[i].min;x<ar->data[i].max;x++) i_ppix(im,x,i,val);
}
#endif
static
void
i_arcdraw(i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_mmarray *ar) {
double alpha;
double dsec;
i_img_dim temp;
alpha=(double)(y2-y1)/(double)(x2-x1);
if (fabs(alpha) <= 1)
{
if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
dsec=y1;
while(x1<=x2)
{
i_mmarray_add(ar,x1,(i_img_dim)(dsec+0.5));
dsec+=alpha;
x1++;
}
}
else
{
alpha=1/alpha;
if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
dsec=x1;
while(y1<=y2)
{
i_mmarray_add(ar,(i_img_dim)(dsec+0.5),y1);
dsec+=alpha;
y1++;
}
}
}
void
i_mmarray_info(i_mmarray *ar) {
i_img_dim i;
for(i=0;i<ar->lines;i++)
if (ar->data[i].max!=-1)
printf("line %"i_DF ": min=%" i_DF ", max=%" i_DF ".\n",
i_DFc(i), i_DFc(ar->data[i].min), i_DFc(ar->data[i].max));
}
static void
i_arc_minmax(i_int_hlines *hlines,i_img_dim x,i_img_dim y, double rad,float d1,float d2) {
i_mmarray dot;
double f;
i_img_dim x1,y1;
i_mmarray_cr(&dot, hlines->limit_y);
x1=(i_img_dim)(x+0.5+rad*cos(d1*PI/180.0));
y1=(i_img_dim)(y+0.5+rad*sin(d1*PI/180.0));
/* printf("x1: %d.\ny1: %d.\n",x1,y1); */
i_arcdraw(x, y, x1, y1, &dot);
x1=(i_img_dim)(x+0.5+rad*cos(d2*PI/180.0));
y1=(i_img_dim)(y+0.5+rad*sin(d2*PI/180.0));
for(f=d1;f<=d2;f+=0.01)
i_mmarray_add(&dot,(i_img_dim)(x+0.5+rad*cos(f*PI/180.0)),(i_img_dim)(y+0.5+rad*sin(f*PI/180.0)));
/* printf("x1: %d.\ny1: %d.\n",x1,y1); */
i_arcdraw(x, y, x1, y1, &dot);
/* render the minmax values onto the hlines */
for (y = 0; y < dot.lines; y++) {
if (dot.data[y].max!=-1) {
i_img_dim minx, width;
minx = dot.data[y].min;
width = dot.data[y].max - dot.data[y].min + 1;
i_int_hlines_add(hlines, y, minx, width);
}
}
/* dot.info(); */
i_mmarray_dst(&dot);
}
static void
i_arc_hlines(i_int_hlines *hlines,i_img_dim x,i_img_dim y,double rad,float d1,float d2) {
if (d1 <= d2) {
i_arc_minmax(hlines, x, y, rad, d1, d2);
}
else {
i_arc_minmax(hlines, x, y, rad, d1, 360);
i_arc_minmax(hlines, x, y, rad, 0, d2);
}
}
/*
=item i_arc(im, x, y, rad, d1, d2, color)
=category Drawing
=synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
Fills an arc centered at (x,y) with radius I<rad> covering the range
of angles in degrees from d1 to d2, with the color.
=cut
*/
void
i_arc(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,const i_color *val) {
i_int_hlines hlines;
dIMCTXim(im);
im_log((aIMCTX,1,"i_arc(im %p,(x,y)=(" i_DFp "), rad %f, d1 %f, d2 %f, col %p)",
im, i_DFcp(x, y), rad, d1, d2, val));
i_int_init_hlines_img(&hlines, im);
i_arc_hlines(&hlines, x, y, rad, d1, d2);
i_int_hlines_fill_color(im, &hlines, val);
i_int_hlines_destroy(&hlines);
}
/*
=item i_arc_cfill(im, x, y, rad, d1, d2, fill)
=category Drawing
=synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
Fills an arc centered at (x,y) with radius I<rad> covering the range
of angles in degrees from d1 to d2, with the fill object.
=cut
*/
#define MIN_CIRCLE_STEPS 8
#define MAX_CIRCLE_STEPS 360
void
i_arc_cfill(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,i_fill_t *fill) {
i_int_hlines hlines;
dIMCTXim(im);
im_log((aIMCTX,1,"i_arc_cfill(im %p,(x,y)=(" i_DFp "), rad %f, d1 %f, d2 %f, fill %p)",
im, i_DFcp(x, y), rad, d1, d2, fill));
i_int_init_hlines_img(&hlines, im);
i_arc_hlines(&hlines, x, y, rad, d1, d2);
i_int_hlines_fill_fill(im, &hlines, fill);
i_int_hlines_destroy(&hlines);
}
static void
arc_poly(int *count, double **xvals, double **yvals,
double x, double y, double rad, double d1, double d2) {
double d1_rad, d2_rad;
double circum;
i_img_dim steps, point_count;
double angle_inc;
/* normalize the angles */
d1 = fmod(d1, 360);
if (d1 == 0) {
if (d2 >= 360) { /* default is 361 */
d2 = 360;
}
else {
d2 = fmod(d2, 360);
if (d2 < d1)
d2 += 360;
}
}
else {
d2 = fmod(d2, 360);
if (d2 < d1)
d2 += 360;
}
d1_rad = d1 * PI / 180;
d2_rad = d2 * PI / 180;
/* how many segments for the curved part?
we do a maximum of one per degree, with a minimum of 8/circle
we try to aim at having about one segment per 2 pixels
Work it out per circle to get a step size.
I was originally making steps = circum/2 but that looked horrible.
I think there might be an issue in the polygon filler.
*/
circum = 2 * PI * rad;
steps = circum;
if (steps > MAX_CIRCLE_STEPS)
steps = MAX_CIRCLE_STEPS;
else if (steps < MIN_CIRCLE_STEPS)
steps = MIN_CIRCLE_STEPS;
angle_inc = 2 * PI / steps;
point_count = steps + 5; /* rough */
/* point_count is always relatively small, so allocation won't overflow */
*xvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
*yvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
/* from centre to edge at d1 */
(*xvals)[0] = x;
(*yvals)[0] = y;
(*xvals)[1] = x + rad * cos(d1_rad);
(*yvals)[1] = y + rad * sin(d1_rad);
*count = 2;
/* step around the curve */
while (d1_rad < d2_rad) {
(*xvals)[*count] = x + rad * cos(d1_rad);
(*yvals)[*count] = y + rad * sin(d1_rad);
++*count;
d1_rad += angle_inc;
}
/* finish off the curve */
(*xvals)[*count] = x + rad * cos(d2_rad);
(*yvals)[*count] = y + rad * sin(d2_rad);
++*count;
}
/*
=item i_arc_aa(im, x, y, rad, d1, d2, color)
=category Drawing
=synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
the range of angles in degrees from d1 to d2, with the color.
=cut
*/
void
i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
const i_color *val) {
double *xvals, *yvals;
int count;
dIMCTXim(im);
im_log((aIMCTX,1,"i_arc_aa(im %p,(x,y)=(%f,%f), rad %f, d1 %f, d2 %f, col %p)",
im, x, y, rad, d1, d2, val));
arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
i_poly_aa(im, count, xvals, yvals, val);
myfree(xvals);
myfree(yvals);
}
/*
=item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
=category Drawing
=synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
the range of angles in degrees from d1 to d2, with the fill object.
=cut
*/
void
i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
i_fill_t *fill) {
double *xvals, *yvals;
int count;
dIMCTXim(im);
im_log((aIMCTX,1,"i_arc_aa_cfill(im %p,(x,y)=(%f,%f), rad %f, d1 %f, d2 %f, fill %p)",
im, x, y, rad, d1, d2, fill));
arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
i_poly_aa_cfill(im, count, xvals, yvals, fill);
myfree(xvals);
myfree(yvals);
}
typedef i_img_dim frac;
static frac float_to_frac(double x) { return (frac)(0.5+x*16.0); }
typedef void
(*flush_render_t)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_sample_t *cover, void *ctx);
static void
i_circle_aa_low(i_img *im, double x, double y, double rad, flush_render_t r, void *ctx);
static void
scanline_flush_color(i_img *im, i_img_dim l, i_img_dim y, i_img_dim width, const i_sample_t *cover, void *ctx);
static void
scanline_flush_fill(i_img *im, i_img_dim l, i_img_dim y, i_img_dim width, const i_sample_t *cover, void *ctx);
typedef struct {
i_render r;
i_color c;
} flush_color_t;
typedef struct {
i_render r;
i_fill_t *fill;
} flush_fill_t;
/*
=item i_circle_aa(im, x, y, rad, color)
=category Drawing
=synopsis i_circle_aa(im, 50, 50, 45, &color);
Anti-alias fills a circle centered at (x,y) for radius I<rad> with
color.
=cut
*/
void
i_circle_aa(i_img *im, double x, double y, double rad, const i_color *val) {
flush_color_t fc;
fc.c = *val;
i_render_init(&fc.r, im, rad * 2 + 1);
i_circle_aa_low(im, x, y, rad, scanline_flush_color, &fc);
i_render_done(&fc.r);
}
/*
=item i_circle_aa_fill(im, x, y, rad, fill)
=category Drawing
=synopsis i_circle_aa_fill(im, 50, 50, 45, fill);
Anti-alias fills a circle centered at (x,y) for radius I<rad> with
fill.
=cut
*/
void
i_circle_aa_fill(i_img *im, double x, double y, double rad, i_fill_t *fill) {
flush_fill_t ff;
ff.fill = fill;
i_render_init(&ff.r, im, rad * 2 + 1);
i_circle_aa_low(im, x, y, rad, scanline_flush_fill, &ff);
i_render_done(&ff.r);
}
static void
i_circle_aa_low(i_img *im, double x, double y, double rad, flush_render_t r,
void *ctx) {
i_color temp;
i_img_dim ly;
dIMCTXim(im);
double ceil_rad = ceil(rad);
i_img_dim first_row = floor(y) - ceil_rad;
i_img_dim last_row = ceil(y) + ceil_rad;
i_img_dim first_col = floor(x) - ceil_rad;
i_img_dim last_col = ceil(x) + ceil_rad;
double r_sqr = rad * rad;
i_img_dim max_width = 2 * ceil(rad) + 1;
unsigned char *coverage = NULL;
size_t coverage_size;
int sub;
im_log((aIMCTX, 1, "i_circle_aa_low(im %p, centre(" i_DFp "), rad %.2f, r %p, ctx %p)\n",
im, i_DFcp(x, y), rad, r, ctx));
if (first_row < 0)
first_row = 0;
if (last_row > im->ysize-1)
last_row = im->ysize - 1;
if (first_col < 0)
first_col = 0;
if (last_col > im->xsize-1)
last_col = im->xsize - 1;
if (rad <= 0 || last_row < first_row || last_col < first_col) {
/* outside the image */
return;
}
coverage_size = max_width;
coverage = mymalloc(coverage_size);
for(ly = first_row; ly < last_row; ly++) {
frac min_frac_x[16];
frac max_frac_x[16];
i_img_dim min_frac_left_x = 16 *(ceil(x) + ceil(rad));
i_img_dim max_frac_left_x = -1;
i_img_dim min_frac_right_x = 16 * (floor(x) - ceil(rad));
i_img_dim max_frac_right_x = -1;
/* reset work_y each row so the error doesn't build up */
double work_y = ly;
double dy, dy_sqr;
for (sub = 0; sub < 16; ++sub) {
work_y += 1.0 / 16.0;
dy = work_y - y;
dy_sqr = dy * dy;
if (dy_sqr < r_sqr) {
double dx = sqrt(r_sqr - dy_sqr);
double left_x = x - dx;
double right_x = x + dx;
frac frac_left_x = float_to_frac(left_x);
frac frac_right_x = float_to_frac(right_x);
if (frac_left_x < min_frac_left_x)
min_frac_left_x = frac_left_x;
if (frac_left_x > max_frac_left_x)
max_frac_left_x = frac_left_x;
if (frac_right_x < min_frac_right_x)
min_frac_right_x = frac_right_x;
if (frac_right_x > max_frac_right_x)
max_frac_right_x = frac_right_x;
min_frac_x[sub] = frac_left_x;
max_frac_x[sub] = frac_right_x;
}
else {
min_frac_x[sub] = max_frac_x[sub] = 0;
max_frac_left_x = im->xsize * 16;
min_frac_right_x = -1;
}
}
if (min_frac_left_x != -1) {
/* something to draw on this line */
i_img_dim min_x = (min_frac_left_x / 16);
i_img_dim max_x = (max_frac_right_x + 15) / 16;
i_img_dim left_solid = (max_frac_left_x + 15) / 16;
i_img_dim right_solid = min_frac_right_x / 16;
i_img_dim work_x;
i_img_dim frac_work_x;
i_sample_t *cout = coverage;
for (work_x = min_x, frac_work_x = min_x * 16;
work_x <= max_x;
++work_x, frac_work_x += 16) {
if (work_x <= left_solid || work_x >= right_solid) {
int pix_coverage = 0;
int ch;
double ratio;
i_img_dim frac_work_right = frac_work_x + 16;
for (sub = 0; sub < 16; ++sub) {
frac pix_left = min_frac_x[sub];
frac pix_right = max_frac_x[sub];
if (pix_left < pix_right
&& pix_left < frac_work_right
&& pix_right >= frac_work_x) {
if (pix_left < frac_work_x)
pix_left = frac_work_x;
if (pix_right > frac_work_right)
pix_right = frac_work_right;
pix_coverage += pix_right - pix_left;
}
}
assert(pix_coverage <= 256);
*cout++ = pix_coverage * 255 / 256;
}
else {
/* full coverage */
*cout++ = 255;
}
}
r(im, min_x, ly, max_x - min_x + 1, coverage, ctx);
}
}
myfree(coverage);
}
static void
scanline_flush_color(i_img *im, i_img_dim x, i_img_dim y, i_img_dim width, const unsigned char *cover, void *ctx) {
flush_color_t *fc = ctx;
i_render_color(&fc->r, x, y, width, cover, &fc->c);
}
static void
scanline_flush_fill(i_img *im, i_img_dim x, i_img_dim y, i_img_dim width, const unsigned char *cover, void *ctx) {
flush_fill_t *ff = ctx;
i_render_fill(&ff->r, x, y, width, cover, ff->fill);
}
/*
=item i_circle_out(im, x, y, r, col)
=category Drawing
=synopsis i_circle_out(im, 50, 50, 45, &color);
Draw a circle outline centered at (x,y) with radius r,
non-anti-aliased.
Parameters:
=over
=item *
(x, y) - the center of the circle
=item *
r - the radius of the circle in pixels, must be non-negative
=back
Returns non-zero on success.
Implementation:
=cut
*/
int
i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
const i_color *col) {
i_img_dim x, y;
i_img_dim dx, dy;
int error;
dIMCTXim(im);
im_log((aIMCTX, 1, "i_circle_out(im %p, centre(" i_DFp "), rad %" i_DF ", col %p)\n",
im, i_DFcp(xc, yc), i_DFc(r), col));
im_clear_error(aIMCTX);
if (r < 0) {
im_push_error(aIMCTX, 0, "circle: radius must be non-negative");
return 0;
}
i_ppix(im, xc+r, yc, col);
i_ppix(im, xc-r, yc, col);
i_ppix(im, xc, yc+r, col);
i_ppix(im, xc, yc-r, col);
x = 0;
y = r;
dx = 1;
dy = -2 * r;
error = 1 - r;
while (x < y) {
if (error >= 0) {
--y;
dy += 2;
error += dy;
}
++x;
dx += 2;
error += dx;
i_ppix(im, xc + x, yc + y, col);
i_ppix(im, xc + x, yc - y, col);
i_ppix(im, xc - x, yc + y, col);
i_ppix(im, xc - x, yc - y, col);
if (x != y) {
i_ppix(im, xc + y, yc + x, col);
i_ppix(im, xc + y, yc - x, col);
i_ppix(im, xc - y, yc + x, col);
i_ppix(im, xc - y, yc - x, col);
}
}
return 1;
}
/*
=item arc_seg(angle)
Convert an angle in degrees into an angle measure we can generate
simply from the numbers we have when drawing the circle.
=cut
*/
static i_img_dim
arc_seg(double angle, int scale) {
i_img_dim seg = (angle + 45) / 90;
double remains = angle - seg * 90; /* should be in the range [-45,45] */
while (seg > 4)
seg -= 4;
if (seg == 4 && remains > 0)
seg = 0;
return scale * (seg * 2 + sin(remains * PI/180));
}
/*
=item i_arc_out(im, x, y, r, d1, d2, col)
=category Drawing
=synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
over the angle range d1 through d2 degrees.
Parameters:
=over
=item *
(x, y) - the center of the circle
=item *
r - the radius of the circle in pixels, must be non-negative
=item *
d1, d2 - the range of angles to draw the arc over, in degrees.
=back
Returns non-zero on success.
Implementation:
=cut
*/
int
i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
double d1, double d2, const i_color *col) {
i_img_dim x, y;
i_img_dim dx, dy;
int error;
i_img_dim segs[2][2];
int seg_count;
i_img_dim sin_th;
i_img_dim seg_d1, seg_d2;
int seg_num;
i_img_dim scale = r + 1;
i_img_dim seg1 = scale * 2;
i_img_dim seg2 = scale * 4;
i_img_dim seg3 = scale * 6;
i_img_dim seg4 = scale * 8;
dIMCTXim(im);
im_log((aIMCTX,1,"i_arc_out(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
im_clear_error(aIMCTX);
if (r <= 0) {
im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
return 0;
}
if (d1 + 360 <= d2)
return i_circle_out(im, xc, yc, r, col);
if (d1 < 0)
d1 += 360 * floor((-d1 + 359) / 360);
if (d2 < 0)
d2 += 360 * floor((-d2 + 359) / 360);
d1 = fmod(d1, 360);
d2 = fmod(d2, 360);
seg_d1 = arc_seg(d1, scale);
seg_d2 = arc_seg(d2, scale);
if (seg_d2 < seg_d1) {
/* split into two segments */
segs[0][0] = 0;
segs[0][1] = seg_d2;
segs[1][0] = seg_d1;
segs[1][1] = seg4;
seg_count = 2;
}
else {
segs[0][0] = seg_d1;
segs[0][1] = seg_d2;
seg_count = 1;
}
for (seg_num = 0; seg_num < seg_count; ++seg_num) {
i_img_dim seg_start = segs[seg_num][0];
i_img_dim seg_end = segs[seg_num][1];
if (seg_start == 0)
i_ppix(im, xc+r, yc, col);
if (seg_start <= seg1 && seg_end >= seg1)
i_ppix(im, xc, yc+r, col);
if (seg_start <= seg2 && seg_end >= seg2)
i_ppix(im, xc-r, yc, col);
if (seg_start <= seg3 && seg_end >= seg3)
i_ppix(im, xc, yc-r, col);
y = 0;
x = r;
dy = 1;
dx = -2 * r;
error = 1 - r;
while (y < x) {
if (error >= 0) {
--x;
dx += 2;
error += dx;
}
++y;
dy += 2;
error += dy;
sin_th = y;
if (seg_start <= sin_th && seg_end >= sin_th)
i_ppix(im, xc + x, yc + y, col);
if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
i_ppix(im, xc + y, yc + x, col);
if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
i_ppix(im, xc - y, yc + x, col);
if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
i_ppix(im, xc - x, yc + y, col);
if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
i_ppix(im, xc - x, yc - y, col);
if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
i_ppix(im, xc - y, yc - x, col);
if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
i_ppix(im, xc + y, yc - x, col);
if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
i_ppix(im, xc + x, yc - y, col);
}
}
return 1;
}
static double
cover(i_img_dim r, i_img_dim j) {
double rjsqrt = sqrt(r*r - j*j);
return ceil(rjsqrt) - rjsqrt;
}
/*
=item i_circle_out_aa(im, xc, yc, r, col)
=synopsis i_circle_out_aa(im, 50, 50, 45, &color);
Draw a circle outline centered at (x,y) with radius r, anti-aliased.
Parameters:
=over
=item *
(xc, yc) - the center of the circle
=item *
r - the radius of the circle in pixels, must be non-negative
=item *
col - an i_color for the color to draw in.
=back
Returns non-zero on success.
=cut
Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
Gems.
I use floating point for I<D> since for large circles the precision of
a [0,255] value isn't sufficient when approaching the end of the
octant.
*/
int
i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
i_img_dim i, j;
double t;
i_color workc = *col;
int orig_alpha = col->channel[3];
dIMCTXim(im);
im_log((aIMCTX,1,"i_circle_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", col %p)",
im, i_DFcp(xc, yc), i_DFc(r), col));
im_clear_error(aIMCTX);
if (r <= 0) {
im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
return 0;
}
i = r;
j = 0;
t = 0;
i_ppix_norm(im, xc+i, yc+j, col);
i_ppix_norm(im, xc-i, yc+j, col);
i_ppix_norm(im, xc+j, yc+i, col);
i_ppix_norm(im, xc+j, yc-i, col);
while (i > j+1) {
double d;
int cv, inv_cv;
j++;
d = cover(r, j);
cv = (int)(d * 255 + 0.5);
inv_cv = 255-cv;
if (d < t) {
--i;
}
if (inv_cv) {
workc.channel[3] = orig_alpha * inv_cv / 255;
i_ppix_norm(im, xc+i, yc+j, &workc);
i_ppix_norm(im, xc-i, yc+j, &workc);
i_ppix_norm(im, xc+i, yc-j, &workc);
i_ppix_norm(im, xc-i, yc-j, &workc);
if (i != j) {
i_ppix_norm(im, xc+j, yc+i, &workc);
i_ppix_norm(im, xc-j, yc+i, &workc);
i_ppix_norm(im, xc+j, yc-i, &workc);
i_ppix_norm(im, xc-j, yc-i, &workc);
}
}
if (cv && i > j) {
workc.channel[3] = orig_alpha * cv / 255;
i_ppix_norm(im, xc+i-1, yc+j, &workc);
i_ppix_norm(im, xc-i+1, yc+j, &workc);
i_ppix_norm(im, xc+i-1, yc-j, &workc);
i_ppix_norm(im, xc-i+1, yc-j, &workc);
if (j != i-1) {
i_ppix_norm(im, xc+j, yc+i-1, &workc);
i_ppix_norm(im, xc-j, yc+i-1, &workc);
i_ppix_norm(im, xc+j, yc-i+1, &workc);
i_ppix_norm(im, xc-j, yc-i+1, &workc);
}
}
t = d;
}
return 1;
}
/*
=item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
=synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
Draw a circle arc outline centered at (x,y) with radius r, from angle
d1 degrees through angle d2 degrees, anti-aliased.