Coverage Report

Created: 2026-10-28 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gscspace.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
17
/* Color space operators and support */
18
#include "memory_.h"
19
#include "gx.h"
20
#include "gserrors.h"
21
#include "gsstruct.h"
22
#include "gsccolor.h"
23
#include "gsutil.h"   /* for gs_next_ids */
24
#include "gxcmap.h"
25
#include "gxcspace.h"
26
#include "gxgstate.h"
27
#include "gsovrc.h"
28
#include "gsstate.h"
29
#include "gsdevice.h"
30
#include "gxdevcli.h"
31
#include "gzstate.h"
32
#include "stream.h"
33
#include "gsnamecl.h"  /* Custom color call back define */
34
#include "gsicc.h"
35
#include "gsicc_manage.h"
36
#include "string_.h"
37
#include "strmio.h"         /* needed for sfclose */
38
#include "gsicc_cache.h"    /* Needed for gsicc_get_icc_buff_hash */
39
#include "gxdevsop.h"
40
41
static cs_proc_install_cspace(gx_install_DeviceGray);
42
static cs_proc_install_cspace(gx_install_DeviceRGB);
43
static cs_proc_install_cspace(gx_install_DeviceCMYK);
44
/*
45
 * Define the standard color space types.  We include DeviceCMYK in the base
46
 * build because it's too awkward to omit it, but we don't provide any of
47
 * the PostScript operator procedures (setcmykcolor, etc.) for dealing with
48
 * it.
49
 */
50
static const gs_color_space_type gs_color_space_type_DeviceGray = {
51
    gs_color_space_index_DeviceGray, true, true,
52
    &st_base_color_space, gx_num_components_1,
53
    gx_init_paint_1, gx_restrict01_paint_1,
54
    gx_same_concrete_space,
55
    gx_concretize_DeviceGray, gx_remap_concrete_DGray,
56
    gx_remap_DeviceGray, gx_install_DeviceGray,
57
    gx_spot_colors_set_overprint,
58
    NULL, gx_no_adjust_color_count,
59
    gx_serialize_cspace_type,
60
    gx_cspace_is_linear_default, gx_polarity_additive
61
};
62
static const gs_color_space_type gs_color_space_type_DeviceRGB = {
63
    gs_color_space_index_DeviceRGB, true, true,
64
    &st_base_color_space, gx_num_components_3,
65
    gx_init_paint_3, gx_restrict01_paint_3,
66
    gx_same_concrete_space,
67
    gx_concretize_DeviceRGB, gx_remap_concrete_DRGB,
68
    gx_remap_DeviceRGB, gx_install_DeviceRGB,
69
    gx_spot_colors_set_overprint,
70
    NULL, gx_no_adjust_color_count,
71
    gx_serialize_cspace_type,
72
    gx_cspace_is_linear_default, gx_polarity_additive
73
};
74
75
static cs_proc_set_overprint(gx_set_overprint_DeviceCMYK);
76
77
static const gs_color_space_type gs_color_space_type_DeviceCMYK = {
78
    gs_color_space_index_DeviceCMYK, true, true,
79
    &st_base_color_space, gx_num_components_4,
80
    gx_init_paint_4, gx_restrict01_paint_4,
81
    gx_same_concrete_space,
82
    gx_concretize_DeviceCMYK, gx_remap_concrete_DCMYK,
83
    gx_remap_DeviceCMYK, gx_install_DeviceCMYK,
84
    gx_set_overprint_DeviceCMYK,
85
    NULL, gx_no_adjust_color_count,
86
    gx_serialize_cspace_type,
87
    gx_cspace_is_linear_default, gx_polarity_subtractive
88
};
89
90
/* Structure descriptors */
91
public_st_color_space();
92
public_st_base_color_space();
93
94
/* ------ Create/copy/destroy ------ */
95
96
/* Ghostscript object finalizers can be called many times and hence
97
 * must be idempotent. */
98
static void
99
gs_cspace_final(const gs_memory_t *cmem, void *vptr)
100
31.4M
{
101
31.4M
    gs_color_space *pcs = (gs_color_space *)vptr;
102
31.4M
    (void)cmem; /* unused */
103
104
31.4M
    if (pcs->interpreter_free_cspace_proc != NULL) {
105
1.33M
        (*pcs->interpreter_free_cspace_proc) ((gs_memory_t *)cmem, pcs);
106
1.33M
        pcs->interpreter_free_cspace_proc = NULL;
107
1.33M
    }
108
31.4M
    if (pcs->type->final)
109
18.5M
        pcs->type->final(pcs);
110
31.4M
    if_debug2m('c', cmem, "[c]cspace final "PRI_INTPTR" %d\n", (intptr_t)pcs, (int)pcs->id);
111
31.4M
    rc_decrement_only_cs(pcs->base_space, "gs_cspace_final");
112
31.4M
    pcs->base_space = NULL;
113
31.4M
    if (gs_color_space_get_index(pcs) == gs_color_space_index_DeviceN) {
114
4.53k
        if (pcs->params.device_n.devn_process_space != NULL) {
115
0
            rc_decrement_only_cs(pcs->params.device_n.devn_process_space, "gs_cspace_final");
116
0
            pcs->params.device_n.devn_process_space = NULL;
117
0
        }
118
4.53k
    }
119
    /* No need to decrement the ICC profile data.  It is handled
120
       by the finalize of the ICC space which is called above using
121
       pcs->type->final(pcs);  */
122
31.4M
}
123
124
static gs_color_space *
125
gs_cspace_alloc_with_id(gs_memory_t *mem, ulong id,
126
                   const gs_color_space_type *pcstype)
127
31.4M
{
128
31.4M
    gs_color_space *pcs;
129
130
31.4M
    rc_alloc_struct_1(pcs, gs_color_space, &st_color_space, mem, return NULL,
131
31.4M
                      "gs_cspace_alloc_with_id");
132
31.4M
    if_debug3m('c', mem, "[c]cspace alloc "PRI_INTPTR" %s %d\n",
133
31.4M
               (intptr_t)pcs, pcstype->stype->sname, pcstype->index);
134
31.4M
    pcs->type = pcstype;
135
31.4M
    pcs->id = id;
136
31.4M
    pcs->base_space = NULL;
137
31.4M
    pcs->pclient_color_space_data = NULL;
138
31.4M
    pcs->interpreter_data = NULL;
139
31.4M
    pcs->interpreter_free_cspace_proc = NULL;
140
31.4M
    pcs->cmm_icc_profile_data = NULL;
141
31.4M
    pcs->ICC_Alternate_space = gs_ICC_Alternate_None;
142
31.4M
    pcs->icc_equivalent = NULL;
143
31.4M
    pcs->params.device_n.devn_process_space = NULL;
144
31.4M
    pcs->params.device_n.all_none = false;
145
31.4M
    return pcs;
146
31.4M
}
147
148
static cs_proc_install_cspace(gx_install_DeviceGray);
149
static cs_proc_install_cspace(gx_install_DeviceRGB);
150
static cs_proc_install_cspace(gx_install_DeviceCMYK);
151
152
/*
153
 * Generic allocation function for colorspace implementations. Return
154
 * NULL on allocation failure.
155
 */
