Coverage Report

Created: 2025-06-10 06:59

/src/ghostpdl/base/gxdtfill.h
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Configurable algorithm for filling a trapezoid */
18
19
/*
20
 * Since we need several statically defined variants of this agorithm,
21
 * we store it in .h file and include several times into gdevddrw.c and
22
 * into gxfill.h . Configuration flags (macros) are :
23
 *
24
 *   GX_FILL_TRAPEZOID - a name of method
25
 *   CONTIGUOUS_FILL   - prevent dropouts in narrow trapezoids
26
 *   SWAP_AXES         - assume swapped axes
27
 *   FILL_DIRECT       - See LOOP_FILL_RECTANGLE_DIRECT.
28
 *   LINEAR_COLOR      - Fill with a linear color.
29
 *   EDGE_TYPE         - a type of edge structure.
30
 *   FILL_ATTRS        - operation attributes.
31
 */
32
33
/*
34
 * Fill a trapezoid.  left.start => left.end and right.start => right.end
35
 * define the sides; ybot and ytop define the top and bottom.  Requires:
36
 *      {left,right}->start.y <= ybot <= ytop <= {left,right}->end.y.
37
 * Lines where left.x >= right.x will not be drawn.  Thanks to Paul Haeberli
38
 * for an early floating point version of this algorithm.
39
 */
40
41
/*
42
 * With CONTIGUOUS_FILL is off,
43
 * this algorithm paints pixels, which centers fall between
44
 * the left and the right side of the trapezoid, excluding the
45
 * right side (see PLRM3, 7.5. Scan conversion details).
46
 * Particularly 0-width trapezoids are not painted.
47
 *
48
 * Similarly, it paints pixels, which centers
49
 * fall between ybot and ytop, excluding ytop.
50
 * Particularly 0-height trapezoids are not painted.
51
 *
52
 * With CONTIGUOUS_FILL is on, it paints a contigous area,
53
 * adding a minimal number of pixels outside the trapezoid.
54
 * Particularly it may paint pixels on the right and on the top sides,
55
 * if they are necessary for the contiguity.
56
 *
57
 * With LINEAR_COLOR returns 1 if the gradient arithmetics overflows..
58
 */
59
60
/*
61
We must paint pixels with index i such that
62
63
    Xl <= i + 0.5 < Xr
64
65
The condition is is equivalent to
66
67
    Xl - 0.5 <= i < Xr - 0.5
68
69
which is equivalent to
70
71
    (is_integer(Xl - 0.5) ? Xl - 0.5 : ceil(Xl - 0.5)) <= i <
72
    (is_integer(Xr - 0.5) ? Xr - 0.5 : floor(Xr - 0.5) + 1)
73
74
(the last '+1" happens due to the strong comparizon '<')
75
which is equivalent to
76
77
    ceil(Xl - 0.5) <= i < ceil(Xr - 0.5)
78
79
trap_line represents the intersection coordinate as a rational value :
80
81
    Xl = xl + e - fl
82
    Xr = xr + e - fr
83
84
Where 'e' is 'fixed_epsilon', 0.5 is 'fixed_half', and fl == l.fx / l.h, fr == - r.fx / r.h,
85
e <= fl < 0, e <= fr < 0.
86
Let
87
88
    xl' := xl + 0.5
89
    xr' := xr + 0.5
90
91
Then
92
93
    xl = xl' - 0.5
94
    xr = xr' - 0.5
95
96
    Xl = xl' - 0.5 + e - fl
97
    Xr = xr' - 0.5 + e - fr
98
99
    ceil(xl' - 0.5 + e - fl - 0.5) <= i < ceil(xr' - 0.5 + e - fr - 0.5)
100
101
which is equivalent to
102
103
    ceil(xl' + e - fl) - 1 <= i < ceil(xr' + e - fr) - 1
104
105
which is equivalent to
106
107
    (is_integer(xl' + e - fl) ? xl' + e - fl - 1 : ceil(xl' + e - fl) - 1) <= i <
108
    (is_integer(xr' + e - fr) ? xr' + e - fr - 1 : ceil(xr' + e - fr) - 1)
109
110
which is equivalent to
111
112
    (is_integer(xl' + e - fl) ? xl' + e - fl - 1 : floor(xl' + e - fl)) <= i <
113
    (is_integer(xr' + e - fr) ? xr' + e - fr - 1 : floor(xr' + e - fr))
114
115
which is equivalent to
116
117
    (is_integer(xl') && e == fl ? xl' - 1 : floor(xl' + e - fl)) <= i <
118
    (is_integer(xr') && e == fr ? xr' - 1 : floor(xr' + e - fr))
119
120
Note that e != fl ==> floor(xl' + e - fl) == floor(xl')  due to e - fl < LeastSignificantBit(xl') ;
121
          e == fl ==> floor(xl' + e - fl) == floor(xl')  due to e - fl == 0;
122
123
thus the condition is is equivalent to
124
125
    (is_integer(xl') && e == fl ? xl' - 1 : floor(xl')) <= i <
126
    (is_integer(xr') && e == fr ? xr' - 1 : floor(xr'))
127
128
It is computed with the macro 'rational_floor'.
129
130
*/
131
132
#if defined(GX_FILL_TRAPEZOID) && defined(EDGE_TYPE)
133
134
GX_FILL_TRAPEZOID (gx_device * dev, const EDGE_TYPE * left,
135
    const EDGE_TYPE * right, fixed ybot, fixed ytop, int flags,
136
    const gx_device_color * pdevc, FILL_ATTRS fa)
