Coverage Report

Created: 2025-06-10 07:06

/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
3.63M
{
138
3.63M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
3.63M
    const fixed ymax = fixed_pixround(ytop);
140
141
3.63M
    if (ymin >= ymax)
142
842k
        return 0;    /* no scan lines to sample */
143
2.79M
    {
144
2.79M
        int iy = fixed2int_var(ymin);
145
2.79M
        const int iy1 = fixed2int_var(ymax);
146
2.79M
        trap_line l, r;
147
2.79M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
2.79M
        const fixed
152
2.79M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
2.79M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
2.79M
        const fixed /* partial pixel offset to first line to sample */
155
2.79M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
2.79M
        fixed fxl;
157
2.79M
        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.55M
            dev_proc_fill_rectangle((*fill_rect)) =
179
1.55M
                dev_proc(dev, fill_rectangle);
180
# endif
181
182
2.79M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
2.79M
        l.h = left->end.y - left->start.y;
185
2.79M
        if (l.h == 0)
186
0
           return 0;
187
2.79M
        r.h = right->end.y - right->start.y;
188
2.79M
        if (r.h == 0)
189
0
           return 0;
190
2.79M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
2.79M
        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.79M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
2.79M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
24.2k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
2.79M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
2.79M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
2.56M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
6.24M
        (!(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
2.59M
        (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.79M
#define YMULT_QUO(ys, tl)\
228
3.70M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
3.70M
   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.74M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
2.07M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
#endif
264
2.79M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
907k
            l.di = 0, l.df = 0;
267
907k
            fxl = 0;
268
1.88M
        } else {
269
1.88M
            compute_dx(&l, dxl, ysl);
270
1.88M
            fxl = YMULT_QUO(ysl, l);
271
1.88M
            l.x += fxl;
272
1.88M
        }
273
2.79M
        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
658k
                if (l.di == 0 && l.df == 0) {
278
343k
                    rxl = fixed2int_var(l.x);
279
343k
                    rxr = fixed2int_var(r.x);
280
343k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
343k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
343k
                    goto xit;
283
343k
                }
284
314k
#     endif
285
314k
            r.di = 0, r.df = 0;
286
314k
        }
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.89M
        else if (dxr == dxl && fxl != 0) {
292
85.1k
            if (l.di == 0)
293
26.1k
                r.di = 0, r.df = l.df;
294
58.9k
            else
295
58.9k
                compute_dx(&r, dxr, ysr);
296
85.1k
            if (ysr == ysl && r.h == l.h)
297
72.8k
                r.x += fxl;
298
12.2k
            else
299
12.2k
                r.x += YMULT_QUO(ysr, r);
300
1.80M
        } else {
301
1.80M
            compute_dx(&r, dxr, ysr);
302
1.80M
            r.x += YMULT_QUO(ysr, r);
303
1.80M
        }
304
        /* Compute one line's worth of dx/dy. */
305
1.20M
        compute_ldx(&l, ysl);
306
1.20M
        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.45M
        l.x += fixed_epsilon;
310
2.45M
        r.x += fixed_epsilon;
311
# if LINEAR_COLOR
312
#     ifdef DEBUG
313
                if (check_gradient_overflow(left, right)) {
314
                    /* The caller must care of.
315
                       Checking it here looses some performance with triangles. */
316
                    return_error(gs_error_unregistered);
317
                }
318
#     endif
319
            lg.c = lgc;
320
            lg.f = lgf;
321
            lg.num = lgnum;
322
            rg.c = rgc;
323
            rg.f = rgf;
324
            rg.num = rgnum;
325
            xg.c = xgc;
326
            xg.f = xgf;
327
            xg.num = xgnum;
328
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
1.24M
            if (code < 0)
330
0
                return code;
331
1.24M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
1.24M
            if (code < 0)
333
0
                return code;
334
335
1.24M
# endif
336
337
1.24M
#define rational_floor(tl)\
338
30.2M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
1.24M
#define STEP_LINE(ix, tl)\
340
24.0M
  tl.x += tl.ldi;\
341
24.0M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
25.3M
  ix = rational_floor(tl)
343
344
2.45M
        rxl = rational_floor(l);
345
2.45M
        rxr = rational_floor(r);
346
2.45M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
14.4M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
11.3M
                if (rxl != rxr) {
350
6.24M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
6.24M
                    if (code < 0)
352
0
                        goto xit;
353
6.24M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
6.24M
                    if (code < 0)
355
0
                        goto xit;
356
6.24M
                }
357
11.3M
                if (++iy == iy1)
358
1.24M
                    break;
359
10.0M
                STEP_LINE(rxl, l);
360
10.0M
                STEP_LINE(rxr, r);
361
10.0M
                step_gradient(&lg, num_components);
362
10.0M
                step_gradient(&rg, num_components);
363
#     else
364
                register int ixl, ixr;
365
366
1.94M
                STEP_LINE(ixl, l);
367
1.94M
                STEP_LINE(ixr, r);
368
1.94M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
1.94M
                if (ixl != rxl || ixr != rxr) {
370
1.03M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
1.03M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
1.03M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
1.03M
                    if (code < 0)
374
0
                        goto xit;
375
1.03M
                    rxl = ixl, rxr = ixr, ry = iy;
376
1.03M
                }
377
#     endif
378
10.0M
        }
379
# if !LINEAR_COLOR
380
1.20M
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
1.24M
            code = 0;
383
1.24M
# endif
384
1.24M
#undef STEP_LINE
385
1.24M
#undef SET_MINIMAL_WIDTH
386
1.24M
#undef CONNECT_RECTANGLES
387
1.24M
#undef FILL_TRAP_RECT
388
1.24M
#undef FILL_TRAP_RECT_DIRECT
389
1.24M
#undef FILL_TRAP_RECT_INRECT
390
1.24M
#undef YMULT_QUO
391
2.79M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
2.79M
        return_if_interrupt(dev->memory);
394
2.79M
        return code;
395
2.79M
    }
396
2.79M
}
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
31.8k
{
138
31.8k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
31.8k
    const fixed ymax = fixed_pixround(ytop);
140
141
31.8k
    if (ymin >= ymax)
142
3.72k
        return 0;    /* no scan lines to sample */
143
28.1k
    {
144
28.1k
        int iy = fixed2int_var(ymin);
145
28.1k
        const int iy1 = fixed2int_var(ymax);
146
28.1k
        trap_line l, r;
147
28.1k
        register int rxl, rxr;
148
28.1k
#if !LINEAR_COLOR
149
28.1k
        int ry;
150
28.1k
#endif
151
28.1k
        const fixed
152
28.1k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
28.1k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
28.1k
        const fixed /* partial pixel offset to first line to sample */
155
28.1k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
28.1k
        fixed fxl;
157
28.1k
        int code;
158
# if CONTIGUOUS_FILL
159
            const bool peak0 = ((flags & 1) != 0);
160
            const bool peak1 = ((flags & 2) != 0);
161
            int peak_y0 = ybot + fixed_half;
162
            int peak_y1 = ytop - fixed_half;
163
# endif
164
# if LINEAR_COLOR
165
            int num_components = dev->color_info.num_components;
166
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
            trap_gradient lg, rg, xg;
176
# else
177
28.1k
            gx_color_index cindex = pdevc->colors.pure;
178
28.1k
            dev_proc_fill_rectangle((*fill_rect)) =
179
28.1k
                dev_proc(dev, fill_rectangle);
180
28.1k
# endif
181
182
28.1k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
28.1k
        l.h = left->end.y - left->start.y;
185
28.1k
        if (l.h == 0)
186
0
           return 0;
187
28.1k
        r.h = right->end.y - right->start.y;
188
28.1k
        if (r.h == 0)
189
0
           return 0;
190
28.1k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
28.1k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
28.1k
#if !LINEAR_COLOR
193
28.1k
        ry = iy;
194
28.1k
#endif
195
196
/*
197
 * Free variables of FILL_TRAP_RECT:
198
 *  SWAP_AXES, pdevc, dev, fa
199
 * Free variables of FILL_TRAP_RECT_DIRECT:
200
 *  SWAP_AXES, fill_rect, dev, cindex
201
 */
202
28.1k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
28.1k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
28.1k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
28.1k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
28.1k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
28.1k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
        (!(w) ? 0 : dev_proc(dev, fill_linear_color_scanline)(dev, fa, x, y, w, xg.c, xg.f, xg.num, xg.den))
212
#else
213
28.1k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
28.1k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
28.1k
#endif
216
217
        /* Compute the dx/dy ratios. */
218
219
        /*
220
         * Compute the x offsets at the first scan line to sample.  We need
221
         * to be careful in computing ys# * dx#f {/,%} h# because the
222
         * multiplication may overflow.  We know that all the quantities
223
         * involved are non-negative, and that ys# is usually less than 1 (as
224
         * a fixed, of course); this gives us a cheap conservative check for
225
         * overflow in the multiplication.
226
         */
227
28.1k
#define YMULT_QUO(ys, tl)\
228
28.1k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
28.1k
   fixed_mult_quo(ys, tl.df, tl.h))
230
231
#if CONTIGUOUS_FILL
232
/*
233
 * If left and right boundary round to same pixel index,
234
 * we would not paing the scan and would get a dropout.
235
 * Check for this case and choose one of two pixels
236
 * which is closer to the "axis". We need to exclude
237
 * 'peak' because it would paint an excessive pixel.
238
 */
239
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) \
240
    if (ixl == ixr) \
