Coverage Report

Created: 2026-04-01 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gxscanc.c
Line
Count
Source
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
1.87M
{
157
1.87M
    return *((int*)a) - *((int *)b);
158
1.87M
}
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
77.3k
{
204
77.3k
    if (sx < zf[0])
205
0
        zf[0] = sx;
206
77.3k
    if (ex < zf[0])
207
9.80k
        zf[0] = ex;
208
77.3k
    if (sx > zf[1])
209
0
        zf[1] = sx;
210
77.3k
    if (ex > zf[1])
211
11.9k
        zf[1] = ex;
212
77.3k
}
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
6.97k
{
271
6.97k
    const subpath *psub;
272
6.97k
    fixed zf[2];
273
274
    /* Step 2 continued: Now we run through the path, filling in the real
275
     * values. */
276
14.5k
    for (psub = path->first_subpath; psub != 0;) {
277
7.56k
        const segment *pseg = (const segment *)psub;
278
7.56k
        fixed ex = pseg->pt.x;
279
7.56k
        fixed sy = pseg->pt.y;
280
7.56k
        fixed ix = ex;
281
7.56k
        int iy = fixed2int(pseg->pt.y);
282
283
7.56k
        zf[0] = ex;
284
7.56k
        zf[1] = ex;
285
286
77.3k
        while ((pseg = pseg->next) != 0 &&
287
70.3k
               pseg->type != s_start
288
69.7k
            ) {
289
69.7k
            fixed sx = ex;
290
69.7k
            ex = pseg->pt.x;
291
292
69.7k
            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
63.2k
                case s_line:
307
69.7k
                case s_line_close:
308
69.7k
                    mark_line_zero(sx, ex, zf);
309
69.7k
                    break;
310
69.7k
            }
311
69.7k
        }
312
        /* And close any open segments */
313
7.56k
        mark_line_zero(ex, ix, zf);
314
7.56k
        fill(&table[index[iy-ibox->p.y]], zf);
315
7.56k
        psub = (const subpath *)pseg;
316
7.56k
    }
317
318
6.97k
    return 0;
319
6.97k
}
320
321
static void mark_line(fixed sx, fixed sy, fixed ex, fixed ey, int base_y, int height, int *table, int *index)
322
174M
{
323
174M
    int64_t delta;
324
174M
    int iy, ih;
325
174M
    fixed clip_sy, clip_ey;
326
174M
    int dirn = DIRN_UP;
327
174M
    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
174M
    if (fixed2int(sy + fixed_half-1) == fixed2int(ey + fixed_half-1))
341
152M
        return;
342
21.9M
    if (sy > ey) {
343
12.5M
        int t;
344
12.5M
        t = sy; sy = ey; ey = t;
345
12.5M
        t = sx; sx = ex; ex = t;
346
12.5M
        dirn = DIRN_DOWN;
347
12.5M
    }
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
21.9M
    clip_sy = ((sy + fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
352
    /* The last scanline marked (< ey) is: */
353
21.9M
    clip_ey = ((ey - fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
354
    /* Now allow for banding */
355
21.9M
    if (clip_sy < int2fixed(base_y) + fixed_half)
356
10.6M
        clip_sy = int2fixed(base_y) + fixed_half;
357
21.9M
    if (ey <= clip_sy)
358
10.2M
        return;
359
11.7M
    if (clip_ey > int2fixed(base_y + height - 1) + fixed_half)
360
4.25M
        clip_ey = int2fixed(base_y + height - 1) + fixed_half;
361
11.7M
    if (sy > clip_ey)
362
3.88M
        return;
363
7.84M
    delta = (int64_t)clip_sy - (int64_t)sy;
364
7.84M
    if (delta > 0)
365
7.14M
    {
366
7.14M
        int64_t dx = (int64_t)ex - (int64_t)sx;
367
7.14M
        int64_t dy = (int64_t)ey - (int64_t)sy;
368
7.14M
        int advance = (int)((dx * delta + (dy>>1)) / dy);
369
7.14M
        sx += advance;
370
7.14M
        sy += delta;
371
7.14M
    }
372
7.84M
    delta = (int64_t)ey - (int64_t)clip_ey;
373
7.84M
    if (delta > 0)
374
7.84M
    {
375
7.84M
        int64_t dx = (int64_t)ex - (int64_t)sx;
376
7.84M
        int64_t dy = (int64_t)ey - (int64_t)sy;
377
7.84M
        int advance = (int)((dx * delta + (dy>>1)) / dy);
378
7.84M
        ex -= advance;
379
7.84M
        ey -= delta;
380
7.84M
    }
381
7.84M
    ex -= sx;
382
7.84M
    ey -= sy;
383
7.84M
    ih = fixed2int(ey);
384
7.84M
    assert(ih >= 0);
385
7.84M
    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
7.84M
    assert(iy >= 0 && iy < height);
391
    /* We always cross at least one scanline */
392
7.84M
    row = &table[index[iy]];
393
7.84M
    *row = (*row)+1; /* Increment the count */
394
7.84M
    row[*row] = (sx&~1) | dirn;
395
7.84M
    if (ih == 0)
396
6.12M
        return;
397
1.71M
    if (ex >= 0) {
398
1.01M
        int x_inc, n_inc, f;
399
400
        /* We want to change sx by ex in ih steps. So each step, we add
401
         * ex/ih to sx. That's x_inc + n_inc/ih.
402
         */
403
1.01M
        x_inc = ex/ih;
404
1.01M
        n_inc = ex-(x_inc*ih);
405
1.01M
        f     = ih>>1;
406
1.01M
        delta = ih;
407
7.37M
        do {
408
7.37M
            int count;
409
7.37M
            iy++;
410
7.37M
            sx += x_inc;
411
7.37M
            f  -= n_inc;
412
7.37M
            if (f < 0) {
413
1.38M
                f += ih;
414
1.38M
                sx++;
415
1.38M
            }
416
7.37M
            assert(iy >= 0 && iy < height);
417
7.37M
            row = &table[index[iy]];
418
7.37M
            count = *row = (*row)+1; /* Increment the count */
419
7.37M
            row[count] = (sx&~1) | dirn;
420
7.37M
        } while (--delta);
421
1.01M
    } else {
422
707k
        int x_dec, n_dec, f;
423
424
707k
        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
707k
        x_dec = ex/ih;
429
707k
        n_dec = ex-(x_dec*ih);
430
707k
        f     = ih>>1;
431
707k
        delta = ih;
432
6.53M
        do {
433
6.53M
            int count;
434
6.53M
            iy++;
435
6.53M
            sx -= x_dec;
436
6.53M
            f  -= n_dec;
437
6.53M
            if (f < 0) {
438
3.27M
                f += ih;
439
3.27M
                sx--;
440
3.27M
            }
441
6.53M
            assert(iy >= 0 && iy < height);
442
6.53M
            row = &table[index[iy]];
443
6.53M
            count = *row = (*row)+1; /* Increment the count */
444
6.53M
            row[count] = (sx&~1) | dirn;
445
6.53M
        } while (--delta);
446
707k
    }
447
1.71M
}
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
13.0M
{
515
13.0M
    int           code;
516
13.0M
    int           ret = 0;
517
518
    /* Find the bbox - fixed */
519
13.0M
    code = gx_path_bbox(path, bbox);
520
13.0M
    if (code < 0)
521
0
        return code;
522
523
13.0M
    if (bbox->p.y == bbox->q.y) {
524
        /* Zero height path */
525
7.60k
        if (!clip ||
526
7.60k
            (bbox->p.y >= clip->p.y && bbox->q.y <= clip->q.y)) {
527
            /* Either we're not clipping, or we are vertically inside the clip */
528
7.45k
            if (clip) {
529
7.45k
                if (bbox->p.x < clip->p.x)
530
775
                    bbox->p.x = clip->p.x;
531
7.45k
                if (bbox->q.x > clip->q.x)
532
496
                    bbox->q.x = clip->q.x;
533
7.45k
            }
534
7.45k
            if (bbox->p.x <= bbox->q.x) {
535
                /* Zero height rectangle, not clipped completely away */
536
6.97k
                ret = 1;
537
6.97k
            }
538
7.45k
        }
539
7.60k
    }
540
541
13.0M
    if (clip) {
542
13.0M
        if (bbox->p.y < clip->p.y)
543
3.93M
            bbox->p.y = clip->p.y;
544
13.0M
        if (bbox->q.y > clip->q.y)
545
3.94M
            bbox->q.y = clip->q.y;
546
13.0M
    }
547
548
    /* Convert to bbox - int */
549
13.0M
    ibox->p.x = fixed2int(bbox->p.x+adjust-(adjust?1:0));
550
13.0M
    ibox->p.y = fixed2int(bbox->p.y+adjust-(adjust?1:0));
551
13.0M
    ibox->q.x = fixed2int(bbox->q.x-adjust+fixed_1);
552
13.0M
    ibox->q.y = fixed2int(bbox->q.y-adjust+fixed_1);
553
554
13.0M
    return ret;
555
13.0M
}
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
12.9M
{
567
12.9M
    int             scanlines;
568
12.9M
    const subpath * gs_restrict psub;
569
12.9M
    int           * gs_restrict index;
570
12.9M
    int           * gs_restrict table;
571
12.9M
    int             i;
572
12.9M
    int64_t         offset;
573
12.9M
    int             delta;
574
12.9M
    fixed           base_y;
575
576
12.9M
    *scanlinesp = 0;
577
12.9M
    *indexp     = NULL;
578
12.9M
    *tablep     = NULL;
579
580
12.9M
    if (pdev->max_fill_band != 0)
581
0
        ibox->p.y &= ~(pdev->max_fill_band-1);
582
12.9M
    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
12.9M
    scanlines = ibox->q.y-base_y;
593
    /* +1+adjust simplifies the loop below */
594
12.9M
    index = (int *)gs_alloc_bytes(pdev->memory,
595
12.9M
                                  (size_t)(scanlines+1+adjust) * sizeof(*index),
596
12.9M
                                  "scanc index buffer");
597
12.9M
    if (index == NULL)
598
0
        return_error(gs_error_VMerror);
599
600
    /* Step 1 continued: Blank the index */
601
12.9M
    memset(index, 0, (scanlines+1)*sizeof(int));
602
603
    /* Step 1 continued: Run through the path, filling in the index */
604
34.4M
    for (psub = path->first_subpath; psub != 0;) {
605
21.4M
        const segment * gs_restrict pseg = (const segment *)psub;
606
21.4M
        fixed          ey = pseg->pt.y;
607
21.4M
        fixed          iy = ey;
608
21.4M
        int            iey = fixed2int(iy) - base_y;
609
610
21.4M
        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
21.4M
        if (iey >= 0 && iey < scanlines)
615
10.0M
        {
616
10.0M
            index[iey] += 2;
617
10.0M
            if (iey+1 < scanlines)
618
6.70M
                index[iey+1] -= 2;
619
10.0M
        }
620
621
1.13G
        while ((pseg = pseg->next) != 0 &&
622
1.12G
               pseg->type != s_start
623
1.11G
            ) {
624
1.11G
            fixed sy = ey;
625
1.11G
            ey = pseg->pt.y;
626
627
1.11G
            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
109k
                case s_curve: {
634
109k
                    const curve_segment *const gs_restrict pcur = (const curve_segment *)pseg;
635
109k
                    fixed c1y = pcur->p1.y;
636
109k
                    fixed c2y = pcur->p2.y;
637
109k
                    fixed maxy = sy, miny = sy;
638
109k
                    int imaxy, iminy;
639
109k
                    if (miny > c1y)
640
52.4k
                        miny = c1y;
641
109k
                    if (miny > c2y)
642
39.4k
                        miny = c2y;
643
109k
                    if (miny > ey)
644
30.0k
                        miny = ey;
645
109k
                    if (maxy < c1y)
646
53.2k
                        maxy = c1y;
647
109k
                    if (maxy < c2y)
648
37.9k
                        maxy = c2y;
649
109k
                    if (maxy < ey)
650
30.6k
                        maxy = ey;
651
#ifdef DEBUG_SCAN_CONVERTER
652
                    if (debugging_scan_converter)
653
                        dlprintf2("Curve (%x->%x) ", miny, maxy);
654
#endif
655
109k
                    iminy = fixed2int(miny) - base_y;
656
109k
                    if (iminy <= 0)
657
72.7k
                        iminy = 0;
658
36.7k
                    else
659
36.7k
                        iminy -= adjust;
660
109k
                    if (iminy < scanlines) {
661
90.1k
                        imaxy = fixed2int(maxy) - base_y;
662
90.1k
                        if (imaxy >= 0) {
663
#ifdef DEBUG_SCAN_CONVERTER
664
                            if (debugging_scan_converter)
665
                                dlprintf1("+%x ", iminy);
666
#endif
667
45.8k
                            index[iminy]+=3;
668
45.8k
                            if (imaxy < scanlines) {
669
#ifdef DEBUG_SCAN_CONVERTER
670
                                if (debugging_scan_converter)
671
                                    dlprintf1("-%x ", imaxy+1);
672
#endif
673
18.4k
                                index[imaxy+1+adjust]-=3;
674
18.4k
                            }
675
45.8k
                        }
676
90.1k
                    }
677
#ifdef DEBUG_SCAN_CONVERTER
678
                    if (debugging_scan_converter)
679
                        dlprintf("\n");
680
#endif
681
109k
                    break;
682
0
                }
683
0
                case s_gap:
684
1.09G
                case s_line:
685
1.11G
                case s_line_close: {
686
1.11G
                    fixed miny, maxy;
687
1.11G
                    int imaxy, iminy;
688
1.11G
                    if (sy == ey) {
689
#ifdef DEBUG_SCAN_CONVERTER
690
                        if (debugging_scan_converter)
691
                            dlprintf("Line (Horiz)\n");
692
#endif
693
96.4M
                        break;
694
96.4M
                    }
695
1.01G
                    if (sy < ey)
696
505M
                        miny = sy, maxy = ey;
697
511M
                    else
698
511M
                        miny = ey, maxy = sy;
699
#ifdef DEBUG_SCAN_CONVERTER
700
                    if (debugging_scan_converter)
701
                        dlprintf2("Line (%x->%x) ", miny, maxy);
702
#endif
703
1.01G
                    iminy = fixed2int(miny) - base_y;
704
1.01G
                    if (iminy <= 0)
705
468M
                        iminy = 0;
706
548M
                    else
707
548M
                        iminy -= adjust;
708
1.01G
                    if (iminy < scanlines) {
709
698M
                        imaxy = fixed2int(maxy) - base_y;
710
698M
                        if (imaxy >= 0) {
711
#ifdef DEBUG_SCAN_CONVERTER
712
                            if (debugging_scan_converter)
713
                                dlprintf1("+%x ", iminy);
714
#endif
715
277M
                            index[iminy]++;
716
277M
                            if (imaxy < scanlines) {
717
#ifdef DEBUG_SCAN_CONVERTER
718
                                if (debugging_scan_converter)
719
                                    dlprintf1("-%x ", imaxy+1);
720
#endif
721
264M
                                index[imaxy+1+adjust]--;
722
264M
                            }
723
277M
                        }
724
698M
                    }
725
#ifdef DEBUG_SCAN_CONVERTER
726
                    if (debugging_scan_converter)
727
                        dlprintf("\n");
728
#endif
729
1.01G
                    break;
730
1.11G
                }
731
1.11G
            }
732
1.11G
        }
733
734
        /* And close any segments that need it */
735
21.4M
        if (ey != iy) {
736
849k
            fixed miny, maxy;
737
849k
            int imaxy, iminy;
738
849k
            if (iy < ey)
739
398k
                miny = iy, maxy = ey;
740
450k
            else
741
450k
                miny = ey, maxy = iy;
742
#ifdef DEBUG_SCAN_CONVERTER
743
            if (debugging_scan_converter)
744
                dlprintf2("Close (%x->%x) ", miny, maxy);
745
#endif
746
849k
            iminy = fixed2int(miny) - base_y;
747
849k
            if (iminy <= 0)
748
626k
                iminy = 0;
749
223k
            else
750
223k
                iminy -= adjust;
751
849k
            if (iminy < scanlines) {
752
637k
                imaxy = fixed2int(maxy) - base_y;
753
637k
                if (imaxy >= 0) {
754
#ifdef DEBUG_SCAN_CONVERTER
755
                    if (debugging_scan_converter)
756
                        dlprintf1("+%x ", iminy);
757
#endif
758
348k
                    index[iminy]++;
759
348k
                    if (imaxy < scanlines) {
760
#ifdef DEBUG_SCAN_CONVERTER
761
                        if (debugging_scan_converter)
762
                            dlprintf1("-%x ", imaxy+1);
763
#endif
764
157k
                        index[imaxy+1+adjust]--;
765
157k
                    }
766
348k
                }
767
637k
            }
768
#ifdef DEBUG_SCAN_CONVERTER
769
            if (debugging_scan_converter)
770
                dlprintf("\n");
771
#endif
772
849k
        }
773
#ifdef DEBUG_SCAN_CONVERTER
774
        if (debugging_scan_converter)
775
            dlprintf("\n");
776
#endif
777
21.4M
        psub = (const subpath *)pseg;
778
21.4M
    }
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
12.9M
    offset = 0;
787
12.9M
    delta  = 0;
788
485M
    for (i=0; i < scanlines+adjust; i++) {
789
472M
        delta    += intersection_size*index[i];  /* delta = Num ints on this scanline. */
790
472M
        index[i]  = offset;                      /* Offset into table for this lines data. */
791
472M
        offset   += delta+1;                     /* Adjust offset for next line. */
792
472M
    }
793
    /* Ensure we always have enough room for our zero height rectangle hack. */
794
12.9M
    if (offset < 2*intersection_size)
795
61
        offset += 2*intersection_size;
796
12.9M
    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
12.9M
    if (scanlines > 16 && offset > 1024*1024) { /* Arbitrary */
803
202
        gs_free_object(pdev->memory, index, "scanc index buffer");
804
202
        return offset/(1024*1024) + 1;
805
202
    }
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
12.9M
    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
12.9M
    table = (int *)gs_alloc_bytes(pdev->memory, offset,
821
12.9M
                                  "scanc intersects buffer");
822
12.9M
    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
436M
    for (i=0; i < scanlines; i++) {
831
423M
        table[index[i]] = 0;
832
423M
    }
833
834
12.9M
    *scanlinesp = scanlines;
835
12.9M
    *tablep     = table;
836
12.9M
    *indexp     = index;
837
838
12.9M
    return 0;
839
12.9M
}
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
410k
{
848
410k
    return make_table_template(pdev, path, ibox, 1, 1, scanlines, index, table);
849
410k
}
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
420k
{
865
420k
    gs_fixed_rect  ibox;
866
420k
    gs_fixed_rect  bbox;
867
420k
    int            scanlines;
868
420k
    const subpath *psub;
869
420k
    int           *index;
870
420k
    int           *table;
871
420k
    int            i;
872
420k
    int            code;
873
420k
    int            zero;
874
875
420k
    edgebuffer->index = NULL;
876
420k
    edgebuffer->table = NULL;
877
878
    /* Bale out if no actual path. We see this with the clist */
879
420k
    if (path->first_subpath == NULL)
880
1.02k
        return 0;
881
882
419k
    zero = make_bbox(path, clip, &bbox, &ibox, fixed_half);
883
419k
    if (zero < 0)
884
0
        return zero;
885
886
419k
    if (ibox.q.y <= ibox.p.y)
887
8.95k
        return 0;
888
889
410k
    code = make_table(pdev, path, &ibox, &scanlines, &index, &table);
890
410k
    if (code != 0) /* >0 means "retry with smaller height" */
891
0
        return code;
892
893
410k
    if (scanlines == 0)
894
0
        return 0;
895
896
410k
    if (zero) {
897
0
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero);
898
410k
    } else {
899
900
    /* Step 2 continued: Now we run through the path, filling in the real
901
     * values. */
902
1.07M
    for (psub = path->first_subpath; psub != 0;) {
903
667k
        const segment *pseg = (const segment *)psub;
904
667k
        fixed ex = pseg->pt.x;
905
667k
        fixed ey = pseg->pt.y;
906
667k
        fixed ix = ex;
907
667k
        fixed iy = ey;
908
909
206M
        while ((pseg = pseg->next) != 0 &&
910
205M
               pseg->type != s_start
911
205M
            ) {
912
205M
            fixed sx = ex;
913
205M
            fixed sy = ey;
914
205M
            ex = pseg->pt.x;
915
205M
            ey = pseg->pt.y;
916
917
205M
            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
205M
                case s_line:
932
205M
                case s_line_close:
933
205M
                    if (sy != ey)
934
174M
                        mark_line(sx, sy, ex, ey, ibox.p.y, scanlines, table, index);
935
205M
                    break;
936
205M
            }
937
205M
        }
938
        /* And close any open segments */
939
667k
        if (iy != ey)
940
167k
            mark_line(ex, ey, ix, iy, ibox.p.y, scanlines, table, index);
941
667k
        psub = (const subpath *)pseg;
942
667k
    }
943
410k
    }
944
945
    /* Step 2 complete: We now have a complete list of intersection data in
946
     * table, indexed by index. */
947
948
410k
    edgebuffer->base   = ibox.p.y;
949
410k
    edgebuffer->height = scanlines;
950
410k
    edgebuffer->xmin   = ibox.p.x;
951
410k
    edgebuffer->xmax   = ibox.q.x;
952
410k
    edgebuffer->index  = index;
953
410k
    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
6.91M
    for (i=0; i < scanlines; i++) {
964
6.50M
        int *row = &table[index[i]];
965
6.50M
        int  rowlen = *row++;
966
967
        /* Bubblesort short runs, qsort longer ones. */
968
        /* FIXME: Check "6" below */
969
6.50M
        if (rowlen <= 6) {
970
6.39M
            int j, k;
971
20.8M
            for (j = 0; j < rowlen-1; j++) {
972
14.4M
                int t = row[j];
973
42.4M
                for (k = j+1; k < rowlen; k++) {
974
27.9M
                    int s = row[k];
975
27.9M
                    if (t > s)
976
13.3M
                         row[k] = t, t = row[j] = s;
977
27.9M
                }
978
14.4M
            }
979
6.39M
        } else
980
109k
            qsort(row, rowlen, sizeof(int), intcmp);
981
6.50M
    }
982
983
410k
    return 0;
984
410k
}
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
420k
{
992
420k
    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
6.92M
    for (i=0; i < edgebuffer->height; i++) {
1002
6.50M
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
1003
6.50M
        int *rowstart = row;
1004
6.50M
        int  rowlen   = *row++;
1005
6.50M
        int *rowout   = row;
1006
1007
17.0M
        while (rowlen > 0)
1008
10.5M
        {
1009
10.5M
            int left, right;
1010
1011
10.5M
            if (rule == gx_rule_even_odd) {
1012
                /* Even Odd */
1013
0
                left  = (*row++)&~1;
1014
0
                right = (*row++)&~1;
1015
0
                rowlen -= 2;
1016
10.5M
            } else {
1017
                /* Non-Zero */
1018
10.5M
                int w;
1019
1020
10.5M
                left = *row++;
1021
10.5M
                w = ((left&1)-1) | (left&1);
1022
10.5M
                rowlen--;
1023
11.1M
                do {
1024
11.1M
                    right  = *row++;
1025
11.1M
                    rowlen--;
1026
11.1M
                    w += ((right&1)-1) | (right&1);
1027
11.1M
                } while (w != 0);
1028
10.5M
                left &= ~1;
1029
10.5M
                right &= ~1;
1030
10.5M
            }
1031
1032
10.5M
            if (right > left) {
1033
10.5M
                *rowout++ = left;
1034
10.5M
                *rowout++ = right;
1035
10.5M
            }
1036
10.5M
        }
1037
6.50M
        *rowstart = (rowout-rowstart)-1;
1038
6.50M
    }
1039
420k
    return 0;
1040
420k
}
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
420k
{
1049
420k
    int i, code;
1050
1051
6.92M
    for (i=0; i < edgebuffer->height; i++) {
1052
6.50M
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
1053
6.50M
        int  rowlen = *row++;
1054
1055
17.0M
        while (rowlen > 0) {
1056
10.5M
            int left, right;
1057
1058
10.5M
            left  = *row++;
1059
10.5M
            right = *row++;
1060
10.5M
            rowlen -= 2;
1061
10.5M
            left  = fixed2int(left + fixed_half);
1062
10.5M
            right = fixed2int(right + fixed_half);
1063
10.5M
            right -= left;
1064
10.5M
            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
10.2M
                if (log_op < 0)
1074
6.07M
                    code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
1075
4.14M
                else
1076
4.14M
                    code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
1077
10.2M
                if (code < 0)
1078
0
                    return code;
1079
10.2M
            }
1080
10.5M
        }
1081
6.50M
    }
1082
420k
    return 0;
1083
420k
}
1084
1085
/* Any part of a pixel routines */
1086
1087
static int edgecmp(const void *a, const void *b)
1088
836k
{
1089
836k
    int left  = ((int*)a)[0];
1090
836k
    int right = ((int*)b)[0];
1091
836k
    left -= right;
1092
836k
    if (left)
1093
813k
        return left;
1094
23.8k
    return ((int*)a)[1] - ((int*)b)[1];
1095
836k
}
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
4.01M
{
1159
4.01M
    int *row;
1160
4.01M
    int count;
1161
1162
4.01M
    if (iy >= 0 && iy < cr->scanlines) {
1163
2.35M
        if (cr->first) {
1164
            /* Save it for later in case we join up */
1165
167k
            cr->save_left  = cr->left;
1166
167k
            cr->save_right = cr->right;
1167
167k
            cr->save_iy    = iy;
1168
167k
            cr->save_d     = cr->d;
1169
167k
            cr->saved      = 1;
1170
2.18M
        } else if (cr->d != DIRN_UNSET) {
1171
            /* Enter it into the table */
1172
2.18M
            row = &cr->table[cr->index[iy]];
1173
2.18M
            *row = count = (*row)+1; /* Increment the count */
1174
2.18M
            row[2 * count - 1] = (cr->left&~1) | cr->d;
1175
2.18M
            row[2 * count    ] = cr->right;
1176
2.18M
        } else {
1177
704
            assert(cr->left == max_fixed && cr->right == min_fixed);
1178
704
        }
1179
2.35M
    }
1180
4.01M
    cr->first = 0;
1181
4.01M
}
1182
1183
static inline void
1184
cursor_output_inrange(cursor * gs_restrict cr, int iy)
1185
2.86M
{
1186
2.86M
    int *row;
1187
2.86M
    int count;
1188
1189
2.86M
    assert(iy >= 0 && iy < cr->scanlines);
1190
2.86M
    if (cr->first) {
1191
        /* Save it for later in case we join up */
1192
8.57k
        cr->save_left  = cr->left;
1193
8.57k
        cr->save_right = cr->right;
1194
8.57k
        cr->save_iy    = iy;
1195
8.57k
        cr->save_d     = cr->d;
1196
8.57k
        cr->saved      = 1;
1197
2.85M
    } else {
1198
        /* Enter it into the table */
1199
2.85M
        assert(cr->d != DIRN_UNSET);
1200
1201
2.85M
        row = &cr->table[cr->index[iy]];
1202
2.85M
        *row = count = (*row)+1; /* Increment the count */
1203
2.85M
        row[2 * count - 1] = (cr->left&~1) | cr->d;
1204
2.85M
        row[2 * count    ] = cr->right;
1205
2.85M
    }
1206
2.86M
    cr->first = 0;
1207
2.86M
}
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
669k
{
1213
669k
    int new_iy;
1214
669k
    int iy = fixed2int(cr->y) - cr->base;
1215
1216
669k
    cr->y += dy;
1217
669k
    new_iy = fixed2int(cr->y) - cr->base;
1218
669k
    if (new_iy != iy) {
1219
669k
        if (!skip)
1220
655k
            cursor_output(cr, iy);
1221
669k
        cr->left = x;
1222
669k
        cr->right = x;
1223
669k
    } 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
669k
}
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
18.4k
{
1235
18.4k
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
1236
1237
18.4k
    cr->y += dy;
1238
18.4k
}
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
152k
{
1246
152k
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
1247
1248
152k
    if (x < cr->left)
1249
112k
        cr->left = x;
1250
152k
    cr->y += dy;
1251
152k
}
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
151k
{
1259
151k
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
1260
1261
151k
    if (x > cr->right)
1262
147k
        cr->right = x;
1263
151k
    cr->y += dy;
1264
151k
}
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
665k
{
1270
665k
    int iy = fixed2int(cr->y) - cr->base;
1271
1272
665k
    if (!skip)
1273
507k
        cursor_output(cr, iy);
1274
665k
    cr->y += dy;
1275
665k
    cr->left = x;
1276
665k
    cr->right = x;
1277
665k
}
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
725k
{
1285
725k
    int iy = fixed2int(cr->y) - cr->base;
1286
1287
725k
    cursor_output(cr, iy);
1288
725k
    cr->y += dy;
1289
725k
}
1290
1291
/* Step the cursor in y, always by enough to cross a scanline, as
1292
 * part of a left moving line, knowing that we are moving from a
1293
 * position guaranteed to be in the valid y range. */
