File: gscicach.c

package info (click to toggle)
ghostscript 8.71~dfsg2-9+squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 79,896 kB
  • ctags: 80,654
  • sloc: ansic: 501,432; sh: 25,689; python: 4,853; cpp: 3,633; perl: 3,597; tcl: 1,480; makefile: 1,187; lisp: 407; asm: 284; xml: 263; awk: 66; csh: 17; yacc: 15
file content (294 lines) | stat: -rw-r--r-- 9,959 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* Copyright (C) 2001-2007 Artifex Software, Inc.
   All Rights Reserved.
  
   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id: gscicach.c 10624 2010-01-20 18:11:39Z mvrhel $ */
/* A color index cache. */
#include "gx.h"
#include "gserrors.h"
#include "gsccolor.h"
#include "gxcspace.h"
#include "gxdcolor.h"
#include "gscicach.h"
#include "memory_.h"

#define COLOR_INDEX_CACHE_SIZE 256
#define COLOR_INDEX_CACHE_CHAINS (COLOR_INDEX_CACHE_SIZE / 16)

typedef struct gs_color_index_cache_elem_s gs_color_index_cache_elem_t;

struct gs_color_index_cache_elem_s {
    gx_color_index cindex;
    uint chain;
    uint prev, next; /* NULL for unused. */
    uint touch_prev, touch_next;
    bool frac_values_done;
};

struct gs_color_index_cache_s {
    const gs_color_space *direct_space;
    gs_imager_state *pis;
    gx_device *dev;
    gx_device *trans_dev;
    int client_num_components;
    int device_num_components;
    gs_memory_t *memory;
    int used;
    gs_color_index_cache_elem_t *buf;
    uint recent_touch;
    float *paint_values;
    frac31 *frac_values;
    int chains[COLOR_INDEX_CACHE_CHAINS];
    /* Note : the 0th element of buf, paint_values, frac_values is never used,
       because we consider the index 0 as NULL
       just for a faster initialization. */
#   define MYNULL 0
};



gs_private_st_ptrs6(st_color_index_cache, gs_color_index_cache_t, "gs_color_index_cache_t",
		    gs_color_index_cache_elem_ptrs, gs_color_index_cache_reloc_ptrs,
		    direct_space, memory, buf, paint_values, frac_values, trans_dev);

gs_color_index_cache_t *
gs_color_index_cache_create(gs_memory_t *memory, const gs_color_space *direct_space, gx_device *dev, 
                            gs_imager_state *pis, bool need_frac, gx_device *trans_dev)
{
    int client_num_components = cs_num_components(direct_space);
    int device_num_components = trans_dev->color_info.num_components;
    gs_color_index_cache_elem_t *buf = ( gs_color_index_cache_elem_t *)gs_alloc_byte_array(memory, COLOR_INDEX_CACHE_SIZE, 
		    sizeof(gs_color_index_cache_elem_t), "gs_color_index_cache_create");
    float *paint_values = (float *)gs_alloc_byte_array(memory, COLOR_INDEX_CACHE_SIZE * client_num_components, 
		    sizeof(float), "gs_color_index_cache_create");
    frac31 *frac_values = (need_frac ? (frac31 *)gs_alloc_byte_array(memory, COLOR_INDEX_CACHE_SIZE * device_num_components, 
					    sizeof(frac31), "gs_color_index_cache_create") : NULL);
    gs_color_index_cache_t *pcic = gs_alloc_struct(memory, gs_color_index_cache_t, &st_color_index_cache, "gs_color_index_cache_create");

    if (buf == NULL || paint_values == NULL || (need_frac && frac_values == NULL) || pcic == NULL) {
	gs_free_object(memory, buf, "gs_color_index_cache_create");
	gs_free_object(memory, paint_values, "gs_color_index_cache_create");
	gs_free_object(memory, frac_values, "gs_color_index_cache_create");
	gs_free_object(memory, pcic, "gs_color_index_cache_create");
	return NULL;
    }
    memset(pcic, 0, sizeof(*pcic));
    memset(buf, 0, COLOR_INDEX_CACHE_SIZE * sizeof(gs_color_index_cache_elem_t));
    pcic->direct_space = direct_space;
    pcic->pis = pis;
    pcic->dev = dev;
    pcic->trans_dev = trans_dev;
    pcic->device_num_components = device_num_components;
    pcic->client_num_components = client_num_components;
    pcic->memory = memory;
    pcic->used = 1; /* Never use the 0th element. */
    pcic->buf = buf;
    pcic->recent_touch = MYNULL;
    pcic->paint_values = paint_values;
    pcic->frac_values = frac_values;
    return pcic;
}

