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 */
42 int *line; /* temporary buffer for scanline */
43 i_img_dim linelen; /* length of scanline */
48 p_compy(const p_point *p1, const p_point *p2) {
49 if (p1->y > p2->y) return 1;
50 if (p1->y < p2->y) return -1;
56 p_compx(const p_slice *p1, const p_slice *p2) {
57 if (p1->x > p2->x) return 1;
58 if (p1->x < p2->x) return -1;
62 /* Change this to int? and round right goddamn it! */
66 p_eval_aty(p_line *l, pcord y) {
69 if (t) return ( (y-l->y1)*l->x2 + (l->y2-y)*l->x1 )/t;
70 return (l->x1+l->x2)/2.0;
75 p_eval_atx(p_line *l, pcord x) {
78 if (t) return ( (x-l->x1)*l->y2 + (l->x2-x)*l->y1 )/t;
79 return (l->y1+l->y2)/2.0;
84 line_set_new(const i_polygon_t *polys, size_t count, size_t *line_count) {
89 for (i = 0; i < count; ++i)
90 lines += polys[i].count;
94 line = lset = mymalloc(sizeof(p_line) * lines);
97 for (i = 0; i < count; ++i) {
98 const i_polygon_t *p = polys + i;
100 for(j = 0; j < p->count; j++) {
102 line->x1 = IMTRUNC(p->x[j]);
103 line->y1 = IMTRUNC(p->y[j]);
104 line->x2 = IMTRUNC(p->x[(j + 1) % p->count]);
105 line->y2 = IMTRUNC(p->y[(j + 1) % p->count]);
106 line->miny = i_min(line->y1, line->y2);
107 line->maxy = i_max(line->y1, line->y2);
108 line->minx = i_min(line->x1, line->x2);
109 line->maxx = i_max(line->x1, line->x2);
119 point_set_new(const i_polygon_t *polys, size_t count, size_t *point_count) {
124 for (i = 0; i < count; ++i)
125 points += polys[i].count;
127 *point_count = points;
129 pt = pset = mymalloc(sizeof(p_point) * points);
132 for (i = 0; i < count; ++i) {
133 const i_polygon_t *p = polys + i;
135 for(j = 0; j < p->count; j++) {
137 pt->x = IMTRUNC(p->x[j]);
138 pt->y = IMTRUNC(p->y[j]);
147 ss_scanline_reset(ss_scanline *ss) {
148 memset(ss->line, 0, sizeof(int) * ss->linelen);
153 ss_scanline_init(ss_scanline *ss, i_img_dim linelen, int linepairs) {
154 ss->line = mymalloc( sizeof(int) * linelen );
155 ss->linelen = linelen;
156 ss_scanline_reset(ss);
161 ss_scanline_exorcise(ss_scanline *ss) {
168 /* returns the number of matches */
172 lines_in_interval(p_line *lset, int l, p_slice *tllist, pcord minc, pcord maxc) {
176 if (lset[k].maxy > minc && lset[k].miny < maxc) {
177 if (lset[k].miny == lset[k].maxy) {
178 POLY_DEB( printf(" HORIZONTAL - skipped\n") );
180 tllist[count].x=p_eval_aty(&lset[k],(minc+maxc)/2.0 );
189 /* marks the up variable for all lines in a slice */
193 mark_updown_slices(p_line *lset, p_slice *tllist, int count) {
196 for(k=0; k<count; k+=2) {
197 l = lset + tllist[k].n;
199 if (l->y1 == l->y2) {
200 mm_log((1, "mark_updown_slices: horizontal line being marked: internal error!\n"));
204 l->updown = (l->x1 == l->x2) ?
208 (l->y1 > l->y2) ? -1 : 1
210 (l->y1 > l->y2) ? 1 : -1;
212 POLY_DEB( printf("marking left line %d as %s(%d)\n", l->n,
213 l->updown ? l->updown == 1 ? "up" : "down" : "vert", l->updown, l->updown)
217 mm_log((1, "Invalid polygon spec, odd number of line crossings.\n"));
221 r = lset + tllist[k+1].n;
222 if (r->y1 == r->y2) {
223 mm_log((1, "mark_updown_slices: horizontal line being marked: internal error!\n"));
227 r->updown = (r->x1 == r->x2) ?
231 (r->y1 > r->y2) ? -1 : 1
233 (r->y1 > r->y2) ? 1 : -1;
235 POLY_DEB( printf("marking right line %d as %s(%d)\n", r->n,
236 r->updown ? r->updown == 1 ? "up" : "down" : "vert", r->updown, r->updown)
244 if (in>255) { return 255; }
245 else if (in>0) return in;
249 typedef void (*scanline_flusher)(i_img *im, ss_scanline *ss, int y, void *ctx);
251 /* This function must be modified later to do proper blending */
254 scanline_flush(i_img *im, ss_scanline *ss, int y, void *ctx) {
257 i_color *val = (i_color *)ctx;
258 POLY_DEB( printf("Flushing line %d\n", y) );
259 for(x=0; x<im->xsize; x++) {
260 tv = saturate(ss->line[x]);
261 i_gpix(im, x, y, &t);
262 for(ch=0; ch<im->channels; ch++)
263 t.channel[ch] = tv/255.0 * val->channel[ch] + (1.0-tv/255.0) * t.channel[ch];
264 i_ppix(im, x, y, &t);
272 trap_square(pcord xlen, pcord ylen, double xl, double yl) {
273 POLY_DEB( printf("trap_square: %d %d %.2f %.2f\n", xlen, ylen, xl, yl) );
274 return xlen*ylen-(xl*yl)/2.0;
279 pixel_coverage calculates the 'left side' pixel coverage of a pixel that is
280 within the min/max ranges. The shape always corresponds to a square with some
281 sort of a triangle cut from it (which can also yield a triangle).
287 pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord miny, pcord maxy) {
288 double lycross, rycross;
293 printf(" pixel_coverage(..., minx %g, maxx%g, miny %g, maxy %g)\n",
294 minx/16.0, maxx/16.0, miny/16.0, maxy/16.0)
300 lycross = p_eval_atx(line, minx);
301 rycross = p_eval_atx(line, maxx);
302 l = lycross <= maxy && lycross >= miny; /* true if it enters through left side */
303 r = rycross <= maxy && rycross >= miny; /* true if it enters through left side */
306 printf(" %4s(%+d): ", line->updown ? line->updown == 1 ? "up" : "down" : "vert", line->updown);
307 printf(" (%2d,%2d) [%3d-%3d, %3d-%3d] lycross=%.2f rycross=%.2f", coarse(minx), coarse(miny), minx, maxx, miny, maxy, lycross, rycross);
308 printf(" l=%d r=%d\n", l, r)
312 return line->updown == 1 ?
313 (double)(maxx-minx) * (2.0*maxy-lycross-rycross)/2.0 /* up case */
315 (double)(maxx-minx) * (lycross+rycross-2*miny)/2.0; /* down case */
317 if (!l && !r) return (maxy-miny)*(maxx*2-p_eval_aty(line, miny)-p_eval_aty(line, maxy))/2.0;
320 return line->updown == 1 ?
321 trap_square(maxx-minx, maxy-miny, p_eval_aty(line, miny)-minx, p_eval_atx(line, minx)-miny) :
322 trap_square(maxx-minx, maxy-miny, p_eval_aty(line, maxy)-minx, maxy-p_eval_atx(line, minx));
326 int r = line->updown == 1 ?
327 (maxx-p_eval_aty(line, maxy))*(maxy-p_eval_atx(line, maxx))/2.0 :
328 (maxx-p_eval_aty(line, miny))*(p_eval_atx(line, maxx)-miny)/2.0;
332 return 0; /* silence compiler warning */
340 handle the scanline slice in three steps
342 1. Where only the left edge is inside a pixel
343 2a. Where both left and right edge are inside a pixel
344 2b. Where neither left or right edge are inside a pixel
345 3. Where only the right edge is inside a pixel
350 render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r, pcord miny, pcord maxy) {
352 pcord lminx, lmaxx; /* left line min/max within y bounds in fine coords */
353 pcord rminx, rmaxx; /* right line min/max within y bounds in fine coords */
354 i_img_dim cpix; /* x-coordinate of current pixel */
355 i_img_dim startpix; /* temporary variable for "start of this interval" */
356 i_img_dim stoppix; /* temporary variable for "end of this interval" */
358 /* Find the y bounds of scanline_slice */
362 printf("render_slice_scanline(..., y=%d)\n");
363 printf(" left n=%d p1(%.2g, %.2g) p2(%.2g,%.2g) min(%.2g, %.2g) max(%.2g,%.2g) updown(%d)\n",
364 l->n, l->x1/16.0, l->y1/16.0, l->x2/16.0, l->y2/16.0,
365 l->minx/16.0, l->miny/16.0, l->maxx/16.0, l->maxy/16.0,
367 printf(" right n=%d p1(%.2g, %.2g) p2(%.2g,%.2g) min(%.2g, %.2g) max(%.2g,%.2g) updown(%d)\n",
368 r->n, r->x1/16.0, r->y1/16.0, r->x2/16.0, r->y2/16.0,
369 r->minx/16.0, r->miny/16.0, r->maxx/16.0, r->maxy/16.0,
373 lminx = i_min( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
374 lmaxx = i_max( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
376 rminx = i_min( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
377 rmaxx = i_max( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
379 startpix = i_max( coarse(lminx), 0 );
380 stoppix = i_min( coarse(rmaxx-1), ss->linelen-1 );
382 POLY_DEB( printf(" miny=%g maxy=%g\n", miny/16.0, maxy/16.0) );
384 for(cpix=startpix; cpix<=stoppix; cpix++) {
385 int lt = coarse(lmaxx-1) >= cpix;
386 int rt = coarse(rminx) <= cpix;
390 POLY_DEB( printf(" (%d,%d) lt=%d rt=%d\n", cpix, y, lt, rt) );
392 A = lt ? pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy) : 0;
393 B = lt ? 0 : 16*(maxy-miny);
394 C = rt ? pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy) : 0;
396 POLY_DEB( printf(" A=%d B=%d C=%d\n", A, B, C) );
398 ss->line[cpix] += A+B-C;
401 POLY_DEB( printf("end render_slice_scanline()\n") );
404 /* Antialiasing polygon algorithm
406 1. only nice polygons - no crossovers
407 2. 1/16 pixel resolution
408 3. full antialiasing ( complete spectrum of blends )
409 4. uses hardly any memory
410 5. no subsampling phase
414 1. Split into vertical intervals.
415 2. handle each interval
417 For each interval we must:
418 1. find which lines are in it
419 2. order the lines from in increasing x order.
420 since we are assuming no crossovers it is sufficent
421 to check a single point on each line.
427 1. Interval: A vertical segment in which no lines cross nor end.
428 2. Scanline: A physical line, contains 16 subpixels in the horizontal direction
429 3. Slice: A start stop line pair.
435 i_poly_poly_aa_low(i_img *im, int count, const i_polygon_t *polys,
436 void *ctx, scanline_flusher flusher) {
437 int i ,k; /* Index variables */
438 i_img_dim clc; /* Lines inside current interval */
439 /* initialize to avoid compiler warnings */
441 i_img_dim cscl = 0; /* Current scanline */
443 ss_scanline templine; /* scanline accumulator */
444 p_point *pset; /* List of points in polygon */
445 p_line *lset; /* List of lines in polygon */
446 p_slice *tllist; /* List of slices */
447 size_t pcount, lcount;
449 mm_log((1, "i_poly_poly_aa_low(im %p, count %d, polys %p, ctx %p, flusher %p)\n", im, count, polys, ctx, flusher));
451 for (k = 0; k < count; ++k) {
452 const i_polygon_t *p = polys + k;
453 mm_log((2, "poly %d\n", k));
454 for(i = 0; i < p->count; i++) {
455 mm_log((2, " (%.2f, %.2f)\n", p->x[i], p->y[i]));
462 setbuf(stdout, NULL);
465 pset = point_set_new(polys, count, &pcount);
466 lset = line_set_new(polys, count, &lcount);
468 ss_scanline_init(&templine, im->xsize, lcount);
470 tllist = mymalloc(sizeof(p_slice) * lcount);
472 qsort(pset, pcount, sizeof(p_point), (int(*)(const void *,const void *))p_compy);
475 for(i=0;i<lcount;i++) {
476 printf("%d [ %d ] (%d , %d) -> (%d , %d) yspan ( %d , %d )\n",
477 i, lset[i].n, lset[i].x1, lset[i].y1, lset[i].x2, lset[i].y2, lset[i].miny, lset[i].maxy);
479 printf("MAIN LOOP\n\n");
483 /* loop on intervals */
484 for(i=0; i<pcount-1; i++) {
485 i_img_dim startscan = i_max( coarse(pset[i].y), 0);
486 i_img_dim stopscan = i_min( coarse(pset[i+1].y+15), im->ysize);
487 pcord miny, maxy; /* y bounds in fine coordinates */
489 POLY_DEB( pcord cc = (pset[i].y + pset[i+1].y)/2 );
492 printf("current slice is %d: %d to %d ( cpoint %d ) scanlines %d to %d\n",
493 i, pset[i].y, pset[i+1].y, cc, startscan, stopscan)
496 if (pset[i].y == pset[i+1].y) {
497 POLY_DEB( printf("current slice thickness = 0 => skipping\n") );
501 clc = lines_in_interval(lset, lcount, tllist, pset[i].y, pset[i+1].y);
502 qsort(tllist, clc, sizeof(p_slice), (int(*)(const void *,const void *))p_compx);
504 mark_updown_slices(lset, tllist, clc);
508 printf("Interval contains %d lines\n", clc);
509 for(k=0; k<clc; k++) {
510 int lno = tllist[k].n;
511 p_line *ln = lset+lno;
512 printf("%d: line #%2d: (%2d, %2d)->(%2d, %2d) (%2d/%2d, %2d/%2d) -> (%2d/%2d, %2d/%2d) alignment=%s\n",
513 k, lno, ln->x1, ln->y1, ln->x2, ln->y2,
514 coarse(ln->x1), fine(ln->x1),
515 coarse(ln->y1), fine(ln->y1),
516 coarse(ln->x2), fine(ln->x2),
517 coarse(ln->y2), fine(ln->y2),
518 ln->updown == 0 ? "vert" : ln->updown == 1 ? "up" : "down");
522 maxy = im->ysize * 16;
524 for (k = 0; k < clc; ++k) {
525 p_line const * line = lset + tllist[k].n;
526 if (line->miny > miny)
528 if (line->maxy < maxy)
530 POLY_DEB( printf(" line miny %g maxy %g\n", line->miny/16.0, line->maxy/16.0) );
532 POLY_DEB( printf("miny %g maxy %g\n", miny/16.0, maxy/16.0) );
534 for(cscl=startscan; cscl<stopscan; cscl++) {
535 pcord scan_miny = i_max(miny, cscl * 16);
536 pcord scan_maxy = i_min(maxy, (cscl + 1 ) * 16);
538 tempy = i_min(cscl*16+16, pset[i+1].y);
539 POLY_DEB( printf("evaluating scan line %d \n", cscl) );
540 for(k=0; k<clc-1; k+=2) {
541 POLY_DEB( printf("evaluating slice %d\n", k) );
542 render_slice_scanline(&templine, cscl, lset+tllist[k].n, lset+tllist[k+1].n, scan_miny, scan_maxy);
544 if (16*coarse(tempy) == tempy) {
545 POLY_DEB( printf("flushing scan line %d\n", cscl) );
546 flusher(im, &templine, cscl, ctx);
547 ss_scanline_reset(&templine);
551 scanline_flush(im, &templine, cscl, val);
552 ss_scanline_reset(&templine);
558 if (16*coarse(tempy) != tempy)
559 flusher(im, &templine, cscl-1, ctx);
561 ss_scanline_exorcise(&templine);
570 i_poly_poly_aa(i_img *im, int count, const i_polygon_t *polys,
571 const i_color *val) {
573 return i_poly_poly_aa_low(im, count, polys, &c, scanline_flush);
577 i_poly_aa(i_img *im, int l, const double *x, const double *y, const i_color *val) {
583 return i_poly_poly_aa(im, 1, &poly, val);
586 struct poly_render_state {
589 unsigned char *cover;
593 scanline_flush_render(i_img *im, ss_scanline *ss, int y, void *ctx) {
595 i_img_dim left, right;
596 struct poly_render_state *state = (struct poly_render_state *)ctx;
599 while (left < im->xsize && ss->line[left] <= 0)
601 if (left < im->xsize) {
603 /* since going from the left found something, moving from the
605 while (/* right > left && */ ss->line[right-1] <= 0)
608 /* convert to the format the render interface wants */
609 for (x = left; x < right; ++x) {
610 state->cover[x-left] = saturate(ss->line[x]);
612 i_render_fill(&state->render, left, y, right-left, state->cover,
618 i_poly_poly_aa_cfill(i_img *im, int count, const i_polygon_t *polys,
620 struct poly_render_state ctx;
623 i_render_init(&ctx.render, im, im->xsize);
625 ctx.cover = mymalloc(im->xsize);
627 result = i_poly_poly_aa_low(im, count, polys, &ctx, scanline_flush_render);
630 i_render_done(&ctx.render);
636 i_poly_aa_cfill(i_img *im, int l, const double *x, const double *y,
644 return i_poly_poly_aa_cfill(im, 1, &poly, fill);