Coverage Report

Created: 2026-04-01 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gscicach.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
17
/* A color index cache. */
18
#include "gx.h"
19
#include "gserrors.h"
20
#include "gsccolor.h"
21
#include "gxcspace.h"
22
#include "gxdcolor.h"
23
#include "gscicach.h"
24
#include "memory_.h"
25
26
68.6M
#define COLOR_INDEX_CACHE_SIZE 256
27
53.1M
#define COLOR_INDEX_CACHE_CHAINS (COLOR_INDEX_CACHE_SIZE / 16)
28
29
typedef struct gs_color_index_cache_elem_s gs_color_index_cache_elem_t;
30
31
struct gs_color_index_cache_elem_s {
32
    union _color {
33
      gx_color_index cindex;
34
      ushort devn[GS_CLIENT_COLOR_MAX_COMPONENTS];
35
    } color;
36
    gx_device_color_type color_type;
37
    uint chain;
38
    uint prev, next; /* NULL for unused. */
39
    uint touch_prev, touch_next;
40
    bool frac_values_done;
41
};
42
43
struct gs_color_index_cache_s {
44
    const gs_color_space *direct_space;
45
    gs_gstate *pgs;
46
    gx_device *dev;
47
    gx_device *trans_dev;
48
    int client_num_components;
49
    int device_num_components;
50
    gs_memory_t *memory;
51
    int used;
52
    gs_color_index_cache_elem_t *buf;
53
    uint recent_touch;
54
    float *paint_values;
55
    frac31 *frac_values;
56
    int chains[COLOR_INDEX_CACHE_CHAINS];
57
    /* Note : the 0th element of buf, paint_values, frac_values is never used,
58
       because we consider the index 0 as NULL
59
       just for a faster initialization. */
60
107M
#   define MYNULL 0
61
};
62
63
gs_private_st_ptrs6(st_color_index_cache, gs_color_index_cache_t, "gs_color_index_cache_t",
64
                    gs_color_index_cache_elem_ptrs, gs_color_index_cache_reloc_ptrs,
65
                    direct_space, memory, buf, paint_values, frac_values, trans_dev);
66
67
gs_color_index_cache_t *
68
gs_color_index_cache_create(gs_memory_t *memory, const gs_color_space *direct_space, gx_device *dev,
69
                            gs_gstate *pgs, bool need_frac, gx_device *trans_dev)