void 
gs_color_index_cache_destroy(gs_color_index_cache_t *pcic)
{
    gs_free_object(pcic->memory, pcic->buf, "gs_color_index_cache_create");
    gs_free_object(pcic->memory, pcic->paint_values, "gs_color_index_cache_create");
    gs_free_object(pcic->memory, pcic->frac_values, "gs_color_index_cache_create");
    pcic->buf = NULL;
    pcic->paint_values = NULL;
    pcic->frac_values = NULL;
    gs_free_object(pcic->memory, pcic, "gs_color_index_cache_create");
}

static inline int 
hash_paint_values(const gs_color_index_cache_t *this, const float *paint_values)
{
    int i;
    float v = 0;
    uint k = 0;
    const uint a_prime = 79;

    for (i = 0; i < this->client_num_components; i++) 
	v = v * a_prime + paint_values[i];
    /* Don't know the range of v, so hash its bytes : */
    for(i = 0; i < sizeof(v); i++)
	k = k * a_prime + ((byte *)&v)[i];
    return k % COLOR_INDEX_CACHE_CHAINS;
}

static inline void
exclude_from_chain(gs_color_index_cache_t *this, uint i)
{
    uint co = this->buf[i].chain;
    uint ip = this->buf[i].prev, in = this->buf[i].next;

    this->buf[ip].next = in;
    this->buf[in].prev = ip;
    if (this->chains[co] == i)
	this->chains[co] = in;
}

static inline void
include_into_chain(gs_color_index_cache_t *this, uint i, uint c)
{
    if (this->chains[c] != MYNULL) {
	uint in = this->chains[c], ip = this->buf[in].prev;

	this->buf[i].next = in;
	this->buf[i].prev = ip;
	this->buf[in].prev = i;
	this->buf[ip].next = i;
    } else
	this->buf[i].prev = this->buf[i].next = i;
    this->chains[c] = i;
    this->buf[i].chain = c;
}

static inline void
exclude_from_touch_list(gs_color_index_cache_t *this, uint i)
{
    uint ip = this->buf[i].touch_prev, in = this->buf[i].touch_next;

    this->buf[ip].touch_next = in;
    this->buf[in].touch_prev = ip;
    if (this->recent_touch == i) {
	if (i == in)
	    this->recent_touch = MYNULL;
	else
	    this->recent_touch = in;
    }
}

static inline void
include_into_touch_list(gs_color_index_cache_t *this, uint i)
{
    if (this->recent_touch != MYNULL) {
	uint in = this->recent_touch, ip = this->buf[in].touch_prev;

	this->buf[i].touch_next = in;
	this->buf[i].touch_prev = ip;
	this->buf[in].touch_prev = i;
	this->buf[ip].touch_next = i;
    } else
	this->buf[i].touch_prev = this->buf[i].touch_next = i;
    this->recent_touch = i;
}

