Coverage Report

Created: 2025-06-10 06:59

/src/ghostpdl/base/gxdtfill.h
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 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
17
/* Configurable algorithm for filling a trapezoid */
18
19
/*
20
 * Since we need several statically defined variants of this agorithm,
21
 * we store it in .h file and include several times into gdevddrw.c and
22
 * into gxfill.h . Configuration flags (macros) are :
23
 *
24
 *   GX_FILL_TRAPEZOID - a name of method
25
 *   CONTIGUOUS_FILL   - prevent dropouts in narrow trapezoids
26
 *   SWAP_AXES         - assume swapped axes
27
 *   FILL_DIRECT       - See LOOP_FILL_RECTANGLE_DIRECT.
28
 *   LINEAR_COLOR      - Fill with a linear color.
29
 *   EDGE_TYPE         - a type of edge structure.
30
 *   FILL_ATTRS        - operation attributes.
31
 */
32
33
/*
34
 * Fill a trapezoid.  left.start => left.end and right.start => right.end
35
 * define the sides; ybot and ytop define the top and bottom.  Requires:
36
 *      {left,right}->start.y <= ybot <= ytop <= {left,right}->end.y.
37
 * Lines where left.x >= right.x will not be drawn.  Thanks to Paul Haeberli
38
 * for an early floating point version of this algorithm.
39
 */
40
41
/*
42
 * With CONTIGUOUS_FILL is off,
43
 * this algorithm paints pixels, which centers fall between
44
 * the left and the right side of the trapezoid, excluding the
45
 * right side (see PLRM3, 7.5. Scan conversion details).
46
 * Particularly 0-width trapezoids are not painted.
47
 *
48
 * Similarly, it paints pixels, which centers
49
 * fall between ybot and ytop, excluding ytop.
50
 * Particularly 0-height trapezoids are not painted.
51
 *
52
 * With CONTIGUOUS_FILL is on, it paints a contigous area,
53
 * adding a minimal number of pixels outside the trapezoid.
54
 * Particularly it may paint pixels on the right and on the top sides,
55
 * if they are necessary for the contiguity.
56
 *
57
 * With LINEAR_COLOR returns 1 if the gradient arithmetics overflows..
58
 */
59
60
/*
61
We must paint pixels with index i such that
62
63
    Xl <= i + 0.5 < Xr
64
65
The condition is is equivalent to
66
67
    Xl - 0.5 <= i < Xr - 0.5
68
69
which is equivalent to
70
71
    (is_integer(Xl - 0.5) ? Xl - 0.5 : ceil(Xl - 0.5)) <= i <
72
    (is_integer(Xr - 0.5) ? Xr - 0.5 : floor(Xr - 0.5) + 1)
73
74
(the last '+1" happens due to the strong comparizon '<')
75
which is equivalent to
76
77
    ceil(Xl - 0.5) <= i < ceil(Xr - 0.5)
78
79
trap_line represents the intersection coordinate as a rational value :
80
81
    Xl = xl + e - fl
82
    Xr = xr + e - fr
83
84
Where 'e' is 'fixed_epsilon', 0.5 is 'fixed_half', and fl == l.fx / l.h, fr == - r.fx / r.h,
85
e <= fl < 0, e <= fr < 0.
86
Let
87
88
    xl' := xl + 0.5
89
    xr' := xr + 0.5
90
91
Then
92
93
    xl = xl' - 0.5
94
    xr = xr' - 0.5
95
96
    Xl = xl' - 0.5 + e - fl
97
    Xr = xr' - 0.5 + e - fr
98
99
    ceil(xl' - 0.5 + e - fl - 0.5) <= i < ceil(xr' - 0.5 + e - fr - 0.5)
100
101
which is equivalent to
102
103
    ceil(xl' + e - fl) - 1 <= i < ceil(xr' + e - fr) - 1
104
105
which is equivalent to
106
107
    (is_integer(xl' + e - fl) ? xl' + e - fl - 1 : ceil(xl' + e - fl) - 1) <= i <
108
    (is_integer(xr' + e - fr) ? xr' + e - fr - 1 : ceil(xr' + e - fr) - 1)
109
110
which is equivalent to
111
112
    (is_integer(xl' + e - fl) ? xl' + e - fl - 1 : floor(xl' + e - fl)) <= i <
113
    (is_integer(xr' + e - fr) ? xr' + e - fr - 1 : floor(xr' + e - fr))
114
115
which is equivalent to
116
117
    (is_integer(xl') && e == fl ? xl' - 1 : floor(xl' + e - fl)) <= i <
118
    (is_integer(xr') && e == fr ? xr' - 1 : floor(xr' + e - fr))
119
120
Note that e != fl ==> floor(xl' + e - fl) == floor(xl')  due to e - fl < LeastSignificantBit(xl') ;
121
          e == fl ==> floor(xl' + e - fl) == floor(xl')  due to e - fl == 0;
122
123
thus the condition is is equivalent to
124
125
    (is_integer(xl') && e == fl ? xl' - 1 : floor(xl')) <= i <
126
    (is_integer(xr') && e == fr ? xr' - 1 : floor(xr'))
127
128
It is computed with the macro 'rational_floor'.
129
130
*/
131
132
#if defined(GX_FILL_TRAPEZOID) && defined(EDGE_TYPE)
133
134
GX_FILL_TRAPEZOID (gx_device * dev, const EDGE_TYPE * left,
135
    const EDGE_TYPE * right, fixed ybot, fixed ytop, int flags,
136
    const gx_device_color * pdevc, FILL_ATTRS fa)
137
2.30M
{
138
2.30M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
2.30M
    const fixed ymax = fixed_pixround(ytop);
140
141
2.30M
    if (ymin >= ymax)
142
699k
        return 0;    /* no scan lines to sample */
143
1.60M
    {
144
1.60M
        int iy = fixed2int_var(ymin);
145
1.60M
        const int iy1 = fixed2int_var(ymax);
146
1.60M
        trap_line l, r;
147
1.60M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
1.60M
        const fixed
152
1.60M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
1.60M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
1.60M
        const fixed /* partial pixel offset to first line to sample */
155
1.60M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
1.60M
        fixed fxl;
157
1.60M
        int code;
158
# if CONTIGUOUS_FILL
159
            const bool peak0 = ((flags & 1) != 0);
160
            const bool peak1 = ((flags & 2) != 0);
161
0
            int peak_y0 = ybot + fixed_half;
162
0
            int peak_y1 = ytop - fixed_half;
163
# endif
164
# if LINEAR_COLOR
165
            int num_components = dev->color_info.num_components;
166
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
            trap_gradient lg, rg, xg;
176
# else
177
            gx_color_index cindex = pdevc->colors.pure;
178
399k
            dev_proc_fill_rectangle((*fill_rect)) =
179
399k
                dev_proc(dev, fill_rectangle);
180
# endif
181
182
1.60M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
1.60M
        l.h = left->end.y - left->start.y;
185
1.60M
        if (l.h == 0)
186
0
           return 0;
187
1.60M
        r.h = right->end.y - right->start.y;
188
1.60M
        if (r.h == 0)
189
0
           return 0;
190
1.60M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
1.60M
        r.x = x0r + (fixed_half - fixed_epsilon);
192
#if !LINEAR_COLOR
193
        ry = iy;
194
#endif
195
196
/*
197
 * Free variables of FILL_TRAP_RECT:
198
 *  SWAP_AXES, pdevc, dev, fa
199
 * Free variables of FILL_TRAP_RECT_DIRECT:
200
 *  SWAP_AXES, fill_rect, dev, cindex
201
 */
202
1.60M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
1.60M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
41.2k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
1.60M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
1.60M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
1.25M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
5.38M
        (!(w) ? 0 : dev_proc(dev, fill_linear_color_scanline)(dev, fa, x, y, w, xg.c, xg.f, xg.num, xg.den))
212
#else
213
#   define FILL_TRAP_RECT(x,y,w,h)\
214
1.29M
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
#endif
216
217
        /* Compute the dx/dy ratios. */
218
219
        /*
220
         * Compute the x offsets at the first scan line to sample.  We need
221
         * to be careful in computing ys# * dx#f {/,%} h# because the
222
         * multiplication may overflow.  We know that all the quantities
223
         * involved are non-negative, and that ys# is usually less than 1 (as
224
         * a fixed, of course); this gives us a cheap conservative check for
225
         * overflow in the multiplication.
226
         */
227
1.60M
#define YMULT_QUO(ys, tl)\
228
2.22M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
2.22M
   fixed_mult_quo(ys, tl.df, tl.h))
230
231
#if CONTIGUOUS_FILL
232
/*
233
 * If left and right boundary round to same pixel index,
234
 * we would not paing the scan and would get a dropout.
235
 * Check for this case and choose one of two pixels
236
 * which is closer to the "axis". We need to exclude
237
 * 'peak' because it would paint an excessive pixel.
238
 */
239
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) \
240
0
    if (ixl == ixr) \
