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 double ceil_rad = ceil(rad);
504 i_img_dim first_row = floor(y) - ceil_rad;
505 i_img_dim last_row = ceil(y) + ceil_rad;
506 i_img_dim first_col = floor(x) - ceil_rad;
507 i_img_dim last_col = ceil(x) + ceil_rad;
508 double r_sqr = rad * rad;
509 i_img_dim max_width = 2 * ceil(rad) + 1;
510 unsigned char *coverage = NULL;
511 size_t coverage_size;
514 im_log((aIMCTX, 1, "i_circle_aa_low(im %p, centre(" i_DFp "), rad %.2f, r %p, ctx %p)\n",
515 im, i_DFcp(x, y), rad, r, ctx));
519 if (last_row > im->ysize-1)
520 last_row = im->ysize - 1;
523 if (last_col > im->xsize-1)
524 last_col = im->xsize - 1;
526 if (rad <= 0 || last_row < first_row || last_col < first_col) {
527 /* outside the image */
531 coverage_size = max_width;
532 coverage = mymalloc(coverage_size);
534 for(ly = first_row; ly < last_row; ly++) {
537 i_img_dim min_frac_left_x = 16 *(ceil(x) + ceil(rad));
538 i_img_dim max_frac_left_x = -1;
539 i_img_dim min_frac_right_x = 16 * (floor(x) - ceil(rad));
540 i_img_dim max_frac_right_x = -1;
541 /* reset work_y each row so the error doesn't build up */
545 for (sub = 0; sub < 16; ++sub) {
546 work_y += 1.0 / 16.0;
550 if (dy_sqr < r_sqr) {
551 double dx = sqrt(r_sqr - dy_sqr);
552 double left_x = x - dx;
553 double right_x = x + dx;
554 frac frac_left_x = float_to_frac(left_x);
555 frac frac_right_x = float_to_frac(right_x);
557 if (frac_left_x < min_frac_left_x)
558 min_frac_left_x = frac_left_x;
559 if (frac_left_x > max_frac_left_x)
560 max_frac_left_x = frac_left_x;
561 if (frac_right_x < min_frac_right_x)
562 min_frac_right_x = frac_right_x;
563 if (frac_right_x > max_frac_right_x)
564 max_frac_right_x = frac_right_x;
565 min_frac_x[sub] = frac_left_x;
566 max_frac_x[sub] = frac_right_x;
569 min_frac_x[sub] = max_frac_x[sub] = 0;
570 max_frac_left_x = im->xsize * 16;
571 min_frac_right_x = -1;
575 if (min_frac_left_x != -1) {
576 /* something to draw on this line */
577 i_img_dim min_x = (min_frac_left_x / 16);
578 i_img_dim max_x = (max_frac_right_x + 15) / 16;
579 i_img_dim left_solid = (max_frac_left_x + 15) / 16;
580 i_img_dim right_solid = min_frac_right_x / 16;
582 i_img_dim frac_work_x;
583 i_sample_t *cout = coverage;
585 for (work_x = min_x, frac_work_x = min_x * 16;
587 ++work_x, frac_work_x += 16) {
588 if (work_x <= left_solid || work_x >= right_solid) {
589 int pix_coverage = 0;
592 i_img_dim frac_work_right = frac_work_x + 16;
593 for (sub = 0; sub < 16; ++sub) {
594 frac pix_left = min_frac_x[sub];
595 frac pix_right = max_frac_x[sub];
596 if (pix_left < pix_right
597 && pix_left < frac_work_right
598 && pix_right >= frac_work_x) {
599 if (pix_left < frac_work_x)
600 pix_left = frac_work_x;
601 if (pix_right > frac_work_right)
602 pix_right = frac_work_right;
603 pix_coverage += pix_right - pix_left;
607 assert(pix_coverage <= 256);
608 *cout++ = pix_coverage * 255 / 256;
615 r(im, min_x, ly, max_x - min_x + 1, coverage, ctx);
623 scanline_flush_color(i_img *im, i_img_dim x, i_img_dim y, i_img_dim width, const unsigned char *cover, void *ctx) {
624 flush_color_t *fc = ctx;
626 i_render_color(&fc->r, x, y, width, cover, &fc->c);
630 scanline_flush_fill(i_img *im, i_img_dim x, i_img_dim y, i_img_dim width, const unsigned char *cover, void *ctx) {
631 flush_fill_t *ff = ctx;
633 i_render_fill(&ff->r, x, y, width, cover, ff->fill);
638 =item i_circle_out(im, x, y, r, col)
641 =synopsis i_circle_out(im, 50, 50, 45, &color);
643 Draw a circle outline centered at (x,y) with radius r,
652 (x, y) - the center of the circle
656 r - the radius of the circle in pixels, must be non-negative
660 Returns non-zero on success.
668 i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
669 const i_color *col) {
675 im_log((aIMCTX, 1, "i_circle_out(im %p, centre(" i_DFp "), rad %" i_DF ", col %p)\n",
676 im, i_DFcp(xc, yc), i_DFc(r), col));
678 im_clear_error(aIMCTX);
681 im_push_error(aIMCTX, 0, "circle: radius must be non-negative");
685 i_ppix(im, xc+r, yc, col);
686 i_ppix(im, xc-r, yc, col);
687 i_ppix(im, xc, yc+r, col);
688 i_ppix(im, xc, yc-r, col);
705 i_ppix(im, xc + x, yc + y, col);
706 i_ppix(im, xc + x, yc - y, col);
707 i_ppix(im, xc - x, yc + y, col);
708 i_ppix(im, xc - x, yc - y, col);
710 i_ppix(im, xc + y, yc + x, col);
711 i_ppix(im, xc + y, yc - x, col);
712 i_ppix(im, xc - y, yc + x, col);
713 i_ppix(im, xc - y, yc - x, col);
723 Convert an angle in degrees into an angle measure we can generate
724 simply from the numbers we have when drawing the circle.
730 arc_seg(double angle, int scale) {
731 i_img_dim seg = (angle + 45) / 90;
732 double remains = angle - seg * 90; /* should be in the range [-45,45] */
736 if (seg == 4 && remains > 0)
739 return scale * (seg * 2 + sin(remains * PI/180));
743 =item i_arc_out(im, x, y, r, d1, d2, col)
746 =synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
748 Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
749 over the angle range d1 through d2 degrees.
757 (x, y) - the center of the circle
761 r - the radius of the circle in pixels, must be non-negative
765 d1, d2 - the range of angles to draw the arc over, in degrees.
769 Returns non-zero on success.
777 i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
778 double d1, double d2, const i_color *col) {
782 i_img_dim segs[2][2];
785 i_img_dim seg_d1, seg_d2;
787 i_img_dim scale = r + 1;
788 i_img_dim seg1 = scale * 2;
789 i_img_dim seg2 = scale * 4;
790 i_img_dim seg3 = scale * 6;
791 i_img_dim seg4 = scale * 8;
794 im_log((aIMCTX,1,"i_arc_out(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
795 im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
797 im_clear_error(aIMCTX);
800 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
804 return i_circle_out(im, xc, yc, r, col);
807 d1 += 360 * floor((-d1 + 359) / 360);
809 d2 += 360 * floor((-d2 + 359) / 360);
812 seg_d1 = arc_seg(d1, scale);
813 seg_d2 = arc_seg(d2, scale);
814 if (seg_d2 < seg_d1) {
815 /* split into two segments */
828 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
829 i_img_dim seg_start = segs[seg_num][0];
830 i_img_dim seg_end = segs[seg_num][1];
832 i_ppix(im, xc+r, yc, col);
833 if (seg_start <= seg1 && seg_end >= seg1)
834 i_ppix(im, xc, yc+r, col);
835 if (seg_start <= seg2 && seg_end >= seg2)
836 i_ppix(im, xc-r, yc, col);
837 if (seg_start <= seg3 && seg_end >= seg3)
838 i_ppix(im, xc, yc-r, col);
856 if (seg_start <= sin_th && seg_end >= sin_th)
857 i_ppix(im, xc + x, yc + y, col);
858 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
859 i_ppix(im, xc + y, yc + x, col);
861 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
862 i_ppix(im, xc - y, yc + x, col);
863 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
864 i_ppix(im, xc - x, yc + y, col);
866 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
867 i_ppix(im, xc - x, yc - y, col);
868 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
869 i_ppix(im, xc - y, yc - x, col);
871 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
872 i_ppix(im, xc + y, yc - x, col);
873 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
874 i_ppix(im, xc + x, yc - y, col);
882 cover(i_img_dim r, i_img_dim j) {
883 double rjsqrt = sqrt(r*r - j*j);
885 return ceil(rjsqrt) - rjsqrt;
889 =item i_circle_out_aa(im, xc, yc, r, col)
891 =synopsis i_circle_out_aa(im, 50, 50, 45, &color);
893 Draw a circle outline centered at (x,y) with radius r, anti-aliased.
901 (xc, yc) - the center of the circle
905 r - the radius of the circle in pixels, must be non-negative
909 col - an i_color for the color to draw in.
913 Returns non-zero on success.
917 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
920 I use floating point for I<D> since for large circles the precision of
921 a [0,255] value isn't sufficient when approaching the end of the
927 i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
930 i_color workc = *col;
931 int orig_alpha = col->channel[3];
934 im_log((aIMCTX,1,"i_circle_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", col %p)",
935 im, i_DFcp(xc, yc), i_DFc(r), col));
937 im_clear_error(aIMCTX);
939 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
945 i_ppix_norm(im, xc+i, yc+j, col);
946 i_ppix_norm(im, xc-i, yc+j, col);
947 i_ppix_norm(im, xc+j, yc+i, col);
948 i_ppix_norm(im, xc+j, yc-i, col);
955 cv = (int)(d * 255 + 0.5);
961 workc.channel[3] = orig_alpha * inv_cv / 255;
962 i_ppix_norm(im, xc+i, yc+j, &workc);
963 i_ppix_norm(im, xc-i, yc+j, &workc);
964 i_ppix_norm(im, xc+i, yc-j, &workc);
965 i_ppix_norm(im, xc-i, yc-j, &workc);
968 i_ppix_norm(im, xc+j, yc+i, &workc);
969 i_ppix_norm(im, xc-j, yc+i, &workc);
970 i_ppix_norm(im, xc+j, yc-i, &workc);
971 i_ppix_norm(im, xc-j, yc-i, &workc);
975 workc.channel[3] = orig_alpha * cv / 255;
976 i_ppix_norm(im, xc+i-1, yc+j, &workc);
977 i_ppix_norm(im, xc-i+1, yc+j, &workc);
978 i_ppix_norm(im, xc+i-1, yc-j, &workc);
979 i_ppix_norm(im, xc-i+1, yc-j, &workc);
982 i_ppix_norm(im, xc+j, yc+i-1, &workc);
983 i_ppix_norm(im, xc-j, yc+i-1, &workc);
984 i_ppix_norm(im, xc+j, yc-i+1, &workc);
985 i_ppix_norm(im, xc-j, yc-i+1, &workc);
995 =item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
997 =synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
999 Draw a circle arc outline centered at (x,y) with radius r, from angle
1000 d1 degrees through angle d2 degrees, anti-aliased.
1008 (xc, yc) - the center of the circle
1012 r - the radius of the circle in pixels, must be non-negative
1016 d1, d2 - the range of angle in degrees to draw the arc through. If
1017 d2-d1 >= 360 a full circle is drawn.
1021 Returns non-zero on success.
1025 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
1031 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) {
1034 i_color workc = *col;
1035 i_img_dim segs[2][2];
1038 i_img_dim seg_d1, seg_d2;
1040 int orig_alpha = col->channel[3];
1041 i_img_dim scale = r + 1;
1042 i_img_dim seg1 = scale * 2;
1043 i_img_dim seg2 = scale * 4;
1044 i_img_dim seg3 = scale * 6;
1045 i_img_dim seg4 = scale * 8;
1048 im_log((aIMCTX,1,"i_arc_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
1049 im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
1051 im_clear_error(aIMCTX);
1053 im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
1057 return i_circle_out_aa(im, xc, yc, r, col);
1060 d1 += 360 * floor((-d1 + 359) / 360);
1062 d2 += 360 * floor((-d2 + 359) / 360);
1065 seg_d1 = arc_seg(d1, scale);
1066 seg_d2 = arc_seg(d2, scale);
1067 if (seg_d2 < seg_d1) {
1068 /* split into two segments */
1070 segs[0][1] = seg_d2;
1071 segs[1][0] = seg_d1;
1076 segs[0][0] = seg_d1;
1077 segs[0][1] = seg_d2;
1081 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
1082 i_img_dim seg_start = segs[seg_num][0];
1083 i_img_dim seg_end = segs[seg_num][1];
1090 i_ppix_norm(im, xc+i, yc+j, col);
1091 if (seg_start <= seg1 && seg_end >= seg1)
1092 i_ppix_norm(im, xc+j, yc+i, col);
1093 if (seg_start <= seg2 && seg_end >= seg2)
1094 i_ppix_norm(im, xc-i, yc+j, col);
1095 if (seg_start <= seg3 && seg_end >= seg3)
1096 i_ppix_norm(im, xc+j, yc-i, col);
1103 cv = (int)(d * 255 + 0.5);
1110 workc.channel[3] = orig_alpha * inv_cv / 255;
1112 if (seg_start <= sin_th && seg_end >= sin_th)
1113 i_ppix_norm(im, xc+i, yc+j, &workc);
1114 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1115 i_ppix_norm(im, xc-i, yc+j, &workc);
1116 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1117 i_ppix_norm(im, xc+i, yc-j, &workc);
1118 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1119 i_ppix_norm(im, xc-i, yc-j, &workc);
1122 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1123 i_ppix_norm(im, xc+j, yc+i, &workc);
1124 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1125 i_ppix_norm(im, xc-j, yc+i, &workc);
1126 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1127 i_ppix_norm(im, xc+j, yc-i, &workc);
1128 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1129 i_ppix_norm(im, xc-j, yc-i, &workc);
1133 workc.channel[3] = orig_alpha * cv / 255;
1134 if (seg_start <= sin_th && seg_end >= sin_th)
1135 i_ppix_norm(im, xc+i-1, yc+j, &workc);
1136 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1137 i_ppix_norm(im, xc-i+1, yc+j, &workc);
1138 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1139 i_ppix_norm(im, xc+i-1, yc-j, &workc);
1140 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1141 i_ppix_norm(im, xc-i+1, yc-j, &workc);
1143 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1144 i_ppix_norm(im, xc+j, yc+i-1, &workc);
1145 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1146 i_ppix_norm(im, xc-j, yc+i-1, &workc);
1147 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1148 i_ppix_norm(im, xc+j, yc-i+1, &workc);
1149 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1150 i_ppix_norm(im, xc-j, yc-i+1, &workc);
1160 =item i_box(im, x1, y1, x2, y2, color)
1163 =synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
1165 Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
1171 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) {
1175 im_log((aIMCTX, 1,"i_box(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1176 im, i_DFcp(x1,y1), i_DFcp(x2,y2), val));
1177 for(x=x1;x<x2+1;x++) {
1178 i_ppix(im,x,y1,val);
1179 i_ppix(im,x,y2,val);
1181 for(y=y1;y<y2+1;y++) {
1182 i_ppix(im,x1,y,val);
1183 i_ppix(im,x2,y,val);
1188 =item i_box_filled(im, x1, y1, x2, y2, color)
1191 =synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
1193 Fills the box from (x1,y1) to (x2,y2) inclusive with color.
1199 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) {
1200 i_img_dim x, y, width;
1204 im_log((aIMCTX,1,"i_box_filled(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1205 im, i_DFcp(x1, y1), i_DFcp(x2,y2) ,val));
1207 if (x1 > x2 || y1 > y2
1209 || x1 >= im->xsize || y1 > im->ysize)
1214 if (x2 >= im->xsize)
1218 if (y2 >= im->ysize)
1221 width = x2 - x1 + 1;
1223 if (im->type == i_palette_type
1224 && i_findcolor(im, val, &index)) {
1225 i_palidx *line = mymalloc(sizeof(i_palidx) * width);
1227 for (x = 0; x < width; ++x)
1230 for (y = y1; y <= y2; ++y)
1231 i_ppal(im, x1, x2+1, y, line);
1236 i_color *line = mymalloc(sizeof(i_color) * width);
1238 for (x = 0; x < width; ++x)
1241 for (y = y1; y <= y2; ++y)
1242 i_plin(im, x1, x2+1, y, line);
1249 =item i_box_filledf(im, x1, y1, x2, y2, color)
1252 =synopsis i_box_filledf(im, 0, 0, im->xsize-1, im->ysize-1, &fcolor);
1254 Fills the box from (x1,y1) to (x2,y2) inclusive with a floating point
1261 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) {
1262 i_img_dim x, y, width;
1265 im_log((aIMCTX, 1,"i_box_filledf(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1266 im, i_DFcp(x1, y1), i_DFcp(x2, y2), val));
1268 if (x1 > x2 || y1 > y2
1270 || x1 >= im->xsize || y1 > im->ysize)
1275 if (x2 >= im->xsize)
1279 if (y2 >= im->ysize)
1282 width = x2 - x1 + 1;
1284 if (im->bits <= 8) {
1286 c.rgba.r = SampleFTo8(val->rgba.r);
1287 c.rgba.g = SampleFTo8(val->rgba.g);
1288 c.rgba.b = SampleFTo8(val->rgba.b);
1289 c.rgba.a = SampleFTo8(val->rgba.a);
1291 i_box_filled(im, x1, y1, x2, y2, &c);
1294 i_fcolor *line = mymalloc(sizeof(i_fcolor) * width);
1296 for (x = 0; x < width; ++x)
1299 for (y = y1; y <= y2; ++y)
1300 i_plinf(im, x1, x2+1, y, line);
1309 =item i_box_cfill(im, x1, y1, x2, y2, fill)
1312 =synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
1314 Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
1320 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) {
1324 im_log((aIMCTX,1,"i_box_cfill(im* %p, p1(" i_DFp "), p2(" i_DFp "), fill %p)\n",
1325 im, i_DFcp(x1, y1), i_DFcp(x2,y2), fill));
1334 if (y2 >= im->ysize)
1336 if (x1 >= x2 || y1 > y2)
1339 i_render_init(&r, im, x2-x1);
1341 i_render_fill(&r, x1, y1, x2-x1, NULL, fill);
1348 =item i_line(C<im>, C<x1>, C<y1>, C<x2>, C<y2>, C<color>, C<endp>)
1352 =for stopwords Bresenham's
1354 Draw a line to image using Bresenham's line drawing algorithm
1356 im - image to draw to
1357 x1 - starting x coordinate
1358 y1 - starting x coordinate
1359 x2 - starting x coordinate
1360 y2 - starting x coordinate
1361 color - color to write to image
1362 endp - endpoint flag (boolean)
1368 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) {
1377 /* choose variable to iterate on */
1378 if (i_abs(dx) > i_abs(dy)) {
1379 i_img_dim dx2, dy2, cpy;
1384 t = x1; x1 = x2; x2 = t;
1385 t = y1; y1 = y2; y2 = t;
1403 for(x=x1; x<x2-1; x++) {
1410 i_ppix(im, x+1, y, val);
1413 i_img_dim dy2, dx2, cpx;
1418 t = x1; x1 = x2; x2 = t;
1419 t = y1; y1 = y2; y2 = t;
1437 for(y=y1; y<y2-1; y++) {
1444 i_ppix(im, x, y+1, val);
1448 i_ppix(im, x1, y1, val);
1449 i_ppix(im, x2, y2, val);
1451 if (x1 != x2 || y1 != y2)
1452 i_ppix(im, x1, y1, val);
1458 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) {
1463 for(x=x1; x<=x2; x++) {
1464 dy = y1+ (x-x1)/(double)(x2-x1)*(y2-y1);
1465 i_ppix(im, x, (i_img_dim)(dy+0.5), val);
1470 =item i_line_aa(C<im>, C<x1>, C<x2>, C<y1>, C<y2>, C<color>, C<endp>)
1474 Anti-alias draws a line from (x1,y1) to (x2, y2) in color.
1476 The point (x2, y2) is drawn only if C<endp> is set.
1482 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) {
1490 /* choose variable to iterate on */
1491 if (i_abs(dx) > i_abs(dy)) {
1492 i_img_dim dx2, dy2, cpy;
1497 t = x1; x1 = x2; x2 = t;
1498 t = y1; y1 = y2; y2 = t;
1512 p = dy2 - dx2; /* this has to be like this for AA */
1516 for(x=x1; x<x2-1; x++) {
1519 double t = (dy) ? -(float)(p)/(float)(dx2) : 1;
1526 i_gpix(im,x+1,y,&tval);
1527 for(ch=0;ch<im->channels;ch++)
1528 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
1529 i_ppix(im,x+1,y,&tval);
1531 i_gpix(im,x+1,y+cpy,&tval);
1532 for(ch=0;ch<im->channels;ch++)
1533 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
1534 i_ppix(im,x+1,y+cpy,&tval);
1544 i_img_dim dy2, dx2, cpx;
1549 t = x1; x1 = x2; x2 = t;
1550 t = y1; y1 = y2; y2 = t;
1564 p = dx2 - dy2; /* this has to be like this for AA */
1568 for(y=y1; y<y2-1; y++) {
1571 double t = (dx) ? -(double)(p)/(double)(dy2) : 1;
1578 i_gpix(im,x,y+1,&tval);
1579 for(ch=0;ch<im->channels;ch++)
1580 tval.channel[ch]=(unsigned char)(t1*(double)tval.channel[ch]+t2*(double)val->channel[ch]);
1581 i_ppix(im,x,y+1,&tval);
1583 i_gpix(im,x+cpx,y+1,&tval);
1584 for(ch=0;ch<im->channels;ch++)
1585 tval.channel[ch]=(unsigned char)(t2*(double)tval.channel[ch]+t1*(double)val->channel[ch]);
1586 i_ppix(im,x+cpx,y+1,&tval);
1599 i_ppix(im, x1, y1, val);
1600 i_ppix(im, x2, y2, val);
1602 if (x1 != x2 || y1 != y2)
1603 i_ppix(im, x1, y1, val);
1610 perm(i_img_dim n,i_img_dim k) {
1614 for(i=k+1;i<=n;i++) r*=i;
1615 for(i=1;i<=(n-k);i++) r/=i;
1620 /* Note in calculating t^k*(1-t)^(n-k)
1621 we can start by using t^0=1 so this simplifies to
1622 t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
1623 to get a new level - this may lead to errors who knows lets test it */
1626 i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
1630 i_img_dim lx = 0,ly = 0;
1634 /* this is the same size as the x and y arrays, so shouldn't overflow */
1635 bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
1636 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1640 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1642 for(t=0;t<=1;t+=0.005) {
1647 /* cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k);
1648 cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1650 cx+=bzcoef[k]*x[k]*ccoef;
1651 cy+=bzcoef[k]*y[k]*ccoef;
1654 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1656 i_line_aa(im,lx,ly,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val, 1);
1658 /* i_ppix(im,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val); */
1659 lx=(i_img_dim)(0.5+cx);
1660 ly=(i_img_dim)(0.5+cy);
1668 REF: Graphics Gems I. page 282+
1672 /* This should be moved into a seperate file? */
1674 /* This is the truncation used:
1676 a double is multiplied by 16 and then truncated.
1677 This means that 0 -> 0
1678 So a triangle of (0,0) (10,10) (10,0) Will look like it's
1679 not filling the (10,10) point nor the (10,0)-(10,10) line segment
1684 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
1689 i_img_dim mylx,myrx;
1690 i_img_dim dadlx,dadrx;
1699 struct stack_element {
1700 i_img_dim myLx,myRx;
1701 i_img_dim dadLx,dadRx;
1707 /* create the link data to put push onto the stack */
1710 struct stack_element*
1711 crdata(i_img_dim left,i_img_dim right,i_img_dim dadl,i_img_dim dadr,i_img_dim y, int dir) {
1712 struct stack_element *ste;
1713 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
1719 ste->myDirection = dir;
1723 /* i_ccomp compares two colors and gives true if they are the same */
1725 typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1728 i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
1730 for(i = 0; i < ch; i++)
1731 if (val1->channel[i] !=val2->channel[i])
1737 i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
1739 for(i = 0; i < ch; i++)
1740 if (val1->channel[i] !=val2->channel[i])
1746 i_lspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1749 if (seedx-1 < 0) break;
1750 i_gpix(im,seedx-1,seedy,&cval);
1751 if (!cmpfunc(val,&cval,im->channels))
1759 i_rspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1762 if (seedx+1 > im->xsize-1) break;
1763 i_gpix(im,seedx+1,seedy,&cval);
1764 if (!cmpfunc(val,&cval,im->channels)) break;
1770 #ifdef DEBUG_FLOOD_FILL
1772 #define ST_PUSH_NOTE(left, right, dadl, dadr, y, dir) \
1773 fprintf(stderr, "push(left %" i_DF ", right %" i_DF ", dadleft %" i_DF ", dadright %" i_DF ", y %" i_DF ", dir %d, line %d)\n", \
1774 i_DFc(left), i_DFc(right), i_DFc(dadl), i_DFc(dadr), i_DFc(y), (dir), __LINE__)
1776 #define ST_POP_NOTE(left, right, dadl, dadr, y, dir) \
1777 fprintf(stderr, "popped(left %" i_DF ", right %" i_DF ", dadleft %" i_DF ", dadright %" i_DF ", y %" i_DF ", dir %d, line %d)\n", \
1778 i_DFc(left), i_DFc(right), i_DFc(dadl), i_DFc(dadr), i_DFc(y), (dir), __LINE__)
1780 #define ST_STACK_NOTE(dadl, dadr, left, right, y, dir) \
1781 fprintf(stderr, "stack(left %" i_DF ", right %" i_DF ", dadleft %" i_DF ", dadright %" i_DF ", y %" i_DF ", dir %d, line %d)\n", \
1782 i_DFc(left), i_DFc(right), i_DFc(dadl), i_DFc(dadr), i_DFc(y), (dir), __LINE__)
1786 #define ST_PUSH_NOTE(left, right, dadl, dadr, y, dir)
1788 #define ST_POP_NOTE(left, right, dadl, dadr, y, dir)
1790 #define ST_STACK_NOTE(dadl, dadr, left, right, y, dir)
1795 /* Macro to create a link and push on to the list */
1797 #define ST_PUSH(left,right,dadl,dadr,y,dir) do { \
1798 struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); \
1799 ST_PUSH_NOTE(left, right, dadl, dadr, y, dir); \
1800 llist_push(st,&s); \
1803 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1804 /* No overflow check! */
1806 #define ST_POP() do { \
1807 struct stack_element *s; \
1814 direction = s->myDirection; \
1815 ST_POP_NOTE(lx, rx, dadLx, dadRx, y, direction); \
1819 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
1820 i_img_dim pushrx = rx+1; \
1821 i_img_dim pushlx = lx-1; \
1822 ST_STACK_NOTE(lx, rx, dadLx, dadRx, y, dir); \
1823 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
1825 ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); \
1827 ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); \
1830 #define SET(x,y) btm_set(btm,x,y)
1832 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1833 #define INSIDE(x,y, seed) \
1834 (assert((x) >= 0 && (x) < (im)->xsize && (y) >= 0 && (y) < (im)->ysize), \
1835 (!btm_test(btm,x,y) && \
1836 ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels) ) ))
1838 /* The function that does all the real work */
1840 static struct i_bitmap *
1841 i_flood_fill_low(i_img *im,i_img_dim seedx,i_img_dim seedy,
1842 i_img_dim *bxminp, i_img_dim *bxmaxp, i_img_dim *byminp, i_img_dim *bymaxp,
1843 i_color const *seed, ff_cmpfunc cmpfunc) {
1847 i_img_dim bxmin = seedx;
1848 i_img_dim bxmax = seedx;
1849 i_img_dim bymin = seedy;
1850 i_img_dim bymax = seedy;
1853 struct i_bitmap *btm;
1856 i_img_dim xsize,ysize;
1857 i_color cval; /* used by the INSIDE() macro */
1859 channels = im->channels;
1863 btm = btm_new(xsize, ysize);
1864 st = llist_new(100, sizeof(struct stack_element*));
1866 /* Find the starting span and fill it */
1867 ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1868 rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
1869 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1873 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1874 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1877 /* Stack variables */
1879 i_img_dim dadLx,dadRx;
1886 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1889 if (y<0 || y>ysize-1) continue;
1890 if (bymin > y) bymin=y; /* in the worst case an extra line */
1891 if (bymax < y) bymax=y;
1895 if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
1898 while(lx >= 0 && INSIDE(lx, y, seed)) {
1902 /* lx should point at the left-most INSIDE() pixel */
1906 if (bxmin > lx) bxmin = lx;
1907 while(x <= xsize-1) {
1908 /* printf("x=%d\n",x); */
1911 if (INSIDE(x, y, seed)) {
1912 /* case 1: was inside, am still inside */
1915 /* case 2: was inside, am no longer inside: just found the
1916 right edge of a span */
1917 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1919 if (bxmax < x) bxmax = x;
1923 if (x > rx) goto EXT;
1924 if (INSIDE(x, y, seed)) {
1926 /* case 3: Wasn't inside, am now: just found the start of a new run */
1930 /* case 4: Wasn't inside, still isn't */
1935 EXT: /* out of loop */
1937 /* hit an edge of the frame buffer while inside a run */
1938 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1939 if (bxmax < x) bxmax = x;
1954 =item i_flood_fill(C<im>, C<seedx>, C<seedy>, C<color>)
1957 =synopsis i_flood_fill(im, 50, 50, &color);
1959 Flood fills the 4-connected region starting from the point (C<seedx>,
1960 C<seedy>) with I<color>.
1962 Returns false if (C<seedx>, C<seedy>) are outside the image.
1968 i_flood_fill(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol) {
1969 i_img_dim bxmin, bxmax, bymin, bymax;
1970 struct i_bitmap *btm;
1975 im_log((aIMCTX, 1, "i_flood_fill(im %p, seed(" i_DFp "), col %p)",
1976 im, i_DFcp(seedx, seedy), dcol));
1978 im_clear_error(aIMCTX);
1979 if (seedx < 0 || seedx >= im->xsize ||
1980 seedy < 0 || seedy >= im->ysize) {
1981 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
1985 /* Get the reference color */
1986 i_gpix(im, seedx, seedy, &val);
1988 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1989 &val, i_ccomp_normal);
1991 for(y=bymin;y<=bymax;y++)
1992 for(x=bxmin;x<=bxmax;x++)
1993 if (btm_test(btm,x,y))
1994 i_ppix(im,x,y,dcol);
2000 =item i_flood_cfill(C<im>, C<seedx>, C<seedy>, C<fill>)
2003 =synopsis i_flood_cfill(im, 50, 50, fill);
2005 Flood fills the 4-connected region starting from the point (C<seedx>,
2006 C<seedy>) with C<fill>.
2008 Returns false if (C<seedx>, C<seedy>) are outside the image.
2014 i_flood_cfill(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill) {
2015 i_img_dim bxmin, bxmax, bymin, bymax;
2016 struct i_bitmap *btm;
2020 im_log((aIMCTX, 1, "i_flood_cfill(im %p, seed(" i_DFp "), fill %p)",
2021 im, i_DFcp(seedx, seedy), fill));
2023 im_clear_error(aIMCTX);
2025 if (seedx < 0 || seedx >= im->xsize ||
2026 seedy < 0 || seedy >= im->ysize) {
2027 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
2031 /* Get the reference color */
2032 i_gpix(im, seedx, seedy, &val);
2034 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
2035 &val, i_ccomp_normal);
2037 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
2044 =item i_flood_fill_border(C<im>, C<seedx>, C<seedy>, C<color>, C<border>)
2047 =synopsis i_flood_fill_border(im, 50, 50, &color, &border);
2049 Flood fills the 4-connected region starting from the point (C<seedx>,
2050 C<seedy>) with C<color>, fill stops when the fill reaches a pixels
2051 with color C<border>.
2053 Returns false if (C<seedx>, C<seedy>) are outside the image.
2059 i_flood_fill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol,
2060 const i_color *border) {
2061 i_img_dim bxmin, bxmax, bymin, bymax;
2062 struct i_bitmap *btm;
2066 im_log((aIMCTX, 1, "i_flood_cfill(im %p, seed(" i_DFp "), dcol %p, border %p)",
2067 im, i_DFcp(seedx, seedy), dcol, border));
2069 im_clear_error(aIMCTX);
2070 if (seedx < 0 || seedx >= im->xsize ||
2071 seedy < 0 || seedy >= im->ysize) {
2072 im_push_error(aIMCTX, 0, "i_flood_cfill: Seed pixel outside of image");
2076 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
2077 border, i_ccomp_border);
2079 for(y=bymin;y<=bymax;y++)
2080 for(x=bxmin;x<=bxmax;x++)
2081 if (btm_test(btm,x,y))
2082 i_ppix(im,x,y,dcol);
2088 =item i_flood_cfill_border(C<im>, C<seedx>, C<seedy>, C<fill>, C<border>)
2091 =synopsis i_flood_cfill_border(im, 50, 50, fill, border);
2093 Flood fills the 4-connected region starting from the point (C<seedx>,
2094 C<seedy>) with C<fill>, the fill stops when it reaches pixels of color
2097 Returns false if (C<seedx>, C<seedy>) are outside the image.
2103 i_flood_cfill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill,
2104 const i_color *border) {
2105 i_img_dim bxmin, bxmax, bymin, bymax;
2106 struct i_bitmap *btm;
2109 im_log((aIMCTX, 1, "i_flood_cfill_border(im %p, seed(" i_DFp "), fill %p, border %p)",
2110 im, i_DFcp(seedx, seedy), fill, border));
2112 im_clear_error(aIMCTX);
2114 if (seedx < 0 || seedx >= im->xsize ||
2115 seedy < 0 || seedy >= im->ysize) {
2116 im_push_error(aIMCTX, 0, "i_flood_cfill_border: Seed pixel outside of image");
2120 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
2121 border, i_ccomp_border);
2123 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
2131 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
2132 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax) {
2138 i_render_init(&r, im, bxmax - bxmin + 1);
2140 for(y=bymin; y<=bymax; y++) {
2142 while (x <= bxmax) {
2143 while (x <= bxmax && !btm_test(btm, x, y)) {
2146 if (btm_test(btm, x, y)) {
2148 while (x <= bxmax && btm_test(btm, x, y)) {
2151 i_render_fill(&r, start, y, x-start, NULL, fill);