Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gsicc_manage.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
/*  GS ICC Manager.  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 "strmio.h"
25
#include "gx.h"
26
#include "gp.h"
27
#include "gxgstate.h"
28
#include "gxcspace.h"
29
#include "gscms.h"
30
#include "gsicc_manage.h"
31
#include "gsicc_cache.h"
32
#include "gsicc_profilecache.h"
33
#include "gsicc_cms.h"
34
#include "gserrors.h"
35
#include "string_.h"
36
#include "gxclist.h"
37
#include "gxcldev.h"
38
#include "gzstate.h"
39
#include "gsicc_create.h"
40
#include "gpmisc.h"
41
#include "gxdevice.h"
42
#include "gxdevsop.h"
43
#include "assert_.h"
44
45
11.7M
#define ICC_HEADER_SIZE 128
46
#define CREATE_V2_DATA 0
47
48
#if ICC_DUMP
49
unsigned int global_icc_index = 0;
50
#endif
51
52
/* Needed for gsicc_set_devicen_equiv_colors. */
53
extern const gs_color_space_type gs_color_space_type_ICC;
54
55
/* Static prototypes */
56
57
static void gsicc_set_default_cs_value(cmm_profile_t *picc_profile,
58
                                       gs_gstate *pgs);
59
static gsicc_namelist_t* gsicc_new_namelist(gs_memory_t *memory);
60
static gsicc_colorname_t* gsicc_new_colorname(gs_memory_t *memory);
61
static gsicc_namelist_t* gsicc_get_spotnames(gcmmhprofile_t profile,
62
                                             gs_memory_t *memory);
63
static void gsicc_manager_free_contents(gsicc_manager_t *icc_man,
64
                                        client_name_t cname);
65
66
static void rc_gsicc_manager_free(gs_memory_t * mem, void *ptr_in,
67
                                  client_name_t cname);
68
static void rc_free_icc_profile(gs_memory_t * mem, void *ptr_in,
69
                                client_name_t cname);
70
static int gsicc_load_profile_buffer(cmm_profile_t *profile, stream *s,
71
                                     gs_memory_t *memory);
72
static int64_t gsicc_search_icc_table(clist_icctable_t *icc_table,
73
                                      int64_t icc_hashcode, int *size);
74
static int gsicc_load_namedcolor_buffer(cmm_profile_t *profile, stream *s,
75
                          gs_memory_t *memory);
76
static cmm_srcgtag_profile_t* gsicc_new_srcgtag_profile(gs_memory_t *memory);
77
static void gsicc_free_spotnames(gsicc_namelist_t *spotnames, gs_memory_t * mem);
78
79
static void
80
gsicc_manager_finalize(const gs_memory_t *memory, void * vptr);
81
82
static void
83
gsicc_smask_finalize(const gs_memory_t *memory, void * vptr);
84
85
/* profile data structure */
86
/* profile_handle should NOT be garbage collected since it is allocated by the external CMS */
87
gs_private_st_ptrs2(st_gsicc_colorname, gsicc_colorname_t, "gsicc_colorname",
88
                    gsicc_colorname_enum_ptrs, gsicc_colorname_reloc_ptrs, name, next);
89
90
gs_private_st_ptrs2_final(st_gsicc_manager, gsicc_manager_t, "gsicc_manager",
91
                    gsicc_manager_enum_ptrs, gsicc_manager_profile_reloc_ptrs,
92
                    gsicc_manager_finalize, smask_profiles, device_n);
93
94
gs_private_st_simple_final(st_gsicc_smask, gsicc_smask_t, "gsicc_smask", gsicc_smask_finalize);
95
96
gs_private_st_ptrs2(st_gsicc_devicen, gsicc_devicen_t, "gsicc_devicen",
97
                gsicc_devicen_enum_ptrs, gsicc_devicen_reloc_ptrs, head, final);
98
99
gs_private_st_ptrs1(st_gsicc_devicen_entry, gsicc_devicen_entry_t,
100
                    "gsicc_devicen_entry", gsicc_devicen_entry_enum_ptrs,
101
                    gsicc_devicen_entry_reloc_ptrs, next);
102
103
typedef struct default_profile_def_s {
104
    const char *path;
105
    gsicc_profile_t default_type;
106
} default_profile_def_t;
107
108
static default_profile_def_t default_profile_params[] =
109
{
110
    {DEFAULT_GRAY_ICC, DEFAULT_GRAY},
111
    {DEFAULT_RGB_ICC, DEFAULT_RGB},
112
    {DEFAULT_CMYK_ICC, DEFAULT_CMYK},
113
    {LAB_ICC, LAB_TYPE}
114
};
115
116
void
117
gsicc_setcoloraccuracy(gs_memory_t *mem, uint level)
118
1.95M
{
119
1.95M
    gs_lib_ctx_t *ctx = gs_lib_ctx_get_interp_instance(mem);
120
121
1.95M
    ctx->icc_color_accuracy = level;
122
1.95M
}
123
124
uint
125
gsicc_currentcoloraccuracy(gs_memory_t *mem)
126
1.95M
{
127
1.95M
    gs_lib_ctx_t *ctx = gs_lib_ctx_get_interp_instance(mem);
128
129
1.95M
    return ctx->icc_color_accuracy;
130
1.95M
}
131
132
/* Get the size of the ICC profile that is in the buffer */
133
unsigned int
134
gsicc_getprofilesize(unsigned char *buffer)
135
0
{
136
0
    return ( (buffer[0] << 24) + (buffer[1] << 16) +
137
0
             (buffer[2] << 8)  +  buffer[3] );
138
0
}
139
140
/* Get major and minor ICC version number */
141
int
142
gsicc_getprofilevers(cmm_profile_t *icc_profile, unsigned char *major,
143
    unsigned char *minor)
144
1.67k
{
145
1.67k
    if (icc_profile == NULL || icc_profile->buffer == NULL)
146
0
        return -1;
147
148
1.67k
    *major = icc_profile->buffer[8];
149
1.67k
    *minor = icc_profile->buffer[9];
150
151
1.67k
    return 0;
152
1.67k
}
153
154
void
155
gsicc_set_icc_range(cmm_profile_t **icc_profile)
156
10.5M
{
157
10.5M
    int num_comp = (*icc_profile)->num_comps;
158
10.5M
    int k;
159
160
39.5M
    for ( k = 0; k < num_comp; k++) {
161
28.9M
        (*icc_profile)->Range.ranges[k].rmin = 0.0;
162
28.9M
        (*icc_profile)->Range.ranges[k].rmax = 1.0;
163
28.9M
    }
164
10.5M
}
165
166
cmm_profile_t*
167
gsicc_set_iccsmaskprofile(const char *pname,
168
                          int namelen, gsicc_manager_t *icc_manager,
169
                          gs_memory_t *mem)
170
139k
{
171
139k
    stream *str;
172
139k
    int code;
173
139k
    cmm_profile_t *icc_profile;
174
175
139k
    if (icc_manager == NULL) {
176
0
        code = gsicc_open_search(pname, namelen, mem, NULL, 0, &str);
177
139k
    } else {
178
139k
        code = gsicc_open_search(pname, namelen, mem, mem->gs_lib_ctx->profiledir,
179
139k
                                 mem->gs_lib_ctx->profiledir_len, &str);
180
139k
    }
181
139k
    if (code < 0 || str == NULL)
182
0
        return NULL;
183
139k
    icc_profile = gsicc_profile_new(str, mem, pname, namelen);
184
139k
    code = sfclose(str);
185
139k
    if (icc_profile == NULL)
186
0
        return NULL;
187
    /* Get the profile handle */
188
139k
    icc_profile->profile_handle =
189
139k
            gsicc_get_profile_handle_buffer(icc_profile->buffer,
190
139k
                                            icc_profile->buffer_size,
191
139k
                                            mem);
192
139k
    if (!icc_profile->profile_handle) {
193
0
        rc_free_icc_profile(mem, icc_profile, "gsicc_set_iccsmaskprofile");
194
0
        return NULL;
195
0
    }
196
    /* Compute the hash code of the profile. Everything in the
197
       ICC manager will have it's hash code precomputed */
198
139k
    gsicc_get_icc_buff_hash(icc_profile->buffer, &(icc_profile->hashcode),
199
139k
                            icc_profile->buffer_size);
200
139k
    icc_profile->hash_is_valid = true;
201
139k
    icc_profile->num_comps =
202
139k
            gscms_get_input_channel_count(icc_profile->profile_handle, icc_profile->memory);
203
139k
    if (icc_profile->num_comps > ICC_MAX_CHANNELS) {
204
0
        rc_free_icc_profile(mem, icc_profile, "gsicc_set_iccsmaskprofile");
205
0
        return NULL;
206
0
    }
207
139k
    icc_profile->num_comps_out =
208
139k
            gscms_get_output_channel_count(icc_profile->profile_handle, icc_profile->memory);
209
139k
    icc_profile->data_cs =
210
139k
            gscms_get_profile_data_space(icc_profile->profile_handle, icc_profile->memory);
211
139k
    gsicc_set_icc_range(&icc_profile);
212
139k
    return icc_profile;
213
139k
}
214
215
static void
216
gsicc_smask_finalize(const gs_memory_t *memory, void * vptr)
217
60.9k
{
218
60.9k
    gsicc_smask_t *iccsmask = (gsicc_smask_t *)vptr;
219
220
60.9k
    gsicc_adjust_profile_rc(iccsmask->smask_gray, -1,
221
60.9k
        "gsicc_smask_finalize");
222
60.9k
    gsicc_adjust_profile_rc(iccsmask->smask_rgb, -1,
223
60.9k
        "gsicc_smask_finalize");
224
60.9k
    gsicc_adjust_profile_rc(iccsmask->smask_cmyk, -1,
225
60.9k
        "gsicc_smask_finalize");
226
60.9k
}
227
228
gsicc_smask_t*
229
gsicc_new_iccsmask(gs_memory_t *memory)
230
60.9k
{
231
60.9k
    gsicc_smask_t *result;
232
233
60.9k
    result = (gsicc_smask_t *) gs_alloc_struct(memory, gsicc_smask_t, &st_gsicc_smask, "gsicc_new_iccsmask");
234
60.9k
    if (result != NULL) {
235
60.9k
        result->smask_gray = NULL;
236
60.9k
        result->smask_rgb = NULL;
237
60.9k
        result->smask_cmyk = NULL;
238
60.9k
        result->memory = memory;
239
60.9k
        result->swapped = false;
240
60.9k
    }
241
60.9k
    return result;
242
60.9k
}
243
244
/* Allocate a new structure to hold the profiles that contains the profiles
245
   used when we are in a softmask group */
246
int
247
gsicc_initialize_iccsmask(gsicc_manager_t *icc_manager)
248
40.2k
{
249
40.2k
    gs_memory_t *stable_mem = icc_manager->memory->stable_memory;
250
251
    /* Allocations need to be done in stable memory.  We want to maintain
252
       the smask_profiles object */
253
40.2k
    icc_manager->smask_profiles = gsicc_new_iccsmask(stable_mem);
254
40.2k
    if (icc_manager->smask_profiles == NULL)
255
0
        return gs_throw(gs_error_VMerror, "insufficient memory to allocate smask profiles");
256
    /* Load the gray, rgb, and cmyk profiles */
257
40.2k
    if ((icc_manager->smask_profiles->smask_gray =
258
40.2k
        gsicc_set_iccsmaskprofile(SMASK_GRAY_ICC, strlen(SMASK_GRAY_ICC),
259
40.2k
        icc_manager, stable_mem) ) == NULL)
260
0
        goto error;
261
40.2k
    if ((icc_manager->smask_profiles->smask_rgb =
262
40.2k
        gsicc_set_iccsmaskprofile(SMASK_RGB_ICC, strlen(SMASK_RGB_ICC),
263
40.2k
        icc_manager, stable_mem)) == NULL)
264
0
        goto error;
265
40.2k
    if ((icc_manager->smask_profiles->smask_cmyk =
266
40.2k
        gsicc_set_iccsmaskprofile(SMASK_CMYK_ICC, strlen(SMASK_CMYK_ICC),
267
40.2k
        icc_manager, stable_mem)) == NULL)
268
0
        goto error;
269
270
    /* Set these as "default" so that pdfwrite or other high level devices
271
       will know that these are manufactured profiles, and default spaces
272
       should be used */
273
40.2k
    icc_manager->smask_profiles->smask_gray->default_match = DEFAULT_GRAY;
274
40.2k
    icc_manager->smask_profiles->smask_rgb->default_match = DEFAULT_RGB;
275
40.2k
    icc_manager->smask_profiles->smask_cmyk->default_match = DEFAULT_CMYK;
276
40.2k
    return 0;
277
278
0
error:
279
0
    if (icc_manager->smask_profiles->smask_gray)
280
0
        rc_free_icc_profile(stable_mem, icc_manager->smask_profiles->smask_gray, "gsicc_initialize_iccsmask");
281
0
    icc_manager->smask_profiles->smask_gray = NULL;
282
0
    if (icc_manager->smask_profiles->smask_rgb)
283
0
        rc_free_icc_profile(stable_mem, icc_manager->smask_profiles->smask_rgb, "gsicc_initialize_iccsmask");
284
0
    icc_manager->smask_profiles->smask_rgb = NULL;
285
0
    if (icc_manager->smask_profiles->smask_cmyk)
286
0
        rc_free_icc_profile(stable_mem, icc_manager->smask_profiles->smask_cmyk, "gsicc_initialize_iccsmask");
287
0
    icc_manager->smask_profiles->smask_cmyk = NULL;
288
0
    gs_free_object(stable_mem, icc_manager->smask_profiles, "gsicc_initialize_iccsmask");
289
0
    icc_manager->smask_profiles = NULL;
290
0
    return gs_throw(-1, "failed to load an smask profile");
291
40.2k
}
292
293
static int
294
gsicc_new_devicen(gsicc_manager_t *icc_manager)
295
0
{
296
/* Allocate a new deviceN ICC profile entry in the deviceN list */
297
0
    gsicc_devicen_entry_t *device_n_entry =
298
0
        gs_alloc_struct(icc_manager->memory, gsicc_devicen_entry_t,
299
0
                &st_gsicc_devicen_entry, "gsicc_new_devicen");
300
0
    if (device_n_entry == NULL)
301
0
        return gs_throw(gs_error_VMerror, "insufficient memory to allocate device n profile");
302
0
    device_n_entry->next = NULL;
303
0
    device_n_entry->iccprofile = NULL;
304
/* Check if we already have one in the manager */
305
0
    if ( icc_manager->device_n == NULL ) {
306
        /* First one.  Need to allocate the DeviceN main object */
307
0
        icc_manager->device_n = gs_alloc_struct(icc_manager->memory,
308
0
            gsicc_devicen_t, &st_gsicc_devicen, "gsicc_new_devicen");
309
310
0
        if (icc_manager->device_n == NULL)
311
0
            return gs_throw(gs_error_VMerror, "insufficient memory to allocate device n profile");
312
313
0
        icc_manager->device_n->head = device_n_entry;
314
0
        icc_manager->device_n->final = device_n_entry;
315
0
        icc_manager->device_n->count = 1;
316
0
        return 0;
317
0
    } else {
318
        /* We have one or more in the list. */
319
0
        icc_manager->device_n->final->next = device_n_entry;
320
0
        icc_manager->device_n->final = device_n_entry;
321
0
        icc_manager->device_n->count++;
322
0
        return 0;
323
0
    }
324
0
}
325
326
cmm_profile_t*
327
gsicc_finddevicen(const gs_color_space *pcs, gsicc_manager_t *icc_manager)
328
0
{
329
0
    int k,j,i;
330
0
    gsicc_devicen_entry_t *curr_entry;
331
0
    int num_comps;
332
0
    char **names = pcs->params.device_n.names;
333
0
    unsigned char *pname;
334
0
    unsigned int name_size;
335
0
    gsicc_devicen_t *devicen_profiles = icc_manager->device_n;
336
0
    gsicc_colorname_t *icc_spot_entry;
337
0
    int match_count = 0;
338
0
    bool permute_needed = false;
339
340
0
    num_comps = gs_color_space_num_components(pcs);
341
342
    /* Go through the list looking for a match */
343
0
    curr_entry = devicen_profiles->head;
344
0
    for ( k = 0; k < devicen_profiles->count; k++ ) {
345
0
        if (curr_entry->iccprofile->num_comps == num_comps ) {
346
347
            /* Now check the names.  The order is important
348
               since this is supposed to be the laydown order.
349
               If the order is off, the ICC profile will likely
350
               not be accurate.  The ICC profile drives the laydown
351
               order here.  A permutation vector is used to
352
               reorganize the data prior to the transform application */
353
0
            for ( j = 0; j < num_comps; j++) {
354
                /* Get the character string and length for the component name. */
355
0
                pname = (unsigned char *)names[j];
356
0
                name_size = strlen(names[j]);
357
                /* Compare to the jth entry in the ICC profile */
358
0
                icc_spot_entry = curr_entry->iccprofile->spotnames->head;
359
0
                for ( i = 0; i < num_comps; i++) {
360
0
                    if( strncmp((const char *) pname,
361
0
                        icc_spot_entry->name, name_size) == 0 ) {
362
                        /* Found a match */
363
0
                        match_count++;
364
0
                        curr_entry->iccprofile->devicen_permute[j] = i;
365
0
                        if ( j != i) {
366
                            /* Document ink order does not match ICC
367
                               profile ink order */
368
0
                            permute_needed = true;
369
0
                        }
370
0
                        break;
371
0
                    } else
372
0
                        icc_spot_entry = icc_spot_entry->next;
373
0
                }
374
0
                if (match_count < j+1)
375
0
                    return(NULL);
376
0
            }
377
0
            if ( match_count == num_comps) {
378
                /* We have a match.  Order of components does not match laydown
379
                   order specified by the ICC profile.  Set a flag.  This may
380
                   be an issue if we are using 2 DeviceN color spaces with the
381
                   same colorants but with different component orders.  The problem
382
                   comes about since we would be sharing the profile in the
383
                   DeviceN entry of the icc manager. */
384
0
                curr_entry->iccprofile->devicen_permute_needed = permute_needed;
385
0
                return(curr_entry->iccprofile);
386
0
            }
387
0
            match_count = 0;
388
0
        }
389
0
    }
390
0
    return NULL;
391
0
}
392
393
/* Populate the color names entries that should
394
   be contained in the DeviceN ICC profile */
395
static gsicc_namelist_t*
396
gsicc_get_spotnames(gcmmhprofile_t profile, gs_memory_t *memory)
397
0
{
398
0
    int k;
399
0
    gsicc_namelist_t *list;
400
0
    gsicc_colorname_t *name;
401
0
    gsicc_colorname_t **curr_entry;
402
0
    int num_colors;
403
0
    char *clr_name;
404
405
0
    num_colors = gscms_get_numberclrtnames(profile, memory);
406
0
    if (num_colors == 0)
407
0
        return(NULL);
408
    /* Allocate structure for managing this */
409
0
    list = gsicc_new_namelist(memory);
410
0
    if (list == NULL)
411
0
        return(NULL);
412
0
    curr_entry = &(list->head);
413
0
    list->count = num_colors;
414
0
    for (k = 0; k < num_colors; k++) {
415
        /* Allocate a new name object */
416
0
        clr_name = gscms_get_clrtname(profile, k, memory);
417
0
        if (clr_name == NULL)
418
0
            break;
419
0
        name = gsicc_new_colorname(memory);
420
0
        if (name == NULL) {
421
            /* FIXME: Free clr_name */
422
0
            gs_free_object(memory, clr_name, "gsicc_get_spotnames");
423
0
            break;
424
0
        }
425
        /* Get the name */
426
0
        name->name = clr_name;
427
0
        name->length = strlen(clr_name);
428
0
        *curr_entry = name;
429
0
        curr_entry = &(name->next);
430
0
    }
431
0
    if (k < num_colors) {
432
        /* Failed allocation */
433
0
        gsicc_free_spotnames(list, memory);
434
0
        return NULL;
435
0
    }
436
0
    return list;
437
0
}
438
439
static void
440
gsicc_get_devicen_names(cmm_profile_t *icc_profile, gs_memory_t *memory)
441
0
{
442
    /* The names are contained in the
443
       named color tag.  We use the
444
       CMM to extract the data from the
445
       profile */
446
0
    if (icc_profile->profile_handle == NULL) {
447
0
        if (icc_profile->buffer != NULL) {
448
0
            icc_profile->profile_handle =
449
0
                gsicc_get_profile_handle_buffer(icc_profile->buffer,
450
0
                                                icc_profile->buffer_size,
451
0
                                                memory);
452
0
        } else
453
0
            return;
454
0
    }
455
0
    icc_profile->spotnames =
456
0
        gsicc_get_spotnames(icc_profile->profile_handle, memory->non_gc_memory);
457
0
    return;
458
0
}
459
460
/* Allocate new spot name list object.  */
461
static gsicc_namelist_t*
462
gsicc_new_namelist(gs_memory_t *memory)
463
0
{
464
0
    gsicc_namelist_t *result;
465
466
0
    result = (gsicc_namelist_t *) gs_alloc_bytes(memory->non_gc_memory, sizeof(gsicc_namelist_t),
467
0
                                                 "gsicc_new_namelist");
468
0
    if (result == NULL)
469
0
        return NULL;
470
0
    result->count = 0;
471
0
    result->head = NULL;
472
0
    result->name_str = NULL;
473
0
    result->color_map = NULL;
474
0
    return result;
475
0
}
476
477
/* Allocate new spot name.  */
478
static gsicc_colorname_t*
479
gsicc_new_colorname(gs_memory_t *memory)
480
0
{
481
0
    gsicc_colorname_t *result;
482
483
0
    result = gs_alloc_struct(memory,gsicc_colorname_t,
484
0
                &st_gsicc_colorname, "gsicc_new_colorname");
485
0
    if (result == NULL)
486
0
        return NULL;
487
0
    result->length = 0;
488
0
    result->name = NULL;
489
0
    result->next = NULL;
490
0
    return result;
491
0
}
492
493
/* If the profile is one of the default types that were set in the iccmanager,
494
   then the index for that type is returned.  Otherwise the ICC index is returned.
495
   This is currently used to keep us from writing out the default profiles for
496
   high level devices, if desired. */
