Coverage Report

Created: 2025-06-10 07:26

/src/ghostpdl/base/gxscanc.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2025 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
365k
{
157
365k
    return *((int*)a) - *((int *)b);
158
365k
}
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
2.40k
{
204
2.40k
    if (sx < zf[0])
205
0
        zf[0] = sx;
206
2.40k
    if (ex < zf[0])
207
26
        zf[0] = ex;
208
2.40k
    if (sx > zf[1])
209
0
        zf[1] = sx;
210
2.40k
    if (ex > zf[1])
211
569
        zf[1] = ex;
212
2.40k
}
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
678
{
271
678
    const subpath *psub;
272
678
    fixed zf[2];
273
274
    /* Step 2 continued: Now we run through the path, filling in the real
275
     * values. */
276
1.36k
    for (psub = path->first_subpath; psub != 0;) {
277
682
        const segment *pseg = (const segment *)psub;
278
682
        fixed ex = pseg->pt.x;
279
682
        fixed sy = pseg->pt.y;
280
682
        fixed ix = ex;
281
682
        int iy = fixed2int(pseg->pt.y);
282
283
682
        zf[0] = ex;
284
682
        zf[1] = ex;
285
286
2.40k
        while ((pseg = pseg->next) != 0 &&
287
2.40k
               pseg->type != s_start
288
1.72k
            ) {
289
1.72k
            fixed sx = ex;
290
1.72k
            ex = pseg->pt.x;
291
292
1.72k
            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
1.22k
                case s_line:
307
1.72k
                case s_line_close:
308
1.72k
                    mark_line_zero(sx, ex, zf);
309
1.72k
                    break;
310
1.72k
            }
311
1.72k
        }
312
        /* And close any open segments */
313
682
        mark_line_zero(ex, ix, zf);
314
682
        fill(&table[index[iy-ibox->p.y]], zf);
315
682
        psub = (const subpath *)pseg;
316
682
    }
317
318
678
    return 0;
