Coverage Report

Created: 2025-06-10 07:17

/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
11.0M
{
138
11.0M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
11.0M
    const fixed ymax = fixed_pixround(ytop);
140
141
11.0M
    if (ymin >= ymax)
142
4.07M
        return 0;    /* no scan lines to sample */
143
7.01M
    {
144
7.01M
        int iy = fixed2int_var(ymin);
145
7.01M
        const int iy1 = fixed2int_var(ymax);
146
7.01M
        trap_line l, r;
147
7.01M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
7.01M
        const fixed
152
7.01M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
7.01M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
7.01M
        const fixed /* partial pixel offset to first line to sample */
155
7.01M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
7.01M
        fixed fxl;
157
7.01M
        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
1.30M
            dev_proc_fill_rectangle((*fill_rect)) =
179
1.30M
                dev_proc(dev, fill_rectangle);
180
# endif
181
182
7.01M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
7.01M
        l.h = left->end.y - left->start.y;
185
7.01M
        if (l.h == 0)
186
3
           return 0;
187
7.01M
        r.h = right->end.y - right->start.y;
188
7.01M
        if (r.h == 0)
189
3
           return 0;
190
7.01M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
7.01M
        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
7.01M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
7.01M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
31.4k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
7.01M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
49.1M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
49.1M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
17.9M
        (!(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
49.1M
        (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
7.01M
#define YMULT_QUO(ys, tl)\
228
9.57M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
9.57M
   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
56.3M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
95.6M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
#endif
264
7.01M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
2.04M
            l.di = 0, l.df = 0;
267
2.04M
            fxl = 0;
268
4.96M
        } else {
269
4.96M
            compute_dx(&l, dxl, ysl);
270
4.96M
            fxl = YMULT_QUO(ysl, l);
271
4.96M
            l.x += fxl;
272
4.96M
        }
273
7.01M
        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
760k
                if (l.di == 0 && l.df == 0) {
278
690k
                    rxl = fixed2int_var(l.x);
279
690k
                    rxr = fixed2int_var(r.x);
280
690k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
690k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
690k
                    goto xit;
283
690k
                }
284
69.4k
#     endif
285
69.4k
            r.di = 0, r.df = 0;
286
69.4k
        }
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
4.96M
        else if (dxr == dxl && fxl != 0) {
292
447k
            if (l.di == 0)
293
108k
                r.di = 0, r.df = l.df;
294
339k
            else
295
339k
                compute_dx(&r, dxr, ysr);
296
447k
            if (ysr == ysl && r.h == l.h)
297
358k
                r.x += fxl;
298
89.1k
            else
299
89.1k
                r.x += YMULT_QUO(ysr, r);
300
4.51M
        } else {
301
4.51M
            compute_dx(&r, dxr, ysr);
302
4.51M
            r.x += YMULT_QUO(ysr, r);
303
4.51M
        }
304
        /* Compute one line's worth of dx/dy. */
305
618k
        compute_ldx(&l, ysl);
306
618k
        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
6.32M
        l.x += fixed_epsilon;
310
6.32M
        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
5.70M
            if (code < 0)
330
0
                return code;
331
5.70M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
5.70M
            if (code < 0)
333
0
                return code;
334
335
5.70M
# endif
336
337
5.70M
#define rational_floor(tl)\
338
250M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
5.70M
#define STEP_LINE(ix, tl)\
340
232M
  tl.x += tl.ldi;\
341
232M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
238M
  ix = rational_floor(tl)
343
344
6.32M
        rxl = rational_floor(l);
345
6.32M
        rxr = rational_floor(r);
346
6.32M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
122M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
72.5M
                if (rxl != rxr) {
350
17.9M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
17.9M
                    if (code < 0)
352
0
                        goto xit;
353
17.9M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
17.9M
                    if (code < 0)
355
9
                        goto xit;
356
17.9M
                }
357
72.5M
                if (++iy == iy1)
358
5.70M
                    break;
359
66.8M
                STEP_LINE(rxl, l);
360
66.8M
                STEP_LINE(rxr, r);
361
66.8M
                step_gradient(&lg, num_components);
362
66.8M
                step_gradient(&rg, num_components);
363
#     else
364
                register int ixl, ixr;
365
366
49.3M
                STEP_LINE(ixl, l);
367
49.3M
                STEP_LINE(ixr, r);
368
49.3M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
49.3M
                if (ixl != rxl || ixr != rxr) {
370
47.8M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
47.8M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
47.8M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
47.8M
                    if (code < 0)
374
0
                        goto xit;
375
47.8M
                    rxl = ixl, rxr = ixr, ry = iy;
376
47.8M
                }
377
#     endif
378
66.8M
        }
379
# if !LINEAR_COLOR
380
618k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
5.70M
            code = 0;
383
5.70M
# endif
384
5.70M
#undef STEP_LINE
385
5.70M
#undef SET_MINIMAL_WIDTH
386
5.70M
#undef CONNECT_RECTANGLES
387
5.70M
#undef FILL_TRAP_RECT
388
5.70M
#undef FILL_TRAP_RECT_DIRECT
389
5.70M
#undef FILL_TRAP_RECT_INRECT
390
5.70M
#undef YMULT_QUO
391
7.01M
xit:  if (code < 0 && FILL_DIRECT)
392
9
            return_error(code);
393
7.01M
        return_if_interrupt(dev->memory);
394
7.01M
        return code;
395
7.01M
    }
396
7.01M
}
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
61.7k
{
138
61.7k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
61.7k
    const fixed ymax = fixed_pixround(ytop);
140
141
61.7k
    if (ymin >= ymax)
142
4.74k
        return 0;    /* no scan lines to sample */
143
56.9k
    {
144
56.9k
        int iy = fixed2int_var(ymin);
145
56.9k
        const int iy1 = fixed2int_var(ymax);
146
56.9k
        trap_line l, r;
147
56.9k
        register int rxl, rxr;
148
56.9k
#if !LINEAR_COLOR
149
56.9k
        int ry;
150
56.9k
#endif
151
56.9k
        const fixed
152
56.9k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
56.9k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
56.9k
        const fixed /* partial pixel offset to first line to sample */
155
56.9k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
56.9k
        fixed fxl;
157
56.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
# 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
56.9k
            gx_color_index cindex = pdevc->colors.pure;
178
56.9k
            dev_proc_fill_rectangle((*fill_rect)) =
179
56.9k
                dev_proc(dev, fill_rectangle);
180
56.9k
# endif
181
182
56.9k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
56.9k
        l.h = left->end.y - left->start.y;
185
56.9k
        if (l.h == 0)
186
0
           return 0;
187
56.9k
        r.h = right->end.y - right->start.y;
188
56.9k
        if (r.h == 0)
189
0
           return 0;
190
56.9k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
56.9k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
56.9k
#if !LINEAR_COLOR
193
56.9k
        ry = iy;
194
56.9k
#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
56.9k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
56.9k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
56.9k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
56.9k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
56.9k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
56.9k
   (*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
56.9k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
56.9k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
56.9k
#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
56.9k
#define YMULT_QUO(ys, tl)\
228
56.9k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
56.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
56.9k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
56.9k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
56.9k
#endif
264
56.9k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
27.9k
            l.di = 0, l.df = 0;
267
27.9k
            fxl = 0;
268
28.9k
        } else {
269
28.9k
            compute_dx(&l, dxl, ysl);
270
28.9k
            fxl = YMULT_QUO(ysl, l);
271
28.9k
            l.x += fxl;
272
28.9k
        }
273
56.9k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
31.9k
#     if !LINEAR_COLOR
277
31.9k
                if (l.di == 0 && l.df == 0) {
278
25.9k
                    rxl = fixed2int_var(l.x);
279
25.9k
                    rxr = fixed2int_var(r.x);
280
25.9k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
25.9k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
25.9k
                    goto xit;
283
25.9k
                }
284
5.92k
#     endif
285
5.92k
            r.di = 0, r.df = 0;
286
5.92k
        }
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
25.0k
        else if (dxr == dxl && fxl != 0) {
292
9.31k
            if (l.di == 0)
293
3.93k
                r.di = 0, r.df = l.df;
294
5.37k
            else
295
5.37k
                compute_dx(&r, dxr, ysr);
296
9.31k
            if (ysr == ysl && r.h == l.h)
297
9.30k
                r.x += fxl;
298
8
            else
299
8
                r.x += YMULT_QUO(ysr, r);
300
15.7k
        } else {
301
15.7k
            compute_dx(&r, dxr, ysr);
302
15.7k
            r.x += YMULT_QUO(ysr, r);
303
15.7k
        }
304
        /* Compute one line's worth of dx/dy. */
305
30.9k
        compute_ldx(&l, ysl);
306
30.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
30.9k
        l.x += fixed_epsilon;
310
30.9k
        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
30.9k
#define rational_floor(tl)\
338
30.9k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
30.9k
#define STEP_LINE(ix, tl)\
340
30.9k
  tl.x += tl.ldi;\
341
30.9k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
30.9k
  ix = rational_floor(tl)
343
344
30.9k
        rxl = rational_floor(l);
345
30.9k
        rxr = rational_floor(r);
346
30.9k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
642k
        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
611k
                register int ixl, ixr;
365
366
611k
                STEP_LINE(ixl, l);
367
611k
                STEP_LINE(ixr, r);
368
611k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
611k
                if (ixl != rxl || ixr != rxr) {
370
165k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
165k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
165k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
165k
                    if (code < 0)
374
0
                        goto xit;
375
165k
                    rxl = ixl, rxr = ixr, ry = iy;
376
165k
                }
377
611k
#     endif
378
611k
        }
