Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/base/gscicach.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
/* 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
11.9k
#define COLOR_INDEX_CACHE_SIZE 256
27
11.9k
#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
14.4k
#   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
7
{
71
7
    int client_num_components = cs_num_components(direct_space);
72
7
    int device_num_components = trans_dev->color_info.num_components;
73
7
    gs_color_index_cache_elem_t *buf = ( gs_color_index_cache_elem_t *)gs_alloc_byte_array(memory, COLOR_INDEX_CACHE_SIZE,
74
7
                    sizeof(gs_color_index_cache_elem_t), "gs_color_index_cache_create");
75
7
    float *paint_values = (float *)gs_alloc_byte_array(memory, COLOR_INDEX_CACHE_SIZE * client_num_components,
76
7
                    sizeof(float), "gs_color_index_cache_create");
77
7
    frac31 *frac_values = (need_frac ? (frac31 *)gs_alloc_byte_array(memory, COLOR_INDEX_CACHE_SIZE * device_num_components,
78
7
                                            sizeof(frac31), "gs_color_index_cache_create") : NULL);
79
7
    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
7
    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
7
    memset(pcic, 0, sizeof(*pcic));
89
7
    memset(buf, 0, COLOR_INDEX_CACHE_SIZE * sizeof(gs_color_index_cache_elem_t));
90
7
    pcic->direct_space = direct_space;
91
7
    pcic->pgs = pgs;
92
7
    pcic->dev = dev;
93
7
    pcic->trans_dev = trans_dev;
94
7
    pcic->device_num_components = device_num_components;
95
7
    pcic->client_num_components = client_num_components;
96
7
    pcic->memory = memory;
97
7
    pcic->used = 1; /* Never use the 0th element. */
98
7
    pcic->buf = buf;
99
7
    pcic->recent_touch = MYNULL;
100
7
    pcic->paint_values = paint_values;
101
7
    pcic->frac_values = frac_values;
102
7
    return pcic;
103
7
}
104
105
void
106
gs_color_index_cache_destroy(gs_color_index_cache_t *pcic)
107
7
{
108
7
    gs_free_object(pcic->memory, pcic->buf, "gs_color_index_cache_create");
109
7
    gs_free_object(pcic->memory, pcic->paint_values, "gs_color_index_cache_create");
110
7
    gs_free_object(pcic->memory, pcic->frac_values, "gs_color_index_cache_create");
111
7
    pcic->buf = NULL;
112
7
    pcic->paint_values = NULL;
113
7
    pcic->frac_values = NULL;
114
7
    gs_free_object(pcic->memory, pcic, "gs_color_index_cache_create");
115
7
}
116
117
static inline int
118
hash_paint_values(const gs_color_index_cache_t *self, const float *paint_values)
119
11.9k
{
120
11.9k
    int i;
121
11.9k
    float v = 0;
122
11.9k
    uint k = 0;
123
11.9k
    const uint a_prime = 79;
124
125
34.9k
    for (i = 0; i < self->client_num_components; i++)
126
23.0k
        v = v * a_prime + paint_values[i];
127
    /* Don't know the range of v, so hash its bytes : */
128
59.5k
    for(i = 0; i < sizeof(v); i++)
129
47.6k
        k = k * a_prime + ((byte *)&v)[i];
130
11.9k
    return k % COLOR_INDEX_CACHE_CHAINS;
131
11.9k
}
132
133
static inline void
134
exclude_from_chain(gs_color_index_cache_t *self, uint i)
135
831
{
136
831
    uint co = self->buf[i].chain;
137
831
    uint ip = self->buf[i].prev, in = self->buf[i].next;
138
139
831
    self->buf[ip].next = in;
140
831
    self->buf[in].prev = ip;
141
831
    if (self->chains[co] == i)
142
0
        self->chains[co] = in;
143
831
}
144
145
static inline void
146
include_into_chain(gs_color_index_cache_t *self, uint i, uint c)
147
846
{
148
846
    if (self->chains[c] != MYNULL) {
149
834
        uint in = self->chains[c], ip = self->buf[in].prev;
150
151
834
        self->buf[i].next = in;
152
834
        self->buf[i].prev = ip;
153
834
        self->buf[in].prev = i;
154
834
        self->buf[ip].next = i;
155
834
    } else
156
12
        self->buf[i].prev = self->buf[i].next = i;
157
846
    self->chains[c] = i;
158
846
    self->buf[i].chain = c;
159
846
}
160
161
static inline void
162
exclude_from_touch_list(gs_color_index_cache_t *self, uint i)
163
1.68k
{
164
1.68k
    uint ip = self->buf[i].touch_prev, in = self->buf[i].touch_next;
165
166
1.68k
    self->buf[ip].touch_next = in;
167
1.68k
    self->buf[in].touch_prev = ip;
168
1.68k
    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
1.68k
}
175
176
static inline void
177
include_into_touch_list(gs_color_index_cache_t *self, uint i)
178
1.70k
{
179
1.70k
    if (self->recent_touch != MYNULL) {
180
1.69k
        uint in = self->recent_touch, ip = self->buf[in].touch_prev;
181
182
1.69k
        self->buf[i].touch_next = in;
183
1.69k
        self->buf[i].touch_prev = ip;
184
1.69k
        self->buf[in].touch_prev = i;
185
1.69k
        self->buf[ip].touch_next = i;
186
1.69k
    } else
187
7
        self->buf[i].touch_prev = self->buf[i].touch_next = i;
188
1.70k
    self->recent_touch = i;
189
1.70k
}
190
191
static int
192
get_color_index_cache_elem(gs_color_index_cache_t *self,
193
                           const float *paint_values, uint *pi)
