summaryrefslogtreecommitdiff
path: root/base/gscscie.c
blob: aae7ea19df397e49b3082502b57118787e4a1e83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* CIE color space management */
#include "math_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsstruct.h"
#include "gsmatrix.h"		/* for gscolor2.h */
#include "gxcspace.h"
#include "gscolor2.h"		/* for gs_set/currentcolorrendering */
#include "gsicc_manage.h"	/* for gsicc_adjust_profile_rc */
#include "gxcie.h"
#include "gxarith.h"
#include "gxdevice.h"		/* for gxcmap.h */
#include "gxcmap.h"
#include "gzstate.h"
#include "stream.h"

/* ---------------- Color space definition ---------------- */

/* GC descriptors */
public_st_cie_common();
public_st_cie_common_elements();
private_st_cie_a();
private_st_cie_abc();
private_st_cie_def();
private_st_cie_defg();

/* Define the CIE color space types. */

/* CIEBasedDEFG */
gs_private_st_ptrs1(st_color_space_CIEDEFG, gs_color_space,
     "gs_color_space(CIEDEFG)", cs_CIEDEFG_enum_ptrs, cs_CIEDEFG_reloc_ptrs,
                    params.defg);
static cs_proc_final(gx_final_CIEDEFG);
static cs_proc_serialize(gx_serialize_CIEDEFG);
const gs_color_space_type gs_color_space_type_CIEDEFG = {
    gs_color_space_index_CIEDEFG, true, true,
    &st_color_space_CIEDEFG, gx_num_components_4,
    gx_init_CIE, gx_restrict_CIEDEFG,
    gx_concrete_space_CIE,
    gx_concretize_CIEDEFG, NULL,
    gx_remap_CIEDEFG,
    gx_install_CIE,
    gx_spot_colors_set_overprint,
    gx_final_CIEDEFG, gx_no_adjust_color_count,
    gx_serialize_CIEDEFG,
    gx_cspace_is_linear_default, gx_polarity_subtractive
};

/* CIEBasedDEF */
gs_private_st_ptrs1(st_color_space_CIEDEF, gs_color_space,
        "gs_color_space(CIEDEF)", cs_CIEDEF_enum_ptrs, cs_CIEDEF_reloc_ptrs,
                    params.def);
static cs_proc_final(gx_final_CIEDEF);
static cs_proc_serialize(gx_serialize_CIEDEF);
const gs_color_space_type gs_color_space_type_CIEDEF = {
    gs_color_space_index_CIEDEF, true, true,
    &st_color_space_CIEDEF, gx_num_components_3,
    gx_init_CIE, gx_restrict_CIEDEF,
    gx_concrete_space_CIE,
    gx_concretize_CIEDEF, NULL,
    gx_remap_CIEDEF,
    gx_install_CIE,
    gx_spot_colors_set_overprint,
    gx_final_CIEDEF, gx_no_adjust_color_count,
    gx_serialize_CIEDEF,
    gx_cspace_is_linear_default, gx_polarity_subtractive
};

/* CIEBasedABC */
gs_private_st_ptrs1(st_color_space_CIEABC, gs_color_space,
        "gs_color_space(CIEABC)", cs_CIEABC_enum_ptrs, cs_CIEABC_reloc_ptrs,
                    params.abc);
static cs_proc_final(gx_final_CIEABC);
static cs_proc_serialize(gx_serialize_CIEABC);
const gs_color_space_type gs_color_space_type_CIEABC = {
    gs_color_space_index_CIEABC, true, true,
    &st_color_space_CIEABC, gx_num_components_3,
    gx_init_CIE, gx_restrict_CIEABC,
    gx_concrete_space_CIE,
    gx_concretize_CIEABC, NULL,
    gx_remap_CIEABC, gx_install_CIE,
    gx_spot_colors_set_overprint,
    gx_final_CIEABC, gx_no_adjust_color_count,
    gx_serialize_CIEABC,
    gx_cspace_is_linear_default, gx_polarity_additive
};