241
0
        if ((!peak0 || iy >= peak_y0) && (!peak1 || iy <= peak_y1)) {\
242
0
            fixed x = int2fixed(ixl) + fixed_half;\
243
0
            if (x - l.x < r.x - x)\
244
0
                ++ixr;\
245
0
            else\
246
0
                --ixl;\
247
0
        }
248
249
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill)\
250
0
    if (adj1 < adj2) {\
251
0
        if (iy - ry > 1) {\
252
0
            code = fill(rxl, ry, rxr - rxl, iy - ry - 1);\
253
0
            if (code < 0)\
254
0
                goto xit;\
255
0
            ry = iy - 1;\
256
0
        }\
257
0
        adj1 = adj2 = (adj2 + adj2) / 2;\
258
0
    }
259
260
#else
261
9.56M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
1.78M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
#endif
264
1.60M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
424k
            l.di = 0, l.df = 0;
267
424k
            fxl = 0;
268
1.18M
        } else {
269
1.18M
            compute_dx(&l, dxl, ysl);
270
1.18M
            fxl = YMULT_QUO(ysl, l);
271
1.18M
            l.x += fxl;
272
1.18M
        }
273
1.60M
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
#     if !LINEAR_COLOR
277
210k
                if (l.di == 0 && l.df == 0) {
278
192k
                    rxl = fixed2int_var(l.x);
279
192k
                    rxr = fixed2int_var(r.x);
280
192k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
192k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
192k
                    goto xit;
283
192k
                }
284
17.5k
#     endif
285
17.5k
            r.di = 0, r.df = 0;
286
17.5k
        }
287
        /*
288
         * The test for fxl != 0 is required because the right edge might
289
         * cross some pixel centers even if the left edge doesn't.
290
         */
291
1.18M
        else if (dxr == dxl && fxl != 0) {
292
218k
            if (l.di == 0)
293
32.9k
                r.di = 0, r.df = l.df;
294
185k
            else
295
185k
                compute_dx(&r, dxr, ysr);
296
218k
            if (ysr == ysl && r.h == l.h)
297
134k
                r.x += fxl;
298
83.4k
            else
299
83.4k
                r.x += YMULT_QUO(ysr, r);
300
963k
        } else {
301
963k
            compute_dx(&r, dxr, ysr);
302
963k
            r.x += YMULT_QUO(ysr, r);
303
963k
        }
304
        /* Compute one line's worth of dx/dy. */
305
207k
        compute_ldx(&l, ysl);
306
207k
        compute_ldx(&r, ysr);
307
        /* We subtracted fixed_epsilon from l.x, r.x to simplify rounding
308
           when the rational part is zero. Now add it back to get xl', xr' */
309
1.41M
        l.x += fixed_epsilon;
310
1.41M
        r.x += fixed_epsilon;
311
# if LINEAR_COLOR
312
#     ifdef DEBUG
313
                if (check_gradient_overflow(left, right)) {
314
                    /* The caller must care of.
315
                       Checking it here looses some performance with triangles. */
316
                    return_error(gs_error_unregistered);
317
                }
318
#     endif
319
            lg.c = lgc;
320
            lg.f = lgf;
321
            lg.num = lgnum;
322
            rg.c = rgc;
323
            rg.f = rgf;
324
            rg.num = rgnum;
325
            xg.c = xgc;
326
            xg.f = xgf;
327
            xg.num = xgnum;
328
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
1.20M
            if (code < 0)
330
0
                return code;
331
1.20M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
1.20M
            if (code < 0)
333
0
                return code;
334
335
1.20M
# endif
336
337
1.20M
#define rational_floor(tl)\
338
37.9M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
1.20M
#define STEP_LINE(ix, tl)\
340
33.9M
  tl.x += tl.ldi;\
341
33.9M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
35.1M
  ix = rational_floor(tl)
343
344
1.41M
        rxl = rational_floor(l);
345
1.41M
        rxr = rational_floor(r);
346
1.41M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
18.3M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
10.2M
                if (rxl != rxr) {
350
5.38M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
5.38M
                    if (code < 0)
352
0
                        goto xit;
353
5.38M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
5.38M
                    if (code < 0)
355
0
                        goto xit;
356
5.38M
                }
357
10.2M
                if (++iy == iy1)
358
1.20M
                    break;
359
8.99M
                STEP_LINE(rxl, l);
360
8.99M
                STEP_LINE(rxr, r);
361
8.99M
                step_gradient(&lg, num_components);
362
8.99M
                step_gradient(&rg, num_components);
363
#     else
364
                register int ixl, ixr;
365
366
7.96M
                STEP_LINE(ixl, l);
367
7.96M
                STEP_LINE(ixr, r);
368
7.96M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
7.96M
                if (ixl != rxl || ixr != rxr) {
370
894k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
894k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
894k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
894k
                    if (code < 0)
374
0
                        goto xit;
375
894k
                    rxl = ixl, rxr = ixr, ry = iy;
376
894k
                }
377
#     endif
378
8.99M
        }
379
# if !LINEAR_COLOR
380
207k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
1.20M
            code = 0;
383
1.20M
# endif
384
1.20M
#undef STEP_LINE
385
1.20M
#undef SET_MINIMAL_WIDTH
386
1.20M
#undef CONNECT_RECTANGLES
387
1.20M
#undef FILL_TRAP_RECT
388
1.20M
#undef FILL_TRAP_RECT_DIRECT
389
1.20M
#undef FILL_TRAP_RECT_INRECT
390
1.20M
#undef YMULT_QUO
391
1.60M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
1.60M
        return_if_interrupt(dev->memory);
394
1.60M
        return code;
395
1.60M
    }
