9 i_mmarray_cr(i_mmarray *ar,int l) {
14 alloc_size = sizeof(minmax) * l;
15 /* check for overflow */
16 if (alloc_size / l != sizeof(minmax)) {
17 fprintf(stderr, "overflow calculating memory allocation");
20 ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
21 for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
25 i_mmarray_dst(i_mmarray *ar) {
27 if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
31 i_mmarray_add(i_mmarray *ar,int x,int y) {
32 if (y>-1 && y<ar->lines)
34 if (x<ar->data[y].min) ar->data[y].min=x;
35 if (x>ar->data[y].max) ar->data[y].max=x;
40 i_mmarray_gmin(i_mmarray *ar,int y) {
41 if (y>-1 && y<ar->lines) return ar->data[y].min;
46 i_mmarray_getm(i_mmarray *ar,int y) {
47 if (y>-1 && y<ar->lines) return ar->data[y].max;
52 i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
54 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);
58 i_mmarray_render_fill(i_img *im,i_mmarray *ar,i_fill_t *fill) {
60 if (im->bits == i_8_bits && fill->fill_with_color) {
61 i_color *line = mymalloc(sizeof(i_color) * im->xsize); /* checked 5jul05 tonyc */
64 work = mymalloc(sizeof(i_color) * im->xsize); /* checked 5jul05 tonyc */
65 for(y=0;y<ar->lines;y++) {
66 if (ar->data[y].max!=-1) {
68 w = ar->data[y].max-ar->data[y].min;
71 i_glin(im, x, x+w, y, line);
72 (fill->fill_with_color)(fill, x, y, w, im->channels, work);
73 (fill->combine)(line, work, im->channels, w);
76 (fill->fill_with_color)(fill, x, y, w, im->channels, line);
78 i_plin(im, x, x+w, y, line);
87 i_fcolor *line = mymalloc(sizeof(i_fcolor) * im->xsize); /* checked 5jul05 tonyc */
88 i_fcolor *work = NULL;
90 work = mymalloc(sizeof(i_fcolor) * im->xsize); /* checked 5jul05 tonyc */
91 for(y=0;y<ar->lines;y++) {
92 if (ar->data[y].max!=-1) {
94 w = ar->data[y].max-ar->data[y].min;
97 i_glinf(im, x, x+w, y, line);
98 (fill->fill_with_fcolor)(fill, x, y, w, im->channels, work);
99 (fill->combinef)(line, work, im->channels, w);
102 (fill->fill_with_fcolor)(fill, x, y, w, im->channels, line);
104 i_plinf(im, x, x+w, y, line);
117 i_arcdraw(int x1, int y1, int x2, int y2, i_mmarray *ar) {
121 alpha=(double)(y2-y1)/(double)(x2-x1);
122 if (fabs(alpha) <= 1)
124 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
128 i_mmarray_add(ar,x1,(int)(dsec+0.5));
136 if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
140 i_mmarray_add(ar,(int)(dsec+0.5),y1);
148 i_mmarray_info(i_mmarray *ar) {
150 for(i=0;i<ar->lines;i++)
151 if (ar->data[i].max!=-1) printf("line %d: min=%d, max=%d.\n",i,ar->data[i].min,ar->data[i].max);
155 i_arc_minmax(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
160 /*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));*/
162 i_mmarray_cr(&dot, hlines->limit_y);
164 x1=(int)(x+0.5+rad*cos(d1*PI/180.0));
165 y1=(int)(y+0.5+rad*sin(d1*PI/180.0));
166 fx=(float)x1; fy=(float)y1;
168 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
169 i_arcdraw(x, y, x1, y1, &dot);
171 x1=(int)(x+0.5+rad*cos(d2*PI/180.0));
172 y1=(int)(y+0.5+rad*sin(d2*PI/180.0));
174 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)));
176 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
177 i_arcdraw(x, y, x1, y1, &dot);
179 /* render the minmax values onto the hlines */
180 for (y = 0; y < dot.lines; y++) {
181 if (dot.data[y].max!=-1) {
183 minx = dot.data[y].min;
184 width = dot.data[y].max - dot.data[y].min + 1;
185 i_int_hlines_add(hlines, y, minx, width);
194 i_arc_hlines(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
196 i_arc_minmax(hlines, x, y, rad, d1, d2);
199 i_arc_minmax(hlines, x, y, rad, d1, 360);
200 i_arc_minmax(hlines, x, y, rad, 0, d2);
205 i_arc(i_img *im,int x,int y,float rad,float d1,float d2,i_color *val) {
208 i_int_init_hlines_img(&hlines, im);
210 i_arc_hlines(&hlines, x, y, rad, d1, d2);
212 i_int_hlines_fill_color(im, &hlines, val);
214 i_int_hlines_destroy(&hlines);
217 #define MIN_CIRCLE_STEPS 8
218 #define MAX_CIRCLE_STEPS 360
221 i_arc_cfill(i_img *im,int x,int y,float rad,float d1,float d2,i_fill_t *fill) {
224 i_int_init_hlines_img(&hlines, im);
226 i_arc_hlines(&hlines, x, y, rad, d1, d2);
228 i_int_hlines_fill_fill(im, &hlines, fill);
230 i_int_hlines_destroy(&hlines);
234 arc_poly(int *count, double **xvals, double **yvals,
235 double x, double y, double rad, double d1, double d2) {
236 double d1_rad, d2_rad;
238 int steps, point_count;
241 /* normalize the angles */
244 if (d2 >= 360) { /* default is 361 */
258 d1_rad = d1 * PI / 180;
259 d2_rad = d2 * PI / 180;
261 /* how many segments for the curved part?
262 we do a maximum of one per degree, with a minimum of 8/circle
263 we try to aim at having about one segment per 2 pixels
264 Work it out per circle to get a step size.
266 I was originally making steps = circum/2 but that looked horrible.
268 I think there might be an issue in the polygon filler.
270 circum = 2 * PI * rad;
272 if (steps > MAX_CIRCLE_STEPS)
273 steps = MAX_CIRCLE_STEPS;
274 else if (steps < MIN_CIRCLE_STEPS)
275 steps = MIN_CIRCLE_STEPS;
277 angle_inc = 2 * PI / steps;
279 point_count = steps + 5; /* rough */
280 *xvals = mymalloc(point_count * sizeof(double));
281 *yvals = mymalloc(point_count * sizeof(double));
283 /* from centre to edge at d1 */
286 (*xvals)[1] = x + rad * cos(d1_rad);
287 (*yvals)[1] = y + rad * sin(d1_rad);
290 /* step around the curve */
291 while (d1_rad < d2_rad) {
292 (*xvals)[*count] = x + rad * cos(d1_rad);
293 (*yvals)[*count] = y + rad * sin(d1_rad);
298 /* finish off the curve */
299 (*xvals)[*count] = x + rad * cos(d2_rad);
300 (*yvals)[*count] = y + rad * sin(d2_rad);
305 i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
307 double *xvals, *yvals;
310 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
312 i_poly_aa(im, count, xvals, yvals, val);
319 i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
321 double *xvals, *yvals;
324 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
326 i_poly_aa_cfill(im, count, xvals, yvals, fill);
332 /* Temporary AA HACK */
336 static frac float_to_frac(float x) { return (frac)(0.5+x*16.0); }
337 static int frac_sub (frac x) { return (x%16); }
338 static int frac_int (frac x) { return (x/16); }
339 static float frac_to_float(float x) { return (float)x/16.0; }
343 polar_to_plane(float cx, float cy, float angle, float radius, frac *x, frac *y) {
344 *x = float_to_frac(cx+radius*cos(angle));
345 *y = float_to_frac(cy+radius*sin(angle));
350 order_pair(frac *x, frac *y) {
363 make_minmax_list(i_mmarray *dot, float x, float y, float radius) {
365 float astep = radius>0.1 ? .5/radius : 10;
366 frac cx, cy, lx, ly, sx, sy;
368 mm_log((1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
370 polar_to_plane(x, y, angle, radius, &sx, &sy);
372 for(angle = 0.0; angle<361; angle +=astep) {
374 polar_to_plane(x, y, angle, radius, &cx, &cy);
377 if (fabs(cx-lx) > fabs(cy-ly)) {
380 ccx = lx; lx = cx; cx = ccx;
381 ccy = ly; ly = cy; cy = ccy;
384 for(ccx=lx; ccx<=cx; ccx++) {
385 ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
386 i_mmarray_add(dot, ccx, ccy);
392 ccy = ly; ly = cy; cy = ccy;
393 ccx = lx; lx = cx; cx = ccx;
396 for(ccy=ly; ccy<=cy; ccy++) {
397 if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
398 i_mmarray_add(dot, ccx, ccy);
404 /* Get the number of subpixels covered */
408 i_pixel_coverage(i_mmarray *dot, int x, int y) {
414 for(cy=y*16; cy<(y+1)*16; cy++) {
415 frac tmin = dot->data[cy].min;
416 frac tmax = dot->data[cy].max;
418 if (tmax == -1 || tmin > maxx || tmax < minx) continue;
420 if (tmin < minx) tmin = minx;
421 if (tmax > maxx) tmax = maxx;
429 i_circle_aa(i_img *im, float x, float y, float rad, i_color *val) {
434 mm_log((1, "i_circle_aa(im %p, x %d, y %d, rad %.2f, val %p)\n", im, x, y, rad, val));
436 i_mmarray_cr(&dot,16*im->ysize);
437 make_minmax_list(&dot, x, y, rad);
439 for(ly = 0; ly<im->ysize; ly++) {
440 int ix, cy, minx = INT_MAX, maxx = INT_MIN;
442 /* Find the left/rightmost set subpixels */
443 for(cy = 0; cy<16; cy++) {
444 frac tmin = dot.data[ly*16+cy].min;
445 frac tmax = dot.data[ly*16+cy].max;
446 if (tmax == -1) continue;
448 if (minx > tmin) minx = tmin;
449 if (maxx < tmax) maxx = tmax;
452 if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
456 for(ix=minx; ix<=maxx; ix++) {
457 int cnt = i_pixel_coverage(&dot, ix, ly);
458 if (cnt>255) cnt = 255;
459 if (cnt) { /* should never be true */
461 float ratio = (float)cnt/255.0;
462 i_gpix(im, ix, ly, &temp);
463 for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
464 i_ppix(im, ix, ly, &temp);
477 i_box(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
479 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));
480 for(x=x1;x<x2+1;x++) {
484 for(y=y1;y<y2+1;y++) {
491 i_box_filled(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
493 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));
494 for(x=x1;x<x2+1;x++) for (y=y1;y<y2+1;y++) i_ppix(im,x,y,val);
498 i_box_cfill(i_img *im,int x1,int y1,int x2,int y2,i_fill_t *fill) {
499 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));
510 if (x1 >= x2 || y1 > y2)
512 if (im->bits == i_8_bits && fill->fill_with_color) {
513 i_color *line = mymalloc(sizeof(i_color) * (x2 - x1)); /* checked 5jul05 tonyc */
514 i_color *work = NULL;
516 work = mymalloc(sizeof(i_color) * (x2-x1)); /* checked 5jul05 tonyc */
519 i_glin(im, x1, x2, y1, line);
520 (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, work);
521 (fill->combine)(line, work, im->channels, x2-x1);
524 (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, line);
526 i_plin(im, x1, x2, y1, line);
534 i_fcolor *line = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
536 work = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
540 i_glinf(im, x1, x2, y1, line);
541 (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, work);
542 (fill->combinef)(line, work, im->channels, x2-x1);
545 (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, line);
547 i_plinf(im, x1, x2, y1, line);
558 =item i_line(im, x1, y1, x2, y2, val, endp)
560 Draw a line to image using bresenhams linedrawing algorithm
562 im - image to draw to
563 x1 - starting x coordinate
564 y1 - starting x coordinate
565 x2 - starting x coordinate
566 y2 - starting x coordinate
567 val - color to write to image
568 endp - endpoint flag (boolean)
574 i_line(i_img *im, int x1, int y1, int x2, int y2, i_color *val, int endp) {
583 /* choose variable to iterate on */
584 if (abs(dx)>abs(dy)) {
590 t = x1; x1 = x2; x2 = t;
591 t = y1; y1 = y2; y2 = t;
609 for(x=x1; x<x2-1; x++) {
616 i_ppix(im, x+1, y, val);
624 t = x1; x1 = x2; x2 = t;
625 t = y1; y1 = y2; y2 = t;
643 for(y=y1; y<y2-1; y++) {
650 i_ppix(im, x, y+1, val);
654 i_ppix(im, x1, y1, val);
655 i_ppix(im, x2, y2, val);
657 if (x1 != x2 || y1 != y2)
658 i_ppix(im, x1, y1, val);
664 i_line_dda(i_img *im, int x1, int y1, int x2, int y2, i_color *val) {
669 for(x=x1; x<=x2; x++) {
670 dy = y1+ (x-x1)/(float)(x2-x1)*(y2-y1);
671 i_ppix(im, x, (int)(dy+0.5), val);
702 i_line_aa3(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
706 int temp,dx,dy,isec,ch;
708 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));
713 if (abs(dx)>abs(dy)) { /* alpha < 1 */
714 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
715 alpha=(float)(y2-y1)/(float)(x2-x1);
721 /* dfrac=1-(1-dfrac)*(1-dfrac); */
722 /* This is something we can play with to try to get better looking lines */
724 i_gpix(im,x1,isec,&tval);
725 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
726 i_ppix(im,x1,isec,&tval);
728 i_gpix(im,x1,isec+1,&tval);
729 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
730 i_ppix(im,x1,isec+1,&tval);
736 if (y2<y1) { temp=y1; y1=y2; y2=temp; temp=x1; x1=x2; x2=temp; }
737 alpha=(float)(x2-x1)/(float)(y2-y1);
742 /* dfrac=sqrt(dfrac); */
743 /* This is something we can play with */
744 i_gpix(im,isec,y1,&tval);
745 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
746 i_ppix(im,isec,y1,&tval);
748 i_gpix(im,isec+1,y1,&tval);
749 for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
750 i_ppix(im,isec+1,y1,&tval);
762 i_line_aa(i_img *im, int x1, int y1, int x2, int y2, i_color *val, int endp) {
770 /* choose variable to iterate on */
771 if (abs(dx)>abs(dy)) {
777 t = x1; x1 = x2; x2 = t;
778 t = y1; y1 = y2; y2 = t;
792 p = dy2 - dx2; /* this has to be like this for AA */
796 for(x=x1; x<x2-1; x++) {
799 float t = (dy) ? -(float)(p)/(float)(dx2) : 1;
806 i_gpix(im,x+1,y,&tval);
807 for(ch=0;ch<im->channels;ch++)
808 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
809 i_ppix(im,x+1,y,&tval);
811 i_gpix(im,x+1,y+cpy,&tval);
812 for(ch=0;ch<im->channels;ch++)
813 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
814 i_ppix(im,x+1,y+cpy,&tval);
829 t = x1; x1 = x2; x2 = t;
830 t = y1; y1 = y2; y2 = t;
844 p = dx2 - dy2; /* this has to be like this for AA */
848 for(y=y1; y<y2-1; y++) {
851 float t = (dx) ? -(float)(p)/(float)(dy2) : 1;
858 i_gpix(im,x,y+1,&tval);
859 for(ch=0;ch<im->channels;ch++)
860 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
861 i_ppix(im,x,y+1,&tval);
863 i_gpix(im,x+cpx,y+1,&tval);
864 for(ch=0;ch<im->channels;ch++)
865 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
866 i_ppix(im,x+cpx,y+1,&tval);
879 i_ppix(im, x1, y1, val);
880 i_ppix(im, x2, y2, val);
882 if (x1 != x2 || y1 != y2)
883 i_ppix(im, x1, y1, val);
894 for(i=k+1;i<=n;i++) r*=i;
895 for(i=1;i<=(n-k);i++) r/=i;
900 /* Note in calculating t^k*(1-t)^(n-k)
901 we can start by using t^0=1 so this simplifies to
902 t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
903 to get a new level - this may lead to errors who knows lets test it */
906 i_bezier_multi(i_img *im,int l,double *x,double *y,i_color *val) {
914 /* this is the same size as the x and y arrays, so shouldn't overflow */
915 bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
916 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
920 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
922 for(t=0;t<=1;t+=0.005) {
927 /* cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k);
928 cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
930 cx+=bzcoef[k]*x[k]*ccoef;
931 cy+=bzcoef[k]*y[k]*ccoef;
934 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
936 i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val, 1);
938 /* i_ppix(im,(int)(0.5+cx),(int)(0.5+cy),val); */
948 REF: Graphics Gems I. page 282+
952 /* This should be moved into a seperate file? */
954 /* This is the truncation used:
956 a double is multiplied by 16 and then truncated.
957 This means that 0 -> 0
958 So a triangle of (0,0) (10,10) (10,0) Will look like it's
959 not filling the (10,10) point nor the (10,0)-(10,10) line segment
964 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
979 struct stack_element {
987 /* create the link data to put push onto the stack */
990 struct stack_element*
991 crdata(int left,int right,int dadl,int dadr,int y, int dir) {
992 struct stack_element *ste;
993 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
999 ste->myDirection = dir;
1003 /* i_ccomp compares two colors and gives true if they are the same */
1006 i_ccomp(i_color *val1,i_color *val2,int ch) {
1008 for(i=0;i<ch;i++) if (val1->channel[i] !=val2->channel[i]) return 0;
1014 i_lspan(i_img *im, int seedx, int seedy, i_color *val) {
1017 if (seedx-1 < 0) break;
1018 i_gpix(im,seedx-1,seedy,&cval);
1019 if (!i_ccomp(val,&cval,im->channels)) break;
1026 i_rspan(i_img *im, int seedx, int seedy, i_color *val) {
1029 if (seedx+1 > im->xsize-1) break;
1030 i_gpix(im,seedx+1,seedy,&cval);
1031 if (!i_ccomp(val,&cval,im->channels)) break;
1037 /* Macro to create a link and push on to the list */
1039 #define ST_PUSH(left,right,dadl,dadr,y,dir) do { \
1040 struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); \
1041 llist_push(st,&s); \
1044 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1045 /* No overflow check! */
1047 #define ST_POP() do { \
1048 struct stack_element *s; \
1055 direction = s->myDirection; \
1059 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
1060 int pushrx = rx+1; \
1061 int pushlx = lx-1; \
1062 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
1064 ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); \
1065 if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); \
1068 #define SET(x,y) btm_set(btm,x,y)
1070 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1071 #define INSIDE(x,y) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),i_ccomp(&val,&cval,channels) ) ))
1075 /* The function that does all the real work */
1077 static struct i_bitmap *
1078 i_flood_fill_low(i_img *im,int seedx,int seedy,
1079 int *bxminp, int *bxmaxp, int *byminp, int *bymaxp) {
1097 struct i_bitmap *btm;
1099 int channels,xsize,ysize;
1102 channels = im->channels;
1106 btm = btm_new(xsize, ysize);
1107 st = llist_new(100, sizeof(struct stack_element*));
1109 /* Get the reference color */
1110 i_gpix(im, seedx, seedy, &val);
1112 /* Find the starting span and fill it */
1113 ltx = i_lspan(im, seedx, seedy, &val);
1114 rtx = i_rspan(im, seedx, seedy, &val);
1115 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1117 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1118 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1121 /* Stack variables */
1130 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1133 if (y<0 || y>ysize-1) continue;
1134 if (bymin > y) bymin=y; /* in the worst case an extra line */
1135 if (bymax < y) bymax=y;
1139 if ( lx >= 0 && (wasIn = INSIDE(lx, y)) ) {
1142 while(INSIDE(lx, y) && lx > 0) {
1148 if (bxmin > lx) bxmin = lx;
1149 while(x <= xsize-1) {
1150 /* printf("x=%d\n",x); */
1154 /* case 1: was inside, am still inside */
1157 /* case 2: was inside, am no longer inside: just found the
1158 right edge of a span */
1159 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1161 if (bxmax < x) bxmax = x;
1165 if (x > rx) goto EXT;
1168 /* case 3: Wasn't inside, am now: just found the start of a new run */
1172 /* case 4: Wasn't inside, still isn't */
1177 EXT: /* out of loop */
1179 /* hit an edge of the frame buffer while inside a run */
1180 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1181 if (bxmax < x) bxmax = x;
1199 i_flood_fill(i_img *im, int seedx, int seedy, i_color *dcol) {
1200 int bxmin, bxmax, bymin, bymax;
1201 struct i_bitmap *btm;
1205 if (seedx < 0 || seedx >= im->xsize ||
1206 seedy < 0 || seedy >= im->ysize) {
1207 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1211 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
1213 for(y=bymin;y<=bymax;y++)
1214 for(x=bxmin;x<=bxmax;x++)
1215 if (btm_test(btm,x,y))
1216 i_ppix(im,x,y,dcol);
1224 i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
1225 int bxmin, bxmax, bymin, bymax;
1226 struct i_bitmap *btm;
1232 if (seedx < 0 || seedx >= im->xsize ||
1233 seedy < 0 || seedy >= im->ysize) {
1234 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1238 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
1240 if (im->bits == i_8_bits && fill->fill_with_color) {
1241 /* bxmax/bxmin are inside the image, hence this won't overflow */
1242 i_color *line = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1243 i_color *work = NULL;
1245 work = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1247 for(y=bymin; y<=bymax; y++) {
1250 while (x < bxmax && !btm_test(btm, x, y)) {
1253 if (btm_test(btm, x, y)) {
1255 while (x < bxmax && btm_test(btm, x, y)) {
1258 if (fill->combine) {
1259 i_glin(im, start, x, y, line);
1260 (fill->fill_with_color)(fill, start, y, x-start, im->channels,
1262 (fill->combine)(line, work, im->channels, x-start);
1265 (fill->fill_with_color)(fill, start, y, x-start, im->channels,
1268 i_plin(im, start, x, y, line);
1277 /* bxmax/bxmin are inside the image, hence this won't overflow */
1278 i_fcolor *line = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1279 i_fcolor *work = NULL;
1281 work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1283 for(y=bymin;y<=bymax;y++) {
1286 while (x < bxmax && !btm_test(btm, x, y)) {
1289 if (btm_test(btm, x, y)) {
1291 while (x < bxmax && btm_test(btm, x, y)) {
1294 if (fill->combinef) {
1295 i_glinf(im, start, x, y, line);
1296 (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels,
1298 (fill->combinef)(line, work, im->channels, x-start);
1301 (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels,
1304 i_plinf(im, start, x, y, line);