9 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
10 int bxmin, int bxmax, int bymin, int bymax);
13 i_mmarray_cr(i_mmarray *ar,int l) {
18 alloc_size = sizeof(minmax) * l;
19 /* check for overflow */
20 if (alloc_size / l != sizeof(minmax)) {
21 fprintf(stderr, "overflow calculating memory allocation");
24 ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
25 for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
29 i_mmarray_dst(i_mmarray *ar) {
31 if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
35 i_mmarray_add(i_mmarray *ar,int x,int y) {
36 if (y>-1 && y<ar->lines)
38 if (x<ar->data[y].min) ar->data[y].min=x;
39 if (x>ar->data[y].max) ar->data[y].max=x;
44 i_mmarray_gmin(i_mmarray *ar,int y) {
45 if (y>-1 && y<ar->lines) return ar->data[y].min;
50 i_mmarray_getm(i_mmarray *ar,int y) {
51 if (y>-1 && y<ar->lines) return ar->data[y].max;
56 i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
58 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);
62 i_mmarray_render_fill(i_img *im,i_mmarray *ar,i_fill_t *fill) {
64 if (im->bits == i_8_bits && fill->fill_with_color) {
65 i_color *line = mymalloc(sizeof(i_color) * im->xsize); /* checked 5jul05 tonyc */
68 work = mymalloc(sizeof(i_color) * im->xsize); /* checked 5jul05 tonyc */
69 for(y=0;y<ar->lines;y++) {
70 if (ar->data[y].max!=-1) {
72 w = ar->data[y].max-ar->data[y].min;
75 i_glin(im, x, x+w, y, line);
76 (fill->fill_with_color)(fill, x, y, w, im->channels, work);
77 (fill->combine)(line, work, im->channels, w);
80 (fill->fill_with_color)(fill, x, y, w, im->channels, line);
82 i_plin(im, x, x+w, y, line);
91 i_fcolor *line = mymalloc(sizeof(i_fcolor) * im->xsize); /* checked 5jul05 tonyc */
92 i_fcolor *work = NULL;
94 work = mymalloc(sizeof(i_fcolor) * im->xsize); /* checked 5jul05 tonyc */
95 for(y=0;y<ar->lines;y++) {
96 if (ar->data[y].max!=-1) {
98 w = ar->data[y].max-ar->data[y].min;
100 if (fill->combinef) {
101 i_glinf(im, x, x+w, y, line);
102 (fill->fill_with_fcolor)(fill, x, y, w, im->channels, work);
103 (fill->combinef)(line, work, im->channels, w);
106 (fill->fill_with_fcolor)(fill, x, y, w, im->channels, line);
108 i_plinf(im, x, x+w, y, line);
121 i_arcdraw(int x1, int y1, int x2, int y2, i_mmarray *ar) {
125 alpha=(double)(y2-y1)/(double)(x2-x1);
126 if (fabs(alpha) <= 1)
128 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
132 i_mmarray_add(ar,x1,(int)(dsec+0.5));
140 if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
144 i_mmarray_add(ar,(int)(dsec+0.5),y1);
152 i_mmarray_info(i_mmarray *ar) {
154 for(i=0;i<ar->lines;i++)
155 if (ar->data[i].max!=-1) printf("line %d: min=%d, max=%d.\n",i,ar->data[i].min,ar->data[i].max);
159 i_arc_minmax(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
164 /*mm_log((1,"i_arc(im* 0x%x,x %d,y %d,rad %.2f,d1 %.2f,d2 %.2f,val 0x%x)\n",im,x,y,rad,d1,d2,val));*/
166 i_mmarray_cr(&dot, hlines->limit_y);
168 x1=(int)(x+0.5+rad*cos(d1*PI/180.0));
169 y1=(int)(y+0.5+rad*sin(d1*PI/180.0));
170 fx=(float)x1; fy=(float)y1;
172 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
173 i_arcdraw(x, y, x1, y1, &dot);
175 x1=(int)(x+0.5+rad*cos(d2*PI/180.0));
176 y1=(int)(y+0.5+rad*sin(d2*PI/180.0));
178 for(f=d1;f<=d2;f+=0.01) i_mmarray_add(&dot,(int)(x+0.5+rad*cos(f*PI/180.0)),(int)(y+0.5+rad*sin(f*PI/180.0)));
180 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
181 i_arcdraw(x, y, x1, y1, &dot);
183 /* render the minmax values onto the hlines */
184 for (y = 0; y < dot.lines; y++) {
185 if (dot.data[y].max!=-1) {
187 minx = dot.data[y].min;
188 width = dot.data[y].max - dot.data[y].min + 1;
189 i_int_hlines_add(hlines, y, minx, width);
198 i_arc_hlines(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
200 i_arc_minmax(hlines, x, y, rad, d1, d2);
203 i_arc_minmax(hlines, x, y, rad, d1, 360);
204 i_arc_minmax(hlines, x, y, rad, 0, d2);
209 =item i_arc(im, x, y, rad, d1, d2, color)
212 =synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
214 Fills an arc centered at (x,y) with radius I<rad> covering the range
215 of angles in degrees from d1 to d2, with the color.
221 i_arc(i_img *im,int x,int y,float rad,float d1,float d2,const i_color *val) {
224 i_int_init_hlines_img(&hlines, im);
226 i_arc_hlines(&hlines, x, y, rad, d1, d2);
228 i_int_hlines_fill_color(im, &hlines, val);
230 i_int_hlines_destroy(&hlines);
234 =item i_arc_cfill(im, x, y, rad, d1, d2, fill)
237 =synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
239 Fills an arc centered at (x,y) with radius I<rad> covering the range
240 of angles in degrees from d1 to d2, with the fill object.
245 #define MIN_CIRCLE_STEPS 8
246 #define MAX_CIRCLE_STEPS 360
249 i_arc_cfill(i_img *im,int x,int y,float rad,float d1,float d2,i_fill_t *fill) {
252 i_int_init_hlines_img(&hlines, im);
254 i_arc_hlines(&hlines, x, y, rad, d1, d2);
256 i_int_hlines_fill_fill(im, &hlines, fill);
258 i_int_hlines_destroy(&hlines);
262 arc_poly(int *count, double **xvals, double **yvals,
263 double x, double y, double rad, double d1, double d2) {
264 double d1_rad, d2_rad;
266 int steps, point_count;
269 /* normalize the angles */
272 if (d2 >= 360) { /* default is 361 */
286 d1_rad = d1 * PI / 180;
287 d2_rad = d2 * PI / 180;
289 /* how many segments for the curved part?
290 we do a maximum of one per degree, with a minimum of 8/circle
291 we try to aim at having about one segment per 2 pixels
292 Work it out per circle to get a step size.
294 I was originally making steps = circum/2 but that looked horrible.
296 I think there might be an issue in the polygon filler.
298 circum = 2 * PI * rad;
300 if (steps > MAX_CIRCLE_STEPS)
301 steps = MAX_CIRCLE_STEPS;
302 else if (steps < MIN_CIRCLE_STEPS)
303 steps = MIN_CIRCLE_STEPS;
305 angle_inc = 2 * PI / steps;
307 point_count = steps + 5; /* rough */
308 /* point_count is always relatively small, so allocation won't overflow */
309 *xvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
310 *yvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
312 /* from centre to edge at d1 */
315 (*xvals)[1] = x + rad * cos(d1_rad);
316 (*yvals)[1] = y + rad * sin(d1_rad);
319 /* step around the curve */
320 while (d1_rad < d2_rad) {
321 (*xvals)[*count] = x + rad * cos(d1_rad);
322 (*yvals)[*count] = y + rad * sin(d1_rad);
327 /* finish off the curve */
328 (*xvals)[*count] = x + rad * cos(d2_rad);
329 (*yvals)[*count] = y + rad * sin(d2_rad);
334 =item i_arc_aa(im, x, y, rad, d1, d2, color)
337 =synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
339 Antialias fills an arc centered at (x,y) with radius I<rad> covering
340 the range of angles in degrees from d1 to d2, with the color.
346 i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
347 const i_color *val) {
348 double *xvals, *yvals;
351 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
353 i_poly_aa(im, count, xvals, yvals, val);
360 =item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
363 =synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
365 Antialias fills an arc centered at (x,y) with radius I<rad> covering
366 the range of angles in degrees from d1 to d2, with the fill object.
372 i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
374 double *xvals, *yvals;
377 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
379 i_poly_aa_cfill(im, count, xvals, yvals, fill);
385 /* Temporary AA HACK */
389 static frac float_to_frac(float x) { return (frac)(0.5+x*16.0); }
393 polar_to_plane(float cx, float cy, float angle, float radius, frac *x, frac *y) {
394 *x = float_to_frac(cx+radius*cos(angle));
395 *y = float_to_frac(cy+radius*sin(angle));
400 make_minmax_list(i_mmarray *dot, float x, float y, float radius) {
402 float astep = radius>0.1 ? .5/radius : 10;
403 frac cx, cy, lx, ly, sx, sy;
405 mm_log((1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
407 polar_to_plane(x, y, angle, radius, &sx, &sy);
409 for(angle = 0.0; angle<361; angle +=astep) {
411 polar_to_plane(x, y, angle, radius, &cx, &cy);
414 if (fabs(cx-lx) > fabs(cy-ly)) {
417 ccx = lx; lx = cx; cx = ccx;
418 ccy = ly; ly = cy; cy = ccy;
421 for(ccx=lx; ccx<=cx; ccx++) {
422 ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
423 i_mmarray_add(dot, ccx, ccy);
429 ccy = ly; ly = cy; cy = ccy;
430 ccx = lx; lx = cx; cx = ccx;
433 for(ccy=ly; ccy<=cy; ccy++) {
434 if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
435 i_mmarray_add(dot, ccx, ccy);
441 /* Get the number of subpixels covered */
445 i_pixel_coverage(i_mmarray *dot, int x, int y) {
451 for(cy=y*16; cy<(y+1)*16; cy++) {
452 frac tmin = dot->data[cy].min;
453 frac tmax = dot->data[cy].max;
455 if (tmax == -1 || tmin > maxx || tmax < minx) continue;
457 if (tmin < minx) tmin = minx;
458 if (tmax > maxx) tmax = maxx;
466 =item i_circle_aa(im, x, y, rad, color)
469 =synopsis i_circle_aa(im, 50, 50, 45, &color);
471 Antialias fills a circle centered at (x,y) for radius I<rad> with
477 i_circle_aa(i_img *im, float x, float y, float rad, const i_color *val) {
482 mm_log((1, "i_circle_aa(im %p, x %d, y %d, rad %.2f, val %p)\n", im, x, y, rad, val));
484 i_mmarray_cr(&dot,16*im->ysize);
485 make_minmax_list(&dot, x, y, rad);
487 for(ly = 0; ly<im->ysize; ly++) {
488 int ix, cy, minx = INT_MAX, maxx = INT_MIN;
490 /* Find the left/rightmost set subpixels */
491 for(cy = 0; cy<16; cy++) {
492 frac tmin = dot.data[ly*16+cy].min;
493 frac tmax = dot.data[ly*16+cy].max;
494 if (tmax == -1) continue;
496 if (minx > tmin) minx = tmin;
497 if (maxx < tmax) maxx = tmax;
500 if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
504 for(ix=minx; ix<=maxx; ix++) {
505 int cnt = i_pixel_coverage(&dot, ix, ly);
506 if (cnt>255) cnt = 255;
507 if (cnt) { /* should never be true */
509 float ratio = (float)cnt/255.0;
510 i_gpix(im, ix, ly, &temp);
511 for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
512 i_ppix(im, ix, ly, &temp);
520 =item i_box(im, x1, y1, x2, y2, color)
523 =synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
525 Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
531 i_box(i_img *im,int x1,int y1,int x2,int y2,const i_color *val) {
533 mm_log((1,"i_box(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,val 0x%x)\n",im,x1,y1,x2,y2,val));
534 for(x=x1;x<x2+1;x++) {
538 for(y=y1;y<y2+1;y++) {
545 =item i_box_filled(im, x1, y1, x2, y2, color)
548 =synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
550 Fills the box from (x1,y1) to (x2,y2) inclusive with color.
556 i_box_filled(i_img *im,int x1,int y1,int x2,int y2, const i_color *val) {
558 mm_log((1,"i_box_filled(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,val 0x%x)\n",im,x1,y1,x2,y2,val));
559 for(x=x1;x<x2+1;x++) for (y=y1;y<y2+1;y++) i_ppix(im,x,y,val);
563 =item i_box_cfill(im, x1, y1, x2, y2, fill)
566 =synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
568 Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
574 i_box_cfill(i_img *im,int x1,int y1,int x2,int y2,i_fill_t *fill) {
575 mm_log((1,"i_box_cfill(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,fill 0x%x)\n",im,x1,y1,x2,y2,fill));
586 if (x1 >= x2 || y1 > y2)
588 if (im->bits == i_8_bits && fill->fill_with_color) {
589 i_color *line = mymalloc(sizeof(i_color) * (x2 - x1)); /* checked 5jul05 tonyc */
590 i_color *work = NULL;
592 work = mymalloc(sizeof(i_color) * (x2-x1)); /* checked 5jul05 tonyc */
595 i_glin(im, x1, x2, y1, line);
596 (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, work);
597 (fill->combine)(line, work, im->channels, x2-x1);
600 (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, line);
602 i_plin(im, x1, x2, y1, line);
610 i_fcolor *line = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
612 work = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
616 i_glinf(im, x1, x2, y1, line);
617 (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, work);
618 (fill->combinef)(line, work, im->channels, x2-x1);
621 (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, line);
623 i_plinf(im, x1, x2, y1, line);
634 =item i_line(im, x1, y1, x2, y2, val, endp)
638 Draw a line to image using bresenhams linedrawing algorithm
640 im - image to draw to
641 x1 - starting x coordinate
642 y1 - starting x coordinate
643 x2 - starting x coordinate
644 y2 - starting x coordinate
645 val - color to write to image
646 endp - endpoint flag (boolean)
652 i_line(i_img *im, int x1, int y1, int x2, int y2, const i_color *val, int endp) {
661 /* choose variable to iterate on */
662 if (abs(dx)>abs(dy)) {
668 t = x1; x1 = x2; x2 = t;
669 t = y1; y1 = y2; y2 = t;
687 for(x=x1; x<x2-1; x++) {
694 i_ppix(im, x+1, y, val);
702 t = x1; x1 = x2; x2 = t;
703 t = y1; y1 = y2; y2 = t;
721 for(y=y1; y<y2-1; y++) {
728 i_ppix(im, x, y+1, val);
732 i_ppix(im, x1, y1, val);
733 i_ppix(im, x2, y2, val);
735 if (x1 != x2 || y1 != y2)
736 i_ppix(im, x1, y1, val);
742 i_line_dda(i_img *im, int x1, int y1, int x2, int y2, i_color *val) {
747 for(x=x1; x<=x2; x++) {
748 dy = y1+ (x-x1)/(float)(x2-x1)*(y2-y1);
749 i_ppix(im, x, (int)(dy+0.5), val);
780 i_line_aa3(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
784 int temp,dx,dy,isec,ch;
786 mm_log((1,"i_line_aa(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,val 0x%x)\n",im,x1,y1,x2,y2,val));
791 if (abs(dx)>abs(dy)) { /* alpha < 1 */
792 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
793 alpha=(float)(y2-y1)/(float)(x2-x1);
799 /* dfrac=1-(1-dfrac)*(1-dfrac); */
800 /* This is something we can play with to try to get better looking lines */
802 i_gpix(im,x1,isec,&tval);
803 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
804 i_ppix(im,x1,isec,&tval);
806 i_gpix(im,x1,isec+1,&tval);
807 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
808 i_ppix(im,x1,isec+1,&tval);
814 if (y2<y1) { temp=y1; y1=y2; y2=temp; temp=x1; x1=x2; x2=temp; }
815 alpha=(float)(x2-x1)/(float)(y2-y1);
820 /* dfrac=sqrt(dfrac); */
821 /* This is something we can play with */
822 i_gpix(im,isec,y1,&tval);
823 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
824 i_ppix(im,isec,y1,&tval);
826 i_gpix(im,isec+1,y1,&tval);
827 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
828 i_ppix(im,isec+1,y1,&tval);
838 =item i_line_aa(im, x1, x2, y1, y2, color, endp)
842 Antialias draws a line from (x1,y1) to (x2, y2) in color.
844 The point (x2, y2) is drawn only if endp is set.
850 i_line_aa(i_img *im, int x1, int y1, int x2, int y2, const i_color *val, int endp) {
858 /* choose variable to iterate on */
859 if (abs(dx)>abs(dy)) {
865 t = x1; x1 = x2; x2 = t;
866 t = y1; y1 = y2; y2 = t;
880 p = dy2 - dx2; /* this has to be like this for AA */
884 for(x=x1; x<x2-1; x++) {
887 float t = (dy) ? -(float)(p)/(float)(dx2) : 1;
894 i_gpix(im,x+1,y,&tval);
895 for(ch=0;ch<im->channels;ch++)
896 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
897 i_ppix(im,x+1,y,&tval);
899 i_gpix(im,x+1,y+cpy,&tval);
900 for(ch=0;ch<im->channels;ch++)
901 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
902 i_ppix(im,x+1,y+cpy,&tval);
917 t = x1; x1 = x2; x2 = t;
918 t = y1; y1 = y2; y2 = t;
932 p = dx2 - dy2; /* this has to be like this for AA */
936 for(y=y1; y<y2-1; y++) {
939 float t = (dx) ? -(float)(p)/(float)(dy2) : 1;
946 i_gpix(im,x,y+1,&tval);
947 for(ch=0;ch<im->channels;ch++)
948 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
949 i_ppix(im,x,y+1,&tval);
951 i_gpix(im,x+cpx,y+1,&tval);
952 for(ch=0;ch<im->channels;ch++)
953 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
954 i_ppix(im,x+cpx,y+1,&tval);
967 i_ppix(im, x1, y1, val);
968 i_ppix(im, x2, y2, val);
970 if (x1 != x2 || y1 != y2)
971 i_ppix(im, x1, y1, val);
982 for(i=k+1;i<=n;i++) r*=i;
983 for(i=1;i<=(n-k);i++) r/=i;
988 /* Note in calculating t^k*(1-t)^(n-k)
989 we can start by using t^0=1 so this simplifies to
990 t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
991 to get a new level - this may lead to errors who knows lets test it */
994 i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
1002 /* this is the same size as the x and y arrays, so shouldn't overflow */
1003 bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
1004 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1008 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1010 for(t=0;t<=1;t+=0.005) {
1015 /* cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k);
1016 cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1018 cx+=bzcoef[k]*x[k]*ccoef;
1019 cy+=bzcoef[k]*y[k]*ccoef;
1022 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1024 i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val, 1);
1026 /* i_ppix(im,(int)(0.5+cx),(int)(0.5+cy),val); */
1036 REF: Graphics Gems I. page 282+
1040 /* This should be moved into a seperate file? */
1042 /* This is the truncation used:
1044 a double is multiplied by 16 and then truncated.
1045 This means that 0 -> 0
1046 So a triangle of (0,0) (10,10) (10,0) Will look like it's
1047 not filling the (10,10) point nor the (10,0)-(10,10) line segment
1052 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
1067 struct stack_element {
1075 /* create the link data to put push onto the stack */
1078 struct stack_element*
1079 crdata(int left,int right,int dadl,int dadr,int y, int dir) {
1080 struct stack_element *ste;
1081 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
1087 ste->myDirection = dir;
1091 /* i_ccomp compares two colors and gives true if they are the same */
1093 typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1096 i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
1098 for(i = 0; i < ch; i++)
1099 if (val1->channel[i] !=val2->channel[i])
1105 i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
1107 for(i = 0; i < ch; i++)
1108 if (val1->channel[i] !=val2->channel[i])
1114 i_lspan(i_img *im, int seedx, int seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1117 if (seedx-1 < 0) break;
1118 i_gpix(im,seedx-1,seedy,&cval);
1119 if (!cmpfunc(val,&cval,im->channels))
1127 i_rspan(i_img *im, int seedx, int seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1130 if (seedx+1 > im->xsize-1) break;
1131 i_gpix(im,seedx+1,seedy,&cval);
1132 if (!cmpfunc(val,&cval,im->channels)) break;
1138 /* Macro to create a link and push on to the list */
1140 #define ST_PUSH(left,right,dadl,dadr,y,dir) do { \
1141 struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); \
1142 llist_push(st,&s); \
1145 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1146 /* No overflow check! */
1148 #define ST_POP() do { \
1149 struct stack_element *s; \
1156 direction = s->myDirection; \
1160 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
1161 int pushrx = rx+1; \
1162 int pushlx = lx-1; \
1163 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
1165 ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); \
1166 if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); \
1169 #define SET(x,y) btm_set(btm,x,y)
1171 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1172 #define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels) ) ))
1176 /* The function that does all the real work */
1178 static struct i_bitmap *
1179 i_flood_fill_low(i_img *im,int seedx,int seedy,
1180 int *bxminp, int *bxmaxp, int *byminp, int *bymaxp,
1181 i_color const *seed, ff_cmpfunc cmpfunc) {
1191 struct i_bitmap *btm;
1193 int channels,xsize,ysize;
1196 channels = im->channels;
1200 btm = btm_new(xsize, ysize);
1201 st = llist_new(100, sizeof(struct stack_element*));
1203 /* Find the starting span and fill it */
1204 ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1205 rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
1206 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1208 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1209 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1212 /* Stack variables */
1221 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1224 if (y<0 || y>ysize-1) continue;
1225 if (bymin > y) bymin=y; /* in the worst case an extra line */
1226 if (bymax < y) bymax=y;
1230 if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
1233 while(INSIDE(lx, y, seed) && lx > 0) {
1239 if (bxmin > lx) bxmin = lx;
1240 while(x <= xsize-1) {
1241 /* printf("x=%d\n",x); */
1244 if (INSIDE(x, y, seed)) {
1245 /* case 1: was inside, am still inside */
1248 /* case 2: was inside, am no longer inside: just found the
1249 right edge of a span */
1250 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1252 if (bxmax < x) bxmax = x;
1256 if (x > rx) goto EXT;
1257 if (INSIDE(x, y, seed)) {
1259 /* case 3: Wasn't inside, am now: just found the start of a new run */
1263 /* case 4: Wasn't inside, still isn't */
1268 EXT: /* out of loop */
1270 /* hit an edge of the frame buffer while inside a run */
1271 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1272 if (bxmax < x) bxmax = x;
1287 =item i_flood_fill(im, seedx, seedy, color)
1290 =synopsis i_flood_fill(im, 50, 50, &color);
1292 Flood fills the 4-connected region starting from the point (seedx,
1293 seedy) with I<color>.
1295 Returns false if (seedx, seedy) are outside the image.
1301 i_flood_fill(i_img *im, int seedx, int seedy, const i_color *dcol) {
1302 int bxmin, bxmax, bymin, bymax;
1303 struct i_bitmap *btm;
1308 if (seedx < 0 || seedx >= im->xsize ||
1309 seedy < 0 || seedy >= im->ysize) {
1310 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1314 /* Get the reference color */
1315 i_gpix(im, seedx, seedy, &val);
1317 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1318 &val, i_ccomp_normal);
1320 for(y=bymin;y<=bymax;y++)
1321 for(x=bxmin;x<=bxmax;x++)
1322 if (btm_test(btm,x,y))
1323 i_ppix(im,x,y,dcol);
1329 =item i_flood_cfill(im, seedx, seedy, fill)
1332 =synopsis i_flood_cfill(im, 50, 50, fill);
1334 Flood fills the 4-connected region starting from the point (seedx,
1335 seedy) with I<fill>.
1337 Returns false if (seedx, seedy) are outside the image.
1343 i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
1344 int bxmin, bxmax, bymin, bymax;
1345 struct i_bitmap *btm;
1350 if (seedx < 0 || seedx >= im->xsize ||
1351 seedy < 0 || seedy >= im->ysize) {
1352 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1356 /* Get the reference color */
1357 i_gpix(im, seedx, seedy, &val);
1359 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1360 &val, i_ccomp_normal);
1362 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1369 =item i_flood_fill_border(im, seedx, seedy, color, border)
1372 =synopsis i_flood_fill_border(im, 50, 50, &color, &border);
1374 Flood fills the 4-connected region starting from the point (seedx,
1375 seedy) with I<color>, fill stops when the fill reaches a pixels with
1378 Returns false if (seedx, seedy) are outside the image.
1384 i_flood_fill_border(i_img *im, int seedx, int seedy, const i_color *dcol,
1385 const i_color *border) {
1386 int bxmin, bxmax, bymin, bymax;
1387 struct i_bitmap *btm;
1391 if (seedx < 0 || seedx >= im->xsize ||
1392 seedy < 0 || seedy >= im->ysize) {
1393 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1397 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1398 border, i_ccomp_border);
1400 for(y=bymin;y<=bymax;y++)
1401 for(x=bxmin;x<=bxmax;x++)
1402 if (btm_test(btm,x,y))
1403 i_ppix(im,x,y,dcol);
1409 =item i_flood_cfill_border(im, seedx, seedy, fill, border)
1412 =synopsis i_flood_cfill_border(im, 50, 50, fill, border);
1414 Flood fills the 4-connected region starting from the point (seedx,
1415 seedy) with I<fill>, the fill stops when it reaches pixels of color
1418 Returns false if (seedx, seedy) are outside the image.
1424 i_flood_cfill_border(i_img *im, int seedx, int seedy, i_fill_t *fill,
1425 const i_color *border) {
1426 int bxmin, bxmax, bymin, bymax;
1427 struct i_bitmap *btm;
1431 if (seedx < 0 || seedx >= im->xsize ||
1432 seedy < 0 || seedy >= im->ysize) {
1433 i_push_error(0, "i_flood_cfill_border: Seed pixel outside of image");
1437 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1438 border, i_ccomp_border);
1440 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1448 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
1449 int bxmin, int bxmax, int bymin, int bymax) {
1453 if (im->bits == i_8_bits && fill->fill_with_color) {
1454 /* bxmax/bxmin are inside the image, hence this won't overflow */
1455 i_color *line = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1456 i_color *work = NULL;
1458 work = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1460 for(y=bymin; y<=bymax; y++) {
1463 while (x < bxmax && !btm_test(btm, x, y)) {
1466 if (btm_test(btm, x, y)) {
1468 while (x < bxmax && btm_test(btm, x, y)) {
1471 if (fill->combine) {
1472 i_glin(im, start, x, y, line);
1473 (fill->fill_with_color)(fill, start, y, x-start, im->channels,
1475 (fill->combine)(line, work, im->channels, x-start);
1478 (fill->fill_with_color)(fill, start, y, x-start, im->channels,
1481 i_plin(im, start, x, y, line);
1490 /* bxmax/bxmin are inside the image, hence this won't overflow */
1491 i_fcolor *line = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1492 i_fcolor *work = NULL;
1494 work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1496 for(y=bymin;y<=bymax;y++) {
1499 while (x < bxmax && !btm_test(btm, x, y)) {
1502 if (btm_test(btm, x, y)) {
1504 while (x < bxmax && btm_test(btm, x, y)) {
1507 if (fill->combinef) {
1508 i_glinf(im, start, x, y, line);
1509 (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels,
1511 (fill->combinef)(line, work, im->channels, x-start);
1514 (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels,
1517 i_plinf(im, start, x, y, line);