1294
static inline void
1295
cursor_always_inrange_step_left(cursor * gs_restrict cr, fixed dy, fixed x)
1296
1.51M
{
1297
1.51M
    int iy = fixed2int(cr->y) - cr->base;
1298
1299
1.51M
    cr->y += dy;
1300
1.51M
    cursor_output_inrange(cr, iy);
1301
1.51M
    cr->right = x;
1302
1.51M
}
1303
1304
/* Step the cursor in y, always by enough to cross a scanline, as
1305
 * part of a right moving line, knowing that we are moving from a
1306
 * position guaranteed to be in the valid y range. */
1307
static inline void
1308
cursor_always_inrange_step_right(cursor * gs_restrict cr, fixed dy, fixed x)
1309
1.34M
{
1310
1.34M
    int iy = fixed2int(cr->y) - cr->base;
1311
1312
1.34M
    cr->y += dy;
1313
1.34M
    cursor_output_inrange(cr, iy);
1314
1.34M
    cr->left = x;
1315
1.34M
}
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
2.24M
{
1329
2.24M
    if (x < cr->left)
1330
909k
        cr->left = x;
1331
2.24M
}
1332
1333
static inline void cursor_left(cursor * gs_restrict cr, fixed x)
1334
1.97M
{
1335
1.97M
    cr->left = x;
1336
1.97M
}
1337
1338
static inline void cursor_right_merge(cursor * gs_restrict cr, fixed x)
1339
2.28M
{
1340
2.28M
    if (x > cr->right)
1341
861k
        cr->right = x;
1342
2.28M
}
1343
1344
static inline void cursor_right(cursor * gs_restrict cr, fixed x)
1345
1.80M
{
1346
1.80M
    cr->right = x;
1347
1.80M
}
1348
1349
static inline int cursor_down(cursor * gs_restrict cr, fixed x)
1350
775k
{
1351
775k
    int skip = 0;
1352
775k
    if ((cr->y & 0xff) == 0)
1353
172k
        skip = 1;
1354
775k
    if (cr->d == DIRN_UP)
1355
104k
    {
1356
104k
        if (!skip)
1357
104k
            cursor_output(cr, fixed2int(cr->y) - cr->base);
1358
104k
        cr->left = x;
1359
104k
        cr->right = x;
1360
104k
    }
1361
775k
    cr->d = DIRN_DOWN;
1362
775k
    return skip;
1363
775k
}
1364
1365
static inline void cursor_up(cursor * gs_restrict cr, fixed x)
1366
771k
{
1367
771k
    if (cr->d == DIRN_DOWN)
1368
101k
    {
1369
101k
        cursor_output(cr, fixed2int(cr->y) - cr->base);
1370
101k
        cr->left = x;
1371
101k
        cr->right = x;
1372
101k
    }
1373
771k
    cr->d = DIRN_UP;
1374
771k
}
1375
1376
static inline void
1377
cursor_flush(cursor * gs_restrict cr, fixed x)
1378
1.79M
{
1379
1.79M
    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
1.79M
    if (cr->first) {
1385
249
        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
249
        if (cr->d == DIRN_UNSET && (cr->y & 0xff) == 0)
1389
6
            return;
1390
249
        assert(cr->left != max_fixed && cr->right != min_fixed);
1391
243
        if (iy >= 0 && iy < cr->scanlines) {
1392
5
            int *row = &cr->table[cr->index[iy]];
1393
5
            int count = *row = (*row)+2; /* Increment the count */
1394
5
            row[2 * count - 3] = (cr->left & ~1) | DIRN_UP;
1395
5
            row[2 * count - 2] = (cr->right & ~1);
1396
5
            row[2 * count - 1] = (cr->right & ~1) | DIRN_DOWN;
1397
5
            row[2 * count    ] = cr->right;
1398
5
        }
1399
243
        return;
1400
249
    }
1401
1402
    /* Merge save into current if we can */
1403
1.79M
    iy = fixed2int(cr->y) - cr->base;
1404
1.79M
    if (cr->saved && iy == cr->save_iy &&
1405
131k
        (cr->d == cr->save_d || cr->save_d == DIRN_UNSET)) {
1406
63.5k
        if (cr->left > cr->save_left)
1407
17.5k
            cr->left = cr->save_left;
1408
63.5k
        if (cr->right < cr->save_right)
1409
18.9k
            cr->right = cr->save_right;
1410
63.5k
        cursor_output(cr, iy);
1411
63.5k
        return;
1412
63.5k
    }
1413
1414
    /* Merge not possible */
1415
1.72M
    cursor_output(cr, iy);
1416
1.72M
    if (cr->saved) {
1417
112k
        cr->left  = cr->save_left;
1418
112k
        cr->right = cr->save_right;
1419
112k
        assert(cr->save_d != DIRN_UNSET);
1420
112k
        if (cr->save_d != DIRN_UNSET)
1421
112k
            cr->d = cr->save_d;
1422
112k
        cursor_output(cr, cr->save_iy);
1423
112k
    }
1424
1.72M
}
1425
1426
static inline void
1427
cursor_null(cursor *cr)
1428
751k
{
1429
751k
    cr->right = min_fixed;
1430
751k
    cr->left  = max_fixed;
1431
751k
    cr->d     = DIRN_UNSET;
1432
751k
}
1433
1434
static void mark_line_app(cursor * gs_restrict cr, fixed sx, fixed sy, fixed ex, fixed ey)
1435
20.0M
{
1436
20.0M
    int isy, iey;
1437
20.0M
    fixed saved_sy = sy;
1438
20.0M
    fixed saved_ex = ex;
1439
20.0M
    fixed saved_ey = ey;
1440
20.0M
    int truncated;
1441
1442
20.0M
    if (sx == ex && sy == ey)
1443
1.75M
        return;
1444
1445
18.2M
    isy = fixed2int(sy) - cr->base;
1446
18.2M
    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
18.2M
    if (sy == ey && (sy & 0xff) == 0)
1460
21.3k
        return;
1461
1462
18.2M
    assert(cr->y == sy &&
1463
18.2M
           ((cr->left <= sx && cr->right >= sx) || ((sy & 0xff) == 0)) &&
1464
18.2M
           cr->d >= DIRN_UNSET && cr->d <= DIRN_DOWN);
1465
1466
18.2M
    if (isy < iey) {
1467
        /* Rising line */
1468
7.41M
        if (iey < 0 || isy >= cr->scanlines) {
1469
            /* All line is outside. */
1470
6.91M
            if ((ey & 0xff) == 0)
1471
50.3k
                cursor_null(cr);
1472
6.86M
            else {
1473
6.86M
                cr->left = ex;
1474
6.86M
                cr->right = ex;
1475
6.86M
            }
1476
6.91M
            cr->y = ey;
1477
6.91M
            cr->first = 0;
1478
6.91M
            return;
1479
6.91M
        }
1480
502k
        if (isy < 0) {
1481
            /* Move sy up */
1482
168k
            int64_t y = (int64_t)ey - (int64_t)sy;
1483
168k
            fixed new_sy = int2fixed(cr->base);
1484
168k
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
1485
168k
            sx += (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1486
168k
            sy = new_sy;
1487
168k
            cursor_null(cr);
1488
168k
            cr->y = sy;
1489
168k
            isy = 0;
1490
168k
        }
1491
502k
        truncated = iey > cr->scanlines;
1492
502k
        if (truncated) {
1493
            /* Move ey down */
1494
156k
            int64_t y = ey - sy;
1495
156k
            fixed new_ey = int2fixed(cr->base + cr->scanlines);
1496
156k
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
1497
156k
            saved_ex = ex;
1498
156k
            saved_ey = ey;
1499
156k
            ex -= (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1500
156k
            ey = new_ey;
1501
156k
            iey = cr->scanlines;
1502
156k
        }
1503
10.8M
    } else {
1504
        /* Falling line */
1505
10.8M
        if (isy < 0 || iey >= cr->scanlines) {
1506
            /* All line is outside. */
1507
9.70M
            if ((ey & 0xff) == 0)
1508
32.3k
                cursor_null(cr);
1509
9.67M
            else {
1510
9.67M
                cr->left = ex;
1511
9.67M
                cr->right = ex;
1512
9.67M
            }
1513
9.70M
            cr->y = ey;
1514
9.70M
            cr->first = 0;
1515
9.70M
            return;
1516
9.70M
        }
1517
1.13M
        truncated = iey < 0;
1518
1.13M
        if (truncated) {
1519
            /* Move ey up */
1520
168k
            int64_t y = (int64_t)ey - (int64_t)sy;
1521
168k
            fixed new_ey = int2fixed(cr->base);
1522
168k
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
1523
168k
            ex -= (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1524
168k
            ey = new_ey;
1525
168k
            iey = 0;
1526
168k
        }
1527
1.13M
        if (isy >= cr->scanlines) {
1528
            /* Move sy down */
1529
169k
            int64_t y = (int64_t)ey - (int64_t)sy;
1530
169k
            fixed new_sy = int2fixed(cr->base + cr->scanlines);
1531
169k
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
1532
169k
            sx += (int)((((int64_t)(ex-sx))*dy + y/2)/y);
1533
169k
            sy = new_sy;
1534
169k
            cursor_null(cr);
1535
169k
            cr->y = sy;
1536
169k
            isy = cr->scanlines;
1537
169k
        }
1538
1.13M
    }
1539
1540
1.63M
    cursor_left_merge(cr, sx);
1541
1.63M
    cursor_right_merge(cr, sx);
1542
1543
1.63M
    assert(cr->left <= sx);
1544
1.63M
    assert(cr->right >= sx);
1545
1.63M
    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.63M
    if (isy == iey) {
1568
651k
        if (saved_sy == saved_ey) {
1569
            /* Horizontal line. Don't change cr->d, don't flush. */
1570
90.4k
            if ((ey & 0xff) == 0)
1571
0
                goto no_merge;
1572
560k
        } else if (saved_sy > saved_ey) {
1573
            /* Falling line, flush if previous was rising */
1574
280k
            int skip = cursor_down(cr, sx);
1575
280k
            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
13.5k
                if (sx <= ex) {
1579
8.13k
                    cursor_right_merge(cr, ex);
1580
8.13k
                } else {
1581
5.38k
                    cursor_left_merge(cr, ex);
1582
5.38k
                }
1583
13.5k
                if (!skip)
1584
13.3k
                    cursor_output(cr, fixed2int(cr->y) - cr->base);
1585
13.5k
                cursor_null(cr);
1586
13.5k
                goto no_merge;
1587
13.5k
            }
1588
280k
        } else {
1589
            /* Rising line, flush if previous was falling */
1590
279k
            cursor_up(cr, sx);
1591
279k
            if ((ey & 0xff) == 0) {
1592
185
                cursor_null(cr);
1593
185
                goto no_merge;
1594
185
            }
1595
279k
        }
1596
637k
        if (sx <= ex) {
1597
336k
            cursor_right_merge(cr, ex);
1598
336k
        } else {
1599
300k
            cursor_left_merge(cr, ex);
1600
300k
        }
1601
651k
no_merge:
1602
651k
        cr->y = ey;
1603
651k
        if (sy > saved_ey)
1604
280k
            goto endFalling;
1605
985k
    } 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
491k
        int phase1_y_steps = (-sy) & (fixed_1 - 1);
1619
491k
        int phase3_y_steps = ey & (fixed_1 - 1);
1620
491k
        ufixed y_steps = (ufixed)ey - (ufixed)sy;
1621
1622
491k
        cursor_up(cr, sx);
1623
1624
491k
        if (sx == ex) {
1625
            /* Vertical line. (Rising) */
1626
1627
            /* Phase 1: */
1628
46.3k
            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
19.6k
                cursor_step(cr, phase1_y_steps, sx, 0);
1632
19.6k
                sy += phase1_y_steps;
1633
19.6k
                y_steps -= phase1_y_steps;
1634
19.6k
                if (y_steps == 0) {
1635
880
                    cursor_null(cr);
1636
880
                    goto end;
1637
880
                }
1638
19.6k
            }
1639
1640
            /* Phase 3: precalculation */
1641
45.4k
            y_steps -= phase3_y_steps;
1642
1643
            /* Phase 2: */
1644
45.4k
            y_steps = fixed2int(y_steps);
1645
45.4k
            assert(y_steps >= 0);
1646
45.4k
            if (y_steps > 0) {
1647
39.9k
                cursor_always_step(cr, fixed_1, sx, 0);
1648
39.9k
                y_steps--;
1649
323k
                while (y_steps) {
1650
283k
                    cursor_always_step_inrange_vertical(cr, fixed_1, sx);
1651
283k
                    y_steps--;
1652
283k
                }
1653
39.9k
            }
1654
1655
            /* Phase 3 */
1656
45.4k
            assert(cr->left == sx && cr->right == sx);
1657
45.4k
            if (phase3_y_steps == 0)
1658
25.7k
                cursor_null(cr);
1659
19.7k
            else
1660
19.7k
                cr->y += phase3_y_steps;
1661
444k
        } else if (sx < ex) {
1662
            /* Lines increasing in x. (Rightwards, rising) */
1663
218k
            int phase1_x_steps, phase3_x_steps;
1664
218k
            fixed x_steps = ex - sx;
1665
1666
            /* Phase 1: */
1667
218k
            if (phase1_y_steps) {
1668
157k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1669
157k
                sx += phase1_x_steps;
1670
157k
                cursor_right_merge(cr, sx);
1671
157k
                x_steps -= phase1_x_steps;
1672
157k
                cursor_step(cr, phase1_y_steps, sx, 0);
1673
157k
                sy += phase1_y_steps;
1674
157k
                y_steps -= phase1_y_steps;
1675
157k
                if (y_steps == 0) {
1676
4.35k
                    cursor_null(cr);
1677
4.35k
                    goto end;
1678
4.35k
                }
1679
157k
            }
1680
1681
            /* Phase 3: precalculation */
1682
213k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1683
213k
            x_steps -= phase3_x_steps;
1684
213k
            y_steps -= phase3_y_steps;
1685
213k
            assert((y_steps & (fixed_1 - 1)) == 0);
1686
1687
            /* Phase 2: */
1688
213k
            y_steps = fixed2int(y_steps);
1689
213k
            assert(y_steps >= 0);
1690
213k
            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
142k
                int x_inc = x_steps/y_steps;
1694
142k
                int n_inc = x_steps - (x_inc * y_steps);
1695
142k
                int f = y_steps/2;
1696
142k
                int d = y_steps;
1697
1698
                /* Special casing the first iteration, allows us to simplify
1699
                 * the following loop. */
1700
142k
                sx += x_inc;
1701
142k
                f -= n_inc;
1702
142k
                if (f < 0)
1703
35.9k
                    f += d, sx++;
1704
142k
                cursor_right_merge(cr, sx);
1705
142k
                cursor_always_step(cr, fixed_1, sx, 0);
1706
142k
                y_steps--;
1707
1708
805k
                while (y_steps) {
1709
663k
                    sx += x_inc;
1710
663k
                    f -= n_inc;
1711
663k
                    if (f < 0)
1712
304k
                        f += d, sx++;
1713
663k
                    cursor_right(cr, sx);
1714
663k
                    cursor_always_inrange_step_right(cr, fixed_1, sx);
1715
663k
                    y_steps--;
1716
663k
                };
1717
142k
            }
1718
1719
            /* Phase 3 */
1720
213k
            assert(cr->left <= ex && cr->right >= sx);
1721
213k
            if (phase3_y_steps == 0)
1722
56.7k
                cursor_null(cr);
1723
157k
            else {
1724
157k
                cursor_right(cr, ex);
1725
157k
                cr->y += phase3_y_steps;
1726
157k
            }
1727
226k
        } else {
1728
            /* Lines decreasing in x. (Leftwards, rising) */
1729
226k
            int phase1_x_steps, phase3_x_steps;
1730
226k
            fixed x_steps = sx - ex;
1731
1732
            /* Phase 1: */
1733
226k
            if (phase1_y_steps) {
1734
155k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1735
155k
                x_steps -= phase1_x_steps;
1736
155k
                sx -= phase1_x_steps;
1737
155k
                cursor_left_merge(cr, sx);
1738
155k
                cursor_step(cr, phase1_y_steps, sx, 0);
1739
155k
                sy += phase1_y_steps;
1740
155k
                y_steps -= phase1_y_steps;
1741
155k
                if (y_steps == 0) {
1742
4.41k
                    cursor_null(cr);
1743
4.41k
                    goto end;
1744
4.41k
                }
1745
155k
            }
1746
1747
            /* Phase 3: precalculation */
1748
222k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1749
222k
            x_steps -= phase3_x_steps;
1750
222k
            y_steps -= phase3_y_steps;
1751
222k
            assert((y_steps & (fixed_1 - 1)) == 0);
1752
1753
            /* Phase 2: */
1754
222k
            y_steps = fixed2int(y_steps);
1755
222k
            assert(y_steps >= 0);
1756
222k
            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
150k
                int x_inc = x_steps/y_steps;
1760
150k
                int n_inc = x_steps - (x_inc * y_steps);
1761
150k
                int f = y_steps/2;
1762
150k
                int d = y_steps;
1763
1764
                /* Special casing the first iteration, allows us to simplify
1765
                 * the following loop. */
1766
150k
                sx -= x_inc;
1767
150k
                f -= n_inc;
1768
150k
                if (f < 0)
1769
32.0k
                    f += d, sx--;
1770
150k
                cursor_left_merge(cr, sx);
1771
150k
                cursor_always_step(cr, fixed_1, sx, 0);
1772
150k
                y_steps--;
1773
1774
921k
                while (y_steps) {
1775
771k
                    sx -= x_inc;
1776
771k
                    f -= n_inc;
1777
771k
                    if (f < 0)
1778
304k
                        f += d, sx--;
1779
771k
                    cursor_left(cr, sx);
1780
771k
                    cursor_always_inrange_step_left(cr, fixed_1, sx);
1781
771k
                    y_steps--;
1782
771k
                }
1783
150k
            }
1784
1785
            /* Phase 3 */
1786
222k
            assert(cr->right >= ex && cr->left <= sx);
1787
222k
            if (phase3_y_steps == 0)
1788
67.4k
                cursor_null(cr);
1789
154k
            else {
1790
154k
                cursor_left(cr, ex);
1791
154k
                cr->y += phase3_y_steps;
1792
154k
            }
1793
222k
        }
