Coverage Report

Created: 2022-04-16 11:23

/src/ghostpdl/base/gdevddrw.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2021 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.,  1305 Grant Avenue - Suite 200, Novato,
13
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14
*/
15
16
/* Default polygon and image drawing device procedures */
17
#include "math_.h"
18
#include "memory_.h"
19
#include "stdint_.h"
20
#include "gx.h"
21
#include "gpcheck.h"
22
#include "gserrors.h"
23
#include "gsrect.h"
24
#include "gxfixed.h"
25
#include "gxmatrix.h"
26
#include "gxdcolor.h"
27
#include "gxdevice.h"
28
#include "gxiparam.h"
29
#include "gxgstate.h"
30
#include "gxhldevc.h"
31
#include "gdevddrw.h"
32
/*
33
#include "gxdtfill.h" - Do not remove this comment.
34
                        "gxdtfill.h" is included below.
35
*/
36
37
#define SWAP(a, b, t)\
38
286k
  (t = a, a = b, b = t)
39
40
/* ---------------- Polygon and line drawing ---------------- */
41
42
/* Define the 'remainder' analogue of fixed_mult_quo. */
43
static fixed
44
fixed_mult_rem(fixed a, fixed b, fixed c)
45
898k
{
46
    /* All kinds of truncation may happen here, but it's OK. */
47
898k
    return a * b - fixed_mult_quo(a, b, c) * c;
48
898k
}
49
50
/*
51
 * The trapezoid fill algorithm uses trap_line structures to keep track of
52
 * the left and right edges during the Bresenham loop.
53
 */
54
typedef struct trap_line_s {
55
        /*
56
         * h is the y extent of the line (edge.end.y - edge.start.y).
57
         * We know h > 0.
58
         */
59
    fixed h;
60
        /*
61
         * The dx/dy ratio for the line is di + df/h.
62
         * (The quotient refers to the l.s.b. of di, not fixed_1.)
63
         * We know 0 <= df < h.
64
         */
65
    int di;
66
    fixed df;
67
        /*
68
         * The intersection of the line with a scan line is x + xf/h + 1.
69
         * (The 1 refers to the least significant bit of x, not fixed_1;
70
         * similarly, the quotient refers to the l.s.b. of x.)
71
         * We know -h <= xf < 0.
72
         *
73
         * This rational value preciselly represents the mathematical line
74
         * (with no machine arithmetic error).
75
         *
76
         * Note that the fractional part is negative to simplify
77
         * some conditions in the Bresenham algorithm.
78
         * Due to that some expressions are inobvious.
79
         * We believe that it's a kind of archaic
80
         * for the modern hyperthreading architecture,
81
         * we still keep it because the code passed a huge testing
82
         * on various platforms.
83
         */
84
    fixed x, xf;
85
        /*
86
         * We increment (x,xf) by (ldi,ldf) after each scan line.
87
         * (ldi,ldf) is just (di,df) converted to fixed point.
88
         * We know 0 <= ldf < h.
89
         */
90
    fixed ldi, ldf;
91
} trap_line;
92
93
/*
94
 * The linear color trapezoid fill algorithm uses trap_color structures to keep track of
95
 * the color change during the Bresenham loop.
96
 */
97
typedef struct trap_gradient_s {
98
        frac31 *c; /* integer part of the color in frac32 units. */
99
        int32_t *f; /* the fraction part numerator */
100
        int32_t *num; /* the gradient numerator */
101
        int32_t den; /* color gradient denominator */
102
} trap_gradient;
103
104
/*
105
 * Compute the di and df members of a trap_line structure.  The x extent
106
 * (edge.end.x - edge.start.x) is a parameter; the y extent (h member)
107
 * has already been set.  Also adjust x for the initial y.
108
 */
109
static inline void
110
compute_dx(trap_line *tl, fixed xd, fixed ys)
111
3.91M
{
112
3.91M
    fixed h = tl->h;
113
3.91M
    int di;
114
115
3.91M
    if (xd >= 0) {
116
1.19M
        if (xd < h)
117
660k
            tl->di = 0, tl->df = xd;
118
530k
        else {
119
530k
            tl->di = di = (int)(xd / h);
120
530k
            tl->df = xd - di * h;
121
530k
            tl->x += ys * di;
122
530k
        }
123
2.72M
    } else {
124
2.72M
        if ((tl->df = xd + h) >= 0 /* xd >= -h */)
125
1.40M
            tl->di = -1, tl->x -= ys;
126
1.31M
        else {
127
1.31M
            tl->di = di = (int)((xd + 1) / h - 1);
128
1.31M
            tl->df = xd - di * h;
129
1.31M
            tl->x += ys * di;
130
1.31M
        }
131
2.72M
    }
132
3.91M
}
133
134
11.1M
#define YMULT_LIMIT (max_fixed / fixed_1)
135
136
/* Compute ldi, ldf, and xf similarly. */
137
static inline void
138
compute_ldx(trap_line *tl, fixed ys)
139
4.35M
{
140
4.35M
    int di = tl->di;
141
4.35M
    fixed df = tl->df;
142
4.35M
    fixed h = tl->h;
143
144
4.35M
    if ( df < YMULT_LIMIT ) {
145
4.35M
         if ( df == 0 )    /* vertical edge, worth checking for */
146
529k
             tl->ldi = int2fixed(di), tl->ldf = 0, tl->xf = -h;
147
3.82M
         else {
148
3.82M
             tl->ldi = int2fixed(di) + int2fixed(df) / h;
149
3.82M
             tl->ldf = int2fixed(df) % h;
150
3.82M
             tl->xf =
151
3.82M
                 (ys < fixed_1 ? ys * df % h : fixed_mult_rem(ys, df, h)) - h;
152
3.82M
         }
153
4.35M
    }
154
541
    else {
155
541
        tl->ldi = int2fixed(di) + fixed_mult_quo(fixed_1, df, h);
156
541
        tl->ldf = fixed_mult_rem(fixed_1, df, h);
157
541
        tl->xf = fixed_mult_rem(ys, df, h) - h;
158
541
    }
159
4.35M
}
160
161
static inline int
162
init_gradient(trap_gradient *g, const gs_fill_attributes *fa,
163
                const gs_linear_color_edge *e, const gs_linear_color_edge *e1,
164
                const trap_line *l, fixed ybot, int num_components)
