Coverage Report

Created: 2025-06-10 07:15

/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
14.3M
{
138
14.3M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
14.3M
    const fixed ymax = fixed_pixround(ytop);
140
141
14.3M
    if (ymin >= ymax)
142
4.78M
        return 0;    /* no scan lines to sample */
143
9.56M
    {
144
9.56M
        int iy = fixed2int_var(ymin);
145
9.56M
        const int iy1 = fixed2int_var(ymax);
146
9.56M
        trap_line l, r;
147
9.56M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
9.56M
        const fixed
152
9.56M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
9.56M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
9.56M
        const fixed /* partial pixel offset to first line to sample */
155
9.56M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
9.56M
        fixed fxl;
157
9.56M
        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.59M
            dev_proc_fill_rectangle((*fill_rect)) =
179
1.59M
                dev_proc(dev, fill_rectangle);
180
# endif
181
182
9.56M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
9.56M
        l.h = left->end.y - left->start.y;
185
9.56M
        if (l.h == 0)
186
0
           return 0;
187
9.56M
        r.h = right->end.y - right->start.y;
188
9.56M
        if (r.h == 0)
189
0
           return 0;
190
9.56M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
9.56M
        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
9.56M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
9.56M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
1.95M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
9.56M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
9.56M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
4.24M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
23.3M
        (!(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
6.20M
        (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
9.56M
#define YMULT_QUO(ys, tl)\
228
11.9M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
11.9M
   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
19.6M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
9.21M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
#endif
264
9.56M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
3.19M
            l.di = 0, l.df = 0;
267
3.19M
            fxl = 0;
268
6.37M
        } else {
269
6.37M
            compute_dx(&l, dxl, ysl);
270
6.37M
            fxl = YMULT_QUO(ysl, l);
271
6.37M
            l.x += fxl;
272
6.37M
        }
273
9.56M
        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
1.05M
                if (l.di == 0 && l.df == 0) {
278
990k
                    rxl = fixed2int_var(l.x);
279
990k
                    rxr = fixed2int_var(r.x);
280
990k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
990k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
990k
                    goto xit;
283
990k
                }
284
67.7k
#     endif
285
67.7k
            r.di = 0, r.df = 0;
286
67.7k
        }
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
6.38M
        else if (dxr == dxl && fxl != 0) {
292
1.01M
            if (l.di == 0)
293
455k
                r.di = 0, r.df = l.df;
294
557k
            else
295
557k
                compute_dx(&r, dxr, ysr);
296
1.01M
            if (ysr == ysl && r.h == l.h)
297
780k
                r.x += fxl;
298
231k
            else
299
231k
                r.x += YMULT_QUO(ysr, r);
300
5.37M
        } else {
301
5.37M
            compute_dx(&r, dxr, ysr);
302
5.37M
            r.x += YMULT_QUO(ysr, r);
303
5.37M
        }
304
        /* Compute one line's worth of dx/dy. */
305
605k
        compute_ldx(&l, ysl);
306
605k
        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
8.57M
        l.x += fixed_epsilon;
310
8.57M
        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
7.96M
            if (code < 0)
330
0
                return code;
331
7.96M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
7.96M
            if (code < 0)
333
0
                return code;
334
335
7.96M
# endif
336
337
7.96M
#define rational_floor(tl)\
338
235M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
7.96M
#define STEP_LINE(ix, tl)\
340
210M
  tl.x += tl.ldi;\
341
210M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
218M
  ix = rational_floor(tl)
343
344
8.57M
        rxl = rational_floor(l);
345
8.57M
        rxr = rational_floor(r);
346
8.57M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
113M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
103M
                if (rxl != rxr) {
350
23.3M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
23.3M
                    if (code < 0)
352
0
                        goto xit;
353
23.3M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
23.3M
                    if (code < 0)
355
0
                        goto xit;
356
23.3M
                }
357
103M
                if (++iy == iy1)
358
7.96M
                    break;
359
95.0M
                STEP_LINE(rxl, l);
360
95.0M
                STEP_LINE(rxr, r);
361
95.0M
                step_gradient(&lg, num_components);
362
95.0M
                step_gradient(&rg, num_components);
363
#     else
364
                register int ixl, ixr;
365
366
10.0M
                STEP_LINE(ixl, l);
367
10.0M
                STEP_LINE(ixr, r);
368
10.0M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
10.0M
                if (ixl != rxl || ixr != rxr) {
370
4.60M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
4.60M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
4.60M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
4.60M
                    if (code < 0)
374
0
                        goto xit;
375
4.60M
                    rxl = ixl, rxr = ixr, ry = iy;
376
4.60M
                }
377
#     endif
378
95.0M
        }
379
# if !LINEAR_COLOR
380
605k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
7.96M
            code = 0;
383
7.96M
# endif
384
7.96M
#undef STEP_LINE
385
7.96M
#undef SET_MINIMAL_WIDTH
386
7.96M
#undef CONNECT_RECTANGLES
387
7.96M
#undef FILL_TRAP_RECT
388
7.96M
#undef FILL_TRAP_RECT_DIRECT
389
7.96M
#undef FILL_TRAP_RECT_INRECT
390
7.96M
#undef YMULT_QUO
391
9.56M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
9.56M
        return_if_interrupt(dev->memory);
394
9.56M
        return code;
395
9.56M
    }
396
9.56M
}
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
71.6k
{
138
71.6k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
71.6k
    const fixed ymax = fixed_pixround(ytop);
140
141
71.6k
    if (ymin >= ymax)
142
5.15k
        return 0;    /* no scan lines to sample */
143
66.5k
    {
144
66.5k
        int iy = fixed2int_var(ymin);
145
66.5k
        const int iy1 = fixed2int_var(ymax);
146
66.5k
        trap_line l, r;
147
66.5k
        register int rxl, rxr;
148
66.5k
#if !LINEAR_COLOR
149
66.5k
        int ry;
150
66.5k
#endif
151
66.5k
        const fixed
152
66.5k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
66.5k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
66.5k
        const fixed /* partial pixel offset to first line to sample */
155
66.5k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
66.5k
        fixed fxl;
157
66.5k
        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
66.5k
            gx_color_index cindex = pdevc->colors.pure;
178
66.5k
            dev_proc_fill_rectangle((*fill_rect)) =
179
66.5k
                dev_proc(dev, fill_rectangle);
180
66.5k
# endif
181
182
66.5k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
66.5k
        l.h = left->end.y - left->start.y;
185
66.5k
        if (l.h == 0)
186
0
           return 0;
187
66.5k
        r.h = right->end.y - right->start.y;
188
66.5k
        if (r.h == 0)
189
0
           return 0;
190
66.5k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
66.5k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
66.5k
#if !LINEAR_COLOR
193
66.5k
        ry = iy;
194
66.5k
#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
66.5k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
66.5k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
66.5k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
66.5k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
66.5k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
66.5k
   (*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
66.5k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
66.5k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
66.5k
#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
66.5k
#define YMULT_QUO(ys, tl)\
228
66.5k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
66.5k
   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
66.5k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
66.5k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
66.5k
#endif
264
66.5k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
39.5k
            l.di = 0, l.df = 0;
267
39.5k
            fxl = 0;
268
39.5k
        } else {
269
26.9k
            compute_dx(&l, dxl, ysl);
270
26.9k
            fxl = YMULT_QUO(ysl, l);
271
26.9k
            l.x += fxl;
272
26.9k
        }
273
66.5k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
40.0k
#     if !LINEAR_COLOR
277
40.0k
                if (l.di == 0 && l.df == 0) {
278
35.2k
                    rxl = fixed2int_var(l.x);
279
35.2k
                    rxr = fixed2int_var(r.x);
280
35.2k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
35.2k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
35.2k
                    goto xit;
283
35.2k
                }
284
4.85k
#     endif
285
4.85k
            r.di = 0, r.df = 0;
286
4.85k
        }
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
26.4k
        else if (dxr == dxl && fxl != 0) {
292
6.30k
            if (l.di == 0)
293
3.18k
                r.di = 0, r.df = l.df;
294
3.12k
            else
295
3.12k
                compute_dx(&r, dxr, ysr);
296
6.30k
            if (ysr == ysl && r.h == l.h)
297
6.29k
                r.x += fxl;
298
8
            else
299
8
                r.x += YMULT_QUO(ysr, r);
300
20.1k
        } else {
301
20.1k
            compute_dx(&r, dxr, ysr);
302
20.1k
            r.x += YMULT_QUO(ysr, r);
303
20.1k
        }
304
        /* Compute one line's worth of dx/dy. */
305
31.2k
        compute_ldx(&l, ysl);
306
31.2k
        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
31.2k
        l.x += fixed_epsilon;
310
31.2k
        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
31.2k
#define rational_floor(tl)\
338
31.2k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
31.2k
#define STEP_LINE(ix, tl)\
340
31.2k
  tl.x += tl.ldi;\
341
31.2k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
31.2k
  ix = rational_floor(tl)
343
344
31.2k
        rxl = rational_floor(l);
345
31.2k
        rxr = rational_floor(r);
346
31.2k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
3.41M
        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
3.38M
                register int ixl, ixr;
365
366
3.38M
                STEP_LINE(ixl, l);
367
3.38M
                STEP_LINE(ixr, r);
368
3.38M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
3.38M
                if (ixl != rxl || ixr != rxr) {
370
223k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
223k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
223k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
223k
                    if (code < 0)
374
0
                        goto xit;
375
223k
                    rxl = ixl, rxr = ixr, ry = iy;
376
223k
                }
377
3.38M
#     endif
378
3.38M
        }