70
23.6k
{
71
23.6k
    size_t client_num_components = cs_num_components(direct_space);
72
23.6k
    size_t device_num_components = trans_dev->color_info.num_components;
73
23.6k
    gs_color_index_cache_elem_t *buf = ( gs_color_index_cache_elem_t *)gs_alloc_byte_array(memory, COLOR_INDEX_CACHE_SIZE,
74
23.6k
                    sizeof(gs_color_index_cache_elem_t), "gs_color_index_cache_create");
75
23.6k
    float *paint_values = (float *)gs_alloc_byte_array(memory, COLOR_INDEX_CACHE_SIZE * client_num_components,
76
23.6k
                    sizeof(float), "gs_color_index_cache_create");
77
23.6k
    frac31 *frac_values = (need_frac ? (frac31 *)gs_alloc_byte_array(memory, COLOR_INDEX_CACHE_SIZE * device_num_components,
78
23.6k
                                            sizeof(frac31), "gs_color_index_cache_create") : NULL);
79
23.6k
    gs_color_index_cache_t *pcic = gs_alloc_struct(memory, gs_color_index_cache_t, &st_color_index_cache, "gs_color_index_cache_create");
80
81
23.6k
    if (buf == NULL || paint_values == NULL || (need_frac && frac_values == NULL) || pcic == NULL) {
82
0
        gs_free_object(memory, buf, "gs_color_index_cache_create");
83
0
        gs_free_object(memory, paint_values, "gs_color_index_cache_create");
84
0
        gs_free_object(memory, frac_values, "gs_color_index_cache_create");
85
0
        gs_free_object(memory, pcic, "gs_color_index_cache_create");
86
0
        return NULL;
87
0
    }
88
23.6k
    memset(pcic, 0, sizeof(*pcic));
89
23.6k
    memset(buf, 0, COLOR_INDEX_CACHE_SIZE * sizeof(gs_color_index_cache_elem_t));
90
23.6k
    pcic->direct_space = direct_space;
91
23.6k
    pcic->pgs = pgs;
92
23.6k
    pcic->dev = dev;
93
23.6k
    pcic->trans_dev = trans_dev;
94
23.6k
    pcic->device_num_components = device_num_components;
95
23.6k
    pcic->client_num_components = client_num_components;
96
23.6k
    pcic->memory = memory;
97
23.6k
    pcic->used = 1; /* Never use the 0th element. */
98
23.6k
    pcic->buf = buf;
99
23.6k
    pcic->recent_touch = MYNULL;
100
23.6k
    pcic->paint_values = paint_values;
101
23.6k
    pcic->frac_values = frac_values;
102
23.6k
    return pcic;
103
23.6k
}
104
105
void
106
gs_color_index_cache_destroy(gs_color_index_cache_t *pcic)
107
23.6k
{
108
23.6k
    gs_free_object(pcic->memory, pcic->buf, "gs_color_index_cache_create");
109
23.6k
    gs_free_object(pcic->memory, pcic->paint_values, "gs_color_index_cache_create");
110
23.6k
    gs_free_object(pcic->memory, pcic->frac_values, "gs_color_index_cache_create");
111
23.6k
    pcic->buf = NULL;
112
23.6k
    pcic->paint_values = NULL;
113
23.6k
    pcic->frac_values = NULL;
114
23.6k
    gs_free_object(pcic->memory, pcic, "gs_color_index_cache_create");
115
23.6k
}
116
117
static inline int
118
hash_paint_values(const gs_color_index_cache_t *self, const float *paint_values)
119
53.1M
{
120
53.1M
    int i;
121
53.1M
    float v = 0;
122
53.1M
    uint k = 0;
123
53.1M
    const uint a_prime = 79;
124
125
244M
    for (i = 0; i < self->client_num_components; i++)
126
191M
        v = v * a_prime + paint_values[i];
127
    /* Don't know the range of v, so hash its bytes : */
128
265M
    for(i = 0; i < sizeof(v); i++)
129
212M
        k = k * a_prime + ((byte *)&v)[i];
130
53.1M
    return k % COLOR_INDEX_CACHE_CHAINS;
131
53.1M
}
132
133
static inline void
134
exclude_from_chain(gs_color_index_cache_t *self, uint i)
135
22.7M
{
136
22.7M
    uint co = self->buf[i].chain;
137
22.7M
    uint ip = self->buf[i].prev, in = self->buf[i].next;
138
139
22.7M
    self->buf[ip].next = in;
140
22.7M
    self->buf[in].prev = ip;
141
22.7M
    if (self->chains[co] == i)
142
11.5M
        self->chains[co] = in;
143
22.7M
}
144
145
static inline void
146
include_into_chain(gs_color_index_cache_t *self, uint i, uint c)
147
22.9M
{
148
22.9M
    if (self->chains[c] != MYNULL) {
149
22.9M
        uint in = self->chains[c], ip = self->buf[in].prev;
150
151
22.9M
        self->buf[i].next = in;
152
22.9M
        self->buf[i].prev = ip;
153
22.9M
        self->buf[in].prev = i;
154
22.9M
        self->buf[ip].next = i;
155
22.9M
    } else
156
67.5k
        self->buf[i].prev = self->buf[i].next = i;
157
22.9M
    self->chains[c] = i;
158
22.9M
    self->buf[i].chain = c;
159
22.9M
}
160
161
static inline void
162
exclude_from_touch_list(gs_color_index_cache_t *self, uint i)
163
31.1M
{
164
31.1M
    uint ip = self->buf[i].touch_prev, in = self->buf[i].touch_next;
165
166
31.1M
    self->buf[ip].touch_next = in;
167
31.1M
    self->buf[in].touch_prev = ip;
168
31.1M
    if (self->recent_touch == i) {
169
0
        if (i == in)
170
0
            self->recent_touch = MYNULL;
171
0
        else
172
0
            self->recent_touch = in;
173
0
    }
174
31.1M
}
175
176
static inline void
177
include_into_touch_list(gs_color_index_cache_t *self, uint i)
178
31.3M
{
179
31.3M
    if (self->recent_touch != MYNULL) {
180
31.3M
        uint in = self->recent_touch, ip = self->buf[in].touch_prev;
181
182
31.3M
        self->buf[i].touch_next = in;
183
31.3M
        self->buf[i].touch_prev = ip;
184
31.3M
        self->buf[in].touch_prev = i;
185
31.3M
        self->buf[ip].touch_next = i;
186
31.3M
    } else
187
22.3k
        self->buf[i].touch_prev = self->buf[i].touch_next = i;
188
31.3M
    self->recent_touch = i;
189
31.3M
}
190
191
static int
192
get_color_index_cache_elem(gs_color_index_cache_t *self,
193
                           const float *paint_values, uint *pi)