379
30.9k
# if !LINEAR_COLOR
380
30.9k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
30.9k
#undef STEP_LINE
385
30.9k
#undef SET_MINIMAL_WIDTH
386
30.9k
#undef CONNECT_RECTANGLES
387
30.9k
#undef FILL_TRAP_RECT
388
30.9k
#undef FILL_TRAP_RECT_DIRECT
389
30.9k
#undef FILL_TRAP_RECT_INRECT
390
30.9k
#undef YMULT_QUO
391
56.9k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
56.9k
        return_if_interrupt(dev->memory);
394
56.9k
        return code;
395
56.9k
    }
396
56.9k
}
Unexecuted instantiation: gdevddrw.c:gx_fill_trapezoid_as_nd
gdevddrw.c:gx_fill_trapezoid_ns_fd
Line
Count
Source
137
1.27M
{
138
1.27M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
1.27M
    const fixed ymax = fixed_pixround(ytop);
140
141
1.27M
    if (ymin >= ymax)
142
44.6k
        return 0;    /* no scan lines to sample */
143
1.23M
    {
144
1.23M
        int iy = fixed2int_var(ymin);
145
1.23M
        const int iy1 = fixed2int_var(ymax);
146
1.23M
        trap_line l, r;
147
1.23M
        register int rxl, rxr;
148
1.23M
#if !LINEAR_COLOR
149
1.23M
        int ry;
150
1.23M
#endif
151
1.23M
        const fixed
152
1.23M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
1.23M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
1.23M
        const fixed /* partial pixel offset to first line to sample */
155
1.23M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
1.23M
        fixed fxl;
157
1.23M
        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
1.23M
            gx_color_index cindex = pdevc->colors.pure;
178
1.23M
            dev_proc_fill_rectangle((*fill_rect)) =
179
1.23M
                dev_proc(dev, fill_rectangle);
180
1.23M
# endif
181
182
1.23M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
1.23M
        l.h = left->end.y - left->start.y;
185
1.23M
        if (l.h == 0)
186
3
           return 0;
187
1.23M
        r.h = right->end.y - right->start.y;
188
1.23M
        if (r.h == 0)
189
3
           return 0;
190
1.23M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
1.23M
        r.x = x0r + (fixed_half - fixed_epsilon);
192
1.23M
#if !LINEAR_COLOR
193
1.23M
        ry = iy;
194
1.23M
#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.23M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
1.23M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
1.23M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
1.23M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
1.23M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
1.23M
   (*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
1.23M
#   define FILL_TRAP_RECT(x,y,w,h)\
214
1.23M
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
1.23M
#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.23M
#define YMULT_QUO(ys, tl)\
228
1.23M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
1.23M
   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.23M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
1.23M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
1.23M
#endif
264
1.23M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
710k
            l.di = 0, l.df = 0;
267
710k
            fxl = 0;
268
710k
        } else {
269
524k
            compute_dx(&l, dxl, ysl);
270
524k
            fxl = YMULT_QUO(ysl, l);
271
524k
            l.x += fxl;
272
524k
        }
273
1.23M
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
712k
#     if !LINEAR_COLOR
277
712k
                if (l.di == 0 && l.df == 0) {
278
649k
                    rxl = fixed2int_var(l.x);
279
649k
                    rxr = fixed2int_var(r.x);
280
649k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
649k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
649k
                    goto xit;
283
649k
                }
284
63.3k
#     endif
285
63.3k
            r.di = 0, r.df = 0;
286
63.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
521k
        else if (dxr == dxl && fxl != 0) {
292
189k
            if (l.di == 0)
293
39.5k
                r.di = 0, r.df = l.df;
294
149k
            else
295
149k
                compute_dx(&r, dxr, ysr);
296
189k
            if (ysr == ysl && r.h == l.h)
297
171k
                r.x += fxl;
298
17.4k
            else
299
17.4k
                r.x += YMULT_QUO(ysr, r);
300
332k
        } else {
301
332k
            compute_dx(&r, dxr, ysr);
302
332k
            r.x += YMULT_QUO(ysr, r);
303
332k
        }
304
        /* Compute one line's worth of dx/dy. */
305
585k
        compute_ldx(&l, ysl);
306
585k
        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
585k
        l.x += fixed_epsilon;
310
585k
        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
585k
#define rational_floor(tl)\
338
585k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
585k
#define STEP_LINE(ix, tl)\
340
585k
  tl.x += tl.ldi;\
341
585k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
585k
  ix = rational_floor(tl)
343
344
585k
        rxl = rational_floor(l);
345
585k
        rxr = rational_floor(r);
346
585k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
49.2M
        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
48.7M
                register int ixl, ixr;
365
366
48.7M
                STEP_LINE(ixl, l);
367
48.7M
                STEP_LINE(ixr, r);
368
48.7M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
48.7M
                if (ixl != rxl || ixr != rxr) {
370
47.6M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
47.6M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
47.6M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
47.6M
                    if (code < 0)
374
0
                        goto xit;
375
47.6M
                    rxl = ixl, rxr = ixr, ry = iy;
376
47.6M
                }
377
48.7M
#     endif
378
48.7M
        }
379
585k
# if !LINEAR_COLOR
380
585k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
585k
#undef STEP_LINE
385
585k
#undef SET_MINIMAL_WIDTH
386
585k
#undef CONNECT_RECTANGLES
387
585k
#undef FILL_TRAP_RECT
388
585k
#undef FILL_TRAP_RECT_DIRECT
389
585k
#undef FILL_TRAP_RECT_INRECT
390
585k
#undef YMULT_QUO
391
1.23M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
1.23M
        return_if_interrupt(dev->memory);
394
1.23M
        return code;
395
1.23M
    }