319
678
}
320
321
static void mark_line(fixed sx, fixed sy, fixed ex, fixed ey, int base_y, int height, int *table, int *index)
322
2.39M
{
323
2.39M
    int64_t delta;
324
2.39M
    int iy, ih;
325
2.39M
    fixed clip_sy, clip_ey;
326
2.39M
    int dirn = DIRN_UP;
327
2.39M
    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
2.39M
    if (fixed2int(sy + fixed_half-1) == fixed2int(ey + fixed_half-1))
341
945k
        return;
342
1.45M
    if (sy > ey) {
343
762k
        int t;
344
762k
        t = sy; sy = ey; ey = t;
345
762k
        t = sx; sx = ex; ex = t;
346
762k
        dirn = DIRN_DOWN;
347
762k
    }
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
1.45M
    clip_sy = ((sy + fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
352
    /* The last scanline marked (< ey) is: */
353
1.45M
    clip_ey = ((ey - fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
354
    /* Now allow for banding */
355
1.45M
    if (clip_sy < int2fixed(base_y) + fixed_half)
356
726k
        clip_sy = int2fixed(base_y) + fixed_half;
357
1.45M
    if (ey <= clip_sy)
358
714k
        return;
359
736k
    if (clip_ey > int2fixed(base_y + height - 1) + fixed_half)
360
558k
        clip_ey = int2fixed(base_y + height - 1) + fixed_half;
361
736k
    if (sy > clip_ey)
362
547k
        return;
363
189k
    delta = (int64_t)clip_sy - (int64_t)sy;
364
189k
    if (delta > 0)
365
186k
    {
366
186k
        int64_t dx = (int64_t)ex - (int64_t)sx;
367
186k
        int64_t dy = (int64_t)ey - (int64_t)sy;
368
186k
        int advance = (int)((dx * delta + (dy>>1)) / dy);
369
186k
        sx += advance;
370
186k
        sy += delta;
371
186k
    }
372
189k
    delta = (int64_t)ey - (int64_t)clip_ey;
373
189k
    if (delta > 0)
374
189k
    {
375
189k
        int64_t dx = (int64_t)ex - (int64_t)sx;
376
189k
        int64_t dy = (int64_t)ey - (int64_t)sy;
377
189k
        int advance = (int)((dx * delta + (dy>>1)) / dy);
378
189k
        ex -= advance;
379
189k
        ey -= delta;
380
189k
    }
381
189k
    ex -= sx;
382
189k
    ey -= sy;
383
189k
    ih = fixed2int(ey);
384
189k
    assert(ih >= 0);
385
189k
    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
189k
    assert(iy >= 0 && iy < height);
391
    /* We always cross at least one scanline */
392
189k
    row = &table[index[iy]];
393
189k
    *row = (*row)+1; /* Increment the count */
394
189k
    row[*row] = (sx&~1) | dirn;
395
189k
    if (ih == 0)
396
68.9k
        return;
397
120k
    if (ex >= 0) {
398
48.9k
        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
48.9k
        x_inc = ex/ih;
404
48.9k
        n_inc = ex-(x_inc*ih);
405
48.9k
        f     = ih>>1;
406
48.9k
        delta = ih;
407
689k
        do {
408
689k
            int count;
409
689k
            iy++;
410
689k
            sx += x_inc;
411
689k
            f  -= n_inc;
412
689k
            if (f < 0) {
413
200k
                f += ih;
414
200k
                sx++;
415
200k
            }
416
689k
            assert(iy >= 0 && iy < height);
417
689k
            row = &table[index[iy]];
418
689k
            count = *row = (*row)+1; /* Increment the count */
419
689k
            row[count] = (sx&~1) | dirn;
420
689k
        } while (--delta);
421
71.5k
    } else {
422
71.5k
        int x_dec, n_dec, f;
423
424
71.5k
        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
71.5k
        x_dec = ex/ih;
429
71.5k
        n_dec = ex-(x_dec*ih);
430
71.5k
        f     = ih>>1;
431
71.5k
        delta = ih;
432
977k
        do {
433
977k
            int count;
434
977k
            iy++;
435
977k
            sx -= x_dec;
436
977k
            f  -= n_dec;
437
977k
            if (f < 0) {
438
482k
                f += ih;
439
482k
                sx--;
440
482k
            }
441
977k
            assert(iy >= 0 && iy < height);
442
977k
            row = &table[index[iy]];
443
977k
            count = *row = (*row)+1; /* Increment the count */
444
977k
            row[count] = (sx&~1) | dirn;
445
977k
        } while (--delta);
446
71.5k
    }
447
120k
}
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
2.49M
{
515
2.49M
    int           code;
516
2.49M
    int           ret = 0;
517
518
    /* Find the bbox - fixed */
519
2.49M
    code = gx_path_bbox(path, bbox);
520
2.49M
    if (code < 0)
521
0
        return code;
522
523
2.49M
    if (bbox->p.y == bbox->q.y) {
524
        /* Zero height path */
525
694
        if (!clip ||
526
694
            (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
678
            if (clip) {
529
678
                if (bbox->p.x < clip->p.x)
530
68
                    bbox->p.x = clip->p.x;
531
678
                if (bbox->q.x > clip->q.x)
532
49
                    bbox->q.x = clip->q.x;
533
678
            }
534
678
            if (bbox->p.x <= bbox->q.x) {
535
                /* Zero height rectangle, not clipped completely away */
536
678
                ret = 1;
537
678
            }
538
678
        }
539
694
    }
540
541
2.49M
    if (clip) {
542
2.49M
        if (bbox->p.y < clip->p.y)
543
635k
            bbox->p.y = clip->p.y;
544
2.49M
        if (bbox->q.y > clip->q.y)
545
628k
            bbox->q.y = clip->q.y;
546
2.49M
    }
547
548
    /* Convert to bbox - int */
549
2.49M
    ibox->p.x = fixed2int(bbox->p.x+adjust-(adjust?1:0));
550
2.49M
    ibox->p.y = fixed2int(bbox->p.y+adjust-(adjust?1:0));
551
2.49M
    ibox->q.x = fixed2int(bbox->q.x-adjust+fixed_1);
552
2.49M
    ibox->q.y = fixed2int(bbox->q.y-adjust+fixed_1);
553
554
2.49M
    return ret;
555
2.49M
}
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
2.48M
{
567
2.48M
    int             scanlines;
568
2.48M
    const subpath * gs_restrict psub;
569
2.48M
    int           * gs_restrict index;
570
2.48M
    int           * gs_restrict table;
571
2.48M
    int             i;
572
2.48M
    int64_t         offset;
573
2.48M
    int             delta;
574
2.48M
    fixed           base_y;
575
576
2.48M
    *scanlinesp = 0;
577
2.48M
    *indexp     = NULL;
578
2.48M
    *tablep     = NULL;
579
580
2.48M
    if (pdev->max_fill_band != 0)
581
0
        ibox->p.y &= ~(pdev->max_fill_band-1);
582
2.48M
    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
2.48M
    scanlines = ibox->q.y-base_y;
593
    /* +1+adjust simplifies the loop below */
594
2.48M
    index = (int *)gs_alloc_bytes(pdev->memory,
595
2.48M
                                  (scanlines+1+adjust) * sizeof(*index),
596
2.48M
                                  "scanc index buffer");
597
2.48M
    if (index == NULL)
598
0
        return_error(gs_error_VMerror);
599
600
    /* Step 1 continued: Blank the index */
601
2.48M
    memset(index, 0, (scanlines+1)*sizeof(int));
602
603
    /* Step 1 continued: Run through the path, filling in the index */
604
5.19M
    for (psub = path->first_subpath; psub != 0;) {
605
2.71M
        const segment * gs_restrict pseg = (const segment *)psub;
606
2.71M
        fixed          ey = pseg->pt.y;
607
2.71M
        fixed          iy = ey;
608
2.71M
        int            iey = fixed2int(iy) - base_y;
609
610
2.71M
        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
2.71M
        if (iey >= 0 && iey < scanlines)
615
2.02M
        {
616
2.02M
            index[iey] += 2;
617
2.02M
            if (iey+1 < scanlines)
618
1.28M
                index[iey+1] -= 2;
619
2.02M
        }
620
621
67.3M
        while ((pseg = pseg->next) != 0 &&
622
67.3M
               pseg->type != s_start
623
64.6M
            ) {
624
64.6M
            fixed sy = ey;
625
64.6M
            ey = pseg->pt.y;
626
627
64.6M
            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
2.18k
                case s_curve: {
634
2.18k
                    const curve_segment *const gs_restrict pcur = (const curve_segment *)pseg;
635
2.18k
                    fixed c1y = pcur->p1.y;
636
2.18k
                    fixed c2y = pcur->p2.y;
637
2.18k
                    fixed maxy = sy, miny = sy;
638
2.18k
                    int imaxy, iminy;
639
2.18k
                    if (miny > c1y)
640
967
                        miny = c1y;
641
2.18k
                    if (miny > c2y)
642
1.08k
                        miny = c2y;
643
2.18k
                    if (miny > ey)
644
893
                        miny = ey;
645
2.18k
                    if (maxy < c1y)
646
1.05k
                        maxy = c1y;
647
2.18k
                    if (maxy < c2y)
648
1.06k
                        maxy = c2y;
649
2.18k
                    if (maxy < ey)
650
972
                        maxy = ey;
651
#ifdef DEBUG_SCAN_CONVERTER
652
                    if (debugging_scan_converter)
653
                        dlprintf2("Curve (%x->%x) ", miny, maxy);
654
#endif
655
2.18k
                    iminy = fixed2int(miny) - base_y;
656
2.18k
                    if (iminy <= 0)
657
756
                        iminy = 0;
658
1.43k
                    else
659
1.43k
                        iminy -= adjust;
660
2.18k
                    if (iminy < scanlines) {
661
1.57k
                        imaxy = fixed2int(maxy) - base_y;
662
1.57k
                        if (imaxy >= 0) {
663
#ifdef DEBUG_SCAN_CONVERTER
664
                            if (debugging_scan_converter)
665
                                dlprintf1("+%x ", iminy);
666
#endif
667
873
                            index[iminy]+=3;
668
873
                            if (imaxy < scanlines) {
669
#ifdef DEBUG_SCAN_CONVERTER
670
                                if (debugging_scan_converter)
671
                                    dlprintf1("-%x ", imaxy+1);
672
#endif
673
861
                                index[imaxy+1+adjust]-=3;
674
861
                            }
675
873
                        }
676
1.57k
                    }
677
#ifdef DEBUG_SCAN_CONVERTER
678
                    if (debugging_scan_converter)
679
                        dlprintf("\n");
680
#endif
681
2.18k
                    break;
682
0
                }
683
0
                case s_gap:
684
62.3M
                case s_line:
685
64.6M
                case s_line_close: {
686
64.6M
                    fixed miny, maxy;
687
64.6M
                    int imaxy, iminy;
688
64.6M
                    if (sy == ey) {
689
#ifdef DEBUG_SCAN_CONVERTER
690
                        if (debugging_scan_converter)
691
                            dlprintf("Line (Horiz)\n");
692
#endif
693
5.85M
                        break;
694
5.85M
                    }
695
58.8M
                    if (sy < ey)
696
28.6M
                        miny = sy, maxy = ey;
697
30.1M
                    else
698
30.1M
                        miny = ey, maxy = sy;
699
#ifdef DEBUG_SCAN_CONVERTER
700
                    if (debugging_scan_converter)
701
                        dlprintf2("Line (%x->%x) ", miny, maxy);
702
#endif
703
58.8M
                    iminy = fixed2int(miny) - base_y;
704
58.8M
                    if (iminy <= 0)
705
35.5M
                        iminy = 0;
706
23.2M
                    else
707
23.2M
                        iminy -= adjust;
708
58.8M
                    if (iminy < scanlines) {
709
46.4M
                        imaxy = fixed2int(maxy) - base_y;
710
46.4M
                        if (imaxy >= 0) {
711
#ifdef DEBUG_SCAN_CONVERTER
712
                            if (debugging_scan_converter)
713
                                dlprintf1("+%x ", iminy);
714
#endif
715
17.4M
                            index[iminy]++;
716
17.4M
                            if (imaxy < scanlines) {
717
#ifdef DEBUG_SCAN_CONVERTER
718
                                if (debugging_scan_converter)
719
                                    dlprintf1("-%x ", imaxy+1);
720
#endif
721
16.1M
                                index[imaxy+1+adjust]--;
722
16.1M
                            }
723
17.4M
                        }
724
46.4M
                    }
725
#ifdef DEBUG_SCAN_CONVERTER
726
                    if (debugging_scan_converter)
727
                        dlprintf("\n");
728
#endif
729
58.8M
                    break;
730
64.6M
                }
731
64.6M
            }
732
64.6M
        }
733
734
        /* And close any segments that need it */
735
2.71M
        if (ey != iy) {
736
112k
            fixed miny, maxy;
737
112k
            int imaxy, iminy;
738
112k
            if (iy < ey)
739
44.6k
                miny = iy, maxy = ey;
740
67.5k
            else
741
67.5k
                miny = ey, maxy = iy;
742
#ifdef DEBUG_SCAN_CONVERTER
743
            if (debugging_scan_converter)
744
                dlprintf2("Close (%x->%x) ", miny, maxy);
745
#endif
746
112k
            iminy = fixed2int(miny) - base_y;
747
112k
            if (iminy <= 0)
748
84.2k
                iminy = 0;
749
27.8k
            else
750
27.8k
                iminy -= adjust;
751
112k
            if (iminy < scanlines) {
752
87.0k
                imaxy = fixed2int(maxy) - base_y;
753
87.0k
                if (imaxy >= 0) {
754
#ifdef DEBUG_SCAN_CONVERTER
755
                    if (debugging_scan_converter)
756
                        dlprintf1("+%x ", iminy);
757
#endif
758
70.2k
                    index[iminy]++;
759
70.2k
                    if (imaxy < scanlines) {
760
#ifdef DEBUG_SCAN_CONVERTER
761
                        if (debugging_scan_converter)
762
                            dlprintf1("-%x ", imaxy+1);
763
#endif
764
44.5k
                        index[imaxy+1+adjust]--;
765
44.5k
                    }
766
70.2k
                }
767
87.0k
            }
768
#ifdef DEBUG_SCAN_CONVERTER
769
            if (debugging_scan_converter)
770
                dlprintf("\n");
771
#endif
772
112k
        }
773
#ifdef DEBUG_SCAN_CONVERTER
774
        if (debugging_scan_converter)
775
            dlprintf("\n");
776
#endif
777
2.71M
        psub = (const subpath *)pseg;
778
2.71M
    }
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
2.48M
    offset = 0;
787
2.48M
    delta  = 0;
788
71.0M
    for (i=0; i < scanlines+adjust; i++) {
789
68.6M
        delta    += intersection_size*index[i];  /* delta = Num ints on this scanline. */
790
68.6M
        index[i]  = offset;                      /* Offset into table for this lines data. */
791
68.6M
        offset   += delta+1;                     /* Adjust offset for next line. */
792
68.6M
    }
793
    /* Ensure we always have enough room for our zero height rectangle hack. */
794
2.48M
    if (offset < 2*intersection_size)
795
10
        offset += 2*intersection_size;
796
2.48M
    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
2.48M
    if (scanlines > 16 && offset > 1024*1024) { /* Arbitrary */
803
1
        gs_free_object(pdev->memory, index, "scanc index buffer");
804
1
        return offset/(1024*1024) + 1;
805
1
    }
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
2.48M
    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
2.48M
    table = (int *)gs_alloc_bytes(pdev->memory, offset,
821
2.48M
                                  "scanc intersects buffer");
822
2.48M
    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
71.0M
    for (i=0; i < scanlines; i++) {
831
68.6M
        table[index[i]] = 0;
832
68.6M
    }
833
834
2.48M
    *scanlinesp = scanlines;
835
2.48M
    *tablep     = table;
836
2.48M
    *indexp     = index;
837
838
2.48M
    return 0;
839
2.48M
}
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
4.11k
{
848
4.11k
    return make_table_template(pdev, path, ibox, 1, 1, scanlines, index, table);
849
4.11k
}
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
4.61k
{
865
4.61k
    gs_fixed_rect  ibox;
866
4.61k
    gs_fixed_rect  bbox;
867
4.61k
    int            scanlines;
868
4.61k
    const subpath *psub;
869
4.61k
    int           *index;
870
4.61k
    int           *table;
871
4.61k
    int            i;
872
4.61k
    int            code;
873
4.61k
    int            zero;
874
875
4.61k
    edgebuffer->index = NULL;
876
4.61k
    edgebuffer->table = NULL;
877
878
    /* Bale out if no actual path. We see this with the clist */
879
4.61k
    if (path->first_subpath == NULL)
880
488
        return 0;
881
882
4.12k
    zero = make_bbox(path, clip, &bbox, &ibox, fixed_half);
883
4.12k
    if (zero < 0)
884
0
        return zero;
885
886
4.12k
    if (ibox.q.y <= ibox.p.y)
887
10
        return 0;
888
889
4.11k
    code = make_table(pdev, path, &ibox, &scanlines, &index, &table);
890
4.11k
    if (code != 0) /* >0 means "retry with smaller height" */
891
0
        return code;
892
893
4.11k
    if (scanlines == 0)
894
0
        return 0;
895
896
4.11k
    if (zero) {
897
0
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero);
898
4.11k
    } else {
899
900
    /* Step 2 continued: Now we run through the path, filling in the real
901
     * values. */
902
14.4k
    for (psub = path->first_subpath; psub != 0;) {
903
10.3k
        const segment *pseg = (const segment *)psub;
904
10.3k
        fixed ex = pseg->pt.x;
905
10.3k
        fixed ey = pseg->pt.y;
906
10.3k
        fixed ix = ex;
907
10.3k
        fixed iy = ey;
908
909
2.71M
        while ((pseg = pseg->next) != 0 &&
910
2.71M
               pseg->type != s_start
911
2.70M
            ) {
912
2.70M
            fixed sx = ex;
913
2.70M
            fixed sy = ey;
914
2.70M
            ex = pseg->pt.x;
915
2.70M
            ey = pseg->pt.y;
916
917
2.70M
            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
2.69M
                case s_line:
932
2.70M
                case s_line_close:
933
2.70M
                    if (sy != ey)
934
2.39M
                        mark_line(sx, sy, ex, ey, ibox.p.y, scanlines, table, index);
935
2.70M
                    break;
936
2.70M
            }
937
2.70M
        }
938
        /* And close any open segments */
939
10.3k
        if (iy != ey)
940
2.64k
            mark_line(ex, ey, ix, iy, ibox.p.y, scanlines, table, index);
941
10.3k
        psub = (const subpath *)pseg;
942
10.3k
    }
943
4.11k
    }
944
945
    /* Step 2 complete: We now have a complete list of intersection data in
946
     * table, indexed by index. */
947
948
4.11k
    edgebuffer->base   = ibox.p.y;
949
4.11k
    edgebuffer->height = scanlines;
950
4.11k
    edgebuffer->xmin   = ibox.p.x;
951
4.11k
    edgebuffer->xmax   = ibox.q.x;
952
4.11k
    edgebuffer->index  = index;
953
4.11k
    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
567k
    for (i=0; i < scanlines; i++) {
964
562k
        int *row = &table[index[i]];
965
562k
        int  rowlen = *row++;
966
967
        /* Bubblesort short runs, qsort longer ones. */
968
        /* FIXME: Check "6" below */
969
562k
        if (rowlen <= 6) {
970
542k
            int j, k;
971
1.67M
            for (j = 0; j < rowlen-1; j++) {
972
1.13M
                int t = row[j];
973
3.25M
                for (k = j+1; k < rowlen; k++) {
974
2.12M
                    int s = row[k];
975
2.12M
                    if (t > s)
976
1.05M
                         row[k] = t, t = row[j] = s;
977
2.12M
                }
978
1.13M
            }
979
542k
        } else
980
20.9k
            qsort(row, rowlen, sizeof(int), intcmp);
981
562k
    }
982
983
4.11k
    return 0;
984
4.11k
}
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
4.61k
{
992
4.61k
    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
567k
    for (i=0; i < edgebuffer->height; i++) {
1002
562k
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
1003
562k
        int *rowstart = row;
1004
562k
        int  rowlen   = *row++;
1005
562k
        int *rowout   = row;
1006
1007
1.45M
        while (rowlen > 0)
1008
895k
        {
1009
895k
            int left, right;
1010
1011
895k
            if (rule == gx_rule_even_odd) {
1012
                /* Even Odd */
1013
0
                left  = (*row++)&~1;
1014
0
                right = (*row++)&~1;
1015
0
                rowlen -= 2;
1016
895k
            } else {
1017
                /* Non-Zero */
1018
895k
                int w;
1019
1020
895k
                left = *row++;
1021
895k
                w = ((left&1)-1) | (left&1);
1022
895k
                rowlen--;
1023
961k
                do {
1024
961k
                    right  = *row++;
1025
961k
                    rowlen--;
1026
961k
                    w += ((right&1)-1) | (right&1);
1027
961k
                } while (w != 0);
1028
895k
                left &= ~1;
1029
895k
                right &= ~1;
1030
895k
            }
1031
1032
895k
            if (right > left) {
1033
867k
                *rowout++ = left;
1034
867k
                *rowout++ = right;
1035
867k
            }
1036
895k
        }
1037
562k
        *rowstart = (rowout-rowstart)-1;
1038
562k
    }
1039
4.61k
    return 0;
1040
4.61k
}
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
4.61k
{
1049
4.61k
    int i, code;
1050
1051
567k
    for (i=0; i < edgebuffer->height; i++) {
1052
562k
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
1053
562k
        int  rowlen = *row++;
1054
1055
1.43M
        while (rowlen > 0) {
1056
867k
            int left, right;
1057
1058
867k
            left  = *row++;
1059
867k
            right = *row++;
1060
867k
            rowlen -= 2;
1061
867k
            left  = fixed2int(left + fixed_half);
1062
867k
            right = fixed2int(right + fixed_half);
1063
867k
            right -= left;
1064
867k
            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
731k
                if (log_op < 0)
1074
722k
                    code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
1075
8.89k
                else
1076
8.89k
                    code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
1077
731k
                if (code < 0)
1078
0
                    return code;
1079
731k
            }
1080
867k
        }
1081
562k
    }
1082
4.61k
    return 0;
1083
4.61k
}
1084
1085
/* Any part of a pixel routines */
1086
1087
static int edgecmp(const void *a, const void *b)
1088
90.7k
{
1089
90.7k
    int left  = ((int*)a)[0];
1090
90.7k
    int right = ((int*)b)[0];
1091
90.7k
    left -= right;
1092
90.7k
    if (left)
1093
90.0k
        return left;
1094
781
    return ((int*)a)[1] - ((int*)b)[1];
1095
90.7k
}
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
1.23M
{
1159
1.23M
    int *row;
1160
1.23M
    int count;
1161
1162
1.23M
    if (iy >= 0 && iy < cr->scanlines) {
1163
1.18M
        if (cr->first) {
1164
            /* Save it for later in case we join up */
1165
106k
            cr->save_left  = cr->left;
1166
106k
            cr->save_right = cr->right;
1167
106k
            cr->save_iy    = iy;
1168
106k
            cr->save_d     = cr->d;
1169
106k
            cr->saved      = 1;
1170
1.07M
        } else if (cr->d != DIRN_UNSET) {
1171
            /* Enter it into the table */
1172
1.07M
            row = &cr->table[cr->index[iy]];
1173
1.07M
            *row = count = (*row)+1; /* Increment the count */
1174
1.07M
            row[2 * count - 1] = (cr->left&~1) | cr->d;
1175
1.07M
            row[2 * count    ] = cr->right;
1176
1.07M
        } else {
1177
589
            assert(cr->left == max_fixed && cr->right == min_fixed);
1178
589
        }
1179
1.18M
    }
1180
1.23M
    cr->first = 0;
1181
1.23M
}
1182
1183
static inline void
1184
cursor_output_inrange(cursor * gs_restrict cr, int iy)
1185
370k
{
1186
370k
    int *row;
1187
370k
    int count;
1188
1189
370k
    assert(iy >= 0 && iy < cr->scanlines);
1190
370k
    if (cr->first) {
1191
        /* Save it for later in case we join up */
1192
396
        cr->save_left  = cr->left;
1193
396
        cr->save_right = cr->right;
1194
396
        cr->save_iy    = iy;
1195
396
        cr->save_d     = cr->d;
1196
396
        cr->saved      = 1;
1197
370k
    } else {
1198
        /* Enter it into the table */
1199
370k
        assert(cr->d != DIRN_UNSET);
1200
1201
370k
        row = &cr->table[cr->index[iy]];
1202
370k
        *row = count = (*row)+1; /* Increment the count */
1203
370k
        row[2 * count - 1] = (cr->left&~1) | cr->d;
1204
370k
        row[2 * count    ] = cr->right;
1205
370k
    }
1206
370k
    cr->first = 0;
1207
370k
}
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
462k
{
1213
462k
    int new_iy;
1214
462k
    int iy = fixed2int(cr->y) - cr->base;
1215
1216
462k
    cr->y += dy;
1217
462k
    new_iy = fixed2int(cr->y) - cr->base;
1218
462k
    if (new_iy != iy) {
1219
462k
        if (!skip)
1220
456k
            cursor_output(cr, iy);
1221
462k
        cr->left = x;
1222
462k
        cr->right = x;
1223
462k
    } 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
462k
}
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
6.67k
{
1235
6.67k
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
1236
1237
6.67k
    cr->y += dy;
1238
6.67k
}
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
109k
{
1246
109k
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
1247
1248
109k
    if (x < cr->left)
1249
75.8k
        cr->left = x;
1250
109k
    cr->y += dy;
1251
109k
}
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
110k
{
1259
110k
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
1260
1261
110k
    if (x > cr->right)
1262
107k
        cr->right = x;
1263
110k
    cr->y += dy;
1264
110k
}
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
249k
{
1270
249k
    int iy = fixed2int(cr->y) - cr->base;
1271
1272
249k
    if (!skip)
1273
231k
        cursor_output(cr, iy);
1274
249k
    cr->y += dy;
1275
249k
    cr->left = x;
1276
249k
    cr->right = x;
1277
249k
}
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
173k
{
1285
173k
    int iy = fixed2int(cr->y) - cr->base;
1286
1287
173k
    cursor_output(cr, iy);
1288
173k
    cr->y += dy;
1289
173k
}
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
188k
{
1297
188k
    int iy = fixed2int(cr->y) - cr->base;
1298
1299
188k
    cr->y += dy;
1300
188k
    cursor_output_inrange(cr, iy);
1301
188k
    cr->right = x;
1302
188k
}
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
182k
{
1310
182k
    int iy = fixed2int(cr->y) - cr->base;
1311
1312
182k
    cr->y += dy;
1313
182k
    cursor_output_inrange(cr, iy);
1314
182k
    cr->left = x;
1315
182k
}
1316
1317
static inline void cursor_init(cursor * gs_restrict cr, fixed y, fixed x)
1318
0
{
1319
0
    assert(y >= int2fixed(cr->base) && y <= int2fixed(cr->base + cr->scanlines));
1320
0
1321
0
    cr->y = y;
1322
0
    cr->left = x;
1323
0
    cr->right = x;
1324
0
    cr->d = DIRN_UNSET;
1325
0
}
1326
1327
static inline void cursor_left_merge(cursor * gs_restrict cr, fixed x)
1328
1.42M
{
1329
1.42M
    if (x < cr->left)
1330
422k
        cr->left = x;
1331
1.42M
}
1332
1333
static inline void cursor_left(cursor * gs_restrict cr, fixed x)
1334
468k
{
1335
468k
    cr->left = x;
1336
468k
}
1337
1338
static inline void cursor_right_merge(cursor * gs_restrict cr, fixed x)
1339
1.44M
{
1340
1.44M
    if (x > cr->right)
1341
375k
        cr->right = x;
1342
1.44M
}
1343
1344
static inline void cursor_right(cursor * gs_restrict cr, fixed x)
1345
464k
{
1346
464k
    cr->right = x;
1347
464k
}
1348
1349
static inline int cursor_down(cursor * gs_restrict cr, fixed x)
1350
477k
{
1351
477k
    int skip = 0;
1352
477k
    if ((cr->y & 0xff) == 0)
1353
23.3k
        skip = 1;
1354
477k
    if (cr->d == DIRN_UP)
1355
81.1k
    {
1356
81.1k
        if (!skip)
1357
81.1k
            cursor_output(cr, fixed2int(cr->y) - cr->base);
1358
81.1k
        cr->left = x;
1359
81.1k
        cr->right = x;
1360
81.1k
    }
1361
477k
    cr->d = DIRN_DOWN;
1362
477k
    return skip;
1363
477k
}
1364
1365
static inline void cursor_up(cursor * gs_restrict cr, fixed x)
1366
479k
{
1367
479k
    if (cr->d == DIRN_DOWN)
1368
81.5k
    {
1369
81.5k
        cursor_output(cr, fixed2int(cr->y) - cr->base);
1370
81.5k
        cr->left = x;
1371
81.5k
        cr->right = x;
1372
81.5k
    }
1373
479k
    cr->d = DIRN_UP;
1374
479k
}
1375
1376
static inline void
1377
cursor_flush(cursor * gs_restrict cr, fixed x)
1378
151k
{
1379
151k
    int iy;
1380
1381
    /* This should only happen if we were entirely out of bounds,
1382
     * or if everything was within a zero height horizontal
1383
     * rectangle from the start point. */
1384
151k
    if (cr->first) {
1385
9
        int iy = fixed2int(cr->y) - cr->base;
1386
        /* Any zero height rectangle counts as filled, except
1387
         * those on the baseline of a pixel. */
1388
9
        if (cr->d == DIRN_UNSET && (cr->y & 0xff) == 0)
1389
0
            return;
1390
9
        assert(cr->left != max_fixed && cr->right != min_fixed);
1391
9
        if (iy >= 0 && iy < cr->scanlines) {
1392
9
            int *row = &cr->table[cr->index[iy]];
1393
9
            int count = *row = (*row)+2; /* Increment the count */
1394
9
            row[2 * count - 3] = (cr->left & ~1) | DIRN_UP;
1395
9
            row[2 * count - 2] = (cr->right & ~1);
1396
9
            row[2 * count - 1] = (cr->right & ~1) | DIRN_DOWN;
1397
9
            row[2 * count    ] = cr->right;
1398
9
        }
1399
9
        return;
1400
9
    }
1401
1402
    /* Merge save into current if we can */
1403
151k
    iy = fixed2int(cr->y) - cr->base;
1404
151k
    if (cr->saved && iy == cr->save_iy &&
1405
151k
        (cr->d == cr->save_d || cr->save_d == DIRN_UNSET)) {
1406
51.6k
        if (cr->left > cr->save_left)
1407
15.4k
            cr->left = cr->save_left;
1408
51.6k
        if (cr->right < cr->save_right)
1409
14.3k
            cr->right = cr->save_right;
1410
51.6k
        cursor_output(cr, iy);
1411
51.6k
        return;
1412
51.6k
    }
1413
1414
    /* Merge not possible */
1415
99.9k
    cursor_output(cr, iy);
1416
99.9k
    if (cr->saved) {
1417
55.2k
        cr->left  = cr->save_left;
1418
55.2k
        cr->right = cr->save_right;
1419
55.2k
        assert(cr->save_d != DIRN_UNSET);
1420
55.2k
        if (cr->save_d != DIRN_UNSET)
1421
55.2k
            cr->d = cr->save_d;
1422
55.2k
        cursor_output(cr, cr->save_iy);
1423
55.2k
    }
1424
99.9k
}
1425
1426
static inline void
1427
cursor_null(cursor *cr)
1428
92.0k
{
1429
92.0k
    cr->right = min_fixed;
1430
92.0k
    cr->left  = max_fixed;
1431
92.0k
    cr->d     = DIRN_UNSET;
1432
92.0k
}
1433
1434
static void mark_line_app(cursor * gs_restrict cr, fixed sx, fixed sy, fixed ex, fixed ey)
1435
1.68M
{
1436
1.68M
    int isy, iey;
1437
1.68M
    fixed saved_sy = sy;
1438
1.68M
    fixed saved_ex = ex;
1439
1.68M
    fixed saved_ey = ey;
1440
1.68M
    int truncated;
1441
1442
1.68M
    if (sx == ex && sy == ey)
1443
176k
        return;
1444
1445
1.50M
    isy = fixed2int(sy) - cr->base;
1446
1.50M
    iey = fixed2int(ey) - cr->base;
1447
#ifdef DEBUG_SCAN_CONVERTER
1448
    if (debugging_scan_converter)
1449
        dlprintf6("Marking line (app) from %x,%x to %x,%x (%x,%x)\n", sx, sy, ex, ey, isy, iey);
1450
#endif
1451
#ifdef DEBUG_OUTPUT_SC_AS_PS
1452
    dlprintf("0.001 setlinewidth 0 0 0 setrgbcolor %%PS\n");
1453
    coord("moveto", sx, sy);
1454
    coord("lineto", ex, ey);
1455
    dlprintf("stroke %%PS\n");
1456
#endif
1457
1458
    /* Horizontal motion at the bottom of a pixel is ignored */
1459
1.50M
    if (sy == ey && (sy & 0xff) == 0)
1460
4.82k
        return;
1461
1462
1.50M
    assert(cr->y == sy &&
1463
1.50M
           ((cr->left <= sx && cr->right >= sx) || ((sy & 0xff) == 0)) &&
1464
1.50M
           cr->d >= DIRN_UNSET && cr->d <= DIRN_DOWN);
1465
1466
1.50M
    if (isy < iey) {
1467
        /* Rising line */
1468
432k
        if (iey < 0 || isy >= cr->scanlines) {
1469
            /* All line is outside. */
1470
180k
            if ((ey & 0xff) == 0)
1471
3.81k
                cursor_null(cr);
1472
176k
            else {
1473
176k
                cr->left = ex;
1474
176k
                cr->right = ex;
1475
176k
            }
1476
180k
            cr->y = ey;
1477
180k
            cr->first = 0;
1478
180k
            return;
1479
180k
        }
1480
252k
        if (isy < 0) {
1481
            /* Move sy up */
1482
20.3k
            int64_t y = (int64_t)ey - (int64_t)sy;
1483
20.3k
            fixed new_sy = int2fixed(cr->base);
1484
20.3k
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
1485
20.3k
            sx += (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1486
20.3k
            sy = new_sy;
1487
20.3k
            cursor_null(cr);
1488
20.3k
            cr->y = sy;
1489
20.3k
            isy = 0;
1490
20.3k
        }
1491
252k
        truncated = iey > cr->scanlines;
1492
252k
        if (truncated) {
1493
            /* Move ey down */
1494
16.9k
            int64_t y = ey - sy;
1495
16.9k
            fixed new_ey = int2fixed(cr->base + cr->scanlines);
1496
16.9k
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
1497
16.9k
            saved_ex = ex;
1498
16.9k
            saved_ey = ey;
1499
16.9k
            ex -= (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1500
16.9k
            ey = new_ey;
1501
16.9k
            iey = cr->scanlines;
1502
16.9k
        }
1503
1.06M
    } else {
1504
        /* Falling line */
1505
1.06M
        if (isy < 0 || iey >= cr->scanlines) {
1506
            /* All line is outside. */
1507
309k
            if ((ey & 0xff) == 0)
1508
5.01k
                cursor_null(cr);
1509
304k
            else {
1510
304k
                cr->left = ex;
1511
304k
                cr->right = ex;
1512
304k
            }
1513
309k
            cr->y = ey;
1514
309k
            cr->first = 0;
1515
309k
            return;
1516
309k
        }
1517
758k
        truncated = iey < 0;
1518
758k
        if (truncated) {
1519
            /* Move ey up */
1520
20.3k
            int64_t y = (int64_t)ey - (int64_t)sy;
1521
20.3k
            fixed new_ey = int2fixed(cr->base);
1522
20.3k
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
1523
20.3k
            ex -= (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1524
20.3k
            ey = new_ey;
1525
20.3k
            iey = 0;
1526
20.3k
        }
1527
758k
        if (isy >= cr->scanlines) {
1528
            /* Move sy down */
1529
21.1k
            int64_t y = (int64_t)ey - (int64_t)sy;
1530
21.1k
            fixed new_sy = int2fixed(cr->base + cr->scanlines);
1531
21.1k
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
1532
21.1k
            sx += (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1533
21.1k
            sy = new_sy;
1534
21.1k
            cursor_null(cr);
1535
21.1k
            cr->y = sy;
1536
21.1k
            isy = cr->scanlines;
1537
21.1k
        }
1538
758k
    }
1539
1540
1.01M
    cursor_left_merge(cr, sx);
1541
1.01M
    cursor_right_merge(cr, sx);
1542
1543
1.01M
    assert(cr->left <= sx);
1544
1.01M
    assert(cr->right >= sx);
1545
1.01M
    assert(cr->y == sy);
1546
1547
    /* A note: The code below used to be of the form:
1548
     *   if (isy == iey)   ... deal with horizontal lines
1549
     *   else if (ey > sy) {
1550
     *     fixed y_steps = ey - sy;
1551
     *      ... deal with rising lines ...
1552
     *   } else {
1553
     *     fixed y_steps = ey - sy;
1554
     *     ... deal with falling lines
1555
     *   }
1556
     * but that lead to problems, for instance, an example seen
1557
     * has sx=2aa8e, sy=8aee7, ex=7ffc1686, ey=8003e97a.
1558
     * Thus isy=84f, iey=ff80038a. We can see that ey < sy, but
1559
     * sy - ey < 0!
1560
     * We therefore rejig our code so that the choice between
1561
     * cases is done based on the sign of y_steps rather than
1562
     * the relative size of ey and sy.
1563
     */
1564
1565
    /* First, deal with lines that don't change scanline.
1566
     * This accommodates horizontal lines. */
1567
1.01M
    if (isy == iey) {
1568
511k
        if (saved_sy == saved_ey) {
1569
            /* Horizontal line. Don't change cr->d, don't flush. */
1570
52.9k
            if ((ey & 0xff) == 0)
1571
0
                goto no_merge;
1572
458k
        } else if (saved_sy > saved_ey) {
1573
            /* Falling line, flush if previous was rising */
1574
228k
            int skip = cursor_down(cr, sx);
1575
228k
            if ((ey & 0xff) == 0) {
1576
                /* We are falling to the baseline of a subpixel, so output
1577
                 * for the current pixel, and leave the cursor nulled. */
1578
4.33k
                if (sx <= ex) {
1579
2.56k
                    cursor_right_merge(cr, ex);
1580
2.56k
                } else {
1581
1.76k
                    cursor_left_merge(cr, ex);
1582
1.76k
                }
1583
4.33k
                if (!skip)
1584
4.29k
                    cursor_output(cr, fixed2int(cr->y) - cr->base);
1585
4.33k
                cursor_null(cr);
1586
4.33k
                goto no_merge;
1587
4.33k
            }
1588
229k
        } else {
1589
            /* Rising line, flush if previous was falling */
1590
229k
            cursor_up(cr, sx);
1591
229k
            if ((ey & 0xff) == 0) {
1592
29
                cursor_null(cr);
1593
29
                goto no_merge;
1594
29
            }
1595
229k
        }
1596
507k
        if (sx <= ex) {
1597
263k
            cursor_right_merge(cr, ex);
1598
263k
        } else {
1599
243k
            cursor_left_merge(cr, ex);
1600
243k
        }
1601
511k
no_merge:
1602
511k
        cr->y = ey;
1603
511k
        if (sy > saved_ey)
1604
228k
            goto endFalling;
1605
511k
    } else if (iey > isy) {
1606
        /* We want to change from sy to ey, which are guaranteed to be on
1607
         * different scanlines. We do this in 3 phases.
1608
         * Phase 1 gets us from sy to the next scanline boundary.
1609
         * Phase 2 gets us all the way to the last scanline boundary.
1610
         * Phase 3 gets us from the last scanline boundary to ey.
1611
         */
1612
        /* We want to change from sy to ey, which are guaranteed to be on
1613
         * different scanlines. We do this in 3 phases.
1614
         * Phase 1 gets us from sy to the next scanline boundary. (We may exit after phase 1).
1615
         * Phase 2 gets us all the way to the last scanline boundary. (This may be a null operation)
1616
         * 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).
1617
         */
1618
249k
        int phase1_y_steps = (-sy) & (fixed_1 - 1);
1619
249k
        int phase3_y_steps = ey & (fixed_1 - 1);
1620
249k
        ufixed y_steps = (ufixed)ey - (ufixed)sy;
1621
1622
249k
        cursor_up(cr, sx);
1623
1624
249k
        if (sx == ex) {
1625
            /* Vertical line. (Rising) */
1626
1627
            /* Phase 1: */
1628
12.8k
            if (phase1_y_steps) {
1629
                /* If phase 1 will move us into a new scanline, then we must
1630
                 * flush it before we move. */
1631
7.25k
                cursor_step(cr, phase1_y_steps, sx, 0);
1632
7.25k
                sy += phase1_y_steps;
1633
7.25k
                y_steps -= phase1_y_steps;
1634
7.25k
                if (y_steps == 0) {
1635
57
                    cursor_null(cr);
1636
57
                    goto end;
1637
57
                }
1638
7.25k
            }
1639
1640
            /* Phase 3: precalculation */
1641
12.8k
            y_steps -= phase3_y_steps;
1642
1643
            /* Phase 2: */
1644
12.8k
            y_steps = fixed2int(y_steps);
1645
12.8k
            assert(y_steps >= 0);
1646
12.8k
            if (y_steps > 0) {
1647
9.15k
                cursor_always_step(cr, fixed_1, sx, 0);
1648
9.15k
                y_steps--;
1649
86.7k
                while (y_steps) {
1650
77.6k
                    cursor_always_step_inrange_vertical(cr, fixed_1, sx);
1651
77.6k
                    y_steps--;
1652
77.6k
                }
1653
9.15k
            }
1654
1655
            /* Phase 3 */
1656
12.8k
            assert(cr->left == sx && cr->right == sx);
1657
12.8k
            if (phase3_y_steps == 0)
1658
5.23k
                cursor_null(cr);
1659
7.60k
            else
1660
7.60k
                cr->y += phase3_y_steps;
1661
236k
        } else if (sx < ex) {
1662
            /* Lines increasing in x. (Rightwards, rising) */
1663
117k
            int phase1_x_steps, phase3_x_steps;
1664
117k
            fixed x_steps = ex - sx;
1665
1666
            /* Phase 1: */
1667
117k
            if (phase1_y_steps) {
1668
111k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1669
111k
                sx += phase1_x_steps;
1670
111k
                cursor_right_merge(cr, sx);
1671
111k
                x_steps -= phase1_x_steps;
1672
111k
                cursor_step(cr, phase1_y_steps, sx, 0);
1673
111k
                sy += phase1_y_steps;
1674
111k
                y_steps -= phase1_y_steps;
1675
111k
                if (y_steps == 0) {
1676
1.56k
                    cursor_null(cr);
1677
1.56k
                    goto end;
1678
1.56k
                }
1679
111k
            }
1680
1681
            /* Phase 3: precalculation */
1682
115k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1683
115k
            x_steps -= phase3_x_steps;
1684
115k
            y_steps -= phase3_y_steps;
1685
115k
            assert((y_steps & (fixed_1 - 1)) == 0);
1686
1687
            /* Phase 2: */
1688
115k
            y_steps = fixed2int(y_steps);
1689
115k
            assert(y_steps >= 0);
1690
115k
            if (y_steps) {
1691
                /* We want to change sx by x_steps in y_steps steps.
1692
                 * So each step, we add x_steps/y_steps to sx. That's x_inc + n_inc/y_steps. */
1693
57.3k
                int x_inc = x_steps/y_steps;
1694
57.3k
                int n_inc = x_steps - (x_inc * y_steps);
1695
57.3k
                int f = y_steps/2;
1696
57.3k
                int d = y_steps;
1697
1698
                /* Special casing the first iteration, allows us to simplify
1699
                 * the following loop. */
1700
57.3k
                sx += x_inc;
1701
57.3k
                f -= n_inc;
1702
57.3k
                if (f < 0)
1703
6.17k
                    f += d, sx++;
1704
57.3k
                cursor_right_merge(cr, sx);
1705
57.3k
                cursor_always_step(cr, fixed_1, sx, 0);
1706
57.3k
                y_steps--;
1707
1708
144k
                while (y_steps) {
1709
87.1k
                    sx += x_inc;
1710
87.1k
                    f -= n_inc;
1711
87.1k
                    if (f < 0)
1712
38.7k
                        f += d, sx++;
1713
87.1k
                    cursor_right(cr, sx);
1714
87.1k
                    cursor_always_inrange_step_right(cr, fixed_1, sx);
1715
87.1k
                    y_steps--;
1716
87.1k
                };
1717
57.3k
            }
1718
1719
            /* Phase 3 */
1720
115k
            assert(cr->left <= ex && cr->right >= sx);
1721
115k
            if (phase3_y_steps == 0)
1722
4.83k
                cursor_null(cr);
1723
110k
            else {
1724
110k
                cursor_right(cr, ex);
1725
110k
                cr->y += phase3_y_steps;
1726
110k
            }
1727
119k
        } else {
1728
            /* Lines decreasing in x. (Leftwards, rising) */
1729
119k
            int phase1_x_steps, phase3_x_steps;
1730
119k
            fixed x_steps = sx - ex;
1731
1732
            /* Phase 1: */
1733
119k
            if (phase1_y_steps) {
1734
112k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1735
112k
                x_steps -= phase1_x_steps;
1736
112k
                sx -= phase1_x_steps;
1737
112k
                cursor_left_merge(cr, sx);
1738
112k
                cursor_step(cr, phase1_y_steps, sx, 0);
1739
112k
                sy += phase1_y_steps;
1740
112k
                y_steps -= phase1_y_steps;
1741
112k
                if (y_steps == 0) {
1742
1.45k
                    cursor_null(cr);
1743
1.45k
                    goto end;
1744
1.45k
                }
1745
112k
            }
1746
1747
            /* Phase 3: precalculation */
1748
117k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1749
117k
            x_steps -= phase3_x_steps;
1750
117k
            y_steps -= phase3_y_steps;
1751
117k
            assert((y_steps & (fixed_1 - 1)) == 0);
1752
1753
            /* Phase 2: */
1754
117k
            y_steps = fixed2int(y_steps);
1755
117k
            assert(y_steps >= 0);
1756
117k
            if (y_steps) {
1757
                /* We want to change sx by x_steps in y_steps steps.
1758
                 * So each step, we sub x_steps/y_steps from sx. That's x_inc + n_inc/ey. */
1759
59.0k
                int x_inc = x_steps/y_steps;
1760
59.0k
                int n_inc = x_steps - (x_inc * y_steps);
1761
59.0k
                int f = y_steps/2;
1762
59.0k
                int d = y_steps;
1763
1764
                /* Special casing the first iteration, allows us to simplify
1765
                 * the following loop. */
1766
59.0k
                sx -= x_inc;
1767
59.0k
                f -= n_inc;
1768
59.0k
                if (f < 0)
1769
6.69k
                    f += d, sx--;
1770
59.0k
                cursor_left_merge(cr, sx);
1771
59.0k
                cursor_always_step(cr, fixed_1, sx, 0);
1772
59.0k
                y_steps--;
1773
1774
156k
                while (y_steps) {
1775
97.4k
                    sx -= x_inc;
1776
97.4k
                    f -= n_inc;
1777
97.4k
                    if (f < 0)
1778
41.8k
                        f += d, sx--;
1779
97.4k
                    cursor_left(cr, sx);
1780
97.4k
                    cursor_always_inrange_step_left(cr, fixed_1, sx);
1781
97.4k
                    y_steps--;
1782
97.4k
                }
1783
59.0k
            }
1784
1785
            /* Phase 3 */
1786
117k
            assert(cr->right >= ex && cr->left <= sx);
1787
117k
            if (phase3_y_steps == 0)
1788
5.97k
                cursor_null(cr);
1789
111k
            else {
1790
111k
                cursor_left(cr, ex);
1791
111k
                cr->y += phase3_y_steps;
1792
111k
            }
1793
117k
        }
1794
249k
    } else {
1795
        /* So lines decreasing in y. */
1796
        /* We want to change from sy to ey, which are guaranteed to be on
1797
         * different scanlines. We do this in 3 phases.
1798
         * Phase 1 gets us from sy to the next scanline boundary. This never causes an output.
1799
         * Phase 2 gets us all the way to the last scanline boundary. This is guaranteed to cause an output.
1800
         * Phase 3 gets us from the last scanline boundary to ey. We are guaranteed to have outputted by now.
1801
         */
1802
249k
        int phase1_y_steps = sy & (fixed_1 - 1);
1803
249k
        int phase3_y_steps = (-ey) & (fixed_1 - 1);
1804
249k
        ufixed y_steps = (ufixed)sy - (ufixed)ey;
1805
1806
249k
        int skip = cursor_down(cr, sx);
1807
1808
249k
        if (sx == ex) {
1809
            /* Vertical line. (Falling) */
1810
1811
            /* Phase 1: */
1812
12.6k
            if (phase1_y_steps) {
1813
                /* Phase 1 in a falling line never moves us into a new scanline. */
1814
6.67k
                cursor_never_step_vertical(cr, -phase1_y_steps, sx);
1815
6.67k
                sy -= phase1_y_steps;
1816
6.67k
                y_steps -= phase1_y_steps;
1817
6.67k
                if (y_steps == 0)
1818
0
                    goto endFallingLeftOnEdgeOfPixel;
1819
6.67k
            }
1820
1821
            /* Phase 3: precalculation */
1822
12.6k
            y_steps -= phase3_y_steps;
1823
12.6k
            assert((y_steps & (fixed_1 - 1)) == 0);
1824
1825
            /* Phase 2: */
1826
12.6k
            y_steps = fixed2int(y_steps);
1827
12.6k
            assert(y_steps >= 0);
1828
12.6k
            if (y_steps) {
1829
9.06k
                cursor_always_step(cr, -fixed_1, sx, skip);
1830
9.06k
                skip = 0;
1831
9.06k
                y_steps--;
1832
86.5k
                while (y_steps) {
1833
77.4k
                    cursor_always_step_inrange_vertical(cr, -fixed_1, sx);
1834
77.4k
                    y_steps--;
1835
77.4k
                }
1836
9.06k
            }
1837
1838
            /* Phase 3 */
1839
12.6k
            if (phase3_y_steps == 0) {
1840
5.69k
endFallingLeftOnEdgeOfPixel:
1841
5.69k
                cursor_always_step_inrange_vertical(cr, 0, sx);
1842
5.69k
                cursor_null(cr);
1843
6.97k
            } else {
1844
6.97k
                cursor_step(cr, -phase3_y_steps, sx, skip);
1845
6.97k
                assert(cr->left == sx && cr->right == sx);
1846
6.97k
            }
1847
236k
        } else if (sx < ex) {
1848
            /* Lines increasing in x. (Rightwards, falling) */
1849
118k
            int phase1_x_steps, phase3_x_steps;
1850
118k
            fixed x_steps = ex - sx;
1851
1852
            /* Phase 1: */
1853
118k
            if (phase1_y_steps) {
1854
110k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1855
110k
                x_steps -= phase1_x_steps;
1856
110k
                sx += phase1_x_steps;
1857
                /* Phase 1 in a falling line never moves us into a new scanline. */
1858
110k
                cursor_never_step_right(cr, -phase1_y_steps, sx);
1859
110k
                sy -= phase1_y_steps;
1860
110k
                y_steps -= phase1_y_steps;
1861
110k
                if (y_steps == 0)
1862
0
                    goto endFallingRightOnEdgeOfPixel;
1863
110k
            }
1864
1865
            /* Phase 3: precalculation */
1866
118k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1867
118k
            x_steps -= phase3_x_steps;
1868
118k
            y_steps -= phase3_y_steps;
1869
118k
            assert((y_steps & (fixed_1 - 1)) == 0);
1870
1871
            /* Phase 2: */
1872
118k
            y_steps = fixed2int(y_steps);
1873
118k
            assert(y_steps >= 0);
1874
118k
            if (y_steps) {
1875
                /* We want to change sx by x_steps in y_steps steps.
1876
                 * So each step, we add x_steps/y_steps to sx. That's x_inc + n_inc/ey. */
1877
58.6k
                int x_inc = x_steps/y_steps;
1878
58.6k
                int n_inc = x_steps - (x_inc * y_steps);
1879
58.6k
                int f = y_steps/2;
1880
58.6k
                int d = y_steps;
1881
1882
58.6k
                cursor_always_step(cr, -fixed_1, sx, skip);
1883
58.6k
                skip = 0;
1884
58.6k
                sx += x_inc;
1885
58.6k
                f -= n_inc;
1886
58.6k
                if (f < 0)
1887
6.59k
                    f += d, sx++;
1888
58.6k
                cursor_right(cr, sx);
1889
58.6k
                y_steps--;
1890
1891
154k
                while (y_steps) {
1892
95.3k
                    cursor_always_inrange_step_right(cr, -fixed_1, sx);
1893
95.3k
                    sx += x_inc;
1894
95.3k
                    f -= n_inc;
1895
95.3k
                    if (f < 0)
1896
38.7k
                        f += d, sx++;
1897
95.3k
                    cursor_right(cr, sx);
1898
95.3k
                    y_steps--;
1899
95.3k
                }
1900
58.6k
            }
1901
1902
            /* Phase 3 */
1903
118k
            if (phase3_y_steps == 0) {
1904
6.46k
endFallingRightOnEdgeOfPixel:
1905
6.46k
                cursor_always_step_inrange_vertical(cr, 0, sx);
1906
6.46k
                cursor_null(cr);
1907
112k
            } else {
1908
112k
                cursor_step(cr, -phase3_y_steps, sx, skip);
1909
112k
                cursor_right(cr, ex);
1910
112k
                assert(cr->left == sx && cr->right == ex);
1911
112k
            }
1912
118k
        } else {
1913
            /* Lines decreasing in x. (Falling) */
1914
117k
            int phase1_x_steps, phase3_x_steps;
1915
117k
            fixed x_steps = sx - ex;
1916
1917
            /* Phase 1: */
1918
117k
            if (phase1_y_steps) {
1919
109k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1920
109k
                x_steps -= phase1_x_steps;
1921
109k
                sx -= phase1_x_steps;
1922
                /* Phase 1 in a falling line never moves us into a new scanline. */
1923
109k
                cursor_never_step_left(cr, -phase1_y_steps, sx);
1924
109k
                sy -= phase1_y_steps;
1925
109k
                y_steps -= phase1_y_steps;
1926
109k
                if (y_steps == 0)
1927
0
                    goto endFallingVerticalOnEdgeOfPixel;
1928
109k
            }
1929
1930
            /* Phase 3: precalculation */
1931
117k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1932
117k
            x_steps -= phase3_x_steps;
1933
117k
            y_steps -= phase3_y_steps;
1934
117k
            assert((y_steps & (fixed_1 - 1)) == 0);
1935
1936
            /* Phase 2: */
1937
117k
            y_steps = fixed2int(y_steps);
1938
117k
            assert(y_steps >= 0);
1939
117k
            if (y_steps) {
1940
                /* We want to change sx by x_steps in y_steps steps.
1941
                 * So each step, we sub x_steps/y_steps from sx. That's x_inc + n_inc/ey. */
1942
56.2k
                int x_inc = x_steps/y_steps;
1943
56.2k
                int n_inc = x_steps - (x_inc * y_steps);
1944
56.2k
                int f = y_steps/2;
1945
56.2k
                int d = y_steps;
1946
1947
56.2k
                cursor_always_step(cr, -fixed_1, sx, skip);
1948
56.2k
                skip = 0;
1949
56.2k
                sx -= x_inc;
1950
56.2k
                f -= n_inc;
1951
56.2k
                if (f < 0)
1952
6.22k
                    f += d, sx--;
1953
56.2k
                cursor_left(cr, sx);
1954
56.2k
                y_steps--;
1955
1956
147k
                while (y_steps) {
1957
90.8k
                    cursor_always_inrange_step_left(cr, -fixed_1, sx);
1958
90.8k
                    sx -= x_inc;
1959
90.8k
                    f -= n_inc;
1960
90.8k
                    if (f < 0)
1961
41.7k
                        f += d, sx--;
1962
90.8k
                    cursor_left(cr, sx);
1963
90.8k
                    y_steps--;
1964
90.8k
                }
1965
56.2k
            }
1966
1967
            /* Phase 3 */
1968
117k
            if (phase3_y_steps == 0) {
1969
5.99k
endFallingVerticalOnEdgeOfPixel:
1970
5.99k
                cursor_always_step_inrange_vertical(cr, 0, sx);
1971
5.99k
                cursor_null(cr);
1972
111k
            } else {
1973
111k
                cursor_step(cr, -phase3_y_steps, sx, skip);
1974
111k
                cursor_left(cr, ex);
1975
111k
                assert(cr->left == ex && cr->right == sx);
1976
111k
            }
1977
117k
        }
1978
477k
endFalling: {}
1979
477k
    }
1980
1981
1.01M
end:
1982
1.01M
    if (truncated) {
1983
37.3k
        cr->left = saved_ex;
1984
37.3k
        cr->right = saved_ex;
1985
37.3k
        cr->y = saved_ey;
1986
37.3k
    }
1987
1.01M
}
1988
1989
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)
1990
1.24k
{
1991
1.24k
        int ax = (sx + c1x)>>1;
1992
1.24k
        int ay = (sy + c1y)>>1;
1993
1.24k
        int bx = (c1x + c2x)>>1;
1994
1.24k
        int by = (c1y + c2y)>>1;
1995
1.24k
        int cx = (c2x + ex)>>1;
1996
1.24k
        int cy = (c2y + ey)>>1;
1997
1.24k
        int dx = (ax + bx)>>1;
1998
1.24k
        int dy = (ay + by)>>1;
1999
1.24k
        int fx = (bx + cx)>>1;
2000
1.24k
        int fy = (by + cy)>>1;
2001
1.24k
        int gx = (dx + fx)>>1;
2002
1.24k
        int gy = (dy + fy)>>1;
2003
2004
1.24k
        assert(depth >= 0);
2005
1.24k
        if (depth == 0)
2006
1.19k
            mark_line_app(cr, sx, sy, ex, ey);
2007
48
        else {
2008
48
            depth--;
2009
48
            mark_curve_app(cr, sx, sy, ax, ay, dx, dy, gx, gy, depth);
2010
48
            mark_curve_app(cr, gx, gy, fx, fy, cx, cy, ex, ey, depth);
2011
48
        }
2012
1.24k
}
2013
2014
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)
2015
0
{
2016
0
    fixed64 ax = (sx + c1x)>>1;
2017
0
    fixed64 ay = (sy + c1y)>>1;
2018
0
    fixed64 bx = (c1x + c2x)>>1;
2019
0
    fixed64 by = (c1y + c2y)>>1;
2020
0
    fixed64 cx = (c2x + ex)>>1;
2021
0
    fixed64 cy = (c2y + ey)>>1;
2022
0
    fixed64 dx = (ax + bx)>>1;
2023
0
    fixed64 dy = (ay + by)>>1;
2024
0
    fixed64 fx = (bx + cx)>>1;
2025
0
    fixed64 fy = (by + cy)>>1;
2026
0
    fixed64 gx = (dx + fx)>>1;
2027
0
    fixed64 gy = (dy + fy)>>1;
2028
2029
0
    assert(depth >= 0);
2030
0
    if (depth == 0)
2031
0
        mark_line_app(cr, (fixed)sx, (fixed)sy, (fixed)ex, (fixed)ey);
2032
0
    else {
2033
0
        depth--;
2034
0
        mark_curve_big_app(cr, sx, sy, ax, ay, dx, dy, gx, gy, depth);
2035
0
        mark_curve_big_app(cr, gx, gy, fx, fy, cx, cy, ex, ey, depth);
2036
0
    }
2037
0
}
2038
2039
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)
2040
1.14k
{
2041
1.14k
    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));
2042
2043
1.14k
    if (test < 0)
2044
0
        mark_curve_big_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth);
