Coverage Report

Created: 2025-06-10 06:49

/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
86.0k
{
138
86.0k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
86.0k
    const fixed ymax = fixed_pixround(ytop);
140
141
86.0k
    if (ymin >= ymax)
142
4
        return 0;    /* no scan lines to sample */
143
86.0k
    {
144
86.0k
        int iy = fixed2int_var(ymin);
145
86.0k
        const int iy1 = fixed2int_var(ymax);
146
86.0k
        trap_line l, r;
147
86.0k
        register int rxl, rxr;
148
#if !LINEAR_COLOR
149
        int ry;
150
#endif
151
86.0k
        const fixed
152
86.0k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
86.0k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
86.0k
        const fixed /* partial pixel offset to first line to sample */
155
86.0k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
86.0k
        fixed fxl;
157
86.0k
        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
86.0k
            dev_proc_fill_rectangle((*fill_rect)) =
179
86.0k
                dev_proc(dev, fill_rectangle);
180
# endif
181
182
86.0k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
86.0k
        l.h = left->end.y - left->start.y;
185
86.0k
        if (l.h == 0)
186
0
           return 0;
187
86.0k
        r.h = right->end.y - right->start.y;
188
86.0k
        if (r.h == 0)
189
0
           return 0;
190
86.0k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
86.0k
        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
86.0k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
86.0k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
8.37k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
86.0k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
5.90M
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
5.90M
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
0
        (!(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
5.91M
        (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
86.0k
#define YMULT_QUO(ys, tl)\
228
94.7k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
94.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
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
10.5M
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
11.6M
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
#endif
264
86.0k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
36.8k
            l.di = 0, l.df = 0;
267
36.8k
            fxl = 0;
268
49.2k
        } else {
269
49.2k
            compute_dx(&l, dxl, ysl);
270
49.2k
            fxl = YMULT_QUO(ysl, l);
271
49.2k
            l.x += fxl;
272
49.2k
        }
273
86.0k
        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
35.5k
                if (l.di == 0 && l.df == 0) {
278
31.9k
                    rxl = fixed2int_var(l.x);
279
31.9k
                    rxr = fixed2int_var(r.x);
280
31.9k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
31.9k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
31.9k
                    goto xit;
283
31.9k
                }
284
3.67k
#     endif
285
3.67k
            r.di = 0, r.df = 0;
286
3.67k
        }
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
50.5k
        else if (dxr == dxl && fxl != 0) {
292
6.62k
            if (l.di == 0)
293
3.15k
                r.di = 0, r.df = l.df;
294
3.47k
            else
295
3.47k
                compute_dx(&r, dxr, ysr);
296
6.62k
            if (ysr == ysl && r.h == l.h)
297
5.03k
                r.x += fxl;
298
1.59k
            else
299
1.59k
                r.x += YMULT_QUO(ysr, r);
300
43.8k
        } else {
301
43.8k
            compute_dx(&r, dxr, ysr);
302
43.8k
            r.x += YMULT_QUO(ysr, r);
303
43.8k
        }
304
        /* Compute one line's worth of dx/dy. */
305
54.1k
        compute_ldx(&l, ysl);
306
54.1k
        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
54.1k
        l.x += fixed_epsilon;
310
54.1k
        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
0
            if (code < 0)
330
0
                return code;
331
0
            code = init_gradient(&rg, fa, right, left, &r, ymin, num_components);
332
0
            if (code < 0)
333
0
                return code;
334
335
0
# endif
336
337
0
#define rational_floor(tl)\
338
21.1M
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
0
#define STEP_LINE(ix, tl)\
340
20.9M
  tl.x += tl.ldi;\
341
20.9M
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
20.9M
  ix = rational_floor(tl)
343
344
54.1k
        rxl = rational_floor(l);
345
54.1k
        rxr = rational_floor(r);
346
54.1k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
10.5M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
0
                if (rxl != rxr) {
350
0
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
0
                    if (code < 0)
352
0
                        goto xit;
353
0
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
0
                    if (code < 0)
355
0
                        goto xit;
356
0
                }
357
0
                if (++iy == iy1)
358
0
                    break;
359
0
                STEP_LINE(rxl, l);
360
0
                STEP_LINE(rxr, r);
361
0
                step_gradient(&lg, num_components);
362
0
                step_gradient(&rg, num_components);
363
#     else
364
                register int ixl, ixr;
365
366
10.4M
                STEP_LINE(ixl, l);
367
10.4M
                STEP_LINE(ixr, r);
368
10.4M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
10.4M
                if (ixl != rxl || ixr != rxr) {
370
5.82M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
5.82M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
5.82M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
5.82M
                    if (code < 0)
374
0
                        goto xit;
375
5.82M
                    rxl = ixl, rxr = ixr, ry = iy;
376
5.82M
                }
377
#     endif
378
0
        }
379
# if !LINEAR_COLOR
380
54.1k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
0
            code = 0;
383
0
# endif
384
0
#undef STEP_LINE
385
0
#undef SET_MINIMAL_WIDTH
386
0
#undef CONNECT_RECTANGLES
387
0
#undef FILL_TRAP_RECT
388
0
#undef FILL_TRAP_RECT_DIRECT
389
0
#undef FILL_TRAP_RECT_INRECT
390
0
#undef YMULT_QUO
391
86.0k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
86.0k
        return_if_interrupt(dev->memory);
394
86.0k
        return code;
395
86.0k
    }
396
86.0k
}
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
1.05k
{
138
1.05k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
1.05k
    const fixed ymax = fixed_pixround(ytop);
140
141
1.05k
    if (ymin >= ymax)
142
4
        return 0;    /* no scan lines to sample */
143
1.05k
    {
144
1.05k
        int iy = fixed2int_var(ymin);
145
1.05k
        const int iy1 = fixed2int_var(ymax);
146
1.05k
        trap_line l, r;
147
1.05k
        register int rxl, rxr;
148
1.05k
#if !LINEAR_COLOR
149
1.05k
        int ry;
150
1.05k
#endif
151
1.05k
        const fixed
152
1.05k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
1.05k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
1.05k
        const fixed /* partial pixel offset to first line to sample */
155
1.05k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
1.05k
        fixed fxl;
157
1.05k
        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.05k
            gx_color_index cindex = pdevc->colors.pure;
178
1.05k
            dev_proc_fill_rectangle((*fill_rect)) =
179
1.05k
                dev_proc(dev, fill_rectangle);
180
1.05k
# endif
181
182
1.05k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
1.05k
        l.h = left->end.y - left->start.y;
185
1.05k
        if (l.h == 0)
186
0
           return 0;
187
1.05k
        r.h = right->end.y - right->start.y;
188
1.05k
        if (r.h == 0)
189
0
           return 0;
190
1.05k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
1.05k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
1.05k
#if !LINEAR_COLOR
193
1.05k
        ry = iy;
194
1.05k
#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.05k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
1.05k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
1.05k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
1.05k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
1.05k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
1.05k
   (*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.05k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
1.05k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
1.05k
#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.05k
#define YMULT_QUO(ys, tl)\
228
1.05k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
1.05k
   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.05k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
1.05k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
1.05k
#endif
264
1.05k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
0
            l.di = 0, l.df = 0;
267
0
            fxl = 0;
268
1.05k
        } else {
269
1.05k
            compute_dx(&l, dxl, ysl);
270
1.05k
            fxl = YMULT_QUO(ysl, l);
271
1.05k
            l.x += fxl;
272
1.05k
        }
273
1.05k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
0
#     if !LINEAR_COLOR
277
0
                if (l.di == 0 && l.df == 0) {
278
0
                    rxl = fixed2int_var(l.x);
279
0
                    rxr = fixed2int_var(r.x);
280
0
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
0
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
0
                    goto xit;
283
0
                }
284
0
#     endif
285
0
            r.di = 0, r.df = 0;
286
0
        }
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.05k
        else if (dxr == dxl && fxl != 0) {
292
935
            if (l.di == 0)
293
485
                r.di = 0, r.df = l.df;
294
450
            else
295
450
                compute_dx(&r, dxr, ysr);
296
935
            if (ysr == ysl && r.h == l.h)
297
935
                r.x += fxl;
298
0
            else
299
0
                r.x += YMULT_QUO(ysr, r);
300
935
        } else {
301
118
            compute_dx(&r, dxr, ysr);
302
118
            r.x += YMULT_QUO(ysr, r);
303
118
        }
304
        /* Compute one line's worth of dx/dy. */
305
1.05k
        compute_ldx(&l, ysl);
306
1.05k
        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.05k
        l.x += fixed_epsilon;
310
1.05k
        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.05k
#define rational_floor(tl)\
338
1.05k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
1.05k
#define STEP_LINE(ix, tl)\
340
1.05k
  tl.x += tl.ldi;\
341
1.05k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
1.05k
  ix = rational_floor(tl)
343
344
1.05k
        rxl = rational_floor(l);
345
1.05k
        rxr = rational_floor(r);
346
1.05k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
1.12M
        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.12M
                register int ixl, ixr;
365
366
1.12M
                STEP_LINE(ixl, l);
367
1.12M
                STEP_LINE(ixr, r);
368
1.12M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
1.12M
                if (ixl != rxl || ixr != rxr) {
370
458k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
458k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
458k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
458k
                    if (code < 0)
374
0
                        goto xit;
375
458k
                    rxl = ixl, rxr = ixr, ry = iy;
376
458k
                }
377
1.12M
#     endif
378
1.12M
        }
