Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gsicc_cache.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 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
/*  GS ICC link cache.  Initial stubbing of functions.  */
17
18
#include "std.h"
19
#include "stdpre.h"
20
#include "gstypes.h"
21
#include "gsmemory.h"
22
#include "gsstruct.h"
23
#include "scommon.h"
24
#include "gx.h"
25
#include "gpsync.h" /* for MAX_THREADS */
26
#include "gxgstate.h"
27
#include "smd5.h"
28
#include "gscms.h"
29
#include "gsicc_cms.h"
30
#include "gsicc_manage.h"
31
#include "gsicc_cache.h"
32
#include "gserrors.h"
33
#include "gsmalloc.h" /* Needed for named color structure allocation */
34
#include "string_.h"  /* Needed for named color structure allocation */
35
#include "gxsync.h"
36
#include "gzstate.h"
37
#include "stdint_.h"
38
#include "assert_.h"
39
        /*
40
         *  Note that the the external memory used to maintain
41
         *  links in the CMS is generally not visible to GS.
42
         *  For most CMS's the  links are 33x33x33x33x4 bytes at worst
43
         *  for a CMYK to CMYK MLUT which is about 4.5Mb per link.
44
         *  If the link were matrix based it would be much much smaller.
45
         *  We will likely want to do at least have an estimate of the
46
         *  memory used based upon how the CMS is configured.
47
         *  This will be done later.  For now, just limit the number
48
         *  of links.
49
         */
50
670k
#define ICC_CACHE_MAXLINKS (MAX_THREADS*2)  /* allow up to two active links per thread */
51
0
#define ICC_CACHE_NOT_VALID_COUNT 20  /* This should not really occur. If it does we need to take a closer look */
52
53
/* Static prototypes */
54
55
static gsicc_link_t * gsicc_alloc_link(gs_memory_t *memory, gsicc_hashlink_t hashcode);
56
57
static int gsicc_get_cspace_hash(gsicc_manager_t *icc_manager, gx_device *dev,
58
                                 cmm_profile_t *profile, int64_t *hash);
59
60
static int gsicc_compute_linkhash(gsicc_manager_t *icc_manager, gx_device *dev,
61
                                  cmm_profile_t *input_profile,
62
                                  cmm_profile_t *output_profile,
63
                                  gsicc_rendering_param_t *rendering_params,
64
                                  gsicc_hashlink_t *hash);
65
66
static void gsicc_remove_link(gsicc_link_t *link);
67
68
static void gsicc_get_buff_hash(unsigned char *data, int64_t *hash, unsigned int num_bytes);
69
70
static void rc_gsicc_link_cache_free(gs_memory_t * mem, void *ptr_in, client_name_t cname);
71
72
/* Structure pointer information */
73
74
struct_proc_finalize(icc_link_finalize);
75
76
gs_private_st_ptrs3_final(st_icc_link, gsicc_link_t, "gsiccmanage_link",
77
                    icc_link_enum_ptrs, icc_link_reloc_ptrs, icc_link_finalize,
78
                    icc_link_cache, next, lock);
79
80
struct_proc_finalize(icc_linkcache_finalize);
81
82
gs_private_st_ptrs3_final(st_icc_linkcache, gsicc_link_cache_t, "gsiccmanage_linkcache",
83
                    icc_linkcache_enum_ptrs, icc_linkcache_reloc_ptrs, icc_linkcache_finalize,
84
                    head, lock, full_wait);
85
86
/* These are used to construct a hash for the ICC link based upon the
87
   render parameters */
88
89
184M
#define BP_SHIFT 0
90
184M
#define REND_SHIFT 8
91
184M
#define PRESERVE_SHIFT 16
92
93
/**
94
 * gsicc_cache_new: Allocate a new ICC cache manager
95
 * Return value: Pointer to allocated manager, or NULL on failure.
96
 **/
97
98
gsicc_link_cache_t *
99
gsicc_cache_new(gs_memory_t *memory)
100
2.66M
{
101
2.66M
    gsicc_link_cache_t *result;
102
103
    /* We want this to be maintained in stable_memory.  It should be be effected by the
104
       save and restores */
105
2.66M
    memory = memory->stable_memory;
106
2.66M
    result = gs_alloc_struct(memory, gsicc_link_cache_t, &st_icc_linkcache,
107
2.66M
                             "gsicc_cache_new");
108
2.66M
    if ( result == NULL )
109
0
        return(NULL);
110
2.66M
    result->head = NULL;
111
2.66M
    result->num_links = 0;
112
2.66M
    result->cache_full = false;
113
2.66M
    result->memory = memory;
114
2.66M
    result->full_wait = NULL; /* Required so finaliser can work when result freed. */
115
2.66M
    rc_init_free(result, memory, 1, rc_gsicc_link_cache_free);
116
2.66M
    result->lock = gx_monitor_label(gx_monitor_alloc(memory),
117
2.66M
                                    "gsicc_cache_new");
118
2.66M
    if (result->lock == NULL) {
119
0
        rc_decrement(result, "gsicc_cache_new");
120
0
        return(NULL);
121
0
    }
122
2.66M
    result->full_wait = gx_semaphore_label(gx_semaphore_alloc(memory),
123
2.66M
                                           "gsicc_cache_new");
124
2.66M
    if (result->full_wait == NULL) {
125
        /* Don't free result->lock, as the finaliser for result does that! */
126
0
        rc_decrement(result, "gsicc_cache_new");
127
0
        return(NULL);
128
0
    }
129
2.66M
    if_debug2m(gs_debug_flag_icc, memory,
130
2.66M
               "[icc] Allocating link cache = "PRI_INTPTR" memory = "PRI_INTPTR"\n",
131
2.66M
         (intptr_t)result, (intptr_t)result->memory);
132
2.66M
    return(result);
133
2.66M
}
134
135
static void
136
rc_gsicc_link_cache_free(gs_memory_t * mem, void *ptr_in, client_name_t cname)
137
2.66M
{
138
    /* Ending the entire cache.  The ref counts on all the links should be 0 */
139
2.66M
    gsicc_link_cache_t *link_cache = (gsicc_link_cache_t * ) ptr_in;
140
141
    /* mem is unused, but we are passed it anyway by the ref counting mechanisms. */
142
2.66M
    assert(link_cache != NULL && mem == link_cache->memory);
143
2.66M
    if (link_cache == NULL)
144
0
        return;
145
2.66M
    if_debug2m(gs_debug_flag_icc, link_cache->memory,
146
2.66M
               "[icc] Removing link cache = "PRI_INTPTR" memory = "PRI_INTPTR"\n",
147
2.66M
               (intptr_t)link_cache, (intptr_t)link_cache->memory);
148
    /* NB: freeing the link_cache will call icc_linkcache_finalize */
149
2.66M
    gs_free_object(link_cache->memory, link_cache, "rc_gsicc_link_cache_free");
150
2.66M
}
151
152
/* release the monitor of the link_cache when it is freed */
153
void
154
icc_linkcache_finalize(const gs_memory_t *mem, void *ptr)
155
2.66M
{
156
2.66M
    gsicc_link_cache_t *link_cache = (gsicc_link_cache_t * ) ptr;
157
158
    /* The link cache lock is not held here, but presumably we must be safe as
159
     * we are shutting down. */
160
161
    /* mem is unused, but we are passed it anyway by the ref counting mechanisms. */
162
2.66M
    assert(link_cache != NULL && mem == link_cache->memory);
163
2.66M
    if (link_cache == NULL)
164
0
        return;
165
3.20M
    while (link_cache->head != NULL) {
166
541k
        if (link_cache->head->ref_count != 0) {
167
25
            if_debug2m(gs_debug_flag_icc, link_cache->memory, "link at "PRI_INTPTR" being removed, but has ref_count = %d\n",
168
25
                      (intptr_t)link_cache->head, link_cache->head->ref_count);
169
25
            link_cache->head->ref_count = 0;  /* force removal */
170
25
        }
171
541k
        gsicc_remove_link(link_cache->head);
172
541k
    }
173
#ifdef DEBUG
174
    if (link_cache->num_links != 0) {
175
        emprintf1(link_cache->memory, "num_links is %d, should be 0.\n", link_cache->num_links);
176
    }
177
#endif
178
2.66M
    if (link_cache->rc.ref_count == 0) {
179
2.66M
        gx_monitor_free(link_cache->lock);
180
2.66M
        link_cache->lock = NULL;
181
2.66M
        gx_semaphore_free(link_cache->full_wait);
182
2.66M
        link_cache->full_wait = 0;
183
2.66M
    }
184
2.66M
}
185
186
/* This is a special allocation for a link that is used by devices for
187
   doing color management on post rendered data.  It is not tied into the
188
   profile cache like gsicc_alloc_link. Also it goes ahead and creates
189
   the link, i.e. link creation is not delayed. */
190
gsicc_link_t *
191
gsicc_alloc_link_dev(gs_memory_t *memory, cmm_profile_t *src_profile,
192
    cmm_profile_t *des_profile, gsicc_rendering_param_t *rendering_params)
