Coverage Report

Created: 2026-10-28 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gxscanc.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
/* Path stroking procedures for Ghostscript library */
17
#include "math_.h"
18
#include "memory_.h"
19
#include "string_.h"
20
#include "gx.h"
21
#include "gpcheck.h"
22
#include "gserrors.h"
23
#include "gsdcolor.h"
24
#include "gsptype1.h"
25
#include "gxfixed.h"
26
#include "gxfarith.h"
27
#include "gxmatrix.h"
28
#include "gscoord.h"
29
#include "gsdevice.h"
30
#include "gxdevice.h"
31
#include "gxhttile.h"
32
#include "gxgstate.h"
33
#include "gzline.h"
34
#include "gzpath.h"
35
#include "gzcpath.h"
36
#include "gxpaint.h"
37
#include "gxscanc.h"
38
#include "gxfill.h"
39
#include "gxdcolor.h"
40
#include "assert_.h"
41
#include <stdlib.h>             /* for qsort */
42
#include <limits.h>             /* For INT_MAX */
43
44
/* Overview of the scan conversion algorithm.
45
 *
46
 * The normal scan conversion algorithm runs through a path, converting
47
 * it into a sequence of edges. It then runs through those edges from
48
 * top to bottom keeping a list of which ones are "active", and ordering
49
 * them so that it can read out a list of intersection points from left
50
 * to right across any given scanline (or scan "band" when working with
51
 * trapezoids).
52
 *
53
 * This scan conversion algorithm avoids the need to maintain an active
54
 * line list, and to repeatedly re-sort lines. It is thus faster, at
55
 * the cost of using more memory, and not being able to cope with
56
 * trapezoids.
57
 *
58
 * Conceptually, the idea is to make an (initially empty) table. Each
59
 * row of the table holds the set of intersection data for a given
60
 * scanline. We therefore just need to run through the path once,
61
 * decomposing it to a sequence of edges. We then step along each edge
62
 * adding intersection information into each row of the table as we go.
63
 * Each piece of intersection information includes the point at which
64
 * the edge crosses the scanline, and the direction in which it does so
65
 * (up or down).
66
 *
67
 * At the end of this process, we can then sort each rows data, and
68
 * simply 'fill in' the scanline according to the winding rule.
69
 *
70
 * This copes well with 'centre of a pixel' fill modes, but 'any part
71
 * of a pixel' requires some extra work. Let's describe 'centre of a
72
 * pixel' first.
73
 *
74
 * Assume we have a path with n segments in, and a bbox that crosses
75
 * a region x wide, y high.
76
 *
77
 * 1) Create a table, A, 1 int entry per scan line. Run through the path,
78
 * segment by segment counting how many intersections occur on each
79
 * scanline. (O(y * n))
80
 *
81
 * 2) Create a table, B, with as many entries per scanline as determined in
82
 * the table A. (O(y * n))
83
 *
84
 * [Each entry is a (xcoord,direction) tuple. xcoord = the xcoord where
85
 * an edge crosses the horizontal line through the middle of the pixel.
86
 * direction = 0 if the edge is rising, 1 if falling.]
87
 *
88
 * 3) Run through the path segment by segment, inserting entries for each
89
 * scanline intersection in table B. (O(y * n))
90
 *
91
 * 4) Sort the scanline intersections of table B (on left,right,direction).
92
 * (O(y * n log n) for current code)
93
 *
94
 * 5) Filter the scanline intersections according to the winding rule.
95
 * (O(y * n))
96
 *
97
 * 6) Fill rectangles according to each set of scanline intersections.
98
 * (O(y * n))
99
 *
100
 * So worst case complexity (when every segment crosses every scanline) is
101
 * O(y * n log n).
102
 *
103
 * NOTE: If we use a binary comparison based sort, then the best we can manage
104
 * is n log n for step 4. If we use a radix based sort, we can get O(n).
105
 * Consider this if we ever need it.
106
 *
107
 * In order to cope with 'any part of a pixel' it no longer suffices
108
 * to keep a single intersection point for each scanline intersection.
109
 * Instead we keep the interval of a scanline that the edge intersects.
110
 * Thus each entry is a (left,right,direction) tuple. left = the
111
 * leftmost point at which this edge intersects this scanline. right =
112
 * the rightmost point at which this edge intersects this scanline.
113
 * direction = 0 for rising edges, 1 for falling edges.
114
 *
115
 * The rest of the algorithm is unchanged, apart from additional care
116
 * being required when filling the scanlines to allow for the fact
117
 * that edges are no longer point intersections.
118
 *
119
 * The first set of routines (gx_scan_convert and gx_fill_edgebuffer)
120
 * implement the "pixel centre" covered routines by drawing rectangle
121
 * high scanlines at a time. The second set of routines
122
 * (gx_scan_convert_app and gx_fill_edgebuffer_app) is the equivalent,
123
 * for "Any Part of Pixel" covered.
124
 *
125
 * The third and fourth are the same things, but using trapezoids
126
 * that can be multiple scanlines high rather than scanlines.
127
 *
128
 * In order to do trapezoid extraction, we extend the edge intersection
129
 * information to be (left,right,id,direction) (for the "centre pixel"
130
 * variants) and (left,left_id,right,right_id,direction) (for the "any
131
 * part of a pixel" variants). The 'id' is a int that is guaranteed
132
 * unique for each flattened line in path.
133
 *
134
 * If we spot that each scanlines data has the same set of ids in the
135
 * same order, then we can 'collate' them into a trapezoid.
136
 */
137
138
/* NOTE: code in this file assumes that fixed and int can be used
139
 * interchangably. */
140
141
#undef DEBUG_SCAN_CONVERTER
142
#undef DEBUG_OUTPUT_SC_AS_PS
143
144
typedef int64_t fixed64;
145
146
enum
147
{
148
    DIRN_UNSET = -1,
149
    DIRN_UP = 0,
150
    DIRN_DOWN = 1
151
};
152
153
/* Centre of a pixel routines */
154
155
static int intcmp(const void *a, const void *b)
156
2.15M
{
157
2.15M
    return *((int*)a) - *((int *)b);
158
2.15M
}
159
160
#if defined(DEBUG_SCAN_CONVERTER)
161
int debugging_scan_converter = 1;
162
163
static void
164
gx_edgebuffer_print(gx_edgebuffer * edgebuffer)
165
{
166
    int i;
167
168
    dlprintf1("Edgebuffer %x\n", edgebuffer);
169
    dlprintf4("xmin=%x xmax=%x base=%x height=%x\n",
170
              edgebuffer->xmin, edgebuffer->xmax, edgebuffer->base, edgebuffer->height);
171
    for (i=0; i < edgebuffer->height; i++) {
172
        int  offset = edgebuffer->index[i];
173
        int *row    = &edgebuffer->table[offset];
174
        int count   = *row++;
175
        dlprintf3("%d @ %d: %d =", i, offset, count);
176
        while (count-- > 0) {
177
            int v = *row++;
178
            dlprintf2(" %x:%d", v&~1, v&1);
179
        }
180
        dlprintf("\n");
181
    }
182
}
183
#endif
184
185
#ifdef DEBUG_OUTPUT_SC_AS_PS
186
static void coord(const char *str, fixed x, fixed y)
187
{
188
    if (x > 0)
189
        dlprintf1(" 16#%x ", x);
190
    else
191
        dlprintf1("0 16#%x sub ", -x);
192
    if (y > 0)
193
        dlprintf1(" 16#%x ", y);
194
    else
195
        dlprintf1("0 16#%x sub ", -y);
196
    dlprintf1("%s %%PS\n", str);
197
}
198
#endif
199
200
typedef void (zero_filler_fn)(int *, const fixed *);
201
202
static void mark_line_zero(fixed sx, fixed ex, fixed *zf)
203
40.0k
{
204
40.0k
    if (sx < zf[0])
205
0
        zf[0] = sx;
206
40.0k
    if (ex < zf[0])
207
5.79k
        zf[0] = ex;
208
40.0k
    if (sx > zf[1])
209
0
        zf[1] = sx;
210
40.0k
    if (ex > zf[1])
211
8.31k
        zf[1] = ex;
212
40.0k
}
213
214
static void mark_curve_zero(fixed sx, fixed c1x, fixed c2x, fixed ex, int depth, fixed *zf)
215
0
{
216
0
    fixed ax = (sx + c1x)>>1;
217
0
    fixed bx = (c1x + c2x)>>1;
218
0
    fixed cx = (c2x + ex)>>1;
219
0
    fixed dx = (ax + bx)>>1;
220
0
    fixed fx = (bx + cx)>>1;
221
0
    fixed gx = (dx + fx)>>1;
222
223
0
    assert(depth >= 0);
224
0
    if (depth == 0)
225
0
        mark_line_zero(sx, ex, zf);
226
0
    else {
227
0
        depth--;
228
0
        mark_curve_zero(sx, ax, dx, gx, depth, zf);
229
0
        mark_curve_zero(gx, fx, cx, ex, depth, zf);
230
0
    }
231
0
}
232
233
static void mark_curve_big_zero(fixed64 sx, fixed64 c1x, fixed64 c2x, fixed64 ex, int depth, fixed *zf)
234
0
{
235
0
    fixed64 ax = (sx + c1x)>>1;
236
0
    fixed64 bx = (c1x + c2x)>>1;
237
0
    fixed64 cx = (c2x + ex)>>1;
238
0
    fixed64 dx = (ax + bx)>>1;
239
0
    fixed64 fx = (bx + cx)>>1;
240
0
    fixed64 gx = (dx + fx)>>1;
241
242
0
    assert(depth >= 0);
243
0
    if (depth == 0)
244
0
        mark_line_zero((fixed)sx, (fixed)ex, zf);
245
0
    else {
246
0
        depth--;
247
0
        mark_curve_big_zero(sx, ax, dx, gx, depth, zf);
248
0
        mark_curve_big_zero(gx, fx, cx, ex, depth, zf);
249
0
    }
250
0
}
251
252
static void mark_curve_top_zero(fixed sx, fixed c1x, fixed c2x, fixed ex, int depth, fixed *zf)
253
0
{
254
0
    fixed test = (sx^(sx<<1))|(c1x^(c1x<<1))|(c2x^(c2x<<1))|(ex^(ex<<1));
255
256
0
    if (test < 0)
257
0
        mark_curve_big_zero(sx, c1x, c2x, ex, depth, zf);
258
0
    else
259
0
        mark_curve_zero(sx, c1x, c2x, ex, depth, zf);
260
0
}
261
262
static int
263
zero_case(gx_device      * gs_restrict pdev,
264
          gx_path        * gs_restrict path,
265
          gs_fixed_rect  * gs_restrict ibox,
266
          int            * gs_restrict index,
267
          int            * gs_restrict table,
268
          fixed                        fixed_flat,
269
          zero_filler_fn *             fill)