156
gs_color_space *
157
gs_cspace_alloc(gs_memory_t *mem, const gs_color_space_type *pcstype)
158
4.31M
{
159
4.31M
    return gs_cspace_alloc_with_id(mem, gs_next_ids(mem, 1), pcstype);
160
4.31M
}
161
162
/* Constructors for simple device color spaces. */
163
164
gs_color_space *
165
gs_cspace_new_DeviceGray(gs_memory_t *mem)
166
18.5M
{
167
18.5M
    return gs_cspace_alloc_with_id(mem, cs_DeviceGray_id,
168
18.5M
                                   &gs_color_space_type_DeviceGray);
169
18.5M
}
170
171
gs_color_space *
172
gs_cspace_new_DeviceRGB(gs_memory_t *mem)
173
5.72M
{
174
5.72M
    return gs_cspace_alloc_with_id(mem, cs_DeviceRGB_id,
175
5.72M
                                   &gs_color_space_type_DeviceRGB);
176
5.72M
}
177
gs_color_space *
178
gs_cspace_new_DeviceCMYK(gs_memory_t *mem)
179
2.80M
{
180
2.80M
    return gs_cspace_alloc_with_id(mem, cs_DeviceCMYK_id,
181
2.80M
                                   &gs_color_space_type_DeviceCMYK);
182
2.80M
}
183
184
/* For use in initializing ICC color spaces for XPS */
185
gs_color_space *
186
gs_cspace_new_scrgb(gs_memory_t *pmem, gs_gstate * pgs)
187
16.0k
{
188
16.0k
    gs_color_space *pcspace = gs_cspace_alloc(pmem, &gs_color_space_type_ICC);
189
16.0k
    cmm_profile_t *profile;
190
16.0k
    stream *str;
191
16.0k
    int code;
192
193
16.0k
    if (pcspace == NULL)
194
0
        return pcspace;
195
196
16.0k
    code = gsicc_open_search(SCRGB, strlen(SCRGB), pmem, pmem->gs_lib_ctx->profiledir,
197
16.0k
        pmem->gs_lib_ctx->profiledir_len, &str);
198
199
16.0k
    if (code < 0 || str == NULL) {
200
0
        rc_decrement(pcspace, "gs_cspace_new_scrgb");
201
0
        return NULL;
202
0
    }
203
204
16.0k
    pcspace->cmm_icc_profile_data = gsicc_profile_new(str, pmem, SCRGB, strlen(SCRGB));
205
16.0k
    code = sfclose(str);
206
16.0k
    if (pcspace->cmm_icc_profile_data == NULL) {
207
0
        rc_decrement(pcspace, "gs_cspace_new_scrgb");
208
0
        return NULL;
209
0
    }
210
211
    /* Get the profile handle */
212
16.0k
    pcspace->cmm_icc_profile_data->profile_handle =
213
16.0k
        gsicc_get_profile_handle_buffer(pcspace->cmm_icc_profile_data->buffer,
214
16.0k
            pcspace->cmm_icc_profile_data->buffer_size, pmem);
215
16.0k
    if (!pcspace->cmm_icc_profile_data->profile_handle) {
216
0
        rc_decrement(pcspace, "gs_cspace_new_scrgb");
217
0
        return NULL;
218
0
    }
219
16.0k
    profile = pcspace->cmm_icc_profile_data;
220
221
    /* Compute the hash code of the profile. Everything in the
222
    ICC manager will have it's hash code precomputed */
223
16.0k
    gsicc_get_icc_buff_hash(profile->buffer, &(profile->hashcode),
224
16.0k
        profile->buffer_size);
225
16.0k
    profile->hash_is_valid = true;
226
16.0k
    profile->num_comps =
227
16.0k
        gscms_get_input_channel_count(profile->profile_handle, profile->memory);
228
16.0k
    if (profile->num_comps > ICC_MAX_CHANNELS) {
229
0
        rc_decrement(pcspace, "gs_cspace_new_scrgb");
230
0
        return NULL;
231
0
    }
232
16.0k
    profile->num_comps_out =
233
16.0k
        gscms_get_output_channel_count(profile->profile_handle, profile->memory);
234
16.0k
    profile->data_cs =
235
16.0k
        gscms_get_profile_data_space(profile->profile_handle, profile->memory);
236
16.0k
    gsicc_set_icc_range(&profile);
237
16.0k
    return pcspace;
238
16.0k
}
239
240
gs_color_space *
241
gs_cspace_new_ICC(gs_memory_t *pmem, gs_gstate * pgs, int components)
242
2.71M
{
243
2.71M
    gsicc_manager_t *icc_manage = pgs->icc_manager;
244
2.71M
    int code = 0;
245
2.71M
    gs_color_space *pcspace = gs_cspace_alloc(pmem, &gs_color_space_type_ICC);
246
247
2.71M
    if (pcspace == NULL)
248
0
        return pcspace;
249
250
2.71M
    switch (components) {
251
16.0k
        case -1: /* alpha case */
252
16.0k
            if (icc_manage->smask_profiles == NULL) {
253
16.0k
                code = gsicc_initialize_iccsmask(icc_manage);
254
16.0k
            }
255
16.0k
            if (code == 0) {
256
16.0k
                pcspace->cmm_icc_profile_data =
257
16.0k
                    icc_manage->smask_profiles->smask_gray;
258
16.0k
            } else {
259
0
                pcspace->cmm_icc_profile_data = icc_manage->default_gray;
260
0
            }
261
16.0k
            break;
262
0
        case -3: /* alpha case.  needs linear RGB */
263
0
            if (icc_manage->smask_profiles == NULL) {
264
0
                code = gsicc_initialize_iccsmask(icc_manage);
265
0
            }
266
0
            if (code == 0) {
267
0
                pcspace->cmm_icc_profile_data =
268
0
                    icc_manage->smask_profiles->smask_rgb;
269
0
            } else {
270
0
                pcspace->cmm_icc_profile_data = icc_manage->default_rgb;
271
0
            }
272
0
            break;
273
2.49M
        case 1: pcspace->cmm_icc_profile_data = icc_manage->default_gray; break;
274
112k
        case 3: pcspace->cmm_icc_profile_data = icc_manage->default_rgb; break;
275
96.0k
        case 4: pcspace->cmm_icc_profile_data = icc_manage->default_cmyk; break;
276
0
        default: rc_decrement(pcspace,"gs_cspace_new_ICC"); return NULL;
277
2.71M
    }
278
2.71M
    gsicc_adjust_profile_rc(pcspace->cmm_icc_profile_data, 1, "gs_cspace_new_ICC");
279
2.71M
    return pcspace;
280
2.71M
}
281
282
/* ------ Accessors ------ */
283
284
/* Get the index of a color space. */
285
gs_color_space_index
286
gs_color_space_get_index(const gs_color_space * pcs)
287
82.4M
{
288
82.4M
    return pcs->type->index;
289
82.4M
}
290
291
/* See if the space is CIE based */
292
bool gs_color_space_is_CIE(const gs_color_space * pcs)
293
902k
{
294
902k
    switch(gs_color_space_get_index(pcs)){
295
0
        case gs_color_space_index_CIEDEFG:
296
0
        case gs_color_space_index_CIEDEF:
297
0
        case gs_color_space_index_CIEABC:
298
0
        case gs_color_space_index_CIEA:
299
902k
        case gs_color_space_index_ICC:
300
902k
            return true;
301
0
        break;
302
633
        default:
303
633
            return false;
304
902k
    }
305
902k
}
306
307
/* See if the space is Postscript CIE based */
308
bool gs_color_space_is_PSCIE(const gs_color_space * pcs)
309
33.6M
{
310
33.6M
    switch(gs_color_space_get_index(pcs)){
311
0
        case gs_color_space_index_CIEDEFG:
312
0
        case gs_color_space_index_CIEDEF:
313
0
        case gs_color_space_index_CIEABC:
314
0
        case gs_color_space_index_CIEA:
315
0
            return true;
316
0
        break;
317
33.6M
        default:
318
33.6M
            return false;
319
33.6M
}
320
33.6M
}
321
322
/* See if the space is ICC based */
323
bool gs_color_space_is_ICC(const gs_color_space * pcs)
324
2.01M
{
325
2.01M
    return(gs_color_space_get_index(pcs) == gs_color_space_index_ICC);
326
2.01M
}
327
328
/* Get the number of components in a color space. */
329
int
330
gs_color_space_num_components(const gs_color_space * pcs)
331
13.3M
{
332
13.3M
    return cs_num_components(pcs);
333
13.3M
}
334
335
/* Restrict a color to its legal range. */
336
void
337
gs_color_space_restrict_color(gs_client_color *pcc, const gs_color_space *pcs)
338
0
{
339
0
    cs_restrict_color(pcc, pcs);
340
0
}
341
342
/* Install a DeviceGray color space. */
343
static int
344
gx_install_DeviceGray(gs_color_space * pcs, gs_gstate * pgs)
345
13.0M
{
346
    /* If we already have profile data installed, nothing to do here. */
347
13.0M
    if (pcs->cmm_icc_profile_data != NULL)
348
0
        return 0;
349
350
    /* If we haven't initialised the iccmanager, do it now. */
351
13.0M
    if (pgs->icc_manager->default_gray == NULL) {
352
2.14M
        int code = gsicc_init_iccmanager(pgs);
353
2.14M
        if (code < 0)
354
0
            return code;
355
2.14M
    }
356
357
    /* pcs takes a reference to the default_gray profile data */
358
13.0M
    pcs->cmm_icc_profile_data = pgs->icc_manager->default_gray;
359
13.0M
    gsicc_adjust_profile_rc(pgs->icc_manager->default_gray, 1, "gx_install_DeviceGray");
360
13.0M
    pcs->type = &gs_color_space_type_ICC;
361
13.0M
    return 0;
362
13.0M
}
363
364
int
365
gx_num_components_1(const gs_color_space * pcs)
366
19.8M
{
367
19.8M
    return 1;
368
19.8M
}
369
int
370
gx_num_components_3(const gs_color_space * pcs)
371
33.5k
{
372
33.5k
    return 3;
373
33.5k
}
374
int
375
gx_num_components_4(const gs_color_space * pcs)
376
0
{
377
0
    return 4;
378
0
}
379
380
gx_color_polarity_t
381
gx_polarity_subtractive(const gs_color_space * pcs)
382
0
{
383
0
    return GX_CINFO_POLARITY_SUBTRACTIVE;
384
0
}
385
386
gx_color_polarity_t
387
gx_polarity_additive(const gs_color_space * pcs)
388
0
{
389
0
    return GX_CINFO_POLARITY_ADDITIVE;
390
0
}
391
392
gx_color_polarity_t
393
gx_polarity_unknown(const gs_color_space * pcs)
394
0
{
395
0
    return GX_CINFO_POLARITY_UNKNOWN;
396
0
}
397
398
/*
399
 * For color spaces that have a base or alternative color space, return that
400
 * color space. Otherwise return null.
401
 */