193
0
{
194
0
    gsicc_link_t *result;
195
0
    int cms_flags = 0;
196
197
0
    memory = memory->non_gc_memory;
198
0
    result = (gsicc_link_t*)gs_alloc_byte_array(memory, 1,
199
0
        sizeof(gsicc_link_t), "gsicc_alloc_link_dev");
200
201
0
    if (result == NULL)
202
0
        return NULL;
203
0
    result->lock = gx_monitor_label(gx_monitor_alloc(memory),
204
0
        "gsicc_link_new");
205
0
    if (result->lock == NULL) {
206
0
        gs_free_object(memory, result, "gsicc_alloc_link(lock)");
207
0
        return NULL;
208
0
    }
209
0
    gx_monitor_enter(result->lock);
210
211
    /* set up placeholder values */
212
0
    result->is_monitored = false;
213
0
    result->orig_procs.map_buffer = NULL;
214
0
    result->orig_procs.map_color = NULL;
215
0
    result->orig_procs.free_link = NULL;
216
0
    result->next = NULL;
217
0
    result->link_handle = NULL;
218
0
    result->icc_link_cache = NULL;
219
0
    result->procs.map_buffer = gscms_transform_color_buffer;
220
0
    result->procs.map_color = gscms_transform_color;
221
0
    result->procs.free_link = gscms_release_link;
222
0
    result->hashcode.link_hashcode = 0;
223
0
    result->hashcode.des_hash = 0;
224
0
    result->hashcode.src_hash = 0;
225
0
    result->hashcode.rend_hash = 0;
226
0
    result->ref_count = 1;
227
0
    result->includes_softproof = 0;
228
0
    result->includes_devlink = 0;
229
0
    result->is_identity = false;
230
0
    result->validity = 1;
231
0
    result->memory = memory;
232
233
0
    if_debug1m('^', result->memory, "[^]icclink "PRI_INTPTR" init = 1\n",
234
0
               (intptr_t)result);
235
236
0
    if (src_profile->profile_handle == NULL) {
237
0
        src_profile->profile_handle = gsicc_get_profile_handle_buffer(
238
0
            src_profile->buffer, src_profile->buffer_size, memory);
239
0
    }
240
241
0
    if (des_profile->profile_handle == NULL) {
242
0
        des_profile->profile_handle = gsicc_get_profile_handle_buffer(
243
0
            des_profile->buffer, des_profile->buffer_size, memory);
244
0
    }
245
246
    /* Check for problems.. */
247
    /* It is (apparently) permissible for the destination profile_handle to be NULL, for device links */
248
0
    if (src_profile->profile_handle == 0 /* || des_profile->profile_handle == 0 */) {
249
0
        gs_free_object(memory, result, "gsicc_alloc_link_dev");
250
0
        return NULL;
251
0
    }
252
253
    /* [0] is chunky, littleendian, noalpha, 16-in, 16-out */
254
0
    result->link_handle = gscms_get_link(src_profile->profile_handle,
255
0
        des_profile->profile_handle, rendering_params, cms_flags,
256
0
        memory);
257
258
    /* Check for problems.. */
259
0
    if (result->link_handle == NULL) {
260
0
        gs_free_object(memory, result, "gsicc_alloc_link_dev");
261
0
        return NULL;
262
0
    }
263
264
    /* Check for identity transform */
265
0
    if (gsicc_get_hash(src_profile) == gsicc_get_hash(des_profile))
266
0
        result->is_identity = true;
267
268
    /* Set the rest */
269
0
    result->data_cs = src_profile->data_cs;
270
0
    result->num_input = src_profile->num_comps;
271
0
    result->num_output = des_profile->num_comps;
272
273
0
    return result;
274
0
}
275
276
/* And the related release of the link */
277
void
278
gsicc_free_link_dev(gsicc_link_t *link)
279
0
{
280
0
    if (link == NULL)
281
0
        return;
282
0
    link->procs.free_link(link);
283
0
    gs_free_object(link->memory, link, "gsicc_free_link_dev");
284
0
}
285
286
static gsicc_link_t *
287
gsicc_alloc_link(gs_memory_t *memory, gsicc_hashlink_t hashcode)
288
670k
{
289
670k
    gsicc_link_t *result;
290
291
    /* The link has to be added in stable memory. We want them
292
       to be maintained across the gsave and grestore process */
293
670k
    memory = memory->stable_memory;
294
670k
    result = gs_alloc_struct(memory, gsicc_link_t, &st_icc_link,
295
670k
                             "gsicc_alloc_link");
296
670k
    if (result == NULL)
297
0
        return NULL;
298
    /* set up placeholder values */
299
670k
    result->is_monitored = false;
300
670k
    result->orig_procs.map_buffer = NULL;
301
670k
    result->orig_procs.map_color = NULL;
302
670k
    result->orig_procs.free_link = NULL;
303
670k
    result->next = NULL;
304
670k
    result->link_handle = NULL;
305
670k
    result->procs.map_buffer = gscms_transform_color_buffer;
306
670k
    result->procs.map_color = gscms_transform_color;
307
670k
    result->procs.free_link = gscms_release_link;
308
670k
    result->hashcode.link_hashcode = hashcode.link_hashcode;
309
670k
    result->hashcode.des_hash = 0;
310
670k
    result->hashcode.src_hash = 0;
311
670k
    result->hashcode.rend_hash = 0;
312
670k
    result->ref_count = 1;    /* prevent it from being freed */
313
670k
    result->includes_softproof = 0;
314
670k
    result->includes_devlink = 0;
315
670k
    result->is_identity = false;
316
670k
    result->validity = 0;   /* not yet complete */
317
670k
    result->memory = memory;
318
319
670k
    result->lock = gx_monitor_label(gx_monitor_alloc(memory),
320
670k
                                    "gsicc_link_new");
321
670k
    if (result->lock == NULL) {
322
0
        gs_free_object(memory, result, "gsicc_alloc_link(lock)");
323
0
        return NULL;
324
0
    }
325
670k
    gx_monitor_enter(result->lock);     /* this link is owned by this thread until built and made "valid" */
326
327
670k
    if_debug1m('^', result->memory, "[^]icclink "PRI_INTPTR" init = 1\n",
328
670k
               (intptr_t)result);
329
670k
    return result;
330
670k
}
331
332
static void
333
gsicc_set_link_data(gsicc_link_t *icc_link, void *link_handle,
334
                    gsicc_hashlink_t hashcode,
335
                    bool includes_softproof, bool includes_devlink,
336
                    bool pageneutralcolor, gsicc_colorbuffer_t data_cs)
337
541k
{
338
541k
    icc_link->link_handle = link_handle;
339
541k
    gscms_get_link_dim(link_handle, &(icc_link->num_input), &(icc_link->num_output),
340
541k
        icc_link->memory);
341
541k
    icc_link->hashcode.link_hashcode = hashcode.link_hashcode;
342
541k
    icc_link->hashcode.des_hash = hashcode.des_hash;
343
541k
    icc_link->hashcode.src_hash = hashcode.src_hash;
344
541k
    icc_link->hashcode.rend_hash = hashcode.rend_hash;
345
541k
    icc_link->includes_softproof = includes_softproof;
346
541k
    icc_link->includes_devlink = includes_devlink;
347
541k
    if ( (hashcode.src_hash == hashcode.des_hash) &&
348
377k
          !includes_softproof && !includes_devlink) {
349
377k
        icc_link->is_identity = true;
350
377k
    } else {
351
163k
        icc_link->is_identity = false;
352
163k
    }
353
    /* Set up for monitoring */
354
541k
    icc_link->data_cs = data_cs;
355
541k
    if (pageneutralcolor)
356
0
        gsicc_mcm_set_link(icc_link);
357
358
541k
    icc_link->validity = 1;
359
541k
}
360
361
static void
362
gsicc_link_free_contents(gsicc_link_t *icc_link)
363
1.34M
{
364
1.34M
    icc_link->procs.free_link(icc_link);
365
1.34M
    gx_monitor_free(icc_link->lock);
366
1.34M
    icc_link->lock = NULL;
367
1.34M
}
368
369
void
370
gsicc_link_free(gsicc_link_t *icc_link)
371
670k
{
372
670k
    if (icc_link == NULL)
373
0
        return;
374
670k
    gsicc_link_free_contents(icc_link);
375
376
670k
    gs_free_object(icc_link->memory, icc_link, "gsicc_link_free");
377
670k
}
378
379
void
380
icc_link_finalize(const gs_memory_t *mem, void *ptr)
381
670k
{
382
670k
    gsicc_link_t *icc_link = (gsicc_link_t * ) ptr;
383
384
670k
    gsicc_link_free_contents(icc_link);
385
670k
}
386
387
static void
388
gsicc_mash_hash(gsicc_hashlink_t *hash)
389
184M
{
390
184M
    hash->link_hashcode =
391
184M
        (hash->des_hash >> 1) ^ (hash->rend_hash) ^ (hash->src_hash);
392
184M
}
393
394
static void
395
gsicc_set_hash(cmm_profile_t *profile)
396
0
{
397
0
    if (!profile->hash_is_valid) {
398
0
        int64_t hash;
399
400
0
        gsicc_get_icc_buff_hash(profile->buffer, &hash, profile->buffer_size);
401
0
        profile->hashcode = hash;
402
0
        profile->hash_is_valid = true;
403
0
    }
404
0
    return;
405
0
}
406
407
int64_t
408
gsicc_get_hash(cmm_profile_t *profile)
409
352k
{
410
352k
    if (!profile->hash_is_valid) {
411
3.42k
        int64_t hash;
412
413
3.42k
        gsicc_get_icc_buff_hash(profile->buffer, &hash, profile->buffer_size);
414
3.42k
        profile->hashcode = hash;
415
3.42k
        profile->hash_is_valid = true;
416
3.42k
    }
417
352k
    return profile->hashcode;
418
352k
}
419
420
bool
421
3.63M
gsicc_profiles_equal(cmm_profile_t *profile1, cmm_profile_t *profile2) {
422
423
3.63M
    if (profile1 == NULL || profile2 == NULL)
424
0
        return false;
425
426
3.63M
    if (!(profile1->hash_is_valid)) {
427
0
        gsicc_set_hash(profile1);
428
0
    }
429
430
3.63M
    if (!(profile1->hash_is_valid)) {
431
0
        gsicc_set_hash(profile2);
432
0
    }
433
434
3.63M
    return profile1->hashcode == profile2->hashcode;
435
3.63M
}
436
437
void
438
gsicc_get_icc_buff_hash(unsigned char *buffer, int64_t *hash, unsigned int buff_size)
439
1.20M
{
440
1.20M
    gsicc_get_buff_hash(buffer, hash, buff_size);
441
1.20M
}
442
443
static void
444
gsicc_get_buff_hash(unsigned char *data, int64_t *hash, unsigned int num_bytes)
445
1.20M
{
446
1.20M
    gs_md5_state_t md5;
447
1.20M
    byte digest[16];
448
1.20M
    int k;
449
1.20M
    uint64_t word1,word2,shift;
450
451
   /* We could probably do something faster than this. But use this for now. */
452
1.20M
    gs_md5_init(&md5);
453
1.20M
    gs_md5_append(&md5, data, num_bytes);
454
1.20M
    gs_md5_finish(&md5, digest);
455
456
    /* For now, xor this into 64 bit word */
457
1.20M
    word1 = 0;
458
1.20M
    word2 = 0;
459
1.20M
    shift = 0;
460
461
    /* need to do it this way because of
462
       potential word boundary issues */
463
10.8M
    for( k = 0; k<8; k++) {
464
9.67M
       word1 += ((uint64_t) digest[k]) << shift;
465
9.67M
       word2 += ((uint64_t) digest[k+8]) << shift;
466
9.67M
       shift += 8;
467
9.67M
    }
468
1.20M
    *hash = word1 ^ word2;
469
1.20M
}
470
471
/* Compute a hash code for the current transformation case.
472
    This just computes a 64bit xor of upper and lower portions of
473
    md5 for the input, output
474
    and rendering params structure.  We may change this later */
475
static int
476
gsicc_compute_linkhash(gsicc_manager_t *icc_manager, gx_device *dev,
477
                       cmm_profile_t *input_profile,
478
                       cmm_profile_t *output_profile,
479
                       gsicc_rendering_param_t *rendering_params,
480
                       gsicc_hashlink_t *hash)
481
184M
{
482
184M
    int code;
483
484
    /* first get the hash codes for the color spaces */
485
184M
    code = gsicc_get_cspace_hash(icc_manager, dev, input_profile,
486
184M
                                 &(hash->src_hash));
487
184M
    if (code < 0)
488
0
        return code;
489
184M
    code = gsicc_get_cspace_hash(icc_manager, dev, output_profile,
490
184M
                                 &(hash->des_hash));
491
184M
    if (code < 0)
492
0
        return code;
493
494
    /* now for the rendering paramaters, just use the word itself.  At this
495
       point in time, we only include the black point setting, the intent
496
       and if we are preserving black.  We don't differentiate at this time
497
       with object type since the current CMM does not create different
498
       links based upon this type setting.  Other parameters such as cmm
499
       and override ICC are used prior to a link creation and so should also
500
       not factor into the link hash calculation */
501
184M
    hash->rend_hash = ((rendering_params->black_point_comp) << BP_SHIFT) +
502
184M
                      ((rendering_params->rendering_intent) << REND_SHIFT) +
503
184M
                      ((rendering_params->preserve_black) << PRESERVE_SHIFT);
504
    /* for now, mash all of these into a link hash */
505
184M
    gsicc_mash_hash(hash);
506
184M
    return 0;
507
184M
}
508
509
static int
510
gsicc_get_cspace_hash(gsicc_manager_t *icc_manager, gx_device *dev,
511
                      cmm_profile_t *cmm_icc_profile_data, int64_t *hash)