241
        if ((!peak0 || iy >= peak_y0) && (!peak1 || iy <= peak_y1)) {\
242
            fixed x = int2fixed(ixl) + fixed_half;\
243
            if (x - l.x < r.x - x)\
244
                ++ixr;\
245
            else\
246
                --ixl;\
247
        }
248
249
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill)\
250
    if (adj1 < adj2) {\
251
        if (iy - ry > 1) {\
252
            code = fill(rxl, ry, rxr - rxl, iy - ry - 1);\
253
            if (code < 0)\
254
                goto xit;\
255
            ry = iy - 1;\
256
        }\
257
        adj1 = adj2 = (adj2 + adj2) / 2;\
258
    }
259
260
#else
261
28.1k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
28.1k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
28.1k
#endif
264
28.1k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
13.9k
            l.di = 0, l.df = 0;
267
13.9k
            fxl = 0;
268
14.1k
        } else {
269
14.1k
            compute_dx(&l, dxl, ysl);
270
14.1k
            fxl = YMULT_QUO(ysl, l);
271
14.1k
            l.x += fxl;
272
14.1k
        }
273
28.1k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
14.4k
#     if !LINEAR_COLOR
277
14.4k
                if (l.di == 0 && l.df == 0) {
278
13.3k
                    rxl = fixed2int_var(l.x);
279
13.3k
                    rxr = fixed2int_var(r.x);
280
13.3k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
13.3k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
13.3k
                    goto xit;
283
13.3k
                }
