1 #define IMAGER_NO_CONTEXT
10 i_ppix_norm(i_img *im, i_img_dim x, i_img_dim y, i_color const *col) {
19 switch (im->channels) {
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);
31 i_adapt_colors(2, 4, &work, 1);
32 i_gpix(im, x, y, &src);
33 remains = 255 - work.channel[1];
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);
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);
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);
59 i_gpix(im, x, y, &src);
60 remains = 255 - work.channel[3];
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);
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);
80 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
81 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax);
84 i_mmarray_cr(i_mmarray *ar,i_img_dim l) {
89 alloc_size = sizeof(minmax) * l;
90 /* check for overflow */
91 if (alloc_size / l != sizeof(minmax)) {
92 fprintf(stderr, "overflow calculating memory allocation");
95 ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
96 for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
100 i_mmarray_dst(i_mmarray *ar) {
102 if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
106 i_mmarray_add(i_mmarray *ar,i_img_dim x,i_img_dim y) {
107 if (y>-1 && y<ar->lines)
109 if (x<ar->data[y].min) ar->data[y].min=x;
110 if (x>ar->data[y].max) ar->data[y].max=x;
115 i_mmarray_gmin(i_mmarray *ar,i_img_dim y) {
116 if (y>-1 && y<ar->lines) return ar->data[y].min;
121 i_mmarray_getm(i_mmarray *ar,i_img_dim y) {
122 if (y>-1 && y<ar->lines) return ar->data[y].max;
129 i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
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);
137 i_arcdraw(i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_mmarray *ar) {
141 alpha=(double)(y2-y1)/(double)(x2-x1);
142 if (fabs(alpha) <= 1)
144 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
148 i_mmarray_add(ar,x1,(i_img_dim)(dsec+0.5));
156 if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
160 i_mmarray_add(ar,(i_img_dim)(dsec+0.5),y1);
168 i_mmarray_info(i_mmarray *ar) {
170 for(i=0;i<ar->lines;i++)
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));
177 i_arc_minmax(i_int_hlines *hlines,i_img_dim x,i_img_dim y, double rad,float d1,float d2) {
182 i_mmarray_cr(&dot, hlines->limit_y);
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));
186 fx=(float)x1; fy=(float)y1;
188 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
189 i_arcdraw(x, y, x1, y1, &dot);
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));
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)));
197 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
198 i_arcdraw(x, y, x1, y1, &dot);
200 /* render the minmax values onto the hlines */
201 for (y = 0; y < dot.lines; y++) {
202 if (dot.data[y].max!=-1) {
203 i_img_dim minx, width;
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);
215 i_arc_hlines(i_int_hlines *hlines,i_img_dim x,i_img_dim y,double rad,float d1,float d2) {
217 i_arc_minmax(hlines, x, y, rad, d1, d2);
220 i_arc_minmax(hlines, x, y, rad, d1, 360);
221 i_arc_minmax(hlines, x, y, rad, 0, d2);
226 =item i_arc(im, x, y, rad, d1, d2, color)
229 =synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
231 Fills an arc centered at (x,y) with radius I<rad> covering the range
232 of angles in degrees from d1 to d2, with the color.
238 i_arc(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,const i_color *val) {
242 im_log((aIMCTX,1,"i_arc(im %p,(x,y)=(" i_DFp "), rad %f, d1 %f, d2 %f, col %p)",
243 im, i_DFcp(x, y), rad, d1, d2, val));
245 i_int_init_hlines_img(&hlines, im);
247 i_arc_hlines(&hlines, x, y, rad, d1, d2);
249 i_int_hlines_fill_color(im, &hlines, val);
251 i_int_hlines_destroy(&hlines);
255 =item i_arc_cfill(im, x, y, rad, d1, d2, fill)
258 =synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
260 Fills an arc centered at (x,y) with radius I<rad> covering the range
261 of angles in degrees from d1 to d2, with the fill object.
266 #define MIN_CIRCLE_STEPS 8
267 #define MAX_CIRCLE_STEPS 360
270 i_arc_cfill(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,i_fill_t *fill) {
274 im_log((aIMCTX,1,"i_arc_cfill(im %p,(x,y)=(" i_DFp "), rad %f, d1 %f, d2 %f, fill %p)",
275 im, i_DFcp(x, y), rad, d1, d2, fill));
277 i_int_init_hlines_img(&hlines, im);
279 i_arc_hlines(&hlines, x, y, rad, d1, d2);
281 i_int_hlines_fill_fill(im, &hlines, fill);
283 i_int_hlines_destroy(&hlines);
287 arc_poly(int *count, double **xvals, double **yvals,
288 double x, double y, double rad, double d1, double d2) {
289 double d1_rad, d2_rad;
291 i_img_dim steps, point_count;
294 /* normalize the angles */
297 if (d2 >= 360) { /* default is 361 */
311 d1_rad = d1 * PI / 180;
312 d2_rad = d2 * PI / 180;
314 /* how many segments for the curved part?
315 we do a maximum of one per degree, with a minimum of 8/circle
316 we try to aim at having about one segment per 2 pixels
317 Work it out per circle to get a step size.
319 I was originally making steps = circum/2 but that looked horrible.
321 I think there might be an issue in the polygon filler.
323 circum = 2 * PI * rad;
325 if (steps > MAX_CIRCLE_STEPS)
326 steps = MAX_CIRCLE_STEPS;
327 else if (steps < MIN_CIRCLE_STEPS)
328 steps = MIN_CIRCLE_STEPS;
330 angle_inc = 2 * PI / steps;
332 point_count = steps + 5; /* rough */
333 /* point_count is always relatively small, so allocation won't overflow */
334 *xvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
335 *yvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
337 /* from centre to edge at d1 */
340 (*xvals)[1] = x + rad * cos(d1_rad);
341 (*yvals)[1] = y + rad * sin(d1_rad);
344 /* step around the curve */
345 while (d1_rad < d2_rad) {
346 (*xvals)[*count] = x + rad * cos(d1_rad);
347 (*yvals)[*count] = y + rad * sin(d1_rad);
352 /* finish off the curve */
353 (*xvals)[*count] = x + rad * cos(d2_rad);
354 (*yvals)[*count] = y + rad * sin(d2_rad);
359 =item i_arc_aa(im, x, y, rad, d1, d2, color)
362 =synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
364 Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
365 the range of angles in degrees from d1 to d2, with the color.
371 i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
372 const i_color *val) {
373 double *xvals, *yvals;
377 im_log((aIMCTX,1,"i_arc_aa(im %p,(x,y)=(%f,%f), rad %f, d1 %f, d2 %f, col %p)",
378 im, x, y, rad, d1, d2, val));
380 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
382 i_poly_aa(im, count, xvals, yvals, val);
389 =item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
392 =synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
394 Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
395 the range of angles in degrees from d1 to d2, with the fill object.
401 i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
403 double *xvals, *yvals;
407 im_log((aIMCTX,1,"i_arc_aa_cfill(im %p,(x,y)=(%f,%f), rad %f, d1 %f, d2 %f, fill %p)",
408 im, x, y, rad, d1, d2, fill));
410 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
412 i_poly_aa_cfill(im, count, xvals, yvals, fill);
418 /* Temporary AA HACK */
421 typedef i_img_dim frac;
422 static frac float_to_frac(double x) { return (frac)(0.5+x*16.0); }
426 polar_to_plane(double cx, double cy, float angle, double radius, frac *x, frac *y) {
427 *x = float_to_frac(cx+radius*cos(angle));
428 *y = float_to_frac(cy+radius*sin(angle));
433 make_minmax_list(pIMCTX, i_mmarray *dot, double x, double y, double radius) {
435 float astep = radius>0.1 ? .5/radius : 10;
436 frac cx, cy, lx, ly, sx, sy;
438 im_log((aIMCTX, 1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
440 polar_to_plane(x, y, angle, radius, &sx, &sy);
442 for(angle = 0.0; angle<361; angle +=astep) {
444 polar_to_plane(x, y, angle, radius, &cx, &cy);
447 if (fabs(cx-lx) > fabs(cy-ly)) {
450 ccx = lx; lx = cx; cx = ccx;
451 ccy = ly; ly = cy; cy = ccy;
454 for(ccx=lx; ccx<=cx; ccx++) {
455 ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
456 i_mmarray_add(dot, ccx, ccy);
462 ccy = ly; ly = cy; cy = ccy;
463 ccx = lx; lx = cx; cx = ccx;
466 for(ccy=ly; ccy<=cy; ccy++) {
467 if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
468 i_mmarray_add(dot, ccx, ccy);
474 /* Get the number of subpixels covered */
478 i_pixel_coverage(i_mmarray *dot, i_img_dim x, i_img_dim y) {
484 for(cy=y*16; cy<(y+1)*16; cy++) {
485 frac tmin = dot->data[cy].min;
486 frac tmax = dot->data[cy].max;
488 if (tmax == -1 || tmin > maxx || tmax < minx) continue;
490 if (tmin < minx) tmin = minx;
491 if (tmax > maxx) tmax = maxx;
499 =item i_circle_aa(im, x, y, rad, color)
502 =synopsis i_circle_aa(im, 50, 50, 45, &color);
504 Anti-alias fills a circle centered at (x,y) for radius I<rad> with
510 i_circle_aa(i_img *im, double x, double y, double rad, const i_color *val) {
516 im_log((aIMCTX, 1, "i_circle_aa(im %p, centre(" i_DFp "), rad %.2f, val %p)\n",
517 im, i_DFcp(x, y), rad, val));
519 i_mmarray_cr(&dot,16*im->ysize);
520 make_minmax_list(aIMCTX, &dot, x, y, rad);
522 for(ly = 0; ly<im->ysize; ly++) {
523 int ix, cy, minx = INT_MAX, maxx = INT_MIN;
525 /* Find the left/rightmost set subpixels */
526 for(cy = 0; cy<16; cy++) {
527 frac tmin = dot.data[ly*16+cy].min;
528 frac tmax = dot.data[ly*16+cy].max;
529 if (tmax == -1) continue;
531 if (minx > tmin) minx = tmin;
532 if (maxx < tmax) maxx = tmax;
535 if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
539 for(ix=minx; ix<=maxx; ix++) {
540 int cnt = i_pixel_coverage(&dot, ix, ly);
541 if (cnt>255) cnt = 255;
542 if (cnt) { /* should never be true */
544 float ratio = (float)cnt/255.0;
545 i_gpix(im, ix, ly, &temp);
546 for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
547 i_ppix(im, ix, ly, &temp);
555 =item i_circle_out(im, x, y, r, col)
558 =synopsis i_circle_out(im, 50, 50, 45, &color);
560 Draw a circle outline centered at (x,y) with radius r,
569 (x, y) - the center of the circle
573 r - the radius of the circle in pixels, must be non-negative
577 Returns non-zero on success.
585 i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
586 const i_color *col) {
592 im_log((aIMCTX, 1, "i_circle_out(im %p, centre(" i_DFp "), rad %" i_DF ", col %p)\n",
593 im, i_DFcp(xc, yc), i_DFc(r), col));
595 im_clear_error(aIMCTX);
598 im_push_error(aIMCTX, 0, "circle: radius must be non-negative");
602 i_ppix(im, xc+r, yc, col);
603 i_ppix(im, xc-r, yc, col);
604 i_ppix(im, xc, yc+r, col);
605 i_ppix(im, xc, yc-r, col);
622 i_ppix(im, xc + x, yc + y, col);
623 i_ppix(im, xc + x, yc - y, col);
624 i_ppix(im, xc - x, yc + y, col);
625 i_ppix(im, xc - x, yc - y, col);
627 i_ppix(im, xc + y, yc + x, col);
628 i_ppix(im, xc + y, yc - x, col);
629 i_ppix(im, xc - y, yc + x, col);
630 i_ppix(im, xc - y, yc - x, col);
640 Convert an angle in degrees into an angle measure we can generate
641 simply from the numbers we have when drawing the circle.
647 arc_seg(double angle, int scale) {
648 i_img_dim seg = (angle + 45) / 90;
649 double remains = angle - seg * 90; /* should be in the range [-45,45] */
653 if (seg == 4 && remains > 0)
656 return scale * (seg * 2 + sin(remains * PI/180));
660 =item i_arc_out(im, x, y, r, d1, d2, col)
663 =synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
665 Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
666 over the angle range d1 through d2 degrees.
674 (x, y) - the center of the circle
678 r - the radius of the circle in pixels, must be non-negative
682 d1, d2 - the range of angles to draw the arc over, in degrees.
686 Returns non-zero on success.
694 i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
695 double d1, double d2, const i_color *col) {
699 i_img_dim segs[2][2];
702 i_img_dim seg_d1, seg_d2;
704 i_img_dim scale = r + 1;
705 i_img_dim seg1 = scale * 2;
706 i_img_dim seg2 = scale * 4;
707 i_img_dim seg3 = scale * 6;
708 i_img_dim seg4 = scale * 8;
711 im_log((aIMCTX,1,"i_arc_out(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
712 im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
714 im_clear_error(aIMCTX);
717 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
721 return i_circle_out(im, xc, yc, r, col);
724 d1 += 360 * floor((-d1 + 359) / 360);
726 d2 += 360 * floor((-d2 + 359) / 360);
729 seg_d1 = arc_seg(d1, scale);
730 seg_d2 = arc_seg(d2, scale);
731 if (seg_d2 < seg_d1) {
732 /* split into two segments */
745 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
746 i_img_dim seg_start = segs[seg_num][0];
747 i_img_dim seg_end = segs[seg_num][1];
749 i_ppix(im, xc+r, yc, col);
750 if (seg_start <= seg1 && seg_end >= seg1)
751 i_ppix(im, xc, yc+r, col);
752 if (seg_start <= seg2 && seg_end >= seg2)
753 i_ppix(im, xc-r, yc, col);
754 if (seg_start <= seg3 && seg_end >= seg3)
755 i_ppix(im, xc, yc-r, col);
773 if (seg_start <= sin_th && seg_end >= sin_th)
774 i_ppix(im, xc + x, yc + y, col);
775 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
776 i_ppix(im, xc + y, yc + x, col);
778 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
779 i_ppix(im, xc - y, yc + x, col);
780 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
781 i_ppix(im, xc - x, yc + y, col);
783 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
784 i_ppix(im, xc - x, yc - y, col);
785 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
786 i_ppix(im, xc - y, yc - x, col);
788 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
789 i_ppix(im, xc + y, yc - x, col);
790 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
791 i_ppix(im, xc + x, yc - y, col);
799 cover(i_img_dim r, i_img_dim j) {
800 double rjsqrt = sqrt(r*r - j*j);
802 return ceil(rjsqrt) - rjsqrt;
806 =item i_circle_out_aa(im, xc, yc, r, col)
808 =synopsis i_circle_out_aa(im, 50, 50, 45, &color);
810 Draw a circle outline centered at (x,y) with radius r, anti-aliased.
818 (xc, yc) - the center of the circle
822 r - the radius of the circle in pixels, must be non-negative
826 col - an i_color for the color to draw in.
830 Returns non-zero on success.
834 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
837 I use floating point for I<D> since for large circles the precision of
838 a [0,255] value isn't sufficient when approaching the end of the
844 i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
847 i_color workc = *col;
848 int orig_alpha = col->channel[3];
851 im_log((aIMCTX,1,"i_circle_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", col %p)",
852 im, i_DFcp(xc, yc), i_DFc(r), col));
854 im_clear_error(aIMCTX);
856 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
862 i_ppix_norm(im, xc+i, yc+j, col);
863 i_ppix_norm(im, xc-i, yc+j, col);
864 i_ppix_norm(im, xc+j, yc+i, col);
865 i_ppix_norm(im, xc+j, yc-i, col);
872 cv = (int)(d * 255 + 0.5);
878 workc.channel[3] = orig_alpha * inv_cv / 255;
879 i_ppix_norm(im, xc+i, yc+j, &workc);
880 i_ppix_norm(im, xc-i, yc+j, &workc);
881 i_ppix_norm(im, xc+i, yc-j, &workc);
882 i_ppix_norm(im, xc-i, yc-j, &workc);
885 i_ppix_norm(im, xc+j, yc+i, &workc);
886 i_ppix_norm(im, xc-j, yc+i, &workc);
887 i_ppix_norm(im, xc+j, yc-i, &workc);
888 i_ppix_norm(im, xc-j, yc-i, &workc);
892 workc.channel[3] = orig_alpha * cv / 255;
893 i_ppix_norm(im, xc+i-1, yc+j, &workc);
894 i_ppix_norm(im, xc-i+1, yc+j, &workc);
895 i_ppix_norm(im, xc+i-1, yc-j, &workc);
896 i_ppix_norm(im, xc-i+1, yc-j, &workc);
899 i_ppix_norm(im, xc+j, yc+i-1, &workc);
900 i_ppix_norm(im, xc-j, yc+i-1, &workc);
901 i_ppix_norm(im, xc+j, yc-i+1, &workc);
902 i_ppix_norm(im, xc-j, yc-i+1, &workc);
912 =item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
914 =synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
916 Draw a circle arc outline centered at (x,y) with radius r, from angle
917 d1 degrees through angle d2 degrees, anti-aliased.
925 (xc, yc) - the center of the circle
929 r - the radius of the circle in pixels, must be non-negative
933 d1, d2 - the range of angle in degrees to draw the arc through. If
934 d2-d1 >= 360 a full circle is drawn.
938 Returns non-zero on success.
942 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
948 i_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) {
951 i_color workc = *col;
952 i_img_dim segs[2][2];
955 i_img_dim seg_d1, seg_d2;
957 int orig_alpha = col->channel[3];
958 i_img_dim scale = r + 1;
959 i_img_dim seg1 = scale * 2;
960 i_img_dim seg2 = scale * 4;
961 i_img_dim seg3 = scale * 6;
962 i_img_dim seg4 = scale * 8;
965 im_log((aIMCTX,1,"i_arc_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
966 im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
968 im_clear_error(aIMCTX);
970 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
974 return i_circle_out_aa(im, xc, yc, r, col);
977 d1 += 360 * floor((-d1 + 359) / 360);
979 d2 += 360 * floor((-d2 + 359) / 360);
982 seg_d1 = arc_seg(d1, scale);
983 seg_d2 = arc_seg(d2, scale);
984 if (seg_d2 < seg_d1) {
985 /* split into two segments */
998 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
999 i_img_dim seg_start = segs[seg_num][0];
1000 i_img_dim seg_end = segs[seg_num][1];
1007 i_ppix_norm(im, xc+i, yc+j, col);
1008 if (seg_start <= seg1 && seg_end >= seg1)
1009 i_ppix_norm(im, xc+j, yc+i, col);
1010 if (seg_start <= seg2 && seg_end >= seg2)
1011 i_ppix_norm(im, xc-i, yc+j, col);
1012 if (seg_start <= seg3 && seg_end >= seg3)
1013 i_ppix_norm(im, xc+j, yc-i, col);
1020 cv = (int)(d * 255 + 0.5);
1027 workc.channel[3] = orig_alpha * inv_cv / 255;
1029 if (seg_start <= sin_th && seg_end >= sin_th)
1030 i_ppix_norm(im, xc+i, yc+j, &workc);
1031 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1032 i_ppix_norm(im, xc-i, yc+j, &workc);
1033 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - 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);
1039 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1040 i_ppix_norm(im, xc+j, yc+i, &workc);
1041 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1042 i_ppix_norm(im, xc-j, yc+i, &workc);
1043 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1044 i_ppix_norm(im, xc+j, yc-i, &workc);
1045 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1046 i_ppix_norm(im, xc-j, yc-i, &workc);
1050 workc.channel[3] = orig_alpha * cv / 255;
1051 if (seg_start <= sin_th && seg_end >= sin_th)
1052 i_ppix_norm(im, xc+i-1, yc+j, &workc);
1053 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1054 i_ppix_norm(im, xc-i+1, yc+j, &workc);
1055 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - 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);
1060 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1061 i_ppix_norm(im, xc+j, yc+i-1, &workc);
1062 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1063 i_ppix_norm(im, xc-j, yc+i-1, &workc);
1064 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1065 i_ppix_norm(im, xc+j, yc-i+1, &workc);
1066 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1067 i_ppix_norm(im, xc-j, yc-i+1, &workc);
1077 =item i_box(im, x1, y1, x2, y2, color)
1080 =synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
1082 Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
1088 i_box(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2,const i_color *val) {
1092 im_log((aIMCTX, 1,"i_box(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1093 im, i_DFcp(x1,y1), i_DFcp(x2,y2), val));
1094 for(x=x1;x<x2+1;x++) {
1095 i_ppix(im,x,y1,val);
1096 i_ppix(im,x,y2,val);
1098 for(y=y1;y<y2+1;y++) {
1099 i_ppix(im,x1,y,val);
1100 i_ppix(im,x2,y,val);
1105 =item i_box_filled(im, x1, y1, x2, y2, color)
1108 =synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
1110 Fills the box from (x1,y1) to (x2,y2) inclusive with color.
1116 i_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) {
1117 i_img_dim x, y, width;
1121 im_log((aIMCTX,1,"i_box_filled(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1122 im, i_DFcp(x1, y1), i_DFcp(x2,y2) ,val));
1124 if (x1 > x2 || y1 > y2
1126 || x1 >= im->xsize || y1 > im->ysize)
1131 if (x2 >= im->xsize)
1135 if (y2 >= im->ysize)
1138 width = x2 - x1 + 1;
1140 if (im->type == i_palette_type
1141 && i_findcolor(im, val, &index)) {
1142 i_palidx *line = mymalloc(sizeof(i_palidx) * width);
1144 for (x = 0; x < width; ++x)
1147 for (y = y1; y <= y2; ++y)
1148 i_ppal(im, x1, x2+1, y, line);
1153 i_color *line = mymalloc(sizeof(i_color) * width);
1155 for (x = 0; x < width; ++x)
1158 for (y = y1; y <= y2; ++y)
1159 i_plin(im, x1, x2+1, y, line);
1166 =item i_box_filledf(im, x1, y1, x2, y2, color)
1169 =synopsis i_box_filledf(im, 0, 0, im->xsize-1, im->ysize-1, &fcolor);
1171 Fills the box from (x1,y1) to (x2,y2) inclusive with a floating point
1178 i_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) {
1179 i_img_dim x, y, width;
1182 im_log((aIMCTX, 1,"i_box_filledf(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1183 im, i_DFcp(x1, y1), i_DFcp(x2, y2), val));
1185 if (x1 > x2 || y1 > y2
1187 || x1 >= im->xsize || y1 > im->ysize)
1192 if (x2 >= im->xsize)
1196 if (y2 >= im->ysize)
1199 width = x2 - x1 + 1;
1201 if (im->bits <= 8) {
1203 c.rgba.r = SampleFTo8(val->rgba.r);
1204 c.rgba.g = SampleFTo8(val->rgba.g);
1205 c.rgba.b = SampleFTo8(val->rgba.b);
1206 c.rgba.a = SampleFTo8(val->rgba.a);
1208 i_box_filled(im, x1, y1, x2, y2, &c);
1211 i_fcolor *line = mymalloc(sizeof(i_fcolor) * width);
1213 for (x = 0; x < width; ++x)
1216 for (y = y1; y <= y2; ++y)
1217 i_plinf(im, x1, x2+1, y, line);
1226 =item i_box_cfill(im, x1, y1, x2, y2, fill)
1229 =synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
1231 Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
1237 i_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) {
1241 im_log((aIMCTX,1,"i_box_cfill(im* %p, p1(" i_DFp "), p2(" i_DFp "), fill %p)\n",
1242 im, i_DFcp(x1, y1), i_DFcp(x2,y2), fill));
1251 if (y2 >= im->ysize)
1253 if (x1 >= x2 || y1 > y2)
1256 i_render_init(&r, im, x2-x1);
1258 i_render_fill(&r, x1, y1, x2-x1, NULL, fill);
1265 =item i_line(C<im>, C<x1>, C<y1>, C<x2>, C<y2>, C<color>, C<endp>)
1269 =for stopwords Bresenham's
1271 Draw a line to image using Bresenham's line drawing algorithm
1273 im - image to draw to
1274 x1 - starting x coordinate
1275 y1 - starting x coordinate
1276 x2 - starting x coordinate
1277 y2 - starting x coordinate
1278 color - color to write to image
1279 endp - endpoint flag (boolean)
1285 i_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) {
1294 /* choose variable to iterate on */
1295 if (i_abs(dx) > i_abs(dy)) {
1296 i_img_dim dx2, dy2, cpy;
1301 t = x1; x1 = x2; x2 = t;
1302 t = y1; y1 = y2; y2 = t;
1320 for(x=x1; x<x2-1; x++) {
1327 i_ppix(im, x+1, y, val);
1330 i_img_dim dy2, dx2, cpx;
1335 t = x1; x1 = x2; x2 = t;
1336 t = y1; y1 = y2; y2 = t;
1354 for(y=y1; y<y2-1; y++) {
1361 i_ppix(im, x, y+1, val);
1365 i_ppix(im, x1, y1, val);
1366 i_ppix(im, x2, y2, val);
1368 if (x1 != x2 || y1 != y2)
1369 i_ppix(im, x1, y1, val);
1375 i_line_dda(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_color *val) {
1380 for(x=x1; x<=x2; x++) {
1381 dy = y1+ (x-x1)/(double)(x2-x1)*(y2-y1);
1382 i_ppix(im, x, (i_img_dim)(dy+0.5), val);
1387 =item i_line_aa(C<im>, C<x1>, C<x2>, C<y1>, C<y2>, C<color>, C<endp>)
1391 Anti-alias draws a line from (x1,y1) to (x2, y2) in color.
1393 The point (x2, y2) is drawn only if C<endp> is set.
1399 i_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) {
1407 /* choose variable to iterate on */
1408 if (i_abs(dx) > i_abs(dy)) {
1409 i_img_dim dx2, dy2, cpy;
1414 t = x1; x1 = x2; x2 = t;
1415 t = y1; y1 = y2; y2 = t;
1429 p = dy2 - dx2; /* this has to be like this for AA */
1433 for(x=x1; x<x2-1; x++) {
1436 double t = (dy) ? -(float)(p)/(float)(dx2) : 1;
1443 i_gpix(im,x+1,y,&tval);
1444 for(ch=0;ch<im->channels;ch++)
1445 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
1446 i_ppix(im,x+1,y,&tval);
1448 i_gpix(im,x+1,y+cpy,&tval);
1449 for(ch=0;ch<im->channels;ch++)
1450 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
1451 i_ppix(im,x+1,y+cpy,&tval);
1461 i_img_dim dy2, dx2, cpx;
1466 t = x1; x1 = x2; x2 = t;
1467 t = y1; y1 = y2; y2 = t;
1481 p = dx2 - dy2; /* this has to be like this for AA */
1485 for(y=y1; y<y2-1; y++) {
1488 double t = (dx) ? -(double)(p)/(double)(dy2) : 1;
1495 i_gpix(im,x,y+1,&tval);
1496 for(ch=0;ch<im->channels;ch++)
1497 tval.channel[ch]=(unsigned char)(t1*(double)tval.channel[ch]+t2*(double)val->channel[ch]);
1498 i_ppix(im,x,y+1,&tval);
1500 i_gpix(im,x+cpx,y+1,&tval);
1501 for(ch=0;ch<im->channels;ch++)
1502 tval.channel[ch]=(unsigned char)(t2*(double)tval.channel[ch]+t1*(double)val->channel[ch]);
1503 i_ppix(im,x+cpx,y+1,&tval);
1516 i_ppix(im, x1, y1, val);
1517 i_ppix(im, x2, y2, val);
1519 if (x1 != x2 || y1 != y2)
1520 i_ppix(im, x1, y1, val);
1527 perm(i_img_dim n,i_img_dim k) {
1531 for(i=k+1;i<=n;i++) r*=i;
1532 for(i=1;i<=(n-k);i++) r/=i;
1537 /* Note in calculating t^k*(1-t)^(n-k)
1538 we can start by using t^0=1 so this simplifies to
1539 t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
1540 to get a new level - this may lead to errors who knows lets test it */
1543 i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
1547 i_img_dim lx = 0,ly = 0;
1551 /* this is the same size as the x and y arrays, so shouldn't overflow */
1552 bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
1553 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1557 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1559 for(t=0;t<=1;t+=0.005) {
1564 /* cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k);
1565 cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1567 cx+=bzcoef[k]*x[k]*ccoef;
1568 cy+=bzcoef[k]*y[k]*ccoef;
1571 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1573 i_line_aa(im,lx,ly,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val, 1);
1575 /* i_ppix(im,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val); */
1576 lx=(i_img_dim)(0.5+cx);
1577 ly=(i_img_dim)(0.5+cy);
1585 REF: Graphics Gems I. page 282+
1589 /* This should be moved into a seperate file? */
1591 /* This is the truncation used:
1593 a double is multiplied by 16 and then truncated.
1594 This means that 0 -> 0
1595 So a triangle of (0,0) (10,10) (10,0) Will look like it's
1596 not filling the (10,10) point nor the (10,0)-(10,10) line segment
1601 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
1606 i_img_dim mylx,myrx;
1607 i_img_dim dadlx,dadrx;
1616 struct stack_element {
1617 i_img_dim myLx,myRx;
1618 i_img_dim dadLx,dadRx;
1624 /* create the link data to put push onto the stack */
1627 struct stack_element*
1628 crdata(i_img_dim left,i_img_dim right,i_img_dim dadl,i_img_dim dadr,i_img_dim y, int dir) {
1629 struct stack_element *ste;
1630 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
1636 ste->myDirection = dir;
1640 /* i_ccomp compares two colors and gives true if they are the same */
1642 typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1645 i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
1647 for(i = 0; i < ch; i++)
1648 if (val1->channel[i] !=val2->channel[i])
1654 i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
1656 for(i = 0; i < ch; i++)
1657 if (val1->channel[i] !=val2->channel[i])
1663 i_lspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1666 if (seedx-1 < 0) break;
1667 i_gpix(im,seedx-1,seedy,&cval);
1668 if (!cmpfunc(val,&cval,im->channels))
1676 i_rspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1679 if (seedx+1 > im->xsize-1) break;
1680 i_gpix(im,seedx+1,seedy,&cval);
1681 if (!cmpfunc(val,&cval,im->channels)) break;
1687 /* Macro to create a link and push on to the list */
1689 #define ST_PUSH(left,right,dadl,dadr,y,dir) do { \
1690 struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); \
1691 llist_push(st,&s); \
1694 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1695 /* No overflow check! */
1697 #define ST_POP() do { \
1698 struct stack_element *s; \
1705 direction = s->myDirection; \
1709 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
1710 i_img_dim pushrx = rx+1; \
1711 i_img_dim pushlx = lx-1; \
1712 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
1714 ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); \
1715 if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); \
1718 #define SET(x,y) btm_set(btm,x,y)
1720 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1721 #define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels) ) ))
1725 /* The function that does all the real work */
1727 static struct i_bitmap *
1728 i_flood_fill_low(i_img *im,i_img_dim seedx,i_img_dim seedy,
1729 i_img_dim *bxminp, i_img_dim *bxmaxp, i_img_dim *byminp, i_img_dim *bymaxp,
1730 i_color const *seed, ff_cmpfunc cmpfunc) {
1734 i_img_dim bxmin = seedx;
1735 i_img_dim bxmax = seedx;
1736 i_img_dim bymin = seedy;
1737 i_img_dim bymax = seedy;
1740 struct i_bitmap *btm;
1743 i_img_dim xsize,ysize;
1746 channels = im->channels;
1750 btm = btm_new(xsize, ysize);
1751 st = llist_new(100, sizeof(struct stack_element*));
1753 /* Find the starting span and fill it */
1754 ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1755 rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
1756 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1760 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1761 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1764 /* Stack variables */
1766 i_img_dim dadLx,dadRx;
1773 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1776 if (y<0 || y>ysize-1) continue;
1777 if (bymin > y) bymin=y; /* in the worst case an extra line */
1778 if (bymax < y) bymax=y;
1782 if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
1785 while(lx >= 0 && INSIDE(lx, y, seed)) {
1791 if (bxmin > lx) bxmin = lx;
1792 while(x <= xsize-1) {
1793 /* printf("x=%d\n",x); */
1796 if (INSIDE(x, y, seed)) {
1797 /* case 1: was inside, am still inside */
1800 /* case 2: was inside, am no longer inside: just found the
1801 right edge of a span */
1802 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1804 if (bxmax < x) bxmax = x;
1808 if (x > rx) goto EXT;
1809 if (INSIDE(x, y, seed)) {
1811 /* case 3: Wasn't inside, am now: just found the start of a new run */
1815 /* case 4: Wasn't inside, still isn't */
1820 EXT: /* out of loop */
1822 /* hit an edge of the frame buffer while inside a run */
1823 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1824 if (bxmax < x) bxmax = x;
1839 =item i_flood_fill(C<im>, C<seedx>, C<seedy>, C<color>)
1842 =synopsis i_flood_fill(im, 50, 50, &color);
1844 Flood fills the 4-connected region starting from the point (C<seedx>,
1845 C<seedy>) with I<color>.
1847 Returns false if (C<seedx>, C<seedy>) are outside the image.
1853 i_flood_fill(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol) {
1854 i_img_dim bxmin, bxmax, bymin, bymax;
1855 struct i_bitmap *btm;
1860 im_log((aIMCTX, 1, "i_flood_fill(im %p, seed(" i_DFp "), col %p)",
1861 im, i_DFcp(seedx, seedy), dcol));
1863 im_clear_error(aIMCTX);
1864 if (seedx < 0 || seedx >= im->xsize ||
1865 seedy < 0 || seedy >= im->ysize) {
1866 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
1870 /* Get the reference color */
1871 i_gpix(im, seedx, seedy, &val);
1873 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1874 &val, i_ccomp_normal);
1876 for(y=bymin;y<=bymax;y++)
1877 for(x=bxmin;x<=bxmax;x++)
1878 if (btm_test(btm,x,y))
1879 i_ppix(im,x,y,dcol);
1885 =item i_flood_cfill(C<im>, C<seedx>, C<seedy>, C<fill>)
1888 =synopsis i_flood_cfill(im, 50, 50, fill);
1890 Flood fills the 4-connected region starting from the point (C<seedx>,
1891 C<seedy>) with C<fill>.
1893 Returns false if (C<seedx>, C<seedy>) are outside the image.
1899 i_flood_cfill(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill) {
1900 i_img_dim bxmin, bxmax, bymin, bymax;
1901 struct i_bitmap *btm;
1905 im_log((aIMCTX, 1, "i_flood_cfill(im %p, seed(" i_DFp "), fill %p)",
1906 im, i_DFcp(seedx, seedy), fill));
1908 im_clear_error(aIMCTX);
1910 if (seedx < 0 || seedx >= im->xsize ||
1911 seedy < 0 || seedy >= im->ysize) {
1912 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
1916 /* Get the reference color */
1917 i_gpix(im, seedx, seedy, &val);
1919 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1920 &val, i_ccomp_normal);
1922 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1929 =item i_flood_fill_border(C<im>, C<seedx>, C<seedy>, C<color>, C<border>)
1932 =synopsis i_flood_fill_border(im, 50, 50, &color, &border);
1934 Flood fills the 4-connected region starting from the point (C<seedx>,
1935 C<seedy>) with C<color>, fill stops when the fill reaches a pixels
1936 with color C<border>.
1938 Returns false if (C<seedx>, C<seedy>) are outside the image.
1944 i_flood_fill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol,
1945 const i_color *border) {
1946 i_img_dim bxmin, bxmax, bymin, bymax;
1947 struct i_bitmap *btm;
1951 im_log((aIMCTX, 1, "i_flood_cfill(im %p, seed(" i_DFp "), dcol %p, border %p)",
1952 im, i_DFcp(seedx, seedy), dcol, border));
1954 im_clear_error(aIMCTX);
1955 if (seedx < 0 || seedx >= im->xsize ||
1956 seedy < 0 || seedy >= im->ysize) {
1957 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
1961 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1962 border, i_ccomp_border);
1964 for(y=bymin;y<=bymax;y++)
1965 for(x=bxmin;x<=bxmax;x++)
1966 if (btm_test(btm,x,y))
1967 i_ppix(im,x,y,dcol);
1973 =item i_flood_cfill_border(C<im>, C<seedx>, C<seedy>, C<fill>, C<border>)
1976 =synopsis i_flood_cfill_border(im, 50, 50, fill, border);
1978 Flood fills the 4-connected region starting from the point (C<seedx>,
1979 C<seedy>) with C<fill>, the fill stops when it reaches pixels of color
1982 Returns false if (C<seedx>, C<seedy>) are outside the image.
1988 i_flood_cfill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill,
1989 const i_color *border) {
1990 i_img_dim bxmin, bxmax, bymin, bymax;
1991 struct i_bitmap *btm;
1994 im_log((aIMCTX, 1, "i_flood_cfill_border(im %p, seed(" i_DFp "), fill %p, border %p)",
1995 im, i_DFcp(seedx, seedy), fill, border));
1997 im_clear_error(aIMCTX);
1999 if (seedx < 0 || seedx >= im->xsize ||
2000 seedy < 0 || seedy >= im->ysize) {
2001 im_push_error(aIMCTX, 0, "i_flood_cfill_border: Seed pixel outside of image");
2005 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
2006 border, i_ccomp_border);
2008 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
2016 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
2017 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax) {
2023 i_render_init(&r, im, bxmax - bxmin + 1);
2025 for(y=bymin; y<=bymax; y++) {
2027 while (x <= bxmax) {
2028 while (x <= bxmax && !btm_test(btm, x, y)) {
2031 if (btm_test(btm, x, y)) {
2033 while (x <= bxmax && btm_test(btm, x, y)) {
2036 i_render_fill(&r, start, y, x-start, NULL, fill);