512
368M
{
513
368M
    cmm_dev_profile_t *dev_profile;
514
368M
    cmm_profile_t *icc_profile;
515
368M
    gsicc_rendering_param_t render_cond;
516
368M
    int code;
517
518
368M
    if (cmm_icc_profile_data == NULL)
519
0
    {
520
0
        if (dev == NULL)
521
0
            return -1;
522
0
        code = dev_proc(dev, get_profile)(dev, &dev_profile);
523
0
        if (code < 0)
524
0
            return code;
525
0
        gsicc_extract_profile(dev->graphics_type_tag, dev_profile,
526
0
            &(icc_profile), &render_cond);
527
0
        *hash = icc_profile->hashcode;
528
0
        return 0;
529
0
    }
530
368M
    if (cmm_icc_profile_data->hash_is_valid ) {
531
367M
        *hash = cmm_icc_profile_data->hashcode;
532
367M
    } else {
533
        /* We need to compute for this color space */
534
501k
        gsicc_get_icc_buff_hash(cmm_icc_profile_data->buffer, hash,
535
501k
                                cmm_icc_profile_data->buffer_size);
536
501k
        cmm_icc_profile_data->hashcode = *hash;
537
501k
        cmm_icc_profile_data->hash_is_valid = true;
538
501k
    }
539
368M
    return 0;
540
368M
}
541
542
gsicc_link_t*
543
gsicc_findcachelink(gsicc_hashlink_t hash, gsicc_link_cache_t *icc_link_cache,
544
                    bool includes_proof, bool includes_devlink)
545
184M
{
546
184M
    gsicc_link_t *curr, *prev;
547
184M
    int64_t hashcode = hash.link_hashcode;
548
184M
    int cache_loop = 0;
549
550
    /* Look through the cache for the hashcode */
551
184M
    gx_monitor_enter(icc_link_cache->lock);
552
553
    /* List scanning is fast, so we scan the entire list, this includes   */
554
    /* links that are currently unused, but still in the cache (zero_ref) */
555
184M
    curr = icc_link_cache->head;
556
184M
    prev = NULL;
557
558
185M
    while (curr != NULL ) {
559
184M
        if (curr->hashcode.link_hashcode == hashcode &&
560
183M
            includes_proof == curr->includes_softproof &&
561
183M
            includes_devlink == curr->includes_devlink) {
562
            /* move this one to the front of the list hoping we will use it
563
               again soon */
564
183M
            if (prev != NULL) {
565
                /* if prev == NULL, curr is already the head */
566
917k
                prev->next = curr->next;
567
917k
                curr->next = icc_link_cache->head;
568
917k
                icc_link_cache->head = curr;
569
917k
            }
570
            /* bump the ref_count since we will be using this one */
571
183M
            curr->ref_count++;
572
183M
            if_debug3m('^', curr->memory, "[^]%s "PRI_INTPTR" ++ => %d\n",
573
183M
                       "icclink", (intptr_t)curr, curr->ref_count);
574
183M
            while (curr->validity != 1) {
575
0
                int invalid = curr->validity == -1;
576
0
                gx_monitor_leave(icc_link_cache->lock); /* exit to let other threads run briefly */
577
0
                if (invalid || cache_loop > ICC_CACHE_NOT_VALID_COUNT) {
578
                    /* Clearly something is wrong.  Return NULL.
579
                       File a bug report. */
580
0
                    if (invalid)
581
0
                        emprintf(curr->memory, "Reached maximum invalid counts \n");
582
0
                    else
583
0
                        emprintf(curr->memory, "Reached maximum invalid counts \n");
584
                    /* We need to drop our link cache reference. */
585
0
                    {
586
0
                        int zerod;
587
0
                        gx_monitor_enter(icc_link_cache->lock);
588
0
                        curr->ref_count--;
589
0
                        zerod = curr->ref_count == 0;
590
0
                        gx_monitor_leave(icc_link_cache->lock);
591
0
                        if (zerod)
592
0
                            gsicc_remove_link(curr);
593
0
                    }
594
0
                    return NULL;
595
0
                }
596
0
                cache_loop++;
597
0
                gx_monitor_enter(curr->lock);     /* wait until we can acquire the lock */
598
0
                gx_monitor_leave(curr->lock);     /* it _should be valid now */
599
                /* If it is still not valid, but we were able to lock, it means that the thread */
600
                /* that was building it failed to be able to complete building it.  Try this only
601
                   a limited number of times before we bail. */
602
0
                if (curr->validity != 1) {
603
0
                    if_debug1m(gs_debug_flag_icc, curr->memory, "link "PRI_INTPTR" lock released, but still not valid.\n", (intptr_t)curr); /* Breakpoint here */
604
0
                }
605
0
                gx_monitor_enter(icc_link_cache->lock); /* re-enter to loop and check */
606
0
            }
607
183M
            gx_monitor_leave(icc_link_cache->lock);
608
183M
            return curr; /* success */
609
183M
        }
610
1.43M
        prev = curr;
611
1.43M
        curr = curr->next;
612
1.43M
    }
613
670k
    gx_monitor_leave(icc_link_cache->lock);
614
670k
    return NULL;
615
184M
}
616
617
/* Remove link from cache.  Notify CMS and free */
618
static void
619
gsicc_remove_link(gsicc_link_t *link)
620
670k
{
621
670k
    gsicc_link_t *curr, *prev;
622
670k
    gsicc_link_cache_t *icc_link_cache = link->icc_link_cache;
623
624
670k
    if_debug2m(gs_debug_flag_icc, link->memory,
625
670k
               "[icc] Removing link = "PRI_INTPTR" memory = "PRI_INTPTR"\n",
626
670k
               (intptr_t)link, (intptr_t)link->memory);
627
    /* NOTE: link->ref_count must be 0: assert ? */
628
670k
    gx_monitor_enter(icc_link_cache->lock);
629
670k
    if (link->ref_count != 0) {
630
0
      if_debug2m(gs_debug_flag_icc, link->memory, "link at "PRI_INTPTR" being removed, but has ref_count = %d\n", (intptr_t)link, link->ref_count);
631
0
    }
632
670k
    curr = icc_link_cache->head;
633
670k
    prev = NULL;
634
635
670k
    while (curr != NULL ) {
636
        /* don't get rid of it if another thread has decided to use it */
637
670k
        if (curr == link && link->ref_count == 0) {
638
            /* remove this one from the list */
639
670k
            if (prev == NULL)
640
670k
                icc_link_cache->head = curr->next;
641
0
            else
642
0
                prev->next = curr->next;
643
670k
            break;
644
670k
        }
645
0
        prev = curr;
646
0
        curr = curr->next;
647
0
    }
648
    /* if curr != link we didn't find it or another thread may have decided to */
649
    /* use it (ref_count > 0). Skip freeing it if so.                          */
650
670k
    if (curr == link && link->ref_count == 0) {
651
670k
        icc_link_cache->num_links--;  /* no longer in the cache */
652
670k
        if (icc_link_cache->cache_full) {
653
0
            icc_link_cache->cache_full = false;
654
0
            gx_semaphore_signal(icc_link_cache->full_wait);  /* let a waiting thread run */
655
0
        }
656
670k
        gx_monitor_leave(icc_link_cache->lock);
657
670k
        gsicc_link_free(link);  /* outside link cache now. */
658
670k
    } else {
659
        /* even if we didn't find the link to remove, unlock the cache */
660
0
        gx_monitor_leave(icc_link_cache->lock);
661
0
    }
662
670k
}
663
664
static void
665
gsicc_get_srcprofile(gsicc_colorbuffer_t data_cs,
666
    gs_graphics_type_tag_t graphics_type_tag,
667
    cmm_srcgtag_profile_t *srcgtag_profile, cmm_profile_t **profile,
668
    gsicc_rendering_param_t *render_cond)
669
0
{
670
0
    (*profile) = NULL;
671
0
    (*render_cond).rendering_intent = gsPERCEPTUAL;
672
0
    (*render_cond).cmm = gsCMM_DEFAULT;
673
0
    switch (graphics_type_tag & GS_DEVICE_TAG_MASK) {
674
0
    case GS_UNKNOWN_TAG:
675
0
    case GS_UNTOUCHED_TAG:
676
0
    default:
677
0
        break;
678
0
    case GS_VECTOR_TAG:
679
0
        if (data_cs == gsRGB) {
680
0
            (*profile) = srcgtag_profile->rgb_profiles[gsSRC_GRAPPRO];
681
0
            *render_cond = srcgtag_profile->rgb_rend_cond[gsSRC_GRAPPRO];
682
0
        }
683
0
        else if (data_cs == gsCMYK) {
684
0
            (*profile) = srcgtag_profile->cmyk_profiles[gsSRC_GRAPPRO];
685
0
            *render_cond = srcgtag_profile->cmyk_rend_cond[gsSRC_GRAPPRO];
686
0
        }
687
0
        else if (data_cs == gsGRAY) {
688
0
            (*profile) = srcgtag_profile->gray_profiles[gsSRC_GRAPPRO];
689
0
            *render_cond = srcgtag_profile->gray_rend_cond[gsSRC_GRAPPRO];
690
0
        }
691
0
        break;
692
0
    case GS_IMAGE_TAG:
693
0
        if (data_cs == gsRGB) {
694
0
            (*profile) = srcgtag_profile->rgb_profiles[gsSRC_IMAGPRO];
695
0
            *render_cond = srcgtag_profile->rgb_rend_cond[gsSRC_IMAGPRO];
696
0
        }
697
0
        else if (data_cs == gsCMYK) {
698
0
            (*profile) = srcgtag_profile->cmyk_profiles[gsSRC_IMAGPRO];
699
0
            *render_cond = srcgtag_profile->cmyk_rend_cond[gsSRC_IMAGPRO];
700
0
        }
701
0
        else if (data_cs == gsGRAY) {
702
0
            (*profile) = srcgtag_profile->gray_profiles[gsSRC_IMAGPRO];
703
0
            *render_cond = srcgtag_profile->gray_rend_cond[gsSRC_IMAGPRO];
704
0
        }
705
0
        break;
706
0
    case GS_TEXT_TAG:
707
0
        if (data_cs == gsRGB) {
708
0
            (*profile) = srcgtag_profile->rgb_profiles[gsSRC_TEXTPRO];
709
0
            *render_cond = srcgtag_profile->rgb_rend_cond[gsSRC_TEXTPRO];
710
0
        }
711
0
        else if (data_cs == gsCMYK) {
712
0
            (*profile) = srcgtag_profile->cmyk_profiles[gsSRC_TEXTPRO];
713
0
            *render_cond = srcgtag_profile->cmyk_rend_cond[gsSRC_TEXTPRO];
714
0
        }
715
0
        else if (data_cs == gsGRAY) {
716
0
            (*profile) = srcgtag_profile->gray_profiles[gsSRC_TEXTPRO];
717
0
            *render_cond = srcgtag_profile->gray_rend_cond[gsSRC_TEXTPRO];
718
0
        }
719
0
        break;
720
0
    }
721
0
}
722
723
gsicc_link_t*
724
gsicc_get_link(const gs_gstate *pgs, gx_device *dev,
725
               const gs_color_space *pcs_in,
726
               gs_color_space *output_colorspace,
727
               gsicc_rendering_param_t *rendering_params, gs_memory_t *memory)