379
1.05k
# if !LINEAR_COLOR
380
1.05k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
1.05k
#undef STEP_LINE
385
1.05k
#undef SET_MINIMAL_WIDTH
386
1.05k
#undef CONNECT_RECTANGLES
387
1.05k
#undef FILL_TRAP_RECT
388
1.05k
#undef FILL_TRAP_RECT_DIRECT
389
1.05k
#undef FILL_TRAP_RECT_INRECT
390
1.05k
#undef YMULT_QUO
391
1.05k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
1.05k
        return_if_interrupt(dev->memory);
394
1.05k
        return code;
395
1.05k
    }
396
1.05k
}
gdevddrw.c:gx_fill_trapezoid_as_nd
Line
Count
Source
137
264
{
138
264
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
264
    const fixed ymax = fixed_pixround(ytop);
140
141
264
    if (ymin >= ymax)
142
0
        return 0;   /* no scan lines to sample */
143
264
    {
144
264
        int iy = fixed2int_var(ymin);
145
264
        const int iy1 = fixed2int_var(ymax);
146
264
        trap_line l, r;
147
264
        register int rxl, rxr;
148
264
#if !LINEAR_COLOR
149
264
        int ry;
150
264
#endif
151
264
        const fixed
152
264
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
264
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
264
        const fixed /* partial pixel offset to first line to sample */
155
264
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
264
        fixed fxl;
157
264
        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
264
            gx_color_index cindex = pdevc->colors.pure;
178
264
            dev_proc_fill_rectangle((*fill_rect)) =
179
264
                dev_proc(dev, fill_rectangle);
180
264
# endif
181
182
264
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
264
        l.h = left->end.y - left->start.y;
185
264
        if (l.h == 0)
186
0
           return 0;
187
264
        r.h = right->end.y - right->start.y;
188
264
        if (r.h == 0)
189
0
           return 0;
190
264
        l.x = x0l + (fixed_half - fixed_epsilon);
191
264
        r.x = x0r + (fixed_half - fixed_epsilon);
192
264
#if !LINEAR_COLOR
193
264
        ry = iy;
194
264
#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
264
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
264
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
264
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
264
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
264
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
264
   (*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
264
#   define FILL_TRAP_RECT(x,y,w,h)\
214
264
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
264
#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
264
#define YMULT_QUO(ys, tl)\
228
264
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
264
   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
264
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
264
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
264
#endif
264
264
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
264
            l.di = 0, l.df = 0;
267
264
            fxl = 0;
268
264
        } else {
269
0
            compute_dx(&l, dxl, ysl);
270
0
            fxl = YMULT_QUO(ysl, l);
271
0
            l.x += fxl;
272
0
        }
273
264
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
264
#     if !LINEAR_COLOR
277
264
                if (l.di == 0 && l.df == 0) {
278
264
                    rxl = fixed2int_var(l.x);
279
264
                    rxr = fixed2int_var(r.x);
280
264
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
264
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
264
                    goto xit;
283
264
                }
284
0
#     endif
285
0
            r.di = 0, r.df = 0;
286
0
        }
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
0
        else if (dxr == dxl && fxl != 0) {
292
0
            if (l.di == 0)
293
0
                r.di = 0, r.df = l.df;
294
0
            else
295
0
                compute_dx(&r, dxr, ysr);
296
0
            if (ysr == ysl && r.h == l.h)
297
0
                r.x += fxl;
298
0
            else
299
0
                r.x += YMULT_QUO(ysr, r);
300
0
        } else {
301
0
            compute_dx(&r, dxr, ysr);
302
0
            r.x += YMULT_QUO(ysr, r);
303
0
        }
304
        /* Compute one line's worth of dx/dy. */
305
0
        compute_ldx(&l, ysl);
306
0
        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
0
        l.x += fixed_epsilon;
310
0
        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
0
#define rational_floor(tl)\
338
0
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
0
#define STEP_LINE(ix, tl)\
340
0
  tl.x += tl.ldi;\
341
0
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
0
  ix = rational_floor(tl)
343
344
0
        rxl = rational_floor(l);
345
0
        rxr = rational_floor(r);
346
0
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
0
        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
0
                register int ixl, ixr;
365
366
0
                STEP_LINE(ixl, l);
367
0
                STEP_LINE(ixr, r);
368
0
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
0
                if (ixl != rxl || ixr != rxr) {
370
0
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
0
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
0
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
0
                    if (code < 0)
374
0
                        goto xit;
375
0
                    rxl = ixl, rxr = ixr, ry = iy;
376
0
                }
377
0
#     endif
378
0
        }