402
const gs_color_space *
403
gs_cspace_base_space(const gs_color_space * pcspace)
404
169k
{
405
169k
    return pcspace->base_space;
406
169k
}
407
408
const gs_color_space *
409
gs_cspace_devn_process_space(const gs_color_space * pcspace)
410
0
{
411
0
    return pcspace->params.device_n.devn_process_space;
412
0
}
413
414
/* Abstract the reference counting for color spaces
415
   so that we can also increment the ICC profile
416
   if there is one associated with the color space */
417
418
void rc_increment_cs(gs_color_space *pcs)
419
18.4M
{
420
18.4M
    rc_increment(pcs);
421
18.4M
}
422
423
5.57M
void rc_decrement_cs(gs_color_space *pcs, const char *cname) {
424
425
5.57M
    if (pcs) {
426
5.57M
        rc_decrement(pcs, cname);
427
5.57M
    }
428
5.57M
}
429
430
void rc_decrement_only_cs(gs_color_space *pcs, const char *cname)
431
62.9M
{
432
62.9M
    if (pcs) {
433
31.9M
        rc_decrement_only(pcs, cname);
434
31.9M
    }
435
62.9M
}
436
437
void cs_adjust_counts_icc(gs_gstate *pgs, int delta)
438
129M
{
439
129M
    gs_color_space *pcs = gs_currentcolorspace_inline(pgs);
440
441
129M
    if (pcs) {
442
86.5M
        cs_adjust_color_count(pgs, delta);
443
86.5M
        rc_adjust_const(pcs, delta, "cs_adjust_counts_icc");
444
86.5M
    }
445
129M
}
446
447
void cs_adjust_swappedcounts_icc(gs_gstate *pgs, int delta)
448
129M
{
449
129M
    gs_color_space *pcs = gs_swappedcolorspace_inline(pgs);
450
451
129M
    if (pcs) {
452
86.5M
        cs_adjust_swappedcolor_count(pgs, delta);
453
86.5M
        rc_adjust_const(pcs, delta, "cs_adjust_swappedcounts_icc");
454
86.5M
    }
455
129M
}
456
457
/* ------ Other implementation procedures ------ */
458
459
/* Null color space installation procedure. */
460
int
461
gx_no_install_cspace(gs_color_space * pcs, gs_gstate * pgs)
462
0
{
463
0
    return 0;
464
0
}
465
466
/* Install a DeviceRGB color space. */
467
static int
468
gx_install_DeviceRGB(gs_color_space * pcs, gs_gstate * pgs)
469
1.12M
{
470
    /* If we already have profile_data, nothing to do here. */
471
1.12M
    if (pcs->cmm_icc_profile_data != NULL)
472
0
        return 0;
473
474
    /* If the icc manager hasn't been set up yet, then set it up. */
475
1.12M
    if (pgs->icc_manager->default_rgb == NULL)
476
0
        gsicc_init_iccmanager(pgs);
477
478
    /* pcs takes a reference to default_rgb */
479
1.12M
    pcs->cmm_icc_profile_data = pgs->icc_manager->default_rgb;
480
1.12M
    gsicc_adjust_profile_rc(pcs->cmm_icc_profile_data, 1, "gx_install_DeviceRGB");
481
1.12M
    pcs->type = &gs_color_space_type_ICC;
482
1.12M
    return 0;
483
1.12M
}
484
485
/* Install a DeviceCMYK color space. */
486
static int
487
gx_install_DeviceCMYK(gs_color_space * pcs, gs_gstate * pgs)
488
105k
{
489
    /* If we already have profile data, nothing to do here. */
490
105k
    if (pcs->cmm_icc_profile_data != NULL)
491
0
        return 0;
492
493
    /* If the icc manager hasn't been set up yet, then set it up. */
494
105k
    if (pgs->icc_manager->default_cmyk == NULL)
495
0
        gsicc_init_iccmanager(pgs);
496
497
    /* pcs takes a reference to default_cmyk */
498
105k
    pcs->cmm_icc_profile_data = pgs->icc_manager->default_cmyk;
499
105k
    gsicc_adjust_profile_rc(pcs->cmm_icc_profile_data, 1, "gx_install_DeviceCMYK");
500
105k
    pcs->type = &gs_color_space_type_ICC;
501
105k
    return 0;
502
105k
}
503
504
/*
505
 * Communicate to the overprint compositor that this particular
506
 * state overprint is not enabled.  This could be due to a
507
 * mismatched color space, or that overprint is false or the
508
 * device does not support it.
509
 */
510
int
511
gx_set_no_overprint(gs_gstate* pgs)
512
451k
{
513
451k
    gs_overprint_params_t   params = { 0 };
514
515
451k
    params.retain_any_comps = false;
516
451k
    params.op_state = GS_OP_STATE_NONE;
517
451k
    params.is_fill_color = pgs->is_fill_color;
518
451k
    params.effective_opm = pgs->color[0].effective_opm = 0;
519
520
451k
    return gs_gstate_update_overprint(pgs, &params);
521
451k
}
522
523
/* Retain all the spot colorants and not the process
524
   colorants.  This occurs if we have a process color
525
   mismatch between the source and the destination but
526
   the output device supports spot colors */
