eliminate Imager's internal MAXINT macro
[imager.git] / draw.c
CommitLineData
af22c916 1#define IMAGER_NO_CONTEXT
92bda632 2#include "imager.h"
02d1d628
AMH
3#include "draw.h"
4#include "log.h"
92bda632 5#include "imageri.h"
9b1ec2b8 6#include "imrender.h"
6af18d2b
AMH
7#include <limits.h>
8
40068b33
TC
9int
10i_ppix_norm(i_img *im, i_img_dim x, i_img_dim y, i_color const *col) {
11 i_color src;
12 i_color work;
13 int dest_alpha;
14 int remains;
15
16 if (!col->channel[3])
17 return 0;
18
19 switch (im->channels) {
20 case 1:
21 work = *col;
22 i_adapt_colors(2, 4, &work, 1);
23 i_gpix(im, x, y, &src);
24 remains = 255 - work.channel[1];
25 src.channel[0] = (src.channel[0] * remains
26 + work.channel[0] * work.channel[1]) / 255;
27 return i_ppix(im, x, y, &src);
28
29 case 2:
30 work = *col;
31 i_adapt_colors(2, 4, &work, 1);
32 i_gpix(im, x, y, &src);
8d14daab 33 remains = 255 - work.channel[1];
40068b33
TC
34 dest_alpha = work.channel[1] + remains * src.channel[1] / 255;
35 if (work.channel[1] == 255) {
36 return i_ppix(im, x, y, &work);
37 }
38 else {
39 src.channel[0] = (work.channel[1] * work.channel[0]
40 + remains * src.channel[0] * src.channel[1] / 255) / dest_alpha;
41 src.channel[1] = dest_alpha;
42 return i_ppix(im, x, y, &src);
43 }
44
45 case 3:
46 work = *col;
47 i_gpix(im, x, y, &src);
48 remains = 255 - work.channel[3];
49 src.channel[0] = (src.channel[0] * remains
50 + work.channel[0] * work.channel[3]) / 255;
51 src.channel[1] = (src.channel[1] * remains
52 + work.channel[1] * work.channel[3]) / 255;
53 src.channel[2] = (src.channel[2] * remains
54 + work.channel[2] * work.channel[3]) / 255;
55 return i_ppix(im, x, y, &src);
56
57 case 4:
58 work = *col;
59 i_gpix(im, x, y, &src);
8d14daab 60 remains = 255 - work.channel[3];
40068b33
TC
61 dest_alpha = work.channel[3] + remains * src.channel[3] / 255;
62 if (work.channel[3] == 255) {
63 return i_ppix(im, x, y, &work);
64 }
65 else {
66 src.channel[0] = (work.channel[3] * work.channel[0]
67 + remains * src.channel[0] * src.channel[3] / 255) / dest_alpha;
68 src.channel[1] = (work.channel[3] * work.channel[1]
69 + remains * src.channel[1] * src.channel[3] / 255) / dest_alpha;
70 src.channel[2] = (work.channel[3] * work.channel[2]
71 + remains * src.channel[2] * src.channel[3] / 255) / dest_alpha;
72 src.channel[3] = dest_alpha;
73 return i_ppix(im, x, y, &src);
74 }
75 }
76 return 0;
77}
78
3efb0915
TC
79static void
80cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
8d14daab 81 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax);
3efb0915 82
02d1d628 83void
8d14daab
TC
84i_mmarray_cr(i_mmarray *ar,i_img_dim l) {
85 i_img_dim i;
86 size_t alloc_size;
02d1d628
AMH
87
88 ar->lines=l;
f0960b14
TC
89 alloc_size = sizeof(minmax) * l;
90 /* check for overflow */
91 if (alloc_size / l != sizeof(minmax)) {
92 fprintf(stderr, "overflow calculating memory allocation");
93 exit(3);
94 }
95 ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
498a7bc2
TC
96 for(i=0;i<l;i++) {
97 ar->data[i].max = -1;
98 ar->data[i].min = i_img_dim_MAX;
99 }
02d1d628
AMH
100}
101
102void
103i_mmarray_dst(i_mmarray *ar) {
104 ar->lines=0;
105 if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
106}
107
108void
8d14daab 109i_mmarray_add(i_mmarray *ar,i_img_dim x,i_img_dim y) {
02d1d628
AMH
110 if (y>-1 && y<ar->lines)
111 {
112 if (x<ar->data[y].min) ar->data[y].min=x;
113 if (x>ar->data[y].max) ar->data[y].max=x;
114 }
115}
116
498a7bc2 117i_img_dim
8d14daab 118i_mmarray_gmin(i_mmarray *ar,i_img_dim y) {
02d1d628
AMH
119 if (y>-1 && y<ar->lines) return ar->data[y].min;
120 else return -1;
121}
122
498a7bc2 123i_img_dim
8d14daab 124i_mmarray_getm(i_mmarray *ar,i_img_dim y) {
498a7bc2
TC
125 if (y>-1 && y<ar->lines)
126 return ar->data[y].max;
127 else
128 return i_img_dim_MAX;
02d1d628
AMH
129}
130
8d14daab
TC
131#if 0
132/* unused? */
02d1d628
AMH
133void
134i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
8d14daab 135 i_img_dim i,x;
02d1d628
AMH
136 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);
137}
8d14daab 138#endif
02d1d628 139
02d1d628
AMH
140static
141void
8d14daab 142i_arcdraw(i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_mmarray *ar) {
02d1d628
AMH
143 double alpha;
144 double dsec;
8d14daab 145 i_img_dim temp;
02d1d628 146 alpha=(double)(y2-y1)/(double)(x2-x1);
b254292b 147 if (fabs(alpha) <= 1)
02d1d628
AMH
148 {
149 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
150 dsec=y1;
b254292b 151 while(x1<=x2)
02d1d628 152 {
8d14daab 153 i_mmarray_add(ar,x1,(i_img_dim)(dsec+0.5));
b254292b 154 dsec+=alpha;
02d1d628
AMH
155 x1++;
156 }
157 }
158 else
159 {
160 alpha=1/alpha;
161 if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
162 dsec=x1;
b254292b 163 while(y1<=y2)
02d1d628 164 {
8d14daab 165 i_mmarray_add(ar,(i_img_dim)(dsec+0.5),y1);
b254292b 166 dsec+=alpha;
02d1d628
AMH
167 y1++;
168 }
169 }
170}
171
172void
173i_mmarray_info(i_mmarray *ar) {
8d14daab 174 i_img_dim i;
02d1d628 175 for(i=0;i<ar->lines;i++)
8d14daab
TC
176 if (ar->data[i].max!=-1)
177 printf("line %"i_DF ": min=%" i_DF ", max=%" i_DF ".\n",
178 i_DFc(i), i_DFc(ar->data[i].min), i_DFc(ar->data[i].max));
02d1d628
AMH
179}
180
a8652edf 181static void
8d14daab 182i_arc_minmax(i_int_hlines *hlines,i_img_dim x,i_img_dim y, double rad,float d1,float d2) {
02d1d628 183 i_mmarray dot;
98747309 184 double f;
8d14daab 185 i_img_dim x1,y1;
02d1d628 186
a8652edf 187 i_mmarray_cr(&dot, hlines->limit_y);
02d1d628 188
8d14daab
TC
189 x1=(i_img_dim)(x+0.5+rad*cos(d1*PI/180.0));
190 y1=(i_img_dim)(y+0.5+rad*sin(d1*PI/180.0));
02d1d628
AMH
191
192 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
193 i_arcdraw(x, y, x1, y1, &dot);
194
8d14daab
TC
195 x1=(i_img_dim)(x+0.5+rad*cos(d2*PI/180.0));
196 y1=(i_img_dim)(y+0.5+rad*sin(d2*PI/180.0));
02d1d628 197
8d14daab
TC
198 for(f=d1;f<=d2;f+=0.01)
199 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)));
6af18d2b 200
02d1d628
AMH
201 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
202 i_arcdraw(x, y, x1, y1, &dot);
203
a8652edf
TC
204 /* render the minmax values onto the hlines */
205 for (y = 0; y < dot.lines; y++) {
206 if (dot.data[y].max!=-1) {
8d14daab 207 i_img_dim minx, width;
a8652edf
TC
208 minx = dot.data[y].min;
209 width = dot.data[y].max - dot.data[y].min + 1;
210 i_int_hlines_add(hlines, y, minx, width);
211 }
212 }
213
02d1d628 214 /* dot.info(); */
7f882a01 215 i_mmarray_dst(&dot);
02d1d628
AMH
216}
217
a8652edf 218static void
8d14daab 219i_arc_hlines(i_int_hlines *hlines,i_img_dim x,i_img_dim y,double rad,float d1,float d2) {
a8652edf
TC
220 if (d1 <= d2) {
221 i_arc_minmax(hlines, x, y, rad, d1, d2);
222 }
223 else {
224 i_arc_minmax(hlines, x, y, rad, d1, 360);
225 i_arc_minmax(hlines, x, y, rad, 0, d2);
226 }
227}
228
92bda632
TC
229/*
230=item i_arc(im, x, y, rad, d1, d2, color)
231
232=category Drawing
233=synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
234
235Fills an arc centered at (x,y) with radius I<rad> covering the range
236of angles in degrees from d1 to d2, with the color.
237
238=cut
239*/
240
a8652edf 241void
8d14daab 242i_arc(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,const i_color *val) {
a8652edf 243 i_int_hlines hlines;
857e686a
TC
244 dIMCTXim(im);
245
246 im_log((aIMCTX,1,"i_arc(im %p,(x,y)=(" i_DFp "), rad %f, d1 %f, d2 %f, col %p)",
247 im, i_DFcp(x, y), rad, d1, d2, val));
a8652edf
TC
248
249 i_int_init_hlines_img(&hlines, im);
250
251 i_arc_hlines(&hlines, x, y, rad, d1, d2);
252
253 i_int_hlines_fill_color(im, &hlines, val);
254
255 i_int_hlines_destroy(&hlines);
256}
257
92bda632
TC
258/*
259=item i_arc_cfill(im, x, y, rad, d1, d2, fill)
260
261=category Drawing
262=synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
263
264Fills an arc centered at (x,y) with radius I<rad> covering the range
265of angles in degrees from d1 to d2, with the fill object.
266
267=cut
268*/
269
a8652edf
TC
270#define MIN_CIRCLE_STEPS 8
271#define MAX_CIRCLE_STEPS 360
272
f1ac5027 273void
8d14daab 274i_arc_cfill(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,i_fill_t *fill) {
a8652edf 275 i_int_hlines hlines;
857e686a
TC
276 dIMCTXim(im);
277
278 im_log((aIMCTX,1,"i_arc_cfill(im %p,(x,y)=(" i_DFp "), rad %f, d1 %f, d2 %f, fill %p)",
279 im, i_DFcp(x, y), rad, d1, d2, fill));
f1ac5027 280
a8652edf 281 i_int_init_hlines_img(&hlines, im);
f1ac5027 282
a8652edf 283 i_arc_hlines(&hlines, x, y, rad, d1, d2);
f1ac5027 284
a8652edf 285 i_int_hlines_fill_fill(im, &hlines, fill);
f1ac5027 286
a8652edf
TC
287 i_int_hlines_destroy(&hlines);
288}
f1ac5027 289
a8652edf
TC
290static void
291arc_poly(int *count, double **xvals, double **yvals,
292 double x, double y, double rad, double d1, double d2) {
293 double d1_rad, d2_rad;
294 double circum;
8d14daab 295 i_img_dim steps, point_count;
a8652edf
TC
296 double angle_inc;
297
298 /* normalize the angles */
299 d1 = fmod(d1, 360);
300 if (d1 == 0) {
301 if (d2 >= 360) { /* default is 361 */
302 d2 = 360;
303 }
304 else {
305 d2 = fmod(d2, 360);
306 if (d2 < d1)
307 d2 += 360;
308 }
309 }
310 else {
311 d2 = fmod(d2, 360);
312 if (d2 < d1)
313 d2 += 360;
314 }
315 d1_rad = d1 * PI / 180;
316 d2_rad = d2 * PI / 180;
317
318 /* how many segments for the curved part?
319 we do a maximum of one per degree, with a minimum of 8/circle
320 we try to aim at having about one segment per 2 pixels
321 Work it out per circle to get a step size.
322
323 I was originally making steps = circum/2 but that looked horrible.
324
325 I think there might be an issue in the polygon filler.
326 */
327 circum = 2 * PI * rad;
328 steps = circum;
329 if (steps > MAX_CIRCLE_STEPS)
330 steps = MAX_CIRCLE_STEPS;
331 else if (steps < MIN_CIRCLE_STEPS)
332 steps = MIN_CIRCLE_STEPS;
333
334 angle_inc = 2 * PI / steps;
335
336 point_count = steps + 5; /* rough */
e310e5f9
TC
337 /* point_count is always relatively small, so allocation won't overflow */
338 *xvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
339 *yvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
a8652edf
TC
340
341 /* from centre to edge at d1 */
342 (*xvals)[0] = x;
343 (*yvals)[0] = y;
344 (*xvals)[1] = x + rad * cos(d1_rad);
345 (*yvals)[1] = y + rad * sin(d1_rad);
346 *count = 2;
347
348 /* step around the curve */
349 while (d1_rad < d2_rad) {
350 (*xvals)[*count] = x + rad * cos(d1_rad);
351 (*yvals)[*count] = y + rad * sin(d1_rad);
352 ++*count;
353 d1_rad += angle_inc;
354 }
f1ac5027 355
a8652edf
TC
356 /* finish off the curve */
357 (*xvals)[*count] = x + rad * cos(d2_rad);
358 (*yvals)[*count] = y + rad * sin(d2_rad);
359 ++*count;
360}
f1ac5027 361
92bda632
TC
362/*
363=item i_arc_aa(im, x, y, rad, d1, d2, color)
364
365=category Drawing
366=synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
367
5715f7c3 368Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
92bda632
TC
369the range of angles in degrees from d1 to d2, with the color.
370
371=cut
372*/
373
a8652edf
TC
374void
375i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
97ac0a96 376 const i_color *val) {
a8652edf
TC
377 double *xvals, *yvals;
378 int count;
857e686a
TC
379 dIMCTXim(im);
380
381 im_log((aIMCTX,1,"i_arc_aa(im %p,(x,y)=(%f,%f), rad %f, d1 %f, d2 %f, col %p)",
382 im, x, y, rad, d1, d2, val));
a8652edf 383
6b8fe08b 384 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
a8652edf
TC
385
386 i_poly_aa(im, count, xvals, yvals, val);
387
388 myfree(xvals);
389 myfree(yvals);
f1ac5027
TC
390}
391
92bda632
TC
392/*
393=item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
394
395=category Drawing
396=synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
397
5715f7c3 398Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
92bda632
TC
399the range of angles in degrees from d1 to d2, with the fill object.
400
401=cut
402*/
403
a8652edf
TC
404void
405i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
406 i_fill_t *fill) {
407 double *xvals, *yvals;
408 int count;
857e686a
TC
409 dIMCTXim(im);
410
411 im_log((aIMCTX,1,"i_arc_aa_cfill(im %p,(x,y)=(%f,%f), rad %f, d1 %f, d2 %f, fill %p)",
412 im, x, y, rad, d1, d2, fill));
a8652edf
TC
413
414 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
415
416 i_poly_aa_cfill(im, count, xvals, yvals, fill);
6af18d2b 417
a8652edf
TC
418 myfree(xvals);
419 myfree(yvals);
420}
6af18d2b
AMH
421
422/* Temporary AA HACK */
423
424
8d14daab
TC
425typedef i_img_dim frac;
426static frac float_to_frac(double x) { return (frac)(0.5+x*16.0); }
6af18d2b
AMH
427
428static
429void
8d14daab 430polar_to_plane(double cx, double cy, float angle, double radius, frac *x, frac *y) {
6af18d2b
AMH
431 *x = float_to_frac(cx+radius*cos(angle));
432 *y = float_to_frac(cy+radius*sin(angle));
433}
434
6af18d2b
AMH
435static
436void
af22c916 437make_minmax_list(pIMCTX, i_mmarray *dot, double x, double y, double radius) {
6af18d2b
AMH
438 float angle = 0.0;
439 float astep = radius>0.1 ? .5/radius : 10;
440 frac cx, cy, lx, ly, sx, sy;
441
af22c916 442 im_log((aIMCTX, 1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
6af18d2b
AMH
443
444 polar_to_plane(x, y, angle, radius, &sx, &sy);
445
446 for(angle = 0.0; angle<361; angle +=astep) {
6af18d2b
AMH
447 lx = sx; ly = sy;
448 polar_to_plane(x, y, angle, radius, &cx, &cy);
449 sx = cx; sy = cy;
450
451 if (fabs(cx-lx) > fabs(cy-ly)) {
452 int ccx, ccy;
453 if (lx>cx) {
454 ccx = lx; lx = cx; cx = ccx;
455 ccy = ly; ly = cy; cy = ccy;
456 }
457
458 for(ccx=lx; ccx<=cx; ccx++) {
459 ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
460 i_mmarray_add(dot, ccx, ccy);
461 }
462 } else {
463 int ccx, ccy;
464
465 if (ly>cy) {
466 ccy = ly; ly = cy; cy = ccy;
467 ccx = lx; lx = cx; cx = ccx;
468 }
469
470 for(ccy=ly; ccy<=cy; ccy++) {
471 if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
472 i_mmarray_add(dot, ccx, ccy);
473 }
474 }
475 }
476}
477
478/* Get the number of subpixels covered */
479
480static
481int
8d14daab 482i_pixel_coverage(i_mmarray *dot, i_img_dim x, i_img_dim y) {
6af18d2b
AMH
483 frac minx = x*16;
484 frac maxx = minx+15;
485 frac cy;
486 int cnt = 0;
487
488 for(cy=y*16; cy<(y+1)*16; cy++) {
489 frac tmin = dot->data[cy].min;
490 frac tmax = dot->data[cy].max;
491
492 if (tmax == -1 || tmin > maxx || tmax < minx) continue;
493
494 if (tmin < minx) tmin = minx;
495 if (tmax > maxx) tmax = maxx;
496
497 cnt+=1+tmax-tmin;
498 }
499 return cnt;
500}
501
92bda632
TC
502/*
503=item i_circle_aa(im, x, y, rad, color)
504
505=category Drawing
506=synopsis i_circle_aa(im, 50, 50, 45, &color);
507
5715f7c3 508Anti-alias fills a circle centered at (x,y) for radius I<rad> with
92bda632
TC
509color.
510
511=cut
512*/
6af18d2b 513void
8d14daab 514i_circle_aa(i_img *im, double x, double y, double rad, const i_color *val) {
6af18d2b
AMH
515 i_mmarray dot;
516 i_color temp;
8d14daab 517 i_img_dim ly;
af22c916 518 dIMCTXim(im);
6af18d2b 519
af22c916 520 im_log((aIMCTX, 1, "i_circle_aa(im %p, centre(" i_DFp "), rad %.2f, val %p)\n",
8d14daab 521 im, i_DFcp(x, y), rad, val));
6af18d2b
AMH
522
523 i_mmarray_cr(&dot,16*im->ysize);
af22c916 524 make_minmax_list(aIMCTX, &dot, x, y, rad);
6af18d2b
AMH
525
526 for(ly = 0; ly<im->ysize; ly++) {
a659442a 527 int ix, cy, minx = INT_MAX, maxx = INT_MIN;
6af18d2b
AMH
528
529 /* Find the left/rightmost set subpixels */
530 for(cy = 0; cy<16; cy++) {
531 frac tmin = dot.data[ly*16+cy].min;
532 frac tmax = dot.data[ly*16+cy].max;
533 if (tmax == -1) continue;
534
535 if (minx > tmin) minx = tmin;
536 if (maxx < tmax) maxx = tmax;
537 }
538
539 if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
540
541 minx /= 16;
542 maxx /= 16;
543 for(ix=minx; ix<=maxx; ix++) {
544 int cnt = i_pixel_coverage(&dot, ix, ly);
545 if (cnt>255) cnt = 255;
546 if (cnt) { /* should never be true */
547 int ch;
548 float ratio = (float)cnt/255.0;
549 i_gpix(im, ix, ly, &temp);
550 for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
551 i_ppix(im, ix, ly, &temp);
552 }
553 }
554 }
4b19f77a 555 i_mmarray_dst(&dot);
6af18d2b
AMH
556}
557
40068b33
TC
558/*
559=item i_circle_out(im, x, y, r, col)
560
561=category Drawing
562=synopsis i_circle_out(im, 50, 50, 45, &color);
563
564Draw a circle outline centered at (x,y) with radius r,
565non-anti-aliased.
566
567Parameters:
568
569=over
570
571=item *
572
573(x, y) - the center of the circle
574
575=item *
576
577r - the radius of the circle in pixels, must be non-negative
578
579=back
580
581Returns non-zero on success.
582
583Implementation:
584
585=cut
586*/
587
588int
589i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
590 const i_color *col) {
591 i_img_dim x, y;
592 i_img_dim dx, dy;
593 int error;
af22c916 594 dIMCTXim(im);
40068b33 595
857e686a
TC
596 im_log((aIMCTX, 1, "i_circle_out(im %p, centre(" i_DFp "), rad %" i_DF ", col %p)\n",
597 im, i_DFcp(xc, yc), i_DFc(r), col));
598
af22c916 599 im_clear_error(aIMCTX);
40068b33
TC
600
601 if (r < 0) {
af22c916 602 im_push_error(aIMCTX, 0, "circle: radius must be non-negative");
40068b33
TC
603 return 0;
604 }
605
606 i_ppix(im, xc+r, yc, col);
607 i_ppix(im, xc-r, yc, col);
608 i_ppix(im, xc, yc+r, col);
609 i_ppix(im, xc, yc-r, col);
610
611 x = 0;
612 y = r;
613 dx = 1;
614 dy = -2 * r;
615 error = 1 - r;
616 while (x < y) {
617 if (error >= 0) {
618 --y;
619 dy += 2;
620 error += dy;
621 }
622 ++x;
623 dx += 2;
624 error += dx;
625
626 i_ppix(im, xc + x, yc + y, col);
627 i_ppix(im, xc + x, yc - y, col);
628 i_ppix(im, xc - x, yc + y, col);
629 i_ppix(im, xc - x, yc - y, col);
630 if (x != y) {
631 i_ppix(im, xc + y, yc + x, col);
632 i_ppix(im, xc + y, yc - x, col);
633 i_ppix(im, xc - y, yc + x, col);
634 i_ppix(im, xc - y, yc - x, col);
635 }
636 }
637
638 return 1;
639}
640
641/*
642=item arc_seg(angle)
643
644Convert an angle in degrees into an angle measure we can generate
645simply from the numbers we have when drawing the circle.
646
12db268a 647=cut
40068b33
TC
648*/
649
650static i_img_dim
651arc_seg(double angle, int scale) {
652 i_img_dim seg = (angle + 45) / 90;
653 double remains = angle - seg * 90; /* should be in the range [-45,45] */
40068b33
TC
654
655 while (seg > 4)
656 seg -= 4;
657 if (seg == 4 && remains > 0)
658 seg = 0;
659
660 return scale * (seg * 2 + sin(remains * PI/180));
661}
662
663/*
664=item i_arc_out(im, x, y, r, d1, d2, col)
665
666=category Drawing
667=synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
668
669Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
670over the angle range d1 through d2 degrees.
671
672Parameters:
673
674=over
675
676=item *
677
678(x, y) - the center of the circle
679
680=item *
681
682r - the radius of the circle in pixels, must be non-negative
683
684=item *
685
686d1, d2 - the range of angles to draw the arc over, in degrees.
687
688=back
689
690Returns non-zero on success.
691
692Implementation:
693
694=cut
695*/
696
697int
698i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
8d14daab 699 double d1, double d2, const i_color *col) {
40068b33
TC
700 i_img_dim x, y;
701 i_img_dim dx, dy;
702 int error;
703 i_img_dim segs[2][2];
704 int seg_count;
705 i_img_dim sin_th;
706 i_img_dim seg_d1, seg_d2;
707 int seg_num;
40068b33
TC
708 i_img_dim scale = r + 1;
709 i_img_dim seg1 = scale * 2;
710 i_img_dim seg2 = scale * 4;
711 i_img_dim seg3 = scale * 6;
712 i_img_dim seg4 = scale * 8;
857e686a
TC
713 dIMCTXim(im);
714
715 im_log((aIMCTX,1,"i_arc_out(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
716 im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
40068b33 717
af22c916 718 im_clear_error(aIMCTX);
40068b33
TC
719
720 if (r <= 0) {
af22c916 721 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
40068b33
TC
722 return 0;
723 }
724 if (d1 + 360 <= d2)
725 return i_circle_out(im, xc, yc, r, col);
726
727 if (d1 < 0)
728 d1 += 360 * floor((-d1 + 359) / 360);
729 if (d2 < 0)
730 d2 += 360 * floor((-d2 + 359) / 360);
731 d1 = fmod(d1, 360);
732 d2 = fmod(d2, 360);
733 seg_d1 = arc_seg(d1, scale);
734 seg_d2 = arc_seg(d2, scale);
735 if (seg_d2 < seg_d1) {
736 /* split into two segments */
737 segs[0][0] = 0;
738 segs[0][1] = seg_d2;
739 segs[1][0] = seg_d1;
740 segs[1][1] = seg4;
741 seg_count = 2;
742 }
743 else {
744 segs[0][0] = seg_d1;
745 segs[0][1] = seg_d2;
746 seg_count = 1;
747 }
748
749 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
750 i_img_dim seg_start = segs[seg_num][0];
751 i_img_dim seg_end = segs[seg_num][1];
752 if (seg_start == 0)
753 i_ppix(im, xc+r, yc, col);
754 if (seg_start <= seg1 && seg_end >= seg1)
755 i_ppix(im, xc, yc+r, col);
756 if (seg_start <= seg2 && seg_end >= seg2)
757 i_ppix(im, xc-r, yc, col);
758 if (seg_start <= seg3 && seg_end >= seg3)
759 i_ppix(im, xc, yc-r, col);
760
761 y = 0;
762 x = r;
763 dy = 1;
764 dx = -2 * r;
765 error = 1 - r;
766 while (y < x) {
767 if (error >= 0) {
768 --x;
769 dx += 2;
770 error += dx;
771 }
772 ++y;
773 dy += 2;
774 error += dy;
775
776 sin_th = y;
777 if (seg_start <= sin_th && seg_end >= sin_th)
778 i_ppix(im, xc + x, yc + y, col);
779 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
780 i_ppix(im, xc + y, yc + x, col);
781
782 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
783 i_ppix(im, xc - y, yc + x, col);
784 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
785 i_ppix(im, xc - x, yc + y, col);
786
787 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
788 i_ppix(im, xc - x, yc - y, col);
789 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
790 i_ppix(im, xc - y, yc - x, col);
791
792 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
793 i_ppix(im, xc + y, yc - x, col);
794 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
795 i_ppix(im, xc + x, yc - y, col);
796 }
797 }
798
799 return 1;
800}
801
802static double
803cover(i_img_dim r, i_img_dim j) {
8d14daab 804 double rjsqrt = sqrt(r*r - j*j);
40068b33
TC
805
806 return ceil(rjsqrt) - rjsqrt;
807}
808
809/*
810=item i_circle_out_aa(im, xc, yc, r, col)
811
812=synopsis i_circle_out_aa(im, 50, 50, 45, &color);
813
814Draw a circle outline centered at (x,y) with radius r, anti-aliased.
815
816Parameters:
817
818=over
819
820=item *
821
822(xc, yc) - the center of the circle
823
824=item *
825
826r - the radius of the circle in pixels, must be non-negative
827
828=item *
829
830col - an i_color for the color to draw in.
831
832=back
833
834Returns non-zero on success.
835
836=cut
837
838Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
839Gems.
840
841I use floating point for I<D> since for large circles the precision of
842a [0,255] value isn't sufficient when approaching the end of the
843octant.
844
845*/
846
847int
848i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
849 i_img_dim i, j;
850 double t;
851 i_color workc = *col;
852 int orig_alpha = col->channel[3];
af22c916 853 dIMCTXim(im);
dc95a369 854
857e686a
TC
855 im_log((aIMCTX,1,"i_circle_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", col %p)",
856 im, i_DFcp(xc, yc), i_DFc(r), col));
857
af22c916 858 im_clear_error(aIMCTX);
40068b33 859 if (r <= 0) {
af22c916 860 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
40068b33
TC
861 return 0;
862 }
863 i = r;
864 j = 0;
865 t = 0;
866 i_ppix_norm(im, xc+i, yc+j, col);
867 i_ppix_norm(im, xc-i, yc+j, col);
868 i_ppix_norm(im, xc+j, yc+i, col);
869 i_ppix_norm(im, xc+j, yc-i, col);
870
871 while (i > j+1) {
872 double d;
873 int cv, inv_cv;
40068b33
TC
874 j++;
875 d = cover(r, j);
876 cv = (int)(d * 255 + 0.5);
877 inv_cv = 255-cv;
878 if (d < t) {
879 --i;
880 }
881 if (inv_cv) {
882 workc.channel[3] = orig_alpha * inv_cv / 255;
883 i_ppix_norm(im, xc+i, yc+j, &workc);
884 i_ppix_norm(im, xc-i, yc+j, &workc);
885 i_ppix_norm(im, xc+i, yc-j, &workc);
886 i_ppix_norm(im, xc-i, yc-j, &workc);
887
888 if (i != j) {
889 i_ppix_norm(im, xc+j, yc+i, &workc);
890 i_ppix_norm(im, xc-j, yc+i, &workc);
891 i_ppix_norm(im, xc+j, yc-i, &workc);
892 i_ppix_norm(im, xc-j, yc-i, &workc);
893 }
894 }
895 if (cv && i > j) {
896 workc.channel[3] = orig_alpha * cv / 255;
897 i_ppix_norm(im, xc+i-1, yc+j, &workc);
898 i_ppix_norm(im, xc-i+1, yc+j, &workc);
899 i_ppix_norm(im, xc+i-1, yc-j, &workc);
900 i_ppix_norm(im, xc-i+1, yc-j, &workc);
901
902 if (j != i-1) {
903 i_ppix_norm(im, xc+j, yc+i-1, &workc);
904 i_ppix_norm(im, xc-j, yc+i-1, &workc);
905 i_ppix_norm(im, xc+j, yc-i+1, &workc);
906 i_ppix_norm(im, xc-j, yc-i+1, &workc);
907 }
908 }
909 t = d;
910 }
911
912 return 1;
913}
914
915/*
916=item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
917
918=synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
919
920Draw a circle arc outline centered at (x,y) with radius r, from angle
921d1 degrees through angle d2 degrees, anti-aliased.
922
923Parameters:
924
925=over
926
927=item *
928
929(xc, yc) - the center of the circle
930
931=item *
932
933r - the radius of the circle in pixels, must be non-negative
934
935=item *
936
937d1, d2 - the range of angle in degrees to draw the arc through. If
938d2-d1 >= 360 a full circle is drawn.
939
940=back
941
942Returns non-zero on success.
943
944=cut
945
946Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
947Gems.
948
949*/
950
951int
8d14daab 952i_arc_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, double d1, double d2, const i_color *col) {
40068b33
TC
953 i_img_dim i, j;
954 double t;
955 i_color workc = *col;
956 i_img_dim segs[2][2];
957 int seg_count;
958 i_img_dim sin_th;
959 i_img_dim seg_d1, seg_d2;
960 int seg_num;
961 int orig_alpha = col->channel[3];
962 i_img_dim scale = r + 1;
963 i_img_dim seg1 = scale * 2;
964 i_img_dim seg2 = scale * 4;
965 i_img_dim seg3 = scale * 6;
966 i_img_dim seg4 = scale * 8;
af22c916 967 dIMCTXim(im);
40068b33 968
857e686a
TC
969 im_log((aIMCTX,1,"i_arc_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
970 im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
971
af22c916 972 im_clear_error(aIMCTX);
40068b33 973 if (r <= 0) {
af22c916 974 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
40068b33
TC
975 return 0;
976 }
977 if (d1 + 360 <= d2)
978 return i_circle_out_aa(im, xc, yc, r, col);
979
980 if (d1 < 0)
981 d1 += 360 * floor((-d1 + 359) / 360);
982 if (d2 < 0)
983 d2 += 360 * floor((-d2 + 359) / 360);
984 d1 = fmod(d1, 360);
985 d2 = fmod(d2, 360);
986 seg_d1 = arc_seg(d1, scale);
987 seg_d2 = arc_seg(d2, scale);
988 if (seg_d2 < seg_d1) {
989 /* split into two segments */
990 segs[0][0] = 0;
991 segs[0][1] = seg_d2;
992 segs[1][0] = seg_d1;
993 segs[1][1] = seg4;
994 seg_count = 2;
995 }
996 else {
997 segs[0][0] = seg_d1;
998 segs[0][1] = seg_d2;
999 seg_count = 1;
1000 }
1001
1002 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
1003 i_img_dim seg_start = segs[seg_num][0];
1004 i_img_dim seg_end = segs[seg_num][1];
1005
1006 i = r;
1007 j = 0;
1008 t = 0;
1009
1010 if (seg_start == 0)
1011 i_ppix_norm(im, xc+i, yc+j, col);
1012 if (seg_start <= seg1 && seg_end >= seg1)
1013 i_ppix_norm(im, xc+j, yc+i, col);
1014 if (seg_start <= seg2 && seg_end >= seg2)
1015 i_ppix_norm(im, xc-i, yc+j, col);
1016 if (seg_start <= seg3 && seg_end >= seg3)
1017 i_ppix_norm(im, xc+j, yc-i, col);
1018
1019 while (i > j+1) {
1020 int cv, inv_cv;
40068b33
TC
1021 double d;
1022 j++;
1023 d = cover(r, j);
1024 cv = (int)(d * 255 + 0.5);
1025 inv_cv = 255-cv;
1026 if (d < t) {
1027 --i;
1028 }
1029 sin_th = j;
1030 if (inv_cv) {
1031 workc.channel[3] = orig_alpha * inv_cv / 255;
1032
1033 if (seg_start <= sin_th && seg_end >= sin_th)
1034 i_ppix_norm(im, xc+i, yc+j, &workc);
1035 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1036 i_ppix_norm(im, xc-i, yc+j, &workc);
1037 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1038 i_ppix_norm(im, xc+i, yc-j, &workc);
1039 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1040 i_ppix_norm(im, xc-i, yc-j, &workc);
1041
1042 if (i != j) {
1043 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1044 i_ppix_norm(im, xc+j, yc+i, &workc);
1045 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1046 i_ppix_norm(im, xc-j, yc+i, &workc);
1047 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1048 i_ppix_norm(im, xc+j, yc-i, &workc);
1049 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1050 i_ppix_norm(im, xc-j, yc-i, &workc);
1051 }
1052 }
1053 if (cv && i > j) {
1054 workc.channel[3] = orig_alpha * cv / 255;
1055 if (seg_start <= sin_th && seg_end >= sin_th)
1056 i_ppix_norm(im, xc+i-1, yc+j, &workc);
1057 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1058 i_ppix_norm(im, xc-i+1, yc+j, &workc);
1059 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1060 i_ppix_norm(im, xc+i-1, yc-j, &workc);
1061 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1062 i_ppix_norm(im, xc-i+1, yc-j, &workc);
1063
1064 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1065 i_ppix_norm(im, xc+j, yc+i-1, &workc);
1066 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1067 i_ppix_norm(im, xc-j, yc+i-1, &workc);
1068 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1069 i_ppix_norm(im, xc+j, yc-i+1, &workc);
1070 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1071 i_ppix_norm(im, xc-j, yc-i+1, &workc);
1072 }
1073 t = d;
1074 }
1075 }
1076
1077 return 1;
1078}
1079
92bda632
TC
1080/*
1081=item i_box(im, x1, y1, x2, y2, color)
6af18d2b 1082
92bda632
TC
1083=category Drawing
1084=synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
6af18d2b 1085
92bda632 1086Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
6af18d2b 1087
92bda632
TC
1088=cut
1089*/
6af18d2b 1090
02d1d628 1091void
8d14daab
TC
1092i_box(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2,const i_color *val) {
1093 i_img_dim x,y;
857e686a
TC
1094 dIMCTXim(im);
1095
1096 im_log((aIMCTX, 1,"i_box(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
8d14daab 1097 im, i_DFcp(x1,y1), i_DFcp(x2,y2), val));
02d1d628
AMH
1098 for(x=x1;x<x2+1;x++) {
1099 i_ppix(im,x,y1,val);
1100 i_ppix(im,x,y2,val);
1101 }
1102 for(y=y1;y<y2+1;y++) {
1103 i_ppix(im,x1,y,val);
1104 i_ppix(im,x2,y,val);
1105 }
1106}
1107
92bda632
TC
1108/*
1109=item i_box_filled(im, x1, y1, x2, y2, color)
1110
1111=category Drawing
1112=synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
1113
1114Fills the box from (x1,y1) to (x2,y2) inclusive with color.
1115
1116=cut
1117*/
1118
02d1d628 1119void
8d14daab 1120i_box_filled(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2, const i_color *val) {
3b000586
TC
1121 i_img_dim x, y, width;
1122 i_palidx index;
857e686a 1123 dIMCTXim(im);
3b000586 1124
857e686a 1125 im_log((aIMCTX,1,"i_box_filled(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
8d14daab 1126 im, i_DFcp(x1, y1), i_DFcp(x2,y2) ,val));
3b000586
TC
1127
1128 if (x1 > x2 || y1 > y2
1129 || x2 < 0 || y2 < 0
1130 || x1 >= im->xsize || y1 > im->ysize)
1131 return;
1132
1133 if (x1 < 0)
1134 x1 = 0;
1135 if (x2 >= im->xsize)
1136 x2 = im->xsize - 1;
1137 if (y1 < 0)
1138 y1 = 0;
1139 if (y2 >= im->ysize)
1140 y2 = im->ysize - 1;
1141
1142 width = x2 - x1 + 1;
1143
1144 if (im->type == i_palette_type
1145 && i_findcolor(im, val, &index)) {
1146 i_palidx *line = mymalloc(sizeof(i_palidx) * width);
1147
1148 for (x = 0; x < width; ++x)
1149 line[x] = index;
1150
1151 for (y = y1; y <= y2; ++y)
1152 i_ppal(im, x1, x2+1, y, line);
1153
1154 myfree(line);
1155 }
1156 else {
1157 i_color *line = mymalloc(sizeof(i_color) * width);
1158
1159 for (x = 0; x < width; ++x)
1160 line[x] = *val;
1161
1162 for (y = y1; y <= y2; ++y)
1163 i_plin(im, x1, x2+1, y, line);
1164
1165 myfree(line);
1166 }
02d1d628
AMH
1167}
1168
7477ff14
TC
1169/*
1170=item i_box_filledf(im, x1, y1, x2, y2, color)
1171
1172=category Drawing
1173=synopsis i_box_filledf(im, 0, 0, im->xsize-1, im->ysize-1, &fcolor);
1174
1175Fills the box from (x1,y1) to (x2,y2) inclusive with a floating point
1176color.
1177
1178=cut
1179*/
1180
1181int
8d14daab 1182i_box_filledf(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2, const i_fcolor *val) {
7477ff14 1183 i_img_dim x, y, width;
857e686a 1184 dIMCTXim(im);
7477ff14 1185
857e686a 1186 im_log((aIMCTX, 1,"i_box_filledf(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
8d14daab 1187 im, i_DFcp(x1, y1), i_DFcp(x2, y2), val));
7477ff14
TC
1188
1189 if (x1 > x2 || y1 > y2
1190 || x2 < 0 || y2 < 0
1191 || x1 >= im->xsize || y1 > im->ysize)
1192 return 0;
1193
1194 if (x1 < 0)
1195 x1 = 0;
1196 if (x2 >= im->xsize)
1197 x2 = im->xsize - 1;
1198 if (y1 < 0)
1199 y1 = 0;
1200 if (y2 >= im->ysize)
1201 y2 = im->ysize - 1;
1202
1203 width = x2 - x1 + 1;
1204
1205 if (im->bits <= 8) {
1206 i_color c;
1207 c.rgba.r = SampleFTo8(val->rgba.r);
1208 c.rgba.g = SampleFTo8(val->rgba.g);
1209 c.rgba.b = SampleFTo8(val->rgba.b);
1210 c.rgba.a = SampleFTo8(val->rgba.a);
1211
1212 i_box_filled(im, x1, y1, x2, y2, &c);
1213 }
1214 else {
1215 i_fcolor *line = mymalloc(sizeof(i_fcolor) * width);
1216
1217 for (x = 0; x < width; ++x)
1218 line[x] = *val;
1219
1220 for (y = y1; y <= y2; ++y)
1221 i_plinf(im, x1, x2+1, y, line);
1222
1223 myfree(line);
1224 }
1225
1226 return 1;
1227}
1228
92bda632
TC
1229/*
1230=item i_box_cfill(im, x1, y1, x2, y2, fill)
1231
1232=category Drawing
1233=synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
1234
1235Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
1236
1237=cut
1238*/
1239
f1ac5027 1240void
8d14daab 1241i_box_cfill(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2,i_fill_t *fill) {
9b1ec2b8 1242 i_render r;
857e686a 1243 dIMCTXim(im);
8d14daab 1244
857e686a 1245 im_log((aIMCTX,1,"i_box_cfill(im* %p, p1(" i_DFp "), p2(" i_DFp "), fill %p)\n",
8d14daab 1246 im, i_DFcp(x1, y1), i_DFcp(x2,y2), fill));
f1ac5027
TC
1247
1248 ++x2;
f0960b14
TC
1249 if (x1 < 0)
1250 x1 = 0;
1251 if (y1 < 0)
1252 y1 = 0;
1253 if (x2 > im->xsize)
1254 x2 = im->xsize;
1255 if (y2 >= im->ysize)
1256 y2 = im->ysize-1;
1257 if (x1 >= x2 || y1 > y2)
1258 return;
9b1ec2b8
TC
1259
1260 i_render_init(&r, im, x2-x1);
1261 while (y1 <= y2) {
1262 i_render_fill(&r, x1, y1, x2-x1, NULL, fill);
1263 ++y1;
f1ac5027 1264 }
9b1ec2b8 1265 i_render_done(&r);
f1ac5027 1266}
02d1d628 1267
aa833c97 1268/*
5715f7c3 1269=item i_line(C<im>, C<x1>, C<y1>, C<x2>, C<y2>, C<color>, C<endp>)
aa833c97 1270
92bda632
TC
1271=category Drawing
1272
5715f7c3 1273=for stopwords Bresenham's
aa833c97 1274
5715f7c3
TC
1275Draw a line to image using Bresenham's line drawing algorithm
1276
1277 im - image to draw to
1278 x1 - starting x coordinate
1279 y1 - starting x coordinate
1280 x2 - starting x coordinate
1281 y2 - starting x coordinate
1282 color - color to write to image
1283 endp - endpoint flag (boolean)
aa833c97
AMH
1284
1285=cut
1286*/
1287
02d1d628 1288void
8d14daab
TC
1289i_line(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, const i_color *val, int endp) {
1290 i_img_dim x, y;
1291 i_img_dim dx, dy;
1292 i_img_dim p;
02d1d628 1293
aa833c97
AMH
1294 dx = x2 - x1;
1295 dy = y2 - y1;
02d1d628 1296
aa833c97
AMH
1297
1298 /* choose variable to iterate on */
8d14daab
TC
1299 if (i_abs(dx) > i_abs(dy)) {
1300 i_img_dim dx2, dy2, cpy;
aa833c97
AMH
1301
1302 /* sort by x */
1303 if (x1 > x2) {
8d14daab 1304 i_img_dim t;
aa833c97
AMH
1305 t = x1; x1 = x2; x2 = t;
1306 t = y1; y1 = y2; y2 = t;
02d1d628 1307 }
aa833c97 1308
8d14daab 1309 dx = i_abs(dx);
aa833c97
AMH
1310 dx2 = dx*2;
1311 dy = y2 - y1;
1312
1313 if (dy<0) {
1314 dy = -dy;
1315 cpy = -1;
1316 } else {
1317 cpy = 1;
1318 }
1319 dy2 = dy*2;
1320 p = dy2 - dx;
1321
1322
1323 y = y1;
1324 for(x=x1; x<x2-1; x++) {
1325 if (p<0) {
1326 p += dy2;
1327 } else {
1328 y += cpy;
1329 p += dy2-dx2;
1330 }
1331 i_ppix(im, x+1, y, val);
02d1d628 1332 }
aa833c97 1333 } else {
8d14daab 1334 i_img_dim dy2, dx2, cpx;
aa833c97
AMH
1335
1336 /* sort bx y */
1337 if (y1 > y2) {
8d14daab 1338 i_img_dim t;
aa833c97
AMH
1339 t = x1; x1 = x2; x2 = t;
1340 t = y1; y1 = y2; y2 = t;
1341 }
1342
8d14daab 1343 dy = i_abs(dy);
aa833c97
AMH
1344 dx = x2 - x1;
1345 dy2 = dy*2;
1346
1347 if (dx<0) {
1348 dx = -dx;
1349 cpx = -1;
1350 } else {
1351 cpx = 1;
1352 }
1353 dx2 = dx*2;
1354 p = dx2 - dy;
1355
1356 x = x1;
1357
1358 for(y=y1; y<y2-1; y++) {
1359 if (p<0) {
1360 p += dx2;
1361 } else {
1362 x += cpx;
1363 p += dx2-dy2;
1364 }
1365 i_ppix(im, x, y+1, val);
1366 }
1367 }
1368 if (endp) {
1369 i_ppix(im, x1, y1, val);
1370 i_ppix(im, x2, y2, val);
1371 } else {
1372 if (x1 != x2 || y1 != y2)
1373 i_ppix(im, x1, y1, val);
1374 }
02d1d628
AMH
1375}
1376
aa833c97 1377
02d1d628 1378void
8d14daab 1379i_line_dda(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_color *val) {
b437ce0a 1380
8d14daab
TC
1381 double dy;
1382 i_img_dim x;
b437ce0a
AMH
1383
1384 for(x=x1; x<=x2; x++) {
8d14daab
TC
1385 dy = y1+ (x-x1)/(double)(x2-x1)*(y2-y1);
1386 i_ppix(im, x, (i_img_dim)(dy+0.5), val);
b437ce0a
AMH
1387 }
1388}
1389
92bda632 1390/*
5715f7c3 1391=item i_line_aa(C<im>, C<x1>, C<x2>, C<y1>, C<y2>, C<color>, C<endp>)
92bda632
TC
1392
1393=category Drawing
1394
5715f7c3 1395Anti-alias draws a line from (x1,y1) to (x2, y2) in color.
b437ce0a 1396
5715f7c3 1397The point (x2, y2) is drawn only if C<endp> is set.
92bda632
TC
1398
1399=cut
1400*/
b437ce0a
AMH
1401
1402void
8d14daab
TC
1403i_line_aa(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, const i_color *val, int endp) {
1404 i_img_dim x, y;
1405 i_img_dim dx, dy;
1406 i_img_dim p;
b437ce0a
AMH
1407
1408 dx = x2 - x1;
1409 dy = y2 - y1;
1410
1411 /* choose variable to iterate on */
8d14daab
TC
1412 if (i_abs(dx) > i_abs(dy)) {
1413 i_img_dim dx2, dy2, cpy;
b437ce0a
AMH
1414
1415 /* sort by x */
1416 if (x1 > x2) {
8d14daab 1417 i_img_dim t;
b437ce0a
AMH
1418 t = x1; x1 = x2; x2 = t;
1419 t = y1; y1 = y2; y2 = t;
1420 }
1421
8d14daab 1422 dx = i_abs(dx);
b437ce0a
AMH
1423 dx2 = dx*2;
1424 dy = y2 - y1;
1425
1426 if (dy<0) {
1427 dy = -dy;
1428 cpy = -1;
1429 } else {
1430 cpy = 1;
1431 }
1432 dy2 = dy*2;
1433 p = dy2 - dx2; /* this has to be like this for AA */
1434
1435 y = y1;
1436
1437 for(x=x1; x<x2-1; x++) {
1438 int ch;
1439 i_color tval;
8d14daab
TC
1440 double t = (dy) ? -(float)(p)/(float)(dx2) : 1;
1441 double t1, t2;
b437ce0a
AMH
1442
1443 if (t<0) t = 0;
1444 t1 = 1-t;
1445 t2 = t;
1446
1447 i_gpix(im,x+1,y,&tval);
1448 for(ch=0;ch<im->channels;ch++)
1449 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
1450 i_ppix(im,x+1,y,&tval);
1451
1452 i_gpix(im,x+1,y+cpy,&tval);
1453 for(ch=0;ch<im->channels;ch++)
1454 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
1455 i_ppix(im,x+1,y+cpy,&tval);
1456
1457 if (p<0) {
1458 p += dy2;
1459 } else {
1460 y += cpy;
1461 p += dy2-dx2;
1462 }
1463 }
1464 } else {
8d14daab 1465 i_img_dim dy2, dx2, cpx;
b437ce0a
AMH
1466
1467 /* sort bx y */
1468 if (y1 > y2) {
8d14daab 1469 i_img_dim t;
b437ce0a
AMH
1470 t = x1; x1 = x2; x2 = t;
1471 t = y1; y1 = y2; y2 = t;
1472 }
1473
8d14daab 1474 dy = i_abs(dy);
b437ce0a
AMH
1475 dx = x2 - x1;
1476 dy2 = dy*2;
1477
1478 if (dx<0) {
1479 dx = -dx;
1480 cpx = -1;
1481 } else {
1482 cpx = 1;
1483 }
1484 dx2 = dx*2;
1485 p = dx2 - dy2; /* this has to be like this for AA */
1486
1487 x = x1;
1488
1489 for(y=y1; y<y2-1; y++) {
1490 int ch;
1491 i_color tval;
8d14daab
TC
1492 double t = (dx) ? -(double)(p)/(double)(dy2) : 1;
1493 double t1, t2;
b437ce0a
AMH
1494
1495 if (t<0) t = 0;
1496 t1 = 1-t;
1497 t2 = t;
1498
1499 i_gpix(im,x,y+1,&tval);
1500 for(ch=0;ch<im->channels;ch++)
8d14daab 1501 tval.channel[ch]=(unsigned char)(t1*(double)tval.channel[ch]+t2*(double)val->channel[ch]);
b437ce0a
AMH
1502 i_ppix(im,x,y+1,&tval);
1503
1504 i_gpix(im,x+cpx,y+1,&tval);
1505 for(ch=0;ch<im->channels;ch++)
8d14daab 1506 tval.channel[ch]=(unsigned char)(t2*(double)tval.channel[ch]+t1*(double)val->channel[ch]);
b437ce0a
AMH
1507 i_ppix(im,x+cpx,y+1,&tval);
1508
1509 if (p<0) {
1510 p += dx2;
1511 } else {
1512 x += cpx;
1513 p += dx2-dy2;
1514 }
1515 }
1516 }
1517
1518
1519 if (endp) {
1520 i_ppix(im, x1, y1, val);
1521 i_ppix(im, x2, y2, val);
1522 } else {
1523 if (x1 != x2 || y1 != y2)
1524 i_ppix(im, x1, y1, val);
1525 }
1526}
1527
1528
1529
b33c08f8 1530static double
8d14daab 1531perm(i_img_dim n,i_img_dim k) {
02d1d628 1532 double r;
8d14daab 1533 i_img_dim i;
02d1d628
AMH
1534 r=1;
1535 for(i=k+1;i<=n;i++) r*=i;
1536 for(i=1;i<=(n-k);i++) r/=i;
1537 return r;
1538}
1539
1540
1541/* Note in calculating t^k*(1-t)^(n-k)
1542 we can start by using t^0=1 so this simplifies to
1543 t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
1544 to get a new level - this may lead to errors who knows lets test it */
1545
1546void
97ac0a96 1547i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
02d1d628
AMH
1548 double *bzcoef;
1549 double t,cx,cy;
1550 int k,i;
8d14daab 1551 i_img_dim lx = 0,ly = 0;
02d1d628
AMH
1552 int n=l-1;
1553 double itr,ccoef;
1554
f0960b14
TC
1555 /* this is the same size as the x and y arrays, so shouldn't overflow */
1556 bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
02d1d628
AMH
1557 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1558 ICL_info(val);
1559
1560
1561 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1562 i=0;
1563 for(t=0;t<=1;t+=0.005) {
1564 cx=cy=0;
1565 itr=t/(1-t);
1566 ccoef=pow(1-t,n);
1567 for(k=0;k<l;k++) {
1568 /* cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k);
1569 cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1570
1571 cx+=bzcoef[k]*x[k]*ccoef;
1572 cy+=bzcoef[k]*y[k]*ccoef;
1573 ccoef*=itr;
1574 }
1575 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1576 if (i++) {
8d14daab 1577 i_line_aa(im,lx,ly,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val, 1);
02d1d628 1578 }
8d14daab
TC
1579 /* i_ppix(im,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val); */
1580 lx=(i_img_dim)(0.5+cx);
1581 ly=(i_img_dim)(0.5+cy);
02d1d628
AMH
1582 }
1583 ICL_info(val);
1584 myfree(bzcoef);
1585}
1586
02d1d628
AMH
1587/* Flood fill
1588
1589 REF: Graphics Gems I. page 282+
1590
1591*/
1592
02d1d628
AMH
1593/* This should be moved into a seperate file? */
1594
1595/* This is the truncation used:
1596
1597 a double is multiplied by 16 and then truncated.
1598 This means that 0 -> 0
1599 So a triangle of (0,0) (10,10) (10,0) Will look like it's
1600 not filling the (10,10) point nor the (10,0)-(10,10) line segment
1601
1602*/
1603
1604
02d1d628
AMH
1605/* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
1606 graphics gems I */
1607
1608/*
1609struct stc {
8d14daab
TC
1610 i_img_dim mylx,myrx;
1611 i_img_dim dadlx,dadrx;
1612 i_img_dim myy;
02d1d628
AMH
1613 int mydirection;
1614};
1615
1616Not used code???
1617*/
1618
1619
1620struct stack_element {
8d14daab
TC
1621 i_img_dim myLx,myRx;
1622 i_img_dim dadLx,dadRx;
1623 i_img_dim myY;
02d1d628
AMH
1624 int myDirection;
1625};
1626
1627
1628/* create the link data to put push onto the stack */
1629
1630static
1631struct stack_element*
8d14daab 1632crdata(i_img_dim left,i_img_dim right,i_img_dim dadl,i_img_dim dadr,i_img_dim y, int dir) {
02d1d628 1633 struct stack_element *ste;
f0960b14 1634 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
a73aeb5f
AMH
1635 ste->myLx = left;
1636 ste->myRx = right;
1637 ste->dadLx = dadl;
1638 ste->dadRx = dadr;
1639 ste->myY = y;
1640 ste->myDirection = dir;
02d1d628
AMH
1641 return ste;
1642}
1643
1644/* i_ccomp compares two colors and gives true if they are the same */
1645
3efb0915
TC
1646typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1647
02d1d628 1648static int
3efb0915 1649i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
02d1d628 1650 int i;
3efb0915
TC
1651 for(i = 0; i < ch; i++)
1652 if (val1->channel[i] !=val2->channel[i])
1653 return 0;
02d1d628
AMH
1654 return 1;
1655}
1656
3efb0915
TC
1657static int
1658i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
1659 int i;
1660 for(i = 0; i < ch; i++)
1661 if (val1->channel[i] !=val2->channel[i])
1662 return 1;
1663 return 0;
1664}
02d1d628
AMH
1665
1666static int
8d14daab 1667i_lspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
02d1d628
AMH
1668 i_color cval;
1669 while(1) {
1670 if (seedx-1 < 0) break;
1671 i_gpix(im,seedx-1,seedy,&cval);
3efb0915
TC
1672 if (!cmpfunc(val,&cval,im->channels))
1673 break;
02d1d628
AMH
1674 seedx--;
1675 }
1676 return seedx;
1677}
1678
1679static int
8d14daab 1680i_rspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
02d1d628
AMH
1681 i_color cval;
1682 while(1) {
1683 if (seedx+1 > im->xsize-1) break;
1684 i_gpix(im,seedx+1,seedy,&cval);
3efb0915 1685 if (!cmpfunc(val,&cval,im->channels)) break;
02d1d628
AMH
1686 seedx++;
1687 }
1688 return seedx;
1689}
1690
1691/* Macro to create a link and push on to the list */
1692
e25e59b1
AMH
1693#define ST_PUSH(left,right,dadl,dadr,y,dir) do { \
1694 struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); \
1695 llist_push(st,&s); \
1696} while (0)
02d1d628
AMH
1697
1698/* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1699/* No overflow check! */
1700
e25e59b1
AMH
1701#define ST_POP() do { \
1702 struct stack_element *s; \
1703 llist_pop(st,&s); \
1704 lx = s->myLx; \
1705 rx = s->myRx; \
1706 dadLx = s->dadLx; \
1707 dadRx = s->dadRx; \
1708 y = s->myY; \
1709 direction = s->myDirection; \
1710 myfree(s); \
1711} while (0)
1712
1713#define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
8d14daab
TC
1714 i_img_dim pushrx = rx+1; \
1715 i_img_dim pushlx = lx-1; \
e25e59b1
AMH
1716 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
1717 if (rx > dadRx) \
1718 ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); \
1719 if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); \
1720} while (0)
1721
1722#define SET(x,y) btm_set(btm,x,y)
02d1d628 1723
86d20cb9 1724/* INSIDE returns true if pixel is correct color and we haven't set it before. */
3efb0915 1725#define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels) ) ))
02d1d628 1726
02d1d628 1727
02d1d628 1728
aa833c97
AMH
1729/* The function that does all the real work */
1730
1731static struct i_bitmap *
8d14daab
TC
1732i_flood_fill_low(i_img *im,i_img_dim seedx,i_img_dim seedy,
1733 i_img_dim *bxminp, i_img_dim *bxmaxp, i_img_dim *byminp, i_img_dim *bymaxp,
3efb0915 1734 i_color const *seed, ff_cmpfunc cmpfunc) {
8d14daab
TC
1735 i_img_dim ltx, rtx;
1736 i_img_dim tx = 0;
02d1d628 1737
8d14daab
TC
1738 i_img_dim bxmin = seedx;
1739 i_img_dim bxmax = seedx;
1740 i_img_dim bymin = seedy;
1741 i_img_dim bymax = seedy;
02d1d628
AMH
1742
1743 struct llist *st;
1744 struct i_bitmap *btm;
1745
8d14daab
TC
1746 int channels;
1747 i_img_dim xsize,ysize;
3efb0915 1748 i_color cval;
02d1d628 1749
a73aeb5f
AMH
1750 channels = im->channels;
1751 xsize = im->xsize;
1752 ysize = im->ysize;
02d1d628 1753
86d20cb9
AMH
1754 btm = btm_new(xsize, ysize);
1755 st = llist_new(100, sizeof(struct stack_element*));
02d1d628 1756
02d1d628 1757 /* Find the starting span and fill it */
3efb0915
TC
1758 ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1759 rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
aa833c97 1760 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
353eb6e7
TC
1761 bxmin = ltx;
1762 bxmax = rtx;
02d1d628 1763
aa833c97
AMH
1764 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1765 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
02d1d628
AMH
1766
1767 while(st->count) {
aa833c97 1768 /* Stack variables */
8d14daab
TC
1769 i_img_dim lx,rx;
1770 i_img_dim dadLx,dadRx;
1771 i_img_dim y;
aa833c97 1772 int direction;
e25e59b1 1773
8d14daab 1774 i_img_dim x;
aa833c97 1775 int wasIn=0;
02d1d628 1776
aa833c97
AMH
1777 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1778
1779
1780 if (y<0 || y>ysize-1) continue;
02d1d628
AMH
1781 if (bymin > y) bymin=y; /* in the worst case an extra line */
1782 if (bymax < y) bymax=y;
1783
e25e59b1
AMH
1784
1785 x = lx+1;
3efb0915 1786 if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
aa833c97 1787 SET(lx, y);
02d1d628 1788 lx--;
95b9922f 1789 while(lx >= 0 && INSIDE(lx, y, seed)) {
02d1d628
AMH
1790 SET(lx,y);
1791 lx--;
1792 }
1793 }
1794
86d20cb9 1795 if (bxmin > lx) bxmin = lx;
02d1d628
AMH
1796 while(x <= xsize-1) {
1797 /* printf("x=%d\n",x); */
1798 if (wasIn) {
1799
3efb0915 1800 if (INSIDE(x, y, seed)) {
02d1d628
AMH
1801 /* case 1: was inside, am still inside */
1802 SET(x,y);
1803 } else {
1804 /* case 2: was inside, am no longer inside: just found the
1805 right edge of a span */
aa833c97 1806 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
02d1d628 1807
aa833c97 1808 if (bxmax < x) bxmax = x;
02d1d628
AMH
1809 wasIn=0;
1810 }
1811 } else {
aa833c97 1812 if (x > rx) goto EXT;
3efb0915 1813 if (INSIDE(x, y, seed)) {
aa833c97 1814 SET(x, y);
02d1d628 1815 /* case 3: Wasn't inside, am now: just found the start of a new run */
aa833c97
AMH
1816 wasIn = 1;
1817 lx = x;
02d1d628
AMH
1818 } else {
1819 /* case 4: Wasn't inside, still isn't */
1820 }
1821 }
1822 x++;
1823 }
1824 EXT: /* out of loop */
1825 if (wasIn) {
1826 /* hit an edge of the frame buffer while inside a run */
aa833c97
AMH
1827 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1828 if (bxmax < x) bxmax = x;
02d1d628
AMH
1829 }
1830 }
02d1d628 1831
02d1d628 1832 llist_destroy(st);
cc6483e0 1833
aa833c97
AMH
1834 *bxminp = bxmin;
1835 *bxmaxp = bxmax;
1836 *byminp = bymin;
1837 *bymaxp = bymax;
cc6483e0 1838
aa833c97
AMH
1839 return btm;
1840}
cc6483e0 1841
92bda632 1842/*
5715f7c3 1843=item i_flood_fill(C<im>, C<seedx>, C<seedy>, C<color>)
92bda632
TC
1844
1845=category Drawing
1846=synopsis i_flood_fill(im, 50, 50, &color);
cc6483e0 1847
5715f7c3
TC
1848Flood fills the 4-connected region starting from the point (C<seedx>,
1849C<seedy>) with I<color>.
cc6483e0 1850
5715f7c3 1851Returns false if (C<seedx>, C<seedy>) are outside the image.
92bda632
TC
1852
1853=cut
1854*/
cc6483e0 1855
aa833c97 1856undef_int
8d14daab
TC
1857i_flood_fill(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol) {
1858 i_img_dim bxmin, bxmax, bymin, bymax;
aa833c97 1859 struct i_bitmap *btm;
8d14daab 1860 i_img_dim x, y;
3efb0915 1861 i_color val;
af22c916 1862 dIMCTXim(im);
dc95a369 1863
857e686a
TC
1864 im_log((aIMCTX, 1, "i_flood_fill(im %p, seed(" i_DFp "), col %p)",
1865 im, i_DFcp(seedx, seedy), dcol));
1866
af22c916 1867 im_clear_error(aIMCTX);
aa833c97
AMH
1868 if (seedx < 0 || seedx >= im->xsize ||
1869 seedy < 0 || seedy >= im->ysize) {
af22c916 1870 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
aa833c97 1871 return 0;
cc6483e0 1872 }
cc6483e0 1873
3efb0915
TC
1874 /* Get the reference color */
1875 i_gpix(im, seedx, seedy, &val);
1876
1877 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1878 &val, i_ccomp_normal);
cc6483e0 1879
aa833c97
AMH
1880 for(y=bymin;y<=bymax;y++)
1881 for(x=bxmin;x<=bxmax;x++)
1882 if (btm_test(btm,x,y))
1883 i_ppix(im,x,y,dcol);
1884 btm_destroy(btm);
1885 return 1;
cc6483e0
TC
1886}
1887
92bda632 1888/*
5715f7c3 1889=item i_flood_cfill(C<im>, C<seedx>, C<seedy>, C<fill>)
92bda632
TC
1890
1891=category Drawing
1892=synopsis i_flood_cfill(im, 50, 50, fill);
aa833c97 1893
5715f7c3
TC
1894Flood fills the 4-connected region starting from the point (C<seedx>,
1895C<seedy>) with C<fill>.
92bda632 1896
5715f7c3 1897Returns false if (C<seedx>, C<seedy>) are outside the image.
92bda632
TC
1898
1899=cut
1900*/
aa833c97 1901
a321d497 1902undef_int
8d14daab
TC
1903i_flood_cfill(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill) {
1904 i_img_dim bxmin, bxmax, bymin, bymax;
cc6483e0 1905 struct i_bitmap *btm;
3efb0915 1906 i_color val;
af22c916 1907 dIMCTXim(im);
cc6483e0 1908
857e686a
TC
1909 im_log((aIMCTX, 1, "i_flood_cfill(im %p, seed(" i_DFp "), fill %p)",
1910 im, i_DFcp(seedx, seedy), fill));
1911
af22c916 1912 im_clear_error(aIMCTX);
a321d497
AMH
1913
1914 if (seedx < 0 || seedx >= im->xsize ||
1915 seedy < 0 || seedy >= im->ysize) {
af22c916 1916 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
a321d497
AMH
1917 return 0;
1918 }
1919
3efb0915
TC
1920 /* Get the reference color */
1921 i_gpix(im, seedx, seedy, &val);
1922
1923 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1924 &val, i_ccomp_normal);
1925
1926 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1927
1928 btm_destroy(btm);
1929 return 1;
1930}
1931
1932/*
5715f7c3 1933=item i_flood_fill_border(C<im>, C<seedx>, C<seedy>, C<color>, C<border>)
3efb0915
TC
1934
1935=category Drawing
1936=synopsis i_flood_fill_border(im, 50, 50, &color, &border);
1937
5715f7c3
TC
1938Flood fills the 4-connected region starting from the point (C<seedx>,
1939C<seedy>) with C<color>, fill stops when the fill reaches a pixels
1940with color C<border>.
3efb0915 1941
5715f7c3 1942Returns false if (C<seedx>, C<seedy>) are outside the image.
3efb0915
TC
1943
1944=cut
1945*/
1946
1947undef_int
8d14daab 1948i_flood_fill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol,
3efb0915 1949 const i_color *border) {
8d14daab 1950 i_img_dim bxmin, bxmax, bymin, bymax;
3efb0915 1951 struct i_bitmap *btm;
8d14daab 1952 i_img_dim x, y;
af22c916 1953 dIMCTXim(im);
dc95a369 1954
857e686a
TC
1955 im_log((aIMCTX, 1, "i_flood_cfill(im %p, seed(" i_DFp "), dcol %p, border %p)",
1956 im, i_DFcp(seedx, seedy), dcol, border));
1957
af22c916 1958 im_clear_error(aIMCTX);
3efb0915
TC
1959 if (seedx < 0 || seedx >= im->xsize ||
1960 seedy < 0 || seedy >= im->ysize) {
af22c916 1961 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
3efb0915
TC
1962 return 0;
1963 }
1964
1965 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1966 border, i_ccomp_border);
1967
1968 for(y=bymin;y<=bymax;y++)
1969 for(x=bxmin;x<=bxmax;x++)
1970 if (btm_test(btm,x,y))
1971 i_ppix(im,x,y,dcol);
1972 btm_destroy(btm);
1973 return 1;
1974}
1975
1976/*
5715f7c3 1977=item i_flood_cfill_border(C<im>, C<seedx>, C<seedy>, C<fill>, C<border>)
3efb0915
TC
1978
1979=category Drawing
1980=synopsis i_flood_cfill_border(im, 50, 50, fill, border);
1981
5715f7c3
TC
1982Flood fills the 4-connected region starting from the point (C<seedx>,
1983C<seedy>) with C<fill>, the fill stops when it reaches pixels of color
1984C<border>.
3efb0915 1985
5715f7c3 1986Returns false if (C<seedx>, C<seedy>) are outside the image.
3efb0915
TC
1987
1988=cut
1989*/
1990
1991undef_int
8d14daab 1992i_flood_cfill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill,
3efb0915 1993 const i_color *border) {
8d14daab 1994 i_img_dim bxmin, bxmax, bymin, bymax;
3efb0915 1995 struct i_bitmap *btm;
af22c916 1996 dIMCTXim(im);
dc95a369 1997
857e686a
TC
1998 im_log((aIMCTX, 1, "i_flood_cfill_border(im %p, seed(" i_DFp "), fill %p, border %p)",
1999 im, i_DFcp(seedx, seedy), fill, border));
2000
af22c916 2001 im_clear_error(aIMCTX);
3efb0915
TC
2002
2003 if (seedx < 0 || seedx >= im->xsize ||
2004 seedy < 0 || seedy >= im->ysize) {
af22c916 2005 im_push_error(aIMCTX, 0, "i_flood_cfill_border: Seed pixel outside of image");
3efb0915
TC
2006 return 0;
2007 }
2008
2009 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
2010 border, i_ccomp_border);
2011
2012 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
2013
2014 btm_destroy(btm);
2015
2016 return 1;
2017}
2018
2019static void
2020cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
8d14daab
TC
2021 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax) {
2022 i_img_dim x, y;
2023 i_img_dim start;
cc6483e0 2024
9b1ec2b8
TC
2025 i_render r;
2026
2027 i_render_init(&r, im, bxmax - bxmin + 1);
2028
2029 for(y=bymin; y<=bymax; y++) {
2030 x = bxmin;
2031 while (x <= bxmax) {
2032 while (x <= bxmax && !btm_test(btm, x, y)) {
2033 ++x;
cc6483e0 2034 }
9b1ec2b8
TC
2035 if (btm_test(btm, x, y)) {
2036 start = x;
2037 while (x <= bxmax && btm_test(btm, x, y)) {
2038 ++x;
2039 }
2040 i_render_fill(&r, start, y, x-start, NULL, fill);
cc6483e0
TC
2041 }
2042 }
cc6483e0 2043 }
9b1ec2b8 2044 i_render_done(&r);
cc6483e0 2045}
12db268a
TC
2046
2047/*
2048=back
2049
2050=cut
2051*/