270
7.25k
{
271
7.25k
    const subpath *psub;
272
7.25k
    fixed zf[2];
273
274
    /* Step 2 continued: Now we run through the path, filling in the real
275
     * values. */
276
15.0k
    for (psub = path->first_subpath; psub != 0;) {
277
7.74k
        const segment *pseg = (const segment *)psub;
278
7.74k
        fixed ex = pseg->pt.x;
279
7.74k
        fixed sy = pseg->pt.y;
280
7.74k
        fixed ix = ex;
281
7.74k
        int iy = fixed2int(pseg->pt.y);
282
283
7.74k
        zf[0] = ex;
284
7.74k
        zf[1] = ex;
285
286
40.0k
        while ((pseg = pseg->next) != 0 &&
287
32.8k
               pseg->type != s_start
288
32.3k
            ) {
289
32.3k
            fixed sx = ex;
290
32.3k
            ex = pseg->pt.x;
291
292
32.3k
            switch (pseg->type) {
293
0
                default:
294
0
                case s_start: /* Should never happen */
295
0
                case s_dash:  /* We should never be seeing a dash here */
296
0
                    assert("This should never happen" == NULL);
297
0
                    break;
298
0
                case s_curve: {
299
0
                    const curve_segment *const pcur = (const curve_segment *)pseg;
300
0
                    int k = gx_curve_log2_samples(sx, sy, pcur, fixed_flat);
301
302
0
                    mark_curve_top_zero(sx, pcur->p1.x, pcur->p2.x, ex, k, zf);
303
0
                    break;
304
0
                }
305
0
                case s_gap:
306
25.9k
                case s_line:
307
32.3k
                case s_line_close:
308
32.3k
                    mark_line_zero(sx, ex, zf);
309
32.3k
                    break;
310
32.3k
            }
311
32.3k
        }
312
        /* And close any open segments */
313
7.74k
        mark_line_zero(ex, ix, zf);
314
7.74k
        fill(&table[index[iy-ibox->p.y]], zf);
315
7.74k
        psub = (const subpath *)pseg;
316
7.74k
    }
317
318
7.25k
    return 0;
319
7.25k
}
320
321
static void mark_line(fixed sx, fixed sy, fixed ex, fixed ey, int base_y, int height, int *table, int *index)
322
177M
{
323
177M
    int64_t delta;
324
177M
    int iy, ih;
325
177M
    fixed clip_sy, clip_ey;
326
177M
    int dirn = DIRN_UP;
327
177M
    int *row;
328
329
#ifdef DEBUG_SCAN_CONVERTER
330
    if (debugging_scan_converter)
331
        dlprintf6("Marking line from %x,%x to %x,%x (%x,%x)\n", sx, sy, ex, ey, fixed2int(sy + fixed_half-1) - base_y, fixed2int(ey + fixed_half-1) - base_y);
332
#endif
333
#ifdef DEBUG_OUTPUT_SC_AS_PS
334
    dlprintf("0.001 setlinewidth 0 0 0 setrgbcolor %%PS\n");
335
    coord("moveto", sx, sy);
336
    coord("lineto", ex, ey);
337
    dlprintf("stroke %%PS\n");
338
#endif
339
340
177M
    if (fixed2int(sy + fixed_half-1) == fixed2int(ey + fixed_half-1))
341
154M
        return;
342
22.2M
    if (sy > ey) {
343
12.1M
        int t;
344
12.1M
        t = sy; sy = ey; ey = t;
345
12.1M
        t = sx; sx = ex; ex = t;
346
12.1M
        dirn = DIRN_DOWN;
347
12.1M
    }
348
    /* Lines go from sy to ey, closed at the start, open at the end. */
349
    /* We clip them to a region to make them closed at both ends. */
350
    /* Thus the first scanline marked (>= sy) is: */
351
22.2M
    clip_sy = ((sy + fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
352
    /* The last scanline marked (< ey) is: */
353
22.2M
    clip_ey = ((ey - fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
354
    /* Now allow for banding */
355
22.2M
    if (clip_sy < int2fixed(base_y) + fixed_half)
356
9.63M
        clip_sy = int2fixed(base_y) + fixed_half;
357
22.2M
    if (ey <= clip_sy)
358
9.21M
        return;
359
13.0M
    if (clip_ey > int2fixed(base_y + height - 1) + fixed_half)
360
4.30M
        clip_ey = int2fixed(base_y + height - 1) + fixed_half;
361
13.0M
    if (sy > clip_ey)
362
3.86M
        return;
363
9.15M
    delta = (int64_t)clip_sy - (int64_t)sy;
364
9.15M
    if (delta > 0)
365
8.41M
    {
366
8.41M
        int64_t dx = (int64_t)ex - (int64_t)sx;
367
8.41M
        int64_t dy = (int64_t)ey - (int64_t)sy;
368
8.41M
        int advance = (int)((dx * delta + (dy>>1)) / dy);
369
8.41M
        sx += advance;
370
8.41M
        sy += delta;
371
8.41M
    }
372
9.15M
    delta = (int64_t)ey - (int64_t)clip_ey;
373
9.15M
    if (delta > 0)
374
9.15M
    {
375
9.15M
        int64_t dx = (int64_t)ex - (int64_t)sx;
376
9.15M
        int64_t dy = (int64_t)ey - (int64_t)sy;
377
9.15M
        int advance = (int)((dx * delta + (dy>>1)) / dy);
378
9.15M
        ex -= advance;
379
9.15M
        ey -= delta;
380
9.15M
    }
381
9.15M
    ex -= sx;
382
9.15M
    ey -= sy;
383
9.15M
    ih = fixed2int(ey);
384
9.15M
    assert(ih >= 0);
385
9.15M
    iy = fixed2int(sy) - base_y;
386
#ifdef DEBUG_SCAN_CONVERTER
387
    if (debugging_scan_converter)
388
        dlprintf2("    iy=%x ih=%x\n", iy, ih);
389
#endif
390
9.15M
    assert(iy >= 0 && iy < height);
391
    /* We always cross at least one scanline */
392
9.15M
    row = &table[index[iy]];
393
9.15M
    *row = (*row)+1; /* Increment the count */
394
9.15M
    row[*row] = (sx&~1) | dirn;
395
9.15M
    if (ih == 0)
396
7.25M
        return;
397
1.89M
    if (ex >= 0) {
398
1.15M
        int x_inc, n_inc, f;
399
400
        /* We want to change sx by ex in ih steps. So each step, we add
401
         * ex/ih to sx. That's x_inc + n_inc/ih.
402
         */
403
1.15M
        x_inc = ex/ih;
404
1.15M
        n_inc = ex-(x_inc*ih);
405
1.15M
        f     = ih>>1;
406
1.15M
        delta = ih;
407
8.08M
        do {
408
8.08M
            int count;
409
8.08M
            iy++;
410
8.08M
            sx += x_inc;
411
8.08M
            f  -= n_inc;
412
8.08M
            if (f < 0) {
413
1.42M
                f += ih;
414
1.42M
                sx++;
415
1.42M
            }
416
8.08M
            assert(iy >= 0 && iy < height);
417
8.08M
            row = &table[index[iy]];
418
8.08M
            count = *row = (*row)+1; /* Increment the count */
419
8.08M
            row[count] = (sx&~1) | dirn;
420
8.08M
        } while (--delta);
421
1.15M
    } else {
422
744k
        int x_dec, n_dec, f;
423
424
744k
        ex = -ex;
425
        /* We want to change sx by ex in ih steps. So each step, we subtract
426
         * ex/ih from sx. That's x_dec + n_dec/ih.
427
         */
428
744k
        x_dec = ex/ih;
429
744k
        n_dec = ex-(x_dec*ih);
430
744k
        f     = ih>>1;
431
744k
        delta = ih;
432
6.92M
        do {
433
6.92M
            int count;
434
6.92M
            iy++;
435
6.92M
            sx -= x_dec;
436
6.92M
            f  -= n_dec;
437
6.92M
            if (f < 0) {
438
3.55M
                f += ih;
439
3.55M
                sx--;
440
3.55M
            }
441
6.92M
            assert(iy >= 0 && iy < height);
442
6.92M
            row = &table[index[iy]];
443
6.92M
            count = *row = (*row)+1; /* Increment the count */
444
6.92M
            row[count] = (sx&~1) | dirn;
445
6.92M
        } while (--delta);
446
744k
    }
447
1.89M
}
448
449
static void mark_curve(fixed sx, fixed sy, fixed c1x, fixed c1y, fixed c2x, fixed c2y, fixed ex, fixed ey, fixed base_y, fixed height, int *table, int *index, int depth)
450
0
{
451
0
    fixed ax = (sx + c1x)>>1;
452
0
    fixed ay = (sy + c1y)>>1;
453
0
    fixed bx = (c1x + c2x)>>1;
454
0
    fixed by = (c1y + c2y)>>1;
455
0
    fixed cx = (c2x + ex)>>1;
456
0
    fixed cy = (c2y + ey)>>1;
457
0
    fixed dx = (ax + bx)>>1;
458
0
    fixed dy = (ay + by)>>1;
459
0
    fixed fx = (bx + cx)>>1;
460
0
    fixed fy = (by + cy)>>1;
461
0
    fixed gx = (dx + fx)>>1;
462
0
    fixed gy = (dy + fy)>>1;
463
464
0
    assert(depth >= 0);
465
0
    if (depth == 0)
466
0
        mark_line(sx, sy, ex, ey, base_y, height, table, index);
467
0
    else {
468
0
        depth--;
469
0
        mark_curve(sx, sy, ax, ay, dx, dy, gx, gy, base_y, height, table, index, depth);
470
0
        mark_curve(gx, gy, fx, fy, cx, cy, ex, ey, base_y, height, table, index, depth);
471
0
    }
472
0
}
473
474
static void mark_curve_big(fixed64 sx, fixed64 sy, fixed64 c1x, fixed64 c1y, fixed64 c2x, fixed64 c2y, fixed64 ex, fixed64 ey, fixed base_y, fixed height, int *table, int *index, int depth)
475
0
{
476
0
    fixed64 ax = (sx + c1x)>>1;
477
0
    fixed64 ay = (sy + c1y)>>1;
478
0
    fixed64 bx = (c1x + c2x)>>1;
479
0
    fixed64 by = (c1y + c2y)>>1;
480
0
    fixed64 cx = (c2x + ex)>>1;
481
0
    fixed64 cy = (c2y + ey)>>1;
482
0
    fixed64 dx = (ax + bx)>>1;
483
0
    fixed64 dy = (ay + by)>>1;
484
0
    fixed64 fx = (bx + cx)>>1;
485
0
    fixed64 fy = (by + cy)>>1;
486
0
    fixed64 gx = (dx + fx)>>1;
487
0
    fixed64 gy = (dy + fy)>>1;
488
489
0
    assert(depth >= 0);
490
0
    if (depth == 0)
491
0
        mark_line((fixed)sx, (fixed)sy, (fixed)ex, (fixed)ey, base_y, height, table, index);
492
0
    else {
493
0
        depth--;
494
0
        mark_curve_big(sx, sy, ax, ay, dx, dy, gx, gy, base_y, height, table, index, depth);
495
0
        mark_curve_big(gx, gy, fx, fy, cx, cy, ex, ey, base_y, height, table, index, depth);
496
0
    }
497
0
}
498
499
static void mark_curve_top(fixed sx, fixed sy, fixed c1x, fixed c1y, fixed c2x, fixed c2y, fixed ex, fixed ey, fixed base_y, fixed height, int *table, int *index, int depth)
500
0
{
501
0
    fixed test = (sx^(sx<<1))|(sy^(sy<<1))|(c1x^(c1x<<1))|(c1y^(c1y<<1))|(c2x^(c2x<<1))|(c2y^(c2y<<1))|(ex^(ex<<1))|(ey^(ey<<1));
502
503
0
    if (test < 0)
504
0
        mark_curve_big(sx, sy, c1x, c1y, c2x, c2y, ex, ey, base_y, height, table, index, depth);
505
0
    else
506
0
        mark_curve(sx, sy, c1x, c1y, c2x, c2y, ex, ey, base_y, height, table, index, depth);
507
0
}
508
509
static int make_bbox(gx_path       * path,
510
               const gs_fixed_rect * clip,
511
                     gs_fixed_rect * bbox,
512
                     gs_fixed_rect * ibox,
513
                     fixed           adjust)
514
17.9M
{
515
17.9M
    int           code;
516
17.9M
    int           ret = 0;
517
518
    /* Find the bbox - fixed */
519
17.9M
    code = gx_path_bbox(path, bbox);
520
17.9M
    if (code < 0)
521
0
        return code;
522
523
17.9M
    if (bbox->p.y == bbox->q.y) {
524
        /* Zero height path */
525
7.63k
        if (!clip ||
526
7.63k
            (bbox->p.y >= clip->p.y && bbox->q.y <= clip->q.y)) {
527
            /* Either we're not clipping, or we are vertically inside the clip */
528
7.48k
            if (clip) {
529
7.48k
                if (bbox->p.x < clip->p.x)
530
520
                    bbox->p.x = clip->p.x;
531
7.48k
                if (bbox->q.x > clip->q.x)
532
449
                    bbox->q.x = clip->q.x;
533
7.48k
            }
534
7.48k
            if (bbox->p.x <= bbox->q.x) {
535
                /* Zero height rectangle, not clipped completely away */
536
7.25k
                ret = 1;
537
7.25k
            }
538
7.48k
        }
539
7.63k
    }
540
541
17.9M
    if (clip) {
542
17.9M
        if (bbox->p.y < clip->p.y)
543
5.11M
            bbox->p.y = clip->p.y;
544
17.9M
        if (bbox->q.y > clip->q.y)
545
5.08M
            bbox->q.y = clip->q.y;
546
17.9M
    }
547
548
    /* Convert to bbox - int */
549
17.9M
    ibox->p.x = fixed2int(bbox->p.x+adjust-(adjust?1:0));
550
17.9M
    ibox->p.y = fixed2int(bbox->p.y+adjust-(adjust?1:0));
551
17.9M
    ibox->q.x = fixed2int(bbox->q.x-adjust+fixed_1);
552
17.9M
    ibox->q.y = fixed2int(bbox->q.y-adjust+fixed_1);
553
554
17.9M
    return ret;
555
17.9M
}
556
557
static inline int
558
make_table_template(gx_device     * pdev,
559
                    gx_path       * path,
560
                    gs_fixed_rect * ibox,
561
                    int             intersection_size,
562
                    int             adjust,
563
                    int           * scanlinesp,
564
                    int          ** indexp,
565
                    int          ** tablep)
566
17.8M
{
567
17.8M
    int             scanlines;
568
17.8M
    const subpath * gs_restrict psub;
569
17.8M
    int           * gs_restrict index;
570
17.8M
    int           * gs_restrict table;
571
17.8M
    int             i;
572
17.8M
    int64_t         offset;
573
17.8M
    int             delta;
574
17.8M
    fixed           base_y;
575
576
17.8M
    *scanlinesp = 0;
577
17.8M
    *indexp     = NULL;
578
17.8M
    *tablep     = NULL;
579
580
17.8M
    if (pdev->max_fill_band != 0)
581
0
        ibox->p.y &= ~(pdev->max_fill_band-1);
582
17.8M
    base_y = ibox->p.y;
583
584
    /* Previously we took adjust as a fixed distance to add to miny/maxy
585
     * to allow for the expansion due to 'any part of a pixel'. This causes
586
     * problems with over/underflow near INT_MAX/INT_MIN, so instead we
587
     * take adjust as boolean telling us whether to expand y by 1 or not, and
588
     * then adjust the assignments into the index as appropriate. This
589
     * solves Bug 697970. */
590
591
    /* Step 1: Make us a table */
592
17.8M
    scanlines = ibox->q.y-base_y;
593
    /* +1+adjust simplifies the loop below */
594
17.8M
    index = (int *)gs_alloc_bytes(pdev->memory,
595
17.8M
                                  (size_t)(scanlines+1+adjust) * sizeof(*index),
596
17.8M
                                  "scanc index buffer");
597
17.8M
    if (index == NULL)
598
0
        return_error(gs_error_VMerror);
599
600
    /* Step 1 continued: Blank the index */
601
17.8M
    memset(index, 0, (scanlines+1)*sizeof(int));
602
603
    /* Step 1 continued: Run through the path, filling in the index */
604
43.4M
    for (psub = path->first_subpath; psub != 0;) {
605
25.5M
        const segment * gs_restrict pseg = (const segment *)psub;
606
25.5M
        fixed          ey = pseg->pt.y;
607
25.5M
        fixed          iy = ey;
608
25.5M
        int            iey = fixed2int(iy) - base_y;
609
610
25.5M
        assert(pseg->type == s_start);
611
612
        /* Allow for 2 extra intersections on the start scanline.
613
         * This copes with the 'zero height rectangle' case. */
614
25.5M
        if (iey >= 0 && iey < scanlines)
615
14.0M
        {
616
14.0M
            index[iey] += 2;
617
14.0M
            if (iey+1 < scanlines)
618
9.55M
                index[iey+1] -= 2;
619
14.0M
        }
620
621
1.25G
        while ((pseg = pseg->next) != 0 &&
622
1.23G
               pseg->type != s_start
623
1.22G
            ) {
624
1.22G
            fixed sy = ey;
625
1.22G
            ey = pseg->pt.y;
626
627
1.22G
            switch (pseg->type) {
628
0
                default:
629
0
                case s_start: /* Should never happen */
630
0
                case s_dash:  /* We should never be seeing a dash here */
631
0
                    assert("This should never happen" == NULL);
632
0
                    break;
633
21.5k
                case s_curve: {
634
21.5k
                    const curve_segment *const gs_restrict pcur = (const curve_segment *)pseg;
635
21.5k
                    fixed c1y = pcur->p1.y;
636
21.5k
                    fixed c2y = pcur->p2.y;
637
21.5k
                    fixed maxy = sy, miny = sy;
638
21.5k
                    int imaxy, iminy;
639
21.5k
                    if (miny > c1y)
640
8.94k
                        miny = c1y;
641
21.5k
                    if (miny > c2y)
642
10.6k
                        miny = c2y;
643
21.5k
                    if (miny > ey)
644
8.50k
                        miny = ey;
645
21.5k
                    if (maxy < c1y)
646
9.60k
                        maxy = c1y;
647
21.5k
                    if (maxy < c2y)
648
10.5k
                        maxy = c2y;
649
21.5k
                    if (maxy < ey)
650
9.22k
                        maxy = ey;
651
#ifdef DEBUG_SCAN_CONVERTER
652
                    if (debugging_scan_converter)
653
                        dlprintf2("Curve (%x->%x) ", miny, maxy);
654
#endif
655
21.5k
                    iminy = fixed2int(miny) - base_y;
656
21.5k
                    if (iminy <= 0)
657
6.89k
                        iminy = 0;
658
14.6k
                    else
659
14.6k
                        iminy -= adjust;
660
21.5k
                    if (iminy < scanlines) {
661
16.6k
                        imaxy = fixed2int(maxy) - base_y;
662
16.6k
                        if (imaxy >= 0) {
663
#ifdef DEBUG_SCAN_CONVERTER
664
                            if (debugging_scan_converter)
665
                                dlprintf1("+%x ", iminy);
666
#endif
667
11.1k
                            index[iminy]+=3;
668
11.1k
                            if (imaxy < scanlines) {
669
#ifdef DEBUG_SCAN_CONVERTER
670
                                if (debugging_scan_converter)
671
                                    dlprintf1("-%x ", imaxy+1);
672
#endif
673
10.6k
                                index[imaxy+1+adjust]-=3;
674
10.6k
                            }
675
11.1k
                        }
676
16.6k
                    }
677
#ifdef DEBUG_SCAN_CONVERTER
678
                    if (debugging_scan_converter)
679
                        dlprintf("\n");
680
#endif
681
21.5k
                    break;
682
0
                }
683
0
                case s_gap:
684
1.20G
                case s_line:
685
1.22G
                case s_line_close: {
686
1.22G
                    fixed miny, maxy;
687
1.22G
                    int imaxy, iminy;
688
1.22G
                    if (sy == ey) {
689
#ifdef DEBUG_SCAN_CONVERTER
690
                        if (debugging_scan_converter)
691
                            dlprintf("Line (Horiz)\n");
692
#endif
693
102M
                        break;
694
102M
                    }
695
1.12G
                    if (sy < ey)
696
556M
                        miny = sy, maxy = ey;
697
568M
                    else
698
568M
                        miny = ey, maxy = sy;
699
#ifdef DEBUG_SCAN_CONVERTER
700
                    if (debugging_scan_converter)
701
                        dlprintf2("Line (%x->%x) ", miny, maxy);
702
#endif
703
1.12G
                    iminy = fixed2int(miny) - base_y;
704
1.12G
                    if (iminy <= 0)
705
543M
                        iminy = 0;
706
582M
                    else
707
582M
                        iminy -= adjust;
708
1.12G
                    if (iminy < scanlines) {
709
804M
                        imaxy = fixed2int(maxy) - base_y;
710
804M
                        if (imaxy >= 0) {
711
#ifdef DEBUG_SCAN_CONVERTER
712
                            if (debugging_scan_converter)
713
                                dlprintf1("+%x ", iminy);
714
#endif
715
322M
                            index[iminy]++;
716
322M
                            if (imaxy < scanlines) {
717
#ifdef DEBUG_SCAN_CONVERTER
718
                                if (debugging_scan_converter)
719
                                    dlprintf1("-%x ", imaxy+1);
720
#endif
721
305M
                                index[imaxy+1+adjust]--;
722
305M
                            }
723
322M
                        }
724
804M
                    }
725
#ifdef DEBUG_SCAN_CONVERTER
726
                    if (debugging_scan_converter)
727
                        dlprintf("\n");
728
#endif
729
1.12G
                    break;
730
1.22G
                }
731
1.22G
            }
732
1.22G
        }
733
734
        /* And close any segments that need it */
735
25.5M
        if (ey != iy) {
736
769k
            fixed miny, maxy;
737
769k
            int imaxy, iminy;
738
769k
            if (iy < ey)
739
341k
                miny = iy, maxy = ey;
740
427k
            else
741
427k
                miny = ey, maxy = iy;
742
#ifdef DEBUG_SCAN_CONVERTER
743
            if (debugging_scan_converter)
744
                dlprintf2("Close (%x->%x) ", miny, maxy);
745
#endif
746
769k
            iminy = fixed2int(miny) - base_y;
747
769k
            if (iminy <= 0)
748
567k
                iminy = 0;
749
202k
            else
750
202k
                iminy -= adjust;
751
769k
            if (iminy < scanlines) {
752
578k
                imaxy = fixed2int(maxy) - base_y;
753
578k
                if (imaxy >= 0) {
754
#ifdef DEBUG_SCAN_CONVERTER
755
                    if (debugging_scan_converter)
756
                        dlprintf1("+%x ", iminy);
757
#endif
758
307k
                    index[iminy]++;
759
307k
                    if (imaxy < scanlines) {
760
#ifdef DEBUG_SCAN_CONVERTER
761
                        if (debugging_scan_converter)
762
                            dlprintf1("-%x ", imaxy+1);
763
#endif
764
172k
                        index[imaxy+1+adjust]--;
765
172k
                    }
766
307k
                }
767
578k
            }
768
#ifdef DEBUG_SCAN_CONVERTER
769
            if (debugging_scan_converter)
770
                dlprintf("\n");
771
#endif
772
769k
        }
773
#ifdef DEBUG_SCAN_CONVERTER
774
        if (debugging_scan_converter)
775
            dlprintf("\n");
776
#endif
777
25.5M
        psub = (const subpath *)pseg;
778
25.5M
    }
779
780
    /* Step 1 continued: index now contains a list of deltas (how the
781
     * number of intersects on line x differs from the number on line x-1).
782
     * First convert them to be the real number of intersects on that line.
783
     * Sum these values to get us the total number of intersects. Then
784
     * convert the table to be a list of offsets into the real intersect
785
     * buffer. */
786
17.8M
    offset = 0;
787
17.8M
    delta  = 0;
788
515M
    for (i=0; i < scanlines+adjust; i++) {
789
497M
        delta    += intersection_size*index[i];  /* delta = Num ints on this scanline. */
790
497M
        index[i]  = offset;                      /* Offset into table for this lines data. */
791
497M
        offset   += delta+1;                     /* Adjust offset for next line. */
792
497M
    }
793
    /* Ensure we always have enough room for our zero height rectangle hack. */
794
17.8M
    if (offset < 2*intersection_size)
795
66
        offset += 2*intersection_size;
796
17.8M
    offset *= sizeof(*table);
797
798
    /* Try to keep the size to 1Meg. This is enough for the vast majority
799
     * of files. Allow us to grow above this if it would mean dropping
800
     * the height below a suitably small number (set to be larger than
801
     * any max_fill_band we might meet). */
802
17.8M
    if (scanlines > 16 && offset > 1024*1024) { /* Arbitrary */
803
188
        gs_free_object(pdev->memory, index, "scanc index buffer");
804
188
        return offset/(1024*1024) + 1;
805
188
    }
806
807
    /* In the case where we have let offset be large, at least make sure
808
     * it's not TOO large for us to malloc. */
809
17.8M
    if (offset != (int64_t)(uint)offset)
810
0
    {
811
0
        gs_free_object(pdev->memory, index, "scanc index buffer");
812
0
        return_error(gs_error_VMerror);
813
0
    }
814
815
    /* End of step 1: index[i] = offset into table 2 for scanline i's
816
     * intersection data. offset = Total number of int entries required for
817
     * table. */
818
819
    /* Step 2: Collect the real intersections */
820
17.8M
    table = (int *)gs_alloc_bytes(pdev->memory, offset,
821
17.8M
                                  "scanc intersects buffer");
822
17.8M
    if (table == NULL) {
823
0
        gs_free_object(pdev->memory, index, "scanc index buffer");
824
0
        return_error(gs_error_VMerror);
825
0
    }
826
827
    /* Step 2 continued: initialise table's data; each scanlines data starts
828
     * with a count of the number of intersects so far, followed by a record
829
     * of the intersect points on this scanline. */
830
492M
    for (i=0; i < scanlines; i++) {
831
474M
        table[index[i]] = 0;
832
474M
    }
833
834
17.8M
    *scanlinesp = scanlines;
835
17.8M
    *tablep     = table;
836
17.8M
    *indexp     = index;
837
838
17.8M
    return 0;
839
17.8M
}
840
841
static int make_table(gx_device     * pdev,
842
                      gx_path       * path,
843
                      gs_fixed_rect * ibox,
844
                      int           * scanlines,
845
                      int          ** index,
846
                      int          ** table)
847
499k
{
848
499k
    return make_table_template(pdev, path, ibox, 1, 1, scanlines, index, table);
849
499k
}
850
851
static void
852
fill_zero(int *row, const fixed *x)
853
0
{
854
0
    int n = *row = (*row)+2; /* Increment the count */
855
0
    row[n-1] = (x[0]&~1);
856
0
    row[n  ] = (x[1]|1);
857
0
}
858
859
int gx_scan_convert(gx_device     * gs_restrict pdev,
860
                    gx_path       * gs_restrict path,
861
              const gs_fixed_rect * gs_restrict clip,
862
                    gx_edgebuffer * gs_restrict edgebuffer,
863
                    fixed                       fixed_flat)
864
513k
{
865
513k
    gs_fixed_rect  ibox;
866
513k
    gs_fixed_rect  bbox;
867
513k
    int            scanlines;
868
513k
    const subpath *psub;
869
513k
    int           *index;
870
513k
    int           *table;
871
513k
    int            i;
872
513k
    int            code;
873
513k
    int            zero;
874
875
513k
    edgebuffer->index = NULL;
876
513k
    edgebuffer->table = NULL;
877
878
    /* Bale out if no actual path. We see this with the clist */
879
513k
    if (path->first_subpath == NULL)
880
1.93k
        return 0;
881
882
511k
    zero = make_bbox(path, clip, &bbox, &ibox, fixed_half);
883
511k
    if (zero < 0)
884
0
        return zero;
885
886
511k
    if (ibox.q.y <= ibox.p.y)
887
11.5k
        return 0;
888
889
499k
    code = make_table(pdev, path, &ibox, &scanlines, &index, &table);
890
499k
    if (code != 0) /* >0 means "retry with smaller height" */
891
0
        return code;
892
893
499k
    if (scanlines == 0)
894
0
        return 0;
895
896
499k
    if (zero) {
897
0
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero);
898
499k
    } else {
899
900
    /* Step 2 continued: Now we run through the path, filling in the real
901
     * values. */
902
1.30M
    for (psub = path->first_subpath; psub != 0;) {
903
802k
        const segment *pseg = (const segment *)psub;
904
802k
        fixed ex = pseg->pt.x;
905
802k
        fixed ey = pseg->pt.y;
906
802k
        fixed ix = ex;
907
802k
        fixed iy = ey;
908
909
209M
        while ((pseg = pseg->next) != 0 &&
910
208M
               pseg->type != s_start
911
208M
            ) {
912
208M
            fixed sx = ex;
913
208M
            fixed sy = ey;
914
208M
            ex = pseg->pt.x;
915
208M
            ey = pseg->pt.y;
916
917
208M
            switch (pseg->type) {
918
0
                default:
919
0
                case s_start: /* Should never happen */
920
0
                case s_dash:  /* We should never be seeing a dash here */
921
0
                    assert("This should never happen" == NULL);
922
0
                    break;
923
0
                case s_curve: {
924
0
                    const curve_segment *const pcur = (const curve_segment *)pseg;
925
0
                    int k = gx_curve_log2_samples(sx, sy, pcur, fixed_flat);
926
927
0
                    mark_curve_top(sx, sy, pcur->p1.x, pcur->p1.y, pcur->p2.x, pcur->p2.y, ex, ey, ibox.p.y, scanlines, table, index, k);
928
0
                    break;
929
0
                }
930
0
                case s_gap:
931
207M
                case s_line:
932
208M
                case s_line_close:
933
208M
                    if (sy != ey)
934
176M
                        mark_line(sx, sy, ex, ey, ibox.p.y, scanlines, table, index);
935
208M
                    break;
936
208M
            }
937
208M
        }
938
        /* And close any open segments */
939
802k
        if (iy != ey)
940
205k
            mark_line(ex, ey, ix, iy, ibox.p.y, scanlines, table, index);
941
802k
        psub = (const subpath *)pseg;
942
802k
    }
943
499k
    }
944
945
    /* Step 2 complete: We now have a complete list of intersection data in
946
     * table, indexed by index. */
947
948
499k
    edgebuffer->base   = ibox.p.y;
949
499k
    edgebuffer->height = scanlines;
950
499k
    edgebuffer->xmin   = ibox.p.x;
951
499k
    edgebuffer->xmax   = ibox.q.x;
952
499k
    edgebuffer->index  = index;
953
499k
    edgebuffer->table  = table;
954
955
#ifdef DEBUG_SCAN_CONVERTER
956
    if (debugging_scan_converter) {
957
        dlprintf("Before sorting:\n");
958
        gx_edgebuffer_print(edgebuffer);
959
    }
960
#endif
961
962
    /* Step 3: Sort the intersects on x */
963
7.76M
    for (i=0; i < scanlines; i++) {
964
7.26M
        int *row = &table[index[i]];
965
7.26M
        int  rowlen = *row++;
966
967
        /* Bubblesort short runs, qsort longer ones. */
968
        /* FIXME: Check "6" below */
969
7.26M
        if (rowlen <= 6) {
970
7.14M
            int j, k;
971
23.0M
            for (j = 0; j < rowlen-1; j++) {
972
15.9M
                int t = row[j];
973
46.8M
                for (k = j+1; k < rowlen; k++) {
974
30.8M
                    int s = row[k];
975
30.8M
                    if (t > s)
976
14.9M
                         row[k] = t, t = row[j] = s;
977
30.8M
                }
978
15.9M
            }
979
7.14M
        } else
980
123k
            qsort(row, rowlen, sizeof(int), intcmp);
981
7.26M
    }
982
983
499k
    return 0;
984
499k
}
985
986
/* Step 5: Filter the intersections according to the rules */
987
int
988
gx_filter_edgebuffer(gx_device       * gs_restrict pdev,
989
                     gx_edgebuffer   * gs_restrict edgebuffer,
990
                     int                        rule)
991
513k
{
992
513k
    int i;
993
994
#ifdef DEBUG_SCAN_CONVERTER
995
    if (debugging_scan_converter) {
996
        dlprintf("Before filtering:\n");
997
        gx_edgebuffer_print(edgebuffer);
998
    }
999
#endif
1000
1001
7.77M
    for (i=0; i < edgebuffer->height; i++) {
1002
7.26M
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
1003
7.26M
        int *rowstart = row;
1004
7.26M
        int  rowlen   = *row++;
1005
7.26M
        int *rowout   = row;
1006
1007
19.0M
        while (rowlen > 0)
1008
11.7M
        {
1009
11.7M
            int left, right;
1010
1011
11.7M
            if (rule == gx_rule_even_odd) {
1012
                /* Even Odd */
1013
0
                left  = (*row++)&~1;
1014
0
                right = (*row++)&~1;
1015
0
                rowlen -= 2;
1016
11.7M
            } else {
1017
                /* Non-Zero */
1018
11.7M
                int w;
1019
1020
11.7M
                left = *row++;
1021
11.7M
                w = ((left&1)-1) | (left&1);
1022
11.7M
                rowlen--;
1023
12.3M
                do {
1024
12.3M
                    right  = *row++;
1025
12.3M
                    rowlen--;
1026
12.3M
                    w += ((right&1)-1) | (right&1);
1027
12.3M
                } while (w != 0);
1028
11.7M
                left &= ~1;
1029
11.7M
                right &= ~1;
1030
11.7M
            }
1031
1032
11.7M
            if (right > left) {
1033
11.7M
                *rowout++ = left;
1034
11.7M
                *rowout++ = right;
1035
11.7M
            }
1036
11.7M
        }
1037
7.26M
        *rowstart = (rowout-rowstart)-1;
1038
7.26M
    }
1039
513k
    return 0;
1040
513k
}
1041
1042
/* Step 6: Fill the edgebuffer */
1043
int
1044
gx_fill_edgebuffer(gx_device       * gs_restrict pdev,
1045
             const gx_device_color * gs_restrict pdevc,
1046
                   gx_edgebuffer   * gs_restrict edgebuffer,
1047
                   int                        log_op)
1048
513k
{
1049
513k
    int i, code;
1050
1051
7.77M
    for (i=0; i < edgebuffer->height; i++) {
1052
7.26M
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
1053
7.26M
        int  rowlen = *row++;
1054
1055
18.9M
        while (rowlen > 0) {
1056
11.7M
            int left, right;
1057
1058
11.7M
            left  = *row++;
1059
11.7M
            right = *row++;
1060
11.7M
            rowlen -= 2;
1061
11.7M
            left  = fixed2int(left + fixed_half);
1062
11.7M
            right = fixed2int(right + fixed_half);
1063
11.7M
            right -= left;
1064
11.7M
            if (right > 0) {
1065
#ifdef DEBUG_OUTPUT_SC_AS_PS
1066
                dlprintf("0.001 setlinewidth 1 0.5 0 setrgbcolor %% orange %%PS\n");
1067
                coord("moveto", int2fixed(left), int2fixed(edgebuffer->base+i));
1068
                coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+i));
1069
                coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+i+1));
1070
                coord("lineto", int2fixed(left), int2fixed(edgebuffer->base+i+1));
1071
                dlprintf("closepath stroke %%PS\n");
1072
#endif
1073
11.3M
                if (log_op < 0)
1074
6.12M
                    code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
1075
5.22M
                else
1076
5.22M
                    code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
1077
11.3M
                if (code < 0)
1078
0
                    return code;
1079
11.3M
            }
1080
11.7M
        }
1081
7.26M
    }
1082
513k
    return 0;
1083
513k
}
1084
1085
/* Any part of a pixel routines */
1086
1087
static int edgecmp(const void *a, const void *b)
1088
971k
{
1089
971k
    int left  = ((int*)a)[0];
1090
971k
    int right = ((int*)b)[0];
1091
971k
    left -= right;
1092
971k
    if (left)
1093
935k
        return left;
1094
36.6k
    return ((int*)a)[1] - ((int*)b)[1];
1095
971k
}
1096
1097
#ifdef DEBUG_SCAN_CONVERTER
1098
static void
1099
gx_edgebuffer_print_app(gx_edgebuffer * edgebuffer)
1100
{
1101
    int i;
1102
    int borked = 0;
1103
1104
    if (!debugging_scan_converter)
1105
        return;
1106
1107
    dlprintf1("Edgebuffer %x\n", edgebuffer);
1108
    dlprintf4("xmin=%x xmax=%x base=%x height=%x\n",
1109
              edgebuffer->xmin, edgebuffer->xmax, edgebuffer->base, edgebuffer->height);
1110
    for (i=0; i < edgebuffer->height; i++) {
1111
        int  offset = edgebuffer->index[i];
1112
        int *row    = &edgebuffer->table[offset];
1113
        int count   = *row++;
1114
        int c       = count;
1115
        int wind    = 0;
1116
        dlprintf3("%x @ %d: %d =", i, offset, count);
1117
        while (count-- > 0) {
1118
            int left  = *row++;
1119
            int right = *row++;
1120
            int w     = -(left&1) | 1;
1121
            wind += w;
1122
            dlprintf3(" (%x,%x)%c", left&~1, right, left&1 ? 'v' : '^');
1123
        }
1124
        if (wind != 0 || c & 1) {
1125
            dlprintf(" <- BROKEN");
1126
            borked = 1;
1127
        }
1128
        dlprintf("\n");
1129
    }
1130
    if (borked) {
1131
        borked = borked; /* Breakpoint here */
1132
    }
1133
}
1134
#endif
1135
1136
typedef struct
1137
{
1138
    fixed  left;
1139
    fixed  right;
1140
    fixed  y;
1141
    signed char d; /* 0 up (or horiz), 1 down, -1 uninited */
1142
    unsigned char first;
1143
    unsigned char saved;
1144
1145
    fixed  save_left;
1146
    fixed  save_right;
1147
    int    save_iy;
1148
    int    save_d;
1149
1150
    int    scanlines;
1151
    int   *table;
1152
    int   *index;
1153
    int    base;
1154
} cursor;
1155
1156
static inline void
1157
cursor_output(cursor * gs_restrict cr, int iy)
1158
2.98M
{
1159
2.98M
    int *row;
1160
2.98M
    int count;
1161
1162
2.98M
    if (iy >= 0 && iy < cr->scanlines) {
1163
2.28M
        if (cr->first) {
1164
            /* Save it for later in case we join up */
1165
167k
            cr->save_left  = cr->left;
1166
167k
            cr->save_right = cr->right;
1167
167k
            cr->save_iy    = iy;
1168
167k
            cr->save_d     = cr->d;
1169
167k
            cr->saved      = 1;
1170
2.12M
        } else if (cr->d != DIRN_UNSET) {
1171
            /* Enter it into the table */
1172
2.11M
            row = &cr->table[cr->index[iy]];
1173
2.11M
            *row = count = (*row)+1; /* Increment the count */
1174
2.11M
            row[2 * count - 1] = (cr->left&~1) | cr->d;
1175
2.11M
            row[2 * count    ] = cr->right;
1176
2.11M
        } else {
1177
716
            assert(cr->left == max_fixed && cr->right == min_fixed);
1178
716
        }
1179
2.28M
    }
1180
2.98M
    cr->first = 0;
1181
2.98M
}
1182
1183
static inline void
1184
cursor_output_inrange(cursor * gs_restrict cr, int iy)
1185
2.76M
{
1186
2.76M
    int *row;
1187
2.76M
    int count;
1188
1189
2.76M
    assert(iy >= 0 && iy < cr->scanlines);
1190
2.76M
    if (cr->first) {
1191
        /* Save it for later in case we join up */
1192
7.82k
        cr->save_left  = cr->left;
1193
7.82k
        cr->save_right = cr->right;
1194
7.82k
        cr->save_iy    = iy;
1195
7.82k
        cr->save_d     = cr->d;
1196
7.82k
        cr->saved      = 1;
1197
2.75M
    } else {
1198
        /* Enter it into the table */
1199
2.75M
        assert(cr->d != DIRN_UNSET);
1200
1201
2.75M
        row = &cr->table[cr->index[iy]];
1202
2.75M
        *row = count = (*row)+1; /* Increment the count */
1203
2.75M
        row[2 * count - 1] = (cr->left&~1) | cr->d;
1204
2.75M
        row[2 * count    ] = cr->right;
1205
2.75M
    }
1206
2.76M
    cr->first = 0;
1207
2.76M
}
1208
1209
/* Step the cursor in y, allowing for maybe crossing a scanline */
1210
static inline void
1211
cursor_step(cursor * gs_restrict cr, fixed dy, fixed x, int skip)
1212
687k
{
1213
687k
    int new_iy;
1214
687k
    int iy = fixed2int(cr->y) - cr->base;
1215
1216
687k
    cr->y += dy;
1217
687k
    new_iy = fixed2int(cr->y) - cr->base;
1218
687k
    if (new_iy != iy) {
1219
687k
        if (!skip)
1220
673k
            cursor_output(cr, iy);
1221
687k
        cr->left = x;
1222
687k
        cr->right = x;
1223
687k
    } else {
1224
0
        if (x < cr->left)
1225
0
            cr->left = x;
1226
0
        if (x > cr->right)
1227
0
            cr->right = x;
1228
0
    }
1229
687k
}
1230
1231
/* Step the cursor in y, never by enough to cross a scanline. */
1232
static inline void
1233
cursor_never_step_vertical(cursor * gs_restrict cr, fixed dy, fixed x)
1234
17.7k
{
1235
17.7k
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
1236
1237
17.7k
    cr->y += dy;
1238
17.7k
}
1239
1240
/* Step the cursor in y, never by enough to cross a scanline,
1241
 * knowing that we are moving left, and that the right edge
1242
 * has already been accounted for. */