379
0
# if !LINEAR_COLOR
380
0
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
0
#undef STEP_LINE
385
0
#undef SET_MINIMAL_WIDTH
386
0
#undef CONNECT_RECTANGLES
387
0
#undef FILL_TRAP_RECT
388
0
#undef FILL_TRAP_RECT_DIRECT
389
0
#undef FILL_TRAP_RECT_INRECT
390
0
#undef YMULT_QUO
391
264
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
264
        return_if_interrupt(dev->memory);
394
264
        return code;
395
264
    }
396
264
}
gdevddrw.c:gx_fill_trapezoid_ns_fd
Line
Count
Source
137
83.3k
{
138
83.3k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
83.3k
    const fixed ymax = fixed_pixround(ytop);
140
141
83.3k
    if (ymin >= ymax)
142
0
        return 0;   /* no scan lines to sample */
143
83.3k
    {
144
83.3k
        int iy = fixed2int_var(ymin);
145
83.3k
        const int iy1 = fixed2int_var(ymax);
146
83.3k
        trap_line l, r;
147
83.3k
        register int rxl, rxr;
148
83.3k
#if !LINEAR_COLOR
149
83.3k
        int ry;
150
83.3k
#endif
151
83.3k
        const fixed
152
83.3k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
83.3k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
83.3k
        const fixed /* partial pixel offset to first line to sample */
155
83.3k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
83.3k
        fixed fxl;
157
83.3k
        int code;
158
# if CONTIGUOUS_FILL
159
            const bool peak0 = ((flags & 1) != 0);
160
            const bool peak1 = ((flags & 2) != 0);
161
            int peak_y0 = ybot + fixed_half;
162
            int peak_y1 = ytop - fixed_half;
163
# endif
164
# if LINEAR_COLOR
165
            int num_components = dev->color_info.num_components;
166
            frac31 lgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
167
            int32_t lgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
168
            int32_t lgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
169
            frac31 rgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
170
            int32_t rgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
171
            int32_t rgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
172
            frac31 xgc[GX_DEVICE_COLOR_MAX_COMPONENTS];
173
            int32_t xgf[GX_DEVICE_COLOR_MAX_COMPONENTS];
174
            int32_t xgnum[GX_DEVICE_COLOR_MAX_COMPONENTS];
175
            trap_gradient lg, rg, xg;
176
# else
177
83.3k
            gx_color_index cindex = pdevc->colors.pure;
178
83.3k
            dev_proc_fill_rectangle((*fill_rect)) =
179
83.3k
                dev_proc(dev, fill_rectangle);
180
83.3k
# endif
181
182
83.3k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
83.3k
        l.h = left->end.y - left->start.y;
185
83.3k
        if (l.h == 0)
186
0
           return 0;
187
83.3k
        r.h = right->end.y - right->start.y;
188
83.3k
        if (r.h == 0)
189
0
           return 0;
190
83.3k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
83.3k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
83.3k
#if !LINEAR_COLOR
193
83.3k
        ry = iy;
194
83.3k
#endif
195
196
/*
197
 * Free variables of FILL_TRAP_RECT:
198
 *  SWAP_AXES, pdevc, dev, fa
199
 * Free variables of FILL_TRAP_RECT_DIRECT:
200
 *  SWAP_AXES, fill_rect, dev, cindex
201
 */
202
83.3k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
83.3k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
83.3k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
83.3k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
83.3k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
83.3k
   (*fill_rect)(dev, x, y, w, h, cindex))
208
209
#if LINEAR_COLOR
210
#   define FILL_TRAP_RECT(x,y,w,h)\
211
        (!(w) ? 0 : dev_proc(dev, fill_linear_color_scanline)(dev, fa, x, y, w, xg.c, xg.f, xg.num, xg.den))
212
#else
213
83.3k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
83.3k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
83.3k
#endif
216
217
        /* Compute the dx/dy ratios. */
218
219
        /*
220
         * Compute the x offsets at the first scan line to sample.  We need
221
         * to be careful in computing ys# * dx#f {/,%} h# because the
222
         * multiplication may overflow.  We know that all the quantities
223
         * involved are non-negative, and that ys# is usually less than 1 (as
224
         * a fixed, of course); this gives us a cheap conservative check for
225
         * overflow in the multiplication.
226
         */
227
83.3k
#define YMULT_QUO(ys, tl)\
228
83.3k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
83.3k
   fixed_mult_quo(ys, tl.df, tl.h))