396
1.23M
}
gdevddrw.c:gx_fill_trapezoid_ns_nd
Line
Count
Source
137
17.3k
{
138
17.3k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
17.3k
    const fixed ymax = fixed_pixround(ytop);
140
141
17.3k
    if (ymin >= ymax)
142
0
        return 0;   /* no scan lines to sample */
143
17.3k
    {
144
17.3k
        int iy = fixed2int_var(ymin);
145
17.3k
        const int iy1 = fixed2int_var(ymax);
146
17.3k
        trap_line l, r;
147
17.3k
        register int rxl, rxr;
148
17.3k
#if !LINEAR_COLOR
149
17.3k
        int ry;
150
17.3k
#endif
151
17.3k
        const fixed
152
17.3k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
17.3k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
17.3k
        const fixed /* partial pixel offset to first line to sample */
155
17.3k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
17.3k
        fixed fxl;
157
17.3k
        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.3k
            gx_color_index cindex = pdevc->colors.pure;
178
17.3k
            dev_proc_fill_rectangle((*fill_rect)) =
179
17.3k
                dev_proc(dev, fill_rectangle);
180
17.3k
# endif
181
182
17.3k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
17.3k
        l.h = left->end.y - left->start.y;
185
17.3k
        if (l.h == 0)
186
0
           return 0;
187
17.3k
        r.h = right->end.y - right->start.y;
188
17.3k
        if (r.h == 0)
189
0
           return 0;
190
17.3k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
17.3k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
17.3k
#if !LINEAR_COLOR
193
17.3k
        ry = iy;
194
17.3k
#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.3k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
17.3k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
17.3k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
17.3k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
17.3k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
17.3k
   (*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.3k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
17.3k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
17.3k
#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.3k
#define YMULT_QUO(ys, tl)\
228
17.3k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
17.3k
   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.3k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
17.3k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
17.3k
#endif
264
17.3k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
15.5k
            l.di = 0, l.df = 0;
267
15.5k
            fxl = 0;
268
15.5k
        } else {
269
1.77k
            compute_dx(&l, dxl, ysl);
270
1.77k
            fxl = YMULT_QUO(ysl, l);
271
1.77k
            l.x += fxl;
272
1.77k
        }
273
17.3k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
15.5k
#     if !LINEAR_COLOR
277
15.5k
                if (l.di == 0 && l.df == 0) {
278
15.3k
                    rxl = fixed2int_var(l.x);
279
15.3k
                    rxr = fixed2int_var(r.x);
280
15.3k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
15.3k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
15.3k
                    goto xit;
283
15.3k
                }
284
161
#     endif
285
161
            r.di = 0, r.df = 0;
286
161
        }
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.78k
        else if (dxr == dxl && fxl != 0) {
292
1
            if (l.di == 0)
293
0
                r.di = 0, r.df = l.df;
294
1
            else
295
1
                compute_dx(&r, dxr, ysr);
296
1
            if (ysr == ysl && r.h == l.h)
297
0
                r.x += fxl;
298
1
            else
299
1
                r.x += YMULT_QUO(ysr, r);
300
1.78k
        } else {
301
1.78k
            compute_dx(&r, dxr, ysr);
302
1.78k
            r.x += YMULT_QUO(ysr, r);
303
1.78k
        }
304
        /* Compute one line's worth of dx/dy. */
305
1.94k
        compute_ldx(&l, ysl);
306
1.94k
        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.94k
        l.x += fixed_epsilon;
310
1.94k
        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
1.94k
#define rational_floor(tl)\
338
1.94k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
1.94k
#define STEP_LINE(ix, tl)\
340
1.94k
  tl.x += tl.ldi;\
341
1.94k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
1.94k
  ix = rational_floor(tl)
343
344
1.94k
        rxl = rational_floor(l);
345
1.94k
        rxr = rational_floor(r);
346
1.94k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
24.0k
        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
22.0k
                register int ixl, ixr;
365
366
22.0k
                STEP_LINE(ixl, l);
367
22.0k
                STEP_LINE(ixr, r);
368
22.0k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
22.0k
                if (ixl != rxl || ixr != rxr) {
370
14.0k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
14.0k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
14.0k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
14.0k
                    if (code < 0)
374
0
                        goto xit;
375
14.0k
                    rxl = ixl, rxr = ixr, ry = iy;
376
14.0k
                }
377
22.0k
#     endif
378
22.0k
        }
