Coverage Report

Created: 2025-11-16 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gdevdsha.c
Line
Count
Source
1
/* Copyright (C) 2001-2025 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
/* Default shading drawing device procedures. */
17
18
#include "gx.h"
19
#include "gserrors.h"
20
#include "gxdevice.h"
21
#include "gxcindex.h"
22
#include "gxdevsop.h"
23
24
static bool
25
gx_devn_diff(frac31 devn1[], frac31 devn2[], int num)
26
37.0M
{
27
37.0M
    int k;
28
29
61.0M
    for (k = 0; k < num; k++) {
30
59.4M
        if (devn1[k] != devn2[k]) {
31
35.4M
            return true;
32
35.4M
        }
33
59.4M
    }
34
1.59M
    return false;
35
37.0M
}
36
37
int
38
gx_hl_fill_linear_color_scanline(gx_device *dev, const gs_fill_attributes *fa,
39
        int i0, int j, int w, const frac31 *c0, const int32_t *c0f,
40
        const int32_t *cg_num, int32_t cg_den)
41
16.2M
{
42
16.2M
    frac31 c[GX_DEVICE_COLOR_MAX_COMPONENTS];
43
16.2M
    frac31 curr[GX_DEVICE_COLOR_MAX_COMPONENTS];
44
16.2M
    ulong f[GX_DEVICE_COLOR_MAX_COMPONENTS];
45
16.2M
    int i, i1 = i0 + w, bi = i0, k;
46
16.2M
    const gx_device_color_info *cinfo = &dev->color_info;
47
16.2M
    int n = cinfo->num_components;
48
16.2M
    int si, ei, di, code;
49
16.2M
    gs_fixed_rect rect;
50
16.2M
    gx_device_color devc;
51
52
    /* Note: All the stepping math is done with frac color values */
53
54
16.2M
    devc.type = gx_dc_type_devn;
55
994M
    for (i = n; i < GX_DEVICE_COLOR_MAX_COMPONENTS; i++)
56
978M
         devc.colors.devn.values[i] = 0;
57
58
16.2M
    if (j < fixed2int(fa->clip->p.y) ||
59
16.2M
            j > fixed2int_ceiling(fa->clip->q.y)) /* Must be compatible to the clipping logic. */
60
0
        return 0;
61
75.3M
    for (k = 0; k < n; k++) {
62
59.1M
        curr[k] = c[k] = c0[k];
63
59.1M
        f[k] = c0f[k];
64
59.1M
    }
65
53.2M
    for (i = i0 + 1, di = 1; i < i1; i += di) {
66
37.0M
        if (di == 1) {
67
            /* Advance colors by 1 pixel. */
68
157M
            for (k = 0; k < n; k++) {
69
120M
                if (cg_num[k]) {
70
84.7M
                    int32_t m = f[k] + cg_num[k];
71
72
84.7M
                    c[k] += m / cg_den;
73
84.7M
                    m -= m / cg_den * cg_den;
74
84.7M
                    if (m < 0) {
75
38.6M
                        c[k]--;
76
38.6M
                        m += cg_den;
77
38.6M
                    }
78
84.7M
                    f[k] = m;
79
84.7M
                }
80
120M
            }
81
37.0M
        } else {
82
            /* Advance colors by di pixels. */
83
0
            for (k = 0; k < n; k++) {
84
0
                if (cg_num[k]) {
85
0
                    int64_t M = f[k] + (int64_t)cg_num[k] * di;
86
0
                    int32_t m;
87
88
0
                    c[k] += (frac31)(M / cg_den);
89
0
                    m = (int32_t)(M - M / cg_den * cg_den);
90
0
                    if (m < 0) {
91
0
                        c[k]--;
92
0
                        m += cg_den;
93
0
                    }
94
0
                    f[k] = m;
95
0
                }
96
0
            }
97
0
        }
98
37.0M
        if (gx_devn_diff(c, curr, n)) {
99
35.4M
            si = max(bi, fixed2int(fa->clip->p.x));     /* Must be compatible to the clipping logic. */
100
35.4M
            ei = min(i, fixed2int_ceiling(fa->clip->q.x));  /* Must be compatible to the clipping logic. */
101
35.4M
            if (si < ei) {
102
33.7M
                if (fa->swap_axes) {
103
0
                    rect.p.x = int2fixed(j);
104
0
                    rect.p.y = int2fixed(si);
105
0
                    rect.q.x = int2fixed(j + 1);
106
0
                    rect.q.y = int2fixed(ei);
107
33.7M
                } else {
108
33.7M
                    rect.p.x = int2fixed(si);
109
33.7M
                    rect.p.y = int2fixed(j);
110
33.7M
                    rect.q.x = int2fixed(ei);
111
33.7M
                    rect.q.y = int2fixed(j + 1);
112
33.7M
                }
113
143M
                for (k = 0; k < n; k++) {
114
109M
                    devc.colors.devn.values[k] = frac312cv(curr[k]);
115
109M
                }
116
33.7M
                devc.tag = device_current_tag(dev);
117
33.7M
                code = dev_proc(dev, fill_rectangle_hl_color) (dev, &rect, NULL, &devc, NULL);
118
33.7M
                if (code < 0)
119
0
                    return code;
120
33.7M
            }
121
35.4M
            bi = i;
122
151M
            for (k = 0; k < n; k++) {
123
115M
                curr[k] = c[k];
124
115M
            }
125
35.4M
            di = 1;
126
35.4M
        } else if (i == i1) {
127
0
            i++;
128
0
            break;
129
1.59M
        } else {
130
            /* Compute a color change pixel analytically. */
131
1.59M
            di = i1 - i;
132
6.75M
            for (k = 0; k < n; k++) {
133
5.15M
                int32_t a;
134
5.15M
                int64_t x;
135
5.15M
                frac31 v = 1 << (31 - cinfo->comp_bits[k]); /* Color index precision in frac31. */
136
5.15M
                frac31 u = c[k] & (v - 1);
137
138
5.15M
                if (cg_num[k] == 0) {
139
                    /* No change. */
140
5.15M
                    continue;
141
5.15M
                } if (cg_num[k] > 0) {
142
                    /* Solve[(f[k] + cg_num[k]*x)/cg_den == v - u, x]  */
143
0
                    a = v - u;
144
0
                } else {
145
                    /* Solve[(f[k] + cg_num[k]*x)/cg_den == - u - 1, x]  */
146
0
                    a = -u - 1;
147
0
                }
148
0
                x = ((int64_t)a * cg_den - f[k]) / cg_num[k];
149
0
                if (i + x >= i1)
150
0
                    continue;
151
0
                else if (x < 0)
152
0
                    return_error(gs_error_unregistered); /* Must not happen. */
153
0
                else if (di > (int)x) {
154
0
                    di = (int)x;
155
0
                    if (di <= 1) {
156
0
                        di = 1;
157
0
                        break;
158
0
                    }
159
0
                }
160
0
            }
161
1.59M
        }
162
37.0M
    }
163
16.2M
    si = max(bi, fixed2int(fa->clip->p.x));     /* Must be compatible to the clipping logic. */
164
16.2M
    ei = min(i, fixed2int_ceiling(fa->clip->q.x));  /* Must be compatible to the clipping logic. */
165
16.2M
    if (si < ei) {
166
14.7M
        if (fa->swap_axes) {
167
4.10M
            rect.p.x = int2fixed(j);
168
4.10M
            rect.p.y = int2fixed(si);
169
4.10M
            rect.q.x = int2fixed(j + 1);
170
4.10M
            rect.q.y = int2fixed(ei);
171
10.6M
        } else {
172
10.6M
            rect.p.x = int2fixed(si);
173
10.6M
            rect.p.y = int2fixed(j);
174
10.6M
            rect.q.x = int2fixed(ei);
175
10.6M
            rect.q.y = int2fixed(j + 1);
176
10.6M
        }
177
68.5M
        for (k = 0; k < n; k++) {
178
53.8M
            devc.colors.devn.values[k] = frac312cv(curr[k]);
179
53.8M
        }
180
14.7M
        devc.tag = device_current_tag(dev);
181
14.7M
        return dev_proc(dev, fill_rectangle_hl_color) (dev, &rect, NULL, &devc, NULL);
182
14.7M
    }
183
1.49M
    return 0;
184
16.2M
}
185
186
int
187
gx_default_fill_linear_color_scanline(gx_device *dev, const gs_fill_attributes *fa,
188
        int i0, int j, int w,
189
        const frac31 *c0, const int32_t *c0f, const int32_t *cg_num, int32_t cg_den)