165
0
{
166
0
    int i;
167
0
    int64_t c;
168
0
    int32_t d;
169
170
0
    if (e->c1 == NULL || e->c0 == NULL)
171
0
        g->den = 0; /* A wedge - the color is axial along another edge. */
172
0
    else {
173
0
        bool ends_from_fa = (e1->c1 == NULL || e1->c0 == NULL);
174
175
0
        if (ends_from_fa)
176
0
            g->den = fa->yend - fa->ystart;
177
0
        else {
178
0
            g->den = e->end.y - e->start.y;
179
0
            if (g->den != l->h)
180
0
                return_error(gs_error_unregistered); /* Must not happen. */
181
0
        }
182
0
        for (i = 0; i < num_components; i++) {
183
0
            g->num[i] = e->c1[i] - e->c0[i];
184
0
            c = (int64_t)g->num[i] * (uint32_t)(ybot -
185
0
                    (ends_from_fa ? fa->ystart : e->start.y));
186
0
            d = (int32_t)(c / g->den);
187
0
            g->c[i] = e->c0[i] + d;
188
0
            c -= (int64_t)d * g->den;
189
0
            if (c < 0) {
190
0
                g->c[i]--;
191
0
                c += g->den;
192
0
            }
193
0
            g->f[i] = (int32_t)c;
194
0
        }
195
0
    }
196
0
    return 0;
197
0
}
198
199
static inline void
200
step_gradient(trap_gradient *g, int num_components)
201
0
{
202
0
    int i;
203
204
0
    if (g->den == 0)
205
0
        return;
206
0
    for (i = 0; i < num_components; i++) {
207
0
        int64_t fc = g->f[i] + (int64_t)g->num[i] * fixed_1;
208
0
        int32_t fc32;
209
210
0
        g->c[i] += (int32_t)(fc / g->den);
211
0
        fc32 = (int32_t)(fc -  fc / g->den * g->den);
212
0
        if (fc32 < 0) {
213
0
            fc32 += g->den;
214
0
            g->c[i]--;
215
0
        }
216
0
        g->f[i] = fc32;
217
0
    }
218
0
}
219
220
static inline bool
221
check_gradient_overflow(const gs_linear_color_edge *le, const gs_linear_color_edge *re)
222
0
{
223
0
    if (le->c1 == NULL || re->c1 == NULL) {
224
        /* A wedge doesn't use a gradient by X. */
225
0
        return false;
226
0
    } else {
227
        /* Check whether set_x_gradient, fill_linear_color_scanline can overflow.
228
229
           dev_proc(dev, fill_linear_color_scanline) can perform its computation in 32-bit fractions,
230
           so we assume it never overflows. Devices which implement it with no this
231
           assumption must implement the check in gx_default_fill_linear_color_trapezoid,
232
           gx_default_fill_linear_color_triangle with a function other than this one.
233
234
           Since set_x_gradient perform computations in int64_t, which provides 63 bits
235
           while multiplying a 32-bits color value to a coordinate,
236
           we must restrict the X span with 63 - 32 = 31 bits.
237
         */
238
0
        int32_t xl = min(le->start.x, le->end.x);
239
0
        int32_t xr = min(re->start.x, re->end.x);
240
        /* The pixel span boundaries : */
241
0
        return arith_rshift_1(xr) - arith_rshift_1(xl) >= 0x3FFFFFFE;
242
0
    }
243
0
}
244
245
static inline int
246
set_x_gradient_nowedge(trap_gradient *xg, const trap_gradient *lg, const trap_gradient *rg,
247
             const trap_line *l, const trap_line *r, int il, int ir, int num_components)
248
0
{
249
    /* Ignoring the ending coordinats fractions,
250
       so the gridient is slightly shifted to the left (in <1 'fixed' unit). */
251
0
    int32_t xl = l->x - (l->xf == -l->h ? 1 : 0) - fixed_half; /* Revert the GX_FILL_TRAPEZOID shift. */
252
0
    int32_t xr = r->x - (r->xf == -r->h ? 1 : 0) - fixed_half; /* Revert the GX_FILL_TRAPEZOID shift. */
253
    /* The pixel span boundaries : */
254
0
    int32_t x0 = int2fixed(il) + fixed_half; /* Shift to the pixel center. */
255
0
    int32_t x1 = int2fixed(ir) - fixed_half; /* The center of the last pixel to paint. */
256
0
    int i;
257
258
#   ifdef DEBUG
259
        if (arith_rshift_1(xr) - arith_rshift_1(xl) >= 0x3FFFFFFE) /* Can overflow ? */
260
            return_error(gs_error_unregistered); /* Must not happen. */
261
#   endif
262
    /* We cannot compute the color of the 'ir' pixel
263
       because it can overflow 'c1' due to the pixel ir center
264
       may be greater that r->x .
265
       Therefore we base the proportion on the pixel index ir-1 (see comment to 'x1').
266
       Debugged with CET 12-14O.PS SpecialTestJ02Test12.
267
     */
268
0
    xg->den = fixed2int(x1 - x0);
269
0
    if (xg->den <= 0) {
270
        /* The span contains a single pixel, will construct a degenerate gradient. */
271
0
        xg->den = 1; /* Safety (against zerodivide). */
272
0
    }
273
0
    for (i = 0; i < num_components; i++) {
274
        /* Ignoring the ending colors fractions,
275
           so the color gets a slightly smaller value
276
           (in <1 'frac31' unit), but it's not important due to
277
           the further conversion to [0, 1 << cinfo->comp_bits[j]],
278
           which drops the fraction anyway. */
279
0
        int32_t cl = lg->c[i];
280
0
        int32_t cr = rg->c[i];
281
0
        int32_t c0 = (int32_t)(cl + ((int64_t)cr - cl) * (x0 - xl) / (xr - xl));
282
0
        int32_t c1 = (int32_t)(cl + ((int64_t)cr - cl) * (x1 - xl) / (xr - xl));
283
284
0
        xg->c[i] = c0;
285
0
        xg->f[i] = 0; /* Insufficient bits to compute it better.
286
                         The color so the color gets a slightly smaller value
287
                         (in <1 'frac31' unit), but it's not important due to
288
                         the further conversion to [0, 1 << cinfo->comp_bits[j]],
289
                         which drops the fraction anyway.
290
                         So setting 0 appears pretty good and fast. */
291
0
        xg->num[i] = c1 - c0;
292
0
    }
293
0
    return 0;
294
0
}
295
296
static inline int
297
set_x_gradient(trap_gradient *xg, const trap_gradient *lg, const trap_gradient *rg,
298
             const trap_line *l, const trap_line *r, int il, int ir, int num_components)
