Coverage Report

Created: 2025-06-10 06:56

/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
4.07M
{
138
4.07M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
4.07M
    const fixed ymax = fixed_pixround(ytop);
140
141
4.07M
    if (ymin >= ymax)
142
1.58M
        return 0;    /* no scan lines to sample */
143
2.49M
    {
144
2.49M
        int iy = fixed2int_var(ymin);
145
2.49M
        const int iy1 = fixed2int_var(ymax);
146
2.49M
        trap_line l, r;
147
2.49M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
2.49M
        const fixed
152
2.49M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
2.49M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
2.49M
        const fixed /* partial pixel offset to first line to sample */
155
2.49M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
2.49M
        fixed fxl;
157
2.49M
        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
389k
            dev_proc_fill_rectangle((*fill_rect)) =
179
389k
                dev_proc(dev, fill_rectangle);
180
# endif
181
182
2.49M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
2.49M
        l.h = left->end.y - left->start.y;
185
2.49M
        if (l.h == 0)
186
0
           return 0;
187
2.49M
        r.h = right->end.y - right->start.y;
188
2.49M
        if (r.h == 0)
189
0
           return 0;
190
2.49M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
2.49M
        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
2.49M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
2.49M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
799k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
2.49M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
2.49M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
665k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
5.87M
        (!(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.46M
        (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
2.49M
#define YMULT_QUO(ys, tl)\
228
3.49M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
3.49M
   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
4.08M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
2.14M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
#endif
264
2.49M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
658k
            l.di = 0, l.df = 0;
267
658k
            fxl = 0;
268
1.83M
        } else {
269
1.83M
            compute_dx(&l, dxl, ysl);
270
1.83M
            fxl = YMULT_QUO(ysl, l);
271
1.83M
            l.x += fxl;
272
1.83M
        }
273
2.49M
        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
247k
                if (l.di == 0 && l.df == 0) {
278
230k
                    rxl = fixed2int_var(l.x);
279
230k
                    rxr = fixed2int_var(r.x);
280
230k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
230k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
230k
                    goto xit;
283
230k
                }
284
17.0k
#     endif
285
17.0k
            r.di = 0, r.df = 0;
286
17.0k
        }
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.84M
        else if (dxr == dxl && fxl != 0) {
292
220k
            if (l.di == 0)
293
45.8k
                r.di = 0, r.df = l.df;
294
175k
            else
295
175k
                compute_dx(&r, dxr, ysr);
296
220k
            if (ysr == ysl && r.h == l.h)
297
178k
                r.x += fxl;
298
42.2k
            else
299
42.2k
                r.x += YMULT_QUO(ysr, r);
300
1.61M
        } else {
301
1.61M
            compute_dx(&r, dxr, ysr);
302
1.61M
            r.x += YMULT_QUO(ysr, r);
303
1.61M
        }
304
        /* Compute one line's worth of dx/dy. */
305
158k
        compute_ldx(&l, ysl);
306
158k
        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
2.26M
        l.x += fixed_epsilon;
310
2.26M
        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
2.10M
            if (code < 0)
330
0
                return code;
331
2.10M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
2.10M
            if (code < 0)
333
0
                return code;
334
335
2.10M
# endif
336
337
2.10M
#define rational_floor(tl)\
338
38.4M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
2.10M
#define STEP_LINE(ix, tl)\
340
31.8M
  tl.x += tl.ldi;\
341
31.8M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
33.9M
  ix = rational_floor(tl)
343
344
2.26M
        rxl = rational_floor(l);
345
2.26M
        rxr = rational_floor(r);
346
2.26M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
18.1M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
16.4M
                if (rxl != rxr) {
350
5.87M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
5.87M
                    if (code < 0)
352
0
                        goto xit;
353
5.87M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
5.87M
                    if (code < 0)
355
0
                        goto xit;
356
5.87M
                }
357
16.4M
                if (++iy == iy1)
358
2.10M
                    break;
359
14.3M
                STEP_LINE(rxl, l);
360
14.3M
                STEP_LINE(rxr, r);
361
14.3M
                step_gradient(&lg, num_components);
362
14.3M
                step_gradient(&rg, num_components);
363
#     else
364
                register int ixl, ixr;
365
366
1.58M
                STEP_LINE(ixl, l);
367
1.58M
                STEP_LINE(ixr, r);
368
1.58M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
1.58M
                if (ixl != rxl || ixr != rxr) {
370
1.07M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
1.07M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
1.07M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
1.07M
                    if (code < 0)
374
0
                        goto xit;
375
1.07M
                    rxl = ixl, rxr = ixr, ry = iy;
376
1.07M
                }
377
#     endif
378
14.3M
        }
379
# if !LINEAR_COLOR
380
158k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
2.10M
            code = 0;
383
2.10M
# endif
384
2.10M
#undef STEP_LINE
385
2.10M
#undef SET_MINIMAL_WIDTH
386
2.10M
#undef CONNECT_RECTANGLES
387
2.10M
#undef FILL_TRAP_RECT
388
2.10M
#undef FILL_TRAP_RECT_DIRECT
389
2.10M
#undef FILL_TRAP_RECT_INRECT
390
2.10M
#undef YMULT_QUO
391
2.49M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
2.49M
        return_if_interrupt(dev->memory);
394
2.49M
        return code;
395
2.49M
    }
396
2.49M
}
Unexecuted instantiation: gx_fill_trapezoid_cf_fd
Unexecuted instantiation: gx_fill_trapezoid_cf_nd
Unexecuted instantiation: gdevddrw.c:gx_fill_trapezoid_as_fd
gdevddrw.c:gx_fill_trapezoid_as_nd
Line
Count
Source
137
24.3k
{
138
24.3k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
24.3k
    const fixed ymax = fixed_pixround(ytop);
140
141
24.3k
    if (ymin >= ymax)
142
1.72k
        return 0;    /* no scan lines to sample */
143
22.6k
    {
144
22.6k
        int iy = fixed2int_var(ymin);
145
22.6k
        const int iy1 = fixed2int_var(ymax);
146
22.6k
        trap_line l, r;
147
22.6k
        register int rxl, rxr;
148
22.6k
#if !LINEAR_COLOR
149
22.6k
        int ry;
150
22.6k
#endif
151
22.6k
        const fixed
152
22.6k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
22.6k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
22.6k
        const fixed /* partial pixel offset to first line to sample */
155
22.6k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
22.6k
        fixed fxl;
157
22.6k
        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
22.6k
            gx_color_index cindex = pdevc->colors.pure;
178
22.6k
            dev_proc_fill_rectangle((*fill_rect)) =
179
22.6k
                dev_proc(dev, fill_rectangle);
180
22.6k
# endif
181
182
22.6k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
22.6k
        l.h = left->end.y - left->start.y;
185
22.6k
        if (l.h == 0)
186
0
           return 0;
187
22.6k
        r.h = right->end.y - right->start.y;
188
22.6k
        if (r.h == 0)
189
0
           return 0;
190
22.6k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
22.6k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
22.6k
#if !LINEAR_COLOR
193
22.6k
        ry = iy;
194
22.6k
#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
22.6k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
22.6k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
22.6k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
22.6k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
22.6k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
22.6k
   (*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
22.6k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
22.6k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
22.6k
#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
22.6k
#define YMULT_QUO(ys, tl)\
228
22.6k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
22.6k
   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
22.6k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
22.6k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
22.6k
#endif
264
22.6k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
12.6k
            l.di = 0, l.df = 0;
267
12.6k
            fxl = 0;
268
12.6k
        } else {
269
10.0k
            compute_dx(&l, dxl, ysl);
270
10.0k
            fxl = YMULT_QUO(ysl, l);
271
10.0k
            l.x += fxl;
272
10.0k
        }
273
22.6k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
12.7k
#     if !LINEAR_COLOR
277
12.7k
                if (l.di == 0 && l.df == 0) {
278
10.2k
                    rxl = fixed2int_var(l.x);
279
10.2k
                    rxr = fixed2int_var(r.x);
280
10.2k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
10.2k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
10.2k
                    goto xit;
283
10.2k
                }
284
2.53k
#     endif
285
2.53k
            r.di = 0, r.df = 0;
286
2.53k
        }
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
9.89k
        else if (dxr == dxl && fxl != 0) {
292
1.69k
            if (l.di == 0)
293
544
                r.di = 0, r.df = l.df;
294
1.15k
            else
295
1.15k
                compute_dx(&r, dxr, ysr);
296
1.69k
            if (ysr == ysl && r.h == l.h)
297
1.69k
                r.x += fxl;
298
0
            else
299
0
                r.x += YMULT_QUO(ysr, r);
300
8.19k
        } else {
301
8.19k
            compute_dx(&r, dxr, ysr);
302
8.19k
            r.x += YMULT_QUO(ysr, r);
303
8.19k
        }
304
        /* Compute one line's worth of dx/dy. */
305
12.4k
        compute_ldx(&l, ysl);
306
12.4k
        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
12.4k
        l.x += fixed_epsilon;
310
12.4k
        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
12.4k
#define rational_floor(tl)\
338
12.4k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
12.4k
#define STEP_LINE(ix, tl)\
340
12.4k
  tl.x += tl.ldi;\
341
12.4k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
12.4k
  ix = rational_floor(tl)
343
344
12.4k
        rxl = rational_floor(l);
345
12.4k
        rxr = rational_floor(r);
346
12.4k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
78.6k
        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
66.2k
                register int ixl, ixr;
365
366
66.2k
                STEP_LINE(ixl, l);
367
66.2k
                STEP_LINE(ixr, r);
368
66.2k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
66.2k
                if (ixl != rxl || ixr != rxr) {
370
15.4k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
15.4k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
15.4k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
15.4k
                    if (code < 0)
374
0
                        goto xit;
375
15.4k
                    rxl = ixl, rxr = ixr, ry = iy;
376
15.4k
                }
377
66.2k
#     endif
378
66.2k
        }