/* CIEBasedA */
gs_private_st_ptrs1(st_color_space_CIEA, gs_color_space,
              "gs_color_space(CIEA)", cs_CIEA_enum_ptrs, cs_CIEA_reloc_ptrs,
                    params.a);
static cs_proc_final(gx_final_CIEA);
static cs_proc_serialize(gx_serialize_CIEA);
const gs_color_space_type gs_color_space_type_CIEA = {
    gs_color_space_index_CIEA, true, true,
    &st_color_space_CIEA, gx_num_components_1,
    gx_init_CIE, gx_restrict_CIEA,
    gx_concrete_space_CIE,
    gx_concretize_CIEA, NULL,
    gx_remap_CIEA,
    gx_install_CIE,
    gx_spot_colors_set_overprint,
    gx_final_CIEA, gx_no_adjust_color_count,
    gx_serialize_CIEA,
    gx_cspace_is_linear_default, gx_polarity_additive
};

/* Determine the concrete space underlying a CIEBased space. */
const gs_color_space *
gx_concrete_space_CIE(const gs_color_space * pcs, const gs_gstate * pgs)
{
    const gs_cie_render *pcie = pgs->cie_render;

    if (pcie == 0 || pcie->RenderTable.lookup.table == 0 ||
        pcie->RenderTable.lookup.m == 3
        ) {
        return pgs->devicergb_cs;
    } else {			/* pcie->RenderTable.lookup.m == 4 */
        return pgs->devicecmyk_cs;
    }
}

/* Install a CIE space in the graphics state. */
/* We go through an extra level of procedure so that */
/* interpreters can substitute their own installer. */
/* This procedure is exported for the benefit of gsicc.c */
int
gx_install_CIE(gs_color_space * pcs, gs_gstate * pgs)
{
    return (*pcs->params.a->common.install_cspace) (pcs, pgs);
}

/* Free params for a CIE color space */
static void
gx_final_CIEDEFG(gs_color_space * pcs)
{
    if (pcs->icc_equivalent != NULL) {
        rc_decrement(pcs->icc_equivalent, "gx_final_CIEDEFG");
        pcs->icc_equivalent = NULL;
    }
    if (pcs->cmm_icc_profile_data != NULL) {
        gsicc_adjust_profile_rc(pcs->cmm_icc_profile_data, -1, "gx_final_CIEDEFG");
        pcs->cmm_icc_profile_data = NULL;
    }
    rc_decrement(pcs->params.defg, "gx_final_CIEDEFG");
    pcs->params.defg = NULL;
}

static void
gx_final_CIEDEF(gs_color_space * pcs)
{
    if (pcs->icc_equivalent != NULL) {
        rc_decrement(pcs->icc_equivalent,"gx_final_CIEDEF");
        pcs->icc_equivalent = NULL;
    }
    if (pcs->cmm_icc_profile_data != NULL) {
        gsicc_adjust_profile_rc(pcs->cmm_icc_profile_data, -1, "gx_final_CIEDEF");
        pcs->cmm_icc_profile_data = NULL;
    }
    rc_decrement(pcs->params.def, "gx_final_CIEDEF");
    pcs->params.def = NULL;
}

static void
gx_final_CIEABC(gs_color_space * pcs)
{
    if (pcs->icc_equivalent != NULL) {
        rc_decrement(pcs->icc_equivalent,"gx_final_CIEABC");
        pcs->icc_equivalent = NULL;
    }
    if (pcs->cmm_icc_profile_data != NULL) {
        gsicc_adjust_profile_rc(pcs->cmm_icc_profile_data, -1, "gx_final_CIEABC");
        pcs->cmm_icc_profile_data = NULL;
    }
    rc_decrement(pcs->params.abc, "gx_final_CIEABC");
    pcs->params.abc = NULL;
}

