7 #define IMTRUNC(x) ((int)((x)*16))
9 #define coarse(x) ((x)/16)
10 #define fine(x) ((x)%16)
12 /*#define DEBUG_POLY*/
20 typedef i_img_dim pcord;
33 int updown; /* -1 means down, 0 vertical, 1 up */
34 int dir; /* 1 for down, -1 for up */
43 int *line; /* temporary buffer for scanline */
44 i_img_dim linelen; /* length of scanline */
49 p_compy(const p_point *p1, const p_point *p2) {
50 if (p1->y > p2->y) return 1;
51 if (p1->y < p2->y) return -1;
57 p_compx(const p_slice *p1, const p_slice *p2) {
58 if (p1->x > p2->x) return 1;
59 if (p1->x < p2->x) return -1;
63 /* Change this to int? and round right goddamn it! */
67 p_eval_aty(p_line *l, pcord y) {
70 if (t) return ( (y-l->y1)*l->x2 + (l->y2-y)*l->x1 )/t;
71 return (l->x1+l->x2)/2.0;
76 p_eval_atx(p_line *l, pcord x) {
79 if (t) return ( (x-l->x1)*l->y2 + (l->x2-x)*l->y1 )/t;
80 return (l->y1+l->y2)/2.0;
85 line_set_new(const i_polygon_t *polys, size_t count, size_t *line_count) {
90 for (i = 0; i < count; ++i)
91 lines += polys[i].count;
93 line = lset = mymalloc(sizeof(p_line) * lines);
96 for (i = 0; i < count; ++i) {
97 const i_polygon_t *p = polys + i;
99 for(j = 0; j < p->count; j++) {
100 line->x1 = IMTRUNC(p->x[j]);
101 line->y1 = IMTRUNC(p->y[j]);
102 line->x2 = IMTRUNC(p->x[(j + 1) % p->count]);
103 line->y2 = IMTRUNC(p->y[(j + 1) % p->count]);
105 /* don't include purely horizontal lines, we don't usefully
106 intersect with them. */
107 if (line->y1 == line->y2)
110 line->miny = i_min(line->y1, line->y2);
111 line->maxy = i_max(line->y1, line->y2);
112 line->minx = i_min(line->x1, line->x2);
113 line->maxx = i_max(line->x1, line->x2);
125 point_set_new(const i_polygon_t *polys, size_t count, size_t *point_count) {
130 for (i = 0; i < count; ++i)
131 points += polys[i].count;
133 *point_count = points;
135 pt = pset = mymalloc(sizeof(p_point) * points);
138 for (i = 0; i < count; ++i) {
139 const i_polygon_t *p = polys + i;
141 for(j = 0; j < p->count; j++) {
143 pt->x = IMTRUNC(p->x[j]);
144 pt->y = IMTRUNC(p->y[j]);
153 ss_scanline_reset(ss_scanline *ss) {
154 memset(ss->line, 0, sizeof(int) * ss->linelen);
159 ss_scanline_init(ss_scanline *ss, i_img_dim linelen, int linepairs) {
160 ss->line = mymalloc( sizeof(int) * linelen );
161 ss->linelen = linelen;
162 ss_scanline_reset(ss);
167 ss_scanline_exorcise(ss_scanline *ss) {
174 /* returns the number of matches */
178 lines_in_interval(p_line *lset, int l, p_slice *tllist, pcord minc, pcord maxc) {
182 if (lset[k].maxy > minc && lset[k].miny < maxc) {
183 if (lset[k].miny == lset[k].maxy) {
184 POLY_DEB( printf(" HORIZONTAL - skipped\n") );
186 tllist[count].x=p_eval_aty(&lset[k],(minc+maxc)/2.0 );
195 /* marks the up variable for all lines in a slice */
199 mark_updown_slices(p_line *lset, p_slice *tllist, int count) {
202 for(k=0; k<count; k+=2) {
203 l = lset + tllist[k].n;
205 if (l->y1 == l->y2) {
206 mm_log((1, "mark_updown_slices: horizontal line being marked: internal error!\n"));
210 l->updown = (l->x1 == l->x2) ?
214 (l->y1 > l->y2) ? -1 : 1
216 (l->y1 > l->y2) ? 1 : -1;
217 l->dir = l->y1 < l->y2 ? 1 : -1;
219 POLY_DEB( printf("marking left line %d as %s(%d)\n", l->n,
220 l->updown ? l->updown == 1 ? "up" : "down" : "vert", l->updown, l->updown)
224 mm_log((1, "Invalid polygon spec, odd number of line crossings.\n"));
228 r = lset + tllist[k+1].n;
229 if (r->y1 == r->y2) {
230 mm_log((1, "mark_updown_slices: horizontal line being marked: internal error!\n"));
234 r->updown = (r->x1 == r->x2) ?
238 (r->y1 > r->y2) ? -1 : 1
240 (r->y1 > r->y2) ? 1 : -1;
241 r->dir = r->y1 < r->y2 ? 1 : -1;
243 POLY_DEB( printf("marking right line %d as %s(%d)\n", r->n,
244 r->updown ? r->updown == 1 ? "up" : "down" : "vert", r->updown, r->updown)
252 if (in>255) { return 255; }
253 else if (in>0) return in;
257 typedef void (*scanline_flusher)(i_img *im, ss_scanline *ss, int y, void *ctx);
259 /* This function must be modified later to do proper blending */
262 scanline_flush(i_img *im, ss_scanline *ss, int y, void *ctx) {
265 i_color *val = (i_color *)ctx;
266 POLY_DEB( printf("Flushing line %d\n", y) );
267 for(x=0; x<im->xsize; x++) {
268 tv = saturate(ss->line[x]);
269 i_gpix(im, x, y, &t);
270 for(ch=0; ch<im->channels; ch++)
271 t.channel[ch] = tv/255.0 * val->channel[ch] + (1.0-tv/255.0) * t.channel[ch];
272 i_ppix(im, x, y, &t);
280 trap_square(pcord xlen, pcord ylen, double xl, double yl) {
281 POLY_DEB( printf("trap_square: %d %d %.2f %.2f\n", xlen, ylen, xl, yl) );
282 return xlen*ylen-(xl*yl)/2.0;
287 pixel_coverage calculates the 'left side' pixel coverage of a pixel that is
288 within the min/max ranges. The shape always corresponds to a square with some
289 sort of a triangle cut from it (which can also yield a triangle).
295 pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord miny, pcord maxy) {
296 double lycross, rycross;
301 printf(" pixel_coverage(..., minx %g, maxx%g, miny %g, maxy %g)\n",
302 minx/16.0, maxx/16.0, miny/16.0, maxy/16.0)
308 lycross = p_eval_atx(line, minx);
309 rycross = p_eval_atx(line, maxx);
310 l = lycross <= maxy && lycross >= miny; /* true if it enters through left side */
311 r = rycross <= maxy && rycross >= miny; /* true if it enters through left side */
314 printf(" %4s(%+d): ", line->updown ? line->updown == 1 ? "up" : "down" : "vert", line->updown);
315 printf(" (%2d,%2d) [%3d-%3d, %3d-%3d] lycross=%.2f rycross=%.2f", coarse(minx), coarse(miny), minx, maxx, miny, maxy, lycross, rycross);
316 printf(" l=%d r=%d\n", l, r)
320 return line->updown == 1 ?
321 (double)(maxx-minx) * (2.0*maxy-lycross-rycross)/2.0 /* up case */
323 (double)(maxx-minx) * (lycross+rycross-2*miny)/2.0; /* down case */
325 if (!l && !r) return (maxy-miny)*(maxx*2-p_eval_aty(line, miny)-p_eval_aty(line, maxy))/2.0;
328 return line->updown == 1 ?
329 trap_square(maxx-minx, maxy-miny, p_eval_aty(line, miny)-minx, p_eval_atx(line, minx)-miny) :
330 trap_square(maxx-minx, maxy-miny, p_eval_aty(line, maxy)-minx, maxy-p_eval_atx(line, minx));
334 int r = line->updown == 1 ?
335 (maxx-p_eval_aty(line, maxy))*(maxy-p_eval_atx(line, maxx))/2.0 :
336 (maxx-p_eval_aty(line, miny))*(p_eval_atx(line, maxx)-miny)/2.0;
340 return 0; /* silence compiler warning */
348 handle the scanline slice in three steps
350 1. Where only the left edge is inside a pixel
351 2a. Where both left and right edge are inside a pixel
352 2b. Where neither left or right edge are inside a pixel
353 3. Where only the right edge is inside a pixel
358 render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r, pcord miny, pcord maxy) {
360 pcord lminx, lmaxx; /* left line min/max within y bounds in fine coords */
361 pcord rminx, rmaxx; /* right line min/max within y bounds in fine coords */
362 i_img_dim cpix; /* x-coordinate of current pixel */
363 i_img_dim startpix; /* temporary variable for "start of this interval" */
364 i_img_dim stoppix; /* temporary variable for "end of this interval" */
366 /* Find the y bounds of scanline_slice */
370 printf("render_slice_scanline(..., y=%d)\n");
371 printf(" left n=%d p1(%.2g, %.2g) p2(%.2g,%.2g) min(%.2g, %.2g) max(%.2g,%.2g) updown(%d)\n",
372 l->n, l->x1/16.0, l->y1/16.0, l->x2/16.0, l->y2/16.0,
373 l->minx/16.0, l->miny/16.0, l->maxx/16.0, l->maxy/16.0,
375 printf(" right n=%d p1(%.2g, %.2g) p2(%.2g,%.2g) min(%.2g, %.2g) max(%.2g,%.2g) updown(%d)\n",
376 r->n, r->x1/16.0, r->y1/16.0, r->x2/16.0, r->y2/16.0,
377 r->minx/16.0, r->miny/16.0, r->maxx/16.0, r->maxy/16.0,
381 lminx = i_min( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
382 lmaxx = i_max( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
384 rminx = i_min( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
385 rmaxx = i_max( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
387 startpix = i_max( coarse(lminx), 0 );
388 stoppix = i_min( coarse(rmaxx-1), ss->linelen-1 );
390 POLY_DEB( printf(" miny=%g maxy=%g\n", miny/16.0, maxy/16.0) );
392 for(cpix=startpix; cpix<=stoppix; cpix++) {
393 int lt = coarse(lmaxx-1) >= cpix;
394 int rt = coarse(rminx) <= cpix;
398 POLY_DEB( printf(" (%d,%d) lt=%d rt=%d\n", cpix, y, lt, rt) );
400 A = lt ? pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy) : 0;
401 B = lt ? 0 : 16*(maxy-miny);
402 C = rt ? pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy) : 0;
404 POLY_DEB( printf(" A=%d B=%d C=%d\n", A, B, C) );
406 ss->line[cpix] += A+B-C;
409 POLY_DEB( printf("end render_slice_scanline()\n") );
412 /* Antialiasing polygon algorithm
414 1. only nice polygons - no crossovers
415 2. 1/16 pixel resolution
416 3. full antialiasing ( complete spectrum of blends )
417 4. uses hardly any memory
418 5. no subsampling phase
422 1. Split into vertical intervals.
423 2. handle each interval
425 For each interval we must:
426 1. find which lines are in it
427 2. order the lines from in increasing x order.
428 since we are assuming no crossovers it is sufficent
429 to check a single point on each line.
435 1. Interval: A vertical segment in which no lines cross nor end.
436 2. Scanline: A physical line, contains 16 subpixels in the horizontal direction
437 3. Slice: A start stop line pair.
443 i_poly_poly_aa_low(i_img *im, int count, const i_polygon_t *polys,
444 i_poly_fill_mode_t mode, void *ctx,
445 scanline_flusher flusher) {
446 int i ,k; /* Index variables */
447 i_img_dim clc; /* Lines inside current interval */
448 /* initialize to avoid compiler warnings */
450 i_img_dim cscl = 0; /* Current scanline */
452 ss_scanline templine; /* scanline accumulator */
453 p_point *pset; /* List of points in polygon */
454 p_line *lset; /* List of lines in polygon */
455 p_slice *tllist; /* List of slices */
456 size_t pcount, lcount;
458 mm_log((1, "i_poly_poly_aa_low(im %p, count %d, polys %p, ctx %p, flusher %p)\n", im, count, polys, ctx, flusher));
460 for (k = 0; k < count; ++k) {
461 const i_polygon_t *p = polys + k;
462 mm_log((2, "poly %d\n", k));
463 for(i = 0; i < p->count; i++) {
464 mm_log((2, " (%.2f, %.2f)\n", p->x[i], p->y[i]));
471 setbuf(stdout, NULL);
474 pset = point_set_new(polys, count, &pcount);
475 lset = line_set_new(polys, count, &lcount);
477 ss_scanline_init(&templine, im->xsize, lcount);
479 tllist = mymalloc(sizeof(p_slice) * lcount);
481 qsort(pset, pcount, sizeof(p_point), (int(*)(const void *,const void *))p_compy);
484 for(i=0;i<lcount;i++) {
485 printf("%d [ %d ] (%d , %d) -> (%d , %d) yspan ( %d , %d )\n",
486 i, lset[i].n, lset[i].x1, lset[i].y1, lset[i].x2, lset[i].y2, lset[i].miny, lset[i].maxy);
488 printf("MAIN LOOP\n\n");
492 /* loop on intervals */
493 for(i=0; i<pcount-1; i++) {
494 i_img_dim startscan = i_max( coarse(pset[i].y), 0);
495 i_img_dim stopscan = i_min( coarse(pset[i+1].y+15), im->ysize);
496 pcord miny, maxy; /* y bounds in fine coordinates */
498 POLY_DEB( pcord cc = (pset[i].y + pset[i+1].y)/2 );
501 printf("current slice is %d: %d to %d ( cpoint %d ) scanlines %d to %d\n",
502 i, pset[i].y, pset[i+1].y, cc, startscan, stopscan)
505 if (pset[i].y == pset[i+1].y) {
506 POLY_DEB( printf("current slice thickness = 0 => skipping\n") );
510 clc = lines_in_interval(lset, lcount, tllist, pset[i].y, pset[i+1].y);
511 qsort(tllist, clc, sizeof(p_slice), (int(*)(const void *,const void *))p_compx);
513 mark_updown_slices(lset, tllist, clc);
517 printf("Interval contains %d lines\n", clc);
518 for(k=0; k<clc; k++) {
519 int lno = tllist[k].n;
520 p_line *ln = lset+lno;
521 printf("%d: line #%2d: (%2d, %2d)->(%2d, %2d) (%2d/%2d, %2d/%2d) -> (%2d/%2d, %2d/%2d) alignment=%s\n",
522 k, lno, ln->x1, ln->y1, ln->x2, ln->y2,
523 coarse(ln->x1), fine(ln->x1),
524 coarse(ln->y1), fine(ln->y1),
525 coarse(ln->x2), fine(ln->x2),
526 coarse(ln->y2), fine(ln->y2),
527 ln->updown == 0 ? "vert" : ln->updown == 1 ? "up" : "down");
531 maxy = im->ysize * 16;
533 for (k = 0; k < clc; ++k) {
534 p_line const * line = lset + tllist[k].n;
535 if (line->miny > miny)
537 if (line->maxy < maxy)
539 POLY_DEB( printf(" line miny %g maxy %g\n", line->miny/16.0, line->maxy/16.0) );
541 POLY_DEB( printf("miny %g maxy %g\n", miny/16.0, maxy/16.0) );
543 for(cscl=startscan; cscl<stopscan; cscl++) {
544 pcord scan_miny = i_max(miny, cscl * 16);
545 pcord scan_maxy = i_min(maxy, (cscl + 1 ) * 16);
547 tempy = i_min(cscl*16+16, pset[i+1].y);
548 POLY_DEB( printf("evaluating scan line %d \n", cscl) );
549 if (mode == i_pfm_evenodd) {
550 for(k=0; k<clc-1; k+=2) {
551 POLY_DEB( printf("evaluating slice %d\n", k) );
552 render_slice_scanline(&templine, cscl, lset+tllist[k].n, lset+tllist[k+1].n, scan_miny, scan_maxy);
558 p_line *left = lset + tllist[k++].n;
562 while (k < clc && acc) {
563 current = lset + tllist[k++].n;
567 render_slice_scanline(&templine, cscl, left, current,
568 scan_miny, scan_maxy);
572 if (16*coarse(tempy) == tempy) {
573 POLY_DEB( printf("flushing scan line %d\n", cscl) );
574 flusher(im, &templine, cscl, ctx);
575 ss_scanline_reset(&templine);
579 scanline_flush(im, &templine, cscl, val);
580 ss_scanline_reset(&templine);
586 if (16*coarse(tempy) != tempy)
587 flusher(im, &templine, cscl-1, ctx);
589 ss_scanline_exorcise(&templine);
598 i_poly_poly_aa(i_img *im, int count, const i_polygon_t *polys,
599 i_poly_fill_mode_t mode, const i_color *val) {
601 return i_poly_poly_aa_low(im, count, polys, mode, &c, scanline_flush);
605 i_poly_aa_m(i_img *im, int l, const double *x, const double *y,
606 i_poly_fill_mode_t mode, const i_color *val) {
612 return i_poly_poly_aa(im, 1, &poly, mode, val);
616 i_poly_aa(i_img *im, int l, const double *x, const double *y, const i_color *val) {
622 return i_poly_poly_aa(im, 1, &poly, i_pfm_evenodd, val);
625 struct poly_render_state {
628 unsigned char *cover;
632 scanline_flush_render(i_img *im, ss_scanline *ss, int y, void *ctx) {
634 i_img_dim left, right;
635 struct poly_render_state *state = (struct poly_render_state *)ctx;
638 while (left < im->xsize && ss->line[left] <= 0)
640 if (left < im->xsize) {
642 /* since going from the left found something, moving from the
644 while (/* right > left && */ ss->line[right-1] <= 0)
647 /* convert to the format the render interface wants */
648 for (x = left; x < right; ++x) {
649 state->cover[x-left] = saturate(ss->line[x]);
651 i_render_fill(&state->render, left, y, right-left, state->cover,
657 i_poly_poly_aa_cfill(i_img *im, int count, const i_polygon_t *polys,
658 i_poly_fill_mode_t mode, i_fill_t *fill) {
659 struct poly_render_state ctx;
662 i_render_init(&ctx.render, im, im->xsize);
664 ctx.cover = mymalloc(im->xsize);
666 result = i_poly_poly_aa_low(im, count, polys, mode, &ctx,
667 scanline_flush_render);
670 i_render_done(&ctx.render);
676 i_poly_aa_cfill_m(i_img *im, int l, const double *x, const double *y,
677 i_poly_fill_mode_t mode, i_fill_t *fill) {
684 return i_poly_poly_aa_cfill(im, 1, &poly, mode, fill);
688 i_poly_aa_cfill(i_img *im, int l, const double *x, const double *y,
696 return i_poly_poly_aa_cfill(im, 1, &poly, i_pfm_evenodd, fill);