230
231
#if CONTIGUOUS_FILL
232
/*
233
 * If left and right boundary round to same pixel index,
234
 * we would not paing the scan and would get a dropout.
235
 * Check for this case and choose one of two pixels
236
 * which is closer to the "axis". We need to exclude
237
 * 'peak' because it would paint an excessive pixel.
238
 */
239
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) \
240
    if (ixl == ixr) \
241
        if ((!peak0 || iy >= peak_y0) && (!peak1 || iy <= peak_y1)) {\
242
            fixed x = int2fixed(ixl) + fixed_half;\
243
            if (x - l.x < r.x - x)\
244
                ++ixr;\
245
            else\
246
                --ixl;\
247
        }
248
249
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill)\
250
    if (adj1 < adj2) {\
251
        if (iy - ry > 1) {\
252
            code = fill(rxl, ry, rxr - rxl, iy - ry - 1);\
253
            if (code < 0)\
254
                goto xit;\
255
            ry = iy - 1;\
256
        }\
257
        adj1 = adj2 = (adj2 + adj2) / 2;\
258
    }
259
260
#else
261
83.3k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
83.3k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
83.3k
#endif
264
83.3k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
35.5k
            l.di = 0, l.df = 0;
267
35.5k
            fxl = 0;
268
47.8k
        } else {
269
47.8k
            compute_dx(&l, dxl, ysl);
270
47.8k
            fxl = YMULT_QUO(ysl, l);
271
47.8k
            l.x += fxl;
272
47.8k
        }