2045
1.14k
    else
2046
1.14k
        mark_curve_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth);
2047
1.14k
}
2048
2049
static int make_table_app(gx_device     * pdev,
2050
                          gx_path       * path,
2051
                          gs_fixed_rect * ibox,
2052
                          int           * scanlines,
2053
                          int          ** index,
2054
                          int          ** table)
2055
113k
{
2056
113k
    return make_table_template(pdev, path, ibox, 2, 0, scanlines, index, table);
2057
113k
}
2058
2059
static void
2060
fill_zero_app(int *row, const fixed *x)
2061
1
{
2062
1
    int n = *row = (*row)+2; /* Increment the count */
2063
1
    row[2*n-3] = (x[0]&~1);
2064
1
    row[2*n-2] = (x[1]&~1);
2065
1
    row[2*n-1] = (x[1]&~1)|1;
2066
1
    row[2*n  ] = x[1];
2067
1
}
2068
2069
int gx_scan_convert_app(gx_device     * gs_restrict pdev,
2070
                        gx_path       * gs_restrict path,
2071
                  const gs_fixed_rect * gs_restrict clip,
2072
                        gx_edgebuffer * gs_restrict edgebuffer,
2073
                        fixed                    fixed_flat)
2074
115k
{
2075
115k
    gs_fixed_rect  ibox;
2076
115k
    gs_fixed_rect  bbox;
2077
115k
    int            scanlines;
2078
115k
    const subpath *psub;
2079
115k
    int           *index;
2080
115k
    int           *table;
2081
115k
    int            i;
2082
115k
    cursor         cr;
2083
115k
    int            code;
2084
115k
    int            zero;
2085
2086
115k
    edgebuffer->index = NULL;
2087
115k
    edgebuffer->table = NULL;
2088
2089
    /* Bale out if no actual path. We see this with the clist */
2090
115k
    if (path->first_subpath == NULL)
2091
132
        return 0;
2092
2093
115k
    zero = make_bbox(path, clip, &bbox, &ibox, 0);
2094
115k
    if (zero < 0)
2095
0
        return zero;
2096
2097
115k
    if (ibox.q.y <= ibox.p.y)
2098
1.49k
        return 0;
2099
2100
113k
    code = make_table_app(pdev, path, &ibox, &scanlines, &index, &table);
2101
113k
    if (code != 0) /* > 0 means "retry with smaller height" */
2102
0
        return code;
2103
2104
113k
    if (scanlines == 0)
2105
0
        return 0;
2106
2107
113k
    if (zero) {
2108
1
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero_app);
2109
113k
    } else {
2110
2111
    /* Step 2 continued: Now we run through the path, filling in the real
2112
     * values. */
2113
113k
    cr.scanlines = scanlines;
2114
113k
    cr.index     = index;
2115
113k
    cr.table     = table;
2116
113k
    cr.base      = ibox.p.y;
2117
265k
    for (psub = path->first_subpath; psub != 0;) {
2118
151k
        const segment *pseg = (const segment *)psub;
2119
151k
        fixed ex = pseg->pt.x;
2120
151k
        fixed ey = pseg->pt.y;
2121
151k
        fixed ix = ex;
2122
151k
        fixed iy = ey;
2123
151k
        fixed sx, sy;
2124
2125
151k
        if ((ey & 0xff) == 0) {
2126
3.63k
            cr.left  = max_fixed;
2127
3.63k
            cr.right = min_fixed;
2128
147k
        } else {
2129
147k
            cr.left = cr.right = ex;
2130
147k
        }
2131
151k
        cr.y = ey;
2132
151k
        cr.d = DIRN_UNSET;
2133
151k
        cr.first = 1;
2134
151k
        cr.saved = 0;
2135
2136
1.68M
        while ((pseg = pseg->next) != 0 &&
2137
1.68M
               pseg->type != s_start
2138
1.52M
            ) {
2139
1.52M
            sx = ex;
2140
1.52M
            sy = ey;
2141
1.52M
            ex = pseg->pt.x;
2142
1.52M
            ey = pseg->pt.y;
2143
2144
1.52M
            switch (pseg->type) {
2145
0
                default:
2146
0
                case s_start: /* Should never happen */
2147
0
                case s_dash:  /* We should never be seeing a dash here */
2148
0
                    assert("This should never happen" == NULL);
2149
0
                    break;
2150
1.14k
                case s_curve: {
2151
1.14k
                    const curve_segment *const pcur = (const curve_segment *)pseg;
2152
1.14k
                    int k = gx_curve_log2_samples(sx, sy, pcur, fixed_flat);
2153
2154
1.14k
                    mark_curve_top_app(&cr, sx, sy, pcur->p1.x, pcur->p1.y, pcur->p2.x, pcur->p2.y, ex, ey, k);
2155
1.14k
                    break;
2156
0
                }
2157
0
                case s_gap:
2158
1.38M
                case s_line:
2159
1.52M
                case s_line_close:
2160
1.52M
                    mark_line_app(&cr, sx, sy, ex, ey);
2161
1.52M
                    break;
2162
1.52M
            }
2163
1.52M
        }
2164
        /* And close any open segments */
2165
151k
        mark_line_app(&cr, ex, ey, ix, iy);
2166
151k
        cursor_flush(&cr, ex);
2167
151k
        psub = (const subpath *)pseg;
2168
151k
    }
2169
113k
    }