497
gs_color_space_index
498
gsicc_get_default_type(cmm_profile_t *profile_data)
499
1.71M
{
500
1.71M
    switch (profile_data->default_match) {
501
513k
        case DEFAULT_GRAY:
502
513k
            return gs_color_space_index_DeviceGray;
503
904k
        case DEFAULT_RGB:
504
904k
            return gs_color_space_index_DeviceRGB;
505
108k
        case DEFAULT_CMYK:
506
108k
            return gs_color_space_index_DeviceCMYK;
507
0
        case CIE_A:
508
0
            return gs_color_space_index_CIEA;
509
0
        case CIE_ABC:
510
0
            return gs_color_space_index_CIEABC;
511
0
        case CIE_DEF:
512
0
            return gs_color_space_index_CIEDEF;
513
0
        case CIE_DEFG:
514
0
            return gs_color_space_index_CIEDEFG;
515
184k
        default:
516
184k
            return gs_color_space_index_ICC;
517
1.71M
    }
518
1.71M
}
519
520
int
521
gsicc_use_fast_color(cmm_profile_t* profile_data)
522
183M
{
523
183M
    switch (profile_data->default_match) {
524
40
    case CIE_A:
525
64
    case CIE_ABC:
526
64
    case CIE_DEF:
527
64
    case CIE_DEFG:
528
1.02M
    case LAB_TYPE:
529
1.02M
    case NAMED_TYPE:
530
1.02M
    case DEVICEN_TYPE:
531
1.02M
        return 0;
532
182M
    default:
533
182M
        return profile_data->num_comps;
534
183M
    }
535
183M
}
536
537
bool
538
gsicc_is_default_profile(cmm_profile_t *profile_data)
539
0
{
540
0
    switch (profile_data->default_match) {
541
0
        case DEFAULT_GRAY:
542
0
        case DEFAULT_RGB:
543
0
        case DEFAULT_CMYK:
544
0
            return true;
545
0
        default:
546
0
            return false;
547
0
    }
548
0
}
549
550
bool
551
gsicc_profile_from_ps(cmm_profile_t *profile_data)
552
670k
{
553
670k
    switch ( profile_data->default_match ) {
554
27
        case CIE_A:
555
51
        case CIE_ABC:
556
51
        case CIE_DEF:
557
51
        case CIE_DEFG:
558
51
            return true;
559
670k
        default:
560
670k
            return false;
561
670k
    }
562
670k
}
563
564
/*
565
 * Adjust the reference count of the profile. This is intended to support
566
 * applications (such as XPS) which maintain ICC profiles outside of the
567
 * graphic state.
568
 */
569
/* for multi-threaded use, we need to adjust the ref_count safely */
570
void
571
gsicc_adjust_profile_rc(cmm_profile_t *profile_data, int delta, const char *name_str)
572
71.4M
{
573
71.4M
    if (profile_data != NULL) {
574
66.4M
        gx_monitor_enter(profile_data->lock);
575
66.4M
        if (profile_data->rc.ref_count == 1 && delta < 0) {
576
11.5M
            profile_data->rc.ref_count = 0;   /* while locked */
577
11.5M
            gx_monitor_leave(profile_data->lock);
578
11.5M
            rc_free_struct(profile_data, name_str);
579
54.8M
        } else {
580
54.8M
            rc_adjust(profile_data, delta, name_str);
581
54.8M
            gx_monitor_leave(profile_data->lock);
582
54.8M
        }
583
66.4M
    }
584
71.4M
}
585
586
/* Fill in the actual source structure rending information */
587
static int
588
gsicc_fill_srcgtag_item(gsicc_rendering_param_t *r_params, char **pstrlast, bool cmyk)
589
0
{
590
0
    char *curr_ptr;
591
0
    int blackptcomp;
592
0
    int or_icc, preserve_k;
593
0
    int ri;
594
595
    /* Get the intent */
596
0
    curr_ptr = gs_strtok(NULL, "\t, \n\r", pstrlast);
597
0
    if (curr_ptr == NULL || sscanf(curr_ptr, "%d", &ri) != 1)
598
0
        return_error(gs_error_unknownerror);
599
0
    r_params->rendering_intent = ri | gsRI_OVERRIDE;
600
    /* Get the black point compensation setting */
601
0
    curr_ptr = gs_strtok(NULL, "\t, \n\r", pstrlast);
602
0
    if (curr_ptr == NULL || sscanf(curr_ptr, "%d", &blackptcomp) != 1)
603
0
        return_error(gs_error_unknownerror);
604
0
    r_params->black_point_comp = blackptcomp | gsBP_OVERRIDE;
605
    /* Get the over-ride embedded ICC boolean */
606
0
    curr_ptr = gs_strtok(NULL, "\t, \n\r", pstrlast);
607
0
    if (curr_ptr == NULL || sscanf(curr_ptr, "%d", &or_icc) != 1)
608
0
        return_error(gs_error_unknownerror);
609
0
    r_params->override_icc = or_icc;
610
0
    if (cmyk) {
611
        /* Get the preserve K control */
612
0
        curr_ptr = gs_strtok(NULL, "\t, \n\r", pstrlast);
613
0
        if (curr_ptr == NULL || sscanf(curr_ptr, "%d", &preserve_k) < 1)
614
0
            return_error(gs_error_unknownerror);
615
0
        r_params->preserve_black = preserve_k | gsKP_OVERRIDE;
616
0
    } else {
617
0
        r_params->preserve_black = gsBKPRESNOTSPECIFIED;
618
0
    }
619
0
    return 0;
620
0
}
621
622
static int
623
gsicc_check_device_link(cmm_profile_t *icc_profile, gs_memory_t *memory)
624
0
{
625
0
    bool value;
626
627
0
    value = gscms_is_device_link(icc_profile->profile_handle, memory);
628
0
    icc_profile->isdevlink = value;
629
630
0
    return value;
631
0
}
632
633
int
634
gsicc_get_device_class(cmm_profile_t *icc_profile)
635
6
{
636
6
    return gscms_get_device_class(icc_profile->profile_handle, icc_profile->memory);
637
6
}
638
639
/* This inititializes the srcgtag structure in the ICC manager */
640
static int
641
gsicc_set_srcgtag_struct(gx_device *pdev, gsicc_manager_t *icc_manager, const char* pname,
642
                        int namelen)
643
0
{
644
0
    gs_memory_t *mem;
645
0
    stream *str;
646
0
    int code;
647
0
    int info_size;
648
0
    char *buffer_ptr, *curr_ptr, *last;
649
0
    int num_bytes;
650
0
    int k;
651
0
    static const char *const srcgtag_keys[] = {GSICC_SRCGTAG_KEYS};
652
0
    cmm_profile_t *icc_profile = NULL;
653
0
    cmm_srcgtag_profile_t *srcgtag;
654
0
    bool start = true;
655
0
    gsicc_cmm_t cmm = gsCMM_DEFAULT;
656
657
    /* If we don't have an icc manager or if this thing is already set
658
       then ignore the call.  For now, I am going to allow it to
659
       be set one time. */
660
0
    if (icc_manager == NULL)
661
0
        return 0;
662
0
    if (icc_manager->srcgtag_profile != NULL) {
663
0
        return dev_proc(pdev, dev_spec_op)(pdev, gxdso_set_srcgtag, icc_manager->srcgtag_profile, sizeof(srcgtag));
664
0
    } else {
665
0
        mem = icc_manager->memory->non_gc_memory;
666
0
        code = gsicc_open_search(pname, namelen, mem, mem->gs_lib_ctx->profiledir,
667
0
                                 mem->gs_lib_ctx->profiledir_len, &str);
668
0
        if (code < 0)
669
0
            return code;
670
0
    }
671
0
    if (str != NULL) {
672
        /* Get the information in the file */
673
0
        code = sfseek(str,0,SEEK_END);
674
0
        if (code < 0)
675
0
            return code;
676
0
        info_size = sftell(str);
677
0
        code = srewind(str);
678
0
        if (code < 0)
679
0
            return code;
680
0
        if (info_size > (GSICC_NUM_SRCGTAG_KEYS + 1) * FILENAME_MAX) {
681
0
            return gs_throw1(-1, "setting of %s src obj color info failed",
682
0
                               pname);
683
0
        }
684
        /* Allocate the buffer, stuff with the data */
685
0
        buffer_ptr = (char*) gs_alloc_bytes(mem, info_size+1,
686
0
                                            "gsicc_set_srcgtag_struct");
687
0
        if (buffer_ptr == NULL) {
688
0
            return gs_throw1(gs_error_VMerror, "setting of %s src obj color info failed",
689
0
                               pname);
690
0
        }
691
0
        num_bytes = sfread(buffer_ptr,sizeof(unsigned char), info_size, str);
692
0
        code = sfclose(str);
693
0
        if (code < 0) {
694
0
            gs_free_object(mem, buffer_ptr, "gsicc_set_srcgtag_struct");
695
0
             return code;
696
0
        }
697
0
        buffer_ptr[info_size] = 0;
698
0
        if (num_bytes != info_size) {
699
0
            gs_free_object(mem, buffer_ptr, "gsicc_set_srcgtag_struct");
700
0
            return gs_throw1(-1, "setting of %s src obj color info failed",
701
0
                               pname);
702
0
        }
703
        /* Create the structure in which we will store this data */
704
0
        srcgtag = gsicc_new_srcgtag_profile(mem);
705
0
        if (srcgtag == NULL) {
706
0
            gs_free_object(mem, buffer_ptr, "gsicc_set_srcgtag_struct");
707
0
            return gs_throw1(gs_error_VMerror, "creation of profile for %s failed",
708
0
                               pname);
709
0
        }
710
        /* Now parse through the data opening the profiles that are needed */
711
0
        curr_ptr = buffer_ptr;
712
        /* Initialize that we want color management.  Then if profile is not
713
           present we know we did not want anything special done with that
714
           source type.  Else if we have no profile and don't want color
715
           management we will make sure to do that */
716
0
        for (k = 0; k < NUM_SOURCE_PROFILES; k++) {
717
0
            srcgtag->rgb_rend_cond[k].cmm = gsCMM_DEFAULT;
718
0
            srcgtag->cmyk_rend_cond[k].cmm = gsCMM_DEFAULT;
719
0
            srcgtag->gray_rend_cond[k].cmm = gsCMM_DEFAULT;
720
0
        }
721
0
        while (start || (curr_ptr != NULL && strlen(curr_ptr) > 0)) {
722
0
            if (start) {
723
0
                curr_ptr = gs_strtok(buffer_ptr, "\t, \n\r", &last);
724
0
                start = false;
725
0
            } else {
726
0
                curr_ptr = gs_strtok(NULL, "\t, \n\r", &last);
727
0
            }
728
0
            if (curr_ptr == NULL) break;
729
            /* Now go ahead and see if we have a match */
730
0
            for (k = 0; k < GSICC_NUM_SRCGTAG_KEYS; k++) {
731
0
                if (strncmp(curr_ptr, srcgtag_keys[k], strlen(srcgtag_keys[k])) == 0 ) {
732
                    /* Check if the curr_ptr is None which indicates that this
733
                       object is not to be color managed.  Also, if the
734
                       curr_ptr is Replace which indicates we will be doing
735
                       direct replacement of the colors.  */
736
0
                    curr_ptr = gs_strtok(NULL, "\t, \n\r", &last);
737
0
                    if (curr_ptr == NULL) break;
738
0
                    if (strncmp(curr_ptr, GSICC_SRCTAG_NOCM, strlen(GSICC_SRCTAG_NOCM)) == 0 &&
739
0
                        strlen(curr_ptr) == strlen(GSICC_SRCTAG_NOCM)) {
740
0
                        cmm = gsCMM_NONE;
741
0
                        icc_profile = NULL;
742
0
                        break;
743
0
                    } else if ((strncmp(curr_ptr, GSICC_SRCTAG_REPLACE, strlen(GSICC_SRCTAG_REPLACE)) == 0 &&
744
0
                        strlen(curr_ptr) == strlen(GSICC_SRCTAG_REPLACE))) {
745
0
                        cmm = gsCMM_REPLACE;
746
0
                        icc_profile = NULL;
747
0
                        break;
748
0
                    } else {
749
                        /* Try to open the file and set the profile */
750
0
                        code = gsicc_open_search(curr_ptr, strlen(curr_ptr), mem,
751
0
                                                 mem->gs_lib_ctx->profiledir,
752
0
                                                 mem->gs_lib_ctx->profiledir_len, &str);
753
0
                        if (code < 0)
754
0
                            return code;
755
0
                        if (str != NULL) {
756
0
                            icc_profile =
757
0
                                gsicc_profile_new(str, mem, curr_ptr, strlen(curr_ptr));
758
0
                            code = sfclose(str);
759
0
                            if (code < 0)
760
0
                                return code;
761
0
                        }
762
0
                        if (str != NULL && icc_profile != NULL) {
763
0
                            code = gsicc_init_profile_info(icc_profile);
764
0
                            if (code < 0)
765
0
                                return code;
766
0
                            cmm = gsCMM_DEFAULT;
767
                            /* Check if this object is a devicelink profile.
768
                               If it is then the intent, blackpoint etc. are not
769
                               read nor used when dealing with these profiles */
770
0
                            gsicc_check_device_link(icc_profile, icc_profile->memory);
771
0
                            break;
772
0
                        } else {
773
                            /* Failed to open profile file. End this now. */
774
0
                            gs_free_object(mem, buffer_ptr, "gsicc_set_srcgtag_struct");
775
0
                            rc_decrement(srcgtag, "gsicc_set_srcgtag_struct");
776
0
                            return gs_throw1(-1,
777
0
                                    "setting of %s src obj color info failed", pname);
778
0
                        }
779
0
                    }
780
0
                }
781
0
            }
782
            /* Get the intent now and set the profile. If GSICC_SRCGTAG_KEYS
783
               order changes this switch needs to change also */
784
0
            switch (k) {
785
0
                case COLOR_TUNE:
786
                    /* Color tune profile. No intent */
787
0
                    srcgtag->color_warp_profile = icc_profile;
788
0
                    break;
789
0
                case VECTOR_CMYK:
790
0
                    srcgtag->cmyk_profiles[gsSRC_GRAPPRO] = icc_profile;
791
0
                    srcgtag->cmyk_rend_cond[gsSRC_GRAPPRO].cmm = cmm;
792
0
                    if (cmm == gsCMM_DEFAULT) {
793
0
                        code = gsicc_fill_srcgtag_item(&(srcgtag->cmyk_rend_cond[gsSRC_GRAPPRO]), &last, true);
794
0
                        if (code < 0)
795
0
                            return code;
796
0
                    }
797
0
                    break;
798
0
                case IMAGE_CMYK:
799
0
                    srcgtag->cmyk_profiles[gsSRC_IMAGPRO] = icc_profile;
800
0
                    srcgtag->cmyk_rend_cond[gsSRC_IMAGPRO].cmm = cmm;
801
0
                    if (cmm == gsCMM_DEFAULT) {
802
0
                        code = gsicc_fill_srcgtag_item(&(srcgtag->cmyk_rend_cond[gsSRC_IMAGPRO]), &last, true);
803
0
                        if (code < 0)
804
0
                            return code;
805
0
                    }
806
0
                    break;
807
0
                case TEXT_CMYK:
808
0
                    srcgtag->cmyk_profiles[gsSRC_TEXTPRO] = icc_profile;
809
0
                    srcgtag->cmyk_rend_cond[gsSRC_TEXTPRO].cmm = cmm;
810
0
                    if (cmm == gsCMM_DEFAULT) {
811
0
                        code = gsicc_fill_srcgtag_item(&(srcgtag->cmyk_rend_cond[gsSRC_TEXTPRO]), &last, true);
812
0
                        if (code < 0)
813
0
                            return code;
814
0
                    }
815
0
                    break;
816
0
                case VECTOR_RGB:
817
0
                    srcgtag->rgb_profiles[gsSRC_GRAPPRO] = icc_profile;
818
0
                    srcgtag->rgb_rend_cond[gsSRC_GRAPPRO].cmm = cmm;
819
0
                    if (cmm == gsCMM_DEFAULT) {
820
0
                        code = gsicc_fill_srcgtag_item(&(srcgtag->rgb_rend_cond[gsSRC_GRAPPRO]), &last, false);
821
0
                        if (code < 0)
822
0
                            return code;
823
0
                    }
824
0
                   break;
825
0
                case IMAGE_RGB:
826
0
                    srcgtag->rgb_profiles[gsSRC_IMAGPRO] = icc_profile;
827
0
                    srcgtag->rgb_rend_cond[gsSRC_IMAGPRO].cmm = cmm;
828
0
                    if (cmm == gsCMM_DEFAULT) {
829
0
                        code = gsicc_fill_srcgtag_item(&(srcgtag->rgb_rend_cond[gsSRC_IMAGPRO]), &last, false);
830
0
                        if (code < 0)
831
0
                            return code;
832
0
                    }
833
0
                    break;
834
0
                case TEXT_RGB:
835
0
                    srcgtag->rgb_profiles[gsSRC_TEXTPRO] = icc_profile;
836
0
                    srcgtag->rgb_rend_cond[gsSRC_TEXTPRO].cmm = cmm;
837
0
                    if (cmm == gsCMM_DEFAULT) {
838
0
                        code = gsicc_fill_srcgtag_item(&(srcgtag->rgb_rend_cond[gsSRC_TEXTPRO]), &last, false);
839
0
                        if (code < 0)
840
0
                            return code;
841
0
                    }
842
0
                    break;
843
0
                case VECTOR_GRAY:
844
0
                    srcgtag->gray_profiles[gsSRC_GRAPPRO] = icc_profile;
845
0
                    srcgtag->gray_rend_cond[gsSRC_GRAPPRO].cmm = cmm;
846
0
                    if (cmm == gsCMM_DEFAULT) {
847
0
                        code = gsicc_fill_srcgtag_item(&(srcgtag->gray_rend_cond[gsSRC_GRAPPRO]), &last, false);
848
0
                        if (code < 0)
849
0
                            return code;
850
0
                    }
851
0
                    break;
852
0
                case IMAGE_GRAY:
853
0
                    srcgtag->gray_profiles[gsSRC_IMAGPRO] = icc_profile;
854
0
                    srcgtag->gray_rend_cond[gsSRC_IMAGPRO].cmm = cmm;
855
0
                    if (cmm == gsCMM_DEFAULT) {
856
0
                        code = gsicc_fill_srcgtag_item(&(srcgtag->gray_rend_cond[gsSRC_IMAGPRO]), &last, false);
857
0
                        if (code < 0)
858
0
                            return code;
859
0
                    }
860
0
                    break;
861
0
                case TEXT_GRAY:
862
0
                    srcgtag->gray_profiles[gsSRC_TEXTPRO] = icc_profile;
863
0
                    srcgtag->gray_rend_cond[gsSRC_TEXTPRO].cmm = cmm;
864
0
                    if (cmm == gsCMM_DEFAULT) {
865
0
                        code = gsicc_fill_srcgtag_item(&(srcgtag->gray_rend_cond[gsSRC_TEXTPRO]), &last, false);
866
0
                        if (code < 0)
867
0
                            return code;
868
0
                    }
869
0
                    break;
870
0
                case GSICC_NUM_SRCGTAG_KEYS:
871
                    /* Failed to match the key */
872
0
                    gs_free_object(mem, buffer_ptr, "gsicc_set_srcgtag_struct");
873
0
                    rc_decrement(srcgtag, "gsicc_set_srcgtag_struct");
874
0
                    return gs_throw1(-1, "failed to find key in %s", pname);
875
0
                    break;
876
0
                default:
877
                    /* Some issue */
878
0
                    gs_free_object(mem, buffer_ptr, "gsicc_set_srcgtag_struct");
879
0
                    rc_decrement(srcgtag, "gsicc_set_srcgtag_struct");
880
0
                    return gs_throw1(-1, "Error in srcgtag data %s", pname);
881
0
                    break;
882
0
            }
883
0
        }
884
0
    } else {
885
0
        return gs_throw1(-1, "setting of %s src obj color info failed", pname);
886
0
    }
887
0
    gs_free_object(mem, buffer_ptr, "gsicc_set_srcgtag_struct");
888
0
    srcgtag->name_length = namelen;
889
0
    srcgtag->name = (char*) gs_alloc_bytes(mem, srcgtag->name_length + 1,
890
0
                                  "gsicc_set_srcgtag_struct");
891
0
    if (srcgtag->name == NULL)
892
0
        return gs_throw(gs_error_VMerror, "Insufficient memory for tag name");
893
0
    memcpy(srcgtag->name, pname, namelen);
894
0
    srcgtag->name[namelen] = 0x00;
895
0
    icc_manager->srcgtag_profile = srcgtag;
896
0
    code = dev_proc(pdev, dev_spec_op)(pdev, gxdso_set_srcgtag, srcgtag, sizeof(srcgtag));
897
0
    return 0;
898
0
}
899
900
/*  This computes the hash code for the ICC data and assigns the code and the
901
    profile to the appropriate member variable in the ICC manager */