299
0
{
300
0
    if (lg->den == 0 || rg->den == 0) {
301
        /* A wedge doesn't use a gradient by X. */
302
0
        int i;
303
304
0
        xg->den = 1;
305
0
        for (i = 0; i < num_components; i++) {
306
0
            xg->c[i] = (lg->den == 0 ? rg->c[i] : lg->c[i]);
307
0
            xg->f[i] = 0; /* Compatible to set_x_gradient_nowedge. */
308
0
            xg->num[i] = 0;
309
0
        }
310
0
        return 0;
311
0
    } else
312
0
        return set_x_gradient_nowedge(xg, lg, rg, l, r, il, ir, num_components);
313
0
}
314
315
/*
316
 * Fill a trapezoid.
317
 * Since we need several statically defined variants of this algorithm,
318
 * we stored it in gxdtfill.h and include it configuring with
319
 * macros defined here.
320
 */
321
63.8M
#define LINEAR_COLOR 0 /* Common for shading variants. */
322
#define EDGE_TYPE gs_fixed_edge  /* Common for non-shading variants. */
323
#define FILL_ATTRS gs_logical_operation_t  /* Common for non-shading variants. */
324
325
#define GX_FILL_TRAPEZOID static int gx_fill_trapezoid_as_fd
326
#define CONTIGUOUS_FILL 0
327
2.17M
#define SWAP_AXES 1
328
2.17M
#define FILL_DIRECT 1
329
#include "gxdtfill.h"
330
#undef GX_FILL_TRAPEZOID
331
#undef CONTIGUOUS_FILL
332
#undef SWAP_AXES
333
#undef FILL_DIRECT
334
335
#define GX_FILL_TRAPEZOID static int gx_fill_trapezoid_as_nd
336
#define CONTIGUOUS_FILL 0
337
6.35M
#define SWAP_AXES 1
338
6.35M
#define FILL_DIRECT 0
339
#include "gxdtfill.h"
340
#undef GX_FILL_TRAPEZOID
341
#undef CONTIGUOUS_FILL
342
#undef SWAP_AXES
343
#undef FILL_DIRECT
344
345
#define GX_FILL_TRAPEZOID static int gx_fill_trapezoid_ns_fd
346
#define CONTIGUOUS_FILL 0
347
6.68M
#define SWAP_AXES 0
348
6.68M
#define FILL_DIRECT 1
349
#include "gxdtfill.h"
350
#undef GX_FILL_TRAPEZOID
351
#undef CONTIGUOUS_FILL
352
#undef SWAP_AXES
353
#undef FILL_DIRECT
354
355
#define GX_FILL_TRAPEZOID static int gx_fill_trapezoid_ns_nd
356
#define CONTIGUOUS_FILL 0
357
5.93M
#define SWAP_AXES 0
358
5.93M
#define FILL_DIRECT 0
359
#include "gxdtfill.h"
360
#undef GX_FILL_TRAPEZOID
361
#undef CONTIGUOUS_FILL
362
#undef SWAP_AXES
363
#undef FILL_DIRECT
364
365
#define GX_FILL_TRAPEZOID int gx_fill_trapezoid_cf_fd
366
#define CONTIGUOUS_FILL 1
367
0
#define SWAP_AXES 0
368
0
#define FILL_DIRECT 1
369
#include "gxdtfill.h"
370
#undef GX_FILL_TRAPEZOID
371
#undef CONTIGUOUS_FILL
372
#undef SWAP_AXES
373
#undef FILL_DIRECT
374
375
#define GX_FILL_TRAPEZOID int gx_fill_trapezoid_cf_nd
376
#define CONTIGUOUS_FILL 1
377
0
#define SWAP_AXES 0
378
0
#define FILL_DIRECT 0
379
#include "gxdtfill.h"
380
#undef GX_FILL_TRAPEZOID
381
#undef CONTIGUOUS_FILL
382
#undef SWAP_AXES
383
#undef FILL_DIRECT
384
385
#undef EDGE_TYPE
386
#undef LINEAR_COLOR
387
#undef FILL_ATTRS
388
389
0
#define LINEAR_COLOR 1 /* Common for shading variants. */
390
#define EDGE_TYPE gs_linear_color_edge /* Common for shading variants. */
391
#define FILL_ATTRS const gs_fill_attributes *  /* Common for non-shading variants. */
392
393
#define GX_FILL_TRAPEZOID static int gx_fill_trapezoid_ns_lc
394
#define CONTIGUOUS_FILL 0
395
#define SWAP_AXES 0
396
0
#define FILL_DIRECT 1
397
#include "gxdtfill.h"
398
#undef GX_FILL_TRAPEZOID
399
#undef CONTIGUOUS_FILL
400
#undef SWAP_AXES
401
#undef FILL_DIRECT
402
403
#define GX_FILL_TRAPEZOID static int gx_fill_trapezoid_as_lc
404
#define CONTIGUOUS_FILL 0
405
#define SWAP_AXES 1
406
0
#define FILL_DIRECT 1
407
#include "gxdtfill.h"
408
#undef GX_FILL_TRAPEZOID
409
#undef CONTIGUOUS_FILL
410
#undef SWAP_AXES
411
#undef FILL_DIRECT
412
413
#undef EDGE_TYPE
414
#undef LINEAR_COLOR
415
#undef FILL_ATTRS
416
417
int
418
gx_default_fill_trapezoid(gx_device * dev, const gs_fixed_edge * left,
419
    const gs_fixed_edge * right, fixed ybot, fixed ytop, bool swap_axes,
420
    const gx_device_color * pdevc, gs_logical_operation_t lop)
