X-Git-Url: http://git.imager.perl.org/imager.git/blobdiff_plain/43c5dacbc0c703d1eee26a0eb93adf6db6a1e247..c631b2d57fceb0c61d566f7489d2c8e56f078425:/polygon.c diff --git a/polygon.c b/polygon.c index 27151323..3ea18d7a 100644 --- a/polygon.c +++ b/polygon.c @@ -1,18 +1,23 @@ -#include "image.h" +#include "imager.h" #include "draw.h" #include "log.h" - +#include "imrender.h" +#include "imageri.h" #define IMTRUNC(x) ((int)((x)*16)) #define coarse(x) ((x)/16) #define fine(x) ((x)%16) +/*#define DEBUG_POLY*/ +#ifdef DEBUG_POLY +#define POLY_DEB(x) x +#else #define POLY_DEB(x) +#endif - -typedef int pcord; +typedef i_img_dim pcord; typedef struct { int n; @@ -33,26 +38,11 @@ typedef struct { double x; } p_slice; -typedef struct { - int start; - int stop; -} ss_pair; - typedef struct { int *line; /* temporary buffer for scanline */ - int linelen; /* length of scanline */ - ss_pair *ss_list; /* list of start stop linepairs */ - int ssnext; /* index of the next pair to use */ - int sslen; /* maximum number of start stop pairs */ + i_img_dim linelen; /* length of scanline */ } ss_scanline; - - - - - - - static int p_compy(const p_point *p1, const p_point *p2) { @@ -91,7 +81,7 @@ p_eval_atx(p_line *l, pcord x) { static p_line * -line_set_new(double *x, double *y, int l) { +line_set_new(const double *x, const double *y, int l) { int i; p_line *lset = mymalloc(sizeof(p_line) * l); @@ -101,17 +91,17 @@ line_set_new(double *x, double *y, int l) { 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); - lset[i].minx=min(lset[i].x1,lset[i].x2); - lset[i].maxx=max(lset[i].x1,lset[i].x2); + lset[i].miny=i_min(lset[i].y1,lset[i].y2); + lset[i].maxy=i_max(lset[i].y1,lset[i].y2); + lset[i].minx=i_min(lset[i].x1,lset[i].x2); + lset[i].maxx=i_max(lset[i].x1,lset[i].x2); } return lset; } static p_point * -point_set_new(double *x, double *y, int l) { +point_set_new(const double *x, const double *y, int l) { int i; p_point *pset = mymalloc(sizeof(p_point) * l); @@ -123,28 +113,17 @@ point_set_new(double *x, double *y, int l) { return pset; } -static -void -p_line_dump(p_line *l) { - printf("%d (%d,%d)->(%d,%d) [%d-%d,%d-%d]\n", l->n, l->x1, l->y1, l->x2, l->y2, - l->minx, l->maxx, l->miny, l->maxy); -} - - static void ss_scanline_reset(ss_scanline *ss) { - ss->ssnext = 0; memset(ss->line, 0, sizeof(int) * ss->linelen); } static void -ss_scanline_init(ss_scanline *ss, int linelen, int linepairs) { +ss_scanline_init(ss_scanline *ss, i_img_dim linelen, int linepairs) { ss->line = mymalloc( sizeof(int) * linelen ); ss->linelen = linelen; - ss->ss_list = mymalloc( sizeof(ss_pair) * linepairs ); - ss->sslen = linepairs; ss_scanline_reset(ss); } @@ -152,7 +131,6 @@ static void ss_scanline_exorcise(ss_scanline *ss) { myfree(ss->line); - myfree(ss->ss_list); } @@ -179,26 +157,6 @@ lines_in_interval(p_line *lset, int l, p_slice *tllist, pcord minc, pcord maxc) return count; } -static -int -lines_in_interval_old(p_line *lset, int l, p_slice *tllist, pcord cc) { - int k; - int count = 0; - for(k=0; k= lset[k].miny && cc <= lset[k].maxy) { - if (lset[k].miny == lset[k].maxy) { - POLY_DEB( printf(" HORIZONTAL - skipped\n") ); - } - else { - tllist[count].x=p_eval_aty(&lset[k],cc); - tllist[count].n=k; - count++; - } - } - } - return count; -} - /* marks the up variable for all lines in a slice */ static @@ -265,11 +223,12 @@ typedef void (*scanline_flusher)(i_img *im, ss_scanline *ss, int y, void *ctx); /* This function must be modified later to do proper blending */ -void +static void scanline_flush(i_img *im, ss_scanline *ss, int y, void *ctx) { int x, ch, tv; i_color t; i_color *val = (i_color *)ctx; + POLY_DEB( printf("Flushing line %d\n", y) ); for(x=0; xxsize; x++) { tv = saturate(ss->line[x]); i_gpix(im, x, y, &t); @@ -302,8 +261,12 @@ pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord miny, pcord maxy) { double lycross, rycross; int l, r; - double xs, ys; - + POLY_DEB + ( + printf(" pixel_coverage(..., minx %g, maxx%g, miny %g, maxy %g)\n", + minx/16.0, maxx/16.0, miny/16.0, maxy/16.0) + ); + if (!line->updown) { l = r = 0; } else { @@ -313,9 +276,9 @@ pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord miny, pcord maxy) { r = rycross <= maxy && rycross >= miny; /* true if it enters through left side */ } POLY_DEB( - printf("%4s(%+d): ", line->updown ? line->updown == 1 ? "up" : "down" : "vert", line->updown); - printf("(%2d,%2d) [%3d-%3d, %3d-%3d] lycross=%.2f rycross=%.2f", coarse(minx), coarse(miny), minx, maxx, miny, maxy, lycross, rycross); - printf(" l=%d r=%d\n", l, r) + printf(" %4s(%+d): ", line->updown ? line->updown == 1 ? "up" : "down" : "vert", line->updown); + printf(" (%2d,%2d) [%3d-%3d, %3d-%3d] lycross=%.2f rycross=%.2f", coarse(minx), coarse(miny), minx, maxx, miny, maxy, lycross, rycross); + printf(" l=%d r=%d\n", l, r) ); if (l && r) @@ -338,6 +301,8 @@ pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord miny, pcord maxy) { (maxx-p_eval_aty(line, miny))*(p_eval_atx(line, maxx)-miny)/2.0; return r; } + + return 0; /* silence compiler warning */ } @@ -355,35 +320,42 @@ pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord miny, pcord maxy) { static void -render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r) { +render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r, pcord miny, pcord maxy) { - pcord miny, maxy; /* y bounds in fine coordinates */ pcord lminx, lmaxx; /* left line min/max within y bounds in fine coords */ pcord rminx, rmaxx; /* right line min/max within y bounds in fine coords */ - int cpix; /* x-coordinate of current pixel */ + i_img_dim cpix; /* x-coordinate of current pixel */ int thin; /* boolean for thin/thick segment */ - int startpix; /* temporary variable for "start of this interval" */ - int stoppix; /* temporary variable for "end of this interval" */ - int step2end; /* temporary variable to mark where step2 ends */ + i_img_dim startpix; /* temporary variable for "start of this interval" */ + i_img_dim stoppix; /* temporary variable for "end of this interval" */ /* Find the y bounds of scanline_slice */ - maxy = min( l->maxy, r->maxy ); - miny = max( l->miny, r->miny ); - - maxy = min( maxy, (y+1)*16 ); - miny = max( miny, y*16 ); - - lminx = min( p_eval_aty(l, maxy), p_eval_aty(l, miny) ); - lmaxx = max( p_eval_aty(l, maxy), p_eval_aty(l, miny) ); + POLY_DEB + ( + printf("render_slice_scanline(..., y=%d)\n"); + printf(" left n=%d p1(%.2g, %.2g) p2(%.2g,%.2g) min(%.2g, %.2g) max(%.2g,%.2g) updown(%d)\n", + l->n, l->x1/16.0, l->y1/16.0, l->x2/16.0, l->y2/16.0, + l->minx/16.0, l->miny/16.0, l->maxx/16.0, l->maxy/16.0, + l->updown); + printf(" right n=%d p1(%.2g, %.2g) p2(%.2g,%.2g) min(%.2g, %.2g) max(%.2g,%.2g) updown(%d)\n", + r->n, r->x1/16.0, r->y1/16.0, r->x2/16.0, r->y2/16.0, + r->minx/16.0, r->miny/16.0, r->maxx/16.0, r->maxy/16.0, + r->updown); + ); + + lminx = i_min( p_eval_aty(l, maxy), p_eval_aty(l, miny) ); + lmaxx = i_max( p_eval_aty(l, maxy), p_eval_aty(l, miny) ); - rminx = min( p_eval_aty(r, maxy), p_eval_aty(r, miny) ); - rmaxx = max( p_eval_aty(r, maxy), p_eval_aty(r, miny) ); + rminx = i_min( p_eval_aty(r, maxy), p_eval_aty(r, miny) ); + rmaxx = i_max( p_eval_aty(r, maxy), p_eval_aty(r, miny) ); thin = coarse(lmaxx) >= coarse(rminx); - startpix = max( coarse(lminx), 0 ); - stoppix = min( coarse(rmaxx-1), ss->linelen-1 ); + startpix = i_max( coarse(lminx), 0 ); + stoppix = i_min( coarse(rmaxx-1), ss->linelen-1 ); + + POLY_DEB( printf(" miny=%g maxy=%g\n", miny/16.0, maxy/16.0) ); for(cpix=startpix; cpix<=stoppix; cpix++) { int lt = coarse(lmaxx-1) >= cpix; @@ -391,118 +363,20 @@ render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r) { int A, B, C; - POLY_DEB( printf("(%d,%d) lt=%d rt=%d\n", cpix, y, lt, rt) ); + POLY_DEB( printf(" (%d,%d) lt=%d rt=%d\n", cpix, y, lt, rt) ); A = lt ? pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy) : 0; B = lt ? 0 : 16*(maxy-miny); C = rt ? pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy) : 0; - POLY_DEB( printf("A=%d B=%d C=%d\n", A, B, C) ); + POLY_DEB( printf(" A=%d B=%d C=%d\n", A, B, C) ); ss->line[cpix] += A+B-C; } - + POLY_DEB( printf("end render_slice_scanline()\n") ); } - - -static -void -render_slice_scanline_old(ss_scanline *ss, int y, p_line *l, p_line *r) { - - pcord miny, maxy; /* y bounds in fine coordinates */ - pcord lminx, lmaxx; /* left line min/max within y bounds in fine coords */ - pcord rminx, rmaxx; /* right line min/max within y bounds in fine coords */ - int cpix; /* x-coordinate of current pixel */ - int thin; /* boolean for thin/thick segment */ - int startpix; /* temporary variable for "start of this interval" */ - int stoppix; /* temporary variable for "end of this interval" */ - int step2end; /* temporary variable to mark where step2 ends */ - - /* Find the y bounds of scanline_slice */ - - maxy = min( l->maxy, r->maxy ); - miny = max( l->miny, r->miny ); - - maxy = min( maxy, (y+1)*16 ); - miny = max( miny, y*16 ); - - lminx = min( p_eval_aty(l, maxy), p_eval_aty(l, miny) ); - lmaxx = max( p_eval_aty(l, maxy), p_eval_aty(l, miny) ); - - rminx = min( p_eval_aty(r, maxy), p_eval_aty(r, miny) ); - rmaxx = max( p_eval_aty(r, maxy), p_eval_aty(r, miny) ); - - thin = coarse(lmaxx) >= coarse(rminx); - - - /* First step */ - startpix = coarse(lminx); /* includes tricky starting pixel */ - stoppix = min(coarse(lmaxx), coarse(rminx) ); /* last pixel is tricky */ - - /* handle start pixel */ - - cpix = startpix; - if (cpix < stoppix) { - ss->line[cpix] += pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy); - printf("%2d: step1 - start pixel\n", cpix); - } - - for(cpix=startpix+1; cpixline[cpix] += l->updown == 1 ? - 8.0 * (2*maxy-p_eval_atx(l, 16*cpix)-p_eval_atx(l, 16*cpix+16)) /* up case */ - : - 8.0 * (p_eval_atx(l, 16*cpix)+p_eval_atx(l, 16*cpix+16)-2*miny); /* down case */ - } - - - /* handle stop pixel */ - - if (thin) { /* step 2a */ - startpix = coarse(rminx); - stoppix = coarse(lmaxx+15); /* one more than needed */ - - for(cpix=startpix; cpixline[cpix] += - pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy) - +(cpix*16+16-min(cpix*16+16, l->maxx))*(maxy-miny) - -pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy); - } - } else { /* step 2b */ - stoppix = coarse(rminx); - for(/* cpix already correct */; cpixline[cpix] += 16.0*(maxy-miny); - } - } - - /* step 3 */ - - cpix = max(coarse(rminx), coarse(lmaxx+15)); - stoppix = coarse(rmaxx-15); - - printf("step3 from %d to %d\n", cpix, stoppix); - - for(; cpixline[cpix] += 0+ - (l->updown == 1 ? - 8.0 * (2*maxy-p_eval_atx(r, 16*cpix)-p_eval_atx(r, 16*cpix+16)) /* up case */ - : - 8.0 * (p_eval_atx(r, 16*cpix)+p_eval_atx(r, 16*cpix+16)-2*miny)); /* down case */ - } - - ss->line[cpix] += (16.0)*(maxy-miny) - pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy); -} - - - - - - /* Antialiasing polygon algorithm specs: 1. only nice polygons - no crossovers @@ -533,13 +407,13 @@ render_slice_scanline_old(ss_scanline *ss, int y, p_line *l, p_line *r) { */ -void -i_poly_aa_low(i_img *im, int l, double *x, double *y, void *ctx, scanline_flusher flusher) { +static void +i_poly_aa_low(i_img *im, int l, const double *x, const double *y, void *ctx, scanline_flusher flusher) { int i ,k; /* Index variables */ - int clc; /* Lines inside current interval */ - pcord miny ,maxy; /* Min and max values of the current slice in the subcord system */ - pcord tempy; - int cscl; /* Current scanline */ + i_img_dim clc; /* Lines inside current interval */ + /* initialize to avoid compiler warnings */ + pcord tempy = 0; + i_img_dim cscl = 0; /* Current scanline */ ss_scanline templine; /* scanline accumulator */ p_point *pset; /* List of points in polygon */ @@ -579,47 +453,64 @@ i_poly_aa_low(i_img *im, int l, double *x, double *y, void *ctx, scanline_flushe /* loop on intervals */ for(i=0; iysize); - pcord cc = (pset[i].y + pset[i+1].y)/2; + i_img_dim startscan = i_max( coarse(pset[i].y), 0); + i_img_dim stopscan = i_min( coarse(pset[i+1].y+15), im->ysize); + pcord miny, maxy; /* y bounds in fine coordinates */ - if (pset[i].y == pset[i+1].y) { - POLY_DEB( printf("current slice thickness = 0 => skipping\n") ); - continue; - } + POLY_DEB( pcord cc = (pset[i].y + pset[i+1].y)/2 ); POLY_DEB( printf("current slice is %d: %d to %d ( cpoint %d ) scanlines %d to %d\n", i, pset[i].y, pset[i+1].y, cc, startscan, stopscan) ); - + if (pset[i].y == pset[i+1].y) { + POLY_DEB( printf("current slice thickness = 0 => skipping\n") ); + continue; + } + clc = lines_in_interval(lset, l, tllist, pset[i].y, pset[i+1].y); qsort(tllist, clc, sizeof(p_slice), (int(*)(const void *,const void *))p_compx); mark_updown_slices(lset, tllist, clc); - POLY_DEB( printf("Interval contains %d lines\n", clc) ); - - for(k=0; k(%2d, %2d) (%2d/%2d, %2d/%2d) -> (%2d/%2d, %2d/%2d) alignment=%s\n", - k, lno, ln->x1, ln->y1, ln->x2, ln->y2, - coarse(ln->x1), fine(ln->x1), - coarse(ln->y1), fine(ln->y1), - coarse(ln->x2), fine(ln->x2), - coarse(ln->y2), fine(ln->y2), - ln->updown == 0 ? "vert" : ln->updown == 1 ? "up" : "down") - ); + POLY_DEB + ( + printf("Interval contains %d lines\n", clc); + for(k=0; k(%2d, %2d) (%2d/%2d, %2d/%2d) -> (%2d/%2d, %2d/%2d) alignment=%s\n", + k, lno, ln->x1, ln->y1, ln->x2, ln->y2, + coarse(ln->x1), fine(ln->x1), + coarse(ln->y1), fine(ln->y1), + coarse(ln->x2), fine(ln->x2), + coarse(ln->y2), fine(ln->y2), + ln->updown == 0 ? "vert" : ln->updown == 1 ? "up" : "down"); + + } + ); + maxy = im->ysize * 16; + miny = 0; + for (k = 0; k < clc; ++k) { + p_line const * line = lset + tllist[k].n; + if (line->miny > miny) + miny = line->miny; + if (line->maxy < maxy) + maxy = line->maxy; + POLY_DEB( printf(" line miny %g maxy %g\n", line->miny/16.0, line->maxy/16.0) ); } + POLY_DEB( printf("miny %g maxy %g\n", miny/16.0, maxy/16.0) ); + for(cscl=startscan; csclfillbuf; - i_color *line = state->linebuf; - - left = 0; - while (left < im->xsize && ss->line[left] <= 0) - ++left; - if (left < im->xsize) { - right = im->xsize; - /* since going from the left found something, moving from the - right should */ - while (/* right > left && */ ss->line[right-1] <= 0) - --right; - - (state->fill->fill_with_color)(state->fill, left, y, right-left, - im->channels, fillbuf); - i_glin(im, left, right, y, line); - pos = 0; - if (state->fill->combine) { - for (x = left; x < right; ++x) { - tv = saturate(ss->line[x]); - fillbuf[pos].channel[3] = - fillbuf[pos].channel[3] * tv / 255; - } - (state->fill->combine)(line, fillbuf, im->channels, right-left); - pos++; - } - else { - for (x = left; x < right; ++x) { - tv = saturate(ss->line[x]); - if (tv) { - if (tv == 255) { - line[pos] = fillbuf[pos]; - } - else { - i_color *to = line + pos; - i_color *from = fillbuf + pos; - for (ch = 0; ch < im->channels; ++ch) { - to->channel[ch] = (tv * from->channel[ch] + - (255 - tv) * to->channel[ch]) / 255; - } - } - } - pos++; - } - } - i_plin(im, left, right, y, line); - } +int +i_poly_aa(i_img *im, int l, const double *x, const double *y, const i_color *val) { + i_color c = *val; + i_poly_aa_low(im, l, x, y, &c, scanline_flush); + return 1; } -struct poly_cfill_state_f { - i_fcolor *fillbuf; - i_fcolor *linebuf; - int *cover; +struct poly_render_state { + i_render render; i_fill_t *fill; + unsigned char *cover; }; -void -scanline_flush_cfill_f(i_img *im, ss_scanline *ss, int y, void *ctx) { - int x, ch, tv; - int pos; - int left, right; - struct poly_cfill_state_f *state = (struct poly_cfill_state_f *)ctx; - i_fcolor *fillbuf = state->fillbuf; - i_fcolor *line = state->linebuf; +static void +scanline_flush_render(i_img *im, ss_scanline *ss, int y, void *ctx) { + i_img_dim x; + i_img_dim left, right; + struct poly_render_state *state = (struct poly_render_state *)ctx; left = 0; while (left < im->xsize && ss->line[left] <= 0) @@ -739,62 +565,25 @@ scanline_flush_cfill_f(i_img *im, ss_scanline *ss, int y, void *ctx) { while (/* right > left && */ ss->line[right-1] <= 0) --right; - (state->fill->fill_with_fcolor)(state->fill, left, y, right-left, - im->channels, fillbuf); - i_glinf(im, left, right, y, line); - pos = 0; - if (state->fill->combinef) { - for (x = left; x < right; ++x) { - tv = saturate(ss->line[x]); - fillbuf[pos].channel[3] = - fillbuf[pos].channel[3] * tv / 255; - } - (state->fill->combinef)(line, fillbuf, im->channels, right-left); - pos++; - } - else { - for (x = left; x < right; ++x) { - tv = saturate(ss->line[x]); - if (tv) { - if (tv == 255) { - line[pos] = fillbuf[pos]; - } - else { - i_fcolor *to = line + pos; - i_fcolor *from = fillbuf + pos; - for (ch = 0; ch < im->channels; ++ch) { - to->channel[ch] = (tv * from->channel[ch] + - (255 - tv) * to->channel[ch]) / 255; - } - } - } - pos++; - } + /* convert to the format the render interface wants */ + for (x = left; x < right; ++x) { + state->cover[x-left] = saturate(ss->line[x]); } - i_plinf(im, left, right, y, line); + i_render_fill(&state->render, left, y, right-left, state->cover, + state->fill); } } -void -i_poly_aa_cfill(i_img *im, int l, double *x, double *y, i_fill_t *fill) { - if (im->bits == i_8_bits && fill->fill_with_color) { - struct poly_cfill_state ctx; - ctx.fillbuf = mymalloc(sizeof(i_color) * im->xsize * 2); - ctx.linebuf = ctx.fillbuf + im->xsize; - ctx.cover = mymalloc(sizeof(int) * im->xsize); - ctx.fill = fill; - i_poly_aa_low(im, l, x, y, &ctx, scanline_flush_cfill); - myfree(ctx.fillbuf); - myfree(ctx.cover); - } - else { - struct poly_cfill_state_f ctx; - ctx.fillbuf = mymalloc(sizeof(i_fcolor) * im->xsize * 2); - ctx.linebuf = ctx.fillbuf + im->xsize; - ctx.cover = mymalloc(sizeof(int) * im->xsize); - ctx.fill = fill; - i_poly_aa_low(im, l, x, y, &ctx, scanline_flush_cfill_f); - myfree(ctx.fillbuf); - myfree(ctx.cover); - } +int +i_poly_aa_cfill(i_img *im, int l, const double *x, const double *y, + i_fill_t *fill) { + struct poly_render_state ctx; + + i_render_init(&ctx.render, im, im->xsize); + ctx.fill = fill; + ctx.cover = mymalloc(im->xsize); + i_poly_aa_low(im, l, x, y, &ctx, scanline_flush_render); + myfree(ctx.cover); + i_render_done(&ctx.render); + return 1; }