2170
2171
    /* Step 2 complete: We now have a complete list of intersection data in
2172
     * table, indexed by index. */
2173
2174
113k
    edgebuffer->base   = ibox.p.y;
2175
113k
    edgebuffer->height = scanlines;
2176
113k
    edgebuffer->xmin   = ibox.p.x;
2177
113k
    edgebuffer->xmax   = ibox.q.x;
2178
113k
    edgebuffer->index  = index;
2179
113k
    edgebuffer->table  = table;
2180
2181
#ifdef DEBUG_SCAN_CONVERTER
2182
    if (debugging_scan_converter) {
2183
        dlprintf("Before sorting:\n");
2184
        gx_edgebuffer_print_app(edgebuffer);
2185
    }
2186
#endif
2187
2188
    /* Step 3: Sort the intersects on x */
2189
754k
    for (i=0; i < scanlines; i++) {
2190
641k
        int *row = &table[index[i]];
2191
641k
        int  rowlen = *row++;
2192
2193
        /* Bubblesort short runs, qsort longer ones. */
2194
        /* FIXME: Verify the figure 6 below */
2195
641k
        if (rowlen <= 6) {
2196
638k
            int j, k;
2197
1.41M
            for (j = 0; j < rowlen-1; j++) {
2198
777k
                int * gs_restrict t = &row[j<<1];
2199
1.77M
                for (k = j+1; k < rowlen; k++) {
2200
1.00M
                    int * gs_restrict s = &row[k<<1];
2201
1.00M
                    int tmp;
2202
1.00M
                    if (t[0] < s[0])
2203
434k
                        continue;
2204
567k
                    if (t[0] > s[0])
2205
567k
                        goto swap01;
2206
149
                    if (t[1] <= s[1])
2207
117
                        continue;
2208
32
                    if (0) {
2209
567k
swap01:
2210
567k
                        tmp = t[0], t[0] = s[0], s[0] = tmp;
2211
567k
                    }
2212
567k
                    tmp = t[1], t[1] = s[1], s[1] = tmp;
2213
567k
                }
2214
777k
            }
2215
638k
        } else
2216
2.71k
            qsort(row, rowlen, 2*sizeof(int), edgecmp);
2217
641k
    }
2218
2219
113k
    return 0;
2220
113k
}
2221
2222
/* Step 5: Filter the intersections according to the rules */
2223
int
2224
gx_filter_edgebuffer_app(gx_device       * gs_restrict pdev,
2225
                         gx_edgebuffer   * gs_restrict edgebuffer,
2226
                         int                        rule)
2227
115k
{
2228
115k
    int i;
2229
2230
#ifdef DEBUG_SCAN_CONVERTER
2231
    if (debugging_scan_converter) {
2232
        dlprintf("Before filtering:\n");
2233
        gx_edgebuffer_print_app(edgebuffer);
2234
    }
2235
#endif
2236
2237
756k
    for (i=0; i < edgebuffer->height; i++) {
2238
641k
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
2239
641k
        int  rowlen   = *row++;
2240
641k
        int *rowstart = row;
2241
641k
        int *rowout   = row;
2242
641k
        int  ll, lr, rl, rr, wind, marked_to;
2243
2244
        /* Avoid double setting pixels, by keeping where we have marked to. */
2245
641k
        marked_to = INT_MIN;
2246
1.34M
        while (rowlen > 0) {
2247
704k
            if (rule == gx_rule_even_odd) {
2248
                /* Even Odd */
2249
9.68k
                ll = (*row++)&~1;
2250
9.68k
                lr = *row;
2251
9.68k
                row += 2;
2252
9.68k
                rowlen-=2;
2253
2254
                /* We will fill solidly from ll to at least lr, possibly further */
2255
9.68k
                assert(rowlen >= 0);
2256
9.68k
                rr = (*row++);
2257
9.68k
                if (rr > lr)
2258
9.67k
                    lr = rr;
2259
694k
            } else {
2260
                /* Non-Zero */
2261
694k
                int w;
2262
2263
694k
                ll = *row++;
2264
694k
                lr = *row++;
2265
694k
                wind = -(ll&1) | 1;
2266
694k
                ll &= ~1;
2267
694k
                rowlen--;
2268
2269
694k
                assert(rowlen > 0);
2270
735k
                do {
2271
735k
                    rl = *row++;
2272
735k
                    rr = *row++;
2273
735k
                    w = -(rl&1) | 1;
2274
735k
                    rl &= ~1;
2275
735k
                    rowlen--;
2276
735k
                    if (rr > lr)
2277
718k
                        lr = rr;
2278
735k
                    wind += w;
2279
735k
                    if (wind == 0)
2280
694k
                        break;
2281
735k
                } while (rowlen > 0);
2282
694k
            }
2283
2284
704k
            if (marked_to >= lr)
2285
384
                continue;
2286
2287
703k
            if (marked_to >= ll) {
2288
6.26k
                if (rowout == rowstart)
2289
0
                    ll = marked_to;
2290
6.26k
                else {
2291
6.26k
                    rowout -= 2;
2292
6.26k
                    ll = *rowout;
2293
6.26k
                }
2294
6.26k
            }
2295
2296
703k
            if (lr >= ll) {
2297
703k
                *rowout++ = ll;
2298
703k
                *rowout++ = lr;
2299
703k
                marked_to = lr;
2300
703k
            }
2301
703k
        }
2302
641k
        rowstart[-1] = rowout - rowstart;
2303
641k
    }
2304
115k
    return 0;
2305
115k
}
2306
2307
/* Step 6: Fill */
2308
int
2309
gx_fill_edgebuffer_app(gx_device       * gs_restrict pdev,
2310
                 const gx_device_color * gs_restrict pdevc,
2311
                       gx_edgebuffer   * gs_restrict edgebuffer,
2312
                       int                        log_op)
2313
115k
{
2314
115k
    int i, code;
2315
2316
756k
    for (i=0; i < edgebuffer->height; i++) {
2317
641k
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
2318
641k
        int  rowlen = *row++;
2319
641k
        int  left, right;
2320
2321
1.33M
        while (rowlen > 0) {
2322
697k
            left  = *row++;
2323
697k
            right = *row++;
2324
697k
            left  = fixed2int(left);
2325
697k
            right = fixed2int(right + fixed_1 - 1);
2326
697k
            rowlen -= 2;
2327
2328
697k
            right -= left;
2329
697k
            if (right > 0) {
2330
697k
                if (log_op < 0)
2331
689k
                    code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
2332
7.85k
                else
2333
7.85k
                    code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
2334
697k
                if (code < 0)
2335
0
                    return code;
2336
697k
            }
2337
697k
        }
2338
641k
    }
2339
115k
    return 0;
2340
115k
}
2341
2342
/* Centre of a pixel trapezoid routines */
2343
2344
static int intcmp_tr(const void *a, const void *b)
2345
203k
{
2346
203k
    int left  = ((int*)a)[0];
2347
203k
    int right = ((int*)b)[0];
2348
203k
    if (left != right)
2349
202k
        return left - right;
2350
417
    return ((int*)a)[1] - ((int*)b)[1];
2351
203k
}
2352
2353
#ifdef DEBUG_SCAN_CONVERTER
2354
static void
2355
gx_edgebuffer_print_tr(gx_edgebuffer * edgebuffer)
2356
{
2357
    int i;
2358
2359
    if (!debugging_scan_converter)
2360
        return;
2361
2362
    dlprintf1("Edgebuffer %x\n", edgebuffer);
2363
    dlprintf4("xmin=%x xmax=%x base=%x height=%x\n",
2364
              edgebuffer->xmin, edgebuffer->xmax, edgebuffer->base, edgebuffer->height);
2365
    for (i=0; i < edgebuffer->height; i++)
2366
    {
2367
        int  offset = edgebuffer->index[i];
2368
        int *row    = &edgebuffer->table[offset];
2369
        int count   = *row++;
2370
        dlprintf3("%d @ %d: %d =", i, offset, count);
2371
        while (count-- > 0) {
2372
            int e  = *row++;
2373
            int id = *row++;
2374
            dlprintf3(" %x%c%d", e, id&1 ? 'v' : '^', id>>1);
2375
        }
2376
        dlprintf("\n");
2377
    }
2378
}
2379
#endif
2380
2381
static void mark_line_tr(fixed sx, fixed sy, fixed ex, fixed ey, int base_y, int height, int *table, int *index, int id)
2382
317k
{
2383
317k
    int64_t delta;
2384
317k
    int iy, ih;
2385
317k
    fixed clip_sy, clip_ey;
2386
317k
    int dirn = DIRN_UP;
2387
317k
    int *row;
2388
2389
#ifdef DEBUG_SCAN_CONVERTER
2390
    if (debugging_scan_converter)
2391
        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);
2392
#endif
2393
#ifdef DEBUG_OUTPUT_SC_AS_PS
2394
    dlprintf("0.001 setlinewidth 0 0 0 setrgbcolor %%PS\n");
2395
    coord("moveto", sx, sy);
2396
    coord("lineto", ex, ey);
2397
    dlprintf("stroke %%PS\n");
2398
#endif
2399
2400
317k
    if (fixed2int(sy + fixed_half-1) == fixed2int(ey + fixed_half-1))
2401
8.63k
        return;
2402
308k
    if (sy > ey) {
2403
131k
        int t;
2404
131k
        t = sy; sy = ey; ey = t;
2405
131k
        t = sx; sx = ex; ex = t;
2406
131k
        dirn = DIRN_DOWN;
2407
131k
    }
2408
    /* Lines go from sy to ey, closed at the start, open at the end. */
2409
    /* We clip them to a region to make them closed at both ends. */
2410
    /* Thus the first scanline marked (>= sy) is: */
2411
308k
    clip_sy = ((sy + fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
2412
    /* The last scanline marked (< ey) is: */
2413
308k
    clip_ey = ((ey - fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
2414
    /* Now allow for banding */
2415
308k
    if (clip_sy < int2fixed(base_y) + fixed_half)
2416
154k
        clip_sy = int2fixed(base_y) + fixed_half;
2417
308k
    if (ey <= clip_sy)
2418
137k
        return;
2419
170k
    if (clip_ey > int2fixed(base_y + height - 1) + fixed_half)
2420
132k
        clip_ey = int2fixed(base_y + height - 1) + fixed_half;
2421
170k
    if (sy > clip_ey)
2422
116k
        return;
2423
54.4k
    delta = (int64_t)clip_sy - (int64_t)sy;
2424
54.4k
    if (delta > 0)
2425
53.9k
    {
2426
53.9k
        int64_t dx = (int64_t)ex - (int64_t)sx;
2427
53.9k
        int64_t dy = (int64_t)ey - (int64_t)sy;
2428
53.9k
        int advance = (int)((dx * delta + (dy>>1)) / dy);
2429
53.9k
        sx += advance;
2430
53.9k
        sy += delta;
2431
53.9k
    }
2432
54.4k
    delta = (int64_t)ey - (int64_t)clip_ey;
2433
54.4k
    if (delta > 0)
2434
54.4k
    {
2435
54.4k
        int64_t dx = (int64_t)ex - (int64_t)sx;
2436
54.4k
        int64_t dy = (int64_t)ey - (int64_t)sy;
2437
54.4k
        int advance = (int)((dx * delta + (dy>>1)) / dy);
2438
54.4k
        ex -= advance;
2439
54.4k
        ey -= delta;
2440
54.4k
    }
2441
54.4k
    ex -= sx;
2442
54.4k
    ey -= sy;
2443
54.4k
    ih = fixed2int(ey);
2444
54.4k
    assert(ih >= 0);
2445
54.4k
    iy = fixed2int(sy) - base_y;
2446
#ifdef DEBUG_SCAN_CONVERTER
2447
    if (debugging_scan_converter)
2448
        dlprintf2("    iy=%x ih=%x\n", iy, ih);
2449
#endif
2450
54.4k
    assert(iy >= 0 && iy < height);
2451
54.4k
    id = (id<<1) | dirn;
2452
    /* We always cross at least one scanline */
2453
54.4k
    row = &table[index[iy]];
2454
54.4k
    *row = (*row)+1; /* Increment the count */
2455
54.4k
    row[*row * 2 - 1] = sx;
2456
54.4k
    row[*row * 2    ] = id;
2457
54.4k
    if (ih == 0)
2458
11.1k
        return;
2459
43.2k
    if (ex >= 0) {
2460
23.9k
        int x_inc, n_inc, f;
2461
2462
        /* We want to change sx by ex in ih steps. So each step, we add
2463
         * ex/ih to sx. That's x_inc + n_inc/ih.
2464
         */
2465
23.9k
        x_inc = ex/ih;
2466
23.9k
        n_inc = ex-(x_inc*ih);
2467
23.9k
        f     = ih>>1;
2468
23.9k
        delta = ih;
2469
1.13M
        do {
2470
1.13M
            int count;
2471
1.13M
            iy++;
2472
1.13M
            sx += x_inc;
2473
1.13M
            f  -= n_inc;
2474
1.13M
            if (f < 0) {
2475
234k
                f += ih;
2476
234k
                sx++;
2477
234k
            }
2478
1.13M
            assert(iy >= 0 && iy < height);
2479
1.13M
            row = &table[index[iy]];
2480
1.13M
            count = *row = (*row)+1; /* Increment the count */
2481
1.13M
            row[count * 2 - 1] = sx;
2482
1.13M
            row[count * 2    ] = id;
2483
1.13M
        }
2484
1.13M
        while (--delta);
2485
23.9k
    } else {
2486
19.3k
        int x_dec, n_dec, f;
2487
2488
19.3k
        ex = -ex;
2489
        /* We want to change sx by ex in ih steps. So each step, we subtract
2490
         * ex/ih from sx. That's x_dec + n_dec/ih.
2491
         */
2492
19.3k
        x_dec = ex/ih;
2493
19.3k
        n_dec = ex-(x_dec*ih);
2494
19.3k
        f     = ih>>1;
2495
19.3k
        delta = ih;
2496
726k
        do {
2497
726k
            int count;
2498
726k
            iy++;
2499
726k
            sx -= x_dec;
2500
726k
            f  -= n_dec;
2501
726k
            if (f < 0) {
2502
337k
                f += ih;
2503
337k
                sx--;
2504
337k
            }
2505
726k
            assert(iy >= 0 && iy < height);
2506
726k
            row = &table[index[iy]];
2507
726k
            count = *row = (*row)+1; /* Increment the count */
2508
726k
            row[count * 2 - 1] = sx;
2509
726k
            row[count * 2    ] = id;
2510
726k
         }
2511
726k
         while (--delta);
2512
19.3k
    }
2513
43.2k
}
2514
2515
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)
2516
0
{
2517
0
    fixed ax = (sx + c1x)>>1;
2518
0
    fixed ay = (sy + c1y)>>1;
2519
0
    fixed bx = (c1x + c2x)>>1;
2520
0
    fixed by = (c1y + c2y)>>1;
2521
0
    fixed cx = (c2x + ex)>>1;
2522
0
    fixed cy = (c2y + ey)>>1;
2523
0
    fixed dx = (ax + bx)>>1;
2524
0
    fixed dy = (ay + by)>>1;
2525
0
    fixed fx = (bx + cx)>>1;
2526
0
    fixed fy = (by + cy)>>1;
2527
0
    fixed gx = (dx + fx)>>1;
2528
0
    fixed gy = (dy + fy)>>1;
2529
2530
0
    assert(depth >= 0);
2531
0
    if (depth == 0) {
2532
0
        *id += 1;
2533
0
        mark_line_tr(sx, sy, ex, ey, base_y, height, table, index, *id);
2534
0
    } else {
2535
0
        depth--;
2536
0
        mark_curve_tr(sx, sy, ax, ay, dx, dy, gx, gy, base_y, height, table, index, id, depth);
2537
0
        mark_curve_tr(gx, gy, fx, fy, cx, cy, ex, ey, base_y, height, table, index, id, depth);
2538
0
    }
2539
0
}
2540
2541
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)
2542
0
{
2543
0
    fixed64 ax = (sx + c1x)>>1;
2544
0
    fixed64 ay = (sy + c1y)>>1;
2545
0
    fixed64 bx = (c1x + c2x)>>1;
2546
0
    fixed64 by = (c1y + c2y)>>1;
2547
0
    fixed64 cx = (c2x + ex)>>1;
2548
0
    fixed64 cy = (c2y + ey)>>1;
2549
0
    fixed64 dx = (ax + bx)>>1;
2550
0
    fixed64 dy = (ay + by)>>1;
2551
0
    fixed64 fx = (bx + cx)>>1;
2552
0
    fixed64 fy = (by + cy)>>1;
2553
0
    fixed64 gx = (dx + fx)>>1;
2554
0
    fixed64 gy = (dy + fy)>>1;
2555
2556
0
    assert(depth >= 0);
2557
0
    if (depth == 0) {
2558
0
        *id += 1;
2559
0
        mark_line_tr((fixed)sx, (fixed)sy, (fixed)ex, (fixed)ey, base_y, height, table, index, *id);
2560
0
    } else {
2561
0
        depth--;
2562
0
        mark_curve_big_tr(sx, sy, ax, ay, dx, dy, gx, gy, base_y, height, table, index, id, depth);
2563
0
        mark_curve_big_tr(gx, gy, fx, fy, cx, cy, ex, ey, base_y, height, table, index, id, depth);
2564
0
    }
2565
0
}
2566
2567
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)
2568
0
{
2569
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));
2570
2571
0
    if (test < 0)
2572
0
        mark_curve_big_tr(sx, sy, c1x, c1y, c2x, c2y, ex, ey, base_y, height, table, index, id, depth);
2573
0
    else
2574
0
        mark_curve_tr(sx, sy, c1x, c1y, c2x, c2y, ex, ey, base_y, height, table, index, id, depth);
2575
0
}
2576
2577
static int make_table_tr(gx_device     * pdev,
2578
                         gx_path       * path,
2579
                         gs_fixed_rect * ibox,
2580
                         int           * scanlines,
2581
                         int          ** index,
2582
                         int          ** table)
2583
5.88k
{
2584
5.88k
    return make_table_template(pdev, path, ibox, 2, 1, scanlines, index, table);
2585
5.88k
}
2586
2587
static void
2588
fill_zero_tr(int *row, const fixed *x)
2589
0
{
2590
0
    int n = *row = (*row)+2; /* Increment the count */
2591
0
    row[2*n-3] = x[0];
2592
0
    row[2*n-2] = 0;
2593
0
    row[2*n-1] = x[1];
2594
0
    row[2*n  ] = 1;
2595
0
}
2596
2597
int gx_scan_convert_tr(gx_device     * gs_restrict pdev,
2598
                       gx_path       * gs_restrict path,
2599
                 const gs_fixed_rect * gs_restrict clip,
2600
                       gx_edgebuffer * gs_restrict edgebuffer,
2601
                       fixed                    fixed_flat)
2602
20.7k
{
2603
20.7k
    gs_fixed_rect  ibox;
2604
20.7k
    gs_fixed_rect  bbox;
2605
20.7k
    int            scanlines;
2606
20.7k
    const subpath *psub;
2607
20.7k
    int           *index;
2608
20.7k
    int           *table;
2609
20.7k
    int            i;
2610
20.7k
    int            code;
2611
20.7k
    int            id = 0;
2612
20.7k
    int            zero;
2613
2614
20.7k
    edgebuffer->index = NULL;
2615
20.7k
    edgebuffer->table = NULL;
2616
2617
    /* Bale out if no actual path. We see this with the clist */
2618
20.7k
    if (path->first_subpath == NULL)
2619
14.7k
        return 0;
2620
2621
5.90k
    zero = make_bbox(path, clip, &bbox, &ibox, fixed_half);
2622
5.90k
    if (zero < 0)
2623
0
        return zero;
2624
2625
5.90k
    if (ibox.q.y <= ibox.p.y)
2626
18
        return 0;
2627
2628
5.88k
    code = make_table_tr(pdev, path, &ibox, &scanlines, &index, &table);
2629
5.88k
    if (code != 0) /* > 0 means "retry with smaller height" */
2630
0
        return code;
2631
2632
5.88k
    if (scanlines == 0)
2633
0
        return 0;
2634
2635
5.88k
    if (zero) {
2636
0
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero_tr);
2637
5.88k
    } else {
2638
2639
    /* Step 3: Now we run through the path, filling in the real
2640
     * values. */
2641
14.2k
    for (psub = path->first_subpath; psub != 0;) {
2642
8.34k
        const segment *pseg = (const segment *)psub;
2643
8.34k
        fixed ex = pseg->pt.x;
2644
8.34k
        fixed ey = pseg->pt.y;
2645
8.34k
        fixed ix = ex;
2646
8.34k
        fixed iy = ey;
2647
2648
336k
        while ((pseg = pseg->next) != 0 &&
2649
336k
               pseg->type != s_start
2650
328k
            ) {
2651
328k
            fixed sx = ex;
2652
328k
            fixed sy = ey;
2653
328k
            ex = pseg->pt.x;
2654
328k
            ey = pseg->pt.y;
2655
2656
328k
            switch (pseg->type) {
2657
0
                default:
2658
0
                case s_start: /* Should never happen */
2659
0
                case s_dash:  /* We should never be seeing a dash here */
2660
0
                    assert("This should never happen" == NULL);
2661
0
                    break;
2662
0
                case s_curve: {
2663
0
                    const curve_segment *const pcur = (const curve_segment *)pseg;
2664
0
                    int k = gx_curve_log2_samples(sx, sy, pcur, fixed_flat);
2665
2666
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);
2667
0
                    break;
2668
0
                }
2669
0
                case s_gap:
2670
326k
                case s_line:
2671
328k
                case s_line_close:
2672
328k
                    if (sy != ey)
2673
313k
                        mark_line_tr(sx, sy, ex, ey, ibox.p.y, scanlines, table, index, ++id);
2674
328k
                    break;
2675
328k
            }
2676
328k
        }
2677
        /* And close any open segments */
2678
8.34k
        if (iy != ey)
2679
4.12k
            mark_line_tr(ex, ey, ix, iy, ibox.p.y, scanlines, table, index, ++id);
2680
8.34k
        psub = (const subpath *)pseg;
2681
8.34k
    }