1794
494k
    } 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
494k
        int phase1_y_steps = sy & (fixed_1 - 1);
1803
494k
        int phase3_y_steps = (-ey) & (fixed_1 - 1);
1804
494k
        ufixed y_steps = (ufixed)sy - (ufixed)ey;
1805
1806
494k
        int skip = cursor_down(cr, sx);
1807
1808
494k
        if (sx == ex) {
1809
            /* Vertical line. (Falling) */
1810
1811
            /* Phase 1: */
1812
47.0k
            if (phase1_y_steps) {
1813
                /* Phase 1 in a falling line never moves us into a new scanline. */
1814
18.4k
                cursor_never_step_vertical(cr, -phase1_y_steps, sx);
1815
18.4k
                sy -= phase1_y_steps;
1816
18.4k
                y_steps -= phase1_y_steps;
1817
18.4k
                if (y_steps == 0)
1818
0
                    goto endFallingLeftOnEdgeOfPixel;
1819
18.4k
            }
1820
1821
            /* Phase 3: precalculation */
1822
47.0k
            y_steps -= phase3_y_steps;
1823
47.0k
            assert((y_steps & (fixed_1 - 1)) == 0);
1824
1825
            /* Phase 2: */
1826
47.0k
            y_steps = fixed2int(y_steps);
1827
47.0k
            assert(y_steps >= 0);
1828
47.0k
            if (y_steps) {
1829
40.7k
                cursor_always_step(cr, -fixed_1, sx, skip);
1830
40.7k
                skip = 0;
1831
40.7k
                y_steps--;
1832
324k
                while (y_steps) {
1833
283k
                    cursor_always_step_inrange_vertical(cr, -fixed_1, sx);
1834
283k
                    y_steps--;
1835
283k
                }
1836
40.7k
            }
1837
1838
            /* Phase 3 */
1839
47.0k
            if (phase3_y_steps == 0) {
1840
27.1k
endFallingLeftOnEdgeOfPixel:
1841
27.1k
                cursor_always_step_inrange_vertical(cr, 0, sx);
1842
27.1k
                cursor_null(cr);
1843
27.1k
            } else {
1844
19.8k
                cursor_step(cr, -phase3_y_steps, sx, skip);
1845
19.8k
                assert(cr->left == sx && cr->right == sx);
1846
19.8k
            }
1847
447k
        } else if (sx < ex) {
1848
            /* Lines increasing in x. (Rightwards, falling) */
1849
219k
            int phase1_x_steps, phase3_x_steps;
1850
219k
            fixed x_steps = ex - sx;
1851
1852
            /* Phase 1: */
1853
219k
            if (phase1_y_steps) {
1854
151k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1855
151k
                x_steps -= phase1_x_steps;
1856
151k
                sx += phase1_x_steps;
1857
                /* Phase 1 in a falling line never moves us into a new scanline. */
1858
151k
                cursor_never_step_right(cr, -phase1_y_steps, sx);
1859
151k
                sy -= phase1_y_steps;
1860
151k
                y_steps -= phase1_y_steps;
1861
151k
                if (y_steps == 0)
1862
0
                    goto endFallingRightOnEdgeOfPixel;
1863
151k
            }
1864
1865
            /* Phase 3: precalculation */
1866
219k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1867
219k
            x_steps -= phase3_x_steps;
1868
219k
            y_steps -= phase3_y_steps;
1869
219k
            assert((y_steps & (fixed_1 - 1)) == 0);
1870
1871
            /* Phase 2: */
1872
219k
            y_steps = fixed2int(y_steps);
1873
219k
            assert(y_steps >= 0);
1874
219k
            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
144k
                int x_inc = x_steps/y_steps;
1878
144k
                int n_inc = x_steps - (x_inc * y_steps);
1879
144k
                int f = y_steps/2;
1880
144k
                int d = y_steps;
1881
1882
144k
                cursor_always_step(cr, -fixed_1, sx, skip);
1883
144k
                skip = 0;
1884
144k
                sx += x_inc;
1885
144k
                f -= n_inc;
1886
144k
                if (f < 0)
1887
38.4k
                    f += d, sx++;
1888
144k
                cursor_right(cr, sx);
1889
144k
                y_steps--;
1890
1891
829k
                while (y_steps) {
1892
685k
                    cursor_always_inrange_step_right(cr, -fixed_1, sx);
1893
685k
                    sx += x_inc;
1894
685k
                    f -= n_inc;
1895
685k
                    if (f < 0)
1896
317k
                        f += d, sx++;
1897
685k
                    cursor_right(cr, sx);
1898
685k
                    y_steps--;
1899
685k
                }
1900
144k
            }
1901
1902
            /* Phase 3 */
1903
219k
            if (phase3_y_steps == 0) {
1904
62.9k
endFallingRightOnEdgeOfPixel:
1905
62.9k
                cursor_always_step_inrange_vertical(cr, 0, sx);
1906
62.9k
                cursor_null(cr);
1907
156k
            } else {
1908
156k
                cursor_step(cr, -phase3_y_steps, sx, skip);
1909
156k
                cursor_right(cr, ex);
1910
156k
                assert(cr->left == sx && cr->right == ex);
1911
156k
            }