273
83.3k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
34.3k
#     if !LINEAR_COLOR
277
34.3k
                if (l.di == 0 && l.df == 0) {
278
30.6k
                    rxl = fixed2int_var(l.x);
279
30.6k
                    rxr = fixed2int_var(r.x);
280
30.6k
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
30.6k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
30.6k
                    goto xit;
283
30.6k
                }
284
3.60k
#     endif
285
3.60k
            r.di = 0, r.df = 0;
286
3.60k
        }
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
49.0k
        else if (dxr == dxl && fxl != 0) {
292
5.69k
            if (l.di == 0)
293
2.66k
                r.di = 0, r.df = l.df;
294
3.02k
            else
295
3.02k
                compute_dx(&r, dxr, ysr);
296
5.69k
            if (ysr == ysl && r.h == l.h)
297
4.09k
                r.x += fxl;
298
1.59k
            else
299
1.59k
                r.x += YMULT_QUO(ysr, r);
300
43.3k
        } else {
301
43.3k
            compute_dx(&r, dxr, ysr);
302
43.3k
            r.x += YMULT_QUO(ysr, r);
303
43.3k
        }
304
        /* Compute one line's worth of dx/dy. */
305
52.6k
        compute_ldx(&l, ysl);
306
52.6k
        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
52.6k
        l.x += fixed_epsilon;