728
183M
{
729
183M
    cmm_profile_t *gs_input_profile;
730
183M
    cmm_profile_t *gs_srcgtag_profile = NULL;
731
183M
    cmm_profile_t *gs_output_profile;
732
183M
    gsicc_rendering_param_t render_cond;
733
183M
    cmm_dev_profile_t *dev_profile;
734
183M
    int code;
735
183M
    bool devicegraytok;
736
183M
    gs_color_space *input_colorspace = (gs_color_space*) pcs_in;
737
738
183M
    if (dev == NULL) {
739
        /* This only occurs for the other (non-ps/pdf) interpreters */
740
44.3k
        dev = pgs->device;
741
44.3k
    }
742
183M
    if (input_colorspace->cmm_icc_profile_data == NULL) {
743
0
        if (input_colorspace->icc_equivalent != NULL) {
744
0
            gs_input_profile = input_colorspace->icc_equivalent->cmm_icc_profile_data;
745
0
        } else {
746
            /* Use default type */
747
0
            gs_input_profile = gsicc_get_gscs_profile(input_colorspace,
748
0
                                                      pgs->icc_manager);
749
0
        }
750
183M
    } else {
751
183M
        gs_input_profile = input_colorspace->cmm_icc_profile_data;
752
183M
    }
753
183M
    code = dev_proc(dev, get_profile)(dev,  &dev_profile);
754
183M
    if (code < 0)
755
0
        return NULL;
756
    /* If present, use an graphic object defined source profile */
757
183M
    if (pgs->icc_manager != NULL &&
758
183M
        pgs->icc_manager->srcgtag_profile != NULL) {
759
            /* This is to do with 'object' based colour management which allows the user to
760
             * selectively disable (or override) colour management based on the colour space
761
             * and object type. The problem is that the profile data_cs is not what we'd expect
762
             * for CIEBased input, so we need to check an additional member. See Bug #706789.
763
             */
764
0
            if ((gs_input_profile->data_cs == gsRGB
765
0
                || gs_input_profile->data_cs == gsCMYK
766
0
                || gs_input_profile->data_cs == gsGRAY) && gs_input_profile->default_match < CIE_A) {
767
0
                gsicc_get_srcprofile(gs_input_profile->data_cs,
768
0
                                      dev->graphics_type_tag,
769
0
                                      pgs->icc_manager->srcgtag_profile,
770
0
                                      &(gs_srcgtag_profile), &render_cond);
771
0
                if (gs_srcgtag_profile != NULL) {
772
                    /* In this case, the user is letting the source profiles
773
                       drive the color management.  Let that set the
774
                       rendering intent and blackpoint compensation also as they
775
                       must know what they are doing.  However, before we do
776
                       this we need to check if they want to overide
777
                       embedded source profiles.   See if our profile is a
778
                       default one that came from DefaultRGB or DefaultCMYK
779
                       for example */
780
0
                    int csi;
781
782
0
                    csi = gsicc_get_default_type(gs_input_profile);
783
0
                    if (render_cond.override_icc ||
784
0
                        csi == gs_color_space_index_DeviceRGB ||
785
0
                        csi == gs_color_space_index_DeviceCMYK ||
786
0
                        csi == gs_color_space_index_DeviceGray) {
787
0
                        gs_input_profile = gs_srcgtag_profile;
788
0
                        (*rendering_params) = render_cond;
789
0
                    }
790
                    /* We also need to worry about the case when the source
791
                       profile is actually a device link profile.  In this case
792
                       we can go ahead now and the our link transform as we
793
                       don't need to worry about a destination profile.
794
                       However, it is possible that someone could do another
795
                       device link profile associated with the device. */
796
0
                    if (gs_input_profile->isdevlink) {
797
                        /* OK. Go ahead and use this one.  Note output profile
798
                           is not NULL so that we can compute a hash with out
799
                           special conditional logic */
800
0
                        rendering_params->rendering_intent =
801
0
                            render_cond.rendering_intent & gsRI_MASK;
802
0
                        rendering_params->black_point_comp =
803
0
                            render_cond.black_point_comp & gsBP_MASK;
804
805
0
                            return gsicc_get_link_profile(pgs, dev, gs_input_profile,
806
0
                                             dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE],
807
0
                                             rendering_params, memory, false);
808
0
                    }
809
0
                } else {
810
                    /* In this case we may be wanting for a "unmanaged color"
811
                       result. This is done by specifying "None" on the
812
                       particular line for that source object.  Check if this
813
                       is what is desired.  If it is, then return the link now.
814
                       Also need to worry about the replace case */
815
0
                    if (render_cond.cmm == gsCMM_NONE) {
816
0
                        gsicc_link_t *link;
817
818
0
                        link = gsicc_nocm_get_link(pgs, dev, gs_input_profile->num_comps);
819
820
                        /* Set the identity case if we are in that situation */
821
0
                        if (link != NULL) {
822
0
                            if (gs_input_profile->num_comps ==
823
0
                                dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE]->num_comps) {
824
0
                                link->is_identity = true;
825
0
                            }
826
0
                            return link;
827
0
                        }
828
0
                    } else if (render_cond.cmm == gsCMM_REPLACE) {
829
0
                        return gsicc_rcm_get_link(pgs, dev,
830
0
                                                  gs_input_profile->data_cs);
831
                        /* Note that there is never an identity case  */
832
0
                    }
833
0
                }
834
0
            }
835
0
    }
836
183M
    if (output_colorspace != NULL) {
837
25
        gs_output_profile = output_colorspace->cmm_icc_profile_data;
838
25
        devicegraytok = false;
839
183M
    } else {
840
        /* Check for unmanaged color case */
841
183M
        if (gsicc_use_fast_color(gs_input_profile) > 0 && dev_profile->usefastcolor) {
842
            /* Return a "link" from the source space to the device color space */
843
0
            gsicc_link_t *link = gsicc_nocm_get_link(pgs, dev,
844
0
                                                     gs_input_profile->num_comps);
845
0
            if (link != NULL) {
846
0
                if (gs_input_profile->num_comps ==
847
0
                    dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE]->num_comps) {
848
0
                    link->is_identity = true;
849
0
                }
850
0
                return link;
851
0
            }
852
0
        }
853
854
        /* Use the device profile. Only use the rendering intent if it has
855
           an override setting.  Also, only use the blackpoint if overide_bp
856
           is set. Note that this can conflict with intents set from the source
857
           objects so the user needs to understand what options to set. */
858
183M
        gsicc_extract_profile(dev->graphics_type_tag, dev_profile,
859
183M
                               &(gs_output_profile), &render_cond);
860
        /* Check if the incoming rendering intent was source based
861
           (this can occur for high level images in the clist) in
862
           that case we need to use the source ri and not the device one */
863
183M
        if (!(rendering_params->rendering_intent & gsRI_OVERRIDE)) {
864
            /* No it was not.  Check if our device profile RI was specified */
865
183M
            if (render_cond.rendering_intent != gsRINOTSPECIFIED) {
866
0
                rendering_params->rendering_intent = render_cond.rendering_intent;
867
0
            }
868
183M
        }
869
        /* Similar for the black point compensation */
870
183M
        if (!(rendering_params->black_point_comp & gsBP_OVERRIDE)) {
871
183M
            if (render_cond.black_point_comp != gsBPNOTSPECIFIED) {
872
0
                rendering_params->black_point_comp = render_cond.black_point_comp;
873
0
            }
874
183M
        }
875
        /* And the Black preservation */
876
183M
        if (!(rendering_params->preserve_black & gsKP_OVERRIDE)) {
877
183M
            if (render_cond.preserve_black != gsBKPRESNOTSPECIFIED) {
878
0
                rendering_params->preserve_black = render_cond.preserve_black;
879
0
            }
880
183M
        }
881
183M
        devicegraytok = dev_profile->devicegraytok;
882
183M
    }
883
    /* If we are going from DeviceGray to DeviceCMYK and devicegraytok
884
       is true then use the ps_gray and ps_cmyk profiles instead of these
885
       profiles */
886
183M
    rendering_params->rendering_intent = rendering_params->rendering_intent & gsRI_MASK;
887
183M
    rendering_params->black_point_comp = rendering_params->black_point_comp & gsBP_MASK;
888
183M
    rendering_params->preserve_black = rendering_params->preserve_black & gsKP_MASK;
889
183M
    return gsicc_get_link_profile(pgs, dev, gs_input_profile, gs_output_profile,
890
183M
                    rendering_params, memory, devicegraytok);
891
183M
}
892
893
/* This operation of adding in a new link entry is actually shared amongst
894
   different functions that can each add an entry.  For example, entrys may
895
   come from the CMM or they may come from the non color managed approach
896
   (i.e. gsicc_nocm_get_link)
897
   Returns true if link was found with that has, false if alloc fails.
898
*/
899
bool
900
gsicc_alloc_link_entry(gsicc_link_cache_t *icc_link_cache,
901
                       gsicc_link_t **ret_link, gsicc_hashlink_t hash,
902
                       bool include_softproof, bool include_devlink)
903
670k
{
904
670k
    gs_memory_t *cache_mem = icc_link_cache->memory;
905
670k
    gsicc_link_t *link;
906
670k
    int retries = 0;
907
908
670k
    assert(cache_mem == cache_mem->stable_memory);
909
910
670k
    *ret_link = NULL;
911
    /* First see if we can add a link */
912
    /* TODO: this should be based on memory usage, not just num_links */
913
670k
    gx_monitor_enter(icc_link_cache->lock);
914
670k
    while (icc_link_cache->num_links >= ICC_CACHE_MAXLINKS) {
915
        /* Look through the cache for first zero ref count to re-use that entry.
916
           When ref counts go to zero, the icc_link will have been moved to
917
           the end of the list, so the first we find is the 'oldest'.
918
           If we get to the last entry we release the lock, set the cache_full
919
           flag and wait on full_wait for some other thread to let this thread
920
           run again after releasing a cache slot. Release the cache lock to
921
           let other threads run and finish with (release) a cache entry.
922
        */
923
0
        link = icc_link_cache->head;
924
0
        while (link != NULL ) {
925
0
            if (link->ref_count == 0) {
926
                /* we will use this one */
927
0
                if_debug3m('^', cache_mem, "[^]%s "PRI_INTPTR" ++ => %d\n",
928
0
                           "icclink", (intptr_t)link, link->ref_count);
929
0
                break;
930
0
            }
931
0
            link = link->next;
932
0
        }
933
0
        if (link == NULL) {
934
0
            icc_link_cache->cache_full = true;
935
            /* unlock while waiting for a link to come available */
936
0
            gx_monitor_leave(icc_link_cache->lock);
937
0
            gx_semaphore_wait(icc_link_cache->full_wait);
938
            /* repeat the findcachelink to see if some other thread has */
939
            /* already started building the link we need    */
940
0
            *ret_link = gsicc_findcachelink(hash, icc_link_cache,
941
0
                                            include_softproof, include_devlink);
942
            /* Got a hit, return link. ref_count for the link was already bumped */
943
0
            if (*ret_link != NULL)
944
0
                return true;
945
946
0
            gx_monitor_enter(icc_link_cache->lock);     /* restore the lock */
947
            /* we will re-test the num_links above while locked to insure */
948
            /* that some other thread didn't grab the slot and max us out */
949
0
            if (retries++ > 10)
950
0
                return false;
951
0
        } else {
952
            /* Remove the zero ref_count link profile we found.   */
953
            /* Even if we remove this link, we may still be maxed out so*/
954
            /* the outermost 'while' will check to make sure some other */
955
            /* thread did not grab the one we remove.     */
956
0
            gsicc_remove_link(link);
957
0
        }
958
0
    }
959
    /* insert an empty link that we will reserve so we can unlock while */
960
    /* building the link contents. If successful, the entry will set  */
961
    /* the hash for the link, Set valid=false, and lock the profile     */
962
670k
    (*ret_link) = gsicc_alloc_link(cache_mem, hash);
963
    /* NB: the link returned will be have the lock owned by this thread */
964
    /* the lock will be released when the link becomes valid.           */
965
670k
    if (*ret_link) {
966
670k
        (*ret_link)->icc_link_cache = icc_link_cache;
967
670k
        (*ret_link)->next = icc_link_cache->head;
968
670k
        icc_link_cache->head = *ret_link;
969
670k
        icc_link_cache->num_links++;
970
670k
    }
971
    /* unlock before returning */
972
670k
    gx_monitor_leave(icc_link_cache->lock);
973
670k
    return false; /* we didn't find it, but return a link to be filled */
974
670k
}
975
976
/* This is the main function called to obtain a linked transform from the ICC
977
   cache If the cache has the link ready, it will return it.  If not, it will
978
   request one from the CMS and then return it.  We may need to do some cache
979
   locking during this process to avoid multi-threaded issues (e.g. someone
980
   deleting while someone is updating a reference count).  Note that if the
981
   source profile is a device link profile we have no output profile but
982
   may still have a proofing or another device link profile to use */
