]> git.imager.perl.org - imager.git/blobdiff - draw.c
- add samples/tk-photo.pl
[imager.git] / draw.c
diff --git a/draw.c b/draw.c
index ecf0bad352b10f1bdbb655afd512d656d15172b3..05d69c80ce51c64dab09bf58232651139d3f1a38 100644 (file)
--- a/draw.c
+++ b/draw.c
@@ -51,37 +51,55 @@ i_mmarray_render_fill(i_img *im,i_mmarray *ar,i_fill_t *fill) {
   int x, w, y;
   if (im->bits == i_8_bits && fill->fill_with_color) {
     i_color *line = mymalloc(sizeof(i_color) * im->xsize);
+    i_color *work = NULL;
+    if (fill->combine)
+      work = mymalloc(sizeof(i_color) * im->xsize);
     for(y=0;y<ar->lines;y++) {
       if (ar->data[y].max!=-1) {
         x = ar->data[y].min;
         w = ar->data[y].max-ar->data[y].min;
 
-        if (fill->combines) 
+        if (fill->combine) {
           i_glin(im, x, x+w, y, line);
-        
-        (fill->fill_with_color)(fill, x, y, w, im->channels, line);
+          (fill->fill_with_color)(fill, x, y, w, im->channels, work);
+          (fill->combine)(line, work, im->channels, w);
+        }
+        else {
+          (fill->fill_with_color)(fill, x, y, w, im->channels, line);
+        }
         i_plin(im, x, x+w, y, line);
       }
     }
   
     myfree(line);
+    if (work)
+      myfree(work);
   }
   else {
     i_fcolor *line = mymalloc(sizeof(i_fcolor) * im->xsize);
+    i_fcolor *work = NULL;
+    if (fill->combinef)
+      work = mymalloc(sizeof(i_fcolor) * im->xsize);
     for(y=0;y<ar->lines;y++) {
       if (ar->data[y].max!=-1) {
         x = ar->data[y].min;
         w = ar->data[y].max-ar->data[y].min;
 
-        if (fill->combines) 
+        if (fill->combinef) {
           i_glinf(im, x, x+w, y, line);
-        
-        (fill->fill_with_fcolor)(fill, x, y, w, im->channels, line);
+          (fill->fill_with_fcolor)(fill, x, y, w, im->channels, work);
+          (fill->combinef)(line, work, im->channels, w);
+        }
+        else {
+          (fill->fill_with_fcolor)(fill, x, y, w, im->channels, line);
+        }
         i_plinf(im, x, x+w, y, line);
       }
     }
   
     myfree(line);
+    if (work)
+      myfree(work);
   }
 }
 
@@ -153,6 +171,7 @@ i_arc(i_img *im,int x,int y,float rad,float d1,float d2,i_color *val) {
 
   /*  dot.info(); */
   i_mmarray_render(im,&dot,val);
+  i_mmarray_dst(&dot);
 }
 
 void
@@ -182,6 +201,7 @@ i_arc_cfill(i_img *im,int x,int y,float rad,float d1,float d2,i_fill_t *fill) {
 
   /*  dot.info(); */
   i_mmarray_render_fill(im,&dot,fill);
+  i_mmarray_dst(&dot);
 }
 
 
