6 #define IMTRUNC(x) ((int)((x)*16))
8 #define coarse(x) ((x)/16)
9 #define fine(x) ((x)%16)
28 int updown; /* -1 means down, 0 vertical, 1 up */
42 int *line; /* temporary buffer for scanline */
43 int linelen; /* length of scanline */
44 ss_pair *ss_list; /* list of start stop linepairs */
45 int ssnext; /* index of the next pair to use */
46 int sslen; /* maximum number of start stop pairs */
58 p_compy(const p_point *p1, const p_point *p2) {
59 if (p1->y > p2->y) return 1;
60 if (p1->y < p2->y) return -1;
66 p_compx(const p_slice *p1, const p_slice *p2) {
67 if (p1->x > p2->x) return 1;
68 if (p1->x < p2->x) return -1;
72 /* Change this to int? and round right goddamn it! */
76 p_eval_aty(p_line *l, pcord y) {
79 if (t) return ( (y-l->y1)*l->x2 + (l->y2-y)*l->x1 )/t;
80 return (l->x1+l->x2)/2.0;
85 p_eval_atx(p_line *l, pcord x) {
88 if (t) return ( (x-l->x1)*l->y2 + (l->x2-x)*l->y1 )/t;
89 return (l->y1+l->y2)/2.0;
94 line_set_new(const double *x, const double *y, int l) {
96 p_line *lset = mymalloc(sizeof(p_line) * l);
100 lset[i].x1 = IMTRUNC(x[i]);
101 lset[i].y1 = IMTRUNC(y[i]);
102 lset[i].x2 = IMTRUNC(x[(i+1)%l]);
103 lset[i].y2 = IMTRUNC(y[(i+1)%l]);
104 lset[i].miny=i_min(lset[i].y1,lset[i].y2);
105 lset[i].maxy=i_max(lset[i].y1,lset[i].y2);
106 lset[i].minx=i_min(lset[i].x1,lset[i].x2);
107 lset[i].maxx=i_max(lset[i].x1,lset[i].x2);
114 point_set_new(const double *x, const double *y, int l) {
116 p_point *pset = mymalloc(sizeof(p_point) * l);
120 pset[i].x=IMTRUNC(x[i]);
121 pset[i].y=IMTRUNC(y[i]);
129 p_line_dump(p_line *l) {
130 printf("%d (%d,%d)->(%d,%d) [%d-%d,%d-%d]\n", l->n, l->x1, l->y1, l->x2, l->y2,
131 l->minx, l->maxx, l->miny, l->maxy);
137 ss_scanline_reset(ss_scanline *ss) {
139 memset(ss->line, 0, sizeof(int) * ss->linelen);
144 ss_scanline_init(ss_scanline *ss, int linelen, int linepairs) {
145 ss->line = mymalloc( sizeof(int) * linelen );
146 ss->linelen = linelen;
147 ss->ss_list = mymalloc( sizeof(ss_pair) * linepairs );
148 ss->sslen = linepairs;
149 ss_scanline_reset(ss);
154 ss_scanline_exorcise(ss_scanline *ss) {
162 /* returns the number of matches */
166 lines_in_interval(p_line *lset, int l, p_slice *tllist, pcord minc, pcord maxc) {
170 if (lset[k].maxy > minc && lset[k].miny < maxc) {
171 if (lset[k].miny == lset[k].maxy) {
172 POLY_DEB( printf(" HORIZONTAL - skipped\n") );
174 tllist[count].x=p_eval_aty(&lset[k],(minc+maxc)/2.0 );
186 lines_in_interval_old(p_line *lset, int l, p_slice *tllist, pcord cc) {
190 if (cc >= lset[k].miny && cc <= lset[k].maxy) {
191 if (lset[k].miny == lset[k].maxy) {
192 POLY_DEB( printf(" HORIZONTAL - skipped\n") );
195 tllist[count].x=p_eval_aty(&lset[k],cc);
205 /* marks the up variable for all lines in a slice */
209 mark_updown_slices(p_line *lset, p_slice *tllist, int count) {
212 for(k=0; k<count; k+=2) {
213 l = lset + tllist[k].n;
215 if (l->y1 == l->y2) {
216 mm_log((1, "mark_updown_slices: horizontal line being marked: internal error!\n"));
220 l->updown = (l->x1 == l->x2) ?
224 (l->y1 > l->y2) ? -1 : 1
226 (l->y1 > l->y2) ? 1 : -1;
228 POLY_DEB( printf("marking left line %d as %s(%d)\n", l->n,
229 l->updown ? l->updown == 1 ? "up" : "down" : "vert", l->updown, l->updown)
233 mm_log((1, "Invalid polygon spec, odd number of line crossings.\n"));
237 r = lset + tllist[k+1].n;
238 if (r->y1 == r->y2) {
239 mm_log((1, "mark_updown_slices: horizontal line being marked: internal error!\n"));
243 r->updown = (r->x1 == r->x2) ?
247 (r->y1 > r->y2) ? -1 : 1
249 (r->y1 > r->y2) ? 1 : -1;
251 POLY_DEB( printf("marking right line %d as %s(%d)\n", r->n,
252 r->updown ? r->updown == 1 ? "up" : "down" : "vert", r->updown, r->updown)
262 if (in>255) { return 255; }
263 else if (in>0) return in;
267 typedef void (*scanline_flusher)(i_img *im, ss_scanline *ss, int y, const void *ctx);
269 /* This function must be modified later to do proper blending */
272 scanline_flush(i_img *im, ss_scanline *ss, int y, const void *ctx) {
275 i_color *val = (i_color *)ctx;
276 for(x=0; x<im->xsize; x++) {
277 tv = saturate(ss->line[x]);
278 i_gpix(im, x, y, &t);
279 for(ch=0; ch<im->channels; ch++)
280 t.channel[ch] = tv/255.0 * val->channel[ch] + (1.0-tv/255.0) * t.channel[ch];
281 i_ppix(im, x, y, &t);
289 trap_square(pcord xlen, pcord ylen, double xl, double yl) {
290 POLY_DEB( printf("trap_square: %d %d %.2f %.2f\n", xlen, ylen, xl, yl) );
291 return xlen*ylen-(xl*yl)/2.0;
296 pixel_coverage calculates the 'left side' pixel coverage of a pixel that is
297 within the min/max ranges. The shape always corresponds to a square with some
298 sort of a triangle cut from it (which can also yield a triangle).
304 pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord miny, pcord maxy) {
305 double lycross, rycross;
311 lycross = p_eval_atx(line, minx);
312 rycross = p_eval_atx(line, maxx);
313 l = lycross <= maxy && lycross >= miny; /* true if it enters through left side */
314 r = rycross <= maxy && rycross >= miny; /* true if it enters through left side */
317 printf("%4s(%+d): ", line->updown ? line->updown == 1 ? "up" : "down" : "vert", line->updown);
318 printf("(%2d,%2d) [%3d-%3d, %3d-%3d] lycross=%.2f rycross=%.2f", coarse(minx), coarse(miny), minx, maxx, miny, maxy, lycross, rycross);
319 printf(" l=%d r=%d\n", l, r)
323 return line->updown == 1 ?
324 (double)(maxx-minx) * (2.0*maxy-lycross-rycross)/2.0 /* up case */
326 (double)(maxx-minx) * (lycross+rycross-2*miny)/2.0; /* down case */
328 if (!l && !r) return (maxy-miny)*(maxx*2-p_eval_aty(line, miny)-p_eval_aty(line, maxy))/2.0;
331 return line->updown == 1 ?
332 trap_square(maxx-minx, maxy-miny, p_eval_aty(line, miny)-minx, p_eval_atx(line, minx)-miny) :
333 trap_square(maxx-minx, maxy-miny, p_eval_aty(line, maxy)-minx, maxy-p_eval_atx(line, minx));
337 int r = line->updown == 1 ?
338 (maxx-p_eval_aty(line, maxy))*(maxy-p_eval_atx(line, maxx))/2.0 :
339 (maxx-p_eval_aty(line, miny))*(p_eval_atx(line, maxx)-miny)/2.0;
349 handle the scanline slice in three steps
351 1. Where only the left edge is inside a pixel
352 2a. Where both left and right edge are inside a pixel
353 2b. Where neither left or right edge are inside a pixel
354 3. Where only the right edge is inside a pixel
359 render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r) {
361 pcord miny, maxy; /* y bounds in fine coordinates */
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 int cpix; /* x-coordinate of current pixel */
365 int thin; /* boolean for thin/thick segment */
366 int startpix; /* temporary variable for "start of this interval" */
367 int stoppix; /* temporary variable for "end of this interval" */
369 /* Find the y bounds of scanline_slice */
371 maxy = i_min( l->maxy, r->maxy );
372 miny = i_max( l->miny, r->miny );
374 maxy = i_min( maxy, (y+1)*16 );
375 miny = i_max( miny, y*16 );
377 lminx = i_min( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
378 lmaxx = i_max( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
380 rminx = i_min( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
381 rmaxx = i_max( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
383 thin = coarse(lmaxx) >= coarse(rminx);
385 startpix = i_max( coarse(lminx), 0 );
386 stoppix = i_min( coarse(rmaxx-1), ss->linelen-1 );
388 for(cpix=startpix; cpix<=stoppix; cpix++) {
389 int lt = coarse(lmaxx-1) >= cpix;
390 int rt = coarse(rminx) <= cpix;
394 POLY_DEB( printf("(%d,%d) lt=%d rt=%d\n", cpix, y, lt, rt) );
396 A = lt ? pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy) : 0;
397 B = lt ? 0 : 16*(maxy-miny);
398 C = rt ? pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy) : 0;
400 POLY_DEB( printf("A=%d B=%d C=%d\n", A, B, C) );
402 ss->line[cpix] += A+B-C;
412 render_slice_scanline_old(ss_scanline *ss, int y, p_line *l, p_line *r) {
414 pcord miny, maxy; /* y bounds in fine coordinates */
415 pcord lminx, lmaxx; /* left line min/max within y bounds in fine coords */
416 pcord rminx, rmaxx; /* right line min/max within y bounds in fine coords */
417 int cpix; /* x-coordinate of current pixel */
418 int thin; /* boolean for thin/thick segment */
419 int startpix; /* temporary variable for "start of this interval" */
420 int stoppix; /* temporary variable for "end of this interval" */
422 /* Find the y bounds of scanline_slice */
424 maxy = i_min( l->maxy, r->maxy );
425 miny = i_max( l->miny, r->miny );
427 maxy = i_min( maxy, (y+1)*16 );
428 miny = i_max( miny, y*16 );
430 lminx = i_min( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
431 lmaxx = i_max( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
433 rminx = i_min( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
434 rmaxx = i_max( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
436 thin = coarse(lmaxx) >= coarse(rminx);
440 startpix = coarse(lminx); /* includes tricky starting pixel */
441 stoppix = i_min(coarse(lmaxx), coarse(rminx) ); /* last pixel is tricky */
443 /* handle start pixel */
446 if (cpix < stoppix) {
447 ss->line[cpix] += pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy);
448 printf("%2d: step1 - start pixel\n", cpix);
451 for(cpix=startpix+1; cpix<stoppix; cpix++) {
452 printf("%2d: step1 pixel\n", cpix);
453 ss->line[cpix] += l->updown == 1 ?
454 8.0 * (2*maxy-p_eval_atx(l, 16*cpix)-p_eval_atx(l, 16*cpix+16)) /* up case */
456 8.0 * (p_eval_atx(l, 16*cpix)+p_eval_atx(l, 16*cpix+16)-2*miny); /* down case */
460 /* handle stop pixel */
462 if (thin) { /* step 2a */
463 startpix = coarse(rminx);
464 stoppix = coarse(lmaxx+15); /* one more than needed */
466 for(cpix=startpix; cpix<stoppix; cpix++) {
467 printf("%2d: step2a pixel\n", cpix);
469 pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy)
470 +(cpix*16+16-i_min(cpix*16+16, l->maxx))*(maxy-miny)
471 -pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy);
473 } else { /* step 2b */
474 stoppix = coarse(rminx);
475 for(/* cpix already correct */; cpix<stoppix; cpix++) {
476 printf("%2d: step2b pixel\n", cpix);
477 ss->line[cpix] += 16.0*(maxy-miny);
483 cpix = i_max(coarse(rminx), coarse(lmaxx+15));
484 stoppix = coarse(rmaxx-15);
486 printf("step3 from %d to %d\n", cpix, stoppix);
488 for(; cpix<stoppix; cpix++) {
489 printf("%2d: step3 pixel\n", cpix);
492 8.0 * (2*maxy-p_eval_atx(r, 16*cpix)-p_eval_atx(r, 16*cpix+16)) /* up case */
494 8.0 * (p_eval_atx(r, 16*cpix)+p_eval_atx(r, 16*cpix+16)-2*miny)); /* down case */
497 ss->line[cpix] += (16.0)*(maxy-miny) - pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy);
501 /* Antialiasing polygon algorithm
503 1. only nice polygons - no crossovers
504 2. 1/16 pixel resolution
505 3. full antialiasing ( complete spectrum of blends )
506 4. uses hardly any memory
507 5. no subsampling phase
511 1. Split into vertical intervals.
512 2. handle each interval
514 For each interval we must:
515 1. find which lines are in it
516 2. order the lines from in increasing x order.
517 since we are assuming no crossovers it is sufficent
518 to check a single point on each line.
524 1. Interval: A vertical segment in which no lines cross nor end.
525 2. Scanline: A physical line, contains 16 subpixels in the horizontal direction
526 3. Slice: A start stop line pair.
532 i_poly_aa_low(i_img *im, int l, const double *x, const double *y, void const *ctx, scanline_flusher flusher) {
533 int i ,k; /* Index variables */
534 int clc; /* Lines inside current interval */
536 int cscl; /* Current scanline */
538 ss_scanline templine; /* scanline accumulator */
539 p_point *pset; /* List of points in polygon */
540 p_line *lset; /* List of lines in polygon */
541 p_slice *tllist; /* List of slices */
543 mm_log((1, "i_poly_aa(im %p, l %d, x %p, y %p, ctx %p, flusher %p)\n", im, l, x, y, ctx, flusher));
546 mm_log((2, "(%.2f, %.2f)\n", x[i], y[i]));
552 setbuf(stdout, NULL);
555 tllist = mymalloc(sizeof(p_slice)*l);
557 ss_scanline_init(&templine, im->xsize, l);
559 pset = point_set_new(x, y, l);
560 lset = line_set_new(x, y, l);
563 qsort(pset, l, sizeof(p_point), (int(*)(const void *,const void *))p_compy);
567 printf("%d [ %d ] (%d , %d) -> (%d , %d) yspan ( %d , %d )\n",
568 i, lset[i].n, lset[i].x1, lset[i].y1, lset[i].x2, lset[i].y2, lset[i].miny, lset[i].maxy);
570 printf("MAIN LOOP\n\n");
574 /* loop on intervals */
575 for(i=0; i<l-1; i++) {
576 int startscan = i_max( coarse(pset[i].y), 0);
577 int stopscan = i_min( coarse(pset[i+1].y+15), im->ysize);
579 if (pset[i].y == pset[i+1].y) {
580 POLY_DEB( printf("current slice thickness = 0 => skipping\n") );
585 printf("current slice is %d: %d to %d ( cpoint %d ) scanlines %d to %d\n",
586 i, pset[i].y, pset[i+1].y, cc, startscan, stopscan)
590 clc = lines_in_interval(lset, l, tllist, pset[i].y, pset[i+1].y);
591 qsort(tllist, clc, sizeof(p_slice), (int(*)(const void *,const void *))p_compx);
593 mark_updown_slices(lset, tllist, clc);
595 POLY_DEB( printf("Interval contains %d lines\n", clc) );
597 for(k=0; k<clc; k++) {
599 printf("%d: line #%2d: (%2d, %2d)->(%2d, %2d) (%2d/%2d, %2d/%2d) -> (%2d/%2d, %2d/%2d) alignment=%s\n",
600 k, lno, ln->x1, ln->y1, ln->x2, ln->y2,
601 coarse(ln->x1), fine(ln->x1),
602 coarse(ln->y1), fine(ln->y1),
603 coarse(ln->x2), fine(ln->x2),
604 coarse(ln->y2), fine(ln->y2),
605 ln->updown == 0 ? "vert" : ln->updown == 1 ? "up" : "down")
608 for(cscl=startscan; cscl<stopscan; cscl++) {
609 tempy = i_min(cscl*16+16, pset[i+1].y);
610 POLY_DEB( printf("evaluating scan line %d \n", cscl) );
611 for(k=0; k<clc-1; k+=2) {
612 POLY_DEB( printf("evaluating slice %d\n", k) );
613 render_slice_scanline(&templine, cscl, lset+tllist[k].n, lset+tllist[k+1].n);
615 if (16*coarse(tempy) == tempy) {
616 POLY_DEB( printf("flushing scan line %d\n", cscl) );
617 flusher(im, &templine, cscl, ctx);
618 ss_scanline_reset(&templine);
622 scanline_flush(im, &templine, cscl, val);
623 ss_scanline_reset(&templine);
629 if (16*coarse(tempy) != tempy)
630 flusher(im, &templine, cscl-1, ctx);
632 ss_scanline_exorcise(&templine);
640 i_poly_aa(i_img *im, int l, const double *x, const double *y, const i_color *val) {
641 i_poly_aa_low(im, l, x, y, val, scanline_flush);
644 struct poly_cfill_state {
652 scanline_flush_cfill(i_img *im, ss_scanline *ss, int y, const void *ctx) {
656 struct poly_cfill_state const *state = (struct poly_cfill_state const *)ctx;
657 i_color *fillbuf = state->fillbuf;
658 i_color *line = state->linebuf;
661 while (left < im->xsize && ss->line[left] <= 0)
663 if (left < im->xsize) {
665 /* since going from the left found something, moving from the
667 while (/* right > left && */ ss->line[right-1] <= 0)
670 (state->fill->fill_with_color)(state->fill, left, y, right-left,
671 im->channels, fillbuf);
672 i_glin(im, left, right, y, line);
674 if (state->fill->combine) {
675 for (x = left; x < right; ++x) {
676 tv = saturate(ss->line[x]);
677 fillbuf[pos].channel[3] =
678 fillbuf[pos].channel[3] * tv / 255;
681 (state->fill->combine)(line, fillbuf, im->channels, right-left);
684 for (x = left; x < right; ++x) {
685 tv = saturate(ss->line[x]);
688 line[pos] = fillbuf[pos];
691 i_color *to = line + pos;
692 i_color *from = fillbuf + pos;
693 for (ch = 0; ch < im->channels; ++ch) {
694 to->channel[ch] = (tv * from->channel[ch] +
695 (255 - tv) * to->channel[ch]) / 255;
702 i_plin(im, left, right, y, line);
706 struct poly_cfill_state_f {
714 scanline_flush_cfill_f(i_img *im, ss_scanline *ss, int y, const void *ctx) {
718 struct poly_cfill_state_f const *state = (struct poly_cfill_state_f const *)ctx;
719 i_fcolor *fillbuf = state->fillbuf;
720 i_fcolor *line = state->linebuf;
723 while (left < im->xsize && ss->line[left] <= 0)
725 if (left < im->xsize) {
727 /* since going from the left found something, moving from the
729 while (/* right > left && */ ss->line[right-1] <= 0)
732 (state->fill->fill_with_fcolor)(state->fill, left, y, right-left,
733 im->channels, fillbuf);
734 i_glinf(im, left, right, y, line);
736 if (state->fill->combinef) {
737 for (x = left; x < right; ++x) {
738 tv = saturate(ss->line[x]);
739 fillbuf[pos].channel[3] =
740 fillbuf[pos].channel[3] * tv / 255;
743 (state->fill->combinef)(line, fillbuf, im->channels, right-left);
746 for (x = left; x < right; ++x) {
747 tv = saturate(ss->line[x]);
750 line[pos] = fillbuf[pos];
753 i_fcolor *to = line + pos;
754 i_fcolor *from = fillbuf + pos;
755 for (ch = 0; ch < im->channels; ++ch) {
756 to->channel[ch] = (tv * from->channel[ch] +
757 (255 - tv) * to->channel[ch]) / 255;
764 i_plinf(im, left, right, y, line);
769 i_poly_aa_cfill(i_img *im, int l, const double *x, const double *y, i_fill_t *fill) {
770 if (im->bits == i_8_bits && fill->fill_with_color) {
771 struct poly_cfill_state ctx;
772 ctx.fillbuf = mymalloc(sizeof(i_color) * im->xsize * 2);
773 ctx.linebuf = ctx.fillbuf + im->xsize;
774 ctx.cover = mymalloc(sizeof(int) * im->xsize);
776 i_poly_aa_low(im, l, x, y, &ctx, scanline_flush_cfill);
781 struct poly_cfill_state_f ctx;
782 ctx.fillbuf = mymalloc(sizeof(i_fcolor) * im->xsize * 2);
783 ctx.linebuf = ctx.fillbuf + im->xsize;
784 ctx.cover = mymalloc(sizeof(int) * im->xsize);
786 i_poly_aa_low(im, l, x, y, &ctx, scanline_flush_cfill_f);