Coverage Report

Created: 2025-06-24 07:01

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