421
3.89M
{
422
3.89M
    bool fill_direct = color_writes_pure(pdevc, lop);
423
424
3.89M
    if (swap_axes) {
425
360k
        if (fill_direct)
426
315k
            return gx_fill_trapezoid_as_fd(dev, left, right, ybot, ytop, 0, pdevc, lop);
427
44.6k
        else
428
44.6k
            return gx_fill_trapezoid_as_nd(dev, left, right, ybot, ytop, 0, pdevc, lop);
429
3.53M
    } else {
430
3.53M
        if (fill_direct)
431
3.46M
            return gx_fill_trapezoid_ns_fd(dev, left, right, ybot, ytop, 0, pdevc, lop);
432
71.9k
        else
433
71.9k
            return gx_fill_trapezoid_ns_nd(dev, left, right, ybot, ytop, 0, pdevc, lop);
434
3.53M
    }
435
3.89M
}
436
437
static inline int
438
fill_linear_color_trapezoid_nocheck(gx_device *dev, const gs_fill_attributes *fa,
439
        const gs_linear_color_edge *le, const gs_linear_color_edge *re)
440
0
{
441
0
    fixed y02 = max(le->start.y, re->start.y), ymin = max(y02, fa->clip->p.y);
442
0
    fixed y13 = min(le->end.y, re->end.y), ymax = min(y13, fa->clip->q.y);
443
0
    int code;
444
445
0
    code = (fa->swap_axes ? gx_fill_trapezoid_as_lc : gx_fill_trapezoid_ns_lc)(dev,
446
0
            le, re, ymin, ymax, 0, NULL, fa);
447
0
    if (code < 0)
448
0
        return code;
449
0
    return !code;
450
0
}
451
452
/*  Fill a trapezoid with a linear color.
453
    [p0 : p1] - left edge, from bottom to top.
454
    [p2 : p3] - right edge, from bottom to top.
455
    The filled area is within Y-spans of both edges.
456
457
    This implemetation actually handles a bilinear color,
458
    in which the generatrix keeps a parallelizm to the X axis.
459
    In general a bilinear function doesn't keep the generatrix parallelizm,
460
    so the caller must decompose/approximate such functions.
461
462
    Return values :
463
    1 - success;
464
    0 - Too big. The area isn't filled. The client must decompose the area.
465
    <0 - error.
466
 */
467
int
468
gx_default_fill_linear_color_trapezoid(gx_device *dev, const gs_fill_attributes *fa,
469
        const gs_fixed_point *p0, const gs_fixed_point *p1,
470
        const gs_fixed_point *p2, const gs_fixed_point *p3,
471
        const frac31 *c0, const frac31 *c1,
472
        const frac31 *c2, const frac31 *c3)
473
0
{
474
0
    gs_linear_color_edge le, re;
475
476
0
    le.start = *p0;
477
0
    le.end = *p1;
478
0
    le.c0 = c0;
479
0
    le.c1 = c1;
480
0
    le.clip_x = fa->clip->p.x;
481
0
    re.start = *p2;
482
0
    re.end = *p3;
483
0
    re.c0 = c2;
484
0
    re.c1 = c3;
485
0
    re.clip_x = fa->clip->q.x;
486
0
    if (check_gradient_overflow(&le, &re))
487
0
        return 0;
488
0
    return fill_linear_color_trapezoid_nocheck(dev, fa, &le, &re);
489
0
}
490
491
static inline int
492
fill_linear_color_triangle(gx_device *dev, const gs_fill_attributes *fa,
493
        const gs_fixed_point *p0, const gs_fixed_point *p1,
494
        const gs_fixed_point *p2,
495
        const frac31 *c0, const frac31 *c1, const frac31 *c2)
496
0
{   /* p0 must be the lowest vertex. */
497
0
    int code;
498
0
    gs_linear_color_edge e0, e1, e2;
499
500
0
    if (p0->y == p1->y)
501
0
        return gx_default_fill_linear_color_trapezoid(dev, fa, p0, p2, p1, p2, c0, c2, c1, c2);
502
0
    if (p1->y == p2->y)
503
0
        return gx_default_fill_linear_color_trapezoid(dev, fa, p0, p2, p0, p1, c0, c2, c0, c1);
504
0
    e0.start = *p0;
505
0
    e0.end = *p2;
506
0
    e0.c0 = c0;
507
0
    e0.c1 = c2;
508
0
    e0.clip_x = fa->clip->p.x;
509
0
    e1.start = *p0;
510
0
    e1.end = *p1;
511
0
    e1.c0 = c0;
512
0
    e1.c1 = c1;
513
0
    e1.clip_x = fa->clip->q.x;
514
0
    if (p0->y < p1->y && p1->y < p2->y) {
515
0
        e2.start = *p1;
516
0
        e2.end = *p2;
517
0
        e2.c0 = c1;
518
0
        e2.c1 = c2;
519
0
        e2.clip_x = fa->clip->q.x;
520
0
        if (check_gradient_overflow(&e0, &e1))
521
0
            return 0;
522
0
        if (check_gradient_overflow(&e0, &e2))
523
0
            return 0;
524
0
        code = fill_linear_color_trapezoid_nocheck(dev, fa, &e0, &e1);
525
0
        if (code <= 0) /* Sic! */
526
0
            return code;
527
0
        return fill_linear_color_trapezoid_nocheck(dev, fa, &e0, &e2);
528
0
    } else { /* p0->y < p2->y && p2->y < p1->y */
529
0
        e2.start = *p2;
530
0
        e2.end = *p1;
531
0
        e2.c0 = c2;
532
0
        e2.c1 = c1;
533
0
        e2.clip_x = fa->clip->q.x;
534
0
        if (check_gradient_overflow(&e0, &e1))
535
0
            return 0;
536
0
        if (check_gradient_overflow(&e2, &e1))
537
0
            return 0;
538
0
        code = fill_linear_color_trapezoid_nocheck(dev, fa, &e0, &e1);
539
0
        if (code <= 0) /* Sic! */
540
0
            return code;
541
0
        return fill_linear_color_trapezoid_nocheck(dev, fa, &e2, &e1);
542
0
    }
543
0
}
544
545
/*  Fill a triangle with a linear color. */
546
int
547
gx_default_fill_linear_color_triangle(gx_device *dev, const gs_fill_attributes *fa,
548
        const gs_fixed_point *p0, const gs_fixed_point *p1,
549
        const gs_fixed_point *p2,
550
        const frac31 *c0, const frac31 *c1, const frac31 *c2)