284
1.11k
#     endif
285
1.11k
            r.di = 0, r.df = 0;
286
1.11k
        }
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
13.7k
        else if (dxr == dxl && fxl != 0) {
292
9.66k
            if (l.di == 0)
293
2.40k
                r.di = 0, r.df = l.df;
294
7.26k
            else
295
7.26k
                compute_dx(&r, dxr, ysr);
296
9.66k
            if (ysr == ysl && r.h == l.h)
297
9.64k
                r.x += fxl;
298
27
            else
299
27
                r.x += YMULT_QUO(ysr, r);
300
9.66k
        } else {
301
4.04k
            compute_dx(&r, dxr, ysr);
302
4.04k
            r.x += YMULT_QUO(ysr, r);
303
4.04k
        }
304
        /* Compute one line's worth of dx/dy. */
305
14.8k
        compute_ldx(&l, ysl);
306
14.8k
        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
14.8k
        l.x += fixed_epsilon;
310
14.8k
        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
14.8k
#define rational_floor(tl)\
338
14.8k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
14.8k
#define STEP_LINE(ix, tl)\
340
14.8k
  tl.x += tl.ldi;\
341
14.8k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
14.8k
  ix = rational_floor(tl)
343
344
14.8k
        rxl = rational_floor(l);
345
14.8k
        rxr = rational_floor(r);
346
14.8k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
580k
        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
566k
                register int ixl, ixr;
365
366
566k
                STEP_LINE(ixl, l);
367
566k
                STEP_LINE(ixr, r);
368
566k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
566k
                if (ixl != rxl || ixr != rxr) {
370
143k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
143k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
143k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
143k
                    if (code < 0)
374
0
                        goto xit;
375
143k
                    rxl = ixl, rxr = ixr, ry = iy;
376
143k
                }
377
566k
#     endif
378
566k
        }
379
14.8k
# if !LINEAR_COLOR
380
14.8k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
14.8k
#undef STEP_LINE
385
14.8k
#undef SET_MINIMAL_WIDTH
386
14.8k
#undef CONNECT_RECTANGLES
387
14.8k
#undef FILL_TRAP_RECT
388
14.8k
#undef FILL_TRAP_RECT_DIRECT
389
14.8k
#undef FILL_TRAP_RECT_INRECT
390
14.8k
#undef YMULT_QUO
391
28.1k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
28.1k
        return_if_interrupt(dev->memory);
394
28.1k
        return code;
395
28.1k
    }