static void
gx_final_CIEA(gs_color_space * pcs)
{
    if (pcs->icc_equivalent != NULL) {
        rc_decrement(pcs->icc_equivalent,"gx_final_CIEA");
        pcs->icc_equivalent = NULL;
    }
    if (pcs->cmm_icc_profile_data != NULL) {
        gsicc_adjust_profile_rc(pcs->cmm_icc_profile_data, -1, "gx_final_CIEA");
        pcs->cmm_icc_profile_data = NULL;
    }
    rc_decrement(pcs->params.a, "gx_adjust_cspace_CIEA");
    pcs->params.a = NULL;
}

/* ---------------- Procedures ---------------- */

/* ------ Internal initializers ------ */

/*
 * Set up the default values for the CIE parameters that are common to
 * all CIE color spaces.
 *
 * There is no default for the white point, so it is set equal to the
 * black point. If anyone actually uses the color space in that form,
 * the results are likely to be unsatisfactory.
 *
 * This procedure is exported for the benefit of gsicc.c.
 */
void
gx_set_common_cie_defaults(gs_cie_common * pcommon, void *client_data)
{
    pcommon->RangeLMN = Range3_default;
    pcommon->DecodeLMN = DecodeLMN_default;
    pcommon->MatrixLMN = Matrix3_default;
    pcommon->points.WhitePoint = BlackPoint_default;
    pcommon->points.BlackPoint = BlackPoint_default;
    pcommon->client_data = client_data;
}

/*
 * Set defaults for a CIEBasedABC color space.  This is also used for
 * CIEBasedDEF and CIEBasedDEFG color spaces.
 */
static void
set_cie_abc_defaults(gs_cie_abc * pabc, void *client_data)
{
    gx_set_common_cie_defaults(&pabc->common, client_data);
    pabc->RangeABC = Range3_default;
    pabc->DecodeABC = DecodeABC_default;
    pabc->MatrixABC = Matrix3_default;
}

/*
 * Set up a default color lookup table for a CIEBasedDEF[G] space. There is
 * no specified default for this structure, so the values used here (aside
 * from the input and output component numbers) are intended only to make
 * the system fail in a predictable manner.
 */
static void
set_ctbl_defaults(gx_color_lookup_table * plktblp, int num_comps)
{
    int i;

    plktblp->n = num_comps;
    plktblp->m = 3;		/* always output CIE ABC */
    for (i = 0; i < countof(plktblp->dims); i++)
        plktblp->dims[i] = 0;
    plktblp->table = 0;
}

/*
 * Allocate a color space and its parameter structure.
 * Return 0 if VMerror, otherwise the parameter structure.
 *
 * This is exported for the benefit of gsicc.c
 */
void *
gx_build_cie_space(gs_color_space ** ppcspace,
                const gs_color_space_type * pcstype,
                gs_memory_type_ptr_t stype, gs_memory_t * pmem)
{
    gs_color_space *pcspace = gs_cspace_alloc(pmem, pcstype);
    gs_cie_common_elements_t *pdata;

    if (pcspace == NULL)
        return NULL;
    rc_alloc_struct_1(pdata, gs_cie_common_elements_t, stype, pmem,
                      {
                      gs_free_object(pmem, pcspace, "gx_build_cie_space");
                      return 0;
                      }
                      ,
                      "gx_build_cie_space(data)");
    *ppcspace = pcspace;
    return (void *)pdata;
}

/* ------ Constructors ------ */

int
gs_cspace_build_CIEA(gs_color_space ** ppcspace, void *client_data,
                     gs_memory_t * pmem)
{
    gs_cie_a *pciea =
    gx_build_cie_space(ppcspace, &gs_color_space_type_CIEA, &st_cie_a, pmem);

    if (pciea == 0)
        return_error(gs_error_VMerror);

    gx_set_common_cie_defaults(&pciea->common, client_data);
    pciea->common.install_cspace = gx_install_CIEA;
    pciea->RangeA = RangeA_default;
    pciea->DecodeA = DecodeA_default;
    pciea->MatrixA = MatrixA_default;

    (*ppcspace)->params.a = pciea;
    return 0;
}

