Coverage Report

Created: 2026-08-08 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gdevdevn.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
/* Example DeviceN process color model devices. */
17
18
#include "math_.h"
19
#include "string_.h"
20
#include "gdevprn.h"
21
#include "gsparam.h"
22
#include "gscrd.h"
23
#include "gscrdp.h"
24
#include "gxlum.h"
25
#include "gdevdcrd.h"
26
#include "gstypes.h"
27
#include "gxdcconv.h"
28
#include "gdevdevn.h"
29
#include "gsequivc.h"
30
#include "gxblend.h"
31
#include "gdevp14.h"
32
#include "gdevdevnprn.h"
33
#include "gxdevsop.h"
34
35
/*
36
 * Utility routines for common DeviceN related parameters:
37
 *   SeparationColorNames, SeparationOrder, and MaxSeparations
38
 */
39
40
/* Convert a gray color space to DeviceN colorants. */
41
void
42
gray_cs_to_devn_cm(const gx_device * dev, int * map, frac gray, frac out[])
43
7.12k
{
44
7.12k
    int i = dev->color_info.num_components - 1;
45
46
41.3k
    for(; i >= 0; i--)                  /* Clear colors */
47
34.1k
        out[i] = frac_0;
48
7.12k
    if ((i = map[3]) != GX_DEVICE_COLOR_MAX_COMPONENTS)
49
7.12k
        out[i] = frac_1 - gray;
50
7.12k
}
51
52
/* Convert an RGB color space to DeviceN colorants. */
53
void
54
rgb_cs_to_devn_cm(const gx_device * dev, int * map,
55
                const gs_gstate *pgs, frac r, frac g, frac b, frac out[])
56
0
{
57
0
    int i = dev->color_info.num_components - 1;
58
0
    frac cmyk[4];
59
60
0
    for(; i >= 0; i--)                  /* Clear colors */
61
0
        out[i] = frac_0;
62
0
    color_rgb_to_cmyk(r, g, b, pgs, cmyk, dev->memory);
63
0
    if ((i = map[0]) != GX_DEVICE_COLOR_MAX_COMPONENTS)
64
0
        out[i] = cmyk[0];
65
0
    if ((i = map[1]) != GX_DEVICE_COLOR_MAX_COMPONENTS)
66
0
        out[i] = cmyk[1];
67
0
    if ((i = map[2]) != GX_DEVICE_COLOR_MAX_COMPONENTS)
68
0
        out[i] = cmyk[2];
69
0
    if ((i = map[3]) != GX_DEVICE_COLOR_MAX_COMPONENTS)
70
0
        out[i] = cmyk[3];
71
0
}
72
73
/* Convert a CMYK color space to DeviceN colorants. */
74
void
75
cmyk_cs_to_devn_cm(const gx_device * dev, const int * map,
76
                frac c, frac m, frac y, frac k, frac out[])
77
19.4M
{
78
19.4M
    int i = dev->color_info.num_components - 1;
79
80
98.2M
    for(; i >= 0; i--)                  /* Clear colors */
81
78.7M
        out[i] = frac_0;
82
19.4M
    if ((i = map[0]) != GX_DEVICE_COLOR_MAX_COMPONENTS)
83
19.4M
        out[i] = c;
84
19.4M
    if ((i = map[1]) != GX_DEVICE_COLOR_MAX_COMPONENTS)
85
19.4M
        out[i] = m;
86
19.4M
    if ((i = map[2]) != GX_DEVICE_COLOR_MAX_COMPONENTS)
87
19.4M
        out[i] = y;
88
19.4M
    if ((i = map[3]) != GX_DEVICE_COLOR_MAX_COMPONENTS)
89
19.4M
        out[i] = k;
90
19.4M
}
91
92
/* Some devices need to create composite mappings of the spot colorants.
93
   This code was originally in the tiffsep device but was moved here to be
94
   sharable across multiple separation devices that need this capability */
95
96
97
/*
98
* Build the map to be used to create a CMYK equivalent to the current
99
* device components.
100
*/
101
void build_cmyk_map(gx_device *pdev, int num_comp,
102
    equivalent_cmyk_color_params *equiv_cmyk_colors,
103
    cmyk_composite_map * cmyk_map)
104
4.24k
{
105
4.24k
    int comp_num;
106
4.24k
    gs_devn_params *devn_params =  dev_proc(pdev, ret_devn_params)(pdev);
107
108
4.24k
    if (devn_params == NULL)
109
0
        return;
110
111
21.3k
    for (comp_num = 0; comp_num < num_comp; comp_num++) {
112
17.0k
        int sep_num = devn_params->separation_order_map[comp_num];
113
114
17.0k
        cmyk_map[comp_num].c = cmyk_map[comp_num].m =
115
17.0k
            cmyk_map[comp_num].y = cmyk_map[comp_num].k = frac_0;
116
        /* The tiffsep device has 4 standard colors:  CMYK */
117
17.0k
        if (sep_num < devn_params->num_std_colorant_names) {
118
16.9k
            switch (sep_num) {
119
4.24k
            case 0: cmyk_map[comp_num].c = frac_1; break;
120
4.24k
            case 1: cmyk_map[comp_num].m = frac_1; break;
121
4.24k
            case 2: cmyk_map[comp_num].y = frac_1; break;
122
4.24k
            case 3: cmyk_map[comp_num].k = frac_1; break;
123
16.9k
            }
124
16.9k
        } else {
125
86
            sep_num -= devn_params->num_std_colorant_names;
126
86
            if (equiv_cmyk_colors->color[sep_num].color_info_valid) {
127
74
                cmyk_map[comp_num].c = equiv_cmyk_colors->color[sep_num].c;
128
74
                cmyk_map[comp_num].m = equiv_cmyk_colors->color[sep_num].m;
129
74
                cmyk_map[comp_num].y = equiv_cmyk_colors->color[sep_num].y;
130
74
                cmyk_map[comp_num].k = equiv_cmyk_colors->color[sep_num].k;
131
74
            }
132
86
        }
133
17.0k
    }
134
4.24k
}
135
136
/*
137
 * This utility routine calculates the number of bits required to store
138
 * color information.  In general the values are rounded up to an even
139
 * byte boundary except those cases in which mulitple pixels can evenly
140
 * into a single byte.
141
 *
142
 * The parameter are:
143
 *   ncomp - The number of components (colorants) for the device.  Valid
144
 *           values are 1 to GX_DEVICE_COLOR_MAX_COMPONENTS
145
 *   bpc - The number of bits per component.  Valid values are 1, 2, 4, 5,
146
 *         and 8.
147
 * Input values are not tested for validity.
148
 */
149
int
150
bpc_to_depth(uchar ncomp, int bpc)
151
27.6k
{
152
27.6k
    static const byte depths[4][8] = {
153
27.6k
        {1, 2, 0, 4, 8, 0, 0, 8},
154
27.6k
        {2, 4, 0, 8, 16, 0, 0, 16},
155
27.6k
        {4, 8, 0, 16, 16, 0, 0, 24},
156
27.6k
        {4, 8, 0, 16, 32, 0, 0, 32}
157
27.6k
    };
158
159
27.6k
    if (ncomp <=4 && bpc <= 8)
160
3.86k
        return depths[ncomp -1][bpc-1];
161
23.7k
    else
162
23.7k
        return (ncomp * bpc + 7) & ~7;
163
27.6k
}
164
165
#define compare_color_names(name, name_size, str, str_size) \
166
4.18M
    (name_size == str_size && \
167
4.18M
        (strncmp((const char *)name, (const char *)str, name_size) == 0))
168
169
/*
170
 * This routine will check if a name matches any item in a list of process
171
 * color model colorant names.
172
 */
173
static bool
174
check_process_color_names(fixed_colorant_names_list plist,
175
                          const gs_param_string * pstring)
176
0
{
177
0
    if (plist) {
178
0
        uint size = pstring->size;
179
180
0
        while( *plist) {
181
0
            if (compare_color_names(*plist, strlen(*plist), pstring->data, size)) {
182
0
                return true;
183
0
            }
184
0
            plist++;
185
0
        }
186
0
    }
187
0
    return false;
188
0
}
189
190
static int count_process_color_names(fixed_colorant_names_list plist)
191
59.8k
{
192
59.8k
    int count = 0;
193
194
59.8k
    if (plist) {
195
299k
        while( *plist){
196
239k
            count++;
197
239k
            plist++;
198
239k
        }
199
59.8k
    }
200
59.8k
    return count;
201
59.8k
}
202
203
/* Check only the separation names */
204
int
205
check_separation_names(const gx_device * dev, const gs_devn_params * pparams,
206
    const char * pname, int name_size, int component_type, int number)
207
131k
{
208
131k
    const gs_separations * separations = &pparams->separations;
209
131k
    int num_spot = separations->num_separations;
210
131k
    int color_component_number = number;
211
131k
    int i;
212
213
132k
    for (i = 0; i<num_spot; i++) {
214
3.24k
        if (compare_color_names((const char *)separations->names[i].data,
215
3.24k
            separations->names[i].size, pname, name_size)) {
216
2.33k
            return color_component_number;
217
2.33k
        }
218
913
        color_component_number++;
219
913
    }
220
128k
    return -1;
221
131k
}
222
223
/*
224
 * This routine will check to see if the color component name  match those
225
 * of either the process color model colorants or the names on the
226
 * SeparationColorNames list.
227
 *
228
 * Parameters:
229
 *   dev - pointer to device data structure.
230
 *   pname - pointer to name (zero termination not required)
231
 *   nlength - length of the name
232
 *
233
 * This routine returns a positive value (0 to n) which is the device colorant
234
 * number if the name is found.  It returns a negative value if not found.
235
 */
236
int
237
check_pcm_and_separation_names(const gx_device * dev,
238
                const gs_devn_params * pparams, const char * pname,
239
                int name_size, int component_type)
240
1.36M
{
241
1.36M
    fixed_colorant_name * pcolor = pparams->std_colorant_names;
242
1.36M
    int color_component_number = 0;
243
244
    /* Check if the component is in the process color model list. */
245
1.36M
    if (pcolor) {
246
4.31M
        while( *pcolor) {
247
4.18M
            if (compare_color_names(pname, name_size, *pcolor, strlen(*pcolor)))
248
1.23M
                return color_component_number;
249
2.95M
            pcolor++;
250
2.95M
            color_component_number++;
251
2.95M
        }
252
1.36M
    }
253
    /* For some devices, Tags is part of the process color model list. If so,
254
     * that throws us off here since it is thrown at the end of the list. Adjust. */
255
131k
    if (device_encodes_tags(dev)) {
256
0
        color_component_number--;
257
0
    }
258
259
131k
    return check_separation_names(dev, pparams, pname, name_size,
260
131k
        component_type, color_component_number);
261
1.36M
}
262
263
/*
264
 * This routine will check to see if the color component name  match those
265
 * that are available amoung the current device's color components.
266
 *
267
 * Parameters:
268
 *   dev - pointer to device data structure.
269
 *   pname - pointer to name (zero termination not required)
270
 *   nlength - length of the name
271
 *   component_type - separation name or not
272
 *   pdevn_params - pointer to device's DeviceN paramters
273
 *   pequiv_colors - pointer to equivalent color structure (may be NULL)
274
 *
275
 * This routine returns a positive value (0 to n) which is the device colorant
276
 * number if the name is found.  It returns GX_DEVICE_COLOR_MAX_COMPONENTS if
277
 * the color component is found but is not being used due to the
278
 * SeparationOrder device parameter.  It returns a negative value if not found.
279
 *
280
 * This routine will also add separations to the device if space is
281
 * available.
282
 */
283
int
284
devn_get_color_comp_index(gx_device * dev, gs_devn_params * pdevn_params,
285
                    equivalent_cmyk_color_params * pequiv_colors,
286
                    const char * pname, int name_size, int component_type,
287
                    int auto_spot_colors)