983
gsicc_link_t*
984
gsicc_get_link_profile(const gs_gstate *pgs, gx_device *dev,
985
                       cmm_profile_t *gs_input_profile,
986
                       cmm_profile_t *gs_output_profile,
987
                       gsicc_rendering_param_t *rendering_params,
988
                       gs_memory_t *memory, bool devicegraytok)
989
184M
{
990
184M
    gsicc_hashlink_t hash;
991
184M
    gsicc_link_t *link, *found_link;
992
184M
    gcmmhlink_t link_handle = NULL;
993
184M
    gsicc_manager_t *icc_manager = pgs->icc_manager;
994
184M
    gsicc_link_cache_t *icc_link_cache = pgs->icc_link_cache;
995
184M
    gs_memory_t *cache_mem = pgs->icc_link_cache->memory;
996
184M
    gcmmhprofile_t *cms_input_profile = NULL;
997
184M
    gcmmhprofile_t *cms_output_profile = NULL;
998
184M
    gcmmhprofile_t *cms_proof_profile = NULL;
999
184M
    gcmmhprofile_t *cms_devlink_profile = NULL;
1000
184M
    int code;
1001
184M
    bool include_softproof = false;
1002
184M
    bool include_devicelink = false;
1003
184M
    cmm_dev_profile_t *dev_profile;
1004
184M
    cmm_profile_t *proof_profile = NULL;
1005
184M
    cmm_profile_t *devlink_profile = NULL;
1006
184M
    bool src_dev_link = gs_input_profile->isdevlink;
1007
184M
    bool pageneutralcolor = false;
1008
184M
    int cms_flags = 0;
1009
1010
    /* Determine if we are using a soft proof or device link profile */
1011
184M
    if (dev != NULL ) {
1012
184M
        code = dev_proc(dev, get_profile)(dev,  &dev_profile);
1013
184M
        if (code < 0)
1014
0
            return NULL;
1015
184M
        if (dev_profile != NULL) {
1016
184M
            proof_profile = dev_profile->proof_profile;
1017
184M
            devlink_profile = dev_profile->link_profile;
1018
184M
            pageneutralcolor = dev_profile->pageneutralcolor;
1019
184M
        }
1020
        /* If the source color is the same as the proofing color then we do not
1021
           need to apply the proofing color in this case.  This occurs in cases
1022
           where we have a CMYK output intent profile, a Device CMYK color, and
1023
           are going out to an RGB device */
1024
184M
        if (proof_profile != NULL ) {
1025
0
            if (proof_profile->hashcode == gs_input_profile->hashcode) {
1026
0
                proof_profile = NULL;
1027
0
            } else {
1028
0
                include_softproof = true;
1029
0
            }
1030
0
        }
1031
184M
        if (devlink_profile != NULL) include_devicelink = true;
1032
184M
    }
1033
    /* First compute the hash code for the incoming case.  If the output color
1034
       space is NULL we will use the device profile for the output color space */
1035
184M
    code = gsicc_compute_linkhash(icc_manager, dev, gs_input_profile,
1036
184M
                                  gs_output_profile,
1037
184M
                                  rendering_params, &hash);
1038
184M
    if (code < 0)
1039
0
        return NULL;
1040
    /* Check the cache for a hit.  Need to check if softproofing was used */
1041
184M
    found_link = gsicc_findcachelink(hash, icc_link_cache, include_softproof,
1042
184M
                                     include_devicelink);
1043
    /* Got a hit, return link (ref_count for the link was already bumped */
1044
184M
    if (found_link != NULL) {
1045
183M
        if_debug2m(gs_debug_flag_icc, memory,
1046
183M
                   "[icc] Found Link = "PRI_INTPTR", hash = %lld \n",
1047
183M
                   (intptr_t)found_link, (long long)hash.link_hashcode);
1048
183M
        if_debug2m(gs_debug_flag_icc, memory,
1049
183M
                   "[icc] input_numcomps = %d, input_hash = %lld \n",
1050
183M
                   gs_input_profile->num_comps,
1051
183M
                   (long long)gs_input_profile->hashcode);
1052
183M
        if_debug2m(gs_debug_flag_icc, memory,
1053
183M
                   "[icc] output_numcomps = %d, output_hash = %lld \n",
1054
183M
                   gs_output_profile->num_comps,
1055
183M
                   (long long)gs_output_profile->hashcode);
1056
183M
        return found_link;
1057
183M
    }
1058
    /* Before we do anything, check if we have a case where the source profile
1059
       is coming from the clist and we don't even want to be doing any color
1060
       managment */
1061
670k
    if (gs_input_profile->profile_handle == NULL &&
1062
514k
        gs_input_profile->buffer == NULL &&
1063
8.30k
        gs_input_profile->dev != NULL) {
1064
1065
        /* ICC profile should be in clist. This is the first call to it.  Note that
1066
           the profiles are not really shared amongst threads like the links are.
1067
           Hence the memory is for the local thread's chunk */
1068
8.30k
        cms_input_profile =
1069
8.30k
            gsicc_get_profile_handle_clist(gs_input_profile,
1070
8.30k
                                           gs_input_profile->memory);
1071
8.30k
        gs_input_profile->profile_handle = cms_input_profile;
1072
1073
        /* It is possible that we are not using color management
1074
           due to a setting forced from srcgtag object (the None option)
1075
           which has made its way though the clist in the clist imaging
1076
           code.   In this case, the srcgtag_profile structure
1077
           which was part of the ICC manager is no longer available.
1078
           We also have the Replace option.  */
1079
8.30k
        if (gs_input_profile->rend_is_valid &&
1080
4.22k
            gs_input_profile->rend_cond.cmm == gsCMM_NONE) {
1081
1082
0
            link = gsicc_nocm_get_link(pgs, dev, gs_input_profile->num_comps);
1083
1084
            /* Set the identity case if we are in that situation */
1085
0
            if (link != NULL) {
1086
0
                if (dev_profile != NULL && gs_input_profile->num_comps ==
1087
0
                    dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE]->num_comps) {
1088
0
                    link->is_identity = true;
1089
0
                }
1090
0
                return link;
1091
0
            }
1092
8.30k
        } else if (gs_input_profile->rend_is_valid &&
1093
4.22k
            gs_input_profile->rend_cond.cmm == gsCMM_REPLACE) {
1094
0
            return gsicc_rcm_get_link(pgs, dev, gs_input_profile->data_cs);
1095
            /* Note that there is never an identity case for
1096
               this type.  */
1097
0
        }
1098
        /* We may have a source profile that is a device link profile and
1099
           made its way through the clist.  If so get things set up to
1100
           handle that properly. */
1101
8.30k
        src_dev_link = gs_input_profile->isdevlink;
1102
8.30k
    }
1103
    /* No link was found so lets create a new one if there is room. This will
1104
       usually return a link that is not yet valid, but may return a valid link
1105
       if another thread has already created it */
1106
670k
    if (gsicc_alloc_link_entry(icc_link_cache, &link, hash, include_softproof,
1107
670k
                               include_devicelink))
1108
0
        return link;
1109
670k
    if (link == NULL)
1110
0
        return NULL;   /* error, couldn't allocate a link.  Nothing to cleanup */
1111
1112
    /* Here the link was new and the contents have valid=false and we */
1113
    /* own the lock for the link_profile. Build the profile, set valid  */
1114
    /* to true and release the lock.          */
1115
    /* Now compute the link contents */
1116
670k
    cms_input_profile = gs_input_profile->profile_handle;
1117
    /*  Check if the source was generated from a PS CIE color space.  If yes,
1118
        then we need to make sure that the CMM does not do something like
1119
        force a white point mapping like lcms does.  This is also the source
1120
        of the issue with the flower picture that has the hand made ICC
1121
        profile that is highly nonlinear at the whitepoint */
1122
670k
    if (gsicc_profile_from_ps(gs_input_profile)) {
1123
51
        cms_flags = cms_flags | gscms_avoid_white_fix_flag(memory);
1124
51
    }
1125
670k
    if (cms_input_profile == NULL) {
1126
506k
        if (gs_input_profile->buffer != NULL) {
1127
506k
            cms_input_profile =
1128
506k
                gsicc_get_profile_handle_buffer(gs_input_profile->buffer,
1129
506k
                                                gs_input_profile->buffer_size,
1130
506k
                                                memory);
1131
506k
            if (cms_input_profile == NULL)
1132
696
                goto drop_link_ref;
1133
1134
505k
            gs_input_profile->profile_handle = cms_input_profile;
1135
            /* This *must* be a default profile that was not set up at start-up/
1136
               However it could be one from the icc creator code which does not
1137
               do an initialization at the time of creation from CalRGB etc. */
1138
505k
            code = gsicc_initialize_default_profile(gs_input_profile);
1139
505k
            if (code < 0)
1140
580
                goto drop_link_ref;
1141
505k
        } else {
1142
            /* Cant create the link.  No profile present,
1143
               nor any defaults to use for this. */
1144
0
            goto drop_link_ref;
1145
0
        }
1146
506k
    }
1147
    /* No need to worry about an output profile handle if our source is a
1148
       device link profile */
1149
669k
    if (!src_dev_link) {
1150
669k
        cms_output_profile = gs_output_profile->profile_handle;
1151
669k
    }
1152
669k
    if (cms_output_profile == NULL && !src_dev_link) {
1153
2.28k
        if (gs_output_profile->buffer != NULL) {
1154
508
            cms_output_profile =
1155
508
                gsicc_get_profile_handle_buffer(gs_output_profile->buffer,
1156
508
                                                gs_output_profile->buffer_size,
1157
508
                                                memory);
1158
508
            gs_output_profile->profile_handle = cms_output_profile;
1159
            /* This *must* be a default profile that was not set up at start-up */
1160
508
            code = gsicc_initialize_default_profile(gs_output_profile);
1161
508
            if (code < 0)
1162
0
                goto drop_link_ref;
1163
1.77k
        } else {
1164
              /* See if we have a clist device pointer. */
1165
1.77k
            if ( gs_output_profile->dev != NULL ) {
1166
                /* ICC profile should be in clist. This is
1167
                   the first call to it. */
1168
1.77k
                cms_output_profile =
1169
1.77k
                    gsicc_get_profile_handle_clist(gs_output_profile,
1170
1.77k
                                                   gs_output_profile->memory);
1171
1.77k
                gs_output_profile->profile_handle = cms_output_profile;
1172
1.77k
            } else {
1173
                /* Cant create the link.  No profile present,
1174
                   nor any defaults to use for this. */
1175
0
                goto drop_link_ref;
1176
0
            }
1177
1.77k
        }
1178
2.28k
    }
1179
669k
    if (include_softproof) {
1180
0
        cms_proof_profile = proof_profile->profile_handle;
1181
0
        if (cms_proof_profile == NULL) {
1182
0
            if (proof_profile->buffer != NULL) {
1183
0
                cms_proof_profile =
1184
0
                    gsicc_get_profile_handle_buffer(proof_profile->buffer,
1185
0
                                                    proof_profile->buffer_size,
1186
0
                                                    memory);
1187
0
                proof_profile->profile_handle = cms_proof_profile;
1188
0
                if (!gscms_is_threadsafe())
1189
0
                    gx_monitor_enter(proof_profile->lock);
1190
0
            } else {
1191
                /* Cant create the link */
1192
0
                goto drop_link_ref;
1193
0
            }
1194
0
        }
1195
0
    }