527
int
528
gx_set_spot_only_overprint(gs_gstate* pgs)
529
1.85k
{
530
1.85k
    gs_overprint_params_t   params = { 0 };
531
1.85k
    gx_device* dev = pgs->device;
532
1.85k
    gx_color_index drawn_comps = dev == NULL ? 0 : gx_get_process_comps(dev);
533
534
1.85k
    params.retain_any_comps = true;
535
1.85k
    params.op_state = GS_OP_STATE_NONE;
536
1.85k
    params.is_fill_color = pgs->is_fill_color;
537
1.85k
    params.effective_opm = pgs->color[0].effective_opm = 0;
538
1.85k
    params.drawn_comps = drawn_comps;
539
540
1.85k
    return gs_gstate_update_overprint(pgs, &params);
541
1.85k
}
542
543
/*
544
 * Push an overprint compositor onto the current device indicating that,
545
 * at most, the spot color parameters are to be preserved.
546
 *
547
 * This routine should be used for all Device, CIEBased, and ICCBased
548
 * color spaces, except for DeviceCMKY.
549
 */
550
int
551
gx_spot_colors_set_overprint(const gs_color_space * pcs, gs_gstate * pgs)
552
0
{
553
0
    gs_overprint_params_t   params = {0};
554
0
    bool op = pgs->is_fill_color ? pgs->overprint : pgs->stroke_overprint;
555
556
0
    if (!op)
557
0
        params.retain_any_comps = false;
558
0
    else
559
0
        params.retain_any_comps = true;
560
561
0
    params.is_fill_color = pgs->is_fill_color;
562
0
    params.op_state = GS_OP_STATE_NONE;
563
0
    if (params.retain_any_comps)
564
0
    {
565
0
        int nc = gs_color_space_num_components(pcs);
566
0
        params.drawn_comps = (1<<nc)-1;
567
0
    }
568
569
    /* Only DeviceCMYK case can have overprint mode set to true */
570
0
    params.effective_opm = pgs->color[0].effective_opm = 0;
571
0
    return gs_gstate_update_overprint(pgs, &params);
572
0
}
573
574
static bool
575
check_single_comp(int comp, frac targ_val, int ncomps, const frac * pval)
576
648
{
577
648
    int     i;
578
579
3.28k
    for (i = 0; i < ncomps; i++) {
580
2.63k
        if ( (i != comp && pval[i] != frac_0)  ||
581
2.63k
             (i == comp && pval[i] != targ_val)  )
582
0
            return false;
583
2.63k
    }
584
648
    return true;
585
648
}
586
587
/*
588
 * Determine if the current color model is a "DeviceCMYK" color model, and
589
 * if so what are its process color components. This information is required
590
 * when PLRM defines special rules for CMYK devices. This includes:
591
 * 1. DeviceGray to CMYK color conversion
592
 * 2. when overprint is true and overprint mode is set to 1.
593
 *
594
 * A color model is considered a "DeviceCMYK" color model if it supports the
595
 * cyan, magenta, yellow, and black color components, and maps the DeviceCMYK
596
 * color model components directly to these color components. Note that this
597
 * does not require any particular component order, allows for additional
598
 * spot color components, and does admit DeviceN color spaces if they have
599
 * the requisite behavior.
600
 *
601
 * If the color model is a "DeviceCMYK" color model, return the set of
602
 * process color components; otherwise return 0.
603
 */
604
gx_color_index
605
check_cmyk_color_model_comps(gx_device * dev)
606
899
{
607
899
    gx_device_color_info *          pcinfo = &dev->color_info;
608
899
    uchar                           ncomps = pcinfo->num_components;
609
899
    int                             cyan_c, magenta_c, yellow_c, black_c;
610
899
    frac                            frac_14 = frac_1 / 4;
611
899
    frac                            out[GX_DEVICE_COLOR_MAX_COMPONENTS];
612
899
    gx_color_index                  process_comps;
613
899
    const gx_cm_color_map_procs    *cmprocs;
614
899
    const gx_device                *cmdev;
615
616
617
899
    if (pcinfo->num_components < 4                     ||
618
172
        pcinfo->polarity == GX_CINFO_POLARITY_ADDITIVE ||
619
737
        pcinfo->gray_index == GX_CINFO_COMP_NO_INDEX) {
620
737
        pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED_NOT;
621
737
        return 0;
622
737
    }
623
624
    /* check for the appropriate components */
625
162
    if ( ncomps < 4                                       ||
626
162
         (cyan_c = dev_proc(dev, get_color_comp_index)(
627
162
                       dev,
628
162
                       "Cyan",
629
162
                       sizeof("Cyan") - 1,
630
162
                       NO_COMP_NAME_TYPE_OP)) < 0           ||
631
162
         cyan_c == GX_DEVICE_COLOR_MAX_COMPONENTS         ||
632
162
         (magenta_c = dev_proc(dev, get_color_comp_index)(
633
162
                          dev,
634
162
                          "Magenta",
635
162
                          sizeof("Magenta") - 1,
636
162
                          NO_COMP_NAME_TYPE_OP)) < 0        ||
637
162
         magenta_c == GX_DEVICE_COLOR_MAX_COMPONENTS      ||
638
162
         (yellow_c = dev_proc(dev, get_color_comp_index)(
639
162
                        dev,
640
162
                        "Yellow",
641
162
                        sizeof("Yellow") - 1,
642
162
                        NO_COMP_NAME_TYPE_OP)) < 0               ||
643
162
         yellow_c == GX_DEVICE_COLOR_MAX_COMPONENTS       ||
644
162
         (black_c = dev_proc(dev, get_color_comp_index)(
645
162
                        dev,
646
162
                        "Black",
647
162
                        sizeof("Black") - 1,
648
162
                        NO_COMP_NAME_TYPE_OP)) < 0                         ||
649
162
         black_c == GX_DEVICE_COLOR_MAX_COMPONENTS          )
650
0
        return 0;
651
652
    /* check the mapping */
653
162
    cmprocs = dev_proc(dev, get_color_mapping_procs)(dev, &cmdev);
654
655
162
    ncomps -= device_encodes_tags(dev);
656
162
    cmprocs->map_cmyk(cmdev, frac_14, frac_0, frac_0, frac_0, out);
657
162
    if (!check_single_comp(cyan_c, frac_14, ncomps, out)) {
658
0
        pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED_NOT;
659
0
        return 0;
660
0
    }
661
162
    cmprocs->map_cmyk(cmdev, frac_0, frac_14, frac_0, frac_0, out);
662
162
    if (!check_single_comp(magenta_c, frac_14, ncomps, out)) {
663
0
        pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED_NOT;
664
0
        return 0;
665
0
    }
666
162
    cmprocs->map_cmyk(cmdev, frac_0, frac_0, frac_14, frac_0, out);
667
162
    if (!check_single_comp(yellow_c, frac_14, ncomps, out)) {
668
0
        pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED_NOT;
669
0
        return 0;
670
0
    }
671
162
    cmprocs->map_cmyk(cmdev, frac_0, frac_0, frac_0, frac_14, out);
672
162
    if (!check_single_comp(black_c, frac_14, ncomps, out)) {
673
0
        pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED_NOT;
674
0
        return 0;
675
0
    }
676
677
162
    process_comps =  ((gx_color_index)1 << cyan_c)
678
162
                   | ((gx_color_index)1 << magenta_c)
679
162
                   | ((gx_color_index)1 << yellow_c)
680
162
                   | ((gx_color_index)1 << black_c);
681
162
    pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED;
682
162
    pcinfo->process_comps = process_comps;
683
162
    pcinfo->black_component = black_c;
684
162
    return process_comps;
685
162
}
686
687
static void
688
check_rgb_color_model_comps(gx_device * dev)
689
5.07k
{
690
5.07k
    gx_device_color_info *          pcinfo = &dev->color_info;
691
5.07k
    uchar                           ncomps = pcinfo->num_components;
692
5.07k
    int                             red_c, green_c, blue_c;
693
5.07k
    frac                            frac_14 = frac_1 / 4;
694
5.07k
    frac                            out[GX_DEVICE_COLOR_MAX_COMPONENTS];
695
5.07k
    gx_color_index                  process_comps;
696
5.07k
    const gx_cm_color_map_procs    *cmprocs;
697
5.07k
    const gx_device                *cmdev;
698
699
700
5.07k
    if (pcinfo->num_components < 3                     ||
701
3.21k
        pcinfo->polarity != GX_CINFO_POLARITY_ADDITIVE ||
702
3.21k
        pcinfo->gray_index == GX_CINFO_COMP_NO_INDEX   ||
703
1.33k
        dev_proc(dev, dev_spec_op)(dev, gxdso_is_sep_supporting_additive_device, NULL, 0) <= 0
704
5.07k
        ) {
705
5.07k
        pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED_NOT;
706
5.07k
        return;
707
5.07k
    }
708
709
    /* check for the appropriate components */
710
0
    if ( ncomps < 3                                      ||
711
0
         (red_c = dev_proc(dev, get_color_comp_index)(
712
0
                       dev,
713
0
                       "Red",
714
0
                       sizeof("Red") - 1,
715
0
                       NO_COMP_NAME_TYPE_OP)) < 0        ||
716
0
         red_c == GX_DEVICE_COLOR_MAX_COMPONENTS         ||
717
0
         (green_c = dev_proc(dev, get_color_comp_index)(
718
0
                          dev,
719
0
                          "Green",
720
0
                          sizeof("Green") - 1,
721
0
                          NO_COMP_NAME_TYPE_OP)) < 0     ||
722
0
         green_c == GX_DEVICE_COLOR_MAX_COMPONENTS       ||
723
0
         (blue_c = dev_proc(dev, get_color_comp_index)(
724
0
                        dev,
725
0
                        "Blue",
726
0
                        sizeof("Blue") - 1,
727
0
                        NO_COMP_NAME_TYPE_OP)) < 0       ||
728
0
         blue_c == GX_DEVICE_COLOR_MAX_COMPONENTS)
729
0
    {
730
0
        pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED_NOT;
731
0
        return;
732
0
    }
733
734
    /* check the mapping */
735
0
    cmprocs = dev_proc(dev, get_color_mapping_procs)(dev, &cmdev);
736
737
0
    ncomps -= device_encodes_tags(dev);
738
0
    cmprocs->map_rgb(cmdev, NULL, frac_14, frac_0, frac_0, out);
739
0
    if (!check_single_comp(red_c, frac_14, ncomps, out)) {
740
0
        pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED_NOT;
741
0
        return;
742
0
    }
743
0
    cmprocs->map_rgb(cmdev, NULL, frac_0, frac_14, frac_0, out);
744
0
    if (!check_single_comp(green_c, frac_14, ncomps, out)) {
745
0
        pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED_NOT;
746
0
        return;
747
0
    }
748
0
    cmprocs->map_rgb(cmdev, NULL, frac_0, frac_0, frac_14, out);
749
0
    if (!check_single_comp(blue_c, frac_14, ncomps, out)) {
750
0
        pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED_NOT;
751
0
        return;
752
0
    }
753
754
0
    process_comps =  ((gx_color_index)1 << red_c)
755
0
                   | ((gx_color_index)1 << green_c)
756
0
                   | ((gx_color_index)1 << blue_c);
757
0
    pcinfo->opmsupported = GX_CINFO_OPMSUPPORTED;
758
0
    pcinfo->process_comps = process_comps;
759
0
    pcinfo->black_component = 0; /* ? */
760
0
}
761
762
void check_opmsupported(gx_device * dev)
763
5.97k
{
764
5.97k
    if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
765
5.07k
        check_rgb_color_model_comps(dev);
766
899
    else
767
899
        (void)check_cmyk_color_model_comps(dev);
768
5.97k
}
769
770
/*
771
 * This set_overprint method is unique. If overprint is true, overprint
772
 * mode is set to 1, the process color model has DeviceCMYK behavior (see
773
 * the comment ahead of gx_is_cmyk_color_model above), and the device
774
 * color is set, the device color needs to be considered in setting up
775
 * the set of drawn components.
776
 */
