1 #define IMAGER_NO_CONTEXT
12 i_ppix_norm(i_img *im, i_img_dim x, i_img_dim y, i_color const *col) {
21 switch (im->channels) {
24 i_adapt_colors(2, 4, &work, 1);
25 i_gpix(im, x, y, &src);
26 remains = 255 - work.channel[1];
27 src.channel[0] = (src.channel[0] * remains
28 + work.channel[0] * work.channel[1]) / 255;
29 return i_ppix(im, x, y, &src);
33 i_adapt_colors(2, 4, &work, 1);
34 i_gpix(im, x, y, &src);
35 remains = 255 - work.channel[1];
36 dest_alpha = work.channel[1] + remains * src.channel[1] / 255;
37 if (work.channel[1] == 255) {
38 return i_ppix(im, x, y, &work);
41 src.channel[0] = (work.channel[1] * work.channel[0]
42 + remains * src.channel[0] * src.channel[1] / 255) / dest_alpha;
43 src.channel[1] = dest_alpha;
44 return i_ppix(im, x, y, &src);
49 i_gpix(im, x, y, &src);
50 remains = 255 - work.channel[3];
51 src.channel[0] = (src.channel[0] * remains
52 + work.channel[0] * work.channel[3]) / 255;
53 src.channel[1] = (src.channel[1] * remains
54 + work.channel[1] * work.channel[3]) / 255;
55 src.channel[2] = (src.channel[2] * remains
56 + work.channel[2] * work.channel[3]) / 255;
57 return i_ppix(im, x, y, &src);
61 i_gpix(im, x, y, &src);
62 remains = 255 - work.channel[3];
63 dest_alpha = work.channel[3] + remains * src.channel[3] / 255;
64 if (work.channel[3] == 255) {
65 return i_ppix(im, x, y, &work);
68 src.channel[0] = (work.channel[3] * work.channel[0]
69 + remains * src.channel[0] * src.channel[3] / 255) / dest_alpha;
70 src.channel[1] = (work.channel[3] * work.channel[1]
71 + remains * src.channel[1] * src.channel[3] / 255) / dest_alpha;
72 src.channel[2] = (work.channel[3] * work.channel[2]
73 + remains * src.channel[2] * src.channel[3] / 255) / dest_alpha;
74 src.channel[3] = dest_alpha;
75 return i_ppix(im, x, y, &src);
82 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
83 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax);
86 i_mmarray_cr(i_mmarray *ar,i_img_dim l) {
91 alloc_size = sizeof(minmax) * l;
92 /* check for overflow */
93 if (alloc_size / l != sizeof(minmax)) {
94 fprintf(stderr, "overflow calculating memory allocation");
97 ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
100 ar->data[i].min = i_img_dim_MAX;
105 i_mmarray_dst(i_mmarray *ar) {
107 if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
111 i_mmarray_add(i_mmarray *ar,i_img_dim x,i_img_dim y) {
112 if (y>-1 && y<ar->lines)
114 if (x<ar->data[y].min) ar->data[y].min=x;
115 if (x>ar->data[y].max) ar->data[y].max=x;
120 i_mmarray_gmin(i_mmarray *ar,i_img_dim y) {
121 if (y>-1 && y<ar->lines) return ar->data[y].min;
126 i_mmarray_getm(i_mmarray *ar,i_img_dim y) {
127 if (y>-1 && y<ar->lines)
128 return ar->data[y].max;
130 return i_img_dim_MAX;
136 i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
138 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);
144 i_arcdraw(i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_mmarray *ar) {
148 alpha=(double)(y2-y1)/(double)(x2-x1);
149 if (fabs(alpha) <= 1)
151 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
155 i_mmarray_add(ar,x1,(i_img_dim)(dsec+0.5));
163 if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
167 i_mmarray_add(ar,(i_img_dim)(dsec+0.5),y1);
175 i_mmarray_info(i_mmarray *ar) {
177 for(i=0;i<ar->lines;i++)
178 if (ar->data[i].max!=-1)
179 printf("line %"i_DF ": min=%" i_DF ", max=%" i_DF ".\n",
180 i_DFc(i), i_DFc(ar->data[i].min), i_DFc(ar->data[i].max));
184 i_arc_minmax(i_int_hlines *hlines,i_img_dim x,i_img_dim y, double rad,float d1,float d2) {
189 i_mmarray_cr(&dot, hlines->limit_y);
191 x1=(i_img_dim)(x+0.5+rad*cos(d1*PI/180.0));
192 y1=(i_img_dim)(y+0.5+rad*sin(d1*PI/180.0));
194 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
195 i_arcdraw(x, y, x1, y1, &dot);
197 x1=(i_img_dim)(x+0.5+rad*cos(d2*PI/180.0));
198 y1=(i_img_dim)(y+0.5+rad*sin(d2*PI/180.0));
200 for(f=d1;f<=d2;f+=0.01)
201 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)));
203 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
204 i_arcdraw(x, y, x1, y1, &dot);
206 /* render the minmax values onto the hlines */
207 for (y = 0; y < dot.lines; y++) {
208 if (dot.data[y].max!=-1) {
209 i_img_dim minx, width;
210 minx = dot.data[y].min;
211 width = dot.data[y].max - dot.data[y].min + 1;
212 i_int_hlines_add(hlines, y, minx, width);
221 i_arc_hlines(i_int_hlines *hlines,i_img_dim x,i_img_dim y,double rad,float d1,float d2) {
223 i_arc_minmax(hlines, x, y, rad, d1, d2);
226 i_arc_minmax(hlines, x, y, rad, d1, 360);
227 i_arc_minmax(hlines, x, y, rad, 0, d2);
232 =item i_arc(im, x, y, rad, d1, d2, color)
235 =synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
237 Fills an arc centered at (x,y) with radius I<rad> covering the range
238 of angles in degrees from d1 to d2, with the color.
244 i_arc(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,const i_color *val) {
248 im_log((aIMCTX,1,"i_arc(im %p,(x,y)=(" i_DFp "), rad %f, d1 %f, d2 %f, col %p)",
249 im, i_DFcp(x, y), rad, d1, d2, val));
251 i_int_init_hlines_img(&hlines, im);
253 i_arc_hlines(&hlines, x, y, rad, d1, d2);
255 i_int_hlines_fill_color(im, &hlines, val);
257 i_int_hlines_destroy(&hlines);
261 =item i_arc_cfill(im, x, y, rad, d1, d2, fill)
264 =synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
266 Fills an arc centered at (x,y) with radius I<rad> covering the range
267 of angles in degrees from d1 to d2, with the fill object.
272 #define MIN_CIRCLE_STEPS 8
273 #define MAX_CIRCLE_STEPS 360
276 i_arc_cfill(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,i_fill_t *fill) {
280 im_log((aIMCTX,1,"i_arc_cfill(im %p,(x,y)=(" i_DFp "), rad %f, d1 %f, d2 %f, fill %p)",
281 im, i_DFcp(x, y), rad, d1, d2, fill));
283 i_int_init_hlines_img(&hlines, im);
285 i_arc_hlines(&hlines, x, y, rad, d1, d2);
287 i_int_hlines_fill_fill(im, &hlines, fill);
289 i_int_hlines_destroy(&hlines);
293 arc_poly(int *count, double **xvals, double **yvals,
294 double x, double y, double rad, double d1, double d2) {
295 double d1_rad, d2_rad;
297 i_img_dim steps, point_count;
300 /* normalize the angles */
303 if (d2 >= 360) { /* default is 361 */
317 d1_rad = d1 * PI / 180;
318 d2_rad = d2 * PI / 180;
320 /* how many segments for the curved part?
321 we do a maximum of one per degree, with a minimum of 8/circle
322 we try to aim at having about one segment per 2 pixels
323 Work it out per circle to get a step size.
325 I was originally making steps = circum/2 but that looked horrible.
327 I think there might be an issue in the polygon filler.
329 circum = 2 * PI * rad;
331 if (steps > MAX_CIRCLE_STEPS)
332 steps = MAX_CIRCLE_STEPS;
333 else if (steps < MIN_CIRCLE_STEPS)
334 steps = MIN_CIRCLE_STEPS;
336 angle_inc = 2 * PI / steps;
338 point_count = steps + 5; /* rough */
339 /* point_count is always relatively small, so allocation won't overflow */
340 *xvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
341 *yvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
343 /* from centre to edge at d1 */
346 (*xvals)[1] = x + rad * cos(d1_rad);
347 (*yvals)[1] = y + rad * sin(d1_rad);
350 /* step around the curve */
351 while (d1_rad < d2_rad) {
352 (*xvals)[*count] = x + rad * cos(d1_rad);
353 (*yvals)[*count] = y + rad * sin(d1_rad);
358 /* finish off the curve */
359 (*xvals)[*count] = x + rad * cos(d2_rad);
360 (*yvals)[*count] = y + rad * sin(d2_rad);
365 =item i_arc_aa(im, x, y, rad, d1, d2, color)
368 =synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
370 Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
371 the range of angles in degrees from d1 to d2, with the color.
377 i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
378 const i_color *val) {
379 double *xvals, *yvals;
383 im_log((aIMCTX,1,"i_arc_aa(im %p,(x,y)=(%f,%f), rad %f, d1 %f, d2 %f, col %p)",
384 im, x, y, rad, d1, d2, val));
386 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
388 i_poly_aa(im, count, xvals, yvals, val);
395 =item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
398 =synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
400 Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
401 the range of angles in degrees from d1 to d2, with the fill object.
407 i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
409 double *xvals, *yvals;
413 im_log((aIMCTX,1,"i_arc_aa_cfill(im %p,(x,y)=(%f,%f), rad %f, d1 %f, d2 %f, fill %p)",
414 im, x, y, rad, d1, d2, fill));
416 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
418 i_poly_aa_cfill(im, count, xvals, yvals, fill);
424 typedef i_img_dim frac;
425 static frac float_to_frac(double x) { return (frac)(0.5+x*16.0); }
428 =item i_circle_aa(im, x, y, rad, color)
431 =synopsis i_circle_aa(im, 50, 50, 45, &color);
433 Anti-alias fills a circle centered at (x,y) for radius I<rad> with
439 i_circle_aa(i_img *im, double x, double y, double rad, const i_color *val) {
443 i_img_dim first_row = floor(y) - ceil(rad);
444 i_img_dim last_row = ceil(y) + ceil(rad);
445 double r_sqr = rad * rad;
446 /*i_img_dim max_width = 2 * ceil(rad);
447 i_sample_t *coverage = NULL;
448 size_t coverage_size;*/
451 im_log((aIMCTX, 1, "i_circle_aa(im %p, centre(" i_DFp "), rad %.2f, val %p)\n",
452 im, i_DFcp(x, y), rad, val));
456 if (last_row > im->ysize-1)
457 last_row = im->ysize - 1;
459 if (rad <= 0 || last_row < first_row) {
460 /* outside the image */
464 /* coverage_size = sizeof(i_sample_t) * max_width;
465 coverage = mymalloc(coverage_size);*/
468 for(ly = first_row; ly < last_row; ly++) {
471 i_img_dim min_frac_left_x = im->xsize * 16;
472 i_img_dim max_frac_left_x = -1;
473 i_img_dim min_frac_right_x = im->xsize * 16;
474 i_img_dim max_frac_right_x = -1;
475 /* reset work_y each row so the error doesn't build up */
479 for (sub = 0; sub < 16; ++sub) {
480 work_y += 1.0 / 16.0;
484 if (dy_sqr < r_sqr) {
485 double dx = sqrt(r_sqr - dy_sqr);
486 double left_x = x - dx;
487 double right_x = x + dx;
488 frac frac_left_x = float_to_frac(left_x);
489 frac frac_right_x = float_to_frac(right_x);
491 if (frac_left_x < min_frac_left_x)
492 min_frac_left_x = frac_left_x;
493 if (frac_left_x > max_frac_left_x)
494 max_frac_left_x = frac_left_x;
495 if (frac_right_x < min_frac_right_x)
496 min_frac_right_x = frac_right_x;
497 if (frac_right_x > max_frac_right_x)
498 max_frac_right_x = frac_right_x;
499 min_frac_x[sub] = frac_left_x;
500 max_frac_x[sub] = frac_right_x;
503 min_frac_x[sub] = max_frac_x[sub] = 0;
504 max_frac_left_x = im->xsize * 16;
505 min_frac_right_x = -1;
509 if (min_frac_left_x != -1) {
510 /* something to draw on this line */
511 i_img_dim min_x = (min_frac_left_x / 16);
512 i_img_dim max_x = (max_frac_right_x + 15) / 16;
513 i_img_dim left_solid = (max_frac_left_x + 15) / 16;
514 i_img_dim right_solid = min_frac_right_x / 16;
516 i_img_dim frac_work_x;
518 for (work_x = min_x, frac_work_x = min_x * 16;
520 ++work_x, frac_work_x += 16) {
521 if (work_x <= left_solid || work_x >= right_solid) {
525 i_img_dim frac_work_right = frac_work_x + 16;
526 for (sub = 0; sub < 16; ++sub) {
527 frac pix_left = min_frac_x[sub];
528 frac pix_right = max_frac_x[sub];
529 if (pix_left < pix_right
530 && pix_left < frac_work_right
531 && pix_right >= frac_work_x) {
532 if (pix_left < frac_work_x)
533 pix_left = frac_work_x;
534 if (pix_right > frac_work_right)
535 pix_right = frac_work_right;
536 coverage += pix_right - pix_left;
540 assert(coverage <= 256);
541 ratio = coverage / 256.0;
542 i_gpix(im, work_x, ly, &temp);
543 for(ch=0;ch<im->channels; ch++)
544 temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
545 i_ppix(im, work_x, ly, &temp);
549 i_ppix(im, work_x, ly, val);
557 =item i_circle_out(im, x, y, r, col)
560 =synopsis i_circle_out(im, 50, 50, 45, &color);
562 Draw a circle outline centered at (x,y) with radius r,
571 (x, y) - the center of the circle
575 r - the radius of the circle in pixels, must be non-negative
579 Returns non-zero on success.
587 i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
588 const i_color *col) {
594 im_log((aIMCTX, 1, "i_circle_out(im %p, centre(" i_DFp "), rad %" i_DF ", col %p)\n",
595 im, i_DFcp(xc, yc), i_DFc(r), col));
597 im_clear_error(aIMCTX);
600 im_push_error(aIMCTX, 0, "circle: radius must be non-negative");
604 i_ppix(im, xc+r, yc, col);
605 i_ppix(im, xc-r, yc, col);
606 i_ppix(im, xc, yc+r, col);
607 i_ppix(im, xc, yc-r, col);
624 i_ppix(im, xc + x, yc + y, col);
625 i_ppix(im, xc + x, yc - y, col);
626 i_ppix(im, xc - x, yc + y, col);
627 i_ppix(im, xc - x, yc - y, col);
629 i_ppix(im, xc + y, yc + x, col);
630 i_ppix(im, xc + y, yc - x, col);
631 i_ppix(im, xc - y, yc + x, col);
632 i_ppix(im, xc - y, yc - x, col);
642 Convert an angle in degrees into an angle measure we can generate
643 simply from the numbers we have when drawing the circle.
649 arc_seg(double angle, int scale) {
650 i_img_dim seg = (angle + 45) / 90;
651 double remains = angle - seg * 90; /* should be in the range [-45,45] */
655 if (seg == 4 && remains > 0)
658 return scale * (seg * 2 + sin(remains * PI/180));
662 =item i_arc_out(im, x, y, r, d1, d2, col)
665 =synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
667 Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
668 over the angle range d1 through d2 degrees.
676 (x, y) - the center of the circle
680 r - the radius of the circle in pixels, must be non-negative
684 d1, d2 - the range of angles to draw the arc over, in degrees.
688 Returns non-zero on success.
696 i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
697 double d1, double d2, const i_color *col) {
701 i_img_dim segs[2][2];
704 i_img_dim seg_d1, seg_d2;
706 i_img_dim scale = r + 1;
707 i_img_dim seg1 = scale * 2;
708 i_img_dim seg2 = scale * 4;
709 i_img_dim seg3 = scale * 6;
710 i_img_dim seg4 = scale * 8;
713 im_log((aIMCTX,1,"i_arc_out(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
714 im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
716 im_clear_error(aIMCTX);
719 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
723 return i_circle_out(im, xc, yc, r, col);
726 d1 += 360 * floor((-d1 + 359) / 360);
728 d2 += 360 * floor((-d2 + 359) / 360);
731 seg_d1 = arc_seg(d1, scale);
732 seg_d2 = arc_seg(d2, scale);
733 if (seg_d2 < seg_d1) {
734 /* split into two segments */
747 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
748 i_img_dim seg_start = segs[seg_num][0];
749 i_img_dim seg_end = segs[seg_num][1];
751 i_ppix(im, xc+r, yc, col);
752 if (seg_start <= seg1 && seg_end >= seg1)
753 i_ppix(im, xc, yc+r, col);
754 if (seg_start <= seg2 && seg_end >= seg2)
755 i_ppix(im, xc-r, yc, col);
756 if (seg_start <= seg3 && seg_end >= seg3)
757 i_ppix(im, xc, yc-r, col);
775 if (seg_start <= sin_th && seg_end >= sin_th)
776 i_ppix(im, xc + x, yc + y, col);
777 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
778 i_ppix(im, xc + y, yc + x, col);
780 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
781 i_ppix(im, xc - y, yc + x, col);
782 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
783 i_ppix(im, xc - x, yc + y, col);
785 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
786 i_ppix(im, xc - x, yc - y, col);
787 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
788 i_ppix(im, xc - y, yc - x, col);
790 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
791 i_ppix(im, xc + y, yc - x, col);
792 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
793 i_ppix(im, xc + x, yc - y, col);
801 cover(i_img_dim r, i_img_dim j) {
802 double rjsqrt = sqrt(r*r - j*j);
804 return ceil(rjsqrt) - rjsqrt;
808 =item i_circle_out_aa(im, xc, yc, r, col)
810 =synopsis i_circle_out_aa(im, 50, 50, 45, &color);
812 Draw a circle outline centered at (x,y) with radius r, anti-aliased.
820 (xc, yc) - the center of the circle
824 r - the radius of the circle in pixels, must be non-negative
828 col - an i_color for the color to draw in.
832 Returns non-zero on success.
836 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
839 I use floating point for I<D> since for large circles the precision of
840 a [0,255] value isn't sufficient when approaching the end of the
846 i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
849 i_color workc = *col;
850 int orig_alpha = col->channel[3];
853 im_log((aIMCTX,1,"i_circle_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", col %p)",
854 im, i_DFcp(xc, yc), i_DFc(r), col));
856 im_clear_error(aIMCTX);
858 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
864 i_ppix_norm(im, xc+i, yc+j, col);
865 i_ppix_norm(im, xc-i, yc+j, col);
866 i_ppix_norm(im, xc+j, yc+i, col);
867 i_ppix_norm(im, xc+j, yc-i, col);
874 cv = (int)(d * 255 + 0.5);
880 workc.channel[3] = orig_alpha * inv_cv / 255;
881 i_ppix_norm(im, xc+i, yc+j, &workc);
882 i_ppix_norm(im, xc-i, yc+j, &workc);
883 i_ppix_norm(im, xc+i, yc-j, &workc);
884 i_ppix_norm(im, xc-i, yc-j, &workc);
887 i_ppix_norm(im, xc+j, yc+i, &workc);
888 i_ppix_norm(im, xc-j, yc+i, &workc);
889 i_ppix_norm(im, xc+j, yc-i, &workc);
890 i_ppix_norm(im, xc-j, yc-i, &workc);
894 workc.channel[3] = orig_alpha * cv / 255;
895 i_ppix_norm(im, xc+i-1, yc+j, &workc);
896 i_ppix_norm(im, xc-i+1, yc+j, &workc);
897 i_ppix_norm(im, xc+i-1, yc-j, &workc);
898 i_ppix_norm(im, xc-i+1, yc-j, &workc);
901 i_ppix_norm(im, xc+j, yc+i-1, &workc);
902 i_ppix_norm(im, xc-j, yc+i-1, &workc);
903 i_ppix_norm(im, xc+j, yc-i+1, &workc);
904 i_ppix_norm(im, xc-j, yc-i+1, &workc);
914 =item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
916 =synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
918 Draw a circle arc outline centered at (x,y) with radius r, from angle
919 d1 degrees through angle d2 degrees, anti-aliased.
927 (xc, yc) - the center of the circle
931 r - the radius of the circle in pixels, must be non-negative
935 d1, d2 - the range of angle in degrees to draw the arc through. If
936 d2-d1 >= 360 a full circle is drawn.
940 Returns non-zero on success.
944 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
950 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) {
953 i_color workc = *col;
954 i_img_dim segs[2][2];
957 i_img_dim seg_d1, seg_d2;
959 int orig_alpha = col->channel[3];
960 i_img_dim scale = r + 1;
961 i_img_dim seg1 = scale * 2;
962 i_img_dim seg2 = scale * 4;
963 i_img_dim seg3 = scale * 6;
964 i_img_dim seg4 = scale * 8;
967 im_log((aIMCTX,1,"i_arc_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
968 im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
970 im_clear_error(aIMCTX);
972 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
976 return i_circle_out_aa(im, xc, yc, r, col);
979 d1 += 360 * floor((-d1 + 359) / 360);
981 d2 += 360 * floor((-d2 + 359) / 360);
984 seg_d1 = arc_seg(d1, scale);
985 seg_d2 = arc_seg(d2, scale);
986 if (seg_d2 < seg_d1) {
987 /* split into two segments */
1000 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
1001 i_img_dim seg_start = segs[seg_num][0];
1002 i_img_dim seg_end = segs[seg_num][1];
1009 i_ppix_norm(im, xc+i, yc+j, col);
1010 if (seg_start <= seg1 && seg_end >= seg1)
1011 i_ppix_norm(im, xc+j, yc+i, col);
1012 if (seg_start <= seg2 && seg_end >= seg2)
1013 i_ppix_norm(im, xc-i, yc+j, col);
1014 if (seg_start <= seg3 && seg_end >= seg3)
1015 i_ppix_norm(im, xc+j, yc-i, col);
1022 cv = (int)(d * 255 + 0.5);
1029 workc.channel[3] = orig_alpha * inv_cv / 255;
1031 if (seg_start <= sin_th && seg_end >= sin_th)
1032 i_ppix_norm(im, xc+i, yc+j, &workc);
1033 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1034 i_ppix_norm(im, xc-i, yc+j, &workc);
1035 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1036 i_ppix_norm(im, xc+i, yc-j, &workc);
1037 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1038 i_ppix_norm(im, xc-i, yc-j, &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 <= seg1 + sin_th && seg_end >= seg1 + 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);
1047 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1048 i_ppix_norm(im, xc-j, yc-i, &workc);
1052 workc.channel[3] = orig_alpha * cv / 255;
1053 if (seg_start <= sin_th && seg_end >= sin_th)
1054 i_ppix_norm(im, xc+i-1, yc+j, &workc);
1055 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1056 i_ppix_norm(im, xc-i+1, yc+j, &workc);
1057 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1058 i_ppix_norm(im, xc+i-1, yc-j, &workc);
1059 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1060 i_ppix_norm(im, xc-i+1, yc-j, &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 <= seg1 + sin_th && seg_end >= seg1 + 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);
1068 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1069 i_ppix_norm(im, xc-j, yc-i+1, &workc);
1079 =item i_box(im, x1, y1, x2, y2, color)
1082 =synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
1084 Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
1090 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) {
1094 im_log((aIMCTX, 1,"i_box(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1095 im, i_DFcp(x1,y1), i_DFcp(x2,y2), val));
1096 for(x=x1;x<x2+1;x++) {
1097 i_ppix(im,x,y1,val);
1098 i_ppix(im,x,y2,val);
1100 for(y=y1;y<y2+1;y++) {
1101 i_ppix(im,x1,y,val);
1102 i_ppix(im,x2,y,val);
1107 =item i_box_filled(im, x1, y1, x2, y2, color)
1110 =synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
1112 Fills the box from (x1,y1) to (x2,y2) inclusive with color.
1118 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) {
1119 i_img_dim x, y, width;
1123 im_log((aIMCTX,1,"i_box_filled(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1124 im, i_DFcp(x1, y1), i_DFcp(x2,y2) ,val));
1126 if (x1 > x2 || y1 > y2
1128 || x1 >= im->xsize || y1 > im->ysize)
1133 if (x2 >= im->xsize)
1137 if (y2 >= im->ysize)
1140 width = x2 - x1 + 1;
1142 if (im->type == i_palette_type
1143 && i_findcolor(im, val, &index)) {
1144 i_palidx *line = mymalloc(sizeof(i_palidx) * width);
1146 for (x = 0; x < width; ++x)
1149 for (y = y1; y <= y2; ++y)
1150 i_ppal(im, x1, x2+1, y, line);
1155 i_color *line = mymalloc(sizeof(i_color) * width);
1157 for (x = 0; x < width; ++x)
1160 for (y = y1; y <= y2; ++y)
1161 i_plin(im, x1, x2+1, y, line);
1168 =item i_box_filledf(im, x1, y1, x2, y2, color)
1171 =synopsis i_box_filledf(im, 0, 0, im->xsize-1, im->ysize-1, &fcolor);
1173 Fills the box from (x1,y1) to (x2,y2) inclusive with a floating point
1180 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) {
1181 i_img_dim x, y, width;
1184 im_log((aIMCTX, 1,"i_box_filledf(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1185 im, i_DFcp(x1, y1), i_DFcp(x2, y2), val));
1187 if (x1 > x2 || y1 > y2
1189 || x1 >= im->xsize || y1 > im->ysize)
1194 if (x2 >= im->xsize)
1198 if (y2 >= im->ysize)
1201 width = x2 - x1 + 1;
1203 if (im->bits <= 8) {
1205 c.rgba.r = SampleFTo8(val->rgba.r);
1206 c.rgba.g = SampleFTo8(val->rgba.g);
1207 c.rgba.b = SampleFTo8(val->rgba.b);
1208 c.rgba.a = SampleFTo8(val->rgba.a);
1210 i_box_filled(im, x1, y1, x2, y2, &c);
1213 i_fcolor *line = mymalloc(sizeof(i_fcolor) * width);
1215 for (x = 0; x < width; ++x)
1218 for (y = y1; y <= y2; ++y)
1219 i_plinf(im, x1, x2+1, y, line);
1228 =item i_box_cfill(im, x1, y1, x2, y2, fill)
1231 =synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
1233 Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
1239 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) {
1243 im_log((aIMCTX,1,"i_box_cfill(im* %p, p1(" i_DFp "), p2(" i_DFp "), fill %p)\n",
1244 im, i_DFcp(x1, y1), i_DFcp(x2,y2), fill));
1253 if (y2 >= im->ysize)
1255 if (x1 >= x2 || y1 > y2)
1258 i_render_init(&r, im, x2-x1);
1260 i_render_fill(&r, x1, y1, x2-x1, NULL, fill);
1267 =item i_line(C<im>, C<x1>, C<y1>, C<x2>, C<y2>, C<color>, C<endp>)
1271 =for stopwords Bresenham's
1273 Draw a line to image using Bresenham's line drawing algorithm
1275 im - image to draw to
1276 x1 - starting x coordinate
1277 y1 - starting x coordinate
1278 x2 - starting x coordinate
1279 y2 - starting x coordinate
1280 color - color to write to image
1281 endp - endpoint flag (boolean)
1287 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) {
1296 /* choose variable to iterate on */
1297 if (i_abs(dx) > i_abs(dy)) {
1298 i_img_dim dx2, dy2, cpy;
1303 t = x1; x1 = x2; x2 = t;
1304 t = y1; y1 = y2; y2 = t;
1322 for(x=x1; x<x2-1; x++) {
1329 i_ppix(im, x+1, y, val);
1332 i_img_dim dy2, dx2, cpx;
1337 t = x1; x1 = x2; x2 = t;
1338 t = y1; y1 = y2; y2 = t;
1356 for(y=y1; y<y2-1; y++) {
1363 i_ppix(im, x, y+1, val);
1367 i_ppix(im, x1, y1, val);
1368 i_ppix(im, x2, y2, val);
1370 if (x1 != x2 || y1 != y2)
1371 i_ppix(im, x1, y1, val);
1377 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) {
1382 for(x=x1; x<=x2; x++) {
1383 dy = y1+ (x-x1)/(double)(x2-x1)*(y2-y1);
1384 i_ppix(im, x, (i_img_dim)(dy+0.5), val);
1389 =item i_line_aa(C<im>, C<x1>, C<x2>, C<y1>, C<y2>, C<color>, C<endp>)
1393 Anti-alias draws a line from (x1,y1) to (x2, y2) in color.
1395 The point (x2, y2) is drawn only if C<endp> is set.
1401 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) {
1409 /* choose variable to iterate on */
1410 if (i_abs(dx) > i_abs(dy)) {
1411 i_img_dim dx2, dy2, cpy;
1416 t = x1; x1 = x2; x2 = t;
1417 t = y1; y1 = y2; y2 = t;
1431 p = dy2 - dx2; /* this has to be like this for AA */
1435 for(x=x1; x<x2-1; x++) {
1438 double t = (dy) ? -(float)(p)/(float)(dx2) : 1;
1445 i_gpix(im,x+1,y,&tval);
1446 for(ch=0;ch<im->channels;ch++)
1447 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
1448 i_ppix(im,x+1,y,&tval);
1450 i_gpix(im,x+1,y+cpy,&tval);
1451 for(ch=0;ch<im->channels;ch++)
1452 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
1453 i_ppix(im,x+1,y+cpy,&tval);
1463 i_img_dim dy2, dx2, cpx;
1468 t = x1; x1 = x2; x2 = t;
1469 t = y1; y1 = y2; y2 = t;
1483 p = dx2 - dy2; /* this has to be like this for AA */
1487 for(y=y1; y<y2-1; y++) {
1490 double t = (dx) ? -(double)(p)/(double)(dy2) : 1;
1497 i_gpix(im,x,y+1,&tval);
1498 for(ch=0;ch<im->channels;ch++)
1499 tval.channel[ch]=(unsigned char)(t1*(double)tval.channel[ch]+t2*(double)val->channel[ch]);
1500 i_ppix(im,x,y+1,&tval);
1502 i_gpix(im,x+cpx,y+1,&tval);
1503 for(ch=0;ch<im->channels;ch++)
1504 tval.channel[ch]=(unsigned char)(t2*(double)tval.channel[ch]+t1*(double)val->channel[ch]);
1505 i_ppix(im,x+cpx,y+1,&tval);
1518 i_ppix(im, x1, y1, val);
1519 i_ppix(im, x2, y2, val);
1521 if (x1 != x2 || y1 != y2)
1522 i_ppix(im, x1, y1, val);
1529 perm(i_img_dim n,i_img_dim k) {
1533 for(i=k+1;i<=n;i++) r*=i;
1534 for(i=1;i<=(n-k);i++) r/=i;
1539 /* Note in calculating t^k*(1-t)^(n-k)
1540 we can start by using t^0=1 so this simplifies to
1541 t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
1542 to get a new level - this may lead to errors who knows lets test it */
1545 i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
1549 i_img_dim lx = 0,ly = 0;
1553 /* this is the same size as the x and y arrays, so shouldn't overflow */
1554 bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
1555 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1559 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1561 for(t=0;t<=1;t+=0.005) {
1566 /* cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k);
1567 cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1569 cx+=bzcoef[k]*x[k]*ccoef;
1570 cy+=bzcoef[k]*y[k]*ccoef;
1573 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1575 i_line_aa(im,lx,ly,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val, 1);
1577 /* i_ppix(im,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val); */
1578 lx=(i_img_dim)(0.5+cx);
1579 ly=(i_img_dim)(0.5+cy);
1587 REF: Graphics Gems I. page 282+
1591 /* This should be moved into a seperate file? */
1593 /* This is the truncation used:
1595 a double is multiplied by 16 and then truncated.
1596 This means that 0 -> 0
1597 So a triangle of (0,0) (10,10) (10,0) Will look like it's
1598 not filling the (10,10) point nor the (10,0)-(10,10) line segment
1603 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
1608 i_img_dim mylx,myrx;
1609 i_img_dim dadlx,dadrx;
1618 struct stack_element {
1619 i_img_dim myLx,myRx;
1620 i_img_dim dadLx,dadRx;
1626 /* create the link data to put push onto the stack */
1629 struct stack_element*
1630 crdata(i_img_dim left,i_img_dim right,i_img_dim dadl,i_img_dim dadr,i_img_dim y, int dir) {
1631 struct stack_element *ste;
1632 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
1638 ste->myDirection = dir;
1642 /* i_ccomp compares two colors and gives true if they are the same */
1644 typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1647 i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
1649 for(i = 0; i < ch; i++)
1650 if (val1->channel[i] !=val2->channel[i])
1656 i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
1658 for(i = 0; i < ch; i++)
1659 if (val1->channel[i] !=val2->channel[i])
1665 i_lspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1668 if (seedx-1 < 0) break;
1669 i_gpix(im,seedx-1,seedy,&cval);
1670 if (!cmpfunc(val,&cval,im->channels))
1678 i_rspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1681 if (seedx+1 > im->xsize-1) break;
1682 i_gpix(im,seedx+1,seedy,&cval);
1683 if (!cmpfunc(val,&cval,im->channels)) break;
1689 /* Macro to create a link and push on to the list */
1691 #define ST_PUSH(left,right,dadl,dadr,y,dir) do { \
1692 struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); \
1693 llist_push(st,&s); \
1696 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1697 /* No overflow check! */
1699 #define ST_POP() do { \
1700 struct stack_element *s; \
1707 direction = s->myDirection; \
1711 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
1712 i_img_dim pushrx = rx+1; \
1713 i_img_dim pushlx = lx-1; \
1714 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
1716 ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); \
1717 if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); \
1720 #define SET(x,y) btm_set(btm,x,y)
1722 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1723 #define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels) ) ))
1727 /* The function that does all the real work */
1729 static struct i_bitmap *
1730 i_flood_fill_low(i_img *im,i_img_dim seedx,i_img_dim seedy,
1731 i_img_dim *bxminp, i_img_dim *bxmaxp, i_img_dim *byminp, i_img_dim *bymaxp,
1732 i_color const *seed, ff_cmpfunc cmpfunc) {
1736 i_img_dim bxmin = seedx;
1737 i_img_dim bxmax = seedx;
1738 i_img_dim bymin = seedy;
1739 i_img_dim bymax = seedy;
1742 struct i_bitmap *btm;
1745 i_img_dim xsize,ysize;
1748 channels = im->channels;
1752 btm = btm_new(xsize, ysize);
1753 st = llist_new(100, sizeof(struct stack_element*));
1755 /* Find the starting span and fill it */
1756 ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1757 rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
1758 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1762 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1763 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1766 /* Stack variables */
1768 i_img_dim dadLx,dadRx;
1775 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1778 if (y<0 || y>ysize-1) continue;
1779 if (bymin > y) bymin=y; /* in the worst case an extra line */
1780 if (bymax < y) bymax=y;
1784 if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
1787 while(lx >= 0 && INSIDE(lx, y, seed)) {
1793 if (bxmin > lx) bxmin = lx;
1794 while(x <= xsize-1) {
1795 /* printf("x=%d\n",x); */
1798 if (INSIDE(x, y, seed)) {
1799 /* case 1: was inside, am still inside */
1802 /* case 2: was inside, am no longer inside: just found the
1803 right edge of a span */
1804 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1806 if (bxmax < x) bxmax = x;
1810 if (x > rx) goto EXT;
1811 if (INSIDE(x, y, seed)) {
1813 /* case 3: Wasn't inside, am now: just found the start of a new run */
1817 /* case 4: Wasn't inside, still isn't */
1822 EXT: /* out of loop */
1824 /* hit an edge of the frame buffer while inside a run */
1825 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1826 if (bxmax < x) bxmax = x;
1841 =item i_flood_fill(C<im>, C<seedx>, C<seedy>, C<color>)
1844 =synopsis i_flood_fill(im, 50, 50, &color);
1846 Flood fills the 4-connected region starting from the point (C<seedx>,
1847 C<seedy>) with I<color>.
1849 Returns false if (C<seedx>, C<seedy>) are outside the image.
1855 i_flood_fill(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol) {
1856 i_img_dim bxmin, bxmax, bymin, bymax;
1857 struct i_bitmap *btm;
1862 im_log((aIMCTX, 1, "i_flood_fill(im %p, seed(" i_DFp "), col %p)",
1863 im, i_DFcp(seedx, seedy), dcol));
1865 im_clear_error(aIMCTX);
1866 if (seedx < 0 || seedx >= im->xsize ||
1867 seedy < 0 || seedy >= im->ysize) {
1868 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
1872 /* Get the reference color */
1873 i_gpix(im, seedx, seedy, &val);
1875 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1876 &val, i_ccomp_normal);
1878 for(y=bymin;y<=bymax;y++)
1879 for(x=bxmin;x<=bxmax;x++)
1880 if (btm_test(btm,x,y))
1881 i_ppix(im,x,y,dcol);
1887 =item i_flood_cfill(C<im>, C<seedx>, C<seedy>, C<fill>)
1890 =synopsis i_flood_cfill(im, 50, 50, fill);
1892 Flood fills the 4-connected region starting from the point (C<seedx>,
1893 C<seedy>) with C<fill>.
1895 Returns false if (C<seedx>, C<seedy>) are outside the image.
1901 i_flood_cfill(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill) {
1902 i_img_dim bxmin, bxmax, bymin, bymax;
1903 struct i_bitmap *btm;
1907 im_log((aIMCTX, 1, "i_flood_cfill(im %p, seed(" i_DFp "), fill %p)",
1908 im, i_DFcp(seedx, seedy), fill));
1910 im_clear_error(aIMCTX);
1912 if (seedx < 0 || seedx >= im->xsize ||
1913 seedy < 0 || seedy >= im->ysize) {
1914 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
1918 /* Get the reference color */
1919 i_gpix(im, seedx, seedy, &val);
1921 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1922 &val, i_ccomp_normal);
1924 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1931 =item i_flood_fill_border(C<im>, C<seedx>, C<seedy>, C<color>, C<border>)
1934 =synopsis i_flood_fill_border(im, 50, 50, &color, &border);
1936 Flood fills the 4-connected region starting from the point (C<seedx>,
1937 C<seedy>) with C<color>, fill stops when the fill reaches a pixels
1938 with color C<border>.
1940 Returns false if (C<seedx>, C<seedy>) are outside the image.
1946 i_flood_fill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol,
1947 const i_color *border) {
1948 i_img_dim bxmin, bxmax, bymin, bymax;
1949 struct i_bitmap *btm;
1953 im_log((aIMCTX, 1, "i_flood_cfill(im %p, seed(" i_DFp "), dcol %p, border %p)",
1954 im, i_DFcp(seedx, seedy), dcol, border));
1956 im_clear_error(aIMCTX);
1957 if (seedx < 0 || seedx >= im->xsize ||
1958 seedy < 0 || seedy >= im->ysize) {
1959 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
1963 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1964 border, i_ccomp_border);
1966 for(y=bymin;y<=bymax;y++)
1967 for(x=bxmin;x<=bxmax;x++)
1968 if (btm_test(btm,x,y))
1969 i_ppix(im,x,y,dcol);
1975 =item i_flood_cfill_border(C<im>, C<seedx>, C<seedy>, C<fill>, C<border>)
1978 =synopsis i_flood_cfill_border(im, 50, 50, fill, border);
1980 Flood fills the 4-connected region starting from the point (C<seedx>,
1981 C<seedy>) with C<fill>, the fill stops when it reaches pixels of color
1984 Returns false if (C<seedx>, C<seedy>) are outside the image.
1990 i_flood_cfill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill,
1991 const i_color *border) {
1992 i_img_dim bxmin, bxmax, bymin, bymax;
1993 struct i_bitmap *btm;
1996 im_log((aIMCTX, 1, "i_flood_cfill_border(im %p, seed(" i_DFp "), fill %p, border %p)",
1997 im, i_DFcp(seedx, seedy), fill, border));
1999 im_clear_error(aIMCTX);
2001 if (seedx < 0 || seedx >= im->xsize ||
2002 seedy < 0 || seedy >= im->ysize) {
2003 im_push_error(aIMCTX, 0, "i_flood_cfill_border: Seed pixel outside of image");
2007 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
2008 border, i_ccomp_border);
2010 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
2018 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
2019 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax) {
2025 i_render_init(&r, im, bxmax - bxmin + 1);
2027 for(y=bymin; y<=bymax; y++) {
2029 while (x <= bxmax) {
2030 while (x <= bxmax && !btm_test(btm, x, y)) {
2033 if (btm_test(btm, x, y)) {
2035 while (x <= bxmax && btm_test(btm, x, y)) {
2038 i_render_fill(&r, start, y, x-start, NULL, fill);