396
1.60M
}
Unexecuted instantiation: gx_fill_trapezoid_cf_fd
Unexecuted instantiation: gx_fill_trapezoid_cf_nd
gdevddrw.c:gx_fill_trapezoid_as_fd
Line
Count
Source
137
18.3k
{
138
18.3k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
18.3k
    const fixed ymax = fixed_pixround(ytop);
140
141
18.3k
    if (ymin >= ymax)
142
1.29k
        return 0;    /* no scan lines to sample */
143
17.0k
    {
144
17.0k
        int iy = fixed2int_var(ymin);
145
17.0k
        const int iy1 = fixed2int_var(ymax);
146
17.0k
        trap_line l, r;
147
17.0k
        register int rxl, rxr;
148
17.0k
#if !LINEAR_COLOR
149
17.0k
        int ry;
150
17.0k
#endif
151
17.0k
        const fixed
152
17.0k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
17.0k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
17.0k
        const fixed /* partial pixel offset to first line to sample */
155
17.0k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
17.0k
        fixed fxl;
157
17.0k
        int code;
158
# if CONTIGUOUS_FILL
159
            const bool peak0 = ((flags & 1) != 0);
160
            const bool peak1 = ((flags & 2) != 0);
161
            int peak_y0 = ybot + fixed_half;
162
            int peak_y1 = ytop - fixed_half;
163
# endif
164
# if LINEAR_COLOR
165
            int num_components = dev->color_info.num_components;
166
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
            trap_gradient lg, rg, xg;
176
# else
177
17.0k
            gx_color_index cindex = pdevc->colors.pure;
178
17.0k
            dev_proc_fill_rectangle((*fill_rect)) =
179
17.0k
                dev_proc(dev, fill_rectangle);
180
17.0k
# endif
181
182
17.0k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
17.0k
        l.h = left->end.y - left->start.y;
185
17.0k
        if (l.h == 0)
186
0
           return 0;
187
17.0k
        r.h = right->end.y - right->start.y;
188
17.0k
        if (r.h == 0)
189
0
           return 0;
190
17.0k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
17.0k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
17.0k
#if !LINEAR_COLOR
193
17.0k
        ry = iy;
194
17.0k
#endif
195
196
/*
197
 * Free variables of FILL_TRAP_RECT:
198
 *  SWAP_AXES, pdevc, dev, fa
199
 * Free variables of FILL_TRAP_RECT_DIRECT:
200
 *  SWAP_AXES, fill_rect, dev, cindex
201
 */
202
17.0k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
17.0k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
17.0k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
17.0k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
17.0k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
17.0k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
        (!(w) ? 0 : dev_proc(dev, fill_linear_color_scanline)(dev, fa, x, y, w, xg.c, xg.f, xg.num, xg.den))
212
#else
213
17.0k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
17.0k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
17.0k
#endif
216
217
        /* Compute the dx/dy ratios. */
218
219
        /*
220
         * Compute the x offsets at the first scan line to sample.  We need
221
         * to be careful in computing ys# * dx#f {/,%} h# because the
222
         * multiplication may overflow.  We know that all the quantities
223
         * involved are non-negative, and that ys# is usually less than 1 (as
224
         * a fixed, of course); this gives us a cheap conservative check for
225
         * overflow in the multiplication.
226
         */
227
17.0k
#define YMULT_QUO(ys, tl)\
228
17.0k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
17.0k
   fixed_mult_quo(ys, tl.df, tl.h))
230
231
#if CONTIGUOUS_FILL
232
/*
233
 * If left and right boundary round to same pixel index,
234
 * we would not paing the scan and would get a dropout.
235
 * Check for this case and choose one of two pixels
236
 * which is closer to the "axis". We need to exclude
237
 * 'peak' because it would paint an excessive pixel.
238
 */
239
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) \
240
    if (ixl == ixr) \
241
        if ((!peak0 || iy >= peak_y0) && (!peak1 || iy <= peak_y1)) {\
242
            fixed x = int2fixed(ixl) + fixed_half;\
243
            if (x - l.x < r.x - x)\
244
                ++ixr;\
245
            else\
246
                --ixl;\
247
        }
248
249
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill)\
250
    if (adj1 < adj2) {\
251
        if (iy - ry > 1) {\
252
            code = fill(rxl, ry, rxr - rxl, iy - ry - 1);\
253
            if (code < 0)\
254
                goto xit;\
255
            ry = iy - 1;\
256
        }\
257
        adj1 = adj2 = (adj2 + adj2) / 2;\
258
    }
259
260
#else
261
17.0k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
17.0k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
17.0k
#endif
264
17.0k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
11.7k
            l.di = 0, l.df = 0;
267
11.7k
            fxl = 0;
268
11.7k
        } else {
269
5.24k
            compute_dx(&l, dxl, ysl);
270
5.24k
            fxl = YMULT_QUO(ysl, l);
271
5.24k
            l.x += fxl;
272
5.24k
        }
273
17.0k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
11.8k
#     if !LINEAR_COLOR
277
11.8k
                if (l.di == 0 && l.df == 0) {
278
11.0k
                    rxl = fixed2int_var(l.x);
279
11.0k
                    rxr = fixed2int_var(r.x);
280
11.0k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
11.0k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
11.0k
                    goto xit;
283
11.0k
                }
284
801
#     endif
285
801
            r.di = 0, r.df = 0;
286
801
        }
287
        /*
288
         * The test for fxl != 0 is required because the right edge might
289
         * cross some pixel centers even if the left edge doesn't.
290
         */
291
5.19k
        else if (dxr == dxl && fxl != 0) {
292
1.62k
            if (l.di == 0)
293
469
                r.di = 0, r.df = l.df;
294
1.15k
            else
295
1.15k
                compute_dx(&r, dxr, ysr);
296
1.62k
            if (ysr == ysl && r.h == l.h)
297
1.61k
                r.x += fxl;
298
16
            else
299
16
                r.x += YMULT_QUO(ysr, r);
300
3.56k
        } else {
301
3.56k
            compute_dx(&r, dxr, ysr);
302
3.56k
            r.x += YMULT_QUO(ysr, r);
303
3.56k
        }
304
        /* Compute one line's worth of dx/dy. */
305
5.99k
        compute_ldx(&l, ysl);
306
5.99k
        compute_ldx(&r, ysr);
307
        /* We subtracted fixed_epsilon from l.x, r.x to simplify rounding
308
           when the rational part is zero. Now add it back to get xl', xr' */
309
5.99k
        l.x += fixed_epsilon;
310
5.99k
        r.x += fixed_epsilon;
311
# if LINEAR_COLOR
312
#     ifdef DEBUG
313
                if (check_gradient_overflow(left, right)) {
314
                    /* The caller must care of.
315
                       Checking it here looses some performance with triangles. */
316
                    return_error(gs_error_unregistered);
317
                }
318
#     endif
319
            lg.c = lgc;
320
            lg.f = lgf;
321
            lg.num = lgnum;
322
            rg.c = rgc;
323
            rg.f = rgf;
324
            rg.num = rgnum;
325
            xg.c = xgc;
326
            xg.f = xgf;
327
            xg.num = xgnum;
328
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
            if (code < 0)
330
                return code;
331
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
            if (code < 0)
333
                return code;
334
335
# endif
336
337
5.99k
#define rational_floor(tl)\
338
5.99k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
5.99k
#define STEP_LINE(ix, tl)\
340
5.99k
  tl.x += tl.ldi;\
341
5.99k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
5.99k
  ix = rational_floor(tl)
343
344
5.99k
        rxl = rational_floor(l);
345
5.99k
        rxr = rational_floor(r);
346
5.99k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
224k
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
                if (rxl != rxr) {
350
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
                    if (code < 0)
352
                        goto xit;
353
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
                    if (code < 0)
355
                        goto xit;
356
                }
357
                if (++iy == iy1)
358
                    break;
359
                STEP_LINE(rxl, l);
360
                STEP_LINE(rxr, r);
361
                step_gradient(&lg, num_components);
362
                step_gradient(&rg, num_components);