379
31.2k
# if !LINEAR_COLOR
380
31.2k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
31.2k
#undef STEP_LINE
385
31.2k
#undef SET_MINIMAL_WIDTH
386
31.2k
#undef CONNECT_RECTANGLES
387
31.2k
#undef FILL_TRAP_RECT
388
31.2k
#undef FILL_TRAP_RECT_DIRECT
389
31.2k
#undef FILL_TRAP_RECT_INRECT
390
31.2k
#undef YMULT_QUO
391
66.5k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
66.5k
        return_if_interrupt(dev->memory);
394
66.5k
        return code;
395
66.5k
    }
396
66.5k
}
Unexecuted instantiation: gdevddrw.c:gx_fill_trapezoid_as_nd
gdevddrw.c:gx_fill_trapezoid_ns_fd
Line
Count
Source
137
1.55M
{
138
1.55M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
1.55M
    const fixed ymax = fixed_pixround(ytop);
140
141
1.55M
    if (ymin >= ymax)
142
150k
        return 0;    /* no scan lines to sample */
143
1.40M
    {
144
1.40M
        int iy = fixed2int_var(ymin);
145
1.40M
        const int iy1 = fixed2int_var(ymax);
146
1.40M
        trap_line l, r;
147
1.40M
        register int rxl, rxr;
148
1.40M
#if !LINEAR_COLOR
149
1.40M
        int ry;
150
1.40M
#endif
151
1.40M
        const fixed
152
1.40M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
1.40M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
1.40M
        const fixed /* partial pixel offset to first line to sample */
155
1.40M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
1.40M
        fixed fxl;
157
1.40M
        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.40M
            gx_color_index cindex = pdevc->colors.pure;
178
1.40M
            dev_proc_fill_rectangle((*fill_rect)) =
179
1.40M
                dev_proc(dev, fill_rectangle);
180
1.40M
# endif
181
182
1.40M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
1.40M
        l.h = left->end.y - left->start.y;
185
1.40M
        if (l.h == 0)
186
0
           return 0;
187
1.40M
        r.h = right->end.y - right->start.y;
188
1.40M
        if (r.h == 0)
189
0
           return 0;
190
1.40M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
1.40M
        r.x = x0r + (fixed_half - fixed_epsilon);
192
1.40M
#if !LINEAR_COLOR
193
1.40M
        ry = iy;
194
1.40M
#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.40M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
1.40M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
1.40M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
1.40M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
1.40M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
1.40M
   (*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.40M
#   define FILL_TRAP_RECT(x,y,w,h)\
214
1.40M
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
1.40M
#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.40M
#define YMULT_QUO(ys, tl)\
228
1.40M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
1.40M
   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.40M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
1.40M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
1.40M
#endif
264
1.40M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
969k
            l.di = 0, l.df = 0;
267
969k
            fxl = 0;
268
969k
        } else {
269
436k
            compute_dx(&l, dxl, ysl);
270
436k
            fxl = YMULT_QUO(ysl, l);
271
436k
            l.x += fxl;
272
436k
        }
273
1.40M
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
967k
#     if !LINEAR_COLOR
277
967k
                if (l.di == 0 && l.df == 0) {
278
915k
                    rxl = fixed2int_var(l.x);
279
915k
                    rxr = fixed2int_var(r.x);
280
915k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
915k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
915k
                    goto xit;
283
915k
                }
284
52.0k
#     endif
285
52.0k
            r.di = 0, r.df = 0;
286
52.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
438k
        else if (dxr == dxl && fxl != 0) {
292
212k
            if (l.di == 0)
293
106k
                r.di = 0, r.df = l.df;
294
106k
            else
295
106k
                compute_dx(&r, dxr, ysr);
296
212k
            if (ysr == ysl && r.h == l.h)
297
139k
                r.x += fxl;
298
73.1k
            else
299
73.1k
                r.x += YMULT_QUO(ysr, r);
300
226k
        } else {
301
226k
            compute_dx(&r, dxr, ysr);
302
226k
            r.x += YMULT_QUO(ysr, r);
303
226k
        }
304
        /* Compute one line's worth of dx/dy. */
305
490k
        compute_ldx(&l, ysl);
306
490k
        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
490k
        l.x += fixed_epsilon;
310
490k
        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
490k
#define rational_floor(tl)\
338
490k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
490k
#define STEP_LINE(ix, tl)\
340
490k
  tl.x += tl.ldi;\
341
490k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
490k
  ix = rational_floor(tl)
343
344
490k
        rxl = rational_floor(l);
345
490k
        rxr = rational_floor(r);
346
490k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
4.31M
        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
3.82M
                register int ixl, ixr;
365
366
3.82M
                STEP_LINE(ixl, l);
367
3.82M
                STEP_LINE(ixr, r);
368
3.82M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
3.82M
                if (ixl != rxl || ixr != rxr) {
370
2.54M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
2.54M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
2.54M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
2.54M
                    if (code < 0)
374
0
                        goto xit;
375
2.54M
                    rxl = ixl, rxr = ixr, ry = iy;
376
2.54M
                }
377
3.82M
#     endif
378
3.82M
        }
379
490k
# if !LINEAR_COLOR
380
490k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
490k
#undef STEP_LINE
385
490k
#undef SET_MINIMAL_WIDTH
386
490k
#undef CONNECT_RECTANGLES
387
490k
#undef FILL_TRAP_RECT
388
490k
#undef FILL_TRAP_RECT_DIRECT
389
490k
#undef FILL_TRAP_RECT_INRECT
390
490k
#undef YMULT_QUO
391
1.40M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
1.40M
        return_if_interrupt(dev->memory);
394
1.40M
        return code;
395
1.40M
    }