379
12.4k
# if !LINEAR_COLOR
380
12.4k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
12.4k
#undef STEP_LINE
385
12.4k
#undef SET_MINIMAL_WIDTH
386
12.4k
#undef CONNECT_RECTANGLES
387
12.4k
#undef FILL_TRAP_RECT
388
12.4k
#undef FILL_TRAP_RECT_DIRECT
389
12.4k
#undef FILL_TRAP_RECT_INRECT
390
12.4k
#undef YMULT_QUO
391
22.6k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
22.6k
        return_if_interrupt(dev->memory);
394
22.6k
        return code;
395
22.6k
    }
396
22.6k
}
gdevddrw.c:gx_fill_trapezoid_ns_fd
Line
Count
Source
137
57.6k
{
138
57.6k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
57.6k
    const fixed ymax = fixed_pixround(ytop);
140
141
57.6k
    if (ymin >= ymax)
142
0
        return 0;   /* no scan lines to sample */
143
57.6k
    {
144
57.6k
        int iy = fixed2int_var(ymin);
145
57.6k
        const int iy1 = fixed2int_var(ymax);
146
57.6k
        trap_line l, r;
147
57.6k
        register int rxl, rxr;
148
57.6k
#if !LINEAR_COLOR
149
57.6k
        int ry;
150
57.6k
#endif
151
57.6k
        const fixed
152
57.6k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
57.6k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
57.6k
        const fixed /* partial pixel offset to first line to sample */
155
57.6k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
57.6k
        fixed fxl;
157
57.6k
        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
57.6k
            gx_color_index cindex = pdevc->colors.pure;
178
57.6k
            dev_proc_fill_rectangle((*fill_rect)) =
179
57.6k
                dev_proc(dev, fill_rectangle);
180
57.6k
# endif
181
182
57.6k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
57.6k
        l.h = left->end.y - left->start.y;
185
57.6k
        if (l.h == 0)
186
0
           return 0;
187
57.6k
        r.h = right->end.y - right->start.y;
188
57.6k
        if (r.h == 0)
189
0
           return 0;
190
57.6k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
57.6k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
57.6k
#if !LINEAR_COLOR
193
57.6k
        ry = iy;
194
57.6k
#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
57.6k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
57.6k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
57.6k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
57.6k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
57.6k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
57.6k
   (*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
57.6k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
57.6k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
57.6k
#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
57.6k
#define YMULT_QUO(ys, tl)\
228
57.6k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
57.6k
   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
57.6k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
57.6k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
57.6k
#endif
264
57.6k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
34.9k
            l.di = 0, l.df = 0;
267
34.9k
            fxl = 0;
268
34.9k
        } else {
269
22.6k
            compute_dx(&l, dxl, ysl);
270
22.6k
            fxl = YMULT_QUO(ysl, l);
271
22.6k
            l.x += fxl;
272
22.6k
        }
273
57.6k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
32.7k
#     if !LINEAR_COLOR
277
32.7k
                if (l.di == 0 && l.df == 0) {
278
30.6k
                    rxl = fixed2int_var(l.x);
279
30.6k
                    rxr = fixed2int_var(r.x);
280
30.6k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
30.6k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
30.6k
                    goto xit;
283
30.6k
                }
284
2.16k
#     endif
285
2.16k
            r.di = 0, r.df = 0;
286
2.16k
        }
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
24.8k
        else if (dxr == dxl && fxl != 0) {
292
329
            if (l.di == 0)
293
0
                r.di = 0, r.df = l.df;
294
329
            else
295
329
                compute_dx(&r, dxr, ysr);
296
329
            if (ysr == ysl && r.h == l.h)
297
0
                r.x += fxl;
298
329
            else
299
329
                r.x += YMULT_QUO(ysr, r);
300
24.4k
        } else {
301
24.4k
            compute_dx(&r, dxr, ysr);
302
24.4k
            r.x += YMULT_QUO(ysr, r);
303
24.4k
        }
304
        /* Compute one line's worth of dx/dy. */
305
26.9k
        compute_ldx(&l, ysl);
306
26.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
26.9k
        l.x += fixed_epsilon;
310
26.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
26.9k
#define rational_floor(tl)\
338
26.9k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
26.9k
#define STEP_LINE(ix, tl)\
340
26.9k
  tl.x += tl.ldi;\
341
26.9k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
26.9k
  ix = rational_floor(tl)
343
344
26.9k
        rxl = rational_floor(l);
345
26.9k
        rxr = rational_floor(r);
346
26.9k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
726k
        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
699k
                register int ixl, ixr;
365
366
699k
                STEP_LINE(ixl, l);
367
699k
                STEP_LINE(ixr, r);
368
699k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
699k
                if (ixl != rxl || ixr != rxr) {
370
607k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
607k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
607k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
607k
                    if (code < 0)
374
0
                        goto xit;
375
607k
                    rxl = ixl, rxr = ixr, ry = iy;
376
607k
                }
377
699k
#     endif
378
699k
        }
