Coverage Report

Created: 2026-04-09 07:06

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