396
28.1k
}
Unexecuted instantiation: gdevddrw.c:gx_fill_trapezoid_as_nd
gdevddrw.c:gx_fill_trapezoid_ns_fd
Line
Count
Source
137
1.51M
{
138
1.51M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
1.51M
    const fixed ymax = fixed_pixround(ytop);
140
141
1.51M
    if (ymin >= ymax)
142
17.0k
        return 0;    /* no scan lines to sample */
143
1.50M
    {
144
1.50M
        int iy = fixed2int_var(ymin);
145
1.50M
        const int iy1 = fixed2int_var(ymax);
146
1.50M
        trap_line l, r;
147
1.50M
        register int rxl, rxr;
148
1.50M
#if !LINEAR_COLOR
149
1.50M
        int ry;
150
1.50M
#endif
151
1.50M
        const fixed
152
1.50M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
1.50M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
1.50M
        const fixed /* partial pixel offset to first line to sample */
155
1.50M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
1.50M
        fixed fxl;
157
1.50M
        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.50M
            gx_color_index cindex = pdevc->colors.pure;
178
1.50M
            dev_proc_fill_rectangle((*fill_rect)) =
179
1.50M
                dev_proc(dev, fill_rectangle);
180
1.50M
# endif
181
182
1.50M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
1.50M
        l.h = left->end.y - left->start.y;
185
1.50M
        if (l.h == 0)
186
0
           return 0;
187
1.50M
        r.h = right->end.y - right->start.y;
188
1.50M
        if (r.h == 0)
189
0
           return 0;
190
1.50M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
1.50M
        r.x = x0r + (fixed_half - fixed_epsilon);
192
1.50M
#if !LINEAR_COLOR
193
1.50M
        ry = iy;
194
1.50M
#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.50M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
1.50M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
1.50M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
1.50M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
1.50M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
1.50M
   (*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.50M
#   define FILL_TRAP_RECT(x,y,w,h)\
214
1.50M
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
1.50M
#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.50M
#define YMULT_QUO(ys, tl)\
228
1.50M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
1.50M
   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.50M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
1.50M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
1.50M
#endif
264
1.50M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
624k
            l.di = 0, l.df = 0;
267
624k
            fxl = 0;
268
878k
        } else {
269
878k
            compute_dx(&l, dxl, ysl);
270
878k
            fxl = YMULT_QUO(ysl, l);
271
878k
            l.x += fxl;
272
878k
        }
273
1.50M
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
623k
#     if !LINEAR_COLOR
277
623k
                if (l.di == 0 && l.df == 0) {
278
310k
                    rxl = fixed2int_var(l.x);
279
310k
                    rxr = fixed2int_var(r.x);
280
310k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
310k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
310k
                    goto xit;
283
310k
                }
284
313k
#     endif
285
313k
            r.di = 0, r.df = 0;
286
313k
        }
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
879k
        else if (dxr == dxl && fxl != 0) {
292
25.4k
            if (l.di == 0)
293
5.26k
                r.di = 0, r.df = l.df;
294
20.1k
            else
295
20.1k
                compute_dx(&r, dxr, ysr);
296
25.4k
            if (ysr == ysl && r.h == l.h)
297
19.9k
                r.x += fxl;
298
5.42k
            else
299
5.42k
                r.x += YMULT_QUO(ysr, r);
300
854k
        } else {
301
854k
            compute_dx(&r, dxr, ysr);
302
854k
            r.x += YMULT_QUO(ysr, r);
303
854k
        }
304
        /* Compute one line's worth of dx/dy. */
305
1.19M
        compute_ldx(&l, ysl);
306
1.19M
        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.19M
        l.x += fixed_epsilon;
310
1.19M
        r.x += fixed_epsilon;
311
# if LINEAR_COLOR
312
#     ifdef DEBUG
313
                if (check_gradient_overflow(left, right)) {
314
                    /* The caller must care of.
315
                       Checking it here looses some performance with triangles. */
316
                    return_error(gs_error_unregistered);
317
                }
318
#     endif
319
            lg.c = lgc;
320
            lg.f = lgf;
321
            lg.num = lgnum;
322
            rg.c = rgc;
323
            rg.f = rgf;
324
            rg.num = rgnum;
325
            xg.c = xgc;
326
            xg.f = xgf;
327
            xg.num = xgnum;
328
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
            if (code < 0)
330
                return code;
331
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
            if (code < 0)
333
                return code;
334
335
# endif
336
337
1.19M
#define rational_floor(tl)\
338
1.19M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
1.19M
#define STEP_LINE(ix, tl)\
340
1.19M
  tl.x += tl.ldi;\
341
1.19M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
1.19M
  ix = rational_floor(tl)
343
344
1.19M
        rxl = rational_floor(l);
345
1.19M
        rxr = rational_floor(r);
346
1.19M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
2.57M
        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
1.37M
                register int ixl, ixr;
365
366
1.37M
                STEP_LINE(ixl, l);
367
1.37M
                STEP_LINE(ixr, r);
368
1.37M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
1.37M
                if (ixl != rxl || ixr != rxr) {
370
892k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
892k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
892k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
892k
                    if (code < 0)
374
0
                        goto xit;
375
892k
                    rxl = ixl, rxr = ixr, ry = iy;
376
892k
                }
377
1.37M
#     endif
378
1.37M
        }