194
11.9k
{
195
11.9k
    int client_num_components = self->client_num_components;
196
11.9k
    uint c = hash_paint_values(self, paint_values);
197
11.9k
    uint i = self->chains[c], j;
198
199
11.9k
    if (i != MYNULL) {
200
11.8k
        uint tries = 16; /* Arbitrary. */
201
202
11.8k
        if (!memcmp(paint_values, self->paint_values + i * client_num_components,
203
11.8k
                    sizeof(*paint_values) * client_num_components)) {
204
11.0k
            if (self->recent_touch != i) {
205
857
                exclude_from_touch_list(self, i);
206
857
                include_into_touch_list(self, i);
207
857
            }
208
11.0k
            *pi = i;
209
11.0k
            return 1;
210
11.0k
        }
211
834
        for (j = self->buf[i].next; tries -- && j != i; j = self->buf[j].next) {
212
831
            if (!memcmp(paint_values, self->paint_values + j * client_num_components,
213
831
                        sizeof(*paint_values) * client_num_components)) {
214
831
                exclude_from_chain(self, j);
215
831
                include_into_chain(self, j, c);
216
831
                if (self->recent_touch != j) {
217
831
                    exclude_from_touch_list(self, j);
218
831
                    include_into_touch_list(self, j);
219
831
                }
220
831
                *pi = j;
221
831
                return 1;
222
831
            }
223
831
        }
224
834
    }
225
15
    if (self->used < COLOR_INDEX_CACHE_SIZE) {
226
        /* Use a new one */
227
15
        i = self->used++;
228
15
        include_into_touch_list(self, i);
229
15
    } else {
230
0
        i = self->recent_touch;
231
0
        self->recent_touch = self->buf[i].touch_prev; /* Assuming the cyclic list,
232
                                                      just move the head pointer to the last element. */
233
0
        exclude_from_chain(self, i);
234
0
    }
235
15
    include_into_chain(self, i, c);
236
15
    *pi = i;
237
15
    return 0;
238
11.9k
}
239
240
static inline void
241
compute_frac_values(gs_color_index_cache_t *self, uint i)
242
15
{
243
244
15
    const gx_device_color_info *cinfo = &self->trans_dev->color_info;
245
15
    int device_num_components = self->device_num_components;
246
15
    int j;
247
15
    gx_color_index c;
248
249
15
    if (self->buf[i].color_type == &gx_dc_type_data_pure) {
250
15
        c = self->buf[i].color.cindex;
251
42
        for (j = 0; j < device_num_components; j++) {
252
27
                int shift = cinfo->comp_shift[j];
253
27
                int bits = cinfo->comp_bits[j];
254
27
                self->frac_values[i * device_num_components + j] =
255
27
                    ((c >> shift) & ((1 << bits) - 1)) <<
256
27
                    (sizeof(frac31) * 8 - 1 - bits);
257
27
        }
258
15
        self->buf[i].frac_values_done = true;
259
15
    } else {
260
        /* Must be devn */
261
0
        for (j = 0; j < device_num_components; j++) {
262
0
            self->frac_values[i * device_num_components + j] =
263
0
                cv2frac31(self->buf[i].color.devn[j]);
264
0
        }
265
0
        self->buf[i].frac_values_done = true;
266
0
    }
267
15
}
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
11.9k
{
273
    /* Must return 2 if the color is not pure.
274
       See patch_color_to_device_color. */
275
11.9k
    const gs_color_space *pcs = self->direct_space;
276
11.9k
    int client_num_components = self->client_num_components;
277
11.9k
    int device_num_components = self->device_num_components;
278
11.9k
    uint i, j;
279
11.9k
    int code;
280
281
11.9k
    if (get_color_index_cache_elem(self, paint_values, &i)) {
282
11.8k
        if (pdevc != NULL) {
283
6.08k
            if (self->buf[i].color_type == &gx_dc_type_data_pure) {
284
6.08k
                pdevc->colors.pure = self->buf[i].color.cindex;
285
6.08k
                pdevc->type = &gx_dc_type_data_pure;
286
6.08k
                memcpy(pdevc->ccolor.paint.values, paint_values,
287
6.08k
                       sizeof(*paint_values) * client_num_components);
288
6.08k
            } else {
289
                /* devn case */
290
0
                for (j = 0; j < device_num_components; j++) {
291
0
                    pdevc->colors.devn.values[j] = self->buf[i].color.devn[j];
292
0
                }
293
0
                pdevc->type = &gx_dc_type_data_devn;
294
0
                memcpy(pdevc->ccolor.paint.values, paint_values,
295
0
                       sizeof(*paint_values) * client_num_components);
296
0
            }
297
6.08k
            pdevc->ccolor_valid = true;
298
6.08k
        }
299
11.8k
        if (frac_values != NULL && !self->buf[i].frac_values_done)
300
0
            compute_frac_values(self, i);
301
11.8k
    } else {
302
15
        gx_device_color devc_local;
303
15
        gs_client_color fcc;
304
305
15
        if (pdevc == NULL)
306
15
            pdevc = &devc_local;
307
15
        memcpy(self->paint_values + i * client_num_components, paint_values,
308
15
               sizeof(*paint_values) * client_num_components);
309
15
        memcpy(fcc.paint.values, paint_values,
310
15
               sizeof(*paint_values) * client_num_components);
311
15
        code = pcs->type->remap_color(&fcc, pcs, pdevc, self->pgs,
312
15
                                      self->trans_dev, gs_color_select_texture);
313
15
        if (code < 0)
314
0
            return code;
315
15
        if (pdevc->type == &gx_dc_type_data_pure) {
316
15
            self->buf[i].color.cindex = pdevc->colors.pure;
317
15
        } else if (pdevc->type == &gx_dc_type_data_devn) {
318
0
            for (j = 0; j < device_num_components; j++) {
319
0
                self->buf[i].color.devn[j] = pdevc->colors.devn.values[j];
320
0
            }
321
0
        } else {
322
0
            return 2;
323
0
        }
324
15
        self->buf[i].color_type = pdevc->type;
325
15
        if (frac_values != NULL)
326
15
            compute_frac_values(self, i);
327
0
        else
328
0
            self->buf[i].frac_values_done = false;
329
15
    }
330
11.9k
    if (frac_values != NULL)
331
10.4k
        memcpy(frac_values, self->frac_values + i * device_num_components,
332
10.4k
               sizeof(*frac_values) * device_num_components);
333
11.9k
    return 0;
334
11.9k
}