310
52.6k
        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
52.6k
#define rational_floor(tl)\
338
52.6k
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
52.6k
#define STEP_LINE(ix, tl)\
340
52.6k
  tl.x += tl.ldi;\
341
52.6k
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
52.6k
  ix = rational_floor(tl)
343
344
52.6k
        rxl = rational_floor(l);
345
52.6k
        rxr = rational_floor(r);
346
52.6k
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
9.41M
        while (LINEAR_COLOR ? 1 : ++iy != iy1) {
348
#     if LINEAR_COLOR
349
                if (rxl != rxr) {
350
                    code = set_x_gradient(&xg, &lg, &rg, &l, &r, rxl, rxr, num_components);
351
                    if (code < 0)
352
                        goto xit;
353
                    code = FILL_TRAP_RECT(rxl, iy, rxr - rxl, 1);
354
                    if (code < 0)
355
                        goto xit;
356
                }
357
                if (++iy == iy1)
358
                    break;
359
                STEP_LINE(rxl, l);
360
                STEP_LINE(rxr, r);
361
                step_gradient(&lg, num_components);
362
                step_gradient(&rg, num_components);
363
#     else
364
9.36M
                register int ixl, ixr;
365
366
9.36M
                STEP_LINE(ixl, l);
367
9.36M
                STEP_LINE(ixr, r);
368
9.36M
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
9.36M
                if (ixl != rxl || ixr != rxr) {
370
5.35M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
5.35M
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
5.35M
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
5.35M
                    if (code < 0)
374
0
                        goto xit;
375
5.35M
                    rxl = ixl, rxr = ixr, ry = iy;
376
5.35M
                }
377
9.36M
#     endif
378
9.36M
        }
379
52.6k
# if !LINEAR_COLOR
380
52.6k
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
52.6k
#undef STEP_LINE
385
52.6k
#undef SET_MINIMAL_WIDTH
386
52.6k
#undef CONNECT_RECTANGLES
387
52.6k
#undef FILL_TRAP_RECT
388
52.6k
#undef FILL_TRAP_RECT_DIRECT
389
52.6k
#undef FILL_TRAP_RECT_INRECT
390
52.6k
#undef YMULT_QUO
391
83.3k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
83.3k
        return_if_interrupt(dev->memory);
