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 (*flush_render_t)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_sample_t *cover, void *ctx);
431 i_circle_aa_low(i_img *im, double x, double y, double rad, flush_render_t r, void *ctx);
434 scanline_flush_color(i_img *im, i_img_dim l, i_img_dim y, i_img_dim width, const i_sample_t *cover, void *ctx);
437 scanline_flush_fill(i_img *im, i_img_dim l, i_img_dim y, i_img_dim width, const i_sample_t *cover, void *ctx);
450 =item i_circle_aa(im, x, y, rad, color)
453 =synopsis i_circle_aa(im, 50, 50, 45, &color);
455 Anti-alias fills a circle centered at (x,y) for radius I<rad> with
462 i_circle_aa(i_img *im, double x, double y, double rad, const i_color *val) {
466 i_render_init(&fc.r, im, rad * 2 + 1);
468 i_circle_aa_low(im, x, y, rad, scanline_flush_color, &fc);
470 i_render_done(&fc.r);
474 =item i_circle_aa_fill(im, x, y, rad, fill)
477 =synopsis i_circle_aa_fill(im, 50, 50, 45, fill);
479 Anti-alias fills a circle centered at (x,y) for radius I<rad> with
486 i_circle_aa_fill(i_img *im, double x, double y, double rad, i_fill_t *fill) {
490 i_render_init(&ff.r, im, rad * 2 + 1);
492 i_circle_aa_low(im, x, y, rad, scanline_flush_fill, &ff);
494 i_render_done(&ff.r);
498 i_circle_aa_low(i_img *im, double x, double y, double rad, flush_render_t r,
503 i_img_dim first_row = floor(y) - ceil(rad);
504 i_img_dim last_row = ceil(y) + ceil(rad);
505 double r_sqr = rad * rad;
506 i_img_dim max_width = 2 * ceil(rad) + 1;
507 unsigned char *coverage = NULL;
508 size_t coverage_size;
511 im_log((aIMCTX, 1, "i_circle_aa_low(im %p, centre(" i_DFp "), rad %.2f, r %p, ctx %p)\n",
512 im, i_DFcp(x, y), rad, r, ctx));
516 if (last_row > im->ysize-1)
517 last_row = im->ysize - 1;
519 if (rad <= 0 || last_row < first_row) {
520 /* outside the image */
524 coverage_size = max_width;
525 coverage = mymalloc(coverage_size);
527 for(ly = first_row; ly < last_row; ly++) {
530 i_img_dim min_frac_left_x = im->xsize * 16;
531 i_img_dim max_frac_left_x = -1;
532 i_img_dim min_frac_right_x = im->xsize * 16;
533 i_img_dim max_frac_right_x = -1;
534 /* reset work_y each row so the error doesn't build up */
538 for (sub = 0; sub < 16; ++sub) {
539 work_y += 1.0 / 16.0;
543 if (dy_sqr < r_sqr) {
544 double dx = sqrt(r_sqr - dy_sqr);
545 double left_x = x - dx;
546 double right_x = x + dx;
547 frac frac_left_x = float_to_frac(left_x);
548 frac frac_right_x = float_to_frac(right_x);
550 if (frac_left_x < min_frac_left_x)
551 min_frac_left_x = frac_left_x;
552 if (frac_left_x > max_frac_left_x)
553 max_frac_left_x = frac_left_x;
554 if (frac_right_x < min_frac_right_x)
555 min_frac_right_x = frac_right_x;
556 if (frac_right_x > max_frac_right_x)
557 max_frac_right_x = frac_right_x;
558 min_frac_x[sub] = frac_left_x;
559 max_frac_x[sub] = frac_right_x;
562 min_frac_x[sub] = max_frac_x[sub] = 0;
563 max_frac_left_x = im->xsize * 16;
564 min_frac_right_x = -1;
568 if (min_frac_left_x != -1) {
569 /* something to draw on this line */
570 i_img_dim min_x = (min_frac_left_x / 16);
571 i_img_dim max_x = (max_frac_right_x + 15) / 16;
572 i_img_dim left_solid = (max_frac_left_x + 15) / 16;
573 i_img_dim right_solid = min_frac_right_x / 16;
575 i_img_dim frac_work_x;
576 i_sample_t *cout = coverage;
578 for (work_x = min_x, frac_work_x = min_x * 16;
580 ++work_x, frac_work_x += 16) {
581 if (work_x <= left_solid || work_x >= right_solid) {
582 int pix_coverage = 0;
585 i_img_dim frac_work_right = frac_work_x + 16;
586 for (sub = 0; sub < 16; ++sub) {
587 frac pix_left = min_frac_x[sub];
588 frac pix_right = max_frac_x[sub];
589 if (pix_left < pix_right
590 && pix_left < frac_work_right
591 && pix_right >= frac_work_x) {
592 if (pix_left < frac_work_x)
593 pix_left = frac_work_x;
594 if (pix_right > frac_work_right)
595 pix_right = frac_work_right;
596 pix_coverage += pix_right - pix_left;
600 assert(pix_coverage <= 256);
601 *cout++ = pix_coverage * 255 / 256;
608 r(im, min_x, ly, max_x - min_x + 1, coverage, ctx);
616 scanline_flush_color(i_img *im, i_img_dim x, i_img_dim y, i_img_dim width, const unsigned char *cover, void *ctx) {
617 flush_color_t *fc = ctx;
619 i_render_color(&fc->r, x, y, width, cover, &fc->c);
623 scanline_flush_fill(i_img *im, i_img_dim x, i_img_dim y, i_img_dim width, const unsigned char *cover, void *ctx) {
624 flush_fill_t *ff = ctx;
626 i_render_fill(&ff->r, x, y, width, cover, ff->fill);
631 =item i_circle_out(im, x, y, r, col)
634 =synopsis i_circle_out(im, 50, 50, 45, &color);
636 Draw a circle outline centered at (x,y) with radius r,
645 (x, y) - the center of the circle
649 r - the radius of the circle in pixels, must be non-negative
653 Returns non-zero on success.
661 i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
662 const i_color *col) {
668 im_log((aIMCTX, 1, "i_circle_out(im %p, centre(" i_DFp "), rad %" i_DF ", col %p)\n",
669 im, i_DFcp(xc, yc), i_DFc(r), col));
671 im_clear_error(aIMCTX);
674 im_push_error(aIMCTX, 0, "circle: radius must be non-negative");
678 i_ppix(im, xc+r, yc, col);
679 i_ppix(im, xc-r, yc, col);
680 i_ppix(im, xc, yc+r, col);
681 i_ppix(im, xc, yc-r, col);
698 i_ppix(im, xc + x, yc + y, col);
699 i_ppix(im, xc + x, yc - y, col);
700 i_ppix(im, xc - x, yc + y, col);
701 i_ppix(im, xc - x, yc - y, col);
703 i_ppix(im, xc + y, yc + x, col);
704 i_ppix(im, xc + y, yc - x, col);
705 i_ppix(im, xc - y, yc + x, col);
706 i_ppix(im, xc - y, yc - x, col);
716 Convert an angle in degrees into an angle measure we can generate
717 simply from the numbers we have when drawing the circle.
723 arc_seg(double angle, int scale) {
724 i_img_dim seg = (angle + 45) / 90;
725 double remains = angle - seg * 90; /* should be in the range [-45,45] */
729 if (seg == 4 && remains > 0)
732 return scale * (seg * 2 + sin(remains * PI/180));
736 =item i_arc_out(im, x, y, r, d1, d2, col)
739 =synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
741 Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
742 over the angle range d1 through d2 degrees.
750 (x, y) - the center of the circle
754 r - the radius of the circle in pixels, must be non-negative
758 d1, d2 - the range of angles to draw the arc over, in degrees.
762 Returns non-zero on success.
770 i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
771 double d1, double d2, const i_color *col) {
775 i_img_dim segs[2][2];
778 i_img_dim seg_d1, seg_d2;
780 i_img_dim scale = r + 1;
781 i_img_dim seg1 = scale * 2;
782 i_img_dim seg2 = scale * 4;
783 i_img_dim seg3 = scale * 6;
784 i_img_dim seg4 = scale * 8;
787 im_log((aIMCTX,1,"i_arc_out(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
788 im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
790 im_clear_error(aIMCTX);
793 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
797 return i_circle_out(im, xc, yc, r, col);
800 d1 += 360 * floor((-d1 + 359) / 360);
802 d2 += 360 * floor((-d2 + 359) / 360);
805 seg_d1 = arc_seg(d1, scale);
806 seg_d2 = arc_seg(d2, scale);
807 if (seg_d2 < seg_d1) {
808 /* split into two segments */
821 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
822 i_img_dim seg_start = segs[seg_num][0];
823 i_img_dim seg_end = segs[seg_num][1];
825 i_ppix(im, xc+r, yc, col);
826 if (seg_start <= seg1 && seg_end >= seg1)
827 i_ppix(im, xc, yc+r, col);
828 if (seg_start <= seg2 && seg_end >= seg2)
829 i_ppix(im, xc-r, yc, col);
830 if (seg_start <= seg3 && seg_end >= seg3)
831 i_ppix(im, xc, yc-r, col);
849 if (seg_start <= sin_th && seg_end >= sin_th)
850 i_ppix(im, xc + x, yc + y, col);
851 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
852 i_ppix(im, xc + y, yc + x, col);
854 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
855 i_ppix(im, xc - y, yc + x, col);
856 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
857 i_ppix(im, xc - x, yc + y, col);
859 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
860 i_ppix(im, xc - x, yc - y, col);
861 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
862 i_ppix(im, xc - y, yc - x, col);
864 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
865 i_ppix(im, xc + y, yc - x, col);
866 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
867 i_ppix(im, xc + x, yc - y, col);
875 cover(i_img_dim r, i_img_dim j) {
876 double rjsqrt = sqrt(r*r - j*j);
878 return ceil(rjsqrt) - rjsqrt;
882 =item i_circle_out_aa(im, xc, yc, r, col)
884 =synopsis i_circle_out_aa(im, 50, 50, 45, &color);
886 Draw a circle outline centered at (x,y) with radius r, anti-aliased.
894 (xc, yc) - the center of the circle
898 r - the radius of the circle in pixels, must be non-negative
902 col - an i_color for the color to draw in.
906 Returns non-zero on success.
910 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
913 I use floating point for I<D> since for large circles the precision of
914 a [0,255] value isn't sufficient when approaching the end of the
920 i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
923 i_color workc = *col;
924 int orig_alpha = col->channel[3];
927 im_log((aIMCTX,1,"i_circle_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", col %p)",
928 im, i_DFcp(xc, yc), i_DFc(r), col));
930 im_clear_error(aIMCTX);
932 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
938 i_ppix_norm(im, xc+i, yc+j, col);
939 i_ppix_norm(im, xc-i, yc+j, col);
940 i_ppix_norm(im, xc+j, yc+i, col);
941 i_ppix_norm(im, xc+j, yc-i, col);
948 cv = (int)(d * 255 + 0.5);
954 workc.channel[3] = orig_alpha * inv_cv / 255;
955 i_ppix_norm(im, xc+i, yc+j, &workc);
956 i_ppix_norm(im, xc-i, yc+j, &workc);
957 i_ppix_norm(im, xc+i, yc-j, &workc);
958 i_ppix_norm(im, xc-i, yc-j, &workc);
961 i_ppix_norm(im, xc+j, yc+i, &workc);
962 i_ppix_norm(im, xc-j, yc+i, &workc);
963 i_ppix_norm(im, xc+j, yc-i, &workc);
964 i_ppix_norm(im, xc-j, yc-i, &workc);
968 workc.channel[3] = orig_alpha * cv / 255;
969 i_ppix_norm(im, xc+i-1, yc+j, &workc);
970 i_ppix_norm(im, xc-i+1, yc+j, &workc);
971 i_ppix_norm(im, xc+i-1, yc-j, &workc);
972 i_ppix_norm(im, xc-i+1, yc-j, &workc);
975 i_ppix_norm(im, xc+j, yc+i-1, &workc);
976 i_ppix_norm(im, xc-j, yc+i-1, &workc);
977 i_ppix_norm(im, xc+j, yc-i+1, &workc);
978 i_ppix_norm(im, xc-j, yc-i+1, &workc);
988 =item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
990 =synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
992 Draw a circle arc outline centered at (x,y) with radius r, from angle
993 d1 degrees through angle d2 degrees, anti-aliased.
1001 (xc, yc) - the center of the circle
1005 r - the radius of the circle in pixels, must be non-negative
1009 d1, d2 - the range of angle in degrees to draw the arc through. If
1010 d2-d1 >= 360 a full circle is drawn.
1014 Returns non-zero on success.
1018 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
1024 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) {
1027 i_color workc = *col;
1028 i_img_dim segs[2][2];
1031 i_img_dim seg_d1, seg_d2;
1033 int orig_alpha = col->channel[3];
1034 i_img_dim scale = r + 1;
1035 i_img_dim seg1 = scale * 2;
1036 i_img_dim seg2 = scale * 4;
1037 i_img_dim seg3 = scale * 6;
1038 i_img_dim seg4 = scale * 8;
1041 im_log((aIMCTX,1,"i_arc_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
1042 im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
1044 im_clear_error(aIMCTX);
1046 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
1050 return i_circle_out_aa(im, xc, yc, r, col);
1053 d1 += 360 * floor((-d1 + 359) / 360);
1055 d2 += 360 * floor((-d2 + 359) / 360);
1058 seg_d1 = arc_seg(d1, scale);
1059 seg_d2 = arc_seg(d2, scale);
1060 if (seg_d2 < seg_d1) {
1061 /* split into two segments */
1063 segs[0][1] = seg_d2;
1064 segs[1][0] = seg_d1;
1069 segs[0][0] = seg_d1;
1070 segs[0][1] = seg_d2;
1074 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
1075 i_img_dim seg_start = segs[seg_num][0];
1076 i_img_dim seg_end = segs[seg_num][1];
1083 i_ppix_norm(im, xc+i, yc+j, col);
1084 if (seg_start <= seg1 && seg_end >= seg1)
1085 i_ppix_norm(im, xc+j, yc+i, col);
1086 if (seg_start <= seg2 && seg_end >= seg2)
1087 i_ppix_norm(im, xc-i, yc+j, col);
1088 if (seg_start <= seg3 && seg_end >= seg3)
1089 i_ppix_norm(im, xc+j, yc-i, col);
1096 cv = (int)(d * 255 + 0.5);
1103 workc.channel[3] = orig_alpha * inv_cv / 255;
1105 if (seg_start <= sin_th && seg_end >= sin_th)
1106 i_ppix_norm(im, xc+i, yc+j, &workc);
1107 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1108 i_ppix_norm(im, xc-i, yc+j, &workc);
1109 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1110 i_ppix_norm(im, xc+i, yc-j, &workc);
1111 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1112 i_ppix_norm(im, xc-i, yc-j, &workc);
1115 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1116 i_ppix_norm(im, xc+j, yc+i, &workc);
1117 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1118 i_ppix_norm(im, xc-j, yc+i, &workc);
1119 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1120 i_ppix_norm(im, xc+j, yc-i, &workc);
1121 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1122 i_ppix_norm(im, xc-j, yc-i, &workc);
1126 workc.channel[3] = orig_alpha * cv / 255;
1127 if (seg_start <= sin_th && seg_end >= sin_th)
1128 i_ppix_norm(im, xc+i-1, yc+j, &workc);
1129 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1130 i_ppix_norm(im, xc-i+1, yc+j, &workc);
1131 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1132 i_ppix_norm(im, xc+i-1, yc-j, &workc);
1133 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1134 i_ppix_norm(im, xc-i+1, yc-j, &workc);
1136 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1137 i_ppix_norm(im, xc+j, yc+i-1, &workc);
1138 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1139 i_ppix_norm(im, xc-j, yc+i-1, &workc);
1140 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1141 i_ppix_norm(im, xc+j, yc-i+1, &workc);
1142 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1143 i_ppix_norm(im, xc-j, yc-i+1, &workc);
1153 =item i_box(im, x1, y1, x2, y2, color)
1156 =synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
1158 Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
1164 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) {
1168 im_log((aIMCTX, 1,"i_box(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1169 im, i_DFcp(x1,y1), i_DFcp(x2,y2), val));
1170 for(x=x1;x<x2+1;x++) {
1171 i_ppix(im,x,y1,val);
1172 i_ppix(im,x,y2,val);
1174 for(y=y1;y<y2+1;y++) {
1175 i_ppix(im,x1,y,val);
1176 i_ppix(im,x2,y,val);
1181 =item i_box_filled(im, x1, y1, x2, y2, color)
1184 =synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
1186 Fills the box from (x1,y1) to (x2,y2) inclusive with color.
1192 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) {
1193 i_img_dim x, y, width;
1197 im_log((aIMCTX,1,"i_box_filled(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1198 im, i_DFcp(x1, y1), i_DFcp(x2,y2) ,val));
1200 if (x1 > x2 || y1 > y2
1202 || x1 >= im->xsize || y1 > im->ysize)
1207 if (x2 >= im->xsize)
1211 if (y2 >= im->ysize)
1214 width = x2 - x1 + 1;
1216 if (im->type == i_palette_type
1217 && i_findcolor(im, val, &index)) {
1218 i_palidx *line = mymalloc(sizeof(i_palidx) * width);
1220 for (x = 0; x < width; ++x)
1223 for (y = y1; y <= y2; ++y)
1224 i_ppal(im, x1, x2+1, y, line);
1229 i_color *line = mymalloc(sizeof(i_color) * width);
1231 for (x = 0; x < width; ++x)
1234 for (y = y1; y <= y2; ++y)
1235 i_plin(im, x1, x2+1, y, line);
1242 =item i_box_filledf(im, x1, y1, x2, y2, color)
1245 =synopsis i_box_filledf(im, 0, 0, im->xsize-1, im->ysize-1, &fcolor);
1247 Fills the box from (x1,y1) to (x2,y2) inclusive with a floating point
1254 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) {
1255 i_img_dim x, y, width;
1258 im_log((aIMCTX, 1,"i_box_filledf(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1259 im, i_DFcp(x1, y1), i_DFcp(x2, y2), val));
1261 if (x1 > x2 || y1 > y2
1263 || x1 >= im->xsize || y1 > im->ysize)
1268 if (x2 >= im->xsize)
1272 if (y2 >= im->ysize)
1275 width = x2 - x1 + 1;
1277 if (im->bits <= 8) {
1279 c.rgba.r = SampleFTo8(val->rgba.r);
1280 c.rgba.g = SampleFTo8(val->rgba.g);
1281 c.rgba.b = SampleFTo8(val->rgba.b);
1282 c.rgba.a = SampleFTo8(val->rgba.a);
1284 i_box_filled(im, x1, y1, x2, y2, &c);
1287 i_fcolor *line = mymalloc(sizeof(i_fcolor) * width);
1289 for (x = 0; x < width; ++x)
1292 for (y = y1; y <= y2; ++y)
1293 i_plinf(im, x1, x2+1, y, line);
1302 =item i_box_cfill(im, x1, y1, x2, y2, fill)
1305 =synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
1307 Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
1313 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) {
1317 im_log((aIMCTX,1,"i_box_cfill(im* %p, p1(" i_DFp "), p2(" i_DFp "), fill %p)\n",
1318 im, i_DFcp(x1, y1), i_DFcp(x2,y2), fill));
1327 if (y2 >= im->ysize)
1329 if (x1 >= x2 || y1 > y2)
1332 i_render_init(&r, im, x2-x1);
1334 i_render_fill(&r, x1, y1, x2-x1, NULL, fill);
1341 =item i_line(C<im>, C<x1>, C<y1>, C<x2>, C<y2>, C<color>, C<endp>)
1345 =for stopwords Bresenham's
1347 Draw a line to image using Bresenham's line drawing algorithm
1349 im - image to draw to
1350 x1 - starting x coordinate
1351 y1 - starting x coordinate
1352 x2 - starting x coordinate
1353 y2 - starting x coordinate
1354 color - color to write to image
1355 endp - endpoint flag (boolean)
1361 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) {
1370 /* choose variable to iterate on */
1371 if (i_abs(dx) > i_abs(dy)) {
1372 i_img_dim dx2, dy2, cpy;
1377 t = x1; x1 = x2; x2 = t;
1378 t = y1; y1 = y2; y2 = t;
1396 for(x=x1; x<x2-1; x++) {
1403 i_ppix(im, x+1, y, val);
1406 i_img_dim dy2, dx2, cpx;
1411 t = x1; x1 = x2; x2 = t;
1412 t = y1; y1 = y2; y2 = t;
1430 for(y=y1; y<y2-1; y++) {
1437 i_ppix(im, x, y+1, val);
1441 i_ppix(im, x1, y1, val);
1442 i_ppix(im, x2, y2, val);
1444 if (x1 != x2 || y1 != y2)
1445 i_ppix(im, x1, y1, val);
1451 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) {
1456 for(x=x1; x<=x2; x++) {
1457 dy = y1+ (x-x1)/(double)(x2-x1)*(y2-y1);
1458 i_ppix(im, x, (i_img_dim)(dy+0.5), val);
1463 =item i_line_aa(C<im>, C<x1>, C<x2>, C<y1>, C<y2>, C<color>, C<endp>)
1467 Anti-alias draws a line from (x1,y1) to (x2, y2) in color.
1469 The point (x2, y2) is drawn only if C<endp> is set.
1475 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) {
1483 /* choose variable to iterate on */
1484 if (i_abs(dx) > i_abs(dy)) {
1485 i_img_dim dx2, dy2, cpy;
1490 t = x1; x1 = x2; x2 = t;
1491 t = y1; y1 = y2; y2 = t;
1505 p = dy2 - dx2; /* this has to be like this for AA */
1509 for(x=x1; x<x2-1; x++) {
1512 double t = (dy) ? -(float)(p)/(float)(dx2) : 1;
1519 i_gpix(im,x+1,y,&tval);
1520 for(ch=0;ch<im->channels;ch++)
1521 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
1522 i_ppix(im,x+1,y,&tval);
1524 i_gpix(im,x+1,y+cpy,&tval);
1525 for(ch=0;ch<im->channels;ch++)
1526 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
1527 i_ppix(im,x+1,y+cpy,&tval);
1537 i_img_dim dy2, dx2, cpx;
1542 t = x1; x1 = x2; x2 = t;
1543 t = y1; y1 = y2; y2 = t;
1557 p = dx2 - dy2; /* this has to be like this for AA */
1561 for(y=y1; y<y2-1; y++) {
1564 double t = (dx) ? -(double)(p)/(double)(dy2) : 1;
1571 i_gpix(im,x,y+1,&tval);
1572 for(ch=0;ch<im->channels;ch++)
1573 tval.channel[ch]=(unsigned char)(t1*(double)tval.channel[ch]+t2*(double)val->channel[ch]);
1574 i_ppix(im,x,y+1,&tval);
1576 i_gpix(im,x+cpx,y+1,&tval);
1577 for(ch=0;ch<im->channels;ch++)
1578 tval.channel[ch]=(unsigned char)(t2*(double)tval.channel[ch]+t1*(double)val->channel[ch]);
1579 i_ppix(im,x+cpx,y+1,&tval);
1592 i_ppix(im, x1, y1, val);
1593 i_ppix(im, x2, y2, val);
1595 if (x1 != x2 || y1 != y2)
1596 i_ppix(im, x1, y1, val);
1603 perm(i_img_dim n,i_img_dim k) {
1607 for(i=k+1;i<=n;i++) r*=i;
1608 for(i=1;i<=(n-k);i++) r/=i;
1613 /* Note in calculating t^k*(1-t)^(n-k)
1614 we can start by using t^0=1 so this simplifies to
1615 t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
1616 to get a new level - this may lead to errors who knows lets test it */
1619 i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
1623 i_img_dim lx = 0,ly = 0;
1627 /* this is the same size as the x and y arrays, so shouldn't overflow */
1628 bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
1629 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1633 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1635 for(t=0;t<=1;t+=0.005) {
1640 /* cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k);
1641 cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1643 cx+=bzcoef[k]*x[k]*ccoef;
1644 cy+=bzcoef[k]*y[k]*ccoef;
1647 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1649 i_line_aa(im,lx,ly,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val, 1);
1651 /* i_ppix(im,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val); */
1652 lx=(i_img_dim)(0.5+cx);
1653 ly=(i_img_dim)(0.5+cy);
1661 REF: Graphics Gems I. page 282+
1665 /* This should be moved into a seperate file? */
1667 /* This is the truncation used:
1669 a double is multiplied by 16 and then truncated.
1670 This means that 0 -> 0
1671 So a triangle of (0,0) (10,10) (10,0) Will look like it's
1672 not filling the (10,10) point nor the (10,0)-(10,10) line segment
1677 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
1682 i_img_dim mylx,myrx;
1683 i_img_dim dadlx,dadrx;
1692 struct stack_element {
1693 i_img_dim myLx,myRx;
1694 i_img_dim dadLx,dadRx;
1700 /* create the link data to put push onto the stack */
1703 struct stack_element*
1704 crdata(i_img_dim left,i_img_dim right,i_img_dim dadl,i_img_dim dadr,i_img_dim y, int dir) {
1705 struct stack_element *ste;
1706 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
1712 ste->myDirection = dir;
1716 /* i_ccomp compares two colors and gives true if they are the same */
1718 typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1721 i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
1723 for(i = 0; i < ch; i++)
1724 if (val1->channel[i] !=val2->channel[i])
1730 i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
1732 for(i = 0; i < ch; i++)
1733 if (val1->channel[i] !=val2->channel[i])
1739 i_lspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1742 if (seedx-1 < 0) break;
1743 i_gpix(im,seedx-1,seedy,&cval);
1744 if (!cmpfunc(val,&cval,im->channels))
1752 i_rspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1755 if (seedx+1 > im->xsize-1) break;
1756 i_gpix(im,seedx+1,seedy,&cval);
1757 if (!cmpfunc(val,&cval,im->channels)) break;
1763 /* Macro to create a link and push on to the list */
1765 #define ST_PUSH(left,right,dadl,dadr,y,dir) do { \
1766 struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); \
1767 llist_push(st,&s); \
1770 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1771 /* No overflow check! */
1773 #define ST_POP() do { \
1774 struct stack_element *s; \
1781 direction = s->myDirection; \
1785 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
1786 i_img_dim pushrx = rx+1; \
1787 i_img_dim pushlx = lx-1; \
1788 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
1790 ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); \
1791 if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); \
1794 #define SET(x,y) btm_set(btm,x,y)
1796 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1797 #define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels) ) ))
1801 /* The function that does all the real work */
1803 static struct i_bitmap *
1804 i_flood_fill_low(i_img *im,i_img_dim seedx,i_img_dim seedy,
1805 i_img_dim *bxminp, i_img_dim *bxmaxp, i_img_dim *byminp, i_img_dim *bymaxp,
1806 i_color const *seed, ff_cmpfunc cmpfunc) {
1810 i_img_dim bxmin = seedx;
1811 i_img_dim bxmax = seedx;
1812 i_img_dim bymin = seedy;
1813 i_img_dim bymax = seedy;
1816 struct i_bitmap *btm;
1819 i_img_dim xsize,ysize;
1822 channels = im->channels;
1826 btm = btm_new(xsize, ysize);
1827 st = llist_new(100, sizeof(struct stack_element*));
1829 /* Find the starting span and fill it */
1830 ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1831 rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
1832 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1836 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1837 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1840 /* Stack variables */
1842 i_img_dim dadLx,dadRx;
1849 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1852 if (y<0 || y>ysize-1) continue;
1853 if (bymin > y) bymin=y; /* in the worst case an extra line */
1854 if (bymax < y) bymax=y;
1858 if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
1861 while(lx >= 0 && INSIDE(lx, y, seed)) {
1867 if (bxmin > lx) bxmin = lx;
1868 while(x <= xsize-1) {
1869 /* printf("x=%d\n",x); */
1872 if (INSIDE(x, y, seed)) {
1873 /* case 1: was inside, am still inside */
1876 /* case 2: was inside, am no longer inside: just found the
1877 right edge of a span */
1878 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1880 if (bxmax < x) bxmax = x;
1884 if (x > rx) goto EXT;
1885 if (INSIDE(x, y, seed)) {
1887 /* case 3: Wasn't inside, am now: just found the start of a new run */
1891 /* case 4: Wasn't inside, still isn't */
1896 EXT: /* out of loop */
1898 /* hit an edge of the frame buffer while inside a run */
1899 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1900 if (bxmax < x) bxmax = x;
1915 =item i_flood_fill(C<im>, C<seedx>, C<seedy>, C<color>)
1918 =synopsis i_flood_fill(im, 50, 50, &color);
1920 Flood fills the 4-connected region starting from the point (C<seedx>,
1921 C<seedy>) with I<color>.
1923 Returns false if (C<seedx>, C<seedy>) are outside the image.
1929 i_flood_fill(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol) {
1930 i_img_dim bxmin, bxmax, bymin, bymax;
1931 struct i_bitmap *btm;
1936 im_log((aIMCTX, 1, "i_flood_fill(im %p, seed(" i_DFp "), col %p)",
1937 im, i_DFcp(seedx, seedy), dcol));
1939 im_clear_error(aIMCTX);
1940 if (seedx < 0 || seedx >= im->xsize ||
1941 seedy < 0 || seedy >= im->ysize) {
1942 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
1946 /* Get the reference color */
1947 i_gpix(im, seedx, seedy, &val);
1949 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1950 &val, i_ccomp_normal);
1952 for(y=bymin;y<=bymax;y++)
1953 for(x=bxmin;x<=bxmax;x++)
1954 if (btm_test(btm,x,y))
1955 i_ppix(im,x,y,dcol);
1961 =item i_flood_cfill(C<im>, C<seedx>, C<seedy>, C<fill>)
1964 =synopsis i_flood_cfill(im, 50, 50, fill);
1966 Flood fills the 4-connected region starting from the point (C<seedx>,
1967 C<seedy>) with C<fill>.
1969 Returns false if (C<seedx>, C<seedy>) are outside the image.
1975 i_flood_cfill(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill) {
1976 i_img_dim bxmin, bxmax, bymin, bymax;
1977 struct i_bitmap *btm;
1981 im_log((aIMCTX, 1, "i_flood_cfill(im %p, seed(" i_DFp "), fill %p)",
1982 im, i_DFcp(seedx, seedy), fill));
1984 im_clear_error(aIMCTX);
1986 if (seedx < 0 || seedx >= im->xsize ||
1987 seedy < 0 || seedy >= im->ysize) {
1988 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
1992 /* Get the reference color */
1993 i_gpix(im, seedx, seedy, &val);
1995 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1996 &val, i_ccomp_normal);
1998 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
2005 =item i_flood_fill_border(C<im>, C<seedx>, C<seedy>, C<color>, C<border>)
2008 =synopsis i_flood_fill_border(im, 50, 50, &color, &border);
2010 Flood fills the 4-connected region starting from the point (C<seedx>,
2011 C<seedy>) with C<color>, fill stops when the fill reaches a pixels
2012 with color C<border>.
2014 Returns false if (C<seedx>, C<seedy>) are outside the image.
2020 i_flood_fill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol,
2021 const i_color *border) {
2022 i_img_dim bxmin, bxmax, bymin, bymax;
2023 struct i_bitmap *btm;
2027 im_log((aIMCTX, 1, "i_flood_cfill(im %p, seed(" i_DFp "), dcol %p, border %p)",
2028 im, i_DFcp(seedx, seedy), dcol, border));
2030 im_clear_error(aIMCTX);
2031 if (seedx < 0 || seedx >= im->xsize ||
2032 seedy < 0 || seedy >= im->ysize) {
2033 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
2037 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
2038 border, i_ccomp_border);
2040 for(y=bymin;y<=bymax;y++)
2041 for(x=bxmin;x<=bxmax;x++)
2042 if (btm_test(btm,x,y))
2043 i_ppix(im,x,y,dcol);
2049 =item i_flood_cfill_border(C<im>, C<seedx>, C<seedy>, C<fill>, C<border>)
2052 =synopsis i_flood_cfill_border(im, 50, 50, fill, border);
2054 Flood fills the 4-connected region starting from the point (C<seedx>,
2055 C<seedy>) with C<fill>, the fill stops when it reaches pixels of color
2058 Returns false if (C<seedx>, C<seedy>) are outside the image.
2064 i_flood_cfill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill,
2065 const i_color *border) {
2066 i_img_dim bxmin, bxmax, bymin, bymax;
2067 struct i_bitmap *btm;
2070 im_log((aIMCTX, 1, "i_flood_cfill_border(im %p, seed(" i_DFp "), fill %p, border %p)",
2071 im, i_DFcp(seedx, seedy), fill, border));
2073 im_clear_error(aIMCTX);
2075 if (seedx < 0 || seedx >= im->xsize ||
2076 seedy < 0 || seedy >= im->ysize) {
2077 im_push_error(aIMCTX, 0, "i_flood_cfill_border: Seed pixel outside of image");
2081 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
2082 border, i_ccomp_border);
2084 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
2092 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
2093 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax) {
2099 i_render_init(&r, im, bxmax - bxmin + 1);
2101 for(y=bymin; y<=bymax; y++) {
2103 while (x <= bxmax) {
2104 while (x <= bxmax && !btm_test(btm, x, y)) {
2107 if (btm_test(btm, x, y)) {
2109 while (x <= bxmax && btm_test(btm, x, y)) {
2112 i_render_fill(&r, start, y, x-start, NULL, fill);