1 #define IMAGER_NO_CONTEXT
8 #define IMTRUNC(x) ((int)((x)*16))
10 #define coarse(x) ((x)/16)
11 #define fine(x) ((x)%16)
13 /*#define DEBUG_POLY*/
21 typedef i_img_dim pcord;
34 int updown; /* -1 means down, 0 vertical, 1 up */
35 int dir; /* 1 for down, -1 for up */
44 int *line; /* temporary buffer for scanline */
45 i_img_dim linelen; /* length of scanline */
50 p_compy(const p_point *p1, const p_point *p2) {
51 if (p1->y > p2->y) return 1;
52 if (p1->y < p2->y) return -1;
58 p_compx(const p_slice *p1, const p_slice *p2) {
59 if (p1->x > p2->x) return 1;
60 if (p1->x < p2->x) return -1;
64 /* Change this to int? and round right goddamn it! */
68 p_eval_aty(p_line *l, pcord y) {
71 if (t) return ( (y-l->y1)*l->x2 + (l->y2-y)*l->x1 )/t;
72 return (l->x1+l->x2)/2.0;
77 p_eval_atx(p_line *l, pcord x) {
80 if (t) return ( (x-l->x1)*l->y2 + (l->x2-x)*l->y1 )/t;
81 return (l->y1+l->y2)/2.0;
86 line_set_new(const i_polygon_t *polys, size_t count, size_t *line_count) {
91 for (i = 0; i < count; ++i)
92 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++) {
101 line->x1 = IMTRUNC(p->x[j]);
102 line->y1 = IMTRUNC(p->y[j]);
103 line->x2 = IMTRUNC(p->x[(j + 1) % p->count]);
104 line->y2 = IMTRUNC(p->y[(j + 1) % p->count]);
106 /* don't include purely horizontal lines, we don't usefully
107 intersect with them. */
108 if (line->y1 == line->y2)
111 line->miny = i_min(line->y1, line->y2);
112 line->maxy = i_max(line->y1, line->y2);
113 line->minx = i_min(line->x1, line->x2);
114 line->maxx = i_max(line->x1, line->x2);
126 point_set_new(const i_polygon_t *polys, size_t count, size_t *point_count) {
131 for (i = 0; i < count; ++i)
132 points += polys[i].count;
134 *point_count = points;
136 pt = pset = mymalloc(sizeof(p_point) * points);
139 for (i = 0; i < count; ++i) {
140 const i_polygon_t *p = polys + i;
142 for(j = 0; j < p->count; j++) {
144 pt->x = IMTRUNC(p->x[j]);
145 pt->y = IMTRUNC(p->y[j]);
154 ss_scanline_reset(ss_scanline *ss) {
155 memset(ss->line, 0, sizeof(int) * ss->linelen);
160 ss_scanline_init(ss_scanline *ss, i_img_dim linelen, int linepairs) {
161 ss->line = mymalloc( sizeof(int) * linelen );
162 ss->linelen = linelen;
163 ss_scanline_reset(ss);
168 ss_scanline_exorcise(ss_scanline *ss) {
175 /* returns the number of matches */
179 lines_in_interval(p_line *lset, int l, p_slice *tllist, pcord minc, pcord maxc) {
183 if (lset[k].maxy > minc && lset[k].miny < maxc) {
184 if (lset[k].miny == lset[k].maxy) {
185 POLY_DEB( printf(" HORIZONTAL - skipped\n") );
187 tllist[count].x=p_eval_aty(&lset[k],(minc+maxc)/2.0 );
196 /* marks the up variable for all lines in a slice */
200 mark_updown_slices(pIMCTX, p_line *lset, p_slice *tllist, int count) {
203 for(k=0; k<count; k+=2) {
204 l = lset + tllist[k].n;
206 if (l->y1 == l->y2) {
207 im_log((aIMCTX,1, "mark_updown_slices: horizontal line being marked: internal error!\n"));
211 l->updown = (l->x1 == l->x2) ?
215 (l->y1 > l->y2) ? -1 : 1
217 (l->y1 > l->y2) ? 1 : -1;
218 l->dir = l->y1 < l->y2 ? 1 : -1;
220 POLY_DEB( printf("marking left line %d as %s(%d)\n", l->n,
221 l->updown ? l->updown == 1 ? "up" : "down" : "vert", l->updown, l->updown)
225 im_log((aIMCTX, 1, "Invalid polygon spec, odd number of line crossings.\n"));
229 r = lset + tllist[k+1].n;
230 if (r->y1 == r->y2) {
231 im_log((aIMCTX, 1, "mark_updown_slices: horizontal line being marked: internal error!\n"));
235 r->updown = (r->x1 == r->x2) ?
239 (r->y1 > r->y2) ? -1 : 1
241 (r->y1 > r->y2) ? 1 : -1;
242 r->dir = r->y1 < r->y2 ? 1 : -1;
244 POLY_DEB( printf("marking right line %d as %s(%d)\n", r->n,
245 r->updown ? r->updown == 1 ? "up" : "down" : "vert", r->updown, r->updown)
253 if (in>255) { return 255; }
254 else if (in>0) return in;
258 typedef void (*scanline_flusher)(i_img *im, ss_scanline *ss, int y, void *ctx);
260 /* This function must be modified later to do proper blending */
263 scanline_flush(i_img *im, ss_scanline *ss, int y, void *ctx) {
266 i_color *val = (i_color *)ctx;
267 POLY_DEB( printf("Flushing line %d\n", y) );
268 for(x=0; x<im->xsize; x++) {
269 tv = saturate(ss->line[x]);
270 i_gpix(im, x, y, &t);
271 for(ch=0; ch<im->channels; ch++)
272 t.channel[ch] = tv/255.0 * val->channel[ch] + (1.0-tv/255.0) * t.channel[ch];
273 i_ppix(im, x, y, &t);
281 trap_square(pcord xlen, pcord ylen, double xl, double yl) {
282 POLY_DEB( printf("trap_square: %d %d %.2f %.2f\n", xlen, ylen, xl, yl) );
283 return xlen*ylen-(xl*yl)/2.0;
288 pixel_coverage calculates the 'left side' pixel coverage of a pixel that is
289 within the min/max ranges. The shape always corresponds to a square with some
290 sort of a triangle cut from it (which can also yield a triangle).
296 pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord miny, pcord maxy) {
297 double lycross, rycross;
302 printf(" pixel_coverage(..., minx %g, maxx%g, miny %g, maxy %g)\n",
303 minx/16.0, maxx/16.0, miny/16.0, maxy/16.0)
309 lycross = p_eval_atx(line, minx);
310 rycross = p_eval_atx(line, maxx);
311 l = lycross <= maxy && lycross >= miny; /* true if it enters through left side */
312 r = rycross <= maxy && rycross >= miny; /* true if it enters through left side */
315 printf(" %4s(%+d): ", line->updown ? line->updown == 1 ? "up" : "down" : "vert", line->updown);
316 printf(" (%2d,%2d) [%3d-%3d, %3d-%3d] lycross=%.2f rycross=%.2f", coarse(minx), coarse(miny), minx, maxx, miny, maxy, lycross, rycross);
317 printf(" l=%d r=%d\n", l, r)
321 return line->updown == 1 ?
322 (double)(maxx-minx) * (2.0*maxy-lycross-rycross)/2.0 /* up case */
324 (double)(maxx-minx) * (lycross+rycross-2*miny)/2.0; /* down case */
326 if (!l && !r) return (maxy-miny)*(maxx*2-p_eval_aty(line, miny)-p_eval_aty(line, maxy))/2.0;
329 return line->updown == 1 ?
330 trap_square(maxx-minx, maxy-miny, p_eval_aty(line, miny)-minx, p_eval_atx(line, minx)-miny) :
331 trap_square(maxx-minx, maxy-miny, p_eval_aty(line, maxy)-minx, maxy-p_eval_atx(line, minx));
335 int r = line->updown == 1 ?
336 (maxx-p_eval_aty(line, maxy))*(maxy-p_eval_atx(line, maxx))/2.0 :
337 (maxx-p_eval_aty(line, miny))*(p_eval_atx(line, maxx)-miny)/2.0;
342 return 0; /* silence compiler warning */
350 handle the scanline slice in three steps
352 1. Where only the left edge is inside a pixel
353 2a. Where both left and right edge are inside a pixel
354 2b. Where neither left or right edge are inside a pixel
355 3. Where only the right edge is inside a pixel
360 render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r, pcord miny, pcord maxy) {
362 pcord lminx, lmaxx; /* left line min/max within y bounds in fine coords */
363 pcord rminx, rmaxx; /* right line min/max within y bounds in fine coords */
364 i_img_dim cpix; /* x-coordinate of current pixel */
365 i_img_dim startpix; /* temporary variable for "start of this interval" */
366 i_img_dim stoppix; /* temporary variable for "end of this interval" */
368 /* Find the y bounds of scanline_slice */
372 printf("render_slice_scanline(..., y=%d)\n");
373 printf(" left n=%d p1(%.2g, %.2g) p2(%.2g,%.2g) min(%.2g, %.2g) max(%.2g,%.2g) updown(%d)\n",
374 l->n, l->x1/16.0, l->y1/16.0, l->x2/16.0, l->y2/16.0,
375 l->minx/16.0, l->miny/16.0, l->maxx/16.0, l->maxy/16.0,
377 printf(" right n=%d p1(%.2g, %.2g) p2(%.2g,%.2g) min(%.2g, %.2g) max(%.2g,%.2g) updown(%d)\n",
378 r->n, r->x1/16.0, r->y1/16.0, r->x2/16.0, r->y2/16.0,
379 r->minx/16.0, r->miny/16.0, r->maxx/16.0, r->maxy/16.0,
383 lminx = i_min( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
384 lmaxx = i_max( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
386 rminx = i_min( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
387 rmaxx = i_max( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
389 startpix = i_max( coarse(lminx), 0 );
390 stoppix = i_min( coarse(rmaxx-1), ss->linelen-1 );
392 POLY_DEB( printf(" miny=%g maxy=%g\n", miny/16.0, maxy/16.0) );
394 for(cpix=startpix; cpix<=stoppix; cpix++) {
395 int lt = coarse(lmaxx-1) >= cpix;
396 int rt = coarse(rminx) <= cpix;
400 POLY_DEB( printf(" (%d,%d) lt=%d rt=%d\n", cpix, y, lt, rt) );
402 A = lt ? pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy) : 0;
403 B = lt ? 0 : 16*(maxy-miny);
404 C = rt ? pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy) : 0;
406 POLY_DEB( printf(" A=%d B=%d C=%d\n", A, B, C) );
408 ss->line[cpix] += A+B-C;
411 POLY_DEB( printf("end render_slice_scanline()\n") );
414 /* Antialiasing polygon algorithm
416 1. only nice polygons - no crossovers
417 2. 1/16 pixel resolution
418 3. full antialiasing ( complete spectrum of blends )
419 4. uses hardly any memory
420 5. no subsampling phase
424 1. Split into vertical intervals.
425 2. handle each interval
427 For each interval we must:
428 1. find which lines are in it
429 2. order the lines from in increasing x order.
430 since we are assuming no crossovers it is sufficent
431 to check a single point on each line.
437 1. Interval: A vertical segment in which no lines cross nor end.
438 2. Scanline: A physical line, contains 16 subpixels in the horizontal direction
439 3. Slice: A start stop line pair.
445 i_poly_poly_aa_low(i_img *im, int count, const i_polygon_t *polys,
446 i_poly_fill_mode_t mode, void *ctx,
447 scanline_flusher flusher) {
448 int i ,k; /* Index variables */
449 i_img_dim clc; /* Lines inside current interval */
450 /* initialize to avoid compiler warnings */
452 i_img_dim cscl = 0; /* Current scanline */
454 ss_scanline templine; /* scanline accumulator */
455 p_point *pset; /* List of points in polygon */
456 p_line *lset; /* List of lines in polygon */
457 p_slice *tllist; /* List of slices */
458 size_t pcount, lcount;
461 im_log((aIMCTX, 1, "i_poly_poly_aa_low(im %p, count %d, polys %p, ctx %p, flusher %p)\n", im, count, polys, ctx, flusher));
466 i_push_error(0, "no polygons to draw");
470 for (k = 0; k < count; ++k) {
471 if (polys[k].count < 3) {
472 i_push_error(0, "polygons must have at least 3 points");
477 for (k = 0; k < count; ++k) {
478 const i_polygon_t *p = polys + k;
479 im_log((aIMCTX, 2, "poly %d\n", k));
480 for(i = 0; i < p->count; i++) {
481 im_log((aIMCTX, 2, " (%.2f, %.2f)\n", p->x[i], p->y[i]));
488 setbuf(stdout, NULL);
491 pset = point_set_new(polys, count, &pcount);
492 lset = line_set_new(polys, count, &lcount);
494 ss_scanline_init(&templine, im->xsize, lcount);
496 tllist = mymalloc(sizeof(p_slice) * lcount);
498 qsort(pset, pcount, sizeof(p_point), (int(*)(const void *,const void *))p_compy);
501 for(i=0;i<lcount;i++) {
502 printf("%d [ %d ] (%d , %d) -> (%d , %d) yspan ( %d , %d )\n",
503 i, lset[i].n, lset[i].x1, lset[i].y1, lset[i].x2, lset[i].y2, lset[i].miny, lset[i].maxy);
505 printf("MAIN LOOP\n\n");
509 /* loop on intervals */
510 for(i=0; i<pcount-1; i++) {
511 i_img_dim startscan = i_max( coarse(pset[i].y), 0);
512 i_img_dim stopscan = i_min( coarse(pset[i+1].y+15), im->ysize);
513 pcord miny, maxy; /* y bounds in fine coordinates */
515 POLY_DEB( pcord cc = (pset[i].y + pset[i+1].y)/2 );
518 printf("current slice is %d: %d to %d ( cpoint %d ) scanlines %d to %d\n",
519 i, pset[i].y, pset[i+1].y, cc, startscan, stopscan)
522 if (pset[i].y == pset[i+1].y) {
523 POLY_DEB( printf("current slice thickness = 0 => skipping\n") );
527 clc = lines_in_interval(lset, lcount, tllist, pset[i].y, pset[i+1].y);
528 qsort(tllist, clc, sizeof(p_slice), (int(*)(const void *,const void *))p_compx);
530 mark_updown_slices(aIMCTX, lset, tllist, clc);
534 printf("Interval contains %d lines\n", clc);
535 for(k=0; k<clc; k++) {
536 int lno = tllist[k].n;
537 p_line *ln = lset+lno;
538 printf("%d: line #%2d: (%2d, %2d)->(%2d, %2d) (%2d/%2d, %2d/%2d) -> (%2d/%2d, %2d/%2d) alignment=%s\n",
539 k, lno, ln->x1, ln->y1, ln->x2, ln->y2,
540 coarse(ln->x1), fine(ln->x1),
541 coarse(ln->y1), fine(ln->y1),
542 coarse(ln->x2), fine(ln->x2),
543 coarse(ln->y2), fine(ln->y2),
544 ln->updown == 0 ? "vert" : ln->updown == 1 ? "up" : "down");
548 maxy = im->ysize * 16;
550 for (k = 0; k < clc; ++k) {
551 p_line const * line = lset + tllist[k].n;
552 if (line->miny > miny)
554 if (line->maxy < maxy)
556 POLY_DEB( printf(" line miny %g maxy %g\n", line->miny/16.0, line->maxy/16.0) );
558 POLY_DEB( printf("miny %g maxy %g\n", miny/16.0, maxy/16.0) );
560 for(cscl=startscan; cscl<stopscan; cscl++) {
561 pcord scan_miny = i_max(miny, cscl * 16);
562 pcord scan_maxy = i_min(maxy, (cscl + 1 ) * 16);
564 tempy = i_min(cscl*16+16, pset[i+1].y);
565 POLY_DEB( printf("evaluating scan line %d \n", cscl) );
566 if (mode == i_pfm_evenodd) {
567 for(k=0; k<clc-1; k+=2) {
568 POLY_DEB( printf("evaluating slice %d\n", k) );
569 render_slice_scanline(&templine, cscl, lset+tllist[k].n, lset+tllist[k+1].n, scan_miny, scan_maxy);
575 p_line *left = lset + tllist[k++].n;
576 p_line *current = NULL;
579 while (k < clc && acc) {
580 current = lset + tllist[k++].n;
584 render_slice_scanline(&templine, cscl, left, current,
585 scan_miny, scan_maxy);
589 if (16*coarse(tempy) == tempy) {
590 POLY_DEB( printf("flushing scan line %d\n", cscl) );
591 flusher(im, &templine, cscl, ctx);
592 ss_scanline_reset(&templine);
596 scanline_flush(im, &templine, cscl, val);
597 ss_scanline_reset(&templine);
603 if (16*coarse(tempy) != tempy)
604 flusher(im, &templine, cscl-1, ctx);
606 ss_scanline_exorcise(&templine);
615 =item i_poly_poly_aa(im, count, polys, mode, color)
616 =synopsis i_poly_poly_aa(im, 1, &poly, mode, color);
619 Fill the C<count> polygons defined by C<polys> the color specified by
622 At least one polygon must be supplied.
624 All polygons must have at least 3 points.
630 i_poly_poly_aa(i_img *im, int count, const i_polygon_t *polys,
631 i_poly_fill_mode_t mode, const i_color *val) {
633 return i_poly_poly_aa_low(im, count, polys, mode, &c, scanline_flush);
637 =item i_poly_aa_m(im, count, x, y, mode, color)
638 =synopsis i_poly_aa_m(im, count, x, y, mode, color);
641 Fill a polygon defined by the points specified by the x and y arrays with
642 the color specified by C<color>.
648 i_poly_aa_m(i_img *im, int l, const double *x, const double *y,
649 i_poly_fill_mode_t mode, const i_color *val) {
655 return i_poly_poly_aa(im, 1, &poly, mode, val);
659 i_poly_aa(i_img *im, int l, const double *x, const double *y, const i_color *val) {
665 return i_poly_poly_aa(im, 1, &poly, i_pfm_evenodd, val);
668 struct poly_render_state {
671 unsigned char *cover;
675 scanline_flush_render(i_img *im, ss_scanline *ss, int y, void *ctx) {
677 i_img_dim left, right;
678 struct poly_render_state *state = (struct poly_render_state *)ctx;
681 while (left < im->xsize && ss->line[left] <= 0)
683 if (left < im->xsize) {
685 /* since going from the left found something, moving from the
687 while (/* right > left && */ ss->line[right-1] <= 0)
690 /* convert to the format the render interface wants */
691 for (x = left; x < right; ++x) {
692 state->cover[x-left] = saturate(ss->line[x]);
694 i_render_fill(&state->render, left, y, right-left, state->cover,
700 =item i_poly_poly_aa_cfill(im, count, polys, mode, fill)
701 =synopsis i_poly_poly_aa_cfill(im, 1, &poly, mode, fill);
704 Fill the C<count> polygons defined by C<polys> the fill specified by
707 At least one polygon must be supplied.
709 All polygons must have at least 3 points.
715 i_poly_poly_aa_cfill(i_img *im, int count, const i_polygon_t *polys,
716 i_poly_fill_mode_t mode, i_fill_t *fill) {
717 struct poly_render_state ctx;
720 i_render_init(&ctx.render, im, im->xsize);
722 ctx.cover = mymalloc(im->xsize);
724 result = i_poly_poly_aa_low(im, count, polys, mode, &ctx,
725 scanline_flush_render);
728 i_render_done(&ctx.render);
734 =item i_poly_aa_cfill_m(im, count, x, y, mode, fill)
735 =synopsis i_poly_aa_cfill(im, count, x, y, mode, fill);
738 Fill a polygon defined by the points specified by the x and y arrays with
739 the fill specified by C<fill>.
745 i_poly_aa_cfill_m(i_img *im, int l, const double *x, const double *y,
746 i_poly_fill_mode_t mode, i_fill_t *fill) {
753 return i_poly_poly_aa_cfill(im, 1, &poly, mode, fill);
757 i_poly_aa_cfill(i_img *im, int l, const double *x, const double *y,
765 return i_poly_poly_aa_cfill(im, 1, &poly, i_pfm_evenodd, fill);