363
#     else
364
218k
                register int ixl, ixr;
365
366
218k
                STEP_LINE(ixl, l);
367
218k
                STEP_LINE(ixr, r);
368
218k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
218k
                if (ixl != rxl || ixr != rxr) {
370
66.7k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
66.7k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
66.7k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
66.7k
                    if (code < 0)
374
0
                        goto xit;
375
66.7k
                    rxl = ixl, rxr = ixr, ry = iy;
376
66.7k
                }
377
218k
#     endif
378
218k
        }
379
5.99k
# if !LINEAR_COLOR
380
5.99k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
5.99k
#undef STEP_LINE
385
5.99k
#undef SET_MINIMAL_WIDTH
386
5.99k
#undef CONNECT_RECTANGLES
387
5.99k
#undef FILL_TRAP_RECT
388
5.99k
#undef FILL_TRAP_RECT_DIRECT
389
5.99k
#undef FILL_TRAP_RECT_INRECT
390
5.99k
#undef YMULT_QUO
391
17.0k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
17.0k
        return_if_interrupt(dev->memory);
394
17.0k
        return code;
395
17.0k
    }
396
17.0k
}
Unexecuted instantiation: gdevddrw.c:gx_fill_trapezoid_as_nd
gdevddrw.c:gx_fill_trapezoid_ns_fd
Line
Count
Source
137
383k
{
138
383k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
383k
    const fixed ymax = fixed_pixround(ytop);
140
141
383k
    if (ymin >= ymax)
142
20.3k
        return 0;    /* no scan lines to sample */
143
362k
    {
144
362k
        int iy = fixed2int_var(ymin);
145
362k
        const int iy1 = fixed2int_var(ymax);
146
362k
        trap_line l, r;
147
362k
        register int rxl, rxr;
148
362k
#if !LINEAR_COLOR
149
362k
        int ry;
150
362k
#endif
151
362k
        const fixed
152
362k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
362k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
362k
        const fixed /* partial pixel offset to first line to sample */
155
362k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
362k
        fixed fxl;
157
362k
        int code;
158
# if CONTIGUOUS_FILL
159
            const bool peak0 = ((flags & 1) != 0);
160
            const bool peak1 = ((flags & 2) != 0);
161
            int peak_y0 = ybot + fixed_half;
162
            int peak_y1 = ytop - fixed_half;
163
# endif
164
# if LINEAR_COLOR
165
            int num_components = dev->color_info.num_components;
166
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
            trap_gradient lg, rg, xg;
176
# else
177
362k
            gx_color_index cindex = pdevc->colors.pure;
178
362k
            dev_proc_fill_rectangle((*fill_rect)) =
179
362k
                dev_proc(dev, fill_rectangle);
180
362k
# endif
181
182
362k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
362k
        l.h = left->end.y - left->start.y;
185
362k
        if (l.h == 0)
186
0
           return 0;
187
362k
        r.h = right->end.y - right->start.y;
188
362k
        if (r.h == 0)
189
0
           return 0;
190
362k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
362k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
362k
#if !LINEAR_COLOR
193
362k
        ry = iy;
194
362k
#endif
195
196
/*
197
 * Free variables of FILL_TRAP_RECT:
198
 *  SWAP_AXES, pdevc, dev, fa
199
 * Free variables of FILL_TRAP_RECT_DIRECT:
200
 *  SWAP_AXES, fill_rect, dev, cindex
201
 */
202
362k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
362k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
362k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
362k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
362k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
362k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
        (!(w) ? 0 : dev_proc(dev, fill_linear_color_scanline)(dev, fa, x, y, w, xg.c, xg.f, xg.num, xg.den))
212
#else
213
362k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
362k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
362k
#endif
216
217
        /* Compute the dx/dy ratios. */
218
219
        /*
220
         * Compute the x offsets at the first scan line to sample.  We need
221
         * to be careful in computing ys# * dx#f {/,%} h# because the
222
         * multiplication may overflow.  We know that all the quantities
223
         * involved are non-negative, and that ys# is usually less than 1 (as
224
         * a fixed, of course); this gives us a cheap conservative check for
225
         * overflow in the multiplication.
226
         */
227
362k
#define YMULT_QUO(ys, tl)\
228
362k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
362k
   fixed_mult_quo(ys, tl.df, tl.h))
230
231
#if CONTIGUOUS_FILL
232
/*
233
 * If left and right boundary round to same pixel index,
234
 * we would not paing the scan and would get a dropout.
235
 * Check for this case and choose one of two pixels
236
 * which is closer to the "axis". We need to exclude
237
 * 'peak' because it would paint an excessive pixel.
238
 */
239
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) \
240
    if (ixl == ixr) \
241
        if ((!peak0 || iy >= peak_y0) && (!peak1 || iy <= peak_y1)) {\
242
            fixed x = int2fixed(ixl) + fixed_half;\
243
            if (x - l.x < r.x - x)\
244
                ++ixr;\
245
            else\
246
                --ixl;\
247
        }
248
249
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill)\
250
    if (adj1 < adj2) {\
251
        if (iy - ry > 1) {\
252
            code = fill(rxl, ry, rxr - rxl, iy - ry - 1);\
253
            if (code < 0)\
254
                goto xit;\
255
            ry = iy - 1;\
256
        }\
257
        adj1 = adj2 = (adj2 + adj2) / 2;\
258
    }
259
260
#else
261
362k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
362k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
362k
#endif
264
362k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
183k
            l.di = 0, l.df = 0;
267
183k
            fxl = 0;
268
183k
        } else {
269
179k
            compute_dx(&l, dxl, ysl);
270
179k
            fxl = YMULT_QUO(ysl, l);
271
179k
            l.x += fxl;
272
179k
        }
273
362k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
182k
#     if !LINEAR_COLOR
277
182k
                if (l.di == 0 && l.df == 0) {
278
165k
                    rxl = fixed2int_var(l.x);
279
165k
                    rxr = fixed2int_var(r.x);
280
165k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
165k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
165k
                    goto xit;
283
165k
                }
284
16.3k
#     endif
285
16.3k
            r.di = 0, r.df = 0;
286
16.3k
        }
287
        /*
288
         * The test for fxl != 0 is required because the right edge might
289
         * cross some pixel centers even if the left edge doesn't.
290
         */
291
180k
        else if (dxr == dxl && fxl != 0) {
292
112k
            if (l.di == 0)
293
2.93k
                r.di = 0, r.df = l.df;
294
109k
            else
295
109k
                compute_dx(&r, dxr, ysr);
296
112k
            if (ysr == ysl && r.h == l.h)
297
52.6k
                r.x += fxl;
298
60.1k
            else
299
60.1k
                r.x += YMULT_QUO(ysr, r);
300
112k
        } else {
301
67.8k
            compute_dx(&r, dxr, ysr);
302
67.8k
            r.x += YMULT_QUO(ysr, r);
303
67.8k
        }
304
        /* Compute one line's worth of dx/dy. */
305
197k
        compute_ldx(&l, ysl);
306
197k
        compute_ldx(&r, ysr);
307
        /* We subtracted fixed_epsilon from l.x, r.x to simplify rounding
308
           when the rational part is zero. Now add it back to get xl', xr' */
309
197k
        l.x += fixed_epsilon;
310
197k
        r.x += fixed_epsilon;
311
# if LINEAR_COLOR
312
#     ifdef DEBUG
313
                if (check_gradient_overflow(left, right)) {
314
                    /* The caller must care of.
315
                       Checking it here looses some performance with triangles. */
316
                    return_error(gs_error_unregistered);
317
                }
