Coverage Report

Created: 2025-08-28 07:06

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