9 i_mmarray_cr(i_mmarray *ar,int l) {
13 ar->data=mymalloc(sizeof(minmax)*l);
14 for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
18 i_mmarray_dst(i_mmarray *ar) {
20 if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
24 i_mmarray_add(i_mmarray *ar,int x,int y) {
25 if (y>-1 && y<ar->lines)
27 if (x<ar->data[y].min) ar->data[y].min=x;
28 if (x>ar->data[y].max) ar->data[y].max=x;
33 i_mmarray_gmin(i_mmarray *ar,int y) {
34 if (y>-1 && y<ar->lines) return ar->data[y].min;
39 i_mmarray_getm(i_mmarray *ar,int y) {
40 if (y>-1 && y<ar->lines) return ar->data[y].max;
45 i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
47 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);
51 i_mmarray_render_fill(i_img *im,i_mmarray *ar,i_fill_t *fill) {
53 if (im->bits == i_8_bits && fill->fill_with_color) {
54 i_color *line = mymalloc(sizeof(i_color) * im->xsize);
57 work = mymalloc(sizeof(i_color) * im->xsize);
58 for(y=0;y<ar->lines;y++) {
59 if (ar->data[y].max!=-1) {
61 w = ar->data[y].max-ar->data[y].min;
64 i_glin(im, x, x+w, y, line);
65 (fill->fill_with_color)(fill, x, y, w, im->channels, work);
66 (fill->combine)(line, work, im->channels, w);
69 (fill->fill_with_color)(fill, x, y, w, im->channels, line);
71 i_plin(im, x, x+w, y, line);
80 i_fcolor *line = mymalloc(sizeof(i_fcolor) * im->xsize);
81 i_fcolor *work = NULL;
83 work = mymalloc(sizeof(i_fcolor) * im->xsize);
84 for(y=0;y<ar->lines;y++) {
85 if (ar->data[y].max!=-1) {
87 w = ar->data[y].max-ar->data[y].min;
90 i_glinf(im, x, x+w, y, line);
91 (fill->fill_with_fcolor)(fill, x, y, w, im->channels, work);
92 (fill->combinef)(line, work, im->channels, w);
95 (fill->fill_with_fcolor)(fill, x, y, w, im->channels, line);
97 i_plinf(im, x, x+w, y, line);
110 i_arcdraw(int x1, int y1, int x2, int y2, i_mmarray *ar) {
114 alpha=(double)(y2-y1)/(double)(x2-x1);
115 if (fabs(alpha) <= 1)
117 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
121 i_mmarray_add(ar,x1,(int)(dsec+0.5));
129 if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
133 i_mmarray_add(ar,(int)(dsec+0.5),y1);
141 i_mmarray_info(i_mmarray *ar) {
143 for(i=0;i<ar->lines;i++)
144 if (ar->data[i].max!=-1) printf("line %d: min=%d, max=%d.\n",i,ar->data[i].min,ar->data[i].max);
148 i_arc_minmax(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
153 /*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));*/
155 i_mmarray_cr(&dot, hlines->limit_y);
157 x1=(int)(x+0.5+rad*cos(d1*PI/180.0));
158 y1=(int)(y+0.5+rad*sin(d1*PI/180.0));
159 fx=(float)x1; fy=(float)y1;
161 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
162 i_arcdraw(x, y, x1, y1, &dot);
164 x1=(int)(x+0.5+rad*cos(d2*PI/180.0));
165 y1=(int)(y+0.5+rad*sin(d2*PI/180.0));
167 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)));
169 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
170 i_arcdraw(x, y, x1, y1, &dot);
172 /* render the minmax values onto the hlines */
173 for (y = 0; y < dot.lines; y++) {
174 if (dot.data[y].max!=-1) {
176 minx = dot.data[y].min;
177 width = dot.data[y].max - dot.data[y].min + 1;
178 i_int_hlines_add(hlines, y, minx, width);
187 i_arc_hlines(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
189 i_arc_minmax(hlines, x, y, rad, d1, d2);
192 i_arc_minmax(hlines, x, y, rad, d1, 360);
193 i_arc_minmax(hlines, x, y, rad, 0, d2);
198 i_arc(i_img *im,int x,int y,float rad,float d1,float d2,i_color *val) {
201 i_int_init_hlines_img(&hlines, im);
203 i_arc_hlines(&hlines, x, y, rad, d1, d2);
205 i_int_hlines_fill_color(im, &hlines, val);
207 i_int_hlines_destroy(&hlines);
210 #define MIN_CIRCLE_STEPS 8
211 #define MAX_CIRCLE_STEPS 360
214 i_arc_cfill(i_img *im,int x,int y,float rad,float d1,float d2,i_fill_t *fill) {
217 i_int_init_hlines_img(&hlines, im);
219 i_arc_hlines(&hlines, x, y, rad, d1, d2);
221 i_int_hlines_fill_fill(im, &hlines, fill);
223 i_int_hlines_destroy(&hlines);
227 arc_poly(int *count, double **xvals, double **yvals,
228 double x, double y, double rad, double d1, double d2) {
229 double d1_rad, d2_rad;
231 int steps, point_count;
234 /* normalize the angles */
237 if (d2 >= 360) { /* default is 361 */
251 d1_rad = d1 * PI / 180;
252 d2_rad = d2 * PI / 180;
254 /* how many segments for the curved part?
255 we do a maximum of one per degree, with a minimum of 8/circle
256 we try to aim at having about one segment per 2 pixels
257 Work it out per circle to get a step size.
259 I was originally making steps = circum/2 but that looked horrible.
261 I think there might be an issue in the polygon filler.
263 circum = 2 * PI * rad;
265 if (steps > MAX_CIRCLE_STEPS)
266 steps = MAX_CIRCLE_STEPS;
267 else if (steps < MIN_CIRCLE_STEPS)
268 steps = MIN_CIRCLE_STEPS;
270 angle_inc = 2 * PI / steps;
272 point_count = steps + 5; /* rough */
273 *xvals = mymalloc(point_count * sizeof(double));
274 *yvals = mymalloc(point_count * sizeof(double));
276 /* from centre to edge at d1 */
279 (*xvals)[1] = x + rad * cos(d1_rad);
280 (*yvals)[1] = y + rad * sin(d1_rad);
283 /* step around the curve */
284 while (d1_rad < d2_rad) {
285 (*xvals)[*count] = x + rad * cos(d1_rad);
286 (*yvals)[*count] = y + rad * sin(d1_rad);
291 /* finish off the curve */
292 (*xvals)[*count] = x + rad * cos(d2_rad);
293 (*yvals)[*count] = y + rad * sin(d2_rad);
298 i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
300 double *xvals, *yvals;
303 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
305 i_poly_aa(im, count, xvals, yvals, val);
312 i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
314 double *xvals, *yvals;
317 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
319 i_poly_aa_cfill(im, count, xvals, yvals, fill);
325 /* Temporary AA HACK */
329 static frac float_to_frac(float x) { return (frac)(0.5+x*16.0); }
330 static int frac_sub (frac x) { return (x%16); }
331 static int frac_int (frac x) { return (x/16); }
332 static float frac_to_float(float x) { return (float)x/16.0; }
336 polar_to_plane(float cx, float cy, float angle, float radius, frac *x, frac *y) {
337 *x = float_to_frac(cx+radius*cos(angle));
338 *y = float_to_frac(cy+radius*sin(angle));
343 order_pair(frac *x, frac *y) {
356 make_minmax_list(i_mmarray *dot, float x, float y, float radius) {
358 float astep = radius>0.1 ? .5/radius : 10;
359 frac cx, cy, lx, ly, sx, sy;
361 mm_log((1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
363 polar_to_plane(x, y, angle, radius, &sx, &sy);
365 for(angle = 0.0; angle<361; angle +=astep) {
367 polar_to_plane(x, y, angle, radius, &cx, &cy);
370 if (fabs(cx-lx) > fabs(cy-ly)) {
373 ccx = lx; lx = cx; cx = ccx;
374 ccy = ly; ly = cy; cy = ccy;
377 for(ccx=lx; ccx<=cx; ccx++) {
378 ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
379 i_mmarray_add(dot, ccx, ccy);
385 ccy = ly; ly = cy; cy = ccy;
386 ccx = lx; lx = cx; cx = ccx;
389 for(ccy=ly; ccy<=cy; ccy++) {
390 if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
391 i_mmarray_add(dot, ccx, ccy);
397 /* Get the number of subpixels covered */
401 i_pixel_coverage(i_mmarray *dot, int x, int y) {
407 for(cy=y*16; cy<(y+1)*16; cy++) {
408 frac tmin = dot->data[cy].min;
409 frac tmax = dot->data[cy].max;
411 if (tmax == -1 || tmin > maxx || tmax < minx) continue;
413 if (tmin < minx) tmin = minx;
414 if (tmax > maxx) tmax = maxx;
422 i_circle_aa(i_img *im, float x, float y, float rad, i_color *val) {
427 mm_log((1, "i_circle_aa(im %p, x %d, y %d, rad %.2f, val %p)\n", im, x, y, rad, val));
429 i_mmarray_cr(&dot,16*im->ysize);
430 make_minmax_list(&dot, x, y, rad);
432 for(ly = 0; ly<im->ysize; ly++) {
433 int ix, cy, minx = INT_MAX, maxx = INT_MIN;
435 /* Find the left/rightmost set subpixels */
436 for(cy = 0; cy<16; cy++) {
437 frac tmin = dot.data[ly*16+cy].min;
438 frac tmax = dot.data[ly*16+cy].max;
439 if (tmax == -1) continue;
441 if (minx > tmin) minx = tmin;
442 if (maxx < tmax) maxx = tmax;
445 if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
449 for(ix=minx; ix<=maxx; ix++) {
450 int cnt = i_pixel_coverage(&dot, ix, ly);
451 if (cnt>255) cnt = 255;
452 if (cnt) { /* should never be true */
454 float ratio = (float)cnt/255.0;
455 i_gpix(im, ix, ly, &temp);
456 for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
457 i_ppix(im, ix, ly, &temp);
470 i_box(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
472 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));
473 for(x=x1;x<x2+1;x++) {
477 for(y=y1;y<y2+1;y++) {
484 i_box_filled(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
486 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));
487 for(x=x1;x<x2+1;x++) for (y=y1;y<y2+1;y++) i_ppix(im,x,y,val);
491 i_box_cfill(i_img *im,int x1,int y1,int x2,int y2,i_fill_t *fill) {
492 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));
495 if (im->bits == i_8_bits && fill->fill_with_color) {
496 i_color *line = mymalloc(sizeof(i_color) * (x2 - x1));
497 i_color *work = NULL;
499 work = mymalloc(sizeof(i_color) * (x2-x1));
502 i_glin(im, x1, x2, y1, line);
503 (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, work);
504 (fill->combine)(line, work, im->channels, x2-x1);
507 (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, line);
509 i_plin(im, x1, x2, y1, line);
517 i_fcolor *line = mymalloc(sizeof(i_fcolor) * (x2 - x1));
519 work = mymalloc(sizeof(i_fcolor) * (x2 - x1));
523 i_glinf(im, x1, x2, y1, line);
524 (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, work);
525 (fill->combinef)(line, work, im->channels, x2-x1);
528 (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, line);
530 i_plinf(im, x1, x2, y1, line);
541 =item i_line(im, x1, y1, x2, y2, val, endp)
543 Draw a line to image using bresenhams linedrawing algorithm
545 im - image to draw to
546 x1 - starting x coordinate
547 y1 - starting x coordinate
548 x2 - starting x coordinate
549 y2 - starting x coordinate
550 val - color to write to image
551 endp - endpoint flag (boolean)
557 i_line(i_img *im, int x1, int y1, int x2, int y2, i_color *val, int endp) {
566 /* choose variable to iterate on */
567 if (abs(dx)>abs(dy)) {
573 t = x1; x1 = x2; x2 = t;
574 t = y1; y1 = y2; y2 = t;
592 for(x=x1; x<x2-1; x++) {
599 i_ppix(im, x+1, y, val);
607 t = x1; x1 = x2; x2 = t;
608 t = y1; y1 = y2; y2 = t;
626 for(y=y1; y<y2-1; y++) {
633 i_ppix(im, x, y+1, val);
637 i_ppix(im, x1, y1, val);
638 i_ppix(im, x2, y2, val);
640 if (x1 != x2 || y1 != y2)
641 i_ppix(im, x1, y1, val);
647 i_line_dda(i_img *im, int x1, int y1, int x2, int y2, i_color *val) {
652 for(x=x1; x<=x2; x++) {
653 dy = y1+ (x-x1)/(float)(x2-x1)*(y2-y1);
654 i_ppix(im, x, (int)(dy+0.5), val);
685 i_line_aa3(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
689 int temp,dx,dy,isec,ch;
691 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));
696 if (abs(dx)>abs(dy)) { /* alpha < 1 */
697 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
698 alpha=(float)(y2-y1)/(float)(x2-x1);
704 /* dfrac=1-(1-dfrac)*(1-dfrac); */
705 /* This is something we can play with to try to get better looking lines */
707 i_gpix(im,x1,isec,&tval);
708 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
709 i_ppix(im,x1,isec,&tval);
711 i_gpix(im,x1,isec+1,&tval);
712 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
713 i_ppix(im,x1,isec+1,&tval);
719 if (y2<y1) { temp=y1; y1=y2; y2=temp; temp=x1; x1=x2; x2=temp; }
720 alpha=(float)(x2-x1)/(float)(y2-y1);
725 /* dfrac=sqrt(dfrac); */
726 /* This is something we can play with */
727 i_gpix(im,isec,y1,&tval);
728 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
729 i_ppix(im,isec,y1,&tval);
731 i_gpix(im,isec+1,y1,&tval);
732 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
733 i_ppix(im,isec+1,y1,&tval);
745 i_line_aa(i_img *im, int x1, int y1, int x2, int y2, i_color *val, int endp) {
753 /* choose variable to iterate on */
754 if (abs(dx)>abs(dy)) {
760 t = x1; x1 = x2; x2 = t;
761 t = y1; y1 = y2; y2 = t;
775 p = dy2 - dx2; /* this has to be like this for AA */
779 for(x=x1; x<x2-1; x++) {
782 float t = (dy) ? -(float)(p)/(float)(dx2) : 1;
789 i_gpix(im,x+1,y,&tval);
790 for(ch=0;ch<im->channels;ch++)
791 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
792 i_ppix(im,x+1,y,&tval);
794 i_gpix(im,x+1,y+cpy,&tval);
795 for(ch=0;ch<im->channels;ch++)
796 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
797 i_ppix(im,x+1,y+cpy,&tval);
812 t = x1; x1 = x2; x2 = t;
813 t = y1; y1 = y2; y2 = t;
827 p = dx2 - dy2; /* this has to be like this for AA */
831 for(y=y1; y<y2-1; y++) {
834 float t = (dx) ? -(float)(p)/(float)(dy2) : 1;
841 i_gpix(im,x,y+1,&tval);
842 for(ch=0;ch<im->channels;ch++)
843 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
844 i_ppix(im,x,y+1,&tval);
846 i_gpix(im,x+cpx,y+1,&tval);
847 for(ch=0;ch<im->channels;ch++)
848 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
849 i_ppix(im,x+cpx,y+1,&tval);
862 i_ppix(im, x1, y1, val);
863 i_ppix(im, x2, y2, val);
865 if (x1 != x2 || y1 != y2)
866 i_ppix(im, x1, y1, val);
877 for(i=k+1;i<=n;i++) r*=i;
878 for(i=1;i<=(n-k);i++) r/=i;
883 /* Note in calculating t^k*(1-t)^(n-k)
884 we can start by using t^0=1 so this simplifies to
885 t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
886 to get a new level - this may lead to errors who knows lets test it */
889 i_bezier_multi(i_img *im,int l,double *x,double *y,i_color *val) {
898 bzcoef=mymalloc(sizeof(double)*l);
899 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
903 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
905 for(t=0;t<=1;t+=0.005) {
910 /* cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k);
911 cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
913 cx+=bzcoef[k]*x[k]*ccoef;
914 cy+=bzcoef[k]*y[k]*ccoef;
917 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
919 i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val, 1);
921 /* i_ppix(im,(int)(0.5+cx),(int)(0.5+cy),val); */
941 REF: Graphics Gems I. page 282+
945 /* This should be moved into a seperate file? */
947 /* This is the truncation used:
949 a double is multiplied by 16 and then truncated.
950 This means that 0 -> 0
951 So a triangle of (0,0) (10,10) (10,0) Will look like it's
952 not filling the (10,10) point nor the (10,0)-(10,10) line segment
957 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
972 struct stack_element {
980 /* create the link data to put push onto the stack */
983 struct stack_element*
984 crdata(int left,int right,int dadl,int dadr,int y, int dir) {
985 struct stack_element *ste;
986 ste = mymalloc(sizeof(struct stack_element));
992 ste->myDirection = dir;
996 /* i_ccomp compares two colors and gives true if they are the same */
999 i_ccomp(i_color *val1,i_color *val2,int ch) {
1001 for(i=0;i<ch;i++) if (val1->channel[i] !=val2->channel[i]) return 0;
1007 i_lspan(i_img *im, int seedx, int seedy, i_color *val) {
1010 if (seedx-1 < 0) break;
1011 i_gpix(im,seedx-1,seedy,&cval);
1012 if (!i_ccomp(val,&cval,im->channels)) break;
1019 i_rspan(i_img *im, int seedx, int seedy, i_color *val) {
1022 if (seedx+1 > im->xsize-1) break;
1023 i_gpix(im,seedx+1,seedy,&cval);
1024 if (!i_ccomp(val,&cval,im->channels)) break;
1030 /* Macro to create a link and push on to the list */
1032 #define ST_PUSH(left,right,dadl,dadr,y,dir) do { \
1033 struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); \
1034 llist_push(st,&s); \
1037 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1038 /* No overflow check! */
1040 #define ST_POP() do { \
1041 struct stack_element *s; \
1048 direction = s->myDirection; \
1052 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
1053 int pushrx = rx+1; \
1054 int pushlx = lx-1; \
1055 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
1057 ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); \
1058 if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); \
1061 #define SET(x,y) btm_set(btm,x,y)
1063 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1064 #define INSIDE(x,y) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),i_ccomp(&val,&cval,channels) ) ))
1068 /* The function that does all the real work */
1070 static struct i_bitmap *
1071 i_flood_fill_low(i_img *im,int seedx,int seedy,
1072 int *bxminp, int *bxmaxp, int *byminp, int *bymaxp) {
1090 struct i_bitmap *btm;
1092 int channels,xsize,ysize;
1095 channels = im->channels;
1099 btm = btm_new(xsize, ysize);
1100 st = llist_new(100, sizeof(struct stack_element*));
1102 /* Get the reference color */
1103 i_gpix(im, seedx, seedy, &val);
1105 /* Find the starting span and fill it */
1106 ltx = i_lspan(im, seedx, seedy, &val);
1107 rtx = i_rspan(im, seedx, seedy, &val);
1108 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1110 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1111 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1114 /* Stack variables */
1123 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1126 if (y<0 || y>ysize-1) continue;
1127 if (bymin > y) bymin=y; /* in the worst case an extra line */
1128 if (bymax < y) bymax=y;
1132 if ( lx >= 0 && (wasIn = INSIDE(lx, y)) ) {
1135 while(INSIDE(lx, y) && lx > 0) {
1141 if (bxmin > lx) bxmin = lx;
1142 while(x <= xsize-1) {
1143 /* printf("x=%d\n",x); */
1147 /* case 1: was inside, am still inside */
1150 /* case 2: was inside, am no longer inside: just found the
1151 right edge of a span */
1152 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1154 if (bxmax < x) bxmax = x;
1158 if (x > rx) goto EXT;
1161 /* case 3: Wasn't inside, am now: just found the start of a new run */
1165 /* case 4: Wasn't inside, still isn't */
1170 EXT: /* out of loop */
1172 /* hit an edge of the frame buffer while inside a run */
1173 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1174 if (bxmax < x) bxmax = x;
1192 i_flood_fill(i_img *im, int seedx, int seedy, i_color *dcol) {
1193 int bxmin, bxmax, bymin, bymax;
1194 struct i_bitmap *btm;
1198 if (seedx < 0 || seedx >= im->xsize ||
1199 seedy < 0 || seedy >= im->ysize) {
1200 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1204 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
1206 for(y=bymin;y<=bymax;y++)
1207 for(x=bxmin;x<=bxmax;x++)
1208 if (btm_test(btm,x,y))
1209 i_ppix(im,x,y,dcol);
1217 i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
1218 int bxmin, bxmax, bymin, bymax;
1219 struct i_bitmap *btm;
1225 if (seedx < 0 || seedx >= im->xsize ||
1226 seedy < 0 || seedy >= im->ysize) {
1227 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1231 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
1233 if (im->bits == i_8_bits && fill->fill_with_color) {
1234 i_color *line = mymalloc(sizeof(i_color) * (bxmax - bxmin));
1235 i_color *work = NULL;
1237 work = mymalloc(sizeof(i_color) * (bxmax - bxmin));
1239 for(y=bymin; y<=bymax; y++) {
1242 while (x < bxmax && !btm_test(btm, x, y)) {
1245 if (btm_test(btm, x, y)) {
1247 while (x < bxmax && btm_test(btm, x, y)) {
1250 if (fill->combine) {
1251 i_glin(im, start, x, y, line);
1252 (fill->fill_with_color)(fill, start, y, x-start, im->channels,
1254 (fill->combine)(line, work, im->channels, x-start);
1257 (fill->fill_with_color)(fill, start, y, x-start, im->channels,
1260 i_plin(im, start, x, y, line);
1269 i_fcolor *line = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin));
1270 i_fcolor *work = NULL;
1272 work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin));
1274 for(y=bymin;y<=bymax;y++) {
1277 while (x < bxmax && !btm_test(btm, x, y)) {
1280 if (btm_test(btm, x, y)) {
1282 while (x < bxmax && btm_test(btm, x, y)) {
1285 if (fill->combinef) {
1286 i_glinf(im, start, x, y, line);
1287 (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels,
1289 (fill->combinef)(line, work, im->channels, x-start);
1292 (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels,
1295 i_plinf(im, start, x, y, line);