summaryrefslogtreecommitdiff
path: root/base/gscssub.c
blob: 9c19e6a540b960a53260009a8c281fe37e8c9174 (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
/* 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.
*/


/* Color space substitution "operators" */
#include "gx.h"
#include "gserrors.h"
#include "gscssub.h"
#include "gxcspace.h"		/* for st_color_space */
#include "gxdevcli.h"
#include "gzstate.h"

/* .setsubstitutecolorspace */
/* Note that, to support PDF, ICCBased color spaces may be used to substitute
 * for the Device* color spaces (previously, only CIEBased color spaces could
 * be used for this purpose). */
int
gs_setsubstitutecolorspace(gs_gstate *pgs, gs_color_space_index csi,
                           const gs_color_space *pcs)
{
    int index = (int)csi;
    static const uint masks[3] = {
        (1 << gs_color_space_index_DeviceGray) |
          (1 << gs_color_space_index_CIEA),
        (1 << gs_color_space_index_DeviceRGB) |
          (1 << gs_color_space_index_CIEABC) |
          (1 << gs_color_space_index_CIEDEF),
        (1 << gs_color_space_index_DeviceCMYK) |
          (1 << gs_color_space_index_CIEDEFG)
    };
    const gs_color_space *pcs_old;

    if (index < 0 || index > 2)
        return_error(gs_error_rangecheck);
    if (pcs) {
        if (gs_color_space_get_index(pcs) == gs_color_space_index_CIEICC) {
            static const byte dev_ncomps[3] = {1, 3, 4};

             if (dev_ncomps[index] != cs_num_components(pcs))
                 return_error(gs_error_rangecheck);
        } else if (!masks[index] && (1 << gs_color_space_get_index(pcs)))
            return_error(gs_error_rangecheck);
    }
    pcs_old = pgs->device_color_spaces.indexed[index];
    if (pcs_old == 0 &&	(pcs == 0 || gs_color_space_get_index(pcs) == csi))
        return 0;
    rc_assign(pgs->device_color_space.indexed[index],
              (pcs ? pcs :
               pgs->shared->device_color_spaces.indexed[index]),
              "gs_setsubstitutecolorspace");
    return 0;
}

/* Possibly-substituted color space accessors. */
const gs_color_space *
gs_current_DeviceGray_space(const gs_gstate *pgs)
{
    const gs_color_space *pcs;

    return (!pgs->device->UseCIEColor ||
            (pcs = pgs->device_color_spaces.named.Gray) == 0 ?
            pgs->shared->device_color_spaces.named.Gray : pcs);
}
const gs_color_space *
gs_current_DeviceRGB_space(const gs_gstate *pgs)
{
    const gs_color_space *pcs;

    return (!pgs->device->UseCIEColor ||
            (pcs = pgs->device_color_spaces.named.RGB) == 0 ?
            pgs->shared->device_color_spaces.named.RGB : pcs);
}
const gs_color_space *
gs_current_DeviceCMYK_space(const gs_gstate *pgs)
{
    const gs_color_space *pcs;

    return (!pgs->device->UseCIEColor ||
            (pcs = pgs->device_color_spaces.named.CMYK) == 0 ?
            pgs->shared->device_color_spaces.named.CMYK : pcs);
}

/* .currentsubstitutecolorspace */
const gs_color_space *
gs_currentsubstitutecolorspace(const gs_gstate *pgs, gs_color_space_index csi)
{
    switch (csi) {
    case gs_color_space_index_DeviceGray:
        return gs_current_DeviceGray_space(pgs);
    case gs_color_space_index_DeviceRGB:
        return gs_current_DeviceRGB_space(pgs);
    case gs_color_space_index_DeviceCMYK:
        return gs_current_DeviceCMYK_space(pgs);
    default:
        return 0;
    }
}