2682
5.88k
    }
2683
2684
    /*if (zero) {
2685
        if (table[0] == 0) { */
2686
            /* Zero height rectangle fills a span */
2687
/*          table[0] = 2;
2688
            table[1] = int2fixed(fixed2int(bbox.p.x + fixed_half));
2689
            table[2] = 0;
2690
            table[3] = int2fixed(fixed2int(bbox.q.x + fixed_half));
2691
            table[4] = 1;
2692
        }
2693
    }*/
2694
2695
    /* Step 2 complete: We now have a complete list of intersection data in
2696
     * table, indexed by index. */
2697
2698
5.88k
    edgebuffer->base   = ibox.p.y;
2699
5.88k
    edgebuffer->height = scanlines;
2700
5.88k
    edgebuffer->xmin   = ibox.p.x;
2701
5.88k
    edgebuffer->xmax   = ibox.q.x;
2702
5.88k
    edgebuffer->index  = index;
2703
5.88k
    edgebuffer->table  = table;
2704
2705
#ifdef DEBUG_SCAN_CONVERTER
2706
    if (debugging_scan_converter) {
2707
        dlprintf("Before sorting:\n");
2708
        gx_edgebuffer_print_tr(edgebuffer);
2709
    }
2710
#endif
2711
2712
    /* Step 4: Sort the intersects on x */
2713
633k
    for (i=0; i < scanlines; i++) {
2714
627k
        int *row = &table[index[i]];
2715
627k
        int  rowlen = *row++;
2716
2717
        /* Bubblesort short runs, qsort longer ones. */
2718
        /* FIXME: Verify the figure 6 below */
2719
627k
        if (rowlen <= 6) {
2720
622k
            int j, k;
2721
1.84M
            for (j = 0; j < rowlen-1; j++) {
2722
1.22M
                int * gs_restrict t = &row[j<<1];
2723
3.51M
                for (k = j+1; k < rowlen; k++) {
2724
2.29M
                    int * gs_restrict s = &row[k<<1];
2725
2.29M
                    int tmp;
2726
2.29M
                    if (t[0] < s[0])
2727
1.43M
                        continue;
2728
859k
                    if (t[0] == s[0]) {
2729
9.35k
                        if (t[1] <= s[1])
2730
9.35k
                            continue;
2731
9.35k
                    } else
2732
850k
                        tmp = t[0], t[0] = s[0], s[0] = tmp;
2733
850k
                    tmp = t[1], t[1] = s[1], s[1] = tmp;
2734
850k
                }
2735
1.22M
            }
2736
622k
        } else
2737
4.89k
            qsort(row, rowlen, 2*sizeof(int), intcmp_tr);
2738
627k
    }
2739
2740
5.88k
    return 0;
2741
5.88k
}
2742
2743
/* Step 5: Filter the intersections according to the rules */
2744
int
2745
gx_filter_edgebuffer_tr(gx_device       * gs_restrict pdev,
2746
                        gx_edgebuffer   * gs_restrict edgebuffer,
2747
                        int                           rule)
2748
20.7k
{
2749
20.7k
    int i;
2750
2751
#ifdef DEBUG_SCAN_CONVERTER
2752
    if (debugging_scan_converter) {
2753
        dlprintf("Before filtering\n");
2754
        gx_edgebuffer_print_tr(edgebuffer);
2755
    }
2756
#endif
2757
2758
648k
    for (i=0; i < edgebuffer->height; i++) {
2759
627k
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
2760
627k
        int  rowlen   = *row++;
2761
627k
        int *rowstart = row;
2762
627k
        int *rowout   = row;
2763
2764
1.58M
        while (rowlen > 0) {
2765
959k
            int left, lid, right, rid;
2766
2767
959k
            if (rule == gx_rule_even_odd) {
2768
                /* Even Odd */
2769
0
                left  = *row++;
2770
0
                lid   = *row++;
2771
0
                right = *row++;
2772
0
                rid   = *row++;
2773
0
                rowlen -= 2;
2774
959k
            } else {
2775
                /* Non-Zero */
2776
959k
                int w;
2777
2778
959k
                left = *row++;
2779
959k
                lid  = *row++;
2780
959k
                w = ((lid&1)-1) | 1;
2781
959k
                rowlen--;
2782
961k
                do {
2783
961k
                    right = *row++;
2784
961k
                    rid   = *row++;
2785
961k
                    rowlen--;
2786
961k
                    w += ((rid&1)-1) | 1;
2787
961k
                } while (w != 0);
2788
959k
            }
2789
2790
959k
            if (right > left) {
2791
949k
                *rowout++ = left;
2792
949k
                *rowout++ = lid;
2793
949k
                *rowout++ = right;
2794
949k
                *rowout++ = rid;
2795
949k
            }
2796
959k
        }
2797
627k
        rowstart[-1] = (rowout-rowstart)>>1;
2798
627k
    }
2799
20.7k
    return 0;
2800
20.7k
}
2801
2802
/* Step 6: Fill the edgebuffer */
2803
int
2804
gx_fill_edgebuffer_tr(gx_device       * gs_restrict pdev,
2805
                const gx_device_color * gs_restrict pdevc,
2806
                      gx_edgebuffer   * gs_restrict edgebuffer,
2807
                      int                        log_op)
2808
20.7k
{
2809
20.7k
    int i, j, code;
2810
20.7k
    int mfb = pdev->max_fill_band;
2811
2812
#ifdef DEBUG_SCAN_CONVERTER
2813
    if (debugging_scan_converter) {
2814
        dlprintf("Before filling\n");
2815
        gx_edgebuffer_print_tr(edgebuffer);
2816
    }
2817
#endif
2818
2819
44.8k
    for (i=0; i < edgebuffer->height; ) {
2820
24.1k
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
2821
24.1k
        int  rowlen = *row++;
2822
24.1k
        int *row2;
2823
24.1k
        int *rowptr;
2824
24.1k
        int *row2ptr;
2825
24.1k
        int y_band_max;
2826
2827
24.1k
        if (mfb) {
2828
0
            y_band_max = (i & ~(mfb-1)) + mfb;
2829
0
            if (y_band_max > edgebuffer->height)
2830
0
                y_band_max = edgebuffer->height;
2831
24.1k
        } else {
2832
24.1k
            y_band_max = edgebuffer->height;
2833
24.1k
        }
2834
2835
        /* See how many scanlines match i */
2836
627k
        for (j = i+1; j < y_band_max; j++) {
2837
621k
            int row2len;
2838
2839
621k
            row2    = &edgebuffer->table[edgebuffer->index[j]];
2840
621k
            row2len = *row2++;
2841
621k
            row2ptr = row2;
2842
621k
            rowptr  = row;
2843
2844
621k
            if (rowlen != row2len)
2845
2.99k
                break;
2846
2.42M
            while (row2len > 0) {
2847
1.82M
                if ((rowptr[1]&~1) != (row2ptr[1]&~1))
2848
15.2k
                    goto rowdifferent;
2849
1.80M
                rowptr  += 2;
2850
1.80M
                row2ptr += 2;
2851
1.80M
                row2len--;
2852
1.80M
            }
2853
619k
        }
2854
24.1k
rowdifferent:{}
2855
2856
        /* So j is the first scanline that doesn't match i */
2857
2858
24.1k
        if (j == i+1) {
2859
27.4k
            while (rowlen > 0) {
2860
23.4k
                int left, right;
2861
2862
23.4k
                left  = row[0];
2863
23.4k
                right = row[2];
2864
23.4k
                row += 4;
2865
23.4k
                rowlen -= 2;
2866
2867
23.4k
                left  = fixed2int(left + fixed_half);
2868
23.4k
                right = fixed2int(right + fixed_half);
2869
23.4k
                right -= left;
2870
23.4k
                if (right > 0) {
2871
#ifdef DEBUG_OUTPUT_SC_AS_PS
2872
                    dlprintf("0.001 setlinewidth 1 0 1 setrgbcolor %% purple %%PS\n");
2873
                    coord("moveto", int2fixed(left), int2fixed(edgebuffer->base+i));
2874
                    coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+i));
2875
                    coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+i+1));
2876
                    coord("lineto", int2fixed(left), int2fixed(edgebuffer->base+i+1));
2877
                    dlprintf("closepath stroke %%PS\n");
2878
#endif
2879
22.9k
                    if (log_op < 0)
2880
0
                        code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
2881
22.9k
                    else
2882
22.9k
                        code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
2883
22.9k
                    if (code < 0)
2884
0
                        return code;
2885
22.9k
                }
2886
23.4k
            }
2887
20.0k
        } else {
2888
20.0k
            gs_fixed_edge le;
2889
20.0k
            gs_fixed_edge re;
2890
2891
#ifdef DEBUG_OUTPUT_SC_AS_PS
2892
#ifdef DEBUG_OUTPUT_SC_AS_PS_TRAPS_AS_RECTS
2893
            int k;
2894
            for (k = i; k < j; k++)
2895
            {
2896
                int row2len;
2897
                int left, right;
2898
                row2    = &edgebuffer->table[edgebuffer->index[k]];
2899
                row2len = *row2++;
2900
                while (row2len > 0) {
2901
                    left = row2[0];
2902
                    right = row2[2];
2903
                    row2 += 4;
2904
                    row2len -= 2;
2905
2906
                    left  = fixed2int(left + fixed_half);
2907
                    right = fixed2int(right + fixed_half);
2908
                    right -= left;
2909
                    if (right > 0) {
2910
                        dlprintf("0.001 setlinewidth 1 0 0.5 setrgbcolor %%PS\n");
2911
                        coord("moveto", int2fixed(left), int2fixed(edgebuffer->base+k));
2912
                        coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+k));
2913
                        coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+k+1));
2914
                        coord("lineto", int2fixed(left), int2fixed(edgebuffer->base+k+1));
2915
                        dlprintf("closepath stroke %%PS\n");
2916
                    }
2917
                }
2918
            }
2919
#endif
2920
#endif
2921
2922
20.0k
            le.start.y = re.start.y = int2fixed(edgebuffer->base+i) + fixed_half;
2923
20.0k
            le.end.y   = re.end.y   = int2fixed(edgebuffer->base+j) - (fixed_half-1);
2924
20.0k
            row2    = &edgebuffer->table[edgebuffer->index[j-1]+1];
2925
53.1k
            while (rowlen > 0) {
2926
33.0k
                le.start.x = row[0];
2927
33.0k
                re.start.x = row[2];
2928
33.0k
                le.end.x   = row2[0];
2929
33.0k
                re.end.x   = row2[2];
2930
33.0k
                row += 4;
2931
33.0k
                row2 += 4;
2932
33.0k
                rowlen -= 2;
2933
2934
33.0k
                assert(re.start.x >= le.start.x);
2935
33.0k
                assert(re.end.x >= le.end.x);
2936
2937
#ifdef DEBUG_OUTPUT_SC_AS_PS
2938
                dlprintf("0.001 setlinewidth 0 1 1 setrgbcolor %%cyan %%PS\n");
2939
                coord("moveto", le.start.x, le.start.y);
2940
                coord("lineto", le.end.x, le.end.y);
2941
                coord("lineto", re.end.x, re.end.y);
2942
                coord("lineto", re.start.x, re.start.y);
2943
                dlprintf("closepath stroke %%PS\n");
2944
#endif
2945
33.0k
                code = dev_proc(pdev, fill_trapezoid)(
2946
33.0k
                                pdev,
2947
33.0k
                                &le,
2948
33.0k
                                &re,
2949
33.0k
                                le.start.y,
2950
33.0k
                                le.end.y,
2951
33.0k
                                0, /* bool swap_axes */
2952
33.0k
                                pdevc, /*const gx_drawing_color *pdcolor */
2953
33.0k
                                log_op);
2954
33.0k
                if (code < 0)
2955
0
                    return code;
2956
33.0k
            }
2957
20.0k
        }
2958
2959
24.1k
        i = j;
2960
24.1k
    }
2961
20.7k
    return 0;
2962
20.7k
}
2963
2964
/* Any part of a pixel trapezoid routines */
2965
2966
static int edgecmp_tr(const void *a, const void *b)
2967
8.16M
{
2968
8.16M
    int left  = ((int*)a)[0];
2969
8.16M
    int right = ((int*)b)[0];
2970
8.16M
    if (left != right)
2971
7.81M
        return left - right;
2972
353k
    left = ((int*)a)[2] - ((int*)b)[2];
2973
353k
    if (left != 0)
2974
145k
        return left;
2975
207k
    left = ((int*)a)[1] - ((int*)b)[1];
2976
207k
    if (left != 0)
2977
207k
        return left;
2978
0
    return ((int*)a)[3] - ((int*)b)[3];
2979
207k
}
2980
2981
#ifdef DEBUG_SCAN_CONVERTER
2982
static void
2983
gx_edgebuffer_print_filtered_tr_app(gx_edgebuffer * edgebuffer)
2984
{
2985
    int i;
2986
2987
    if (!debugging_scan_converter)
2988
        return;
2989
2990
    dlprintf1("Edgebuffer %x\n", edgebuffer);
2991
    dlprintf4("xmin=%x xmax=%x base=%x height=%x\n",
2992
              edgebuffer->xmin, edgebuffer->xmax, edgebuffer->base, edgebuffer->height);
2993
    for (i=0; i < edgebuffer->height; i++)
2994
    {
2995
        int  offset = edgebuffer->index[i];
2996
        int *row    = &edgebuffer->table[offset];
2997
        int count   = *row++;
2998
        dlprintf3("%x @ %d: %d =", i, offset, count);
2999
        while (count-- > 0) {
3000
            int left  = *row++;
3001
            int lid   = *row++;
3002
            int right = *row++;
3003
            int rid   = *row++;
3004
            dlprintf4(" (%x:%d,%x:%d)", left, lid, right, rid);
3005
        }
3006
        dlprintf("\n");
3007
    }
3008
}
3009
3010
static void
3011
gx_edgebuffer_print_tr_app(gx_edgebuffer * edgebuffer)
3012
{
3013
    int i;
3014
    int borked = 0;
3015
3016
    if (!debugging_scan_converter)
3017
        return;
3018
3019
    dlprintf1("Edgebuffer %x\n", edgebuffer);
3020
    dlprintf4("xmin=%x xmax=%x base=%x height=%x\n",
3021
              edgebuffer->xmin, edgebuffer->xmax, edgebuffer->base, edgebuffer->height);
3022
    for (i=0; i < edgebuffer->height; i++)
3023
    {
3024
        int  offset = edgebuffer->index[i];
3025
        int *row    = &edgebuffer->table[offset];
3026
        int count   = *row++;
3027
        int c       = count;
3028
        int wind    = 0;
3029
        dlprintf3("%x @ %d: %d =", i, offset, count);
3030
        while (count-- > 0) {
3031
            int left  = *row++;
3032
            int lid   = *row++;
3033
            int right = *row++;
3034
            int rid   = *row++;
3035
            int ww    = lid & 1;
3036
            int w     = -ww | 1;
3037
            lid >>= 1;
3038
            wind += w;
3039
            dlprintf5(" (%x:%d,%x:%d)%c", left, lid, right, rid, ww ? 'v' : '^');
3040
        }
3041
        if (wind != 0 || c & 1) {
3042
            dlprintf(" <- BROKEN");
3043
            borked = 1;
3044
        }
3045
        dlprintf("\n");
3046
    }
3047
    if (borked) {
3048
        borked = borked; /* Breakpoint here */
3049
    }
3050
}
3051
#endif
3052
3053
typedef struct
3054
{
3055
    fixed  left;
3056
    int    lid;
3057
    fixed  right;
3058
    int    rid;
3059
    fixed  y;
3060
    signed char  d; /* 0 up (or horiz), 1 down, -1 uninited */
3061
    unsigned char first;
3062
    unsigned char saved;
3063
    fixed  save_left;
3064
    int    save_lid;
3065
    fixed  save_right;
3066
    int    save_rid;
3067
    int    save_iy;
3068
    int    save_d;
3069
3070
    int    scanlines;
3071
    int   *table;
3072
    int   *index;
3073
    int    base;
3074
} cursor_tr;
3075
3076
static inline void
3077
cursor_output_tr(cursor_tr * gs_restrict cr, int iy)
3078
119M
{
3079
119M
    int *row;
3080
119M
    int count;
3081
3082
119M
    if (iy >= 0 && iy < cr->scanlines) {
3083
118M
        if (cr->first) {
3084
            /* Save it for later in case we join up */
3085
2.26M
            cr->save_left  = cr->left;
3086
2.26M
            cr->save_lid   = cr->lid;
3087
2.26M
            cr->save_right = cr->right;
3088
2.26M
            cr->save_rid   = cr->rid;
3089
2.26M
            cr->save_iy    = iy;
3090
2.26M
            cr->save_d     = cr->d;
3091
2.26M
            cr->saved      = 1;
3092
116M
        } else if (cr->d != DIRN_UNSET) {
3093
            /* Enter it into the table */
3094
116M
            row = &cr->table[cr->index[iy]];
3095
116M
            *row = count = (*row)+1; /* Increment the count */
3096
116M
            row[4 * count - 3] = cr->left;
3097
116M
            row[4 * count - 2] = cr->d | (cr->lid<<1);
3098
116M
            row[4 * count - 1] = cr->right;
3099
116M
            row[4 * count    ] = cr->rid;
3100
116M
        } else {
3101
19.8k
            assert(cr->left == max_fixed && cr->right == min_fixed);
3102
19.8k
        }
3103
118M
    }
3104
119M
    cr->first = 0;
3105
119M
}
3106
3107
static inline void
3108
cursor_output_inrange_tr(cursor_tr * gs_restrict cr, int iy)
3109
23.4M
{
3110
23.4M
    int *row;
3111
23.4M
    int count;
3112
3113
23.4M
    assert(iy >= 0 && iy < cr->scanlines);
3114
23.4M
    if (cr->first) {
3115
        /* Save it for later in case we join up */
3116
14.3k
        cr->save_left  = cr->left;
3117
14.3k
        cr->save_lid   = cr->lid;
3118
14.3k
        cr->save_right = cr->right;
3119
14.3k
        cr->save_rid   = cr->rid;
3120
14.3k
        cr->save_iy    = iy;
3121
14.3k
        cr->save_d     = cr->d;
3122
14.3k
        cr->saved      = 1;
3123
23.4M
    } else {
3124
        /* Enter it into the table */
3125
23.4M
        assert(cr->d != DIRN_UNSET);
3126
3127
23.4M
        row = &cr->table[cr->index[iy]];
3128
23.4M
        *row = count = (*row)+1; /* Increment the count */
3129
23.4M
        row[4 * count - 3] = cr->left;
3130
23.4M
        row[4 * count - 2] = cr->d | (cr->lid<<1);
3131
23.4M
        row[4 * count - 1] = cr->right;
3132
23.4M
        row[4 * count    ] = cr->rid;
3133
23.4M
    }
3134
23.4M
    cr->first = 0;
3135
23.4M
}
3136
3137
/* Step the cursor in y, allowing for maybe crossing a scanline */
3138
static inline void
3139
cursor_step_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id, int skip)
3140
8.77M
{
3141
8.77M
    int new_iy;
3142
8.77M
    int iy = fixed2int(cr->y) - cr->base;
3143
3144
8.77M
    cr->y += dy;
3145
8.77M
    new_iy = fixed2int(cr->y) - cr->base;
3146
8.77M
    if (new_iy != iy) {
3147
8.77M
        if (!skip)
3148
8.68M
            cursor_output_tr(cr, iy);
3149
8.77M
        cr->left = x;
3150
8.77M
        cr->lid = id;
3151
8.77M
        cr->right = x;
3152
8.77M
        cr->rid = id;
3153
8.77M
    } else {
3154
0
        if (x < cr->left) {
3155
0
            cr->left = x;
3156
0
            cr->lid = id;
3157
0
        }
3158
0
        if (x > cr->right) {
3159
0
            cr->right = x;
3160
0
            cr->rid = id;
3161
0
        }
3162
0
    }
3163
8.77M
}
3164
3165
/* Step the cursor in y, never by enough to cross a scanline. */
3166
static inline void
3167
cursor_never_step_vertical_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3168
1.15M
{
3169
1.15M
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
3170
3171
1.15M
    cr->y += dy;
3172
1.15M
}
3173
3174
/* Step the cursor in y, never by enough to cross a scanline,
3175
 * knowing that we are moving left, and that the right edge
3176
 * has already been accounted for. */
3177
static inline void
3178
cursor_never_step_left_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3179
1.69M
{
3180
1.69M
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
3181
3182
1.69M
    if (x < cr->left)
3183
1.30M
    {
3184
1.30M
        cr->left = x;
3185
1.30M
        cr->lid = id;
3186
1.30M
    }
3187
1.69M
    cr->y += dy;
3188
1.69M
}
3189
3190
/* Step the cursor in y, never by enough to cross a scanline,
3191
 * knowing that we are moving right, and that the left edge
3192
 * has already been accounted for. */
3193
static inline void
3194
cursor_never_step_right_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3195
1.52M
{
3196
1.52M
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
3197
3198
1.52M
    if (x > cr->right)
3199
1.47M
    {
3200
1.47M
        cr->right = x;
3201
1.47M
        cr->rid = id;
3202
1.47M
    }
3203
1.52M
    cr->y += dy;
3204
1.52M
}
3205
3206
/* Step the cursor in y, always by enough to cross a scanline. */
3207
static inline void
3208
cursor_always_step_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id, int skip)
3209
6.18M
{
3210
6.18M
    int iy = fixed2int(cr->y) - cr->base;
3211
3212
6.18M
    if (!skip)
3213
5.58M
        cursor_output_tr(cr, iy);
3214
6.18M
    cr->y += dy;
3215
6.18M
    cr->left = x;
3216
6.18M
    cr->lid = id;
3217
6.18M
    cr->right = x;
3218
6.18M
    cr->rid = id;
3219
6.18M
}
3220
3221
/* Step the cursor in y, always by enough to cross a scanline, as
3222
 * part of a vertical line, knowing that we are moving from a
3223
 * position guaranteed to be in the valid y range. */