137
9.06M
{
138
9.06M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
9.06M
    const fixed ymax = fixed_pixround(ytop);
140
141
9.06M
    if (ymin >= ymax)
142
2.41M
        return 0;    /* no scan lines to sample */
143
6.64M
    {
144
6.64M
        int iy = fixed2int_var(ymin);
145
6.64M
        const int iy1 = fixed2int_var(ymax);
146
6.64M
        trap_line l, r;
147
6.64M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
6.64M
        const fixed
152
6.64M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
6.64M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
6.64M
        const fixed /* partial pixel offset to first line to sample */
155
6.64M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
6.64M
        fixed fxl;
157
6.64M
        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
3.10M
            dev_proc_fill_rectangle((*fill_rect)) =
179
3.10M
                dev_proc(dev, fill_rectangle);
180
# endif
181
182
6.64M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
6.64M
        l.h = left->end.y - left->start.y;
185
6.64M
        if (l.h == 0)
186
3
           return 0;
187
6.64M
        r.h = right->end.y - right->start.y;
188
6.64M
        if (r.h == 0)
189
3
           return 0;
190
6.64M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
6.64M
        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
6.64M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
6.64M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
23.7k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
6.64M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
24.0M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
24.0M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
15.0M
        (!(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
24.0M
        (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
6.64M
#define YMULT_QUO(ys, tl)\
228
8.78M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
8.78M
   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
54.5M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
41.9M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
#endif
264
6.64M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
2.01M
            l.di = 0, l.df = 0;
267
2.01M
            fxl = 0;
268
4.63M
        } else {
269
4.63M
            compute_dx(&l, dxl, ysl);
270
4.63M
            fxl = YMULT_QUO(ysl, l);
271
4.63M
            l.x += fxl;
272
4.63M
        }
273
6.64M
        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.09M
                if (l.di == 0 && l.df == 0) {
278
854k
                    rxl = fixed2int_var(l.x);
279
854k
                    rxr = fixed2int_var(r.x);
280
854k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
854k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
854k
                    goto xit;
283
854k
                }
284
236k
#     endif
285
236k
            r.di = 0, r.df = 0;
286
236k
        }
287
        /*
288
         * The test for fxl != 0 is required because the right edge might
289
         * cross some pixel centers even if the left edge doesn't.
290
         */
291
4.64M
        else if (dxr == dxl && fxl != 0) {
292
730k
            if (l.di == 0)
293
229k
                r.di = 0, r.df = l.df;
294
501k
            else
295
501k
                compute_dx(&r, dxr, ysr);
296
730k
            if (ysr == ysl && r.h == l.h)
297
494k
                r.x += fxl;
298
236k
            else
299
236k
                r.x += YMULT_QUO(ysr, r);
300
3.91M
        } else {
301
3.91M
            compute_dx(&r, dxr, ysr);
302
3.91M
            r.x += YMULT_QUO(ysr, r);
303
3.91M
        }
304
        /* Compute one line's worth of dx/dy. */
305
2.24M
        compute_ldx(&l, ysl);
306
2.24M
        compute_ldx(&r, ysr);
307
        /* We subtracted fixed_epsilon from l.x, r.x to simplify rounding
308
           when the rational part is zero. Now add it back to get xl', xr' */
309
5.79M
        l.x += fixed_epsilon;
310
5.79M
        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
3.54M
            if (code < 0)
330
0
                return code;
331
3.54M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
3.54M
            if (code < 0)
333
0
                return code;
334
335
3.54M
# endif
336
337
3.54M
#define rational_floor(tl)\
338
220M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
3.54M
#define STEP_LINE(ix, tl)\
340
204M
  tl.x += tl.ldi;\
341
204M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
208M
  ix = rational_floor(tl)
343
344
5.79M
        rxl = rational_floor(l);
345
5.79M
        rxr = rational_floor(r);
346
5.79M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
108M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
58.0M
                if (rxl != rxr) {
350
15.0M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
15.0M
                    if (code < 0)
352
0
                        goto xit;
353
15.0M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
15.0M
                    if (code < 0)
355
0
                        goto xit;
356
15.0M
                }
357
58.0M
                if (++iy == iy1)
358
3.54M
                    break;
359
54.5M
                STEP_LINE(rxl, l);
360
54.5M
                STEP_LINE(rxr, r);
361
54.5M
                step_gradient(&lg, num_components);
362
54.5M
                step_gradient(&rg, num_components);
363
#     else
364
                register int ixl, ixr;
365
366
47.9M
                STEP_LINE(ixl, l);
367
47.9M
                STEP_LINE(ixr, r);
368
47.9M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
47.9M
                if (ixl != rxl || ixr != rxr) {
370
20.9M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
20.9M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
20.9M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
20.9M
                    if (code < 0)
374
0
                        goto xit;
375
20.9M
                    rxl = ixl, rxr = ixr, ry = iy;
376
20.9M
                }
377
#     endif
378
54.5M
        }
379
# if !LINEAR_COLOR
380
2.24M
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
3.54M
            code = 0;
383
3.54M
# endif
384
3.54M
#undef STEP_LINE
385
3.54M
#undef SET_MINIMAL_WIDTH
386
3.54M
#undef CONNECT_RECTANGLES
387
3.54M
#undef FILL_TRAP_RECT
388
3.54M
#undef FILL_TRAP_RECT_DIRECT
389
3.54M
#undef FILL_TRAP_RECT_INRECT
390
3.54M
#undef YMULT_QUO
391
6.64M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
6.64M
        return_if_interrupt(dev->memory);
394
6.64M
        return code;
395
6.64M
    }
396
6.64M
}
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
212k
{
138
212k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
212k
    const fixed ymax = fixed_pixround(ytop);
140
141
212k
    if (ymin >= ymax)
142
12.7k
        return 0;    /* no scan lines to sample */
143
200k
    {
144
200k
        int iy = fixed2int_var(ymin);
145
200k
        const int iy1 = fixed2int_var(ymax);
146
200k
        trap_line l, r;
147
200k
        register int rxl, rxr;
148
200k
#if !LINEAR_COLOR
149
200k
        int ry;
150
200k
#endif
151
200k
        const fixed
152
200k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
200k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
200k
        const fixed /* partial pixel offset to first line to sample */
155
200k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
200k
        fixed fxl;
157
200k
        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
200k
            gx_color_index cindex = pdevc->colors.pure;
178
200k
            dev_proc_fill_rectangle((*fill_rect)) =
179
200k
                dev_proc(dev, fill_rectangle);
180
200k
# endif
181
182
200k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
200k
        l.h = left->end.y - left->start.y;
185
200k
        if (l.h == 0)
186
0
           return 0;
187
200k
        r.h = right->end.y - right->start.y;
188
200k
        if (r.h == 0)
189
0
           return 0;
190
200k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
200k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
200k
#if !LINEAR_COLOR
193
200k
        ry = iy;
194
200k
#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
200k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
200k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
200k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
200k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
200k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
200k
   (*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
200k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
200k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
200k
#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
200k
#define YMULT_QUO(ys, tl)\
228
200k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
200k
   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
200k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
200k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
200k
#endif
264
200k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
20.4k
            l.di = 0, l.df = 0;
267
20.4k
            fxl = 0;
268
179k
        } else {
269
179k
            compute_dx(&l, dxl, ysl);
270
179k
            fxl = YMULT_QUO(ysl, l);
271
179k
            l.x += fxl;
272
179k
        }
273
200k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
21.9k
#     if !LINEAR_COLOR
277
21.9k
                if (l.di == 0 && l.df == 0) {
278
18.8k
                    rxl = fixed2int_var(l.x);
279
18.8k
                    rxr = fixed2int_var(r.x);
280
18.8k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
18.8k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
18.8k
                    goto xit;
283
18.8k
                }
284
3.08k
#     endif
285
3.08k
            r.di = 0, r.df = 0;
286
3.08k
        }
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
178k
        else if (dxr == dxl && fxl != 0) {
292
160k
            if (l.di == 0)
293
109k
                r.di = 0, r.df = l.df;
294
51.2k
            else
295
51.2k
                compute_dx(&r, dxr, ysr);
296
160k
            if (ysr == ysl && r.h == l.h)
297
160k
                r.x += fxl;
298
3
            else
299
3
                r.x += YMULT_QUO(ysr, r);
300
160k
        } else {
301
17.7k
            compute_dx(&r, dxr, ysr);
302
17.7k
            r.x += YMULT_QUO(ysr, r);
303
17.7k
        }
304
        /* Compute one line's worth of dx/dy. */
305
181k
        compute_ldx(&l, ysl);
306
181k
        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
181k
        l.x += fixed_epsilon;
310
181k
        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
181k
#define rational_floor(tl)\
338
181k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
181k
#define STEP_LINE(ix, tl)\
340
181k
  tl.x += tl.ldi;\
341
181k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
181k
  ix = rational_floor(tl)
343
344
181k
        rxl = rational_floor(l);
345
181k
        rxr = rational_floor(r);
346
181k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
23.9M
        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
23.7M
                register int ixl, ixr;
365
366
23.7M
                STEP_LINE(ixl, l);
367
23.7M
                STEP_LINE(ixr, r);
368
23.7M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
23.7M
                if (ixl != rxl || ixr != rxr) {
370
4.91M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
4.91M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
4.91M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
4.91M
                    if (code < 0)
374
0
                        goto xit;
375
4.91M
                    rxl = ixl, rxr = ixr, ry = iy;
376
4.91M
                }
377
23.7M
#     endif
378
23.7M
        }