1912
227k
        } else {
1913
            /* Lines decreasing in x. (Falling) */
1914
227k
            int phase1_x_steps, phase3_x_steps;
1915
227k
            fixed x_steps = sx - ex;
1916
1917
            /* Phase 1: */
1918
227k
            if (phase1_y_steps) {
1919
152k
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
1920
152k
                x_steps -= phase1_x_steps;
1921
152k
                sx -= phase1_x_steps;
1922
                /* Phase 1 in a falling line never moves us into a new scanline. */
1923
152k
                cursor_never_step_left(cr, -phase1_y_steps, sx);
1924
152k
                sy -= phase1_y_steps;
1925
152k
                y_steps -= phase1_y_steps;
1926
152k
                if (y_steps == 0)
1927
0
                    goto endFallingVerticalOnEdgeOfPixel;
1928
152k
            }
1929
1930
            /* Phase 3: precalculation */
1931
227k
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
1932
227k
            x_steps -= phase3_x_steps;
1933
227k
            y_steps -= phase3_y_steps;
1934
227k
            assert((y_steps & (fixed_1 - 1)) == 0);
1935
1936
            /* Phase 2: */
1937
227k
            y_steps = fixed2int(y_steps);
1938
227k
            assert(y_steps >= 0);
1939
227k
            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
148k
                int x_inc = x_steps/y_steps;
1943
148k
                int n_inc = x_steps - (x_inc * y_steps);
1944
148k
                int f = y_steps/2;
1945
148k
                int d = y_steps;
1946
1947
148k
                cursor_always_step(cr, -fixed_1, sx, skip);
1948
148k
                skip = 0;
1949
148k
                sx -= x_inc;
1950
148k
                f -= n_inc;
1951
148k
                if (f < 0)
1952
36.3k
                    f += d, sx--;
1953
148k
                cursor_left(cr, sx);
1954
148k
                y_steps--;
1955
1956
892k
                while (y_steps) {
1957
744k
                    cursor_always_inrange_step_left(cr, -fixed_1, sx);
1958
744k
                    sx -= x_inc;
1959
744k
                    f -= n_inc;
1960
744k
                    if (f < 0)
1961
335k
                        f += d, sx--;
1962
744k
                    cursor_left(cr, sx);
1963
744k
                    y_steps--;
1964
744k
                }
1965
148k
            }
1966
1967
            /* Phase 3 */
1968
227k
            if (phase3_y_steps == 0) {
1969
68.0k
endFallingVerticalOnEdgeOfPixel:
1970
68.0k
                cursor_always_step_inrange_vertical(cr, 0, sx);
1971
68.0k
                cursor_null(cr);
1972
159k
            } else {
1973
159k
                cursor_step(cr, -phase3_y_steps, sx, skip);
1974
159k
                cursor_left(cr, ex);
1975
159k
                assert(cr->left == ex && cr->right == sx);
1976
159k
            }
1977
227k
        }
1978
775k
endFalling: {}
1979
775k
    }
1980
1981
1.63M
end:
1982
1.63M
    if (truncated) {
1983
324k
        cr->left = saved_ex;
1984
324k
        cr->right = saved_ex;
1985
324k
        cr->y = saved_ey;
1986
324k
    }
1987
1.63M
}
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
13.5k
{
1991
13.5k
        int ax = (sx + c1x)>>1;
1992
13.5k
        int ay = (sy + c1y)>>1;
1993
13.5k
        int bx = (c1x + c2x)>>1;
1994
13.5k
        int by = (c1y + c2y)>>1;
1995
13.5k
        int cx = (c2x + ex)>>1;
1996
13.5k
        int cy = (c2y + ey)>>1;
1997
13.5k
        int dx = (ax + bx)>>1;
1998
13.5k
        int dy = (ay + by)>>1;
1999
13.5k
        int fx = (bx + cx)>>1;
2000
13.5k
        int fy = (by + cy)>>1;
2001
13.5k
        int gx = (dx + fx)>>1;
2002
13.5k
        int gy = (dy + fy)>>1;
2003
2004
13.5k
        assert(depth >= 0);
2005
13.5k
        if (depth == 0)
2006
13.1k
            mark_line_app(cr, sx, sy, ex, ey);
2007
414
        else {
2008
414
            depth--;
2009
414
            mark_curve_app(cr, sx, sy, ax, ay, dx, dy, gx, gy, depth);
2010
414
            mark_curve_app(cr, gx, gy, fx, fy, cx, cy, ex, ey, depth);
2011
414
        }
2012
13.5k
}
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
12.7k
{
2041
12.7k
    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
12.7k
    if (test < 0)
2044
0
        mark_curve_big_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth);
2045
12.7k
    else
2046
12.7k
        mark_curve_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth);
2047
12.7k
}
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
243k
{
2056
243k
    return make_table_template(pdev, path, ibox, 2, 0, scanlines, index, table);
2057
243k
}
2058
2059
static void
2060
fill_zero_app(int *row, const fixed *x)
2061
50
{
2062
50
    int n = *row = (*row)+2; /* Increment the count */
2063
50
    row[2*n-3] = (x[0]&~1);
2064
50
    row[2*n-2] = (x[1]&~1);
2065
50
    row[2*n-1] = (x[1]&~1)|1;
2066
50
    row[2*n  ] = x[1];
2067
50
}
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
245k
{
2075
245k
    gs_fixed_rect  ibox;
2076
245k
    gs_fixed_rect  bbox;
2077
245k
    int            scanlines;
2078
245k
    const subpath *psub;
2079
245k
    int           *index;
2080
245k
    int           *table;
2081
245k
    int            i;
2082
245k
    cursor         cr;
2083
245k
    int            code;
2084
245k
    int            zero;
2085
2086
245k
    edgebuffer->index = NULL;
2087
245k
    edgebuffer->table = NULL;
2088
2089
    /* Bale out if no actual path. We see this with the clist */
2090
245k
    if (path->first_subpath == NULL)
2091
605
        return 0;
2092
2093
245k
    zero = make_bbox(path, clip, &bbox, &ibox, 0);
2094
245k
    if (zero < 0)
2095
0
        return zero;
2096
2097
245k
    if (ibox.q.y <= ibox.p.y)
2098
1.33k
        return 0;
2099
2100
243k
    code = make_table_app(pdev, path, &ibox, &scanlines, &index, &table);
2101
243k
    if (code != 0) /* > 0 means "retry with smaller height" */
2102
0
        return code;
2103
2104
243k
    if (scanlines == 0)
2105
0
        return 0;
2106
2107
243k
    if (zero) {
2108
50
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero_app);
2109
243k
    } else {
2110
2111
    /* Step 2 continued: Now we run through the path, filling in the real
2112
     * values. */
2113
243k
    cr.scanlines = scanlines;
2114
243k
    cr.index     = index;
2115
243k
    cr.table     = table;
2116
243k
    cr.base      = ibox.p.y;
2117
2.03M
    for (psub = path->first_subpath; psub != 0;) {
2118
1.79M
        const segment *pseg = (const segment *)psub;
2119
1.79M
        fixed ex = pseg->pt.x;
2120
1.79M
        fixed ey = pseg->pt.y;
2121
1.79M
        fixed ix = ex;
2122
1.79M
        fixed iy = ey;
2123
1.79M
        fixed sx, sy;
2124
2125
1.79M
        if ((ey & 0xff) == 0) {
2126
10.6k
            cr.left  = max_fixed;
2127
10.6k
            cr.right = min_fixed;
2128
1.78M
        } else {
2129
1.78M
            cr.left = cr.right = ex;
2130
1.78M
        }
2131
1.79M
        cr.y = ey;
2132
1.79M
        cr.d = DIRN_UNSET;
2133
1.79M
        cr.first = 1;
2134
1.79M
        cr.saved = 0;
2135
2136
20.0M
        while ((pseg = pseg->next) != 0 &&
2137
19.7M
               pseg->type != s_start
2138
18.2M
            ) {
2139
18.2M
            sx = ex;
2140
18.2M
            sy = ey;
2141
18.2M
            ex = pseg->pt.x;
2142
18.2M
            ey = pseg->pt.y;
2143
2144
18.2M
            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
12.7k
                case s_curve: {
2151
12.7k
                    const curve_segment *const pcur = (const curve_segment *)pseg;
2152
12.7k
                    int k = gx_curve_log2_samples(sx, sy, pcur, fixed_flat);
2153
2154
12.7k
                    mark_curve_top_app(&cr, sx, sy, pcur->p1.x, pcur->p1.y, pcur->p2.x, pcur->p2.y, ex, ey, k);
2155
12.7k
                    break;
2156
0
                }
2157
0
                case s_gap:
2158
16.5M
                case s_line:
2159
18.2M
                case s_line_close:
2160
18.2M
                    mark_line_app(&cr, sx, sy, ex, ey);
2161
18.2M
                    break;
2162
18.2M
            }
2163
18.2M
        }
2164
        /* And close any open segments */
2165
1.79M
        mark_line_app(&cr, ex, ey, ix, iy);
2166
1.79M
        cursor_flush(&cr, ex);
2167
1.79M
        psub = (const subpath *)pseg;
2168
1.79M
    }
2169
243k
    }
2170
2171
    /* Step 2 complete: We now have a complete list of intersection data in
2172
     * table, indexed by index. */
2173
2174
243k
    edgebuffer->base   = ibox.p.y;
2175
243k
    edgebuffer->height = scanlines;
2176
243k
    edgebuffer->xmin   = ibox.p.x;
2177
243k
    edgebuffer->xmax   = ibox.q.x;
2178
243k
    edgebuffer->index  = index;
2179
243k
    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
2.32M
    for (i=0; i < scanlines; i++) {
2190
2.08M
        int *row = &table[index[i]];
2191
2.08M
        int  rowlen = *row++;
2192
2193
        /* Bubblesort short runs, qsort longer ones. */
2194
        /* FIXME: Verify the figure 6 below */
2195
2.08M
        if (rowlen <= 6) {
2196
2.05M
            int j, k;
2197
4.71M
            for (j = 0; j < rowlen-1; j++) {
2198
2.65M
                int * gs_restrict t = &row[j<<1];
2199
6.25M
                for (k = j+1; k < rowlen; k++) {
2200
3.60M
                    int * gs_restrict s = &row[k<<1];
2201
3.60M
                    int tmp;
2202
3.60M
                    if (t[0] < s[0])
2203
1.33M
                        continue;
2204
2.26M
                    if (t[0] > s[0])
2205
2.21M
                        goto swap01;
2206
51.7k
                    if (t[1] <= s[1])
2207
51.3k
                        continue;
2208
377
                    if (0) {
2209
2.21M
swap01:
2210
2.21M
                        tmp = t[0], t[0] = s[0], s[0] = tmp;
2211
2.21M
                    }
2212
2.21M
                    tmp = t[1], t[1] = s[1], s[1] = tmp;
2213
2.21M
                }
2214
2.65M
            }
2215
2.05M
        } else
2216
26.9k
            qsort(row, rowlen, 2*sizeof(int), edgecmp);
2217
2.08M
    }
2218
2219
243k
    return 0;