1243
static inline void
1244
cursor_never_step_left(cursor * gs_restrict cr, fixed dy, fixed x)
1245
157k
{
1246
157k
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
1247
1248
157k
    if (x < cr->left)
1249
115k
        cr->left = x;
1250
157k
    cr->y += dy;
1251
157k
}
1252
1253
/* Step the cursor in y, never by enough to cross a scanline,
1254
 * knowing that we are moving right, and that the left edge
1255
 * has already been accounted for. */
1256
static inline void
1257
cursor_never_step_right(cursor * gs_restrict cr, fixed dy, fixed x)
1258
156k
{
1259
156k
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
1260
1261
156k
    if (x > cr->right)
1262
152k
        cr->right = x;
1263
156k
    cr->y += dy;
1264
156k
}
1265
1266
/* Step the cursor in y, always by enough to cross a scanline. */
1267
static inline void
1268
cursor_always_step(cursor * gs_restrict cr, fixed dy, fixed x, int skip)
1269
656k
{
1270
656k
    int iy = fixed2int(cr->y) - cr->base;
1271
1272
656k
    if (!skip)
1273
505k
        cursor_output(cr, iy);
1274
656k
    cr->y += dy;
1275
656k
    cr->left = x;
1276
656k
    cr->right = x;
1277
656k
}
1278
1279
/* Step the cursor in y, always by enough to cross a scanline, as
1280
 * part of a vertical line, knowing that we are moving from a
1281
 * position guaranteed to be in the valid y range. */
1282
static inline void
1283
cursor_always_step_inrange_vertical(cursor * gs_restrict cr, fixed dy, fixed x)
1284
635k
{
1285
635k
    int iy = fixed2int(cr->y) - cr->base;
1286
1287
635k
    cursor_output(cr, iy);
1288
635k
    cr->y += dy;
1289
635k
}
1290
1291
/* Step the cursor in y, always by enough to cross a scanline, as
1292
 * part of a left moving line, knowing that we are moving from a
1293
 * position guaranteed to be in the valid y range. */
1294
static inline void
1295
cursor_always_inrange_step_left(cursor * gs_restrict cr, fixed dy, fixed x)
1296
1.47M
{
1297
1.47M
    int iy = fixed2int(cr->y) - cr->base;
1298
1299
1.47M
    cr->y += dy;
1300
1.47M
    cursor_output_inrange(cr, iy);
1301
1.47M
    cr->right = x;
1302
1.47M
}
1303
1304
/* Step the cursor in y, always by enough to cross a scanline, as
1305
 * part of a right moving line, knowing that we are moving from a
1306
 * position guaranteed to be in the valid y range. */