194
53.1M
{
195
53.1M
    int client_num_components = self->client_num_components;
196
53.1M
    uint c = hash_paint_values(self, paint_values);
197
53.1M
    uint i = self->chains[c], j;
198
199
53.1M
    if (i != MYNULL) {
200
53.0M
        uint tries = 16; /* Arbitrary. */
201
202
53.0M
        if (!memcmp(paint_values, self->paint_values + i * client_num_components,
203
53.0M
                    sizeof(*paint_values) * client_num_components)) {
204
30.1M
            if (self->recent_touch != i) {
205
23.6M
                exclude_from_touch_list(self, i);
206
23.6M
                include_into_touch_list(self, i);
207
23.6M
            }
208
30.1M
            *pi = i;
209
30.1M
            return 1;
210
30.1M
        }
211
247M
        for (j = self->buf[i].next; tries -- && j != i; j = self->buf[j].next) {
212
232M
            if (!memcmp(paint_values, self->paint_values + j * client_num_components,
213
232M
                        sizeof(*paint_values) * client_num_components)) {
214
7.49M
                exclude_from_chain(self, j);
215
7.49M
                include_into_chain(self, j, c);
216
7.49M
                if (self->recent_touch != j) {
217
7.48M
                    exclude_from_touch_list(self, j);
218
7.48M
                    include_into_touch_list(self, j);
219
7.48M
                }
220
7.49M
                *pi = j;
221
7.49M
                return 1;
222
7.49M
            }
223
232M
        }
224
22.9M
    }
225
15.4M
    if (self->used < COLOR_INDEX_CACHE_SIZE) {
226
        /* Use a new one */
227
245k
        i = self->used++;
228
245k
        include_into_touch_list(self, i);
229
15.2M
    } else {
230
15.2M
        i = self->recent_touch;
231
15.2M
        self->recent_touch = self->buf[i].touch_prev; /* Assuming the cyclic list,
232
                                                      just move the head pointer to the last element. */
233
15.2M
        exclude_from_chain(self, i);
234
15.2M
    }
235
15.4M
    include_into_chain(self, i, c);
236
15.4M
    *pi = i;
237
15.4M
    return 0;
238
53.1M
}
239
240
static inline void
241
compute_frac_values(gs_color_index_cache_t *self, uint i)
242
15.3M
{
243
244
15.3M
    const gx_device_color_info *cinfo = &self->trans_dev->color_info;
245
15.3M
    int device_num_components = self->device_num_components;
246
15.3M
    int j;
247
15.3M
    gx_color_index c;
248
249
15.3M
    if (self->buf[i].color_type == &gx_dc_type_data_pure) {
250
11.3M
        c = self->buf[i].color.cindex;
251
36.2M
        for (j = 0; j < device_num_components; j++) {
252
24.9M
                int shift = cinfo->comp_shift[j];
253
24.9M
                int bits = cinfo->comp_bits[j];
254
24.9M
                self->frac_values[i * device_num_components + j] =
255
24.9M
                    ((c >> shift) & ((1 << bits) - 1)) <<
256
24.9M
                    (sizeof(frac31) * 8 - 1 - bits);
257
24.9M
        }
258
11.3M
        self->buf[i].frac_values_done = true;
259
11.3M
    } else {
260
        /* Must be devn */
261
19.8M
        for (j = 0; j < device_num_components; j++) {
262
15.8M
            self->frac_values[i * device_num_components + j] =
263
15.8M
                cv2frac31(self->buf[i].color.devn[j]);
264
15.8M
        }
265
3.97M
        self->buf[i].frac_values_done = true;
266
3.97M
    }
267
15.3M
}
268
269
int
270
gs_cached_color_index(gs_color_index_cache_t *self, const float *paint_values,
271
                      gx_device_color *pdevc, frac31 *frac_values)