777
static int
778
gx_set_overprint_DeviceCMYK(const gs_color_space * pcs, gs_gstate * pgs)
779
0
{
780
0
    gx_device *             dev = pgs->device;
781
0
    gx_device_color_info *  pcinfo = (dev == 0 ? 0 : &dev->color_info);
782
783
    /* check if we require special handling */
784
0
    if ( !pgs->overprint                      ||
785
0
         pgs->overprint_mode != 1             ||
786
0
         pcinfo == 0                          ||
787
0
         pcinfo->opmsupported == GX_CINFO_OPMSUPPORTED_NOT)
788
0
        return gx_spot_colors_set_overprint(pcs, pgs);
789
    /* Share code with CMYK ICC case */
790
0
    return gx_set_overprint_cmyk(pcs, pgs);
791
0
}
792
793
/* A few comments about ICC profiles and overprint simulation.  In order
794
   to do proper overprint simulation, the source ICC profile and the
795
   destination ICC profile must be the same.  If they are not, then
796
   we end up mapping the source CMYK data to a different CMYK value.  In
797
   this case, the non-zero components, which with overprint mode = 1 specify
798
   which are to be overprinted will not be correct to produce the proper
799
   overprint simulation.  This is seen with AR when doing output preview,
800
   overprint simulation enabled of the file overprint_icc.pdf (see our
801
   test files) which has SWOP ICC based CMYK fills.  In AR, if we use a
802
   simulation ICC profile that is different than the source profile,
803
   overprinting is no longer previewed. We follow the same logic here.
804
   If the source and destination ICC profiles do not match, then there is
805
   effectively no overprinting enabled.  This is bug 692433.  However,
806
   even with the mismatch, if the device supports spot colorants, those
807
   colors should be maintained. This is bug 702725. */