318
#     endif
319
            lg.c = lgc;
320
            lg.f = lgf;
321
            lg.num = lgnum;
322
            rg.c = rgc;
323
            rg.f = rgf;
324
            rg.num = rgnum;
325
            xg.c = xgc;
326
            xg.f = xgf;
327
            xg.num = xgnum;
328
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
            if (code < 0)
330
                return code;
331
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
            if (code < 0)
333
                return code;
334
335
# endif
336
337
197k
#define rational_floor(tl)\
338
197k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
197k
#define STEP_LINE(ix, tl)\
340
197k
  tl.x += tl.ldi;\
341
197k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
197k
  ix = rational_floor(tl)
343
344
197k
        rxl = rational_floor(l);
345
197k
        rxr = rational_floor(r);
346
197k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
7.90M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
                if (rxl != rxr) {
350
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
                    if (code < 0)
352
                        goto xit;
353
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
                    if (code < 0)
355
                        goto xit;
356
                }
357
                if (++iy == iy1)
358
                    break;
359
                STEP_LINE(rxl, l);
360
                STEP_LINE(rxr, r);
361
                step_gradient(&lg, num_components);
362
                step_gradient(&rg, num_components);
363
#     else
364
7.71M
                register int ixl, ixr;
365
366
7.71M
                STEP_LINE(ixl, l);
367
7.71M
                STEP_LINE(ixr, r);
368
7.71M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
7.71M
                if (ixl != rxl || ixr != rxr) {
370
806k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
806k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
806k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
806k
                    if (code < 0)
374
0
                        goto xit;
375
806k
                    rxl = ixl, rxr = ixr, ry = iy;
376
806k
                }
377
7.71M
#     endif
378
7.71M
        }
379
197k
# if !LINEAR_COLOR
380
197k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
197k
#undef STEP_LINE
385
197k
#undef SET_MINIMAL_WIDTH
386
197k
#undef CONNECT_RECTANGLES
387
197k
#undef FILL_TRAP_RECT
388
197k
#undef FILL_TRAP_RECT_DIRECT
389
197k
#undef FILL_TRAP_RECT_INRECT
390
197k
#undef YMULT_QUO
391
362k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
362k
        return_if_interrupt(dev->memory);
394
362k
        return code;
395
362k
    }
396
362k
}
gdevddrw.c:gx_fill_trapezoid_ns_nd
Line
Count
Source
137
20.1k
{
138
20.1k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
20.1k
    const fixed ymax = fixed_pixround(ytop);
140
141
20.1k
    if (ymin >= ymax)
142
0
        return 0;   /* no scan lines to sample */
143
20.1k
    {
144
20.1k
        int iy = fixed2int_var(ymin);
145
20.1k
        const int iy1 = fixed2int_var(ymax);
146
20.1k
        trap_line l, r;
147
20.1k
        register int rxl, rxr;
148
20.1k
#if !LINEAR_COLOR
149
20.1k
        int ry;
150
20.1k
#endif
151
20.1k
        const fixed
152
20.1k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
20.1k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
20.1k
        const fixed /* partial pixel offset to first line to sample */
155
20.1k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
20.1k
        fixed fxl;
157
20.1k
        int code;
158
# if CONTIGUOUS_FILL
159
            const bool peak0 = ((flags & 1) != 0);
160
            const bool peak1 = ((flags & 2) != 0);
161
            int peak_y0 = ybot + fixed_half;
162
            int peak_y1 = ytop - fixed_half;
163
# endif
164
# if LINEAR_COLOR
165
            int num_components = dev->color_info.num_components;
166
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
            trap_gradient lg, rg, xg;
176
# else
177
20.1k
            gx_color_index cindex = pdevc->colors.pure;
178
20.1k
            dev_proc_fill_rectangle((*fill_rect)) =
179
20.1k
                dev_proc(dev, fill_rectangle);
180
20.1k
# endif
181
182
20.1k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
20.1k
        l.h = left->end.y - left->start.y;
185
20.1k
        if (l.h == 0)
186
0
           return 0;
187
20.1k
        r.h = right->end.y - right->start.y;
188
20.1k
        if (r.h == 0)
189
0
           return 0;
190
20.1k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
20.1k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
20.1k
#if !LINEAR_COLOR
193
20.1k
        ry = iy;
194
20.1k
#endif
195
196
/*
197
 * Free variables of FILL_TRAP_RECT:
198
 *  SWAP_AXES, pdevc, dev, fa
199
 * Free variables of FILL_TRAP_RECT_DIRECT:
200
 *  SWAP_AXES, fill_rect, dev, cindex
201
 */
202
20.1k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
20.1k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
20.1k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
20.1k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
20.1k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
20.1k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
        (!(w) ? 0 : dev_proc(dev, fill_linear_color_scanline)(dev, fa, x, y, w, xg.c, xg.f, xg.num, xg.den))
212
#else
213
20.1k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
20.1k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
20.1k
#endif
216
217
        /* Compute the dx/dy ratios. */
218
219
        /*
220
         * Compute the x offsets at the first scan line to sample.  We need
221
         * to be careful in computing ys# * dx#f {/,%} h# because the
222
         * multiplication may overflow.  We know that all the quantities
223
         * involved are non-negative, and that ys# is usually less than 1 (as
224
         * a fixed, of course); this gives us a cheap conservative check for
225
         * overflow in the multiplication.
226
         */
227
20.1k
#define YMULT_QUO(ys, tl)\
228
20.1k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
20.1k
   fixed_mult_quo(ys, tl.df, tl.h))
230
231
#if CONTIGUOUS_FILL
232
/*
233
 * If left and right boundary round to same pixel index,
234
 * we would not paing the scan and would get a dropout.
235
 * Check for this case and choose one of two pixels
236
 * which is closer to the "axis". We need to exclude
237
 * 'peak' because it would paint an excessive pixel.
238
 */
239
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) \
240
    if (ixl == ixr) \
241
        if ((!peak0 || iy >= peak_y0) && (!peak1 || iy <= peak_y1)) {\
242
            fixed x = int2fixed(ixl) + fixed_half;\
243
            if (x - l.x < r.x - x)\
244
                ++ixr;\
245
            else\
246
                --ixl;\
247
        }
248
249
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill)\
250
    if (adj1 < adj2) {\
251
        if (iy - ry > 1) {\
252
            code = fill(rxl, ry, rxr - rxl, iy - ry - 1);\
253
            if (code < 0)\
254
                goto xit;\
255
            ry = iy - 1;\
256
        }\
257
        adj1 = adj2 = (adj2 + adj2) / 2;\
258
    }
259
260
#else
261
20.1k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
20.1k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
20.1k
#endif
264
20.1k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
16.5k
            l.di = 0, l.df = 0;
267
16.5k
            fxl = 0;
268
16.5k
        } else {
269
3.60k
            compute_dx(&l, dxl, ysl);
270
3.60k
            fxl = YMULT_QUO(ysl, l);
271
3.60k
            l.x += fxl;
272
3.60k
        }
273
20.1k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
16.3k
#     if !LINEAR_COLOR
277
16.3k
                if (l.di == 0 && l.df == 0) {
278
16.0k
                    rxl = fixed2int_var(l.x);
279
16.0k
                    rxr = fixed2int_var(r.x);
280
16.0k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
16.0k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
16.0k
                    goto xit;
283
16.0k
                }
284
340
#     endif
285
340
            r.di = 0, r.df = 0;
286
340
        }
287
        /*
288
         * The test for fxl != 0 is required because the right edge might
289
         * cross some pixel centers even if the left edge doesn't.
290
         */