379
26.9k
# if !LINEAR_COLOR
380
26.9k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
26.9k
#undef STEP_LINE
385
26.9k
#undef SET_MINIMAL_WIDTH
386
26.9k
#undef CONNECT_RECTANGLES
387
26.9k
#undef FILL_TRAP_RECT
388
26.9k
#undef FILL_TRAP_RECT_DIRECT
389
26.9k
#undef FILL_TRAP_RECT_INRECT
390
26.9k
#undef YMULT_QUO
391
57.6k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
57.6k
        return_if_interrupt(dev->memory);
394
57.6k
        return code;
395
57.6k
    }
396
57.6k
}
gdevddrw.c:gx_fill_trapezoid_ns_nd
Line
Count
Source
137
360k
{
138
360k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
360k
    const fixed ymax = fixed_pixround(ytop);
140
141
360k
    if (ymin >= ymax)
142
50.9k
        return 0;    /* no scan lines to sample */
143
309k
    {
144
309k
        int iy = fixed2int_var(ymin);
145
309k
        const int iy1 = fixed2int_var(ymax);
146
309k
        trap_line l, r;
147
309k
        register int rxl, rxr;
148
309k
#if !LINEAR_COLOR
149
309k
        int ry;
150
309k
#endif
151
309k
        const fixed
152
309k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
309k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
309k
        const fixed /* partial pixel offset to first line to sample */
155
309k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
309k
        fixed fxl;
157
309k
        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
309k
            gx_color_index cindex = pdevc->colors.pure;
178
309k
            dev_proc_fill_rectangle((*fill_rect)) =
179
309k
                dev_proc(dev, fill_rectangle);
180
309k
# endif
181
182
309k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
309k
        l.h = left->end.y - left->start.y;
185
309k
        if (l.h == 0)
186
0
           return 0;
187
309k
        r.h = right->end.y - right->start.y;
188
309k
        if (r.h == 0)
189
0
           return 0;
190
309k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
309k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
309k
#if !LINEAR_COLOR
193
309k
        ry = iy;
194
309k
#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
309k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
309k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
309k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
309k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
309k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
309k
   (*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
309k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
309k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
309k
#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
309k
#define YMULT_QUO(ys, tl)\
228
309k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
309k
   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
309k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
309k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
309k
#endif
264
309k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
201k
            l.di = 0, l.df = 0;
267
201k
            fxl = 0;
268
201k
        } else {
269
108k
            compute_dx(&l, dxl, ysl);
270
108k
            fxl = YMULT_QUO(ysl, l);
271
108k
            l.x += fxl;
272
108k
        }
273
309k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
202k
#     if !LINEAR_COLOR
277
202k
                if (l.di == 0 && l.df == 0) {
278
189k
                    rxl = fixed2int_var(l.x);
279
189k
                    rxr = fixed2int_var(r.x);
280
189k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
189k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
189k
                    goto xit;
283
189k
                }
284
12.3k
#     endif
285
12.3k
            r.di = 0, r.df = 0;
286
12.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
107k
        else if (dxr == dxl && fxl != 0) {
292
76.1k
            if (l.di == 0)
293
2.18k
                r.di = 0, r.df = l.df;
294
73.9k
            else
295
73.9k
                compute_dx(&r, dxr, ysr);
296
76.1k
            if (ysr == ysl && r.h == l.h)
297
72.2k
                r.x += fxl;
298
3.91k
            else
299
3.91k
                r.x += YMULT_QUO(ysr, r);
300
76.1k
        } else {
301
30.8k
            compute_dx(&r, dxr, ysr);
302
30.8k
            r.x += YMULT_QUO(ysr, r);
303
30.8k
        }
304
        /* Compute one line's worth of dx/dy. */
305
119k
        compute_ldx(&l, ysl);
306
119k
        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
119k
        l.x += fixed_epsilon;
310
119k
        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
119k
#define rational_floor(tl)\
338
119k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
119k
#define STEP_LINE(ix, tl)\
340
119k
  tl.x += tl.ldi;\
341
119k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
119k
  ix = rational_floor(tl)
343
344
119k
        rxl = rational_floor(l);
345
119k
        rxr = rational_floor(r);
346
119k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
939k
        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
819k
                register int ixl, ixr;
365
366
819k
                STEP_LINE(ixl, l);
367
819k
                STEP_LINE(ixr, r);
368
819k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
819k
                if (ixl != rxl || ixr != rxr) {
370
451k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
451k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
451k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
451k
                    if (code < 0)
374
0
                        goto xit;
375
451k
                    rxl = ixl, rxr = ixr, ry = iy;
376
451k
                }
377
819k
#     endif
378
819k
        }
379
119k
# if !LINEAR_COLOR
380
119k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
119k
#undef STEP_LINE
385
119k
#undef SET_MINIMAL_WIDTH
386
119k
#undef CONNECT_RECTANGLES
387
119k
#undef FILL_TRAP_RECT
388
119k
#undef FILL_TRAP_RECT_DIRECT
389
119k
#undef FILL_TRAP_RECT_INRECT
390
119k
#undef YMULT_QUO
391
309k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
309k
        return_if_interrupt(dev->memory);
394
309k
        return code;
395
309k
    }