288
1.36M
{
289
1.36M
    int num_order = pdevn_params->num_separation_order_names;
290
1.36M
    int color_component_number = 0;
291
1.36M
    int num_res_comps = pdevn_params->num_reserved_components;
292
1.36M
    int max_spot_colors = GX_DEVICE_MAX_SEPARATIONS - pdevn_params->num_std_colorant_names - num_res_comps;
293
294
    /*
295
     * Check if the component is in either the process color model list
296
     * or in the SeparationNames list.
297
     */
298
1.36M
    color_component_number = check_pcm_and_separation_names(dev, pdevn_params,
299
1.36M
                                        pname, name_size, component_type);
300
301
    /* If we have a valid component */
302
1.36M
    if (color_component_number >= 0) {
303
        /* Check if the component is in the separation order map. */
304
1.23M
        if (num_order)
305
0
            color_component_number =
306
0
                pdevn_params->separation_order_map[color_component_number];
307
1.23M
        else
308
            /*
309
             * We can have more spot colors than we can image.  We simply
310
             * ignore the component (i.e. treat it the same as we would
311
             * treat a component that is not in the separation order map).
312
             * Note:  Most device do not allow more spot colors than we can
313
             * image.  (See the options for auto_spot_color in gdevdevn.h.)
314
             */
315
1.23M
            if (color_component_number >= dev->color_info.max_components)
316
0
                color_component_number = GX_DEVICE_COLOR_MAX_COMPONENTS;
317
318
1.23M
        return color_component_number;
319
1.23M
    }
320
    /*
321
     * The given name does not match any of our current components or
322
     * separations.  Check if we should add the spot color to our list.
323
     * If the SeparationOrder parameter has been specified then we should
324
     * already have our complete list of desired spot colorants.
325
     */
326
128k
    if (component_type != SEPARATION_NAME ||
327
190
            auto_spot_colors == NO_AUTO_SPOT_COLORS ||
328
190
            pdevn_params->num_separation_order_names != 0)
329
128k
        return -1;      /* Do not add --> indicate colorant unknown. */
330
331
    /* Make sure the name is not "None"  this is sometimes
332
       within a DeviceN list and should not be added as one of the
333
       separations.  */
334
190
    if (strncmp(pname, "None", name_size) == 0) {
335
0
        return -1;
336
0
    }
337
    /* Additive devices should NOT have C/M/Y/K Colorants added to them.
338
     * This is a decision we take here to avoid problems with PDFI not
339
     * counting such colorants as spots. */
340
190
    if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE) {
341
0
        if (name_size == 5 && strncmp(pname, "Black", 7) == 0)
342
0
            return -1;
343
0
        if (name_size == 4 && strncmp(pname, "Cyan", 4) == 0)
344
0
            return -1;
345
0
        if (name_size == 7 && strncmp(pname, "Magenta", 7) == 0)
346
0
            return -1;
347
0
        if (name_size == 6 && strncmp(pname, "Yellow", 6) == 0)
348
0
            return -1;
349
0
    }
350
351
    /*
352
     * Check if we have room for another spot colorant.
353
     */
354
190
    if (auto_spot_colors == ENABLE_AUTO_SPOT_COLORS)
355
        /* limit max_spot_colors to what the device can handle given max_components */
356
190
        max_spot_colors = min(max_spot_colors,
357
190
                              dev->color_info.max_components - pdevn_params->num_std_colorant_names - num_res_comps);
358
190
    if (pdevn_params->separations.num_separations < max_spot_colors) {
359
190
        byte * sep_name;
360
190
        gs_separations * separations = &pdevn_params->separations;
361
190
        int sep_num = separations->num_separations++;
362
        /* We have a new spot colorant - put in stable memory to avoid "restore" */
363
190
        sep_name = gs_alloc_bytes(dev->memory->stable_memory, name_size, "devn_get_color_comp_index");
364
190
        if (sep_name == NULL) {
365
0
            separations->num_separations--; /* we didn't add it */
366
0
            return -1;
367
0
        }
368
190
        memcpy(sep_name, pname, name_size);
369
190
        separations->names[sep_num].size = name_size;
370
190
        separations->names[sep_num].data = sep_name;
371
190
        color_component_number = sep_num + pdevn_params->num_std_colorant_names;
372
190
        if (color_component_number >= dev->color_info.max_components)
373
0
            color_component_number = GX_DEVICE_COLOR_MAX_COMPONENTS;
374
190
        else
375
190
            pdevn_params->separation_order_map[color_component_number] =
376
190
                                               color_component_number;
377
378
190
        if (pequiv_colors != NULL) {
379
            /* Indicate that we need to find equivalent CMYK color. */
380
190
            pequiv_colors->color[sep_num].color_info_valid = false;
381
190
            pequiv_colors->all_color_info_valid = false;
382
190
        }
383
190
    }
384
385
190
    return color_component_number;
386
190
}
387
388
#define set_param_array(a, d, s)\
389
388k
  (a.data = d, a.size = s, a.persistent = false);