@@ -227,7 +247,6 @@ make_minmax_list(i_mmarray *dot, float x, float y, float radius) {
   polar_to_plane(x, y, angle, radius, &sx, &sy);
   
   for(angle = 0.0; angle<361; angle +=astep) {
-    float alpha;
     lx = sx; ly = sy;
     polar_to_plane(x, y, angle, radius, &cx, &cy);
     sx = cx; sy = cy;
@@ -295,7 +314,7 @@ i_circle_aa(i_img *im, float x, float y, float rad, i_color *val) {
   make_minmax_list(&dot, x, y, rad);
 
   for(ly = 0; ly<im->ysize; ly++) {
-    int ix, cy, cnt = 0, minx = INT_MAX, maxx = INT_MIN;
+    int ix, cy, minx = INT_MAX, maxx = INT_MIN;
 
     /* Find the left/rightmost set subpixels */
     for(cy = 0; cy<16; cy++) {
@@ -323,6 +342,7 @@ i_circle_aa(i_img *im, float x, float y, float rad, i_color *val) {
       }
     }
   }
+  i_mmarray_dst(&dot);
 }
 
 
@@ -358,73 +378,201 @@ i_box_cfill(i_img *im,int x1,int y1,int x2,int y2,i_fill_t *fill) {
   ++x2;
   if (im->bits == i_8_bits && fill->fill_with_color) {
     i_color *line = mymalloc(sizeof(i_color) * (x2 - x1));
+    i_color *work = NULL;
+    if (fill->combine)
+      work = mymalloc(sizeof(i_color) * (x2-x1));
     while (y1 <= y2) {
-      if (fill->combines)
+      if (fill->combine) {
         i_glin(im, x1, x2, y1, line);
-
-      (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, line);
+        (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, work);
+        (fill->combine)(line, work, im->channels, x2-x1);
+      }
+      else {
+        (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, line);
+      }
       i_plin(im, x1, x2, y1, line);
       ++y1;
     }
     myfree(line);
+    if (work)
+      myfree(work);
   }
   else {
     i_fcolor *line = mymalloc(sizeof(i_fcolor) * (x2 - x1));
+    i_fcolor *work;
+    work = mymalloc(sizeof(i_fcolor) * (x2 - x1));
+
     while (y1 <= y2) {
-      if (fill->combines)
+      if (fill->combine) {
         i_glinf(im, x1, x2, y1, line);
-
-      (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, line);
+        (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, work);
+        (fill->combinef)(line, work, im->channels, x2-x1);
+      }
+      else {
+        (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, line);
+      }
       i_plinf(im, x1, x2, y1, line);
       ++y1;
     }
     myfree(line);
+    if (work)
+      myfree(work);
   }
 }
 
+
+/* 
+=item i_line(im, x1, y1, x2, y2, val, endp)
+
+Draw a line to image using bresenhams linedrawing algorithm
+
+   im   - image to draw to
+   x1   - starting x coordinate
+   y1   - starting x coordinate
+   x2   - starting x coordinate
+   y2   - starting x coordinate
+   val  - color to write to image
+   endp - endpoint flag (boolean)
+
+=cut
+*/
+
 void
-i_draw(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
-  double alpha;
-  double dsec;
-  int temp;
+i_line(i_img *im, int x1, int y1, int x2, int y2, i_color *val, int endp) {
+  int x, y;
+  int dx, dy;
+  int p;
 
-  mm_log((1,"i_draw(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,val 0x%x)\n",im,x1,y1,x2,y2,val));
+  dx = x2 - x1;
+  dy = y2 - y1;
 
-  alpha=(double)(y2-y1)/(double)(x2-x1);
-  if (fabs(alpha)<1) 
-    {
-      if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
-      dsec=y1;
-      while(x1<x2)
-       {
-         dsec+=alpha;
-         i_ppix(im,x1,(int)(dsec+0.5),val);
-         x1++;
-       }
+
+  /* choose variable to iterate on */
+  if (abs(dx)>abs(dy)) {
+    int dx2, dy2, cpy;
+
+    /* sort by x */
+    if (x1 > x2) {
+      int t;
+      t = x1; x1 = x2; x2 = t;
+      t = y1; y1 = y2; y2 = t;
     }
-  else
-    {
-      alpha=1/alpha;
-      if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
-      dsec=x1;
-      while(y1<y2)
-       {
-         dsec+=alpha;
-         i_ppix(im,(int)(dsec+0.5),y1,val);
-         y1++;
-       }
+    
+    dx = abs(dx);
+    dx2 = dx*2;
+    dy = y2 - y1;
+
+    if (dy<0) {
+      dy = -dy;
+      cpy = -1;
+    } else {
+      cpy = 1;
     }
-  mm_log((1,"i_draw: alpha=%f.\n",alpha));
+    dy2 = dy*2;
+    p = dy2 - dx;
+
+    
+    y = y1;
+    for(x=x1; x<x2-1; x++) {
+      if (p<0) {
+        p += dy2;
+      } else {
+        y += cpy;
+        p += dy2-dx2;
+      }
+      i_ppix(im, x+1, y, val);
+    }
+  } else {
+    int dy2, dx2, cpx;
+
+    /* sort bx y */
+    if (y1 > y2) {
+      int t;
+      t = x1; x1 = x2; x2 = t;
+      t = y1; y1 = y2; y2 = t;
+    }
+    
+    dy = abs(dy);
+    dx = x2 - x1;
+    dy2 = dy*2;
+
+    if (dx<0) {
+      dx = -dx;
+      cpx = -1;
+    } else {
+      cpx = 1;
+    }
+    dx2 = dx*2;
+    p = dx2 - dy;
+
+    x = x1;
+    
+    for(y=y1; y<y2-1; y++) {
+      if (p<0) {
+        p  += dx2;
+      } else {
+        x += cpx;
+        p += dx2-dy2;
+      }
+      i_ppix(im, x, y+1, val);
+    }
+  }
+  if (endp) {
+    i_ppix(im, x1, y1, val);
+    i_ppix(im, x2, y2, val);
+  } else {
+    if (x1 != x2 || y1 != y2) 
+      i_ppix(im, x1, y1, val);
+  }
 }
 
+
 void
-i_line_aa(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
+i_line_dda(i_img *im, int x1, int y1, int x2, int y2, i_color *val) {
+
+  float dy;
+  int x;
+  
+  for(x=x1; x<=x2; x++) {
+    dy = y1+ (x-x1)/(float)(x2-x1)*(y2-y1);
+    i_ppix(im, x, (int)(dy+0.5), val);
+  }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+void
+i_line_aa3(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
   i_color tval;
   float alpha;
   float dsec,dfrac;
   int temp,dx,dy,isec,ch;
 
-  mm_log((1,"i_draw(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,val 0x%x)\n",im,x1,y1,x2,y2,val));
+  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));
 
   dy=y2-y1;
   dx=x2-x1;
@@ -474,7 +622,138 @@ i_line_aa(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
   }
 }
 
-double
+
+
+
+void
+i_line_aa(i_img *im, int x1, int y1, int x2, int y2, i_color *val, int endp) {
+  int x, y;
+  int dx, dy;
+  int p;
+
+  dx = x2 - x1;
+  dy = y2 - y1;
+
+  /* choose variable to iterate on */
+  if (abs(dx)>abs(dy)) {
+    int dx2, dy2, cpy;
+    
+    /* sort by x */
+    if (x1 > x2) {
+      int t;
+      t = x1; x1 = x2; x2 = t;
+      t = y1; y1 = y2; y2 = t;
+    }
+    
+    dx = abs(dx);
+    dx2 = dx*2;
+    dy = y2 - y1;
+
+    if (dy<0) {
+      dy = -dy;
+      cpy = -1;
+    } else {
+      cpy = 1;
+    }
+    dy2 = dy*2;
+    p = dy2 - dx2; /* this has to be like this for AA */
+    
+    y = y1;
+
+    for(x=x1; x<x2-1; x++) {
+      int ch;
+      i_color tval;
+      float t = (dy) ? -(float)(p)/(float)(dx2) : 1;
+      float t1, t2;
+
+      if (t<0) t = 0;
+      t1 = 1-t;
+      t2 = t;
+
+      i_gpix(im,x+1,y,&tval);
+      for(ch=0;ch<im->channels;ch++)
+       tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
+      i_ppix(im,x+1,y,&tval);
+
+      i_gpix(im,x+1,y+cpy,&tval);
+      for(ch=0;ch<im->channels;ch++)
+       tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
+      i_ppix(im,x+1,y+cpy,&tval);
+
+      if (p<0) {
+        p += dy2;
+      } else {
+        y += cpy;
+        p += dy2-dx2;
+      }
+    }
+  } else {
+    int dy2, dx2, cpx;
+
+    /* sort bx y */
+    if (y1 > y2) {
+      int t;
+      t = x1; x1 = x2; x2 = t;
+      t = y1; y1 = y2; y2 = t;
+    }
+    
+    dy = abs(dy);
+    dx = x2 - x1;
+    dy2 = dy*2;
+
+    if (dx<0) {
+      dx = -dx;
+      cpx = -1;
+    } else {
+      cpx = 1;
+    }
+    dx2 = dx*2;
+    p = dx2 - dy2; /* this has to be like this for AA */
+
+    x = x1;
+    
+    for(y=y1; y<y2-1; y++) {
+      int ch;
+      i_color tval;
+      float t = (dx) ? -(float)(p)/(float)(dy2) : 1;
+      float t1, t2;
+      
+      if (t<0) t = 0;
+      t1 = 1-t;
+      t2 = t;
+
+      i_gpix(im,x,y+1,&tval);
+      for(ch=0;ch<im->channels;ch++)
+       tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
+      i_ppix(im,x,y+1,&tval);
+
+      i_gpix(im,x+cpx,y+1,&tval);
+      for(ch=0;ch<im->channels;ch++)
+       tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
+      i_ppix(im,x+cpx,y+1,&tval);
+
+      if (p<0) {
+        p  += dx2;
+      } else {
+        x += cpx;
+        p += dx2-dy2;
+      }
+    }
+  }
+
+
+  if (endp) {
+    i_ppix(im, x1, y1, val);
+    i_ppix(im, x2, y2, val);
+  } else {
+    if (x1 != x2 || y1 != y2) 
+      i_ppix(im, x1, y1, val);
+  }
+}
+
+
+
+static double
 perm(int n,int k) {
   double r;
   int i;
@@ -521,7 +800,7 @@ i_bezier_multi(i_img *im,int l,double *x,double *y,i_color *val) {
     }
     /*    printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
     if (i++) { 
-      i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val);
+      i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val, 1);
     }
       /*     i_ppix(im,(int)(0.5+cx),(int)(0.5+cy),val); */
     lx=(int)(0.5+cx);
@@ -533,6 +812,14 @@ i_bezier_multi(i_img *im,int l,double *x,double *y,i_color *val) {
 
 
 
+
+
+
+
+
+
+
+
 /* Flood fill 
 
    REF: Graphics Gems I. page 282+
@@ -551,329 +838,6 @@ i_bezier_multi(i_img *im,int l,double *x,double *y,i_color *val) {
 */
 
 
-
-
-#define IMTRUNC(x) ((int)(x*16))
-
-
-/*
-typedef struct {
-  short ms,ls;
-} pcord;
-*/
-
-typedef int pcord;
-
-struct p_point {
-  int n;
-  pcord x,y;
-};
-
-struct p_line  {
-  int n;
-  pcord x1,y1;
-  pcord x2,y2;
-  pcord miny,maxy;
-};
-
-struct p_slice {
-  int n;
-  double x;
-};
-
-int
-p_compy(const struct p_point *p1, const struct p_point *p2) {
-  if (p1->y > p2->y) return 1;
-  if (p1->y < p2->y) return -1;
-  return 0;
-}
-
-int
-p_compx(const struct p_slice *p1, const struct p_slice *p2) {
-  if (p1->x > p2->x) return 1;
-  if (p1->x < p2->x) return -1;
-  return 0;
-}
-
-/* Change this to int? and round right goddamn it! */
-
-double
-p_eval_aty(struct p_line *l,pcord y) {
-  int t;
-  t=l->y2-l->y1;
-  if (t) return ( (y-l->y1)*l->x2 + (l->y2-y)*l->x1 )/t;
-  return (l->x1+l->x2)/2.0;
-}
-
-double
-p_eval_atx(struct p_line *l,pcord x) {
-  int t;
-  t=l->x2-l->x1;
-  if (t) return ( (x-l->x1)*l->y2 + (l->x2-x)*l->y1 )/t;
-  return (l->y1+l->y2)/2.0;
-}
-
-
-/* Algorithm to count the pixels covered by line going through pixel (x,y)
-   in coarse coords.
-*/
-
-/*
-static int
-p_eval_coverage(struct p_line *l, int lc, int x, pcord y1, pcord y2) {
-
-  return 0;
-}
-*/
-
-
-/* Antialiasing polygon algorithm 
-   specs:
-     1. only nice polygons - no crossovers
-     2. 1/16 pixel resolution # previously - floating point co-ordinates
-     3. full antialiasing ( complete spectrum of blends )
-     4. uses hardly any memory
-     5. no subsampling phase
-
-   For each interval we must: 
-     1. find which lines are in it
-     2. order the lines from in increasing x order.
-        since we are assuming no crossovers it is sufficent
-        to check a single point on each line.
-*/
-
-/*
-  Terms:
-  
-    1. Interval:  A vertical segment in which no lines cross nor end.
-    2. Scanline:  A physical line, contains 16 subpixels in the horizontal direction
-    3. Slice:     A start stop line pair.
-  
- */
-
-/* Templine logic: 
-   
-   The variable tempflush describes if there is anything in the templine array or not.
-   
-   if tempflush is 0 then the array is clean.
-   if tempflush is 1 then the array contains a partial filled scanline
-   
- */
-
-/* Rendering of a single start stop pair:
-
-?? REWRITE
-   
-   The rendering is split in three parts
-   1. From the first start pixel to the first stop pixel
-   2. Area from the first end pixel to the last start pixel
-   3. Area from the first end pixel to the last start pixel
-
- */
-
-
-void
-i_poly_aa(i_img *im,int l,double *x,double *y,i_color *val) {
-  int i,k;                     /* Index variables */
-  int clc;                     /* Index of next item on interval linelist */
-  int tx;                      /* Coarse x coord within a scanline */
-  pcord miny,maxy;             /* Min and max values of the current slice in the subcord system */
-  pcord minacy,maxacy;         /* Min and max values of the current scanline bounded by the slice
-                                  in the subcord system */
-  int cscl;                    /* Current scanline */
-  pcord cc;                    /* Current vertical centerpoint of interval */
-  int mt1,mt2;
-  int minsx,minex,maxsx,maxex; /* The horizontal stretches of the lines beloning to the current slice within a scanline */
-  int *templine;               /* Line accumulator */
-
-  struct p_point *pset;                /* List of points in polygon */
-  struct p_line *lset;         /* List of lines in polygon */
-  struct p_slice *tllist;      /* List of slices */
-
-  i_color red,blue,yellow;
-  red.rgb.r=255;
-  red.rgb.g=0;
-  red.rgb.b=0;
-
-  blue.rgb.r=0;
-  blue.rgb.g=0;
-  blue.rgb.b=255;
-
-  yellow.rgb.r=255;
-  yellow.rgb.g=255;
-  yellow.rgb.b=255;
-  
-  if ( (pset=mymalloc(sizeof(struct p_point)*l)) == NULL) { m_fatal(2,"malloc failed\n"); return; }
-  if ( (lset=mymalloc(sizeof(struct p_line)*l)) == NULL) { m_fatal(2,"malloc failed\n"); return; }
-  if ( (tllist=mymalloc(sizeof(struct p_slice)*l)) == NULL) { m_fatal(2,"malloc failed\n"); return; }
-  if ( (templine=mymalloc(sizeof(int)*im->xsize)) == NULL) { m_fatal(2,"malloc failed\n"); return; }
-
-  /* insert the lines into the line list */
-  
-  for(i=0;i<l;i++) {
-    pset[i].n=i;
-    pset[i].x=IMTRUNC(x[i]);
-    pset[i].y=IMTRUNC(y[i]);
-    lset[i].n=i;
-    lset[i].x1=IMTRUNC(x[i]);
-    lset[i].y1=IMTRUNC(y[i]);
-    lset[i].x2=IMTRUNC(x[(i+1)%l]);
-    lset[i].y2=IMTRUNC(y[(i+1)%l]);
-    lset[i].miny=min(lset[i].y1,lset[i].y2);
-    lset[i].maxy=max(lset[i].y1,lset[i].y2);
-  }
-  
-  qsort(pset,l,sizeof(struct p_point),(int(*)(const void *,const void *))p_compy);
-  
-  printf("post point list (sorted in ascending y order)\n");
-  for(i=0;i<l;i++) {
-    printf("%d [ %d ] %d %d\n",i,pset[i].n,pset[i].x,pset[i].y);
-  }
-  
-  printf("line list\n");
-  for(i=0;i<l;i++) {
-    printf("%d [ %d ] (%d , %d) -> (%d , %d) yspan ( %d , %d )\n",i,lset[i].n,lset[i].x1,lset[i].y1,lset[i].x2,lset[i].y2,lset[i].miny,lset[i].maxy);
-  }
-
-  printf("MAIN LOOP\n\n");
-
-  /* Zero templine buffer */
-  /* Templine buffer flushed everytime a scan line ends */
-  for(i=0;i<im->xsize;i++) templine[i]=0;
-  
-
-  /* loop on intervals */
-  for(i=0;i<l-1;i++) {
-    cc=(pset[i].y+pset[i+1].y)/2;
-    printf("current slice is: %d to %d ( cpoint %d )\n",pset[i].y,pset[i+1].y,cc);
-    clc=0;
-
-    /* stuff this in a function ?? */
-
-    /* Check what lines belong to interval */
-    for(k=0;k<l;k++) {
-      printf("checking line: %d [ %d ] (%d , %d) -> (%d, %d) yspan ( %d , %d )",
-            k,lset[k].n,lset[k].x1,lset[k].y1,lset[k].x2,lset[k].y2,lset[k].miny,lset[k].maxy);
-      if (cc >= lset[k].miny && cc <=  lset[k].maxy) {
-       if (lset[k].miny == lset[k].maxy) printf(" HORIZONTAL - skipped\n");
-       else {
-         printf(" INSIDE\n"); 
-         tllist[clc].x=p_eval_aty(&lset[k],cc);
-         tllist[clc++].n=k;
-       }
-      } else printf(" OUTSIDE\n");
-    }
-    
-    /* 
-       at this point a table of pixels that need special care should
-       be generated from the line list - it should be ordered so that only
-       one needs to be checked - options: rendering to a list then order - or
-       rendering in the right order might be possible to do nicely with the
-       following heuristic:
-       
-       1. Draw leftmost pixel for this line
-       2. If preceeding pixel was occupied check next one else go to 1 again.
-    */
-    
-    printf("lines in current interval:");
-    for(k=0;k<clc;k++) printf("  %d (%.2f)",tllist[k].n,tllist[k].x);
-    printf("\n");
-    
-    /* evaluate the lines in the middle of the slice */
-    
-    printf("Sort lines left to right within interval\n");
-    qsort(tllist,clc,sizeof(struct p_slice),(int(*)(const void *,const void *))p_compx);
-    
-    printf("sorted lines in interval - output:");
-    for(k=0;k<clc;k++) printf(" %d",tllist[k].n);
-    printf("\n");
-    
-    miny=pset[i].y;
-    maxy=pset[i+1].y;
-
-    /* iterate over scanlines */
-    for(cscl=(miny)/16;cscl<=maxy/16;cscl++) {
-      minacy=max(miny,cscl*16);
-      maxacy=min(maxy,cscl*16+15);
-      
-      printf("Scanline bound %d - %d\n",minacy, maxacy);
-
-      /* iterate over line pairs (slices) within interval */
-      for(k=0;k<clc-1;k+=2) {
-
-       mt1=p_eval_aty(&lset[tllist[k].n],minacy);      /* upper corner */
-       mt2=p_eval_aty(&lset[tllist[k].n],maxacy);      /* lower corner */
-       minsx=min(mt1,mt2);
-       minex=max(mt1,mt2);
-       mt1=p_eval_aty(&lset[tllist[k+1].n],minacy);    /* upper corner */
-       mt2=p_eval_aty(&lset[tllist[k+1].n],maxacy);    /* lower corner */
-       maxsx=min(mt1,mt2);
-       maxex=max(mt1,mt2);
-
-       printf("minsx: %d minex: %d\n",minsx,minex);
-       printf("maxsx: %d maxex: %d\n",maxsx,maxex);
-       
-       if (minex/16<maxsx/16) printf("Scan slice is simple!\n");
-       else printf("Scan slice is complicated!\n");
-       
-       if (minsx/16 == minex/16) { /* The line starts and ends in the same pixel */
-         printf("Low slant start pixel\n");
-         templine[minsx/16]=(maxacy-minacy+1)*(minex-minsx+1)/2+((minex | 0xF)-minex)*(maxacy-minacy+1);
-       } else {
-         for(tx=minsx/16;tx<minex/16+1;tx++) {
-           int minx,maxx,minxy,maxxy;
-           minx=max(minsx,  tx*16   );
-           maxx=min(minex,  tx*16+15);
-
-           if (minx == maxx) {
-             templine[tx]=(maxacy-minacy+1);
-           } else {
-           
-             minxy=p_eval_atx(&lset[tllist[k].n], minx);
-             maxxy=p_eval_atx(&lset[tllist[k].n], maxx);
-             
-             templine[tx]+=(abs(minxy-maxxy)+1)*(minex-minsx+1)/2; /* The triangle between the points */
-             if (mt1 < mt2) { /* \ slant */
-               /* ((minex | 0xF)-minex)*(maxacy-minacy+1); FIXME: unfinished */
-               
-
-
-             } else {
-               templine[tx]+=((minex | 0xF)-minex)*(maxacy-minacy+1);
-             }
-             
-           }
-         }
-       }
-
-       for(tx=maxsx/16;tx<maxex/16+1;tx++) templine[tx]+=16*(maxacy-minacy+1);
-       
-       /*      for(tx=minex/16+1;tx<maxsx/16;tx++) 0; */
-       
-       
-       printf("line %d: painting  %d - %d\n",cscl,minex/16+1,maxsx/16);
-       if ( (minacy != cscl*16) || (maxacy != cscl*16+15) ) {
-         for(tx=minsx/16;tx<maxex/16+1;tx++) {
-           i_ppix(im,tx,cscl,&yellow);
-         }
-       }
-       else {
-         for(tx=minsx/16;tx<minex/16+1;tx++) i_ppix(im,tx,cscl,&red);
-         for(tx=maxsx/16;tx<maxex/16+1;tx++) i_ppix(im,tx,cscl,&blue);
-         for(tx=minex/16+1;tx<maxsx/16;tx++) i_ppix(im,tx,cscl,val);
-       }
-
-      } /* Slices */
-    } /* Scanlines */
-  } /* Intervals */
-} /* Function */
-
-
-
-
-
-
-
 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in 
    graphics gems I */
 
@@ -903,13 +867,13 @@ static
 struct stack_element*
 crdata(int left,int right,int dadl,int dadr,int y, int dir) {
   struct stack_element *ste;
-  ste=(struct stack_element*)mymalloc(sizeof(struct stack_element));
-  ste->myLx=left;
-  ste->myRx=right;
-  ste->dadLx=dadl;
-  ste->dadRx=dadr;
-  ste->myY=y;
-  ste->myDirection=dir;
+  ste              = mymalloc(sizeof(struct stack_element));
+  ste->myLx        = left;
+  ste->myRx        = right;
+  ste->dadLx       = dadl;
+  ste->dadRx       = dadr;
+  ste->myY         = y;
+  ste->myDirection = dir;
   return ste;
 }
 
@@ -924,7 +888,7 @@ i_ccomp(i_color *val1,i_color *val2,int ch) {
 
 
 static int
-i_lspan(i_img *im,int seedx,int seedy,i_color *val) {
+i_lspan(i_img *im, int seedx, int seedy, i_color *val) {
   i_color cval;
   while(1) {
     if (seedx-1 < 0) break;
@@ -936,7 +900,7 @@ i_lspan(i_img *im,int seedx,int seedy,i_color *val) {
 }
 
 static int
-i_rspan(i_img *im,int seedx,int seedy,i_color *val) {
+i_rspan(i_img *im, int seedx, int seedy, i_color *val) {
   i_color cval;
   while(1) {
     if (seedx+1 > im->xsize-1) break;
@@ -949,160 +913,62 @@ i_rspan(i_img *im,int seedx,int seedy,i_color *val) {
 
 /* Macro to create a link and push on to the list */
 
-#define ST_PUSH(left,right,dadl,dadr,y,dir) { struct stack_element *s=crdata(left,right,dadl,dadr,y,dir); llist_push(st,&s);}
+#define ST_PUSH(left,right,dadl,dadr,y,dir) do {                 \
+  struct stack_element *s = crdata(left,right,dadl,dadr,y,dir);  \
+  llist_push(st,&s);                                             \
+} while (0)
 
 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
 /* No overflow check! */
  
-#define ST_POP() { struct stack_element *s; llist_pop(st,&s); lx=s->myLx; rx=s->myRx; dadLx=s->dadLx; dadRx=s->dadRx; y=s->myY; direction=s->myDirection; myfree(s); }
-
-#define ST_STACK(dir,dadLx,dadRx,lx,rx,y) { int pushrx=rx+1; int pushlx=lx-1; ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); if (rx > dadRx) ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); }
-
-#define SET(x,y) btm_set(btm,x,y);
-
+#define ST_POP() do {         \
+  struct stack_element *s;    \
+  llist_pop(st,&s);           \
+  lx        = s->myLx;        \
+  rx        = s->myRx;        \
+  dadLx     = s->dadLx;       \
+  dadRx     = s->dadRx;       \
+  y         = s->myY;         \
+  direction = s->myDirection; \
+  myfree(s);                  \
+} while (0)
+
+#define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do {                    \
+  int pushrx = rx+1;                                              \
+  int pushlx = lx-1;                                              \
+  ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir);                         \
+  if (rx > dadRx)                                                 \
+    ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir);                 \
+  if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir);   \
+} while (0)
+
+#define SET(x,y) btm_set(btm,x,y)
+
+/* INSIDE returns true if pixel is correct color and we haven't set it before. */
 #define INSIDE(x,y) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),i_ccomp(&val,&cval,channels)  ) ))
 
-void
-i_flood_fill(i_img *im,int seedx,int seedy,i_color *dcol) {
-
-  int lx,rx;
-  int y;
-  int direction;
-  int dadLx,dadRx;
-
-  int wasIn=0;
-  int x=0;
-
-  /*  int tx,ty; */
-
-  int bxmin=seedx,bxmax=seedx,bymin=seedy,bymax=seedy;
-
-  struct llist *st;
-  struct i_bitmap *btm;
-
-  int channels,xsize,ysize;
-  i_color cval,val;
-
-  channels=im->channels;
-  xsize=im->xsize;
-  ysize=im->ysize;
-
-  btm=btm_new(xsize,ysize);
-  st=llist_new(100,sizeof(struct stack_element*));
-
-  /* Get the reference color */
-  i_gpix(im,seedx,seedy,&val);
-
-  /* Find the starting span and fill it */
-  lx=i_lspan(im,seedx,seedy,&val);
-  rx=i_rspan(im,seedx,seedy,&val);
-  
-  /* printf("span: %d %d \n",lx,rx); */
-
-  for(x=lx;x<=rx;x++) SET(x,seedy);
-
-  ST_PUSH(lx,rx,lx,rx,seedy+1,1);
-  ST_PUSH(lx,rx,lx,rx,seedy-1,-1);
-
-  while(st->count) {
-    ST_POP();
-    
-    if (y<0 || y>ysize-1) continue;
-
-    if (bymin > y) bymin=y; /* in the worst case an extra line */
-    if (bymax < y) bymax=y; 
-
-    /*     printf("start of scan - on stack : %d \n",st->count); */
 
-    
-    /*     printf("lx=%d rx=%d dadLx=%d dadRx=%d y=%d direction=%d\n",lx,rx,dadLx,dadRx,y,direction); */
-    
-    /*
-    printf(" ");
-    for(tx=0;tx<xsize;tx++) printf("%d",tx%10);
-    printf("\n");
-    for(ty=0;ty<ysize;ty++) {
-      printf("%d",ty%10);
-      for(tx=0;tx<xsize;tx++) printf("%d",!!btm_test(btm,tx,ty));
-      printf("\n");
-    }
-
-    printf("y=%d\n",y);
-    */
-
-
-    x=lx+1;
-    if ( (wasIn = INSIDE(lx,y)) ) {
-      SET(lx,y);
-      lx--;
-      while(INSIDE(lx,y) && lx > 0) {
-       SET(lx,y);
-       lx--;
-      }
-    }
-
-    if (bxmin > lx) bxmin=lx;
-    
-    while(x <= xsize-1) {
-      /*  printf("x=%d\n",x); */
-      if (wasIn) {
-       
-       if (INSIDE(x,y)) {
-         /* case 1: was inside, am still inside */
-         SET(x,y);
-       } else {
-         /* case 2: was inside, am no longer inside: just found the
-            right edge of a span */
-         ST_STACK(direction,dadLx,dadRx,lx,(x-1),y);
-
-         if (bxmax < x) bxmax=x;
 
-         wasIn=0;
-       }
-      } else {
-       if (x>rx) goto EXT;
-       if (INSIDE(x,y)) {
-         SET(x,y);
-         /* case 3: Wasn't inside, am now: just found the start of a new run */
-         wasIn=1;
-           lx=x;
-       } else {
-         /* case 4: Wasn't inside, still isn't */
-       }
-      }
-      x++;
-    }
-  EXT: /* out of loop */
-    if (wasIn) {
-      /* hit an edge of the frame buffer while inside a run */
-      ST_STACK(direction,dadLx,dadRx,lx,(x-1),y);
-      if (bxmax < x) bxmax=x;
-    }
-  }
-  
-  /*   printf("lx=%d rx=%d dadLx=%d dadRx=%d y=%d direction=%d\n",lx,rx,dadLx,dadRx,y,direction); 
-       printf("bounding box: [%d,%d] - [%d,%d]\n",bxmin,bymin,bxmax,bymax); */
-
-  for(y=bymin;y<=bymax;y++) for(x=bxmin;x<=bxmax;x++) if (btm_test(btm,x,y)) i_ppix(im,x,y,dcol);
-
-  btm_destroy(btm);
-  llist_destroy(st);
-}
+/* The function that does all the real work */
 
 static struct i_bitmap *
 i_flood_fill_low(i_img *im,int seedx,int seedy,
                  int *bxminp, int *bxmaxp, int *byminp, int *bymaxp) {
-  int lx,rx;
-  int y;
-  int direction;
-  int dadLx,dadRx;
 
-  int wasIn=0;
-  int x=0;
-
-  /*  int tx,ty; */
-
-  int bxmin=seedx,bxmax=seedx,bymin=seedy,bymax=seedy;
+  /*
+    int lx,rx;
+    int y;
+    int direction;
+    int dadLx,dadRx;
+    int wasIn=0;
+  */
+  int ltx, rtx;
+  int tx = 0;
+
+  int bxmin = seedx;
+  int bxmax = seedx;
+  int bymin = seedy;
+  int bymax = seedy;
 
   struct llist *st;
   struct i_bitmap *btm;
@@ -1110,89 +976,75 @@ i_flood_fill_low(i_img *im,int seedx,int seedy,
   int channels,xsize,ysize;
   i_color cval,val;
 
-  channels=im->channels;
-  xsize=im->xsize;
-  ysize=im->ysize;
+  channels = im->channels;
+  xsize    = im->xsize;
+  ysize    = im->ysize;
 
-  btm=btm_new(xsize,ysize);
-  st=llist_new(100,sizeof(struct stack_element*));
+  btm = btm_new(xsize, ysize);
+  st  = llist_new(100, sizeof(struct stack_element*));
 
   /* Get the reference color */
-  i_gpix(im,seedx,seedy,&val);
+  i_gpix(im, seedx, seedy, &val);
 
   /* Find the starting span and fill it */
-  lx=i_lspan(im,seedx,seedy,&val);
-  rx=i_rspan(im,seedx,seedy,&val);
-  
-  /* printf("span: %d %d \n",lx,rx); */
+  ltx = i_lspan(im, seedx, seedy, &val);
+  rtx = i_rspan(im, seedx, seedy, &val);
+  for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
 
-  for(x=lx;x<=rx;x++) SET(x,seedy);
-
-  ST_PUSH(lx,rx,lx,rx,seedy+1,1);
-  ST_PUSH(lx,rx,lx,rx,seedy-1,-1);
+  ST_PUSH(ltx, rtx, ltx, rtx, seedy+1,  1);
+  ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
 
   while(st->count) {
-    ST_POP();
-    
-    if (y<0 || y>ysize-1) continue;
+    /* Stack variables */
+    int lx,rx;
+    int dadLx,dadRx;
+    int y;
+    int direction;
 
-    if (bymin > y) bymin=y; /* in the worst case an extra line */
-    if (bymax < y) bymax=y; 
+    int x;
+    int wasIn=0;
 
-    /*     printf("start of scan - on stack : %d \n",st->count); */
+    ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
 
-    
-    /*     printf("lx=%d rx=%d dadLx=%d dadRx=%d y=%d direction=%d\n",lx,rx,dadLx,dadRx,y,direction); */
-    
-    /*
-    printf(" ");
-    for(tx=0;tx<xsize;tx++) printf("%d",tx%10);
-    printf("\n");
-    for(ty=0;ty<ysize;ty++) {
-      printf("%d",ty%10);
-      for(tx=0;tx<xsize;tx++) printf("%d",!!btm_test(btm,tx,ty));
-      printf("\n");
-    }
 
-    printf("y=%d\n",y);
-    */
+    if (y<0 || y>ysize-1) continue;
+    if (bymin > y) bymin=y; /* in the worst case an extra line */
+    if (bymax < y) bymax=y; 
 
 
-    x=lx+1;
-    if ( (wasIn = INSIDE(lx,y)) ) {
-      SET(lx,y);
+    x = lx+1;
+    if ( lx >= 0 && (wasIn = INSIDE(lx, y)) ) {
+      SET(lx, y);
       lx--;
-      while(INSIDE(lx,y) && lx > 0) {
+      while(INSIDE(lx, y) && lx > 0) {
        SET(lx,y);
        lx--;
       }
     }
 
-    if (bxmin > lx) bxmin=lx;
-    
+    if (bxmin > lx) bxmin = lx;
     while(x <= xsize-1) {
       /*  printf("x=%d\n",x); */
       if (wasIn) {
        
-       if (INSIDE(x,y)) {
+       if (INSIDE(x, y)) {
          /* case 1: was inside, am still inside */
          SET(x,y);
        } else {
          /* case 2: was inside, am no longer inside: just found the
             right edge of a span */
-         ST_STACK(direction,dadLx,dadRx,lx,(x-1),y);
-
-         if (bxmax < x) bxmax=x;
+         ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
 
+         if (bxmax < x) bxmax = x;
          wasIn=0;
        }
       } else {
-       if (x>rx) goto EXT;
-       if (INSIDE(x,y)) {
-         SET(x,y);
+       if (x > rx) goto EXT;
+       if (INSIDE(x, y)) {
+         SET(x, y);
          /* case 3: Wasn't inside, am now: just found the start of a new run */
-         wasIn=1;
-           lx=x;
+         wasIn = 1;
+           lx = x;
        } else {
          /* case 4: Wasn't inside, still isn't */
        }
@@ -1202,13 +1054,10 @@ i_flood_fill_low(i_img *im,int seedx,int seedy,
   EXT: /* out of loop */
     if (wasIn) {
       /* hit an edge of the frame buffer while inside a run */
-      ST_STACK(direction,dadLx,dadRx,lx,(x-1),y);
-      if (bxmax < x) bxmax=x;
+      ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
+      if (bxmax < x) bxmax = x;
     }
   }
-  
-  /*   printf("lx=%d rx=%d dadLx=%d dadRx=%d y=%d direction=%d\n",lx,rx,dadLx,dadRx,y,direction); 
-       printf("bounding box: [%d,%d] - [%d,%d]\n",bxmin,bymin,bxmax,bymax); */
 
   llist_destroy(st);
 
@@ -1220,19 +1069,58 @@ i_flood_fill_low(i_img *im,int seedx,int seedy,
   return btm;
 }
 
-void
+
+
+
+undef_int
+i_flood_fill(i_img *im, int seedx, int seedy, i_color *dcol) {
+  int bxmin, bxmax, bymin, bymax;
+  struct i_bitmap *btm;
+  int x, y;
+
+  i_clear_error();
+  if (seedx < 0 || seedx >= im->xsize ||
+      seedy < 0 || seedy >= im->ysize) {
+    i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
+    return 0;
+  }
+
+  btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
+
+  for(y=bymin;y<=bymax;y++)
+    for(x=bxmin;x<=bxmax;x++)
+      if (btm_test(btm,x,y)) 
+       i_ppix(im,x,y,dcol);
+  btm_destroy(btm);
+  return 1;
+}
+
+
+
+undef_int
 i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
   int bxmin, bxmax, bymin, bymax;
   struct i_bitmap *btm;
   int x, y;
   int start;
 
+  i_clear_error();
+  
+  if (seedx < 0 || seedx >= im->xsize ||
+      seedy < 0 || seedy >= im->ysize) {
+    i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
+    return 0;
+  }
+
   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
 
   if (im->bits == i_8_bits && fill->fill_with_color) {
     i_color *line = mymalloc(sizeof(i_color) * (bxmax - bxmin));
+    i_color *work = NULL;
+    if (fill->combine)
+      work = mymalloc(sizeof(i_color) * (bxmax - bxmin));
 
-    for(y=bymin;y<=bymax;y++) {
+    for(y=bymin; y<=bymax; y++) {
       x = bxmin;
       while (x < bxmax) {
         while (x < bxmax && !btm_test(btm, x, y)) {
@@ -1243,17 +1131,29 @@ i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
           while (x < bxmax && btm_test(btm, x, y)) {
             ++x;
           }
-          if (fill->combines)
+          if (fill->combine) {
             i_glin(im, start, x, y, line);
-          (fill->fill_with_color)(fill, start, y, x-start, im->channels, line);
+            (fill->fill_with_color)(fill, start, y, x-start, im->channels, 
+                                    work);
+            (fill->combine)(line, work, im->channels, x-start);
+          }
+          else {
+            (fill->fill_with_color)(fill, start, y, x-start, im->channels, 
+                                    line);
+          }
           i_plin(im, start, x, y, line);
         }
       }
     }
     myfree(line);
+    if (work)
+      myfree(work);
   }
   else {
     i_fcolor *line = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin));
+    i_fcolor *work = NULL;
+    if (fill->combinef)
+      work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin));
     
     for(y=bymin;y<=bymax;y++) {
       x = bxmin;
@@ -1266,15 +1166,25 @@ i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
           while (x < bxmax && btm_test(btm, x, y)) {
             ++x;
           }
-          if (fill->combines)
+          if (fill->combinef) {
             i_glinf(im, start, x, y, line);
-          (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, line);
+            (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, 
+                                    work);
+            (fill->combinef)(line, work, im->channels, x-start);
+          }
+          else {
+            (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, 
+                                    line);
+          }
           i_plinf(im, start, x, y, line);
         }
       }
     }
     myfree(line);
+    if (work)
+      myfree(work);
   }
 
   btm_destroy(btm);
+  return 1;
 }