Coverage Report

Created: 2025-06-10 06:58

/src/ghostpdl/base/gxshade4.c
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
/* Rendering for Gouraud triangle shadings */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gserrors.h"
22
#include "gsmatrix.h"   /* for gscoord.h */
23
#include "gscoord.h"
24
#include "gsptype2.h"
25
#include "gxcspace.h"
26
#include "gxdcolor.h"
27
#include "gxdevcli.h"
28
#include "gxgstate.h"
29
#include "gxpath.h"
30
#include "gxshade.h"
31
#include "gxshade4.h"
32
#include "gsicc_cache.h"
33
34
/* Initialize the fill state for triangle shading. */
35
int
36
mesh_init_fill_state(mesh_fill_state_t * pfs, const gs_shading_mesh_t * psh,
37
                     const gs_fixed_rect * rect_clip, gx_device * dev,
38
                     gs_gstate * pgs)
39
46
{
40
46
    int code;
41
46
    code = shade_init_fill_state((shading_fill_state_t *) pfs,
42
46
                                 (const gs_shading_t *)psh, dev, pgs);
43
46
    if (code < 0)
44
0
        return code;
45
46
    pfs->pshm = psh;
46
46
    pfs->rect = *rect_clip;
47
46
    return 0;
48
46
}
49
50
/* ---------------- Gouraud triangle shadings ---------------- */
51
52
static int
53
Gt_next_vertex(const gs_shading_mesh_t * psh, shade_coord_stream_t * cs,
54
               shading_vertex_t * vertex, patch_color_t *c)
55
62.6k
{
56
62.6k
    int code = shade_next_vertex(cs, vertex, c);
57
62.6k
    if (code < 0)
58
15
        return code;
59
60
62.6k
    if (psh->params.Function) {
61
59.9k
        c->t[0] = c->cc.paint.values[0];
62
59.9k
        c->t[1] = 0;
63
        /* Decode the color with the function. */
64
59.9k
        code = gs_function_evaluate(psh->params.Function, c->t,
65
59.9k
                                    c->cc.paint.values);
66
59.9k
    } else
67
2.64k
        psh->params.ColorSpace->type->restrict_color(&c->cc, psh->params.ColorSpace);
68
62.6k
    return code;
69
62.6k
}
70
71
static inline int
72
Gt_fill_triangle(patch_fill_state_t * pfs, const shading_vertex_t * va,
73
                 const shading_vertex_t * vb, const shading_vertex_t * vc)
74
108k
{
75
108k
    int code = 0;
76
77
108k
    if (INTERPATCH_PADDING) {
78
108k
        code = mesh_padding(pfs, &va->p, &vb->p, va->c, vb->c);
79
108k
        if (code >= 0)
80
108k
            code = mesh_padding(pfs, &vb->p, &vc->p, vb->c, vc->c);
81
108k
        if (code >= 0)
82
108k
            code = mesh_padding(pfs, &vc->p, &va->p, vc->c, va->c);
83
108k
    }
84
108k
    if (code >= 0)
85
108k
        code = mesh_triangle(pfs, va, vb, vc);
86
108k
    return code;
87
108k
}
88
89
int
90
gs_shading_FfGt_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
91
                               const gs_fixed_rect * rect_clip,
92
                               gx_device * dev, gs_gstate * pgs)