396
1.40M
}
gdevddrw.c:gx_fill_trapezoid_ns_nd
Line
Count
Source
137
123k
{
138
123k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
123k
    const fixed ymax = fixed_pixround(ytop);
140
141
123k
    if (ymin >= ymax)
142
1
        return 0;    /* no scan lines to sample */
143
123k
    {
144
123k
        int iy = fixed2int_var(ymin);
145
123k
        const int iy1 = fixed2int_var(ymax);
146
123k
        trap_line l, r;
147
123k
        register int rxl, rxr;
148
123k
#if !LINEAR_COLOR
149
123k
        int ry;
150
123k
#endif
151
123k
        const fixed
152
123k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
123k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
123k
        const fixed /* partial pixel offset to first line to sample */
155
123k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
123k
        fixed fxl;
157
123k
        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
123k
            gx_color_index cindex = pdevc->colors.pure;
178
123k
            dev_proc_fill_rectangle((*fill_rect)) =
179
123k
                dev_proc(dev, fill_rectangle);
180
123k
# endif
181
182
123k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
123k
        l.h = left->end.y - left->start.y;
185
123k
        if (l.h == 0)
186
0
           return 0;
187
123k
        r.h = right->end.y - right->start.y;
188
123k
        if (r.h == 0)
189
0
           return 0;
190
123k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
123k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
123k
#if !LINEAR_COLOR
193
123k
        ry = iy;
194
123k
#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
123k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
123k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
123k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
123k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
123k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
123k
   (*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
123k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
123k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
123k
#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
123k
#define YMULT_QUO(ys, tl)\
228
123k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
123k
   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
123k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
123k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
123k
#endif
264
123k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
57.3k
            l.di = 0, l.df = 0;
267
57.3k
            fxl = 0;
268
65.6k
        } else {
269
65.6k
            compute_dx(&l, dxl, ysl);
270
65.6k
            fxl = YMULT_QUO(ysl, l);
271
65.6k
            l.x += fxl;
272
65.6k
        }
273
123k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
50.4k
#     if !LINEAR_COLOR
277
50.4k
                if (l.di == 0 && l.df == 0) {
278
39.6k
                    rxl = fixed2int_var(l.x);
279
39.6k
                    rxr = fixed2int_var(r.x);
280
39.6k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
39.6k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
39.6k
                    goto xit;
283
39.6k
                }
284
10.8k
#     endif
285
10.8k
            r.di = 0, r.df = 0;
286
10.8k
        }
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
72.5k
        else if (dxr == dxl && fxl != 0) {
292
8
            if (l.di == 0)
293
0
                r.di = 0, r.df = l.df;
294
8
            else
295
8
                compute_dx(&r, dxr, ysr);
296
8
            if (ysr == ysl && r.h == l.h)
297
0
                r.x += fxl;
298
8
            else
299
8
                r.x += YMULT_QUO(ysr, r);
300
72.5k
        } else {
301
72.5k
            compute_dx(&r, dxr, ysr);
302
72.5k
            r.x += YMULT_QUO(ysr, r);
303
72.5k
        }
304
        /* Compute one line's worth of dx/dy. */
305
83.3k
        compute_ldx(&l, ysl);
306
83.3k
        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
83.3k
        l.x += fixed_epsilon;
310
83.3k
        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
83.3k
#define rational_floor(tl)\
338
83.3k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
83.3k
#define STEP_LINE(ix, tl)\
340
83.3k
  tl.x += tl.ldi;\
341
83.3k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
83.3k
  ix = rational_floor(tl)
343
344
83.3k
        rxl = rational_floor(l);
345
83.3k
        rxr = rational_floor(r);
346
83.3k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
2.92M
        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
2.84M
                register int ixl, ixr;
365
366
2.84M
                STEP_LINE(ixl, l);
367
2.84M
                STEP_LINE(ixr, r);
368
2.84M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
2.84M
                if (ixl != rxl || ixr != rxr) {
370
1.83M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
1.83M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
1.83M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
1.83M
                    if (code < 0)
374
0
                        goto xit;
375
1.83M
                    rxl = ixl, rxr = ixr, ry = iy;
376
1.83M
                }
377
2.84M
#     endif
378
2.84M
        }