390
391
static int
392
gs_device_supports_spots(gx_device *pdev)
393
253k
{
394
        /* Separations are only valid with a subtractive color model,
395
         * or additive ones that specifically want them. */
396
253k
        if (pdev->color_info.polarity == GX_CINFO_POLARITY_SUBTRACTIVE)
397
253k
                return 1;
398
0
        else if (pdev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
399
0
                return (dev_proc(pdev, dev_spec_op)(pdev, gxdso_is_sep_supporting_additive_device, NULL, 0) > 0);
400
0
        return 0;
401
253k
}
402
403
/* Get parameters.  We provide a default CRD. */
404
int
405
devn_get_params(gx_device * pdev, gs_param_list * plist,
406
    gs_devn_params * pdevn_params, equivalent_cmyk_color_params * pequiv_colors)
407
194k
{
408
194k
    int code, i = 0, spot_num;
409
194k
    bool seprs = false;
410
194k
    gs_param_string_array scna;
411
194k
    gs_param_string_array sona;
412
194k
    gs_param_int_array equiv_cmyk;
413
    /* there are 5 ints  per colorant in equiv_elements: a valid flag and an int for C, M, Y and K */
414
194k
    int equiv_elements[5 * GX_DEVICE_MAX_SEPARATIONS] = { 0 }; /* 5 * max_colors */
415
    /* limit in case num_separations in pdevn_params exceeds what is expected. */
416
194k
    int num_separations = min(pdevn_params->separations.num_separations, sizeof(equiv_elements)/(5*sizeof(int)));
417
418
194k
    set_param_array(scna, NULL, 0);
419
194k
    set_param_array(sona, NULL, 0);
420
421
194k
    if (pequiv_colors != NULL) {
422
194k
        for (spot_num = 0; spot_num < num_separations; spot_num++) {
423
133
            equiv_elements[i++] = pequiv_colors->color[spot_num].color_info_valid ? 1 : 0;
424
133
            equiv_elements[i++] = pequiv_colors->color[spot_num].c;
425
133
            equiv_elements[i++] = pequiv_colors->color[spot_num].m;
426
133
            equiv_elements[i++] = pequiv_colors->color[spot_num].y;
427
133
            equiv_elements[i++] = pequiv_colors->color[spot_num].k;
428
133
        }
429
194k
    }
430
431
194k
    equiv_cmyk.data = equiv_elements;
432
194k
    equiv_cmyk.size = i;
433
194k
    equiv_cmyk.persistent = false;
434
435
194k
    if ( (code = sample_device_crd_get_params(pdev, plist, "CRDDefault")) < 0 ||
436
194k
         (code = param_write_name_array(plist, "SeparationColorNames", &scna)) < 0 ||
437
194k
         (code = param_write_name_array(plist, "SeparationOrder", &sona)) < 0 ||
438
194k
         (code = param_write_bool(plist, "Separations", &seprs)) < 0)
439
0
        return code;
440
441
194k
    if (gs_device_supports_spots(pdev) &&
442
194k
        (code = param_write_int(plist, "PageSpotColors", &(pdevn_params->page_spot_colors))) < 0)
443
0
        return code;
444
445
194k
    if (pdevn_params->separations.num_separations > 0)
446
86
        code = param_write_int_array(plist, ".EquivCMYKColors", &equiv_cmyk);
447
448
194k
    return code;
449
194k
}
450
#undef set_param_array
451
452
#define BEGIN_ARRAY_PARAM(pread, pname, pa, psize, e)\
453
179k
    BEGIN\
454
179k
    switch (code = pread(plist, (param_name = pname), &(pa))) {\
455
40.8k
      case 0:\
456
40.8k
        if ((pa).size != psize) {\
457
0
          ecode = gs_note_error(gs_error_rangecheck);\
458
0
          (pa).data = 0;        /* mark as not filled */\
459
0
        } else
460
#define END_ARRAY_PARAM(pa, e)\
461
0
        goto e;\
462
40.8k
      default:\
463
0
        ecode = code;\
464
0
e:      param_signal_error(plist, param_name, ecode);\
465
138k
      case 1:\
466
138k
        (pa).data = 0;          /* mark as not filled */\
467
358k
    }\
468
358k
    END
469
470
/*
471
 * Utility routine for handling DeviceN related parameters.  This routine
472
 * may modify the color_info, devn_params, and the equiv_cmyk_colors fields.
473
 *
474
 * Note:  This routine does not restore values in case of a problem.  This
475
 * is left to the caller.
476
 */
477
int
478
devn_put_params(gx_device * pdev, gs_param_list * plist,
479
    gs_devn_params * pdevn_params, equivalent_cmyk_color_params * pequiv_colors)
480
59.8k
{
481
59.8k
    int code = 0, ecode, i;
482
59.8k
    gs_param_name param_name;
483
59.8k
    int npcmcolors = pdevn_params->num_std_colorant_names;
484
59.8k
    int num_spot = pdevn_params->separations.num_separations;
485
59.8k
    bool num_spot_changed = false;
486
59.8k
    int num_order = pdevn_params->num_separation_order_names;
487
59.8k
    int max_sep = pdevn_params->max_separations;
488
59.8k
    int page_spot_colors = pdevn_params->page_spot_colors;
489
59.8k
    gs_param_string_array scna;         /* SeparationColorNames array */
490
59.8k
    gs_param_string_array sona;         /* SeparationOrder names array */
491
59.8k
    gs_param_int_array equiv_cmyk;      /* equivalent_cmyk_color_params */
492
59.8k
    int num_res_comps = pdevn_params->num_reserved_components;
493
59.8k
    int num_std_names = npcmcolors;
494
495
    /* Get the SeparationOrder names */
496
65.5k
    BEGIN_ARRAY_PARAM(param_read_name_array, "SeparationOrder",
497
65.5k
                                        sona, sona.size, sone)
498
5.73k
    {
499
5.73k
        break;
500
5.73k
    } END_ARRAY_PARAM(sona, sone);
501
59.8k
    if (sona.data != 0 && sona.size > pdev->color_info.max_components) {
502
0
        param_signal_error(plist, "SeparationOrder", gs_error_rangecheck);
503
0
        return_error(gs_error_rangecheck);
504
0
    }
505
506
    /* Get the SeparationColorNames */
507
94.8k
    BEGIN_ARRAY_PARAM(param_read_name_array, "SeparationColorNames",
508
94.8k
                                        scna, scna.size, scne)
509
35.0k
    {
510
35.0k
        break;
511
35.0k
    } END_ARRAY_PARAM(scna, scne);
512
59.8k
    if (scna.data != 0 && scna.size > pdev->color_info.max_components) {
513
0
        param_signal_error(plist, "SeparationColorNames", gs_error_rangecheck);
514
0
        return_error(gs_error_rangecheck);
515
0
    }
516
    /* Get the equivalent_cmyk_color_params -- array is N * 5 elements */
517
59.8k
    BEGIN_ARRAY_PARAM(param_read_int_array, ".EquivCMYKColors",
518
59.8k
                                        equiv_cmyk, equiv_cmyk.size, equiv_cmyk_e)
519
0
    {
520
0
        break;
521
0
    } END_ARRAY_PARAM(equiv_cmyk, equiv_cmyk_e);
522
59.8k
    if (equiv_cmyk.data != 0 && (equiv_cmyk.size > 5 * pdev->color_info.max_components || equiv_cmyk.size % 5 != 0)) {
523
0
        param_signal_error(plist, ".EquivCMYKColors", gs_error_rangecheck);
524
0
        return_error(gs_error_rangecheck);
525
0
    }
526
527
59.8k
    if (gs_device_supports_spots(pdev)) {
528
59.8k
        fixed_colorant_names_list pcomp_names = pdevn_params->std_colorant_names;
529
        /* You would expect that pdevn_params->num_std_colorant_names would have this value but it does not.
530
         * That appears to be copied from the 'ncomps' of the device and that has to be the number of components
531
         * in the 'base' colour model, 1, 3 or 4 for Gray, RGB or CMYK. Other kinds of DeviceN devices can have
532
         * additional standard names, eg Tags, or Artifex Orange and Artifex Green, but these are not counted in
533
         * the num_std_colorant_names. They are, however, listed in pdevn_params->std_colorant_names (when is a
534
         * std_colorant_name not a std_colorant_name ?), which is checked to see if a SeparationOrder name is one
535
         * of the inks we are already dealing with. If it is, then we *don't* add it to num_spots.
536
         * So we need to actually count the number of colorants in std_colorant_names to make sure that we
537
         * don't exceed the maximum number of components.
538
         */
539
59.8k
        num_std_names = count_process_color_names(pcomp_names);
540
        /*
541
         * Process the SeparationColorNames.  Remove any names that already
542
         * match the process color model colorant names for the device.
543
         */
544
59.8k
        if (scna.data != 0) {
545
0
            int num_names = scna.size, num_std_names = 0;
546
547
0
            num_spot = 0;
548
            /* And now we check each ink to see if it's already in the separations list. If not then we count
549
             * up the number of new inks
550
             */
551
0
            for (i = 0; i < num_names; i++) {
552
                /* Verify that the name is not one of our process colorants */
553
0
                if (!check_process_color_names(pcomp_names, &scna.data[i])) {
554
0
                    if (check_separation_names(pdev, pdevn_params, (const char *)scna.data[i].data, scna.data[i].size, 0, 0) < 0)
555
0
                        num_spot++;
556
0
                }
557
0
            }
558
            /* Now we can check the number of standard colourants (eg CMYKOG) + number of existing separations + number of new separations
559
             * and make sure we have enough components to handle all of them
560
             */
561
0
            if (num_std_names + pdevn_params->separations.num_separations + num_spot > pdev->color_info.max_components) {
562
0
                param_signal_error(plist, "SeparationColorNames", gs_error_rangecheck);
563
0
                return_error(gs_error_rangecheck);
564
0
            }
565
            /* Save this value because we need it after the loop */
566
0
            num_spot = pdevn_params->separations.num_separations;
567
            /* Now go through the ink names again, this time if we get a new one we add it to the list of
568
             * separations. We can do that safely now because we know we can't overflow the array of names.
569
             */
570
0
            for (i = 0; i < num_names; i++) {
571
                /* Verify that the name is not one of our process colorants */
572
0
                if (!check_process_color_names(pcomp_names, &scna.data[i])) {
573
0
                    if (check_separation_names(pdev, pdevn_params, (const char *)scna.data[i].data, scna.data[i].size, 0, 0) < 0) {
574
                        /* We have a new separation */
575
0
                        byte * sep_name;
576
0
                        int name_size = scna.data[i].size;
577
578
0
                        sep_name = (byte *)gs_alloc_bytes(pdev->memory,
579
0
                            name_size, "devicen_put_params_no_sep_order");
580
0
                        if (sep_name == NULL) {
581
0
                            param_signal_error(plist, "SeparationColorNames", gs_error_VMerror);
582
0
                            return_error(gs_error_VMerror);
583
0
                        }
584
0
                        memcpy(sep_name, scna.data[i].data, name_size);
585
0
                        pdevn_params->separations.names[pdevn_params->separations.num_separations].size = name_size;
586
0
                        pdevn_params->separations.names[pdevn_params->separations.num_separations].data = sep_name;
587
0
                        if (pequiv_colors != NULL) {
588
                            /* Indicate that we need to find equivalent CMYK color. */
589
0
                            pequiv_colors->color[num_spot].color_info_valid = false;
590
0
                            pequiv_colors->all_color_info_valid = false;
591
0
                        }
592
0
                        pdevn_params->separations.num_separations++;
593
0
                        num_spot_changed = true;
594
0
                    }
595
0
                }
596
0
            }
597
598
0
            for (i = num_spot; i < pdevn_params->separations.num_separations; i++)
599
0
                pdevn_params->separation_order_map[i + pdevn_params->num_std_colorant_names] =
600
0
                i + pdevn_params->num_std_colorant_names;
601
            /* We use num_spot below, to reset pdevn_params->separations.num_separations, set it
602
             * here in case it gets used elsewhere
603
             */
604
0
            num_spot = pdevn_params->separations.num_separations;
605
0
        }
606
        /* Process any .EquivCMYKColors info */
607
59.8k
        if (equiv_cmyk.data != 0 && pequiv_colors != 0) {
608
0
            int spot_num = 0;
609
610
0
            for (i=0; i < equiv_cmyk.size; i += 5) { /* valid, C, M, Y, K for each equiv_color */
611
0
                if (equiv_cmyk.data[i] == 0) {
612
                    /* This occurs if we've added a spot, but not yet set it's equiv color */
613
0
                    pequiv_colors->color[spot_num].color_info_valid = false;
614
0
                    pequiv_colors->all_color_info_valid = false;
615
0
                } else {
616
0
                    pequiv_colors->color[spot_num].color_info_valid = true;
617
0
                    pequiv_colors->color[spot_num].c = (frac)(equiv_cmyk.data[i+1]);
618
0
                    pequiv_colors->color[spot_num].m = (frac)(equiv_cmyk.data[i+2]);
619
0
                    pequiv_colors->color[spot_num].y = (frac)(equiv_cmyk.data[i+3]);
620
0
                    pequiv_colors->color[spot_num].k = (frac)(equiv_cmyk.data[i+4]);
621
0
                }
622
0
                spot_num++;
623
0
            }
624
0
        }
625
        /*
626
         * Process the SeparationOrder names.
627
         */
628
59.8k
        if (sona.data != 0) {
629
0
            int comp_num;
630
631
0
            num_order = sona.size;
632
0
            for (i = 0; i < num_order; i++) {
633
                /*
634
                 * Check if names match either the process color model or
635
                 * SeparationColorNames.  If not then error.
636
                 */
637
0
                if ((comp_num = (*dev_proc(pdev, get_color_comp_index))
638
0
                                (pdev, (const char *)sona.data[i].data,
639
0
                                sona.data[i].size, SEPARATION_NAME)) < 0) {
640
0
                    param_signal_error(plist, "SeparationOrder", gs_error_rangecheck);
641
0
                    return_error(gs_error_rangecheck);
642
0
                }
643
0
                pdevn_params->separation_order_map[i] = comp_num;
644
                /* If the device enabled AUTO_SPOT_COLORS some separations may */
645
                /* have been added. Adjust num_spots if so.                    */
646
0
                if (num_spot != pdevn_params->separations.num_separations) {
647
0
                    num_spot = pdevn_params->separations.num_separations;
648
0
                    num_spot_changed = true;
649
0
                }
650
0
            }
651
0
        }
652
        /*
653
         * Adobe says that MaxSeparations is supposed to be 'read only'
654
         * however we use this to allow the specification of the maximum
655
         * number of separations.  Memory is allocated for the specified
656
         * number of separations.  This allows us to then accept separation
657
         * colors in color spaces even if they we not specified at the start
658
         * of the image file.
659
         */
660
59.8k
        code = param_read_int(plist, param_name = "MaxSeparations", &max_sep);
661
59.8k
        switch (code) {
662
0
            default:
663
0
                param_signal_error(plist, param_name, code);
664
54.0k
            case 1:
665
54.0k
                break;
666
5.73k
            case 0:
667
5.73k
                if (max_sep < 1 || max_sep > GX_DEVICE_COLOR_MAX_COMPONENTS) {
668
0
                    param_signal_error(plist, "MaxSeparations", gs_error_rangecheck);
669
0
                    return_error(gs_error_rangecheck);
670
0
                }
671
59.8k
        }
672
        /*
673
         * The PDF interpreter scans the resources for pages to try to
674
         * determine the number of spot colors.  (Unfortuneately there is
675
         * no way to determine the number of spot colors for a PS page
676
         * except to interpret the entire page.)  The spot color count for
677
         * a PDF page may be high since there may be spot colors in a PDF
678
         * page's resources that are not used.  However this does give us
679
         * an upper limit on the number of spot colors.  A value of -1
680
         * indicates that the number of spot colors in unknown (a PS file).
681
         */
682
59.8k
        code = param_read_int(plist, param_name = "PageSpotColors",
683
59.8k
                                                        &page_spot_colors);
684
59.8k
        switch (code) {
685
0
            default:
686
0
                param_signal_error(plist, param_name, code);
687
45.8k
            case 1:
688
45.8k
                break;
689
13.9k
            case 0:
690
13.9k
                if (page_spot_colors < -1) {
691
0
                    param_signal_error(plist, "PageSpotColors", gs_error_rangecheck);
692
0
                    return_error(gs_error_rangecheck);
693
0
                }
694
13.9k
                if (page_spot_colors > pdev->color_info.max_components - pdevn_params->num_std_colorant_names - num_res_comps)
695
0
                    page_spot_colors = pdev->color_info.max_components - pdevn_params->num_std_colorant_names - num_res_comps;
696
                    /* Need to leave room for the process colors (and tags!) in GX_DEVICE_COLOR_MAX_COMPONENTS  */
697
59.8k
        }
698
        /*
699
         * The DeviceN device can have zero components if nothing has been
700
         * specified.  This causes some problems so force at least one
701
         * component until something is specified.
702
         */
703
59.8k
        if (!pdev->color_info.num_components)
704
0
            pdev->color_info.num_components = 1;
705
        /*
706
         * Update the number of device components if we have changes in
707
         * SeparationColorNames, SeparationOrder, or MaxSeparations.
708
         */
709
59.8k
        if (num_spot_changed || pdevn_params->max_separations != max_sep ||
710
54.4k
                    pdevn_params->num_separation_order_names != num_order ||
711
54.4k
                    pdevn_params->page_spot_colors != page_spot_colors) {
712
10.6k
            int has_tags = device_encodes_tags(pdev);
713
10.6k
            pdevn_params->separations.num_separations = num_spot;
714
10.6k
            pdevn_params->num_separation_order_names = num_order;
715
10.6k
            pdevn_params->max_separations = max_sep;
716
10.6k
            pdevn_params->page_spot_colors = page_spot_colors;
717
10.6k
            if (max_sep != 0)
718
5.31k
                 pdev->color_info.max_components = max_sep;
719
            /*
720
             * If we have SeparationOrder specified then the number of
721
             * components is given by the number of names in the list.
722
             * Otherwise check if the MaxSeparations parameter has specified
723
             * a value.  If so then use that value, otherwise use the number
724
             * of ProcessColorModel components plus the number of
725
             * SeparationColorNames is used.
726
             */
727
10.6k
            pdev->color_info.num_components = (num_order)
728
10.6k
                ? num_order
729
10.6k
                : (page_spot_colors >= 0)
730
10.6k
                    ? num_std_names + page_spot_colors
731
10.6k
                    : pdev->color_info.max_components;
732
10.6k
            pdev->color_info.num_components += has_tags;
733
734
10.6k
            if (pdev->color_info.num_components >
735
10.6k
                    pdev->color_info.max_components)
736
0
                pdev->color_info.num_components =
737
0
                        pdev->color_info.max_components;
738
739
10.6k
            if (pdev->color_info.num_components > pdev->num_planar_planes)
740
5.26k
                pdev->num_planar_planes = pdev->color_info.num_components;
741
742
            /*
743
             * See earlier comment about the depth and non compressed
744
             * pixel encoding.
745
             */
746
10.6k
            if (pdev->num_planar_planes)
747
10.6k
                pdev->color_info.depth = bpc_to_depth(pdev->num_planar_planes,
748
10.6k
                                                      pdevn_params->bitspercomponent);
749
0
            else
750
0
                pdev->color_info.depth = bpc_to_depth(pdev->color_info.num_components,
751
0
                                                      pdevn_params->bitspercomponent);
752
10.6k
        }
753
59.8k
    }
754
59.8k
    if (code >= 0)
755
59.8k
    {
756
59.8k
        int ecode = dev_proc(pdev, dev_spec_op)(pdev, gxdso_adjust_colors, NULL, 0);
757
59.8k
        if (ecode < 0 && ecode != gs_error_undefined)
758
0
            code = ecode;
759
59.8k
    }
760
59.8k
    return code;
761
59.8k
}
762
763
/* Free the copied deviceN parameters */
764
void
765
devn_free_params(gx_device *thread_cdev)
766
1.01M
{
767
1.01M
    gs_devn_params *devn_params;
768
1.01M
    int k;
769
770
1.01M
    devn_params = dev_proc(thread_cdev, ret_devn_params)(thread_cdev);
771
1.01M
    if (devn_params == NULL) return;
772
773
1.01M
    for (k = 0; k < devn_params->separations.num_separations; k++) {
774
300
        gs_free_object(thread_cdev->memory,
775
300
                       devn_params->separations.names[k].data,
776
300
                       "devn_free_params");
777
300
        devn_params->separations.names[k].data = NULL;
778
300
    }
779
780
1.01M
    for (k = 0; k < devn_params->pdf14_separations.num_separations; k++) {
781
0
        gs_free_object(thread_cdev->memory,
782
0
                       devn_params->pdf14_separations.names[k].data,
783
0
                       "devn_free_params");
784
0
        devn_params->pdf14_separations.names[k].data = NULL;
785
0
    }
786
1.01M
}
787
788
/* This is used to copy the deviceN parameters from the parent clist device to the
789
   individual thread clist devices for multi-threaded rendering */
790
int
791
devn_copy_params(gx_device * psrcdev, gx_device * pdesdev)
792
68.8k
{
793
68.8k
    gs_devn_params *src_devn_params, *des_devn_params;
794
68.8k
    int code = 0;
795
68.8k
    int k;
796
797
    /* Get pointers to the parameters */
798
68.8k
    src_devn_params = dev_proc(psrcdev, ret_devn_params)(psrcdev);
799
68.8k
    des_devn_params = dev_proc(pdesdev, ret_devn_params)(pdesdev);
800
68.8k
    if (src_devn_params == NULL || des_devn_params == NULL)
801
50
        return gs_note_error(gs_error_undefined);
802
803
    /* First the easy items */
804
68.8k
    des_devn_params->bitspercomponent = src_devn_params->bitspercomponent;
805
68.8k
    des_devn_params->max_separations = src_devn_params->max_separations;
806
68.8k
    des_devn_params->num_separation_order_names =
807
68.8k
        src_devn_params->num_separation_order_names;
808
68.8k
    des_devn_params->num_std_colorant_names =
809
68.8k
        src_devn_params->num_std_colorant_names;
810
68.8k
    des_devn_params->page_spot_colors = src_devn_params->page_spot_colors;
811
68.8k
    des_devn_params->std_colorant_names = src_devn_params->std_colorant_names;
812
68.8k
    des_devn_params->separations.num_separations
813
68.8k
        = src_devn_params->separations.num_separations;
814
    /* Now the more complex structures */
815
    /* Spot color names */
816
69.0k
    for (k = 0; k < des_devn_params->separations.num_separations; k++) {
817
243
        byte * sep_name;
818
243
        int name_size = src_devn_params->separations.names[k].size;
819
243
        sep_name = (byte *)gs_alloc_bytes(pdesdev->memory->stable_memory,
820
243
                                          name_size, "devn_copy_params");
821
243
        if (sep_name == NULL) {
822
0
            return_error(gs_error_VMerror);
823
0
        }
824
243
        memcpy(sep_name, src_devn_params->separations.names[k].data, name_size);
825
243
        des_devn_params->separations.names[k].size = name_size;
826
243
        des_devn_params->separations.names[k].data = sep_name;
827
243
    }
828
    /* Order map */
829
68.8k
    memcpy(des_devn_params->separation_order_map,
830
68.8k
           src_devn_params->separation_order_map, sizeof(gs_separation_map));
831
832
    /* Handle the PDF14 items if they are there */
833
68.8k
    des_devn_params->pdf14_separations.num_separations
834
68.8k
        = src_devn_params->pdf14_separations.num_separations;
835
68.8k
    for (k = 0; k < des_devn_params->pdf14_separations.num_separations; k++) {
836
0
        byte * sep_name;
837
0
        int name_size = src_devn_params->pdf14_separations.names[k].size;
838
0
        sep_name = (byte *)gs_alloc_bytes(pdesdev->memory->stable_memory,
839
0
                                          name_size, "devn_copy_params");
840
0
        if (sep_name == NULL) {
841
0
            return_error(gs_error_VMerror);
842
0
        }
843
0
        memcpy(sep_name, src_devn_params->pdf14_separations.names[k].data,
844
0
               name_size);
845
0
        des_devn_params->pdf14_separations.names[k].size = name_size;
846
0
        des_devn_params->pdf14_separations.names[k].data = sep_name;
847
0
    }
848
68.8k
    return code;
849
68.8k
}
850
851
static int
852
compare_equivalent_cmyk_color_params(const equivalent_cmyk_color_params *pequiv_colors1, const equivalent_cmyk_color_params *pequiv_colors2)
853
47.6k
{
854
47.6k
  int i;
855
47.6k
  if (pequiv_colors1->all_color_info_valid != pequiv_colors2->all_color_info_valid)
856
0
    return(1);
857
3.09M
  for (i=0;  i<GX_DEVICE_MAX_SEPARATIONS;  i++) {
858
3.05M
    if (pequiv_colors1->color[i].color_info_valid != pequiv_colors2->color[i].color_info_valid)
859
0
      return(1);
860
3.05M
    if (pequiv_colors1->color[i].c                != pequiv_colors2->color[i].c               )
861
0
      return(1);
862
3.05M
    if (pequiv_colors1->color[i].m                != pequiv_colors2->color[i].m               )
863
0
      return(1);
864
3.05M
    if (pequiv_colors1->color[i].y                != pequiv_colors2->color[i].y               )
865
0
      return(1);
866
3.05M
    if (pequiv_colors1->color[i].k                != pequiv_colors2->color[i].k               )
867
0
      return(1);
868
3.05M
  }
869
47.6k
  return(0);
870
47.6k
}
871
872
static bool separations_equal(const gs_separations *p1, const gs_separations *p2)
873
95.3k
{
874
95.3k
    int k;
875
876
95.3k
    if (p1->num_separations != p2->num_separations)
877
0
        return false;
878
95.3k
    for (k = 0; k < p1->num_separations; k++) {
879
0
        if (p1->names[k].size != p2->names[k].size)
880
0
            return false;
881
0
        else if (p1->names[k].size > 0) {
882
0
            if (memcmp(p1->names[k].data, p2->names[k].data, p1->names[k].size) != 0)
883
0
                return false;
884
0
        }
885
0
    }
886
95.3k
    return true;
887
95.3k
}
888
889
static bool devn_params_equal(const gs_devn_params *p1, const gs_devn_params *p2)
890
51.6k
{
891
51.6k
    if (p1->bitspercomponent != p2->bitspercomponent)
892
0
        return false;
893
51.6k
    if (p1->max_separations != p2->max_separations)
894
57
        return false;
895
51.6k
    if (p1->num_separation_order_names != p2->num_separation_order_names)
896
0
        return false;
897
51.6k
    if (p1->num_std_colorant_names != p2->num_std_colorant_names)
898
0
        return false;
899
51.6k
    if (p1->page_spot_colors != p2->page_spot_colors)
900
3.94k
        return false;
901
47.6k
    if (!separations_equal(&p1->pdf14_separations, &p2->pdf14_separations))
902
0
        return false;
903
47.6k
    if (!separations_equal(&p1->separations, &p2->separations))
904
0
        return false;
905
47.6k
    if (memcmp(p1->separation_order_map, p2->separation_order_map, sizeof(gs_separation_map)) != 0)
906
0
        return false;
907
47.6k
    if (p1->std_colorant_names != p2->std_colorant_names)
908
0
        return false;
909
47.6k
    return true;
910
47.6k
}
911
912
int
913
devn_generic_put_params(gx_device *pdev, gs_param_list *plist,
914
                        gs_devn_params *pdevn_params, equivalent_cmyk_color_params *pequiv_colors,
915
                        int is_printer)
916
59.8k
{
917
59.8k
    int code;
918
    /* Save current data in case we have a problem */
919
59.8k
    gx_device_color_info save_info = pdev->color_info;
920
59.8k
    gs_devn_params saved_devn_params = *pdevn_params;
921
59.8k
    equivalent_cmyk_color_params saved_equiv_colors;
922
59.8k
    int save_planes = pdev->num_planar_planes;
923
924
59.8k
    if (pequiv_colors != NULL)
925
59.8k
        saved_equiv_colors = *pequiv_colors;
926
927
    /* Use utility routine to handle parameters */
928
59.8k
    code = devn_put_params(pdev, plist, pdevn_params, pequiv_colors);
929
930
    /* Check for default printer parameters */
931
59.8k
    if (is_printer && code >= 0)
932
59.8k
        code = gdev_prn_put_params(pdev, plist);
933
934
    /* If we have an error then restore original data. */
935
59.8k
    if (code < 0) {
936
31
        pdev->color_info = save_info;
937
31
        *pdevn_params = saved_devn_params;
938
31
        if (pequiv_colors != NULL)
939
31
           *pequiv_colors = saved_equiv_colors;
940
31
        return code;
941
31
    }
942
943
    /* If anything changed, then close the device, etc. */
944
59.7k
    if (!gx_color_info_equal(&pdev->color_info, &save_info) ||
945
51.6k
        !devn_params_equal(pdevn_params, &saved_devn_params) ||
946
47.6k
        (pequiv_colors != NULL &&
947
47.6k
            compare_equivalent_cmyk_color_params(pequiv_colors, &saved_equiv_colors)) ||
948
47.6k
        pdev->num_planar_planes != save_planes) {
949
12.0k
        gx_device *parent_dev = pdev;
950
12.0k
        gx_device_color_info resave_info = pdev->color_info;
951
12.0k
        int resave_planes = pdev->num_planar_planes;
952
953
12.0k
        while (parent_dev->parent != NULL)
954
0
            parent_dev = parent_dev->parent;
955
956
        /* Temporarily restore the old color_info, so the close happens with
957
         * the old version. In particular this allows Nup to flush properly. */
958
12.0k
        pdev->color_info = save_info;
959
12.0k
        pdev->num_planar_planes = save_planes;
960
12.0k
        gs_closedevice(pdev);
961
        /* Then put the shiny new color_info back in. */
962
12.0k
        parent_dev->color_info = pdev->color_info = resave_info;
963
12.0k
        parent_dev->num_planar_planes = pdev->num_planar_planes = resave_planes;
964
        /* Reset the separable and linear shift, masks, bits. */
965
12.0k
        set_linear_color_bits_mask_shift(pdev);
966
12.0k
        set_linear_color_bits_mask_shift(parent_dev);
967
12.0k
    }
968
    /*
969
     * Also check for parameters which are being passed from the PDF 1.4
970
     * compositior clist write device.  This device needs to pass info
971
     * to the PDF 1.4 compositor clist reader device.  However this device
972
     * is not crated until the clist is being read.  Thus we have to buffer
973
     * this info in the output device.   (This is only needed for devices
974
     * which support spot colors.)
975
     */
976
59.7k
    code = pdf14_put_devn_params(pdev, pdevn_params, plist);
977
59.7k
    return code;
978
59.8k
}
979
980
/*
981
 * Utility routine for handling DeviceN related parameters in a
982
 * standard raster printer type device.
983
 */
984
int
985
devn_printer_put_params(gx_device *pdev, gs_param_list *plist,
986
        gs_devn_params *pdevn_params, equivalent_cmyk_color_params *pequiv_colors)
987
59.8k
{
988
59.8k
    return devn_generic_put_params(pdev, plist, pdevn_params, pequiv_colors, 1);
989
59.8k
}
990
991
/*
992
 * Free a set of separation names
993
 */
994
void
995
free_separation_names(gs_memory_t * mem,
996
                gs_separations * pseparation)
997
8.52k
{
998
8.52k
    int i;
999
1000
    /* Discard the sub levels. */
1001
8.65k
    for (i = 0; i < pseparation->num_separations; i++) {
1002
133
        gs_free_object(mem->stable_memory, pseparation->names[i].data,
1003
133
                                "free_separation_names");
1004
133
        pseparation->names[i].data = NULL;
1005
133
        pseparation->names[i].size = 0;
1006
133
    }
1007
8.52k
    pseparation->num_separations = 0;
1008
8.52k
    return;
1009
8.52k
}
1010
1011
/* ***************** The spotcmyk and devicen devices ***************** */
1012
1013
/* Define the device parameters. */
1014
#ifndef X_DPI
1015
#  define X_DPI 72
1016
#endif
1017
#ifndef Y_DPI
1018
#  define Y_DPI 72
1019
#endif
1020
1021
/* The device descriptor */
1022
static dev_proc_open_device(spotcmyk_prn_open);
1023
static dev_proc_print_page(spotcmyk_print_page);
1024
1025
/* GC procedures */
1026
1027
static
1028
151k
ENUM_PTRS_WITH(gx_devn_prn_device_enum_ptrs, gx_devn_prn_device *pdev)
1029
151k
{
1030
151k
    if (index < pdev->devn_params.separations.num_separations)
1031
0
        ENUM_RETURN(pdev->devn_params.separations.names[index].data);
1032
151k
    ENUM_PREFIX(st_device_printer,
1033
151k
                    pdev->devn_params.separations.num_separations);
1034
151k
}
1035
1036
151k
ENUM_PTRS_END
1037
2.98k
static RELOC_PTRS_WITH(gx_devn_prn_device_reloc_ptrs, gx_devn_prn_device *pdev)
1038
2.98k
{
1039
2.98k
    RELOC_PREFIX(st_device_printer);
1040
2.98k
    {
1041
2.98k
        int i;
1042
1043
2.98k
        for (i = 0; i < pdev->devn_params.separations.num_separations; ++i) {
1044
0
            RELOC_PTR(gx_devn_prn_device, devn_params.separations.names[i].data);
1045
0
        }
1046
2.98k
    }
1047
2.98k
}
1048
2.98k
RELOC_PTRS_END
1049
1050
void
1051
gx_devn_prn_device_finalize(const gs_memory_t *cmem, void *vpdev)
1052
4.73k
{
1053
4.73k
    devn_free_params((gx_device*) vpdev);
1054
4.73k
    gx_device_finalize(cmem, vpdev);
1055
4.73k
}
1056
1057
/* Even though gx_devn_prn_device_finalize is the same as gx_device_finalize, */
1058
/* we need to implement it separately because st_composite_final */
1059
/* declares all 3 procedures as private. */
1060
static void
1061
static_gx_devn_prn_device_finalize(const gs_memory_t *cmem, void *vpdev)
1062
0
{
1063
0
    gx_devn_prn_device_finalize(cmem, vpdev);
1064
0
}
1065
1066
gs_public_st_composite_final(st_gx_devn_prn_device, gx_devn_prn_device,
1067
    "gx_devn_prn_device", gx_devn_prn_device_enum_ptrs, gx_devn_prn_device_reloc_ptrs,
1068
    static_gx_devn_prn_device_finalize);
1069
1070
static void
1071
devicen_initialize_device_procs(gx_device *dev)
1072
0
{
1073
0
    set_dev_proc(dev, open_device, spotcmyk_prn_open);
1074
0
    set_dev_proc(dev, output_page, gdev_prn_output_page_seekable);
1075
0
    set_dev_proc(dev, close_device, gdev_prn_close);
1076
0
    set_dev_proc(dev, get_params, gx_devn_prn_get_params);
1077
0
    set_dev_proc(dev, put_params, gx_devn_prn_put_params);
1078
0
    set_dev_proc(dev, get_page_device, gx_page_device_get_page_device);
1079
0
    set_dev_proc(dev, get_color_mapping_procs, gx_devn_prn_get_color_mapping_procs);
1080
0
    set_dev_proc(dev, get_color_comp_index, gx_devn_prn_get_color_comp_index);
1081
0
    set_dev_proc(dev, encode_color, gx_devn_prn_encode_color);
1082
0
    set_dev_proc(dev, decode_color, gx_devn_prn_decode_color);
1083
0
    set_dev_proc(dev, update_spot_equivalent_colors, gx_devn_prn_update_spot_equivalent_colors);
1084
0
    set_dev_proc(dev, ret_devn_params, gx_devn_prn_ret_devn_params);
1085
0
}
1086
1087
fixed_colorant_name DeviceGrayComponents[] = {
1088
        "Gray",
1089
        0              /* List terminator */
1090
};
1091
1092
fixed_colorant_name DeviceRGBComponents[] = {
1093
        "Red",
1094
        "Green",
1095
        "Blue",
1096
        0              /* List terminator */
1097
};
1098
1099
fixed_colorant_name DeviceCMYKComponents[] = {
1100
        "Cyan",
1101
        "Magenta",
1102
        "Yellow",
1103
        "Black",
1104
        0               /* List terminator */
1105
};
1106
1107
#define gx_devn_prn_device_body(init, dname, ncomp, pol, depth, mg, mc, cn)\
1108
    std_device_full_body_type_extended(gx_devn_prn_device, init, dname,\
1109
          &st_gx_devn_prn_device,\
1110
          (int)((long)(DEFAULT_WIDTH_10THS) * (X_DPI) / 10),\
1111
          (int)((long)(DEFAULT_HEIGHT_10THS) * (Y_DPI) / 10),\
1112
          X_DPI, Y_DPI,\
1113
          GX_DEVICE_COLOR_MAX_COMPONENTS,       /* MaxComponents */\
1114
          ncomp,                /* NumComp */\
1115
          pol,                  /* Polarity */\
1116
          depth, 0,             /* Depth, GrayIndex */\
1117
          mg, mc,               /* MaxGray, MaxColor */\
1118
          mg + 1, mc + 1,       /* DitherGray, DitherColor */\
1119
          GX_CINFO_SEP_LIN,     /* Linear & Separable */\
1120
          cn,                   /* Process color model name */\
1121
          0, 0,                 /* offsets */\
1122
          0, 0, 0, 0            /* margins */\
1123
        ),\
1124
        prn_device_body_rest_(spotcmyk_print_page)
1125
1126
/*
1127
 * Example device with CMYK and spot color support
1128
 */
1129
const gx_devn_prn_device gs_spotcmyk_device =
1130
{
1131
    gx_devn_prn_device_body(devicen_initialize_device_procs, "spotcmyk",
1132
                            4, GX_CINFO_POLARITY_SUBTRACTIVE, 4, 1, 1,
1133
                            "DeviceCMYK"),
1134
    /* DeviceN device specific parameters */
1135
    { 1,                        /* Bits per color - must match ncomp, depth, etc. above */
1136
      DeviceCMYKComponents,     /* Names of color model colorants */
1137
      4,                        /* Number colorants for CMYK */
1138
      0,                        /* MaxSeparations has not been specified */
1139
      -1,                       /* PageSpotColors has not been specified */
1140
      {0},                      /* SeparationNames */
1141
      0,                        /* SeparationOrder names */
1142
      {0, 1, 2, 3, 4, 5, 6, 7 } /* Initial component SeparationOrder */
1143
    }
1144
};
1145
1146
/*
1147
 * Example DeviceN color device
1148
 */
1149
const gx_devn_prn_device gs_devicen_device =
1150
{
1151
    gx_devn_prn_device_body(devicen_initialize_device_procs, "devicen",
1152
                            4, GX_CINFO_POLARITY_SUBTRACTIVE, 32, 255, 255,
1153
                            "DeviceCMYK"),
1154
    /* DeviceN device specific parameters */
1155
    { 8,                        /* Bits per color - must match ncomp, depth, etc. above */
1156
      DeviceCMYKComponents,     /* Names of color model colorants */
1157
      4,                        /* Number colorants for CMYK */
1158
      0,                        /* MaxSeparations has not been specified */
1159
      -1,                       /* PageSpotColors has not been specified */
1160
      {0},                      /* SeparationNames */
1161
      0,                        /* SeparationOrder names */
1162
      {0, 1, 2, 3, 4, 5, 6, 7 } /* Initial component SeparationOrder */
1163
    }
1164
};
1165
1166
/* Open the psd devices */
1167
int
1168
spotcmyk_prn_open(gx_device * pdev)
1169
0
{
1170
0
    int code = gdev_prn_open(pdev);
1171
1172
0
    while (pdev->child)
1173
0
        pdev = pdev->child;
1174
1175
0
    set_linear_color_bits_mask_shift(pdev);
1176
0
    pdev->color_info.separable_and_linear = GX_CINFO_SEP_LIN;
1177
0
    return code;
1178
0
}
1179
1180
/* Color mapping routines for the spotcmyk device */
1181
1182
static void
1183
gray_cs_to_spotcmyk_cm(const gx_device * dev, frac gray, frac out[])
1184
0
{
1185
0
    int * map = ((gx_devn_prn_device *) dev)->devn_params.separation_order_map;
1186
1187
0
    gray_cs_to_devn_cm(dev, map, gray, out);
1188
0
}
1189
1190
static void
1191
rgb_cs_to_spotcmyk_cm(const gx_device * dev, const gs_gstate *pgs,
1192
                                   frac r, frac g, frac b, frac out[])
1193
0
{
1194
0
    int * map = ((gx_devn_prn_device *) dev)->devn_params.separation_order_map;
1195
1196
0
    rgb_cs_to_devn_cm(dev, map, pgs, r, g, b, out);
1197
0
}
1198
1199
static void
1200
cmyk_cs_to_spotcmyk_cm(const gx_device * dev, frac c, frac m, frac y, frac k, frac out[])
1201
0
{
1202
0
    int * map = ((gx_devn_prn_device *) dev)->devn_params.separation_order_map;
1203
1204
0
    cmyk_cs_to_devn_cm(dev, map, c, m, y, k, out);
1205
0
}
1206
1207
static const gx_cm_color_map_procs spotCMYK_procs = {
1208
    gray_cs_to_spotcmyk_cm, rgb_cs_to_spotcmyk_cm, cmyk_cs_to_spotcmyk_cm
1209
};
1210
1211
const gx_cm_color_map_procs *
1212
gx_devn_prn_get_color_mapping_procs(const gx_device * dev, const gx_device **map_dev)
1213
0
{
1214
0
    *map_dev = dev;
1215
0
    return &spotCMYK_procs;
1216
0
}
1217
1218
/*
1219
 * Encode a list of colorant values into a gx_color_index_value.
1220
 */
1221
gx_color_index
1222
gx_devn_prn_encode_color(gx_device *dev, const gx_color_value colors[])
1223
13
{
1224
13
    int bpc = ((gx_devn_prn_device *)dev)->devn_params.bitspercomponent;
1225
13
    gx_color_index color = 0;
1226
13
    int i = 0;
1227
13
    uchar ncomp = dev->color_info.num_components;
1228
13
    COLROUND_VARS;
1229
1230
13
    COLROUND_SETUP(bpc);
1231
65
    for (; i<ncomp; i++) {
1232
52
        color <<= bpc;
1233
52
        color |= COLROUND_ROUND(colors[i]);
1234
52
    }
1235
13
    return (color == gx_no_color_index ? color ^ 1 : color);
1236
13
}
1237
1238
/*
1239
 * Decode a gx_color_index value back to a list of colorant values.
1240
 */
1241
int
1242
gx_devn_prn_decode_color(gx_device * dev, gx_color_index color, gx_color_value * out)
1243
0
{
1244
0
    int bpc = ((gx_devn_prn_device *)dev)->devn_params.bitspercomponent;
1245
0
    int mask = (1 << bpc) - 1;
1246
0
    int i = 0;
1247
0
    uchar ncomp = dev->color_info.num_components;
1248
0
    COLDUP_VARS;
1249
1250
0
    COLDUP_SETUP(bpc);
1251
0
    for (; i<ncomp; i++) {
1252
0
        out[ncomp - i - 1] = COLDUP_DUP(color & mask);
1253
0
        color >>= bpc;
1254
0
    }
1255
0
    return 0;
1256
0
}
1257
1258
/* Get parameters. */
1259
int
1260
gx_devn_prn_get_params(gx_device *dev, gs_param_list *plist)
1261
40.4k
{
1262
40.4k
    gx_devn_prn_device *pdev = (gx_devn_prn_device *)dev;
1263
40.4k
    int code = gdev_prn_get_params(dev, plist);
1264
1265
40.4k
    if (code < 0)
1266
0
        return code;
1267
40.4k
    return devn_get_params(dev, plist, &pdev->devn_params,
1268
40.4k
                           &pdev->equiv_cmyk_colors);
1269
40.4k
}
1270
1271
/* Set parameters. */
1272
int
1273
gx_devn_prn_put_params(gx_device *dev, gs_param_list *plist)
1274
11.2k
{
1275
11.2k
    gx_devn_prn_device *pdev = (gx_devn_prn_device *)dev;
1276
1277
11.2k
    return devn_printer_put_params(dev, plist, &pdev->devn_params,
1278
11.2k
                                   &pdev->equiv_cmyk_colors);
1279
11.2k
}
1280
1281
/*
1282
 *  Device proc for returning a pointer to DeviceN parameter structure
1283
 */
1284
gs_devn_params *
1285
gx_devn_prn_ret_devn_params(gx_device * dev)
1286
41.7k
{
1287
41.7k
    gx_devn_prn_device *pdev = (gx_devn_prn_device *)dev;
1288
1289
41.7k
    return &pdev->devn_params;
1290
41.7k
}
1291
1292
const gs_devn_params *
1293
gx_devn_prn_ret_devn_params_const(const gx_device * dev)
1294
1.26M
{
1295
1.26M
    const gx_devn_prn_device *pdev = (const gx_devn_prn_device *)dev;
1296
1297
1.26M
    return &pdev->devn_params;
1298
1.26M
}
1299
1300
/*
1301
 *  Device proc for updating the equivalent CMYK color for spot colors.
1302
 */
1303
int
1304
gx_devn_prn_update_spot_equivalent_colors(gx_device *dev, const gs_gstate * pgs, const gs_color_space *pcs)
1305
1.98k
{
1306
1.98k
    gx_devn_prn_device *pdev = (gx_devn_prn_device *)dev;
1307
1308
1.98k
    return update_spot_equivalent_cmyk_colors(dev, pgs, pcs, &pdev->devn_params,
1309
1.98k
                                              &pdev->equiv_cmyk_colors);
1310
1.98k
}
1311
1312
/*
1313
 * This routine will check to see if the color component name  match those
1314
 * that are available amoung the current device's color components.
1315
 *
1316
 * Parameters:
1317
 *   dev - pointer to device data structure.
1318
 *   pname - pointer to name (zero termination not required)
1319
 *   nlength - length of the name
1320
 *
1321
 * This routine returns a positive value (0 to n) which is the device colorant
1322
 * number if the name is found.  It returns GX_DEVICE_COLOR_MAX_COMPONENTS if
1323
 * the colorant is not being used due to a SeparationOrder device parameter.
1324
 * It returns a negative value if not found.
1325
 */
1326
int
1327
gx_devn_prn_get_color_comp_index(gx_device * dev, const char * pname,
1328
                                        int name_size, int component_type)
1329
121k
{
1330
121k
    gx_devn_prn_device *pdev = (gx_devn_prn_device *)dev;
1331
1332
121k
    return devn_get_color_comp_index(dev,
1333
121k
                                     &pdev->devn_params,
1334
121k
                                     &pdev->equiv_cmyk_colors,
1335
121k
                                     pname,
1336
121k
                                     name_size,
1337
121k
                                     component_type,
1338
121k
                                     ENABLE_AUTO_SPOT_COLORS);
1339
121k
}
1340
1341
/*
1342
 * This routine will extract a specified set of bits from a buffer and pack
1343
 * them into a given buffer.
1344
 *
1345
 * Parameters:
1346
 *   source - The source of the data
1347
 *   dest - The destination for the data
1348
 *   depth - The size of the bits per pixel - must be a multiple of 8
1349
 *   first_bit - The location of the first data bit (LSB).
1350
 *   bit_width - The number of bits to be extracted.
1351
 *   npixel - The number of pixels.
1352
 *
1353
 * Returns:
1354
 *   Length of the output line (in bytes)
1355
 *   Data in dest.
1356
 */
1357
int
1358
repack_data(byte * source, byte * dest, int depth, int first_bit,
1359
                int bit_width, int npixel)
1360
0
{
1361
0
    int in_nbyte = depth >> 3;          /* Number of bytes per input pixel */
1362
0
    int out_nbyte = bit_width >> 3;     /* Number of bytes per output pixel */
1363
0
    gx_color_index mask = 1;
1364
0
    gx_color_index data;
1365
0
    int i, j, length = 0;
1366
0
    byte temp;
1367
0
    byte * out = dest;
1368
0
    int in_bit_start = 8 - depth;
1369
0
    int out_bit_start = 8 - bit_width;
1370
0
    int in_byte_loc = in_bit_start, out_byte_loc = out_bit_start;
1371
1372
0
    mask = (mask << bit_width) - 1;
1373
0
    for (i=0; i<npixel; i++) {
1374
        /* Get the pixel data */
1375
0
        if (!in_nbyte) {                /* Multiple pixels per byte */
1376
0
            data = *source;
1377
0
            data >>= in_byte_loc;
1378
0
            in_byte_loc -= depth;
1379
0
            if (in_byte_loc < 0) {      /* If finished with byte */
1380
0
                in_byte_loc = in_bit_start;
1381
0
                source++;
1382
0
            }
1383
0
        }
1384
0
        else {                          /* One or more bytes per pixel */
1385
0
            data = *source++;
1386
0
            for (j=1; j<in_nbyte; j++)
1387
0
                data = (data << 8) + *source++;
1388
0
        }
1389
0
        data >>= first_bit;
1390
0
        data &= mask;
1391
1392
        /* Put the output data */
1393
0
        if (!out_nbyte) {               /* Multiple pixels per byte */
1394
0
            temp = (byte)(*out & ~(mask << out_byte_loc));
1395
0
            *out = (byte)(temp | (data << out_byte_loc));
1396
0
            out_byte_loc -= bit_width;
1397
0
            if (out_byte_loc < 0) {     /* If finished with byte */
1398
0
                out_byte_loc = out_bit_start;
1399
0
                out++;
1400
0
            }
1401
0
        }
1402
0
        else {                          /* One or more bytes per pixel */
1403
0
            *out++ = (byte)(data >> ((out_nbyte - 1) * 8));
1404
0
            for (j=1; j<out_nbyte; j++) {
1405
0
                *out++ = (byte)(data >> ((out_nbyte - 1 - j) * 8));
1406
0
            }
1407
0
        }
1408
0
    }
1409
    /* Return the number of bytes in the destination buffer. */
1410
0
    if (out_byte_loc != out_bit_start) {        /* If partially filled last byte */
1411
0
        *out = *out & ((~0) << out_byte_loc);   /* Mask unused part of last byte */
1412
0
        out++;
1413
0
    }
1414
0
    length = out - dest;
1415
0
    return length;
1416
0
}
1417
1418
static int devn_write_pcx_file(gx_device_printer * pdev, char * filename, int ncomp,
1419
                            int bpc, int pcmlinelength);
1420
/*
1421
 * This is an example print page routine for a DeviceN device.  This routine
1422
 * will handle a DeviceN, a CMYK with spot colors, or an RGB process color model.
1423
 *
1424
 * This routine creates several output files.  If the process color model is
1425
 * RGB or CMYK then a bit image file is created which contains the data for the
1426
 * process color model data.  This data is put into the given file stream.
1427
 * I.e. into the output file specified by the user.  This file is not created
1428
 * for the DeviceN process color model.  A separate bit image file is created
1429
 * is created for the data for each of the given spot colors.  The names for
1430
 * these files are created by taking the given output file name and appending
1431
 * "sn" (where n is the spot color number 0 to ...) to the output file name.
1432
 * The results are unknown if the output file is stdout etc.
1433
 *
1434
 * After the bit image files are created, then a set of PCX format files are
1435
 * created from the bit image files.  This files have a ".pcx" appended to the
1436
 * end of the files.  Thus a CMYK process color model with two spot colors
1437
 * would end up with a total of six files being created.  (xxx, xxxs0, xxxs1,
1438
 * xxx.pcx, xxxs0.pcx, and xxxs1.pcx).
1439
 *
1440
 * I do not assume that any users will actually want to create all of these
1441
 * different files.  However I wanted to show an example of how each of the
1442
 * spot * colorants could be unpacked from the process color model colorants.
1443
 * The bit images files are an easy way to show this without the complication
1444
 * of trying to put the data into a specific format.  However I do not have a
1445
 * tool which will display the bit image data directly so I needed to convert
1446
 * it to a form which I can view.  Thus the PCX format files are being created.
1447
 * Note:  The PCX implementation is not complete.  There are many (most)
1448
 * combinations of bits per pixel and number of colorants that are not supported.
1449
 */
1450
static int
1451
spotcmyk_print_page(gx_device_printer * pdev, gp_file * prn_stream)
1452
0
{
1453
0
    int line_size = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
1454
0
    byte *in = gs_alloc_bytes(pdev->memory, line_size, "spotcmyk_print_page(in)");
1455
0
    byte *buf = gs_alloc_bytes(pdev->memory, line_size + 3, "spotcmyk_print_page(buf)");
1456
0
    const gx_devn_prn_device * pdevn = (gx_devn_prn_device *) pdev;
1457
0
    uint npcmcolors = pdevn->devn_params.num_std_colorant_names;
1458
0
    uchar ncomp = pdevn->color_info.num_components;
1459
0
    int depth = pdevn->color_info.depth;
1460
0
    int nspot = pdevn->devn_params.separations.num_separations;
1461
0
    int bpc = pdevn->devn_params.bitspercomponent;
1462
0
    int lnum = 0, bottom = pdev->height;
1463
0
    int width = pdev->width;
1464
0
    gp_file * spot_file[GX_DEVICE_COLOR_MAX_COMPONENTS] = {0};
1465
0
    uint i;
1466
0
    int code = 0;
1467
0
    int first_bit;
1468
0
    int pcmlinelength = 0; /* Initialize against indeterminizm in case of pdev->height == 0. */
1469
0
    int linelength[GX_DEVICE_COLOR_MAX_COMPONENTS];
1470
0
    byte *data;
1471
0
    char *spotname = (char *)gs_alloc_bytes(pdev->memory, gp_file_name_sizeof, "spotcmyk_print_page(spotname)");
1472
1473
0
    if (in == NULL || buf == NULL || spotname == NULL) {
1474
0
        code = gs_note_error(gs_error_VMerror);
1475
0
        goto prn_done;
1476
0
    }
1477
    /*
1478
     * Check if the SeparationOrder list has changed the order of the process
1479
     * color model colorants. If so then we will treat all colorants as if they
1480
     * are spot colors.
1481
     */
1482
0
    for (i = 0; i < npcmcolors; i++)
1483
0
        if (pdevn->devn_params.separation_order_map[i] != i)
1484
0
            break;
1485
0
    if (i < npcmcolors || ncomp < npcmcolors) {
1486
0
        nspot = ncomp;
1487
0
        npcmcolors = 0;
1488
0
    }
1489
1490
    /* Open the output files for the spot colors */
1491
0
    for(i = 0; i < nspot; i++) {
1492
0
        gs_snprintf(spotname, gp_file_name_sizeof, "%ss%d", pdevn->fname, i);
1493
0
        code = gs_add_control_path(pdev->memory, gs_permit_file_writing, spotname);
1494
0
        if (code < 0)
1495
0
            goto prn_done;
1496
0
        spot_file[i] = gp_fopen(pdev->memory, spotname, "wb");
1497
0
        (void)gs_remove_control_path(pdev->memory, gs_permit_file_writing, spotname);
1498
0
        if (spot_file[i] == NULL) {
1499
0
            code = gs_note_error(gs_error_VMerror);
1500
0
            goto prn_done;
1501
0
        }
1502
0
    }
1503
1504
    /* Now create the output bit image files */
1505
0
    for (; lnum < bottom; ++lnum) {
1506
0
        code = gdev_prn_get_bits(pdev, lnum, in, &data);
1507
0
        if (code < 0)
1508
0
            goto prn_done;
1509
        /* Now put the pcm data into the output file */
1510
0
        if (npcmcolors) {
1511
0
            first_bit = bpc * (ncomp - npcmcolors);
1512
0
            pcmlinelength = repack_data(data, buf, depth, first_bit, bpc * npcmcolors, width);
1513
0
            gp_fwrite(buf, 1, pcmlinelength, prn_stream);
1514
0
        }
1515
        /* Put spot color data into the output files */
1516
0
        for (i = 0; i < nspot; i++) {
1517
0
            first_bit = bpc * (nspot - 1 - i);
1518
0
            linelength[i] = repack_data(data, buf, depth, first_bit, bpc, width);
1519
0
            gp_fwrite(buf, 1, linelength[i], spot_file[i]);
1520
0
        }
1521
0
    }
1522
1523
    /* Close the bit image files */
1524
0
    for(i = 0; i < nspot; i++) {
1525
0
        gp_fclose(spot_file[i]);
1526
0
        spot_file[i] = NULL;
1527
0
    }
1528
1529
    /* Now convert the bit image files into PCX files */
1530
0
    if (npcmcolors) {
1531
0
        code = devn_write_pcx_file(pdev, (char *) &pdevn->fname,
1532
0
                                npcmcolors, bpc, pcmlinelength);
1533
0
        if (code < 0)
1534
0
            goto prn_done;
1535
0
    }
1536
0
    for(i = 0; i < nspot; i++) {
1537
0
        gs_snprintf(spotname, gp_file_name_sizeof, "%ss%d", pdevn->fname, i);
1538
0
        code = devn_write_pcx_file(pdev, spotname, 1, bpc, linelength[i]);
1539
0
        if (code < 0)
1540
0
            goto prn_done;
1541
0
    }
1542
1543
    /* Clean up and exit */
1544
0
  prn_done:
1545
0
    for(i = 0; i < nspot; i++) {
1546
0
        if (spot_file[i] != NULL)
1547
0
            gp_fclose(spot_file[i]);
1548
0
    }
1549
0
    if (in != NULL)
1550
0
        gs_free_object(pdev->memory, in, "spotcmyk_print_page(in)");
1551
0
    if (buf != NULL)
1552
0
        gs_free_object(pdev->memory, buf, "spotcmyk_print_page(buf)");
1553
0
    if (spotname != NULL)
1554
0
        gs_free_object(pdev->memory, spotname, "spotcmyk_print_page(spotname)");
1555
0
    return code;
1556
0
}
1557
1558
/*
1559
 * We are using the PCX output format.  This is done for simplicity.
1560
 * Much of the following code was copied from gdevpcx.c.
1561
 */
1562
1563
/* ------ Private definitions ------ */
1564
1565
/* All two-byte quantities are stored LSB-first! */
1566
#if ARCH_IS_BIG_ENDIAN
1567
#  define assign_ushort(a,v) a = ((v) >> 8) + ((v) << 8)
1568
#else
1569
0
#  define assign_ushort(a,v) a = (v)
1570
#endif
1571
1572
typedef struct pcx_header_s {
1573
    byte manuf;                 /* always 0x0a */
1574
    byte version;
1575
#define version_2_5                     0
1576
0
#define version_2_8_with_palette        2
1577
#define version_2_8_without_palette     3
1578
0
#define version_3_0 /* with palette */  5
1579
    byte encoding;              /* 1=RLE */
1580
    byte bpp;                   /* bits per pixel per plane */
1581
    ushort x1;                  /* X of upper left corner */
1582
    ushort y1;                  /* Y of upper left corner */
1583
    ushort x2;                  /* x1 + width - 1 */
1584
    ushort y2;                  /* y1 + height - 1 */
1585
    ushort hres;                /* horz. resolution (dots per inch) */
1586
    ushort vres;                /* vert. resolution (dots per inch) */
1587
    byte palette[16 * 3];       /* color palette */
1588
    byte reserved;
1589
    byte nplanes;               /* number of color planes */
1590
    ushort bpl;                 /* number of bytes per line (uncompressed) */
1591
    ushort palinfo;
1592
#define palinfo_color   1
1593
#define palinfo_gray    2
1594
    byte xtra[58];              /* fill out header to 128 bytes */
1595
} pcx_header;
1596
1597
/* Define the prototype header. */
1598
static const pcx_header pcx_header_prototype =
1599
{
1600
    10,                         /* manuf */
1601
    0,                          /* version (variable) */
1602
    1,                          /* encoding */
1603
    0,                          /* bpp (variable) */
1604
    00, 00,                     /* x1, y1 */
1605
    00, 00,                     /* x2, y2 (variable) */
1606
    00, 00,                     /* hres, vres (variable) */
1607
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,        /* palette (variable) */
1608
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1609
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1610
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1611
    0,                          /* reserved */
1612
    0,                          /* nplanes (variable) */
1613
    00,                         /* bpl (variable) */
1614
    00,                         /* palinfo (variable) */
1615
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      /* xtra */
1616
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1617
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1618
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
1619
};
1620
1621
/* Forward declarations */
1622
static void devn_pcx_write_rle(const byte *, const byte *, int, gp_file *);
1623
static int devn_pcx_write_page(gx_device_printer * pdev, gp_file * infile,
1624
    int linesize, gp_file * outfile, pcx_header * phdr, bool planar, int depth);
1625
1626
static const byte pcx_cmyk_palette[16 * 3] =
1627
{
1628
    0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x0f, 0x0f, 0x00,
1629
    0xff, 0x00, 0xff, 0x0f, 0x00, 0x0f, 0xff, 0x00, 0x00, 0x0f, 0x00, 0x00,
1630
    0x00, 0xff, 0xff, 0x00, 0x0f, 0x0f, 0x00, 0xff, 0x00, 0x00, 0x0f, 0x00,
1631
    0x00, 0x00, 0xff, 0x00, 0x00, 0x0f, 0x1f, 0x1f, 0x1f, 0x0f, 0x0f, 0x0f,
1632
};
1633
1634
static const byte pcx_ega_palette[16 * 3] =
1635
{
1636
    0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0x00, 0xaa, 0xaa,
1637
    0xaa, 0x00, 0x00, 0xaa, 0x00, 0xaa, 0xaa, 0xaa, 0x00, 0xaa, 0xaa, 0xaa,
1638
    0x55, 0x55, 0x55, 0x55, 0x55, 0xff, 0x55, 0xff, 0x55, 0x55, 0xff, 0xff,
1639
    0xff, 0x55, 0x55, 0xff, 0x55, 0xff, 0xff, 0xff, 0x55, 0xff, 0xff, 0xff
1640
};
1641
1642
/*
1643
 * This routine will set up the revision and palatte for the output
1644
 * file.
1645
 *
1646
 * Please note that this routine does not currently handle all possible
1647
 * combinations of bits and planes.
1648
 *
1649
 * Input parameters:
1650
 *   pdev - Pointer to device data structure
1651
 *   file - output file
1652
 *   header - The header structure to hold the data.
1653
 *   bits_per_plane - The number of bits per plane.
1654
 *   num_planes - The number of planes.
1655
 */
1656
static bool
1657
devn_setup_pcx_header(gx_device_printer * pdev, pcx_header * phdr, int num_planes, int bits_per_plane)
1658
0
{
1659
0
    bool planar = true; /* Invalid cases could cause an indeterminizm. */
1660
1661
0
    *phdr = pcx_header_prototype;
1662
0
    phdr->bpp = bits_per_plane;
1663
0
    phdr->nplanes = num_planes;
1664
1665
0
    switch (num_planes) {
1666
0
        case 1:
1667
0
            switch (bits_per_plane) {
1668
0
                case 1:
1669
0
                        phdr->version = version_2_8_with_palette;
1670
0
                        assign_ushort(phdr->palinfo, palinfo_gray);
1671
0
                        memcpy((byte *) phdr->palette, "\000\000\000\377\377\377", 6);
1672
0
                        planar = false;
1673
0
                        break;
1674
0
                case 2:                         /* Not defined */
1675
0
                        break;
1676
0
                case 4:
1677
0
                        phdr->version = version_2_8_with_palette;
1678
0
                        memcpy((byte *) phdr->palette, pcx_ega_palette, sizeof(pcx_ega_palette));
1679
0
                        planar = true;
1680
0
                        break;
1681
0
                case 5:                         /* Not defined */
1682
0
                        break;
1683
0
                case 8:
1684
0
                        phdr->version = version_3_0;
1685
0
                        assign_ushort(phdr->palinfo, palinfo_gray);
1686
0
                        planar = false;
1687
0
                        break;
1688
0
                case 16:                        /* Not defined */
1689
0
                        break;
1690
0
            }
1691
0
            break;
1692
0
        case 2:
1693
0
            switch (bits_per_plane) {
1694
0
                case 1:                         /* Not defined */
1695
0
                        break;
1696
0
                case 2:                         /* Not defined */
1697
0
                        break;
1698
0
                case 4:                         /* Not defined */
1699
0
                        break;
1700
0
                case 5:                         /* Not defined */
1701
0
                        break;
1702
0
                case 8:                         /* Not defined */
1703
0
                        break;
1704
0
                case 16:                        /* Not defined */
1705
0
                        break;
1706
0
            }
1707
0
            break;
1708
0
        case 3:
1709
0
            switch (bits_per_plane) {
1710
0
                case 1:                         /* Not defined */
1711
0
                        break;
1712
0
                case 2:                         /* Not defined */
1713
0
                        break;
1714
0
                case 4:                         /* Not defined */
1715
0
                        break;
1716
0
                case 5:                         /* Not defined */
1717
0
                        break;
1718
0
                case 8:
1719
0
                        phdr->version = version_3_0;
1720
0
                        assign_ushort(phdr->palinfo, palinfo_color);
1721
0
                        planar = true;
1722
0
                        break;
1723
0
                case 16:                        /* Not defined */
1724
0
                        break;
1725
0
            }
1726
0
            break;
1727
0
        case 4:
1728
0
            switch (bits_per_plane) {
1729
0
                case 1:
1730
0
                        phdr->version = 2;
1731
0
                        memcpy((byte *) phdr->palette, pcx_cmyk_palette,
1732
0
                                sizeof(pcx_cmyk_palette));
1733
0
                        planar = false;
1734
0
                        phdr->bpp = 4;
1735
0
                        phdr->nplanes = 1;
1736
0
                        break;
1737
0
                case 2:                         /* Not defined */
1738
0
                        break;
1739
0
                case 4:                         /* Not defined */
1740
0
                        break;
1741
0
                case 5:                         /* Not defined */
1742
0
                        break;
1743
0
                case 8:                         /* Not defined */
1744
0
                        break;
1745
0
                case 16:                        /* Not defined */
1746
0
                        break;
1747
0
            }
1748
0
            break;
1749
0
    }
1750
0
    return planar;
1751
0
}
1752
1753
/* Write a palette on a file. */
1754
static int
1755
pc_write_mono_palette(gx_device * dev, uint max_index, gp_file * file)
1756
0
{
1757
0
    uint i, c;
1758
0
    gx_color_value rgb[3];
1759
1760
0
    for (i = 0; i < max_index; i++) {
1761
0
        rgb[0] = rgb[1] = rgb[2] = i << 8;
1762
0
        for (c = 0; c < 3; c++) {
1763
0
            byte b = gx_color_value_to_byte(rgb[c]);
1764
1765
0
            gp_fputc(b, file);
1766
0
        }
1767
0
    }
1768
0
    return 0;
1769
0
}
1770
/*
1771
 * This routine will send any output data required at the end of a file
1772
 * for a particular combination of planes and bits per plane.
1773
 *
1774
 * Please note that most combinations do not require anything at the end
1775
 * of a data file.
1776
 *
1777
 * Input parameters:
1778
 *   pdev - Pointer to device data structure
1779
 *   file - output file
1780
 *   header - The header structure to hold the data.
1781
 *   bits_per_plane - The number of bits per plane.
1782
 *   num_planes - The number of planes.
1783
 */
1784
static int
1785
devn_finish_pcx_file(gx_device_printer * pdev, gp_file * file, pcx_header * header, int num_planes, int bits_per_plane)
1786
0
{
1787
0
    switch (num_planes) {
1788
0
        case 1:
1789
0
            switch (bits_per_plane) {
1790
0
                case 1:                         /* Do nothing */
1791
0
                        break;
1792
0
                case 2:                         /* Not defined */
1793
0
                        break;
1794
0
                case 4:                         /* Do nothing */
1795
0
                        break;
1796
0
                case 5:                         /* Not defined */
1797
0
                        break;
1798
0
                case 8:
1799
0
                        gp_fputc(0x0c, file);
1800
0
                        return pc_write_mono_palette((gx_device *) pdev, 256, file);
1801
0
                case 16:                        /* Not defined */
1802
0
                        break;
1803
0
            }
1804
0
            break;
1805
0
        case 2:
1806
0
            switch (bits_per_plane) {
1807
0
                case 1:                         /* Not defined */
1808
0
                        break;
1809
0
                case 2:                         /* Not defined */
1810
0
                        break;
1811
0
                case 4:                         /* Not defined */
1812
0
                        break;
1813
0
                case 5:                         /* Not defined */
1814
0
                        break;
1815
0
                case 8:                         /* Not defined */
1816
0
                        break;
1817
0
                case 16:                        /* Not defined */
1818
0
                        break;
1819
0
            }
1820
0
            break;
1821
0
        case 3:
1822
0
            switch (bits_per_plane) {
1823
0
                case 1:                         /* Not defined */
1824
0
                        break;
1825
0
                case 2:                         /* Not defined */
1826
0
                        break;
1827
0
                case 4:                         /* Not defined */
1828
0
                        break;
1829
0
                case 5:                         /* Not defined */
1830
0
                        break;
1831
0
                case 8:                         /* Do nothing */
1832
0
                        break;
1833
0
                case 16:                        /* Not defined */
1834
0
                        break;
1835
0
            }
1836
0
            break;
1837
0
        case 4:
1838
0
            switch (bits_per_plane) {
1839
0
                case 1:                         /* Do nothing */
1840
0
                        break;
1841
0
                case 2:                         /* Not defined */
1842
0
                        break;
1843
0
                case 4:                         /* Not defined */
1844
0
                        break;
1845
0
                case 5:                         /* Not defined */
1846
0
                        break;
1847
0
                case 8:                         /* Not defined */
1848
0
                        break;
1849
0
                case 16:                        /* Not defined */
1850
0
                        break;
1851
0
            }
1852
0
            break;
1853
0
    }
1854
0
    return 0;
1855
0
}
1856
1857
/* Send the page to the printer. */
1858
static int
1859
devn_write_pcx_file(gx_device_printer * pdev, char * filename, int ncomp,
1860
                            int bpc, int linesize)
1861
0
{
1862
0
    pcx_header header;
1863
0
    int code;
1864
0
    bool planar;
1865
0
    char *outname = (char *)gs_alloc_bytes(pdev->memory, gp_file_name_sizeof, "devn_write_pcx_file(outname)");
1866
0
    gp_file * in = NULL;
1867
0
    gp_file * out = NULL;
1868
0
    int depth = bpc_to_depth(ncomp, bpc);
1869
1870
0
    if (outname == NULL) {
1871
0
        code = gs_note_error(gs_error_VMerror);
1872
0
        goto done;
1873
0
    }
1874
1875
0
    code = gs_add_control_path(pdev->memory, gs_permit_file_reading, filename);
1876
0
    if (code < 0)
1877
0
        goto done;
1878
1879
0
    in = gp_fopen(pdev->memory, filename, "rb");
1880
0
    if (!in) {
1881
0
        code = gs_note_error(gs_error_invalidfileaccess);
1882
0
        goto done;
1883
0
    }
1884
0
    gs_snprintf(outname, gp_file_name_sizeof, "%s.pcx", filename);
1885
0
    code = gs_add_control_path(pdev->memory, gs_permit_file_writing, outname);
1886
0
    if (code < 0)
1887
0
        goto done;
1888
0
    out = gp_fopen(pdev->memory, outname, "wb");
1889
0
    if (!out) {
1890
0
        code = gs_note_error(gs_error_invalidfileaccess);
1891
0
        goto done;
1892
0
    }
1893
1894
0
    if (ncomp == 4 && bpc == 8) {
1895
0
        ncomp = 3;    /* we will convert 32-bit to 24-bit RGB */
1896
0
    }
1897
0
    planar = devn_setup_pcx_header(pdev, &header, ncomp, bpc);
1898
0
    code = devn_pcx_write_page(pdev, in, linesize, out, &header, planar, depth);
1899
0
    if (code >= 0)
1900
0
        code = devn_finish_pcx_file(pdev, out, &header, ncomp, bpc);
1901
1902
0
done:
1903
0
    (void)gs_remove_control_path(pdev->memory, gs_permit_file_reading, filename);
1904
0
    (void)gs_remove_control_path(pdev->memory, gs_permit_file_writing, outname);
1905
0
    if (in)
1906
0
      gp_fclose(in);
1907
0
    if (out)
1908
0
      gp_fclose(out);
1909
1910
0
    if (outname)
1911
0
      gs_free_object(pdev->memory, outname, "spotcmyk_print_page(outname)");
1912
1913
0
    return code;
1914
0
}
1915
1916
/* Write out a page in PCX format. */
1917
/* This routine is used for all formats. */
1918
/* The caller has set header->bpp, nplanes, and palette. */
1919
static int
1920
devn_pcx_write_page(gx_device_printer * pdev, gp_file * infile, int linesize, gp_file * outfile,
1921
               pcx_header * phdr, bool planar, int depth)
1922
0
{
1923
0
    int raster = linesize;
1924
0
    uint rsize = ROUND_UP((pdev->width * phdr->bpp + 7) >> 3, 2);       /* PCX format requires even */
1925
0
    int height = pdev->height;
1926
0
    uint lsize = raster + rsize;
1927
0
    byte *line = gs_alloc_bytes(pdev->memory, lsize, "pcx file buffer");
1928
0
    byte *rgb_buff = NULL;
1929
0
    byte *plane = line + raster;
1930
0
    bool convert_to_rgb = false;
1931
0
    int y;
1932
0
    int code = 0;               /* return code */
1933
1934
0
    if (line == 0)              /* can't allocate line buffer */
1935
0
        return_error(gs_error_VMerror);
1936
0
    if (pdev->color_info.num_components == 4 && depth == 32) {
1937
0
        rgb_buff = gs_alloc_bytes(pdev->memory, lsize, "pcx_rgb_buff");
1938
0
        if (rgb_buff == 0)              /* can't allocate line buffer */
1939
0
            return_error(gs_error_VMerror);
1940
0
        raster = (raster * 3) / 4;  /* will be rounded up to even later */
1941
0
        depth = 24;     /* we will be writing 24-bit rgb */
1942
0
        convert_to_rgb = true;
1943
0
    }
1944
1945
    /* Fill in the other variable entries in the header struct. */
1946
1947
0
    assign_ushort(phdr->x2, pdev->width - 1);
1948
0
    assign_ushort(phdr->y2, height - 1);
1949
0
    assign_ushort(phdr->hres, (int)pdev->x_pixels_per_inch);
1950
0
    assign_ushort(phdr->vres, (int)pdev->y_pixels_per_inch);
1951
0
    assign_ushort(phdr->bpl, (planar || depth == 1 ? rsize :
1952
0
                              raster + (raster & 1)));
1953
1954
    /* Write the header. */
1955
1956
0
    if (gp_fwrite((const char *)phdr, 1, 128, outfile) < 128) {
1957
0
        code = gs_error_ioerror;
1958
0
        goto pcx_done;
1959
0
    }
1960
    /* Write the contents of the image. */
1961
0
    for (y = 0; y < height; y++) {
1962
0
        byte *row = line;
1963
0
        byte *end;
1964
1965
0
        code = gp_fread(line, sizeof(byte), linesize, infile);
1966
0
        if (code < 0)
1967
0
            break;
1968
        /* If needed, convert to rgb */
1969
0
        if (convert_to_rgb) {
1970
0
            int i;
1971
0
            byte *row_in = line;
1972
1973
            /* Transform the data. */
1974
0
            row = rgb_buff; /* adjust to converted output buffer */
1975
0
            for (i=0; i < linesize; i += 4) {
1976
0
                *row++ = ((255 - row_in[0]) * (255 - row_in[3])) / 255;
1977
0
                *row++ = ((255 - row_in[1]) * (255 - row_in[3])) / 255;
1978
0
                *row++ = ((255 - row_in[2]) * (255 - row_in[3])) / 255;
1979
0
                row_in += 4;
1980
0
            }
1981
0
            row = rgb_buff; /* adjust to converted output buffer */
1982
0
        }
1983
0
        end = row + raster;
1984
0
        if (!planar) {          /* Just write the bits. */
1985
0
            if (raster & 1) {   /* Round to even, with predictable padding. */
1986
0
                *end = end[-1];
1987
0
                ++end;
1988
0
            }
1989
0
            devn_pcx_write_rle(row, end, 1, outfile);
1990
0
        } else
1991
0
            switch (depth) {
1992
1993
0
                case 4:
1994
0
                    {
1995
0
                        byte *pend = plane + rsize;
1996
0
                        int shift;
1997
1998
0
                        for (shift = 0; shift < 4; shift++) {
1999
0
                            register byte *from, *to;
2000
0
                            register int bright = 1 << shift;
2001
0
                            register int bleft = bright << 4;
2002
2003
0
                            for (from = row, to = plane;
2004
0
                                 from < end; from += 4
2005
0
                                ) {
2006
0
                                *to++ =
2007
0
                                    (from[0] & bleft ? 0x80 : 0) |
2008
0
                                    (from[0] & bright ? 0x40 : 0) |
2009
0
                                    (from[1] & bleft ? 0x20 : 0) |
2010
0
                                    (from[1] & bright ? 0x10 : 0) |
2011
0
                                    (from[2] & bleft ? 0x08 : 0) |
2012
0
                                    (from[2] & bright ? 0x04 : 0) |
2013
0
                                    (from[3] & bleft ? 0x02 : 0) |
2014
0
                                    (from[3] & bright ? 0x01 : 0);
2015
0
                            }
2016
                            /* We might be one byte short of rsize. */
2017
0
                            if (to < pend)
2018
0
                                *to = to[-1];
2019
0
                            devn_pcx_write_rle(plane, pend, 1, outfile);
2020
0
                        }
2021
0
                    }
2022
0
                    break;
2023
2024
0
                case 24:
2025
0
                    {
2026
0
                        int pnum;
2027
2028
0
                        for (pnum = 0; pnum < 3; ++pnum) {
2029
0
                            devn_pcx_write_rle(row + pnum, row + raster, 3, outfile);
2030
0
                            if (pdev->width & 1)
2031
0
                                gp_fputc(0, outfile);              /* pad to even */
2032
0
                        }
2033
0
                    }
2034
0
                    break;
2035
2036
0
                default:
2037
0
                    code = gs_note_error(gs_error_rangecheck);
2038
0
                    goto pcx_done;
2039
2040
0
            }
2041
0
        code = 0;
2042
0
    }
2043
2044
0
  pcx_done:
2045
0
    if (rgb_buff != NULL)
2046
0
        gs_free_object(pdev->memory, rgb_buff, "pcx_rgb_buff");
2047
0
    gs_free_object(pdev->memory, line, "pcx file buffer");
2048
2049
0
    return code;
2050
0
}
2051
2052
/* ------ Internal routines ------ */
2053
2054
/* Write one line in PCX run-length-encoded format. */
2055
static void
2056
devn_pcx_write_rle(const byte * from, const byte * end, int step, gp_file * file)
2057
0
{  /*
2058
    * The PCX format theoretically allows encoding runs of 63
2059
    * identical bytes, but some readers can't handle repetition
2060
    * counts greater than 15.
2061
    */
2062
0
#define MAX_RUN_COUNT 15
2063
0
    int max_run = step * MAX_RUN_COUNT;
2064
2065
0
    while (from < end) {
2066
0
        byte data = *from;
2067
2068
0
        from += step;
2069
0
        if (from >= end || data != *from) {
2070
0
            if (data >= 0xc0)
2071
0
                gp_fputc(0xc1, file);
2072
0
        } else {
2073
0
            const byte *start = from;
2074
2075
0
            while ((from < end) && (*from == data))
2076
0
                from += step;
2077
            /* Now (from - start) / step + 1 is the run length. */
2078
0
            while (from - start >= max_run) {
2079
0
                gp_fputc(0xc0 + MAX_RUN_COUNT, file);
2080
0
                gp_fputc(data, file);
2081
0
                start += max_run;
2082
0
            }
2083
0
            if (from > start || data >= 0xc0)
2084
0
                gp_fputc((from - start) / step + 0xc1, file);
2085
0
        }
2086
0
        gp_fputc(data, file);
2087
0
    }
2088
0
#undef MAX_RUN_COUNT
2089
0
}