3224
static inline void
3225
cursor_always_step_inrange_vertical_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3226
98.1M
{
3227
98.1M
    int iy = fixed2int(cr->y) - cr->base;
3228
3229
98.1M
    cursor_output_tr(cr, iy);
3230
98.1M
    cr->y += dy;
3231
98.1M
}
3232
3233
/* Step the cursor in y, always by enough to cross a scanline, as
3234
 * part of a left moving line, knowing that we are moving from a
3235
 * position guaranteed to be in the valid y range. */
3236
static inline void
3237
cursor_always_inrange_step_left_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3238
11.9M
{
3239
11.9M
    int iy = fixed2int(cr->y) - cr->base;
3240
3241
11.9M
    cr->y += dy;
3242
11.9M
    cursor_output_inrange_tr(cr, iy);
3243
11.9M
    cr->right = x;
3244
11.9M
    cr->rid = id;
3245
11.9M
}
3246
3247
/* Step the cursor in y, always by enough to cross a scanline, as
3248
 * part of a right moving line, knowing that we are moving from a
3249
 * position guaranteed to be in the valid y range. */
3250
static inline void
3251
cursor_always_inrange_step_right_tr(cursor_tr * gs_restrict cr, fixed dy, fixed x, int id)
3252
11.5M
{
3253
11.5M
    int iy = fixed2int(cr->y) - cr->base;
3254
3255
11.5M
    cr->y += dy;
3256
11.5M
    cursor_output_inrange_tr(cr, iy);
3257
11.5M
    cr->left = x;
3258
11.5M
    cr->lid = id;
3259
11.5M
}
3260
3261
static inline void cursor_init_tr(cursor_tr * gs_restrict cr, fixed y, fixed x, int id)
3262
0
{
3263
0
    assert(y >= int2fixed(cr->base) && y <= int2fixed(cr->base + cr->scanlines));
3264
0
3265
0
    cr->y = y;
3266
0
    cr->left = x;
3267
0
    cr->lid = id;
3268
0
    cr->right = x;
3269
0
    cr->rid = id;
3270
0
    cr->d = DIRN_UNSET;
3271
0
}
3272
3273
static inline void cursor_left_merge_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3274
25.7M
{
3275
25.7M
    if (x < cr->left) {
3276
7.69M
        cr->left = x;
3277
7.69M
        cr->lid  = id;
3278
7.69M
    }
3279
25.7M
}
3280
3281
static inline void cursor_left_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3282
16.2M
{
3283
16.2M
    cr->left = x;
3284
16.2M
    cr->lid  = id;
3285
16.2M
}
3286
3287
static inline void cursor_right_merge_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3288
26.6M
{
3289
26.6M
    if (x > cr->right) {
3290
7.80M
        cr->right = x;
3291
7.80M
        cr->rid   = id;
3292
7.80M
    }
3293
26.6M
}
3294
3295
static inline void cursor_right_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3296
15.5M
{
3297
15.5M
    cr->right = x;
3298
15.5M
    cr->rid   = id;
3299
15.5M
}
3300
3301
static inline int cursor_down_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3302
7.78M
{
3303
7.78M
    int skip = 0;
3304
7.78M
    if ((cr->y & 0xff) == 0)
3305
690k
        skip = 1;
3306
7.78M
    if (cr->d == DIRN_UP)
3307
1.40M
    {
3308
1.40M
        if (!skip)
3309
1.40M
            cursor_output_tr(cr, fixed2int(cr->y) - cr->base);
3310
1.40M
        cr->left = x;
3311
1.40M
        cr->lid = id;
3312
1.40M
        cr->right = x;
3313
1.40M
        cr->rid = id;
3314
1.40M
    }
3315
7.78M
    cr->d = DIRN_DOWN;
3316
7.78M
    return skip;
3317
7.78M
}
3318
3319
static inline void cursor_up_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3320
7.79M
{
3321
7.79M
    if (cr->d == DIRN_DOWN)
3322
1.28M
    {
3323
1.28M
        cursor_output_tr(cr, fixed2int(cr->y) - cr->base);
3324
1.28M
        cr->left = x;
3325
1.28M
        cr->lid = id;
3326
1.28M
        cr->right = x;
3327
1.28M
        cr->rid = id;
3328
1.28M
    }
3329
7.79M
    cr->d = DIRN_UP;
3330
7.79M
}
3331
3332
static inline void
3333
cursor_flush_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3334
2.54M
{
3335
2.54M
    int iy;
3336
3337
    /* This should only happen if we were entirely out of bounds,
3338
     * or if everything was within a zero height horizontal
3339
     * rectangle from the start point. */
3340
2.54M
    if (cr->first) {
3341
714
        int iy = fixed2int(cr->y) - cr->base;
3342
        /* Any zero height rectangle counts as filled, except
3343
         * those on the baseline of a pixel. */
3344
714
        if (cr->d == DIRN_UNSET && (cr->y & 0xff) == 0)
3345
29
            return;
3346
685
        assert(cr->left != max_fixed && cr->right != min_fixed);
3347
685
        if (iy >= 0 && iy < cr->scanlines) {
3348
604
            int *row = &cr->table[cr->index[iy]];
3349
604
            int count = *row = (*row)+2; /* Increment the count */
3350
604
            row[4 * count - 7] = cr->left;
3351
604
            row[4 * count - 6] = DIRN_UP | (cr->lid<<1);
3352
604
            row[4 * count - 5] = cr->right;
3353
604
            row[4 * count - 4] = cr->rid;
3354
604
            row[4 * count - 3] = cr->right;
3355
604
            row[4 * count - 2] = DIRN_DOWN | (cr->rid<<1);
3356
604
            row[4 * count - 1] = cr->right;
3357
604
            row[4 * count    ] = cr->rid;
3358
604
        }
3359
685
        return;
3360
714
    }
3361
3362
    /* Merge save into current if we can */
3363
2.54M
    iy = fixed2int(cr->y) - cr->base;
3364
2.54M
    if (cr->saved && iy == cr->save_iy &&
3365
2.54M
        (cr->d == cr->save_d || cr->save_d == DIRN_UNSET)) {
3366
576k
        if (cr->left > cr->save_left) {
3367
137k
            cr->left = cr->save_left;
3368
137k
            cr->lid  = cr->save_lid;
3369
137k
        }
3370
576k
        if (cr->right < cr->save_right) {
3371
139k
            cr->right = cr->save_right;
3372
139k
            cr->rid = cr->save_rid;
3373
139k
        }
3374
576k
        cursor_output_tr(cr, iy);
3375
576k
        return;
3376
576k
    }
3377
3378
    /* Merge not possible */
3379
1.96M
    cursor_output_tr(cr, iy);
3380
1.96M
    if (cr->saved) {
3381
1.70M
        cr->left  = cr->save_left;
3382
1.70M
        cr->lid   = cr->save_lid;
3383
1.70M
        cr->right = cr->save_right;
3384
1.70M
        cr->rid   = cr->save_rid;
3385
1.70M
        assert(cr->save_d != DIRN_UNSET);
3386
1.70M
        if (cr->save_d != DIRN_UNSET)
3387
1.70M
            cr->d = cr->save_d;
3388
1.70M
        cursor_output_tr(cr, cr->save_iy);
3389
1.70M
    }
3390
1.96M
}
3391
3392
static inline void
3393
cursor_null_tr(cursor_tr *cr)
3394
2.77M
{
3395
2.77M
    cr->right = min_fixed;
3396
2.77M
    cr->left  = max_fixed;
3397
2.77M
    cr->d     = DIRN_UNSET;
3398
2.77M
}
3399
3400
static void mark_line_tr_app(cursor_tr * gs_restrict cr, fixed sx, fixed sy, fixed ex, fixed ey, int id)
3401
62.6M
{
3402
62.6M
    int isy, iey;
3403
62.6M
    fixed saved_sy = sy;
3404
62.6M
    fixed saved_ex = ex;
3405
62.6M
    fixed saved_ey = ey;
3406
62.6M
    int truncated;
3407
3408
62.6M
    if (sy == ey && sx == ex)
3409
2.72M
        return;
3410
3411
59.9M
    isy = fixed2int(sy) - cr->base;
3412
59.9M
    iey = fixed2int(ey) - cr->base;
3413
3414
#ifdef DEBUG_SCAN_CONVERTER
3415
    if (debugging_scan_converter)
3416
        dlprintf6("Marking line (tr_app) from %x,%x to %x,%x (%x,%x)\n", sx, sy, ex, ey, isy, iey);
3417
#endif
3418
#ifdef DEBUG_OUTPUT_SC_AS_PS
3419
    dlprintf("0.001 setlinewidth 0 0 0 setrgbcolor %%PS\n");
3420
    coord("moveto", sx, sy);
3421
    coord("lineto", ex, ey);
3422
    dlprintf("stroke %%PS\n");
3423
#endif
3424
3425
    /* Horizontal motion at the bottom of a pixel is ignored */
3426
59.9M
    if (sy == ey && (sy & 0xff) == 0)
3427
185k
        return;
3428
3429
59.7M
    assert(cr->y == sy &&
3430
59.7M
           ((cr->left <= sx && cr->right >= sx) || ((sy & 0xff) == 0)) &&
3431
59.7M
           cr->d >= DIRN_UNSET && cr->d <= DIRN_DOWN);
3432
3433
59.7M
    if (isy < iey) {
3434
        /* Rising line */
3435
22.4M
        if (iey < 0 || isy >= cr->scanlines) {
3436
            /* All line is outside. */
3437
17.5M
            if ((ey & 0xff) == 0) {
3438
76.4k
                cursor_null_tr(cr);
3439
17.4M
            } else {
3440
17.4M
                cr->left = ex;
3441
17.4M
                cr->lid = id;
3442
17.4M
                cr->right = ex;
3443
17.4M
                cr->rid = id;
3444
17.4M
            }
3445
17.5M
            cr->y = ey;
3446
17.5M
            cr->first = 0;
3447
17.5M
            return;
3448
17.5M
        }
3449
4.97M
        if (isy < 0) {
3450
            /* Move sy up */
3451
625k
            int64_t y = (int64_t)ey - (int64_t)sy;
3452
625k
            fixed new_sy = int2fixed(cr->base);
3453
625k
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
3454
625k
            sx += (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3455
625k
            sy = new_sy;
3456
625k
            cursor_null_tr(cr);
3457
625k
            cr->y = sy;
3458
625k
            isy = 0;
3459
625k
        }
3460
4.97M
        truncated = iey > cr->scanlines;
3461
4.97M
        if (truncated) {
3462
            /* Move ey down */
3463
574k
            int64_t y = (int64_t)ey - (int64_t)sy;
3464
574k
            fixed new_ey = int2fixed(cr->base + cr->scanlines);
3465
574k
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
3466
574k
            saved_ex = ex;
3467
574k
            saved_ey = ey;
3468
574k
            ex -= (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3469
574k
            ey = new_ey;
3470
574k
            iey = cr->scanlines;
3471
574k
        }
3472
37.2M
    } else {
3473
        /* Falling line */
3474
37.2M
        if (isy < 0 || iey >= cr->scanlines) {
3475
            /* All line is outside. */
3476
23.0M
            if ((ey & 0xff) == 0) {
3477
90.3k
                cursor_null_tr(cr);
3478
22.9M
            } else {
3479
22.9M
                cr->left = ex;
3480
22.9M
                cr->lid = id;
3481
22.9M
                cr->right = ex;
3482
22.9M
                cr->rid = id;
3483
22.9M
            }
3484
23.0M
            cr->y = ey;
3485
23.0M
            cr->first = 0;
3486
23.0M
            return;
3487
23.0M
        }
3488
14.1M
        truncated = iey < 0;
3489
14.1M
        if (truncated) {
3490
            /* Move ey up */
3491
625k
            int64_t y = (int64_t)ey - (int64_t)sy;
3492
625k
            fixed new_ey = int2fixed(cr->base);
3493
625k
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
3494
625k
            ex -= (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3495
625k
            ey = new_ey;
3496
625k
            iey = 0;
3497
625k
        }
3498
14.1M
        if (isy >= cr->scanlines) {
3499
            /* Move sy down */
3500
614k
            int64_t y = (int64_t)ey - (int64_t)sy;
3501
614k
            fixed new_sy = int2fixed(cr->base + cr->scanlines);
3502
614k
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
3503
614k
            sx += (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3504
614k
            sy = new_sy;
3505
614k
            cursor_null_tr(cr);
3506
614k
            cr->y = sy;
3507
614k
            isy = cr->scanlines;
3508
614k
        }
3509
14.1M
    }
3510
3511
19.1M
    cursor_left_merge_tr(cr, sx, id);
3512
19.1M
    cursor_right_merge_tr(cr, sx, id);
3513
3514
19.1M
    assert(cr->left <= sx);
3515
19.1M
    assert(cr->right >= sx);
3516
19.1M
    assert(cr->y == sy);
3517
3518
    /* A note: The code below used to be of the form:
3519
     *   if (isy == iey)   ... deal with horizontal lines
3520
     *   else if (ey > sy) {
3521
     *     fixed y_steps = ey - sy;
3522
     *      ... deal with rising lines ...
3523
     *   } else {
3524
     *     fixed y_steps = ey - sy;
3525
     *     ... deal with falling lines
3526
     *   }
3527
     * but that lead to problems, for instance, an example seen
3528
     * has sx=2aa8e, sy=8aee7, ex=7ffc1686, ey=8003e97a.
3529
     * Thus isy=84f, iey=ff80038a. We can see that ey < sy, but
3530
     * sy - ey < 0!
3531
     * We therefore rejig our code so that the choice between
3532
     * cases is done based on the sign of y_steps rather than
3533
     * the relative size of ey and sy.
3534
     */
3535
3536
    /* First, deal with lines that don't change scanline.
3537
     * This accommodates horizontal lines. */
3538
19.1M
    if (isy == iey) {
3539
9.13M
        if (saved_sy == saved_ey) {
3540
            /* Horizontal line. Don't change cr->d, don't flush. */
3541
3.56M
            if ((ey & 0xff) == 0) {
3542
0
                cursor_null_tr(cr);
3543
0
                goto no_merge;
3544
0
            }
3545
5.57M
        } else if (saved_sy > saved_ey) {
3546
            /* Falling line, flush if previous was rising */
3547
2.71M
            int skip = cursor_down_tr(cr, sx, id);
3548
2.71M
            if ((ey & 0xff) == 0) {
3549
                /* We are falling to the baseline of a subpixel, so output
3550
                 * for the current pixel, and leave the cursor nulled. */
3551
89.9k
                if (sx <= ex) {
3552
71.6k
                    cursor_right_merge_tr(cr, ex, id);
3553
71.6k
                } else {
3554
18.3k
                    cursor_left_merge_tr(cr, ex, id);
3555
18.3k
                }
3556
89.9k
                if (!skip)
3557
87.7k
                    cursor_output_tr(cr, fixed2int(cr->y) - cr->base);
3558
89.9k
                cursor_null_tr(cr);
3559
89.9k
                goto no_merge;
3560
89.9k
            }
3561
2.85M
        } else {
3562
            /* Rising line, flush if previous was falling */
3563
2.85M
            cursor_up_tr(cr, sx, id);
3564
2.85M
            if ((ey & 0xff) == 0) {
3565
2.32k
                cursor_null_tr(cr);
3566
2.32k
                goto no_merge;
3567
2.32k
            }
3568
2.85M
        }
3569
9.04M
        if (sx <= ex) {
3570
4.79M
            cursor_right_merge_tr(cr, ex, id);
3571
4.79M
        } else {
3572
4.24M
            cursor_left_merge_tr(cr, ex, id);
3573
4.24M
        }
3574
9.13M
no_merge:
3575
9.13M
        cr->y = ey;
3576
9.13M
        if (sy > saved_ey)
3577
2.71M
            goto endFalling;
3578
10.0M
    } else if (iey > isy) {
3579
        /* So lines increasing in y. */
3580
        /* We want to change from sy to ey, which are guaranteed to be on
3581
         * different scanlines. We do this in 3 phases.
3582
         * Phase 1 gets us from sy to the next scanline boundary. (We may exit after phase 1).
3583
         * Phase 2 gets us all the way to the last scanline boundary. (This may be a null operation)
3584
         * 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).
3585
         */
3586
4.93M
        int phase1_y_steps = (-sy) & (fixed_1 - 1);
3587
4.93M
        int phase3_y_steps = ey & (fixed_1 - 1);
3588
4.93M
        ufixed y_steps = (ufixed)ey - (ufixed)sy;
3589
3590
4.93M
        cursor_up_tr(cr, sx, id);
3591
3592
4.93M
        if (sx == ex) {
3593
            /* Vertical line. (Rising) */
3594
3595
            /* Phase 1: */
3596
1.67M
            if (phase1_y_steps) {
3597
                /* If phase 1 will move us into a new scanline, then we must
3598
                 * flush it before we move. */
3599
1.18M
                cursor_step_tr(cr, phase1_y_steps, sx, id, 0);
3600
1.18M
                sy += phase1_y_steps;
3601
1.18M
                y_steps -= phase1_y_steps;
3602
1.18M
                if (y_steps == 0) {
3603
44.4k
                    cursor_null_tr(cr);
3604
44.4k
                    goto end;
3605
44.4k
                }
3606
1.18M
            }
3607
3608
            /* Phase 3: precalculation */
3609
1.62M
            y_steps -= phase3_y_steps;
3610
3611
            /* Phase 2: */
3612
1.62M
            y_steps = fixed2int(y_steps);
3613
1.62M
            assert(y_steps >= 0);
3614
1.62M
            if (y_steps > 0) {
3615
1.16M
                cursor_always_step_tr(cr, fixed_1, sx, id, 0);
3616
1.16M
                y_steps--;
3617
49.9M
                while (y_steps) {
3618
48.8M
                    cursor_always_step_inrange_vertical_tr(cr, fixed_1, sx, id);
3619
48.8M
                    y_steps--;
3620
48.8M
                }
3621
1.16M
            }
3622
3623
            /* Phase 3 */
3624
1.62M
            assert(cr->left == sx && cr->right == sx && cr->lid == id && cr->rid == id);
3625
1.62M
            if (phase3_y_steps == 0)
3626
461k
                cursor_null_tr(cr);
3627
1.16M
            else
3628
1.16M
                cr->y += phase3_y_steps;
3629
3.26M
        } else if (sx < ex) {
3630
            /* Lines increasing in x. (Rightwards, rising) */
3631
1.70M
            int phase1_x_steps, phase3_x_steps;
3632
            /* Use unsigned int here, to allow for extreme cases like
3633
             * ex = 0x7fffffff, sx = 0x80000000 */
3634
1.70M
            unsigned int x_steps = ex - sx;
3635
3636
            /* Phase 1: */
3637
1.70M
            if (phase1_y_steps) {
3638
1.63M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3639
1.63M
                sx += phase1_x_steps;
3640
1.63M
                cursor_right_merge_tr(cr, sx, id);
3641
1.63M
                x_steps -= phase1_x_steps;
3642
1.63M
                cursor_step_tr(cr, phase1_y_steps, sx, id, 0);
3643
1.63M
                sy += phase1_y_steps;
3644
1.63M
                y_steps -= phase1_y_steps;
3645
1.63M
                if (y_steps == 0) {
3646
13.7k
                    cursor_null_tr(cr);
3647
13.7k
                    goto end;
3648
13.7k
                }
3649
1.63M
            }
3650
3651
            /* Phase 3: precalculation */
3652
1.69M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3653
1.69M
            x_steps -= phase3_x_steps;
3654
1.69M
            y_steps -= phase3_y_steps;
3655
1.69M
            assert((y_steps & (fixed_1 - 1)) == 0);
3656
3657
            /* Phase 2: */
3658
1.69M
            y_steps = fixed2int(y_steps);
3659
1.69M
            assert(y_steps >= 0);
3660
1.69M
            if (y_steps) {
3661
                /* We want to change sx by x_steps in y_steps steps.
3662
                 * So each step, we add x_steps/y_steps to sx. That's x_inc + n_inc/y_steps. */
3663
1.03M
                int x_inc = x_steps/y_steps;
3664
1.03M
                int n_inc = x_steps - (x_inc * y_steps);
3665
1.03M
                int f = y_steps/2;
3666
1.03M
                int d = y_steps;
3667
3668
                /* Special casing the first iteration, allows us to simplify
3669
                 * the following loop. */
3670
1.03M
                sx += x_inc;
3671
1.03M
                f -= n_inc;
3672
1.03M
                if (f < 0)
3673
215k
                    f += d, sx++;
3674
1.03M
                cursor_right_merge_tr(cr, sx, id);
3675
1.03M
                cursor_always_step_tr(cr, fixed_1, sx, id, 0);
3676
1.03M
                y_steps--;
3677
3678
7.85M
                while (y_steps) {
3679
6.81M
                    sx += x_inc;
3680
6.81M
                    f -= n_inc;
3681
6.81M
                    if (f < 0)
3682
2.92M
                        f += d, sx++;
3683
6.81M
                    cursor_right_tr(cr, sx, id);
3684
6.81M
                    cursor_always_inrange_step_right_tr(cr, fixed_1, sx, id);
3685
6.81M
                    y_steps--;
3686
6.81M
                };
3687
1.03M
            }
3688
3689
            /* Phase 3 */
3690
1.69M
            assert(cr->left <= ex && cr->lid == id && cr->right >= sx);
3691
1.69M
            if (phase3_y_steps == 0)
3692
63.1k
                cursor_null_tr(cr);
3693
1.63M
            else {
3694
1.63M
                cursor_right_tr(cr, ex, id);
3695
1.63M
                cr->y += phase3_y_steps;
3696
1.63M
            }
3697
1.69M
        } else {
3698
            /* Lines decreasing in x. (Leftwards, rising) */
3699
1.55M
            int phase1_x_steps, phase3_x_steps;
3700
            /* Use unsigned int here, to allow for extreme cases like
3701
             * sx = 0x7fffffff, ex = 0x80000000 */
3702
1.55M
            unsigned int x_steps = sx - ex;
3703
3704
            /* Phase 1: */
3705
1.55M
            if (phase1_y_steps) {
3706
1.49M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3707
1.49M
                x_steps -= phase1_x_steps;
3708
1.49M
                sx -= phase1_x_steps;
3709
1.49M
                cursor_left_merge_tr(cr, sx, id);
3710
1.49M
                cursor_step_tr(cr, phase1_y_steps, sx, id, 0);
3711
1.49M
                sy += phase1_y_steps;
3712
1.49M
                y_steps -= phase1_y_steps;
3713
1.49M
                if (y_steps == 0) {
3714
13.7k
                    cursor_null_tr(cr);
3715
13.7k
                    goto end;
3716
13.7k
                }
3717
1.49M
            }
3718
3719
            /* Phase 3: precalculation */
3720
1.54M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3721
1.54M
            x_steps -= phase3_x_steps;
3722
1.54M
            y_steps -= phase3_y_steps;
3723
1.54M
            assert((y_steps & (fixed_1 - 1)) == 0);
3724
3725
            /* Phase 2: */
3726
1.54M
            y_steps = fixed2int((unsigned int)y_steps);
3727
1.54M
            assert(y_steps >= 0);
3728
1.54M
            if (y_steps) {
3729
                /* We want to change sx by x_steps in y_steps steps.
3730
                 * So each step, we sub x_steps/y_steps from sx. That's x_inc + n_inc/ey. */
3731
849k
                int x_inc = x_steps/y_steps;
3732
849k
                int n_inc = x_steps - (x_inc * y_steps);
3733
849k
                int f = y_steps/2;
3734
849k
                int d = y_steps;
3735
3736
                /* Special casing the first iteration, allows us to simplify
3737
                 * the following loop. */
3738
849k
                sx -= x_inc;
3739
849k
                f -= n_inc;
3740
849k
                if (f < 0)
3741
141k
                    f += d, sx--;
3742
849k
                cursor_left_merge_tr(cr, sx, id);
3743
849k
                cursor_always_step_tr(cr, fixed_1, sx, id, 0);
3744
849k
                y_steps--;
3745
3746
5.80M
                while (y_steps) {
3747
4.96M
                    sx -= x_inc;
3748
4.96M
                    f -= n_inc;
3749
4.96M
                    if (f < 0)
3750
2.19M
                        f += d, sx--;
3751
4.96M
                    cursor_left_tr(cr, sx, id);
3752
4.96M
                    cursor_always_inrange_step_left_tr(cr, fixed_1, sx, id);
3753
4.96M
                    y_steps--;
3754
4.96M
                }
3755
849k
            }
3756
3757
            /* Phase 3 */
3758
1.54M
            assert(cr->right >= ex && cr->rid == id && cr->left <= sx);
3759
1.54M
            if (phase3_y_steps == 0)
3760
57.2k
                cursor_null_tr(cr);
3761
1.48M
            else {
3762
1.48M
                cursor_left_tr(cr, ex, id);
3763
1.48M
                cr->y += phase3_y_steps;
3764
1.48M
            }
3765
1.54M
        }
3766
5.07M
    } else {
3767
        /* So lines decreasing in y. */
3768
        /* We want to change from sy to ey, which are guaranteed to be on
3769
         * different scanlines. We do this in 3 phases.
3770
         * Phase 1 gets us from sy to the next scanline boundary. This never causes an output.
3771
         * Phase 2 gets us all the way to the last scanline boundary. This is guaranteed to cause an output.
3772
         * Phase 3 gets us from the last scanline boundary to ey. We are guaranteed to have outputted by now.
3773
         */
3774
5.07M
        int phase1_y_steps = sy & (fixed_1 - 1);
3775
5.07M
        int phase3_y_steps = (-ey) & (fixed_1 - 1);
3776
5.07M
        ufixed y_steps = (ufixed)sy - (ufixed)ey;
3777
3778
5.07M
        int skip = cursor_down_tr(cr, sx, id);
3779
3780
5.07M
        if (sx == ex) {
3781
            /* Vertical line. (Falling) */
3782
3783
            /* Phase 1: */
3784
1.67M
            if (phase1_y_steps) {
3785
                /* Phase 1 in a falling line never moves us into a new scanline. */
3786
1.15M
                cursor_never_step_vertical_tr(cr, -phase1_y_steps, sx, id);
3787
1.15M
                sy -= phase1_y_steps;
3788
1.15M
                y_steps -= phase1_y_steps;
3789
1.15M
                if (y_steps == 0)
3790
0
                    goto endFallingLeftOnEdgeOfPixel;
3791
1.15M
            }
3792
3793
            /* Phase 3: precalculation */
3794
1.67M
            y_steps -= phase3_y_steps;
3795
1.67M
            assert((y_steps & (fixed_1 - 1)) == 0);
3796
3797
            /* Phase 2: */
3798
1.67M
            y_steps = fixed2int(y_steps);
3799
1.67M
            assert(y_steps >= 0);
3800
1.67M
            if (y_steps) {
3801
1.16M
                cursor_always_step_tr(cr, -fixed_1, sx, id, skip);
3802
1.16M
                skip = 0;
3803
1.16M
                y_steps--;
3804
49.8M
                while (y_steps) {
3805
48.6M
                    cursor_always_step_inrange_vertical_tr(cr, -fixed_1, sx, id);
3806
48.6M
                    y_steps--;
3807
48.6M
                }
3808
1.16M
            }
3809
3810
            /* Phase 3 */
3811
1.67M
            if (phase3_y_steps == 0) {
3812
480k
endFallingLeftOnEdgeOfPixel:
3813
480k
                cursor_always_step_inrange_vertical_tr(cr, 0, sx, id);
3814
480k
                cursor_null_tr(cr);
3815
1.19M
            } else {
3816
1.19M
                cursor_step_tr(cr, -phase3_y_steps, sx, id, skip);
3817
1.19M
                assert(cr->left == sx && cr->lid == id && cr->right == sx && cr->rid == id);
3818
1.19M
            }
3819
3.39M
        } else if (sx < ex) {
3820
            /* Lines increasing in x. (Rightwards, falling) */
3821
1.60M
            int phase1_x_steps, phase3_x_steps;
3822
            /* Use unsigned int here, to allow for extreme cases like
3823
             * ex = 0x7fffffff, sx = 0x80000000 */
3824
1.60M
            unsigned int x_steps = ex - sx;
3825
3826
            /* Phase 1: */
3827
1.60M
            if (phase1_y_steps) {
3828
1.52M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3829
1.52M
                x_steps -= phase1_x_steps;
3830
1.52M
                sx += phase1_x_steps;
3831
                /* Phase 1 in a falling line never moves us into a new scanline. */
3832
1.52M
                cursor_never_step_right_tr(cr, -phase1_y_steps, sx, id);
3833
1.52M
                sy -= phase1_y_steps;
3834
1.52M
                y_steps -= phase1_y_steps;
3835
1.52M
                if (y_steps == 0)
3836
0
                    goto endFallingRightOnEdgeOfPixel;
3837
1.52M
            }
3838
3839
            /* Phase 3: precalculation */
3840
1.60M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3841
1.60M
            x_steps -= phase3_x_steps;
3842
1.60M
            y_steps -= phase3_y_steps;
3843
1.60M
            assert((y_steps & (fixed_1 - 1)) == 0);
3844
3845
            /* Phase 2: */
3846
1.60M
            y_steps = fixed2int(y_steps);
3847
1.60M
            assert(y_steps >= 0);
3848
1.60M
            if (y_steps) {
3849
                /* We want to change sx by x_steps in y_steps steps.
3850
                 * So each step, we add x_steps/y_steps to sx. That's x_inc + n_inc/ey. */
3851
866k
                int x_inc = x_steps/y_steps;
3852
866k
                int n_inc = x_steps - (x_inc * y_steps);
3853
866k
                int f = y_steps/2;
3854
866k
                int d = y_steps;
3855
3856
866k
                cursor_always_step_tr(cr, -fixed_1, sx, id, skip);
3857
866k
                skip = 0;
3858
866k
                sx += x_inc;
3859
866k
                f -= n_inc;
3860
866k
                if (f < 0)
3861
152k
                    f += d, sx++;
3862
866k
                cursor_right_tr(cr, sx, id);
3863
866k
                y_steps--;
3864
3865
5.58M
                while (y_steps) {
3866
4.71M
                    cursor_always_inrange_step_right_tr(cr, -fixed_1, sx, id);
3867
4.71M
                    sx += x_inc;
3868
4.71M
                    f -= n_inc;
3869
4.71M
                    if (f < 0)
3870
2.12M
                        f += d, sx++;
3871
4.71M
                    cursor_right_tr(cr, sx, id);
3872
4.71M
                    y_steps--;
3873
4.71M
                }
3874
866k
            }
3875
3876
            /* Phase 3 */
3877
1.60M
            if (phase3_y_steps == 0) {
3878
60.5k
endFallingRightOnEdgeOfPixel:
3879
60.5k
                cursor_always_step_inrange_vertical_tr(cr, 0, sx, id);
3880
60.5k
                cursor_null_tr(cr);
3881
1.54M
            } else {
3882
1.54M
                cursor_step_tr(cr, -phase3_y_steps, sx, id, skip);
3883
1.54M
                cursor_right_tr(cr, ex, id);
3884
1.54M
                assert(cr->left == sx && cr->lid == id && cr->right == ex && cr->rid == id);
3885
1.54M
            }
3886
1.78M
        } else {
3887
            /* Lines decreasing in x. (Falling) */
3888
1.78M
            int phase1_x_steps, phase3_x_steps;
3889
            /* Use unsigned int here, to allow for extreme cases like
3890
             * sx = 0x7fffffff, ex = 0x80000000 */
3891
1.78M
            unsigned int x_steps = sx - ex;
3892
3893
            /* Phase 1: */
3894
1.78M
            if (phase1_y_steps) {
3895
1.69M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3896
1.69M
                x_steps -= phase1_x_steps;
3897
1.69M
                sx -= phase1_x_steps;
3898
                /* Phase 1 in a falling line never moves us into a new scanline. */
3899
1.69M
                cursor_never_step_left_tr(cr, -phase1_y_steps, sx, id);
3900
1.69M
                sy -= phase1_y_steps;
3901
1.69M
                y_steps -= phase1_y_steps;
3902
1.69M
                if (y_steps == 0)
3903
0
                    goto endFallingVerticalOnEdgeOfPixel;
3904
1.69M
            }
3905
3906
            /* Phase 3: precalculation */
3907
1.78M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3908
1.78M
            x_steps -= phase3_x_steps;
3909
1.78M
            y_steps -= phase3_y_steps;
3910
1.78M
            assert((y_steps & (fixed_1 - 1)) == 0);
3911
3912
            /* Phase 2: */
3913
1.78M
            y_steps = fixed2int(y_steps);
3914
1.78M
            assert(y_steps >= 0);
3915
1.78M
            if (y_steps) {
3916
                /* We want to change sx by x_steps in y_steps steps.
3917
                 * So each step, we sub x_steps/y_steps from sx. That's x_inc + n_inc/ey. */
3918
1.09M
                int x_inc = x_steps/y_steps;
3919
1.09M
                int n_inc = x_steps - (x_inc * y_steps);
3920
1.09M
                int f = y_steps/2;
3921
1.09M
                int d = y_steps;
3922
3923
1.09M
                cursor_always_step_tr(cr, -fixed_1, sx, id, skip);
3924
1.09M
                skip = 0;
3925
1.09M
                sx -= x_inc;
3926
1.09M
                f -= n_inc;
3927
1.09M
                if (f < 0)
3928
218k
                    f += d, sx--;
3929
1.09M
                cursor_left_tr(cr, sx, id);
3930
1.09M
                y_steps--;
3931
3932
8.07M
                while (y_steps) {
3933
6.98M
                    cursor_always_inrange_step_left_tr(cr, -fixed_1, sx, id);
3934
6.98M
                    sx -= x_inc;
3935
6.98M
                    f -= n_inc;
3936
6.98M
                    if (f < 0)
3937
3.01M
                        f += d, sx--;
3938
6.98M
                    cursor_left_tr(cr, sx, id);
3939
6.98M
                    y_steps--;
3940
6.98M
                }
3941
1.09M
            }
3942
3943
            /* Phase 3 */
3944
1.78M
            if (phase3_y_steps == 0) {
3945
76.8k
endFallingVerticalOnEdgeOfPixel:
3946
76.8k
                cursor_always_step_inrange_vertical_tr(cr, 0, sx, id);
3947
76.8k
                cursor_null_tr(cr);
3948
1.71M
            } else {
3949
1.71M
                cursor_step_tr(cr, -phase3_y_steps, sx, id, skip);
3950
1.71M
                cursor_left_tr(cr, ex, id);
3951
1.71M
                assert(cr->left == ex && cr->lid == id && cr->right == sx && cr->rid == id);
3952
1.71M
            }
3953
1.78M
        }
3954
7.78M
endFalling: {}
3955
7.78M
    }
3956
3957
19.1M
end:
3958
19.1M
    if (truncated) {
3959
1.19M
        cr->left = saved_ex;
3960
1.19M
        cr->lid = id;
3961
1.19M
        cr->right = saved_ex;
3962
1.19M
        cr->rid = id;
3963
1.19M
        cr->y = saved_ey;
3964
1.19M
    }
3965
19.1M
}
3966
3967
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)
3968
3.18k
{
3969
3.18k
        int ax = (sx + c1x)>>1;
3970
3.18k
        int ay = (sy + c1y)>>1;
3971
3.18k
        int bx = (c1x + c2x)>>1;
3972
3.18k
        int by = (c1y + c2y)>>1;
3973
3.18k
        int cx = (c2x + ex)>>1;
3974
3.18k
        int cy = (c2y + ey)>>1;
3975
3.18k
        int dx = (ax + bx)>>1;
3976
3.18k
        int dy = (ay + by)>>1;
3977
3.18k
        int fx = (bx + cx)>>1;
3978
3.18k
        int fy = (by + cy)>>1;
3979
3.18k
        int gx = (dx + fx)>>1;
3980
3.18k
        int gy = (dy + fy)>>1;
3981
3982
3.18k
        assert(depth >= 0);
3983
3.18k
        if (depth == 0) {
3984
2.11k
            *id += 1;
3985
2.11k
            mark_line_tr_app(cr, sx, sy, ex, ey, *id);
3986
2.11k
        } else {
3987
1.07k
            depth--;
3988
1.07k
            mark_curve_tr_app(cr, sx, sy, ax, ay, dx, dy, gx, gy, depth, id);
3989
1.07k
            mark_curve_tr_app(cr, gx, gy, fx, fy, cx, cy, ex, ey, depth, id);
3990
1.07k
        }
3991
3.18k
}
3992
3993
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)
3994
0
{
3995
0
    fixed64 ax = (sx + c1x)>>1;
3996
0
    fixed64 ay = (sy + c1y)>>1;
3997
0
    fixed64 bx = (c1x + c2x)>>1;
3998
0
    fixed64 by = (c1y + c2y)>>1;
3999
0
    fixed64 cx = (c2x + ex)>>1;
4000
0
    fixed64 cy = (c2y + ey)>>1;
4001
0
    fixed64 dx = (ax + bx)>>1;
4002
0
    fixed64 dy = (ay + by)>>1;
4003
0
    fixed64 fx = (bx + cx)>>1;
4004
0
    fixed64 fy = (by + cy)>>1;
4005
0
    fixed64 gx = (dx + fx)>>1;
4006
0
    fixed64 gy = (dy + fy)>>1;
4007
4008
0
    assert(depth >= 0);
4009
0
    if (depth == 0) {
4010
0
        *id += 1;
4011
0
        mark_line_tr_app(cr, (fixed)sx, (fixed)sy, (fixed)ex, (fixed)ey, *id);
4012
0
    } else {
4013
0
        depth--;
4014
0
        mark_curve_big_tr_app(cr, sx, sy, ax, ay, dx, dy, gx, gy, depth, id);
4015
0
        mark_curve_big_tr_app(cr, gx, gy, fx, fy, cx, cy, ex, ey, depth, id);
4016
0
    }
4017
0
}
4018
4019
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)
4020
1.04k
{
4021
1.04k
    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));