808
int gx_set_overprint_cmyk(const gs_color_space * pcs, gs_gstate * pgs)
809
1.15k
{
810
1.15k
    gx_device *             dev = pgs->device;
811
1.15k
    gx_color_index          drawn_comps = 0;
812
1.15k
    gs_overprint_params_t   params = { 0 };
813
1.15k
    gx_device_color        *pdc;
814
1.15k
    cmm_dev_profile_t      *dev_profile;
815
1.15k
    cmm_profile_t          *output_profile = 0;
816
1.15k
    int                     code;
817
1.15k
    bool                    profile_ok = false;
818
1.15k
    gsicc_rendering_param_t        render_cond;
819
1.15k
    bool                    eop;
820
821
1.15k
    if_debug0m(gs_debug_flag_overprint, pgs->memory,
822
1.15k
        "[overprint] gx_set_overprint_cmyk\n");
823
824
1.15k
    if (dev) {
825
1.15k
        code = dev_proc(dev, get_profile)(dev, &dev_profile);
826
1.15k
        if (code < 0)
827
0
            return code;
828
829
1.15k
        gsicc_extract_profile(dev->graphics_type_tag, dev_profile, &(output_profile),
830
1.15k
                              &render_cond);
831
832
1.15k
        drawn_comps = gx_get_process_comps(dev);
833
1.15k
    }
834
835
1.15k
    if_debug1m(gs_debug_flag_overprint, pgs->memory,
836
1.15k
        "[overprint] gx_set_overprint_cmyk. drawn_comps = 0x%x\n", (uint)drawn_comps);
837
838
1.15k
    if (drawn_comps == 0)
839
0
        return gx_spot_colors_set_overprint(pcs, pgs);
840
841
    /* correct for any zero'ed color components.  But only if profiles
842
       match AND pgs->overprint_mode is true */
843
1.15k
    if (pcs->cmm_icc_profile_data != NULL && output_profile != NULL) {
844
1.15k
        if (gsicc_profiles_equal(output_profile, pcs->cmm_icc_profile_data)) {
845
1.09k
            profile_ok = true;
846
1.09k
        }
847
1.15k
    }
848
849
1.15k
    eop = gs_currentcolor_eopm(pgs);
850
851
1.15k
    if_debug3m(gs_debug_flag_overprint, pgs->memory,
852
1.15k
        "[overprint] gx_set_overprint_cmyk. is_fill_color = %d, pgs->color[0].effective_opm = %d pgs->color[1].effective_opm = %d\n",
853
1.15k
        pgs->is_fill_color, pgs->color[0].effective_opm, pgs->color[1].effective_opm);
854
855
1.15k
    if (profile_ok && eop) {
856
1.05k
        gx_color_index  nz_comps, one, temp;
857
1.05k
        int             code;
858
1.05k
        int             num_colorant[4], k;
859
1.05k
        bool            colorant_ok;
860
1.05k
        dev_color_proc_get_nonzero_comps((*procp));
861
862
1.05k
        if_debug0m(gs_debug_flag_overprint, pgs->memory,
863
1.05k
            "[overprint] gx_set_overprint_cmyk. color_is_set, profile_ok and eop\n");
864
865
1.05k
        code = gx_set_dev_color(pgs);
866
1.05k
        if (code < 0)
867
0
            return code;
868
1.05k
        pdc = gs_currentdevicecolor_inline(pgs);
869
1.05k
        procp = pdc->type->get_nonzero_comps;
870
1.05k
        if (pdc->ccolor_valid) {
871
            /* If we have the source colors, then use those in making the
872
               decision as to which ones are non-zero.  Then we avoid
873
               accidently looking at small values that get quantized to zero
874
               Note that to get here in the code, the source color data color
875
               space has to be CMYK. Trick is that we do need to worry about
876
               the colorant order on the target device */
877
1.05k
            num_colorant[0] = (dev_proc(dev, get_color_comp_index))\
878
1.05k
                             (dev, "Cyan", strlen("Cyan"), NO_COMP_NAME_TYPE_OP);
879
1.05k
            num_colorant[1] = (dev_proc(dev, get_color_comp_index))\
880
1.05k
                             (dev, "Magenta", strlen("Magenta"), NO_COMP_NAME_TYPE_OP);
881
1.05k
            num_colorant[2] = (dev_proc(dev, get_color_comp_index))\
882
1.05k
                             (dev, "Yellow", strlen("Yellow"), NO_COMP_NAME_TYPE_OP);
883
1.05k
            num_colorant[3] = (dev_proc(dev, get_color_comp_index))\
884
1.05k
                             (dev, "Black", strlen("Black"), NO_COMP_NAME_TYPE_OP);
885
1.05k
            nz_comps = 0;
886
1.05k
            one = 1;
887
1.05k
            colorant_ok = true;
888
5.26k
            for (k = 0; k < 4; k++) {
889
                /* Note: AR assumes the value is zero if it
890
                   is less than 0.5 out of 255 */
891
4.20k
                if (pdc->ccolor.paint.values[k] > (0.5 / 255.0)) {
892
1.05k
                    if (num_colorant[k] == -1) {
893
0
                        colorant_ok = false;
894
1.05k
                    } else {
895
1.05k
                        temp = one << num_colorant[k];
896
1.05k
                        nz_comps = nz_comps | temp;
897
1.05k
                    }
898
1.05k
                }
899
4.20k
            }
900
            /* For some reason we don't have one of the standard colorants */
901
1.05k
            if (!colorant_ok) {
902
0
                if ((code = procp(pdc, dev, &nz_comps)) < 0)
903
0
                    return code;
904
0
            }
905
1.05k
        } else {
906
0
            if ((code = procp(pdc, dev, &nz_comps)) < 0)
907
0
                return code;
908
0
        }
909
1.05k
        drawn_comps &= nz_comps;
910
1.05k
    }
911
1.15k
    params.is_fill_color = pgs->is_fill_color;
912
1.15k
    params.retain_any_comps = true;
913
1.15k
    params.drawn_comps = drawn_comps;
914
1.15k
    params.op_state = GS_OP_STATE_NONE;
915
916
1.15k
    if_debug2m(gs_debug_flag_overprint, pgs->memory,
917
1.15k
        "[overprint] gx_set_overprint_cmyk. retain_any_comps = %d, drawn_comps = 0x%x\n",
918
1.15k
        params.retain_any_comps, (uint)(params.drawn_comps));
919
920
    /* We are in CMYK, the profiles match and overprint is true.  Set effective
921
       overprint mode to overprint mode but only if effective has not already
922
       been set to 0 */
923
1.15k
    params.effective_opm = pgs->color[0].effective_opm =
924
1.15k
        pgs->overprint_mode && gs_currentcolor_eopm(pgs);
925
1.15k
    return gs_gstate_update_overprint(pgs, &params);
926
1.15k
}
927
928
int gx_set_overprint_rgb(const gs_color_space * pcs, gs_gstate * pgs)
929
0
{
930
0
    gx_device *             dev = pgs->device;
931
0
    gx_color_index          drawn_comps = 0;
932
0
    gs_overprint_params_t   params = { 0 };
933
0
    gx_device_color        *pdc;
934
0
    cmm_dev_profile_t      *dev_profile;
935
0
    cmm_profile_t          *output_profile = 0;
936
0
    int                     code;
937
0
    bool                    profile_ok = false;
938
0
    gsicc_rendering_param_t        render_cond;
939
0
    bool                    eop;
940
941
0
    if_debug0m(gs_debug_flag_overprint, pgs->memory,
942
0
        "[overprint] gx_set_overprint_rgb\n");
943
944
0
    if (dev) {
945
0
        code = dev_proc(dev, get_profile)(dev, &dev_profile);
946
0
        if (code < 0)
947
0
            return code;
948
949
0
        gsicc_extract_profile(dev->graphics_type_tag, dev_profile, &(output_profile),
950
0
                              &render_cond);
951
952
0
        drawn_comps = gx_get_process_comps(dev);
953
0
    }
954
955
0
    if_debug1m(gs_debug_flag_overprint, pgs->memory,
956
0
        "[overprint] gx_set_overprint_rgb. drawn_comps = 0x%x\n", (uint)drawn_comps);
957
958
0
    if (drawn_comps == 0)
959
0
        return gx_spot_colors_set_overprint(pcs, pgs);
960
961
    /* correct for any zero'ed color components.  But only if profiles
962
       match AND pgs->overprint_mode is true */
963
0
    if (pcs->cmm_icc_profile_data != NULL && output_profile != NULL) {
964
0
        if (gsicc_profiles_equal(output_profile, pcs->cmm_icc_profile_data)) {
965
0
            profile_ok = true;
966
0
        }
967
0
    }
968
969
0
    eop = gs_currentcolor_eopm(pgs);
970
971
0
    if_debug3m(gs_debug_flag_overprint, pgs->memory,
972
0
        "[overprint] gx_set_overprint_rgb. is_fill_color = %d, pgs->color[0].effective_opm = %d pgs->color[1].effective_opm = %d\n",
973
0
        pgs->is_fill_color, pgs->color[0].effective_opm, pgs->color[1].effective_opm);
974
975
0
    if (profile_ok && eop) {
976
0
        gx_color_index  nz_comps, one, temp;
977
0
        int             code;
978
0
        int             num_colorant[3], k;
979
0
        bool            colorant_ok;
980
0
        dev_color_proc_get_nonzero_comps((*procp));
981
982
0
        if_debug0m(gs_debug_flag_overprint, pgs->memory,
983
0
            "[overprint] gx_set_overprint_cmyk. color_is_set, profile_ok and eop\n");
984
985
0
        code = gx_set_dev_color(pgs);
986
0
        if (code < 0)
987
0
            return code;
988
0
        pdc = gs_currentdevicecolor_inline(pgs);
989
0
        procp = pdc->type->get_nonzero_comps;
990
0
        if (pdc->ccolor_valid) {
991
            /* If we have the source colors, then use those in making the
992
               decision as to which ones are non-zero.  Then we avoid
993
               accidently looking at small values that get quantized to zero
994
               Note that to get here in the code, the source color data color
995
               space has to be CMYK. Trick is that we do need to worry about
996
               the colorant order on the target device */
997
0
            num_colorant[0] = (dev_proc(dev, get_color_comp_index))\
998
0
                             (dev, "Red", strlen("Red"), NO_COMP_NAME_TYPE_OP);
999
0
            num_colorant[1] = (dev_proc(dev, get_color_comp_index))\
1000
0
                             (dev, "Green", strlen("Green"), NO_COMP_NAME_TYPE_OP);
1001
0
            num_colorant[2] = (dev_proc(dev, get_color_comp_index))\
1002
0
                             (dev, "Blue", strlen("Blue"), NO_COMP_NAME_TYPE_OP);
1003
0
            nz_comps = 0;
1004
0
            one = 1;
1005
0
            colorant_ok = true;
1006
0
            for (k = 0; k < 3; k++) {
1007
                /* Note: AR assumes the value is zero if it
1008
                   is less than 0.5 out of 255 */
1009
0
                if (pdc->ccolor.paint.values[k] > (0.5 / 255.0)) {
1010
0
                    if (num_colorant[k] == -1) {
1011
0
                        colorant_ok = false;
1012
0
                    } else {
1013
0
                        temp = one << num_colorant[k];
1014
0
                        nz_comps = nz_comps | temp;
1015
0
                    }
1016
0
                }
1017
0
            }
1018
            /* For some reason we don't have one of the standard colorants */
1019
0
            if (!colorant_ok) {
1020
0
                if ((code = procp(pdc, dev, &nz_comps)) < 0)
1021
0
                    return code;
1022
0
            }
1023
0
        } else {
1024
0
            if ((code = procp(pdc, dev, &nz_comps)) < 0)
1025
0
                return code;
1026
0
        }
1027
0
        drawn_comps &= nz_comps;
1028
0
    }
1029
0
    params.is_fill_color = pgs->is_fill_color;
1030
0
    params.retain_any_comps = true;
1031
0
    params.drawn_comps = drawn_comps;
1032
0
    params.op_state = GS_OP_STATE_NONE;
1033
1034
0
    if_debug2m(gs_debug_flag_overprint, pgs->memory,
1035
0
        "[overprint] gx_set_overprint_rgb. retain_any_comps = %d, drawn_comps = 0x%x\n",
1036
0
        params.retain_any_comps, (uint)(params.drawn_comps));
1037
1038
    /* We are in RGB, the profiles match and overprint is true.  Set effective
1039
       overprint mode to overprint mode but only if effective has not already
1040
       been set to 0 */
1041
0
    params.effective_opm = pgs->color[0].effective_opm =
1042
0
        pgs->overprint_mode && gs_currentcolor_eopm(pgs);
1043
0
    return gs_gstate_update_overprint(pgs, &params);
1044
0
}
1045
/* A stub for a color mapping linearity check, when it is inapplicable. */
1046
int
1047
gx_cspace_no_linear(const gs_color_space *cs, const gs_gstate * pgs,
1048
                gx_device * dev,
1049
                const gs_client_color *c0, const gs_client_color *c1,
1050
                const gs_client_color *c2, const gs_client_color *c3,
1051
                float smoothness, gsicc_link_t *icclink)