static int 
get_color_index_cache_elem(gs_color_index_cache_t *this, const float *paint_values, uint *pi)
{
    int client_num_components = this->client_num_components;
    uint c = hash_paint_values(this, paint_values);
    uint i = this->chains[c], j;

    if (i != MYNULL) {
	uint tries = 16; /* Arbitrary. */

	if (!memcmp(paint_values, this->paint_values + i * client_num_components, sizeof(*paint_values) * client_num_components)) {
	    if (this->recent_touch != i) {
		exclude_from_touch_list(this, i);
		include_into_touch_list(this, i);
	    }
	    *pi = i;
	    return 1;
	}
	for (j = this->buf[i].next; tries -- && j != i; j = this->buf[j].next) {
	    if (!memcmp(paint_values, this->paint_values + j * client_num_components, sizeof(*paint_values) * client_num_components)) {
		exclude_from_chain(this, j);
		include_into_chain(this, j, c);
		if (this->recent_touch != j) {
		    exclude_from_touch_list(this, j);
		    include_into_touch_list(this, j);
		}
		*pi = j;
		return 1;
	    }
	}
	tries+=0;
    }
    if (this->used < COLOR_INDEX_CACHE_SIZE) {
	/* Use a new one */
	i = this->used++;
	include_into_touch_list(this, i);
    } else {
	i = this->recent_touch;
	this->recent_touch = this->buf[i].touch_prev; /* Assuming the cyclic list, 
						      just move the head pointer to the last element. */
	exclude_from_chain(this, i);
    }
    include_into_chain(this, i, c);
    *pi = i;
    return 0;
}

static inline void
compute_frac_values(gs_color_index_cache_t *this, uint i)
{
    gx_color_index c = this->buf[i].cindex;
    const gx_device_color_info *cinfo = &this->trans_dev->color_info;
    int device_num_components = this->device_num_components;
    int j;

    for (j = 0; j < device_num_components; j++) {
	    int shift = cinfo->comp_shift[j];
	    int bits = cinfo->comp_bits[j];

	    this->frac_values[i * device_num_components + j] = ((c >> shift) & ((1 << bits) - 1)) << (sizeof(frac31) * 8 - 1 - bits);
    }
    this->buf[i].frac_values_done = true;
}

int 
gs_cached_color_index(gs_color_index_cache_t *this, const float *paint_values, gx_device_color *pdevc, frac31 *frac_values)
{
    /* Must return 2 if the color is not pure. 
       See patch_color_to_device_color. */
    const gs_color_space *pcs = this->direct_space;
    int client_num_components = this->client_num_components;
    int device_num_components = this->device_num_components;
    uint i;
    int code;

    if (get_color_index_cache_elem(this, paint_values, &i)) {
	if (pdevc != NULL) {
	    pdevc->colors.pure = this->buf[i].cindex;
	    pdevc->type = &gx_dc_type_data_pure;
	    memcpy(pdevc->ccolor.paint.values, paint_values, sizeof(*paint_values) * client_num_components);
	    pdevc->ccolor_valid = true;
	}
	if (frac_values != NULL && !this->buf[i].frac_values_done)
	    compute_frac_values(this, i);
    } else {
	gx_device_color devc_local;
	gs_client_color fcc;

	if (pdevc == NULL)
	    pdevc = &devc_local;
	memcpy(this->paint_values + i * client_num_components, paint_values, sizeof(*paint_values) * client_num_components);
	memcpy(fcc.paint.values, paint_values, sizeof(*paint_values) * client_num_components);
	code = pcs->type->remap_color(&fcc, pcs, pdevc, this->pis, this->dev, gs_color_select_texture);
	if (code < 0)
	    return code;
	if (pdevc->type != &gx_dc_type_data_pure)
	    return 2;
	this->buf[i].cindex = pdevc->colors.pure;
	
	if (frac_values != NULL)
    	    compute_frac_values(this, i);
	else
	    this->buf[i].frac_values_done = false;
    }
    if (frac_values != NULL)
	memcpy(frac_values, this->frac_values + i * device_num_components, sizeof(*frac_values) * device_num_components);
    return 0;
}