379
1.19M
# if !LINEAR_COLOR
380
1.19M
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
1.19M
#undef STEP_LINE
385
1.19M
#undef SET_MINIMAL_WIDTH
386
1.19M
#undef CONNECT_RECTANGLES
387
1.19M
#undef FILL_TRAP_RECT
388
1.19M
#undef FILL_TRAP_RECT_DIRECT
389
1.19M
#undef FILL_TRAP_RECT_INRECT
390
1.19M
#undef YMULT_QUO
391
1.50M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
1.50M
        return_if_interrupt(dev->memory);
394
1.50M
        return code;
395
1.50M
    }
396
1.50M
}
gdevddrw.c:gx_fill_trapezoid_ns_nd
Line
Count
Source
137
21.0k
{
138
21.0k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
21.0k
    const fixed ymax = fixed_pixround(ytop);
140
141
21.0k
    if (ymin >= ymax)
142
0
        return 0;   /* no scan lines to sample */
143
21.0k
    {
144
21.0k
        int iy = fixed2int_var(ymin);
145
21.0k
        const int iy1 = fixed2int_var(ymax);
146
21.0k
        trap_line l, r;
147
21.0k
        register int rxl, rxr;
148
21.0k
#if !LINEAR_COLOR
149
21.0k
        int ry;
150
21.0k
#endif
151
21.0k
        const fixed
152
21.0k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
21.0k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
21.0k
        const fixed /* partial pixel offset to first line to sample */
155
21.0k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
21.0k
        fixed fxl;
157
21.0k
        int code;
158
# if CONTIGUOUS_FILL
159
            const bool peak0 = ((flags & 1) != 0);
160
            const bool peak1 = ((flags & 2) != 0);
161
            int peak_y0 = ybot + fixed_half;
162
            int peak_y1 = ytop - fixed_half;
163
# endif
164
# if LINEAR_COLOR
165
            int num_components = dev->color_info.num_components;
166
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
            trap_gradient lg, rg, xg;
176
# else
177
21.0k
            gx_color_index cindex = pdevc->colors.pure;
178
21.0k
            dev_proc_fill_rectangle((*fill_rect)) =
179
21.0k
                dev_proc(dev, fill_rectangle);
180
21.0k
# endif
181
182
21.0k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
21.0k
        l.h = left->end.y - left->start.y;
185
21.0k
        if (l.h == 0)
186
0
           return 0;
187
21.0k
        r.h = right->end.y - right->start.y;
188
21.0k
        if (r.h == 0)
189
0
           return 0;
190
21.0k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
21.0k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
21.0k
#if !LINEAR_COLOR
193
21.0k
        ry = iy;
194
21.0k
#endif
195
196
/*
197
 * Free variables of FILL_TRAP_RECT:
198
 *  SWAP_AXES, pdevc, dev, fa
199
 * Free variables of FILL_TRAP_RECT_DIRECT:
200
 *  SWAP_AXES, fill_rect, dev, cindex
201
 */
202
21.0k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
21.0k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
21.0k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
21.0k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
21.0k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
21.0k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
        (!(w) ? 0 : dev_proc(dev, fill_linear_color_scanline)(dev, fa, x, y, w, xg.c, xg.f, xg.num, xg.den))
212
#else
213
21.0k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
21.0k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
21.0k
#endif
216
217
        /* Compute the dx/dy ratios. */
218
219
        /*
220
         * Compute the x offsets at the first scan line to sample.  We need
221
         * to be careful in computing ys# * dx#f {/,%} h# because the
222
         * multiplication may overflow.  We know that all the quantities
223
         * involved are non-negative, and that ys# is usually less than 1 (as
224
         * a fixed, of course); this gives us a cheap conservative check for
225
         * overflow in the multiplication.
226
         */
227
21.0k
#define YMULT_QUO(ys, tl)\
228
21.0k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
21.0k
   fixed_mult_quo(ys, tl.df, tl.h))
230
231
#if CONTIGUOUS_FILL
232
/*
233
 * If left and right boundary round to same pixel index,
234
 * we would not paing the scan and would get a dropout.
235
 * Check for this case and choose one of two pixels
236
 * which is closer to the "axis". We need to exclude
237
 * 'peak' because it would paint an excessive pixel.
238
 */
239
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) \
240
    if (ixl == ixr) \
241
        if ((!peak0 || iy >= peak_y0) && (!peak1 || iy <= peak_y1)) {\
242
            fixed x = int2fixed(ixl) + fixed_half;\
243
            if (x - l.x < r.x - x)\
244
                ++ixr;\
245
            else\
246
                --ixl;\
247
        }
