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]);
128 ss_scanline_reset(ss_scanline *ss) {
130 memset(ss->line, 0, sizeof(int) * ss->linelen);
135 ss_scanline_init(ss_scanline *ss, int linelen, int linepairs) {
136 ss->line = mymalloc( sizeof(int) * linelen );
137 ss->linelen = linelen;
138 ss->ss_list = mymalloc( sizeof(ss_pair) * linepairs );
139 ss->sslen = linepairs;
140 ss_scanline_reset(ss);
145 ss_scanline_exorcise(ss_scanline *ss) {
153 /* returns the number of matches */
157 lines_in_interval(p_line *lset, int l, p_slice *tllist, pcord minc, pcord maxc) {
161 if (lset[k].maxy > minc && lset[k].miny < maxc) {
162 if (lset[k].miny == lset[k].maxy) {
163 POLY_DEB( printf(" HORIZONTAL - skipped\n") );
165 tllist[count].x=p_eval_aty(&lset[k],(minc+maxc)/2.0 );
174 /* marks the up variable for all lines in a slice */
178 mark_updown_slices(p_line *lset, p_slice *tllist, int count) {
181 for(k=0; k<count; k+=2) {
182 l = lset + tllist[k].n;
184 if (l->y1 == l->y2) {
185 mm_log((1, "mark_updown_slices: horizontal line being marked: internal error!\n"));
189 l->updown = (l->x1 == l->x2) ?
193 (l->y1 > l->y2) ? -1 : 1
195 (l->y1 > l->y2) ? 1 : -1;
197 POLY_DEB( printf("marking left line %d as %s(%d)\n", l->n,
198 l->updown ? l->updown == 1 ? "up" : "down" : "vert", l->updown, l->updown)
202 mm_log((1, "Invalid polygon spec, odd number of line crossings.\n"));
206 r = lset + tllist[k+1].n;
207 if (r->y1 == r->y2) {
208 mm_log((1, "mark_updown_slices: horizontal line being marked: internal error!\n"));
212 r->updown = (r->x1 == r->x2) ?
216 (r->y1 > r->y2) ? -1 : 1
218 (r->y1 > r->y2) ? 1 : -1;
220 POLY_DEB( printf("marking right line %d as %s(%d)\n", r->n,
221 r->updown ? r->updown == 1 ? "up" : "down" : "vert", r->updown, r->updown)
231 if (in>255) { return 255; }
232 else if (in>0) return in;
236 typedef void (*scanline_flusher)(i_img *im, ss_scanline *ss, int y, void *ctx);
238 /* This function must be modified later to do proper blending */
241 scanline_flush(i_img *im, ss_scanline *ss, int y, void *ctx) {
244 i_color *val = (i_color *)ctx;
245 for(x=0; x<im->xsize; x++) {
246 tv = saturate(ss->line[x]);
247 i_gpix(im, x, y, &t);
248 for(ch=0; ch<im->channels; ch++)
249 t.channel[ch] = tv/255.0 * val->channel[ch] + (1.0-tv/255.0) * t.channel[ch];
250 i_ppix(im, x, y, &t);
258 trap_square(pcord xlen, pcord ylen, double xl, double yl) {
259 POLY_DEB( printf("trap_square: %d %d %.2f %.2f\n", xlen, ylen, xl, yl) );
260 return xlen*ylen-(xl*yl)/2.0;
265 pixel_coverage calculates the 'left side' pixel coverage of a pixel that is
266 within the min/max ranges. The shape always corresponds to a square with some
267 sort of a triangle cut from it (which can also yield a triangle).
273 pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord miny, pcord maxy) {
274 double lycross, rycross;
280 lycross = p_eval_atx(line, minx);
281 rycross = p_eval_atx(line, maxx);
282 l = lycross <= maxy && lycross >= miny; /* true if it enters through left side */
283 r = rycross <= maxy && rycross >= miny; /* true if it enters through left side */
286 printf("%4s(%+d): ", line->updown ? line->updown == 1 ? "up" : "down" : "vert", line->updown);
287 printf("(%2d,%2d) [%3d-%3d, %3d-%3d] lycross=%.2f rycross=%.2f", coarse(minx), coarse(miny), minx, maxx, miny, maxy, lycross, rycross);
288 printf(" l=%d r=%d\n", l, r)
292 return line->updown == 1 ?
293 (double)(maxx-minx) * (2.0*maxy-lycross-rycross)/2.0 /* up case */
295 (double)(maxx-minx) * (lycross+rycross-2*miny)/2.0; /* down case */
297 if (!l && !r) return (maxy-miny)*(maxx*2-p_eval_aty(line, miny)-p_eval_aty(line, maxy))/2.0;
300 return line->updown == 1 ?
301 trap_square(maxx-minx, maxy-miny, p_eval_aty(line, miny)-minx, p_eval_atx(line, minx)-miny) :
302 trap_square(maxx-minx, maxy-miny, p_eval_aty(line, maxy)-minx, maxy-p_eval_atx(line, minx));
306 int r = line->updown == 1 ?
307 (maxx-p_eval_aty(line, maxy))*(maxy-p_eval_atx(line, maxx))/2.0 :
308 (maxx-p_eval_aty(line, miny))*(p_eval_atx(line, maxx)-miny)/2.0;
312 return 0; /* silence compiler warning */
320 handle the scanline slice in three steps
322 1. Where only the left edge is inside a pixel
323 2a. Where both left and right edge are inside a pixel
324 2b. Where neither left or right edge are inside a pixel
325 3. Where only the right edge is inside a pixel
330 render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r) {
332 pcord miny, maxy; /* y bounds in fine coordinates */
333 pcord lminx, lmaxx; /* left line min/max within y bounds in fine coords */
334 pcord rminx, rmaxx; /* right line min/max within y bounds in fine coords */
335 int cpix; /* x-coordinate of current pixel */
336 int thin; /* boolean for thin/thick segment */
337 int startpix; /* temporary variable for "start of this interval" */
338 int stoppix; /* temporary variable for "end of this interval" */
340 /* Find the y bounds of scanline_slice */
342 maxy = i_min( l->maxy, r->maxy );
343 miny = i_max( l->miny, r->miny );
345 maxy = i_min( maxy, (y+1)*16 );
346 miny = i_max( miny, y*16 );
348 lminx = i_min( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
349 lmaxx = i_max( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
351 rminx = i_min( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
352 rmaxx = i_max( p_eval_aty(r, maxy), p_eval_aty(r, miny) );
354 thin = coarse(lmaxx) >= coarse(rminx);
356 startpix = i_max( coarse(lminx), 0 );
357 stoppix = i_min( coarse(rmaxx-1), ss->linelen-1 );
359 for(cpix=startpix; cpix<=stoppix; cpix++) {
360 int lt = coarse(lmaxx-1) >= cpix;
361 int rt = coarse(rminx) <= cpix;
365 POLY_DEB( printf("(%d,%d) lt=%d rt=%d\n", cpix, y, lt, rt) );
367 A = lt ? pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy) : 0;
368 B = lt ? 0 : 16*(maxy-miny);
369 C = rt ? pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy) : 0;
371 POLY_DEB( printf("A=%d B=%d C=%d\n", A, B, C) );
373 ss->line[cpix] += A+B-C;
379 /* Antialiasing polygon algorithm
381 1. only nice polygons - no crossovers
382 2. 1/16 pixel resolution
383 3. full antialiasing ( complete spectrum of blends )
384 4. uses hardly any memory
385 5. no subsampling phase
389 1. Split into vertical intervals.
390 2. handle each interval
392 For each interval we must:
393 1. find which lines are in it
394 2. order the lines from in increasing x order.
395 since we are assuming no crossovers it is sufficent
396 to check a single point on each line.
402 1. Interval: A vertical segment in which no lines cross nor end.
403 2. Scanline: A physical line, contains 16 subpixels in the horizontal direction
404 3. Slice: A start stop line pair.
410 i_poly_aa_low(i_img *im, int l, const double *x, const double *y, void *ctx, scanline_flusher flusher) {
411 int i ,k; /* Index variables */
412 int clc; /* Lines inside current interval */
413 /* initialize to avoid compiler warnings */
415 int cscl = 0; /* Current scanline */
417 ss_scanline templine; /* scanline accumulator */
418 p_point *pset; /* List of points in polygon */
419 p_line *lset; /* List of lines in polygon */
420 p_slice *tllist; /* List of slices */
422 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));
425 mm_log((2, "(%.2f, %.2f)\n", x[i], y[i]));
431 setbuf(stdout, NULL);
434 tllist = mymalloc(sizeof(p_slice)*l);
436 ss_scanline_init(&templine, im->xsize, l);
438 pset = point_set_new(x, y, l);
439 lset = line_set_new(x, y, l);
442 qsort(pset, l, sizeof(p_point), (int(*)(const void *,const void *))p_compy);
446 printf("%d [ %d ] (%d , %d) -> (%d , %d) yspan ( %d , %d )\n",
447 i, lset[i].n, lset[i].x1, lset[i].y1, lset[i].x2, lset[i].y2, lset[i].miny, lset[i].maxy);
449 printf("MAIN LOOP\n\n");
453 /* loop on intervals */
454 for(i=0; i<l-1; i++) {
455 int startscan = i_max( coarse(pset[i].y), 0);
456 int stopscan = i_min( coarse(pset[i+1].y+15), im->ysize);
458 if (pset[i].y == pset[i+1].y) {
459 POLY_DEB( printf("current slice thickness = 0 => skipping\n") );
464 printf("current slice is %d: %d to %d ( cpoint %d ) scanlines %d to %d\n",
465 i, pset[i].y, pset[i+1].y, cc, startscan, stopscan)
469 clc = lines_in_interval(lset, l, tllist, pset[i].y, pset[i+1].y);
470 qsort(tllist, clc, sizeof(p_slice), (int(*)(const void *,const void *))p_compx);
472 mark_updown_slices(lset, tllist, clc);
474 POLY_DEB( printf("Interval contains %d lines\n", clc) );
476 for(k=0; k<clc; k++) {
478 printf("%d: line #%2d: (%2d, %2d)->(%2d, %2d) (%2d/%2d, %2d/%2d) -> (%2d/%2d, %2d/%2d) alignment=%s\n",
479 k, lno, ln->x1, ln->y1, ln->x2, ln->y2,
480 coarse(ln->x1), fine(ln->x1),
481 coarse(ln->y1), fine(ln->y1),
482 coarse(ln->x2), fine(ln->x2),
483 coarse(ln->y2), fine(ln->y2),
484 ln->updown == 0 ? "vert" : ln->updown == 1 ? "up" : "down")
487 for(cscl=startscan; cscl<stopscan; cscl++) {
488 tempy = i_min(cscl*16+16, pset[i+1].y);
489 POLY_DEB( printf("evaluating scan line %d \n", cscl) );
490 for(k=0; k<clc-1; k+=2) {
491 POLY_DEB( printf("evaluating slice %d\n", k) );
492 render_slice_scanline(&templine, cscl, lset+tllist[k].n, lset+tllist[k+1].n);
494 if (16*coarse(tempy) == tempy) {
495 POLY_DEB( printf("flushing scan line %d\n", cscl) );
496 flusher(im, &templine, cscl, ctx);
497 ss_scanline_reset(&templine);
501 scanline_flush(im, &templine, cscl, val);
502 ss_scanline_reset(&templine);
508 if (16*coarse(tempy) != tempy)
509 flusher(im, &templine, cscl-1, ctx);
511 ss_scanline_exorcise(&templine);
519 i_poly_aa(i_img *im, int l, const double *x, const double *y, const i_color *val) {
521 i_poly_aa_low(im, l, x, y, &c, scanline_flush);
524 struct poly_render_state {
527 unsigned char *cover;
531 scanline_flush_render(i_img *im, ss_scanline *ss, int y, void *ctx) {
534 struct poly_render_state *state = (struct poly_render_state *)ctx;
537 while (left < im->xsize && ss->line[left] <= 0)
539 if (left < im->xsize) {
541 /* since going from the left found something, moving from the
543 while (/* right > left && */ ss->line[right-1] <= 0)
546 /* convert to the format the render interface wants */
547 for (x = left; x < right; ++x) {
548 state->cover[x-left] = saturate(ss->line[x]);
550 i_render_fill(&state->render, left, y, right-left, state->cover,
556 i_poly_aa_cfill(i_img *im, int l, const double *x, const double *y,
558 struct poly_render_state ctx;
560 i_render_init(&ctx.render, im, im->xsize);
562 ctx.cover = mymalloc(im->xsize);
563 i_poly_aa_low(im, l, x, y, &ctx, scanline_flush_render);
565 i_render_done(&ctx.render);