Commit | Line | Data |
---|---|---|
92bda632 | 1 | #include "imager.h" |
9982a307 AMH |
2 | #include "draw.h" |
3 | #include "log.h" | |
9b1ec2b8 | 4 | #include "imrender.h" |
50c75381 | 5 | #include "imageri.h" |
9982a307 AMH |
6 | |
7 | #define IMTRUNC(x) ((int)((x)*16)) | |
8 | ||
9 | #define coarse(x) ((x)/16) | |
10 | #define fine(x) ((x)%16) | |
11 | ||
1c5252ed TC |
12 | /*#define DEBUG_POLY*/ |
13 | #ifdef DEBUG_POLY | |
14 | #define POLY_DEB(x) x | |
15 | #else | |
9982a307 | 16 | #define POLY_DEB(x) |
1c5252ed | 17 | #endif |
9982a307 AMH |
18 | |
19 | ||
8d14daab | 20 | typedef i_img_dim pcord; |
9982a307 AMH |
21 | |
22 | typedef struct { | |
7a5914b9 | 23 | size_t n; |
9982a307 AMH |
24 | pcord x,y; |
25 | } p_point; | |
26 | ||
27 | typedef struct { | |
7a5914b9 | 28 | size_t n; |
9982a307 AMH |
29 | pcord x1,y1; |
30 | pcord x2,y2; | |
31 | pcord miny,maxy; | |
32 | pcord minx,maxx; | |
33 | int updown; /* -1 means down, 0 vertical, 1 up */ | |
34 | } p_line; | |
35 | ||
36 | typedef struct { | |
7a5914b9 | 37 | size_t n; |
9982a307 AMH |
38 | double x; |
39 | } p_slice; | |
40 | ||
9982a307 AMH |
41 | typedef struct { |
42 | int *line; /* temporary buffer for scanline */ | |
8d14daab | 43 | i_img_dim linelen; /* length of scanline */ |
9982a307 AMH |
44 | } ss_scanline; |
45 | ||
9982a307 AMH |
46 | static |
47 | int | |
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; | |
51 | return 0; | |
52 | } | |
53 | ||
54 | static | |
55 | int | |
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; | |
59 | return 0; | |
60 | } | |
61 | ||
62 | /* Change this to int? and round right goddamn it! */ | |
63 | ||
64 | static | |
65 | double | |
66 | p_eval_aty(p_line *l, pcord y) { | |
67 | int t; | |
68 | t=l->y2-l->y1; | |
69 | if (t) return ( (y-l->y1)*l->x2 + (l->y2-y)*l->x1 )/t; | |
70 | return (l->x1+l->x2)/2.0; | |
71 | } | |
72 | ||
73 | static | |
74 | double | |
75 | p_eval_atx(p_line *l, pcord x) { | |
76 | int t; | |
77 | t = l->x2-l->x1; | |
78 | if (t) return ( (x-l->x1)*l->y2 + (l->x2-x)*l->y1 )/t; | |
79 | return (l->y1+l->y2)/2.0; | |
80 | } | |
81 | ||
82 | static | |
83 | p_line * | |
7a5914b9 TC |
84 | line_set_new(const i_polygon_t *polys, size_t count, size_t *line_count) { |
85 | size_t i, j, n; | |
86 | p_line *lset, *line; | |
87 | size_t lines = 0; | |
88 | ||
89 | for (i = 0; i < count; ++i) | |
90 | lines += polys[i].count; | |
91 | ||
92 | *line_count = lines; | |
93 | ||
94 | line = lset = mymalloc(sizeof(p_line) * lines); | |
95 | ||
96 | n = 0; | |
97 | for (i = 0; i < count; ++i) { | |
98 | const i_polygon_t *p = polys + i; | |
99 | ||
100 | for(j = 0; j < p->count; j++) { | |
101 | line->n = n++; | |
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); | |
110 | ++line; | |
111 | } | |
9982a307 | 112 | } |
7a5914b9 | 113 | |
9982a307 AMH |
114 | return lset; |
115 | } | |
116 | ||
117 | static | |
118 | p_point * | |
7a5914b9 TC |
119 | point_set_new(const i_polygon_t *polys, size_t count, size_t *point_count) { |
120 | size_t i, j, n; | |
121 | p_point *pset, *pt; | |
122 | size_t points = 0; | |
9982a307 | 123 | |
7a5914b9 TC |
124 | for (i = 0; i < count; ++i) |
125 | points += polys[i].count; | |
126 | ||
127 | *point_count = points; | |
128 | ||
129 | pt = pset = mymalloc(sizeof(p_point) * points); | |
130 | ||
131 | n = 0; | |
132 | for (i = 0; i < count; ++i) { | |
133 | const i_polygon_t *p = polys + i; | |
134 | ||
135 | for(j = 0; j < p->count; j++) { | |
136 | pt->n = n++; | |
137 | pt->x = IMTRUNC(p->x[j]); | |
138 | pt->y = IMTRUNC(p->y[j]); | |
139 | ++pt; | |
140 | } | |
9982a307 AMH |
141 | } |
142 | return pset; | |
143 | } | |
144 | ||
9982a307 AMH |
145 | static |
146 | void | |
147 | ss_scanline_reset(ss_scanline *ss) { | |
9982a307 AMH |
148 | memset(ss->line, 0, sizeof(int) * ss->linelen); |
149 | } | |
150 | ||
151 | static | |
152 | void | |
8d14daab | 153 | ss_scanline_init(ss_scanline *ss, i_img_dim linelen, int linepairs) { |
9982a307 AMH |
154 | ss->line = mymalloc( sizeof(int) * linelen ); |
155 | ss->linelen = linelen; | |
9982a307 AMH |
156 | ss_scanline_reset(ss); |
157 | } | |
158 | ||
d0e7bfee AMH |
159 | static |
160 | void | |
161 | ss_scanline_exorcise(ss_scanline *ss) { | |
162 | myfree(ss->line); | |
d0e7bfee AMH |
163 | } |
164 | ||
165 | ||
166 | ||
9982a307 AMH |
167 | |
168 | /* returns the number of matches */ | |
169 | ||
170 | static | |
171 | int | |
fe24d684 AMH |
172 | lines_in_interval(p_line *lset, int l, p_slice *tllist, pcord minc, pcord maxc) { |
173 | int k; | |
174 | int count = 0; | |
175 | for(k=0; k<l; k++) { | |
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") ); | |
179 | } else { | |
180 | tllist[count].x=p_eval_aty(&lset[k],(minc+maxc)/2.0 ); | |
181 | tllist[count].n=k; | |
182 | count++; | |
183 | } | |
184 | } | |
185 | } | |
186 | return count; | |
187 | } | |
188 | ||
9982a307 AMH |
189 | /* marks the up variable for all lines in a slice */ |
190 | ||
191 | static | |
192 | void | |
193 | mark_updown_slices(p_line *lset, p_slice *tllist, int count) { | |
194 | p_line *l, *r; | |
195 | int k; | |
196 | for(k=0; k<count; k+=2) { | |
197 | l = lset + tllist[k].n; | |
9982a307 AMH |
198 | |
199 | if (l->y1 == l->y2) { | |
200 | mm_log((1, "mark_updown_slices: horizontal line being marked: internal error!\n")); | |
201 | exit(3); | |
202 | } | |
203 | ||
9982a307 AMH |
204 | l->updown = (l->x1 == l->x2) ? |
205 | 0 : | |
206 | (l->x1 > l->x2) | |
207 | ? | |
208 | (l->y1 > l->y2) ? -1 : 1 | |
209 | : | |
210 | (l->y1 > l->y2) ? 1 : -1; | |
211 | ||
d0e7bfee AMH |
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) | |
214 | ); | |
215 | ||
216 | if (k+1 >= count) { | |
217 | mm_log((1, "Invalid polygon spec, odd number of line crossings.\n")); | |
218 | return; | |
219 | } | |
220 | ||
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")); | |
224 | exit(3); | |
225 | } | |
226 | ||
9982a307 AMH |
227 | r->updown = (r->x1 == r->x2) ? |
228 | 0 : | |
229 | (r->x1 > r->x2) | |
230 | ? | |
231 | (r->y1 > r->y2) ? -1 : 1 | |
232 | : | |
233 | (r->y1 > r->y2) ? 1 : -1; | |
234 | ||
d0e7bfee AMH |
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) | |
9982a307 AMH |
237 | ); |
238 | } | |
239 | } | |
240 | ||
9982a307 AMH |
241 | static |
242 | unsigned char | |
243 | saturate(int in) { | |
244 | if (in>255) { return 255; } | |
245 | else if (in>0) return in; | |
246 | return 0; | |
247 | } | |
248 | ||
7bd0a037 | 249 | typedef void (*scanline_flusher)(i_img *im, ss_scanline *ss, int y, void *ctx); |
9982a307 AMH |
250 | |
251 | /* This function must be modified later to do proper blending */ | |
252 | ||
b33c08f8 | 253 | static void |
7bd0a037 | 254 | scanline_flush(i_img *im, ss_scanline *ss, int y, void *ctx) { |
9982a307 AMH |
255 | int x, ch, tv; |
256 | i_color t; | |
43c5dacb | 257 | i_color *val = (i_color *)ctx; |
1c5252ed | 258 | POLY_DEB( printf("Flushing line %d\n", y) ); |
9982a307 AMH |
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); | |
265 | } | |
266 | } | |
267 | ||
268 | ||
269 | ||
270 | static | |
271 | int | |
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; | |
275 | } | |
276 | ||
277 | ||
278 | /* | |
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). | |
282 | */ | |
283 | ||
284 | ||
285 | static | |
286 | int | |
287 | pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord miny, pcord maxy) { | |
288 | double lycross, rycross; | |
289 | int l, r; | |
290 | ||
1c5252ed TC |
291 | POLY_DEB |
292 | ( | |
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) | |
295 | ); | |
296 | ||
9982a307 AMH |
297 | if (!line->updown) { |
298 | l = r = 0; | |
299 | } else { | |
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 */ | |
304 | } | |
305 | POLY_DEB( | |
1c5252ed TC |
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) | |
9982a307 AMH |
309 | ); |
310 | ||
311 | if (l && r) | |
312 | return line->updown == 1 ? | |
313 | (double)(maxx-minx) * (2.0*maxy-lycross-rycross)/2.0 /* up case */ | |
314 | : | |
315 | (double)(maxx-minx) * (lycross+rycross-2*miny)/2.0; /* down case */ | |
316 | ||
317 | if (!l && !r) return (maxy-miny)*(maxx*2-p_eval_aty(line, miny)-p_eval_aty(line, maxy))/2.0; | |
318 | ||
319 | if (l && !r) | |
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)); | |
323 | ||
324 | ||
325 | if (!l && r) { | |
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; | |
329 | return r; | |
330 | } | |
b07bc64b TC |
331 | |
332 | return 0; /* silence compiler warning */ | |
9982a307 AMH |
333 | } |
334 | ||
335 | ||
336 | ||
337 | ||
338 | ||
339 | /* | |
340 | handle the scanline slice in three steps | |
341 | ||
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 | |
346 | */ | |
347 | ||
348 | static | |
349 | void | |
1c5252ed | 350 | render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r, pcord miny, pcord maxy) { |
9982a307 | 351 | |
9982a307 AMH |
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 */ | |
8d14daab | 354 | i_img_dim cpix; /* x-coordinate of current pixel */ |
8d14daab TC |
355 | i_img_dim startpix; /* temporary variable for "start of this interval" */ |
356 | i_img_dim stoppix; /* temporary variable for "end of this interval" */ | |
9982a307 AMH |
357 | |
358 | /* Find the y bounds of scanline_slice */ | |
359 | ||
1c5252ed TC |
360 | POLY_DEB |
361 | ( | |
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, | |
366 | l->updown); | |
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, | |
370 | r->updown); | |
371 | ); | |
372 | ||
b33c08f8 TC |
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) ); | |
9982a307 | 375 | |
b33c08f8 TC |
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) ); | |
9982a307 | 378 | |
b33c08f8 TC |
379 | startpix = i_max( coarse(lminx), 0 ); |
380 | stoppix = i_min( coarse(rmaxx-1), ss->linelen-1 ); | |
1c5252ed TC |
381 | |
382 | POLY_DEB( printf(" miny=%g maxy=%g\n", miny/16.0, maxy/16.0) ); | |
9982a307 AMH |
383 | |
384 | for(cpix=startpix; cpix<=stoppix; cpix++) { | |
385 | int lt = coarse(lmaxx-1) >= cpix; | |
386 | int rt = coarse(rminx) <= cpix; | |
387 | ||
388 | int A, B, C; | |
389 | ||
1c5252ed | 390 | POLY_DEB( printf(" (%d,%d) lt=%d rt=%d\n", cpix, y, lt, rt) ); |
9982a307 AMH |
391 | |
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; | |
395 | ||
1c5252ed | 396 | POLY_DEB( printf(" A=%d B=%d C=%d\n", A, B, C) ); |
9982a307 AMH |
397 | |
398 | ss->line[cpix] += A+B-C; | |
399 | ||
400 | } | |
1c5252ed | 401 | POLY_DEB( printf("end render_slice_scanline()\n") ); |
9982a307 AMH |
402 | } |
403 | ||
9982a307 AMH |
404 | /* Antialiasing polygon algorithm |
405 | specs: | |
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 | |
411 | ||
412 | ||
413 | Algorithm outline: | |
414 | 1. Split into vertical intervals. | |
415 | 2. handle each interval | |
416 | ||
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. | |
422 | */ | |
423 | ||
424 | /* | |
425 | Definitions: | |
426 | ||
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. | |
430 | ||
431 | */ | |
432 | ||
433 | ||
7a5914b9 TC |
434 | static int |
435 | i_poly_poly_aa_low(i_img *im, int count, const i_polygon_t *polys, | |
436 | void *ctx, scanline_flusher flusher) { | |
9982a307 | 437 | int i ,k; /* Index variables */ |
8d14daab | 438 | i_img_dim clc; /* Lines inside current interval */ |
b07bc64b TC |
439 | /* initialize to avoid compiler warnings */ |
440 | pcord tempy = 0; | |
8d14daab | 441 | i_img_dim cscl = 0; /* Current scanline */ |
9982a307 AMH |
442 | |
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 */ | |
7a5914b9 | 447 | size_t pcount, lcount; |
9982a307 | 448 | |
7a5914b9 | 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)); |
d0e7bfee | 450 | |
7a5914b9 TC |
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])); | |
456 | } | |
d0e7bfee AMH |
457 | } |
458 | ||
459 | ||
460 | POLY_DEB( | |
461 | fflush(stdout); | |
462 | setbuf(stdout, NULL); | |
463 | ); | |
9982a307 | 464 | |
7a5914b9 TC |
465 | pset = point_set_new(polys, count, &pcount); |
466 | lset = line_set_new(polys, count, &lcount); | |
9982a307 | 467 | |
7a5914b9 | 468 | ss_scanline_init(&templine, im->xsize, lcount); |
9982a307 | 469 | |
7a5914b9 TC |
470 | tllist = mymalloc(sizeof(p_slice) * lcount); |
471 | ||
472 | qsort(pset, pcount, sizeof(p_point), (int(*)(const void *,const void *))p_compy); | |
9982a307 AMH |
473 | |
474 | POLY_DEB( | |
7a5914b9 | 475 | for(i=0;i<lcount;i++) { |
9982a307 AMH |
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); | |
478 | } | |
479 | printf("MAIN LOOP\n\n"); | |
480 | ); | |
481 | ||
482 | ||
483 | /* loop on intervals */ | |
7a5914b9 | 484 | for(i=0; i<pcount-1; i++) { |
8d14daab TC |
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); | |
1c5252ed | 487 | pcord miny, maxy; /* y bounds in fine coordinates */ |
9982a307 | 488 | |
1c5252ed | 489 | POLY_DEB( pcord cc = (pset[i].y + pset[i+1].y)/2 ); |
fe24d684 | 490 | |
9982a307 AMH |
491 | POLY_DEB( |
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) | |
494 | ); | |
495 | ||
1c5252ed TC |
496 | if (pset[i].y == pset[i+1].y) { |
497 | POLY_DEB( printf("current slice thickness = 0 => skipping\n") ); | |
498 | continue; | |
499 | } | |
500 | ||
7a5914b9 | 501 | clc = lines_in_interval(lset, lcount, tllist, pset[i].y, pset[i+1].y); |
9982a307 AMH |
502 | qsort(tllist, clc, sizeof(p_slice), (int(*)(const void *,const void *))p_compx); |
503 | ||
504 | mark_updown_slices(lset, tllist, clc); | |
505 | ||
1c5252ed TC |
506 | POLY_DEB |
507 | ( | |
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"); | |
519 | ||
520 | } | |
521 | ); | |
522 | maxy = im->ysize * 16; | |
523 | miny = 0; | |
524 | for (k = 0; k < clc; ++k) { | |
525 | p_line const * line = lset + tllist[k].n; | |
526 | if (line->miny > miny) | |
527 | miny = line->miny; | |
528 | if (line->maxy < maxy) | |
529 | maxy = line->maxy; | |
530 | POLY_DEB( printf(" line miny %g maxy %g\n", line->miny/16.0, line->maxy/16.0) ); | |
9982a307 | 531 | } |
1c5252ed TC |
532 | POLY_DEB( printf("miny %g maxy %g\n", miny/16.0, maxy/16.0) ); |
533 | ||
9982a307 | 534 | for(cscl=startscan; cscl<stopscan; cscl++) { |
1c5252ed TC |
535 | pcord scan_miny = i_max(miny, cscl * 16); |
536 | pcord scan_maxy = i_min(maxy, (cscl + 1 ) * 16); | |
537 | ||
b33c08f8 | 538 | tempy = i_min(cscl*16+16, pset[i+1].y); |
9982a307 AMH |
539 | POLY_DEB( printf("evaluating scan line %d \n", cscl) ); |
540 | for(k=0; k<clc-1; k+=2) { | |
fe24d684 | 541 | POLY_DEB( printf("evaluating slice %d\n", k) ); |
1c5252ed | 542 | render_slice_scanline(&templine, cscl, lset+tllist[k].n, lset+tllist[k+1].n, scan_miny, scan_maxy); |
9982a307 AMH |
543 | } |
544 | if (16*coarse(tempy) == tempy) { | |
545 | POLY_DEB( printf("flushing scan line %d\n", cscl) ); | |
43c5dacb | 546 | flusher(im, &templine, cscl, ctx); |
9982a307 AMH |
547 | ss_scanline_reset(&templine); |
548 | } | |
549 | /* | |
550 | else { | |
551 | scanline_flush(im, &templine, cscl, val); | |
552 | ss_scanline_reset(&templine); | |
553 | return 0; | |
554 | } | |
555 | */ | |
556 | } | |
557 | } /* Intervals */ | |
558 | if (16*coarse(tempy) != tempy) | |
43c5dacb | 559 | flusher(im, &templine, cscl-1, ctx); |
9982a307 | 560 | |
d0e7bfee AMH |
561 | ss_scanline_exorcise(&templine); |
562 | myfree(pset); | |
563 | myfree(lset); | |
564 | myfree(tllist); | |
7a5914b9 TC |
565 | |
566 | return 1; | |
567 | } | |
43c5dacb | 568 | |
1c5252ed | 569 | int |
7a5914b9 TC |
570 | i_poly_poly_aa(i_img *im, int count, const i_polygon_t *polys, |
571 | const i_color *val) { | |
7bd0a037 | 572 | i_color c = *val; |
7a5914b9 TC |
573 | return i_poly_poly_aa_low(im, count, polys, &c, scanline_flush); |
574 | } | |
575 | ||
576 | int | |
577 | i_poly_aa(i_img *im, int l, const double *x, const double *y, const i_color *val) { | |
578 | i_polygon_t poly; | |
579 | ||
580 | poly.count = l; | |
581 | poly.x = x; | |
582 | poly.y = y; | |
583 | return i_poly_poly_aa(im, 1, &poly, val); | |
43c5dacb | 584 | } |
9b1ec2b8 TC |
585 | |
586 | struct poly_render_state { | |
587 | i_render render; | |
588 | i_fill_t *fill; | |
589 | unsigned char *cover; | |
590 | }; | |
591 | ||
592 | static void | |
7bd0a037 | 593 | scanline_flush_render(i_img *im, ss_scanline *ss, int y, void *ctx) { |
8d14daab TC |
594 | i_img_dim x; |
595 | i_img_dim left, right; | |
7bd0a037 | 596 | struct poly_render_state *state = (struct poly_render_state *)ctx; |
9b1ec2b8 TC |
597 | |
598 | left = 0; | |
599 | while (left < im->xsize && ss->line[left] <= 0) | |
600 | ++left; | |
601 | if (left < im->xsize) { | |
602 | right = im->xsize; | |
603 | /* since going from the left found something, moving from the | |
604 | right should */ | |
605 | while (/* right > left && */ ss->line[right-1] <= 0) | |
606 | --right; | |
607 | ||
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]); | |
611 | } | |
612 | i_render_fill(&state->render, left, y, right-left, state->cover, | |
613 | state->fill); | |
614 | } | |
615 | } | |
43c5dacb | 616 | |
1c5252ed | 617 | int |
7a5914b9 TC |
618 | i_poly_poly_aa_cfill(i_img *im, int count, const i_polygon_t *polys, |
619 | i_fill_t *fill) { | |
9b1ec2b8 | 620 | struct poly_render_state ctx; |
7a5914b9 | 621 | int result; |
9b1ec2b8 TC |
622 | |
623 | i_render_init(&ctx.render, im, im->xsize); | |
624 | ctx.fill = fill; | |
625 | ctx.cover = mymalloc(im->xsize); | |
7a5914b9 TC |
626 | |
627 | result = i_poly_poly_aa_low(im, count, polys, &ctx, scanline_flush_render); | |
628 | ||
9b1ec2b8 TC |
629 | myfree(ctx.cover); |
630 | i_render_done(&ctx.render); | |
7a5914b9 TC |
631 | |
632 | return result; | |
633 | } | |
634 | ||
635 | int | |
636 | i_poly_aa_cfill(i_img *im, int l, const double *x, const double *y, | |
637 | i_fill_t *fill) { | |
638 | i_polygon_t poly; | |
639 | ||
640 | poly.count = l; | |
641 | poly.x = x; | |
642 | poly.y = y; | |
643 | ||
644 | return i_poly_poly_aa_cfill(im, 1, &poly, fill); | |
43c5dacb | 645 | } |