291
3.75k
        else if (dxr == dxl && fxl != 0) {
292
14
            if (l.di == 0)
293
0
                r.di = 0, r.df = l.df;
294
14
            else
295
14
                compute_dx(&r, dxr, ysr);
296
14
            if (ysr == ysl && r.h == l.h)
297
0
                r.x += fxl;
298
14
            else
299
14
                r.x += YMULT_QUO(ysr, r);
300
3.73k
        } else {
301
3.73k
            compute_dx(&r, dxr, ysr);
302
3.73k
            r.x += YMULT_QUO(ysr, r);
303
3.73k
        }
304
        /* Compute one line's worth of dx/dy. */
305
4.09k
        compute_ldx(&l, ysl);
306
4.09k
        compute_ldx(&r, ysr);
307
        /* We subtracted fixed_epsilon from l.x, r.x to simplify rounding
308
           when the rational part is zero. Now add it back to get xl', xr' */
309
4.09k
        l.x += fixed_epsilon;
310
4.09k
        r.x += fixed_epsilon;
311
# if LINEAR_COLOR
312
#     ifdef DEBUG
313
                if (check_gradient_overflow(left, right)) {
314
                    /* The caller must care of.
315
                       Checking it here looses some performance with triangles. */
316
                    return_error(gs_error_unregistered);
317
                }
318
#     endif
319
            lg.c = lgc;
320
            lg.f = lgf;
321
            lg.num = lgnum;
322
            rg.c = rgc;
323
            rg.f = rgf;
324
            rg.num = rgnum;
325
            xg.c = xgc;
326
            xg.f = xgf;
327
            xg.num = xgnum;
328
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
            if (code < 0)
330
                return code;
331
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
            if (code < 0)
333
                return code;
334
335
# endif
336
337
4.09k
#define rational_floor(tl)\
338
4.09k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
4.09k
#define STEP_LINE(ix, tl)\
340
4.09k
  tl.x += tl.ldi;\
341
4.09k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
4.09k
  ix = rational_floor(tl)
343
344
4.09k
        rxl = rational_floor(l);
345
4.09k
        rxr = rational_floor(r);
346
4.09k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
35.1k
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
                if (rxl != rxr) {
350
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
                    if (code < 0)
352
                        goto xit;
353
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
                    if (code < 0)
355
                        goto xit;
356
                }
357
                if (++iy == iy1)
358
                    break;
359
                STEP_LINE(rxl, l);
360
                STEP_LINE(rxr, r);
361
                step_gradient(&lg, num_components);
362
                step_gradient(&rg, num_components);
363
#     else
364
31.0k
                register int ixl, ixr;
365
366
31.0k
                STEP_LINE(ixl, l);
367
31.0k
                STEP_LINE(ixr, r);
368
31.0k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
31.0k
                if (ixl != rxl || ixr != rxr) {
370
21.0k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
21.0k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
21.0k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
21.0k
                    if (code < 0)
374
0
                        goto xit;
375
21.0k
                    rxl = ixl, rxr = ixr, ry = iy;
376
21.0k
                }
377
31.0k
#     endif
378
31.0k
        }
379
4.09k
# if !LINEAR_COLOR
380
4.09k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
4.09k
#undef STEP_LINE
385
4.09k
#undef SET_MINIMAL_WIDTH
386
4.09k
#undef CONNECT_RECTANGLES
387
4.09k
#undef FILL_TRAP_RECT
388
4.09k
#undef FILL_TRAP_RECT_DIRECT
389
4.09k
#undef FILL_TRAP_RECT_INRECT
390
4.09k
#undef YMULT_QUO
391
20.1k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
20.1k
        return_if_interrupt(dev->memory);
394
20.1k
        return code;
395
20.1k
    }
396
20.1k
}
gdevddrw.c:gx_fill_trapezoid_as_lc
Line
Count
Source
137
100k
{
138
100k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
100k
    const fixed ymax = fixed_pixround(ytop);
140
141
100k
    if (ymin >= ymax)
142
1.06k
        return 0;    /* no scan lines to sample */
143
98.9k
    {
144
98.9k
        int iy = fixed2int_var(ymin);
145
98.9k
        const int iy1 = fixed2int_var(ymax);
146
98.9k
        trap_line l, r;
147
98.9k
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
98.9k
        const fixed
152
98.9k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
98.9k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
98.9k
        const fixed /* partial pixel offset to first line to sample */
155
98.9k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
98.9k
        fixed fxl;
157
98.9k
        int code;
158
# if CONTIGUOUS_FILL
159
            const bool peak0 = ((flags & 1) != 0);
160
            const bool peak1 = ((flags & 2) != 0);
161
            int peak_y0 = ybot + fixed_half;
162
            int peak_y1 = ytop - fixed_half;
163
# endif
164
98.9k
# if LINEAR_COLOR
165
98.9k
            int num_components = dev->color_info.num_components;
166
98.9k
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
98.9k
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
98.9k
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
98.9k
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
98.9k
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
98.9k
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
98.9k
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
98.9k
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
98.9k
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
98.9k
            trap_gradient lg, rg, xg;
176
# else
177
            gx_color_index cindex = pdevc->colors.pure;
178
            dev_proc_fill_rectangle((*fill_rect)) =
179
                dev_proc(dev, fill_rectangle);
180
# endif
181
182
98.9k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
98.9k
        l.h = left->end.y - left->start.y;
185
98.9k
        if (l.h == 0)
186
0
           return 0;
187
98.9k
        r.h = right->end.y - right->start.y;
188
98.9k
        if (r.h == 0)
189
0
           return 0;
190
98.9k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
98.9k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
#if !LINEAR_COLOR
193
        ry = iy;
194
#endif
195
196
/*
197
 * Free variables of FILL_TRAP_RECT:
198
 *  SWAP_AXES, pdevc, dev, fa
199
 * Free variables of FILL_TRAP_RECT_DIRECT:
200
 *  SWAP_AXES, fill_rect, dev, cindex
201
 */
202
98.9k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
98.9k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
98.9k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
98.9k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
98.9k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
98.9k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
98.9k
#if LINEAR_COLOR
210
98.9k
#   define FILL_TRAP_RECT(x,y,w,h)\
211
98.9k
        (!(w) ? 0 : dev_proc(dev, fill_linear_color_scanline)(dev, fa, x, y, w, xg.c, xg.f, xg.num, xg.den))
212
#else
213
#   define FILL_TRAP_RECT(x,y,w,h)\
214
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
#endif
216
217
        /* Compute the dx/dy ratios. */
218
219
        /*
220
         * Compute the x offsets at the first scan line to sample.  We need
221
         * to be careful in computing ys# * dx#f {/,%} h# because the
222
         * multiplication may overflow.  We know that all the quantities
223
         * involved are non-negative, and that ys# is usually less than 1 (as
224
         * a fixed, of course); this gives us a cheap conservative check for
225
         * overflow in the multiplication.
226
         */
227
98.9k
#define YMULT_QUO(ys, tl)\
228
98.9k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
98.9k
   fixed_mult_quo(ys, tl.df, tl.h))
230
231
#if CONTIGUOUS_FILL
232
/*
233
 * If left and right boundary round to same pixel index,
234
 * we would not paing the scan and would get a dropout.
235
 * Check for this case and choose one of two pixels
236
 * which is closer to the "axis". We need to exclude
237
 * 'peak' because it would paint an excessive pixel.
238
 */
239
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) \
240
    if (ixl == ixr) \