1307
static inline void
1308
cursor_always_inrange_step_right(cursor * gs_restrict cr, fixed dy, fixed x)
1309
1.28M
{
1310
1.28M
    int iy = fixed2int(cr->y) - cr->base;
1311
1312
1.28M
    cr->y += dy;
1313
1.28M
    cursor_output_inrange(cr, iy);
1314
1.28M
    cr->left = x;
1315
1.28M
}
1316
1317
static inline void cursor_left_merge(cursor * gs_restrict cr, fixed x)
1318
2.29M
{
1319
2.29M
    if (x < cr->left)
1320
913k
        cr->left = x;
1321
2.29M
}
1322
1323
static inline void cursor_left(cursor * gs_restrict cr, fixed x)
1324
1.95M
{
1325
1.95M
    cr->left = x;
1326
1.95M
}
1327
1328
static inline void cursor_right_merge(cursor * gs_restrict cr, fixed x)
1329
2.32M
{
1330
2.32M
    if (x > cr->right)
1331
859k
        cr->right = x;
1332
2.32M
}
1333
1334
static inline void cursor_right(cursor * gs_restrict cr, fixed x)
1335
1.75M
{
1336
1.75M
    cr->right = x;
1337
1.75M
}
1338
1339
static inline int cursor_down(cursor * gs_restrict cr, fixed x)
1340
789k
{
1341
789k
    int skip = 0;
1342
789k
    if ((cr->y & 0xff) == 0)
1343
164k
        skip = 1;
1344
789k
    if (cr->d == DIRN_UP)
1345
108k
    {
1346
108k
        if (!skip)
1347
108k
            cursor_output(cr, fixed2int(cr->y) - cr->base);
1348
108k
        cr->left = x;
1349
108k
        cr->right = x;
1350
108k
    }
1351
789k
    cr->d = DIRN_DOWN;
1352
789k
    return skip;
1353
789k
}
1354
1355
static inline void cursor_up(cursor * gs_restrict cr, fixed x)
1356
785k
{
1357
785k
    if (cr->d == DIRN_DOWN)
1358
106k
    {
1359
106k
        cursor_output(cr, fixed2int(cr->y) - cr->base);
1360
106k
        cr->left = x;
1361
106k
        cr->right = x;
1362
106k
    }
1363
785k
    cr->d = DIRN_UP;
1364
785k
}
1365
1366
static inline void
1367
cursor_flush(cursor * gs_restrict cr, fixed x)
1368
836k
{
1369
836k
    int iy;
1370
1371
    /* This should only happen if we were entirely out of bounds,
1372
     * or if everything was within a zero height horizontal
1373
     * rectangle from the start point. */
1374
836k
    if (cr->first) {
1375
246
        int iy = fixed2int(cr->y) - cr->base;
1376
        /* Any zero height rectangle counts as filled, except
1377
         * those on the baseline of a pixel. */
1378
246
        if (cr->d == DIRN_UNSET && (cr->y & 0xff) == 0)
1379
6
            return;
1380
246
        assert(cr->left != max_fixed && cr->right != min_fixed);
1381
240
        if (iy >= 0 && iy < cr->scanlines) {
1382
4
            int *row = &cr->table[cr->index[iy]];
1383
4
            int count = *row = (*row)+2; /* Increment the count */
1384
4
            row[2 * count - 3] = (cr->left & ~1) | DIRN_UP;
1385
4
            row[2 * count - 2] = (cr->right & ~1);
1386
4
            row[2 * count - 1] = (cr->right & ~1) | DIRN_DOWN;
1387
4
            row[2 * count    ] = cr->right;
1388
4
        }
1389
240
        return;
1390
246
    }
1391
1392
    /* Merge save into current if we can */
1393
836k
    iy = fixed2int(cr->y) - cr->base;
1394
836k
    if (cr->saved && iy == cr->save_iy &&
1395
136k
        (cr->d == cr->save_d || cr->save_d == DIRN_UNSET)) {
1396
66.6k
        if (cr->left > cr->save_left)
1397
18.4k
            cr->left = cr->save_left;
1398
66.6k
        if (cr->right < cr->save_right)
1399
19.6k
            cr->right = cr->save_right;
1400
66.6k
        cursor_output(cr, iy);
1401
66.6k
        return;
1402
66.6k
    }
1403
1404
    /* Merge not possible */
1405
770k
    cursor_output(cr, iy);
1406
770k
    if (cr->saved) {
1407
108k
        cr->left  = cr->save_left;
1408
108k
        cr->right = cr->save_right;
1409
108k
        assert(cr->save_d != DIRN_UNSET);
1410
108k
        if (cr->save_d != DIRN_UNSET)
1411
108k
            cr->d = cr->save_d;
1412
108k
        cursor_output(cr, cr->save_iy);
1413
108k
    }
1414
770k
}
1415
1416
static inline void
1417
cursor_null(cursor *cr)
1418
706k
{
1419
706k
    cr->right = min_fixed;
1420
706k
    cr->left  = max_fixed;
1421
706k
    cr->d     = DIRN_UNSET;
1422
706k
}
1423
1424
static void mark_line_app(cursor * gs_restrict cr, fixed sx, fixed sy, fixed ex, fixed ey)
1425
14.3M
{
1426
14.3M
    int isy, iey;
1427
14.3M
    fixed saved_sy = sy;
1428
14.3M
    fixed saved_ex = ex;
1429
14.3M
    fixed saved_ey = ey;
1430
14.3M
    int truncated;
1431
1432
14.3M
    if (sx == ex && sy == ey)
1433
817k
        return;
1434
1435
13.5M
    isy = fixed2int(sy) - cr->base;
1436
13.5M
    iey = fixed2int(ey) - cr->base;
1437
#ifdef DEBUG_SCAN_CONVERTER
1438
    if (debugging_scan_converter)
1439
        dlprintf6("Marking line (app) from %x,%x to %x,%x (%x,%x)\n", sx, sy, ex, ey, isy, iey);
1440
#endif
1441
#ifdef DEBUG_OUTPUT_SC_AS_PS
1442
    dlprintf("0.001 setlinewidth 0 0 0 setrgbcolor %%PS\n");
1443
    coord("moveto", sx, sy);
1444
    coord("lineto", ex, ey);
1445
    dlprintf("stroke %%PS\n");
1446
#endif
1447
1448
    /* Horizontal motion at the bottom of a pixel is ignored */
1449
13.5M
    if (sy == ey && (sy & 0xff) == 0)
1450
10.0k
        return;
1451
1452
13.5M
    assert(cr->y == sy &&
1453
13.5M
           ((cr->left <= sx && cr->right >= sx) || ((sy & 0xff) == 0)) &&
1454
13.5M
           cr->d >= DIRN_UNSET && cr->d <= DIRN_DOWN);
1455
1456
13.5M
    if (isy < iey) {
1457
        /* Rising line */
1458
6.12M
        if (iey < 0 || isy >= cr->scanlines) {
1459
            /* All line is outside. */
1460
5.62M
            if ((ey & 0xff) == 0)
1461
38.6k
                cursor_null(cr);
1462
5.58M
            else {
1463
5.58M
                cr->left = ex;
1464
5.58M
                cr->right = ex;
1465
5.58M
            }
1466
5.62M
            cr->y = ey;
1467
5.62M
            cr->first = 0;
1468
5.62M
            return;
1469
5.62M
        }
1470
503k
        if (isy < 0) {
1471
            /* Move sy up */
1472
160k
            int64_t y = (int64_t)ey - (int64_t)sy;
1473
160k
            fixed new_sy = int2fixed(cr->base);
1474
160k
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
1475
160k
            sx += (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1476
160k
            sy = new_sy;
1477
160k
            cursor_null(cr);
1478
160k
            cr->y = sy;
1479
160k
            isy = 0;
1480
160k
        }
1481
503k
        truncated = iey > cr->scanlines;
1482
503k
        if (truncated) {
1483
            /* Move ey down */
1484
148k
            int64_t y = ey - sy;
1485
148k
            fixed new_ey = int2fixed(cr->base + cr->scanlines);
1486
148k
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
1487
148k
            saved_ex = ex;
1488
148k
            saved_ey = ey;
1489
148k
            ex -= (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1490
148k
            ey = new_ey;
1491
148k
            iey = cr->scanlines;
1492
148k
        }
1493
7.43M
    } else {
1494
        /* Falling line */
1495
7.43M
        if (isy < 0 || iey >= cr->scanlines) {
1496
            /* All line is outside. */
1497
6.27M
            if ((ey & 0xff) == 0)
1498
30.7k
                cursor_null(cr);
1499
6.24M
            else {
1500
6.24M
                cr->left = ex;
1501
6.24M
                cr->right = ex;
1502
6.24M
            }
1503
6.27M
            cr->y = ey;
1504
6.27M
            cr->first = 0;
1505
6.27M
            return;
1506
6.27M
        }
1507
1.16M
        truncated = iey < 0;
1508
1.16M
        if (truncated) {
1509
            /* Move ey up */
1510
160k
            int64_t y = (int64_t)ey - (int64_t)sy;
1511
160k
            fixed new_ey = int2fixed(cr->base);
1512
160k
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
1513
160k
            ex -= (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1514
160k
            ey = new_ey;
1515
160k
            iey = 0;
1516
160k
        }
1517
1.16M
        if (isy >= cr->scanlines) {
1518
            /* Move sy down */
1519
161k
            int64_t y = (int64_t)ey - (int64_t)sy;
1520
161k
            fixed new_sy = int2fixed(cr->base + cr->scanlines);
1521
161k
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
1522
161k
            sx += (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1523
161k
            sy = new_sy;
1524
161k
            cursor_null(cr);
1525
161k
            cr->y = sy;
1526
161k
            isy = cr->scanlines;
1527
161k
        }
1528
1.16M
    }
1529
1530
1.66M
    cursor_left_merge(cr, sx);
1531
1.66M
    cursor_right_merge(cr, sx);
1532
1533
1.66M
    assert(cr->left <= sx);
1534
1.66M
    assert(cr->right >= sx);
1535
1.66M
    assert(cr->y == sy);
1536
1537
    /* A note: The code below used to be of the form:
1538
     *   if (isy == iey)   ... deal with horizontal lines
1539
     *   else if (ey > sy) {
1540
     *     fixed y_steps = ey - sy;
1541
     *      ... deal with rising lines ...
1542
     *   } else {
1543
     *     fixed y_steps = ey - sy;
1544
     *     ... deal with falling lines
1545
     *   }
1546
     * but that lead to problems, for instance, an example seen
1547
     * has sx=2aa8e, sy=8aee7, ex=7ffc1686, ey=8003e97a.
1548
     * Thus isy=84f, iey=ff80038a. We can see that ey < sy, but
1549
     * sy - ey < 0!
1550
     * We therefore rejig our code so that the choice between
1551
     * cases is done based on the sign of y_steps rather than
1552
     * the relative size of ey and sy.
1553
     */
1554
1555
    /* First, deal with lines that don't change scanline.
1556
     * This accommodates horizontal lines. */
1557
1.66M
    if (isy == iey) {
1558
678k
        if (saved_sy == saved_ey) {
1559
            /* Horizontal line. Don't change cr->d, don't flush. */
1560
92.3k
            if ((ey & 0xff) == 0)
1561
0
                goto no_merge;
1562
586k
        } else if (saved_sy > saved_ey) {
1563
            /* Falling line, flush if previous was rising */
1564
293k
            int skip = cursor_down(cr, sx);
1565
293k
            if ((ey & 0xff) == 0) {
1566
                /* We are falling to the baseline of a subpixel, so output
1567
                 * for the current pixel, and leave the cursor nulled. */
1568
12.8k
                if (sx <= ex) {
1569
7.54k
                    cursor_right_merge(cr, ex);
1570
7.54k
                } else {
1571
5.30k
                    cursor_left_merge(cr, ex);
1572
5.30k
                }
1573
12.8k
                if (!skip)
1574
12.6k
                    cursor_output(cr, fixed2int(cr->y) - cr->base);
1575
12.8k
                cursor_null(cr);
1576
12.8k
                goto no_merge;
1577
12.8k
            }
1578
293k
        } else {
1579
            /* Rising line, flush if previous was falling */
1580
292k
            cursor_up(cr, sx);
1581
292k
            if ((ey & 0xff) == 0) {
1582
256
                cursor_null(cr);
1583
256
                goto no_merge;
1584
256
            }
1585
292k
        }
1586
665k
        if (sx <= ex) {
1587
351k
            cursor_right_merge(cr, ex);
1588
351k
        } else {
1589
314k
            cursor_left_merge(cr, ex);
1590
314k
        }
1591
678k
no_merge:
1592
678k
        cr->y = ey;
1593
678k
        if (sy > saved_ey)
1594
293k
            goto endFalling;
1595
988k
    } else if (iey > isy) {
1596
        /* We want to change from sy to ey, which are guaranteed to be on
1597
         * different scanlines. We do this in 3 phases.
1598
         * Phase 1 gets us from sy to the next scanline boundary.
1599
         * Phase 2 gets us all the way to the last scanline boundary.
1600
         * Phase 3 gets us from the last scanline boundary to ey.
1601
         */
1602
        /* We want to change from sy to ey, which are guaranteed to be on
1603
         * different scanlines. We do this in 3 phases.
1604
         * Phase 1 gets us from sy to the next scanline boundary. (We may exit after phase 1).
1605
         * Phase 2 gets us all the way to the last scanline boundary. (This may be a null operation)
1606
         * Phase 3 gets us from the last scanline boundary to ey. (We are guaranteed to have output the cursor at least once before phase 3).
1607
         */
1608
492k
        int phase1_y_steps = (-sy) & (fixed_1 - 1);
1609
492k
        int phase3_y_steps = ey & (fixed_1 - 1);
1610
492k
        ufixed y_steps = (ufixed)ey - (ufixed)sy;
1611
1612
492k
        cursor_up(cr, sx);
1613
1614
492k
        if (sx == ex) {
1615
            /* Vertical line. (Rising) */
1616
1617
            /* Phase 1: */
1618
40.8k
            if (phase1_y_steps) {
1619
                /* If phase 1 will move us into a new scanline, then we must
1620
                 * flush it before we move. */
1621
18.8k
                cursor_step(cr, phase1_y_steps, sx, 0);
1622
18.8k
                sy += phase1_y_steps;
1623
18.8k
                y_steps -= phase1_y_steps;
1624
18.8k
                if (y_steps == 0) {
1625
685
                    cursor_null(cr);
1626
685
                    goto end;
1627
685
                }
1628
18.8k
            }
1629
1630
            /* Phase 3: precalculation */
1631
40.1k
            y_steps -= phase3_y_steps;
1632
1633
            /* Phase 2: */
1634
40.1k
            y_steps = fixed2int(y_steps);
1635
40.1k
            assert((int)y_steps >= 0);
1636
40.1k
            if (y_steps > 0) {
1637
34.6k
                cursor_always_step(cr, fixed_1, sx, 0);
1638
34.6k
                y_steps--;
1639
279k
                while (y_steps) {
1640
245k
                    cursor_always_step_inrange_vertical(cr, fixed_1, sx);
1641
245k
                    y_steps--;
1642
245k
                }
1643
34.6k
            }
1644
1645
            /* Phase 3 */
1646
40.1k
            assert(cr->left == sx && cr->right == sx);
1647
40.1k
            if (phase3_y_steps == 0)
1648
21.0k
                cursor_null(cr);
1649
19.1k
            else
1650
19.1k
                cr->y += phase3_y_steps;
1651
451k
        } else if (sx < ex) {
1652
            /* Lines increasing in x. (Rightwards, rising) */
1653
220k
            int phase1_x_steps, phase3_x_steps;
1654
220k
            fixed x_steps = ex - sx;
1655
1656
            /* Phase 1: */
1657
220k
            if (phase1_y_steps) {
1658
161k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1659
161k
                sx += phase1_x_steps;
1660
161k
                cursor_right_merge(cr, sx);
1661
161k
                x_steps -= phase1_x_steps;
1662
161k
                cursor_step(cr, phase1_y_steps, sx, 0);
1663
161k
                sy += phase1_y_steps;
1664
161k
                y_steps -= phase1_y_steps;
1665
161k
                if (y_steps == 0) {
1666
4.10k
                    cursor_null(cr);
1667
4.10k
                    goto end;
1668
4.10k
                }
1669
161k
            }
1670
1671
            /* Phase 3: precalculation */
1672
216k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1673
216k
            x_steps -= phase3_x_steps;
1674
216k
            y_steps -= phase3_y_steps;
1675
216k
            assert((y_steps & (fixed_1 - 1)) == 0);
1676
1677
            /* Phase 2: */
1678
216k
            y_steps = fixed2int(y_steps);
1679
216k
            assert((int)y_steps >= 0);
1680
216k
            if (y_steps) {
1681
                /* We want to change sx by x_steps in y_steps steps.
1682
                 * So each step, we add x_steps/y_steps to sx. That's x_inc + n_inc/y_steps. */
1683
141k
                int x_inc = x_steps/y_steps;
1684
141k
                int n_inc = x_steps - (x_inc * y_steps);
1685
141k
                int f = y_steps/2;
1686
141k
                int d = y_steps;
1687
1688
                /* Special casing the first iteration, allows us to simplify
1689
                 * the following loop. */
1690
141k
                sx += x_inc;
1691
141k
                f -= n_inc;
1692
141k
                if (f < 0)
1693
33.6k
                    f += d, sx++;
1694
141k
                cursor_right_merge(cr, sx);
1695
141k
                cursor_always_step(cr, fixed_1, sx, 0);
1696
141k
                y_steps--;
1697
1698
765k
                while (y_steps) {
1699
624k
                    sx += x_inc;
1700
624k
                    f -= n_inc;
1701
624k
                    if (f < 0)
1702
290k
                        f += d, sx++;
1703
624k
                    cursor_right(cr, sx);
1704
624k
                    cursor_always_inrange_step_right(cr, fixed_1, sx);
1705
624k
                    y_steps--;
1706
624k
                };
1707
141k
            }
1708
1709
            /* Phase 3 */
1710
216k
            assert(cr->left <= ex && cr->right >= sx);
1711
216k
            if (phase3_y_steps == 0)
1712
54.3k
                cursor_null(cr);
1713
161k
            else {
1714
161k
                cursor_right(cr, ex);
1715
161k
                cr->y += phase3_y_steps;
1716
161k
            }
1717
231k
        } else {
1718
            /* Lines decreasing in x. (Leftwards, rising) */
1719
231k
            int phase1_x_steps, phase3_x_steps;
1720
231k
            fixed x_steps = sx - ex;
1721
1722
            /* Phase 1: */
1723
231k
            if (phase1_y_steps) {
1724
161k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1725
161k
                x_steps -= phase1_x_steps;
1726
161k
                sx -= phase1_x_steps;
1727
161k
                cursor_left_merge(cr, sx);
1728
161k
                cursor_step(cr, phase1_y_steps, sx, 0);
1729
161k
                sy += phase1_y_steps;
1730
161k
                y_steps -= phase1_y_steps;
1731
161k
                if (y_steps == 0) {
1732
4.29k
                    cursor_null(cr);
1733
4.29k
                    goto end;
1734
4.29k
                }
1735
161k
            }
1736
1737
            /* Phase 3: precalculation */
1738
227k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1739
227k
            x_steps -= phase3_x_steps;
1740
227k
            y_steps -= phase3_y_steps;
1741
227k
            assert((y_steps & (fixed_1 - 1)) == 0);
1742
1743
            /* Phase 2: */
1744
227k
            y_steps = fixed2int(y_steps);
1745
227k
            assert((int)y_steps >= 0);
1746
227k
            if (y_steps) {
1747
                /* We want to change sx by x_steps in y_steps steps.
1748
                 * So each step, we sub x_steps/y_steps from sx. That's x_inc + n_inc/ey. */
1749
151k
                int x_inc = x_steps/y_steps;
1750
151k
                int n_inc = x_steps - (x_inc * y_steps);
1751
151k
                int f = y_steps/2;
1752
151k
                int d = y_steps;
1753
1754
                /* Special casing the first iteration, allows us to simplify
1755
                 * the following loop. */
1756
151k
                sx -= x_inc;
1757
151k
                f -= n_inc;
1758
151k
                if (f < 0)
1759
32.5k
                    f += d, sx--;
1760
151k
                cursor_left_merge(cr, sx);
1761
151k
                cursor_always_step(cr, fixed_1, sx, 0);
1762
151k
                y_steps--;
1763
1764
908k
                while (y_steps) {
1765
756k
                    sx -= x_inc;
1766
756k
                    f -= n_inc;
1767
756k
                    if (f < 0)
1768
307k
                        f += d, sx--;
1769
756k
                    cursor_left(cr, sx);
1770
756k
                    cursor_always_inrange_step_left(cr, fixed_1, sx);
1771
756k
                    y_steps--;
1772
756k
                }
1773
151k
            }
1774
1775
            /* Phase 3 */
1776
227k
            assert(cr->right >= ex && cr->left <= sx);
1777
227k
            if (phase3_y_steps == 0)
1778
67.2k
                cursor_null(cr);
1779
160k
            else {
1780
160k
                cursor_left(cr, ex);
1781
160k
                cr->y += phase3_y_steps;
1782
160k
            }
1783
227k
        }
1784
495k
    } else {
1785
        /* So lines decreasing in y. */
1786
        /* We want to change from sy to ey, which are guaranteed to be on
1787
         * different scanlines. We do this in 3 phases.
1788
         * Phase 1 gets us from sy to the next scanline boundary. This never causes an output.
1789
         * Phase 2 gets us all the way to the last scanline boundary. This is guaranteed to cause an output.
1790
         * Phase 3 gets us from the last scanline boundary to ey. We are guaranteed to have outputted by now.
1791
         */
1792
495k
        int phase1_y_steps = sy & (fixed_1 - 1);
1793
495k
        int phase3_y_steps = (-ey) & (fixed_1 - 1);
1794
495k
        ufixed y_steps = (ufixed)sy - (ufixed)ey;
1795
1796
495k
        int skip = cursor_down(cr, sx);
1797
1798
495k
        if (sx == ex) {
1799
            /* Vertical line. (Falling) */
1800
1801
            /* Phase 1: */
1802
40.7k
            if (phase1_y_steps) {
1803
                /* Phase 1 in a falling line never moves us into a new scanline. */
1804
17.7k
                cursor_never_step_vertical(cr, -phase1_y_steps, sx);
1805
17.7k
                sy -= phase1_y_steps;
1806
17.7k
                y_steps -= phase1_y_steps;
1807
17.7k
                if (y_steps == 0)
1808
0
                    goto endFallingLeftOnEdgeOfPixel;
1809
17.7k
            }
1810
1811
            /* Phase 3: precalculation */
1812
40.7k
            y_steps -= phase3_y_steps;
1813
40.7k
            assert((y_steps & (fixed_1 - 1)) == 0);
1814
1815
            /* Phase 2: */
1816
40.7k
            y_steps = fixed2int(y_steps);
1817
40.7k
            assert((int)y_steps >= 0);
1818
40.7k
            if (y_steps) {
1819
34.8k
                cursor_always_step(cr, -fixed_1, sx, skip);
1820
34.8k
                skip = 0;
1821
34.8k
                y_steps--;
1822
274k
                while (y_steps) {
1823
239k
                    cursor_always_step_inrange_vertical(cr, -fixed_1, sx);
1824
239k
                    y_steps--;
1825
239k
                }
1826
34.8k
            }
1827
1828
            /* Phase 3 */
1829
40.7k
            if (phase3_y_steps == 0) {
1830
21.8k
endFallingLeftOnEdgeOfPixel:
1831
21.8k
                cursor_always_step_inrange_vertical(cr, 0, sx);
1832
21.8k
                cursor_null(cr);
1833
21.8k
            } else {
1834
18.8k
                cursor_step(cr, -phase3_y_steps, sx, skip);
1835
18.8k
                assert(cr->left == sx && cr->right == sx);
1836
18.8k
            }
1837
455k
        } else if (sx < ex) {
1838
            /* Lines increasing in x. (Rightwards, falling) */
1839
223k
            int phase1_x_steps, phase3_x_steps;
1840
223k
            fixed x_steps = ex - sx;
1841
1842
            /* Phase 1: */
1843
223k
            if (phase1_y_steps) {
1844
156k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1845
156k
                x_steps -= phase1_x_steps;
1846
156k
                sx += phase1_x_steps;
1847
                /* Phase 1 in a falling line never moves us into a new scanline. */
1848
156k
                cursor_never_step_right(cr, -phase1_y_steps, sx);
1849
156k
                sy -= phase1_y_steps;
1850
156k
                y_steps -= phase1_y_steps;
1851
156k
                if (y_steps == 0)
1852
0
                    goto endFallingRightOnEdgeOfPixel;
1853
156k
            }
1854
1855
            /* Phase 3: precalculation */
1856
223k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1857
223k
            x_steps -= phase3_x_steps;
1858
223k
            y_steps -= phase3_y_steps;
1859
223k
            assert((y_steps & (fixed_1 - 1)) == 0);
1860
1861
            /* Phase 2: */
1862
223k
            y_steps = fixed2int(y_steps);
1863
223k
            assert((int)y_steps >= 0);
1864
223k
            if (y_steps) {
1865
                /* We want to change sx by x_steps in y_steps steps.
1866
                 * So each step, we add x_steps/y_steps to sx. That's x_inc + n_inc/ey. */
1867
144k
                int x_inc = x_steps/y_steps;
1868
144k
                int n_inc = x_steps - (x_inc * y_steps);
1869
144k
                int f = y_steps/2;
1870
144k
                int d = y_steps;
1871
1872
144k
                cursor_always_step(cr, -fixed_1, sx, skip);
1873
144k
                skip = 0;
1874
144k
                sx += x_inc;
1875
144k
                f -= n_inc;
1876
144k
                if (f < 0)
1877
36.6k
                    f += d, sx++;
1878
144k
                cursor_right(cr, sx);
1879
144k
                y_steps--;
1880
1881
803k
                while (y_steps) {
1882
658k
                    cursor_always_inrange_step_right(cr, -fixed_1, sx);
1883
658k
                    sx += x_inc;
1884
658k
                    f -= n_inc;
1885
658k
                    if (f < 0)
1886
299k
                        f += d, sx++;
1887
658k
                    cursor_right(cr, sx);
1888
658k
                    y_steps--;
1889
658k
                }
1890
144k
            }
1891
1892
            /* Phase 3 */
1893
223k
            if (phase3_y_steps == 0) {
1894
61.8k
endFallingRightOnEdgeOfPixel:
1895
61.8k
                cursor_always_step_inrange_vertical(cr, 0, sx);
1896
61.8k
                cursor_null(cr);
1897
161k
            } else {
1898
161k
                cursor_step(cr, -phase3_y_steps, sx, skip);
1899
161k
                cursor_right(cr, ex);
1900
161k
                assert(cr->left == sx && cr->right == ex);
1901
161k
            }
1902
231k
        } else {
1903
            /* Lines decreasing in x. (Falling) */
1904
231k
            int phase1_x_steps, phase3_x_steps;
1905
231k
            fixed x_steps = sx - ex;
1906
1907
            /* Phase 1: */
1908
231k
            if (phase1_y_steps) {
1909
157k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1910
157k
                x_steps -= phase1_x_steps;
1911
157k
                sx -= phase1_x_steps;
1912
                /* Phase 1 in a falling line never moves us into a new scanline. */
1913
157k
                cursor_never_step_left(cr, -phase1_y_steps, sx);
1914
157k
                sy -= phase1_y_steps;
1915
157k
                y_steps -= phase1_y_steps;
1916
157k
                if (y_steps == 0)
1917
0
                    goto endFallingVerticalOnEdgeOfPixel;
1918
157k
            }
1919
1920
            /* Phase 3: precalculation */
1921
231k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1922
231k
            x_steps -= phase3_x_steps;
1923
231k
            y_steps -= phase3_y_steps;
1924
231k
            assert((y_steps & (fixed_1 - 1)) == 0);
1925
1926
            /* Phase 2: */
1927
231k
            y_steps = fixed2int(y_steps);
1928
231k
            assert((int)y_steps >= 0);
1929
231k
            if (y_steps) {
1930
                /* We want to change sx by x_steps in y_steps steps.
1931
                 * So each step, we sub x_steps/y_steps from sx. That's x_inc + n_inc/ey. */
1932
149k
                int x_inc = x_steps/y_steps;
1933
149k
                int n_inc = x_steps - (x_inc * y_steps);
1934
149k
                int f = y_steps/2;
1935
149k
                int d = y_steps;
1936
1937
149k
                cursor_always_step(cr, -fixed_1, sx, skip);
1938
149k
                skip = 0;
1939
149k
                sx -= x_inc;
1940
149k
                f -= n_inc;
1941
149k
                if (f < 0)
1942
36.0k
                    f += d, sx--;
1943
149k
                cursor_left(cr, sx);
1944
149k
                y_steps--;
1945
1946
872k
                while (y_steps) {
1947
723k
                    cursor_always_inrange_step_left(cr, -fixed_1, sx);
1948
723k
                    sx -= x_inc;
1949
723k
                    f -= n_inc;
1950
723k
                    if (f < 0)
1951
324k
                        f += d, sx--;
1952
723k
                    cursor_left(cr, sx);
1953
723k
                    y_steps--;
1954
723k
                }
1955
149k
            }
1956
1957
            /* Phase 3 */
1958
231k
            if (phase3_y_steps == 0) {
1959
67.1k
endFallingVerticalOnEdgeOfPixel:
1960
67.1k
                cursor_always_step_inrange_vertical(cr, 0, sx);
1961
67.1k
                cursor_null(cr);
1962
164k
            } else {
1963
164k
                cursor_step(cr, -phase3_y_steps, sx, skip);
1964
164k
                cursor_left(cr, ex);
1965
164k
                assert(cr->left == ex && cr->right == sx);
1966
164k
            }
1967
231k
        }
1968
789k
endFalling: {}
1969
789k
    }
1970
1971
1.66M
end:
1972
1.66M
    if (truncated) {
1973
308k
        cr->left = saved_ex;
1974
308k
        cr->right = saved_ex;
1975
308k
        cr->y = saved_ey;
1976
308k
    }
1977
1.66M
}
1978
1979
static void mark_curve_app(cursor *cr, fixed sx, fixed sy, fixed c1x, fixed c1y, fixed c2x, fixed c2y, fixed ex, fixed ey, int depth)
1980
11.4k
{
1981
11.4k
        int ax = (sx + c1x)>>1;
1982
11.4k
        int ay = (sy + c1y)>>1;
1983
11.4k
        int bx = (c1x + c2x)>>1;
1984
11.4k
        int by = (c1y + c2y)>>1;
1985
11.4k
        int cx = (c2x + ex)>>1;
1986
11.4k
        int cy = (c2y + ey)>>1;
1987
11.4k
        int dx = (ax + bx)>>1;
1988
11.4k
        int dy = (ay + by)>>1;
1989
11.4k
        int fx = (bx + cx)>>1;
1990
11.4k
        int fy = (by + cy)>>1;
1991
11.4k
        int gx = (dx + fx)>>1;
1992
11.4k
        int gy = (dy + fy)>>1;
1993
1994
11.4k
        assert(depth >= 0);
1995
11.4k
        if (depth == 0)
1996
11.0k
            mark_line_app(cr, sx, sy, ex, ey);
1997
395
        else {
1998
395
            depth--;
1999
395
            mark_curve_app(cr, sx, sy, ax, ay, dx, dy, gx, gy, depth);
2000
395
            mark_curve_app(cr, gx, gy, fx, fy, cx, cy, ex, ey, depth);
2001
395
        }
2002
11.4k
}
2003
2004
static void mark_curve_big_app(cursor *cr, fixed64 sx, fixed64 sy, fixed64 c1x, fixed64 c1y, fixed64 c2x, fixed64 c2y, fixed64 ex, fixed64 ey, int depth)
2005
0
{
2006
0
    fixed64 ax = (sx + c1x)>>1;
2007
0
    fixed64 ay = (sy + c1y)>>1;
2008
0
    fixed64 bx = (c1x + c2x)>>1;
2009
0
    fixed64 by = (c1y + c2y)>>1;
2010
0
    fixed64 cx = (c2x + ex)>>1;
2011
0
    fixed64 cy = (c2y + ey)>>1;
2012
0
    fixed64 dx = (ax + bx)>>1;
2013
0
    fixed64 dy = (ay + by)>>1;
2014
0
    fixed64 fx = (bx + cx)>>1;
2015
0
    fixed64 fy = (by + cy)>>1;
2016
0
    fixed64 gx = (dx + fx)>>1;
2017
0
    fixed64 gy = (dy + fy)>>1;
2018
2019
0
    assert(depth >= 0);
2020
0
    if (depth == 0)
2021
0
        mark_line_app(cr, (fixed)sx, (fixed)sy, (fixed)ex, (fixed)ey);
2022
0
    else {
2023
0
        depth--;
2024
0
        mark_curve_big_app(cr, sx, sy, ax, ay, dx, dy, gx, gy, depth);
2025
0
        mark_curve_big_app(cr, gx, gy, fx, fy, cx, cy, ex, ey, depth);
2026
0
    }
2027
0
}
2028
2029
static void mark_curve_top_app(cursor *cr, fixed sx, fixed sy, fixed c1x, fixed c1y, fixed c2x, fixed c2y, fixed ex, fixed ey, int depth)
2030
10.6k
{
2031
10.6k
    fixed test = (sx^(sx<<1))|(sy^(sy<<1))|(c1x^(c1x<<1))|(c1y^(c1y<<1))|(c2x^(c2x<<1))|(c2y^(c2y<<1))|(ex^(ex<<1))|(ey^(ey<<1));
2032
2033
10.6k
    if (test < 0)
2034
0
        mark_curve_big_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth);
2035
10.6k
    else
2036
10.6k
        mark_curve_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth);
2037
10.6k
}
2038
2039
static int make_table_app(gx_device     * pdev,
2040
                          gx_path       * path,
2041
                          gs_fixed_rect * ibox,
2042
                          int           * scanlines,
2043
                          int          ** index,
2044
                          int          ** table)
2045
238k
{
2046
238k
    return make_table_template(pdev, path, ibox, 2, 0, scanlines, index, table);
2047
238k
}
2048
2049
static void
2050
fill_zero_app(int *row, const fixed *x)
2051
37
{
2052
37
    int n = *row = (*row)+2; /* Increment the count */
2053
37
    row[2*n-3] = (x[0]&~1);
2054
37
    row[2*n-2] = (x[1]&~1);
2055
37
    row[2*n-1] = (x[1]&~1)|1;
2056
37
    row[2*n  ] = x[1];
2057
37
}
2058
2059
int gx_scan_convert_app(gx_device     * gs_restrict pdev,
2060
                        gx_path       * gs_restrict path,
2061
                  const gs_fixed_rect * gs_restrict clip,
2062
                        gx_edgebuffer * gs_restrict edgebuffer,
2063
                        fixed                    fixed_flat)
2064
240k
{
2065
240k
    gs_fixed_rect  ibox;
2066
240k
    gs_fixed_rect  bbox;
2067
240k
    int            scanlines;
2068
240k
    const subpath *psub;
2069
240k
    int           *index;
2070
240k
    int           *table;
2071
240k
    int            i;
2072
240k
    cursor         cr;
2073
240k
    int            code;
2074
240k
    int            zero;
2075
2076
240k
    edgebuffer->index = NULL;
2077
240k
    edgebuffer->table = NULL;
2078
2079
    /* Bale out if no actual path. We see this with the clist */
2080
240k
    if (path->first_subpath == NULL)
2081
501
        return 0;
2082
2083
239k
    zero = make_bbox(path, clip, &bbox, &ibox, 0);
2084
239k
    if (zero < 0)
2085
0
        return zero;
2086
2087
239k
    if (ibox.q.y <= ibox.p.y)
2088
1.32k
        return 0;
2089
2090
238k
    code = make_table_app(pdev, path, &ibox, &scanlines, &index, &table);
2091
238k
    if (code != 0) /* > 0 means "retry with smaller height" */
2092
0
        return code;
2093
2094
238k
    if (scanlines == 0)
2095
0
        return 0;
2096
2097
238k
    if (zero) {
2098
37
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero_app);
2099
238k
    } else {
2100
2101
    /* Step 2 continued: Now we run through the path, filling in the real
2102
     * values. */
2103
238k
    cr.scanlines = scanlines;
2104
238k
    cr.index     = index;
2105
238k
    cr.table     = table;
2106
238k
    cr.base      = ibox.p.y;
2107
1.07M
    for (psub = path->first_subpath; psub != 0;) {
2108
836k
        const segment *pseg = (const segment *)psub;
2109
836k
        fixed ex = pseg->pt.x;
2110
836k
        fixed ey = pseg->pt.y;
2111
836k
        fixed ix = ex;
2112
836k
        fixed iy = ey;
2113
836k
        fixed sx, sy;
2114
2115
836k
        if ((ey & 0xff) == 0) {
2116
6.48k
            cr.left  = max_fixed;
2117
6.48k
            cr.right = min_fixed;
2118
830k
        } else {
2119
830k
            cr.left = cr.right = ex;
2120
830k
        }
2121
836k
        cr.y = ey;
2122
836k
        cr.d = DIRN_UNSET;
2123
836k
        cr.first = 1;
2124
836k
        cr.saved = 0;
2125
2126
14.3M
        while ((pseg = pseg->next) != 0 &&
2127
14.1M
               pseg->type != s_start
2128
13.5M
            ) {
2129
13.5M
            sx = ex;
2130
13.5M
            sy = ey;
2131
13.5M
            ex = pseg->pt.x;
2132
13.5M
            ey = pseg->pt.y;
2133
2134
13.5M
            switch (pseg->type) {
2135
0
                default:
2136
0
                case s_start: /* Should never happen */
2137
0
                case s_dash:  /* We should never be seeing a dash here */
2138
0
                    assert("This should never happen" == NULL);
2139
0
                    break;
2140
10.6k
                case s_curve: {
2141
10.6k
                    const curve_segment *const pcur = (const curve_segment *)pseg;
2142
10.6k
                    int k = gx_curve_log2_samples(sx, sy, pcur, fixed_flat);
2143
2144
10.6k
                    mark_curve_top_app(&cr, sx, sy, pcur->p1.x, pcur->p1.y, pcur->p2.x, pcur->p2.y, ex, ey, k);
2145
10.6k
                    break;
2146
0
                }
2147
0
                case s_gap:
2148
12.7M
                case s_line:
2149
13.5M
                case s_line_close:
2150
13.5M
                    mark_line_app(&cr, sx, sy, ex, ey);
2151
13.5M
                    break;
2152
13.5M
            }
2153
13.5M
        }
2154
        /* And close any open segments */
2155
836k
        mark_line_app(&cr, ex, ey, ix, iy);
2156
836k
        cursor_flush(&cr, ex);
2157
836k
        psub = (const subpath *)pseg;
2158
836k
    }
2159
238k
    }
2160
2161
    /* Step 2 complete: We now have a complete list of intersection data in
2162
     * table, indexed by index. */
2163
2164
238k
    edgebuffer->base   = ibox.p.y;
2165
238k
    edgebuffer->height = scanlines;
2166
238k
    edgebuffer->xmin   = ibox.p.x;
2167
238k
    edgebuffer->xmax   = ibox.q.x;
2168
238k
    edgebuffer->index  = index;
2169
238k
    edgebuffer->table  = table;
2170
2171
#ifdef DEBUG_SCAN_CONVERTER
2172
    if (debugging_scan_converter) {
2173
        dlprintf("Before sorting:\n");
2174
        gx_edgebuffer_print_app(edgebuffer);
2175
    }
2176
#endif
2177
2178
    /* Step 3: Sort the intersects on x */
2179
2.19M
    for (i=0; i < scanlines; i++) {
2180
1.96M
        int *row = &table[index[i]];
2181
1.96M
        int  rowlen = *row++;
2182
2183
        /* Bubblesort short runs, qsort longer ones. */
2184
        /* FIXME: Verify the figure 6 below */
2185
1.96M
        if (rowlen <= 6) {
2186
1.92M
            int j, k;
2187
4.47M
            for (j = 0; j < rowlen-1; j++) {
2188
2.55M
                int * gs_restrict t = &row[j<<1];
2189
6.09M
                for (k = j+1; k < rowlen; k++) {
2190
3.54M
                    int * gs_restrict s = &row[k<<1];
2191
3.54M
                    int tmp;
2192
3.54M
                    if (t[0] < s[0])
2193
1.32M
                        continue;
2194
2.22M
                    if (t[0] > s[0])
2195
2.21M
                        goto swap01;
2196
9.55k
                    if (t[1] <= s[1])
2197
9.30k
                        continue;
2198
246
                    if (0) {
2199
2.21M
swap01:
2200
2.21M
                        tmp = t[0], t[0] = s[0], s[0] = tmp;
2201
2.21M
                    }
2202
2.21M
                    tmp = t[1], t[1] = s[1], s[1] = tmp;
2203
2.21M
                }
2204
2.55M
            }
2205
1.92M
        } else
2206
35.0k
            qsort(row, rowlen, 2*sizeof(int), edgecmp);
2207
1.96M
    }
2208
2209
238k
    return 0;
2210
238k
}
2211
2212
/* Step 5: Filter the intersections according to the rules */
2213
int
2214
gx_filter_edgebuffer_app(gx_device       * gs_restrict pdev,
2215
                         gx_edgebuffer   * gs_restrict edgebuffer,
2216
                         int                        rule)
2217
240k
{
2218
240k
    int i;
2219
2220
#ifdef DEBUG_SCAN_CONVERTER
2221
    if (debugging_scan_converter) {
2222
        dlprintf("Before filtering:\n");
2223
        gx_edgebuffer_print_app(edgebuffer);
2224
    }
2225
#endif
2226
2227
2.20M
    for (i=0; i < edgebuffer->height; i++) {
2228
1.96M
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
2229
1.96M
        int  rowlen   = *row++;
2230
1.96M
        int *rowstart = row;
2231
1.96M
        int *rowout   = row;
2232
1.96M
        int  ll, lr, rl, rr, wind, marked_to;
2233
2234
        /* Avoid double setting pixels, by keeping where we have marked to. */
2235
1.96M
        marked_to = INT_MIN;
2236
4.27M
        while (rowlen > 0) {
2237
2.31M
            if (rule == gx_rule_even_odd) {
2238
                /* Even Odd */
2239
125k
                ll = (*row++)&~1;
2240
125k
                lr = *row;
2241
125k
                row += 2;
2242
125k
                rowlen-=2;
2243
2244
                /* We will fill solidly from ll to at least lr, possibly further */
2245
125k
                assert(rowlen >= 0);
2246
125k
                rr = (*row++);
2247
125k
                if (rr > lr)
2248
111k
                    lr = rr;
2249
2.18M
            } else {
2250
                /* Non-Zero */
2251
2.18M
                int w;
2252
2253
2.18M
                ll = *row++;
2254
2.18M
                lr = *row++;
2255
2.18M
                wind = -(ll&1) | 1;
2256
2.18M
                ll &= ~1;
2257
2.18M
                rowlen--;
2258
2259
2.18M
                assert(rowlen > 0);
2260
2.43M
                do {
2261
2.43M
                    rl = *row++;
2262
2.43M
                    rr = *row++;
2263
2.43M
                    w = -(rl&1) | 1;
2264
2.43M
                    rl &= ~1;
2265
2.43M
                    rowlen--;
2266
2.43M
                    if (rr > lr)
2267
2.32M
                        lr = rr;
2268
2.43M
                    wind += w;
2269
2.43M
                    if (wind == 0)
2270
2.18M
                        break;
2271
2.43M
                } while (rowlen > 0);
2272
2.18M
            }
2273
2274
2.31M
            if (marked_to >= lr)
2275
791
                continue;
2276
2277
2.31M
            if (marked_to >= ll) {
2278
18.6k
                if (rowout == rowstart)
2279
0
                    ll = marked_to;
2280
18.6k
                else {
2281
18.6k
                    rowout -= 2;
2282
18.6k
                    ll = *rowout;
2283
18.6k
                }
2284
18.6k
            }
2285
2286
2.31M
            if (lr >= ll) {
2287
2.31M
                *rowout++ = ll;
2288
2.31M
                *rowout++ = lr;
2289
2.31M
                marked_to = lr;
2290
2.31M
            }
2291
2.31M
        }
2292
1.96M
        rowstart[-1] = rowout - rowstart;
2293
1.96M
    }
2294
240k
    return 0;
2295
240k
}
2296
2297
/* Step 6: Fill */
2298
int
2299
gx_fill_edgebuffer_app(gx_device       * gs_restrict pdev,
2300
                 const gx_device_color * gs_restrict pdevc,
2301
                       gx_edgebuffer   * gs_restrict edgebuffer,
2302
                       int                        log_op)
2303
240k
{
2304
240k
    int i, code;
2305
2306
2.20M
    for (i=0; i < edgebuffer->height; i++) {
2307
1.96M
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
2308
1.96M
        int  rowlen = *row++;
2309
1.96M
        int  left, right;
2310
2311
4.25M
        while (rowlen > 0) {
2312
2.29M
            left  = *row++;
2313
2.29M
            right = *row++;
2314
2.29M
            left  = fixed2int(left);
2315
2.29M
            right = fixed2int(right + fixed_1 - 1);
2316
2.29M
            rowlen -= 2;
2317
2318
2.29M
            right -= left;
2319
2.29M
            if (right > 0) {
2320
2.29M
                if (log_op < 0)
2321
1.98M
                    code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
2322
314k
                else
2323
314k
                    code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
2324
2.29M
                if (code < 0)
2325
0
                    return code;
2326
2.29M
            }
2327
2.29M
        }
2328
1.96M
    }
2329
240k
    return 0;
2330
240k
}
2331
2332
/* Centre of a pixel trapezoid routines */
2333
2334
static int intcmp_tr(const void *a, const void *b)
2335
3.04M
{
2336
3.04M
    int left  = ((int*)a)[0];
2337
3.04M
    int right = ((int*)b)[0];
2338
3.04M
    if (left != right)
2339
3.03M
        return left - right;
2340
5.52k
    return ((int*)a)[1] - ((int*)b)[1];
2341
3.04M
}
2342
2343
#ifdef DEBUG_SCAN_CONVERTER
2344
static void
2345
gx_edgebuffer_print_tr(gx_edgebuffer * edgebuffer)
2346
{
2347
    int i;
2348
2349
    if (!debugging_scan_converter)
2350
        return;
2351
2352
    dlprintf1("Edgebuffer %x\n", edgebuffer);
2353
    dlprintf4("xmin=%x xmax=%x base=%x height=%x\n",
2354
              edgebuffer->xmin, edgebuffer->xmax, edgebuffer->base, edgebuffer->height);
2355
    for (i=0; i < edgebuffer->height; i++)
2356
    {
2357
        int  offset = edgebuffer->index[i];
2358
        int *row    = &edgebuffer->table[offset];
2359
        int count   = *row++;
2360
        dlprintf3("%d @ %d: %d =", i, offset, count);
2361
        while (count-- > 0) {
2362
            int e  = *row++;
2363
            int id = *row++;
2364
            dlprintf3(" %x%c%d", e, id&1 ? 'v' : '^', id>>1);
2365
        }
2366
        dlprintf("\n");
2367
    }
2368
}
2369
#endif
2370
2371
static void mark_line_tr(fixed sx, fixed sy, fixed ex, fixed ey, int base_y, int height, int *table, int *index, int id)
2372
27.2M
{
2373
27.2M
    int64_t delta;
2374
27.2M
    int iy, ih;
2375
27.2M
    fixed clip_sy, clip_ey;
2376
27.2M
    int dirn = DIRN_UP;
2377
27.2M
    int *row;
2378
2379
#ifdef DEBUG_SCAN_CONVERTER
2380
    if (debugging_scan_converter)
2381
        dlprintf6("Marking line (tr) from %x,%x to %x,%x (%x,%x)\n", sx, sy, ex, ey, fixed2int(sy + fixed_half-1) - base_y, fixed2int(ey + fixed_half-1) - base_y);
2382
#endif
2383
#ifdef DEBUG_OUTPUT_SC_AS_PS
2384
    dlprintf("0.001 setlinewidth 0 0 0 setrgbcolor %%PS\n");
2385
    coord("moveto", sx, sy);
2386
    coord("lineto", ex, ey);
2387
    dlprintf("stroke %%PS\n");
2388
#endif
2389
2390
27.2M
    if (fixed2int(sy + fixed_half-1) == fixed2int(ey + fixed_half-1))
2391
17.2M
        return;
2392
10.0M
    if (sy > ey) {
2393
4.84M
        int t;
2394
4.84M
        t = sy; sy = ey; ey = t;
2395
4.84M
        t = sx; sx = ex; ex = t;
2396
4.84M
        dirn = DIRN_DOWN;
2397
4.84M
    }
2398
    /* Lines go from sy to ey, closed at the start, open at the end. */
2399
    /* We clip them to a region to make them closed at both ends. */
2400
    /* Thus the first scanline marked (>= sy) is: */
2401
10.0M
    clip_sy = ((sy + fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
2402
    /* The last scanline marked (< ey) is: */
2403
10.0M
    clip_ey = ((ey - fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
2404
    /* Now allow for banding */
2405
10.0M
    if (clip_sy < int2fixed(base_y) + fixed_half)
2406
7.98M
        clip_sy = int2fixed(base_y) + fixed_half;
2407
10.0M
    if (ey <= clip_sy)
2408
7.67M
        return;
2409
2.33M
    if (clip_ey > int2fixed(base_y + height - 1) + fixed_half)
2410
1.14M
        clip_ey = int2fixed(base_y + height - 1) + fixed_half;
2411
2.33M
    if (sy > clip_ey)
2412
866k
        return;
2413
1.47M
    delta = (int64_t)clip_sy - (int64_t)sy;
2414
1.47M
    if (delta > 0)
2415
1.44M
    {
2416
1.44M
        int64_t dx = (int64_t)ex - (int64_t)sx;
2417
1.44M
        int64_t dy = (int64_t)ey - (int64_t)sy;
2418
1.44M
        int advance = (int)((dx * delta + (dy>>1)) / dy);
2419
1.44M
        sx += advance;
2420
1.44M
        sy += delta;
2421
1.44M
    }
2422
1.47M
    delta = (int64_t)ey - (int64_t)clip_ey;
2423
1.47M
    if (delta > 0)
2424
1.47M
    {
2425
1.47M
        int64_t dx = (int64_t)ex - (int64_t)sx;
2426
1.47M
        int64_t dy = (int64_t)ey - (int64_t)sy;
2427
1.47M
        int advance = (int)((dx * delta + (dy>>1)) / dy);
2428
1.47M
        ex -= advance;
2429
1.47M
        ey -= delta;
2430
1.47M
    }
2431
1.47M
    ex -= sx;
2432
1.47M
    ey -= sy;
2433
1.47M
    ih = fixed2int(ey);
2434
1.47M
    assert(ih >= 0);
2435
1.47M
    iy = fixed2int(sy) - base_y;
2436
#ifdef DEBUG_SCAN_CONVERTER
2437
    if (debugging_scan_converter)
2438
        dlprintf2("    iy=%x ih=%x\n", iy, ih);
2439
#endif
2440
1.47M
    assert(iy >= 0 && iy < height);
2441
1.47M
    id = (id<<1) | dirn;
2442
    /* We always cross at least one scanline */
2443
1.47M
    row = &table[index[iy]];
2444
1.47M
    *row = (*row)+1; /* Increment the count */
2445
1.47M
    row[*row * 2 - 1] = sx;
2446
1.47M
    row[*row * 2    ] = id;
2447
1.47M
    if (ih == 0)
2448
750k
        return;
2449
720k
    if (ex >= 0) {
2450
521k
        int x_inc, n_inc, f;
2451
2452
        /* We want to change sx by ex in ih steps. So each step, we add
2453
         * ex/ih to sx. That's x_inc + n_inc/ih.
2454
         */
2455
521k
        x_inc = ex/ih;
2456
521k
        n_inc = ex-(x_inc*ih);
2457
521k
        f     = ih>>1;
2458
521k
        delta = ih;
2459
7.29M
        do {
2460
7.29M
            int count;
2461
7.29M
            iy++;
2462
7.29M
            sx += x_inc;
2463
7.29M
            f  -= n_inc;
2464
7.29M
            if (f < 0) {
2465
1.12M
                f += ih;
2466
1.12M
                sx++;
2467
1.12M
            }
2468
7.29M
            assert(iy >= 0 && iy < height);
2469
7.29M
            row = &table[index[iy]];
2470
7.29M
            count = *row = (*row)+1; /* Increment the count */
2471
7.29M
            row[count * 2 - 1] = sx;
2472
7.29M
            row[count * 2    ] = id;
2473
7.29M
        }
2474
7.29M
        while (--delta);
2475
521k
    } else {
2476
198k
        int x_dec, n_dec, f;
2477
2478
198k
        ex = -ex;
2479
        /* We want to change sx by ex in ih steps. So each step, we subtract
2480
         * ex/ih from sx. That's x_dec + n_dec/ih.
2481
         */
2482
198k
        x_dec = ex/ih;
2483
198k
        n_dec = ex-(x_dec*ih);
2484
198k
        f     = ih>>1;
2485
198k
        delta = ih;
2486
3.04M
        do {
2487
3.04M
            int count;
2488
3.04M
            iy++;
2489
3.04M
            sx -= x_dec;
2490
3.04M
            f  -= n_dec;
2491
3.04M
            if (f < 0) {
2492
1.27M
                f += ih;
2493
1.27M
                sx--;
2494
1.27M
            }
2495
3.04M
            assert(iy >= 0 && iy < height);
2496
3.04M
            row = &table[index[iy]];
2497
3.04M
            count = *row = (*row)+1; /* Increment the count */
2498
3.04M
            row[count * 2 - 1] = sx;
2499
3.04M
            row[count * 2    ] = id;
2500
3.04M
         }
2501
3.04M
         while (--delta);
2502
198k
    }
2503
720k
}
2504
2505
static void mark_curve_tr(fixed sx, fixed sy, fixed c1x, fixed c1y, fixed c2x, fixed c2y, fixed ex, fixed ey, fixed base_y, fixed height, int *table, int *index, int *id, int depth)
2506
0
{
2507
0
    fixed ax = (sx + c1x)>>1;
2508
0
    fixed ay = (sy + c1y)>>1;
2509
0
    fixed bx = (c1x + c2x)>>1;
2510
0
    fixed by = (c1y + c2y)>>1;
2511
0
    fixed cx = (c2x + ex)>>1;
2512
0
    fixed cy = (c2y + ey)>>1;
2513
0
    fixed dx = (ax + bx)>>1;
2514
0
    fixed dy = (ay + by)>>1;
2515
0
    fixed fx = (bx + cx)>>1;
2516
0
    fixed fy = (by + cy)>>1;
2517
0
    fixed gx = (dx + fx)>>1;
2518
0
    fixed gy = (dy + fy)>>1;
2519
2520
0
    assert(depth >= 0);
2521
0
    if (depth == 0) {
2522
0
        *id += 1;
2523
0
        mark_line_tr(sx, sy, ex, ey, base_y, height, table, index, *id);
2524
0
    } else {
2525
0
        depth--;
2526
0
        mark_curve_tr(sx, sy, ax, ay, dx, dy, gx, gy, base_y, height, table, index, id, depth);
2527
0
        mark_curve_tr(gx, gy, fx, fy, cx, cy, ex, ey, base_y, height, table, index, id, depth);
2528
0
    }
2529
0
}
2530
2531
static void mark_curve_big_tr(fixed64 sx, fixed64 sy, fixed64 c1x, fixed64 c1y, fixed64 c2x, fixed64 c2y, fixed64 ex, fixed64 ey, fixed base_y, fixed height, int *table, int *index, int *id, int depth)
2532
0
{
2533
0
    fixed64 ax = (sx + c1x)>>1;
2534
0
    fixed64 ay = (sy + c1y)>>1;
2535
0
    fixed64 bx = (c1x + c2x)>>1;
2536
0
    fixed64 by = (c1y + c2y)>>1;
2537
0
    fixed64 cx = (c2x + ex)>>1;
2538
0
    fixed64 cy = (c2y + ey)>>1;
2539
0
    fixed64 dx = (ax + bx)>>1;
2540
0
    fixed64 dy = (ay + by)>>1;
2541
0
    fixed64 fx = (bx + cx)>>1;
2542
0
    fixed64 fy = (by + cy)>>1;
2543
0
    fixed64 gx = (dx + fx)>>1;
2544
0
    fixed64 gy = (dy + fy)>>1;
2545
2546
0
    assert(depth >= 0);
2547
0
    if (depth == 0) {
2548
0
        *id += 1;
2549
0
        mark_line_tr((fixed)sx, (fixed)sy, (fixed)ex, (fixed)ey, base_y, height, table, index, *id);
2550
0
    } else {
2551
0
        depth--;
2552
0
        mark_curve_big_tr(sx, sy, ax, ay, dx, dy, gx, gy, base_y, height, table, index, id, depth);
2553
0
        mark_curve_big_tr(gx, gy, fx, fy, cx, cy, ex, ey, base_y, height, table, index, id, depth);
2554
0
    }
2555
0
}
2556
2557
static void mark_curve_top_tr(fixed sx, fixed sy, fixed c1x, fixed c1y, fixed c2x, fixed c2y, fixed ex, fixed ey, fixed base_y, fixed height, int *table, int *index, int *id, int depth)
2558
0
{
2559
0
    fixed test = (sx^(sx<<1))|(sy^(sy<<1))|(c1x^(c1x<<1))|(c1y^(c1y<<1))|(c2x^(c2x<<1))|(c2y^(c2y<<1))|(ex^(ex<<1))|(ey^(ey<<1));
2560
2561
0
    if (test < 0)
2562
0
        mark_curve_big_tr(sx, sy, c1x, c1y, c2x, c2y, ex, ey, base_y, height, table, index, id, depth);
2563
0
    else
2564
0
        mark_curve_tr(sx, sy, c1x, c1y, c2x, c2y, ex, ey, base_y, height, table, index, id, depth);
2565
0
}
2566
2567
static int make_table_tr(gx_device     * pdev,
2568
                         gx_path       * path,
2569
                         gs_fixed_rect * ibox,
2570
                         int           * scanlines,
2571
                         int          ** index,
2572
                         int          ** table)
2573
193k
{
2574
193k
    return make_table_template(pdev, path, ibox, 2, 1, scanlines, index, table);
2575
193k
}
2576
2577
static void
2578
fill_zero_tr(int *row, const fixed *x)
2579
0
{
2580
0
    int n = *row = (*row)+2; /* Increment the count */
2581
0
    row[2*n-3] = x[0];
2582
0
    row[2*n-2] = 0;
2583
0
    row[2*n-1] = x[1];
2584
0
    row[2*n  ] = 1;
2585
0
}
2586
2587
int gx_scan_convert_tr(gx_device     * gs_restrict pdev,
2588
                       gx_path       * gs_restrict path,
2589
                 const gs_fixed_rect * gs_restrict clip,
2590
                       gx_edgebuffer * gs_restrict edgebuffer,
2591
                       fixed                    fixed_flat)
2592
377k
{
2593
377k
    gs_fixed_rect  ibox;
2594
377k
    gs_fixed_rect  bbox;
2595
377k
    int            scanlines;
2596
377k
    const subpath *psub;
2597
377k
    int           *index;
2598
377k
    int           *table;
2599
377k
    int            i;
2600
377k
    int            code;
2601
377k
    int            id = 0;
2602
377k
    int            zero;
2603
2604
377k
    edgebuffer->index = NULL;
2605
377k
    edgebuffer->table = NULL;
2606
2607
    /* Bale out if no actual path. We see this with the clist */
2608
377k
    if (path->first_subpath == NULL)
2609
179k
        return 0;
2610
2611
197k
    zero = make_bbox(path, clip, &bbox, &ibox, fixed_half);
2612
197k
    if (zero < 0)
2613
0
        return zero;
2614
2615
197k
    if (ibox.q.y <= ibox.p.y)
2616
4.55k
        return 0;
2617
2618
193k
    code = make_table_tr(pdev, path, &ibox, &scanlines, &index, &table);
2619
193k
    if (code != 0) /* > 0 means "retry with smaller height" */
2620
3
        return code;
2621
2622
193k
    if (scanlines == 0)
2623
0
        return 0;
2624
2625
193k
    if (zero) {
2626
0
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero_tr);
2627
193k
    } else {
2628
2629
    /* Step 3: Now we run through the path, filling in the real
2630
     * values. */
2631
425k
    for (psub = path->first_subpath; psub != 0;) {
2632
232k
        const segment *pseg = (const segment *)psub;
2633
232k
        fixed ex = pseg->pt.x;
2634
232k
        fixed ey = pseg->pt.y;
2635
232k
        fixed ix = ex;
2636
232k
        fixed iy = ey;
2637
2638
28.2M
        while ((pseg = pseg->next) != 0 &&
2639
28.0M
               pseg->type != s_start
2640
28.0M
            ) {
2641
28.0M
            fixed sx = ex;
2642
28.0M
            fixed sy = ey;
2643
28.0M
            ex = pseg->pt.x;
2644
28.0M
            ey = pseg->pt.y;
2645
2646
28.0M
            switch (pseg->type) {
2647
0
                default:
2648
0
                case s_start: /* Should never happen */
2649
0
                case s_dash:  /* We should never be seeing a dash here */
2650
0
                    assert("This should never happen" == NULL);
2651
0
                    break;
2652
0
                case s_curve: {
2653
0
                    const curve_segment *const pcur = (const curve_segment *)pseg;
2654
0
                    int k = gx_curve_log2_samples(sx, sy, pcur, fixed_flat);
2655
2656
0
                    mark_curve_top_tr(sx, sy, pcur->p1.x, pcur->p1.y, pcur->p2.x, pcur->p2.y, ex, ey, ibox.p.y, scanlines, table, index, &id, k);
2657
0
                    break;
2658
0
                }
2659
0
                case s_gap:
2660
27.9M
                case s_line:
2661
28.0M
                case s_line_close:
2662
28.0M
                    if (sy != ey)
2663
27.2M
                        mark_line_tr(sx, sy, ex, ey, ibox.p.y, scanlines, table, index, ++id);
2664
28.0M
                    break;
2665
28.0M
            }
2666
28.0M
        }
2667
        /* And close any open segments */
2668
232k
        if (iy != ey)
2669
55.0k
            mark_line_tr(ex, ey, ix, iy, ibox.p.y, scanlines, table, index, ++id);
2670
232k
        psub = (const subpath *)pseg;
2671
232k
    }
2672
193k
    }
2673
2674
    /*if (zero) {
2675
        if (table[0] == 0) { */
2676
            /* Zero height rectangle fills a span */
2677
/*          table[0] = 2;
2678
            table[1] = int2fixed(fixed2int(bbox.p.x + fixed_half));
2679
            table[2] = 0;
2680
            table[3] = int2fixed(fixed2int(bbox.q.x + fixed_half));
2681
            table[4] = 1;
2682
        }
2683
    }*/
2684
2685
    /* Step 2 complete: We now have a complete list of intersection data in
2686
     * table, indexed by index. */
2687
2688
193k
    edgebuffer->base   = ibox.p.y;
2689
193k
    edgebuffer->height = scanlines;
2690
193k
    edgebuffer->xmin   = ibox.p.x;
2691
193k
    edgebuffer->xmax   = ibox.q.x;
2692
193k
    edgebuffer->index  = index;
2693
193k
    edgebuffer->table  = table;
2694
2695
#ifdef DEBUG_SCAN_CONVERTER
2696
    if (debugging_scan_converter) {
2697
        dlprintf("Before sorting:\n");
2698
        gx_edgebuffer_print_tr(edgebuffer);
2699
    }
2700
#endif
2701
2702
    /* Step 4: Sort the intersects on x */
2703
4.28M
    for (i=0; i < scanlines; i++) {
2704
4.08M
        int *row = &table[index[i]];
2705
4.08M
        int  rowlen = *row++;
2706
2707
        /* Bubblesort short runs, qsort longer ones. */
2708
        /* FIXME: Verify the figure 6 below */
2709
4.08M
        if (rowlen <= 6) {
2710
4.03M
            int j, k;
2711
10.8M
            for (j = 0; j < rowlen-1; j++) {
2712
6.80M
                int * gs_restrict t = &row[j<<1];
2713
18.6M
                for (k = j+1; k < rowlen; k++) {
2714
11.8M
                    int * gs_restrict s = &row[k<<1];
2715
11.8M
                    int tmp;
2716
11.8M
                    if (t[0] < s[0])
2717
7.49M
                        continue;
2718
4.32M
                    if (t[0] == s[0]) {
2719
127k
                        if (t[1] <= s[1])
2720
127k
                            continue;
2721
127k
                    } else
2722
4.19M
                        tmp = t[0], t[0] = s[0], s[0] = tmp;
2723
4.19M
                    tmp = t[1], t[1] = s[1], s[1] = tmp;
2724
4.19M
                }
2725
6.80M
            }
2726
4.03M
        } else
2727
52.0k
            qsort(row, rowlen, 2*sizeof(int), intcmp_tr);
2728
4.08M
    }
2729
2730
193k
    return 0;
2731
193k
}
2732
2733
/* Step 5: Filter the intersections according to the rules */
2734
int
2735
gx_filter_edgebuffer_tr(gx_device       * gs_restrict pdev,
2736
                        gx_edgebuffer   * gs_restrict edgebuffer,
2737
                        int                           rule)
2738
377k
{
2739
377k
    int i;
2740
2741
#ifdef DEBUG_SCAN_CONVERTER
2742
    if (debugging_scan_converter) {
2743
        dlprintf("Before filtering\n");
2744
        gx_edgebuffer_print_tr(edgebuffer);
2745
    }
2746
#endif
2747
2748
4.46M
    for (i=0; i < edgebuffer->height; i++) {
2749
4.08M
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
2750
4.08M
        int  rowlen   = *row++;
2751
4.08M
        int *rowstart = row;
2752
4.08M
        int *rowout   = row;
2753
2754
9.98M
        while (rowlen > 0) {
2755
5.89M
            int left, lid, right, rid;
2756
2757
5.89M
            if (rule == gx_rule_even_odd) {
2758
                /* Even Odd */
2759
6.87k
                left  = *row++;
2760
6.87k
                lid   = *row++;
2761
6.87k
                right = *row++;
2762
6.87k
                rid   = *row++;
2763
6.87k
                rowlen -= 2;
2764
5.88M
            } else {
2765
                /* Non-Zero */
2766
5.88M
                int w;
2767
2768
5.88M
                left = *row++;
2769
5.88M
                lid  = *row++;
2770
5.88M
                w = ((lid&1)-1) | 1;
2771
5.88M
                rowlen--;
2772
5.91M
                do {
2773
5.91M
                    right = *row++;
2774
5.91M
                    rid   = *row++;
2775
5.91M
                    rowlen--;
2776
5.91M
                    w += ((rid&1)-1) | 1;
2777
5.91M
                } while (w != 0);
2778
5.88M
            }
2779
2780
5.89M
            if (right > left) {
2781
5.76M
                *rowout++ = left;
2782
5.76M
                *rowout++ = lid;
2783
5.76M
                *rowout++ = right;
2784
5.76M
                *rowout++ = rid;
2785
5.76M
            }
2786
5.89M
        }
2787
4.08M
        rowstart[-1] = (rowout-rowstart)>>1;
2788
4.08M
    }
2789
377k
    return 0;
2790
377k
}
2791
2792
/* Step 6: Fill the edgebuffer */
2793
int
2794
gx_fill_edgebuffer_tr(gx_device       * gs_restrict pdev,
2795
                const gx_device_color * gs_restrict pdevc,
2796
                      gx_edgebuffer   * gs_restrict edgebuffer,
2797
                      int                        log_op)
2798
377k
{
2799
377k
    int i, j, code;
2800
377k
    int mfb = pdev->max_fill_band;
2801
2802
#ifdef DEBUG_SCAN_CONVERTER
2803
    if (debugging_scan_converter) {
2804
        dlprintf("Before filling\n");
2805
        gx_edgebuffer_print_tr(edgebuffer);
2806
    }
2807
#endif
2808
2809
995k
    for (i=0; i < edgebuffer->height; ) {
2810
618k
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
2811
618k
        int  rowlen = *row++;
2812
618k
        int *row2;
2813
618k
        int *rowptr;
2814
618k
        int *row2ptr;
2815
618k
        int y_band_max;
2816
2817
618k
        if (mfb) {
2818
0
            y_band_max = (i & ~(mfb-1)) + mfb;
2819
0
            if (y_band_max > edgebuffer->height)
2820
0
                y_band_max = edgebuffer->height;
2821
618k
        } else {
2822
618k
            y_band_max = edgebuffer->height;
2823
618k
        }
2824
2825
        /* See how many scanlines match i */
2826
4.08M
        for (j = i+1; j < y_band_max; j++) {
2827
3.89M
            int row2len;
2828
2829
3.89M
            row2    = &edgebuffer->table[edgebuffer->index[j]];
2830
3.89M
            row2len = *row2++;
2831
3.89M
            row2ptr = row2;
2832
3.89M
            rowptr  = row;
2833
2834
3.89M
            if (rowlen != row2len)
2835
60.4k
                break;
2836
13.4M
            while (row2len > 0) {
2837
9.95M
                if ((rowptr[1]&~1) != (row2ptr[1]&~1))
2838
365k
                    goto rowdifferent;
2839
9.58M
                rowptr  += 2;
2840
9.58M
                row2ptr += 2;
2841
9.58M
                row2len--;
2842
9.58M
            }
2843
3.83M
        }
2844
618k
rowdifferent:{}
2845
2846
        /* So j is the first scanline that doesn't match i */
2847
2848
618k
        if (j == i+1) {
2849
1.00M
            while (rowlen > 0) {
2850
670k
                int left, right;
2851
2852
670k
                left  = row[0];
2853
670k
                right = row[2];
2854
670k
                row += 4;
2855
670k
                rowlen -= 2;
2856
2857
670k
                left  = fixed2int(left + fixed_half);
2858
670k
                right = fixed2int(right + fixed_half);
2859
670k
                right -= left;
2860
670k
                if (right > 0) {
2861
#ifdef DEBUG_OUTPUT_SC_AS_PS
2862
                    dlprintf("0.001 setlinewidth 1 0 1 setrgbcolor %% purple %%PS\n");
2863
                    coord("moveto", int2fixed(left), int2fixed(edgebuffer->base+i));
2864
                    coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+i));
2865
                    coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+i+1));
2866
                    coord("lineto", int2fixed(left), int2fixed(edgebuffer->base+i+1));
2867
                    dlprintf("closepath stroke %%PS\n");
2868
#endif
2869
669k
                    if (log_op < 0)
2870
0
                        code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
2871
669k
                    else
2872
669k
                        code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
2873
669k
                    if (code < 0)
2874
0
                        return code;
2875
669k
                }
2876
670k
            }
2877
335k
        } else {
2878
282k
            gs_fixed_edge le;
2879
282k
            gs_fixed_edge re;
2880
2881
#ifdef DEBUG_OUTPUT_SC_AS_PS
2882
#ifdef DEBUG_OUTPUT_SC_AS_PS_TRAPS_AS_RECTS
2883
            int k;
2884
            for (k = i; k < j; k++)
2885
            {
2886
                int row2len;
2887
                int left, right;
2888
                row2    = &edgebuffer->table[edgebuffer->index[k]];
2889
                row2len = *row2++;
2890
                while (row2len > 0) {
2891
                    left = row2[0];
2892
                    right = row2[2];
2893
                    row2 += 4;
2894
                    row2len -= 2;
2895
2896
                    left  = fixed2int(left + fixed_half);
2897
                    right = fixed2int(right + fixed_half);
2898
                    right -= left;
2899
                    if (right > 0) {
2900
                        dlprintf("0.001 setlinewidth 1 0 0.5 setrgbcolor %%PS\n");
2901
                        coord("moveto", int2fixed(left), int2fixed(edgebuffer->base+k));
2902
                        coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+k));
2903
                        coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+k+1));
2904
                        coord("lineto", int2fixed(left), int2fixed(edgebuffer->base+k+1));
2905
                        dlprintf("closepath stroke %%PS\n");
2906
                    }
2907
                }