379
181k
# if !LINEAR_COLOR
380
181k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
181k
#undef STEP_LINE
385
181k
#undef SET_MINIMAL_WIDTH
386
181k
#undef CONNECT_RECTANGLES
387
181k
#undef FILL_TRAP_RECT
388
181k
#undef FILL_TRAP_RECT_DIRECT
389
181k
#undef FILL_TRAP_RECT_INRECT
390
181k
#undef YMULT_QUO
391
200k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
200k
        return_if_interrupt(dev->memory);
394
200k
        return code;
395
200k
    }
396
200k
}
Unexecuted instantiation: gdevddrw.c:gx_fill_trapezoid_as_nd
gdevddrw.c:gx_fill_trapezoid_ns_fd
Line
Count
Source
137
2.92M
{
138
2.92M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
2.92M
    const fixed ymax = fixed_pixround(ytop);
140
141
2.92M
    if (ymin >= ymax)
142
42.8k
        return 0;    /* no scan lines to sample */
143
2.88M
    {
144
2.88M
        int iy = fixed2int_var(ymin);
145
2.88M
        const int iy1 = fixed2int_var(ymax);
146
2.88M
        trap_line l, r;
147
2.88M
        register int rxl, rxr;
148
2.88M
#if !LINEAR_COLOR
149
2.88M
        int ry;
150
2.88M
#endif
151
2.88M
        const fixed
152
2.88M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
2.88M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
2.88M
        const fixed /* partial pixel offset to first line to sample */
155
2.88M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
2.88M
        fixed fxl;
157
2.88M
        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
2.88M
            gx_color_index cindex = pdevc->colors.pure;
178
2.88M
            dev_proc_fill_rectangle((*fill_rect)) =
179
2.88M
                dev_proc(dev, fill_rectangle);
180
2.88M
# endif
181
182
2.88M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
2.88M
        l.h = left->end.y - left->start.y;
185
2.88M
        if (l.h == 0)
186
3
           return 0;
187
2.88M
        r.h = right->end.y - right->start.y;
188
2.88M
        if (r.h == 0)
189
3
           return 0;
190
2.88M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
2.88M
        r.x = x0r + (fixed_half - fixed_epsilon);
192
2.88M
#if !LINEAR_COLOR
193
2.88M
        ry = iy;
194
2.88M
#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.88M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
2.88M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
2.88M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
2.88M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
2.88M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
2.88M
   (*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
2.88M
#   define FILL_TRAP_RECT(x,y,w,h)\
214
2.88M
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
2.88M
#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.88M
#define YMULT_QUO(ys, tl)\
228
2.88M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
2.88M
   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
2.88M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
2.88M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
2.88M
#endif
264
2.88M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
1.06M
            l.di = 0, l.df = 0;
267
1.06M
            fxl = 0;
268
1.82M
        } else {
269
1.82M
            compute_dx(&l, dxl, ysl);
270
1.82M
            fxl = YMULT_QUO(ysl, l);
271
1.82M
            l.x += fxl;
272
1.82M
        }
273
2.88M
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
1.05M
#     if !LINEAR_COLOR
277
1.05M
                if (l.di == 0 && l.df == 0) {
278
820k
                    rxl = fixed2int_var(l.x);
279
820k
                    rxr = fixed2int_var(r.x);
280
820k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
820k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
820k
                    goto xit;
283
820k
                }
284
232k
#     endif
285
232k
            r.di = 0, r.df = 0;
286
232k
        }
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.83M
        else if (dxr == dxl && fxl != 0) {
292
378k
            if (l.di == 0)
293
75.0k
                r.di = 0, r.df = l.df;
294
303k
            else
295
303k
                compute_dx(&r, dxr, ysr);
296
378k
            if (ysr == ysl && r.h == l.h)
297
182k
                r.x += fxl;
298
196k
            else
299
196k
                r.x += YMULT_QUO(ysr, r);
300
1.45M
        } else {
301
1.45M
            compute_dx(&r, dxr, ysr);
302
1.45M
            r.x += YMULT_QUO(ysr, r);
303
1.45M
        }
304
        /* Compute one line's worth of dx/dy. */
305
2.06M
        compute_ldx(&l, ysl);
306
2.06M
        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.06M
        l.x += fixed_epsilon;
310
2.06M
        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
2.06M
#define rational_floor(tl)\
338
2.06M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
2.06M
#define STEP_LINE(ix, tl)\
340
2.06M
  tl.x += tl.ldi;\
341
2.06M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
2.06M
  ix = rational_floor(tl)
343
344
2.06M
        rxl = rational_floor(l);
345
2.06M
        rxr = rational_floor(r);
346
2.06M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
26.2M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
                if (rxl != rxr) {
350
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
                    if (code < 0)
352
                        goto xit;
353
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
                    if (code < 0)
355
                        goto xit;
356
                }
357
                if (++iy == iy1)
358
                    break;
359
                STEP_LINE(rxl, l);
360
                STEP_LINE(rxr, r);
361
                step_gradient(&lg, num_components);
362
                step_gradient(&rg, num_components);
363
#     else
364
24.1M
                register int ixl, ixr;
365
366
24.1M
                STEP_LINE(ixl, l);
367
24.1M
                STEP_LINE(ixr, r);
368
24.1M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
24.1M
                if (ixl != rxl || ixr != rxr) {
370
16.0M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
16.0M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
16.0M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
16.0M
                    if (code < 0)
374
0
                        goto xit;
375
16.0M
                    rxl = ixl, rxr = ixr, ry = iy;
376
16.0M
                }
377
24.1M
#     endif
378
24.1M
        }