551
0
{
552
0
    fixed dx1 = p1->x - p0->x, dy1 = p1->y - p0->y;
553
0
    fixed dx2 = p2->x - p0->x, dy2 = p2->y - p0->y;
554
555
0
    if ((int64_t)dx1 * dy2 < (int64_t)dx2 * dy1) {
556
0
        const gs_fixed_point *p = p1;
557
0
        const frac31 *c = c1;
558
559
0
        p1 = p2;
560
0
        p2 = p;
561
0
        c1 = c2;
562
0
        c2 = c;
563
0
    }
564
0
    if (p0->y <= p1->y && p0->y <= p2->y)
565
0
        return fill_linear_color_triangle(dev, fa, p0, p1, p2, c0, c1, c2);
566
0
    if (p1->y <= p0->y && p1->y <= p2->y)
567
0
        return fill_linear_color_triangle(dev, fa, p1, p2, p0, c1, c2, c0);
568
0
    else
569
0
        return fill_linear_color_triangle(dev, fa, p2, p0, p1, c2, c0, c1);
570
0
}
571
572
/* Fill a parallelogram whose points are p, p+a, p+b, and p+a+b. */
573
/* We should swap axes to get best accuracy, but we don't. */
574
/* We must be very careful to follow the center-of-pixel rule in all cases. */
575
int
576
gx_default_fill_parallelogram(gx_device * dev,
577
                 fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
578
                  const gx_device_color * pdevc, gs_logical_operation_t lop)
579
58.1k
{
580
58.1k
    fixed t;
581
58.1k
    fixed qx, qy, ym;
582
58.1k
    dev_proc_fill_trapezoid((*fill_trapezoid));
583
58.1k
    gs_fixed_edge left, right;
584
58.1k
    int code;
585
586
    /* Make a special fast check for rectangles. */
587
58.1k
    if (PARALLELOGRAM_IS_RECT(ax, ay, bx, by)) {
588
287
        gs_int_rect r;
589
590
287
        INT_RECT_FROM_PARALLELOGRAM(&r, px, py, ax, ay, bx, by);
591
287
        return gx_fill_rectangle_device_rop(r.p.x, r.p.y, r.q.x - r.p.x,
592
287
                                            r.q.y - r.p.y, pdevc, dev, lop);
593
287
    }
594
    /*
595
     * Not a rectangle.  Ensure that the 'a' line is to the left of
596
     * the 'b' line.  Testing ax <= bx is neither sufficient nor
597
     * necessary: in general, we need to compare the slopes.
598
     */
599
    /* Ensure ay >= 0, by >= 0. */
600
57.8k
    if (ay < 0)
601
16.0k
        px += ax, py += ay, ax = -ax, ay = -ay;
602
57.8k
    if (by < 0)
603
0
        px += bx, py += by, bx = -bx, by = -by;
604
57.8k
    qx = px + ax + bx;
605
57.8k
    if ((ax ^ bx) < 0) { /* In this case, the test ax <= bx is sufficient. */
606
16.8k
        if (ax > bx)
607
788
            SWAP(ax, bx, t), SWAP(ay, by, t);
608
41.0k
    } else {     /*
609
                                 * Compare the slopes.  We know that ay >= 0, by >= 0,
610
                                 * and ax and bx have the same sign; the lines are in the
611
                                 * correct order iff
612
                                 *          ay/ax >= by/bx, or
613
                                 *          ay*bx >= by*ax
614
                                 * Eventually we can probably find a better way to test this,
615
                                 * without using floating point.
616
                                 */
617
41.0k
        if ((double)ay * bx < (double)by * ax)
618
0
            SWAP(ax, bx, t), SWAP(ay, by, t);
619
41.0k
    }
620
57.8k
    fill_trapezoid = dev_proc(dev, fill_trapezoid);
621
57.8k
    qy = py + ay + by;
622
57.8k
    left.start.x = right.start.x = px;
623
57.8k
    left.start.y = right.start.y = py;
624
57.8k
    left.end.x = px + ax;
625
57.8k
    left.end.y = py + ay;
626
57.8k
    right.end.x = px + bx;
627
57.8k
    right.end.y = py + by;
628
57.8k
#define ROUNDED_SAME(p1, p2)\
629
173k
  (fixed_pixround(p1) == fixed_pixround(p2))
630
57.8k
    if (ay < by) {
631
0
        if (!ROUNDED_SAME(py, left.end.y)) {
632
0
            code = (*fill_trapezoid) (dev, &left, &right, py, left.end.y,
633
0
                                      false, pdevc, lop);
634
0
            if (code < 0)
635
0
                return code;
636
0
        }
637
0
        left.start = left.end;
638
0
        left.end.x = qx, left.end.y = qy;
639
0
        ym = right.end.y;
640
0
        if (!ROUNDED_SAME(left.start.y, ym)) {
641
0
            code = (*fill_trapezoid) (dev, &left, &right, left.start.y, ym,
642
0
                                      false, pdevc, lop);
643
0
            if (code < 0)
644
0
                return code;
645
0
        }
646
0
        right.start = right.end;
647
0
        right.end.x = qx, right.end.y = qy;
648
57.8k
    } else {
649
57.8k
        if (!ROUNDED_SAME(py, right.end.y)) {
650
784
            code = (*fill_trapezoid) (dev, &left, &right, py, right.end.y,
651
784
                                      false, pdevc, lop);
652
784
            if (code < 0)
653
0
                return code;
654
784
        }
655
57.8k
        right.start = right.end;
656
57.8k
        right.end.x = qx, right.end.y = qy;
657
57.8k
        ym = left.end.y;
658
57.8k
        if (!ROUNDED_SAME(right.start.y, ym)) {
659
52.2k
            code = (*fill_trapezoid) (dev, &left, &right, right.start.y, ym,
660
52.2k
                                      false, pdevc, lop);
661
52.2k
            if (code < 0)
662
0
                return code;
663
52.2k
        }
664
57.8k
        left.start = left.end;
665
57.8k
        left.end.x = qx, left.end.y = qy;
666
57.8k
    }
667
57.8k
    if (!ROUNDED_SAME(ym, qy))
668
784
        return (*fill_trapezoid) (dev, &left, &right, ym, qy,
669
784
                                  false, pdevc, lop);
670
57.0k
    else
671
57.0k
        return 0;
672
57.8k
#undef ROUNDED_SAME
673
57.8k
}
674
675
/* Fill a triangle whose points are p, p+a, and p+b. */
676
/* We should swap axes to get best accuracy, but we don't. */
677
int
678
gx_default_fill_triangle(gx_device * dev,
679
                 fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
680
                  const gx_device_color * pdevc, gs_logical_operation_t lop)