2220
243k
}
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
245k
{
2228
245k
    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
2.33M
    for (i=0; i < edgebuffer->height; i++) {
2238
2.08M
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
2239
2.08M
        int  rowlen   = *row++;
2240
2.08M
        int *rowstart = row;
2241
2.08M
        int *rowout   = row;
2242
2.08M
        int  ll, lr, rl, rr, wind, marked_to;
2243
2244
        /* Avoid double setting pixels, by keeping where we have marked to. */
2245
2.08M
        marked_to = INT_MIN;
2246
4.45M
        while (rowlen > 0) {
2247
2.37M
            if (rule == gx_rule_even_odd) {
2248
                /* Even Odd */
2249
87.7k
                ll = (*row++)&~1;
2250
87.7k
                lr = *row;
2251
87.7k
                row += 2;
2252
87.7k
                rowlen-=2;
2253
2254
                /* We will fill solidly from ll to at least lr, possibly further */
2255
87.7k
                assert(rowlen >= 0);
2256
87.7k
                rr = (*row++);
2257
87.7k
                if (rr > lr)
2258
76.0k
                    lr = rr;
2259
2.28M
            } else {
2260
                /* Non-Zero */
2261
2.28M
                int w;
2262
2263
2.28M
                ll = *row++;
2264
2.28M
                lr = *row++;
2265
2.28M
                wind = -(ll&1) | 1;
2266
2.28M
                ll &= ~1;
2267
2.28M
                rowlen--;
2268
2269
2.28M
                assert(rowlen > 0);
2270
2.57M
                do {
2271
2.57M
                    rl = *row++;
2272
2.57M
                    rr = *row++;
2273
2.57M
                    w = -(rl&1) | 1;
2274
2.57M
                    rl &= ~1;
2275
2.57M
                    rowlen--;
2276
2.57M
                    if (rr > lr)
2277
2.43M
                        lr = rr;
2278
2.57M
                    wind += w;
2279
2.57M
                    if (wind == 0)
2280
2.28M
                        break;
2281
2.57M
                } while (rowlen > 0);
2282
2.28M
            }
2283
2284
2.37M
            if (marked_to >= lr)
2285
780
                continue;
2286
2287
2.37M
            if (marked_to >= ll) {
2288
15.8k
                if (rowout == rowstart)
2289
0
                    ll = marked_to;
2290
15.8k
                else {
2291
15.8k
                    rowout -= 2;
2292
15.8k
                    ll = *rowout;
2293
15.8k
                }
2294
15.8k
            }
2295
2296
2.37M
            if (lr >= ll) {
2297
2.37M
                *rowout++ = ll;
2298
2.37M
                *rowout++ = lr;
2299
2.37M
                marked_to = lr;
2300
2.37M
            }
2301
2.37M
        }
2302
2.08M
        rowstart[-1] = rowout - rowstart;
2303
2.08M
    }
2304
245k
    return 0;
2305
245k
}
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
245k
{
2314
245k
    int i, code;
2315
2316
2.33M
    for (i=0; i < edgebuffer->height; i++) {
2317
2.08M
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
2318
2.08M
        int  rowlen = *row++;
2319
2.08M
        int  left, right;
2320
2321
4.44M
        while (rowlen > 0) {
2322
2.35M
            left  = *row++;
2323
2.35M
            right = *row++;
2324
2.35M
            left  = fixed2int(left);
2325
2.35M
            right = fixed2int(right + fixed_1 - 1);
2326
2.35M
            rowlen -= 2;
2327
2328
2.35M
            right -= left;
2329
2.35M
            if (right > 0) {
2330
2.35M
                if (log_op < 0)
2331
2.07M
                    code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
2332
280k
                else
2333
280k
                    code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
2334
2.35M
                if (code < 0)
2335
0
                    return code;
2336
2.35M
            }
2337
2.35M
        }
2338
2.08M
    }
2339
245k
    return 0;
2340
245k
}
2341
2342
/* Centre of a pixel trapezoid routines */
2343
2344
static int intcmp_tr(const void *a, const void *b)
2345
3.16M
{
2346
3.16M
    int left  = ((int*)a)[0];
2347
3.16M
    int right = ((int*)b)[0];
2348
3.16M
    if (left != right)
2349
3.15M
        return left - right;
2350
5.54k
    return ((int*)a)[1] - ((int*)b)[1];
2351
3.16M
}
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
40.6M
{
2383
40.6M
    int64_t delta;
2384
40.6M
    int iy, ih;
2385
40.6M
    fixed clip_sy, clip_ey;
2386
40.6M
    int dirn = DIRN_UP;
2387
40.6M
    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
40.6M
    if (fixed2int(sy + fixed_half-1) == fixed2int(ey + fixed_half-1))
2401
17.3M
        return;
2402
23.2M
    if (sy > ey) {
2403
12.1M
        int t;
2404
12.1M
        t = sy; sy = ey; ey = t;
2405
12.1M
        t = sx; sx = ex; ex = t;
2406
12.1M
        dirn = DIRN_DOWN;
2407
12.1M
    }
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
23.2M
    clip_sy = ((sy + fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
2412
    /* The last scanline marked (< ey) is: */
2413
23.2M
    clip_ey = ((ey - fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
2414
    /* Now allow for banding */
2415
23.2M
    if (clip_sy < int2fixed(base_y) + fixed_half)
2416
14.5M
        clip_sy = int2fixed(base_y) + fixed_half;
2417
23.2M
    if (ey <= clip_sy)
2418
14.2M
        return;
2419
9.02M
    if (clip_ey > int2fixed(base_y + height - 1) + fixed_half)
2420
7.79M
        clip_ey = int2fixed(base_y + height - 1) + fixed_half;
2421
9.02M
    if (sy > clip_ey)
2422
7.55M
        return;
2423
1.47M
    delta = (int64_t)clip_sy - (int64_t)sy;
2424
1.47M
    if (delta > 0)
2425
1.44M
    {
2426
1.44M
        int64_t dx = (int64_t)ex - (int64_t)sx;
2427
1.44M
        int64_t dy = (int64_t)ey - (int64_t)sy;
2428
1.44M
        int advance = (int)((dx * delta + (dy>>1)) / dy);
2429
1.44M
        sx += advance;
2430
1.44M
        sy += delta;
2431
1.44M
    }
2432
1.47M
    delta = (int64_t)ey - (int64_t)clip_ey;
2433
1.47M
    if (delta > 0)
2434
1.47M
    {
2435
1.47M
        int64_t dx = (int64_t)ex - (int64_t)sx;
2436
1.47M
        int64_t dy = (int64_t)ey - (int64_t)sy;
2437
1.47M
        int advance = (int)((dx * delta + (dy>>1)) / dy);
2438
1.47M
        ex -= advance;
2439
1.47M
        ey -= delta;
2440
1.47M
    }
2441
1.47M
    ex -= sx;
2442
1.47M
    ey -= sy;
2443
1.47M
    ih = fixed2int(ey);
2444
1.47M
    assert(ih >= 0);
2445
1.47M
    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
1.47M
    assert(iy >= 0 && iy < height);
2451
1.47M
    id = (id<<1) | dirn;
2452
    /* We always cross at least one scanline */
2453
1.47M
    row = &table[index[iy]];
2454
1.47M
    *row = (*row)+1; /* Increment the count */
2455
1.47M
    row[*row * 2 - 1] = sx;
2456
1.47M
    row[*row * 2    ] = id;
2457
1.47M
    if (ih == 0)
2458
764k
        return;
2459
707k
    if (ex >= 0) {
2460
496k
        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
496k
        x_inc = ex/ih;
2466
496k
        n_inc = ex-(x_inc*ih);
2467
496k
        f     = ih>>1;
2468
496k
        delta = ih;
2469
40.1M
        do {
2470
40.1M
            int count;
2471
40.1M
            iy++;
2472
40.1M
            sx += x_inc;
2473
40.1M
            f  -= n_inc;
2474
40.1M
            if (f < 0) {
2475
10.9M
                f += ih;
2476
10.9M
                sx++;
2477
10.9M
            }
2478
40.1M
            assert(iy >= 0 && iy < height);
2479
40.1M
            row = &table[index[iy]];
2480
40.1M
            count = *row = (*row)+1; /* Increment the count */
2481
40.1M
            row[count * 2 - 1] = sx;
2482
40.1M
            row[count * 2    ] = id;
2483
40.1M
        }
2484
40.1M
        while (--delta);
2485
496k
    } else {
2486
210k
        int x_dec, n_dec, f;
2487
2488
210k
        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
210k
        x_dec = ex/ih;
2493
210k
        n_dec = ex-(x_dec*ih);
2494
210k
        f     = ih>>1;
2495
210k
        delta = ih;
2496
15.9M
        do {
2497
15.9M
            int count;
2498
15.9M
            iy++;
2499
15.9M
            sx -= x_dec;
2500
15.9M
            f  -= n_dec;
2501
15.9M
            if (f < 0) {
2502
7.67M
                f += ih;
2503
7.67M
                sx--;
2504
7.67M
            }
2505
15.9M
            assert(iy >= 0 && iy < height);
2506
15.9M
            row = &table[index[iy]];
2507
15.9M
            count = *row = (*row)+1; /* Increment the count */
2508
15.9M
            row[count * 2 - 1] = sx;
2509
15.9M
            row[count * 2    ] = id;
2510
15.9M
         }
2511
15.9M
         while (--delta);
2512
210k
    }
2513
707k
}
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
169k
{
2584
169k
    return make_table_template(pdev, path, ibox, 2, 1, scanlines, index, table);
2585
169k
}
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
361k
{
2603
361k
    gs_fixed_rect  ibox;
2604
361k
    gs_fixed_rect  bbox;
2605
361k
    int            scanlines;
2606
361k
    const subpath *psub;
2607
361k
    int           *index;
2608
361k
    int           *table;
2609
361k
    int            i;
2610
361k
    int            code;
2611
361k
    int            id = 0;
2612
361k
    int            zero;
2613
2614
361k
    edgebuffer->index = NULL;
2615
361k
    edgebuffer->table = NULL;
2616
2617
    /* Bale out if no actual path. We see this with the clist */
2618
361k
    if (path->first_subpath == NULL)
2619
188k
        return 0;
2620
2621
173k
    zero = make_bbox(path, clip, &bbox, &ibox, fixed_half);
2622
173k
    if (zero < 0)
2623
0
        return zero;
2624
2625
173k
    if (ibox.q.y <= ibox.p.y)
2626
4.01k
        return 0;
2627
2628
169k
    code = make_table_tr(pdev, path, &ibox, &scanlines, &index, &table);
2629
169k
    if (code != 0) /* > 0 means "retry with smaller height" */
2630
9
        return code;
2631
2632
169k
    if (scanlines == 0)
2633
0
        return 0;
2634
2635
169k
    if (zero) {
2636
0
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero_tr);
2637
169k
    } else {
2638
2639
    /* Step 3: Now we run through the path, filling in the real
2640
     * values. */
2641
380k
    for (psub = path->first_subpath; psub != 0;) {
2642
210k
        const segment *pseg = (const segment *)psub;
2643
210k
        fixed ex = pseg->pt.x;
2644
210k
        fixed ey = pseg->pt.y;
2645
210k
        fixed ix = ex;
2646
210k
        fixed iy = ey;
2647
2648
41.5M
        while ((pseg = pseg->next) != 0 &&
2649
41.3M
               pseg->type != s_start
2650
41.3M
            ) {
2651
41.3M
            fixed sx = ex;
2652
41.3M
            fixed sy = ey;
2653
41.3M
            ex = pseg->pt.x;
2654
41.3M
            ey = pseg->pt.y;
2655
2656
41.3M
            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
41.2M
                case s_line:
2671
41.3M
                case s_line_close:
2672
41.3M
                    if (sy != ey)
2673
40.5M
                        mark_line_tr(sx, sy, ex, ey, ibox.p.y, scanlines, table, index, ++id);
2674
41.3M
                    break;
2675
41.3M
            }
2676
41.3M
        }
2677
        /* And close any open segments */
2678
210k
        if (iy != ey)
2679
48.6k
            mark_line_tr(ex, ey, ix, iy, ibox.p.y, scanlines, table, index, ++id);
2680
210k
        psub = (const subpath *)pseg;
2681
210k
    }
2682
169k
    }
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
169k
    edgebuffer->base   = ibox.p.y;
2699
169k
    edgebuffer->height = scanlines;
2700
169k
    edgebuffer->xmin   = ibox.p.x;
2701
169k
    edgebuffer->xmax   = ibox.q.x;
2702
169k
    edgebuffer->index  = index;
2703
169k
    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
16.9M
    for (i=0; i < scanlines; i++) {
2714
16.8M
        int *row = &table[index[i]];
2715
16.8M
        int  rowlen = *row++;
2716
2717
        /* Bubblesort short runs, qsort longer ones. */
2718
        /* FIXME: Verify the figure 6 below */
2719
16.8M
        if (rowlen <= 6) {
2720
16.7M
            int j, k;
2721
56.6M
            for (j = 0; j < rowlen-1; j++) {
2722
39.8M
                int * gs_restrict t = &row[j<<1];
2723
115M
                for (k = j+1; k < rowlen; k++) {
2724
75.8M
                    int * gs_restrict s = &row[k<<1];
2725
75.8M
                    int tmp;
2726
75.8M
                    if (t[0] < s[0])
2727
40.4M
                        continue;
2728
35.4M
                    if (t[0] == s[0]) {
2729
129k
                        if (t[1] <= s[1])
2730
129k
                            continue;
2731
129k
                    } else
2732
35.3M
                        tmp = t[0], t[0] = s[0], s[0] = tmp;
2733
35.3M
                    tmp = t[1], t[1] = s[1], s[1] = tmp;
2734
35.3M
                }
2735
39.8M
            }
2736
16.7M
        } else
2737
47.5k
            qsort(row, rowlen, 2*sizeof(int), intcmp_tr);
2738
16.8M
    }
2739
2740
169k
    return 0;
2741
169k
}
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
361k
{
2749
361k
    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
17.1M
    for (i=0; i < edgebuffer->height; i++) {
2759
16.8M
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
2760
16.8M
        int  rowlen   = *row++;
2761
16.8M
        int *rowstart = row;
2762
16.8M
        int *rowout   = row;
2763
2764
45.5M
        while (rowlen > 0) {
2765
28.7M
            int left, lid, right, rid;
2766
2767
28.7M
            if (rule == gx_rule_even_odd) {
2768
                /* Even Odd */
2769
700
                left  = *row++;
2770
700
                lid   = *row++;
2771
700
                right = *row++;
2772
700
                rid   = *row++;
2773
700
                rowlen -= 2;
2774
28.7M
            } else {
2775
                /* Non-Zero */
2776
28.7M
                int w;
2777
2778
28.7M
                left = *row++;
2779
28.7M
                lid  = *row++;
2780
28.7M
                w = ((lid&1)-1) | 1;
2781
28.7M
                rowlen--;
2782
28.8M
                do {
2783
28.8M
                    right = *row++;
2784
28.8M
                    rid   = *row++;
2785
28.8M
                    rowlen--;
2786
28.8M
                    w += ((rid&1)-1) | 1;
2787
28.8M
                } while (w != 0);
2788
28.7M
            }
2789
2790
28.7M
            if (right > left) {
2791
28.6M
                *rowout++ = left;
2792
28.6M
                *rowout++ = lid;
2793
28.6M
                *rowout++ = right;
2794
28.6M
                *rowout++ = rid;
2795
28.6M
            }
2796
28.7M
        }
2797
16.8M
        rowstart[-1] = (rowout-rowstart)>>1;
2798
16.8M
    }
2799
361k
    return 0;
2800
361k
}
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
361k
{
2809
361k
    int i, j, code;
2810
361k
    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
988k
    for (i=0; i < edgebuffer->height; ) {
2820
626k
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
2821
626k
        int  rowlen = *row++;
2822
626k
        int *row2;
2823
626k
        int *rowptr;
2824
626k
        int *row2ptr;
2825
626k
        int y_band_max;
2826
2827
626k
        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
626k
        } else {
2832
626k
            y_band_max = edgebuffer->height;
2833
626k
        }
2834
2835
        /* See how many scanlines match i */
2836
16.8M
        for (j = i+1; j < y_band_max; j++) {
2837
16.6M
            int row2len;
2838
2839
16.6M
            row2    = &edgebuffer->table[edgebuffer->index[j]];
2840
16.6M
            row2len = *row2++;
2841
16.6M
            row2ptr = row2;
2842
16.6M
            rowptr  = row;
2843
2844
16.6M
            if (rowlen != row2len)
2845
57.6k
                break;
2846
71.8M
            while (row2len > 0) {
2847
55.6M
                if ((rowptr[1]&~1) != (row2ptr[1]&~1))
2848
398k
                    goto rowdifferent;
2849
55.2M
                rowptr  += 2;
2850
55.2M
                row2ptr += 2;
2851
55.2M
                row2len--;
2852
55.2M
            }
2853
16.5M
        }
2854
626k
rowdifferent:{}
2855
2856
        /* So j is the first scanline that doesn't match i */
2857
2858
626k
        if (j == i+1) {
2859
1.02M
            while (rowlen > 0) {
2860
693k
                int left, right;
2861
2862
693k
                left  = row[0];
2863
693k
                right = row[2];
2864
693k
                row += 4;
2865
693k
                rowlen -= 2;
2866
2867
693k
                left  = fixed2int(left + fixed_half);
2868
693k
                right = fixed2int(right + fixed_half);
2869
693k
                right -= left;
2870
693k
                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
691k
                    if (log_op < 0)
2880
0
                        code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
2881
691k
                    else
2882
691k
                        code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
2883
691k
                    if (code < 0)
2884
0
                        return code;
2885
691k
                }
2886
693k
            }
2887
335k
        } else {
2888
290k
            gs_fixed_edge le;
2889
290k
            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
290k
            le.start.y = re.start.y = int2fixed(edgebuffer->base+i) + fixed_half;
2923
290k
            le.end.y   = re.end.y   = int2fixed(edgebuffer->base+j) - (fixed_half-1);
2924
290k
            row2    = &edgebuffer->table[edgebuffer->index[j-1]+1];
2925
697k
            while (rowlen > 0) {
2926
406k
                le.start.x = row[0];
2927
406k
                re.start.x = row[2];
2928
406k
                le.end.x   = row2[0];
2929
406k
                re.end.x   = row2[2];
2930
406k
                row += 4;
2931
406k
                row2 += 4;
2932
406k
                rowlen -= 2;
2933
2934
406k
                assert(re.start.x >= le.start.x);
2935
406k
                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
406k
                code = dev_proc(pdev, fill_trapezoid)(
2946
406k
                                pdev,
2947
406k
                                &le,
2948
406k
                                &re,
2949
406k
                                le.start.y,
2950
406k
                                le.end.y,
2951
406k
                                0, /* bool swap_axes */
2952
406k
                                pdevc, /*const gx_drawing_color *pdcolor */
2953
406k
                                log_op);
2954
406k
                if (code < 0)
2955
0
                    return code;
2956
406k
            }
2957
290k
        }
2958
2959
626k
        i = j;
2960
626k
    }
2961
361k
    return 0;
2962
361k
}
2963
2964
/* Any part of a pixel trapezoid routines */
2965
2966
static int edgecmp_tr(const void *a, const void *b)
2967
496M
{
2968
496M
    int left  = ((int*)a)[0];
2969
496M
    int right = ((int*)b)[0];
2970
496M
    if (left != right)
2971
483M
        return left - right;
2972
13.0M
    left = ((int*)a)[2] - ((int*)b)[2];
2973
13.0M
    if (left != 0)
2974
1.57M
        return left;
2975
11.4M
    left = ((int*)a)[1] - ((int*)b)[1];
2976
11.4M
    if (left != 0)
2977
11.4M
        return left;
2978
3.13k
    return ((int*)a)[3] - ((int*)b)[3];
2979
11.4M
}
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
595M
{
3079
595M
    int *row;
3080
595M
    int count;
3081
3082
595M
    if (iy >= 0 && iy < cr->scanlines) {
3083
586M
        if (cr->first) {
3084
            /* Save it for later in case we join up */
3085
11.6M
            cr->save_left  = cr->left;
3086
11.6M
            cr->save_lid   = cr->lid;
3087
11.6M
            cr->save_right = cr->right;
3088
11.6M
            cr->save_rid   = cr->rid;
3089
11.6M
            cr->save_iy    = iy;
3090
11.6M
            cr->save_d     = cr->d;
3091
11.6M
            cr->saved      = 1;
3092
574M
        } else if (cr->d != DIRN_UNSET) {
3093
            /* Enter it into the table */
3094
574M
            row = &cr->table[cr->index[iy]];
3095
574M
            *row = count = (*row)+1; /* Increment the count */
3096
574M
            row[4 * count - 3] = cr->left;
3097
574M
            row[4 * count - 2] = cr->d | (cr->lid<<1);
3098
574M
            row[4 * count - 1] = cr->right;
3099
574M
            row[4 * count    ] = cr->rid;
3100
574M
        } else {
3101
106k
            assert(cr->left == max_fixed && cr->right == min_fixed);
3102
106k
        }
3103
586M
    }
3104
595M
    cr->first = 0;
3105
595M
}
3106
3107
static inline void
3108
cursor_output_inrange_tr(cursor_tr * gs_restrict cr, int iy)
3109
393M
{
3110
393M
    int *row;
3111
393M
    int count;
3112
3113
393M
    assert(iy >= 0 && iy < cr->scanlines);
3114
393M
    if (cr->first) {
3115
        /* Save it for later in case we join up */
3116
252k
        cr->save_left  = cr->left;
3117
252k
        cr->save_lid   = cr->lid;
3118
252k
        cr->save_right = cr->right;
3119
252k
        cr->save_rid   = cr->rid;
3120
252k
        cr->save_iy    = iy;
3121
252k
        cr->save_d     = cr->d;
3122
252k
        cr->saved      = 1;
3123
393M
    } else {
3124
        /* Enter it into the table */
3125
393M
        assert(cr->d != DIRN_UNSET);
3126
3127
393M
        row = &cr->table[cr->index[iy]];
3128
393M
        *row = count = (*row)+1; /* Increment the count */
3129
393M
        row[4 * count - 3] = cr->left;
3130
393M
        row[4 * count - 2] = cr->d | (cr->lid<<1);
3131
393M
        row[4 * count - 1] = cr->right;
3132
393M
        row[4 * count    ] = cr->rid;
3133
393M
    }
3134
393M
    cr->first = 0;
3135
393M
}
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
57.4M
{
3141
57.4M
    int new_iy;
3142
57.4M
    int iy = fixed2int(cr->y) - cr->base;
3143
3144
57.4M
    cr->y += dy;
3145
57.4M
    new_iy = fixed2int(cr->y) - cr->base;
3146
57.4M
    if (new_iy != iy) {
3147
57.4M
        if (!skip)
3148
56.5M
            cursor_output_tr(cr, iy);
3149
57.4M
        cr->left = x;
3150
57.4M
        cr->lid = id;
3151
57.4M
        cr->right = x;
3152
57.4M
        cr->rid = id;
3153
57.4M
    } 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
57.4M
}
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
4.03M
{
3169
4.03M
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
3170
3171
4.03M
    cr->y += dy;
3172
4.03M
}
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
12.4M
{
3180
12.4M
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
3181
3182
12.4M
    if (x < cr->left)
3183
9.97M
    {
3184
9.97M
        cr->left = x;
3185
9.97M
        cr->lid = id;
3186
9.97M
    }
3187
12.4M
    cr->y += dy;
3188
12.4M
}
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
11.6M
{
3196
11.6M
    assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
3197
3198
11.6M
    if (x > cr->right)
3199
11.3M
    {
3200
11.3M
        cr->right = x;
3201
11.3M
        cr->rid = id;
3202
11.3M
    }
3203
11.6M
    cr->y += dy;
3204
11.6M
}
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
45.5M
{
3210
45.5M
    int iy = fixed2int(cr->y) - cr->base;
3211
3212
45.5M
    if (!skip)
3213
40.4M
        cursor_output_tr(cr, iy);
3214
45.5M
    cr->y += dy;
3215
45.5M
    cr->left = x;
3216
45.5M
    cr->lid = id;
3217
45.5M
    cr->right = x;
3218
45.5M
    cr->rid = id;
3219
45.5M
}
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
456M
{
3227
456M
    int iy = fixed2int(cr->y) - cr->base;
3228
3229
456M
    cursor_output_tr(cr, iy);
3230
456M
    cr->y += dy;
3231
456M
}
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
198M
{
3239
198M
    int iy = fixed2int(cr->y) - cr->base;
3240
3241
198M
    cr->y += dy;
3242
198M
    cursor_output_inrange_tr(cr, iy);
3243
198M
    cr->right = x;
3244
198M
    cr->rid = id;
3245
198M
}
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
195M
{
3253
195M
    int iy = fixed2int(cr->y) - cr->base;
3254
3255
195M
    cr->y += dy;
3256
195M
    cursor_output_inrange_tr(cr, iy);
3257
195M
    cr->left = x;
3258
195M
    cr->lid = id;
3259
195M
}
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
159M
{
3275
159M
    if (x < cr->left) {
3276
53.1M
        cr->left = x;
3277
53.1M
        cr->lid  = id;
3278
53.1M
    }
3279
159M
}
3280
3281
static inline void cursor_left_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3282
231M
{
3283
231M
    cr->left = x;
3284
231M
    cr->lid  = id;
3285
231M
}
3286
3287
static inline void cursor_right_merge_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3288
163M
{
3289
163M
    if (x > cr->right) {
3290
52.5M
        cr->right = x;
3291
52.5M
        cr->rid   = id;
3292
52.5M
    }
3293
163M
}
3294
3295
static inline void cursor_right_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3296
227M
{
3297
227M
    cr->right = x;
3298
227M
    cr->rid   = id;
3299
227M
}
3300
3301
static inline int cursor_down_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3302
51.1M
{
3303
51.1M
    int skip = 0;
3304
51.1M
    if ((cr->y & 0xff) == 0)
3305
5.90M
        skip = 1;
3306
51.1M
    if (cr->d == DIRN_UP)
3307
7.62M
    {
3308
7.62M
        if (!skip)
3309
7.62M
            cursor_output_tr(cr, fixed2int(cr->y) - cr->base);
3310
7.62M
        cr->left = x;
3311
7.62M
        cr->lid = id;
3312
7.62M
        cr->right = x;
3313
7.62M
        cr->rid = id;
3314
7.62M
    }
3315
51.1M
    cr->d = DIRN_DOWN;
3316
51.1M
    return skip;
3317
51.1M
}
3318
3319
static inline void cursor_up_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3320
51.1M
{
3321
51.1M
    if (cr->d == DIRN_DOWN)
3322
7.37M
    {
3323
7.37M
        cursor_output_tr(cr, fixed2int(cr->y) - cr->base);
3324
7.37M
        cr->left = x;
3325
7.37M
        cr->lid = id;
3326
7.37M
        cr->right = x;
3327
7.37M
        cr->rid = id;
3328
7.37M
    }
3329
51.1M
    cr->d = DIRN_UP;
3330
51.1M
}
3331
3332
static inline void
3333
cursor_flush_tr(cursor_tr * gs_restrict cr, fixed x, int id)
3334
18.5M
{
3335
18.5M
    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
18.5M
    if (cr->first) {
3341
5.79k
        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
5.79k
        if (cr->d == DIRN_UNSET && (cr->y & 0xff) == 0)
3345
325
            return;
3346
5.79k
        assert(cr->left != max_fixed && cr->right != min_fixed);
3347
5.46k
        if (iy >= 0 && iy < cr->scanlines) {
3348
4.37k
            int *row = &cr->table[cr->index[iy]];
3349
4.37k
            int count = *row = (*row)+2; /* Increment the count */
3350
4.37k
            row[4 * count - 7] = cr->left;
3351
4.37k
            row[4 * count - 6] = DIRN_UP | (cr->lid<<1);
3352
4.37k
            row[4 * count - 5] = cr->right;
3353
4.37k
            row[4 * count - 4] = cr->rid;
3354
4.37k
            row[4 * count - 3] = cr->right;
3355
4.37k
            row[4 * count - 2] = DIRN_DOWN | (cr->rid<<1);
3356
4.37k
            row[4 * count - 1] = cr->right;
3357
4.37k
            row[4 * count    ] = cr->rid;
3358
4.37k
        }
3359
5.46k
        return;
3360
5.79k
    }
3361
3362
    /* Merge save into current if we can */
3363
18.5M
    iy = fixed2int(cr->y) - cr->base;
3364
18.5M
    if (cr->saved && iy == cr->save_iy &&
3365
9.36M
        (cr->d == cr->save_d || cr->save_d == DIRN_UNSET)) {
3366
3.70M
        if (cr->left > cr->save_left) {
3367
981k
            cr->left = cr->save_left;
3368
981k
            cr->lid  = cr->save_lid;
3369
981k
        }
3370
3.70M
        if (cr->right < cr->save_right) {
3371
875k
            cr->right = cr->save_right;
3372
875k
            cr->rid = cr->save_rid;
3373
875k
        }
3374
3.70M
        cursor_output_tr(cr, iy);
3375
3.70M
        return;
3376
3.70M
    }
3377
3378
    /* Merge not possible */
3379
14.8M
    cursor_output_tr(cr, iy);
3380
14.8M
    if (cr->saved) {
3381
8.21M
        cr->left  = cr->save_left;
3382
8.21M
        cr->lid   = cr->save_lid;
3383
8.21M
        cr->right = cr->save_right;
3384
8.21M
        cr->rid   = cr->save_rid;
3385
8.21M
        assert(cr->save_d != DIRN_UNSET);
3386
8.21M
        if (cr->save_d != DIRN_UNSET)
3387
8.21M
            cr->d = cr->save_d;
3388
8.21M
        cursor_output_tr(cr, cr->save_iy);
3389
8.21M
    }
3390
14.8M
}
3391
3392
static inline void
3393
cursor_null_tr(cursor_tr *cr)
3394
25.7M
{
3395
25.7M
    cr->right = min_fixed;
3396
25.7M
    cr->left  = max_fixed;
3397
25.7M
    cr->d     = DIRN_UNSET;
3398
25.7M
}
3399
3400
static void mark_line_tr_app(cursor_tr * gs_restrict cr, fixed sx, fixed sy, fixed ex, fixed ey, int id)
3401
847M
{
3402
847M
    int isy, iey;
3403
847M
    fixed saved_sy = sy;
3404
847M
    fixed saved_ex = ex;
3405
847M
    fixed saved_ey = ey;
3406
847M
    int truncated;
3407
3408
847M
    if (sy == ey && sx == ex)
3409
25.2M
        return;
3410
3411
822M
    isy = fixed2int(sy) - cr->base;
3412
822M
    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
822M
    if (sy == ey && (sy & 0xff) == 0)
3427
1.46M
        return;
3428
3429
822M
    assert(cr->y == sy &&
3430
820M
           ((cr->left <= sx && cr->right >= sx) || ((sy & 0xff) == 0)) &&
3431
820M
           cr->d >= DIRN_UNSET && cr->d <= DIRN_DOWN);
3432
3433
820M
    if (isy < iey) {
3434
        /* Rising line */
3435
331M
        if (iey < 0 || isy >= cr->scanlines) {
3436
            /* All line is outside. */
3437
297M
            if ((ey & 0xff) == 0) {
3438
1.96M
                cursor_null_tr(cr);
3439
295M
            } else {
3440
295M
                cr->left = ex;
3441
295M
                cr->lid = id;
3442
295M
                cr->right = ex;
3443
295M
                cr->rid = id;
3444
295M
            }
3445
297M
            cr->y = ey;
3446
297M
            cr->first = 0;
3447
297M
            return;
3448
297M
        }
3449
34.0M
        if (isy < 0) {
3450
            /* Move sy up */
3451
5.41M
            int64_t y = (int64_t)ey - (int64_t)sy;
3452
5.41M
            fixed new_sy = int2fixed(cr->base);
3453
5.41M
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
3454
5.41M
            sx += (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3455
5.41M
            sy = new_sy;
3456
5.41M
            cursor_null_tr(cr);
3457
5.41M
            cr->y = sy;
3458
5.41M
            isy = 0;
3459
5.41M
        }
3460
34.0M
        truncated = iey > cr->scanlines;
3461
34.0M
        if (truncated) {
3462
            /* Move ey down */
3463
4.88M
            int64_t y = (int64_t)ey - (int64_t)sy;
3464
4.88M
            fixed new_ey = int2fixed(cr->base + cr->scanlines);
3465
4.88M
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
3466
4.88M
            saved_ex = ex;
3467
4.88M
            saved_ey = ey;
3468
4.88M
            ex -= (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3469
4.88M
            ey = new_ey;
3470
4.88M
            iey = cr->scanlines;
3471
4.88M
        }
3472
488M
    } else {
3473
        /* Falling line */
3474
488M
        if (isy < 0 || iey >= cr->scanlines) {
3475
            /* All line is outside. */
3476
406M
            if ((ey & 0xff) == 0) {
3477
1.63M
                cursor_null_tr(cr);
3478
404M
            } else {
3479
404M
                cr->left = ex;
3480
404M
                cr->lid = id;
3481
404M
                cr->right = ex;
3482
404M
                cr->rid = id;
3483
404M
            }
3484
406M
            cr->y = ey;
3485
406M
            cr->first = 0;
3486
406M
            return;
3487
406M
        }
3488
82.3M
        truncated = iey < 0;
3489
82.3M
        if (truncated) {
3490
            /* Move ey up */
3491
5.41M
            int64_t y = (int64_t)ey - (int64_t)sy;
3492
5.41M
            fixed new_ey = int2fixed(cr->base);
3493
5.41M
            int64_t dy = (int64_t)ey - (int64_t)new_ey;
3494
5.41M
            ex -= (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3495
5.41M
            ey = new_ey;
3496
5.41M
            iey = 0;
3497
5.41M
        }
3498
82.3M
        if (isy >= cr->scanlines) {
3499
            /* Move sy down */
3500
5.40M
            int64_t y = (int64_t)ey - (int64_t)sy;
3501
5.40M
            fixed new_sy = int2fixed(cr->base + cr->scanlines);
3502
5.40M
            int64_t dy = (int64_t)new_sy - (int64_t)sy;
3503
5.40M
            sx += (int)(((((int64_t)ex-sx))*dy + y/2)/y);
3504
5.40M
            sy = new_sy;
3505
5.40M
            cursor_null_tr(cr);
3506
5.40M
            cr->y = sy;
3507
5.40M
            isy = cr->scanlines;
3508
5.40M
        }
3509
82.3M
    }
3510
3511
116M
    cursor_left_merge_tr(cr, sx, id);
3512
116M
    cursor_right_merge_tr(cr, sx, id);
3513
3514
116M
    assert(cr->left <= sx);
3515
116M
    assert(cr->right >= sx);
3516
116M
    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
116M
    if (isy == iey) {
3539
48.7M
        if (saved_sy == saved_ey) {
3540
            /* Horizontal line. Don't change cr->d, don't flush. */
3541
14.1M
            if ((ey & 0xff) == 0) {
3542
0
                cursor_null_tr(cr);
3543
0
                goto no_merge;
3544
0
            }
3545
34.6M
        } else if (saved_sy > saved_ey) {
3546
            /* Falling line, flush if previous was rising */
3547
17.1M
            int skip = cursor_down_tr(cr, sx, id);
3548
17.1M
            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
834k
                if (sx <= ex) {
3552
580k
                    cursor_right_merge_tr(cr, ex, id);
3553
580k
                } else {
3554
254k
                    cursor_left_merge_tr(cr, ex, id);
3555
254k
                }
3556
834k
                if (!skip)
3557
816k
                    cursor_output_tr(cr, fixed2int(cr->y) - cr->base);
3558
834k
                cursor_null_tr(cr);
3559
834k
                goto no_merge;
3560
834k
            }
3561
17.5M
        } else {
3562
            /* Rising line, flush if previous was falling */
3563
17.5M
            cursor_up_tr(cr, sx, id);
3564
17.5M
            if ((ey & 0xff) == 0) {
3565
20.2k
                cursor_null_tr(cr);
3566
20.2k
                goto no_merge;
3567
20.2k
            }
3568
17.5M
        }
3569
47.9M
        if (sx <= ex) {
3570
25.1M
            cursor_right_merge_tr(cr, ex, id);
3571
25.1M
        } else {
3572
22.8M
            cursor_left_merge_tr(cr, ex, id);
3573
22.8M
        }
3574
48.7M
no_merge:
3575
48.7M
        cr->y = ey;
3576
48.7M
        if (sy > saved_ey)
3577
17.1M
            goto endFalling;
3578
67.6M
    } 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
33.5M
        int phase1_y_steps = (-sy) & (fixed_1 - 1);
3587
33.5M
        int phase3_y_steps = ey & (fixed_1 - 1);
3588
33.5M
        ufixed y_steps = (ufixed)ey - (ufixed)sy;
3589
3590
33.5M
        cursor_up_tr(cr, sx, id);
3591
3592
33.5M
        if (sx == ex) {
3593
            /* Vertical line. (Rising) */
3594
3595
            /* Phase 1: */
3596
7.15M
            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
4.27M
                cursor_step_tr(cr, phase1_y_steps, sx, id, 0);
3600
4.27M
                sy += phase1_y_steps;
3601
4.27M
                y_steps -= phase1_y_steps;
3602
4.27M
                if (y_steps == 0) {
3603
306k
                    cursor_null_tr(cr);
3604
306k
                    goto end;
3605
306k
                }
3606
4.27M
            }
3607
3608
            /* Phase 3: precalculation */
3609
6.84M
            y_steps -= phase3_y_steps;
3610
3611
            /* Phase 2: */
3612
6.84M
            y_steps = fixed2int(y_steps);
3613
6.84M
            assert(y_steps >= 0);
3614
6.84M
            if (y_steps > 0) {
3615
5.73M
                cursor_always_step_tr(cr, fixed_1, sx, id, 0);
3616
5.73M
                y_steps--;
3617
236M
                while (y_steps) {
3618
230M
                    cursor_always_step_inrange_vertical_tr(cr, fixed_1, sx, id);
3619
230M
                    y_steps--;
3620
230M
                }
3621
5.73M
            }
3622
3623
            /* Phase 3 */
3624
6.84M
            assert(cr->left == sx && cr->right == sx && cr->lid == id && cr->rid == id);
3625
6.84M
            if (phase3_y_steps == 0)
3626
2.78M
                cursor_null_tr(cr);
3627
4.06M
            else
3628
4.06M
                cr->y += phase3_y_steps;
3629
26.4M
        } else if (sx < ex) {
3630
            /* Lines increasing in x. (Rightwards, rising) */
3631
13.4M
            int phase1_x_steps, phase3_x_steps;
3632
            /* Use unsigned int here, to allow for extreme cases like
3633
             * ex = 0x7fffffff, sx = 0x80000000 */
3634
13.4M
            unsigned int x_steps = ex - sx;
3635
3636
            /* Phase 1: */
3637
13.4M
            if (phase1_y_steps) {
3638
12.3M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3639
12.3M
                sx += phase1_x_steps;
3640
12.3M
                cursor_right_merge_tr(cr, sx, id);
3641
12.3M
                x_steps -= phase1_x_steps;
3642
12.3M
                cursor_step_tr(cr, phase1_y_steps, sx, id, 0);
3643
12.3M
                sy += phase1_y_steps;
3644
12.3M
                y_steps -= phase1_y_steps;
3645
12.3M
                if (y_steps == 0) {
3646
199k
                    cursor_null_tr(cr);
3647
199k
                    goto end;
3648
199k
                }
3649
12.3M
            }
3650
3651
            /* Phase 3: precalculation */
3652
13.2M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3653
13.2M
            x_steps -= phase3_x_steps;
3654
13.2M
            y_steps -= phase3_y_steps;
3655
13.2M
            assert((y_steps & (fixed_1 - 1)) == 0);
3656
3657
            /* Phase 2: */
3658
13.2M
            y_steps = fixed2int(y_steps);
3659
13.2M
            assert(y_steps >= 0);
3660
13.2M
            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
8.78M
                int x_inc = x_steps/y_steps;
3664
8.78M
                int n_inc = x_steps - (x_inc * y_steps);
3665
8.78M
                int f = y_steps/2;
3666
8.78M
                int d = y_steps;
3667
3668
                /* Special casing the first iteration, allows us to simplify
3669
                 * the following loop. */
3670
8.78M
                sx += x_inc;
3671
8.78M
                f -= n_inc;
3672
8.78M
                if (f < 0)
3673
1.79M
                    f += d, sx++;
3674
8.78M
                cursor_right_merge_tr(cr, sx, id);
3675
8.78M
                cursor_always_step_tr(cr, fixed_1, sx, id, 0);
3676
8.78M
                y_steps--;
3677
3678
99.7M
                while (y_steps) {
3679
91.0M
                    sx += x_inc;
3680
91.0M
                    f -= n_inc;
3681
91.0M
                    if (f < 0)
3682
40.8M
                        f += d, sx++;
3683
91.0M
                    cursor_right_tr(cr, sx, id);
3684
91.0M
                    cursor_always_inrange_step_right_tr(cr, fixed_1, sx, id);
3685
91.0M
                    y_steps--;
3686
91.0M
                };
3687
8.78M
            }
3688
3689
            /* Phase 3 */
3690
13.2M
            assert(cr->left <= ex && cr->lid == id && cr->right >= sx);
3691
13.2M
            if (phase3_y_steps == 0)
3692
941k
                cursor_null_tr(cr);
3693
12.2M
            else {
3694
12.2M
                cursor_right_tr(cr, ex, id);
3695
12.2M
                cr->y += phase3_y_steps;
3696
12.2M
            }
3697
13.2M
        } else {
3698
            /* Lines decreasing in x. (Leftwards, rising) */
3699
13.0M
            int phase1_x_steps, phase3_x_steps;
3700
            /* Use unsigned int here, to allow for extreme cases like
3701
             * sx = 0x7fffffff, ex = 0x80000000 */
3702
13.0M
            unsigned int x_steps = sx - ex;
3703
3704
            /* Phase 1: */
3705
13.0M
            if (phase1_y_steps) {
3706
11.8M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3707
11.8M
                x_steps -= phase1_x_steps;
3708
11.8M
                sx -= phase1_x_steps;
3709
11.8M
                cursor_left_merge_tr(cr, sx, id);
3710
11.8M
                cursor_step_tr(cr, phase1_y_steps, sx, id, 0);
3711
11.8M
                sy += phase1_y_steps;
3712
11.8M
                y_steps -= phase1_y_steps;
3713
11.8M
                if (y_steps == 0) {
3714
193k
                    cursor_null_tr(cr);
3715
193k
                    goto end;
3716
193k
                }
3717
11.8M
            }
3718
3719
            /* Phase 3: precalculation */
3720
12.8M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3721
12.8M
            x_steps -= phase3_x_steps;
3722
12.8M
            y_steps -= phase3_y_steps;
3723
12.8M
            assert((y_steps & (fixed_1 - 1)) == 0);
3724
3725
            /* Phase 2: */
3726
12.8M
            y_steps = fixed2int((unsigned int)y_steps);
3727
12.8M
            assert(y_steps >= 0);
3728
12.8M
            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
8.19M
                int x_inc = x_steps/y_steps;
3732
8.19M
                int n_inc = x_steps - (x_inc * y_steps);
3733
8.19M
                int f = y_steps/2;
3734
8.19M
                int d = y_steps;
3735
3736
                /* Special casing the first iteration, allows us to simplify
3737
                 * the following loop. */
3738
8.19M
                sx -= x_inc;
3739
8.19M
                f -= n_inc;
3740
8.19M
                if (f < 0)
3741
1.59M
                    f += d, sx--;
3742
8.19M
                cursor_left_merge_tr(cr, sx, id);
3743
8.19M
                cursor_always_step_tr(cr, fixed_1, sx, id, 0);
3744
8.19M
                y_steps--;
3745
3746
108M
                while (y_steps) {
3747
100M
                    sx -= x_inc;
3748
100M
                    f -= n_inc;
3749
100M
                    if (f < 0)
3750
42.8M
                        f += d, sx--;
3751
100M
                    cursor_left_tr(cr, sx, id);
3752
100M
                    cursor_always_inrange_step_left_tr(cr, fixed_1, sx, id);
3753
100M
                    y_steps--;
3754
100M
                }
3755
8.19M
            }
3756
3757
            /* Phase 3 */
3758
12.8M
            assert(cr->right >= ex && cr->rid == id && cr->left <= sx);
3759
12.8M
            if (phase3_y_steps == 0)
3760
974k
                cursor_null_tr(cr);
3761
11.8M
            else {
3762
11.8M
                cursor_left_tr(cr, ex, id);
3763
11.8M
                cr->y += phase3_y_steps;
3764
11.8M
            }
3765
12.8M
        }
