summaryrefslogtreecommitdiff
path: root/xps/xpscolor.c
blob: adafbc7f9d0765c11d554635efcba0f246fcb7d2 (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
/* Copyright (C) 2006-2010 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 that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen  Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* XPS interpreter - color functions */

#include "ghostxps.h"

#include "stream.h" /* for sizeof(stream) to work */

void
xps_set_color(xps_context_t *ctx, gs_color_space *cs, float *samples)
{
    gs_client_color cc;
    int i, n;

    if (ctx->opacity_only)
    {
        gs_setopacityalpha(ctx->pgs, 1.0);
        gs_setgray(ctx->pgs, samples[0]);
    }
    else
    {
        n = cs_num_components(cs);
        cc.pattern = 0;
        for (i = 0; i < n; i++)
            cc.paint.values[i] = samples[i + 1];

        gs_setopacityalpha(ctx->pgs, samples[0]);
        gs_setcolorspace(ctx->pgs, cs);
        gs_setcolor(ctx->pgs, &cc);
    }
}

static int unhex(int chr)
{
    const char *hextable = "0123456789ABCDEF";
    return strchr(hextable, (toupper(chr))) - hextable;
}

static int count_commas(char *s)
{
    int n = 0;
    while (*s)
    {
        if (*s == ',')
            n ++;
        s ++;
    }
    return n;
}

void
xps_parse_color(xps_context_t *ctx, char *base_uri, char *string,
        gs_color_space **csp, float *samples)
{
    char *p;
    int i, n;
    char buf[1024];
    char *profile;

    *csp = ctx->srgb;

    samples[0] = 1.0;
    samples[1] = 0.0;
    samples[2] = 0.0;
    samples[3] = 0.0;

    if (string[0] == '#')
    {
        if (strlen(string) == 9)
        {
            samples[0] = unhex(string[1]) * 16 + unhex(string[2]);
            samples[1] = unhex(string[3]) * 16 + unhex(string[4]);
            samples[2] = unhex(string[5]) * 16 + unhex(string[6]);
            samples[3] = unhex(string[7]) * 16 + unhex(string[8]);
        }
        else
        {
            samples[0] = 255.0;
            samples[1] = unhex(string[1]) * 16 + unhex(string[2]);
            samples[2] = unhex(string[3]) * 16 + unhex(string[4]);
            samples[3] = unhex(string[5]) * 16 + unhex(string[6]);
        }

        samples[0] /= 255.0;
        samples[1] /= 255.0;
        samples[2] /= 255.0;
        samples[3] /= 255.0;
    }

    else if (string[0] == 's' && string[1] == 'c' && string[2] == '#')
    {
        *csp = ctx->scrgb;

        if (count_commas(string) == 2)
            sscanf(string, "sc#%g,%g,%g", samples + 1, samples + 2, samples + 3);
        if (count_commas(string) == 3)
            sscanf(string, "sc#%g,%g,%g,%g", samples, samples + 1, samples + 2, samples + 3);
    }

    else if (strstr(string, "ContextColor ") == string)
    {
        /* Crack the string for profile name and sample values */
        strcpy(buf, string);

        profile = strchr(buf, ' ');
        if (!profile)
        {
            gs_warn1("cannot find icc profile uri in '%s'", string);
            return;
        }

        *profile++ = 0;
        p = strchr(profile, ' ');
        if (!p)
        {
            gs_warn1("cannot find component values in '%s'", profile);
            return;
        }

        *p++ = 0;
        n = count_commas(p) + 1;
        i = 0;
        while (i < n)
        {
            samples[i++] = atof(p);
            p = strchr(p, ',');
            if (!p)
                break;
            p ++;
            if (*p == ' ')
                p ++;
        }
        while (i < n)
        {
            samples[i++] = 0.0;
        }

        *csp = xps_read_icc_colorspace(ctx, base_uri, profile);
        if (!*csp)
        {
            /* Default fallbacks if the ICC stuff fails */
            switch (n)
            {
            case 2: *csp = ctx->gray; break; /* alpha + tint */
            case 4: *csp = ctx->srgb; break; /* alpha + RGB */
            case 5: *csp = ctx->cmyk; break; /* alpha + CMYK */
            default: *csp = ctx->gray; break;
            }
        }
    }
}

gs_color_space *
xps_read_icc_colorspace(xps_context_t *ctx, char *base_uri, char *profilename)
{
    gs_color_space *space;
    cmm_profile_t *profile;
    xps_part_t *part;
    char partname[1024];

    /* Find ICC colorspace part */
    xps_absolute_path(partname, base_uri, profilename, sizeof partname);

    /* See if we cached the profile */
    space = xps_hash_lookup(ctx->colorspace_table, partname);
    if (!space)
    {
        part = xps_read_part(ctx, partname);

        /* Problem finding profile.  Don't fail, just use default */
        if (!part) {
            gs_warn1("cannot find icc profile part: %s", partname);
            return NULL;
        }

        /* Create the profile */
        profile = gsicc_profile_new(NULL, ctx->memory, NULL, 0);

        /* Set buffer */
        profile->buffer = part->data;
        profile->buffer_size = part->size;

        /* Parse */
        gsicc_init_profile_info(profile);

        /* Problem with profile.  Don't fail, just use the default */
        if (profile->profile_handle == NULL)
        {
            gsicc_profile_reference(profile, -1);
            gs_warn1("there was a problem with the profile: %s", partname);
            return NULL;
        }

        /* Create a new colorspace and associate with the profile */
        gs_cspace_build_ICC(&space, NULL, ctx->memory);
        space->cmm_icc_profile_data = profile;

        /* Steal the buffer data before freeing the part */
        part->data = NULL;
        xps_free_part(ctx, part);

        /* Add colorspace to xps color cache. */
        xps_hash_insert(ctx, ctx->colorspace_table, xps_strdup(ctx, partname), space);
    }

    return space;
}

int
xps_parse_solid_color_brush(xps_context_t *ctx, char *base_uri, xps_resource_t *dict, xps_item_t *node)
{
    char *opacity_att;
    char *color_att;
    gs_color_space *colorspace;
    float samples[32];

    color_att = xps_att(node, "Color");
    opacity_att = xps_att(node, "Opacity");

    colorspace = ctx->srgb;
    samples[0] = 1.0;
    samples[1] = 0.0;
    samples[2] = 0.0;
    samples[3] = 0.0;

    if (color_att)
        xps_parse_color(ctx, base_uri, color_att, &colorspace, samples);

    if (opacity_att)
        samples[0] = atof(opacity_att);

    xps_set_color(ctx, colorspace, samples);

    xps_fill(ctx);

    return 0;
}