93
26
{
94
26
    const gs_shading_FfGt_t * const psh = (const gs_shading_FfGt_t *)psh0;
95
26
    patch_fill_state_t pfs;
96
26
    const gs_shading_mesh_t *pshm = (const gs_shading_mesh_t *)psh;
97
26
    shade_coord_stream_t cs;
98
26
    int num_bits = psh->params.BitsPerFlag;
99
26
    int flag;
100
26
    shading_vertex_t va, vb, vc;
101
26
    patch_color_t *c, *C[3], *ca, *cb, *cc; /* va.c == ca && vb.c == cb && vc.c == cc always,
102
                                        provides a non-const access. */
103
26
    int code;
104
105
26
    code = shade_init_fill_state((shading_fill_state_t *)&pfs,
106
26
                                 (const gs_shading_t *)psh, dev, pgs);
107
26
    if (code < 0)
108
0
        return code;
109
26
    pfs.Function = pshm->params.Function;
110
26
    pfs.rect = *rect_clip;
111
26
    code = init_patch_fill_state(&pfs);
112
26
    if (code < 0) {
113
0
        if (pfs.icclink != NULL) gsicc_release_link(pfs.icclink);
114
0
        return code;
115
0
    }
116
26
    reserve_colors(&pfs, C, 3); /* Can't fail */
117
26
    va.c = ca = C[0];
118
26
    vb.c = cb = C[1];
119
26
    vc.c = cc = C[2];
120
26
    shade_next_init(&cs, (const gs_shading_mesh_params_t *)&psh->params,
121
26
                    pgs);
122
    /* CET 09-47J.PS SpecialTestI04Test01 does not need the color data alignment. */
123
888
    while ((flag = shade_next_flag(&cs, num_bits)) >= 0) {
124
867
        switch (flag) {
125
5
            default:
126
5
                code = gs_note_error(gs_error_rangecheck);
127
5
                goto error;
128
862
            case 0:
129
862
                if ((code = Gt_next_vertex(pshm, &cs, &va, ca)) < 0 ||
130
862
                    (code = shade_next_flag(&cs, num_bits)) < 0 ||
131
862
                    (code = Gt_next_vertex(pshm, &cs, &vb, cb)) < 0 ||
132
862
                    (code = shade_next_flag(&cs, num_bits)) < 0
133
862
                    )
134
1
                    break;
135
861
                goto v2;
136
861
            case 1:
137
0
                c = ca;
138
0
                va = vb;
139
0
                ca = cb;
140
0
                vb.c = cb = c;
141
                /* fall through */
142
0
            case 2:
143
0
                c = cb;
144
0
                vb = vc;
145
0
                cb = cc;
146
0
                vc.c = cc = c;
147
861
v2:   if ((code = Gt_next_vertex(pshm, &cs, &vc, cc)) < 0)
148
0
                    break;
149
861
                if ((code = Gt_fill_triangle(&pfs, &va, &vb, &vc)) < 0)
150
0
                    break;
151
867
        }
152
862
        cs.align(&cs, 8); /* Debugged with 12-14O.PS page 2. */
153
862
    }
154
26
error:
155
26
    release_colors(&pfs, pfs.color_stack, 3);
156
26
    if (pfs.icclink != NULL) gsicc_release_link(pfs.icclink);
157
26
    if (term_patch_fill_state(&pfs))
158
0
        return_error(gs_error_unregistered); /* Must not happen. */
159
26
    if (!cs.is_eod(&cs))
160
5
        return_error(gs_error_rangecheck);
161
21
    return code;
162
26
}
163
164
int
165
gs_shading_LfGt_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
166
                               const gs_fixed_rect * rect_clip,
167
                               gx_device * dev, gs_gstate * pgs)