2908
            }
2909
#endif
2910
#endif
2911
2912
282k
            le.start.y = re.start.y = int2fixed(edgebuffer->base+i) + fixed_half;
2913
282k
            le.end.y   = re.end.y   = int2fixed(edgebuffer->base+j) - (fixed_half-1);
2914
282k
            row2    = &edgebuffer->table[edgebuffer->index[j-1]+1];
2915
656k
            while (rowlen > 0) {
2916
373k
                le.start.x = row[0];
2917
373k
                re.start.x = row[2];
2918
373k
                le.end.x   = row2[0];
2919
373k
                re.end.x   = row2[2];
2920
373k
                row += 4;
2921
373k
                row2 += 4;
2922
373k
                rowlen -= 2;
2923
2924
373k
                assert(re.start.x >= le.start.x);
2925
373k
                assert(re.end.x >= le.end.x);
2926
2927
#ifdef DEBUG_OUTPUT_SC_AS_PS
2928
                dlprintf("0.001 setlinewidth 0 1 1 setrgbcolor %%cyan %%PS\n");
2929
                coord("moveto", le.start.x, le.start.y);
2930
                coord("lineto", le.end.x, le.end.y);
2931
                coord("lineto", re.end.x, re.end.y);
2932
                coord("lineto", re.start.x, re.start.y);
2933
                dlprintf("closepath stroke %%PS\n");
2934
#endif
2935
373k
                code = dev_proc(pdev, fill_trapezoid)(
2936
373k
                                pdev,
2937
373k
                                &le,
2938
373k
                                &re,
2939
373k
                                le.start.y,
2940
373k
                                le.end.y,
2941
373k
                                0, /* bool swap_axes */
2942
373k
                                pdevc, /*const gx_drawing_color *pdcolor */
2943
373k
                                log_op);
2944
373k
                if (code < 0)
2945
0
                    return code;
2946
373k
            }