3766
34.0M
    } 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
34.0M
        int phase1_y_steps = sy & (fixed_1 - 1);
3775
34.0M
        int phase3_y_steps = (-ey) & (fixed_1 - 1);
3776
34.0M
        ufixed y_steps = (ufixed)sy - (ufixed)ey;
3777
3778
34.0M
        int skip = cursor_down_tr(cr, sx, id);
3779
3780
34.0M
        if (sx == ex) {
3781
            /* Vertical line. (Falling) */
3782
3783
            /* Phase 1: */
3784
7.25M
            if (phase1_y_steps) {
3785
                /* Phase 1 in a falling line never moves us into a new scanline. */
3786
4.03M
                cursor_never_step_vertical_tr(cr, -phase1_y_steps, sx, id);
3787
4.03M
                sy -= phase1_y_steps;
3788
4.03M
                y_steps -= phase1_y_steps;
3789
4.03M
                if (y_steps == 0)
3790
0
                    goto endFallingLeftOnEdgeOfPixel;
3791
4.03M
            }
3792
3793
            /* Phase 3: precalculation */
3794
7.25M
            y_steps -= phase3_y_steps;
3795
7.25M
            assert((y_steps & (fixed_1 - 1)) == 0);
3796
3797
            /* Phase 2: */
3798
7.25M
            y_steps = fixed2int(y_steps);
3799
7.25M
            assert(y_steps >= 0);
3800
7.25M
            if (y_steps) {
3801
5.79M
                cursor_always_step_tr(cr, -fixed_1, sx, id, skip);
3802
5.79M
                skip = 0;
3803
5.79M
                y_steps--;
3804
225M
                while (y_steps) {
3805
220M
                    cursor_always_step_inrange_vertical_tr(cr, -fixed_1, sx, id);
3806
220M
                    y_steps--;
3807
220M
                }
3808
5.79M
            }
3809
3810
            /* Phase 3 */
3811
7.25M
            if (phase3_y_steps == 0) {
3812
2.92M
endFallingLeftOnEdgeOfPixel:
3813
2.92M
                cursor_always_step_inrange_vertical_tr(cr, 0, sx, id);
3814
2.92M
                cursor_null_tr(cr);
3815
4.32M
            } else {
3816
4.32M
                cursor_step_tr(cr, -phase3_y_steps, sx, id, skip);
3817
4.32M
                assert(cr->left == sx && cr->lid == id && cr->right == sx && cr->rid == id);
3818
4.32M
            }
3819
26.7M
        } else if (sx < ex) {
3820
            /* Lines increasing in x. (Rightwards, falling) */
3821
12.9M
            int phase1_x_steps, phase3_x_steps;
3822
            /* Use unsigned int here, to allow for extreme cases like
3823
             * ex = 0x7fffffff, sx = 0x80000000 */
3824
12.9M
            unsigned int x_steps = ex - sx;
3825
3826
            /* Phase 1: */
3827
12.9M
            if (phase1_y_steps) {
3828
11.6M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3829
11.6M
                x_steps -= phase1_x_steps;
3830
11.6M
                sx += phase1_x_steps;
3831
                /* Phase 1 in a falling line never moves us into a new scanline. */
3832
11.6M
                cursor_never_step_right_tr(cr, -phase1_y_steps, sx, id);
3833
11.6M
                sy -= phase1_y_steps;
3834
11.6M
                y_steps -= phase1_y_steps;
3835
11.6M
                if (y_steps == 0)
3836
0
                    goto endFallingRightOnEdgeOfPixel;
3837
11.6M
            }
3838
3839
            /* Phase 3: precalculation */
3840
12.9M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3841
12.9M
            x_steps -= phase3_x_steps;
3842
12.9M
            y_steps -= phase3_y_steps;
3843
12.9M
            assert((y_steps & (fixed_1 - 1)) == 0);
3844
3845
            /* Phase 2: */
3846
12.9M
            y_steps = fixed2int(y_steps);
3847
12.9M
            assert(y_steps >= 0);
3848
12.9M
            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
7.97M
                int x_inc = x_steps/y_steps;
3852
7.97M
                int n_inc = x_steps - (x_inc * y_steps);
3853
7.97M
                int f = y_steps/2;
3854
7.97M
                int d = y_steps;
3855
3856
7.97M
                cursor_always_step_tr(cr, -fixed_1, sx, id, skip);
3857
7.97M
                skip = 0;
3858
7.97M
                sx += x_inc;
3859
7.97M
                f -= n_inc;
3860
7.97M
                if (f < 0)
3861
1.65M
                    f += d, sx++;
3862
7.97M
                cursor_right_tr(cr, sx, id);
3863
7.97M
                y_steps--;
3864
3865
112M
                while (y_steps) {
3866
104M
                    cursor_always_inrange_step_right_tr(cr, -fixed_1, sx, id);
3867
104M
                    sx += x_inc;
3868
104M
                    f -= n_inc;
3869
104M
                    if (f < 0)
3870
45.6M
                        f += d, sx++;
3871
104M
                    cursor_right_tr(cr, sx, id);
3872
104M
                    y_steps--;
3873
104M
                }
3874
7.97M
            }