248
249
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill)\
250
    if (adj1 < adj2) {\
251
        if (iy - ry > 1) {\
252
            code = fill(rxl, ry, rxr - rxl, iy - ry - 1);\
253
            if (code < 0)\
254
                goto xit;\
255
            ry = iy - 1;\
256
        }\
257
        adj1 = adj2 = (adj2 + adj2) / 2;\
258
    }
259
260
#else
261
21.0k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
21.0k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
21.0k
#endif
264
21.0k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
20.2k
            l.di = 0, l.df = 0;
267
20.2k
            fxl = 0;
268
20.2k
        } else {
269
818
            compute_dx(&l, dxl, ysl);
270
818
            fxl = YMULT_QUO(ysl, l);
271
818
            l.x += fxl;
272
818
        }
273
21.0k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
20.2k
#     if !LINEAR_COLOR
277
20.2k
                if (l.di == 0 && l.df == 0) {
278
20.1k
                    rxl = fixed2int_var(l.x);
279
20.1k
                    rxr = fixed2int_var(r.x);
280
20.1k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
20.1k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
20.1k
                    goto xit;
283
20.1k
                }
284
81
#     endif
285
81
            r.di = 0, r.df = 0;
286
81
        }
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
844
        else if (dxr == dxl && fxl != 0) {
292
9
            if (l.di == 0)
293
0
                r.di = 0, r.df = l.df;
294
9
            else
295
9
                compute_dx(&r, dxr, ysr);
296
9
            if (ysr == ysl && r.h == l.h)
297
0
                r.x += fxl;
298
9
            else
299
9
                r.x += YMULT_QUO(ysr, r);
300
835
        } else {
301
835
            compute_dx(&r, dxr, ysr);
302
835
            r.x += YMULT_QUO(ysr, r);
303
835
        }
304
        /* Compute one line's worth of dx/dy. */
305
925
        compute_ldx(&l, ysl);
306
925
        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
925
        l.x += fixed_epsilon;
310
925
        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
925
#define rational_floor(tl)\
338
925
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
925
#define STEP_LINE(ix, tl)\
340
925
  tl.x += tl.ldi;\
341
925
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
925
  ix = rational_floor(tl)
343
344
925
        rxl = rational_floor(l);
345
925
        rxr = rational_floor(r);
346
925
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
5.17k
        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
4.25k
                register int ixl, ixr;
365
366
4.25k
                STEP_LINE(ixl, l);
367
4.25k
                STEP_LINE(ixr, r);
368
4.25k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
4.25k
                if (ixl != rxl || ixr != rxr) {
370
3.15k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
3.15k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
3.15k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
3.15k
                    if (code < 0)
374
0
                        goto xit;
375
3.15k
                    rxl = ixl, rxr = ixr, ry = iy;
376
3.15k
                }
377
4.25k
#     endif
378
4.25k
        }
379
925
# if !LINEAR_COLOR
380
925
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
925
#undef STEP_LINE
385
925
#undef SET_MINIMAL_WIDTH
386
925
#undef CONNECT_RECTANGLES
387
925
#undef FILL_TRAP_RECT
388
925
#undef FILL_TRAP_RECT_DIRECT
389
925
#undef FILL_TRAP_RECT_INRECT
390
925
#undef YMULT_QUO
391
21.0k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
21.0k
        return_if_interrupt(dev->memory);
394
21.0k
        return code;
395
21.0k
    }