241
        if ((!peak0 || iy >= peak_y0) && (!peak1 || iy <= peak_y1)) {\
242
            fixed x = int2fixed(ixl) + fixed_half;\
243
            if (x - l.x < r.x - x)\
244
                ++ixr;\
245
            else\
246
                --ixl;\
247
        }
248
249
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill)\
250
    if (adj1 < adj2) {\
251
        if (iy - ry > 1) {\
252
            code = fill(rxl, ry, rxr - rxl, iy - ry - 1);\
253
            if (code < 0)\
254
                goto xit;\
255
            ry = iy - 1;\
256
        }\
257
        adj1 = adj2 = (adj2 + adj2) / 2;\
258
    }
259
260
#else
261
98.9k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
98.9k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
98.9k
#endif
264
98.9k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
57.3k
            l.di = 0, l.df = 0;
267
57.3k
            fxl = 0;
268
57.3k
        } else {
269
41.6k
            compute_dx(&l, dxl, ysl);
270
41.6k
            fxl = YMULT_QUO(ysl, l);
271
41.6k
            l.x += fxl;
272
41.6k
        }
273
98.9k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
#     if !LINEAR_COLOR
277
                if (l.di == 0 && l.df == 0) {
278
                    rxl = fixed2int_var(l.x);
279
                    rxr = fixed2int_var(r.x);
280
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
                    goto xit;
283
                }
284
#     endif
285
56.9k
            r.di = 0, r.df = 0;
286
56.9k
        }
287
        /*
288
         * The test for fxl != 0 is required because the right edge might
289
         * cross some pixel centers even if the left edge doesn't.
290
         */
291
41.9k
        else if (dxr == dxl && fxl != 0) {
292
25.2k
            if (l.di == 0)
293
12.9k
                r.di = 0, r.df = l.df;
294
12.2k
            else
295
12.2k
                compute_dx(&r, dxr, ysr);
296
25.2k
            if (ysr == ysl && r.h == l.h)
297
25.1k
                r.x += fxl;
298
27
            else
299
27
                r.x += YMULT_QUO(ysr, r);
300
25.2k
        } else {
301
16.7k
            compute_dx(&r, dxr, ysr);
302
16.7k
            r.x += YMULT_QUO(ysr, r);
303
16.7k
        }
304
        /* Compute one line's worth of dx/dy. */
305
98.9k
        compute_ldx(&l, ysl);
306
98.9k
        compute_ldx(&r, ysr);
307
        /* We subtracted fixed_epsilon from l.x, r.x to simplify rounding
308
           when the rational part is zero. Now add it back to get xl', xr' */
309
98.9k
        l.x += fixed_epsilon;
310
98.9k
        r.x += fixed_epsilon;
311
98.9k
# if LINEAR_COLOR
312
#     ifdef DEBUG
313
                if (check_gradient_overflow(left, right)) {
314
                    /* The caller must care of.
315
                       Checking it here looses some performance with triangles. */
316
                    return_error(gs_error_unregistered);
317
                }
318
#     endif
319
98.9k
            lg.c = lgc;
320
98.9k
            lg.f = lgf;
321
98.9k
            lg.num = lgnum;
322
98.9k
            rg.c = rgc;
323
98.9k
            rg.f = rgf;
324
98.9k
            rg.num = rgnum;
325
98.9k
            xg.c = xgc;
326
98.9k
            xg.f = xgf;
327
98.9k
            xg.num = xgnum;
328
98.9k
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
98.9k
            if (code < 0)
330
0
                return code;
331
98.9k
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
98.9k
            if (code < 0)
333
0
                return code;
334
335
98.9k
# endif
336
337
98.9k
#define rational_floor(tl)\
338
98.9k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
98.9k
#define STEP_LINE(ix, tl)\
340
98.9k
  tl.x += tl.ldi;\
341
98.9k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
98.9k
  ix = rational_floor(tl)
343
344
98.9k
        rxl = rational_floor(l);
345
98.9k
        rxr = rational_floor(r);
346
98.9k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
2.20M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
2.20M
#     if LINEAR_COLOR
349
2.20M
                if (rxl != rxr) {
350
2.02M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
2.02M
                    if (code < 0)
352
0
                        goto xit;
353
2.02M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
2.02M
                    if (code < 0)
355
0
                        goto xit;
356
2.02M
                }
357
2.20M
                if (++iy == iy1)
358
98.9k
                    break;
359
2.10M
                STEP_LINE(rxl, l);
360
2.10M
                STEP_LINE(rxr, r);
361
2.10M
                step_gradient(&lg, num_components);
362
2.10M
                step_gradient(&rg, num_components);
363
#     else
364
                register int ixl, ixr;
365
366
                STEP_LINE(ixl, l);
367
                STEP_LINE(ixr, r);
368
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
                if (ixl != rxl || ixr != rxr) {
370
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
                    if (code < 0)
374
                        goto xit;
375
                    rxl = ixl, rxr = ixr, ry = iy;
376
                }
377
#     endif
378
2.10M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
98.9k
            code = 0;
383
98.9k
# endif
384
98.9k
#undef STEP_LINE
385
98.9k
#undef SET_MINIMAL_WIDTH
386
98.9k
#undef CONNECT_RECTANGLES
387
98.9k
#undef FILL_TRAP_RECT
388
98.9k
#undef FILL_TRAP_RECT_DIRECT
389
98.9k
#undef FILL_TRAP_RECT_INRECT
390
98.9k
#undef YMULT_QUO
391
98.9k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
98.9k
        return_if_interrupt(dev->memory);
394
98.9k
        return code;
395
98.9k
    }
396
98.9k
}
gdevddrw.c:gx_fill_trapezoid_ns_lc
Line
Count
Source
137
1.78M
{
138
1.78M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
1.78M
    const fixed ymax = fixed_pixround(ytop);
140
141
1.78M
    if (ymin >= ymax)
142
676k
        return 0;    /* no scan lines to sample */
143
1.10M
    {
144
1.10M
        int iy = fixed2int_var(ymin);
145
1.10M
        const int iy1 = fixed2int_var(ymax);
146
1.10M
        trap_line l, r;
147
1.10M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
1.10M
        const fixed
152
1.10M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
1.10M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
1.10M
        const fixed /* partial pixel offset to first line to sample */
155
1.10M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
1.10M
        fixed fxl;
157
1.10M
        int code;
158
# if CONTIGUOUS_FILL
159
            const bool peak0 = ((flags & 1) != 0);
160
            const bool peak1 = ((flags & 2) != 0);
161
            int peak_y0 = ybot + fixed_half;
162
            int peak_y1 = ytop - fixed_half;
163
# endif
164
1.10M
# if LINEAR_COLOR
165
1.10M
            int num_components = dev->color_info.num_components;
166
1.10M
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
1.10M
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
1.10M
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
1.10M
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
1.10M
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
1.10M
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
1.10M
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
1.10M
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
1.10M
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
1.10M
            trap_gradient lg, rg, xg;
176
# else
177
            gx_color_index cindex = pdevc->colors.pure;
178
            dev_proc_fill_rectangle((*fill_rect)) =
179
                dev_proc(dev, fill_rectangle);
180
# endif
181
182
1.10M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
1.10M
        l.h = left->end.y - left->start.y;
185
1.10M
        if (l.h == 0)
186
0
           return 0;
187
1.10M
        r.h = right->end.y - right->start.y;
188
1.10M
        if (r.h == 0)
189
0
           return 0;
190
1.10M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
1.10M
        r.x = x0r + (fixed_half - fixed_epsilon);
192
#if !LINEAR_COLOR
193
        ry = iy;
194
#endif
195
196
/*
197
 * Free variables of FILL_TRAP_RECT:
198
 *  SWAP_AXES, pdevc, dev, fa
199
 * Free variables of FILL_TRAP_RECT_DIRECT:
200
 *  SWAP_AXES, fill_rect, dev, cindex
201
 */
202
1.10M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
1.10M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
1.10M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
1.10M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
1.10M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
1.10M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
1.10M
#if LINEAR_COLOR
210
1.10M
#   define FILL_TRAP_RECT(x,y,w,h)\
211
1.10M
        (!(w) ? 0 : dev_proc(dev, fill_linear_color_scanline)(dev, fa, x, y, w, xg.c, xg.f, xg.num, xg.den))
212
#else
213
#   define FILL_TRAP_RECT(x,y,w,h)\
214
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
#endif
216
217
        /* Compute the dx/dy ratios. */
218
219
        /*
220
         * Compute the x offsets at the first scan line to sample.  We need
221
         * to be careful in computing ys# * dx#f {/,%} h# because the
222
         * multiplication may overflow.  We know that all the quantities
223
         * involved are non-negative, and that ys# is usually less than 1 (as
224
         * a fixed, of course); this gives us a cheap conservative check for
225
         * overflow in the multiplication.
226
         */
227
1.10M
#define YMULT_QUO(ys, tl)\
228
1.10M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
1.10M
   fixed_mult_quo(ys, tl.df, tl.h))
230
231
#if CONTIGUOUS_FILL
232
/*
233
 * If left and right boundary round to same pixel index,
234
 * we would not paing the scan and would get a dropout.
235
 * Check for this case and choose one of two pixels
236
 * which is closer to the "axis". We need to exclude
237
 * 'peak' because it would paint an excessive pixel.
238
 */
239
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) \
240
    if (ixl == ixr) \