190
153M
{
191
    /* This default implementation decomposes the area into constant color rectangles.
192
       Devices may supply optimized implementations with
193
       the inversed nesting of the i,k cicles,
194
       i.e. with enumerating planes first, with a direct writing to the raster,
195
       and with a fixed bits per component.
196
     */
197
    /* First determine if we are doing high level style colors or pure colors */
198
153M
    bool devn = dev_proc(dev, dev_spec_op)(dev, gxdso_supports_devn, NULL, 0);
199
153M
    frac31 c[GX_DEVICE_COLOR_MAX_COMPONENTS];
200
153M
    ulong f[GX_DEVICE_COLOR_MAX_COMPONENTS];
201
153M
    int i, i1 = i0 + w, bi = i0, k;
202
153M
    gx_color_index ci0 = 0, ci1;
203
153M
    const gx_device_color_info *cinfo = &dev->color_info;
204
153M
    int n = cinfo->num_components;
205
153M
    int si, ei, di, code;
206
207
    /* Todo: set this up to vector earlier */
208
153M
    if (devn)  /* Note, PDF14 could be additive and doing devn */
209
16.2M
        return gx_hl_fill_linear_color_scanline(dev, fa, i0, j, w, c0, c0f,
210
16.2M
                                                cg_num, cg_den);
211
137M
    if (j < fixed2int(fa->clip->p.y) ||
212
137M
            j > fixed2int_ceiling(fa->clip->q.y)) /* Must be compatible to the clipping logic. */
213
0
        return 0;
214
508M
    for (k = 0; k < n; k++) {
215
371M
        int shift = cinfo->comp_shift[k];
216
371M
        int bits = cinfo->comp_bits[k];
217
218
371M
        c[k] = c0[k];
219
371M
        f[k] = c0f[k];
220
371M
        ci0 |= (gx_color_index)(c[k] >> (sizeof(c[k]) * 8 - 1 - bits)) << shift;
221
371M
    }
222
2.97G
    for (i = i0 + 1, di = 1; i < i1; i += di) {
223
2.83G
        if (di == 1) {
224
            /* Advance colors by 1 pixel. */
225
2.80G
            ci1 = 0;
226
7.42G
            for (k = 0; k < n; k++) {
227
4.62G
                int shift = cinfo->comp_shift[k];
228
4.62G
                int bits = cinfo->comp_bits[k];
229
230
4.62G
                if (cg_num[k]) {
231
3.40G
                    int32_t m = f[k] + cg_num[k];
232
233
3.40G
                    c[k] += m / cg_den;
234
3.40G
                    m -= m / cg_den * cg_den;
235
3.40G
                    if (m < 0) {
236
2.81G
                        c[k]--;
237
2.81G
                        m += cg_den;
238
2.81G
                    }
239
3.40G
                    f[k] = m;
240
3.40G
                }
241
4.62G
                ci1 |= (gx_color_index)(c[k] >> (sizeof(c[k]) * 8 - 1 - bits)) << shift;
242
4.62G
            }
243
2.80G
        } else {
244
            /* Advance colors by di pixels. */
245
30.0M
            ci1 = 0;
246
77.5M
            for (k = 0; k < n; k++) {
247
47.4M
                int shift = cinfo->comp_shift[k];
248
47.4M
                int bits = cinfo->comp_bits[k];
249
250
47.4M
                if (cg_num[k]) {
251
30.5M
                    int64_t M = f[k] + (int64_t)cg_num[k] * di;
252
30.5M
                    int32_t m;
253
254
30.5M
                    c[k] += (frac31)(M / cg_den);
255
30.5M
                    m = (int32_t)(M - M / cg_den * cg_den);
256
30.5M
                    if (m < 0) {
257
0
                        c[k]--;
258
0
                        m += cg_den;
259
0
                    }
260
30.5M
                    f[k] = m;
261
30.5M
                }
262
47.4M
                ci1 |= (gx_color_index)(c[k] >> (sizeof(c[k]) * 8 - 1 - bits)) << shift;
263
47.4M
            }
264
30.0M
        }
265
2.83G
        if (ci1 != ci0) {
266
175M
            si = max(bi, fixed2int(fa->clip->p.x));     /* Must be compatible to the clipping logic. */
267
175M
            ei = min(i, fixed2int_ceiling(fa->clip->q.x));  /* Must be compatible to the clipping logic. */
268
175M
            if (si < ei) {
269
117M
                if (fa->swap_axes) {
270
0
                    code = dev_proc(dev, fill_rectangle)(dev, j, si, 1, ei - si, ci0);
271
117M
                } else {
272
117M
                    code = dev_proc(dev, fill_rectangle)(dev, si, j, ei - si, 1, ci0);
273
117M
                }
274
117M
                if (code < 0)
275
0
                    return code;
276
117M
            }
277
175M
            bi = i;
278
175M
            ci0 = ci1;
279
175M
            di = 1;
280
2.66G
        } else if (i == i1) {
281
0
            i++;
282
0
            break;
283
2.66G
        } else {
284
            /* Compute a color change pixel analitically. */
285
2.66G
            di = i1 - i;
286
3.45G
            for (k = 0; k < n; k++) {
287
3.41G
                int32_t a;
288
3.41G
                int64_t x;
289
3.41G
                frac31 v = 1 << (31 - cinfo->comp_bits[k]); /* Color index precision in frac31. */
290
3.41G
                frac31 u = c[k] & (v - 1);
291
292
3.41G
                if (cg_num[k] == 0) {
293
                    /* No change. */
294
582M
                    continue;
295
2.82G
                } if (cg_num[k] > 0) {
296
                    /* Solve[(f[k] + cg_num[k]*x)/cg_den == v - u, x]  */
297
286M
                    a = v - u;
298
2.54G
                } else {
299
                    /* Solve[(f[k] + cg_num[k]*x)/cg_den == - u - 1, x]  */
300
2.54G
                    a = -u - 1;
301
2.54G
                }
302
2.82G
                x = ((int64_t)a * cg_den - f[k]) / cg_num[k];
303
2.82G
                if (i + x >= i1)
304
28.9M
                    continue;
305
2.79G
                else if (x < 0)
306
0
                    return_error(gs_error_unregistered); /* Must not happen. */
307
2.79G
                else if (di > (int)x) {
308
2.73G
                    di = (int)x;
309
2.73G
                    if (di <= 1) {
310
2.61G
                        di = 1;
311
2.61G
                        break;
312
2.61G
                    }
313
2.73G
                }
314
2.82G
            }
315
2.66G
        }
316
2.83G
    }
317
137M
    si = max(bi, fixed2int(fa->clip->p.x));     /* Must be compatible to the clipping logic. */
318
137M
    ei = min(i, fixed2int_ceiling(fa->clip->q.x));  /* Must be compatible to the clipping logic. */
319
137M
    if (si < ei) {
320
110M
        if (fa->swap_axes) {
321
36.8M
            return dev_proc(dev, fill_rectangle)(dev, j, si, 1, ei - si, ci0);
322
73.3M
        } else {
323
73.3M
            return dev_proc(dev, fill_rectangle)(dev, si, j, ei - si, 1, ci0);
324
73.3M
        }
325
110M
    }
326
26.9M
    return 0;
327
137M
}