1052
0
{
1053
0
    return_error(gs_error_rangecheck);
1054
0
}
1055
1056
static inline int
1057
cc2dc(const gs_color_space *cs, const gs_gstate * pgs, gx_device *dev,
1058
            gx_device_color *dc, const gs_client_color *cc)
1059
1.49M
{
1060
1.49M
    return cs->type->remap_color(cc, cs, dc, pgs, dev, gs_color_select_texture);
1061
1.49M
}
1062
1063
static inline void
1064
interpolate_cc(gs_client_color *c,
1065
        const gs_client_color *c0, const gs_client_color *c1, double t, int n)
1066
792k
{
1067
792k
    int i;
1068
1069
1.69M
    for (i = 0; i < n; i++)
1070
902k
        c->paint.values[i] = c0->paint.values[i] * t + c1->paint.values[i] * (1 - t);
1071
792k
}
1072
1073
static inline bool
1074
is_dc_nearly_linear(const gx_device *dev, const gx_device_color *c,
1075
        const gx_device_color *c0, const gx_device_color *c1,
1076
        double t, uchar n, float smoothness)
1077
792k
{
1078
792k
    uchar i;
1079
1080
792k
    if (c0->type == &gx_dc_type_data_pure) {
1081
740k
        gx_color_index pure0 = c0->colors.pure;
1082
740k
        gx_color_index pure1 = c1->colors.pure;
1083
740k
        gx_color_index pure = c->colors.pure;
1084
1085
3.08M
        for (i = 0; i < n; i++) {
1086
2.35M
            int shift = dev->color_info.comp_shift[i];
1087
2.35M
            int mask = (1 << dev->color_info.comp_bits[i]) - 1;
1088
2.35M
            int max_color = (i == dev->color_info.gray_index ? dev->color_info.max_gray
1089
2.35M
                                                             : dev->color_info.max_color);
1090
2.35M
            float max_diff = max(1, max_color * smoothness);
1091
2.35M
            int b0 = (pure0 >> shift) & mask, b1 = (pure1 >> shift) & mask;
1092
2.35M
            int b = (pure >> shift) & mask;
1093
2.35M
            double bb = b0 * t + b1 * (1 - t);
1094
1095
2.35M
            if (any_abs(b - bb) > max_diff)
1096
10.6k
                return false;
1097
2.35M
        }
1098
729k
        return true;
1099
740k
    } else if (c0->type == &gx_dc_type_data_devn) {
1100
259k
        for (i = 0; i < n; i++) {
1101
207k
            int max_color = (i == dev->color_info.gray_index ? dev->color_info.max_gray
1102
207k
                : dev->color_info.max_color);
1103
207k
            double max_diff = max(1, max_color * smoothness);
1104
            /* Color values are 16 bit.  We are basing the smoothness on the
1105
               device bit depth.  So make sure to adjust the above max diff
1106
               based upon our device bit depth */
1107
207k
            double ratio = (double)max_color / (double)gx_max_color_value;
1108
207k
            double b0 = (c0->colors.devn.values[i]) * ratio;
1109
207k
            double b1 = (c1->colors.devn.values[i]) * ratio;
1110
207k
            double b = (c->colors.devn.values[i]) * ratio;
1111
207k
            double bb = b0 * t + b1 * (1 - t);
1112
207k
            if (any_abs(b - bb) > max_diff)
1113
286
                return false;
1114
207k
        }
1115
51.6k
        return true;
1116
51.8k
    } else {
1117
        /* Halftones must not paint with fill_linear_color_*. */
1118
0
        return false;
1119
0
    }
1120
792k
}
1121
1122
/* Default color mapping linearity check, a 2-points case. */
1123
static int
1124
gx_cspace_is_linear_in_line(const gs_color_space *cs, const gs_gstate * pgs,
1125
                gx_device *dev,
1126
                const gs_client_color *c0, const gs_client_color *c1,
1127
                float smoothness)