681
0
{
682
0
    fixed t;
683
0
    fixed ym;
684
685
0
    dev_proc_fill_trapezoid((*fill_trapezoid)) =
686
0
        dev_proc(dev, fill_trapezoid);
687
0
    gs_fixed_edge left, right;
688
0
    int code;
689
690
    /* Ensure ay >= 0, by >= 0. */
691
0
    if (ay < 0)
692
0
        px += ax, py += ay, bx -= ax, by -= ay, ax = -ax, ay = -ay;
693
0
    if (by < 0)
694
0
        px += bx, py += by, ax -= bx, ay -= by, bx = -bx, by = -by;
695
    /* Ensure ay <= by. */
696
0
    if (ay > by)
697
0
        SWAP(ax, bx, t), SWAP(ay, by, t);
698
    /*
699
     * Make a special check for a flat bottom or top,
700
     * which we can handle with a single call on fill_trapezoid.
701
     */
702
0
    left.start.x = right.start.x = px;
703
0
    left.start.y = right.start.y = py;
704
0
    if (ay == 0) {
705
        /* Flat top */
706
0
        if (ax < 0)
707
0
            left.start.x = px + ax;
708
0
        else
709
0
            right.start.x = px + ax;
710
0
        left.end.x = right.end.x = px + bx;
711
0
        left.end.y = right.end.y = py + by;
712
0
        ym = py;
713
0
    } else if (ay == by) {
714
        /* Flat bottom */
715
0
        if (ax < bx)
716
0
            left.end.x = px + ax, right.end.x = px + bx;
717
0
        else
718
0
            left.end.x = px + bx, right.end.x = px + ax;
719
0
        left.end.y = right.end.y = py + by;
720
0
        ym = py;
721
0
    } else {
722
0
        ym = py + ay;
723
0
        if (fixed_mult_quo(bx, ay, by) < ax) {
724
            /* The 'b' line is to the left of the 'a' line. */
725
0
            left.end.x = px + bx, left.end.y = py + by;
726
0
            right.end.x = px + ax, right.end.y = py + ay;
727
0
            code = (*fill_trapezoid) (dev, &left, &right, py, ym,
728
0
                                      false, pdevc, lop);
729
0
            right.start = right.end;
730
0
            right.end = left.end;
731
0
        } else {
732
            /* The 'a' line is to the left of the 'b' line. */
733
0
            left.end.x = px + ax, left.end.y = py + ay;
734
0
            right.end.x = px + bx, right.end.y = py + by;
735
0
            code = (*fill_trapezoid) (dev, &left, &right, py, ym,
736
0
                                      false, pdevc, lop);
737
0
            left.start = left.end;
738
0
            left.end = right.end;
739
0
        }
740
0
        if (code < 0)
741
0
            return code;
742
0
    }
743
0
    return (*fill_trapezoid) (dev, &left, &right, ym, right.end.y,
744
0
                              false, pdevc, lop);
745
0
}
746
747
/* Draw a one-pixel-wide line. */
748
int
749
gx_default_draw_thin_line(gx_device * dev,
750
                          fixed fx0, fixed fy0, fixed fx1, fixed fy1,
751
                    const gx_device_color * pdevc, gs_logical_operation_t lop,
752
                          fixed adjustx, fixed adjusty)