394
83.3k
        return code;
395
83.3k
    }
396
83.3k
}
gdevddrw.c:gx_fill_trapezoid_ns_nd
Line
Count
Source
137
1.39k
{
138
1.39k
    const fixed ymin = fixed_pixround(ybot) + fixed_half;
139
1.39k
    const fixed ymax = fixed_pixround(ytop);
140
141
1.39k
    if (ymin >= ymax)
142
0
        return 0;   /* no scan lines to sample */
143
1.39k
    {
144
1.39k
        int iy = fixed2int_var(ymin);
145
1.39k
        const int iy1 = fixed2int_var(ymax);
146
1.39k
        trap_line l, r;
147
1.39k
        register int rxl, rxr;
148
1.39k
#if !LINEAR_COLOR
149
1.39k
        int ry;
150
1.39k
#endif
151
1.39k
        const fixed
152
1.39k
            x0l = left->start.x, x1l = left->end.x, x0r = right->start.x,
153
1.39k
            x1r = right->end.x, dxl = x1l - x0l, dxr = x1r - x0r;
154
1.39k
        const fixed /* partial pixel offset to first line to sample */
155
1.39k
            ysl = ymin - left->start.y, ysr = ymin - right->start.y;
156
1.39k
        fixed fxl;
157
1.39k
        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.39k
            gx_color_index cindex = pdevc->colors.pure;
178
1.39k
            dev_proc_fill_rectangle((*fill_rect)) =
179
1.39k
                dev_proc(dev, fill_rectangle);
180
1.39k
# endif
181
182
1.39k
        if_debug2m('z', dev->memory, "[z]y=[%d,%d]\n", iy, iy1);
183
184
1.39k
        l.h = left->end.y - left->start.y;
185
1.39k
        if (l.h == 0)
186
0
           return 0;
187
1.39k
        r.h = right->end.y - right->start.y;
188
1.39k
        if (r.h == 0)
189
0
           return 0;
190
1.39k
        l.x = x0l + (fixed_half - fixed_epsilon);
191
1.39k
        r.x = x0r + (fixed_half - fixed_epsilon);
192
1.39k
#if !LINEAR_COLOR
193
1.39k
        ry = iy;
194
1.39k
#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.39k
#define FILL_TRAP_RECT_INDIRECT(x,y,w,h)\
203
1.39k
  (SWAP_AXES ? gx_fill_rectangle_device_rop(y, x, h, w, pdevc, dev, fa) :\
204
1.39k
   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, fa))
205
1.39k
#define FILL_TRAP_RECT_DIRECT(x,y,w,h)\
206
1.39k
  (SWAP_AXES ? (*fill_rect)(dev, y, x, h, w, cindex) :\
207
1.39k
   (*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.39k
#   define FILL_TRAP_RECT(x,y,w,h)\
214
1.39k
        (FILL_DIRECT ? FILL_TRAP_RECT_DIRECT(x,y,w,h) : FILL_TRAP_RECT_INDIRECT(x,y,w,h))
215
1.39k
#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.39k
#define YMULT_QUO(ys, tl)\
228
1.39k
  (ys < fixed_1 && tl.df < YMULT_LIMIT ? ys * tl.df / tl.h :\
229
1.39k
   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.39k
#define SET_MINIMAL_WIDTH(ixl, ixr, l, r) DO_NOTHING
262
1.39k
#define CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, adj1, adj2, fill) DO_NOTHING
263
1.39k
#endif
264
1.39k
        if (fixed_floor(l.x) == fixed_pixround(x1l)) {
265
            /* Left edge is vertical, we don't need to increment. */
266
984
            l.di = 0, l.df = 0;
267
984
            fxl = 0;
268
984
        } else {
269
409
            compute_dx(&l, dxl, ysl);
270
409
            fxl = YMULT_QUO(ysl, l);
271
409
            l.x += fxl;
272
409
        }
273
1.39k
        if (fixed_floor(r.x) == fixed_pixround(x1r)) {
274
            /* Right edge is vertical.  If both are vertical, */
275
            /* we have a rectangle. */
276
1.00k
#     if !LINEAR_COLOR
277
1.00k
                if (l.di == 0 && l.df == 0) {
278
940
                    rxl = fixed2int_var(l.x);
279
940
                    rxr = fixed2int_var(r.x);
280
940
                    SET_MINIMAL_WIDTH(rxl, rxr, l, r);
281
940
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy1 - ry);
282
940
                    goto xit;
283
940
                }
284
65
#     endif
285
65
            r.di = 0, r.df = 0;
286
65
        }
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
388
        else if (dxr == dxl && fxl != 0) {
292
0
            if (l.di == 0)
293
0
                r.di = 0, r.df = l.df;
294
0
            else
295
0
                compute_dx(&r, dxr, ysr);
296
0
            if (ysr == ysl && r.h == l.h)
297
0
                r.x += fxl;
298
0
            else
299
0
                r.x += YMULT_QUO(ysr, r);
300
388
        } else {
301
388
            compute_dx(&r, dxr, ysr);
302
388
            r.x += YMULT_QUO(ysr, r);
303
388
        }
304
        /* Compute one line's worth of dx/dy. */
305
453
        compute_ldx(&l, ysl);
306
453
        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
453
        l.x += fixed_epsilon;
310
453
        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
453
#define rational_floor(tl)\
338
453
  fixed2int_var(fixed_is_int(tl.x) && tl.xf == -tl.h ? tl.x - fixed_1 : tl.x)
339
453
#define STEP_LINE(ix, tl)\
340
453
  tl.x += tl.ldi;\
341
453
  if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= tl.h, tl.x++;\
342
453
  ix = rational_floor(tl)
343
344
453
        rxl = rational_floor(l);
345
453
        rxr = rational_floor(r);
346
453
        SET_MINIMAL_WIDTH(rxl, rxr, l, r);
347
10.3k
        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.88k
                register int ixl, ixr;
365
366
9.88k
                STEP_LINE(ixl, l);
367
9.88k
                STEP_LINE(ixr, r);
368
9.88k
                SET_MINIMAL_WIDTH(ixl, ixr, l, r);
369
9.88k
                if (ixl != rxl || ixr != rxr) {
370
6.72k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, rxr, ixl, FILL_TRAP_RECT);
371
6.72k
                    CONNECT_RECTANGLES(ixl, ixr, rxl, rxr, iy, ry, ixr, rxl, FILL_TRAP_RECT);
372
6.72k
                    code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
373
6.72k
                    if (code < 0)
374
0
                        goto xit;
375
6.72k
                    rxl = ixl, rxr = ixr, ry = iy;
376
6.72k
                }
377
9.88k
#     endif
378
9.88k
        }
379
453
# if !LINEAR_COLOR
380
453
            code = FILL_TRAP_RECT(rxl, ry, rxr - rxl, iy - ry);
381
# else
382
            code = 0;
383
# endif
384
453
#undef STEP_LINE
385
453
#undef SET_MINIMAL_WIDTH
386
453
#undef CONNECT_RECTANGLES
387
453
#undef FILL_TRAP_RECT
388
453
#undef FILL_TRAP_RECT_DIRECT
389
453
#undef FILL_TRAP_RECT_INRECT
390
453
#undef YMULT_QUO
391
1.39k
xit:  if (code < 0 && FILL_DIRECT)
392
0
            return_error(code);
393
1.39k
        return_if_interrupt(dev->memory);
394
1.39k
        return code;
395
1.39k
    }
396
1.39k
}
Unexecuted instantiation: gdevddrw.c:gx_fill_trapezoid_as_lc
Unexecuted instantiation: gdevddrw.c:gx_fill_trapezoid_ns_lc
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