1128
206k
{
1129
206k
    gs_client_color c01a, c01b;
1130
206k
    gx_device_color d[2], d01a, d01b;
1131
206k
    int n = cs->type->num_components(cs);
1132
206k
    uchar ndev = dev->color_info.num_components;
1133
206k
    int code;
1134
1135
206k
    code = cc2dc(cs, pgs, dev, &d[0], c0);
1136
206k
    if (code < 0)
1137
0
        return code;
1138
206k
    code = cc2dc(cs, pgs, dev, &d[1], c1);
1139
206k
    if (code < 0)
1140
0
        return code;
1141
206k
    interpolate_cc(&c01a, c0, c1, 0.3, n);
1142
206k
    code = cc2dc(cs, pgs, dev, &d01a, &c01a);
1143
206k
    if (code < 0)
1144
0
        return code;
1145
206k
    if (!is_dc_nearly_linear(dev, &d01a, &d[0], &d[1], 0.3, ndev, smoothness))
1146
9.62k
        return 0;
1147
196k
    interpolate_cc(&c01b, c0, c1, 0.7, n);
1148
196k
    code = cc2dc(cs, pgs, dev, &d01b, &c01b);
1149
196k
    if (code < 0)
1150
0
        return code;
1151
196k
    if (!is_dc_nearly_linear(dev, &d01b, &d[0], &d[1], 0.7, ndev, smoothness))
1152
1.04k
        return 0;
1153
195k
    return 1;
1154
196k
}
1155
1156
/* Default color mapping linearity check, a triangle case. */
1157
static int
1158
gx_cspace_is_linear_in_triangle(const gs_color_space *cs, const gs_gstate * pgs,
1159
                gx_device *dev,
1160
                const gs_client_color *c0, const gs_client_color *c1,
1161
                const gs_client_color *c2, float smoothness)
1162
97.4k
{
1163
    /* We check 4 points - the median center, and middle points of 3 sides.
1164
       Hopely this is enough for reasonable color spaces and color renderings.
1165
       Note it gives 7 points for a quadrangle. */
1166
97.4k
    gs_client_color c01, c12, c20, c012;
1167
97.4k
    gx_device_color d[3], d01, d12, d20, d012;
1168
1169
    /* Note that the device and the client color space
1170
       can have a different number of components */
1171
1172
97.4k
    int n = cs->type->num_components(cs);
1173
97.4k
    uchar ndev = dev->color_info.num_components;
1174
1175
97.4k
    int code;
1176
1177
97.4k
    code = cc2dc(cs, pgs, dev, &d[0], c0);
1178
97.4k
    if (code < 0)
1179
0
        return code;
1180
97.4k
    code = cc2dc(cs, pgs, dev, &d[1], c1);
1181
97.4k
    if (code < 0)
1182
0
        return code;
1183
97.4k
    code = cc2dc(cs, pgs, dev, &d[2], c2);
1184
97.4k
    if (code < 0)
1185
0
        return code;
1186
1187
97.4k
    interpolate_cc(&c01, c0, c1, 0.5, n);
1188
97.4k
    code = cc2dc(cs, pgs, dev, &d01, &c01);
1189
97.4k
    if (code < 0)
1190
0
        return code;
1191
97.4k
    if (!is_dc_nearly_linear(dev, &d01, &d[0], &d[1], 0.5, ndev, smoothness))
1192
171
        return 0;
1193
1194
97.3k
    interpolate_cc(&c012, c2, &c01, 2.0 / 3, n);
1195
97.3k
    code = cc2dc(cs, pgs, dev, &d012, &c012);
1196
97.3k
    if (code < 0)
1197
0
        return code;
1198
97.3k
    if (!is_dc_nearly_linear(dev, &d012, &d[2], &d01, 2.0 / 3, ndev, smoothness))
1199
115
        return 0;
1200
1201
97.1k
    interpolate_cc(&c12, c1, c2, 0.5, n);
1202
97.1k
    code = cc2dc(cs, pgs, dev, &d12, &c12);
1203
97.1k
    if (code < 0)
1204
0
        return code;
1205
97.1k
    if (!is_dc_nearly_linear(dev, &d12, &d[1], &d[2], 0.5, ndev, smoothness))
1206
0
        return 0;
1207
1208
97.1k
    interpolate_cc(&c20, c2, c0, 0.5, n);
1209
97.1k
    code = cc2dc(cs, pgs, dev, &d20, &c20);
1210
97.1k
    if (code < 0)
1211
0
        return code;
1212
97.1k
    if (!is_dc_nearly_linear(dev, &d20, &d[2], &d[0], 0.5, ndev, smoothness))
1213
0
        return 0;
1214
97.1k
    return 1;
1215
97.1k
}
1216
1217
/* Default color mapping linearity check. */
1218
int
1219
gx_cspace_is_linear_default(const gs_color_space *cs, const gs_gstate * pgs,
1220
                gx_device *dev,
1221
                const gs_client_color *c0, const gs_client_color *c1,
1222
                const gs_client_color *c2, const gs_client_color *c3,
1223
                float smoothness, gsicc_link_t *icclink)
1224
303k
{
1225
    /* Assuming 2 <= nc <= 4. We don't need other cases. */
1226
    /* With nc == 4 assuming a convex plain quadrangle in the client color space. */
1227
303k
    int code;
1228
1229
303k
    if (!colors_are_separable_and_linear(&dev->color_info))
1230
0
        return_error(gs_error_rangecheck);
1231
303k
    if (c2 == NULL)
1232
206k
        return gx_cspace_is_linear_in_line(cs, pgs, dev, c0, c1, smoothness);
1233
97.4k
    code = gx_cspace_is_linear_in_triangle(cs, pgs, dev, c0, c1, c2, smoothness);
1234
97.4k
    if (code <= 0)
1235
286
        return code;
1236
97.1k
    if (c3 == NULL)
1237
97.1k
        return 1;
1238
0
    return gx_cspace_is_linear_in_triangle(cs, pgs, dev, c1, c2, c3, smoothness);
1239
97.1k
}
1240
1241
/* Serialization. */
1242
int
1243
gx_serialize_cspace_type(const gs_color_space * pcs, stream * s)
1244
86.2k
{
1245
86.2k
    const gs_color_space_type * type = pcs->type;
1246
86.2k
    uint n;
1247
86.2k
    return sputs(s, (const byte *)&type->index, sizeof(type->index), &n);
1248
86.2k
}
1249
1250
/* GC procedures */
1251
1252
static
1253
ENUM_PTRS_BEGIN_PROC(color_space_enum_ptrs)
1254
8.92M
{
1255
8.92M
    EV_CONST gs_color_space *pcs = vptr;
1256
1257
8.92M
    if (index == 0)
1258
1.78M
        return ENUM_OBJ(pcs->base_space);
1259
7.14M
    if (index == 1)
1260
1.78M
        return ENUM_OBJ(pcs->pclient_color_space_data);
1261
5.35M
    if (index == 2)
1262
1.78M
        return ENUM_OBJ(pcs->icc_equivalent);
1263
3.57M
    if (index == 3) {
1264
1.78M
        if (gs_color_space_get_index(pcs) == gs_color_space_index_DeviceN)
1265
0
            return ENUM_OBJ(pcs->params.device_n.devn_process_space);
1266
1.78M
        else
1267
1.78M
            return ENUM_OBJ(NULL);
1268
1.78M
    }
1269
1270
1.78M
    return ENUM_USING(*pcs->type->stype, vptr, size, index - 4);
1271
3.57M
    ENUM_PTRS_END_PROC
1272
3.57M
}
1273
static
1274
1.78M
RELOC_PTRS_WITH(color_space_reloc_ptrs, gs_color_space *pcs)
1275
1.78M
{
1276
1.78M
    RELOC_VAR(pcs->base_space);
1277
1.78M
    RELOC_VAR(pcs->pclient_color_space_data);
1278
1.78M
    RELOC_VAR(pcs->icc_equivalent);
1279
1.78M
    if (gs_color_space_get_index(pcs) == gs_color_space_index_DeviceN)
1280
1.78M
        RELOC_VAR(pcs->params.device_n.devn_process_space);
1281
1.78M
    RELOC_USING(*pcs->type->stype, vptr, size);
1282
1.78M
}
1283
1.78M
RELOC_PTRS_END