3875
3876
            /* Phase 3 */
3877
12.9M
            if (phase3_y_steps == 0) {
3878
1.06M
endFallingRightOnEdgeOfPixel:
3879
1.06M
                cursor_always_step_inrange_vertical_tr(cr, 0, sx, id);
3880
1.06M
                cursor_null_tr(cr);
3881
11.8M
            } else {
3882
11.8M
                cursor_step_tr(cr, -phase3_y_steps, sx, id, skip);
3883
11.8M
                cursor_right_tr(cr, ex, id);
3884
11.8M
                assert(cr->left == sx && cr->lid == id && cr->right == ex && cr->rid == id);
3885
11.8M
            }
3886
13.8M
        } else {
3887
            /* Lines decreasing in x. (Falling) */
3888
13.8M
            int phase1_x_steps, phase3_x_steps;
3889
            /* Use unsigned int here, to allow for extreme cases like
3890
             * sx = 0x7fffffff, ex = 0x80000000 */
3891
13.8M
            unsigned int x_steps = sx - ex;
3892
3893
            /* Phase 1: */
3894
13.8M
            if (phase1_y_steps) {
3895
12.4M
                phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
3896
12.4M
                x_steps -= phase1_x_steps;
3897
12.4M
                sx -= phase1_x_steps;
3898
                /* Phase 1 in a falling line never moves us into a new scanline. */
3899
12.4M
                cursor_never_step_left_tr(cr, -phase1_y_steps, sx, id);
3900
12.4M
                sy -= phase1_y_steps;
3901
12.4M
                y_steps -= phase1_y_steps;
3902
12.4M
                if (y_steps == 0)
3903
0
                    goto endFallingVerticalOnEdgeOfPixel;
3904
12.4M
            }
3905
3906
            /* Phase 3: precalculation */
3907
13.8M
            phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
3908
13.8M
            x_steps -= phase3_x_steps;
3909
13.8M
            y_steps -= phase3_y_steps;
3910
13.8M
            assert((y_steps & (fixed_1 - 1)) == 0);
3911
3912
            /* Phase 2: */
3913
13.8M
            y_steps = fixed2int(y_steps);
3914
13.8M
            assert(y_steps >= 0);
3915
13.8M
            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
9.04M
                int x_inc = x_steps/y_steps;
3919
9.04M
                int n_inc = x_steps - (x_inc * y_steps);
3920
9.04M
                int f = y_steps/2;
3921
9.04M
                int d = y_steps;
3922
3923
9.04M
                cursor_always_step_tr(cr, -fixed_1, sx, id, skip);
3924
9.04M
                skip = 0;
3925
9.04M
                sx -= x_inc;
3926
9.04M
                f -= n_inc;
3927
9.04M
                if (f < 0)
3928
1.87M
                    f += d, sx--;
3929
9.04M
                cursor_left_tr(cr, sx, id);
3930
9.04M
                y_steps--;
3931
3932
106M
                while (y_steps) {
3933
97.6M
                    cursor_always_inrange_step_left_tr(cr, -fixed_1, sx, id);
3934
97.6M
                    sx -= x_inc;
3935
97.6M
                    f -= n_inc;
3936
97.6M
                    if (f < 0)
3937
44.7M
                        f += d, sx--;
3938
97.6M
                    cursor_left_tr(cr, sx, id);
3939
97.6M
                    y_steps--;
3940
97.6M
                }
3941
9.04M
            }
3942
3943
            /* Phase 3 */
3944
13.8M
            if (phase3_y_steps == 0) {
3945
1.10M
endFallingVerticalOnEdgeOfPixel:
3946
1.10M
                cursor_always_step_inrange_vertical_tr(cr, 0, sx, id);
3947
1.10M
                cursor_null_tr(cr);
3948
12.7M
            } else {
3949
12.7M
                cursor_step_tr(cr, -phase3_y_steps, sx, id, skip);
3950
12.7M
                cursor_left_tr(cr, ex, id);
3951
12.7M
                assert(cr->left == ex && cr->lid == id && cr->right == sx && cr->rid == id);
3952
12.7M
            }
3953
13.8M
        }
3954
51.1M
endFalling: {}
3955
51.1M
    }
3956
3957
116M
end:
3958
116M
    if (truncated) {
3959
10.3M
        cr->left = saved_ex;
3960
10.3M
        cr->lid = id;
3961
10.3M
        cr->right = saved_ex;
3962
10.3M
        cr->rid = id;
3963
10.3M
        cr->y = saved_ey;
3964
10.3M
    }
3965
116M
}
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.28M
{
3969
3.28M
        int ax = (sx + c1x)>>1;
3970
3.28M
        int ay = (sy + c1y)>>1;
3971
3.28M
        int bx = (c1x + c2x)>>1;
3972
3.28M
        int by = (c1y + c2y)>>1;
3973
3.28M
        int cx = (c2x + ex)>>1;
3974
3.28M
        int cy = (c2y + ey)>>1;
3975
3.28M
        int dx = (ax + bx)>>1;
3976
3.28M
        int dy = (ay + by)>>1;
3977
3.28M
        int fx = (bx + cx)>>1;
3978
3.28M
        int fy = (by + cy)>>1;
3979
3.28M
        int gx = (dx + fx)>>1;
3980
3.28M
        int gy = (dy + fy)>>1;
3981
3982
3.28M
        assert(depth >= 0);
3983
3.28M
        if (depth == 0) {
3984
1.68M
            *id += 1;
3985
1.68M
            mark_line_tr_app(cr, sx, sy, ex, ey, *id);
3986
1.68M
        } else {
3987
1.60M
            depth--;
3988
1.60M
            mark_curve_tr_app(cr, sx, sy, ax, ay, dx, dy, gx, gy, depth, id);
3989
1.60M
            mark_curve_tr_app(cr, gx, gy, fx, fy, cx, cy, ex, ey, depth, id);
3990
1.60M
        }
3991
3.28M
}
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
83.8k
{
4021
83.8k
    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
83.8k
    if (test < 0)
4024
0
        mark_curve_big_tr_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth, id);
4025
83.8k
    else
4026
83.8k
        mark_curve_tr_app(cr, sx, sy, c1x, c1y, c2x, c2y, ex, ey, depth, id);
4027
83.8k
}
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
12.1M
{
4036
12.1M
    return make_table_template(pdev, path, ibox, 4, 0, scanlines, index, table);
4037
12.1M
}
4038
4039
static void
4040
fill_zero_app_tr(int *row, const fixed *x)
4041
7.51k
{
4042
7.51k
    int n = *row = (*row)+2; /* Increment the count */
4043
7.51k
    row[4*n-7] = x[0];
4044
7.51k
    row[4*n-6] = 0;
4045
7.51k
    row[4*n-5] = x[1];
4046
7.51k
    row[4*n-4] = 0;
4047
7.51k
    row[4*n-3] = x[1];
4048
7.51k
    row[4*n-2] = (1<<1)|1;
4049
7.51k
    row[4*n-1] = x[1];
4050
7.51k
    row[4*n  ] = 1;
4051
7.51k
}
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
13.1M
{
4059
13.1M
    gs_fixed_rect  ibox;
4060
13.1M
    gs_fixed_rect  bbox;
4061
13.1M
    int            scanlines;
4062
13.1M
    const subpath *psub;
4063
13.1M
    int           *index;
4064
13.1M
    int           *table;
4065
13.1M
    int            i;
4066
13.1M
    cursor_tr      cr;
4067
13.1M
    int            code;
4068
13.1M
    int            id = 0;
4069
13.1M
    int            zero;
4070
4071
13.1M
    edgebuffer->index = NULL;
4072
13.1M
    edgebuffer->table = NULL;
4073
4074
    /* Bale out if no actual path. We see this with the clist */
4075
13.1M
    if (path->first_subpath == NULL)
4076
895k
        return 0;
4077
4078
12.2M
    zero = make_bbox(path, clip, &bbox, &ibox, 0);
4079
12.2M
    if (zero < 0)
4080
0
        return zero;
4081
4082
12.2M
    if (ibox.q.y <= ibox.p.y)
4083
99.2k
        return 0;
4084
4085
12.1M
    code = make_table_tr_app(pdev, path, &ibox, &scanlines, &index, &table);
4086
12.1M
    if (code != 0) /* > 0 means "retry with smaller height" */
4087
193
        return code;
4088
4089
12.1M
    if (scanlines == 0)
4090
0
        return 0;
4091
4092
12.1M
    if (zero) {
4093
6.92k
        code = zero_case(pdev, path, &ibox, index, table, fixed_flat, fill_zero_app_tr);
4094
12.1M
    } else {
4095
4096
    /* Step 2 continued: Now we run through the path, filling in the real
4097
     * values. */
4098
12.1M
    cr.scanlines = scanlines;
4099
12.1M
    cr.index     = index;
4100
12.1M
    cr.table     = table;
4101
12.1M
    cr.base      = ibox.p.y;
4102
30.6M
    for (psub = path->first_subpath; psub != 0;) {
4103
18.5M
        const segment *pseg = (const segment *)psub;
4104
18.5M
        fixed ex = pseg->pt.x;
4105
18.5M
        fixed ey = pseg->pt.y;
4106
18.5M
        fixed ix = ex;
4107
18.5M
        fixed iy = ey;
4108
18.5M
        fixed sx, sy;
4109
4110
18.5M
        if ((ey & 0xff) == 0) {
4111
976k
            cr.left  = max_fixed;
4112
976k
            cr.right = min_fixed;
4113
17.5M
        } else {
4114
17.5M
            cr.left = cr.right = ex;
4115
17.5M
        }
4116
18.5M
        cr.lid = cr.rid = id+1;
4117
18.5M
        cr.y = ey;
4118
18.5M
        cr.d = DIRN_UNSET;
4119
18.5M
        cr.first = 1;
4120
18.5M
        cr.saved = 0;
4121
4122
845M
        while ((pseg = pseg->next) != 0 &&
4123
833M
               pseg->type != s_start
4124
827M
            ) {
4125
827M
            sx = ex;
4126
827M
            sy = ey;
4127
827M
            ex = pseg->pt.x;
4128
827M
            ey = pseg->pt.y;
4129
4130
827M
            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
83.8k
                case s_curve: {
4137
83.8k
                    const curve_segment *const pcur = (const curve_segment *)pseg;
4138
83.8k
                    int k = gx_curve_log2_samples(sx, sy, pcur, fixed_flat);
4139
4140
83.8k
                    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
83.8k
                    break;
4142
0
                }
4143
0
                case s_gap:
4144
810M
                case s_line:
4145
827M
                case s_line_close:
4146
827M
                    mark_line_tr_app(&cr, sx, sy, ex, ey, ++id);
4147
827M
                    break;
4148
827M
            }
4149
827M
        }
4150
        /* And close any open segments */
4151
18.5M
        mark_line_tr_app(&cr, ex, ey, ix, iy, ++id);
4152
18.5M
        cursor_flush_tr(&cr, ex, id);
4153
18.5M
        psub = (const subpath *)pseg;
4154
18.5M
    }
4155
12.1M
    }