902
int
903
gsicc_set_profile(gsicc_manager_t *icc_manager, const char* pname, int namelen,
904
                  gsicc_profile_t defaulttype)
905
12.1M
{
906
12.1M
    cmm_profile_t *icc_profile;
907
12.1M
    cmm_profile_t **manager_default_profile = NULL; /* quite compiler */
908
12.1M
    stream *str;
909
12.1M
    gs_memory_t *mem_gc = icc_manager->memory;
910
12.1M
    int code;
911
12.1M
    int k;
912
12.1M
    int num_comps = 0;
913
12.1M
    gsicc_colorbuffer_t default_space; /* Used to verify that we have the correct type */
914
915
    /* We need to check for the smask swapped profile condition.  If we are in
916
       that state, then any requests for setting the profile will be ignored.
917
       This is valid, since we are in the middle of drawing right now and this
918
       only would occur if we are doing a vmreclaim while in the middle of
919
       soft mask rendering */
920
12.1M
    default_space = gsUNDEFINED;
921
12.1M
    if (icc_manager->smask_profiles !=NULL &&
922
271k
        icc_manager->smask_profiles->swapped == true) {
923
0
            return 0;
924
12.1M
    } else {
925
12.1M
        switch(defaulttype) {
926
2.97M
            case DEFAULT_GRAY:
927
2.97M
                manager_default_profile = &(icc_manager->default_gray);
928
2.97M
                default_space = gsGRAY;
929
2.97M
                num_comps = 1;
930
2.97M
                break;
931
2.97M
            case DEFAULT_RGB:
932
2.97M
                manager_default_profile = &(icc_manager->default_rgb);
933
2.97M
                default_space = gsRGB;
934
2.97M
                num_comps = 3;
935
2.97M
                break;
936
2.97M
            case DEFAULT_CMYK:
937
2.97M
                 manager_default_profile = &(icc_manager->default_cmyk);
938
2.97M
                 default_space = gsCMYK;
939
2.97M
                 num_comps = 4;
940
2.97M
                 break;
941
0
            case NAMED_TYPE:
942
0
                 manager_default_profile = &(icc_manager->device_named);
943
0
                 default_space = gsNAMED;
944
0
                 break;
945
3.26M
            case LAB_TYPE:
946
3.26M
                 manager_default_profile = &(icc_manager->lab_profile);
947
3.26M
                 num_comps = 3;
948
3.26M
                 default_space = gsCIELAB;
949
3.26M
                 break;
950
0
            case DEVICEN_TYPE:
951
0
                manager_default_profile = NULL;
952
0
                default_space = gsNCHANNEL;
953
0
                break;
954
0
            case DEFAULT_NONE:
955
0
            default:
956
0
                return 0;
957
0
                break;
958
12.1M
        }
959
12.1M
    }
960
    /* If it is not NULL then it has already been set. If it is different than
961
       what we already have then we will want to free it.  Since other
962
       gs_gstates could have different default profiles, this is done via reference
963
       counting.  If it is the same as what we already have then we DONT
964
       increment, since that is done when the gs_gstate is duplicated.  It
965
       could be the same, due to a resetting of the user params. To avoid
966
       recreating the profile data, we compare the string names. */
967
12.1M
    if (defaulttype != DEVICEN_TYPE && (*manager_default_profile) != NULL) {
968
        /* Check if this is what we already have.  Also check if it is the
969
           output intent profile.  */
970
2.40M
        icc_profile = *manager_default_profile;
971
2.40M
        if ( namelen == icc_profile->name_length ) {
972
2.40M
            if( memcmp(pname, icc_profile->name, namelen) == 0)
973
2.40M
                return 0;
974
2.40M
        }
975
2.93k
        if (strncmp(icc_profile->name, OI_PROFILE,
976
2.93k
                    icc_profile->name_length) == 0) {
977
0
                return 0;
978
0
        }
979
2.93k
        gsicc_adjust_profile_rc(icc_profile, -1,"gsicc_set_profile");
980
        /* Icky - if the creation of the new profile fails, we end up with a dangling
981
           pointer, or a wrong reference count - so NULL the appropriate entry here
982
         */
983
2.93k
        switch(defaulttype) {
984
564
            case DEFAULT_GRAY:
985
564
                icc_manager->default_gray = NULL;
986
564
                break;
987
1.92k
            case DEFAULT_RGB:
988
1.92k
                icc_manager->default_rgb = NULL;
989
1.92k
                break;
990
445
            case DEFAULT_CMYK:
991
445
                 icc_manager->default_cmyk = NULL;
992
445
                 break;
993
0
            case NAMED_TYPE:
994
0
                 icc_manager->device_named = NULL;
995
0
                 break;
996
0
            case LAB_TYPE:
997
0
                 icc_manager->lab_profile = NULL;
998
0
                 break;
999
0
            default:
1000
0
                break;
1001
2.93k
        }
1002
2.93k
    }
1003
    /* We need to do a special check for DeviceN since we have a linked list of
1004
       profiles and we can have multiple specifications */
1005
9.78M
    if (defaulttype == DEVICEN_TYPE) {
1006
0
        if (icc_manager->device_n != NULL) {
1007
0
            gsicc_devicen_entry_t *current_entry = icc_manager->device_n->head;
1008
0
            for (k = 0; k < icc_manager->device_n->count; k++) {
1009
0
                if (current_entry->iccprofile != NULL) {
1010
0
                    icc_profile = current_entry->iccprofile;
1011
0
                    if (namelen == icc_profile->name_length)
1012
0
                    if (memcmp(pname, icc_profile->name, namelen) == 0)
1013
0
                        return 0;
1014
0
                }
1015
0
                current_entry = current_entry->next;
1016
0
            }
1017
0
        }
1018
        /* An entry was not found.  We need to create a new one to use */
1019
0
        code = gsicc_new_devicen(icc_manager);
1020
0
        if (code < 0)
1021
0
            return code;
1022
0
        manager_default_profile = &(icc_manager->device_n->final->iccprofile);
1023
0
    }
1024
9.78M
    code = gsicc_open_search(pname, namelen, mem_gc, mem_gc->gs_lib_ctx->profiledir,
1025
9.78M
                             mem_gc->gs_lib_ctx->profiledir_len, &str);
1026
9.78M
    if (code < 0)
1027
0
        return code;
1028
9.78M
    if (str != NULL) {
1029
9.78M
        icc_profile = gsicc_profile_new(str, mem_gc, pname, namelen);
1030
        /* Add check so that we detect cases where we are loading a named
1031
           color structure that is not a standard profile type */
1032
9.78M
        if (icc_profile == NULL && defaulttype == NAMED_TYPE) {
1033
            /* Failed to load the named color profile.  Just load the file
1034
               into the buffer as it is.  The profile_handle member
1035
               variable can then be used to hold the named color
1036
               structure that is actually search. This is created later
1037
               when needed. */
1038
0
            char *nameptr;
1039
1040
0
            icc_profile = gsicc_profile_new(NULL, mem_gc, NULL, 0);
1041
0
            if (icc_profile == NULL)
1042
0
                return gs_throw(gs_error_VMerror, "Creation of ICC profile failed");
1043
0
            icc_profile->data_cs = gsNAMED;
1044
0
            code = gsicc_load_namedcolor_buffer(icc_profile, str, mem_gc);
1045
0
            if (code < 0) return gs_throw1(-1, "problems with profile %s", pname);
1046
0
            *manager_default_profile = icc_profile;
1047
0
            nameptr = (char*) gs_alloc_bytes(icc_profile->memory, namelen+1,
1048
0
                                             "gsicc_set_profile");
1049
0
            if (nameptr == NULL)
1050
0
                return gs_throw(gs_error_VMerror, "Insufficient memory for profile name");
1051
0
            memcpy(nameptr, pname, namelen);
1052
0
            nameptr[namelen] = '\0';
1053
0
            icc_profile->name = nameptr;
1054
0
            icc_profile->name_length = namelen;
1055
0
            return 0;  /* Done now, since this is not a standard ICC profile */
1056
0
        }
1057
9.78M
        code = sfclose(str);
1058
9.78M
        if (icc_profile == NULL) {
1059
0
            return gs_throw1(-1, "problems with profile %s",pname);
1060
0
        }
1061
9.78M
         *manager_default_profile = icc_profile;
1062
9.78M
        icc_profile->default_match = defaulttype;
1063
9.78M
        if (defaulttype == LAB_TYPE)
1064
2.44M
            icc_profile->islab = true;
1065
9.78M
        if ( defaulttype == DEVICEN_TYPE ) {
1066
            /* Lets get the name information out of the profile.
1067
               The names are contained in the icSigNamedColor2Tag
1068
               item.  The table is in the A2B0Tag item.
1069
               The names are in the order such that the fastest
1070
               index in the table is the first name */
1071
0
            gsicc_get_devicen_names(icc_profile, icc_manager->memory);
1072
            /* Init this profile now */
1073
0
            code = gsicc_init_profile_info(icc_profile);
1074
0
            if (code < 0) return gs_throw1(-1, "problems with profile %s", pname);
1075
9.78M
        } else {
1076
            /* Delay the loading of the handle buffer until we need the profile.
1077
               But set some basic stuff that we need. Take care of DeviceN
1078
               profile now, since we don't know the number of components etc */
1079
9.78M
            icc_profile->num_comps = num_comps;
1080
9.78M
            if (icc_profile->num_comps > ICC_MAX_CHANNELS) return gs_throw1(-1, "problems with profile %s",pname);
1081
9.78M
            icc_profile->num_comps_out = 3;
1082
9.78M
            gsicc_set_icc_range(&icc_profile);
1083
9.78M
            icc_profile->data_cs = default_space;
1084
9.78M
        }
1085
9.78M
        return 0;
1086
9.78M
    }
1087
0
    return -1;
1088
9.78M
}
1089
1090
/* This is used ONLY for delayed initialization of the "default" ICC profiles
1091
   that are in the ICC manager.  This way we avoid getting these profile handles
1092
   until we actually need them. Note that defaulttype is preset.  These are
1093
   the *only* profiles that are delayed in this manner.  All embedded profiles
1094
   and internally generated profiles have their handles found immediately */
1095
int
1096
gsicc_initialize_default_profile(cmm_profile_t *icc_profile)
1097
505k
{
1098
505k
    gsicc_profile_t defaulttype = icc_profile->default_match;
1099
505k
    gsicc_colorbuffer_t default_space = gsUNDEFINED;
1100
505k
    int num_comps, num_comps_out;
1101
505k
    gs_memory_t *mem = icc_profile->memory;
1102
1103
    /* Get the profile handle if it is not already set */
1104
505k
    if (icc_profile->profile_handle == NULL) {
1105
0
        icc_profile->profile_handle =
1106
0
                        gsicc_get_profile_handle_buffer(icc_profile->buffer,
1107
0
                                                        icc_profile->buffer_size,
1108
0
                                                        mem);
1109
0
        if (icc_profile->profile_handle == NULL) {
1110
0
            return gs_rethrow1(gs_error_VMerror, "allocation of profile %s handle failed",
1111
0
                               icc_profile->name);
1112
0
        }
1113
0
    }
1114
505k
    if (icc_profile->buffer != NULL && icc_profile->hash_is_valid == false) {
1115
        /* Compute the hash code of the profile. */
1116
0
        gsicc_get_icc_buff_hash(icc_profile->buffer, &(icc_profile->hashcode),
1117
0
                                icc_profile->buffer_size);
1118
0
        icc_profile->hash_is_valid = true;
1119
0
    }
1120
505k
    num_comps = icc_profile->num_comps;
1121
505k
    icc_profile->num_comps =
1122
505k
        gscms_get_input_channel_count(icc_profile->profile_handle,
1123
505k
            icc_profile->memory);
1124
505k
    num_comps_out = icc_profile->num_comps_out;
1125
505k
    icc_profile->num_comps_out =
1126
505k
        gscms_get_output_channel_count(icc_profile->profile_handle,
1127
505k
            icc_profile->memory);
1128
505k
    icc_profile->data_cs =
1129
505k
        gscms_get_profile_data_space(icc_profile->profile_handle,
1130
505k
            icc_profile->memory);
1131
505k
    if_debug0m(gs_debug_flag_icc,mem,"[icc] Setting ICC profile in Manager\n");
1132
505k
    switch(defaulttype) {
1133
456k
        case DEFAULT_GRAY:
1134
456k
            if_debug0m(gs_debug_flag_icc,mem,"[icc] Default Gray\n");
1135
456k
            default_space = gsGRAY;
1136
456k
            break;
1137
37.7k
        case DEFAULT_RGB:
1138
37.7k
            if_debug0m(gs_debug_flag_icc,mem,"[icc] Default RGB\n");
1139
37.7k
            default_space = gsRGB;
1140
37.7k
            break;
1141
8.78k
        case DEFAULT_CMYK:
1142
8.78k
            if_debug0m(gs_debug_flag_icc,mem,"[icc] Default CMYK\n");
1143
8.78k
            default_space = gsCMYK;
1144
8.78k
             break;
1145
0
        case NAMED_TYPE:
1146
0
            if_debug0m(gs_debug_flag_icc,mem,"[icc] Named Color\n");
1147
0
            break;
1148
743
        case LAB_TYPE:
1149
743
            if_debug0m(gs_debug_flag_icc,mem,"[icc] CIELAB Profile\n");
1150
743
            break;
1151
0
        case DEVICEN_TYPE:
1152
0
            if_debug0m(gs_debug_flag_icc,mem,"[icc] DeviceN Profile\n");
1153
0
            break;
1154
0
        case DEFAULT_NONE:
1155
2.20k
        default:
1156
2.20k
            return 0;
1157
0
            break;
1158
505k
    }
1159
505k
    if_debug1m(gs_debug_flag_icc,mem,"[icc] name = %s\n", icc_profile->name);
1160
503k
    if_debug1m(gs_debug_flag_icc,mem,"[icc] num_comps = %d\n", icc_profile->num_comps);
1161
    /* Check that we have the proper color space for the ICC
1162
       profiles that can be externally set */
1163
503k
    if (default_space != gsUNDEFINED ||
1164
743
        num_comps != icc_profile->num_comps ||
1165
502k
        num_comps_out != icc_profile->num_comps_out) {
1166
502k
        if (icc_profile->data_cs != default_space) {
1167
580
            return gs_rethrow(-1, "A default profile has an incorrect color space");
1168
580
        }
1169
502k
    }
1170
503k
    return 0;
1171
503k
}
1172
1173
/* This is used to get the profile handle given a file name  */
1174
cmm_profile_t*
1175
gsicc_get_profile_handle_file(const char* pname, int namelen, gs_memory_t *mem)
1176
3.87k
{
1177
3.87k
    cmm_profile_t *result;
1178
3.87k
    stream* str;
1179
3.87k
    int code;
1180
1181
    /* First see if we can get the stream. */
1182
3.87k
    code = gsicc_open_search(pname, namelen, mem, mem->gs_lib_ctx->profiledir,
1183
3.87k
        mem->gs_lib_ctx->profiledir_len, &str);
1184
3.87k
    if (code < 0 || str == NULL) {
1185
0
        gs_throw(gs_error_VMerror, "Creation of ICC profile failed");
1186
0
        return NULL;
1187
0
    }
1188
3.87k
    result = gsicc_profile_new(str, mem, pname, namelen);
1189
3.87k
    code = sfclose(str);
1190
3.87k
    if (result == NULL) {
1191
0
        gs_throw(gs_error_VMerror, "Creation of ICC profile failed");
1192
0
        return NULL;
1193
0
    }
1194
3.87k
    code = gsicc_init_profile_info(result);
1195
3.87k
    if (code < 0) {
1196
0
        gs_throw(gs_error_VMerror, "Creation of ICC profile failed");
1197
0
        return NULL;
1198
0
    }
1199
3.87k
    return result;
1200
3.87k
}
1201
1202
/* Given that we already have a profile in a buffer (e.g. generated from a PS object)
1203
   this gets the handle and initializes the various member variables that we need */
1204
int
1205
gsicc_init_profile_info(cmm_profile_t *profile)
1206
3.92k
{
1207
3.92k
    int k;
1208
1209
    /* Get the profile handle */
1210
3.92k
    profile->profile_handle =
1211
3.92k
        gsicc_get_profile_handle_buffer(profile->buffer,
1212
3.92k
                                        profile->buffer_size,
1213
3.92k
                                        profile->memory);
1214
3.92k
    if (profile->profile_handle == NULL)
1215
0
        return -1;
1216
1217
    /* Compute the hash code of the profile. */
1218
3.92k
    gsicc_get_icc_buff_hash(profile->buffer, &(profile->hashcode),
1219
3.92k
                            profile->buffer_size);
1220
3.92k
    profile->hash_is_valid = true;
1221
3.92k
    profile->default_match = DEFAULT_NONE;
1222
3.92k
    profile->num_comps = gscms_get_input_channel_count(profile->profile_handle,
1223
3.92k
        profile->memory);
1224
3.92k
    profile->num_comps_out = gscms_get_output_channel_count(profile->profile_handle,
1225
3.92k
        profile->memory);
1226
3.92k
    profile->data_cs = gscms_get_profile_data_space(profile->profile_handle,
1227
3.92k
        profile->memory);
1228
1229
    /* Initialize the range to default values */
1230
8.80k
    for ( k = 0; k < profile->num_comps; k++) {
1231
4.87k
        profile->Range.ranges[k].rmin = 0.0;
1232
4.87k
        profile->Range.ranges[k].rmax = 1.0;
1233
4.87k
    }
1234
3.92k
    return 0;
1235
3.92k
}
1236
1237
/* This is used to try to find the specified or default ICC profiles */
1238
/* This is where we would enhance the directory searching to use a   */
1239
/* list of paths separated by ':' (unix) or ';' Windows              */
1240
int
1241
gsicc_open_search(const char* pname, int namelen, gs_memory_t *mem_gc,
1242
                  const char* dirname, int dirlen, stream**strp)