4022
4023
1.04k
    if (test < 0)
4024
0
        mark_curve_big_tr_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth, id);
4025
1.04k
    else
4026
1.04k
        mark_curve_tr_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth, id);
4027
1.04k
}
4028
4029
static int make_table_tr_app(gx_device     * pdev,
4030
                             gx_path       * path,
4031
                             gs_fixed_rect * ibox,
4032
                             int           * scanlines,
4033
                             int          ** index,
4034
                             int          ** table)
4035
2.35M
{
4036
2.35M
    return make_table_template(pdev, path, ibox, 4, 0, scanlines, index, table);
4037
2.35M
}
4038
4039
static void
4040
fill_zero_app_tr(int *row, const fixed *x)
4041
681
{
4042
681
    int n = *row = (*row)+2; /* Increment the count */
4043
681
    row[4*n-7] = x[0];
4044
681
    row[4*n-6] = 0;
4045
681
    row[4*n-5] = x[1];
4046
681
    row[4*n-4] = 0;
4047
681
    row[4*n-3] = x[1];
4048
681
    row[4*n-2] = (1<<1)|1;
4049
681
    row[4*n-1] = x[1];
4050
681
    row[4*n  ] = 1;
4051
681
}
4052
4053
int gx_scan_convert_tr_app(gx_device     * gs_restrict pdev,
4054
                           gx_path       * gs_restrict path,
4055
                     const gs_fixed_rect * gs_restrict clip,
4056
                           gx_edgebuffer * gs_restrict edgebuffer,
4057
                           fixed                    fixed_flat)
4058
2.45M
{
4059
2.45M
    gs_fixed_rect  ibox;
4060
2.45M
    gs_fixed_rect  bbox;
4061
2.45M
    int            scanlines;
4062
2.45M
    const subpath *psub;
4063
2.45M
    int           *index;
4064
2.45M
    int           *table;
4065
2.45M
    int            i;
4066
2.45M
    cursor_tr      cr;
4067
2.45M
    int            code;
4068
2.45M
    int            id = 0;
4069
2.45M
    int            zero;
4070
4071
2.45M
    edgebuffer->index = NULL;
4072
2.45M
    edgebuffer->table = NULL;
4073
4074
    /* Bale out if no actual path. We see this with the clist */
4075
2.45M
    if (path->first_subpath == NULL)
4076
86.3k
        return 0;
4077
4078
2.37M
    zero = make_bbox(path, clip, &bbox, &ibox, 0);
4079
2.37M
    if (zero < 0)
4080
0
        return zero;
4081
4082
2.37M
    if (ibox.q.y <= ibox.p.y)
4083
14.3k
        return 0;
4084
4085
2.35M
    code = make_table_tr_app(pdev, path, &ibox, &scanlines, &index, &table);
4086
2.35M
    if (code != 0) /* > 0 means "retry with smaller height" */
4087
1
        return code;
4088
4089
2.35M
    if (scanlines == 0)
4090
0
        return 0;
4091
4092
2.35M
    if (zero) {
4093
677
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero_app_tr);
4094
2.35M
    } else {
4095
4096
    /* Step 2 continued: Now we run through the path, filling in the real
4097
     * values. */
4098
2.35M
    cr.scanlines = scanlines;
4099
2.35M
    cr.index     = index;
4100
2.35M
    cr.table     = table;
4101
2.35M
    cr.base      = ibox.p.y;
4102
4.90M
    for (psub = path->first_subpath; psub != 0;) {
4103
2.54M
        const segment *pseg = (const segment *)psub;
4104
2.54M
        fixed ex = pseg->pt.x;
4105
2.54M
        fixed ey = pseg->pt.y;
4106
2.54M
        fixed ix = ex;
4107
2.54M
        fixed iy = ey;
4108
2.54M
        fixed sx, sy;
4109
4110
2.54M
        if ((ey & 0xff) == 0) {
4111
84.3k
            cr.left  = max_fixed;
4112
84.3k
            cr.right = min_fixed;
4113
2.45M
        } else {
4114
2.45M
            cr.left = cr.right = ex;
4115
2.45M
        }
4116
2.54M
        cr.lid = cr.rid = id+1;
4117
2.54M
        cr.y = ey;
4118
2.54M
        cr.d = DIRN_UNSET;
4119
2.54M
        cr.first = 1;
4120
2.54M
        cr.saved = 0;
4121
4122
62.6M
        while ((pseg = pseg->next) != 0 &&
4123
62.6M
               pseg->type != s_start
4124
60.0M
            ) {
4125
60.0M
            sx = ex;
4126
60.0M
            sy = ey;
4127
60.0M
            ex = pseg->pt.x;
4128
60.0M
            ey = pseg->pt.y;
4129
4130
60.0M
            switch (pseg->type) {
4131
0
                default:
4132
0
                case s_start: /* Should never happen */
4133
0
                case s_dash:  /* We should never be seeing a dash here */
4134
0
                    assert("This should never happen" == NULL);
4135
0
                    break;
4136
1.04k
                case s_curve: {
4137
1.04k
                    const curve_segment *const pcur = (const curve_segment *)pseg;
4138
1.04k
                    int k = gx_curve_log2_samples(sx, sy, pcur, fixed_flat);
4139
4140
1.04k
                    mark_curve_top_tr_app(&cr, sx, sy, pcur->p1.x, pcur->p1.y, pcur->p2.x, pcur->p2.y, ex, ey, k, &id);
4141
1.04k
                    break;
4142
0
                }
4143
0
                case s_gap:
4144
57.8M
                case s_line:
4145
60.0M
                case s_line_close:
4146
60.0M
                    mark_line_tr_app(&cr, sx, sy, ex, ey, ++id);
4147
60.0M
                    break;
4148
60.0M
            }
4149
60.0M
        }
4150
        /* And close any open segments */
4151
2.54M
        mark_line_tr_app(&cr, ex, ey, ix, iy, ++id);
4152
2.54M
        cursor_flush_tr(&cr, ex, id);
4153
2.54M
        psub = (const subpath *)pseg;
4154
2.54M
    }
4155
2.35M
    }
4156
4157
    /* Step 2 complete: We now have a complete list of intersection data in
4158
     * table, indexed by index. */
4159
4160
2.35M
    edgebuffer->base   = ibox.p.y;
4161
2.35M
    edgebuffer->height = scanlines;