1196
669k
    if (include_devicelink) {
1197
0
        cms_devlink_profile = devlink_profile->profile_handle;
1198
0
        if (cms_devlink_profile == NULL) {
1199
0
            if (devlink_profile->buffer != NULL) {
1200
0
                cms_devlink_profile =
1201
0
                    gsicc_get_profile_handle_buffer(devlink_profile->buffer,
1202
0
                                                    devlink_profile->buffer_size,
1203
0
                                                    memory);
1204
0
                devlink_profile->profile_handle = cms_devlink_profile;
1205
0
                if (!gscms_is_threadsafe())
1206
0
                    gx_monitor_enter(devlink_profile->lock);
1207
0
            } else {
1208
                /* Cant create the link */
1209
0
                goto drop_link_ref;
1210
0
            }
1211
0
        }
1212
0
    }
1213
    /* Profile reading of same structure not thread safe in CMM */
1214
669k
    if (!gscms_is_threadsafe()) {
1215
0
        gx_monitor_enter(gs_input_profile->lock);
1216
0
        if (!src_dev_link) {
1217
0
            gx_monitor_enter(gs_output_profile->lock);
1218
0
        }
1219
0
    }
1220
    /* We may have to worry about special handling for DeviceGray to
1221
       DeviceCMYK to ensure that Gray is mapped to K only.  This is only
1222
       done once and then it is cached and the link used.  Note that Adobe
1223
       appears to do this only when the source color space was DeviceGray.
1224
       For us, this requirement is meant by the test of
1225
       gs_input_profile->default_match == DEFAULT_GRAY */
1226
669k
    if (!src_dev_link && gs_output_profile->data_cs == gsCMYK &&
1227
31.4k
        gs_input_profile->data_cs == gsGRAY &&
1228
21.0k
        gs_input_profile->default_match == DEFAULT_GRAY &&
1229
19.3k
        pgs->icc_manager != NULL && devicegraytok) {
1230
19.3k
        if (icc_manager->graytok_profile == NULL) {
1231
19.1k
            assert(pgs->icc_manager->memory == pgs->icc_manager->memory->stable_memory);
1232
19.1k
            icc_manager->graytok_profile =
1233
19.1k
                gsicc_set_iccsmaskprofile(GRAY_TO_K, strlen(GRAY_TO_K),
1234
19.1k
                                          pgs->icc_manager,
1235
19.1k
                                          pgs->icc_manager->memory);
1236
19.1k
            if (icc_manager->graytok_profile == NULL) {
1237
                /* Cant create the link */
1238
0
                goto drop_link_ref;
1239
0
            }
1240
19.1k
        }
1241
19.3k
        if (icc_manager->smask_profiles == NULL) {
1242
19.1k
            code = gsicc_initialize_iccsmask(icc_manager);
1243
19.1k
        }
1244
19.3k
        cms_input_profile =
1245
19.3k
            icc_manager->smask_profiles->smask_gray->profile_handle;
1246
19.3k
        cms_output_profile =
1247
19.3k
            icc_manager->graytok_profile->profile_handle;
1248
        /* Turn off bp compensation in this case as there is a bug in lcms */
1249
19.3k
        rendering_params->black_point_comp = false;
1250
19.3k
        cms_flags = 0;  /* Turn off any flag setting */
1251
19.3k
    }
1252
    /* Get the link with the proof and or device link profile */
1253
669k
    if (include_softproof || include_devicelink || src_dev_link) {
1254
0
        link_handle = gscms_get_link_proof_devlink(cms_input_profile,
1255
0
                                                   cms_proof_profile,
1256
0
                                                   cms_output_profile,
1257
0
                                                   cms_devlink_profile,
1258
0
                                                   rendering_params,
1259
0
                                                   src_dev_link, cms_flags,
1260
0
                                                   cache_mem->non_gc_memory);
1261
0
    if (!gscms_is_threadsafe()) {
1262
0
        if (include_softproof) {
1263
0
            gx_monitor_leave(proof_profile->lock);
1264
0
        }
1265
0
        if (include_devicelink) {
1266
0
            gx_monitor_leave(devlink_profile->lock);
1267
0
        }
1268
0
    }
1269
669k
    } else {
1270
669k
        link_handle = gscms_get_link(cms_input_profile, cms_output_profile,
1271
669k
                                     rendering_params, cms_flags,
1272
669k
                                     cache_mem->non_gc_memory);
1273
669k
    }
1274
669k
    if (!gscms_is_threadsafe()) {
1275
0
        if (!src_dev_link) {
1276
0
            gx_monitor_leave(gs_output_profile->lock);
1277
0
        }
1278
0
        gx_monitor_leave(gs_input_profile->lock);
1279
0
    }
1280
669k
    if (link_handle != NULL) {
1281
541k
        if (gs_input_profile->data_cs == gsGRAY)
1282
473k
            pageneutralcolor = false;
1283
1284
541k
        gsicc_set_link_data(link, link_handle, hash,
1285
541k
                            include_softproof, include_devicelink, pageneutralcolor,
1286
541k
                            gs_input_profile->data_cs);
1287
1288
        /* release the lock of the link so it can now be used */
1289
541k
        gx_monitor_leave(link->lock);
1290
541k
        if_debug2m(gs_debug_flag_icc, cache_mem,
1291
541k
                   "[icc] New Link = "PRI_INTPTR", hash = %lld \n",
1292
541k
                   (intptr_t)link, (long long)hash.link_hashcode);
1293
541k
        if_debug2m(gs_debug_flag_icc, cache_mem,
1294
541k
                   "[icc] input_numcomps = %d, input_hash = %lld \n",
1295
541k
                   gs_input_profile->num_comps,
1296
541k
                   (long long)gs_input_profile->hashcode);
1297
541k
        if_debug2m(gs_debug_flag_icc, cache_mem,
1298
541k
                   "[icc] output_numcomps = %d, output_hash = %lld \n",
1299
541k
                   gs_output_profile->num_comps,
1300
541k
                   (long long)gs_output_profile->hashcode);
1301
541k
    } else {
1302
        /* If other threads are waiting, we won't have set link->valid true.  */
1303
        /* This could result in an infinite loop if other threads are waiting */
1304
        /* for it to be made valid. (see gsicc_findcachelink).      */
1305
128k
        if_debug2m('^', link->memory, "[^]icclink "PRI_INTPTR" -- => %d\n",
1306
128k
                   (intptr_t)link, link->ref_count);
1307
1308
128k
        if (icc_link_cache->cache_full) {
1309
0
            icc_link_cache->cache_full = false;
1310
0
            gx_semaphore_signal(icc_link_cache->full_wait);  /* let a waiting thread run */
1311
0
        }
1312
128k
        gx_monitor_leave(link->lock);
1313
128k
        goto drop_link_ref;
1314
128k
    }
1315
541k
    return link;
1316
1317
129k
drop_link_ref:
1318
    /* Something went very wrong. Clean up so that the cache
1319
       does not maintain an invalid entry.  Any other allocations
1320
       (e.g. profile handles) would get freed when the profiles
1321
        are freed */
1322
129k
    {
1323
129k
        int zerod;
1324
129k
        gx_monitor_enter(icc_link_cache->lock);
1325
129k
        link->ref_count--;
1326
129k
        zerod = link->ref_count == 0;
1327
129k
        gx_monitor_leave(icc_link_cache->lock);
1328
129k
        if (zerod)
1329
129k
            gsicc_remove_link(link);
1330
0
        else
1331
0
            link->validity = -1;
1332
129k
    }
1333
1334
129k
    return NULL;
