Coverage Report

Created: 2025-11-16 07:40

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-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
619k
#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
252M
#define BP_SHIFT 0
90
252M
#define REND_SHIFT 8
91
252M
#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
4.08M
{
101
4.08M
    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
4.08M
    memory = memory->stable_memory;
106
4.08M
    result = gs_alloc_struct(memory, gsicc_link_cache_t, &st_icc_linkcache,
107
4.08M
                             "gsicc_cache_new");
108
4.08M
    if ( result == NULL )
109
0
        return(NULL);
110
4.08M
    result->head = NULL;
111
4.08M
    result->num_links = 0;
112
4.08M
    result->cache_full = false;
113
4.08M
    result->memory = memory;
114
4.08M
    result->full_wait = NULL; /* Required so finaliser can work when result freed. */
115
4.08M
    rc_init_free(result, memory, 1, rc_gsicc_link_cache_free);
116
4.08M
    result->lock = gx_monitor_label(gx_monitor_alloc(memory),
117
4.08M
                                    "gsicc_cache_new");
118
4.08M
    if (result->lock == NULL) {
119
0
        rc_decrement(result, "gsicc_cache_new");
120
0
        return(NULL);
121
0
    }
122
4.08M
    result->full_wait = gx_semaphore_label(gx_semaphore_alloc(memory),
123
4.08M
                                           "gsicc_cache_new");
124
4.08M
    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
4.08M
    if_debug2m(gs_debug_flag_icc, memory,
130
4.08M
               "[icc] Allocating link cache = "PRI_INTPTR" memory = "PRI_INTPTR"\n",
131
4.08M
         (intptr_t)result, (intptr_t)result->memory);
132
4.08M
    return(result);
133
4.08M
}
134
135
static void
136
rc_gsicc_link_cache_free(gs_memory_t * mem, void *ptr_in, client_name_t cname)
137
4.08M
{
138
    /* Ending the entire cache.  The ref counts on all the links should be 0 */
139
4.08M
    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
4.08M
    assert(link_cache != NULL && mem == link_cache->memory);
143
4.08M
    if (link_cache == NULL)
144
0
        return;
145
4.08M
    if_debug2m(gs_debug_flag_icc, link_cache->memory,
146
4.08M
               "[icc] Removing link cache = "PRI_INTPTR" memory = "PRI_INTPTR"\n",
147
4.08M
               (intptr_t)link_cache, (intptr_t)link_cache->memory);
148
    /* NB: freeing the link_cache will call icc_linkcache_finalize */
149
4.08M
    gs_free_object(link_cache->memory, link_cache, "rc_gsicc_link_cache_free");
150
4.08M
}
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
4.08M
{
156
4.08M
    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
4.08M
    assert(link_cache != NULL && mem == link_cache->memory);
160
4.08M
    if (link_cache == NULL)
161
0
        return;
162
4.51M
    while (link_cache->head != NULL) {
163
427k
        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
427k
        gsicc_remove_link(link_cache->head);
169
427k
    }
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
4.08M
    if (link_cache->rc.ref_count == 0) {
176
4.08M
        gx_monitor_free(link_cache->lock);
177
4.08M
        link_cache->lock = NULL;
178
4.08M
        gx_semaphore_free(link_cache->full_wait);
179
4.08M
        link_cache->full_wait = 0;
180
4.08M
    }
181
4.08M
}
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
619k
{
285
619k
    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
619k
    memory = memory->stable_memory;
290
619k
    result = gs_alloc_struct(memory, gsicc_link_t, &st_icc_link,
291
619k
                             "gsicc_alloc_link");
292
619k
    if (result == NULL)
293
0
        return NULL;
294
    /* set up placeholder values */
295
619k
    result->is_monitored = false;
296
619k
    result->orig_procs.map_buffer = NULL;
297
619k
    result->orig_procs.map_color = NULL;
298
619k
    result->orig_procs.free_link = NULL;
299
619k
    result->next = NULL;
300
619k
    result->link_handle = NULL;
301
619k
    result->procs.map_buffer = gscms_transform_color_buffer;
302
619k
    result->procs.map_color = gscms_transform_color;
303
619k
    result->procs.free_link = gscms_release_link;
304
619k
    result->hashcode.link_hashcode = hashcode.link_hashcode;
305
619k
    result->hashcode.des_hash = 0;
306
619k
    result->hashcode.src_hash = 0;
307
619k
    result->hashcode.rend_hash = 0;
308
619k
    result->ref_count = 1;    /* prevent it from being freed */
309
619k
    result->includes_softproof = 0;
310
619k
    result->includes_devlink = 0;
311
619k
    result->is_identity = false;
312
619k
    result->valid = false;   /* not yet complete */
313
619k
    result->memory = memory;
314
315
619k
    result->lock = gx_monitor_label(gx_monitor_alloc(memory),
316
619k
                                    "gsicc_link_new");
317
619k
    if (result->lock == NULL) {
318
0
        gs_free_object(memory, result, "gsicc_alloc_link(lock)");
319
0
        return NULL;
320
0
    }
321
619k
    gx_monitor_enter(result->lock);     /* this link is owned by this thread until built and made "valid" */
322
323
619k
    if_debug1m('^', result->memory, "[^]icclink "PRI_INTPTR" init = 1\n",
324
619k
               (intptr_t)result);
325
619k
    return result;
326
619k
}
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
427k
{
334
427k
    gx_monitor_enter(lock);   /* lock the cache while changing data */
335
427k
    icc_link->link_handle = link_handle;
336
427k
    gscms_get_link_dim(link_handle, &(icc_link->num_input), &(icc_link->num_output),
337
427k
        icc_link->memory);
338
427k
    icc_link->hashcode.link_hashcode = hashcode.link_hashcode;
339
427k
    icc_link->hashcode.des_hash = hashcode.des_hash;
340
427k
    icc_link->hashcode.src_hash = hashcode.src_hash;
341
427k
    icc_link->hashcode.rend_hash = hashcode.rend_hash;
342
427k
    icc_link->includes_softproof = includes_softproof;
343
427k
    icc_link->includes_devlink = includes_devlink;
344
427k
    if ( (hashcode.src_hash == hashcode.des_hash) &&
345
217k
          !includes_softproof && !includes_devlink) {
346
217k
        icc_link->is_identity = true;
347
217k
    } else {
348
209k
        icc_link->is_identity = false;
349
209k
    }
350
    /* Set up for monitoring */
351
427k
    icc_link->data_cs = data_cs;
352
427k
    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
427k
    icc_link->valid = true;
357
427k
    gx_monitor_leave(icc_link->lock);
358
427k
    gx_monitor_leave(lock); /* done with updating, let everyone run */
359
427k
}
360
361
static void
362
gsicc_link_free_contents(gsicc_link_t *icc_link)
363
1.23M
{
364
1.23M
    icc_link->procs.free_link(icc_link);
365
1.23M
    gx_monitor_free(icc_link->lock);
366
1.23M
    icc_link->lock = NULL;
367
1.23M
}
368
369
void
370
gsicc_link_free(gsicc_link_t *icc_link)
371
619k
{
372
619k
    if (icc_link == NULL)
373
0
        return;
374
619k
    gsicc_link_free_contents(icc_link);
375
376
619k
    gs_free_object(icc_link->memory, icc_link, "gsicc_link_free");
377
619k
}
378
379
void
380
icc_link_finalize(const gs_memory_t *mem, void *ptr)
381
619k
{
382
619k
    gsicc_link_t *icc_link = (gsicc_link_t * ) ptr;
383
384
619k
    gsicc_link_free_contents(icc_link);
385
619k
}
386
387
static void
388
gsicc_mash_hash(gsicc_hashlink_t *hash)
389
252M
{
390
252M
    hash->link_hashcode =
391
252M
        (hash->des_hash >> 1) ^ (hash->rend_hash) ^ (hash->src_hash);
392
252M
}
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
791k
{
410
791k
    if (!profile->hash_is_valid) {
411
6.67k
        int64_t hash;
412
413
6.67k
        gsicc_get_icc_buff_hash(profile->buffer, &hash, profile->buffer_size);
414
6.67k
        profile->hashcode = hash;
415
6.67k
        profile->hash_is_valid = true;
416
6.67k
    }
417
791k
    return profile->hashcode;
418
791k
}
419
420
bool
421
8.39M
gsicc_profiles_equal(cmm_profile_t *profile1, cmm_profile_t *profile2) {
422
423
8.39M
    if (profile1 == NULL || profile2 == NULL)
424
0
        return false;
425
426
8.39M
    if (!(profile1->hash_is_valid)) {
427
0
        gsicc_set_hash(profile1);
428
0
    }
429
430
8.39M
    if (!(profile1->hash_is_valid)) {
431
0
        gsicc_set_hash(profile2);
432
0
    }
433
434
8.39M
    return profile1->hashcode == profile2->hashcode;
435
8.39M
}
436
437
void
438
gsicc_get_icc_buff_hash(unsigned char *buffer, int64_t *hash, unsigned int buff_size)
439
903k
{
440
903k
    gsicc_get_buff_hash(buffer, hash, buff_size);
441
903k
}
442
443
static void
444
gsicc_get_buff_hash(unsigned char *data, int64_t *hash, unsigned int num_bytes)
445
903k
{
446
903k
    gs_md5_state_t md5;
447
903k
    byte digest[16];
448
903k
    int k;
449
903k
    uint64_t word1,word2,shift;
450
451
   /* We could probably do something faster than this. But use this for now. */
452
903k
    gs_md5_init(&md5);
453
903k
    gs_md5_append(&md5, data, num_bytes);
454
903k
    gs_md5_finish(&md5, digest);
455
456
    /* For now, xor this into 64 bit word */
457
903k
    word1 = 0;
458
903k
    word2 = 0;
459
903k
    shift = 0;
460
461
    /* need to do it this way because of
462
       potential word boundary issues */
463
8.13M
    for( k = 0; k<8; k++) {
464
7.22M
       word1 += ((uint64_t) digest[k]) << shift;
465
7.22M
       word2 += ((uint64_t) digest[k+8]) << shift;
466
7.22M
       shift += 8;
467
7.22M
    }
468
903k
    *hash = word1 ^ word2;
469
903k
}
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
252M
{
482
252M
    int code;
483
484
    /* first get the hash codes for the color spaces */
485
252M
    code = gsicc_get_cspace_hash(icc_manager, dev, input_profile,
486
252M
                                 &(hash->src_hash));
487
252M
    if (code < 0)
488
0
        return code;
489
252M
    code = gsicc_get_cspace_hash(icc_manager, dev, output_profile,
490
252M
                                 &(hash->des_hash));
491
252M
    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
252M
    hash->rend_hash = ((rendering_params->black_point_comp) << BP_SHIFT) +
502
252M
                      ((rendering_params->rendering_intent) << REND_SHIFT) +
503
252M
                      ((rendering_params->preserve_black) << PRESERVE_SHIFT);
504
    /* for now, mash all of these into a link hash */
505
252M
    gsicc_mash_hash(hash);
506
252M
    return 0;
507
252M
}
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
504M
{
513
504M
    cmm_dev_profile_t *dev_profile;
514
504M
    cmm_profile_t *icc_profile;
515
504M
    gsicc_rendering_param_t render_cond;
516
504M
    int code;
517
518
504M
    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
504M
    if (cmm_icc_profile_data->hash_is_valid ) {
531
503M
        *hash = cmm_icc_profile_data->hashcode;
532
503M
    } else {
533
        /* We need to compute for this color space */
534
357k
        gsicc_get_icc_buff_hash(cmm_icc_profile_data->buffer, hash,
535
357k
                                cmm_icc_profile_data->buffer_size);
536
357k
        cmm_icc_profile_data->hashcode = *hash;
537
357k
        cmm_icc_profile_data->hash_is_valid = true;
538
357k
    }
539
504M
    return 0;
540
504M
}
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
252M
{
546
252M
    gsicc_link_t *curr, *prev;
547
252M
    int64_t hashcode = hash.link_hashcode;
548
252M
    int cache_loop = 0;
549
550
    /* Look through the cache for the hashcode */
551
252M
    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
252M
    curr = icc_link_cache->head;
556
252M
    prev = NULL;
557
558
254M
    while (curr != NULL ) {
559
254M
        if (curr->hashcode.link_hashcode == hashcode &&
560
251M
            includes_proof == curr->includes_softproof &&
561
251M
            includes_devlink == curr->includes_devlink) {
562
            /* move this one to the front of the list hoping we will use it
563
               again soon */
564
251M
            if (prev != NULL) {
565
                /* if prev == NULL, curr is already the head */
566
1.55M
                prev->next = curr->next;
567
1.55M
                curr->next = icc_link_cache->head;
568
1.55M
                icc_link_cache->head = curr;
569
1.55M
            }
570
            /* bump the ref_count since we will be using this one */
571
251M
            curr->ref_count++;
572
251M
            if_debug3m('^', curr->memory, "[^]%s "PRI_INTPTR" ++ => %d\n",
573
251M
                       "icclink", (intptr_t)curr, curr->ref_count);
574
251M
            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
251M
            gx_monitor_leave(icc_link_cache->lock);
594
251M
            return curr; /* success */
595
251M
        }
596
2.52M
        prev = curr;
597
2.52M
        curr = curr->next;
598
2.52M
    }
599
619k
    gx_monitor_leave(icc_link_cache->lock);
600
619k
    return NULL;
601
252M
}
602
603
/* Remove link from cache.  Notify CMS and free */
604
static void
605
gsicc_remove_link(gsicc_link_t *link)
606
619k
{
607
619k
    gsicc_link_t *curr, *prev;
608
619k
    gsicc_link_cache_t *icc_link_cache = link->icc_link_cache;
609
610
619k
    if_debug2m(gs_debug_flag_icc, link->memory,
611
619k
               "[icc] Removing link = "PRI_INTPTR" memory = "PRI_INTPTR"\n",
612
619k
               (intptr_t)link, (intptr_t)link->memory);
613
    /* NOTE: link->ref_count must be 0: assert ? */
614
619k
    gx_monitor_enter(icc_link_cache->lock);
615
619k
    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
619k
    curr = icc_link_cache->head;
619
619k
    prev = NULL;
620
621
619k
    while (curr != NULL ) {
622
        /* don't get rid of it if another thread has decided to use it */
623
619k
        if (curr == link && link->ref_count == 0) {
624
            /* remove this one from the list */
625
619k
            if (prev == NULL)
626
619k
                icc_link_cache->head = curr->next;
627
0
            else
628
0
                prev->next = curr->next;
629
619k
            break;
630
619k
        }
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
619k
    if (curr == link && link->ref_count == 0) {
637
619k
        icc_link_cache->num_links--;  /* no longer in the cache */
638
619k
        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
619k
        gx_monitor_leave(icc_link_cache->lock);
643
619k
        gsicc_link_free(link);  /* outside link cache now. */
644
619k
    } 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
619k
}
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
250M
{
715
250M
    cmm_profile_t *gs_input_profile;
716
250M
    cmm_profile_t *gs_srcgtag_profile = NULL;
717
250M
    cmm_profile_t *gs_output_profile;
718
250M
    gsicc_rendering_param_t render_cond;
719
250M
    cmm_dev_profile_t *dev_profile;
720
250M
    int code;
721
250M
    bool devicegraytok;
722
250M
    gs_color_space *input_colorspace = (gs_color_space*) pcs_in;
723
724
250M
    if (dev == NULL) {
725
        /* This only occurs for the other (non-ps/pdf) interpreters */
726
121k
        dev = pgs->device;
727
121k
    }
728
250M
    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
250M
    } else {
737
250M
        gs_input_profile = input_colorspace->cmm_icc_profile_data;
738
250M
    }
739
250M
    code = dev_proc(dev, get_profile)(dev,  &dev_profile);
740
250M
    if (code < 0)
741
0
        return NULL;
742
    /* If present, use an graphic object defined source profile */
743
250M
    if (pgs->icc_manager != NULL &&
744
250M
        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
250M
    if (output_colorspace != NULL) {
823
0
        gs_output_profile = output_colorspace->cmm_icc_profile_data;
824
0
        devicegraytok = false;
825
250M
    } else {
826
        /* Check for unmanaged color case */
827
250M
        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
250M
        gsicc_extract_profile(dev->graphics_type_tag, dev_profile,
845
250M
                               &(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
250M
        if (!(rendering_params->rendering_intent & gsRI_OVERRIDE)) {
850
            /* No it was not.  Check if our device profile RI was specified */
851
250M
            if (render_cond.rendering_intent != gsRINOTSPECIFIED) {
852
0
                rendering_params->rendering_intent = render_cond.rendering_intent;
853
0
            }
854
250M
        }
855
        /* Similar for the black point compensation */
856
250M
        if (!(rendering_params->black_point_comp & gsBP_OVERRIDE)) {
857
250M
            if (render_cond.black_point_comp != gsBPNOTSPECIFIED) {
858
0
                rendering_params->black_point_comp = render_cond.black_point_comp;
859
0
            }
860
250M
        }
861
        /* And the Black preservation */
862
250M
        if (!(rendering_params->preserve_black & gsKP_OVERRIDE)) {
863
250M
            if (render_cond.preserve_black != gsBKPRESNOTSPECIFIED) {
864
0
                rendering_params->preserve_black = render_cond.preserve_black;
865
0
            }
866
250M
        }
867
250M
        devicegraytok = dev_profile->devicegraytok;
868
250M
    }
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
250M
    rendering_params->rendering_intent = rendering_params->rendering_intent & gsRI_MASK;
873
250M
    rendering_params->black_point_comp = rendering_params->black_point_comp & gsBP_MASK;
874
250M
    rendering_params->preserve_black = rendering_params->preserve_black & gsKP_MASK;
875
250M
    return gsicc_get_link_profile(pgs, dev, gs_input_profile, gs_output_profile,
876
250M
                    rendering_params, memory, devicegraytok);
877
250M
}
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
619k
{
890
619k
    gs_memory_t *cache_mem = icc_link_cache->memory;
891
619k
    gsicc_link_t *link;
892
619k
    int retries = 0;
893
894
619k
    assert(cache_mem == cache_mem->stable_memory);
895
896
619k
    *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
619k
    gx_monitor_enter(icc_link_cache->lock);
900
619k
    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
619k
    (*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
619k
    if (*ret_link) {
952
619k
        (*ret_link)->icc_link_cache = icc_link_cache;
953
619k
        (*ret_link)->next = icc_link_cache->head;
954
619k
        icc_link_cache->head = *ret_link;
955
619k
        icc_link_cache->num_links++;
956
619k
    }
957
    /* unlock before returning */
958
619k
    gx_monitor_leave(icc_link_cache->lock);
959
619k
    return false; /* we didn't find it, but return a link to be filled */
960
619k
}
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
252M
{
976
252M
    gsicc_hashlink_t hash;
977
252M
    gsicc_link_t *link, *found_link;
978
252M
    gcmmhlink_t link_handle = NULL;
979
252M
    gsicc_manager_t *icc_manager = pgs->icc_manager;
980
252M
    gsicc_link_cache_t *icc_link_cache = pgs->icc_link_cache;
981
252M
    gs_memory_t *cache_mem = pgs->icc_link_cache->memory;
982
252M
    gcmmhprofile_t *cms_input_profile = NULL;
983
252M
    gcmmhprofile_t *cms_output_profile = NULL;
984
252M
    gcmmhprofile_t *cms_proof_profile = NULL;
985
252M
    gcmmhprofile_t *cms_devlink_profile = NULL;
986
252M
    int code;
987
252M
    bool include_softproof = false;
988
252M
    bool include_devicelink = false;
989
252M
    cmm_dev_profile_t *dev_profile;
990
252M
    cmm_profile_t *proof_profile = NULL;
991
252M
    cmm_profile_t *devlink_profile = NULL;
992
252M
    bool src_dev_link = gs_input_profile->isdevlink;
993
252M
    bool pageneutralcolor = false;
994
252M
    int cms_flags = 0;
995
996
    /* Determine if we are using a soft proof or device link profile */
997
252M
    if (dev != NULL ) {
998
252M
        code = dev_proc(dev, get_profile)(dev,  &dev_profile);
999
252M
        if (code < 0)
1000
0
            return NULL;
1001
252M
        if (dev_profile != NULL) {
1002
252M
            proof_profile = dev_profile->proof_profile;
1003
252M
            devlink_profile = dev_profile->link_profile;
1004
252M
            pageneutralcolor = dev_profile->pageneutralcolor;
1005
252M
        }
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
252M
        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
252M
        if (devlink_profile != NULL) include_devicelink = true;
1018
252M
    }
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
252M
    code = gsicc_compute_linkhash(icc_manager, dev, gs_input_profile,
1022
252M
                                  gs_output_profile,
1023
252M
                                  rendering_params, &hash);
1024
252M
    if (code < 0)
1025
0
        return NULL;
1026
    /* Check the cache for a hit.  Need to check if softproofing was used */
1027
252M
    found_link = gsicc_findcachelink(hash, icc_link_cache, include_softproof,
1028
252M
                                     include_devicelink);
1029
    /* Got a hit, return link (ref_count for the link was already bumped */
1030
252M
    if (found_link != NULL) {
1031
251M
        if_debug2m(gs_debug_flag_icc, memory,
1032
251M
                   "[icc] Found Link = "PRI_INTPTR", hash = %lld \n",
1033
251M
                   (intptr_t)found_link, (long long)hash.link_hashcode);
1034
251M
        if_debug2m(gs_debug_flag_icc, memory,
1035
251M
                   "[icc] input_numcomps = %d, input_hash = %lld \n",
1036
251M
                   gs_input_profile->num_comps,
1037
251M
                   (long long)gs_input_profile->hashcode);
1038
251M
        if_debug2m(gs_debug_flag_icc, memory,
1039
251M
                   "[icc] output_numcomps = %d, output_hash = %lld \n",
1040
251M
                   gs_output_profile->num_comps,
1041
251M
                   (long long)gs_output_profile->hashcode);
1042
251M
        return found_link;
1043
251M
    }
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
619k
    if (gs_input_profile->profile_handle == NULL &&
1048
378k
        gs_input_profile->buffer == NULL &&
1049
14.9k
        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
14.9k
        cms_input_profile =
1055
14.9k
            gsicc_get_profile_handle_clist(gs_input_profile,
1056
14.9k
                                           gs_input_profile->memory);
1057
14.9k
        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
14.9k
        if (gs_input_profile->rend_is_valid &&
1066
6.20k
            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
14.9k
        } else if (gs_input_profile->rend_is_valid &&
1079
6.20k
            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
14.9k
        src_dev_link = gs_input_profile->isdevlink;
1088
14.9k
    }
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
619k
    if (gsicc_alloc_link_entry(icc_link_cache, &link, hash, include_softproof,
1093
619k
                               include_devicelink))
1094
0
        return link;
1095
619k
    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
619k
    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
619k
    if (gsicc_profile_from_ps(gs_input_profile)) {
1109
0
        cms_flags = cms_flags | gscms_avoid_white_fix_flag(memory);
1110
0
    }
1111
619k
    if (cms_input_profile == NULL) {
1112
363k
        if (gs_input_profile->buffer != NULL) {
1113
363k
            cms_input_profile =
1114
363k
                gsicc_get_profile_handle_buffer(gs_input_profile->buffer,
1115
363k
                                                gs_input_profile->buffer_size,
1116
363k
                                                memory);
1117
363k
            if (cms_input_profile == NULL) {
1118
0
                link->ref_count--;
1119
0
                goto icc_link_error;
1120
0
            }
1121
1122
363k
            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
363k
            code = gsicc_initialize_default_profile(gs_input_profile);
1127
363k
            if (code < 0) {
1128
0
                link->ref_count--;
1129
0
                goto icc_link_error;
1130
0
            }
1131
363k
        } 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
363k
    }
1138
    /* No need to worry about an output profile handle if our source is a
1139
       device link profile */
1140
619k
    if (!src_dev_link) {
1141
619k
        cms_output_profile = gs_output_profile->profile_handle;
1142
619k
    }
1143
619k
    if (cms_output_profile == NULL && !src_dev_link) {
1144
4.70k
        if (gs_output_profile->buffer != NULL) {
1145
755
            cms_output_profile =
1146
755
                gsicc_get_profile_handle_buffer(gs_output_profile->buffer,
1147
755
                                                gs_output_profile->buffer_size,
1148
755
                                                memory);
1149
755
            gs_output_profile->profile_handle = cms_output_profile;
1150
            /* This *must* be a default profile that was not set up at start-up */
1151
755
            code = gsicc_initialize_default_profile(gs_output_profile);
1152
755
            if (code < 0)  {
1153
0
                link->ref_count--;
1154
0
                goto icc_link_error;
1155
0
            }
1156
3.95k
        } else {
1157
              /* See if we have a clist device pointer. */
1158
3.95k
            if ( gs_output_profile->dev != NULL ) {
1159
                /* ICC profile should be in clist. This is
1160
                   the first call to it. */
1161
3.95k
                cms_output_profile =
1162
3.95k
                    gsicc_get_profile_handle_clist(gs_output_profile,
1163
3.95k
                                                   gs_output_profile->memory);
1164
3.95k
                gs_output_profile->profile_handle = cms_output_profile;
1165
3.95k
            } 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.95k
        }
1172
4.70k
    }
1173
619k
    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
619k
    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
619k
    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
619k
    if (!src_dev_link && gs_output_profile->data_cs == gsCMYK &&
1223
51.6k
        gs_input_profile->data_cs == gsGRAY &&
1224
30.2k
        gs_input_profile->default_match == DEFAULT_GRAY &&
1225
27.1k
        pgs->icc_manager != NULL && devicegraytok) {
1226
27.1k
        if (icc_manager->graytok_profile == NULL) {
1227
26.5k
            assert(pgs->icc_manager->memory == pgs->icc_manager->memory->stable_memory);
1228
26.5k
            icc_manager->graytok_profile =
1229
26.5k
                gsicc_set_iccsmaskprofile(GRAY_TO_K, strlen(GRAY_TO_K),
1230
26.5k
                                          pgs->icc_manager,
1231
26.5k
                                          pgs->icc_manager->memory);
1232
26.5k
            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
26.5k
        }
1238
27.1k
        if (icc_manager->smask_profiles == NULL) {
1239
26.5k
            code = gsicc_initialize_iccsmask(icc_manager);
1240
26.5k
        }
1241
27.1k
        cms_input_profile =
1242
27.1k
            icc_manager->smask_profiles->smask_gray->profile_handle;
1243
27.1k
        cms_output_profile =
1244
27.1k
            icc_manager->graytok_profile->profile_handle;
1245
        /* Turn off bp compensation in this case as there is a bug in lcms */
1246
27.1k
        rendering_params->black_point_comp = false;
1247
27.1k
        cms_flags = 0;  /* Turn off any flag setting */
1248
27.1k
    }
1249
    /* Get the link with the proof and or device link profile */
1250
619k
    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
619k
    } else {
1267
619k
        link_handle = gscms_get_link(cms_input_profile, cms_output_profile,
1268
619k
                                     rendering_params, cms_flags,
1269
619k
                                     cache_mem->non_gc_memory);
1270
619k
    }
1271
619k
    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
619k
    if (link_handle != NULL) {
1278
427k
        if (gs_input_profile->data_cs == gsGRAY)
1279
337k
            pageneutralcolor = false;
1280
1281
427k
        gsicc_set_link_data(link, link_handle, hash, icc_link_cache->lock,
1282
427k
                            include_softproof, include_devicelink, pageneutralcolor,
1283
427k
                            gs_input_profile->data_cs);
1284
427k
        if_debug2m(gs_debug_flag_icc, cache_mem,
1285
427k
                   "[icc] New Link = "PRI_INTPTR", hash = %lld \n",
1286
427k
                   (intptr_t)link, (long long)hash.link_hashcode);
1287
427k
        if_debug2m(gs_debug_flag_icc, cache_mem,
1288
427k
                   "[icc] input_numcomps = %d, input_hash = %lld \n",
1289
427k
                   gs_input_profile->num_comps,
1290
427k
                   (long long)gs_input_profile->hashcode);
1291
427k
        if_debug2m(gs_debug_flag_icc, cache_mem,
1292
427k
                   "[icc] output_numcomps = %d, output_hash = %lld \n",
1293
427k
                   gs_output_profile->num_comps,
1294
427k
                   (long long)gs_output_profile->hashcode);
1295
427k
    } 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
192k
        link->ref_count--;  /* this thread no longer using this link entry  */
1300
192k
        if_debug2m('^', link->memory, "[^]icclink "PRI_INTPTR" -- => %d\n",
1301
192k
                   (intptr_t)link, link->ref_count);
1302
1303
192k
        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
192k
        gx_monitor_leave(link->lock);
1308
192k
        goto icc_link_error;
1309
192k
    }
1310
427k
    return link;
1311
1312
192k
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
192k
    gsicc_remove_link(link);
1318
1319
192k
    return NULL;
1320
619k
}
1321
1322
/* The following is used to transform a named color value at a particular tint
1323
   value to the output device values.  This function is provided only as a
1324
   demonstration and will likely need to be altered and optimized for those wishing
1325
   to perform full spot color look-up support.
1326
1327
   The object used to perform the transformation is typically
1328
   a look-up table that contains the spot color name and a CIELAB value for
1329
   100% colorant (it could also contain device values in the table).
1330
   It can be more complex where-by you have a 1-D lut that
1331
   provides CIELAB values or direct device values as a function of tint.  In
1332
   such a case, the table would be interpolated to compute all possible tint values.
1333
   If CIELAB values are provided, they can be pushed through the
1334
   device profile using the CMM.  In this particular demonstration, we simply
1335
   provide CIELAB for a few color names in the file
1336
   toolbin/color/named_color/named_color_table.txt .
1337
   The tint value is used to scale the CIELAB value from 100% colorant to a D50
1338
   whitepoint.  The resulting CIELAB value is then pushed through the CMM to
1339
   obtain device values for the current device.  The file named_colors.pdf
1340
   which is in toolbin/color/named_color/ contains these
1341
   spot colors and will enable the user to see how the code behaves.  The named
1342
   color table is specified to ghostscript by the command line option
1343
   -sNamedProfile=./toolbin/color/named_color/named_color_table.txt (or with
1344
   full path name).  If it is desired to have ghostscript compiled with the
1345
   named color table, it can be placed in the iccprofiles directory and then
1346
   build ghostscript with COMPILE_INITS=1.  When specified the file contents
1347
   are pointed to by the buffer member variable of the device_named profile in
1348
   profile manager.  When the first call occurs in here, the contents of the
1349
   buffer are parsed and placed into a custom stucture that is pointed to by
1350
   the profile pointer.  Note that this pointer is not visible to the garbage
1351
   collector and should be allocated in non-gc memory as is demonstrated in
1352
   this sample.  The structure elements are released when the profile is
1353
   destroyed through the call to gsicc_named_profile_release, which is set
1354
   as the value of the profile member variable release.
1355
1356
   Note that there are calls defined in gsicc_littlecms.c that will create link
1357
   transforms between Named Color ICC profiles and the output device.  Such
1358
   profiles are rarely used (at least I have not run across any yet) so the
1359
   code is currently not used.  Also note that for those serious about named
1360
   color support, a cache as well as efficient table-look-up methods would
1361
   likely be important for performance.
1362
1363
   Finally note that PANTONE is a registered trademark and PANTONE colors are a
1364
   licensed product of XRITE Inc. See http://www.pantone.com
1365
   for more information.  Licensees of Pantone color libraries or similar
1366
   libraries should find it straight forward to interface.  Pantone names are
1367
   referred to in named_color_table.txt and contained in the file named_colors.pdf.
1368
1369
   !!!!IT WILL BE NECESSARY TO PERFORM THE PROPER DEALLOCATION
1370
       CLEAN-UP OF THE STRUCTURES WHEN rc_free_icc_profile OCCURS FOR THE NAMED
1371
       COLOR PROFILE!!!!!!   See gsicc_named_profile_release below for an example.
1372
       This is set in the profile release member variable.
1373
*/
1374
1375
/* Define the demo structure and function for named color look-up */
1376
1377
typedef struct gsicc_namedcolortable_s {
1378
    gsicc_namedcolor_t *named_color;    /* The named color */
1379
    unsigned int number_entries;        /* The number of entries */
1380
    gs_memory_t *memory;
1381
} gsicc_namedcolortable_t;
1382
1383
/* Support functions for parsing buffer */
1384
1385
static int
1386
get_to_next_line(char **buffptr, int *buffer_count)
1387
0
{
1388
0
    while (1) {
1389
0
        if (**buffptr == ';') {
1390
0
            (*buffptr)++;
1391
0
            (*buffer_count)--;
1392
0
            return(0);
1393
0
        } else {
1394
0
            (*buffptr)++;
1395
0
            (*buffer_count)--;
1396
0
        }
1397
0
        if (*buffer_count <= 0) {
1398
0
            return -1;
1399
0
        }
1400
0
    }
1401
0
}
1402
1403
/* Release the structures we allocated in gsicc_transform_named_color */
1404
static void
1405
gsicc_named_profile_release(void *ptr, gs_memory_t *memory)
1406
0
{
1407
0
    gsicc_namedcolortable_t *namedcolor_table = (gsicc_namedcolortable_t*) ptr;
1408
0
    unsigned int num_entries;
1409
0
    gs_memory_t *mem;
1410
0
    int k;
1411
0
    gsicc_namedcolor_t *namedcolor_data;
1412
1413
0
    if (namedcolor_table != NULL) {
1414
0
        mem = namedcolor_table->memory;
1415
0
        num_entries = namedcolor_table->number_entries;
1416
0
        namedcolor_data = namedcolor_table->named_color;
1417
1418
0
        for (k = 0; k < num_entries; k++) {
1419
0
            gs_free(mem, namedcolor_data[k].colorant_name, 1,
1420
0
                namedcolor_data[k].name_size + 1,
1421
0
                "gsicc_named_profile_release (colorant_name)");
1422
0
        }
1423
1424
0
        gs_free(mem, namedcolor_data, num_entries, sizeof(gsicc_namedcolor_t),
1425
0
            "gsicc_named_profile_release (namedcolor_data)");
1426
1427
0
        gs_free(namedcolor_table->memory, namedcolor_table, 1,
1428
0
            sizeof(gsicc_namedcolortable_t),
1429
0
            "gsicc_named_profile_release (namedcolor_table)");
1430
0
    }
1431
0
}
1432
1433
static int
1434
create_named_profile(gs_memory_t *mem, cmm_profile_t *named_profile)
1435
0
{
1436
    /* Create the structure that we will use in searching */
1437
    /*  Note that we do this in non-GC memory since the
1438
        profile pointer is not GC'd */
1439
0
    gsicc_namedcolortable_t *namedcolor_table;
1440
0
    gsicc_namedcolor_t *namedcolor_data;
1441
0
    char *buffptr;
1442
0
    int buffer_count;
1443
0
    int count;
1444
0
    unsigned int num_entries;
1445
0
    int code;
1446
0
    int k, j;
1447
0
    char *pch, *temp_ptr, *last = NULL;
1448
0
    bool done;
1449
0
    int curr_name_size;
1450
0
    float lab[3];
1451
1452
0
    namedcolor_table =
1453
0
        (gsicc_namedcolortable_t*)gs_malloc(mem, 1,
1454
0
            sizeof(gsicc_namedcolortable_t), "create_named_profile");
1455
0
    if (namedcolor_table == NULL)
1456
0
        return_error(gs_error_VMerror);
1457
0
    namedcolor_table->memory = mem;
1458
1459
    /* Parse buffer and load the structure we will be searching */
1460
0
    buffptr = (char*)named_profile->buffer;
1461
0
    buffer_count = named_profile->buffer_size;
1462
0
    count = sscanf(buffptr, "%ud", &num_entries);
1463
0
    if (num_entries < 1 || count <= 0) {
1464
0
        gs_free(mem, namedcolor_table, 1, sizeof(gsicc_namedcolortable_t),
1465
0
            "create_named_profile");
1466
0
        return -1;
1467
0
    }
1468
1469
0
    code = get_to_next_line(&buffptr, &buffer_count);
1470
0
    if (code < 0) {
1471
0
        gs_free(mem, namedcolor_table, 1, sizeof(gsicc_namedcolortable_t),
1472
0
            "create_named_profile");
1473
0
        return -1;
1474
0
    }
1475
0
    namedcolor_data =
1476
0
        (gsicc_namedcolor_t*)gs_malloc(mem, num_entries,
1477
0
            sizeof(gsicc_namedcolor_t), "create_named_profile");
1478
0
    if (namedcolor_data == NULL) {
1479
0
        gs_free(mem, namedcolor_table, num_entries,
1480
0
            sizeof(gsicc_namedcolortable_t), "create_named_profile");
1481
0
        return_error(gs_error_VMerror);
1482
0
    }
1483
0
    namedcolor_table->number_entries = num_entries;
1484
0
    namedcolor_table->named_color = namedcolor_data;
1485
0
    for (k = 0; k < num_entries; k++) {
1486
0
        if (k == 0) {
1487
0
            pch = gs_strtok(buffptr, ",;", &last);
1488
0
        } else {
1489
0
            pch = gs_strtok(NULL, ",;", &last);
1490
0
        }
1491
        /* Remove any /0d /0a stuff from start */
1492
0
        temp_ptr = pch;
1493
0
        if (pch == NULL)
1494
0
            return_error(gs_error_unknownerror);
1495
0
        done = 0;
1496
0
        while (!done) {
1497
0
            if (*temp_ptr == 0x0d || *temp_ptr == 0x0a) {
1498
0
                temp_ptr++;
1499
0
            } else {
1500
0
                done = 1;
1501
0
            }
1502
0
        }
1503
0
        curr_name_size = strlen(temp_ptr);
1504
0
        namedcolor_data[k].name_size = curr_name_size;
1505
1506
        /* +1 for the null */
1507
0
        namedcolor_data[k].colorant_name =
1508
0
            (char*)gs_malloc(mem, 1, curr_name_size + 1,
1509
0
                "create_named_profile");
1510
0
        if (namedcolor_data[k].colorant_name == NULL) {
1511
            /* Free up all that has been allocated so far */
1512
0
            for (j = 0; j < k; j++) {
1513
0
                gs_free(mem, namedcolor_table, 1, namedcolor_data[j].name_size+1,
1514
0
                    "create_named_profile");
1515
0
            }
1516
0
            gs_free(mem, namedcolor_data, num_entries, sizeof(gsicc_namedcolor_t),
1517
0
                "create_named_profile");
1518
0
            gs_free(mem, namedcolor_table, num_entries,
1519
0
                sizeof(gsicc_namedcolortable_t), "create_named_profile");
1520
0
            return_error(gs_error_VMerror);
1521
0
        }
1522
0
        strncpy(namedcolor_data[k].colorant_name, temp_ptr,
1523
0
            namedcolor_data[k].name_size + 1);
1524
0
        for (j = 0; j < 3; j++) {
1525
0
            pch = gs_strtok(NULL, ",;", &last);
1526
0
            if (pch == NULL)
1527
0
                return_error(gs_error_unknownerror);
1528
0
            count = sscanf(pch, "%f", &(lab[j]));
1529
0
        }
1530
0
        lab[0] = lab[0] * 65535 / 100.0;
1531
0
        lab[1] = (lab[1] + 128.0) * 65535 / 255;
1532
0
        lab[2] = (lab[2] + 128.0) * 65535 / 255;
1533
0
        for (j = 0; j < 3; j++) {
1534
0
            if (lab[j] > 65535) lab[j] = 65535;
1535
0
            if (lab[j] < 0) lab[j] = 0;
1536
0
            namedcolor_data[k].lab[j] = (unsigned short)lab[j];
1537
0
        }
1538
0
    }
1539
1540
    /* Assign to the profile pointer */
1541
0
    named_profile->profile_handle = namedcolor_table;
1542
0
    named_profile->release = gsicc_named_profile_release;
1543
0
    return 0;
1544
0
}
1545
1546
1547
/* Check for support of named color at time of install of DeviceN or Sep color space.
1548
   This function returning false means that Process colorants (e.g. CMYK) will
1549
   not undergo color management. */
1550
bool
1551
gsicc_support_named_color(const gs_color_space *pcs, const gs_gstate *pgs)
1552
103k
{
1553
103k
    cmm_profile_t *named_profile;
1554
103k
    gsicc_namedcolortable_t *namedcolor_table;
1555
103k
    unsigned int num_entries;
1556
103k
    int k, code, i, num_comp, num_spots=0, num_process=0, num_other=0;
1557
103k
    gs_color_space_index type = gs_color_space_get_index(pcs);
1558
103k
    char **names = NULL;
1559
103k
    byte *pname = NULL; /* Silence compiler warning */
1560
103k
    uint name_size = 0; /* Silence compiler warning */
1561
103k
    bool is_supported;
1562
103k
    bool none_colorant;
1563
1564
    /* Get the data for the named profile */
1565
103k
    named_profile = pgs->icc_manager->device_named;
1566
103k
    if (named_profile == NULL)
1567
103k
        return false;
1568
1569
0
    if (named_profile->buffer != NULL &&
1570
0
        named_profile->profile_handle == NULL) {
1571
0
        code = create_named_profile(pgs->memory->non_gc_memory, named_profile);
1572
0
        if (code < 0)
1573
0
            return false;
1574
0
    }
1575
0
    namedcolor_table =
1576
0
        (gsicc_namedcolortable_t*)named_profile->profile_handle;
1577
0
    num_entries = namedcolor_table->number_entries;
1578
1579
    /* Get the color space specifics */
1580
0
    if (type == gs_color_space_index_DeviceN) {
1581
0
        names = pcs->params.device_n.names;
1582
0
        num_comp = pcs->params.device_n.num_components;
1583
0
    } else if (type == gs_color_space_index_Separation) {
1584
0
        pname = (byte *)pcs->params.separation.sep_name;
1585
0
        name_size = strlen(pcs->params.separation.sep_name);
1586
0
        num_comp = 1;
1587
0
    } else
1588
0
        return false;
1589
1590
    /* Step through the color space colorants */
1591
0
    for (i = 0; i < num_comp; i++) {
1592
0
        if (type == gs_color_space_index_DeviceN) {
1593
0
            pname = (byte *)names[i];
1594
0
            name_size = strlen(names[i]);
1595
0
        }
1596
1597
0
        none_colorant = (strncmp((char*)pname, "None", name_size) == 0);
1598
1599
        /* Classify */
1600
0
        if (none_colorant ||
1601
0
            strncmp((char *)pname, "All", name_size) == 0) {
1602
0
            num_other++;
1603
0
        } else {
1604
0
            if (strncmp((char *)pname, "Cyan", name_size) == 0 ||
1605
0
                strncmp((char *)pname, "Magenta", name_size) == 0 ||
1606
0
                strncmp((char *)pname, "Yellow", name_size) == 0 ||
1607
0
                strncmp((char *)pname, "Black", name_size) == 0) {
1608
0
                num_process++;
1609
0
            } else {
1610
0
                num_spots++;
1611
0
            }
1612
0
        }
1613
1614
        /* Check if the colorant is supported */
1615
0
        is_supported = false;
1616
1617
        /* If we have a none colorant name in the DeviceN list, just ignore it */
1618
0
        if (none_colorant && type == gs_color_space_index_DeviceN)
1619
0
            is_supported = true;
1620
0
        else {
1621
0
            for (k = 0; k < num_entries; k++) {
1622
0
                if (name_size == namedcolor_table->named_color[k].name_size) {
1623
0
                    if (strncmp((const char*)namedcolor_table->named_color[k].colorant_name,
1624
0
                        (const char*)pname, name_size) == 0) {
1625
0
                        is_supported = true;
1626
0
                        break;
1627
0
                    }
1628
0
                }
1629
0
            }
1630
0
        }
1631
0
        if (!is_supported)
1632
0
            return false;
1633
0
    }
1634
    /* If we made it this far, all the individual colorants are supported.
1635
       If the names contained no spots, then let standard color management
1636
       processing occur.  It may be that some applications want standard
1637
       processing in other cases.  For example, [Cyan Magenta Varnish] to
1638
       a tiffsep-like device one may want color management to occur for the
1639
       Cyan and Magenta but Varnish to pass to the separation device unmolested.
1640
       You  will then want to add "Varnish" to the list of names in the above test
1641
       for the Process colorants of Cyan, Magenta, Yellow and Black to avoid
1642
       "Varnish" being counted as a spot here */
1643
0
    if (num_spots == 0)
1644
0
        return false;
1645
0
    return true;
1646
0
}
1647
1648
/* Function returns -1 if a name is not found.  Otherwise it will transform
1649
   the named colors and return 0 */
1650
int
1651
gsicc_transform_named_color(const float tint_values[],
1652
                            gsicc_namedcolor_t color_names[],
1653
                            uint num_names,
1654
                            gx_color_value device_values[],
1655
                            const gs_gstate *pgs, gx_device *dev,
1656
                            cmm_profile_t *gs_output_profile,
1657
                            gsicc_rendering_param_t *rendering_params)
1658
0
{
1659
0
    unsigned int num_entries;
1660
0
    cmm_profile_t *named_profile;
1661
0
    gsicc_namedcolortable_t *namedcolor_table;
1662
0
    int num_nonnone_names;
1663
0
    uint k,j,n;
1664
0
    int code;
1665
0
    bool found_match;
1666
0
    unsigned short psrc[GS_CLIENT_COLOR_MAX_COMPONENTS];
1667
0
    unsigned short psrc_cm[GS_CLIENT_COLOR_MAX_COMPONENTS];
1668
0
    unsigned short *psrc_temp;
1669
0
    unsigned short white_lab[3] = {65535, 32767, 32767};
1670
0
    gsicc_link_t *icc_link;
1671
0
    cmm_profile_t *curr_output_profile;
1672
0
    gsicc_rendering_param_t render_cond;
1673
0
    cmm_dev_profile_t *dev_profile;
1674
0
    int indices[GS_CLIENT_COLOR_MAX_COMPONENTS];
1675
0
    gs_memory_t *nongc_mem = pgs->memory->non_gc_memory;
1676
1677
    /* Set indices to avoid use of uninitialized index. It is actually not
1678
       possible to access them using real data but someone could perhaps
1679
       be malicious and cause a problem */
1680
0
    memset(&(indices[0]), 0, sizeof(indices));
1681
1682
    /* Check if the data that we have has already been generated. */
1683
0
    if (pgs->icc_manager != NULL) {
1684
0
        if (pgs->icc_manager->device_named != NULL) {
1685
0
            named_profile = pgs->icc_manager->device_named;
1686
0
            if (named_profile->buffer != NULL &&
1687
0
                named_profile->profile_handle == NULL) {
1688
0
                code = create_named_profile(nongc_mem, named_profile);
1689
0
                if (code < 0)
1690
0
                    return -1;
1691
0
            }
1692
0
            namedcolor_table =
1693
0
                    (gsicc_namedcolortable_t*)named_profile->profile_handle;
1694
0
            num_entries = namedcolor_table->number_entries;
1695
1696
            /* Go through each of our spot names, getting the color value for
1697
               each one. */
1698
0
            num_nonnone_names = num_names;
1699
0
            for (n = 0; n < num_names; n++) {
1700
                /* Search our structure for the color name.  Ignore the None
1701
                   colorant names.  All is a special case that someone may
1702
                   want to detect and do some special handling for.  In this
1703
                   particular example we would punt with All and let the default
1704
                   methods handle it */
1705
0
                found_match = false;
1706
1707
0
                if (strncmp("None", (const char *)color_names[n].colorant_name,
1708
0
                            color_names[n].name_size) == 0) {
1709
0
                    num_nonnone_names--;
1710
0
                } else {
1711
                    /* Colorant was not None */
1712
0
                    for (k = 0; k < num_entries; k++) {
1713
0
                        if (color_names[n].name_size ==
1714
0
                            namedcolor_table->named_color[k].name_size) {
1715
0
                            if (strncmp((const char *)namedcolor_table->named_color[k].colorant_name,
1716
0
                                (const char *)color_names[n].colorant_name, color_names[n].name_size) == 0) {
1717
0
                                found_match = true;
1718
0
                                break;
1719
0
                            }
1720
0
                        }
1721
0
                    }
1722
0
                    if (found_match) {
1723
0
                        indices[n] = k;
1724
0
                    } else {
1725
                        /* We do not know this colorant, return -1 */
1726
0
                        return -1;
1727
0
                    }
1728
0
                }
1729
0
            }
1730
0
            if (num_nonnone_names < 1)
1731
0
                return -1; /* No non-None colorants. */
1732
            /* We have all the colorants.  Lets go through and see if we can
1733
               make something that looks like a merge of the various ones */
1734
            /* Apply tint, blend LAB values.  Note that we may have wanted to
1735
               check if we even want to do this.  It is possible that the
1736
               device directly supports this particular colorant.  One may
1737
               want to check the alt tint transform boolean */
1738
1739
            /* Start with white */
1740
0
            for (j = 0; j < 3; j++) {
1741
0
                psrc[j] = white_lab[j];
1742
0
            }
1743
1744
0
            for (n = 0; n < num_nonnone_names; n++) {
1745
                /* Blend with the current color based upon current tint value */
1746
0
                for (j = 0; j < 3; j++) {
1747
0
                    psrc[j] = (unsigned short)
1748
0
                              ((float) namedcolor_table->named_color[indices[n]].lab[j] * tint_values[n]
1749
0
                             + (float) psrc[j] * (1.0 - tint_values[n]));
1750
0
                }
1751
0
            }
1752
1753
            /* Push LAB value through CMM to get CMYK device values */
1754
            /* Note that there are several options here. You could us an NCLR
1755
               icc profile to compute the device colors that you want.  For,
1756
               example if the output device had an NCLR profile.
1757
               However, what you MUST do here is set ALL the device values.
1758
               Hence, below we initialize all of them to zero and in this
1759
               particular example, set only the ones that were output from
1760
               the device profile */
1761
0
            if ( gs_output_profile != NULL ) {
1762
0
                curr_output_profile = gs_output_profile;
1763
0
            } else {
1764
                /* Use the device profile.  Note if one was not set for the
1765
                   device, the default CMYK profile is used.  Note that
1766
                   if we specified and NCLR profile it will be used here */
1767
0
                code = dev_proc(dev, get_profile)(dev,  &dev_profile);
1768
0
                gsicc_extract_profile(dev->graphics_type_tag,
1769
0
                                      dev_profile, &(curr_output_profile),
1770
0
                                      &render_cond);
1771
0
            }
1772
0
            icc_link = gsicc_get_link_profile(pgs, dev,
1773
0
                                            pgs->icc_manager->lab_profile,
1774
0
                                            curr_output_profile, rendering_params,
1775
0
                                            pgs->memory, false);
1776
0
            if (icc_link->is_identity) {
1777
0
                psrc_temp = &(psrc[0]);
1778
0
            } else {
1779
                /* Transform the color */
1780
0
                psrc_temp = &(psrc_cm[0]);
1781
0
                (icc_link->procs.map_color)(dev, icc_link, psrc, psrc_temp, 2);
1782
0
            }
1783
0
            gsicc_release_link(icc_link);
1784
1785
            /* Clear out ALL the color values */
1786
0
            for (k = 0; k < dev->color_info.num_components; k++){
1787
0
                device_values[k] = 0;
1788
0
            }
1789
            /* Set only the values that came from the profile.  By default
1790
               this would generally be just CMYK values.  For the equivalent
1791
               color computation case it certainly will be.  If someone
1792
               specified an NCLR profile it could be more.  Note that if an
1793
               NCLR profile is being used we will want to make sure the colorant
1794
               order is correct */
1795
0
            for (k = 0; k < curr_output_profile->num_comps; k++){
1796
0
                device_values[k] = psrc_temp[k];
1797
0
            }
1798
0
            return 0;
1799
0
        }
1800
0
    }
1801
0
    return -1; /* Color not found */
1802
0
}
1803
1804
/* Used by gs to notify the ICC manager that we are done with this link for now */
1805
/* This may release elements waiting on an icc_link_cache slot */
1806
void
1807
gsicc_release_link(gsicc_link_t *icclink)
1808
251M
{
1809
251M
    gsicc_link_cache_t *icc_link_cache;
1810
1811
251M
    if (icclink == NULL)
1812
22.4k
        return;
1813
1814
251M
    icc_link_cache = icclink->icc_link_cache;
1815
1816
251M
    gx_monitor_enter(icc_link_cache->lock);
1817
251M
    if_debug2m('^', icclink->memory, "[^]icclink "PRI_INTPTR" -- => %d\n",
1818
251M
               (intptr_t)icclink, icclink->ref_count - 1);
1819
    /* Decrement the reference count */
1820
251M
    if (--(icclink->ref_count) == 0) {
1821
1822
129M
        gsicc_link_t *curr, *prev;
1823
1824
        /* Find link in cache, and move it to the end of the list.  */
1825
        /* This way zero ref_count links are found LRU first  */
1826
129M
        curr = icc_link_cache->head;
1827
129M
        prev = NULL;
1828
129M
        while (curr != icclink) {
1829
0
            prev = curr;
1830
0
            curr = curr->next;
1831
0
        };
1832
129M
        if (prev == NULL) {
1833
            /* this link was the head */
1834
129M
            icc_link_cache->head = curr->next;
1835
129M
        } else {
1836
0
            prev->next = curr->next;    /* de-link this one */
1837
0
        }
1838
        /* Find the first zero-ref entry on the list */
1839
129M
        curr = icc_link_cache->head;
1840
129M
        prev = NULL;
1841
129M
        while (curr != NULL && curr->ref_count > 0) {
1842
0
            prev = curr;
1843
0
            curr = curr->next;
1844
0
        }
1845
        /* Found where to link this one into the tail of the list */
1846
129M
        if (prev == NULL) {
1847
129M
            icc_link_cache->head = icclink;
1848
129M
            icclink->next = icc_link_cache->head->next;
1849
129M
        } else {
1850
            /* link this one in here */
1851
0
            prev->next = icclink;
1852
0
            icclink->next = curr;
1853
0
        }
1854
        /* Finally, if some thread was waiting because the cache was full, let it run */
1855
129M
        if (icc_link_cache->cache_full) {
1856
0
            icc_link_cache->cache_full = false;
1857
0
            gx_semaphore_signal(icc_link_cache->full_wait);  /* let a waiting thread run */
1858
0
        }
1859
129M
    }
1860
251M
    gx_monitor_leave(icc_link_cache->lock);
1861
251M
}
1862
1863
/* Used to initialize the buffer description prior to color conversion */
1864
void
1865
gsicc_init_buffer(gsicc_bufferdesc_t *buffer_desc, unsigned char num_chan, unsigned char bytes_per_chan,
1866
                  bool has_alpha, bool alpha_first, bool is_planar, int plane_stride, int row_stride,
1867
                  int num_rows, int pixels_per_row)
1868
6.25M
{
1869
6.25M
    buffer_desc->num_chan = num_chan;
1870
6.25M
    buffer_desc->bytes_per_chan = bytes_per_chan;
1871
6.25M
    buffer_desc->has_alpha = has_alpha;
1872
6.25M
    buffer_desc->alpha_first = alpha_first;
1873
6.25M
    buffer_desc->is_planar = is_planar;
1874
6.25M
    buffer_desc->plane_stride = plane_stride;
1875
6.25M
    buffer_desc->row_stride = row_stride;
1876
6.25M
    buffer_desc->num_rows = num_rows;
1877
6.25M
    buffer_desc->pixels_per_row = pixels_per_row;
1878
6.25M
    buffer_desc->endian_swap = false;
1879
6.25M
}
1880
1881
/* Return the proper component numbers based upon the profiles of the device.
1882
   This is in here since it is usually called when creating and using a link
1883
   from the link cache. */
1884
int
1885
gsicc_get_device_profile_comps(const cmm_dev_profile_t *dev_profile)
1886
3.28G
{
1887
3.28G
    if (dev_profile->link_profile == NULL) {
1888
3.28G
       return dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE]->num_comps;
1889
3.28G
    } else {
1890
0
       return dev_profile->link_profile->num_comps_out;
1891
0
    }
1892
3.28G
}