396
309k
}
gdevddrw.c:gx_fill_trapezoid_as_lc
Line
Count
Source
137
115k
{
138
115k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
115k
    const fixed ymax = fixed_pixround(ytop);
140
141
115k
    if (ymin >= ymax)
142
1.37k
        return 0;    /* no scan lines to sample */
143
114k
    {
144
114k
        int iy = fixed2int_var(ymin);
145
114k
        const int iy1 = fixed2int_var(ymax);
146
114k
        trap_line l, r;
147
114k
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
114k
        const fixed
152
114k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
114k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
114k
        const fixed /* partial pixel offset to first line to sample */
155
114k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
114k
        fixed fxl;
157
114k
        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
114k
# if LINEAR_COLOR
165
114k
            int num_components = dev->color_info.num_components;
166
114k
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
114k
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
114k
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
114k
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
114k
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
114k
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
114k
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
114k
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
114k
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
114k
            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
114k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
114k
        l.h = left->end.y - left->start.y;
185
114k
        if (l.h == 0)
186
0
           return 0;
187
114k
        r.h = right->end.y - right->start.y;
188
114k
        if (r.h == 0)
189
0
           return 0;
190
114k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
114k
        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
114k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
114k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
114k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
114k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
114k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
114k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
114k
#if LINEAR_COLOR
210
114k
#   define FILL_TRAP_RECT(x,y,w,h)\
211
114k
        (!(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
114k
#define YMULT_QUO(ys, tl)\
228
114k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
114k
   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
114k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
114k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
114k
#endif
264
114k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
55.9k
            l.di = 0, l.df = 0;
267
55.9k
            fxl = 0;
268
58.1k
        } else {
269
58.1k
            compute_dx(&l, dxl, ysl);
270
58.1k
            fxl = YMULT_QUO(ysl, l);
271
58.1k
            l.x += fxl;
272
58.1k
        }
273
114k
        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
55.1k
            r.di = 0, r.df = 0;
286
55.1k
        }
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
58.8k
        else if (dxr == dxl && fxl != 0) {
292
32.2k
            if (l.di == 0)
293
17.1k
                r.di = 0, r.df = l.df;
294
15.0k
            else
295
15.0k
                compute_dx(&r, dxr, ysr);
296
32.2k
            if (ysr == ysl && r.h == l.h)
297
32.2k
                r.x += fxl;
298
0
            else
299
0
                r.x += YMULT_QUO(ysr, r);
300
32.2k
        } else {
301
26.6k
            compute_dx(&r, dxr, ysr);
302
26.6k
            r.x += YMULT_QUO(ysr, r);
303
26.6k
        }
304
        /* Compute one line's worth of dx/dy. */
305
114k
        compute_ldx(&l, ysl);
306
114k
        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
114k
        l.x += fixed_epsilon;
310
114k
        r.x += fixed_epsilon;
311
114k
# 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
114k
            lg.c = lgc;
320
114k
            lg.f = lgf;
321
114k
            lg.num = lgnum;
322
114k
            rg.c = rgc;
323
114k
            rg.f = rgf;
324
114k
            rg.num = rgnum;
325
114k
            xg.c = xgc;
326
114k
            xg.f = xgf;
327
114k
            xg.num = xgnum;
328
114k
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
114k
            if (code < 0)
330
0
                return code;
331
114k
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
114k
            if (code < 0)
333
0
                return code;
334
335
114k
# endif
336
337
114k
#define rational_floor(tl)\
338
114k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
114k
#define STEP_LINE(ix, tl)\
340
114k
  tl.x += tl.ldi;\
341
114k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
114k
  ix = rational_floor(tl)
343
344
114k
        rxl = rational_floor(l);
345
114k
        rxr = rational_floor(r);
346
114k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
1.65M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
1.65M
#     if LINEAR_COLOR
349
1.65M
                if (rxl != rxr) {
350
1.49M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
1.49M
                    if (code < 0)
352
0
                        goto xit;
353
1.49M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
1.49M
                    if (code < 0)
355
0
                        goto xit;
356
1.49M
                }
357
1.65M
                if (++iy == iy1)
358
114k
                    break;
359
1.53M
                STEP_LINE(rxl, l);
360
1.53M
                STEP_LINE(rxr, r);
361
1.53M
                step_gradient(&lg, num_components);
362
1.53M
                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
1.53M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
114k
            code = 0;
383
114k
# endif
384
114k
#undef STEP_LINE
385
114k
#undef SET_MINIMAL_WIDTH
386
114k
#undef CONNECT_RECTANGLES
387
114k
#undef FILL_TRAP_RECT
388
114k
#undef FILL_TRAP_RECT_DIRECT
389
114k
#undef FILL_TRAP_RECT_INRECT
390
114k
#undef YMULT_QUO
391
114k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
114k
        return_if_interrupt(dev->memory);
394
114k
        return code;
395
114k
    }
396
114k
}
gdevddrw.c:gx_fill_trapezoid_ns_lc
Line
Count
Source
137
3.51M
{
138
3.51M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
3.51M
    const fixed ymax = fixed_pixround(ytop);
140
141
3.51M
    if (ymin >= ymax)
142
1.52M
        return 0;    /* no scan lines to sample */
143
1.99M
    {
144
1.99M
        int iy = fixed2int_var(ymin);
145
1.99M
        const int iy1 = fixed2int_var(ymax);
146
1.99M
        trap_line l, r;
147
1.99M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
1.99M
        const fixed
152
1.99M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
1.99M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
1.99M
        const fixed /* partial pixel offset to first line to sample */
155
1.99M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
1.99M
        fixed fxl;
157
1.99M
        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.99M
# if LINEAR_COLOR
165
1.99M
            int num_components = dev->color_info.num_components;
166
1.99M
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
1.99M
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
1.99M
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
1.99M
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
1.99M
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
1.99M
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
1.99M
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
1.99M
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
1.99M
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
1.99M
            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.99M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
1.99M
        l.h = left->end.y - left->start.y;
185
1.99M
        if (l.h == 0)
186
0
           return 0;
187
1.99M
        r.h = right->end.y - right->start.y;
188
1.99M
        if (r.h == 0)
189
0
           return 0;
190
1.99M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
1.99M
        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.99M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
1.99M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
1.99M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
1.99M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
1.99M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
1.99M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
1.99M
#if LINEAR_COLOR
210
1.99M
#   define FILL_TRAP_RECT(x,y,w,h)\
211
1.99M
        (!(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.99M
#define YMULT_QUO(ys, tl)\
228
1.99M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
1.99M
   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.99M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
1.99M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
1.99M
#endif
264
1.99M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
353k
            l.di = 0, l.df = 0;
267
353k
            fxl = 0;
268
1.63M
        } else {
269
1.63M
            compute_dx(&l, dxl, ysl);
270
1.63M
            fxl = YMULT_QUO(ysl, l);
271
1.63M
            l.x += fxl;
272
1.63M
        }
273
1.99M
        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
351k
            r.di = 0, r.df = 0;
286
351k
        }
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.63M
        else if (dxr == dxl && fxl != 0) {
292
110k
            if (l.di == 0)
293
25.9k
                r.di = 0, r.df = l.df;
294
84.4k
            else
295
84.4k
                compute_dx(&r, dxr, ysr);
296
110k
            if (ysr == ysl && r.h == l.h)
297
72.4k
                r.x += fxl;
298
37.9k
            else
299
37.9k
                r.x += YMULT_QUO(ysr, r);
300
1.52M
        } else {
301
1.52M
            compute_dx(&r, dxr, ysr);
302
1.52M
            r.x += YMULT_QUO(ysr, r);
303
1.52M
        }
304
        /* Compute one line's worth of dx/dy. */
305
1.99M
        compute_ldx(&l, ysl);
306
1.99M
        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.99M
        l.x += fixed_epsilon;
310
1.99M
        r.x += fixed_epsilon;
311
1.99M
# 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.99M
            lg.c = lgc;
320
1.99M
            lg.f = lgf;
321
1.99M
            lg.num = lgnum;
322
1.99M
            rg.c = rgc;
323
1.99M
            rg.f = rgf;
324
1.99M
            rg.num = rgnum;
325
1.99M
            xg.c = xgc;
326
1.99M
            xg.f = xgf;
327
1.99M
            xg.num = xgnum;
328
1.99M
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
1.99M
            if (code < 0)
330
0
                return code;
331
1.99M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
1.99M
            if (code < 0)
333
0
                return code;
334
335
1.99M
# endif
336
337
1.99M
#define rational_floor(tl)\
338
1.99M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
1.99M
#define STEP_LINE(ix, tl)\
340
1.99M
  tl.x += tl.ldi;\
341
1.99M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
1.99M
  ix = rational_floor(tl)
343
344
1.99M
        rxl = rational_floor(l);
345
1.99M
        rxr = rational_floor(r);
346
1.99M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
14.7M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
14.7M
#     if LINEAR_COLOR
349
14.7M
                if (rxl != rxr) {
350
4.37M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
4.37M
                    if (code < 0)
352
0
                        goto xit;
353
4.37M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
4.37M
                    if (code < 0)
355
0
                        goto xit;
356
4.37M
                }
357
14.7M
                if (++iy == iy1)
358
1.99M
                    break;
359
12.7M
                STEP_LINE(rxl, l);
360
12.7M
                STEP_LINE(rxr, r);
361
12.7M
                step_gradient(&lg, num_components);
362
12.7M
                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
12.7M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
1.99M
            code = 0;
383
1.99M
# endif
384
1.99M
#undef STEP_LINE
385
1.99M
#undef SET_MINIMAL_WIDTH
386
1.99M
#undef CONNECT_RECTANGLES
387
1.99M
#undef FILL_TRAP_RECT
388
1.99M
#undef FILL_TRAP_RECT_DIRECT
389
1.99M
#undef FILL_TRAP_RECT_INRECT
390
1.99M
#undef YMULT_QUO
391
1.99M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
1.99M
        return_if_interrupt(dev->memory);
394
1.99M
        return code;
395
1.99M
    }
396
1.99M
}
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