4156
4157
    /* Step 2 complete: We now have a complete list of intersection data in
4158
     * table, indexed by index. */
4159
4160
12.1M
    edgebuffer->base   = ibox.p.y;
4161
12.1M
    edgebuffer->height = scanlines;
4162
12.1M
    edgebuffer->xmin   = ibox.p.x;
4163
12.1M
    edgebuffer->xmax   = ibox.q.x;
4164
12.1M
    edgebuffer->index  = index;
4165
12.1M
    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
410M
    for (i=0; i < scanlines; i++) {
4176
397M
        int *row = &table[index[i]];
4177
397M
        int  rowlen = *row++;
4178
4179
        /* Bubblesort short runs, qsort longer ones. */
4180
        /* Figure of '6' comes from testing */
4181
397M
        if (rowlen <= 6) {
4182
395M
            int j, k;
4183
864M
            for (j = 0; j < rowlen-1; j++) {
4184
469M
                int * gs_restrict t = &row[j<<2];
4185
1.05G
                for (k = j+1; k < rowlen; k++) {
4186
590M
                    int * gs_restrict s = &row[k<<2];
4187
590M
                    int tmp;
4188
590M
                    if (t[0] < s[0])
4189
279M
                        continue;
4190
310M
                    if (t[0] > s[0])
4191
291M
                        goto swap0213;
4192
19.4M
                    if (t[2] < s[2])
4193
1.72M
                        continue;
4194
17.7M
                    if (t[2] > s[2])
4195
2.89M
                        goto swap213;
4196
14.8M
                    if (t[1] < s[1])
4197
13.5M
                        continue;
4198
1.29M
                    if (t[1] > s[1])
4199
1.29M
                        goto swap13;
4200
46
                    if (t[3] <= s[3])
4201
46
                        continue;
4202
0
                    if (0) {
4203
291M
swap0213:
4204
291M
                        tmp = t[0], t[0] = s[0], s[0] = tmp;
4205
294M
swap213:
4206
294M
                        tmp = t[2], t[2] = s[2], s[2] = tmp;
4207
295M
swap13:
4208
295M
                        tmp = t[1], t[1] = s[1], s[1] = tmp;
4209
295M
                    }
4210
295M
                    tmp = t[3], t[3] = s[3], s[3] = tmp;
4211
295M
                }
4212
469M
            }
4213
395M
        } else
4214
2.88M
            qsort(row, rowlen, 4*sizeof(int), edgecmp_tr);
4215
397M
    }
4216
4217
12.1M
    return 0;
4218
12.1M
}
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
13.1M
{
4226
13.1M
    int i;
4227
13.1M
    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
411M
    for (i=0; i < edgebuffer->height; i++) {
4237
397M
        int *row      = &edgebuffer->table[edgebuffer->index[i]];
4238
397M
        int  rowlen   = *row++;
4239
397M
        int *rowstart = row;
4240
397M
        int *rowout   = row;
4241
397M
        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
397M
        marked_to = INT_MIN;
4245
874M
        while (rowlen > 0) {
4246
476M
            if (rule == gx_rule_even_odd) {
4247
                /* Even Odd */
4248
36.4M
                ll   = *row++;
4249
36.4M
                llid = (*row++)>>1;
4250
36.4M
                lr   = *row++;
4251
36.4M
                lrid = *row++;
4252
36.4M
                rowlen--;
4253
4254
                /* We will fill solidly from ll to at least lr, possibly further */
4255
36.4M
                assert(rowlen > 0);
4256
36.4M
                (void)row++; /* rl not needed here */
4257
36.4M
                (void)row++;
4258
36.4M
                rr   = *row++;
4259
36.4M
                rrid = *row++;
4260
36.4M
                rowlen--;
4261
36.4M
                if (rr > lr) {
4262
33.2M
                    lr   = rr;
4263
33.2M
                    lrid = rrid;
4264
33.2M
                }
4265
440M
            } else {
4266
                /* Non-Zero */
4267
440M
                int w;
4268
4269
440M
                ll   = *row++;
4270
440M
                llid = *row++;
4271
440M
                lr   = *row++;
4272
440M
                lrid = *row++;
4273
440M
                wind = -(llid&1) | 1;
4274
440M
                llid >>= 1;
4275
440M
                rowlen--;
4276
4277
440M
                assert(rowlen > 0);
4278
454M
                do {
4279
454M
                    (void)row++; /* rl not needed */
4280
454M
                    rlid = *row++;
4281
454M
                    rr   = *row++;
4282
454M
                    rrid = *row++;
4283
454M
                    w = -(rlid&1) | 1;
4284
454M
                    rlid >>= 1;
4285
454M
                    rowlen--;
4286
454M
                    if (rr > lr) {
4287
436M
                        lr   = rr;
4288
436M
                        lrid = rrid;
4289
436M
                    }
4290
454M
                    wind += w;
4291
454M
                    if (wind == 0)
4292
440M
                        break;
4293
454M
                } while (rowlen > 0);
4294
440M
            }
4295
4296
476M
            if (lr < marked_to)
4297
1.73M
                continue;
4298
4299
475M
            if (marked_to >= ll) {
4300
5.84M
                if (rowout == rowstart) {
4301
1.93k
                    ll   = marked_to;
4302
1.93k
                    llid = --marked_id;
4303
5.84M
                } else {
4304
5.84M
                    rowout -= 4;
4305
5.84M
                    ll   = rowout[0];
4306
5.84M
                    llid = rowout[1];
4307
5.84M
                }
4308
5.84M
            }
4309
4310
475M
            if (lr >= ll) {
4311
475M
                *rowout++ = ll;
4312
475M
                *rowout++ = llid;
4313
475M
                *rowout++ = lr;
4314
475M
                *rowout++ = lrid;
4315
475M
                marked_to = lr;
4316
475M
            }
4317
475M
        }
4318
397M
        rowstart[-1] = (rowout - rowstart)>>2;
4319
397M
    }
4320
13.1M
    return 0;
4321
13.1M
}
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
13.1M
{
4330
13.1M
    int i, j, code;
4331
13.1M
    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
55.4M
    for (i=0; i < edgebuffer->height; ) {
4341
42.3M
        int *row    = &edgebuffer->table[edgebuffer->index[i]];
4342
42.3M
        int  rowlen = *row++;
4343
42.3M
        int *row2;
4344
42.3M
        int *rowptr;
4345
42.3M
        int *row2ptr;
4346
42.3M
        int y_band_max;
4347
4348
42.3M
        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
42.3M
        } else {
4353
42.3M
            y_band_max = edgebuffer->height;
4354
42.3M
        }
4355
4356
        /* See how many scanlines match i */
4357
397M
        for (j = i+1; j < y_band_max; j++) {
4358
385M
            int row2len;
4359
4360
385M
            row2    = &edgebuffer->table[edgebuffer->index[j]];
4361
385M
            row2len = *row2++;
4362
385M
            row2ptr = row2;
4363
385M
            rowptr  = row;
4364
4365
385M
            if (rowlen != row2len)
4366
1.16M
                break;
4367
777M
            while (row2len > 0) {
4368
421M
                if (rowptr[1] != row2ptr[1] || rowptr[3] != row2ptr[3])
4369
29.0M
                    goto rowdifferent;
4370
392M
                rowptr  += 4;
4371
392M
                row2ptr += 4;
4372
392M
                row2len--;
4373
392M
            }
4374
384M
        }
4375
42.3M
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
121M
        while (rowlen > 0) {
4381
79.5M
            int left  = row[0];
4382
79.5M
            int right = row[2];
4383
79.5M
            row += 4;
4384
79.5M
            left = fixed2int(left);
4385
79.5M
            right = fixed2int(right + fixed_1 - 1);
4386
79.5M
            rowlen--;
4387
4388
79.5M
            right -= left;
4389
79.5M
            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
79.5M
                if (log_op < 0)
4399
0
                    code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+i, right, 1, pdevc->colors.pure);
4400
79.5M
                else
4401
79.5M
                    code = gx_fill_rectangle_device_rop(left, edgebuffer->base+i, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
4402
79.5M
                if (code < 0)
4403
1
                    return code;
4404
79.5M
            }
4405
79.5M
        }
4406
4407
        /* The middle section (all but the first and last
4408
         * scanlines) can be sent as a trapezoid. */
4409
42.3M
        if (i + 2 < j) {
4410
12.4M
            gs_fixed_edge le;
4411
12.4M
            gs_fixed_edge re;
4412
12.4M
            fixed ybot = int2fixed(edgebuffer->base+i+1);
4413
12.4M
            fixed ytop = int2fixed(edgebuffer->base+j-1);
4414
12.4M
            int *row3, *row4;
4415
12.4M
            int offset = 1;
4416
12.4M
            row    = &edgebuffer->table[edgebuffer->index[i]];
4417
12.4M
            row2    = &edgebuffer->table[edgebuffer->index[i+1]];
4418
12.4M
            row3    = &edgebuffer->table[edgebuffer->index[j-2]];
4419
12.4M
            row4    = &edgebuffer->table[edgebuffer->index[j-1]];
4420
12.4M
            rowlen = *row;
4421
25.8M
            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
13.4M
                if (row[offset] <= row4[offset]) {
4452
8.87M
                    le.start.x = row2[offset] - (fixed_half-1);
4453
8.87M
                    le.end.x   = row4[offset] - (fixed_half-1);
4454
8.87M
                    le.start.y = ybot + fixed_half;
4455
8.87M
                    le.end.y   = ytop + fixed_half;
4456
8.87M
                } else {
4457
4.59M
                    le.start.x = row [offset] - (fixed_half-1);
4458
4.59M
                    le.end.x   = row3[offset] - (fixed_half-1);
4459
4.59M
                    le.start.y = ybot - (fixed_half-1);
4460
4.59M
                    le.end.y   = ytop - (fixed_half-1);
4461
4.59M
                }
4462
13.4M
                if (row[offset+2] <= row4[offset+2]) {
4463
8.89M
                    re.start.x = row [offset+2] + fixed_half;
4464
8.89M
                    re.end.x   = row3[offset+2] + fixed_half;
4465
8.89M
                    re.start.y = ybot - (fixed_half-1);
4466
8.89M
                    re.end.y   = ytop - (fixed_half-1);
4467
8.89M
                } else {
4468
4.56M
                    re.start.x = row2[offset+2] + fixed_half;
4469
4.56M
                    re.end.x   = row4[offset+2] + fixed_half;
4470
4.56M
                    re.start.y = ybot + fixed_half;
4471
4.56M
                    re.end.y   = ytop + fixed_half;
4472
4.56M
                }
4473
13.4M
                offset += 4;
4474
13.4M
                rowlen--;
4475
4476
13.4M
                assert(re.start.x >= le.start.x);
4477
13.4M
                assert(re.end.x >= le.end.x);
4478
13.4M
                assert(le.start.y <= ybot + fixed_half);
4479
13.4M
                assert(re.start.y <= ybot + fixed_half);
4480
13.4M
                assert(le.end.y >= ytop - (fixed_half - 1));
4481
13.4M
                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
13.4M
                code = dev_proc(pdev, fill_trapezoid)(
4492
13.4M
                                pdev,
4493
13.4M
                                &le,
4494
13.4M
                                &re,
4495
13.4M
                                ybot + fixed_half,
4496
13.4M
                                ytop - (fixed_half - 1),
4497
13.4M
                                0, /* bool swap_axes */
4498
13.4M
                                pdevc, /*const gx_drawing_color *pdcolor */
4499
13.4M
                                log_op);
4500
13.4M
                if (code < 0)
4501
0
                    return code;
4502
13.4M
            }
4503
12.4M
        }
4504
4505
42.3M
        if (i + 1 < j)
4506
18.6M
        {
4507
            /* The last scanline is always sent as rectangles */
4508
18.6M
            row    = &edgebuffer->table[edgebuffer->index[j-1]];
4509
18.6M
            rowlen = *row++;
4510
39.2M
            while (rowlen > 0) {
4511
20.5M
                int left  = row[0];
4512
20.5M
                int right = row[2];
4513
20.5M
                row += 4;
4514
20.5M
                left = fixed2int(left);
4515
20.5M
                right = fixed2int(right + fixed_1 - 1);
4516
20.5M
                rowlen--;
4517
4518
20.5M
                right -= left;
4519
20.5M
                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
20.5M
                    if (log_op < 0)
4529
0
                        code = dev_proc(pdev, fill_rectangle)(pdev, left, edgebuffer->base+j-1, right, 1, pdevc->colors.pure);
4530
20.5M
                    else
4531
20.5M
                        code = gx_fill_rectangle_device_rop(left, edgebuffer->base+j-1, right, 1, pdevc, pdev, (gs_logical_operation_t)log_op);
4532
20.5M
                    if (code < 0)
4533
0
                        return code;
4534
20.5M
                }
4535
20.5M
            }
4536
18.6M
        }
4537
42.3M
        i = j;
4538
42.3M
    }
4539
13.1M
    return 0;
4540
13.1M
}
4541
4542
4543
void
4544
gx_edgebuffer_init(gx_edgebuffer * edgebuffer)
4545
14.1M
{
4546
14.1M
    edgebuffer->base   = 0;
4547
14.1M
    edgebuffer->height = 0;
4548
14.1M
    edgebuffer->index  = NULL;
4549
14.1M
    edgebuffer->table  = NULL;
4550
14.1M
}
4551
4552
void
4553
gx_edgebuffer_fin(gx_device     * pdev,
4554
                  gx_edgebuffer * edgebuffer)
4555
14.1M
{
4556
14.1M
    gs_free_object(pdev->memory, edgebuffer->table, "scanc intersects buffer");
4557
14.1M
    gs_free_object(pdev->memory, edgebuffer->index, "scanc index buffer");
4558
14.1M
    edgebuffer->index = NULL;
4559
14.1M
    edgebuffer->table = NULL;
4560
14.1M
}
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
14.1M
{
4600
14.1M
    int code;
4601
14.1M
    gx_edgebuffer eb;
4602
14.1M
    gs_fixed_rect ibox2 = *ibox;
4603
14.1M
    int height;
4604
14.1M
    int mfb = dev->max_fill_band;
4605
4606
14.1M
    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
14.1M
    height = ibox2.q.y - ibox2.p.y;
4611
4612
14.1M
    do {
4613
14.1M
        gx_edgebuffer_init(&eb);
4614
14.1M
        while (1) {
4615
14.1M
            ibox2.q.y = ibox2.p.y + height;
4616
14.1M
            if (ibox2.q.y > ibox->q.y)
4617
94
                ibox2.q.y = ibox->q.y;
4618
14.1M
            code = sc->scan_convert(dev,
4619
14.1M
                                    ppath,
4620
14.1M
                                    &ibox2,
4621
14.1M
                                    &eb,
4622
14.1M
                                    flat);
4623
14.1M
            if (code <= 0)
4624
14.1M
                break;
4625
            /* Let's shrink the ibox and try again */
4626
202
            if (mfb && height == mfb) {
4627
                /* Can't shrink the height any more! */
4628
0
                code = gs_error_rangecheck;
4629
0
                break;
4630
0
            }
4631
202
            height = height/code;
4632
202
            if (mfb)
4633
0
                height = (height + mfb-1) & ~(mfb-1);
4634
202
            if (height < (mfb ? mfb : 1)) {
4635
0
                code = gs_error_VMerror;
4636
0
                break;
4637
0
            }
4638
202
        }
4639
14.1M
        if (code >= 0)
4640
14.1M
            code = sc->filter(dev,
4641
14.1M
                              &eb,
4642
14.1M
                              rule);
4643
14.1M
        if (code >= 0)
4644
14.1M
            code = sc->fill(dev,
4645
14.1M
                            pdevc,
4646
14.1M
                            &eb,
4647
14.1M
                            lop);
4648
14.1M
        gx_edgebuffer_fin(dev,&eb);
4649
14.1M
        ibox2.p.y += height;
4650
14.1M
    }
4651
14.1M
    while (ibox2.p.y < ibox->q.y);
4652
4653
14.1M
    return code;
4654
14.1M
}