379
2.06M
# if !LINEAR_COLOR
380
2.06M
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
2.06M
#undef STEP_LINE
385
2.06M
#undef SET_MINIMAL_WIDTH
386
2.06M
#undef CONNECT_RECTANGLES
387
2.06M
#undef FILL_TRAP_RECT
388
2.06M
#undef FILL_TRAP_RECT_DIRECT
389
2.06M
#undef FILL_TRAP_RECT_INRECT
390
2.06M
#undef YMULT_QUO
391
2.88M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
2.88M
        return_if_interrupt(dev->memory);
394
2.88M
        return code;
395
2.88M
    }
396
2.88M
}
gdevddrw.c:gx_fill_trapezoid_ns_nd
Line
Count
Source
137
16.7k
{
138
16.7k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
16.7k
    const fixed ymax = fixed_pixround(ytop);
140
141
16.7k
    if (ymin >= ymax)
142
0
        return 0;   /* no scan lines to sample */
143
16.7k
    {
144
16.7k
        int iy = fixed2int_var(ymin);
145
16.7k
        const int iy1 = fixed2int_var(ymax);
146
16.7k
        trap_line l, r;
147
16.7k
        register int rxl, rxr;
148
16.7k
#if !LINEAR_COLOR
149
16.7k
        int ry;
150
16.7k
#endif
151
16.7k
        const fixed
152
16.7k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
16.7k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
16.7k
        const fixed /* partial pixel offset to first line to sample */
155
16.7k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
16.7k
        fixed fxl;
157
16.7k
        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
16.7k
            gx_color_index cindex = pdevc->colors.pure;
178
16.7k
            dev_proc_fill_rectangle((*fill_rect)) =
179
16.7k
                dev_proc(dev, fill_rectangle);
180
16.7k
# endif
181
182
16.7k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
16.7k
        l.h = left->end.y - left->start.y;
185
16.7k
        if (l.h == 0)
186
0
           return 0;
187
16.7k
        r.h = right->end.y - right->start.y;
188
16.7k
        if (r.h == 0)
189
0
           return 0;
190
16.7k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
16.7k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
16.7k
#if !LINEAR_COLOR
193
16.7k
        ry = iy;
194
16.7k
#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
16.7k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
16.7k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
16.7k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
16.7k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
16.7k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
16.7k
   (*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
16.7k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
16.7k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
16.7k
#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
16.7k
#define YMULT_QUO(ys, tl)\
228
16.7k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
16.7k
   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
16.7k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
16.7k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
16.7k
#endif
264
16.7k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
15.3k
            l.di = 0, l.df = 0;
267
15.3k
            fxl = 0;
268
15.3k
        } else {
269
1.40k
            compute_dx(&l, dxl, ysl);
270
1.40k
            fxl = YMULT_QUO(ysl, l);
271
1.40k
            l.x += fxl;
272
1.40k
        }
273
16.7k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
15.2k
#     if !LINEAR_COLOR
277
15.2k
                if (l.di == 0 && l.df == 0) {
278
15.1k
                    rxl = fixed2int_var(l.x);
279
15.1k
                    rxr = fixed2int_var(r.x);
280
15.1k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
15.1k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
15.1k
                    goto xit;
283
15.1k
                }
284
133
#     endif
285
133
            r.di = 0, r.df = 0;
286
133
        }
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.46k
        else if (dxr == dxl && fxl != 0) {
292
10
            if (l.di == 0)
293
0
                r.di = 0, r.df = l.df;
294
10
            else
295
10
                compute_dx(&r, dxr, ysr);
296
10
            if (ysr == ysl && r.h == l.h)
297
0
                r.x += fxl;
298
10
            else
299
10
                r.x += YMULT_QUO(ysr, r);
300
1.45k
        } else {
301
1.45k
            compute_dx(&r, dxr, ysr);
302
1.45k
            r.x += YMULT_QUO(ysr, r);
303
1.45k
        }
304
        /* Compute one line's worth of dx/dy. */
305
1.59k
        compute_ldx(&l, ysl);
306
1.59k
        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.59k
        l.x += fixed_epsilon;
310
1.59k
        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.59k
#define rational_floor(tl)\
338
1.59k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
1.59k
#define STEP_LINE(ix, tl)\
340
1.59k
  tl.x += tl.ldi;\
341
1.59k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
1.59k
  ix = rational_floor(tl)
343
344
1.59k
        rxl = rational_floor(l);
345
1.59k
        rxr = rational_floor(r);
346
1.59k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
10.9k
        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
9.31k
                register int ixl, ixr;
365
366
9.31k
                STEP_LINE(ixl, l);
367
9.31k
                STEP_LINE(ixr, r);
368
9.31k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
9.31k
                if (ixl != rxl || ixr != rxr) {
370
7.02k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
7.02k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
7.02k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
7.02k
                    if (code < 0)
374
0
                        goto xit;
375
7.02k
                    rxl = ixl, rxr = ixr, ry = iy;
376
7.02k
                }
377
9.31k
#     endif
378
9.31k
        }