1243
10.4M
{
1244
10.4M
    char *buffer;
1245
10.4M
    stream* str;
1246
10.4M
    char CFileName[gp_file_name_sizeof];
1247
1248
    /* Check if we need to prepend the file name  */
1249
10.4M
    if ( dirname != NULL) {
1250
        /* If this fails, we will still try the file by itself and with
1251
           %rom% since someone may have left a space some of the spaces
1252
           as our defaults, even if they defined the directory to use.
1253
           This will occur only after searching the defined directory.
1254
           A warning is noted.  */
1255
10.4M
        buffer = (char *) gs_alloc_bytes(mem_gc, namelen + dirlen + 1,
1256
10.4M
                                     "gsicc_open_search");
1257
10.4M
        if (buffer == NULL)
1258
0
            return_error(gs_error_VMerror);
1259
10.4M
        memcpy(buffer, dirname, dirlen);
1260
10.4M
        memcpy(buffer + dirlen, pname, namelen);
1261
        /* Just to make sure we were null terminated */
1262
10.4M
        buffer[namelen + dirlen] = '\0';
1263
1264
10.4M
        str = sfopen(buffer, "r", mem_gc);
1265
10.4M
        gs_free_object(mem_gc, buffer, "gsicc_open_search");
1266
10.4M
        if (str != NULL) {
1267
10.4M
            *strp = str;
1268
10.4M
            return 0;
1269
10.4M
        }
1270
10.4M
    }
1271
1272
    /* First just try it like it is */
1273
2.93k
    if (namelen + 1 > gp_file_name_sizeof)
1274
0
        return_error(gs_error_ioerror);
1275
2.93k
    memcpy(CFileName, pname, namelen);
1276
2.93k
    CFileName[namelen] = 0x00;
1277
1278
2.93k
    str = sfopen(CFileName, "r", mem_gc);
1279
2.93k
    if (str != NULL) {
1280
2.93k
        *strp = str;
1281
2.93k
        return 0;
1282
2.93k
    }
1283
1284
    /* If that fails, try %rom% */ /* FIXME: Not sure this is needed or correct */
1285
                                   /* A better approach might be to have built in defaults */
1286
0
    buffer = (char *) gs_alloc_bytes(mem_gc, 1 + namelen +
1287
0
                        strlen(DEFAULT_DIR_ICC),"gsicc_open_search");
1288
0
    if (buffer == NULL)
1289
0
        return_error(gs_error_VMerror);
1290
0
    strcpy(buffer, DEFAULT_DIR_ICC);
1291
0
    memcpy(buffer + strlen(DEFAULT_DIR_ICC), pname, namelen);
1292
    /* Just to make sure we were null terminated */
1293
0
    buffer[namelen + strlen(DEFAULT_DIR_ICC)] = '\0';
1294
0
    str = sfopen(buffer, "r", mem_gc);
1295
0
    gs_free_object(mem_gc, buffer, "gsicc_open_search");
1296
0
    if (str == NULL) {
1297
0
        gs_warn1("Could not find %s ",pname);
1298
0
    }
1299
0
    *strp = str;
1300
0
    return 0;
1301
0
}
1302
1303
/* Free source object icc array structure.  */
1304
static void
1305
rc_free_srcgtag_profile(gs_memory_t * mem, void *ptr_in, client_name_t cname)
1306
0
{
1307
0
    cmm_srcgtag_profile_t *srcgtag_profile = (cmm_srcgtag_profile_t *)ptr_in;
1308
0
    int k;
1309
0
    gs_memory_t *mem_nongc =  srcgtag_profile->memory;
1310
1311
0
    if (srcgtag_profile->rc.ref_count <= 1 ) {
1312
        /* Decrement any profiles. */
1313
0
        for (k = 0; k < NUM_SOURCE_PROFILES; k++) {
1314
0
            if (srcgtag_profile->gray_profiles[k] != NULL) {
1315
0
                gsicc_adjust_profile_rc(srcgtag_profile->gray_profiles[k], -1,
1316
0
                    "rc_free_srcgtag_profile(gray)");
1317
0
            }
1318
0
            if (srcgtag_profile->rgb_profiles[k] != NULL) {
1319
0
                gsicc_adjust_profile_rc(srcgtag_profile->rgb_profiles[k], -1,
1320
0
                             "rc_free_srcgtag_profile(rgb)");
1321
0
            }
1322
0
            if (srcgtag_profile->cmyk_profiles[k] != NULL) {
1323
0
                gsicc_adjust_profile_rc(srcgtag_profile->cmyk_profiles[k], -1,
1324
0
                             "rc_free_srcgtag_profile(cmyk)");
1325
0
            }
1326
0
            if (srcgtag_profile->color_warp_profile != NULL) {
1327
0
                gsicc_adjust_profile_rc(srcgtag_profile->color_warp_profile, -1,
1328
0
                             "rc_free_srcgtag_profile(warp)");
1329
0
            }
1330
0
        }
1331
0
        gs_free_object(mem_nongc, srcgtag_profile->name, "rc_free_srcgtag_profile");
1332
0
        gs_free_object(mem_nongc, srcgtag_profile, "rc_free_srcgtag_profile");
1333
0
    }
1334
0
}
1335
1336
/* Allocate source object icc structure. */
1337
static cmm_srcgtag_profile_t*
1338
gsicc_new_srcgtag_profile(gs_memory_t *memory)
1339
0
{
1340
0
    cmm_srcgtag_profile_t *result;
1341
0
    int k;
1342
1343
0
    result = (cmm_srcgtag_profile_t *) gs_alloc_bytes(memory->non_gc_memory,
1344
0
                                            sizeof(cmm_srcgtag_profile_t),
1345
0
                                            "gsicc_new_srcgtag_profile");
1346
0
    if (result == NULL)
1347
0
        return NULL;
1348
0
    result->memory = memory->non_gc_memory;
1349
1350
0
    for (k = 0; k < NUM_SOURCE_PROFILES; k++) {
1351
0
        result->rgb_profiles[k] = NULL;
1352
0
        result->cmyk_profiles[k] = NULL;
1353
0
        result->gray_profiles[k] = NULL;
1354
0
        result->gray_rend_cond[k].black_point_comp = gsBPNOTSPECIFIED;
1355
0
        result->gray_rend_cond[k].rendering_intent = gsRINOTSPECIFIED;
1356
0
        result->gray_rend_cond[k].override_icc = false;
1357
0
        result->gray_rend_cond[k].preserve_black = gsBKPRESNOTSPECIFIED;
1358
0
        result->gray_rend_cond[k].cmm = gsCMM_DEFAULT;
1359
0
        result->rgb_rend_cond[k].black_point_comp = gsBPNOTSPECIFIED;
1360
0
        result->rgb_rend_cond[k].rendering_intent = gsRINOTSPECIFIED;
1361
0
        result->rgb_rend_cond[k].override_icc = false;
1362
0
        result->rgb_rend_cond[k].preserve_black = gsBKPRESNOTSPECIFIED;
1363
0
        result->rgb_rend_cond[k].cmm = gsCMM_DEFAULT;
1364
0
        result->cmyk_rend_cond[k].black_point_comp = gsBPNOTSPECIFIED;
1365
0
        result->cmyk_rend_cond[k].rendering_intent = gsRINOTSPECIFIED;
1366
0
        result->cmyk_rend_cond[k].override_icc = false;
1367
0
        result->cmyk_rend_cond[k].preserve_black = gsBKPRESNOTSPECIFIED;
1368
0
        result->cmyk_rend_cond[k].cmm = gsCMM_DEFAULT;
1369
0
    }
1370
0
    result->color_warp_profile = NULL;
1371
0
    result->name = NULL;
1372
0
    result->name_length = 0;
1373
0
    rc_init_free(result, memory->non_gc_memory, 1, rc_free_srcgtag_profile);
1374
0
    return result;
1375
0
}
1376
1377
static void
1378
gsicc_free_spotnames(gsicc_namelist_t *spotnames, gs_memory_t * mem)
1379
0
{
1380
0
    int k;
1381
0
    gsicc_colorname_t *curr_name, *next_name;
1382
1383
0
    curr_name = spotnames->head;
1384
0
    for (k = 0; k < spotnames->count; k++) {
1385
0
        next_name = curr_name->next;
1386
        /* Free the name */
1387
0
        gs_free_object(mem, curr_name->name, "gsicc_free_spotnames");
1388
        /* Free the name structure */
1389
0
        gs_free_object(mem, curr_name, "gsicc_free_spotnames");
1390
0
        curr_name = next_name;
1391
0
    }
1392
0
    if (spotnames->color_map != NULL) {
1393
0
        gs_free_object(mem, spotnames->color_map, "gsicc_free_spotnames");
1394
0
    }
1395
0
    if (spotnames->name_str != NULL) {
1396
0
        gs_free_object(mem, spotnames->name_str, "gsicc_free_spotnames");
1397
0
    }
1398
0
}
1399
1400
/* Free device icc array structure.  */
1401
static void
1402
rc_free_profile_array(gs_memory_t * mem, void *ptr_in, client_name_t cname)
1403
2.14M
{
1404
2.14M
    cmm_dev_profile_t *icc_struct = (cmm_dev_profile_t *)ptr_in;
1405
2.14M
    int k;
1406
2.14M
    gs_memory_t *mem_nongc =  icc_struct->memory;
1407
1408
2.14M
    if (icc_struct->rc.ref_count <= 1 ) {
1409
        /* Decrement any profiles. */
1410
10.7M
        for (k = 0; k < NUM_DEVICE_PROFILES; k++) {
1411
8.56M
            if (icc_struct->device_profile[k] != NULL) {
1412
1.53M
                if_debug1m(gs_debug_flag_icc, mem_nongc,
1413
1.53M
                           "[icc] Releasing device profile %d\n", k);
1414
1.53M
                gsicc_adjust_profile_rc(icc_struct->device_profile[k], -1,
1415
1.53M
                             "rc_free_profile_array");
1416
1.53M
            }
1417
8.56M
        }
1418
2.14M
        if (icc_struct->link_profile != NULL) {
1419
0
            if_debug0m(gs_debug_flag_icc,mem_nongc,"[icc] Releasing link profile\n");
1420
0
            gsicc_adjust_profile_rc(icc_struct->link_profile, -1, "rc_free_profile_array");
1421
0
        }
1422
2.14M
        if (icc_struct->proof_profile != NULL) {
1423
0
            if_debug0m(gs_debug_flag_icc,mem_nongc,"[icc] Releasing proof profile\n");
1424
0
            gsicc_adjust_profile_rc(icc_struct->proof_profile, -1, "rc_free_profile_array");
1425
0
        }
1426
2.14M
        if (icc_struct->oi_profile != NULL) {
1427
0
            if_debug0m(gs_debug_flag_icc,mem_nongc,"[icc] Releasing oi profile\n");
1428
0
            gsicc_adjust_profile_rc(icc_struct->oi_profile, -1, "rc_free_profile_array");
1429
0
        }
1430
2.14M
        if (icc_struct->postren_profile != NULL) {
1431
0
            if_debug0m(gs_debug_flag_icc, mem_nongc, "[icc] Releasing postren profile\n");
1432
0
            gsicc_adjust_profile_rc(icc_struct->postren_profile, -1, "rc_free_profile_array");
1433
0
        }
1434
2.14M
        if (icc_struct->blend_profile != NULL) {
1435
0
            if_debug0m(gs_debug_flag_icc, mem_nongc, "[icc] Releasing blend profile\n");
1436
0
            gsicc_adjust_profile_rc(icc_struct->blend_profile, -1, "rc_free_profile_array");
1437
0
        }
1438
2.14M
        if (icc_struct->spotnames != NULL) {
1439
0
            if_debug0m(gs_debug_flag_icc, mem_nongc, "[icc] Releasing spotnames\n");
1440
            /* Free the linked list in this object */
1441
0
            gsicc_free_spotnames(icc_struct->spotnames, mem_nongc);
1442
            /* Free the main object */
1443
0
            gs_free_object(mem_nongc, icc_struct->spotnames, "rc_free_profile_array");
1444
0
        }
1445
2.14M
        if_debug0m(gs_debug_flag_icc,mem_nongc,"[icc] Releasing device profile struct\n");
1446
2.14M
        gs_free_object(mem_nongc, icc_struct, "rc_free_profile_array");
1447
2.14M
    }
1448
2.14M
}
1449
1450
/* Allocate device icc structure. The actual profiles are in this structure */
1451
cmm_dev_profile_t*
1452
gsicc_new_device_profile_array(gx_device *dev)
1453
2.14M
{
1454
2.14M
    cmm_dev_profile_t *result;
1455
2.14M
    int k;
1456
2.14M
    gs_memory_t *memory = dev->memory;
1457
1458
2.14M
    if_debug0m(gs_debug_flag_icc,memory,"[icc] Allocating device profile struct\n");
1459
2.14M
    result = (cmm_dev_profile_t *) gs_alloc_bytes(memory->non_gc_memory,
1460
2.14M
                                            sizeof(cmm_dev_profile_t),
1461
2.14M
                                            "gsicc_new_device_profile_array");
1462
2.14M
    if (result == NULL)
1463
0
        return NULL;
1464
2.14M
    result->memory = memory->non_gc_memory;
1465
1466
10.7M
    for (k = 0; k < NUM_DEVICE_PROFILES; k++) {
1467
8.56M
        result->device_profile[k] = NULL;
1468
8.56M
        result->rendercond[k].rendering_intent = gsRINOTSPECIFIED;
1469
8.56M
        result->rendercond[k].black_point_comp = gsBPNOTSPECIFIED;
1470
8.56M
        result->rendercond[k].override_icc = false;
1471
8.56M
        result->rendercond[k].preserve_black = gsBKPRESNOTSPECIFIED;
1472
8.56M
        result->rendercond[k].graphics_type_tag = GS_UNKNOWN_TAG;
1473
8.56M
        result->rendercond[k].cmm = gsCMM_DEFAULT;
1474
8.56M
    }
1475
2.14M
    result->proof_profile = NULL;
1476
2.14M
    result->link_profile = NULL;
1477
2.14M
    result->postren_profile = NULL;
1478
2.14M
    result->blend_profile = NULL;
1479
2.14M
    result->oi_profile = NULL;
1480
2.14M
    result->spotnames = NULL;
1481
2.14M
    result->devicegraytok = true;  /* Default is to map gray to pure K */
1482
2.14M
    result->graydetection = false;
1483
2.14M
    result->pageneutralcolor = false;
1484
2.14M
    result->usefastcolor = false;  /* Default is to not use fast color */
1485
2.14M
    result->blacktext = false;
1486
2.14M
    result->blackvector = false;
1487
2.14M
    result->blackthresholdL = 90.0F;
1488
2.14M
    result->blackthresholdC = 0.0F;
1489
2.14M
    result->prebandthreshold = true;
1490
2.14M
    result->supports_devn = false;
1491
2.14M
    result->overprint_control = gs_overprint_control_enable;  /* Default overprint if the device can */
1492
2.14M
    rc_init_free(result, memory->non_gc_memory, 1, rc_free_profile_array);
1493
2.14M
    return result;
1494
2.14M
}
1495
1496
int
1497
gsicc_set_device_blackpreserve(gx_device *dev, gsicc_blackpreserve_t blackpreserve,
1498
                                gsicc_profile_types_t profile_type)
1499
7.82M
{
1500
7.82M
    int code;
1501
7.82M
    cmm_dev_profile_t *profile_struct;
1502
1503
    /* Although device methods should not be NULL, they are not completely filled in until
1504
     * gx_device_fill_in_procs is called, and its possible for us to get here before this
1505
     * happens, so we *must* make sure the method is not NULL before we use it.
1506
     */
1507
7.82M
    if (dev_proc(dev, get_profile) == NULL) {
1508
1.33M
        profile_struct = dev->icc_struct;
1509
6.49M
    } else {
1510
6.49M
        code = dev_proc(dev, get_profile)(dev,  &profile_struct);
1511
6.49M
        if (code < 0)
1512
0
            return code;
1513
6.49M
    }
1514
7.82M
    if (profile_struct ==  NULL)
1515
0
        return 0;
1516
7.82M
    profile_struct->rendercond[profile_type].preserve_black = blackpreserve;
1517
7.82M
    return 0;
1518
7.82M
}
1519
1520
int
1521
gsicc_set_device_profile_intent(gx_device *dev, gsicc_rendering_intents_t intent,
1522
                                gsicc_profile_types_t profile_type)
1523
7.82M
{
1524
7.82M
    int code;
1525
7.82M
    cmm_dev_profile_t *profile_struct;
1526
1527
    /* Although device methods should not be NULL, they are not completely filled in until
1528
     * gx_device_fill_in_procs is called, and its possible for us to get here before this
1529
     * happens, so we *must* make sure the method is not NULL before we use it.
1530
     */
1531
7.82M
    if (dev_proc(dev, get_profile) == NULL) {
1532
1.33M
        profile_struct = dev->icc_struct;
1533
6.49M
    } else {
1534
6.49M
        code = dev_proc(dev, get_profile)(dev,  &profile_struct);
1535
6.49M
        if (code < 0)
1536
0
            return code;
1537
6.49M
    }
1538
7.82M
    if (profile_struct ==  NULL)
1539
0
        return 0;
1540
7.82M
    profile_struct->rendercond[profile_type].rendering_intent = intent;
1541
7.82M
    return 0;
1542
7.82M
}
1543
1544
int
1545
gsicc_set_device_blackptcomp(gx_device *dev, gsicc_blackptcomp_t blackptcomp,
1546
                                gsicc_profile_types_t profile_type)
1547
7.82M
{
1548
7.82M
    int code = 0;
1549
7.82M
    cmm_dev_profile_t *profile_struct;
1550
1551
    /* Although device methods should not be NULL, they are not completely filled in until
1552
     * gx_device_fill_in_procs is called, and its possible for us to get here before this
1553
     * happens, so we *must* make sure the method is not NULL before we use it.
1554
     */
1555
7.82M
    if (dev_proc(dev, get_profile) == NULL) {
1556
1.33M
        profile_struct = dev->icc_struct;
1557
6.49M
    } else {
1558
6.49M
        code = dev_proc(dev, get_profile)(dev,  &profile_struct);
1559
6.49M
    }
1560
7.82M
    if (profile_struct ==  NULL)
1561
0
        return 0;
1562
7.82M
    profile_struct->rendercond[profile_type].black_point_comp = blackptcomp;
1563
7.82M
    return code;
1564
7.82M
}
1565
1566
/* This is used to set up the equivalent cmyk colors for the spots that may
1567
   exist in an output DeviceN profile.  We do this by faking a new separation
1568
   space for each one */
1569
int
1570
gsicc_set_devicen_equiv_colors(gx_device *dev, const gs_gstate * pgs,
1571
                               cmm_profile_t *profile)
1572
0
{
1573
0
    gs_gstate temp_state = *((gs_gstate*)pgs);
1574
0
    gs_color_space *pcspace = gs_cspace_alloc(pgs->memory->non_gc_memory,
1575
0
                                              &gs_color_space_type_ICC);
1576
0
    if (pcspace == NULL)
1577
0
        return gs_throw(gs_error_VMerror, "Insufficient memory for devn equiv colors");
1578
0
    pcspace->cmm_icc_profile_data = profile;
1579
0
    temp_state.color[0].color_space = pcspace;
1580
0
    return dev_proc(dev, update_spot_equivalent_colors)(dev, &temp_state, pcspace);
1581
0
}
1582
1583
0
#define DEFAULT_ICC_PROCESS "Cyan, Magenta, Yellow, Black,"
1584
0
#define DEFAULT_ICC_PROCESS_LENGTH 30
1585
#define DEFAULT_ICC_COLORANT_NAME "ICC_COLOR_"
1586
0
#define DEFAULT_ICC_COLORANT_LENGTH 12
1587
/* allow at most 16 colorants */
1588
/* This sets the colorants structure up in the device profile for when
1589
   we are dealing with DeviceN type output profiles.  Note
1590
   that this feature is only used with the tiffsep and psdcmyk devices.
1591
   If name_str is null it will use default names for the colorants */