272
53.1M
{
273
    /* Must return 2 if the color is not pure.
274
       See patch_color_to_device_color. */
275
53.1M
    const gs_color_space *pcs = self->direct_space;
276
53.1M
    int client_num_components = self->client_num_components;
277
53.1M
    int device_num_components = self->device_num_components;
278
53.1M
    uint i, j;
279
53.1M
    int code;
280
281
53.1M
    if (get_color_index_cache_elem(self, paint_values, &i)) {
282
37.6M
        if (pdevc != NULL) {
283
37.6M
            if (self->buf[i].color_type == &gx_dc_type_data_pure) {
284
25.8M
                pdevc->colors.pure = self->buf[i].color.cindex;
285
25.8M
                pdevc->type = &gx_dc_type_data_pure;
286
25.8M
                memcpy(pdevc->ccolor.paint.values, paint_values,
287
25.8M
                       sizeof(*paint_values) * client_num_components);
288
25.8M
            } else {
289
                /* devn case */
290
59.0M
                for (j = 0; j < device_num_components; j++) {
291
47.2M
                    pdevc->colors.devn.values[j] = self->buf[i].color.devn[j];
292
47.2M
                }
293
11.8M
                pdevc->type = &gx_dc_type_data_devn;
294
11.8M
                memcpy(pdevc->ccolor.paint.values, paint_values,
295
11.8M
                       sizeof(*paint_values) * client_num_components);
296
11.8M
            }
297
37.6M
            pdevc->ccolor_valid = true;
298
37.6M
        }
299
37.6M
        if (frac_values != NULL && !self->buf[i].frac_values_done)
300
81.1k
            compute_frac_values(self, i);
301
37.6M
    } else {
302
15.4M
        gx_device_color devc_local;
303
15.4M
        gs_client_color fcc;
304
305
15.4M
        if (pdevc == NULL)
306
0
            pdevc = &devc_local;
307
15.4M
        memcpy(self->paint_values + i * client_num_components, paint_values,
308
15.4M
               sizeof(*paint_values) * client_num_components);
309
15.4M
        memcpy(fcc.paint.values, paint_values,
310
15.4M
               sizeof(*paint_values) * client_num_components);
311
15.4M
        code = pcs->type->remap_color(&fcc, pcs, pdevc, self->pgs,
312
15.4M
                                      self->trans_dev, gs_color_select_texture);
313
15.4M
        if (code < 0)
314
0
            return code;
315
15.4M
        if (pdevc->type == &gx_dc_type_data_pure) {
316
11.4M
            self->buf[i].color.cindex = pdevc->colors.pure;
317
11.4M
        } else if (pdevc->type == &gx_dc_type_data_devn) {
318
19.9M
            for (j = 0; j < device_num_components; j++) {
319
15.9M
                self->buf[i].color.devn[j] = pdevc->colors.devn.values[j];
320
15.9M
            }
321
3.99M
        } else {
322
0
            return 2;
323
0
        }
324
15.4M
        self->buf[i].color_type = pdevc->type;
325
15.4M
        if (frac_values != NULL)
326
15.2M
            compute_frac_values(self, i);
327
251k
        else
328
251k
            self->buf[i].frac_values_done = false;
329
15.4M
    }
330
53.1M
    if (frac_values != NULL)
331
52.5M
        memcpy(frac_values, self->frac_values + i * device_num_components,
332
52.5M
               sizeof(*frac_values) * device_num_components);
333
53.1M
    return 0;
334
53.1M
}