379
83.3k
# if !LINEAR_COLOR
380
83.3k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
83.3k
#undef STEP_LINE
385
83.3k
#undef SET_MINIMAL_WIDTH
386
83.3k
#undef CONNECT_RECTANGLES
387
83.3k
#undef FILL_TRAP_RECT
388
83.3k
#undef FILL_TRAP_RECT_DIRECT
389
83.3k
#undef FILL_TRAP_RECT_INRECT
390
83.3k
#undef YMULT_QUO
391
123k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
123k
        return_if_interrupt(dev->memory);
394
123k
        return code;
395
123k
    }
396
123k
}
gdevddrw.c:gx_fill_trapezoid_as_lc
Line
Count
Source
137
756k
{
138
756k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
756k
    const fixed ymax = fixed_pixround(ytop);
140
141
756k
    if (ymin >= ymax)
142
211k
        return 0;    /* no scan lines to sample */
143
544k
    {
144
544k
        int iy = fixed2int_var(ymin);
145
544k
        const int iy1 = fixed2int_var(ymax);
146
544k
        trap_line l, r;
147
544k
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
544k
        const fixed
152
544k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
544k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
544k
        const fixed /* partial pixel offset to first line to sample */
155
544k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
544k
        fixed fxl;
157
544k
        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
544k
# if LINEAR_COLOR
165
544k
            int num_components = dev->color_info.num_components;
166
544k
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
544k
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
544k
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
544k
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
544k
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
544k
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
544k
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
544k
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
544k
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
544k
            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
544k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
544k
        l.h = left->end.y - left->start.y;
185
544k
        if (l.h == 0)
186
0
           return 0;
187
544k
        r.h = right->end.y - right->start.y;
188
544k
        if (r.h == 0)
189
0
           return 0;
190
544k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
544k
        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
544k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
544k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
544k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
544k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
544k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
544k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
544k
#if LINEAR_COLOR
210
544k
#   define FILL_TRAP_RECT(x,y,w,h)\
211
544k
        (!(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
544k
#define YMULT_QUO(ys, tl)\
228
544k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
544k
   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
544k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
544k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
544k
#endif
264
544k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
238k
            l.di = 0, l.df = 0;
267
238k
            fxl = 0;
268
306k
        } else {
269
306k
            compute_dx(&l, dxl, ysl);
270
306k
            fxl = YMULT_QUO(ysl, l);
271
306k
            l.x += fxl;
272
306k
        }
273
544k
        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
237k
            r.di = 0, r.df = 0;
286
237k
        }
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
306k
        else if (dxr == dxl && fxl != 0) {
292
221k
            if (l.di == 0)
293
38.6k
                r.di = 0, r.df = l.df;
294
182k
            else
295
182k
                compute_dx(&r, dxr, ysr);
296
221k
            if (ysr == ysl && r.h == l.h)
297
221k
                r.x += fxl;
298
6
            else
299
6
                r.x += YMULT_QUO(ysr, r);
300
221k
        } else {
301
85.1k
            compute_dx(&r, dxr, ysr);
302
85.1k
            r.x += YMULT_QUO(ysr, r);
303
85.1k
        }
304
        /* Compute one line's worth of dx/dy. */
305
544k
        compute_ldx(&l, ysl);
306
544k
        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
544k
        l.x += fixed_epsilon;
310
544k
        r.x += fixed_epsilon;
311
544k
# 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
544k
            lg.c = lgc;
320
544k
            lg.f = lgf;
321
544k
            lg.num = lgnum;
322
544k
            rg.c = rgc;
323
544k
            rg.f = rgf;
324
544k
            rg.num = rgnum;
325
544k
            xg.c = xgc;
326
544k
            xg.f = xgf;
327
544k
            xg.num = xgnum;
328
544k
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
544k
            if (code < 0)
330
0
                return code;
331
544k
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
544k
            if (code < 0)
333
0
                return code;
334
335
544k
# endif
336
337
544k
#define rational_floor(tl)\
338
544k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
544k
#define STEP_LINE(ix, tl)\
340
544k
  tl.x += tl.ldi;\
341
544k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
544k
  ix = rational_floor(tl)
343
344
544k
        rxl = rational_floor(l);
345
544k
        rxr = rational_floor(r);
346
544k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
11.5M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
11.5M
#     if LINEAR_COLOR
349
11.5M
                if (rxl != rxr) {
350
7.57M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
7.57M
                    if (code < 0)
352
0
                        goto xit;
353
7.57M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
7.57M
                    if (code < 0)
355
0
                        goto xit;
356
7.57M
                }
357
11.5M
                if (++iy == iy1)
358
544k
                    break;
359
11.0M
                STEP_LINE(rxl, l);
360
11.0M
                STEP_LINE(rxr, r);
361
11.0M
                step_gradient(&lg, num_components);
362
11.0M
                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
11.0M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
544k
            code = 0;
383
544k
# endif
384
544k
#undef STEP_LINE
385
544k
#undef SET_MINIMAL_WIDTH
386
544k
#undef CONNECT_RECTANGLES
387
544k
#undef FILL_TRAP_RECT
388
544k
#undef FILL_TRAP_RECT_DIRECT
389
544k
#undef FILL_TRAP_RECT_INRECT
390
544k
#undef YMULT_QUO
391
544k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
544k
        return_if_interrupt(dev->memory);
394
544k
        return code;
395
544k
    }
396
544k
}
gdevddrw.c:gx_fill_trapezoid_ns_lc
Line
Count
Source
137
11.8M
{
138
11.8M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
11.8M
    const fixed ymax = fixed_pixround(ytop);
140
141
11.8M
    if (ymin >= ymax)
142
4.41M
        return 0;    /* no scan lines to sample */
143
7.42M
    {
144
7.42M
        int iy = fixed2int_var(ymin);
145
7.42M
        const int iy1 = fixed2int_var(ymax);
146
7.42M
        trap_line l, r;
147
7.42M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
7.42M
        const fixed
152
7.42M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
7.42M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
7.42M
        const fixed /* partial pixel offset to first line to sample */
155
7.42M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
7.42M
        fixed fxl;
157
7.42M
        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
7.42M
# if LINEAR_COLOR
165
7.42M
            int num_components = dev->color_info.num_components;
166
7.42M
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
7.42M
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
7.42M
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
7.42M
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
7.42M
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
7.42M
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
7.42M
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
7.42M
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
7.42M
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
7.42M
            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
7.42M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
7.42M
        l.h = left->end.y - left->start.y;
185
7.42M
        if (l.h == 0)
186
0
           return 0;
187
7.42M
        r.h = right->end.y - right->start.y;
188
7.42M
        if (r.h == 0)
189
0
           return 0;
190
7.42M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
7.42M
        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.42M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
7.42M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
7.42M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
7.42M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
7.42M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
7.42M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
7.42M
#if LINEAR_COLOR
210
7.42M
#   define FILL_TRAP_RECT(x,y,w,h)\
211
7.42M
        (!(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
7.42M
#define YMULT_QUO(ys, tl)\
228
7.42M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
7.42M
   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
7.42M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
7.42M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
7.42M
#endif
264
7.42M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
1.88M
            l.di = 0, l.df = 0;
267
1.88M
            fxl = 0;
268
5.53M
        } else {
269
5.53M
            compute_dx(&l, dxl, ysl);
270
5.53M
            fxl = YMULT_QUO(ysl, l);
271
5.53M
            l.x += fxl;
272
5.53M
        }
273
7.42M
        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.87M
            r.di = 0, r.df = 0;
286
1.87M
        }
287
        /*
288
         * The test for fxl != 0 is required because the right edge might
289
         * cross some pixel centers even if the left edge doesn't.
290
         */
291
5.54M
        else if (dxr == dxl && fxl != 0) {
292
572k
            if (l.di == 0)
293
307k
                r.di = 0, r.df = l.df;
294
264k
            else
295
264k
                compute_dx(&r, dxr, ysr);
296
572k
            if (ysr == ysl && r.h == l.h)
297
413k
                r.x += fxl;
298
158k
            else
299
158k
                r.x += YMULT_QUO(ysr, r);
300
4.97M
        } else {
301
4.97M
            compute_dx(&r, dxr, ysr);
302
4.97M
            r.x += YMULT_QUO(ysr, r);
303
4.97M
        }
304
        /* Compute one line's worth of dx/dy. */
305
7.42M
        compute_ldx(&l, ysl);
306
7.42M
        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
7.42M
        l.x += fixed_epsilon;
310
7.42M
        r.x += fixed_epsilon;
311
7.42M
# 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
7.42M
            lg.c = lgc;
320
7.42M
            lg.f = lgf;
321
7.42M
            lg.num = lgnum;
322
7.42M
            rg.c = rgc;
323
7.42M
            rg.f = rgf;
324
7.42M
            rg.num = rgnum;
325
7.42M
            xg.c = xgc;
326
7.42M
            xg.f = xgf;
327
7.42M
            xg.num = xgnum;
328
7.42M
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
7.42M
            if (code < 0)
330
0
                return code;
331
7.42M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
7.42M
            if (code < 0)
333
0
                return code;
334
335
7.42M
# endif
336
337
7.42M
#define rational_floor(tl)\
338
7.42M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
7.42M
#define STEP_LINE(ix, tl)\
340
7.42M
  tl.x += tl.ldi;\
341
7.42M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
7.42M
  ix = rational_floor(tl)
343
344
7.42M
        rxl = rational_floor(l);
345
7.42M
        rxr = rational_floor(r);
346
7.42M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
91.4M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
91.4M
#     if LINEAR_COLOR
349
91.4M
                if (rxl != rxr) {
350
15.8M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
15.8M
                    if (code < 0)
352
0
                        goto xit;
353
15.8M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
15.8M
                    if (code < 0)
355
0
                        goto xit;
356
15.8M
                }
357
91.4M
                if (++iy == iy1)
358
7.42M
                    break;
359
84.0M
                STEP_LINE(rxl, l);
360
84.0M
                STEP_LINE(rxr, r);
361
84.0M
                step_gradient(&lg, num_components);
362
84.0M
                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
84.0M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
7.42M
            code = 0;
383
7.42M
# endif
384
7.42M
#undef STEP_LINE
385
7.42M
#undef SET_MINIMAL_WIDTH
386
7.42M
#undef CONNECT_RECTANGLES
387
7.42M
#undef FILL_TRAP_RECT
388
7.42M
#undef FILL_TRAP_RECT_DIRECT
389
7.42M
#undef FILL_TRAP_RECT_INRECT
390
7.42M
#undef YMULT_QUO
391
7.42M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
7.42M
        return_if_interrupt(dev->memory);
394
7.42M
        return code;
395
7.42M
    }
396
7.42M
}
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