1335
669k
}
1336
1337
/* The following is used to transform a named color value at a particular tint
1338
   value to the output device values.  This function is provided only as a
1339
   demonstration and will likely need to be altered and optimized for those wishing
1340
   to perform full spot color look-up support.
1341
1342
   The object used to perform the transformation is typically
1343
   a look-up table that contains the spot color name and a CIELAB value for
1344
   100% colorant (it could also contain device values in the table).
1345
   It can be more complex where-by you have a 1-D lut that
1346
   provides CIELAB values or direct device values as a function of tint.  In
1347
   such a case, the table would be interpolated to compute all possible tint values.
1348
   If CIELAB values are provided, they can be pushed through the
1349
   device profile using the CMM.  In this particular demonstration, we simply
1350
   provide CIELAB for a few color names in the file
1351
   toolbin/color/named_color/named_color_table.txt .
1352
   The tint value is used to scale the CIELAB value from 100% colorant to a D50
1353
   whitepoint.  The resulting CIELAB value is then pushed through the CMM to
1354
   obtain device values for the current device.  The file named_colors.pdf
1355
   which is in toolbin/color/named_color/ contains these
1356
   spot colors and will enable the user to see how the code behaves.  The named
1357
   color table is specified to ghostscript by the command line option
1358
   -sNamedProfile=./toolbin/color/named_color/named_color_table.txt (or with
1359
   full path name).  If it is desired to have ghostscript compiled with the
1360
   named color table, it can be placed in the iccprofiles directory and then
1361
   build ghostscript with COMPILE_INITS=1.  When specified the file contents
1362
   are pointed to by the buffer member variable of the device_named profile in
1363
   profile manager.  When the first call occurs in here, the contents of the
1364
   buffer are parsed and placed into a custom stucture that is pointed to by
1365
   the profile pointer.  Note that this pointer is not visible to the garbage
1366
   collector and should be allocated in non-gc memory as is demonstrated in
1367
   this sample.  The structure elements are released when the profile is
1368
   destroyed through the call to gsicc_named_profile_release, which is set
1369
   as the value of the profile member variable release.
1370
1371
   Note that there are calls defined in gsicc_littlecms.c that will create link
1372
   transforms between Named Color ICC profiles and the output device.  Such
1373
   profiles are rarely used (at least I have not run across any yet) so the
1374
   code is currently not used.  Also note that for those serious about named
1375
   color support, a cache as well as efficient table-look-up methods would
1376
   likely be important for performance.
1377
1378
   Finally note that PANTONE is a registered trademark and PANTONE colors are a
1379
   licensed product of XRITE Inc. See http://www.pantone.com
1380
   for more information.  Licensees of Pantone color libraries or similar
1381
   libraries should find it straight forward to interface.  Pantone names are
1382
   referred to in named_color_table.txt and contained in the file named_colors.pdf.
1383
1384
   !!!!IT WILL BE NECESSARY TO PERFORM THE PROPER DEALLOCATION
1385
       CLEAN-UP OF THE STRUCTURES WHEN rc_free_icc_profile OCCURS FOR THE NAMED
1386
       COLOR PROFILE!!!!!!   See gsicc_named_profile_release below for an example.
1387
       This is set in the profile release member variable.
1388
*/
1389
1390
/* Define the demo structure and function for named color look-up */
1391
1392
typedef struct gsicc_namedcolortable_s {
1393
    gsicc_namedcolor_t *named_color;    /* The named color */
1394
    unsigned int number_entries;        /* The number of entries */
1395
    gs_memory_t *memory;
1396
} gsicc_namedcolortable_t;
1397
1398
/* Support functions for parsing buffer */
1399
1400
static int
1401
get_to_next_line(char **buffptr, int *buffer_count)
1402
0
{
1403
0
    while (1) {
1404
0
        if (**buffptr == ';') {
1405
0
            (*buffptr)++;
1406
0
            (*buffer_count)--;
1407
0
            return(0);
1408
0
        } else {
1409
0
            (*buffptr)++;
1410
0
            (*buffer_count)--;
1411
0
        }
1412
0
        if (*buffer_count <= 0) {
1413
0
            return -1;
1414
0
        }
1415
0
    }
1416
0
}
1417
1418
/* Release the structures we allocated in gsicc_transform_named_color */
1419
static void
1420
gsicc_named_profile_release(void *ptr, gs_memory_t *memory)
1421
0
{
1422
0
    gsicc_namedcolortable_t *namedcolor_table = (gsicc_namedcolortable_t*) ptr;
1423
0
    unsigned int num_entries;
1424
0
    gs_memory_t *mem;
1425
0
    int k;
1426
0
    gsicc_namedcolor_t *namedcolor_data;
1427
1428
0
    if (namedcolor_table != NULL) {
1429
0
        mem = namedcolor_table->memory;
1430
0
        num_entries = namedcolor_table->number_entries;
1431
0
        namedcolor_data = namedcolor_table->named_color;
1432
1433
0
        for (k = 0; k < num_entries; k++) {
1434
0
            gs_free(mem, namedcolor_data[k].colorant_name, 1,
1435
0
                namedcolor_data[k].name_size + 1,
1436
0
                "gsicc_named_profile_release (colorant_name)");
1437
0
        }
1438
1439
0
        gs_free(mem, namedcolor_data, num_entries, sizeof(gsicc_namedcolor_t),
1440
0
            "gsicc_named_profile_release (namedcolor_data)");
1441
1442
0
        gs_free(namedcolor_table->memory, namedcolor_table, 1,
1443
0
            sizeof(gsicc_namedcolortable_t),
1444
0
            "gsicc_named_profile_release (namedcolor_table)");
1445
0
    }
1446
0
}
1447
1448
static int
1449
create_named_profile(gs_memory_t *mem, cmm_profile_t *named_profile)
1450
0
{
1451
    /* Create the structure that we will use in searching */
1452
    /*  Note that we do this in non-GC memory since the
1453
        profile pointer is not GC'd */
1454
0
    gsicc_namedcolortable_t *namedcolor_table;
1455
0
    gsicc_namedcolor_t *namedcolor_data;
1456
0
    char *buffptr;
1457
0
    int buffer_count;
1458
0
    int count;
1459
0
    unsigned int num_entries;
1460
0
    int code;
1461
0
    int k, j;
1462
0
    char *pch, *temp_ptr, *last = NULL;
1463
0
    bool done;
1464
0
    int curr_name_size;
1465
0
    float lab[3];
1466
1467
0
    namedcolor_table =
1468
0
        (gsicc_namedcolortable_t*)gs_malloc(mem, 1,
1469
0
            sizeof(gsicc_namedcolortable_t), "create_named_profile");
1470
0
    if (namedcolor_table == NULL)
1471
0
        return_error(gs_error_VMerror);
1472
0
    namedcolor_table->memory = mem;
1473
1474
    /* Parse buffer and load the structure we will be searching */
1475
0
    buffptr = (char*)named_profile->buffer;
1476
0
    buffer_count = named_profile->buffer_size;
1477
0
    count = sscanf(buffptr, "%ud", &num_entries);
1478
0
    if (num_entries < 1 || count <= 0) {
1479
0
        gs_free(mem, namedcolor_table, 1, sizeof(gsicc_namedcolortable_t),
1480
0
            "create_named_profile");
1481
0
        return -1;
1482
0
    }
1483
1484
0
    code = get_to_next_line(&buffptr, &buffer_count);
1485
0
    if (code < 0) {
1486
0
        gs_free(mem, namedcolor_table, 1, sizeof(gsicc_namedcolortable_t),
1487
0
            "create_named_profile");
1488
0
        return -1;
1489
0
    }
1490
0
    namedcolor_data =
1491
0
        (gsicc_namedcolor_t*)gs_malloc(mem, num_entries,
1492
0
            sizeof(gsicc_namedcolor_t), "create_named_profile");
1493
0
    if (namedcolor_data == NULL) {
1494
0
        gs_free(mem, namedcolor_table, num_entries,
1495
0
            sizeof(gsicc_namedcolortable_t), "create_named_profile");
1496
0
        return_error(gs_error_VMerror);
1497
0
    }
1498
0
    namedcolor_table->number_entries = num_entries;
1499
0
    namedcolor_table->named_color = namedcolor_data;
1500
0
    for (k = 0; k < num_entries; k++) {
1501
0
        if (k == 0) {
1502
0
            pch = gs_strtok(buffptr, ",;", &last);
1503
0
        } else {
1504
0
            pch = gs_strtok(NULL, ",;", &last);
1505
0
        }
1506
        /* Remove any /0d /0a stuff from start */
1507
0
        temp_ptr = pch;
1508
0
        if (pch == NULL)
1509
0
            return_error(gs_error_unknownerror);
1510
0
        done = 0;
1511
0
        while (!done) {
1512
0
            if (*temp_ptr == 0x0d || *temp_ptr == 0x0a) {
1513
0
                temp_ptr++;
1514
0
            } else {
1515
0
                done = 1;
1516
0
            }
1517
0
        }
1518
0
        curr_name_size = strlen(temp_ptr);
1519
0
        namedcolor_data[k].name_size = curr_name_size;
1520
1521
        /* +1 for the null */
1522
0
        namedcolor_data[k].colorant_name =
1523
0
            (char*)gs_malloc(mem, 1, curr_name_size + 1,
1524
0
                "create_named_profile");
1525
0
        if (namedcolor_data[k].colorant_name == NULL) {
1526
            /* Free up all that has been allocated so far */
1527
0
            for (j = 0; j < k; j++) {
1528
0
                gs_free(mem, namedcolor_data[j].colorant_name, 1, namedcolor_data[j].name_size+1,
1529
0
                    "create_named_profile");
1530
0
            }
1531
0
            gs_free(mem, namedcolor_data, num_entries, sizeof(gsicc_namedcolor_t),
1532
0
                "create_named_profile");
1533
0
            gs_free(mem, namedcolor_table, num_entries,
1534
0
                sizeof(gsicc_namedcolortable_t), "create_named_profile");
1535
0
            return_error(gs_error_VMerror);
1536
0
        }
1537
0
        strncpy(namedcolor_data[k].colorant_name, temp_ptr,
1538
0
            namedcolor_data[k].name_size + 1);
1539
0
        for (j = 0; j < 3; j++) {
1540
0
            pch = gs_strtok(NULL, ",;", &last);
1541
0
            if (pch == NULL)
1542
0
                return_error(gs_error_unknownerror);
1543
0
            count = sscanf(pch, "%f", &(lab[j]));
1544
0
        }
1545
0
        lab[0] = lab[0] * 65535 / 100.0;
1546
0
        lab[1] = (lab[1] + 128.0) * 65535 / 255;
1547
0
        lab[2] = (lab[2] + 128.0) * 65535 / 255;
1548
0
        for (j = 0; j < 3; j++) {
1549
0
            if (lab[j] > 65535) lab[j] = 65535;
1550
0
            if (lab[j] < 0) lab[j] = 0;
1551
0
            namedcolor_data[k].lab[j] = (unsigned short)lab[j];
1552
0
        }
1553
0
    }
1554
1555
    /* Assign to the profile pointer */
1556
0
    named_profile->profile_handle = namedcolor_table;
1557
0
    named_profile->release = gsicc_named_profile_release;
1558
0
    return 0;
1559
0
}
1560
1561
1562
/* Check for support of named color at time of install of DeviceN or Sep color space.
1563
   This function returning false means that Process colorants (e.g. CMYK) will
1564
   not undergo color management. */
1565
bool
1566
gsicc_support_named_color(const gs_color_space *pcs, const gs_gstate *pgs)
1567
37.3k
{
1568
37.3k
    cmm_profile_t *named_profile;
1569
37.3k
    gsicc_namedcolortable_t *namedcolor_table;
1570
37.3k
    unsigned int num_entries;
1571
37.3k
    int k, code, i, num_comp, num_spots=0;
1572
37.3k
    gs_color_space_index type = gs_color_space_get_index(pcs);
1573
37.3k
    char **names = NULL;
1574
37.3k
    byte *pname = NULL; /* Silence compiler warning */
1575
37.3k
    uint name_size = 0; /* Silence compiler warning */
1576
37.3k
    bool is_supported;
1577
37.3k
    bool none_colorant;
1578
1579
    /* Get the data for the named profile */
1580
37.3k
    named_profile = pgs->icc_manager->device_named;
1581
37.3k
    if (named_profile == NULL)
1582
37.3k
        return false;
1583
1584
0
    if (named_profile->buffer != NULL &&
1585
0
        named_profile->profile_handle == NULL) {
1586
0
        code = create_named_profile(pgs->memory->non_gc_memory, named_profile);
1587
0
        if (code < 0)
1588
0
            return false;
1589
0
    }
1590
0
    namedcolor_table =
1591
0
        (gsicc_namedcolortable_t*)named_profile->profile_handle;
1592
0
    num_entries = namedcolor_table->number_entries;
1593
1594
    /* Get the color space specifics */
1595
0
    if (type == gs_color_space_index_DeviceN) {
1596
0
        names = pcs->params.device_n.names;
1597
0
        num_comp = pcs->params.device_n.num_components;
1598
0
    } else if (type == gs_color_space_index_Separation) {
1599
0
        pname = (byte *)pcs->params.separation.sep_name;
1600
0
        name_size = strlen(pcs->params.separation.sep_name);
1601
0
        num_comp = 1;
1602
0
    } else
1603
0
        return false;
1604
1605
    /* Step through the color space colorants */
1606
0
    for (i = 0; i < num_comp; i++) {
1607
0
        if (type == gs_color_space_index_DeviceN) {
1608
0
            pname = (byte *)names[i];
1609
0
            name_size = strlen(names[i]);
1610
0
        }
1611
1612
0
        none_colorant = (strncmp((char*)pname, "None", name_size) == 0);
1613
1614
        /* Classify */
1615
0
        if (none_colorant ||
1616
0
            strncmp((char *)pname, "All", name_size) == 0) {
1617
            /* Do nothing */
1618
0
        } else {
1619
0
            if (strncmp((char *)pname, "Cyan", name_size) == 0 ||
1620
0
                strncmp((char *)pname, "Magenta", name_size) == 0 ||
1621
0
                strncmp((char *)pname, "Yellow", name_size) == 0 ||
1622
0
                strncmp((char *)pname, "Black", name_size) == 0) {
1623
                /* Do nothing */
1624
0
            } else {
1625
0
                num_spots++;
1626
0
            }
1627
0
        }
1628
1629
        /* Check if the colorant is supported */
1630
0
        is_supported = false;
1631
1632
        /* If we have a none colorant name in the DeviceN list, just ignore it */
1633
0
        if (none_colorant && type == gs_color_space_index_DeviceN)
1634
0
            is_supported = true;
1635
0
        else {
1636
0
            for (k = 0; k < num_entries; k++) {
1637
0
                if (name_size == namedcolor_table->named_color[k].name_size) {
1638
0
                    if (strncmp((const char*)namedcolor_table->named_color[k].colorant_name,
1639
0
                        (const char*)pname, name_size) == 0) {
1640
0
                        is_supported = true;
1641
0
                        break;
1642
0
                    }
1643
0
                }
1644
0
            }
1645
0
        }
1646
0
        if (!is_supported)
1647
0
            return false;
1648
0
    }
1649
    /* If we made it this far, all the individual colorants are supported.
1650
       If the names contained no spots, then let standard color management
1651
       processing occur.  It may be that some applications want standard
1652
       processing in other cases.  For example, [Cyan Magenta Varnish] to
1653
       a tiffsep-like device one may want color management to occur for the
1654
       Cyan and Magenta but Varnish to pass to the separation device unmolested.
1655
       You  will then want to add "Varnish" to the list of names in the above test
1656
       for the Process colorants of Cyan, Magenta, Yellow and Black to avoid
1657
       "Varnish" being counted as a spot here */
1658
0
    if (num_spots == 0)
1659
0
        return false;
1660
0
    return true;
1661
0
}
1662
1663
/* Function returns -1 if a name is not found.  Otherwise it will transform
1664
   the named colors and return 0 */