2947
282k
        }
2948
2949
618k
        i = j;
2950
618k
    }
2951
377k
    return 0;
2952
377k
}
2953
2954
/* Any part of a pixel trapezoid routines */
2955
2956
static int edgecmp_tr(const void *a, const void *b)
2957
493M
{
2958
493M
    int left  = ((int*)a)[0];
2959
493M
    int right = ((int*)b)[0];
2960
493M
    if (left != right)
2961
483M
        return left - right;
2962
10.6M
    left = ((int*)a)[2] - ((int*)b)[2];
2963
10.6M
    if (left != 0)
2964
1.33M
        return left;
2965
9.29M
    left = ((int*)a)[1] - ((int*)b)[1];
2966
9.29M
    if (left != 0)
2967
9.29M
        return left;
2968
3.04k
    return ((int*)a)[3] - ((int*)b)[3];
2969
9.29M
}
2970
2971
#ifdef DEBUG_SCAN_CONVERTER
2972
static void
2973
gx_edgebuffer_print_filtered_tr_app(gx_edgebuffer * edgebuffer)
2974
{
2975
    int i;
2976
2977
    if (!debugging_scan_converter)
2978
        return;
2979
2980
    dlprintf1("Edgebuffer %x\n", edgebuffer);
2981
    dlprintf4("xmin=%x xmax=%x base=%x height=%x\n",
2982
              edgebuffer->xmin, edgebuffer->xmax, edgebuffer->base, edgebuffer->height);
2983
    for (i=0; i < edgebuffer->height; i++)
2984
    {
2985
        int  offset = edgebuffer->index[i];
2986
        int *row    = &edgebuffer->table[offset];
2987
        int count   = *row++;
2988
        dlprintf3("%x @ %d: %d =", i, offset, count);
2989
        while (count-- > 0) {
2990
            int left  = *row++;
2991
            int lid   = *row++;
2992
            int right = *row++;
2993
            int rid   = *row++;
2994
            dlprintf4(" (%x:%d,%x:%d)", left, lid, right, rid);
2995
        }
2996
        dlprintf("\n");
2997
    }
2998
}
2999
3000
static void
3001
gx_edgebuffer_print_tr_app(gx_edgebuffer * edgebuffer)
3002
{
3003
    int i;
3004
    int borked = 0;
3005
3006
    if (!debugging_scan_converter)
3007
        return;
3008
3009
    dlprintf1("Edgebuffer %x\n", edgebuffer);
3010
    dlprintf4("xmin=%x xmax=%x base=%x height=%x\n",
3011
              edgebuffer->xmin, edgebuffer->xmax, edgebuffer->base, edgebuffer->height);
3012
    for (i=0; i < edgebuffer->height; i++)
3013
    {
3014
        int  offset = edgebuffer->index[i];
3015
        int *row    = &edgebuffer->table[offset];
3016
        int count   = *row++;
3017
        int c       = count;
3018
        int wind    = 0;
3019
        dlprintf3("%x @ %d: %d =", i, offset, count);
3020
        while (count-- > 0) {
3021
            int left  = *row++;
3022
            int lid   = *row++;
3023
            int right = *row++;
3024
            int rid   = *row++;
3025
            int ww    = lid & 1;
3026
            int w     = -ww | 1;
3027
            lid >>= 1;
3028
            wind += w;
3029
            dlprintf5(" (%x:%d,%x:%d)%c", left, lid, right, rid, ww ? 'v' : '^');
3030
        }
3031
        if (wind != 0 || c & 1) {
3032
            dlprintf(" <- BROKEN");
3033
            borked = 1;
3034
        }
3035
        dlprintf("\n");
3036
    }
3037
    if (borked) {
3038
        borked = borked; /* Breakpoint here */
3039
    }
3040
}
3041
#endif
3042
3043
typedef struct
3044
{
3045
    fixed  left;
3046
    int    lid;
3047
    fixed  right;
3048
    int    rid;
3049
    fixed  y;
3050
    signed char  d; /* 0 up (or horiz), 1 down, -1 uninited */
3051
    unsigned char first;
3052
    unsigned char saved;
3053
    fixed  save_left;
3054
    int    save_lid;
3055
    fixed  save_right;
3056
    int    save_rid;
3057
    int    save_iy;
3058
    int    save_d;
3059
3060
    int    scanlines;
3061
    int   *table;
3062
    int   *index;
3063
    int    base;
3064
} cursor_tr;
3065
3066
static inline void
3067
cursor_output_tr(cursor_tr * gs_restrict cr, int iy)
3068
729M
{
3069
729M
    int *row;
3070
729M
    int count;
3071
3072
729M
    if (iy >= 0 && iy < cr->scanlines) {
3073
719M
        if (cr->first) {
3074
            /* Save it for later in case we join up */
3075
15.7M
            cr->save_left  = cr->left;
3076
15.7M
            cr->save_lid   = cr->lid;
3077
15.7M
            cr->save_right = cr->right;
3078
15.7M
            cr->save_rid   = cr->rid;
3079
15.7M
            cr->save_iy    = iy;
3080
15.7M
            cr->save_d     = cr->d;
3081
15.7M
            cr->saved      = 1;
3082
704M
        } else if (cr->d != DIRN_UNSET) {
3083
            /* Enter it into the table */
3084
703M
            row = &cr->table[cr->index[iy]];
3085
703M
            *row = count = (*row)+1; /* Increment the count */
3086
703M
            row[4 * count - 3] = cr->left;
3087
703M
            row[4 * count - 2] = cr->d | (cr->lid<<1);
3088
703M
            row[4 * count - 1] = cr->right;
3089
703M
            row[4 * count    ] = cr->rid;
3090
703M
        } else {
3091
152k
            assert(cr->left == max_fixed && cr->right == min_fixed);
3092
152k
        }
3093
719M
    }
3094
729M
    cr->first = 0;
3095
729M
}
3096
3097
static inline void
3098
cursor_output_inrange_tr(cursor_tr * gs_restrict cr, int iy)
3099
383M
{
3100
383M
    int *row;
3101
383M
    int count;
3102
3103
383M
    assert(iy >= 0 && iy < cr->scanlines);
3104
383M
    if (cr->first) {
3105
        /* Save it for later in case we join up */
3106
391k
        cr->save_left  = cr->left;
3107
391k
        cr->save_lid   = cr->lid;
3108
391k
        cr->save_right = cr->right;
3109
391k
        cr->save_rid   = cr->rid;
3110
391k
        cr->save_iy    = iy;
3111
391k
        cr->save_d     = cr->d;
3112
391k
        cr->saved      = 1;
3113
382M
    } else {
3114
        /* Enter it into the table */
3115
382M
        assert(cr->d != DIRN_UNSET);
3116
3117
382M
        row = &cr->table[cr->index[iy]];
3118
382M
        *row = count = (*row)+1; /* Increment the count */
3119
382M
        row[4 * count - 3] = cr->left;
3120
382M
        row[4 * count - 2] = cr->d | (cr->lid<<1);
3121
382M
        row[4 * count - 1] = cr->right;
3122
382M
        row[4 * count    ] = cr->rid;
3123
382M
    }
3124
383M
    cr->first = 0;
3125
383M
}
3126
3127
/* Step the cursor in y, allowing for maybe crossing a scanline */
3128
static inline void
3129
cursor_step_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id, int skip)
3130
82.2M
{
3131
82.2M
    int new_iy;
3132
82.2M
    int iy = fixed2int(cr->y) - cr->base;
3133
3134
82.2M
    cr->y += dy;
3135
82.2M
    new_iy = fixed2int(cr->y) - cr->base;
3136
82.2M
    if (new_iy != iy) {
3137
82.2M
        if (!skip)
3138
81.2M
            cursor_output_tr(cr, iy);
3139
82.2M
        cr->left = x;
3140
82.2M
        cr->lid = id;
3141
82.2M
        cr->right = x;
3142
82.2M
        cr->rid = id;
3143
82.2M
    } else {
3144
0
        if (x < cr->left) {
3145
0
            cr->left = x;
3146
0
            cr->lid = id;
3147
0
        }
3148
0
        if (x > cr->right) {
3149
0
            cr->right = x;
3150
0
            cr->rid = id;
3151
0
        }
3152
0
    }
3153
82.2M
}
3154
3155
/* Step the cursor in y, never by enough to cross a scanline. */
3156
static inline void
3157
cursor_never_step_vertical_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3158
5.28M
{
3159
5.28M
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
3160
3161
5.28M
    cr->y += dy;
3162
5.28M
}
3163
3164
/* Step the cursor in y, never by enough to cross a scanline,
3165
 * knowing that we are moving left, and that the right edge
3166
 * has already been accounted for. */
3167
static inline void
3168
cursor_never_step_left_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3169
18.6M
{
3170
18.6M
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
3171
3172
18.6M
    if (x < cr->left)
3173
14.7M
    {
3174
14.7M
        cr->left = x;
3175
14.7M
        cr->lid = id;
3176
14.7M
    }
3177
18.6M
    cr->y += dy;
3178
18.6M
}
3179
3180
/* Step the cursor in y, never by enough to cross a scanline,
3181
 * knowing that we are moving right, and that the left edge
3182
 * has already been accounted for. */
3183
static inline void
3184
cursor_never_step_right_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3185
16.8M
{
3186
16.8M
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
3187
3188
16.8M
    if (x > cr->right)
3189
16.3M
    {
3190
16.3M
        cr->right = x;
3191
16.3M
        cr->rid = id;
3192
16.3M
    }
3193
16.8M
    cr->y += dy;
3194
16.8M
}
3195
3196
/* Step the cursor in y, always by enough to cross a scanline. */
3197
static inline void
3198
cursor_always_step_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id, int skip)
3199
62.3M
{
3200
62.3M
    int iy = fixed2int(cr->y) - cr->base;
3201
3202
62.3M
    if (!skip)
3203
56.2M
        cursor_output_tr(cr, iy);
3204
62.3M
    cr->y += dy;
3205
62.3M
    cr->left = x;
3206
62.3M
    cr->lid = id;
3207
62.3M
    cr->right = x;
3208
62.3M
    cr->rid = id;
3209
62.3M
}
3210
3211
/* Step the cursor in y, always by enough to cross a scanline, as
3212
 * part of a vertical line, knowing that we are moving from a
3213
 * position guaranteed to be in the valid y range. */
3214
static inline void
3215
cursor_always_step_inrange_vertical_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3216
535M
{
3217
535M
    int iy = fixed2int(cr->y) - cr->base;
3218
3219
535M
    cursor_output_tr(cr, iy);
3220
535M
    cr->y += dy;
3221
535M
}
3222
3223
/* Step the cursor in y, always by enough to cross a scanline, as
3224
 * part of a left moving line, knowing that we are moving from a
3225
 * position guaranteed to be in the valid y range. */
3226
static inline void
3227
cursor_always_inrange_step_left_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3228
192M
{
3229
192M
    int iy = fixed2int(cr->y) - cr->base;
3230
3231
192M
    cr->y += dy;
3232
192M
    cursor_output_inrange_tr(cr, iy);
3233
192M
    cr->right = x;
3234
192M
    cr->rid = id;
3235
192M
}
3236
3237
/* Step the cursor in y, always by enough to cross a scanline, as
3238
 * part of a right moving line, knowing that we are moving from a
3239
 * position guaranteed to be in the valid y range. */