379
1.94k
# if !LINEAR_COLOR
380
1.94k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
1.94k
#undef STEP_LINE
385
1.94k
#undef SET_MINIMAL_WIDTH
386
1.94k
#undef CONNECT_RECTANGLES
387
1.94k
#undef FILL_TRAP_RECT
388
1.94k
#undef FILL_TRAP_RECT_DIRECT
389
1.94k
#undef FILL_TRAP_RECT_INRECT
390
1.94k
#undef YMULT_QUO
391
17.3k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
17.3k
        return_if_interrupt(dev->memory);
394
17.3k
        return code;
395
17.3k
    }
396
17.3k
}
gdevddrw.c:gx_fill_trapezoid_as_lc
Line
Count
Source
137
349k
{
138
349k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
349k
    const fixed ymax = fixed_pixround(ytop);
140
141
349k
    if (ymin >= ymax)
142
28.0k
        return 0;    /* no scan lines to sample */
143
321k
    {
144
321k
        int iy = fixed2int_var(ymin);
145
321k
        const int iy1 = fixed2int_var(ymax);
146
321k
        trap_line l, r;
147
321k
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
321k
        const fixed
152
321k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
321k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
321k
        const fixed /* partial pixel offset to first line to sample */
155
321k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
321k
        fixed fxl;
157
321k
        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
321k
# if LINEAR_COLOR
165
321k
            int num_components = dev->color_info.num_components;
166
321k
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
321k
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
321k
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
321k
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
321k
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
321k
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
321k
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
321k
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
321k
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
321k
            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
321k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
321k
        l.h = left->end.y - left->start.y;
185
321k
        if (l.h == 0)
186
0
           return 0;
187
321k
        r.h = right->end.y - right->start.y;
188
321k
        if (r.h == 0)
189
0
           return 0;
190
321k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
321k
        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
321k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
321k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
321k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
321k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
321k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
321k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
321k
#if LINEAR_COLOR
210
321k
#   define FILL_TRAP_RECT(x,y,w,h)\
211
321k
        (!(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
321k
#define YMULT_QUO(ys, tl)\
228
321k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
321k
   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
321k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
321k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
321k
#endif
264
321k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
184k
            l.di = 0, l.df = 0;
267
184k
            fxl = 0;
268
184k
        } else {
269
136k
            compute_dx(&l, dxl, ysl);
270
136k
            fxl = YMULT_QUO(ysl, l);
271
136k
            l.x += fxl;
272
136k
        }
273
321k
        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
185k
            r.di = 0, r.df = 0;
286
185k
        }
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
135k
        else if (dxr == dxl && fxl != 0) {
292
60.6k
            if (l.di == 0)
293
29.0k
                r.di = 0, r.df = l.df;
294
31.6k
            else
295
31.6k
                compute_dx(&r, dxr, ysr);
296
60.6k
            if (ysr == ysl && r.h == l.h)
297
60.6k
                r.x += fxl;
298
3
            else
299
3
                r.x += YMULT_QUO(ysr, r);
300
74.9k
        } else {
301
74.9k
            compute_dx(&r, dxr, ysr);
302
74.9k
            r.x += YMULT_QUO(ysr, r);
303
74.9k
        }
304
        /* Compute one line's worth of dx/dy. */
305
321k
        compute_ldx(&l, ysl);
306
321k
        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
321k
        l.x += fixed_epsilon;
310
321k
        r.x += fixed_epsilon;
311
321k
# 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
321k
            lg.c = lgc;
320
321k
            lg.f = lgf;
321
321k
            lg.num = lgnum;
322
321k
            rg.c = rgc;
323
321k
            rg.f = rgf;
324
321k
            rg.num = rgnum;
325
321k
            xg.c = xgc;
326
321k
            xg.f = xgf;
327
321k
            xg.num = xgnum;
328
321k
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
321k
            if (code < 0)
330
0
                return code;
331
321k
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
321k
            if (code < 0)
333
0
                return code;
334
335
321k
# endif
336
337
321k
#define rational_floor(tl)\
338
321k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
321k
#define STEP_LINE(ix, tl)\
340
321k
  tl.x += tl.ldi;\
341
321k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
321k
  ix = rational_floor(tl)
343
344
321k
        rxl = rational_floor(l);
345
321k
        rxr = rational_floor(r);
346
321k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
9.81M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
9.81M
#     if LINEAR_COLOR
349
9.81M
                if (rxl != rxr) {
350
5.27M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
5.27M
                    if (code < 0)
352
0
                        goto xit;
353
5.27M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
5.27M
                    if (code < 0)
355
2
                        goto xit;
356
5.27M
                }
357
9.81M
                if (++iy == iy1)
358
321k
                    break;
359
9.49M
                STEP_LINE(rxl, l);
360
9.49M
                STEP_LINE(rxr, r);
361
9.49M
                step_gradient(&lg, num_components);
362
9.49M
                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
9.49M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
321k
            code = 0;
383
321k
# endif
384
321k
#undef STEP_LINE
385
321k
#undef SET_MINIMAL_WIDTH
386
321k
#undef CONNECT_RECTANGLES
387
321k
#undef FILL_TRAP_RECT
388
321k
#undef FILL_TRAP_RECT_DIRECT
389
321k
#undef FILL_TRAP_RECT_INRECT
390
321k
#undef YMULT_QUO
391
321k
xit:  if (code < 0 && FILL_DIRECT)
392
2
            return_error(code);
393
321k
        return_if_interrupt(dev->memory);
394
321k
        return code;
395
321k
    }
396
321k
}
gdevddrw.c:gx_fill_trapezoid_ns_lc
Line
Count
Source
137
9.38M
{
138
9.38M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
9.38M
    const fixed ymax = fixed_pixround(ytop);
140
141
9.38M
    if (ymin >= ymax)
142
4.00M
        return 0;    /* no scan lines to sample */
143
5.38M
    {
144
5.38M
        int iy = fixed2int_var(ymin);
145
5.38M
        const int iy1 = fixed2int_var(ymax);
146
5.38M
        trap_line l, r;
147
5.38M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
5.38M
        const fixed
152
5.38M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
5.38M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
5.38M
        const fixed /* partial pixel offset to first line to sample */
155
5.38M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
5.38M
        fixed fxl;
157
5.38M
        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
5.38M
# if LINEAR_COLOR
165
5.38M
            int num_components = dev->color_info.num_components;
166
5.38M
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
5.38M
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
5.38M
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
5.38M
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
5.38M
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
5.38M
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
5.38M
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
5.38M
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
5.38M
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
5.38M
            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
5.38M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
5.38M
        l.h = left->end.y - left->start.y;
185
5.38M
        if (l.h == 0)
186
0
           return 0;
187
5.38M
        r.h = right->end.y - right->start.y;
188
5.38M
        if (r.h == 0)
189
0
           return 0;
190
5.38M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
5.38M
        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
5.38M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
5.38M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
5.38M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
5.38M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
5.38M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
5.38M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
5.38M
#if LINEAR_COLOR
210
5.38M
#   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
        (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
5.38M
#define YMULT_QUO(ys, tl)\
228
5.38M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
5.38M
   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
5.38M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
5.38M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
5.38M
#endif
264
5.38M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
1.10M
            l.di = 0, l.df = 0;
267
1.10M
            fxl = 0;
268
4.27M
        } else {
269
4.27M
            compute_dx(&l, dxl, ysl);
270
4.27M
            fxl = YMULT_QUO(ysl, l);
271
4.27M
            l.x += fxl;
272
4.27M
        }
273
5.38M
        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
1.09M
            r.di = 0, r.df = 0;
286
1.09M
        }
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
4.28M
        else if (dxr == dxl && fxl != 0) {
292
188k
            if (l.di == 0)
293
35.7k
                r.di = 0, r.df = l.df;
294
152k
            else
295
152k
                compute_dx(&r, dxr, ysr);
296
188k
            if (ysr == ysl && r.h == l.h)
297
116k
                r.x += fxl;
298
71.7k
            else
299
71.7k
                r.x += YMULT_QUO(ysr, r);
300
4.09M
        } else {
301
4.09M
            compute_dx(&r, dxr, ysr);
302
4.09M
            r.x += YMULT_QUO(ysr, r);
303
4.09M
        }
304
        /* Compute one line's worth of dx/dy. */
305
5.38M
        compute_ldx(&l, ysl);
306
5.38M
        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.38M
        l.x += fixed_epsilon;
310
5.38M
        r.x += fixed_epsilon;
311
5.38M
# 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
5.38M
            lg.c = lgc;
320
5.38M
            lg.f = lgf;
321
5.38M
            lg.num = lgnum;
322
5.38M
            rg.c = rgc;
323
5.38M
            rg.f = rgf;
324
5.38M
            rg.num = rgnum;
325
5.38M
            xg.c = xgc;
326
5.38M
            xg.f = xgf;
327
5.38M
            xg.num = xgnum;
328
5.38M
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
5.38M
            if (code < 0)
330
0
                return code;
331
5.38M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
5.38M
            if (code < 0)
333
0
                return code;
334
335
5.38M
# endif
336
337
5.38M
#define rational_floor(tl)\
338
5.38M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
5.38M
#define STEP_LINE(ix, tl)\
340
5.38M
  tl.x += tl.ldi;\
341
5.38M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
5.38M
  ix = rational_floor(tl)
343
344
5.38M
        rxl = rational_floor(l);
345
5.38M
        rxr = rational_floor(r);
346
5.38M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
62.7M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
62.7M
#     if LINEAR_COLOR
349
62.7M
                if (rxl != rxr) {
350
12.6M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
12.6M
                    if (code < 0)
352
0
                        goto xit;
353
12.6M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
12.6M
                    if (code < 0)
355
7
                        goto xit;
356
12.6M
                }
357
62.7M
                if (++iy == iy1)
358
5.38M
                    break;
359
57.3M
                STEP_LINE(rxl, l);
360
57.3M
                STEP_LINE(rxr, r);
361
57.3M
                step_gradient(&lg, num_components);
362
57.3M
                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
57.3M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
5.38M
            code = 0;
383
5.38M
# endif
384
5.38M
#undef STEP_LINE
385
5.38M
#undef SET_MINIMAL_WIDTH
386
5.38M
#undef CONNECT_RECTANGLES
387
5.38M
#undef FILL_TRAP_RECT
388
5.38M
#undef FILL_TRAP_RECT_DIRECT
389
5.38M
#undef FILL_TRAP_RECT_INRECT
390
5.38M
#undef YMULT_QUO
391
5.38M
xit:  if (code < 0 && FILL_DIRECT)
392
7
            return_error(code);
393
5.38M
        return_if_interrupt(dev->memory);
394
5.38M
        return code;
395
5.38M
    }
396
5.38M
}
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