int
gs_cspace_build_CIEABC(gs_color_space ** ppcspace, void *client_data,
                       gs_memory_t * pmem)
{
    gs_cie_abc *pabc =
    gx_build_cie_space(ppcspace, &gs_color_space_type_CIEABC, &st_cie_abc,
                       pmem);

    if (pabc == 0)
        return_error(gs_error_VMerror);

    set_cie_abc_defaults(pabc, client_data);
    pabc->common.install_cspace = gx_install_CIEABC;

    (*ppcspace)->params.abc = pabc;
    return 0;
}

int
gs_cspace_build_CIEDEF(gs_color_space ** ppcspace, void *client_data,
                       gs_memory_t * pmem)
{
    gs_cie_def *pdef =
    gx_build_cie_space(ppcspace, &gs_color_space_type_CIEDEF, &st_cie_def,
                       pmem);

    if (pdef == 0)
        return_error(gs_error_VMerror);

    set_cie_abc_defaults((gs_cie_abc *) pdef, client_data);
    pdef->common.install_cspace = gx_install_CIEDEF;
    pdef->RangeDEF = Range3_default;
    pdef->DecodeDEF = DecodeDEF_default;
    pdef->RangeHIJ = Range3_default;
    set_ctbl_defaults(&pdef->Table, 3);

    (*ppcspace)->params.def = pdef;
    return 0;
}

int
gs_cspace_build_CIEDEFG(gs_color_space ** ppcspace, void *client_data,
                        gs_memory_t * pmem)
{
    gs_cie_defg *pdefg =
    gx_build_cie_space(ppcspace, &gs_color_space_type_CIEDEFG, &st_cie_defg,
                       pmem);

    if (pdefg == 0)
        return_error(gs_error_VMerror);

    set_cie_abc_defaults((gs_cie_abc *) pdefg, client_data);
    pdefg->common.install_cspace = gx_install_CIEDEFG;
    pdefg->RangeDEFG = Range4_default;
    pdefg->DecodeDEFG = DecodeDEFG_default;
    pdefg->RangeHIJK = Range4_default;
    set_ctbl_defaults(&pdefg->Table, 4);

    (*ppcspace)->params.defg = pdefg;
    return 0;
}

/* ------ Accessors ------ */

int
gs_cie_defx_set_lookup_table(gs_color_space * pcspace, int *pdims,
                             const gs_const_string * ptable)
{
    gx_color_lookup_table *plktblp;

    switch (gs_color_space_get_index(pcspace)) {
        case gs_color_space_index_CIEDEF:
            plktblp = &pcspace->params.def->Table;
            break;
        case gs_color_space_index_CIEDEFG:
            plktblp = &pcspace->params.defg->Table;
            plktblp->dims[3] = pdims[3];
            break;
        default:
            return_error(gs_error_rangecheck);
    }

    plktblp->dims[0] = pdims[0];
    plktblp->dims[1] = pdims[1];
    plktblp->dims[2] = pdims[2];
    plktblp->table = ptable;
    return 0;
}

/* ---------------- Serialization. -------------------------------- */

static int
gx_serialize_cie_cache(const cie_cache_floats *c, stream * s)
{ /* p->DecodeA : */
    const uint cache_size = count_of(c->values);
    uint n;
    int code;

    code = sputs(s, (const byte *)&c->params.is_identity, sizeof(c->params.is_identity), &n);
    if (code < 0)
        return_error(gs_error_ioerror);
    if (c->params.is_identity)
        return 0;
    code = sputs(s, (const byte *)&cache_size, sizeof(cache_size), &n);
    if (code < 0)
        return code;
    return sputs(s, (const byte *)c->values, sizeof(c->values), &n);
}