3240
static inline void
3241
cursor_always_inrange_step_right_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3242
190M
{
3243
190M
    int iy = fixed2int(cr->y) - cr->base;
3244
3245
190M
    cr->y += dy;
3246
190M
    cursor_output_inrange_tr(cr, iy);
3247
190M
    cr->left = x;
3248
190M
    cr->lid = id;
3249
190M
}
3250
3251
static inline void cursor_left_merge_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3252
221M
{
3253
221M
    if (x < cr->left) {
3254
71.2M
        cr->left = x;
3255
71.2M
        cr->lid  = id;
3256
71.2M
    }
3257
221M
}
3258
3259
static inline void cursor_left_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3260
241M
{
3261
241M
    cr->left = x;
3262
241M
    cr->lid  = id;
3263
241M
}
3264
3265
static inline void cursor_right_merge_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3266
228M
{
3267
228M
    if (x > cr->right) {
3268
72.5M
        cr->right = x;
3269
72.5M
        cr->rid   = id;
3270
72.5M
    }
3271
228M
}
3272
3273
static inline void cursor_right_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3274
237M
{
3275
237M
    cr->right = x;
3276
237M
    cr->rid   = id;
3277
237M
}
3278
3279
static inline int cursor_down_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3280
72.2M
{
3281
72.2M
    int skip = 0;
3282
72.2M
    if ((cr->y & 0xff) == 0)
3283
7.04M
        skip = 1;
3284
72.2M
    if (cr->d == DIRN_UP)
3285
10.9M
    {
3286
10.9M
        if (!skip)
3287
10.9M
            cursor_output_tr(cr, fixed2int(cr->y) - cr->base);
3288
10.9M
        cr->left = x;
3289
10.9M
        cr->lid = id;
3290
10.9M
        cr->right = x;
3291
10.9M
        cr->rid = id;
3292
10.9M
    }
3293
72.2M
    cr->d = DIRN_DOWN;
3294
72.2M
    return skip;
3295
72.2M
}
3296
3297
static inline void cursor_up_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3298
71.2M
{
3299
71.2M
    if (cr->d == DIRN_DOWN)
3300
10.7M
    {
3301
10.7M
        cursor_output_tr(cr, fixed2int(cr->y) - cr->base);
3302
10.7M
        cr->left = x;
3303
10.7M
        cr->lid = id;
3304
10.7M
        cr->right = x;
3305
10.7M
        cr->rid = id;
3306
10.7M
    }
3307
71.2M
    cr->d = DIRN_UP;
3308
71.2M
}
3309
3310
static inline void
3311
cursor_flush_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3312
23.4M
{
3313
23.4M
    int iy;
3314
3315
    /* This should only happen if we were entirely out of bounds,
3316
     * or if everything was within a zero height horizontal
3317
     * rectangle from the start point. */
3318
23.4M
    if (cr->first) {
3319
6.10k
        int iy = fixed2int(cr->y) - cr->base;
3320
        /* Any zero height rectangle counts as filled, except
3321
         * those on the baseline of a pixel. */
3322
6.10k
        if (cr->d == DIRN_UNSET && (cr->y & 0xff) == 0)
3323
281
            return;
3324
6.10k
        assert(cr->left != max_fixed && cr->right != min_fixed);
3325
5.81k
        if (iy >= 0 && iy < cr->scanlines) {
3326
4.68k
            int *row = &cr->table[cr->index[iy]];
3327
4.68k
            int count = *row = (*row)+2; /* Increment the count */
3328
4.68k
            row[4 * count - 7] = cr->left;
3329
4.68k
            row[4 * count - 6] = DIRN_UP | (cr->lid<<1);
3330
4.68k
            row[4 * count - 5] = cr->right;
3331
4.68k
            row[4 * count - 4] = cr->rid;
3332
4.68k
            row[4 * count - 3] = cr->right;
3333
4.68k
            row[4 * count - 2] = DIRN_DOWN | (cr->rid<<1);
3334
4.68k
            row[4 * count - 1] = cr->right;
3335
4.68k
            row[4 * count    ] = cr->rid;
3336
4.68k
        }
3337
5.81k
        return;
3338
6.10k
    }
3339
3340
    /* Merge save into current if we can */
3341
23.4M
    iy = fixed2int(cr->y) - cr->base;
3342
23.4M
    if (cr->saved && iy == cr->save_iy &&
3343
13.3M
        (cr->d == cr->save_d || cr->save_d == DIRN_UNSET)) {
3344
5.79M
        if (cr->left > cr->save_left) {
3345
1.45M
            cr->left = cr->save_left;
3346
1.45M
            cr->lid  = cr->save_lid;
3347
1.45M
        }
3348
5.79M
        if (cr->right < cr->save_right) {
3349
1.30M
            cr->right = cr->save_right;
3350
1.30M
            cr->rid = cr->save_rid;
3351
1.30M
        }
3352
5.79M
        cursor_output_tr(cr, iy);
3353
5.79M
        return;
3354
5.79M
    }
3355
3356
    /* Merge not possible */
3357
17.6M
    cursor_output_tr(cr, iy);
3358
17.6M
    if (cr->saved) {
3359
10.3M
        cr->left  = cr->save_left;
3360
10.3M
        cr->lid   = cr->save_lid;
3361
10.3M
        cr->right = cr->save_right;
3362
10.3M
        cr->rid   = cr->save_rid;
3363
10.3M
        assert(cr->save_d != DIRN_UNSET);
3364
10.3M
        if (cr->save_d != DIRN_UNSET)
3365
10.3M
            cr->d = cr->save_d;
3366
10.3M
        cursor_output_tr(cr, cr->save_iy);
3367
10.3M
    }
3368
17.6M
}
3369
3370
static inline void
3371
cursor_null_tr(cursor_tr *cr)
3372
30.3M
{
3373
30.3M
    cr->right = min_fixed;
3374
30.3M
    cr->left  = max_fixed;
3375
30.3M
    cr->d     = DIRN_UNSET;
3376
30.3M
}
3377
3378
static void mark_line_tr_app(cursor_tr * gs_restrict cr, fixed sx, fixed sy, fixed ex, fixed ey, int id)
3379
980M
{
3380
980M
    int isy, iey;
3381
980M
    fixed saved_sy = sy;
3382
980M
    fixed saved_ex = ex;
3383
980M
    fixed saved_ey = ey;
3384
980M
    int truncated;
3385
3386
980M
    if (sy == ey && sx == ex)
3387
32.4M
        return;
3388
3389
947M
    isy = fixed2int(sy) - cr->base;
3390
947M
    iey = fixed2int(ey) - cr->base;
3391
3392
#ifdef DEBUG_SCAN_CONVERTER
3393
    if (debugging_scan_converter)
3394
        dlprintf6("Marking line (tr_app) from %x,%x to %x,%x (%x,%x)\n", sx, sy, ex, ey, isy, iey);
3395
#endif
3396
#ifdef DEBUG_OUTPUT_SC_AS_PS
3397
    dlprintf("0.001 setlinewidth 0 0 0 setrgbcolor %%PS\n");
3398
    coord("moveto", sx, sy);
3399
    coord("lineto", ex, ey);
3400
    dlprintf("stroke %%PS\n");
3401
#endif
3402
3403
    /* Horizontal motion at the bottom of a pixel is ignored */
3404
947M
    if (sy == ey && (sy & 0xff) == 0)
3405
1.55M
        return;
3406
3407
947M
    assert(cr->y == sy &&
3408
946M
           ((cr->left <= sx && cr->right >= sx) || ((sy & 0xff) == 0)) &&
3409
946M
           cr->d >= DIRN_UNSET && cr->d <= DIRN_DOWN);
3410
3411
946M
    if (isy < iey) {
3412
        /* Rising line */
3413
377M
        if (iey < 0 || isy >= cr->scanlines) {
3414
            /* All line is outside. */
3415
330M
            if ((ey & 0xff) == 0) {
3416
2.09M
                cursor_null_tr(cr);
3417
328M
            } else {
3418
328M
                cr->left = ex;
3419
328M
                cr->lid = id;
3420
328M
                cr->right = ex;
3421
328M
                cr->rid = id;
3422
328M
            }
3423
330M
            cr->y = ey;
3424
330M
            cr->first = 0;
3425
330M
            return;
3426
330M
        }
3427
47.2M
        if (isy < 0) {
3428
            /* Move sy up */
3429
6.45M
            int64_t y = (int64_t)ey - (int64_t)sy;
3430
6.45M
            fixed new_sy = int2fixed(cr->base);
3431
6.45M
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
3432
6.45M
            sx += (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3433
6.45M
            sy = new_sy;
3434
6.45M
            cursor_null_tr(cr);
3435
6.45M
            cr->y = sy;
3436
6.45M
            isy = 0;
3437
6.45M
        }
3438
47.2M
        truncated = iey > cr->scanlines;
3439
47.2M
        if (truncated) {
3440
            /* Move ey down */
3441
5.82M
            int64_t y = (int64_t)ey - (int64_t)sy;
3442
5.82M
            fixed new_ey = int2fixed(cr->base + cr->scanlines);
3443
5.82M
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
3444
5.82M
            saved_ex = ex;
3445
5.82M
            saved_ey = ey;
3446
5.82M
            ex -= (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3447
5.82M
            ey = new_ey;
3448
5.82M
            iey = cr->scanlines;
3449
5.82M
        }
3450
568M
    } else {
3451
        /* Falling line */
3452
568M
        if (isy < 0 || iey >= cr->scanlines) {
3453
            /* All line is outside. */
3454
453M
            if ((ey & 0xff) == 0) {
3455
1.83M
                cursor_null_tr(cr);
3456
452M
            } else {
3457
452M
                cr->left = ex;
3458
452M
                cr->lid = id;
3459
452M
                cr->right = ex;
3460
452M
                cr->rid = id;
3461
452M
            }
3462
453M
            cr->y = ey;
3463
453M
            cr->first = 0;
3464
453M
            return;
3465
453M
        }
3466
114M
        truncated = iey < 0;
3467
114M
        if (truncated) {
3468
            /* Move ey up */
3469
6.45M
            int64_t y = (int64_t)ey - (int64_t)sy;
3470
6.45M
            fixed new_ey = int2fixed(cr->base);
3471
6.45M
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
3472
6.45M
            ex -= (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3473
6.45M
            ey = new_ey;
3474
6.45M
            iey = 0;
3475
6.45M
        }
3476
114M
        if (isy >= cr->scanlines) {
3477
            /* Move sy down */
3478
6.42M
            int64_t y = (int64_t)ey - (int64_t)sy;
3479
6.42M
            fixed new_sy = int2fixed(cr->base + cr->scanlines);
3480
6.42M
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
3481
6.42M
            sx += (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3482
6.42M
            sy = new_sy;
3483
6.42M
            cursor_null_tr(cr);
3484
6.42M
            cr->y = sy;
3485
6.42M
            isy = cr->scanlines;
3486
6.42M
        }
3487
114M
    }
3488
3489
161M
    cursor_left_merge_tr(cr, sx, id);
3490
161M
    cursor_right_merge_tr(cr, sx, id);
3491
3492
161M
    assert(cr->left <= sx);
3493
161M
    assert(cr->right >= sx);
3494
161M
    assert(cr->y == sy);
3495
3496
    /* A note: The code below used to be of the form:
3497
     *   if (isy == iey)   ... deal with horizontal lines
3498
     *   else if (ey > sy) {
3499
     *     fixed y_steps = ey - sy;
3500
     *      ... deal with rising lines ...
3501
     *   } else {
3502
     *     fixed y_steps = ey - sy;
3503
     *     ... deal with falling lines
3504
     *   }
3505
     * but that lead to problems, for instance, an example seen
3506
     * has sx=2aa8e, sy=8aee7, ex=7ffc1686, ey=8003e97a.
3507
     * Thus isy=84f, iey=ff80038a. We can see that ey < sy, but
3508
     * sy - ey < 0!
3509
     * We therefore rejig our code so that the choice between
3510
     * cases is done based on the sign of y_steps rather than
3511
     * the relative size of ey and sy.
3512
     */
3513
3514
    /* First, deal with lines that don't change scanline.
3515
     * This accommodates horizontal lines. */
3516
161M
    if (isy == iey) {
3517
67.2M
        if (saved_sy == saved_ey) {
3518
            /* Horizontal line. Don't change cr->d, don't flush. */
3519
18.2M
            if ((ey & 0xff) == 0) {
3520
0
                cursor_null_tr(cr);
3521
0
                goto no_merge;
3522
0
            }
3523
48.9M
        } else if (saved_sy > saved_ey) {
3524
            /* Falling line, flush if previous was rising */
3525
24.3M
            int skip = cursor_down_tr(cr, sx, id);
3526
24.3M
            if ((ey & 0xff) == 0) {
3527
                /* We are falling to the baseline of a subpixel, so output
3528
                 * for the current pixel, and leave the cursor nulled. */
3529
957k
                if (sx <= ex) {
3530
646k
                    cursor_right_merge_tr(cr, ex, id);
3531
646k
                } else {
3532
311k
                    cursor_left_merge_tr(cr, ex, id);
3533
311k
                }
3534
957k
                if (!skip)
3535
939k
                    cursor_output_tr(cr, fixed2int(cr->y) - cr->base);
3536
957k
                cursor_null_tr(cr);
3537
957k
                goto no_merge;
3538
957k
            }
3539
24.5M
        } else {
3540
            /* Rising line, flush if previous was falling */
3541
24.5M
            cursor_up_tr(cr, sx, id);
3542
24.5M
            if ((ey & 0xff) == 0) {
3543
20.6k
                cursor_null_tr(cr);
3544
20.6k
                goto no_merge;
3545
20.6k
            }
3546
24.5M
        }
3547
66.2M
        if (sx <= ex) {
3548
34.9M
            cursor_right_merge_tr(cr, ex, id);
3549
34.9M
        } else {
3550
31.3M
            cursor_left_merge_tr(cr, ex, id);
3551
31.3M
        }
3552
67.2M
no_merge:
3553
67.2M
        cr->y = ey;
3554
67.2M
        if (sy > saved_ey)
3555
24.3M
            goto endFalling;
3556
94.5M
    } else if (iey > isy) {
3557
        /* So lines increasing in y. */
3558
        /* We want to change from sy to ey, which are guaranteed to be on
3559
         * different scanlines. We do this in 3 phases.
3560
         * Phase 1 gets us from sy to the next scanline boundary. (We may exit after phase 1).
3561
         * Phase 2 gets us all the way to the last scanline boundary. (This may be a null operation)
3562
         * Phase 3 gets us from the last scanline boundary to ey. (We are guaranteed to have output the cursor at least once before phase 3).
3563
         */
3564
46.7M
        int phase1_y_steps = (-sy) & (fixed_1 - 1);
3565
46.7M
        int phase3_y_steps = ey & (fixed_1 - 1);
3566
46.7M
        ufixed y_steps = (ufixed)ey - (ufixed)sy;
3567
3568
46.7M
        cursor_up_tr(cr, sx, id);
3569
3570
46.7M
        if (sx == ex) {
3571
            /* Vertical line. (Rising) */
3572
3573
            /* Phase 1: */
3574
8.51M
            if (phase1_y_steps) {
3575
                /* If phase 1 will move us into a new scanline, then we must
3576
                 * flush it before we move. */
3577
5.42M
                cursor_step_tr(cr, phase1_y_steps, sx, id, 0);
3578
5.42M
                sy += phase1_y_steps;
3579
5.42M
                y_steps -= phase1_y_steps;
3580
5.42M
                if (y_steps == 0) {
3581
309k
                    cursor_null_tr(cr);
3582
309k
                    goto end;
3583
309k
                }
3584
5.42M
            }
3585
3586
            /* Phase 3: precalculation */
3587
8.20M
            y_steps -= phase3_y_steps;
3588
3589
            /* Phase 2: */
3590
8.20M
            y_steps = fixed2int(y_steps);
3591
8.20M
            assert((int)y_steps >= 0);
3592
8.20M
            if (y_steps > 0) {
3593
6.73M
                cursor_always_step_tr(cr, fixed_1, sx, id, 0);
3594
6.73M
                y_steps--;
3595
274M
                while (y_steps) {
3596
267M
                    cursor_always_step_inrange_vertical_tr(cr, fixed_1, sx, id);
3597
267M
                    y_steps--;
3598
267M
                }
3599
6.73M
            }
3600
3601
            /* Phase 3 */
3602
8.20M
            assert(cr->left == sx && cr->right == sx && cr->lid == id && cr->rid == id);
3603
8.20M
            if (phase3_y_steps == 0)
3604
2.96M
                cursor_null_tr(cr);
3605
5.24M
            else
3606
5.24M
                cr->y += phase3_y_steps;
3607
38.1M
        } else if (sx < ex) {
3608
            /* Lines increasing in x. (Rightwards, rising) */
3609
19.7M
            int phase1_x_steps, phase3_x_steps;
3610
            /* Use unsigned int here, to allow for extreme cases like
3611
             * ex = 0x7fffffff, sx = 0x80000000 */
3612
19.7M
            unsigned int x_steps = ex - sx;
3613
3614
            /* Phase 1: */
3615
19.7M
            if (phase1_y_steps) {
3616
18.1M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3617
18.1M
                sx += phase1_x_steps;
3618
18.1M
                cursor_right_merge_tr(cr, sx, id);
3619
18.1M
                x_steps -= phase1_x_steps;
3620
18.1M
                cursor_step_tr(cr, phase1_y_steps, sx, id, 0);
3621
18.1M
                sy += phase1_y_steps;
3622
18.1M
                y_steps -= phase1_y_steps;
3623
18.1M
                if (y_steps == 0) {
3624
246k
                    cursor_null_tr(cr);
3625
246k
                    goto end;
3626
246k
                }
3627
18.1M
            }
3628
3629
            /* Phase 3: precalculation */
3630
19.5M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3631
19.5M
            x_steps -= phase3_x_steps;
3632
19.5M
            y_steps -= phase3_y_steps;
3633
19.5M
            assert((y_steps & (fixed_1 - 1)) == 0);
3634
3635
            /* Phase 2: */
3636
19.5M
            y_steps = fixed2int(y_steps);
3637
19.5M
            assert((int)y_steps >= 0);
3638
19.5M
            if (y_steps) {
3639
                /* We want to change sx by x_steps in y_steps steps.
3640
                 * So each step, we add x_steps/y_steps to sx. That's x_inc + n_inc/y_steps. */
3641
13.0M
                int x_inc = x_steps/y_steps;
3642
13.0M
                int n_inc = x_steps - (x_inc * y_steps);
3643
13.0M
                int f = y_steps/2;
3644
13.0M
                int d = y_steps;
3645
3646
                /* Special casing the first iteration, allows us to simplify
3647
                 * the following loop. */
3648
13.0M
                sx += x_inc;
3649
13.0M
                f -= n_inc;
3650
13.0M
                if (f < 0)
3651
2.75M
                    f += d, sx++;
3652
13.0M
                cursor_right_merge_tr(cr, sx, id);
3653
13.0M
                cursor_always_step_tr(cr, fixed_1, sx, id, 0);
3654
13.0M
                y_steps--;
3655
3656
112M
                while (y_steps) {
3657
99.2M
                    sx += x_inc;
3658
99.2M
                    f -= n_inc;
3659
99.2M
                    if (f < 0)
3660
43.7M
                        f += d, sx++;
3661
99.2M
                    cursor_right_tr(cr, sx, id);
3662
99.2M
                    cursor_always_inrange_step_right_tr(cr, fixed_1, sx, id);
3663
99.2M
                    y_steps--;
3664
99.2M
                };
3665
13.0M
            }
3666
3667
            /* Phase 3 */
3668
19.5M
            assert(cr->left <= ex && cr->lid == id && cr->right >= sx);
3669
19.5M
            if (phase3_y_steps == 0)
3670
1.39M
                cursor_null_tr(cr);
3671
18.1M
            else {
3672
18.1M
                cursor_right_tr(cr, ex, id);
3673
18.1M
                cr->y += phase3_y_steps;
3674
18.1M
            }
3675
19.5M
        } else {
3676
            /* Lines decreasing in x. (Leftwards, rising) */
3677
18.4M
            int phase1_x_steps, phase3_x_steps;
3678
            /* Use unsigned int here, to allow for extreme cases like
3679
             * sx = 0x7fffffff, ex = 0x80000000 */
3680
18.4M
            unsigned int x_steps = sx - ex;
3681
3682
            /* Phase 1: */
3683
18.4M
            if (phase1_y_steps) {
3684
16.9M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3685
16.9M
                x_steps -= phase1_x_steps;
3686
16.9M
                sx -= phase1_x_steps;
3687
16.9M
                cursor_left_merge_tr(cr, sx, id);
3688
16.9M
                cursor_step_tr(cr, phase1_y_steps, sx, id, 0);
3689
16.9M
                sy += phase1_y_steps;
3690
16.9M
                y_steps -= phase1_y_steps;
3691
16.9M
                if (y_steps == 0) {
3692
223k
                    cursor_null_tr(cr);
3693
223k
                    goto end;
3694
223k
                }
3695
16.9M
            }
3696
3697
            /* Phase 3: precalculation */
3698
18.1M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3699
18.1M
            x_steps -= phase3_x_steps;
3700
18.1M
            y_steps -= phase3_y_steps;
3701
18.1M
            assert((y_steps & (fixed_1 - 1)) == 0);
3702
3703
            /* Phase 2: */
3704
18.1M
            y_steps = fixed2int((unsigned int)y_steps);
3705
18.1M
            assert((int)y_steps >= 0);
3706
18.1M
            if (y_steps) {
3707
                /* We want to change sx by x_steps in y_steps steps.
3708
                 * So each step, we sub x_steps/y_steps from sx. That's x_inc + n_inc/ey. */
3709
11.1M
                int x_inc = x_steps/y_steps;
3710
11.1M
                int n_inc = x_steps - (x_inc * y_steps);
3711
11.1M
                int f = y_steps/2;
3712
11.1M
                int d = y_steps;
3713
3714
                /* Special casing the first iteration, allows us to simplify
3715
                 * the following loop. */
3716
11.1M
                sx -= x_inc;
3717
11.1M
                f -= n_inc;
3718
11.1M
                if (f < 0)
3719
2.09M
                    f += d, sx--;
3720
11.1M
                cursor_left_merge_tr(cr, sx, id);
3721
11.1M
                cursor_always_step_tr(cr, fixed_1, sx, id, 0);
3722
11.1M
                y_steps--;
3723
3724
101M
                while (y_steps) {
3725
90.1M
                    sx -= x_inc;
3726
90.1M
                    f -= n_inc;
3727
90.1M
                    if (f < 0)
3728
37.8M
                        f += d, sx--;
3729
90.1M
                    cursor_left_tr(cr, sx, id);
3730
90.1M
                    cursor_always_inrange_step_left_tr(cr, fixed_1, sx, id);
3731
90.1M
                    y_steps--;
3732
90.1M
                }
3733
11.1M
            }
3734
3735
            /* Phase 3 */
3736
18.1M
            assert(cr->right >= ex && cr->rid == id && cr->left <= sx);
3737
18.1M
            if (phase3_y_steps == 0)
3738
1.31M
                cursor_null_tr(cr);
3739
16.8M
            else {
3740
16.8M
                cursor_left_tr(cr, ex, id);
3741
16.8M
                cr->y += phase3_y_steps;
3742
16.8M
            }
3743
18.1M
        }
3744
47.8M
    } else {
3745
        /* So lines decreasing in y. */
3746
        /* We want to change from sy to ey, which are guaranteed to be on
3747
         * different scanlines. We do this in 3 phases.
3748
         * Phase 1 gets us from sy to the next scanline boundary. This never causes an output.
3749
         * Phase 2 gets us all the way to the last scanline boundary. This is guaranteed to cause an output.
3750
         * Phase 3 gets us from the last scanline boundary to ey. We are guaranteed to have outputted by now.
3751
         */
3752
47.8M
        int phase1_y_steps = sy & (fixed_1 - 1);
3753
47.8M
        int phase3_y_steps = (-ey) & (fixed_1 - 1);
3754
47.8M
        ufixed y_steps = (ufixed)sy - (ufixed)ey;
3755
3756
47.8M
        int skip = cursor_down_tr(cr, sx, id);
3757
3758
47.8M
        if (sx == ex) {
3759
            /* Vertical line. (Falling) */
3760
3761
            /* Phase 1: */
3762
8.65M
            if (phase1_y_steps) {
3763
                /* Phase 1 in a falling line never moves us into a new scanline. */
3764
5.28M
                cursor_never_step_vertical_tr(cr, -phase1_y_steps, sx, id);
3765
5.28M
                sy -= phase1_y_steps;
3766
5.28M
                y_steps -= phase1_y_steps;
3767
5.28M
                if (y_steps == 0)
3768
0
                    goto endFallingLeftOnEdgeOfPixel;
3769
5.28M
            }
3770
3771
            /* Phase 3: precalculation */
3772
8.65M
            y_steps -= phase3_y_steps;
3773
8.65M
            assert((y_steps & (fixed_1 - 1)) == 0);
3774
3775
            /* Phase 2: */
3776
8.65M
            y_steps = fixed2int(y_steps);
3777
8.65M
            assert((int)y_steps >= 0);
3778
8.65M
            if (y_steps) {
3779
6.79M
                cursor_always_step_tr(cr, -fixed_1, sx, id, skip);
3780
6.79M
                skip = 0;
3781
6.79M
                y_steps--;
3782
268M
                while (y_steps) {
3783
261M
                    cursor_always_step_inrange_vertical_tr(cr, -fixed_1, sx, id);
3784
261M
                    y_steps--;
3785
261M
                }
3786
6.79M
            }
3787
3788
            /* Phase 3 */
3789
8.65M
            if (phase3_y_steps == 0) {
3790
3.10M
endFallingLeftOnEdgeOfPixel:
3791
3.10M
                cursor_always_step_inrange_vertical_tr(cr, 0, sx, id);
3792
3.10M
                cursor_null_tr(cr);
3793
5.54M
            } else {
3794
5.54M
                cursor_step_tr(cr, -phase3_y_steps, sx, id, skip);
3795
5.54M
                assert(cr->left == sx && cr->lid == id && cr->right == sx && cr->rid == id);
3796
5.54M
            }
3797
39.1M
        } else if (sx < ex) {
3798
            /* Lines increasing in x. (Rightwards, falling) */
3799
18.5M
            int phase1_x_steps, phase3_x_steps;
3800
            /* Use unsigned int here, to allow for extreme cases like
3801
             * ex = 0x7fffffff, sx = 0x80000000 */
3802
18.5M
            unsigned int x_steps = ex - sx;
3803
3804
            /* Phase 1: */
3805
18.5M
            if (phase1_y_steps) {
3806
16.8M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3807
16.8M
                x_steps -= phase1_x_steps;
3808
16.8M
                sx += phase1_x_steps;
3809
                /* Phase 1 in a falling line never moves us into a new scanline. */
3810
16.8M
                cursor_never_step_right_tr(cr, -phase1_y_steps, sx, id);
3811
16.8M
                sy -= phase1_y_steps;
3812
16.8M
                y_steps -= phase1_y_steps;
3813
16.8M
                if (y_steps == 0)
3814
0
                    goto endFallingRightOnEdgeOfPixel;
3815
16.8M
            }
3816
3817
            /* Phase 3: precalculation */
3818
18.5M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3819
18.5M
            x_steps -= phase3_x_steps;
3820
18.5M
            y_steps -= phase3_y_steps;
3821
18.5M
            assert((y_steps & (fixed_1 - 1)) == 0);
3822
3823
            /* Phase 2: */
3824
18.5M
            y_steps = fixed2int(y_steps);
3825
18.5M
            assert((int)y_steps >= 0);
3826
18.5M
            if (y_steps) {
3827
                /* We want to change sx by x_steps in y_steps steps.
3828
                 * So each step, we add x_steps/y_steps to sx. That's x_inc + n_inc/ey. */
3829
11.0M
                int x_inc = x_steps/y_steps;
3830
11.0M
                int n_inc = x_steps - (x_inc * y_steps);
3831
11.0M
                int f = y_steps/2;
3832
11.0M
                int d = y_steps;
3833
3834
11.0M
                cursor_always_step_tr(cr, -fixed_1, sx, id, skip);
3835
11.0M
                skip = 0;
3836
11.0M
                sx += x_inc;
3837
11.0M
                f -= n_inc;
3838
11.0M
                if (f < 0)
3839
2.20M
                    f += d, sx++;
3840
11.0M
                cursor_right_tr(cr, sx, id);
3841
11.0M
                y_steps--;
3842
3843
102M
                while (y_steps) {
3844
91.6M
                    cursor_always_inrange_step_right_tr(cr, -fixed_1, sx, id);
3845
91.6M
                    sx += x_inc;
3846
91.6M
                    f -= n_inc;
3847
91.6M
                    if (f < 0)
3848
39.0M
                        f += d, sx++;
3849
91.6M
                    cursor_right_tr(cr, sx, id);
3850
91.6M
                    y_steps--;
3851
91.6M
                }
3852
11.0M
            }
3853
3854
            /* Phase 3 */
3855
18.5M
            if (phase3_y_steps == 0) {
3856
1.38M
endFallingRightOnEdgeOfPixel:
3857
1.38M
                cursor_always_step_inrange_vertical_tr(cr, 0, sx, id);
3858
1.38M
                cursor_null_tr(cr);
3859
17.1M
            } else {
3860
17.1M
                cursor_step_tr(cr, -phase3_y_steps, sx, id, skip);
3861
17.1M
                cursor_right_tr(cr, ex, id);
3862
17.1M
                assert(cr->left == sx && cr->lid == id && cr->right == ex && cr->rid == id);
3863
17.1M
            }
3864
20.6M
        } else {
3865
            /* Lines decreasing in x. (Falling) */
3866
20.6M
            int phase1_x_steps, phase3_x_steps;
3867
            /* Use unsigned int here, to allow for extreme cases like
3868
             * sx = 0x7fffffff, ex = 0x80000000 */
3869
20.6M
            unsigned int x_steps = sx - ex;
3870
3871
            /* Phase 1: */
3872
20.6M
            if (phase1_y_steps) {
3873
18.6M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3874
18.6M
                x_steps -= phase1_x_steps;
3875
18.6M
                sx -= phase1_x_steps;
3876
                /* Phase 1 in a falling line never moves us into a new scanline. */
3877
18.6M
                cursor_never_step_left_tr(cr, -phase1_y_steps, sx, id);
3878
18.6M
                sy -= phase1_y_steps;
3879
18.6M
                y_steps -= phase1_y_steps;
3880
18.6M
                if (y_steps == 0)
3881
0
                    goto endFallingVerticalOnEdgeOfPixel;
3882
18.6M
            }
3883
3884
            /* Phase 3: precalculation */
3885
20.6M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3886
20.6M
            x_steps -= phase3_x_steps;
3887
20.6M
            y_steps -= phase3_y_steps;
3888
20.6M
            assert((y_steps & (fixed_1 - 1)) == 0);
3889
3890
            /* Phase 2: */
3891
20.6M
            y_steps = fixed2int(y_steps);
3892
20.6M
            assert((int)y_steps >= 0);
3893
20.6M
            if (y_steps) {
3894
                /* We want to change sx by x_steps in y_steps steps.
3895
                 * So each step, we sub x_steps/y_steps from sx. That's x_inc + n_inc/ey. */
3896
13.5M
                int x_inc = x_steps/y_steps;
3897
13.5M
                int n_inc = x_steps - (x_inc * y_steps);
3898
13.5M
                int f = y_steps/2;
3899
13.5M
                int d = y_steps;
3900
3901
13.5M
                cursor_always_step_tr(cr, -fixed_1, sx, id, skip);
3902
13.5M
                skip = 0;
3903
13.5M
                sx -= x_inc;
3904
13.5M
                f -= n_inc;
3905
13.5M
                if (f < 0)
3906
2.82M
                    f += d, sx--;
3907
13.5M
                cursor_left_tr(cr, sx, id);
3908
13.5M
                y_steps--;
3909
3910
115M
                while (y_steps) {
3911
102M
                    cursor_always_inrange_step_left_tr(cr, -fixed_1, sx, id);
3912
102M
                    sx -= x_inc;
3913
102M
                    f -= n_inc;
3914
102M
                    if (f < 0)
3915
45.5M
                        f += d, sx--;
3916
102M
                    cursor_left_tr(cr, sx, id);
3917
102M
                    y_steps--;
3918
102M
                }
3919
13.5M
            }
3920
3921
            /* Phase 3 */
3922
20.6M
            if (phase3_y_steps == 0) {
3923
1.65M
endFallingVerticalOnEdgeOfPixel:
3924
1.65M
                cursor_always_step_inrange_vertical_tr(cr, 0, sx, id);
3925
1.65M
                cursor_null_tr(cr);
3926
18.9M
            } else {
3927
18.9M
                cursor_step_tr(cr, -phase3_y_steps, sx, id, skip);
3928
18.9M
                cursor_left_tr(cr, ex, id);
3929
18.9M
                assert(cr->left == ex && cr->lid == id && cr->right == sx && cr->rid == id);
3930
18.9M
            }
3931
20.6M
        }
3932
72.2M
endFalling: {}
3933
72.2M
    }
3934
3935
161M
end:
3936
161M
    if (truncated) {
3937
12.2M
        cr->left = saved_ex;
3938
12.2M
        cr->lid = id;
3939
12.2M
        cr->right = saved_ex;
3940
12.2M
        cr->rid = id;
3941
12.2M
        cr->y = saved_ey;
3942
12.2M
    }
3943
161M
}
3944
3945
static void mark_curve_tr_app(cursor_tr * gs_restrict cr, fixed sx, fixed sy, fixed c1x, fixed c1y, fixed c2x, fixed c2y, fixed ex, fixed ey, int depth, int * gs_restrict id)
3946
45.2k
{
3947
45.2k
        int ax = (sx + c1x)>>1;
3948
45.2k
        int ay = (sy + c1y)>>1;
3949
45.2k
        int bx = (c1x + c2x)>>1;
3950
45.2k
        int by = (c1y + c2y)>>1;
3951
45.2k
        int cx = (c2x + ex)>>1;
3952
45.2k
        int cy = (c2y + ey)>>1;
3953
45.2k
        int dx = (ax + bx)>>1;
3954
45.2k
        int dy = (ay + by)>>1;
3955
45.2k
        int fx = (bx + cx)>>1;
3956
45.2k
        int fy = (by + cy)>>1;
3957
45.2k
        int gx = (dx + fx)>>1;
3958
45.2k
        int gy = (dy + fy)>>1;
3959
3960
45.2k
        assert(depth >= 0);
3961
45.2k
        if (depth == 0) {
3962
27.9k
            *id += 1;
3963
27.9k
            mark_line_tr_app(cr, sx, sy, ex, ey, *id);
3964
27.9k
        } else {
3965
17.3k
            depth--;
3966
17.3k
            mark_curve_tr_app(cr, sx, sy, ax, ay, dx, dy, gx, gy, depth, id);
3967
17.3k
            mark_curve_tr_app(cr, gx, gy, fx, fy, cx, cy, ex, ey, depth, id);
3968
17.3k
        }
3969
45.2k
}
3970
3971
static void mark_curve_big_tr_app(cursor_tr * gs_restrict cr, fixed64 sx, fixed64 sy, fixed64 c1x, fixed64 c1y, fixed64 c2x, fixed64 c2y, fixed64 ex, fixed64 ey, int depth, int * gs_restrict id)
3972
270k
{
3973
270k
    fixed64 ax = (sx + c1x)>>1;
3974
270k
    fixed64 ay = (sy + c1y)>>1;
3975
270k
    fixed64 bx = (c1x + c2x)>>1;
3976
270k
    fixed64 by = (c1y + c2y)>>1;
3977
270k
    fixed64 cx = (c2x + ex)>>1;
3978
270k
    fixed64 cy = (c2y + ey)>>1;
3979
270k
    fixed64 dx = (ax + bx)>>1;
3980
270k
    fixed64 dy = (ay + by)>>1;
3981
270k
    fixed64 fx = (bx + cx)>>1;
3982
270k
    fixed64 fy = (by + cy)>>1;
3983
270k
    fixed64 gx = (dx + fx)>>1;
3984
270k
    fixed64 gy = (dy + fy)>>1;
3985
3986
270k
    assert(depth >= 0);
3987
270k
    if (depth == 0) {
3988
135k
        *id += 1;
3989
135k
        mark_line_tr_app(cr, (fixed)sx, (fixed)sy, (fixed)ex, (fixed)ey, *id);
3990
135k
    } else {
3991
135k
        depth--;
3992
135k
        mark_curve_big_tr_app(cr, sx, sy, ax, ay, dx, dy, gx, gy, depth, id);
3993
135k
        mark_curve_big_tr_app(cr, gx, gy, fx, fy, cx, cy, ex, ey, depth, id);
3994
135k
    }
3995
270k
}
3996
3997
static void mark_curve_top_tr_app(cursor_tr * gs_restrict cr, fixed sx, fixed sy, fixed c1x, fixed c1y, fixed c2x, fixed c2y, fixed ex, fixed ey, int depth, int * gs_restrict id)
3998
10.6k
{
3999
10.6k
    fixed test = (sx^(sx<<1))|(sy^(sy<<1))|(c1x^(c1x<<1))|(c1y^(c1y<<1))|(c2x^(c2x<<1))|(c2y^(c2y<<1))|(ex^(ex<<1))|(ey^(ey<<1));
4000
4001
10.6k
    if (test < 0)
4002
36
        mark_curve_big_tr_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth, id);
4003
10.5k
    else
4004
10.5k
        mark_curve_tr_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth, id);
4005
10.6k
}
4006
4007
static int make_table_tr_app(gx_device     * pdev,
4008
                             gx_path       * path,
4009
                             gs_fixed_rect * ibox,
4010
                             int           * scanlines,
4011
                             int          ** index,
4012
                             int          ** table)
4013
16.9M
{
4014
16.9M
    return make_table_template(pdev, path, ibox, 4, 0, scanlines, index, table);
4015
16.9M
}
4016
4017
static void
4018
fill_zero_app_tr(int *row, const fixed *x)
4019
7.70k
{
4020
7.70k
    int n = *row = (*row)+2; /* Increment the count */
4021
7.70k
    row[4*n-7] = x[0];
4022
7.70k
    row[4*n-6] = 0;
4023
7.70k
    row[4*n-5] = x[1];
4024
7.70k
    row[4*n-4] = 0;
4025
7.70k
    row[4*n-3] = x[1];
4026
7.70k
    row[4*n-2] = (1<<1)|1;
4027
7.70k
    row[4*n-1] = x[1];
4028
7.70k
    row[4*n  ] = 1;
4029
7.70k
}
4030
4031
int gx_scan_convert_tr_app(gx_device     * gs_restrict pdev,
4032
                           gx_path       * gs_restrict path,
4033
                     const gs_fixed_rect * gs_restrict clip,
4034
                           gx_edgebuffer * gs_restrict edgebuffer,
4035
                           fixed                    fixed_flat)
4036
17.8M
{
4037
17.8M
    gs_fixed_rect  ibox;
4038
17.8M
    gs_fixed_rect  bbox;
4039
17.8M
    int            scanlines;
4040
17.8M
    const subpath *psub;
4041
17.8M
    int           *index;
4042
17.8M
    int           *table;
4043
17.8M
    int            i;
4044
17.8M
    cursor_tr      cr;
4045
17.8M
    int            code;
4046
17.8M
    int            id = 0;
4047
17.8M
    int            zero;
4048
4049
17.8M
    edgebuffer->index = NULL;
4050
17.8M
    edgebuffer->table = NULL;
4051
4052
    /* Bale out if no actual path. We see this with the clist */
4053
17.8M
    if (path->first_subpath == NULL)
4054
802k
        return 0;
4055
4056
17.0M
    zero = make_bbox(path, clip, &bbox, &ibox, 0);
4057
17.0M
    if (zero < 0)
4058
0
        return zero;
4059
4060
17.0M
    if (ibox.q.y <= ibox.p.y)
4061
140k
        return 0;
4062
4063
16.9M
    code = make_table_tr_app(pdev, path, &ibox, &scanlines, &index, &table);
4064
16.9M
    if (code != 0) /* > 0 means "retry with smaller height" */
4065
185
        return code;
4066
4067
16.9M
    if (scanlines == 0)
4068
0
        return 0;
4069
4070
16.9M
    if (zero) {
4071
7.21k
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero_app_tr);
4072
16.8M
    } else {
4073
4074
    /* Step 2 continued: Now we run through the path, filling in the real
4075
     * values. */
4076
16.8M
    cr.scanlines = scanlines;
4077
16.8M
    cr.index     = index;
4078
16.8M
    cr.table     = table;
4079
16.8M
    cr.base      = ibox.p.y;
4080
40.3M
    for (psub = path->first_subpath; psub != 0;) {
4081
23.4M
        const segment *pseg = (const segment *)psub;
4082
23.4M
        fixed ex = pseg->pt.x;
4083
23.4M
        fixed ey = pseg->pt.y;
4084
23.4M
        fixed ix = ex;
4085
23.4M
        fixed iy = ey;
4086
23.4M
        fixed sx, sy;
4087
4088
23.4M
        if ((ey & 0xff) == 0) {
4089
999k
            cr.left  = max_fixed;
4090
999k
            cr.right = min_fixed;
4091
22.4M
        } else {
4092
22.4M
            cr.left = cr.right = ex;
4093
22.4M
        }
4094
23.4M
        cr.lid = cr.rid = id+1;
4095
23.4M
        cr.y = ey;
4096
23.4M
        cr.d = DIRN_UNSET;
4097
23.4M
        cr.first = 1;
4098
23.4M
        cr.saved = 0;
4099
4100
979M
        while ((pseg = pseg->next) != 0 &&
4101
963M
               pseg->type != s_start
4102
956M
            ) {
4103
956M
            sx = ex;
4104
956M
            sy = ey;
4105
956M
            ex = pseg->pt.x;
4106
956M
            ey = pseg->pt.y;
4107
4108
956M
            switch (pseg->type) {
4109
0
                default:
4110
0
                case s_start: /* Should never happen */
4111
0
                case s_dash:  /* We should never be seeing a dash here */
4112
0
                    assert("This should never happen" == NULL);
4113
0
                    break;
4114
10.6k
                case s_curve: {
4115
10.6k
                    const curve_segment *const pcur = (const curve_segment *)pseg;
4116
10.6k
                    int k = gx_curve_log2_samples(sx, sy, pcur, fixed_flat);
4117
4118
10.6k
                    mark_curve_top_tr_app(&cr, sx, sy, pcur->p1.x, pcur->p1.y, pcur->p2.x, pcur->p2.y, ex, ey, k, &id);
4119
10.6k
                    break;
4120
0
                }
4121
0
                case s_gap:
4122
934M
                case s_line:
4123
956M
                case s_line_close:
4124
956M
                    mark_line_tr_app(&cr, sx, sy, ex, ey, ++id);
4125
956M
                    break;
4126
956M
            }
4127
956M
        }
4128
        /* And close any open segments */
4129
23.4M
        mark_line_tr_app(&cr, ex, ey, ix, iy, ++id);
4130
23.4M
        cursor_flush_tr(&cr, ex, id);
4131
23.4M
        psub = (const subpath *)pseg;
4132
23.4M
    }
4133
16.8M
    }
4134
4135
    /* Step 2 complete: We now have a complete list of intersection data in
4136
     * table, indexed by index. */
4137
4138
16.9M
    edgebuffer->base   = ibox.p.y;
4139
16.9M
    edgebuffer->height = scanlines;
4140
16.9M
    edgebuffer->xmin   = ibox.p.x;
4141
16.9M
    edgebuffer->xmax   = ibox.q.x;
4142
16.9M
    edgebuffer->index  = index;
4143
16.9M
    edgebuffer->table  = table;
4144
4145
#ifdef DEBUG_SCAN_CONVERTER
4146
    if (debugging_scan_converter) {
4147
        dlprintf("Before sorting\n");
4148
        gx_edgebuffer_print_tr_app(edgebuffer);
4149
    }
4150
#endif
4151
4152
    /* Step 3: Sort the intersects on x */
4153
478M
    for (i=0; i < scanlines; i++) {
4154
461M
        int *row = &table[index[i]];
4155
461M
        int  rowlen = *row++;
4156
4157
        /* Bubblesort short runs, qsort longer ones. */
4158
        /* Figure of '6' comes from testing */
4159
461M
        if (rowlen <= 6) {
4160
457M
            int j, k;
4161
980M
            for (j = 0; j < rowlen-1; j++) {
4162
523M
                int * gs_restrict t = &row[j<<2];
4163
1.15G
                for (k = j+1; k < rowlen; k++) {
4164
633M
                    int * gs_restrict s = &row[k<<2];
4165
633M
                    int tmp;
4166
633M
                    if (t[0] < s[0])
4167
292M
                        continue;
4168
341M
                    if (t[0] > s[0])
4169
323M
                        goto swap0213;
4170
17.7M
                    if (t[2] < s[2])
4171
2.02M
                        continue;
4172
15.6M
                    if (t[2] > s[2])
4173
3.57M
                        goto swap213;
4174
12.1M
                    if (t[1] < s[1])
4175
10.7M
                        continue;
4176
1.33M
                    if (t[1] > s[1])
4177
1.33M
                        goto swap13;
4178
80
                    if (t[3] <= s[3])
4179
80
                        continue;
4180
0
                    if (0) {
4181
323M
swap0213:
4182
323M
                        tmp = t[0], t[0] = s[0], s[0] = tmp;
4183
327M
swap213:
4184
327M
                        tmp = t[2], t[2] = s[2], s[2] = tmp;
4185
328M
swap13:
4186
328M
                        tmp = t[1], t[1] = s[1], s[1] = tmp;
4187
328M
                    }
4188
328M
                    tmp = t[3], t[3] = s[3], s[3] = tmp;
4189
328M
                }
4190
523M
            }
4191
457M
        } else
4192
3.19M
            qsort(row, rowlen, 4*sizeof(int), edgecmp_tr);
4193
461M
    }
4194
4195
16.9M
    return 0;
4196
16.9M
}
4197
4198
/* Step 5: Filter the intersections according to the rules */
4199
int
4200
gx_filter_edgebuffer_tr_app(gx_device       * gs_restrict pdev,
4201
                            gx_edgebuffer   * gs_restrict edgebuffer,
4202
                            int                        rule)
4203
17.8M
{
4204
17.8M
    int i;
4205
17.8M
    int marked_id = 0;
4206
4207
#ifdef DEBUG_SCAN_CONVERTER
4208
    if (debugging_scan_converter) {
4209
        dlprintf("Before filtering:\n");
4210
        gx_edgebuffer_print_tr_app(edgebuffer);
4211
    }
4212
#endif
4213
4214
478M
    for (i=0; i < edgebuffer->height; i++) {
4215
461M
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
4216
461M
        int  rowlen   = *row++;
4217
461M
        int *rowstart = row;
4218
461M
        int *rowout   = row;
4219
461M
        int  ll, llid, lr, lrid, rlid, rr, rrid, wind, marked_to;
4220
4221
        /* Avoid double setting pixels, by keeping where we have marked to. */
4222
461M
        marked_to = INT_MIN;
4223
996M
        while (rowlen > 0) {
4224
535M
            if (rule == gx_rule_even_odd) {
4225
                /* Even Odd */
4226
32.7M
                ll   = *row++;
4227
32.7M
                llid = (*row++)>>1;
4228
32.7M
                lr   = *row++;
4229
32.7M
                lrid = *row++;
4230
32.7M
                rowlen--;
4231
4232
                /* We will fill solidly from ll to at least lr, possibly further */
4233
32.7M
                assert(rowlen > 0);
4234
32.7M
                (void)row++; /* rl not needed here */
4235
32.7M
                (void)row++;
4236
32.7M
                rr   = *row++;
4237
32.7M
                rrid = *row++;
4238
32.7M
                rowlen--;
4239
32.7M
                if (rr > lr) {
4240
29.7M
                    lr   = rr;
4241
29.7M
                    lrid = rrid;
4242
29.7M
                }
4243
502M
            } else {
4244
                /* Non-Zero */
4245
502M
                int w;
4246
4247
502M
                ll   = *row++;
4248
502M
                llid = *row++;
4249
502M
                lr   = *row++;
4250
502M
                lrid = *row++;
4251
502M
                wind = -(llid&1) | 1;
4252
502M
                llid >>= 1;
4253
502M
                rowlen--;
4254
4255
502M
                assert(rowlen > 0);
4256
518M
                do {
4257
518M
                    (void)row++; /* rl not needed */
4258
518M
                    rlid = *row++;
4259
518M
                    rr   = *row++;
4260
518M
                    rrid = *row++;
4261
518M
                    w = -(rlid&1) | 1;
4262
518M
                    rlid >>= 1;
4263
518M
                    rowlen--;
4264
518M
                    if (rr > lr) {
4265
502M
                        lr   = rr;
4266
502M
                        lrid = rrid;
4267
502M
                    }
4268
518M
                    wind += w;
4269
518M
                    if (wind == 0)
4270
502M
                        break;
4271
518M
                } while (rowlen > 0);
4272
502M
            }
4273
4274
535M
            if (lr < marked_to)
4275
1.66M
                continue;
4276
4277
533M
            if (marked_to >= ll) {
4278
6.62M
                if (rowout == rowstart) {
4279
1.93k
                    ll   = marked_to;
4280
1.93k
                    llid = --marked_id;
4281
6.62M
                } else {
4282
6.62M
                    rowout -= 4;
4283
6.62M
                    ll   = rowout[0];
4284
6.62M
                    llid = rowout[1];
4285
6.62M
                }
4286
6.62M
            }
4287
4288
533M
            if (lr >= ll) {
4289
533M
                *rowout++ = ll;
4290
533M
                *rowout++ = llid;
4291
533M
                *rowout++ = lr;
4292
533M
                *rowout++ = lrid;
4293
533M
                marked_to = lr;
4294
533M
            }
4295
533M
        }
4296
461M
        rowstart[-1] = (rowout - rowstart)>>2;
4297
461M
    }
4298
17.8M
    return 0;
4299
17.8M
}
4300
4301
/* Step 6: Fill */
4302
int
4303
gx_fill_edgebuffer_tr_app(gx_device       * gs_restrict pdev,
4304
                    const gx_device_color * gs_restrict pdevc,
4305
                          gx_edgebuffer   * gs_restrict edgebuffer,
4306
                          int                        log_op)
4307
17.8M
{
4308
17.8M
    int i, j, code;
4309
17.8M
    int mfb = pdev->max_fill_band;
4310
4311
#ifdef DEBUG_SCAN_CONVERTER
4312
    if (debugging_scan_converter) {
4313
        dlprintf("Filling:\n");
4314
        gx_edgebuffer_print_filtered_tr_app(edgebuffer);
4315
    }
4316
#endif
4317
4318
79.3M
    for (i=0; i < edgebuffer->height; ) {
4319
61.4M
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
4320
61.4M
        int  rowlen = *row++;
4321
61.4M
        int *row2;
4322
61.4M
        int *rowptr;
4323
61.4M
        int *row2ptr;
4324
61.4M
        int y_band_max;
4325
4326
61.4M
        if (mfb) {
4327
0
            y_band_max = (i & ~(mfb-1)) + mfb;
4328
0
            if (y_band_max > edgebuffer->height)
4329
0
                y_band_max = edgebuffer->height;
4330
61.4M
        } else {
4331
61.4M
            y_band_max = edgebuffer->height;
4332
61.4M
        }
4333
4334
        /* See how many scanlines match i */
4335
461M
        for (j = i+1; j < y_band_max; j++) {
4336
444M
            int row2len;
4337
4338
444M
            row2    = &edgebuffer->table[edgebuffer->index[j]];
4339
444M
            row2len = *row2++;
4340
444M
            row2ptr = row2;
4341
444M
            rowptr  = row;
4342
4343
444M
            if (rowlen != row2len)
4344
1.78M
                break;
4345
871M
            while (row2len > 0) {
4346
471M
                if (rowptr[1] != row2ptr[1] || rowptr[3] != row2ptr[3])
4347
42.7M
                    goto rowdifferent;
4348
428M
                rowptr  += 4;
4349
428M
                row2ptr += 4;
4350
428M
                row2len--;
4351
428M
            }
4352
442M
        }
4353
61.4M
rowdifferent:{}
4354
4355
        /* So j is the first scanline that doesn't match i */
4356
4357
        /* The first scanline is always sent as rectangles */
4358
163M
        while (rowlen > 0) {
4359
101M
            int left  = row[0];
4360
101M
            int right = row[2];
4361
101M
            row += 4;
4362
101M
            left = fixed2int(left);
4363
101M
            right = fixed2int(right + fixed_1 - 1);
4364
101M
            rowlen--;
4365
4366
101M
            right -= left;
4367
101M
            if (right > 0) {
4368
#ifdef DEBUG_OUTPUT_SC_AS_PS
4369
                dlprintf("0.001 setlinewidth 1 0 0 setrgbcolor %%red %%PS\n");
4370
                coord("moveto", int2fixed(left), int2fixed(edgebuffer->base+i));
4371
                coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+i));
4372
                coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+i+1));
4373
                coord("lineto", int2fixed(left), int2fixed(edgebuffer->base+i+1));
4374
                dlprintf("closepath stroke %%PS\n");
4375
#endif
4376
101M
                if (log_op < 0)
4377
0
                    code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
4378
101M
                else
4379
101M
                    code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
4380
101M
                if (code < 0)
4381
1
                    return code;
4382
101M
            }