753
367k
{
754
367k
    int ix, iy, itox, itoy;
755
367k
    int epsilon;
756
757
367k
    return_if_interrupt(dev->memory);
758
759
    /* This function was updated in revision 10391 to fix problems with
760
     * mispositioned thin lines. This introduced a regression (see bug
761
     * 691030). The code was then reworked to behave in what we believe is
762
     * the correct manner, but this causes unacceptable problems with PCL
763
     * output. While the current PCL work is underway, we have therefore
764
     * amended this code to take note of the fill adjust values; if non-
765
     * zero (i.e. postscript) we do "the correct thing". If zero, we do
766
     * what we used to.
767
     *
768
     * The one case where this doesn't work is in the case where our PCL
769
     * implementation thickens lines slightly to try and approximate HP
770
     * printer behaviour. Here we do use a non-zero fill_adjust and hence
771
     * have differences; tests show that these are acceptable though.
772
     *
773
     * It is hoped that this difference in behaviour will be short lived.
774
     */
775
776
367k
    epsilon = ((adjustx | adjusty) == 0 ? fixed_epsilon : 0);
777
778
367k
    {
779
367k
        fixed h = fy1 - fy0;
780
367k
        fixed w = fx1 - fx0;
781
367k
        fixed tf;
782
367k
        bool swap_axes;
783
367k
        gs_fixed_edge left, right;
784
785
367k
        if ((w < 0 ? -w : w) <= (h < 0 ? -h : h)) {
786
            /* A "mostly-vertical" line */
787
70.8k
            if (h < 0)
788
53.6k
                SWAP(fx0, fx1, tf), SWAP(fy0, fy1, tf),
789
53.6k
                    h = -h;
790
            /* So we are plotting a trapezoid with horizontal thin edges.
791
             * If we are drawing a non-axis aligned trap, then we check
792
             * for whether a triangular extension area on the end covers an
793
             * additional pixel centre; if so, we fill an extra pixel.
794
             * If we are drawing an axis aligned trap and fill adjust is 0,
795
             * then we shouldn't need to do this.
796
             * If we are drawing an axis aligned trap, and fill adjust is non
797
             * zero, then perform the check, but with a "butt cap" rather than
798
             * a "triangle cap" region.
799
             * See bug 687721 and bug 693212 for this history of this.
800
             */
801
70.8k
            if (w == 0 && adjusty) {
802
5.76k
                int deltay;
803
5.76k
                deltay = int2fixed(fixed2int_var(fy1)) + fixed_half -fy1;
804
805
5.76k
                if ((deltay > 0) && (deltay <= fixed_half))
806
5.15k
                {
807
5.15k
                    int c = gx_fill_rectangle_device_rop(fixed2int_var(fx1),
808
5.15k
                                                         fixed2int_var(fy1),
809
5.15k
                                                         1,1,pdevc,dev,lop);
810
5.15k
                    if (c < 0) return c;
811
5.15k
                }
812
5.76k
                deltay = int2fixed(fixed2int_var(fy0)) + fixed_half -fy0;
813
814
5.76k
                if ((deltay < 0) && (deltay >= -fixed_half))
815
755
                {
816
755
                    int c = gx_fill_rectangle_device_rop(fixed2int_var(fx0),
817
755
                                                         fixed2int_var(fy0),
818
755
                                                         1,1,pdevc,dev,lop);
819
755
                    if (c < 0) return c;
820
755
                }
821
65.0k
            } else if (w != 0) {
822
65.0k
                int deltax, deltay;
823
65.0k
                deltay = int2fixed(fixed2int_var(fy1)) + fixed_half -fy1;
824
65.0k
                deltax = int2fixed(fixed2int_var(fx1)) + fixed_half -fx1;
825
826
65.0k
                if (deltax < 0) deltax=-deltax;
827
65.0k
                if ((deltay > 0) && (deltay <= fixed_half) &&
828
65.0k
                    (deltay+deltax <= fixed_half))
829
14.5k
                {
830
14.5k
                    int c = gx_fill_rectangle_device_rop(fixed2int_var(fx1),
831
14.5k
                                                         fixed2int_var(fy1),
832
14.5k
                                                         1,1,pdevc,dev,lop);
833
14.5k
                    if (c < 0) return c;
834
14.5k
                }
835
65.0k
                deltay = int2fixed(fixed2int_var(fy0)) + fixed_half -fy0;
836
65.0k
                deltax = int2fixed(fixed2int_var(fx0)) + fixed_half -fx0;
837
838
65.0k
                if (deltax < 0) deltax=-deltax;
839
65.0k
                if ((deltay < 0) && (deltay >= -fixed_half) &&
840
65.0k
                    (-deltay+deltax <= fixed_half))
841
18.9k
                {
842
18.9k
                    int c = gx_fill_rectangle_device_rop(fixed2int_var(fx0),
843
18.9k
                                                         fixed2int_var(fy0),
844
18.9k
                                                         1,1,pdevc,dev,lop);
845
18.9k
                    if (c < 0) return c;
846
18.9k
                }
847
65.0k
            }
848
            /* Can we treat it as a vertical rectangle? */
849
70.8k
            ix   = fixed2int_var(fx0-epsilon);
850
70.8k
            itox = fixed2int_var(fx1-epsilon);
851
70.8k
            if (itox == ix) {
852
                /* Figure out the start/height, allowing for our "covers
853
                 * centre of pixel" rule. */
854
30.0k
                iy   = fixed2int_var(fy0+fixed_half-fixed_epsilon);
855
30.0k
                itoy = fixed2int_var(fy1+fixed_half-fixed_epsilon);
856
30.0k
                itoy = itoy - iy;
857
30.0k
                if (itoy <= 0) {
858
                    /* Zero height; drawing this as a trapezoid wouldn't
859
                     * fill any pixels, so just exit. */
860
11.3k
                    return 0;
861
11.3k
                }
862
18.6k
                return gx_fill_rectangle_device_rop(ix, iy, 1, itoy,
863
30.0k
                                                    pdevc, dev, lop);
864
30.0k
            }
865
40.7k
            left.start.x = fx0 - fixed_half + fixed_epsilon - epsilon;
866
40.7k
            right.start.x = left.start.x + fixed_1;
867
40.7k
            left.end.x = fx1 - fixed_half + fixed_epsilon - epsilon;
868
40.7k
            right.end.x = left.end.x + fixed_1;
869
40.7k
            left.start.y = right.start.y = fy0;
870
40.7k
            left.end.y = right.end.y = fy1;
871
40.7k
            swap_axes = false;
872
296k
        } else {
873
            /* A "mostly-horizontal" line */
874
296k
            if (w < 0)
875
88.8k
                SWAP(fx0, fx1, tf), SWAP(fy0, fy1, tf),
876
88.8k
                    w = -w;
877
            /* So we are plotting a trapezoid with vertical thin edges
878
             * Check for whether a triangular extension area on the end
879
             * covers an additional pixel centre. */
880
296k
            if (h == 0 && adjustx) {
881
243k
                int deltax;
882
243k
                deltax = int2fixed(fixed2int_var(fx1)) + fixed_half -fx1;
883
884
243k
                if ((deltax > 0) && (deltax <= fixed_half))
885
121k
                {
886
121k
                    int c = gx_fill_rectangle_device_rop(fixed2int_var(fx1),
887
121k
                                                         fixed2int_var(fy1),
888
121k
                                                         1,1,pdevc,dev,lop);
889
121k
                    if (c < 0) return c;
890
121k
                }
891
243k
                deltax = int2fixed(fixed2int_var(fx0)) + fixed_half -fx0;
892
893
243k
                if ((deltax < 0) && (deltax >= -fixed_half))
894
120k
                {
895
120k
                    int c = gx_fill_rectangle_device_rop(fixed2int_var(fx0),
896
120k
                                                         fixed2int_var(fy0),
897
120k
                                                         1,1,pdevc,dev,lop);
898
120k
                    if (c < 0) return c;
899
120k
                }
900
243k
            } else if (h != 0) {
901
52.8k
                int deltax, deltay;
902
52.8k
                deltax = int2fixed(fixed2int_var(fx1)) + fixed_half -fx1;
903
52.8k
                deltay = int2fixed(fixed2int_var(fy1)) + fixed_half -fy1;
904
905
52.8k
                if (deltay < 0) deltay=-deltay;
906
52.8k
                if ((deltax > 0) && (deltax <= fixed_half) &&
907
52.8k
                    (deltax+deltay <= fixed_half))
908
13.1k
                {
909
13.1k
                    int c = gx_fill_rectangle_device_rop(fixed2int_var(fx1),
910
13.1k
                                                         fixed2int_var(fy1),
911
13.1k
                                                         1,1,pdevc,dev,lop);
912
13.1k
                    if (c < 0) return c;
913
13.1k
                }
914
52.8k
                deltax = int2fixed(fixed2int_var(fx0)) + fixed_half -fx0;
915
52.8k
                deltay = int2fixed(fixed2int_var(fy0)) + fixed_half -fy0;
916
917
52.8k
                if (deltay < 0) deltay=-deltay;
918
52.8k
                if ((deltax < 0) && (deltax >= -fixed_half) &&
919
52.8k
                    (-deltax+deltay <= fixed_half))
920
12.2k
                {
921
12.2k
                    int c = gx_fill_rectangle_device_rop(fixed2int_var(fx0),
922
12.2k
                                                         fixed2int_var(fy0),
923
12.2k
                                                         1,1,pdevc,dev,lop);
924
12.2k
                    if (c < 0) return c;
925
12.2k
                }
926
52.8k
            }
927
            /* Can we treat this as a horizontal rectangle? */
928
296k
            iy   = fixed2int_var(fy0 - epsilon);
929
296k
            itoy = fixed2int_var(fy1 - epsilon);
930
296k
            if (itoy == iy) {
931
                /* Figure out the start/width, allowing for our "covers
932
                * centre of pixel" rule. */
933
282k
                ix   = fixed2int_var(fx0+fixed_half-fixed_epsilon);
934
282k
                itox = fixed2int_var(fx1+fixed_half-fixed_epsilon);
935
282k
                itox = itox - ix;
936
282k
                if (itox <= 0) {
937
                    /* Zero width; drawing this as a trapezoid wouldn't
938
                     * fill any pixels, so just exit. */
939
59.6k
                    return 0;
940
59.6k
                }
941
222k
                return gx_fill_rectangle_device_rop(ix, iy, itox, 1,
942
282k
                                                    pdevc, dev, lop);
943
282k
            }
944
14.5k
            left.start.x = fy0 - fixed_half + fixed_epsilon - epsilon;
945
14.5k
            right.start.x = left.start.x + fixed_1;
946
14.5k
            left.end.x = fy1 - fixed_half + fixed_epsilon - epsilon;
947
14.5k
            right.end.x = left.end.x + fixed_1;
948
14.5k
            left.start.y = right.start.y = fx0;
949
14.5k
            left.end.y = right.end.y = fx1;
950
14.5k
            swap_axes = true;
951
14.5k
        }
952
55.3k
        return (*dev_proc(dev, fill_trapezoid)) (dev, &left, &right,
953
55.3k
                                                 left.start.y, left.end.y,
954
55.3k
                                                 swap_axes, pdevc, lop);
955
367k
    }
956
367k
}
957
958
/* ---------------- Image drawing ---------------- */
959
960
/* GC structures for image enumerator */
961
public_st_gx_image_enum_common();
962
963
static
964
0
ENUM_PTRS_WITH(image_enum_common_enum_ptrs, gx_image_enum_common_t *eptr)
965
0
    return 0;