1592
int
1593
gsicc_set_device_profile_colorants(gx_device *dev, char *name_str)
1594
0
{
1595
0
    int code;
1596
0
    cmm_dev_profile_t *profile_struct;
1597
0
    gsicc_colorname_t *name_entry;
1598
0
    gsicc_colorname_t **curr_entry;
1599
0
    gs_memory_t *mem;
1600
0
    char *temp_ptr, *last = NULL;
1601
0
    int done;
1602
0
    gsicc_namelist_t *spot_names;
1603
0
    char *pch;
1604
0
    int str_len;
1605
0
    int k;
1606
0
    bool free_str = false;
1607
1608
0
    code = dev_proc(dev, get_profile)((gx_device *)dev, &profile_struct);
1609
0
    if (profile_struct != NULL) {
1610
0
        if (name_str == NULL) {
1611
            /* Create a default name string that we can use */
1612
0
            int total_len;
1613
0
            int kk;
1614
0
            int num_comps = profile_struct->device_profile[GS_DEFAULT_DEVICE_PROFILE]->num_comps;
1615
0
            char temp_str[DEFAULT_ICC_COLORANT_LENGTH+2];
1616
1617
            /* If names are already set then we do not want to set default ones */
1618
0
            if (profile_struct->spotnames != NULL) {
1619
                /* Check if we have at least as many spot names
1620
                   as there are channels in the proFfile */
1621
0
                if (num_comps > profile_struct->spotnames->count) {
1622
0
                    gs_warn("ICC profile colorant names count insufficient");
1623
0
                    return_error(gs_error_rangecheck);
1624
0
                } else
1625
0
                    return 0;
1626
0
            }
1627
1628
0
            free_str = true;
1629
            /* Assume first 4 are CMYK */
1630
0
            total_len = ((DEFAULT_ICC_COLORANT_LENGTH + 1) * (num_comps-4)) +
1631
0
                        DEFAULT_ICC_PROCESS_LENGTH - 1;  /* -1 due to no comma at end */
1632
0
            name_str = (char*) gs_alloc_bytes(dev->memory, total_len+1,
1633
0
                                               "gsicc_set_device_profile_colorants");
1634
0
            if (name_str == NULL)
1635
0
                return gs_throw(gs_error_VMerror, "Insufficient memory for colorant name");
1636
0
            gs_snprintf(name_str, total_len+1, DEFAULT_ICC_PROCESS);
1637
0
            for (kk = 0; kk < num_comps-5; kk++) {
1638
0
                gs_snprintf(temp_str,sizeof(temp_str),"ICC_COLOR_%d,",kk);
1639
0
                strcat(name_str,temp_str);
1640
0
            }
1641
            /* Last one no comma */
1642
0
            gs_snprintf(temp_str,sizeof(temp_str),"ICC_COLOR_%d",kk);
1643
0
            strcat(name_str,temp_str);
1644
0
        }
1645
0
        str_len = strlen(name_str);
1646
0
        if (profile_struct->spotnames != NULL &&
1647
0
            profile_struct->spotnames->name_str != NULL &&
1648
0
            strlen(profile_struct->spotnames->name_str) == str_len) {
1649
            /* Here we check if the names are the same */
1650
0
            if (strncmp(name_str, profile_struct->spotnames->name_str, str_len) == 0) {
1651
0
                if (free_str)
1652
0
                    gs_free_object(dev->memory, name_str,
1653
0
                                            "gsicc_set_device_profile_colorants");
1654
0
                return 0;
1655
0
            }
1656
0
        }
1657
0
        mem = dev->memory->non_gc_memory;
1658
        /* We need to free the existing one if there was one */
1659
0
        if (profile_struct->spotnames != NULL) {
1660
            /* Free the linked list in this object */
1661
0
            gsicc_free_spotnames(profile_struct->spotnames, mem);
1662
0
            profile_struct->spotnames = NULL;
1663
            /* Free the main object */
1664
0
            gs_free_object(mem, profile_struct->spotnames,
1665
0
                           "gsicc_set_device_profile_colorants");
1666
0
        }
1667
        /* Allocate structure for managing names */
1668
0
        spot_names = gsicc_new_namelist(mem);
1669
0
        if (spot_names == 0)
1670
0
            return gs_throw(gs_error_VMerror, "Insufficient memory for spot name");
1671
1672
0
        profile_struct->spotnames = spot_names;
1673
0
        spot_names->name_str = (char*) gs_alloc_bytes(mem, str_len+1,
1674
0
                                               "gsicc_set_device_profile_colorants");
1675
0
        if (spot_names->name_str == NULL)
1676
0
            return gs_throw(gs_error_VMerror, "Insufficient memory for spot name");
1677
0
        memcpy(spot_names->name_str, name_str, strlen(name_str));
1678
0
        spot_names->name_str[str_len] = 0;
1679
0
        curr_entry = &(spot_names->head);
1680
         /* Go ahead and tokenize now */
1681
0
        pch = gs_strtok(name_str, ",", &last);
1682
1683
0
        while (pch != NULL) {
1684
0
            if (spot_names->count == GS_CLIENT_COLOR_MAX_COMPONENTS)
1685
0
                return gs_throw(gs_error_rangecheck, "Too many spot names");
1686
1687
0
            temp_ptr = pch;
1688
0
            done = 0;
1689
            /* Remove any leading spaces */
1690
0
            while (!done) {
1691
0
                if (*temp_ptr == 0x20) {
1692
0
                    temp_ptr++;
1693
0
                } else {
1694
0
                    done = 1;
1695
0
                }
1696
0
            }
1697
            /* Allocate a new name object */
1698
0
            name_entry = gsicc_new_colorname(mem);
1699
0
            if (name_entry == NULL)
1700
0
                return gs_throw(gs_error_VMerror, "Insufficient memory for spot name");
1701
            /* Set our current entry to this one */
1702
0
            *curr_entry = name_entry;
1703
0
            spot_names->count += 1;
1704
0
            name_entry->length = strlen(temp_ptr);
1705
0
            name_entry->name = (char *) gs_alloc_bytes(mem, name_entry->length,
1706
0
                                        "gsicc_set_device_profile_colorants");
1707
0
            if (name_entry->name == NULL)
1708
0
                return gs_throw(gs_error_VMerror, "Insufficient memory for spot name");
1709
0
            memcpy(name_entry->name, temp_ptr, name_entry->length);
1710
            /* Get the next entry location */
1711
0
            curr_entry = &((*curr_entry)->next);
1712
0
            pch = gs_strtok(NULL, ",", &last);
1713
0
        }
1714
        /* Create the color map.  Query the device to find out where these
1715
           colorants are located.   It is possible that the device may
1716
           not be opened yet.  In which case, we need to make sure that
1717
           when it is opened that it checks this entry and gets itself
1718
           properly initialized if it is a separation device. */
1719
0
        spot_names->color_map =
1720
0
            (gs_devicen_color_map*) gs_alloc_bytes(mem,
1721
0
                                                   sizeof(gs_devicen_color_map),
1722
0
                                                   "gsicc_set_device_profile_colorants");
1723
0
        if (spot_names->color_map == NULL)
1724
0
            return gs_throw(gs_error_VMerror, "Insufficient memory for spot color map");
1725
0
        spot_names->color_map->num_colorants = spot_names->count;
1726
0
        spot_names->color_map->num_components = spot_names->count;
1727
1728
0
        name_entry = spot_names->head;
1729
0
        for (k = 0; k < spot_names->count; k++) {
1730
0
            int colorant_number = (*dev_proc(dev, get_color_comp_index))
1731
0
                    (dev, (const char *)name_entry->name, name_entry->length,
1732
0
                     SEPARATION_NAME);
1733
0
            name_entry = name_entry->next;
1734
0
            spot_names->color_map->color_map[k] = colorant_number;
1735
0
        }
1736
        /* We need to set the equivalent CMYK color for this colorant.  This is
1737
           done by faking out the update spot equivalent call with a special
1738
           gs_gstate and color space that makes it seem like the
1739
           spot color is a separation color space.  Unfortunately, we need the
1740
           graphic state to do this so we save it for later when we try to do
1741
           our first mapping.  We then use this flag to know if we did it yet */
1742
0
        spot_names->equiv_cmyk_set = false;
1743
0
        if (free_str)
1744
0
            gs_free_object(dev->memory, name_str,
1745
0
                           "gsicc_set_device_profile_colorants");
1746
0
    }
1747
0
    return code;
1748
0
}
1749
1750
/* This sets the device profiles. If the device does not have a defined
1751
   profile, then a default one is selected. */
1752
int
1753
gsicc_init_device_profile_struct(gx_device * dev,
1754
                                 char *profile_name,
1755
                                 gsicc_profile_types_t profile_type)
1756
527k
{
1757
527k
    int code;
1758
527k
    cmm_profile_t *curr_profile;
1759
527k
    cmm_dev_profile_t *profile_struct;
1760
1761
    /* See if the device has a profile structure.  If it does, then do a
1762
       check to see if the profile that we are trying to set is already
1763
       set and the same.  If it is not, then we need to free it and then
1764
       reset. */
1765
527k
    profile_struct = dev->icc_struct;
1766
527k
    if (profile_struct != NULL) {
1767
        /* Get the profile of interest */
1768
181k
        if (profile_type < gsPROOFPROFILE) {
1769
181k
            curr_profile = profile_struct->device_profile[profile_type];
1770
181k
        } else {
1771
            /* The proof, link profile or post render */
1772
0
            if (profile_type == gsPROOFPROFILE) {
1773
0
                curr_profile = profile_struct->proof_profile;
1774
0
            } else if (profile_type == gsLINKPROFILE) {
1775
0
                curr_profile = profile_struct->link_profile;
1776
0
            } else if (profile_type == gsPRPROFILE) {
1777
0
                curr_profile = profile_struct->postren_profile;
1778
0
            } else
1779
0
                curr_profile = profile_struct->blend_profile;
1780
1781
0
        }
1782
        /* See if we have the same profile in this location */
1783
181k
        if (curr_profile != NULL) {
1784
            /* There is something there now.  See if what we have coming in
1785
               is different and it is not the output intent.  In this  */
1786
65.1k
            if (profile_name != NULL && curr_profile->name != NULL) {
1787
65.1k
                if (strncmp(curr_profile->name, profile_name,
1788
65.1k
                            strlen(profile_name)) != 0 &&
1789
0
                    strncmp(curr_profile->name, OI_PROFILE,
1790
0
                            strlen(curr_profile->name)) != 0) {
1791
                    /* A change in the profile.  rc decrement this one as it
1792
                       will be replaced */
1793
0
                    gsicc_adjust_profile_rc(curr_profile, -1, "gsicc_init_device_profile_struct");
1794
                    /* Icky - if the creation of the new profile fails, we end up with a dangling
1795
                       pointer, or a wrong reference count - so NULL the appropriate entry here
1796
                     */
1797
0
                    if (profile_type < gsPROOFPROFILE) {
1798
0
                        profile_struct->device_profile[profile_type] = NULL;
1799
0
                    } else {
1800
                        /* The proof, link profile or post render */
1801
0
                        if (profile_type == gsPROOFPROFILE) {
1802
0
                            profile_struct->proof_profile = NULL;
1803
0
                        } else if (profile_type == gsLINKPROFILE) {
1804
0
                            profile_struct->link_profile = NULL;
1805
0
                        } else if (profile_type == gsPRPROFILE) {
1806
0
                            profile_struct->postren_profile = NULL;
1807
0
                        } else
1808
0
                            profile_struct->blend_profile = NULL;
1809
1810
0
                    }
1811
1812
65.1k
                } else {
1813
                    /* Nothing to change.  It was either the same or is the
1814
                       output intent */
1815
65.1k
                    return 0;
1816
65.1k
                }
1817
65.1k
            }
1818
65.1k
        }
1819
345k
    } else {
1820
        /* We have no profile structure at all. Allocate the structure in
1821
           non-GC memory.  */
1822
345k
        dev->icc_struct = gsicc_new_device_profile_array(dev);
1823
345k
        profile_struct = dev->icc_struct;
1824
345k
        if (profile_struct == NULL)
1825
0
            return_error(gs_error_VMerror);
1826
345k
    }
1827
    /* Either use the incoming or a default */
1828
461k
    if (profile_name == NULL) {
1829
441k
        int has_tags = device_encodes_tags(dev);
1830
441k
        profile_name =
1831
441k
            (char *) gs_alloc_bytes(dev->memory,
1832
441k
                                    MAX_DEFAULT_ICC_LENGTH,
1833
441k
                                    "gsicc_init_device_profile_struct");
1834
441k
        if (profile_name == NULL)
1835
0
            return_error(gs_error_VMerror);
1836
441k
        switch(dev->color_info.num_components - has_tags) {
1837
356k
            case 1:
1838
356k
                if (strlen(DEFAULT_GRAY_ICC) >= MAX_DEFAULT_ICC_LENGTH)
1839
0
                    return_error(gs_error_limitcheck);
1840
356k
                strncpy(profile_name, DEFAULT_GRAY_ICC, strlen(DEFAULT_GRAY_ICC));
1841
356k
                profile_name[strlen(DEFAULT_GRAY_ICC)] = 0;
1842
356k
                break;
1843
67.0k
            case 3:
1844
67.0k
                if (strlen(DEFAULT_RGB_ICC) >= MAX_DEFAULT_ICC_LENGTH)
1845
0
                    return_error(gs_error_limitcheck);
1846
67.0k
                strncpy(profile_name, DEFAULT_RGB_ICC, strlen(DEFAULT_RGB_ICC));
1847
67.0k
                profile_name[strlen(DEFAULT_RGB_ICC)] = 0;
1848
67.0k
                break;
1849
0
            case 4:
1850
0
                if (strlen(DEFAULT_CMYK_ICC) >= MAX_DEFAULT_ICC_LENGTH)
1851
0
                    return_error(gs_error_limitcheck);
1852
0
                strncpy(profile_name, DEFAULT_CMYK_ICC, strlen(DEFAULT_CMYK_ICC));
1853
0
                profile_name[strlen(DEFAULT_CMYK_ICC)] = 0;
1854
0
                break;
1855
17.4k
            default:
1856
17.4k
                if (strlen(DEFAULT_CMYK_ICC) >= MAX_DEFAULT_ICC_LENGTH)
1857
0
                    return_error(gs_error_limitcheck);
1858
17.4k
                strncpy(profile_name, DEFAULT_CMYK_ICC, strlen(DEFAULT_CMYK_ICC));
1859
17.4k
                profile_name[strlen(DEFAULT_CMYK_ICC)] = 0;
1860
17.4k
                break;
1861
441k
        }
1862
        /* Go ahead and set the profile */
1863
441k
        code = gsicc_set_device_profile(dev, dev->memory, profile_name,
1864
441k
                                        profile_type);
1865
441k
        gs_free_object(dev->memory, profile_name,
1866
441k
                       "gsicc_init_device_profile_struct");
1867
441k
        return code;
1868
441k
    } else {
1869
        /* Go ahead and set the profile */
1870
20.5k
        code = gsicc_set_device_profile(dev, dev->memory, profile_name,
1871
20.5k
                                        profile_type);
1872
20.5k
        return code;
1873
20.5k
    }
1874
461k
}
1875
1876
/* This is used in getting a list of colorant names for the intepreters
1877
   device parameter list. */
1878
char* gsicc_get_dev_icccolorants(cmm_dev_profile_t *dev_profile)
1879
0
{
1880
0
    if (dev_profile == NULL || dev_profile->spotnames == NULL ||
1881
0
        dev_profile->spotnames->name_str == NULL)
1882
0
        return 0;
1883
0
    else
1884
0
        return dev_profile->spotnames->name_str;
1885
0
}
1886
1887
/* Check that the current state of the profiles is fine. Proof profile is of no
1888
   concern since it is doing a CIELAB to CIELAB mapping in the mashed up
1889
   transform. */
1890
static int
1891
gsicc_verify_device_profiles(gx_device * pdev)
1892
523k
{
1893
523k
    int k;
1894
523k
    cmm_dev_profile_t *dev_icc = pdev->icc_struct;
1895
523k
    bool check_components = true;
1896
523k
    bool can_postrender = false;
1897
523k
    bool objects = false;
1898
523k
    int has_tags = device_encodes_tags(pdev);
1899
523k
    int num_components = pdev->color_info.num_components - has_tags;
1900
1901
523k
    if (dev_proc(pdev, dev_spec_op) != NULL) {
1902
523k
        check_components = !(dev_proc(pdev, dev_spec_op)(pdev, gxdso_skip_icc_component_validation, NULL, 0));
1903
523k
        can_postrender = dev_proc(pdev, dev_spec_op)(pdev, gxdso_supports_iccpostrender, NULL, 0);
1904
523k
    }
1905
1906
523k
    if (dev_icc->device_profile[GS_DEFAULT_DEVICE_PROFILE] == NULL)
1907
0
        return 0;
1908
1909
523k
    if (dev_icc->postren_profile != NULL && dev_icc->link_profile != NULL) {
1910
0
        return gs_rethrow(-1, "Post render profile not allowed with device link profile");
1911
0
    }
1912
1913
523k
    if (dev_icc->blend_profile != NULL) {
1914
0
        if (!(dev_icc->blend_profile->data_cs == gsGRAY ||
1915
0
            dev_icc->blend_profile->data_cs == gsRGB ||
1916
0
            dev_icc->blend_profile->data_cs == gsCMYK))
1917
0
            return gs_rethrow(-1, "Blending color space must be Gray, RGB or CMYK");
1918
0
    }
1919
1920
523k
    if (dev_icc->postren_profile != NULL) {
1921
0
        if (!can_postrender) {
1922
0
            return gs_rethrow(-1, "Post render profile not supported by device");
1923
0
        }
1924
0
        if (check_components) {
1925
0
            if (dev_icc->postren_profile->num_comps != num_components) {
1926
0
                return gs_rethrow(-1, "Post render profile does not match the device color model");
1927
0
            }
1928
0
            return 0;
1929
0
        }
1930
0
        return 0; /* An interesting case with sep device.  Need to do a bit of testing here */
1931
0
    }
1932
1933
2.09M
    for (k = 1; k < NUM_DEVICE_PROFILES; k++) {
1934
1.57M
        if (dev_icc->device_profile[k] != NULL) {
1935
0
            objects = true;
1936
0
            break;
1937
0
        }
1938
1.57M
    }
1939
1940
523k
    if (dev_icc->link_profile == NULL) {
1941
523k
        if (!objects) {
1942
523k
            if (check_components && dev_icc->device_profile[GS_DEFAULT_DEVICE_PROFILE]->num_comps !=
1943
133k
                num_components)
1944
2
                return gs_rethrow(-1, "Mismatch of ICC profiles and device color model");
1945
523k
            else
1946
523k
                return 0;  /* Currently sep devices have some leeway here */
1947
523k
        } else {
1948
0
            if (check_components) {
1949
0
                for (k = 1; k < NUM_DEVICE_PROFILES; k++)
1950
0
                    if (dev_icc->device_profile[k] != NULL) {
1951
0
                        if (dev_icc->device_profile[k]->num_comps != num_components)
1952
0
                            return gs_rethrow(-1, "Mismatch of object dependent ICC profiles and device color model");
1953
0
                    }
1954
0
            }
1955
0
            return 0;
1956
0
        }
1957
523k
    } else {
1958
        /* The input of the device link must match the output of the device
1959
           profile and the device link output must match the device color
1960
           model */
1961
0
        if (check_components && dev_icc->link_profile->num_comps_out !=
1962
0
            num_components) {
1963
0
            return gs_rethrow(-1, "Mismatch of device link profile and device color model");
1964
0
        }
1965
0
        if (check_components) {
1966
0
            for (k = 0; k < NUM_DEVICE_PROFILES; k++) {
1967
0
                if (dev_icc->device_profile[k] != NULL) {
1968
0
                    if (dev_icc->device_profile[k]->num_comps !=
1969
0
                        dev_icc->link_profile->num_comps) {
1970
0
                        return gs_rethrow(-1, "Mismatch of device link profile and device ICC profile");
1971
0
                    }
1972
0
                }
1973
0
            }
1974
0
        }
1975
0
        return 0;
1976
0
    }
1977
523k
}
1978
1979
/*  This computes the hash code for the device profile and assigns the profile
1980
    in the icc_struct member variable of the device.  This should
1981
    really occur only one time, but may occur twice if a color model is
1982
    specified or a nondefault profile is specified on the command line */
1983
int
1984
gsicc_set_device_profile(gx_device * pdev, gs_memory_t * mem,
1985
                         char *file_name, gsicc_profile_types_t pro_enum)
