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