Coverage Report

Created: 2022-10-31 07:00

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