379
1.59k
# if !LINEAR_COLOR
380
1.59k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
1.59k
#undef STEP_LINE
385
1.59k
#undef SET_MINIMAL_WIDTH
386
1.59k
#undef CONNECT_RECTANGLES
387
1.59k
#undef FILL_TRAP_RECT
388
1.59k
#undef FILL_TRAP_RECT_DIRECT
389
1.59k
#undef FILL_TRAP_RECT_INRECT
390
1.59k
#undef YMULT_QUO
391
16.7k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
16.7k
        return_if_interrupt(dev->memory);
394
16.7k
        return code;
395
16.7k
    }
396
16.7k
}
gdevddrw.c:gx_fill_trapezoid_as_lc
Line
Count
Source
137
276k
{
138
276k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
276k
    const fixed ymax = fixed_pixround(ytop);
140
141
276k
    if (ymin >= ymax)
142
70.4k
        return 0;    /* no scan lines to sample */
143
206k
    {
144
206k
        int iy = fixed2int_var(ymin);
145
206k
        const int iy1 = fixed2int_var(ymax);
146
206k
        trap_line l, r;
147
206k
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
206k
        const fixed
152
206k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
206k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
206k
        const fixed /* partial pixel offset to first line to sample */
155
206k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
206k
        fixed fxl;
157
206k
        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
206k
# if LINEAR_COLOR
165
206k
            int num_components = dev->color_info.num_components;
166
206k
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
206k
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
206k
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
206k
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
206k
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
206k
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
206k
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
206k
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
206k
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
206k
            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
206k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
206k
        l.h = left->end.y - left->start.y;
185
206k
        if (l.h == 0)
186
0
           return 0;
187
206k
        r.h = right->end.y - right->start.y;
188
206k
        if (r.h == 0)
189
0
           return 0;
190
206k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
206k
        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
206k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
206k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
206k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
206k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
206k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
206k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
206k
#if LINEAR_COLOR
210
206k
#   define FILL_TRAP_RECT(x,y,w,h)\
211
206k
        (!(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
206k
#define YMULT_QUO(ys, tl)\
228
206k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
206k
   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
206k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
206k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
206k
#endif
264
206k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
119k
            l.di = 0, l.df = 0;
267
119k
            fxl = 0;
268
119k
        } else {
269
86.2k
            compute_dx(&l, dxl, ysl);
270
86.2k
            fxl = YMULT_QUO(ysl, l);
271
86.2k
            l.x += fxl;
272
86.2k
        }
273
206k
        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
120k
            r.di = 0, r.df = 0;
286
120k
        }
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
85.8k
        else if (dxr == dxl && fxl != 0) {
292
38.8k
            if (l.di == 0)
293
19.8k
                r.di = 0, r.df = l.df;
294
18.9k
            else
295
18.9k
                compute_dx(&r, dxr, ysr);
296
38.8k
            if (ysr == ysl && r.h == l.h)
297
38.8k
                r.x += fxl;
298
5
            else
299
5
                r.x += YMULT_QUO(ysr, r);
300
46.9k
        } else {
301
46.9k
            compute_dx(&r, dxr, ysr);
302
46.9k
            r.x += YMULT_QUO(ysr, r);
303
46.9k
        }
304
        /* Compute one line's worth of dx/dy. */
305
206k
        compute_ldx(&l, ysl);
306
206k
        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
206k
        l.x += fixed_epsilon;
310
206k
        r.x += fixed_epsilon;
311
206k
# 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
206k
            lg.c = lgc;
320
206k
            lg.f = lgf;
321
206k
            lg.num = lgnum;
322
206k
            rg.c = rgc;
323
206k
            rg.f = rgf;
324
206k
            rg.num = rgnum;
325
206k
            xg.c = xgc;
326
206k
            xg.f = xgf;
327
206k
            xg.num = xgnum;
328
206k
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
206k
            if (code < 0)
330
0
                return code;
331
206k
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
206k
            if (code < 0)
333
0
                return code;
334
335
206k
# endif
336
337
206k
#define rational_floor(tl)\
338
206k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
206k
#define STEP_LINE(ix, tl)\
340
206k
  tl.x += tl.ldi;\
341
206k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
206k
  ix = rational_floor(tl)
343
344
206k
        rxl = rational_floor(l);
345
206k
        rxr = rational_floor(r);
346
206k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
14.1M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
14.1M
#     if LINEAR_COLOR
349
14.1M
                if (rxl != rxr) {
350
5.48M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
5.48M
                    if (code < 0)
352
0
                        goto xit;
353
5.48M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
5.48M
                    if (code < 0)
355
0
                        goto xit;
356
5.48M
                }
357
14.1M
                if (++iy == iy1)
358
206k
                    break;
359
13.9M
                STEP_LINE(rxl, l);
360
13.9M
                STEP_LINE(rxr, r);
361
13.9M
                step_gradient(&lg, num_components);
362
13.9M
                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
13.9M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
206k
            code = 0;
383
206k
# endif
384
206k
#undef STEP_LINE
385
206k
#undef SET_MINIMAL_WIDTH
386
206k
#undef CONNECT_RECTANGLES
387
206k
#undef FILL_TRAP_RECT
388
206k
#undef FILL_TRAP_RECT_DIRECT
389
206k
#undef FILL_TRAP_RECT_INRECT
390
206k
#undef YMULT_QUO
391
206k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
206k
        return_if_interrupt(dev->memory);
394
206k
        return code;
395
206k
    }
396
206k
}
gdevddrw.c:gx_fill_trapezoid_ns_lc
Line
Count
Source
137
5.63M
{
138
5.63M
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
5.63M
    const fixed ymax = fixed_pixround(ytop);
140
141
5.63M
    if (ymin >= ymax)
142
2.29M
        return 0;    /* no scan lines to sample */
143
3.33M
    {
144
3.33M
        int iy = fixed2int_var(ymin);
145
3.33M
        const int iy1 = fixed2int_var(ymax);
146
3.33M
        trap_line l, r;
147
3.33M
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
3.33M
        const fixed
152
3.33M
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
3.33M
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
3.33M
        const fixed /* partial pixel offset to first line to sample */
155
3.33M
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
3.33M
        fixed fxl;
157
3.33M
        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
3.33M
# if LINEAR_COLOR
165
3.33M
            int num_components = dev->color_info.num_components;
166
3.33M
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
3.33M
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
3.33M
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
3.33M
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
3.33M
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
3.33M
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
3.33M
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
3.33M
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
3.33M
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
3.33M
            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
3.33M
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
3.33M
        l.h = left->end.y - left->start.y;
185
3.33M
        if (l.h == 0)
186
0
           return 0;
187
3.33M
        r.h = right->end.y - right->start.y;
188
3.33M
        if (r.h == 0)
189
0
           return 0;
190
3.33M
        l.x = x0l + (fixed_half - fixed_epsilon);
191
3.33M
        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
3.33M
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
3.33M
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
3.33M
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
3.33M
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
3.33M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
3.33M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
3.33M
#if LINEAR_COLOR
210
3.33M
#   define FILL_TRAP_RECT(x,y,w,h)\
211
3.33M
        (!(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
3.33M
#define YMULT_QUO(ys, tl)\
228
3.33M
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
3.33M
   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
3.33M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
3.33M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
3.33M
#endif
264
3.33M
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
795k
            l.di = 0, l.df = 0;
267
795k
            fxl = 0;
268
2.54M
        } else {
269
2.54M
            compute_dx(&l, dxl, ysl);
270
2.54M
            fxl = YMULT_QUO(ysl, l);
271
2.54M
            l.x += fxl;
272
2.54M
        }
273
3.33M
        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
793k
            r.di = 0, r.df = 0;
286
793k
        }
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
2.54M
        else if (dxr == dxl && fxl != 0) {
292
153k
            if (l.di == 0)
293
25.4k
                r.di = 0, r.df = l.df;
294
127k
            else
295
127k
                compute_dx(&r, dxr, ysr);
296
153k
            if (ysr == ysl && r.h == l.h)
297
113k
                r.x += fxl;
298
39.7k
            else
299
39.7k
                r.x += YMULT_QUO(ysr, r);
300
2.39M
        } else {
301
2.39M
            compute_dx(&r, dxr, ysr);
302
2.39M
            r.x += YMULT_QUO(ysr, r);
303
2.39M
        }
304
        /* Compute one line's worth of dx/dy. */
305
3.33M
        compute_ldx(&l, ysl);
306
3.33M
        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
3.33M
        l.x += fixed_epsilon;
310
3.33M
        r.x += fixed_epsilon;
311
3.33M
# 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
3.33M
            lg.c = lgc;
320
3.33M
            lg.f = lgf;
321
3.33M
            lg.num = lgnum;
322
3.33M
            rg.c = rgc;
323
3.33M
            rg.f = rgf;
324
3.33M
            rg.num = rgnum;
325
3.33M
            xg.c = xgc;
326
3.33M
            xg.f = xgf;
327
3.33M
            xg.num = xgnum;
328
3.33M
            code = init_gradient(&lg, fa, left, right, &l, ymin, num_components);
329
3.33M
            if (code < 0)
330
0
                return code;
331
3.33M
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
3.33M
            if (code < 0)
333
0
                return code;
334
335
3.33M
# endif
336
337
3.33M
#define rational_floor(tl)\
338
3.33M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
3.33M
#define STEP_LINE(ix, tl)\
340
3.33M
  tl.x += tl.ldi;\
341
3.33M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
3.33M
  ix = rational_floor(tl)
343
344
3.33M
        rxl = rational_floor(l);
345
3.33M
        rxr = rational_floor(r);
346
3.33M
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
43.9M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
43.9M
#     if LINEAR_COLOR
349
43.9M
                if (rxl != rxr) {
350
9.55M
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
9.55M
                    if (code < 0)
352
0
                        goto xit;
353
9.55M
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
9.55M
                    if (code < 0)
355
0
                        goto xit;
356
9.55M
                }
357
43.9M
                if (++iy == iy1)
358
3.33M
                    break;
359
40.5M
                STEP_LINE(rxl, l);
360
40.5M
                STEP_LINE(rxr, r);
361
40.5M
                step_gradient(&lg, num_components);
362
40.5M
                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
40.5M
        }
379
# if !LINEAR_COLOR
380
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
3.33M
            code = 0;
383
3.33M
# endif
384
3.33M
#undef STEP_LINE
385
3.33M
#undef SET_MINIMAL_WIDTH
386
3.33M
#undef CONNECT_RECTANGLES
387
3.33M
#undef FILL_TRAP_RECT
388
3.33M
#undef FILL_TRAP_RECT_DIRECT
389
3.33M
#undef FILL_TRAP_RECT_INRECT
390
3.33M
#undef YMULT_QUO
391
3.33M
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
3.33M
        return_if_interrupt(dev->memory);
394
3.33M
        return code;
395
3.33M
    }
396
3.33M
}
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