1986
523k
{
1987
523k
    cmm_profile_t *icc_profile;
1988
523k
    stream *str;
1989
523k
    int code;
1990
1991
523k
    if (file_name == NULL)
1992
0
        return 0;
1993
1994
    /* Check if device has a profile for this slot. Note that we already
1995
       decremented for any profile that we might be replacing
1996
       in gsicc_init_device_profile_struct */
1997
    /* Silent on failure if this is an output intent profile that
1998
     * could not be found.  Bug 695042.  Multi-threaded rendering
1999
     * set up will try to find the file for the profile during the set
2000
     * up via put/get params. but one does not exist.  The OI profile
2001
     * will be cloned after the put/get params */
2002
523k
    if (strncmp(file_name, OI_PROFILE, strlen(OI_PROFILE)) == 0)
2003
0
        return -1;
2004
2005
523k
    code = gsicc_open_search(file_name, strlen(file_name), mem,
2006
523k
                             mem->gs_lib_ctx->profiledir,
2007
523k
                             mem->gs_lib_ctx->profiledir_len, &str);
2008
523k
    if (code < 0)
2009
0
        return code;
2010
523k
    if (str == NULL)
2011
0
        return gs_rethrow(-1, "cannot find device profile");
2012
2013
523k
    icc_profile =
2014
523k
            gsicc_profile_new(str, mem, file_name, strlen(file_name));
2015
523k
    code = sfclose(str);
2016
523k
    if (icc_profile == NULL)
2017
0
        return gs_throw(gs_error_VMerror, "Creation of ICC profile failed");
2018
2019
    /* Get the profile handle */
2020
523k
    icc_profile->profile_handle =
2021
523k
                gsicc_get_profile_handle_buffer(icc_profile->buffer,
2022
523k
                                                icc_profile->buffer_size,
2023
523k
                                                mem);
2024
523k
    if (icc_profile->profile_handle == NULL) {
2025
0
        rc_decrement(icc_profile, "gsicc_set_device_profile");
2026
0
        return_error(gs_error_unknownerror);
2027
0
    }
2028
2029
    /* Compute the hash code of the profile. Everything in the
2030
       ICC manager will have it's hash code precomputed */
2031
523k
    gsicc_get_icc_buff_hash(icc_profile->buffer,
2032
523k
                            &(icc_profile->hashcode),
2033
523k
                            icc_profile->buffer_size);
2034
523k
    icc_profile->hash_is_valid = true;
2035
2036
    /* Get the number of channels in the output profile */
2037
523k
    icc_profile->num_comps =
2038
523k
                gscms_get_input_channel_count(icc_profile->profile_handle,
2039
523k
                                              icc_profile->memory);
2040
523k
    if_debug1m(gs_debug_flag_icc, mem, "[icc] Profile has %d components\n",
2041
523k
               icc_profile->num_comps);
2042
523k
    icc_profile->num_comps_out =
2043
523k
                gscms_get_output_channel_count(icc_profile->profile_handle,
2044
523k
                                               icc_profile->memory);
2045
523k
    icc_profile->data_cs =
2046
523k
                gscms_get_profile_data_space(icc_profile->profile_handle,
2047
523k
                                             icc_profile->memory);
2048
2049
    /* We need to know if this is one of the "default" profiles or
2050
       if someone has externally set it.  The reason is that if there
2051
       is an output intent in the file, and someone wants to use the
2052
       output intent our handling of the output intent profile is
2053
       different depending upon if someone specified a particular
2054
       output profile */
2055
523k
    switch (icc_profile->num_comps) {
2056
390k
        case 1:
2057
390k
            if (strncmp(icc_profile->name, DEFAULT_GRAY_ICC,
2058
390k
                        strlen(icc_profile->name)) == 0) {
2059
390k
                icc_profile->default_match = DEFAULT_GRAY;
2060
390k
            }
2061
390k
            break;
2062
114k
        case 3:
2063
114k
            if (strncmp(icc_profile->name, DEFAULT_RGB_ICC,
2064
114k
                        strlen(icc_profile->name)) == 0) {
2065
114k
                icc_profile->default_match = DEFAULT_RGB;
2066
114k
            }
2067
114k
            break;
2068
18.9k
        case 4:
2069
18.9k
            if (strncmp(icc_profile->name, DEFAULT_CMYK_ICC,
2070
18.9k
                        strlen(icc_profile->name)) == 0) {
2071
18.9k
                icc_profile->default_match = DEFAULT_CMYK;
2072
18.9k
            }
2073
18.9k
            break;
2074
523k
    }
2075
2076
523k
    if_debug1m(gs_debug_flag_icc, mem, "[icc] Profile data CS is %d\n",
2077
523k
               icc_profile->data_cs);
2078
2079
    /* This is slightly silly, we have a device method for 'get_profile' we really ought to
2080
     * have one for 'set_profile' as well. In its absence, make sure we are setting the profile
2081
     * of the bottom level device.
2082
     */
2083
523k
    while(pdev->child)
2084
0
        pdev = pdev->child;
2085
2086
523k
    switch (pro_enum)
2087
523k
    {
2088
523k
        case gsDEFAULTPROFILE:
2089
523k
        case gsGRAPHICPROFILE:
2090
523k
        case gsIMAGEPROFILE:
2091
523k
        case gsTEXTPROFILE:
2092
523k
            if_debug1m(gs_debug_flag_icc, mem,
2093
523k
                       "[icc] Setting device profile %d\n", pro_enum);
2094
523k
            pdev->icc_struct->device_profile[pro_enum] = icc_profile;
2095
523k
            break;
2096
0
        case gsPROOFPROFILE:
2097
0
            if_debug0m(gs_debug_flag_icc, mem, "[icc] Setting proof profile\n");
2098
0
            pdev->icc_struct->proof_profile = icc_profile;
2099
0
            break;
2100
0
        case gsLINKPROFILE:
2101
0
            if_debug0m(gs_debug_flag_icc, mem, "[icc] Setting link profile\n");
2102
0
            pdev->icc_struct->link_profile = icc_profile;
2103
0
            break;
2104
0
        case gsPRPROFILE:
2105
0
            if_debug0m(gs_debug_flag_icc, mem, "[icc] Setting postrender profile\n");
2106
0
            pdev->icc_struct->postren_profile = icc_profile;
2107
0
            break;
2108
0
        case gsBLENDPROFILE:
2109
0
            if_debug0m(gs_debug_flag_icc, mem, "[icc] Setting blend profile\n");
2110
0
            pdev->icc_struct->blend_profile = icc_profile;
2111
0
            break;
2112
0
        default:
2113
0
        case gsOIPROFILE:
2114
            /* This never happens as output intent profile is set in zicc.c */
2115
0
            rc_decrement(icc_profile, "gsicc_set_device_profile");
2116
0
            return_error(gs_error_unknownerror);
2117
523k
    }
2118
2119
    /* Check that everything is OK with regard to the number of
2120
       components. */
2121
523k
    if (gsicc_verify_device_profiles(pdev) < 0)
2122
2
        return gs_rethrow(-1, "Error in device profiles");
2123
2124
523k
    if (icc_profile->num_comps > 4) {
2125
        /* NCLR Profile.  Set up default colorant names */
2126
0
        code = gsicc_set_device_profile_colorants(pdev, NULL);
2127
0
        if (code < 0)
2128
0
            return code;
2129
0
    }
2130
2131
523k
    return 0;
2132
523k
}
2133
2134
/* Set the icc profile in the gs_color_space object */
2135
int
2136
gsicc_set_gscs_profile(gs_color_space *pcs, cmm_profile_t *icc_profile,
2137
                       gs_memory_t * mem)
2138
331k
{
2139
331k
    if (pcs == NULL)
2140
0
        return -1;
2141
#if ICC_DUMP
2142
    if (icc_profile->buffer) {
2143
        dump_icc_buffer(mem,
2144
                        icc_profile->buffer_size, "set_gscs",
2145
                        icc_profile->buffer);
2146
        global_icc_index++;
2147
    }
2148
#endif
2149
2150
331k
    gsicc_adjust_profile_rc(icc_profile, 1, "gsicc_set_gscs_profile");
2151
331k
    if (pcs->cmm_icc_profile_data != NULL) {
2152
        /* There is already a profile set there */
2153
        /* free it and then set to the new one.  */
2154
        /* should we check the hash code and retain if the same
2155
           or place this job on the caller?  */
2156
0
        gsicc_adjust_profile_rc(pcs->cmm_icc_profile_data, -1, "gsicc_set_gscs_profile");
2157
0
    }
2158
331k
    pcs->cmm_icc_profile_data = icc_profile;
2159
331k
    return 0;
2160
331k
}
2161
2162
int
2163
gsicc_clone_profile(cmm_profile_t *source, cmm_profile_t **destination,
2164
                    gs_memory_t *memory)
2165
0
{
2166
0
    cmm_profile_t *des = gsicc_profile_new(NULL, memory, source->name,
2167
0
        source->name_length);
2168
2169
0
    if (des == NULL)
2170
0
        return gs_throw(gs_error_VMerror, "Profile clone failed");
2171
0
    des->buffer = gs_alloc_bytes(memory, source->buffer_size, "gsicc_clone_profile");
2172
0
    if (des->buffer == NULL) {
2173
0
        gsicc_adjust_profile_rc(des, -1, "gsicc_clone_profile");
2174
0
        return gs_throw(gs_error_VMerror, "Profile clone failed");
2175
0
    }
2176
0
    memcpy(des->buffer, source->buffer, source->buffer_size);
2177
0
    des->buffer_size = source->buffer_size;
2178
0
    gsicc_init_profile_info(des);
2179
0
    *destination = des;
2180
0
    return 0;
2181
0
}
2182
2183
cmm_profile_t *
2184
gsicc_profile_new(stream *s, gs_memory_t *memory, const char* pname,
2185
                  int namelen)
2186
11.5M
{
2187
11.5M
    cmm_profile_t *result;
2188
11.5M
    int code;
2189
11.5M
    char *nameptr = NULL;
2190
11.5M
    gs_memory_t *mem_nongc = memory->non_gc_memory;
2191
2192
11.5M
    result = (cmm_profile_t*) gs_alloc_bytes(mem_nongc, sizeof(cmm_profile_t),
2193
11.5M
                                    "gsicc_profile_new");
2194
11.5M
    if (result == NULL)
2195
0
        return result;
2196
11.5M
    memset(result, 0, GSICC_SERIALIZED_SIZE);
2197
11.5M
    if (namelen > 0) {
2198
10.4M
        nameptr = (char*) gs_alloc_bytes(mem_nongc, namelen+1,
2199
10.4M
                             "gsicc_profile_new");
2200
10.4M
        if (nameptr == NULL) {
2201
0
            gs_free_object(mem_nongc, result, "gsicc_profile_new");
2202
0
            return NULL;
2203
0
        }
2204
10.4M
        memcpy(nameptr, pname, namelen);
2205
10.4M
        nameptr[namelen] = '\0';
2206
10.4M
        result->name = nameptr;
2207
10.4M
    } else {
2208
1.09M
        result->name = NULL;
2209
1.09M
    }
2210
11.5M
    result->name_length = namelen;
2211
2212
    /* We may not have a stream if we are creating this
2213
       object from our own constructed buffer.  For
2214
       example if we are converting CalRGB to an ICC type */
2215
11.5M
    if ( s != NULL) {
2216
10.4M
        code = gsicc_load_profile_buffer(result, s, mem_nongc);
2217
10.4M
        if (code < 0) {
2218
4.12k
            gs_free_object(mem_nongc, result, "gsicc_profile_new");
2219
4.12k
            gs_free_object(mem_nongc, nameptr, "gsicc_profile_new");
2220
4.12k
            return NULL;
2221
4.12k
        }
2222
10.4M
    } else {
2223
1.06M
        result->buffer = NULL;
2224
1.06M
        result->buffer_size = 0;
2225
1.06M
    }
2226
11.5M
    rc_init_free(result, mem_nongc, 1, rc_free_icc_profile);
2227
11.5M
    result->profile_handle = NULL;
2228
11.5M
    result->spotnames = NULL;
2229
11.5M
    result->rend_is_valid = false;
2230
11.5M
    result->isdevlink = false;  /* only used for srcgtag profiles */
2231
11.5M
    result->dev = NULL;
2232
11.5M
    result->memory = mem_nongc;
2233
11.5M
    result->vers = ICCVERS_UNKNOWN;
2234
11.5M
    result->v2_data = NULL;
2235
11.5M
    result->v2_size = 0;
2236
11.5M
    result->release = gscms_release_profile; /* Default case */
2237
2238
11.5M
    result->lock = gx_monitor_label(gx_monitor_alloc(mem_nongc),
2239
11.5M
                                    "gsicc_manage");
2240
11.5M
    if (result->lock == NULL) {
2241
0
        gs_free_object(mem_nongc, result->buffer, "gsicc_load_profile");
2242
0
        gs_free_object(mem_nongc, result, "gsicc_profile_new");
2243
0
        gs_free_object(mem_nongc, nameptr, "gsicc_profile_new");
2244
0
        return NULL;
2245
0
    }
2246
11.5M
    if_debug1m(gs_debug_flag_icc, mem_nongc,
2247
11.5M
               "[icc] allocating ICC profile = "PRI_INTPTR"\n", (intptr_t)result);
2248
11.5M
    return result;
2249
11.5M
}
2250
2251
static void
2252
rc_free_icc_profile(gs_memory_t * mem, void *ptr_in, client_name_t cname)
2253
11.5M
{
2254
11.5M
    cmm_profile_t *profile = (cmm_profile_t *)ptr_in;
2255
11.5M
    gs_memory_t *mem_nongc =  profile->memory;
2256
2257
11.5M
    if_debug2m(gs_debug_flag_icc, mem,
2258
11.5M
               "[icc] rc decrement profile = "PRI_INTPTR" rc = %ld\n",
2259
11.5M
               (intptr_t)ptr_in, profile->rc.ref_count);
2260
11.5M
    if (profile->rc.ref_count <= 1 ) {
2261
        /* Clear out the buffer if it is full */
2262
11.5M
        if (profile->buffer != NULL) {
2263
10.4M
            gs_free_object(mem_nongc, profile->buffer, "rc_free_icc_profile(buffer)");
2264
10.4M
            profile->buffer = NULL;
2265
10.4M
        }
2266
11.5M
        if_debug0m(gs_debug_flag_icc, mem, "[icc] profile freed\n");
2267
        /* Release this handle if it has been set */
2268
11.5M
        if (profile->profile_handle != NULL) {
2269
1.21M
            profile->release(profile->profile_handle, profile->memory);
2270
1.21M
            profile->profile_handle = NULL;
2271
1.21M
        }
2272
        /* Release the name if it has been set */
2273
11.5M
        if (profile->name != NULL) {
2274
10.4M
            gs_free_object(mem_nongc, profile->name,"rc_free_icc_profile(name)");
2275
10.4M
            profile->name = NULL;
2276
10.4M
            profile->name_length = 0;
2277
10.4M
        }
2278
11.5M
        profile->hash_is_valid = 0;
2279
11.5M
        if (profile->lock != NULL) {
2280
11.5M
            gx_monitor_free(profile->lock);
2281
11.5M
            profile->lock = NULL;
2282
11.5M
        }
2283
        /* If we had a DeviceN profile with names deallocate that now */
2284
11.5M
        if (profile->spotnames != NULL) {
2285
            /* Free the linked list in this object */
2286
0
            gsicc_free_spotnames(profile->spotnames, mem_nongc);
2287
            /* Free the main object */
2288
0
            gs_free_object(mem_nongc, profile->spotnames, "rc_free_icc_profile(spotnames)");
2289
0
        }
2290
        /* If we allocated a buffer to hold the v2 profile then free that */
2291
11.5M
        if (profile->v2_data != NULL) {
2292
0
            gs_free_object(mem_nongc, profile->v2_data, "rc_free_icc_profile(v2_data)");
2293
0
        }
2294
11.5M
        gs_free_object(mem_nongc, profile, "rc_free_icc_profile");
2295
11.5M
    }
2296
11.5M
}
2297
2298
/* We are just starting up.  We need to set the initial color space in the
2299
   graphic state at this time */
2300
int
2301
gsicc_init_gs_colors(gs_gstate *pgs)
2302
166k
{
2303
166k
    int             code = 0;
2304
166k
    gs_color_space  *cs_old;
2305
166k
    gs_color_space  *cs_new;
2306
166k
    int k;
2307
2308
166k
    if (pgs->in_cachedevice)
2309
0
        return_error(gs_error_undefined);
2310
2311
499k
    for (k = 0; k < 2; k++) {
2312
        /* First do color space 0 */
2313
333k
        cs_old = pgs->color[k].color_space;
2314
333k
        cs_new = gs_cspace_new_DeviceGray(pgs->memory);
2315
333k
        if (cs_new == NULL)
2316
0
            return_error(gs_error_VMerror);
2317
333k
        rc_increment_cs(cs_new);
2318
333k
        pgs->color[k].color_space = cs_new;
2319
333k
        if ( (code = cs_new->type->install_cspace(cs_new, pgs)) < 0 ) {
2320
0
            pgs->color[k].color_space = cs_old;
2321
0
            rc_decrement_only_cs(cs_new, "gsicc_init_gs_colors");
2322
0
            return code;
2323
333k
        } else {
2324
333k
            rc_decrement_only_cs(cs_old, "gsicc_init_gs_colors");
2325
333k
        }
2326
333k
    }
2327
166k
    return code;
2328
166k
}
2329
2330
/* Only set those that have not already been set. */
2331
int
2332
gsicc_init_iccmanager(gs_gstate * pgs)
2333
2.56M
{
2334
2.56M
    int code = 0, k;
2335
2.56M
    const char *pname;
2336
2.56M
    int namelen;
2337
2.56M
    gsicc_manager_t *iccmanager = pgs->icc_manager;
2338
2.56M
    cmm_profile_t *profile;
2339
2340
12.8M
    for (k = 0; k < 4; k++) {
2341
10.2M
        pname = default_profile_params[k].path;
2342
10.2M
        namelen = strlen(pname);
2343
2344
10.2M
        switch(default_profile_params[k].default_type) {
2345
2.56M
            case DEFAULT_GRAY:
2346
2.56M
                profile = iccmanager->default_gray;
2347
2.56M
                break;
2348
2.56M
            case DEFAULT_RGB:
2349
2.56M
                profile = iccmanager->default_rgb;
2350
2.56M
                break;
2351
2.56M
            case DEFAULT_CMYK:
2352
2.56M
                 profile = iccmanager->default_cmyk;
2353
2.56M
                 break;
2354
2.56M
            default:
2355
2.56M
                profile = NULL;
2356
10.2M
        }
2357
10.2M
        if (profile == NULL)
2358
9.40M
            code = gsicc_set_profile(iccmanager, pname, namelen,
2359
9.40M
                                     default_profile_params[k].default_type);
2360
10.2M
        if (code < 0)
2361
0
            return gs_rethrow(code, "cannot find default icc profile");
2362
10.2M
    }
2363
#if CREATE_V2_DATA
2364
    /* Test bed for V2 creation from V4 */
2365
    for (int j = 2; j < 3; j++)
2366
    {
2367
        byte *data;
2368
        int size;
2369
2370
        switch (default_profile_params[j].default_type) {
2371
        case DEFAULT_GRAY:
2372
            profile = iccmanager->default_gray;
2373
            break;
2374
        case DEFAULT_RGB:
2375
            profile = iccmanager->default_rgb;
2376
            break;
2377
        case DEFAULT_CMYK:
2378
            profile = iccmanager->default_cmyk;
2379
            break;
2380
        default:
2381
            profile = NULL;
2382
        }
2383
        gsicc_initialize_default_profile(profile);
2384
        data = gsicc_create_getv2buffer(pgs, profile, &size);
2385
    }
2386
#endif
2387
2.56M
    return 0;
2388
2.56M
}
2389
2390
static void
2391
gsicc_manager_finalize(const gs_memory_t *memory, void * vptr)
2392
2.44M
{
2393
2.44M
    gsicc_manager_t *icc_man = (gsicc_manager_t *)vptr;
2394
2395
2.44M
    gsicc_manager_free_contents(icc_man, "gsicc_manager_finalize");
2396
2.44M
}
2397
2398
gsicc_manager_t *
2399
gsicc_manager_new(gs_memory_t *memory)
2400
2.44M
{
2401
2.44M
    gsicc_manager_t *result;
2402
2403
    /* Allocated in stable gc memory.  This done since the profiles
2404
       may be introduced late in the process. */
2405
2.44M
    memory = memory->stable_memory;
2406
2.44M
    result = gs_alloc_struct(memory, gsicc_manager_t, &st_gsicc_manager,
2407
2.44M
                             "gsicc_manager_new");
2408
2.44M
    if ( result == NULL )
2409
0
        return NULL;
2410
2.44M
    rc_init_free(result, memory, 1, rc_gsicc_manager_free);
2411
2.44M
    result->default_gray = NULL;
2412
2.44M
    result->default_rgb = NULL;
2413
2.44M
    result->default_cmyk = NULL;
2414
2.44M
    result->lab_profile = NULL;
2415
2.44M
    result->xyz_profile = NULL;
2416
2.44M
    result->graytok_profile = NULL;
2417
2.44M
    result->device_named = NULL;
2418
2.44M
    result->device_n = NULL;
2419
2.44M
    result->smask_profiles = NULL;
2420
2.44M
    result->memory = memory;
2421
2.44M
    result->srcgtag_profile = NULL;
2422
2.44M
    result->override_internal = false;
2423
2.44M
    return result;
2424
2.44M
}
2425
2426
static void gsicc_manager_free_contents(gsicc_manager_t *icc_manager,
2427
                                        client_name_t cname)