int
gx_serialize_cie_common_elements(const gs_color_space * pcs, stream * s)
{
    const gs_cie_a * p = pcs->params.a;
    uint n, k;
    int code = gx_serialize_cspace_type(pcs, s);

    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&p->common.RangeLMN,
                        sizeof(p->common.RangeLMN), &n);
    if (code < 0)
        return code;
    for (k = 0; k < 3 && code >= 0; k++)
        code = gx_serialize_cie_cache(&p->common.caches.DecodeLMN[k].floats, s);
    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&p->common.MatrixLMN,
                        sizeof(p->common.MatrixLMN), &n);
    if (code < 0)
        return code;
    return sputs(s, (const byte *)&p->common.points,
                        sizeof(p->common.points), &n);
}

static int
gx_serialize_CIEA(const gs_color_space * pcs, stream * s)
{
    const gs_cie_a * p = pcs->params.a;
    uint n;
    int code = gx_serialize_cie_common_elements(pcs, s);

    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&p->RangeA, sizeof(p->RangeA), &n);
    if (code < 0)
        return code;
    code = gx_serialize_cie_cache(&p->caches.DecodeA.floats, s);
    if (code < 0)
        return code;
    return sputs(s, (const byte *)&p->MatrixA, sizeof(p->MatrixA), &n);
}

static int
gx_serialize_CIEABC(const gs_color_space * pcs, stream * s)
{
    const gs_cie_abc * p = pcs->params.abc;
    uint n, k;
    int code = gx_serialize_cie_common_elements(pcs, s);

    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&p->RangeABC, sizeof(p->RangeABC), &n);
    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&p->caches.skipABC, sizeof(p->caches.skipABC), &n);
    if (code < 0)
        return code;
    if (p->caches.skipABC)
        return 0;
    for (k = 0; k < 3 && code >= 0; k++)
        code = gx_serialize_cie_cache(&p->caches.DecodeABC.caches[k].floats, s);
    if (code < 0)
        return code;
    return sputs(s, (const byte *)&p->MatrixABC, sizeof(p->MatrixABC), &n);
}

static int
gx_serialize_lookup_table(const gx_color_lookup_table * t, stream * s)
{
    uint n;
    int code = sputs(s, (const byte *)&t->n, sizeof(t->n), &n);

    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&t->dims[0], sizeof(t->dims[0]) * t->n, &n);
    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&t->m, sizeof(t->m), &n);
    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&t->table->size, sizeof(t->table->size), &n);
    if (code < 0)
        return code;
    return sputs(s, (const byte *)t->table->data, t->table->size, &n);
}

static int
gx_serialize_CIEDEF(const gs_color_space * pcs, stream * s)
{
    const gs_cie_def * p = pcs->params.def;
    uint n, k;
    int code = gx_serialize_cie_common_elements(pcs, s);

    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&p->RangeDEF, sizeof(p->RangeDEF), &n);
    if (code < 0)
        return code;
    for (k = 0; k < 3 && code >= 0; k++)
        code = gx_serialize_cie_cache(&p->caches_def.DecodeDEF[k].floats, s);
    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&p->RangeHIJ, sizeof(p->RangeHIJ), &n);
    if (code < 0)
        return code;
    return gx_serialize_lookup_table(&p->Table, s);
}

static int
gx_serialize_CIEDEFG(const gs_color_space * pcs, stream * s)
{
    const gs_cie_defg * p = pcs->params.defg;
    uint n, k;
    int code = gx_serialize_cie_common_elements(pcs, s);

    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&p->RangeDEFG, sizeof(p->RangeDEFG), &n);
    if (code < 0)
        return code;
    for (k = 0; k < 3 && code >= 0; k++)
        code = gx_serialize_cie_cache(&p->caches_defg.DecodeDEFG[k].floats, s);
    if (code < 0)
        return code;
    code = sputs(s, (const byte *)&p->RangeHIJK, sizeof(p->RangeHIJK), &n);
    if (code < 0)
        return code;
    return gx_serialize_lookup_table(&p->Table, s);
}