4383
101M
        }
4384
4385
        /* The middle section (all but the first and last
4386
         * scanlines) can be sent as a trapezoid. */
4387
61.4M
        if (i + 2 < j) {
4388
18.4M
            gs_fixed_edge le;
4389
18.4M
            gs_fixed_edge re;
4390
18.4M
            fixed ybot = int2fixed(edgebuffer->base+i+1);
4391
18.4M
            fixed ytop = int2fixed(edgebuffer->base+j-1);
4392
18.4M
            int *row3, *row4;
4393
18.4M
            int offset = 1;
4394
18.4M
            row    = &edgebuffer->table[edgebuffer->index[i]];
4395
18.4M
            row2    = &edgebuffer->table[edgebuffer->index[i+1]];
4396
18.4M
            row3    = &edgebuffer->table[edgebuffer->index[j-2]];
4397
18.4M
            row4    = &edgebuffer->table[edgebuffer->index[j-1]];
4398
18.4M
            rowlen = *row;
4399
38.1M
            while (rowlen > 0) {
4400
                /* The fill rules used by fill_trap state that if a
4401
                 * pixel centre is touched by a boundary, the pixel
4402
                 * will be filled iff the boundary is horizontal and
4403
                 * the filled region is above it, or the boundary is
4404
                 * not horizontal, and the filled region is to the
4405
                 * right of it.
4406
                 *
4407
                 * We need to fill "any part of a pixel", not just
4408
                 * "centre covered", so we need to adjust our edges
4409
                 * by half a pixel in both X and Y.
4410
                 *
4411
                 * X is relatively easy. We move the left edge back by
4412
                 * just less than half, so ...00 goes to ...81 and
4413
                 * therefore does not cause an extra pixel to get filled.
4414
                 *
4415
                 * Similarly, we move the right edge forward by half, so
4416
                 *  ...00 goes to ...80 and therefore does not cause an
4417
                 * extra pixel to get filled.
4418
                 *
4419
                 * For y, we can adjust edges up or down as appropriate.
4420
                 * We move up by half, so ...0 goes to ..80 and therefore
4421
                 * does not cause an extra pixel to get filled. We move
4422
                 * down by just less than a half so that ...0 goes to
4423
                 * ...81 and therefore does not cause an extra pixel to
4424
                 * get filled.
4425
                 *
4426
                 * We use ybot = ...80 and ytop = ...81 in the trap call
4427
                 * so that it just covers the pixel centres.
4428
                 */
4429
19.7M
                if (row[offset] <= row4[offset]) {
4430
13.3M
                    le.start.x = row2[offset] - (fixed_half-1);
4431
13.3M
                    le.end.x   = row4[offset] - (fixed_half-1);
4432
13.3M
                    le.start.y = ybot + fixed_half;
4433
13.3M
                    le.end.y   = ytop + fixed_half;
4434
13.3M
                } else {
4435
6.46M
                    le.start.x = row [offset] - (fixed_half-1);
4436
6.46M
                    le.end.x   = row3[offset] - (fixed_half-1);
4437
6.46M
                    le.start.y = ybot - (fixed_half-1);
4438
6.46M
                    le.end.y   = ytop - (fixed_half-1);
4439
6.46M
                }
4440
19.7M
                if (row[offset+2] <= row4[offset+2]) {
4441
13.3M
                    re.start.x = row [offset+2] + fixed_half;
4442
13.3M
                    re.end.x   = row3[offset+2] + fixed_half;
4443
13.3M
                    re.start.y = ybot - (fixed_half-1);
4444
13.3M
                    re.end.y   = ytop - (fixed_half-1);
4445
13.3M
                } else {
4446
6.39M
                    re.start.x = row2[offset+2] + fixed_half;
4447
6.39M
                    re.end.x   = row4[offset+2] + fixed_half;
4448
6.39M
                    re.start.y = ybot + fixed_half;
4449
6.39M
                    re.end.y   = ytop + fixed_half;
4450
6.39M
                }
4451
19.7M
                offset += 4;
4452
19.7M
                rowlen--;
4453
4454
19.7M
                assert(re.start.x >= le.start.x);
4455
19.7M
                assert(re.end.x >= le.end.x);
4456
19.7M
                assert(le.start.y <= ybot + fixed_half);
4457
19.7M
                assert(re.start.y <= ybot + fixed_half);
4458
19.7M
                assert(le.end.y >= ytop - (fixed_half - 1));
4459
19.7M
                assert(re.end.y >= ytop - (fixed_half - 1));
4460
4461
#ifdef DEBUG_OUTPUT_SC_AS_PS
4462
                dlprintf("0.001 setlinewidth 0 1 0 setrgbcolor %% green %%PS\n");
4463
                coord("moveto", le.start.x, le.start.y);
4464
                coord("lineto", le.end.x, le.end.y);
4465
                coord("lineto", re.end.x, re.end.y);
4466
                coord("lineto", re.start.x, re.start.y);
4467
                dlprintf("closepath stroke %%PS\n");
4468
#endif
4469
19.7M
                code = dev_proc(pdev, fill_trapezoid)(
4470
19.7M
                                pdev,
4471
19.7M
                                &le,
4472
19.7M
                                &re,
4473
19.7M
                                ybot + fixed_half,
4474
19.7M
                                ytop - (fixed_half - 1),
4475
19.7M
                                0, /* bool swap_axes */
4476
19.7M
                                pdevc, /*const gx_drawing_color *pdcolor */
4477
19.7M
                                log_op);
4478
19.7M
                if (code < 0)
4479
0
                    return code;
4480
19.7M
            }
4481
18.4M
        }
4482
4483
61.4M
        if (i + 1 < j)
4484
27.8M
        {
4485
            /* The last scanline is always sent as rectangles */
4486
27.8M
            row    = &edgebuffer->table[edgebuffer->index[j-1]];
4487
27.8M
            rowlen = *row++;
4488
58.4M
            while (rowlen > 0) {
4489
30.5M
                int left  = row[0];
4490
30.5M
                int right = row[2];
4491
30.5M
                row += 4;
4492
30.5M
                left = fixed2int(left);
4493
30.5M
                right = fixed2int(right + fixed_1 - 1);
4494
30.5M
                rowlen--;
4495
4496
30.5M
                right -= left;
4497
30.5M
                if (right > 0) {
4498
#ifdef DEBUG_OUTPUT_SC_AS_PS
4499
                    dlprintf("0.001 setlinewidth 0 0 1 setrgbcolor %% blue %%PS\n");
4500
                    coord("moveto", int2fixed(left), int2fixed(edgebuffer->base+j-1));
4501
                    coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+j-1));
4502
                    coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+j));
4503
                    coord("lineto", int2fixed(left), int2fixed(edgebuffer->base+j));
4504
                    dlprintf("closepath stroke %%PS\n");
4505
#endif
4506
30.5M
                    if (log_op < 0)
4507
0
                        code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+j-1, right, 1, pdevc->colors.pure);
4508
30.5M
                    else
4509
30.5M
                        code = gx_fill_rectangle_device_rop(left, edgebuffer->base+j-1, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
4510
30.5M
                    if (code < 0)
4511
0
                        return code;
4512
30.5M
                }
4513
30.5M
            }
4514
27.8M
        }
4515
61.4M
        i = j;
4516
61.4M
    }
4517
17.8M
    return 0;
4518
17.8M
}
4519
4520
4521
void
4522
gx_edgebuffer_init(gx_edgebuffer * edgebuffer)
4523
18.9M
{
4524
18.9M
    edgebuffer->base   = 0;
4525
18.9M
    edgebuffer->height = 0;
4526
18.9M
    edgebuffer->index  = NULL;
4527
18.9M
    edgebuffer->table  = NULL;
4528
18.9M
}
4529
4530
void
4531
gx_edgebuffer_fin(gx_device     * pdev,
4532
                  gx_edgebuffer * edgebuffer)
4533
18.9M
{
4534
18.9M
    gs_free_object(pdev->memory, edgebuffer->table, "scanc intersects buffer");
4535
18.9M
    gs_free_object(pdev->memory, edgebuffer->index, "scanc index buffer");
4536
18.9M
    edgebuffer->index = NULL;
4537
18.9M
    edgebuffer->table = NULL;
4538
18.9M
}
4539
4540
gx_scan_converter_t gx_scan_converter =
4541
{
4542
    gx_scan_convert,
4543
    gx_filter_edgebuffer,
4544
    gx_fill_edgebuffer
4545
};
4546
4547
gx_scan_converter_t gx_scan_converter_app =
4548
{
4549
    gx_scan_convert_app,
4550
    gx_filter_edgebuffer_app,
4551
    gx_fill_edgebuffer_app
4552
};
4553
4554
gx_scan_converter_t gx_scan_converter_tr =
4555
{
4556
    gx_scan_convert_tr,
4557
    gx_filter_edgebuffer_tr,
4558
    gx_fill_edgebuffer_tr
4559
};
4560
4561
gx_scan_converter_t gx_scan_converter_tr_app =
4562
{
4563
    gx_scan_convert_tr_app,
4564
    gx_filter_edgebuffer_tr_app,
4565
    gx_fill_edgebuffer_tr_app
4566
};
4567
4568
int
4569
gx_scan_convert_and_fill(const gx_scan_converter_t *sc,
4570
                               gx_device       *dev,
4571
                               gx_path         *ppath,
4572
                         const gs_fixed_rect   *ibox,
4573
                               fixed            flat,
4574
                               int              rule,
4575
                         const gx_device_color *pdevc,
4576
                               int              lop)
4577
18.9M
{
4578
18.9M
    int code;
4579
18.9M
    gx_edgebuffer eb;
4580
18.9M
    gs_fixed_rect ibox2 = *ibox;
4581
18.9M
    int height;
4582
18.9M
    int mfb = dev->max_fill_band;
4583
4584
18.9M
    if (mfb != 0) {
4585
0
        ibox2.p.y &= ~(mfb-1);
4586
0
        ibox2.q.y = (ibox2.q.y+mfb-1) & ~(mfb-1);
4587
0
    }
4588
18.9M
    height = ibox2.q.y - ibox2.p.y;
4589
4590
18.9M
    do {
4591
18.9M
        gx_edgebuffer_init(&eb);
4592
18.9M
        while (1) {
4593
18.9M
            ibox2.q.y = ibox2.p.y + height;
4594
18.9M
            if (ibox2.q.y > ibox->q.y)
4595
82
                ibox2.q.y = ibox->q.y;
4596
18.9M
            code = sc->scan_convert(dev,
4597
18.9M
                                    ppath,
4598
18.9M
                                    &ibox2,
4599
18.9M
                                    &eb,
4600
18.9M
                                    flat);
4601
18.9M
            if (code <= 0)
4602
18.9M
                break;
4603
            /* Let's shrink the ibox and try again */
4604
188
            if (mfb && height == mfb) {
4605
                /* Can't shrink the height any more! */
4606
0
                code = gs_error_rangecheck;
4607
0
                break;
4608
0
            }
4609
188
            height = height/code;
4610
188
            if (mfb)
4611
0
                height = (height + mfb-1) & ~(mfb-1);
4612
188
            if (height < (mfb ? mfb : 1)) {
4613
0
                code = gs_error_VMerror;
4614
0
                break;
4615
0
            }
4616
188
        }
4617
18.9M
        if (code >= 0)
4618
18.9M
            code = sc->filter(dev,
4619
18.9M
                              &eb,
4620
18.9M
                              rule);
4621
18.9M
        if (code >= 0)
4622
18.9M
            code = sc->fill(dev,
4623
18.9M
                            pdevc,
4624
18.9M
                            &eb,
4625
18.9M
                            lop);
4626
18.9M
        gx_edgebuffer_fin(dev,&eb);
4627
18.9M
        ibox2.p.y += height;
4628
18.9M
    }
4629
18.9M
    while (ibox2.p.y < ibox->q.y);
4630
4631
18.9M
    return code;
4632
18.9M
}