2428
2.44M
{
2429
2.44M
    int k;
2430
2.44M
    gsicc_devicen_entry_t *device_n, *device_n_next;
2431
2432
2.44M
    gsicc_adjust_profile_rc(icc_manager->default_cmyk, -1, "gsicc_manager_free_contents");
2433
2.44M
    gsicc_adjust_profile_rc(icc_manager->default_gray, -1, "gsicc_manager_free_contents");
2434
2.44M
    gsicc_adjust_profile_rc(icc_manager->default_rgb, -1, "gsicc_manager_free_contents");
2435
2.44M
    gsicc_adjust_profile_rc(icc_manager->device_named, -1, "gsicc_manager_free_contents");
2436
2.44M
    gsicc_adjust_profile_rc(icc_manager->lab_profile, -1, "gsicc_manager_free_contents");
2437
2.44M
    gsicc_adjust_profile_rc(icc_manager->graytok_profile, -1, "gsicc_manager_free_contents");
2438
2.44M
    rc_decrement(icc_manager->srcgtag_profile, "gsicc_manager_free_contents");
2439
2440
    /* Loop through the DeviceN profiles */
2441
2.44M
    if ( icc_manager->device_n != NULL) {
2442
0
        device_n = icc_manager->device_n->head;
2443
0
        for ( k = 0; k < icc_manager->device_n->count; k++) {
2444
0
            gsicc_adjust_profile_rc(device_n->iccprofile, -1, "gsicc_manager_free_contents");
2445
0
            device_n_next = device_n->next;
2446
0
            gs_free_object(icc_manager->memory, device_n, "gsicc_manager_free_contents");
2447
0
            device_n = device_n_next;
2448
0
        }
2449
0
        gs_free_object(icc_manager->memory, icc_manager->device_n,
2450
0
                       "gsicc_manager_free_contents");
2451
0
    }
2452
2453
    /* The soft mask profiles */
2454
2.44M
    if (icc_manager->smask_profiles != NULL) {
2455
40.2k
        gs_free_object(icc_manager->smask_profiles->memory, icc_manager->smask_profiles, "gsicc_manager_free_contents");
2456
40.2k
        icc_manager->smask_profiles = NULL;
2457
40.2k
    }
2458
2.44M
}
2459
2460
static void
2461
rc_gsicc_manager_free(gs_memory_t * mem, void *ptr_in, client_name_t cname)
2462
2.44M
{
2463
    /* Ending the manager.  Decrement the ref counts of the profiles
2464
       and then free the structure */
2465
2.44M
    gsicc_manager_t *icc_manager = (gsicc_manager_t * ) ptr_in;
2466
2467
2.44M
    assert(mem == icc_manager->memory);
2468
2469
2.44M
    gs_free_object(icc_manager->memory, icc_manager, "rc_gsicc_manager_free");
2470
2.44M
}
2471
2472
/* Allocates and loads the icc buffer from the stream. */
2473
static int
2474
gsicc_load_profile_buffer(cmm_profile_t *profile, stream *s,
2475
                          gs_memory_t *memory)
2476
10.4M
{
2477
10.4M
    int                     num_bytes,profile_size;
2478
10.4M
    unsigned char           *buffer_ptr;
2479
10.4M
    int                     code;
2480
2481
10.4M
    code = srewind(s);  /* Work around for issue with sfread return 0 bytes
2482
                        and not doing a retry if there is an issue.  This
2483
                        is a bug in the stream logic or strmio layer.  Occurs
2484
                        with smask_withicc.pdf on linux 64 bit system */
2485
10.4M
    if (code < 0)
2486
0
        return code;
2487
    /* Get the size from doing a seek to the end and then a rewind instead
2488
       of relying upon the profile size indicated in the header */
2489
10.4M
    code = sfseek(s,0,SEEK_END);
2490
10.4M
    if (code < 0)
2491
0
        return code;
2492
10.4M
    profile_size = sftell(s);
2493
10.4M
    code = srewind(s);
2494
10.4M
    if (code < 0)
2495
0
        return code;
2496
10.4M
    if (profile_size < ICC_HEADER_SIZE)
2497
4.12k
        return_error(gs_error_VMerror);
2498
    /* Allocate the buffer, stuff with the profile */
2499
10.4M
   buffer_ptr = gs_alloc_bytes(memory, profile_size,
2500
10.4M
                                        "gsicc_load_profile");
2501
10.4M
   if (buffer_ptr == NULL)
2502
0
        return gs_throw(gs_error_VMerror, "Insufficient memory for profile buffer");
2503
10.4M
   num_bytes = sfread(buffer_ptr,sizeof(unsigned char),profile_size,s);
2504
10.4M
   if( num_bytes != profile_size) {
2505
0
       gs_free_object(memory, buffer_ptr, "gsicc_load_profile");
2506
0
       return -1;
2507
0
   }
2508
10.4M
   profile->buffer = buffer_ptr;
2509
10.4M
   profile->buffer_size = num_bytes;
2510
10.4M
   return 0;
2511
10.4M
}
2512
2513
/* Allocates and loads the named color structure from the stream. */
2514
static int
2515
gsicc_load_namedcolor_buffer(cmm_profile_t *profile, stream *s,
2516
                             gs_memory_t *memory)
2517
0
{
2518
0
    int                     num_bytes,profile_size;
2519
0
    unsigned char           *buffer_ptr;
2520
0
    int                     code;
2521
2522
0
    code = srewind(s);
2523
0
    if (code < 0)
2524
0
        return code;
2525
0
    code = sfseek(s,0,SEEK_END);
2526
0
    if (code < 0)
2527
0
        return code;
2528
0
    profile_size = sftell(s);
2529
0
    code = srewind(s);
2530
0
    if (code < 0)
2531
0
        return code;
2532
    /* Allocate the buffer, stuff with the profile */
2533
0
    buffer_ptr = gs_alloc_bytes(memory->non_gc_memory, profile_size,
2534
0
                                "gsicc_load_profile");
2535
0
    if (buffer_ptr == NULL)
2536
0
        return gs_throw(gs_error_VMerror, "Insufficient memory for profile buffer");
2537
0
    num_bytes = sfread(buffer_ptr,sizeof(unsigned char),profile_size,s);
2538
0
    if( num_bytes != profile_size) {
2539
0
        gs_free_object(memory->non_gc_memory, buffer_ptr, "gsicc_load_profile");
2540
0
        return -1;
2541
0
    }
2542
0
    profile->buffer = buffer_ptr;
2543
0
    profile->buffer_size = num_bytes;
2544
0
    return 0;
2545
0
}
2546
2547
/* Check if the embedded profile is the same as any of the default profiles */
2548
static void
2549
gsicc_set_default_cs_value(cmm_profile_t *picc_profile, gs_gstate *pgs)
2550
19.3k
{
2551
19.3k
    gsicc_manager_t *icc_manager = pgs->icc_manager;
2552
19.3k
    int64_t hashcode = picc_profile->hashcode;
2553
2554
19.3k
    if ( picc_profile->default_match == DEFAULT_NONE ) {
2555
19.3k
        switch ( picc_profile->data_cs ) {
2556
7.18k
            case gsGRAY:
2557
7.18k
                if ( hashcode == icc_manager->default_gray->hashcode )
2558
0
                    picc_profile->default_match = DEFAULT_GRAY_s;
2559
7.18k
                break;
2560
12.1k
            case gsRGB:
2561
12.1k
                if ( hashcode == icc_manager->default_rgb->hashcode )
2562
0
                    picc_profile->default_match = DEFAULT_RGB_s;
2563
12.1k
                break;
2564
20
            case gsCMYK:
2565
20
                if ( hashcode == icc_manager->default_cmyk->hashcode )
2566
0
                    picc_profile->default_match = DEFAULT_CMYK_s;
2567
20
                break;
2568
0
            case gsCIELAB:
2569
0
                if ( hashcode == icc_manager->lab_profile->hashcode )
2570
0
                    picc_profile->default_match = LAB_TYPE_s;
2571
0
                break;
2572
0
            case gsCIEXYZ:
2573
0
                return;
2574
0
                break;
2575
0
            case gsNCHANNEL:
2576
0
                return;
2577
0
                break;
2578
0
            default:
2579
0
                return;
2580
19.3k
        }
2581
19.3k
    }
2582
19.3k
}
2583
2584
/* Initialize the hash code value */
2585
void
2586
gsicc_init_hash_cs(cmm_profile_t *picc_profile, gs_gstate *pgs)
2587
19.3k
{
2588
19.3k
    if ( !(picc_profile->hash_is_valid) ) {
2589
15.4k
        gsicc_get_icc_buff_hash(picc_profile->buffer, &(picc_profile->hashcode),
2590
15.4k
                                picc_profile->buffer_size);
2591
15.4k
        picc_profile->hash_is_valid = true;
2592
15.4k
    }
2593
19.3k
    gsicc_set_default_cs_value(picc_profile, pgs);
2594
19.3k
}
2595
2596
/* Interface code to get the profile handle for data
2597
   stored in the clist device */
2598
gcmmhprofile_t
2599
gsicc_get_profile_handle_clist(cmm_profile_t *picc_profile, gs_memory_t *memory)
2600
10.0k
{
2601
10.0k
    gcmmhprofile_t profile_handle = NULL;
2602
10.0k
    unsigned int profile_size;
2603
10.0k
    int size;
2604
10.0k
    gx_device_clist_reader *pcrdev = (gx_device_clist_reader*) picc_profile->dev;
2605
10.0k
    unsigned char *buffer_ptr;
2606
10.0k
    int64_t position;
2607
10.0k
    gsicc_serialized_profile_t profile_header;
2608
10.0k
    int k;
2609
2610
10.0k
    if( pcrdev != NULL) {
2611
2612
        /* Check ICC table for hash code and get the whole size icc raw buffer
2613
           plus serialized header information */
2614
10.0k
        position = gsicc_search_icc_table(pcrdev->icc_table,
2615
10.0k
                                          picc_profile->hashcode, &size);
2616
10.0k
        if ( position < 0 )
2617
0
            return NULL;  /* Not found. */
2618
2619
        /* Get the ICC buffer.  We really want to avoid this transfer.
2620
           I need to write  an interface to the CMM to do this through
2621
           the clist ioprocs */
2622
        /* Allocate the buffer */
2623
10.0k
        profile_size = size - GSICC_SERIALIZED_SIZE;
2624
        /* Profile and its members are ALL in non-gc memory */
2625
10.0k
        buffer_ptr = gs_alloc_bytes(memory->non_gc_memory, profile_size,
2626
10.0k
                                            "gsicc_get_profile_handle_clist");
2627
10.0k
        if (buffer_ptr == NULL)
2628
0
            return NULL;
2629
10.0k
        clist_read_chunk(pcrdev, position + GSICC_SERIALIZED_SIZE,
2630
10.0k
            profile_size, (unsigned char *) buffer_ptr);
2631
10.0k
        profile_handle = gscms_get_profile_handle_mem(buffer_ptr, profile_size, memory->non_gc_memory);
2632
10.0k
        if (profile_handle == NULL) {
2633
0
            gs_free_object(memory->non_gc_memory, buffer_ptr, "gsicc_get_profile_handle_clist");
2634
0
            return NULL;
2635
0
        }
2636
        /* We also need to get some of the serialized information */
2637
10.0k
        clist_read_chunk(pcrdev, position, GSICC_SERIALIZED_SIZE,
2638
10.0k
                        (unsigned char *) (&profile_header));
2639
10.0k
        picc_profile->buffer = NULL;
2640
10.0k
        picc_profile->buffer_size = 0;
2641
10.0k
        picc_profile->data_cs = profile_header.data_cs;
2642
10.0k
        picc_profile->default_match = profile_header.default_match;
2643
10.0k
        picc_profile->hash_is_valid = profile_header.hash_is_valid;
2644
10.0k
        picc_profile->hashcode = profile_header.hashcode;
2645
10.0k
        picc_profile->islab = profile_header.islab;
2646
10.0k
        picc_profile->num_comps = profile_header.num_comps;
2647
10.0k
        picc_profile->rend_is_valid = profile_header.rend_is_valid;
2648
10.0k
        picc_profile->rend_cond = profile_header.rend_cond;
2649
10.0k
        picc_profile->isdevlink = profile_header.isdevlink;
2650
34.1k
        for ( k = 0; k < profile_header.num_comps; k++ ) {
2651
24.1k
            picc_profile->Range.ranges[k].rmax =
2652
24.1k
                profile_header.Range.ranges[k].rmax;
2653
24.1k
            picc_profile->Range.ranges[k].rmin =
2654
24.1k
                profile_header.Range.ranges[k].rmin;
2655
24.1k
        }
2656
10.0k
        gs_free_object(memory->non_gc_memory, buffer_ptr, "gsicc_get_profile_handle_clist");
2657
10.0k
        return profile_handle;
2658
10.0k
     }
2659
0
     return NULL;
2660
10.0k
}
2661
2662
gcmmhprofile_t
2663
gsicc_get_profile_handle_buffer(unsigned char *buffer, int profile_size, gs_memory_t *memory)
2664
1.21M
{
2665
2666
1.21M
    gcmmhprofile_t profile_handle = NULL;
2667
2668
1.21M
     if( buffer != NULL) {
2669
1.21M
         if (profile_size < ICC_HEADER_SIZE) {
2670
0
             return 0;
2671
0
         }
2672
1.21M
         profile_handle = gscms_get_profile_handle_mem(buffer, profile_size, memory->non_gc_memory);
2673
1.21M
         return profile_handle;
2674
1.21M
     }
2675
0
     return 0;
2676
1.21M
}
2677
2678
 /*  If we have a profile for the color space already, then we use that.
2679
     If we do not have one then we will use data from
2680
     the ICC manager that is based upon the current color space. */
2681
 cmm_profile_t*
2682
 gsicc_get_gscs_profile(gs_color_space *gs_colorspace,
2683
                        gsicc_manager_t *icc_manager)
2684
0
 {
2685
0
     cmm_profile_t *profile = gs_colorspace->cmm_icc_profile_data;
2686
0
     gs_color_space_index color_space_index =
2687
0
            gs_color_space_get_index(gs_colorspace);
2688
0
     int code = 0;
2689
0
     bool islab;
2690
2691
0
     if (profile != NULL )
2692
0
        return profile;
2693
     /* else, return the default types */
2694
0
     switch( color_space_index ) {
2695
0
        case gs_color_space_index_DeviceGray:
2696
0
            return icc_manager->default_gray;
2697
0
            break;
2698
0
        case gs_color_space_index_DeviceRGB:
2699
0
            return icc_manager->default_rgb;
2700
0
            break;
2701
0
        case gs_color_space_index_DeviceCMYK:
2702
0
            return icc_manager->default_cmyk;
2703
0
            break;
2704
            /* Only used in 3x types */
2705
0
        case gs_color_space_index_DevicePixel:
2706
0
            return 0;
2707
0
            break;
2708
0
       case gs_color_space_index_DeviceN:
2709
            /* If we made it to here, then we will need to use the
2710
               alternate colorspace */
2711
0
            return 0;
2712
0
            break;
2713
0
       case gs_color_space_index_CIEDEFG:
2714
           /* For now just use default CMYK to avoid segfault.  MJV to fix */
2715
0
           gs_colorspace->cmm_icc_profile_data = icc_manager->default_cmyk;
2716
0
           gsicc_adjust_profile_rc(icc_manager->default_cmyk, 1, "gsicc_get_gscs_profile");
2717
0
           return gs_colorspace->cmm_icc_profile_data;
2718
           /* Need to convert to an ICC form */
2719
0
           break;
2720
0
        case gs_color_space_index_CIEDEF:
2721
           /* For now just use default RGB to avoid segfault.  MJV to fix */
2722
0
           gs_colorspace->cmm_icc_profile_data = icc_manager->default_rgb;
2723
0
           gsicc_adjust_profile_rc(icc_manager->default_rgb, 1, "gsicc_get_gscs_profile");
2724
0
           return gs_colorspace->cmm_icc_profile_data;
2725
           /* Need to convert to an ICC form */
2726
0
           break;
2727
0
        case gs_color_space_index_CIEABC:
2728
0
            gs_colorspace->cmm_icc_profile_data =
2729
0
                gsicc_profile_new(NULL, icc_manager->memory, NULL, 0);
2730
0
            if (gs_colorspace->cmm_icc_profile_data == NULL) {
2731
0
                gs_throw(gs_error_VMerror, "Creation of ICC profile for CIEABC failed");
2732
0
                return NULL;
2733
0
            }
2734
0
            code =
2735
0
                gsicc_create_fromabc(gs_colorspace,
2736
0
                        &(gs_colorspace->cmm_icc_profile_data->buffer),
2737
0
                        &(gs_colorspace->cmm_icc_profile_data->buffer_size),
2738
0
                        icc_manager->memory,
2739
0
                        &(gs_colorspace->params.abc->caches.DecodeABC.caches[0]),
2740
0
                        &(gs_colorspace->params.abc->common.caches.DecodeLMN[0]),
2741
0
                        &islab);
2742
0
            if (code < 0) {
2743
0
                gs_warn("Failed to create ICC profile from CIEABC");
2744
0
                gsicc_adjust_profile_rc(gs_colorspace->cmm_icc_profile_data, -1,
2745
0
                             "gsicc_get_gscs_profile");
2746
0
                return NULL;
2747
0
            }
2748
2749
0
            if (islab) {
2750
                /* Destroy the profile */
2751
0
                gsicc_adjust_profile_rc(gs_colorspace->cmm_icc_profile_data, -1,
2752
0
                             "gsicc_get_gscs_profile");
2753
                /* This may be an issue for pdfwrite */
2754
0
                return icc_manager->lab_profile;
2755
0
            }
2756
0
            gs_colorspace->cmm_icc_profile_data->default_match = CIE_ABC;
2757
0
            return gs_colorspace->cmm_icc_profile_data;
2758
0
            break;
2759
0
        case gs_color_space_index_CIEA:
2760
0
            gs_colorspace->cmm_icc_profile_data =
2761
0
                gsicc_profile_new(NULL, icc_manager->memory, NULL, 0);
2762
0
            if (gs_colorspace->cmm_icc_profile_data == NULL) {
2763
0
                gs_throw(gs_error_VMerror, "Creation of ICC profile for CIEA failed");
2764
0
                return NULL;
2765
0
            }
2766
0
            code =
2767
0
                gsicc_create_froma(gs_colorspace,
2768
0
                            &(gs_colorspace->cmm_icc_profile_data->buffer),
2769
0
                            &(gs_colorspace->cmm_icc_profile_data->buffer_size),
2770
0
                            icc_manager->memory,
2771
0
                            &(gs_colorspace->params.a->caches.DecodeA),
2772
0
                            &(gs_colorspace->params.a->common.caches.DecodeLMN[0]));
2773
0
            gs_colorspace->cmm_icc_profile_data->default_match = CIE_A;
2774
0
            return gs_colorspace->cmm_icc_profile_data;
2775
0
            break;
2776
0
        case gs_color_space_index_Separation:
2777
            /* Caller should use named color path */
2778
0
            return 0;
2779
0
            break;
2780
0
        case gs_color_space_index_Pattern:
2781
0
        case gs_color_space_index_Indexed:
2782
            /* Caller should use the base space for these */
2783
0
            return 0;
2784
0
            break;
2785
0
        case gs_color_space_index_ICC:
2786
            /* This should not occur, as the space
2787
               should have had a populated profile handle */
2788
0
            return 0;
2789
0
            break;
2790
0
     }
2791
0
    return 0;
2792
0
 }
2793
2794
static int64_t
2795
gsicc_search_icc_table(clist_icctable_t *icc_table, int64_t icc_hashcode, int *size)
2796
768k
{
2797
768k
    int tablesize = icc_table->tablesize, k;
2798
768k
    clist_icctable_entry_t *curr_entry;
2799
2800
768k
    curr_entry = icc_table->head;
2801
1.04M
    for (k = 0; k < tablesize; k++ ) {
2802
1.04M
        if ( curr_entry->serial_data.hashcode == icc_hashcode ) {
2803
768k
            *size = curr_entry->serial_data.size;
2804
768k
            return curr_entry->serial_data.file_position;
2805
768k
        }
2806
272k
        curr_entry = curr_entry->next;
2807
272k
    }
2808
2809
    /* Did not find it! */
2810
0
    *size = 0;
2811
0
    return -1;
2812
768k
}
2813
2814
/* This is used to get only the serial data from the clist.  We don't bother
2815
   with the whole profile until we actually need it.  It may be that the link
2816
   that we need is already in the link cache */
2817
cmm_profile_t*
2818
gsicc_read_serial_icc(gx_device *dev, int64_t icc_hashcode)
2819
758k
{
2820
758k
    cmm_profile_t *profile;
2821
758k
    int64_t position;
2822
758k
    int size;
2823
758k
    int code;
2824
758k
    gx_device_clist_reader *pcrdev = (gx_device_clist_reader*) dev;
2825
2826
    /* Create a new ICC profile structure */
2827
758k
    profile = gsicc_profile_new(NULL, pcrdev->memory, NULL, 0);
2828
758k
    if (profile == NULL)
2829
0
        return NULL;
2830
2831
    /* Check ICC table for hash code and get the whole size icc raw buffer
2832
       plus serialized header information. Make sure the icc_table has
2833
       been intialized */
2834
758k
    if (pcrdev->icc_table == NULL) {
2835
0
        code = clist_read_icctable(pcrdev);
2836
0
        if (code<0)
2837
0
            return NULL;
2838
0
    }
2839
758k
    position = gsicc_search_icc_table(pcrdev->icc_table, icc_hashcode, &size);
2840
758k
    if ( position < 0 )
2841
0
        return NULL;
2842
2843
    /* Get the serialized portion of the ICC profile information */
2844
758k
    clist_read_chunk(pcrdev, position, GSICC_SERIALIZED_SIZE,
2845
758k
                    (unsigned char *) profile);
2846
758k
    return profile;
2847
758k
}
2848
2849
void
2850
gsicc_profile_serialize(gsicc_serialized_profile_t *profile_data,
2851
                        cmm_profile_t *icc_profile)
