-#include "image.h"
+#include "imager.h"
#include "draw.h"
#include "log.h"
+#include "imageri.h"
#include <limits.h>
+static void
+cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
+ int bxmin, int bxmax, int bymin, int bymax);
+
void
i_mmarray_cr(i_mmarray *ar,int l) {
int i;
+ int alloc_size;
ar->lines=l;
- ar->data=mymalloc(sizeof(minmax)*l);
+ alloc_size = sizeof(minmax) * l;
+ /* check for overflow */
+ if (alloc_size / l != sizeof(minmax)) {
+ fprintf(stderr, "overflow calculating memory allocation");
+ exit(3);
+ }
+ ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
}
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 *line = mymalloc(sizeof(i_color) * im->xsize); /* checked 5jul05 tonyc */
i_color *work = NULL;
if (fill->combine)
- work = mymalloc(sizeof(i_color) * im->xsize);
+ work = mymalloc(sizeof(i_color) * im->xsize); /* checked 5jul05 tonyc */
for(y=0;y<ar->lines;y++) {
if (ar->data[y].max!=-1) {
x = ar->data[y].min;
myfree(work);
}
else {
- i_fcolor *line = mymalloc(sizeof(i_fcolor) * im->xsize);
+ i_fcolor *line = mymalloc(sizeof(i_fcolor) * im->xsize); /* checked 5jul05 tonyc */
i_fcolor *work = NULL;
if (fill->combinef)
- work = mymalloc(sizeof(i_fcolor) * im->xsize);
+ work = mymalloc(sizeof(i_fcolor) * im->xsize); /* checked 5jul05 tonyc */
for(y=0;y<ar->lines;y++) {
if (ar->data[y].max!=-1) {
x = ar->data[y].min;
double dsec;
int temp;
alpha=(double)(y2-y1)/(double)(x2-x1);
- if (fabs(alpha)<1)
+ if (fabs(alpha) <= 1)
{
if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
dsec=y1;
- while(x1<x2)
+ while(x1<=x2)
{
- dsec+=alpha;
i_mmarray_add(ar,x1,(int)(dsec+0.5));
+ dsec+=alpha;
x1++;
}
}
alpha=1/alpha;
if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
dsec=x1;
- while(y1<y2)
+ while(y1<=y2)
{
- dsec+=alpha;
i_mmarray_add(ar,(int)(dsec+0.5),y1);
+ dsec+=alpha;
y1++;
}
}
if (ar->data[i].max!=-1) printf("line %d: min=%d, max=%d.\n",i,ar->data[i].min,ar->data[i].max);
}
-
-void
-i_arc(i_img *im,int x,int y,float rad,float d1,float d2,i_color *val) {
+static void
+i_arc_minmax(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
i_mmarray dot;
float f,fx,fy;
int x1,y1;
- 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));
+ /*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));*/
- i_mmarray_cr(&dot,im->ysize);
+ i_mmarray_cr(&dot, hlines->limit_y);
x1=(int)(x+0.5+rad*cos(d1*PI/180.0));
y1=(int)(y+0.5+rad*sin(d1*PI/180.0));
/* printf("x1: %d.\ny1: %d.\n",x1,y1); */
i_arcdraw(x, y, x1, y1, &dot);
+ /* render the minmax values onto the hlines */
+ for (y = 0; y < dot.lines; y++) {
+ if (dot.data[y].max!=-1) {
+ int minx, width;
+ minx = dot.data[y].min;
+ width = dot.data[y].max - dot.data[y].min + 1;
+ i_int_hlines_add(hlines, y, minx, width);
+ }
+ }
+
/* dot.info(); */
- i_mmarray_render(im,&dot,val);
i_mmarray_dst(&dot);
}
+static void
+i_arc_hlines(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
+ if (d1 <= d2) {
+ i_arc_minmax(hlines, x, y, rad, d1, d2);
+ }
+ else {
+ i_arc_minmax(hlines, x, y, rad, d1, 360);
+ i_arc_minmax(hlines, x, y, rad, 0, d2);
+ }
+}
+
+/*
+=item i_arc(im, x, y, rad, d1, d2, color)
+
+=category Drawing
+=synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
+
+Fills an arc centered at (x,y) with radius I<rad> covering the range
+of angles in degrees from d1 to d2, with the color.
+
+=cut
+*/
+
+void
+i_arc(i_img *im,int x,int y,float rad,float d1,float d2,const i_color *val) {
+ i_int_hlines hlines;
+
+ i_int_init_hlines_img(&hlines, im);
+
+ i_arc_hlines(&hlines, x, y, rad, d1, d2);
+
+ i_int_hlines_fill_color(im, &hlines, val);
+
+ i_int_hlines_destroy(&hlines);
+}
+
+/*
+=item i_arc_cfill(im, x, y, rad, d1, d2, fill)
+
+=category Drawing
+=synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
+
+Fills an arc centered at (x,y) with radius I<rad> covering the range
+of angles in degrees from d1 to d2, with the fill object.
+
+=cut
+*/
+
+#define MIN_CIRCLE_STEPS 8
+#define MAX_CIRCLE_STEPS 360
+
void
i_arc_cfill(i_img *im,int x,int y,float rad,float d1,float d2,i_fill_t *fill) {
- i_mmarray dot;
- float f,fx,fy;
- int x1,y1;
+ i_int_hlines hlines;
- mm_log((1,"i_arc_cfill(im* 0x%x,x %d,y %d,rad %.2f,d1 %.2f,d2 %.2f,fill 0x%x)\n",im,x,y,rad,d1,d2,fill));
+ i_int_init_hlines_img(&hlines, im);
- i_mmarray_cr(&dot,im->ysize);
+ i_arc_hlines(&hlines, x, y, rad, d1, d2);
- x1=(int)(x+0.5+rad*cos(d1*PI/180.0));
- y1=(int)(y+0.5+rad*sin(d1*PI/180.0));
- fx=(float)x1; fy=(float)y1;
+ i_int_hlines_fill_fill(im, &hlines, fill);
- /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
- i_arcdraw(x, y, x1, y1, &dot);
+ i_int_hlines_destroy(&hlines);
+}
- x1=(int)(x+0.5+rad*cos(d2*PI/180.0));
- y1=(int)(y+0.5+rad*sin(d2*PI/180.0));
+static void
+arc_poly(int *count, double **xvals, double **yvals,
+ double x, double y, double rad, double d1, double d2) {
+ double d1_rad, d2_rad;
+ double circum;
+ int steps, point_count;
+ double angle_inc;
+
+ /* normalize the angles */
+ d1 = fmod(d1, 360);
+ if (d1 == 0) {
+ if (d2 >= 360) { /* default is 361 */
+ d2 = 360;
+ }
+ else {
+ d2 = fmod(d2, 360);
+ if (d2 < d1)
+ d2 += 360;
+ }
+ }
+ else {
+ d2 = fmod(d2, 360);
+ if (d2 < d1)
+ d2 += 360;
+ }
+ d1_rad = d1 * PI / 180;
+ d2_rad = d2 * PI / 180;
+
+ /* how many segments for the curved part?
+ we do a maximum of one per degree, with a minimum of 8/circle
+ we try to aim at having about one segment per 2 pixels
+ Work it out per circle to get a step size.
+
+ I was originally making steps = circum/2 but that looked horrible.
+
+ I think there might be an issue in the polygon filler.
+ */
+ circum = 2 * PI * rad;
+ steps = circum;
+ if (steps > MAX_CIRCLE_STEPS)
+ steps = MAX_CIRCLE_STEPS;
+ else if (steps < MIN_CIRCLE_STEPS)
+ steps = MIN_CIRCLE_STEPS;
+
+ angle_inc = 2 * PI / steps;
+
+ point_count = steps + 5; /* rough */
+ /* point_count is always relatively small, so allocation won't overflow */
+ *xvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
+ *yvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
+
+ /* from centre to edge at d1 */
+ (*xvals)[0] = x;
+ (*yvals)[0] = y;
+ (*xvals)[1] = x + rad * cos(d1_rad);
+ (*yvals)[1] = y + rad * sin(d1_rad);
+ *count = 2;
+
+ /* step around the curve */
+ while (d1_rad < d2_rad) {
+ (*xvals)[*count] = x + rad * cos(d1_rad);
+ (*yvals)[*count] = y + rad * sin(d1_rad);
+ ++*count;
+ d1_rad += angle_inc;
+ }
- 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)));
-
- /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
- i_arcdraw(x, y, x1, y1, &dot);
+ /* finish off the curve */
+ (*xvals)[*count] = x + rad * cos(d2_rad);
+ (*yvals)[*count] = y + rad * sin(d2_rad);
+ ++*count;
+}
- /* dot.info(); */
- i_mmarray_render_fill(im,&dot,fill);
- i_mmarray_dst(&dot);
+/*
+=item i_arc_aa(im, x, y, rad, d1, d2, color)
+
+=category Drawing
+=synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
+
+Antialias fills an arc centered at (x,y) with radius I<rad> covering
+the range of angles in degrees from d1 to d2, with the color.
+
+=cut
+*/
+
+void
+i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
+ const i_color *val) {
+ double *xvals, *yvals;
+ int count;
+
+ arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
+
+ i_poly_aa(im, count, xvals, yvals, val);
+
+ myfree(xvals);
+ myfree(yvals);
}
+/*
+=item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
+
+=category Drawing
+=synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
+Antialias fills an arc centered at (x,y) with radius I<rad> covering
+the range of angles in degrees from d1 to d2, with the fill object.
+
+=cut
+*/
+
+void
+i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
+ i_fill_t *fill) {
+ double *xvals, *yvals;
+ int count;
+
+ arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
+
+ i_poly_aa_cfill(im, count, xvals, yvals, fill);
+
+ myfree(xvals);
+ myfree(yvals);
+}
/* Temporary AA HACK */
typedef int frac;
static frac float_to_frac(float x) { return (frac)(0.5+x*16.0); }
-static int frac_sub (frac x) { return (x%16); }
-static int frac_int (frac x) { return (x/16); }
-static float frac_to_float(float x) { return (float)x/16.0; }
static
void
*y = float_to_frac(cy+radius*sin(angle));
}
-static
-void
-order_pair(frac *x, frac *y) {
- frac t = *x;
- if (t>*y) {
- *x = *y;
- *y = t;
- }
-}
-
-
-
-
static
void
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;
return cnt;
}
+/*
+=item i_circle_aa(im, x, y, rad, color)
+
+=category Drawing
+=synopsis i_circle_aa(im, 50, 50, 45, &color);
+
+Antialias fills a circle centered at (x,y) for radius I<rad> with
+color.
+
+=cut
+*/
void
-i_circle_aa(i_img *im, float x, float y, float rad, i_color *val) {
+i_circle_aa(i_img *im, float x, float y, float rad, const i_color *val) {
i_mmarray dot;
i_color temp;
int ly;
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++) {
i_mmarray_dst(&dot);
}
+/*
+=item i_box(im, x1, y1, x2, y2, color)
+=category Drawing
+=synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
+Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
-
+=cut
+*/
void
-i_box(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
+i_box(i_img *im,int x1,int y1,int x2,int y2,const i_color *val) {
int x,y;
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));
for(x=x1;x<x2+1;x++) {
}
}
+/*
+=item i_box_filled(im, x1, y1, x2, y2, color)
+
+=category Drawing
+=synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
+
+Fills the box from (x1,y1) to (x2,y2) inclusive with color.
+
+=cut
+*/
+
void
-i_box_filled(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
+i_box_filled(i_img *im,int x1,int y1,int x2,int y2, const i_color *val) {
int x,y;
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));
for(x=x1;x<x2+1;x++) for (y=y1;y<y2+1;y++) i_ppix(im,x,y,val);
}
+/*
+=item i_box_cfill(im, x1, y1, x2, y2, fill)
+
+=category Drawing
+=synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
+
+Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
+
+=cut
+*/
+
void
i_box_cfill(i_img *im,int x1,int y1,int x2,int y2,i_fill_t *fill) {
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));
++x2;
+ if (x1 < 0)
+ x1 = 0;
+ if (y1 < 0)
+ y1 = 0;
+ if (x2 > im->xsize)
+ x2 = im->xsize;
+ if (y2 >= im->ysize)
+ y2 = im->ysize-1;
+ if (x1 >= x2 || y1 > y2)
+ return;
if (im->bits == i_8_bits && fill->fill_with_color) {
- i_color *line = mymalloc(sizeof(i_color) * (x2 - x1));
+ i_color *line = mymalloc(sizeof(i_color) * (x2 - x1)); /* checked 5jul05 tonyc */
i_color *work = NULL;
if (fill->combine)
- work = mymalloc(sizeof(i_color) * (x2-x1));
+ work = mymalloc(sizeof(i_color) * (x2-x1)); /* checked 5jul05 tonyc */
while (y1 <= y2) {
if (fill->combine) {
i_glin(im, x1, x2, y1, line);
myfree(work);
}
else {
- i_fcolor *line = mymalloc(sizeof(i_fcolor) * (x2 - x1));
+ i_fcolor *line = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
i_fcolor *work;
- work = mymalloc(sizeof(i_fcolor) * (x2 - x1));
+ work = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
while (y1 <= y2) {
if (fill->combine) {
}
}
+
+/*
+=item i_line(im, x1, y1, x2, y2, val, endp)
+
+=category Drawing
+
+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, const 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;
+ }
+ 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;
}
- mm_log((1,"i_draw: alpha=%f.\n",alpha));
+
+ 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;
}
}
+
+/*
+=item i_line_aa(im, x1, x2, y1, y2, color, endp)
+
+=category Drawing
+
+Antialias draws a line from (x1,y1) to (x2, y2) in color.
+
+The point (x2, y2) is drawn only if endp is set.
+
+=cut
+*/
+
+void
+i_line_aa(i_img *im, int x1, int y1, int x2, int y2, const 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;
to get a new level - this may lead to errors who knows lets test it */
void
-i_bezier_multi(i_img *im,int l,double *x,double *y,i_color *val) {
+i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
double *bzcoef;
double t,cx,cy;
int k,i;
int n=l-1;
double itr,ccoef;
-
- bzcoef=mymalloc(sizeof(double)*l);
+ /* this is the same size as the x and y arrays, so shouldn't overflow */
+ bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
ICL_info(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);
myfree(bzcoef);
}
-
-
-
-
-
-
-
-
-
-
/* Flood fill
REF: Graphics Gems I. page 282+
struct stack_element*
crdata(int left,int right,int dadl,int dadr,int y, int dir) {
struct stack_element *ste;
- ste = mymalloc(sizeof(struct stack_element));
+ ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
ste->myLx = left;
ste->myRx = right;
ste->dadLx = dadl;
/* i_ccomp compares two colors and gives true if they are the same */
+typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
+
static int
-i_ccomp(i_color *val1,i_color *val2,int ch) {
+i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
int i;
- for(i=0;i<ch;i++) if (val1->channel[i] !=val2->channel[i]) return 0;
+ for(i = 0; i < ch; i++)
+ if (val1->channel[i] !=val2->channel[i])
+ return 0;
return 1;
}
+static int
+i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
+ int i;
+ for(i = 0; i < ch; i++)
+ if (val1->channel[i] !=val2->channel[i])
+ return 1;
+ return 0;
+}
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 const *val, ff_cmpfunc cmpfunc) {
i_color cval;
while(1) {
if (seedx-1 < 0) break;
i_gpix(im,seedx-1,seedy,&cval);
- if (!i_ccomp(val,&cval,im->channels)) break;
+ if (!cmpfunc(val,&cval,im->channels))
+ break;
seedx--;
}
return seedx;
}
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 const *val, ff_cmpfunc cmpfunc) {
i_color cval;
while(1) {
if (seedx+1 > im->xsize-1) break;
i_gpix(im,seedx+1,seedy,&cval);
- if (!i_ccomp(val,&cval,im->channels)) break;
+ if (!cmpfunc(val,&cval,im->channels)) break;
seedx++;
}
return seedx;
/* 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_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) { 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 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);
+#define SET(x,y) btm_set(btm,x,y)
-#define INSIDE(x,y) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),i_ccomp(&val,&cval,channels) ) ))
+/* INSIDE returns true if pixel is correct color and we haven't set it before. */
+#define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&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;
+/* The function that does all the real work */
- /* int tx,ty; */
+static struct i_bitmap *
+i_flood_fill_low(i_img *im,int seedx,int seedy,
+ int *bxminp, int *bxmaxp, int *byminp, int *bymaxp,
+ i_color const *seed, ff_cmpfunc cmpfunc) {
+ int ltx, rtx;
+ int tx = 0;
- int bxmin=seedx,bxmax=seedx,bymin=seedy,bymax=seedy;
+ int bxmin = seedx;
+ int bxmax = seedx;
+ int bymin = seedy;
+ int bymax = seedy;
struct llist *st;
struct i_bitmap *btm;
int channels,xsize,ysize;
- i_color cval,val;
+ i_color cval;
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);
+ btm = btm_new(xsize, ysize);
+ st = llist_new(100, sizeof(struct stack_element*));
/* 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, seed, cmpfunc);
+ rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
+ 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, seed)) ) {
+ SET(lx, y);
lx--;
- while(INSIDE(lx,y) && lx > 0) {
+ while(INSIDE(lx, y, seed) && 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, seed)) {
/* 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, seed)) {
+ 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 */
}
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); */
- 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);
- mm_log((1, "DESTROY\n"));
llist_destroy(st);
+
+ *bxminp = bxmin;
+ *bxmaxp = bxmax;
+ *byminp = bymin;
+ *bymaxp = bymax;
+
+ return btm;
}
-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;
+/*
+=item i_flood_fill(im, seedx, seedy, color)
- int wasIn=0;
- int x=0;
+=category Drawing
+=synopsis i_flood_fill(im, 50, 50, &color);
- /* int tx,ty; */
+Flood fills the 4-connected region starting from the point (seedx,
+seedy) with I<color>.
- int bxmin=seedx,bxmax=seedx,bymin=seedy,bymax=seedy;
+Returns false if (seedx, seedy) are outside the image.
- struct llist *st;
+=cut
+*/
+
+undef_int
+i_flood_fill(i_img *im, int seedx, int seedy, const i_color *dcol) {
+ int bxmin, bxmax, bymin, bymax;
struct i_bitmap *btm;
+ int x, y;
+ i_color val;
- int channels,xsize,ysize;
- i_color cval,val;
+ 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;
+ }
+
+ /* Get the reference color */
+ i_gpix(im, seedx, seedy, &val);
- channels=im->channels;
- xsize=im->xsize;
- ysize=im->ysize;
+ btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
+ &val, i_ccomp_normal);
- btm=btm_new(xsize,ysize);
- st=llist_new(100,sizeof(struct stack_element*));
+ 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;
+}
- /* Get the reference color */
- i_gpix(im,seedx,seedy,&val);
+/*
+=item i_flood_cfill(im, seedx, seedy, fill)
- /* 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); */
+=category Drawing
+=synopsis i_flood_cfill(im, 50, 50, fill);
- for(x=lx;x<=rx;x++) SET(x,seedy);
+Flood fills the 4-connected region starting from the point (seedx,
+seedy) with I<fill>.
- ST_PUSH(lx,rx,lx,rx,seedy+1,1);
- ST_PUSH(lx,rx,lx,rx,seedy-1,-1);
+Returns false if (seedx, seedy) are outside the image.
- while(st->count) {
- ST_POP();
-
- if (y<0 || y>ysize-1) continue;
+=cut
+*/
- if (bymin > y) bymin=y; /* in the worst case an extra line */
- if (bymax < y) bymax=y;
+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;
+ i_color val;
- /* printf("start of scan - on stack : %d \n",st->count); */
+ 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;
+ }
-
- /* 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");
- }
+ /* Get the reference color */
+ i_gpix(im, seedx, seedy, &val);
- printf("y=%d\n",y);
- */
+ btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
+ &val, i_ccomp_normal);
+ cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
- x=lx+1;
- if ( (wasIn = INSIDE(lx,y)) ) {
- SET(lx,y);
- lx--;
- while(INSIDE(lx,y) && lx > 0) {
- SET(lx,y);
- lx--;
- }
- }
+ btm_destroy(btm);
+ return 1;
+}
- 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);
+/*
+=item i_flood_fill_border(im, seedx, seedy, color, border)
- if (bxmax < x) bxmax=x;
+=category Drawing
+=synopsis i_flood_fill_border(im, 50, 50, &color, &border);
- 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); */
+Flood fills the 4-connected region starting from the point (seedx,
+seedy) with I<color>, fill stops when the fill reaches a pixels with
+color I<border>.
- llist_destroy(st);
+Returns false if (seedx, seedy) are outside the image.
- *bxminp = bxmin;
- *bxmaxp = bxmax;
- *byminp = bymin;
- *bymaxp = bymax;
+=cut
+*/
- return btm;
+undef_int
+i_flood_fill_border(i_img *im, int seedx, int seedy, const i_color *dcol,
+ const i_color *border) {
+ 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,
+ border, i_ccomp_border);
+
+ 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;
}
-void
-i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
+/*
+=item i_flood_cfill_border(im, seedx, seedy, fill, border)
+
+=category Drawing
+=synopsis i_flood_cfill_border(im, 50, 50, fill, border);
+
+Flood fills the 4-connected region starting from the point (seedx,
+seedy) with I<fill>, the fill stops when it reaches pixels of color
+I<border>.
+
+Returns false if (seedx, seedy) are outside the image.
+
+=cut
+*/
+
+undef_int
+i_flood_cfill_border(i_img *im, int seedx, int seedy, i_fill_t *fill,
+ const i_color *border) {
int bxmin, bxmax, bymin, bymax;
struct i_bitmap *btm;
+
+ i_clear_error();
+
+ if (seedx < 0 || seedx >= im->xsize ||
+ seedy < 0 || seedy >= im->ysize) {
+ i_push_error(0, "i_flood_cfill_border: Seed pixel outside of image");
+ return 0;
+ }
+
+ btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
+ border, i_ccomp_border);
+
+ cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
+
+ btm_destroy(btm);
+
+ return 1;
+}
+
+static void
+cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
+ int bxmin, int bxmax, int bymin, int bymax) {
int x, y;
int start;
- 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));
+ /* bxmax/bxmin are inside the image, hence this won't overflow */
+ i_color *line = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
i_color *work = NULL;
if (fill->combine)
- work = mymalloc(sizeof(i_color) * (bxmax - bxmin));
+ work = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
- 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)) {
myfree(work);
}
else {
- i_fcolor *line = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin));
+ /* bxmax/bxmin are inside the image, hence this won't overflow */
+ i_fcolor *line = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
i_fcolor *work = NULL;
if (fill->combinef)
- work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin));
+ work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
for(y=bymin;y<=bymax;y++) {
x = bxmin;
if (work)
myfree(work);
}
-
- btm_destroy(btm);
}