396
21.0k
}
gdevddrw.c:gx_fill_trapezoid_as_lc
Line
Count
Source
137
110k
{
138
110k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
110k
    const fixed ymax = fixed_pixround(ytop);
140
141
110k
    if (ymin >= ymax)
142
1.07k
        return 0;    /* no scan lines to sample */
143
109k
    {
144
109k
        int iy = fixed2int_var(ymin);
145
109k
        const int iy1 = fixed2int_var(ymax);
146
109k
        trap_line l, r;
147
109k
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
109k
        const fixed
152
109k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
109k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
109k
        const fixed /* partial pixel offset to first line to sample */
155
109k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
109k
        fixed fxl;
157
109k
        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
109k
# if LINEAR_COLOR
165
109k
            int num_components = dev->color_info.num_components;
166
109k
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
109k
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
109k
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
109k
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
109k
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
109k
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
109k
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
109k
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
109k
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
109k
            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
109k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
109k
        l.h = left->end.y - left->start.y;
185
109k
        if (l.h == 0)
186
0
           return 0;
187
109k
        r.h = right->end.y - right->start.y;
188
109k
        if (r.h == 0)
189
0
           return 0;
190
109k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
109k
        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
109k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
109k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
109k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
109k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
109k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
109k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
109k
#if LINEAR_COLOR
210
109k
#   define FILL_TRAP_RECT(x,y,w,h)\
211
109k
        (!(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
109k
#define YMULT_QUO(ys, tl)\
228
109k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
109k
   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
109k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
109k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
109k
#endif
264
109k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
74.5k
            l.di = 0, l.df = 0;
267
74.5k
            fxl = 0;
268
74.5k
        } else {
269
34.9k
            compute_dx(&l, dxl, ysl);
270
34.9k
            fxl = YMULT_QUO(ysl, l);
271
34.9k
            l.x += fxl;
272
34.9k
        }
273
109k
        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
74.4k
            r.di = 0, r.df = 0;
286
74.4k
        }
287
        /*
288
         * The test for fxl != 0 is required because the right edge might
289
         * cross some pixel centers even if the left edge doesn't.
290
         */
291
34.9k
        else if (dxr == dxl && fxl != 0) {
292
20.1k
            if (l.di == 0)
293
9.40k
                r.di = 0, r.df = l.df;
294
10.7k
            else
295
10.7k
                compute_dx(&r, dxr, ysr);
296
20.1k
            if (ysr == ysl && r.h == l.h)
297
20.1k
                r.x += fxl;
298
48
            else
299
48
                r.x += YMULT_QUO(ysr, r);
300
20.1k
        } else {
301
14.7k
            compute_dx(&r, dxr, ysr);
302
14.7k
            r.x += YMULT_QUO(ysr, r);
303
14.7k
        }
304
        /* Compute one line's worth of dx/dy. */
305
109k
        compute_ldx(&l, ysl);
306
109k
        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
109k
        l.x += fixed_epsilon;
310
109k
        r.x += fixed_epsilon;
311
109k
# 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
109k
            lg.c = lgc;
320
109k
            lg.f = lgf;
321
109k
            lg.num = lgnum;
322
109k
            rg.c = rgc;
323
109k
            rg.f = rgf;
324
109k
            rg.num = rgnum;
325
109k
            xg.c = xgc;
326
109k
            xg.f = xgf;
327
109k
            xg.num = xgnum;
328
109k
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
109k
            if (code < 0)
330
0
                return code;
331
109k
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
109k
            if (code < 0)
333
0
                return code;
334
335
109k
# endif
336
337
109k
#define rational_floor(tl)\
338
109k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
109k
#define STEP_LINE(ix, tl)\
340
109k
  tl.x += tl.ldi;\
341
109k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
109k
  ix = rational_floor(tl)
343
344
109k
        rxl = rational_floor(l);
345
109k
        rxr = rational_floor(r);
346
109k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
3.54M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
3.54M
#     if LINEAR_COLOR
349
3.54M
                if (rxl != rxr) {
350
3.14M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
3.14M
                    if (code < 0)
352
0
                        goto xit;
353
3.14M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
3.14M
                    if (code < 0)
355
0
                        goto xit;
356
3.14M
                }
357
3.54M
                if (++iy == iy1)
358
109k
                    break;
359
3.43M
                STEP_LINE(rxl, l);
360
3.43M
                STEP_LINE(rxr, r);
361
3.43M
                step_gradient(&lg, num_components);
362
3.43M
                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
3.43M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
109k
            code = 0;
383
109k
# endif
384
109k
#undef STEP_LINE
385
109k
#undef SET_MINIMAL_WIDTH
386
109k
#undef CONNECT_RECTANGLES
387
109k
#undef FILL_TRAP_RECT
388
109k
#undef FILL_TRAP_RECT_DIRECT
389
109k
#undef FILL_TRAP_RECT_INRECT
390
109k
#undef YMULT_QUO
391
109k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
109k
        return_if_interrupt(dev->memory);
394
109k
        return code;
395
109k
    }
396
109k
}
gdevddrw.c:gx_fill_trapezoid_ns_lc
Line
Count
Source
137
1.95M
{
138
1.95M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
1.95M
    const fixed ymax = fixed_pixround(ytop);
140
141
1.95M
    if (ymin >= ymax)
142
820k
        return 0;    /* no scan lines to sample */
143
1.13M
    {
144
1.13M
        int iy = fixed2int_var(ymin);
145
1.13M
        const int iy1 = fixed2int_var(ymax);
146
1.13M
        trap_line l, r;
147
1.13M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
1.13M
        const fixed
152
1.13M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
1.13M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
1.13M
        const fixed /* partial pixel offset to first line to sample */
155
1.13M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
1.13M
        fixed fxl;
157
1.13M
        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.13M
# if LINEAR_COLOR
165
1.13M
            int num_components = dev->color_info.num_components;
166
1.13M
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
1.13M
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
1.13M
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
1.13M
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
1.13M
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
1.13M
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
1.13M
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
1.13M
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
1.13M
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
1.13M
            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.13M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
1.13M
        l.h = left->end.y - left->start.y;
185
1.13M
        if (l.h == 0)
186
0
           return 0;
187
1.13M
        r.h = right->end.y - right->start.y;
188
1.13M
        if (r.h == 0)
189
0
           return 0;
190
1.13M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
1.13M
        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.13M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
1.13M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
1.13M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
1.13M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
1.13M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
1.13M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
1.13M
#if LINEAR_COLOR
210
1.13M
#   define FILL_TRAP_RECT(x,y,w,h)\
211
1.13M
        (!(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.13M
#define YMULT_QUO(ys, tl)\
228
1.13M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
1.13M
   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.13M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
1.13M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
1.13M
#endif
264
1.13M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
173k
            l.di = 0, l.df = 0;
267
173k
            fxl = 0;
268
960k
        } else {
269
960k
            compute_dx(&l, dxl, ysl);
270
960k
            fxl = YMULT_QUO(ysl, l);
271
960k
            l.x += fxl;
272
960k
        }
273
1.13M
        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
172k
            r.di = 0, r.df = 0;
286
172k
        }
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
961k
        else if (dxr == dxl && fxl != 0) {
292
29.8k
            if (l.di == 0)
293
9.11k
                r.di = 0, r.df = l.df;
294
20.7k
            else
295
20.7k
                compute_dx(&r, dxr, ysr);
296
29.8k
            if (ysr == ysl && r.h == l.h)
297
23.0k
                r.x += fxl;
298
6.78k
            else
299
6.78k
                r.x += YMULT_QUO(ysr, r);
300
931k
        } else {
301
931k
            compute_dx(&r, dxr, ysr);
302
931k
            r.x += YMULT_QUO(ysr, r);
303
931k
        }
304
        /* Compute one line's worth of dx/dy. */
305
1.13M
        compute_ldx(&l, ysl);
306
1.13M
        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.13M
        l.x += fixed_epsilon;
310
1.13M
        r.x += fixed_epsilon;
311
1.13M
# 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.13M
            lg.c = lgc;
320
1.13M
            lg.f = lgf;
321
1.13M
            lg.num = lgnum;
322
1.13M
            rg.c = rgc;
323
1.13M
            rg.f = rgf;
324
1.13M
            rg.num = rgnum;
325
1.13M
            xg.c = xgc;
326
1.13M
            xg.f = xgf;
327
1.13M
            xg.num = xgnum;
328
1.13M
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
1.13M
            if (code < 0)
330
0
                return code;
331
1.13M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
1.13M
            if (code < 0)
333
0
                return code;
334
335
1.13M
# endif
336
337
1.13M
#define rational_floor(tl)\
338
1.13M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
1.13M
#define STEP_LINE(ix, tl)\
340
1.13M
  tl.x += tl.ldi;\
341
1.13M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
1.13M
  ix = rational_floor(tl)
343
344
1.13M
        rxl = rational_floor(l);
345
1.13M
        rxr = rational_floor(r);
346
1.13M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
7.78M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
7.78M
#     if LINEAR_COLOR
349
7.78M
                if (rxl != rxr) {
350
3.10M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
3.10M
                    if (code < 0)
352
0
                        goto xit;
353
3.10M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
3.10M
                    if (code < 0)
355
0
                        goto xit;
356
3.10M
                }
357
7.78M
                if (++iy == iy1)
358
1.13M
                    break;
359
6.65M
                STEP_LINE(rxl, l);
360
6.65M
                STEP_LINE(rxr, r);
361
6.65M
                step_gradient(&lg, num_components);
362
6.65M
                step_gradient(&rg, num_components);
363
#     else
364
                register int ixl, ixr;
365
366
                STEP_LINE(ixl, l);
367
                STEP_LINE(ixr, r);
368
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
                if (ixl != rxl || ixr != rxr) {
370
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
                    if (code < 0)
374
                        goto xit;
375
                    rxl = ixl, rxr = ixr, ry = iy;
376
                }
377
#     endif
378
6.65M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
1.13M
            code = 0;
383
1.13M
# endif
384
1.13M
#undef STEP_LINE
385
1.13M
#undef SET_MINIMAL_WIDTH
386
1.13M
#undef CONNECT_RECTANGLES
387
1.13M
#undef FILL_TRAP_RECT
388
1.13M
#undef FILL_TRAP_RECT_DIRECT
389
1.13M
#undef FILL_TRAP_RECT_INRECT
390
1.13M
#undef YMULT_QUO
391
1.13M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
1.13M
        return_if_interrupt(dev->memory);
394
1.13M
        return code;
395
1.13M
    }
396
1.13M
}
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