966
0
case 0: return ENUM_OBJ(gx_device_enum_ptr(eptr->dev));
967
0
ENUM_PTRS_END
968
969
0
static RELOC_PTRS_WITH(image_enum_common_reloc_ptrs, gx_image_enum_common_t *eptr)
970
0
{
971
0
    eptr->dev = gx_device_reloc_ptr(eptr->dev, gcst);
972
0
}
973
0
RELOC_PTRS_END
974
975
int
976
gx_default_begin_typed_image(gx_device * dev,
977
                        const gs_gstate * pgs, const gs_matrix * pmat,
978
                   const gs_image_common_t * pic, const gs_int_rect * prect,
979
              const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
980
                      gs_memory_t * memory, gx_image_enum_common_t ** pinfo)
981
50.0k
{
982
50.0k
    return (*pic->type->begin_typed_image)
983
50.0k
        (dev, pgs, pmat, pic, prect, pdcolor, pcpath, memory, pinfo);
984
50.0k
}
985
986
int
987
gx_default_fillpage(gx_device *dev, gs_gstate * pgs, gx_device_color *pdevc)
988
56.5k
{
989
56.5k
    bool hl_color_available = gx_hld_is_hl_color_available(pgs, pdevc);
990
56.5k
    int code = 0;
991
992
    /* Fill the page directly, ignoring clipping. */
993
    /* Use the default RasterOp. */
994
56.5k
    if (hl_color_available) {
995
0
        gs_fixed_rect rect;
996
997
0
        rect.p.x = rect.p.y = 0;
998
0
        rect.q.x = int2fixed(dev->width);
999
0
        rect.q.y = int2fixed(dev->height);
1000
0
        code = dev_proc(dev, fill_rectangle_hl_color)(dev,
1001
0
                &rect, (const gs_gstate *)pgs, pdevc, NULL);
1002
0
    }
1003
56.5k
    if (!hl_color_available || code == gs_error_rangecheck)
1004
56.5k
        code = gx_fill_rectangle_device_rop(0, 0, dev->width, dev->height, pdevc, dev, lop_default);
1005
56.5k
    return code;
1006
56.5k
}