4162
2.35M
    edgebuffer->xmin   = ibox.p.x;
4163
2.35M
    edgebuffer->xmax   = ibox.q.x;
4164
2.35M
    edgebuffer->index  = index;
4165
2.35M
    edgebuffer->table  = table;
4166
4167
#ifdef DEBUG_SCAN_CONVERTER
4168
    if (debugging_scan_converter) {
4169
        dlprintf("Before sorting\n");
4170
        gx_edgebuffer_print_tr_app(edgebuffer);
4171
    }
4172
#endif
4173
4174
    /* Step 3: Sort the intersects on x */
4175
69.1M
    for (i=0; i < scanlines; i++) {
4176
66.7M
        int *row = &table[index[i]];
4177
66.7M
        int  rowlen = *row++;
4178
4179
        /* Bubblesort short runs, qsort longer ones. */
4180
        /* Figure of '6' comes from testing */
4181
66.7M
        if (rowlen <= 6) {
4182
66.5M
            int j, k;
4183
137M
            for (j = 0; j < rowlen-1; j++) {
4184
70.4M
                int * gs_restrict t = &row[j<<2];
4185
147M
                for (k = j+1; k < rowlen; k++) {
4186
77.2M
                    int * gs_restrict s = &row[k<<2];
4187
77.2M
                    int tmp;
4188
77.2M
                    if (t[0] < s[0])
4189
33.9M
                        continue;
4190
43.3M
                    if (t[0] > s[0])
4191
41.8M
                        goto swap0213;
4192
1.51M
                    if (t[2] < s[2])
4193
316k
                        continue;
4194
1.20M
                    if (t[2] > s[2])
4195
662k
                        goto swap213;
4196
538k
                    if (t[1] < s[1])
4197
473k
                        continue;
4198
64.9k
                    if (t[1] > s[1])
4199
64.9k
                        goto swap13;
4200
9
                    if (t[3] <= s[3])
4201
9
                        continue;
4202
0
                    if (0) {
4203
41.8M
swap0213:
4204
41.8M
                        tmp = t[0], t[0] = s[0], s[0] = tmp;
4205
42.5M
swap213:
4206
42.5M
                        tmp = t[2], t[2] = s[2], s[2] = tmp;
4207
42.5M
swap13:
4208
42.5M
                        tmp = t[1], t[1] = s[1], s[1] = tmp;
4209
42.5M
                    }
4210
42.5M
                    tmp = t[3], t[3] = s[3], s[3] = tmp;
4211
42.5M
                }
4212
70.4M
            }
4213
66.5M
        } else
4214
192k
            qsort(row, rowlen, 4*sizeof(int), edgecmp_tr);
4215
66.7M
    }
4216
4217
2.35M
    return 0;
4218
2.35M
}
4219
4220
/* Step 5: Filter the intersections according to the rules */
4221
int
4222
gx_filter_edgebuffer_tr_app(gx_device       * gs_restrict pdev,
4223
                            gx_edgebuffer   * gs_restrict edgebuffer,
4224
                            int                        rule)
4225
2.45M
{
4226
2.45M
    int i;
4227
2.45M
    int marked_id = 0;
4228
4229
#ifdef DEBUG_SCAN_CONVERTER
4230
    if (debugging_scan_converter) {
4231
        dlprintf("Before filtering:\n");
4232
        gx_edgebuffer_print_tr_app(edgebuffer);
4233
    }
4234
#endif
4235
4236
69.2M
    for (i=0; i < edgebuffer->height; i++) {
4237
66.7M
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
4238
66.7M
        int  rowlen   = *row++;
4239
66.7M
        int *rowstart = row;
4240
66.7M
        int *rowout   = row;
4241
66.7M
        int  ll, llid, lr, lrid, rlid, rr, rrid, wind, marked_to;
4242
4243
        /* Avoid double setting pixels, by keeping where we have marked to. */
4244
66.7M
        marked_to = INT_MIN;
4245
136M
        while (rowlen > 0) {
4246
69.4M
            if (rule == gx_rule_even_odd) {
4247
                /* Even Odd */
4248
3.14M
                ll   = *row++;
4249
3.14M
                llid = (*row++)>>1;
4250
3.14M
                lr   = *row++;
4251
3.14M
                lrid = *row++;
4252
3.14M
                rowlen--;
4253
4254
                /* We will fill solidly from ll to at least lr, possibly further */
4255
3.14M
                assert(rowlen > 0);
4256
3.14M
                (void)row++; /* rl not needed here */
4257
3.14M
                (void)row++;
4258
3.14M
                rr   = *row++;
4259
3.14M
                rrid = *row++;
4260
3.14M
                rowlen--;
4261
3.14M
                if (rr > lr) {
4262
3.13M
                    lr   = rr;
4263
3.13M
                    lrid = rrid;
4264
3.13M
                }
4265
66.3M
            } else {
4266
                /* Non-Zero */
4267
66.3M
                int w;
4268
4269
66.3M
                ll   = *row++;
4270
66.3M
                llid = *row++;
4271
66.3M
                lr   = *row++;
4272
66.3M
                lrid = *row++;
4273
66.3M
                wind = -(llid&1) | 1;
4274
66.3M
                llid >>= 1;
4275
66.3M
                rowlen--;
4276
4277
66.3M
                assert(rowlen > 0);
4278
67.3M
                do {
4279
67.3M
                    (void)row++; /* rl not needed */
4280
67.3M
                    rlid = *row++;
4281
67.3M
                    rr   = *row++;
4282
67.3M
                    rrid = *row++;
4283
67.3M
                    w = -(rlid&1) | 1;
4284
67.3M
                    rlid >>= 1;
4285
67.3M
                    rowlen--;
4286
67.3M
                    if (rr > lr) {
4287
65.9M
                        lr   = rr;
4288
65.9M
                        lrid = rrid;
4289
65.9M
                    }
4290
67.3M
                    wind += w;
4291
67.3M
                    if (wind == 0)
4292
66.3M
                        break;
4293
67.3M
                } while (rowlen > 0);
4294
66.3M
            }
4295
4296
69.4M
            if (lr < marked_to)
4297
12.8k
                continue;
4298
4299
69.4M
            if (marked_to >= ll) {
4300
303k
                if (rowout == rowstart) {
4301
10
                    ll   = marked_to;
4302
10
                    llid = --marked_id;
4303
303k
                } else {
4304
303k
                    rowout -= 4;
4305
303k
                    ll   = rowout[0];
4306
303k
                    llid = rowout[1];
4307
303k
                }
4308
303k
            }
4309
4310
69.4M
            if (lr >= ll) {
4311
69.4M
                *rowout++ = ll;
4312
69.4M
                *rowout++ = llid;
4313
69.4M
                *rowout++ = lr;
4314
69.4M
                *rowout++ = lrid;
4315
69.4M
                marked_to = lr;
4316
69.4M
            }
4317
69.4M
        }
4318
66.7M
        rowstart[-1] = (rowout - rowstart)>>2;
4319
66.7M
    }
4320
2.45M
    return 0;
4321
2.45M
}
4322
4323
/* Step 6: Fill */
4324
int
4325
gx_fill_edgebuffer_tr_app(gx_device       * gs_restrict pdev,
4326
                    const gx_device_color * gs_restrict pdevc,
4327
                          gx_edgebuffer   * gs_restrict edgebuffer,
4328
                          int                        log_op)
4329
2.45M
{
4330
2.45M
    int i, j, code;
4331
2.45M
    int mfb = pdev->max_fill_band;
4332
4333
#ifdef DEBUG_SCAN_CONVERTER
4334
    if (debugging_scan_converter) {
4335
        dlprintf("Filling:\n");
4336
        gx_edgebuffer_print_filtered_tr_app(edgebuffer);
4337
    }
4338
#endif
4339
4340
10.1M
    for (i=0; i < edgebuffer->height; ) {
4341
7.68M
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
4342
7.68M
        int  rowlen = *row++;
4343
7.68M
        int *row2;
4344
7.68M
        int *rowptr;
4345
7.68M
        int *row2ptr;
4346
7.68M
        int y_band_max;
4347
4348
7.68M
        if (mfb) {
4349
0
            y_band_max = (i & ~(mfb-1)) + mfb;
4350
0
            if (y_band_max > edgebuffer->height)
4351
0
                y_band_max = edgebuffer->height;
4352
7.68M
        } else {
4353
7.68M
            y_band_max = edgebuffer->height;
4354
7.68M
        }
4355
4356
        /* See how many scanlines match i */
4357
66.7M
        for (j = i+1; j < y_band_max; j++) {
4358
64.4M
            int row2len;
4359
4360
64.4M
            row2    = &edgebuffer->table[edgebuffer->index[j]];
4361
64.4M
            row2len = *row2++;
4362
64.4M
            row2ptr = row2;
4363
64.4M
            rowptr  = row;
4364
4365
64.4M
            if (rowlen != row2len)
4366
197k
                break;
4367
124M
            while (row2len > 0) {
4368
65.6M
                if (rowptr[1] != row2ptr[1] || rowptr[3] != row2ptr[3])
4369
5.13M
                    goto rowdifferent;
4370
60.5M
                rowptr  += 4;
4371
60.5M
                row2ptr += 4;
4372
60.5M
                row2len--;
4373
60.5M
            }
4374
64.2M
        }
4375
7.68M
rowdifferent:{}
4376
4377
        /* So j is the first scanline that doesn't match i */
4378
4379
        /* The first scanline is always sent as rectangles */
4380
16.7M
        while (rowlen > 0) {
4381
9.03M
            int left  = row[0];
4382
9.03M
            int right = row[2];
4383
9.03M
            row += 4;
4384
9.03M
            left = fixed2int(left);
4385
9.03M
            right = fixed2int(right + fixed_1 - 1);
4386
9.03M
            rowlen--;
4387
4388
9.03M
            right -= left;
4389
9.03M
            if (right > 0) {
4390
#ifdef DEBUG_OUTPUT_SC_AS_PS
4391
                dlprintf("0.001 setlinewidth 1 0 0 setrgbcolor %%red %%PS\n");
4392
                coord("moveto", int2fixed(left), int2fixed(edgebuffer->base+i));
4393
                coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+i));
4394
                coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+i+1));
4395
                coord("lineto", int2fixed(left), int2fixed(edgebuffer->base+i+1));
4396
                dlprintf("closepath stroke %%PS\n");
4397
#endif
4398
9.03M
                if (log_op < 0)
4399
0
                    code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
4400
9.03M
                else
4401
9.03M
                    code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
4402
9.03M
                if (code < 0)
4403
0
                    return code;
4404
9.03M
            }
4405
9.03M
        }
4406
4407
        /* The middle section (all but the first and last
4408
         * scanlines) can be sent as a trapezoid. */
4409
7.68M
        if (i + 2 < j) {
4410
2.24M
            gs_fixed_edge le;
4411
2.24M
            gs_fixed_edge re;
4412
2.24M
            fixed ybot = int2fixed(edgebuffer->base+i+1);
4413
2.24M
            fixed ytop = int2fixed(edgebuffer->base+j-1);
4414
2.24M
            int *row3, *row4;
4415
2.24M
            int offset = 1;
4416
2.24M
            row    = &edgebuffer->table[edgebuffer->index[i]];
4417
2.24M
            row2    = &edgebuffer->table[edgebuffer->index[i+1]];
4418
2.24M
            row3    = &edgebuffer->table[edgebuffer->index[j-2]];
4419
2.24M
            row4    = &edgebuffer->table[edgebuffer->index[j-1]];
4420
2.24M
            rowlen = *row;
4421
4.61M
            while (rowlen > 0) {
4422
                /* The fill rules used by fill_trap state that if a
4423
                 * pixel centre is touched by a boundary, the pixel
4424
                 * will be filled iff the boundary is horizontal and
4425
                 * the filled region is above it, or the boundary is
4426
                 * not horizontal, and the filled region is to the
4427
                 * right of it.
4428
                 *
4429
                 * We need to fill "any part of a pixel", not just
4430
                 * "centre covered", so we need to adjust our edges
4431
                 * by half a pixel in both X and Y.
4432
                 *
4433
                 * X is relatively easy. We move the left edge back by
4434
                 * just less than half, so ...00 goes to ...81 and
4435
                 * therefore does not cause an extra pixel to get filled.
4436
                 *
4437
                 * Similarly, we move the right edge forward by half, so
4438
                 *  ...00 goes to ...80 and therefore does not cause an
4439
                 * extra pixel to get filled.
4440
                 *
4441
                 * For y, we can adjust edges up or down as appropriate.
4442
                 * We move up by half, so ...0 goes to ..80 and therefore
4443
                 * does not cause an extra pixel to get filled. We move
4444
                 * down by just less than a half so that ...0 goes to
4445
                 * ...81 and therefore does not cause an extra pixel to
4446
                 * get filled.
4447
                 *
4448
                 * We use ybot = ...80 and ytop = ...81 in the trap call
4449
                 * so that it just covers the pixel centres.
4450
                 */
4451
2.36M
                if (row[offset] <= row4[offset]) {
4452
1.76M
                    le.start.x = row2[offset] - (fixed_half-1);
4453
1.76M
                    le.end.x   = row4[offset] - (fixed_half-1);
4454
1.76M
                    le.start.y = ybot + fixed_half;
4455
1.76M
                    le.end.y   = ytop + fixed_half;
4456
1.76M
                } else {
4457
594k
                    le.start.x = row [offset] - (fixed_half-1);
4458
594k
                    le.end.x   = row3[offset] - (fixed_half-1);
4459
594k
                    le.start.y = ybot - (fixed_half-1);
4460
594k
                    le.end.y   = ytop - (fixed_half-1);
4461
594k
                }
4462
2.36M
                if (row[offset+2] <= row4[offset+2]) {
4463
1.77M
                    re.start.x = row [offset+2] + fixed_half;
4464
1.77M
                    re.end.x   = row3[offset+2] + fixed_half;
4465
1.77M
                    re.start.y = ybot - (fixed_half-1);
4466
1.77M
                    re.end.y   = ytop - (fixed_half-1);
4467
1.77M
                } else {
4468
591k
                    re.start.x = row2[offset+2] + fixed_half;
4469
591k
                    re.end.x   = row4[offset+2] + fixed_half;
4470
591k
                    re.start.y = ybot + fixed_half;
4471
591k
                    re.end.y   = ytop + fixed_half;
4472
591k
                }
4473
2.36M
                offset += 4;
4474
2.36M
                rowlen--;
4475
4476
2.36M
                assert(re.start.x >= le.start.x);
4477
2.36M
                assert(re.end.x >= le.end.x);
4478
2.36M
                assert(le.start.y <= ybot + fixed_half);
4479
2.36M
                assert(re.start.y <= ybot + fixed_half);
4480
2.36M
                assert(le.end.y >= ytop - (fixed_half - 1));
4481
2.36M
                assert(re.end.y >= ytop - (fixed_half - 1));
4482
4483
#ifdef DEBUG_OUTPUT_SC_AS_PS
4484
                dlprintf("0.001 setlinewidth 0 1 0 setrgbcolor %% green %%PS\n");
4485
                coord("moveto", le.start.x, le.start.y);
4486
                coord("lineto", le.end.x, le.end.y);
4487
                coord("lineto", re.end.x, re.end.y);
4488
                coord("lineto", re.start.x, re.start.y);
4489
                dlprintf("closepath stroke %%PS\n");
4490
#endif
4491
2.36M
                code = dev_proc(pdev, fill_trapezoid)(
4492
2.36M
                                pdev,
4493
2.36M
                                &le,
4494
2.36M
                                &re,
4495
2.36M
                                ybot + fixed_half,
4496
2.36M
                                ytop - (fixed_half - 1),
4497
2.36M
                                0, /* bool swap_axes */
4498
2.36M
                                pdevc, /*const gx_drawing_color *pdcolor */
4499
2.36M
                                log_op);
4500
2.36M
                if (code < 0)
4501
0
                    return code;
4502
2.36M
            }
4503
2.24M
        }
4504
4505
7.68M
        if (i + 1 < j)
4506
3.19M
        {
4507
            /* The last scanline is always sent as rectangles */
4508
3.19M
            row    = &edgebuffer->table[edgebuffer->index[j-1]];
4509
3.19M
            rowlen = *row++;
4510
6.63M
            while (rowlen > 0) {
4511
3.44M
                int left  = row[0];
4512
3.44M
                int right = row[2];
4513
3.44M
                row += 4;
4514
3.44M
                left = fixed2int(left);
4515
3.44M
                right = fixed2int(right + fixed_1 - 1);
4516
3.44M
                rowlen--;
4517
4518
3.44M
                right -= left;
4519
3.44M
                if (right > 0) {
4520
#ifdef DEBUG_OUTPUT_SC_AS_PS
4521
                    dlprintf("0.001 setlinewidth 0 0 1 setrgbcolor %% blue %%PS\n");
4522
                    coord("moveto", int2fixed(left), int2fixed(edgebuffer->base+j-1));
4523
                    coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+j-1));
4524
                    coord("lineto", int2fixed(left+right), int2fixed(edgebuffer->base+j));
4525
                    coord("lineto", int2fixed(left), int2fixed(edgebuffer->base+j));
4526
                    dlprintf("closepath stroke %%PS\n");
4527
#endif
4528
3.44M
                    if (log_op < 0)
4529
0
                        code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+j-1, right, 1, pdevc->colors.pure);
4530
3.44M
                    else
4531
3.44M
                        code = gx_fill_rectangle_device_rop(left, edgebuffer->base+j-1, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
4532
3.44M
                    if (code < 0)
4533
0
                        return code;
4534
3.44M
                }
4535
3.44M
            }
4536
3.19M
        }
4537
7.68M
        i = j;
4538
7.68M
    }
4539
2.45M
    return 0;
4540
2.45M
}
4541
4542
4543
void
4544
gx_edgebuffer_init(gx_edgebuffer * edgebuffer)
4545
2.59M
{
4546
2.59M
    edgebuffer->base   = 0;
4547
2.59M
    edgebuffer->height = 0;
4548
2.59M
    edgebuffer->index  = NULL;
4549
2.59M
    edgebuffer->table  = NULL;
4550
2.59M
}
4551
4552
void
4553
gx_edgebuffer_fin(gx_device     * pdev,
4554
                  gx_edgebuffer * edgebuffer)
4555
2.59M
{
4556
2.59M
    gs_free_object(pdev->memory, edgebuffer->table, "scanc intersects buffer");
4557
2.59M
    gs_free_object(pdev->memory, edgebuffer->index, "scanc index buffer");
4558
2.59M
    edgebuffer->index = NULL;
4559
2.59M
    edgebuffer->table = NULL;
4560
2.59M
}
4561
4562
gx_scan_converter_t gx_scan_converter =
4563
{
4564
    gx_scan_convert,
4565
    gx_filter_edgebuffer,
4566
    gx_fill_edgebuffer
4567
};
4568
4569
gx_scan_converter_t gx_scan_converter_app =
4570
{
4571
    gx_scan_convert_app,
4572
    gx_filter_edgebuffer_app,
4573
    gx_fill_edgebuffer_app
4574
};
4575
4576
gx_scan_converter_t gx_scan_converter_tr =
4577
{
4578
    gx_scan_convert_tr,
4579
    gx_filter_edgebuffer_tr,
4580
    gx_fill_edgebuffer_tr
4581
};
4582
4583
gx_scan_converter_t gx_scan_converter_tr_app =
4584
{
4585
    gx_scan_convert_tr_app,
4586
    gx_filter_edgebuffer_tr_app,
4587
    gx_fill_edgebuffer_tr_app
4588
};
4589
4590
int
4591
gx_scan_convert_and_fill(const gx_scan_converter_t *sc,
4592
                               gx_device       *dev,
4593
                               gx_path         *ppath,
4594
                         const gs_fixed_rect   *ibox,
4595
                               fixed            flat,
4596
                               int              rule,
4597
                         const gx_device_color *pdevc,
4598
                               int              lop)
4599
2.59M
{
4600
2.59M
    int code;
4601
2.59M
    gx_edgebuffer eb;
4602
2.59M
    gs_fixed_rect ibox2 = *ibox;
4603
2.59M
    int height;
4604
2.59M
    int mfb = dev->max_fill_band;
4605
4606
2.59M
    if (mfb != 0) {
4607
0
        ibox2.p.y &= ~(mfb-1);
4608
0
        ibox2.q.y = (ibox2.q.y+mfb-1) & ~(mfb-1);
4609
0
    }
4610
2.59M
    height = ibox2.q.y - ibox2.p.y;
4611
4612
2.59M
    do {
4613
2.59M
        gx_edgebuffer_init(&eb);
4614
2.59M
        while (1) {
4615
2.59M
            ibox2.q.y = ibox2.p.y + height;
4616
2.59M
            if (ibox2.q.y > ibox->q.y)
4617
0
                ibox2.q.y = ibox->q.y;
4618
2.59M
            code = sc->scan_convert(dev,
4619
2.59M
                                    ppath,
4620
2.59M
                                    &ibox2,
4621
2.59M
                                    &eb,
4622
2.59M
                                    flat);
4623
2.59M
            if (code <= 0)
4624
2.59M
                break;
4625
            /* Let's shrink the ibox and try again */
4626
1
            if (mfb && height == mfb) {
4627
                /* Can't shrink the height any more! */
4628
0
                code = gs_error_rangecheck;
4629
0
                break;
4630
0
            }
4631
1
            height = height/code;
4632
1
            if (mfb)
4633
0
                height = (height + mfb-1) & ~(mfb-1);
4634
1
            if (height < (mfb ? mfb : 1)) {
4635
0
                code = gs_error_VMerror;
4636
0
                break;
4637
0
            }
4638
1
        }
4639
2.59M
        if (code >= 0)
4640
2.59M
            code = sc->filter(dev,
4641
2.59M
                              &eb,
4642
2.59M
                              rule);
4643
2.59M
        if (code >= 0)
4644
2.59M
            code = sc->fill(dev,
4645
2.59M
                            pdevc,
4646
2.59M
                            &eb,
4647
2.59M
                            lop);
4648
2.59M
        gx_edgebuffer_fin(dev,&eb);
4649
2.59M
        ibox2.p.y += height;
4650
2.59M
    }
4651
2.59M
    while (ibox2.p.y < ibox->q.y);
4652
4653
2.59M
    return code;
4654
2.59M
}