Coverage Report

Created: 2026-04-09 07:06

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