1665
int
1666
gsicc_transform_named_color(const float tint_values[],
1667
                            gsicc_namedcolor_t color_names[],
1668
                            uint num_names,
1669
                            gx_color_value device_values[],
1670
                            const gs_gstate *pgs, gx_device *dev,
1671
                            cmm_profile_t *gs_output_profile,
1672
                            gsicc_rendering_param_t *rendering_params)
1673
0
{
1674
0
    unsigned int num_entries;
1675
0
    cmm_profile_t *named_profile;
1676
0
    gsicc_namedcolortable_t *namedcolor_table;
1677
0
    int num_nonnone_names;
1678
0
    uint k,j,n;
1679
0
    int code;
1680
0
    bool found_match;
1681
0
    unsigned short psrc[GS_CLIENT_COLOR_MAX_COMPONENTS];
1682
0
    unsigned short psrc_cm[GS_CLIENT_COLOR_MAX_COMPONENTS];
1683
0
    unsigned short *psrc_temp;
1684
0
    unsigned short white_lab[3] = {65535, 32767, 32767};
1685
0
    gsicc_link_t *icc_link;
1686
0
    cmm_profile_t *curr_output_profile;
1687
0
    gsicc_rendering_param_t render_cond;
1688
0
    cmm_dev_profile_t *dev_profile;
1689
0
    int indices[GS_CLIENT_COLOR_MAX_COMPONENTS];
1690
0
    gs_memory_t *nongc_mem = pgs->memory->non_gc_memory;
1691
1692
    /* Set indices to avoid use of uninitialized index. It is actually not
1693
       possible to access them using real data but someone could perhaps
1694
       be malicious and cause a problem */
1695
0
    memset(&(indices[0]), 0, sizeof(indices));
1696
1697
    /* Check if the data that we have has already been generated. */
1698
0
    if (pgs->icc_manager != NULL) {
1699
0
        if (pgs->icc_manager->device_named != NULL) {
1700
0
            named_profile = pgs->icc_manager->device_named;
1701
0
            if (named_profile->buffer != NULL &&
1702
0
                named_profile->profile_handle == NULL) {
1703
0
                code = create_named_profile(nongc_mem, named_profile);
1704
0
                if (code < 0)
1705
0
                    return -1;
1706
0
            }
1707
0
            namedcolor_table =
1708
0
                    (gsicc_namedcolortable_t*)named_profile->profile_handle;
1709
0
            num_entries = namedcolor_table->number_entries;
1710
1711
            /* Go through each of our spot names, getting the color value for
1712
               each one. */
1713
0
            num_nonnone_names = num_names;
1714
0
            for (n = 0; n < num_names; n++) {
1715
                /* Search our structure for the color name.  Ignore the None
1716
                   colorant names.  All is a special case that someone may
1717
                   want to detect and do some special handling for.  In this
1718
                   particular example we would punt with All and let the default
1719
                   methods handle it */
1720
0
                found_match = false;
1721
1722
0
                if (strncmp("None", (const char *)color_names[n].colorant_name,
1723
0
                            color_names[n].name_size) == 0) {
1724
0
                    num_nonnone_names--;
1725
0
                } else {
1726
                    /* Colorant was not None */
1727
0
                    for (k = 0; k < num_entries; k++) {
1728
0
                        if (color_names[n].name_size ==
1729
0
                            namedcolor_table->named_color[k].name_size) {
1730
0
                            if (strncmp((const char *)namedcolor_table->named_color[k].colorant_name,
1731
0
                                (const char *)color_names[n].colorant_name, color_names[n].name_size) == 0) {
1732
0
                                found_match = true;
1733
0
                                break;
1734
0
                            }
1735
0
                        }
1736
0
                    }
1737
0
                    if (found_match) {
1738
0
                        indices[n] = k;
1739
0
                    } else {
1740
                        /* We do not know this colorant, return -1 */
1741
0
                        return -1;
1742
0
                    }
1743
0
                }
1744
0
            }
1745
0
            if (num_nonnone_names < 1)
1746
0
                return -1; /* No non-None colorants. */
1747
            /* We have all the colorants.  Lets go through and see if we can
1748
               make something that looks like a merge of the various ones */
1749
            /* Apply tint, blend LAB values.  Note that we may have wanted to
1750
               check if we even want to do this.  It is possible that the
1751
               device directly supports this particular colorant.  One may
1752
               want to check the alt tint transform boolean */
1753
1754
            /* Start with white */
1755
0
            for (j = 0; j < 3; j++) {
1756
0
                psrc[j] = white_lab[j];
1757
0
            }
1758
1759
0
            for (n = 0; n < num_nonnone_names; n++) {
1760
                /* Blend with the current color based upon current tint value */
1761
0
                for (j = 0; j < 3; j++) {
1762
0
                    psrc[j] = (unsigned short)
1763
0
                              ((float) namedcolor_table->named_color[indices[n]].lab[j] * tint_values[n]
1764
0
                             + (float) psrc[j] * (1.0 - tint_values[n]));
1765
0
                }
1766
0
            }
1767
1768
            /* Push LAB value through CMM to get CMYK device values */
1769
            /* Note that there are several options here. You could us an NCLR
1770
               icc profile to compute the device colors that you want.  For,
1771
               example if the output device had an NCLR profile.
1772
               However, what you MUST do here is set ALL the device values.
1773
               Hence, below we initialize all of them to zero and in this
1774
               particular example, set only the ones that were output from
1775
               the device profile */
1776
0
            if ( gs_output_profile != NULL ) {
1777
0
                curr_output_profile = gs_output_profile;
1778
0
            } else {
1779
                /* Use the device profile.  Note if one was not set for the
1780
                   device, the default CMYK profile is used.  Note that
1781
                   if we specified and NCLR profile it will be used here */
1782
0
                code = dev_proc(dev, get_profile)(dev,  &dev_profile);
1783
0
                gsicc_extract_profile(dev->graphics_type_tag,
1784
0
                                      dev_profile, &(curr_output_profile),
1785
0
                                      &render_cond);
1786
0
            }
1787
0
            icc_link = gsicc_get_link_profile(pgs, dev,
1788
0
                                            pgs->icc_manager->lab_profile,
1789
0
                                            curr_output_profile, rendering_params,
1790
0
                                            pgs->memory, false);
1791
0
            if (icc_link->is_identity) {
1792
0
                psrc_temp = &(psrc[0]);
1793
0
            } else {
1794
                /* Transform the color */
1795
0
                psrc_temp = &(psrc_cm[0]);
1796
0
                (icc_link->procs.map_color)(dev, icc_link, psrc, psrc_temp, 2);
1797
0
            }
1798
0
            gsicc_release_link(icc_link);
1799
1800
            /* Clear out ALL the color values */
1801
0
            for (k = 0; k < dev->color_info.num_components; k++){
1802
0
                device_values[k] = 0;
1803
0
            }
1804
            /* Set only the values that came from the profile.  By default
1805
               this would generally be just CMYK values.  For the equivalent
1806
               color computation case it certainly will be.  If someone
1807
               specified an NCLR profile it could be more.  Note that if an
1808
               NCLR profile is being used we will want to make sure the colorant
1809
               order is correct */
1810
0
            for (k = 0; k < curr_output_profile->num_comps; k++){
1811
0
                device_values[k] = psrc_temp[k];
1812
0
            }
1813
0
            return 0;
1814
0
        }
1815
0
    }
1816
0
    return -1; /* Color not found */
1817
0
}
1818
1819
/* Used by gs to notify the ICC manager that we are done with this link for now */
1820
/* This may release elements waiting on an icc_link_cache slot */
1821
void
1822
gsicc_release_link(gsicc_link_t *icclink)
1823
184M
{
1824
184M
    gsicc_link_cache_t *icc_link_cache;
1825
1826
184M
    if (icclink == NULL)
1827
2.39k
        return;
1828
1829
184M
    icc_link_cache = icclink->icc_link_cache;
1830
1831
184M
    gx_monitor_enter(icc_link_cache->lock);
1832
184M
    if_debug2m('^', icclink->memory, "[^]icclink "PRI_INTPTR" -- => %d\n",
1833
184M
               (intptr_t)icclink, icclink->ref_count - 1);
1834
    /* Decrement the reference count */
1835
184M
    if (--(icclink->ref_count) == 0) {
1836
1837
125M
        gsicc_link_t *curr, *prev;
1838
1839
        /* Find link in cache, and move it to the end of the list.  */
1840
        /* This way zero ref_count links are found LRU first  */
1841
125M
        curr = icc_link_cache->head;
1842
125M
        prev = NULL;
1843
125M
        while (curr != icclink) {
1844
0
            prev = curr;
1845
0
            curr = curr->next;
1846
0
        };
1847
125M
        if (prev == NULL) {
1848
            /* this link was the head */
1849
125M
            icc_link_cache->head = curr->next;
1850
125M
        } else {
1851
0
            prev->next = curr->next;    /* de-link this one */
1852
0
        }
1853
        /* Find the first zero-ref entry on the list */
1854
125M
        curr = icc_link_cache->head;
1855
125M
        prev = NULL;
1856
125M
        while (curr != NULL && curr->ref_count > 0) {
1857
18
            prev = curr;
1858
18
            curr = curr->next;
1859
18
        }
1860
        /* Found where to link this one into the tail of the list */
1861
125M
        if (prev == NULL) {
1862
125M
            icc_link_cache->head = icclink;
1863
125M
            icclink->next = icc_link_cache->head->next;
1864
125M
        } else {
1865
            /* link this one in here */
1866
18
            prev->next = icclink;
1867
18
            icclink->next = curr;
1868
18
        }
1869
        /* Finally, if some thread was waiting because the cache was full, let it run */
1870
125M
        if (icc_link_cache->cache_full) {
1871
0
            icc_link_cache->cache_full = false;
1872
0
            gx_semaphore_signal(icc_link_cache->full_wait);  /* let a waiting thread run */
1873
0
        }
1874
125M
    }
1875
184M
    gx_monitor_leave(icc_link_cache->lock);
1876
184M
}
1877
1878
/* Used to initialize the buffer description prior to color conversion */
1879
void
1880
gsicc_init_buffer(gsicc_bufferdesc_t *buffer_desc, unsigned char num_chan, unsigned char bytes_per_chan,
1881
                  bool has_alpha, bool alpha_first, bool is_planar, int plane_stride, int row_stride,
1882
                  int num_rows, int pixels_per_row)
1883
7.13M
{
1884
7.13M
    buffer_desc->num_chan = num_chan;
1885
7.13M
    buffer_desc->bytes_per_chan = bytes_per_chan;
1886
7.13M
    buffer_desc->has_alpha = has_alpha;
1887
7.13M
    buffer_desc->alpha_first = alpha_first;
1888
7.13M
    buffer_desc->is_planar = is_planar;
1889
7.13M
    buffer_desc->plane_stride = plane_stride;
1890
7.13M
    buffer_desc->row_stride = row_stride;
1891
7.13M
    buffer_desc->num_rows = num_rows;
1892
7.13M
    buffer_desc->pixels_per_row = pixels_per_row;
1893
7.13M
    buffer_desc->endian_swap = false;
1894
7.13M
}
1895
1896
/* Return the proper component numbers based upon the profiles of the device.
1897
   This is in here since it is usually called when creating and using a link
1898
   from the link cache. */
1899
int
1900
gsicc_get_device_profile_comps(const cmm_dev_profile_t *dev_profile)
1901
652M
{
1902
652M
    if (dev_profile->link_profile == NULL) {
1903
652M
       return dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE]->num_comps;
1904
652M
    } else {
1905
0
       return dev_profile->link_profile->num_comps_out;
1906
0
    }
1907
652M
}