2852
14.5k
{
2853
14.5k
    if (icc_profile == NULL)
2854
0
        return;
2855
14.5k
    memcpy(profile_data, icc_profile, GSICC_SERIALIZED_SIZE);
2856
14.5k
}
2857
2858
/* Utility functions */
2859
2860
int
2861
gsicc_getsrc_channel_count(cmm_profile_t *icc_profile)
2862
0
{
2863
0
    return gscms_get_input_channel_count(icc_profile->profile_handle,
2864
0
        icc_profile->memory);
2865
0
}
2866
2867
void
2868
gsicc_extract_profile(gs_graphics_type_tag_t graphics_type_tag,
2869
                       cmm_dev_profile_t *profile_struct,
2870
                       cmm_profile_t **profile, gsicc_rendering_param_t *render_cond)
2871
237M
{
2872
237M
    switch (graphics_type_tag & GS_DEVICE_TAG_MASK) {
2873
0
        case GS_UNKNOWN_TAG:
2874
55.5M
        case GS_UNTOUCHED_TAG:
2875
55.5M
        default:
2876
55.5M
            (*profile) = profile_struct->device_profile[GS_DEFAULT_DEVICE_PROFILE];
2877
55.5M
            *render_cond = profile_struct->rendercond[GS_DEFAULT_DEVICE_PROFILE];
2878
55.5M
            break;
2879
136M
        case GS_VECTOR_TAG:
2880
136M
            *render_cond = profile_struct->rendercond[GS_VECTOR_DEVICE_PROFILE];
2881
136M
            if (profile_struct->device_profile[GS_VECTOR_DEVICE_PROFILE] != NULL) {
2882
0
                (*profile) = profile_struct->device_profile[GS_VECTOR_DEVICE_PROFILE];
2883
136M
            } else {
2884
136M
                (*profile) = profile_struct->device_profile[GS_DEFAULT_DEVICE_PROFILE];
2885
136M
            }
2886
136M
            break;
2887
42.8M
        case GS_IMAGE_TAG:
2888
42.8M
            *render_cond = profile_struct->rendercond[GS_IMAGE_DEVICE_PROFILE];
2889
42.8M
            if (profile_struct->device_profile[GS_IMAGE_DEVICE_PROFILE] != NULL) {
2890
0
                (*profile) = profile_struct->device_profile[GS_IMAGE_DEVICE_PROFILE];
2891
42.8M
            } else {
2892
42.8M
                (*profile) = profile_struct->device_profile[GS_DEFAULT_DEVICE_PROFILE];
2893
42.8M
            }
2894
42.8M
            break;
2895
2.53M
        case GS_TEXT_TAG:
2896
2.53M
            *render_cond = profile_struct->rendercond[GS_TEXT_DEVICE_PROFILE];
2897
2.53M
            if (profile_struct->device_profile[GS_TEXT_DEVICE_PROFILE] != NULL) {
2898
0
                (*profile) = profile_struct->device_profile[GS_TEXT_DEVICE_PROFILE];
2899
2.53M
            } else {
2900
2.53M
                (*profile) = profile_struct->device_profile[GS_DEFAULT_DEVICE_PROFILE];
2901
2.53M
            }
2902
2.53M
            break;
2903
237M
        }
2904
237M
}
2905
2906
/* internal ICC and rendering intent override control */
2907
void
2908
gs_setoverrideicc(gs_gstate *pgs, bool value)
2909
709k
{
2910
709k
    if (pgs->icc_manager != NULL) {
2911
709k
        pgs->icc_manager->override_internal = value;
2912
709k
    }
2913
709k
}
2914
bool
2915
gs_currentoverrideicc(const gs_gstate *pgs)
2916
459k
{
2917
459k
    if (pgs->icc_manager != NULL) {
2918
459k
        return pgs->icc_manager->override_internal;
2919
459k
    } else {
2920
0
        return false;
2921
0
    }
2922
459k
}
2923
2924
void
2925
gsicc_setrange_lab(cmm_profile_t *profile)
2926
0
{
2927
0
    profile->Range.ranges[0].rmin = 0.0;
2928
0
    profile->Range.ranges[0].rmax = 100.0;
2929
0
    profile->Range.ranges[1].rmin = -128.0;
2930
0
    profile->Range.ranges[1].rmax = 127.0;
2931
0
    profile->Range.ranges[2].rmin = -128.0;
2932
0
    profile->Range.ranges[2].rmax = 127.0;
2933
0
}
2934
2935
#if ICC_DUMP
2936
/* Debug dump of ICC buffer data */
2937
static void
2938
dump_icc_buffer(const gs_memory_t *mem, int buffersize, char filename[],byte *Buffer)
2939
{
2940
    char full_file_name[50];
2941
    gp_file *fid;
2942
2943
    gs_snprintf(full_file_name,sizeof(full_file_name),"%d)%s_debug.icc",global_icc_index,filename);
2944
    fid = gp_fopen(mem, full_file_name,"wb");
2945
    gp_fwrite(Buffer,sizeof(unsigned char),buffersize,fid);
2946
    gp_fclose(fid);
2947
}
2948
#endif
2949
2950
/* The following are for setting the system/user params */
2951
/* No default for the deviceN profile. */
2952
void
2953
gs_currentdevicenicc(const gs_gstate * pgs, gs_param_string * pval)
2954
332k
{
2955
332k
    static const char *const rfs = "";
2956
2957
    /*FIXME: This should return the entire list !!! */
2958
    /*       Just return the first one for now      */
2959
332k
    if (pgs->icc_manager->device_n == NULL) {
2960
332k
        pval->data = (const byte *) rfs;
2961
332k
        pval->persistent = true;
2962
332k
    } else {
2963
0
        pval->data =
2964
0
            (const byte *) (pgs->icc_manager->device_n->head->iccprofile->name);
2965
0
        pval->persistent = false;
2966
0
    }
2967
332k
    pval->size = strlen((const char *)pval->data);
2968
332k
}
2969
2970
int
2971
gs_setdevicenprofileicc(const gs_gstate * pgs, gs_param_string * pval)
2972
695k
{
2973
695k
    int code = 0;
2974
695k
    char *pname, *pstr, *pstrend, *last = NULL;
2975
695k
    int namelen = (pval->size)+1;
2976
695k
    gs_memory_t *mem = pgs->memory;
2977
2978
    /* Check if it was "NULL" */
2979
695k
    if (pval->size != 0) {
2980
        /* The DeviceN name can have multiple files
2981
           in it.  This way we can define all the
2982
           DeviceN color spaces with ICC profiles.
2983
           divide using , and ; delimeters as well as
2984
           remove leading and ending spaces (file names
2985
           can have internal spaces). */
2986
0
        pname = (char *)gs_alloc_bytes(mem, namelen,
2987
0
                                     "set_devicen_profile_icc");
2988
0
        if (pname == NULL)
2989
0
            return_error(gs_error_VMerror);
2990
0
        memcpy(pname,pval->data,namelen-1);
2991
0
        pname[namelen-1] = 0;
2992
0
        pstr = gs_strtok(pname, ",;", &last);
2993
0
        while (pstr != NULL) {
2994
0
            namelen = strlen(pstr);
2995
            /* Remove leading and trailing spaces from the name */
2996
0
            while ( namelen > 0 && pstr[0] == 0x20) {
2997
0
                pstr++;
2998
0
                namelen--;
2999
0
            }
3000
0
            namelen = strlen(pstr);
3001
0
            pstrend = &(pstr[namelen-1]);
3002
0
            while ( namelen > 0 && pstrend[0] == 0x20) {
3003
0
                pstrend--;
3004
0
                namelen--;
3005
0
            }
3006
0
            code = gsicc_set_profile(pgs->icc_manager, (const char*) pstr, namelen, DEVICEN_TYPE);
3007
0
            if (code < 0)
3008
0
                return gs_throw(code, "cannot find devicen icc profile");
3009
0
            pstr = gs_strtok(NULL, ",;", &last);
3010
0
        }
3011
0
        gs_free_object(mem, pname,
3012
0
        "set_devicen_profile_icc");
3013
0
        return code;
3014
0
    }
3015
695k
    return 0;
3016
695k
}
3017
3018
void
3019
gs_currentdefaultgrayicc(const gs_gstate * pgs, gs_param_string * pval)
3020
332k
{
3021
332k
    static const char *const rfs = DEFAULT_GRAY_ICC;
3022
3023
332k
    if (pgs->icc_manager->default_gray == NULL) {
3024
166k
        pval->data = (const byte *) rfs;
3025
166k
        pval->persistent = true;
3026
166k
    } else {
3027
166k
        pval->data = (const byte *) (pgs->icc_manager->default_gray->name);
3028
166k
        pval->persistent = false;
3029
166k
    }
3030
332k
    pval->size = strlen((const char *)pval->data);
3031
332k
}
3032
3033
int
3034
gs_setdefaultgrayicc(const gs_gstate * pgs, gs_param_string * pval)
3035
696k
{
3036
696k
    int code;
3037
696k
    char *pname;
3038
696k
    int namelen = (pval->size)+1;
3039
696k
    gs_memory_t *mem = pgs->memory;
3040
696k
    bool not_initialized;
3041
3042
    /* Detect if this is our first time in here.  If so, then we need to
3043
       reset up the default gray color spaces that are in the graphic state
3044
       to be ICC based.  It was not possible to do it until after we get
3045
       the profile */
3046
696k
    not_initialized = (pgs->icc_manager->default_gray == NULL);
3047
3048
696k
    pname = (char *)gs_alloc_bytes(mem, namelen,
3049
696k
                             "set_default_gray_icc");
3050
696k
    if (pname == NULL)
3051
0
        return_error(gs_error_VMerror);
3052
696k
    memcpy(pname,pval->data,namelen-1);
3053
696k
    pname[namelen-1] = 0;
3054
696k
    code = gsicc_set_profile(pgs->icc_manager,
3055
696k
        (const char*) pname, namelen-1, DEFAULT_GRAY);
3056
696k
    gs_free_object(mem, pname,
3057
696k
        "set_default_gray_icc");
3058
696k
    if (code < 0)
3059
0
        return gs_throw(code, "cannot find default gray icc profile");
3060
    /* if this is our first time in here then we need to properly install the
3061
       color spaces that were initialized in the graphic state at this time */
3062
696k
    if (not_initialized) {
3063
166k
        code = gsicc_init_gs_colors((gs_gstate*) pgs);
3064
166k
    }
3065
696k
    if (code < 0)
3066
0
        return gs_throw(code, "error initializing gstate color spaces to icc");
3067
696k
    return code;
3068
696k
}
3069
3070
void
3071
gs_currenticcdirectory(const gs_gstate * pgs, gs_param_string * pval)
3072
332k
{
3073
332k
    static const char *const rfs = DEFAULT_DIR_ICC;   /* as good as any other */
3074
332k
    const gs_lib_ctx_t *lib_ctx = pgs->memory->gs_lib_ctx;
3075
3076
332k
    if (lib_ctx->profiledir == NULL) {
3077
0
        pval->data = (const byte *)rfs;
3078
0
        pval->size = strlen(rfs);
3079
0
        pval->persistent = true;
3080
332k
    } else {
3081
332k
        pval->data = (const byte *)(lib_ctx->profiledir);
3082
332k
        pval->size = lib_ctx->profiledir_len;
3083
332k
        pval->persistent = false;
3084
332k
    }
3085
332k
}
3086
3087
int
3088
gs_seticcdirectory(const gs_gstate * pgs, gs_param_string * pval)
3089
695k
{
3090
695k
    char *pname;
3091
695k
    int namelen = (pval->size)+1;
3092
695k
    gs_memory_t *mem = (gs_memory_t *)pgs->memory;
3093
3094
    /* Check if it was "NULL" */
3095
695k
    if (pval->size != 0 ) {
3096
695k
        pname = (char *)gs_alloc_bytes(mem, namelen,
3097
695k
                                       "gs_seticcdirectory");
3098
695k
        if (pname == NULL)
3099
0
            return gs_rethrow(gs_error_VMerror, "cannot allocate directory name");
3100
695k
        memcpy(pname,pval->data,namelen-1);
3101
695k
        pname[namelen-1] = 0;
3102
695k
        if (gs_lib_ctx_set_icc_directory(mem, (const char*) pname, namelen-1) < 0) {
3103
0
            gs_free_object(mem, pname, "gs_seticcdirectory");
3104
0
            return -1;
3105
0
        }
3106
695k
        gs_free_object(mem, pname, "gs_seticcdirectory");
3107
695k
    }
3108
695k
    return 0;
3109
695k
}
3110
3111
void
3112
gs_currentsrcgtagicc(const gs_gstate * pgs, gs_param_string * pval)
3113
332k
{
3114
332k
    if (pgs->icc_manager->srcgtag_profile == NULL) {
3115
332k
        pval->data = NULL;
3116
332k
        pval->size = 0;
3117
332k
        pval->persistent = true;
3118
332k
    } else {
3119
0
        pval->data = (byte *)pgs->icc_manager->srcgtag_profile->name;
3120
0
        pval->size = pgs->icc_manager->srcgtag_profile->name_length;
3121
0
        pval->persistent = false;
3122
0
    }
3123
332k
}
3124
3125
int
3126
gs_setsrcgtagicc(const gs_gstate * pgs, gs_param_string * pval)
3127
695k
{
3128
695k
    int code;
3129
695k
    char *pname;
3130
695k
    int namelen = (pval->size)+1;
3131
695k
    gs_memory_t *mem = pgs->memory;
3132
3133
695k
    if (pval->size == 0) return 0;
3134
0
    pname = (char *)gs_alloc_bytes(mem, namelen, "set_srcgtag_icc");
3135
0
    if (pname == NULL)
3136
0
        return_error(gs_error_VMerror);
3137
0
    memcpy(pname,pval->data,namelen-1);
3138
0
    pname[namelen-1] = 0;
3139
0
    code = gsicc_set_srcgtag_struct(pgs->device, pgs->icc_manager, (const char*) pname,
3140
0
                                   namelen);
3141
0
    gs_free_object(mem, pname, "set_srcgtag_icc");
3142
0
    if (code < 0)
3143
0
        return gs_rethrow(code, "cannot find srctag file");
3144
0
    return code;
3145
0
}
3146
3147
void
3148
gs_currentdefaultrgbicc(const gs_gstate * pgs, gs_param_string * pval)
3149
332k
{
3150
332k
    static const char *const rfs = DEFAULT_RGB_ICC;
3151
3152
332k
    if (pgs->icc_manager->default_rgb == NULL) {
3153
166k
        pval->data = (const byte *) rfs;
3154
166k
        pval->persistent = true;
3155
166k
    } else {
3156
166k
        pval->data = (const byte *) (pgs->icc_manager->default_rgb->name);
3157
166k
        pval->persistent = false;
3158
166k
    }
3159
332k
    pval->size = strlen((const char *)pval->data);
3160
332k
}
3161
3162
int
3163
gs_setdefaultrgbicc(const gs_gstate * pgs, gs_param_string * pval)
3164
697k
{
3165
697k
    int code;
3166
697k
    char *pname;
3167
697k
    int namelen = (pval->size)+1;
3168
697k
    gs_memory_t *mem = pgs->memory;
3169
3170
697k
    pname = (char *)gs_alloc_bytes(mem, namelen,
3171
697k
                             "set_default_rgb_icc");
3172
697k
    if (pname == NULL)
3173
0
        return_error(gs_error_VMerror);
3174
697k
    memcpy(pname,pval->data,namelen-1);
3175
697k
    pname[namelen-1] = 0;
3176
697k
    code = gsicc_set_profile(pgs->icc_manager,
3177
697k
        (const char*) pname, namelen-1, DEFAULT_RGB);
3178
697k
    gs_free_object(mem, pname,
3179
697k
        "set_default_rgb_icc");
3180
697k
    if (code < 0)
3181
0
        return gs_rethrow(code, "cannot find default rgb icc profile");
3182
697k
    return code;
3183
697k
}
3184
3185
void
3186
gs_currentnamedicc(const gs_gstate * pgs, gs_param_string * pval)
3187
332k
{
3188
332k
    static const char *const rfs = "";
3189
3190
332k
    if (pgs->icc_manager->device_named == NULL) {
3191
332k
        pval->data = (const byte *) rfs;
3192
332k
        pval->persistent = true;
3193
332k
    } else {
3194
0
        pval->data = (const byte *) (pgs->icc_manager->device_named->name);
3195
0
        pval->persistent = false;
3196
0
    }
3197
332k
    pval->size = strlen((const char *)pval->data);
3198
332k
}
3199
3200
int
3201
gs_setnamedprofileicc(const gs_gstate * pgs, gs_param_string * pval)
3202
695k
{
3203
695k
    int code;
3204
695k
    char* pname;
3205
695k
    int namelen = (pval->size)+1;
3206
695k
    gs_memory_t *mem = pgs->memory;
3207
3208
    /* Check if it was "NULL" */
3209
695k
    if (pval->size != 0) {
3210
0
        pname = (char *)gs_alloc_bytes(mem, namelen,
3211
0
                                 "set_named_profile_icc");
3212
0
        if (pname == NULL)
3213
0
            return_error(gs_error_VMerror);
3214
0
        memcpy(pname,pval->data,namelen-1);
3215
0
        pname[namelen-1] = 0;
3216
0
        code = gsicc_set_profile(pgs->icc_manager,
3217
0
            (const char*) pname, namelen-1, NAMED_TYPE);
3218
0
        gs_free_object(mem, pname,
3219
0
                "set_named_profile_icc");
3220
0
        if (code < 0)
3221
0
            return gs_rethrow(code, "cannot find named color icc profile");
3222
0
        return code;
3223
0
    }
3224
695k
    return 0;
3225
695k
}
3226
3227
void
3228
gs_currentdefaultcmykicc(const gs_gstate * pgs, gs_param_string * pval)
3229
332k
{
3230
332k
    static const char *const rfs = DEFAULT_CMYK_ICC;
3231
3232
332k
    if (pgs->icc_manager->default_cmyk == NULL) {
3233
166k
        pval->data = (const byte *) rfs;
3234
166k
        pval->persistent = true;
3235
166k
    } else {
3236
166k
        pval->data = (const byte *) (pgs->icc_manager->default_cmyk->name);
3237
166k
        pval->persistent = false;
3238
166k
    }
3239
332k
    pval->size = strlen((const char *)pval->data);
3240
332k
}
3241
3242
int
3243
gs_setdefaultcmykicc(const gs_gstate * pgs, gs_param_string * pval)
3244
696k
{
3245
696k
    int code;
3246
696k
    char* pname;
3247
696k
    int namelen = (pval->size)+1;
3248
696k
    gs_memory_t *mem = pgs->memory;
3249
3250
696k
    pname = (char *)gs_alloc_bytes(mem, namelen,
3251
696k
                             "set_default_cmyk_icc");
3252
696k
    if (pname == NULL)
3253
0
        return_error(gs_error_VMerror);
3254
696k
    memcpy(pname,pval->data,namelen-1);
3255
696k
    pname[namelen-1] = 0;
3256
696k
    code = gsicc_set_profile(pgs->icc_manager,
3257
696k
        (const char*) pname, namelen-1, DEFAULT_CMYK);
3258
696k
    gs_free_object(mem, pname,
3259
696k
                "set_default_cmyk_icc");
3260
696k
    if (code < 0)
3261
0
        return gs_throw(code, "cannot find default cmyk icc profile");
3262
696k
    return code;
3263
696k
}
3264
3265
void
3266
gs_currentlabicc(const gs_gstate * pgs, gs_param_string * pval)
3267
332k
{
3268
332k
    static const char *const rfs = LAB_ICC;
3269
3270
332k
    pval->data = (const byte *)( (pgs->icc_manager->lab_profile == NULL) ?
3271
166k
                        rfs : pgs->icc_manager->lab_profile->name);
3272
332k
    pval->size = strlen((const char *)pval->data);
3273
332k
    pval->persistent = true;
3274
332k
}
3275
3276
int
3277
gs_setlabicc(const gs_gstate * pgs, gs_param_string * pval)
3278
695k
{
3279
695k
    int code;
3280
695k
    char* pname;
3281
695k
    int namelen = (pval->size)+1;
3282
695k
    gs_memory_t *mem = pgs->memory;
3283
3284
695k
    pname = (char *)gs_alloc_bytes(mem, namelen,
3285
695k
                             "set_lab_icc");
3286
695k
    if (pname == NULL)
3287
0
        return_error(gs_error_VMerror);
3288
695k
    memcpy(pname,pval->data,namelen-1);
3289
695k
    pname[namelen-1] = 0;
3290
695k
    code = gsicc_set_profile(pgs->icc_manager,
3291
695k
        (const char*) pname, namelen-1, LAB_TYPE);
3292
695k
    gs_free_object(mem, pname,
3293
695k
                "set_lab_icc");
3294
695k
    if (code < 0)
3295
0
        return gs_throw(code, "cannot find default lab icc profile");
3296
695k
    return code;
3297
695k
}