241
        if ((!peak0 || iy >= peak_y0) && (!peak1 || iy <= peak_y1)) {\
242
            fixed x = int2fixed(ixl) + fixed_half;\
243
            if (x - l.x < r.x - x)\
244
                ++ixr;\
245
            else\
246
                --ixl;\
247
        }
248
249
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill)\
250
    if (adj1 < adj2) {\
251
        if (iy - ry > 1) {\
252
            code = fill(rxl, ry, rxr - rxl, iy - ry - 1);\
253
            if (code < 0)\
254
                goto xit;\
255
            ry = iy - 1;\
256
        }\
257
        adj1 = adj2 = (adj2 + adj2) / 2;\
258
    }
259
260
#else
261
1.10M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
1.10M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
1.10M
#endif
264
1.10M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
155k
            l.di = 0, l.df = 0;
267
155k
            fxl = 0;
268
950k
        } else {
269
950k
            compute_dx(&l, dxl, ysl);
270
950k
            fxl = YMULT_QUO(ysl, l);
271
950k
            l.x += fxl;
272
950k
        }
273
1.10M
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
#     if !LINEAR_COLOR
277
                if (l.di == 0 && l.df == 0) {
278
                    rxl = fixed2int_var(l.x);
279
                    rxr = fixed2int_var(r.x);
280
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
                    goto xit;
283
                }
284
#     endif
285
156k
            r.di = 0, r.df = 0;
286
156k
        }
287
        /*
288
         * The test for fxl != 0 is required because the right edge might
289
         * cross some pixel centers even if the left edge doesn't.
290
         */
291
949k
        else if (dxr == dxl && fxl != 0) {
292
78.5k
            if (l.di == 0)
293
16.5k
                r.di = 0, r.df = l.df;
294
61.9k
            else
295
61.9k
                compute_dx(&r, dxr, ysr);
296
78.5k
            if (ysr == ysl && r.h == l.h)
297
55.3k
                r.x += fxl;
298
23.2k
            else
299
23.2k
                r.x += YMULT_QUO(ysr, r);
300
871k
        } else {
301
871k
            compute_dx(&r, dxr, ysr);
302
871k
            r.x += YMULT_QUO(ysr, r);
303
871k
        }
304
        /* Compute one line's worth of dx/dy. */
305
1.10M
        compute_ldx(&l, ysl);
306
1.10M
        compute_ldx(&r, ysr);
307
        /* We subtracted fixed_epsilon from l.x, r.x to simplify rounding
308
           when the rational part is zero. Now add it back to get xl', xr' */
309
1.10M
        l.x += fixed_epsilon;
310
1.10M
        r.x += fixed_epsilon;
311
1.10M
# if LINEAR_COLOR
312
#     ifdef DEBUG
313
                if (check_gradient_overflow(left, right)) {
314
                    /* The caller must care of.
315
                       Checking it here looses some performance with triangles. */
316
                    return_error(gs_error_unregistered);
317
                }
318
#     endif
319
1.10M
            lg.c = lgc;
320
1.10M
            lg.f = lgf;
321
1.10M
            lg.num = lgnum;
322
1.10M
            rg.c = rgc;
323
1.10M
            rg.f = rgf;
324
1.10M
            rg.num = rgnum;
325
1.10M
            xg.c = xgc;
326
1.10M
            xg.f = xgf;
327
1.10M
            xg.num = xgnum;
328
1.10M
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
1.10M
            if (code < 0)
330
0
                return code;
331
1.10M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
1.10M
            if (code < 0)
333
0
                return code;
334
335
1.10M
# endif
336
337
1.10M
#define rational_floor(tl)\
338
1.10M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
1.10M
#define STEP_LINE(ix, tl)\
340
1.10M
  tl.x += tl.ldi;\
341
1.10M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
1.10M
  ix = rational_floor(tl)
343
344
1.10M
        rxl = rational_floor(l);
345
1.10M
        rxr = rational_floor(r);
346
1.10M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
7.99M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
7.99M
#     if LINEAR_COLOR
349
7.99M
                if (rxl != rxr) {
350
3.36M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
3.36M
                    if (code < 0)
352
0
                        goto xit;
353
3.36M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
3.36M
                    if (code < 0)
355
0
                        goto xit;
356
3.36M
                }
357
7.99M
                if (++iy == iy1)
358
1.10M
                    break;
359
6.89M
                STEP_LINE(rxl, l);
360
6.89M
                STEP_LINE(rxr, r);
361
6.89M
                step_gradient(&lg, num_components);
362
6.89M
                step_gradient(&rg, num_components);
363
#     else
364
                register int ixl, ixr;
365
366
                STEP_LINE(ixl, l);
367
                STEP_LINE(ixr, r);
368
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
                if (ixl != rxl || ixr != rxr) {
370
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
                    if (code < 0)
374
                        goto xit;
375
                    rxl = ixl, rxr = ixr, ry = iy;
376
                }
377
#     endif
378
6.89M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
1.10M
            code = 0;
383
1.10M
# endif
384
1.10M
#undef STEP_LINE
385
1.10M
#undef SET_MINIMAL_WIDTH
386
1.10M
#undef CONNECT_RECTANGLES
387
1.10M
#undef FILL_TRAP_RECT
388
1.10M
#undef FILL_TRAP_RECT_DIRECT
389
1.10M
#undef FILL_TRAP_RECT_INRECT
390
1.10M
#undef YMULT_QUO
391
1.10M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
1.10M
        return_if_interrupt(dev->memory);
394
1.10M
        return code;
395
1.10M
    }
396
1.10M
}
397
398
#undef GX_FILL_TRAPEZOID
399
#undef CONTIGUOUS_FILL
400
#undef SWAP_AXES
401
#undef FLAGS_TYPE
402
403
#else
404
int dummy;
405
#endif