168
16
{
169
16
    const gs_shading_LfGt_t * const psh = (const gs_shading_LfGt_t *)psh0;
170
16
    patch_fill_state_t pfs;
171
16
    const gs_shading_mesh_t *pshm = (const gs_shading_mesh_t *)psh;
172
16
    shade_coord_stream_t cs;
173
16
    shading_vertex_t *vertex = NULL;
174
16
    byte *color_buffer = NULL;
175
16
    patch_color_t **color_buffer_ptrs = NULL; /* non-const access to vertex[i].c */
176
16
    shading_vertex_t next;
177
16
    int per_row = psh->params.VerticesPerRow;
178
16
    patch_color_t *c, *cn; /* cn == next.c always, provides a non-contst access. */
179
16
    int i, code;
180
181
16
    code = shade_init_fill_state((shading_fill_state_t *)&pfs,
182
16
                                 (const gs_shading_t *)psh, dev, pgs);
183
16
    if (code < 0)
184
0
        return code;
185
16
    pfs.Function = pshm->params.Function;
186
16
    pfs.rect = *rect_clip;
187
16
    code = init_patch_fill_state(&pfs);
188
16
    if (code < 0)
189
0
        goto out;
190
16
    reserve_colors(&pfs, &cn, 1); /* Can't fail. */
191
16
    next.c = cn;
192
16
    shade_next_init(&cs, (const gs_shading_mesh_params_t *)&psh->params,
193
16
                    pgs);
194
16
    vertex = (shading_vertex_t *)
195
16
        gs_alloc_byte_array(pgs->memory, per_row, sizeof(*vertex),
196
16
                            "gs_shading_LfGt_render");
197
16
    if (vertex == NULL) {
198
0
        code = gs_note_error(gs_error_VMerror);
199
0
        goto out;
200
0
    }
201
16
    color_buffer = gs_alloc_bytes(pgs->memory,
202
16
                                  (size_t)pfs.color_stack_step * per_row,
203
16
                                  "gs_shading_LfGt_fill_rectangle");
204
16
    if (color_buffer == NULL) {
205
0
        code = gs_note_error(gs_error_VMerror);
206
0
        goto out;
207
0
    }
208
16
    color_buffer_ptrs = (patch_color_t **)gs_alloc_bytes(pgs->memory,
209
16
                            sizeof(patch_color_t *) * per_row, "gs_shading_LfGt_fill_rectangle");
210
16
    if (color_buffer_ptrs == NULL) {
211
0
        code = gs_note_error(gs_error_VMerror);
212
0
        goto out;
213
0
    }
214
    /* CET 09-47K.PS SpecialTestJ02Test05 needs the color data alignment. */
215
156
    for (i = 0; i < per_row; ++i) {
216
142
        color_buffer_ptrs[i] = (patch_color_t *)(color_buffer + pfs.color_stack_step * i);
217
142
        vertex[i].c = color_buffer_ptrs[i];
218
142
        if ((code = Gt_next_vertex(pshm, &cs, &vertex[i], color_buffer_ptrs[i])) < 0)
219
2
            goto out;
220
142
    }
221
5.99k
    while (!seofp(cs.s)) {
222
5.99k
        code = Gt_next_vertex(pshm, &cs, &next, cn);
223
5.99k
        if (code < 0)
224
3
            goto out;
225
59.8k
        for (i = 1; i < per_row; ++i) {
226
53.9k
            code = Gt_fill_triangle(&pfs, &vertex[i - 1], &vertex[i], &next);
227
53.9k
            if (code < 0)
228
0
                goto out;
229
53.9k
            c = color_buffer_ptrs[i - 1];
230
53.9k
            vertex[i - 1] = next;
231
53.9k
            color_buffer_ptrs[i - 1] = cn;
232
53.9k
            next.c = cn = c;
233
53.9k
            code = Gt_next_vertex(pshm, &cs, &next, cn);
234
53.9k
            if (code < 0)
235
9
                goto out;
236
53.8k
            code = Gt_fill_triangle(&pfs, &vertex[i], &vertex[i - 1], &next);
237
53.8k
            if (code < 0)
238
0
                goto out;
239
53.8k
        }
240
5.98k
        c = color_buffer_ptrs[per_row - 1];
241
5.98k
        vertex[per_row - 1] = next;
242
5.98k
        color_buffer_ptrs[per_row - 1] = cn;
243
5.98k
        next.c = cn = c;
244
5.98k
    }
245
16
out:
246
16
    gs_free_object(pgs->memory, vertex, "gs_shading_LfGt_render");
247
16
    gs_free_object(pgs->memory, color_buffer, "gs_shading_LfGt_render");
248
16
    gs_free_object(pgs->memory, color_buffer_ptrs, "gs_shading_LfGt_render");
249
16
    release_colors(&pfs, pfs.color_stack, 1);
250
16
    if (pfs.icclink != NULL) gsicc_release_link(pfs.icclink);
251
16
    if (term_patch_fill_state(&pfs))
252
0
        return_error(gs_error_unregistered); /* Must not happen. */
253
16
    return code;
254
16
}