Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gdevdflt.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
/* Default device implementation */
17
#include "math_.h"
18
#include "memory_.h"
19
#include "gx.h"
20
#include "gsstruct.h"
21
#include "gxobj.h"
22
#include "gserrors.h"
23
#include "gsropt.h"
24
#include "gxcomp.h"
25
#include "gxdevice.h"
26
#include "gxdevsop.h"
27
#include "gdevp14.h"        /* Needed to patch up the procs after compositor creation */
28
#include "gstrans.h"        /* For gs_pdf14trans_t */
29
#include "gxgstate.h"       /* for gs_image_state_s */
30
31
32
/* defined in gsdpram.c */
33
int gx_default_get_param(gx_device *dev, char *Param, void *list);
34
35
/* ---------------- Default device procedures ---------------- */
36
37
/*
38
 * Set a color model polarity to be additive or subtractive. In either
39
 * case, indicate an error (and don't modify the polarity) if the current
40
 * setting differs from the desired and is not GX_CINFO_POLARITY_UNKNOWN.
41
 */
42
static void
43
set_cinfo_polarity(gx_device * dev, gx_color_polarity_t new_polarity)
44
16.0M
{
45
#ifdef DEBUG
46
    /* sanity check */
47
    if (new_polarity == GX_CINFO_POLARITY_UNKNOWN) {
48
        dmprintf(dev->memory, "set_cinfo_polarity: illegal operand\n");
49
        return;
50
    }
51
#endif
52
    /*
53
     * The meory devices assume that single color devices are gray.
54
     * This may not be true if SeparationOrder is specified.  Thus only
55
     * change the value if the current value is unknown.
56
     */
57
16.0M
    if (dev->color_info.polarity == GX_CINFO_POLARITY_UNKNOWN)
58
0
        dev->color_info.polarity = new_polarity;
59
16.0M
}
60
61
static gx_color_index
62
(*get_encode_color(gx_device *dev))(gx_device *, const gx_color_value *)
63
203M
{
64
203M
    dev_proc_encode_color(*encode_proc);
65
66
    /* use encode_color if it has been provided */
67
203M
    if ((encode_proc = dev_proc(dev, encode_color)) == 0) {
68
16.0M
        if (dev->color_info.num_components == 1                          &&
69
16.0M
            dev_proc(dev, map_rgb_color) != 0) {
70
12.5M
            set_cinfo_polarity(dev, GX_CINFO_POLARITY_ADDITIVE);
71
12.5M
            encode_proc = gx_backwards_compatible_gray_encode;
72
12.5M
        } else  if ( (dev->color_info.num_components == 3    )           &&
73
58.5k
             (encode_proc = dev_proc(dev, map_rgb_color)) != 0  )
74
58.5k
            set_cinfo_polarity(dev, GX_CINFO_POLARITY_ADDITIVE);
75
3.47M
        else if ( dev->color_info.num_components == 4                    &&
76
0
                 (encode_proc = dev_proc(dev, map_cmyk_color)) != 0   )
77
0
            set_cinfo_polarity(dev, GX_CINFO_POLARITY_SUBTRACTIVE);
78
16.0M
    }
79
80
    /*
81
     * If no encode_color procedure at this point, the color model had
82
     * better be monochrome (though not necessarily bi-level). In this
83
     * case, it is assumed to be additive, as that is consistent with
84
     * the pre-DeviceN code.
85
     *
86
     * If this is not the case, then the color model had better be known
87
     * to be separable and linear, for there is no other way to derive
88
     * an encoding. This is the case even for weakly linear and separable
89
     * color models with a known polarity.
90
     */
91
203M
    if (encode_proc == 0) {
92
3.47M
        if (dev->color_info.num_components == 1 && dev->color_info.depth != 0) {
93
3.47M
            set_cinfo_polarity(dev, GX_CINFO_POLARITY_ADDITIVE);
94
3.47M
            if (dev->color_info.max_gray == (1 << dev->color_info.depth) - 1)
95
3.47M
                encode_proc = gx_default_gray_fast_encode;
96
0
            else
97
0
                encode_proc = gx_default_gray_encode;
98
3.47M
            dev->color_info.separable_and_linear = GX_CINFO_SEP_LIN;
99
3.47M
        } else if (colors_are_separable_and_linear(&dev->color_info)) {
100
0
            gx_color_value  max_gray = dev->color_info.max_gray;
101
0
            gx_color_value  max_color = dev->color_info.max_color;
102
103
0
            if ( (max_gray & (max_gray + 1)) == 0  &&
104
0
                 (max_color & (max_color + 1)) == 0  )
105
                /* NB should be gx_default_fast_encode_color */
106
0
                encode_proc = gx_default_encode_color;
107
0
            else
108
0
                encode_proc = gx_default_encode_color;
109
0
        }
110
3.47M
    }
111
112
203M
    return encode_proc;
113
203M
}
114
115
/*
116
 * Determine if a color model has the properties of a DeviceRGB
117
 * color model. This procedure is, in all likelihood, high-grade
118
 * overkill, but since this is not a performance sensitive area
119
 * no harm is done.
120
 *
121
 * Since there is little benefit to checking the values 0, 1, or
122
 * 1/2, we use the values 1/4, 1/3, and 3/4 in their place. We
123
 * compare the results to see if the intensities match to within
124
 * a tolerance of .01, which is arbitrarily selected.
125
 */
126
127
static bool
128
is_like_DeviceRGB(gx_device * dev)
129
16.0M
{
130
16.0M
    frac                            cm_comp_fracs[3];
131
16.0M
    int                             i;
132
16.0M
    const gx_device                *cmdev;
133
16.0M
    const gx_cm_color_map_procs    *cmprocs;
134
135
16.0M
    if ( dev->color_info.num_components != 3                   ||
136
58.5k
         dev->color_info.polarity != GX_CINFO_POLARITY_ADDITIVE  )
137
16.0M
        return false;
138
139
58.5k
    cmprocs = dev_proc(dev, get_color_mapping_procs)(dev, &cmdev);
140
141
    /* check the values 1/4, 1/3, and 3/4 */
142
58.5k
    cmprocs->map_rgb(cmdev, 0, frac_1 / 4, frac_1 / 3, 3 * frac_1 / 4, cm_comp_fracs);
143
144
    /* verify results to .01 */
145
58.5k
    cm_comp_fracs[0] -= frac_1 / 4;
146
58.5k
    cm_comp_fracs[1] -= frac_1 / 3;
147
58.5k
    cm_comp_fracs[2] -= 3 * frac_1 / 4;
148
58.5k
    for ( i = 0;
149
234k
           i < 3                            &&
150
175k
           -frac_1 / 100 < cm_comp_fracs[i] &&
151
175k
           cm_comp_fracs[i] < frac_1 / 100;
152
175k
          i++ )
153
175k
        ;
154
58.5k
    return i == 3;
155
16.0M
}
156
157
/*
158
 * Similar to is_like_DeviceRGB, but for DeviceCMYK.
159
 */
160
static bool
161
is_like_DeviceCMYK(gx_device * dev)
162
0
{
163
0
    frac                            cm_comp_fracs[4];
164
0
    int                             i;
165
0
    const gx_device                *cmdev;
166
0
    const gx_cm_color_map_procs    *cmprocs;
167
168
0
    if ( dev->color_info.num_components != 4                      ||
169
0
         dev->color_info.polarity != GX_CINFO_POLARITY_SUBTRACTIVE  )
170
0
        return false;
171
172
0
    cmprocs = dev_proc(dev, get_color_mapping_procs)(dev, &cmdev);
173
    /* check the values 1/4, 1/3, 3/4, and 1/8 */
174
175
0
    cmprocs->map_cmyk(cmdev,
176
0
                      frac_1 / 4,
177
0
                      frac_1 / 3,
178
0
                      3 * frac_1 / 4,
179
0
                      frac_1 / 8,
180
0
                      cm_comp_fracs);
181
182
    /* verify results to .01 */
183
0
    cm_comp_fracs[0] -= frac_1 / 4;
184
0
    cm_comp_fracs[1] -= frac_1 / 3;
185
0
    cm_comp_fracs[2] -= 3 * frac_1 / 4;
186
0
    cm_comp_fracs[3] -= frac_1 / 8;
187
0
    for ( i = 0;
188
0
           i < 4                            &&
189
0
           -frac_1 / 100 < cm_comp_fracs[i] &&
190
0
           cm_comp_fracs[i] < frac_1 / 100;
191
0
          i++ )
192
0
        ;
193
0
    return i == 4;
194
0
}
195
196
/*
197
 * Two default decode_color procedures to use for monochrome devices.
198
 * These will make use of the map_color_rgb routine, and use the first
199
 * component of the returned value or its inverse.
200
 */
201
static int
202
gx_default_1_add_decode_color(
203
    gx_device *     dev,
204
    gx_color_index  color,
205
    gx_color_value  cv[1] )
206
0
{
207
0
    gx_color_value  rgb[3];
208
0
    int             code = dev_proc(dev, map_color_rgb)(dev, color, rgb);
209
210
0
    cv[0] = rgb[0];
211
0
    return code;
212
0
}
213
214
static int
215
gx_default_1_sub_decode_color(
216
    gx_device *     dev,
217
    gx_color_index  color,
218
    gx_color_value  cv[1] )
219
0
{
220
0
    gx_color_value  rgb[3];
221
0
    int             code = dev_proc(dev, map_color_rgb)(dev, color, rgb);
222
223
0
    cv[0] = gx_max_color_value - rgb[0];
224
0
    return code;
225
0
}
226
227
/*
228
 * A default decode_color procedure for DeviceCMYK color models.
229
 *
230
 * There is no generally accurate way of decode a DeviceCMYK color using
231
 * the map_color_rgb method. Unfortunately, there are many older devices
232
 * employ the DeviceCMYK color model but don't provide a decode_color
233
 * method. The code below works on the assumption of full undercolor
234
 * removal and black generation. This may not be accurate, but is the
235
 * best that can be done in the general case without other information.
236
 */
237
static int
238
gx_default_cmyk_decode_color(
239
    gx_device *     dev,
240
    gx_color_index  color,
241
    gx_color_value  cv[4] )
242
0
{
243
    /* The device may have been determined to be 'separable'. */
244
0
    if (colors_are_separable_and_linear(&dev->color_info))
245
0
        return gx_default_decode_color(dev, color, cv);
246
0
    else {
247
0
        int i, code = dev_proc(dev, map_color_rgb)(dev, color, cv);
248
0
        gx_color_value min_val = gx_max_color_value;
249
250
0
        for (i = 0; i < 3; i++) {
251
0
            if ((cv[i] = gx_max_color_value - cv[i]) < min_val)
252
0
                min_val = cv[i];
253
0
        }
254
0
        for (i = 0; i < 3; i++)
255
0
            cv[i] -= min_val;
256
0
        cv[3] = min_val;
257
258
0
        return code;
259
0
    }
260
0
}
261
262
/*
263
 * Special case default color decode routine for a canonical 1-bit per
264
 * component DeviceCMYK color model.
265
 */
266
static int
267
gx_1bit_cmyk_decode_color(
268
    gx_device *     dev,
269
    gx_color_index  color,
270
    gx_color_value  cv[4] )
271
0
{
272
0
    cv[0] = ((color & 0x8) != 0 ? gx_max_color_value : 0);
273
0
    cv[1] = ((color & 0x4) != 0 ? gx_max_color_value : 0);
274
0
    cv[2] = ((color & 0x2) != 0 ? gx_max_color_value : 0);
275
0
    cv[3] = ((color & 0x1) != 0 ? gx_max_color_value : 0);
276
0
    return 0;
277
0
}
278
279
static int
280
(*get_decode_color(gx_device * dev))(gx_device *, gx_color_index, gx_color_value *)
281
203M
{
282
    /* if a method has already been provided, use it */
283
203M
    if (dev_proc(dev, decode_color) != 0)
284
187M
        return dev_proc(dev, decode_color);
285
286
    /*
287
     * If a map_color_rgb method has been provided, we may be able to use it.
288
     * Currently this will always be the case, as a default value will be
289
     * provided this method. While this default may not be correct, we are not
290
     * introducing any new errors by using it.
291
     */
292
16.0M
    if (dev_proc(dev, map_color_rgb) != 0) {
293
294
        /* if the device has a DeviceRGB color model, use map_color_rgb */
295
16.0M
        if (is_like_DeviceRGB(dev))
296
58.5k
            return dev_proc(dev, map_color_rgb);
297
298
        /* If separable ande linear then use default */
299
16.0M
        if (colors_are_separable_and_linear(&dev->color_info))
300
3.47M
            return &gx_default_decode_color;
301
302
        /* gray devices can be handled based on their polarity */
303
12.5M
        if ( dev->color_info.num_components == 1 &&
304
12.5M
             dev->color_info.gray_index == 0       )
305
12.5M
            return dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE
306
12.5M
                       ? &gx_default_1_add_decode_color
307
12.5M
                       : &gx_default_1_sub_decode_color;
308
309
        /*
310
         * There is no accurate way to decode colors for cmyk devices
311
         * using the map_color_rgb procedure. Unfortunately, this cases
312
         * arises with some frequency, so it is useful not to generate an
313
         * error in this case. The mechanism below assumes full undercolor
314
         * removal and black generation, which may not be accurate but are
315
         * the  best that can be done in the general case in the absence of
316
         * other information.
317
         *
318
         * As a hack to handle certain common devices, if the map_rgb_color
319
         * routine is cmyk_1bit_map_color_rgb, we provide a direct one-bit
320
         * decoder.
321
         */
322
0
        if (is_like_DeviceCMYK(dev)) {
323
0
            if (dev_proc(dev, map_color_rgb) == cmyk_1bit_map_color_rgb)
324
0
                return &gx_1bit_cmyk_decode_color;
325
0
            else
326
0
                return &gx_default_cmyk_decode_color;
327
0
        }
328
0
    }
329
330
    /*
331
     * The separable and linear case will already have been handled by
332
     * code in gx_device_fill_in_procs, so at this point we can only hope
333
     * the device doesn't use the decode_color method.
334
     */
335
0
    if (colors_are_separable_and_linear(&dev->color_info))
336
0
        return &gx_default_decode_color;
337
0
    else
338
0
        return &gx_error_decode_color;
339
0
}
340
341
/*
342
 * If a device has a linear and separable encode color function then
343
 * set up the comp_bits, comp_mask, and comp_shift fields.  Note:  This
344
 * routine assumes that the colorant shift factor decreases with the
345
 * component number.  See check_device_separable() for a general routine.
346
 */
347
void
348
set_linear_color_bits_mask_shift(gx_device * dev)
349
95.5k
{
350
95.5k
    int i;
351
95.5k
    byte gray_index = dev->color_info.gray_index;
352
95.5k
    gx_color_value max_gray = dev->color_info.max_gray;
353
95.5k
    gx_color_value max_color = dev->color_info.max_color;
354
95.5k
    int num_components = dev->color_info.num_components;
355
356
1.37M
#define comp_bits (dev->color_info.comp_bits)
357
798k
#define comp_mask (dev->color_info.comp_mask)
358
2.87M
#define comp_shift (dev->color_info.comp_shift)
359
95.5k
    comp_shift[num_components - 1] = 0;
360
798k
    for ( i = num_components - 1 - 1; i >= 0; i-- ) {
361
703k
        comp_shift[i] = comp_shift[i + 1] +
362
703k
            ( i == gray_index ? ilog2(max_gray + 1) : ilog2(max_color + 1) );
363
703k
    }
364
894k
    for ( i = 0; i < num_components; i++ ) {
365
798k
        comp_bits[i] = ( i == gray_index ?
366
69.7k
                         ilog2(max_gray + 1) :
367
798k
                         ilog2(max_color + 1) );
368
798k
        if (comp_shift[i] >= sizeof(gx_color_index)*8)
369
225k
      comp_mask[i] = 0;
370
573k
        else
371
573k
            comp_mask[i] = (((gx_color_index)1 << comp_bits[i]) - 1)
372
573k
                                               << comp_shift[i];
373
798k
    }
374
95.5k
#undef comp_bits
375
95.5k
#undef comp_mask
376
95.5k
#undef comp_shift
377
95.5k
}
378
379
/* Determine if a number is a power of two.  Works only for integers. */
380
257M
#define is_power_of_two(x) ((((x) - 1) & (x)) == 0)
381
382
/* A brutish way to check if we are a HT device */
383
bool
384
device_is_contone(gx_device* pdev)
385
3.47M
{
386
3.47M
    if ((float)pdev->color_info.depth / (float)pdev->color_info.num_components >= 8)
387
467k
        return true;
388
3.00M
    return false;
389
3.47M
}
390
391
/*
392
 * This routine attempts to determine if a device's encode_color procedure
393
 * produces gx_color_index values which are 'separable'.  A 'separable' value
394
 * means two things.  Each colorant has a group of bits in the gx_color_index
395
 * value which is associated with the colorant.  These bits are separate.
396
 * I.e. no bit is associated with more than one colorant.  If a colorant has
397
 * a value of zero then the bits associated with that colorant are zero.
398
 * These criteria allows the graphics library to build gx_color_index values
399
 * from the colorant values and not using the encode_color routine. This is
400
 * useful and necessary for overprinting, halftoning more
401
 * than four colorants, and the fast shading logic.  However this information
402
 * is not setup by the default device macros.  Thus we attempt to derive this
403
 * information.
404
 *
405
 * This routine can be fooled.  However it usually errors on the side of
406
 * assuing that a device is not separable.  In this case it does not create
407
 * any new problems.  In theory it can be fooled into believing that a device
408
 * is separable when it is not.  However we do not know of any real cases that
409
 * will fool it.
410
 */
411
void
412
check_device_separable(gx_device * dev)
413
192M
{
414
192M
    int i, j;
415
192M
    gx_device_color_info * pinfo = &(dev->color_info);
416
192M
    int num_components = pinfo->num_components;
417
192M
    byte comp_shift[GX_DEVICE_COLOR_MAX_COMPONENTS];
418
192M
    byte comp_bits[GX_DEVICE_COLOR_MAX_COMPONENTS];
419
192M
    gx_color_index comp_mask[GX_DEVICE_COLOR_MAX_COMPONENTS];
420
192M
    gx_color_index color_index;
421
192M
    gx_color_index current_bits = 0;
422
192M
    gx_color_value colorants[GX_DEVICE_COLOR_MAX_COMPONENTS] = { 0 };
423
424
    /* If this is already known then we do not need to do anything. */
425
192M
    if (pinfo->separable_and_linear != GX_CINFO_UNKNOWN_SEP_LIN)
426
93.7M
        return;
427
    /* If there is not an encode_color_routine then we cannot proceed. */
428
98.4M
    if (dev_proc(dev, encode_color) == NULL)
429
12.5M
        return;
430
    /*
431
     * If these values do not check then we should have an error.  However
432
     * we do not know what to do so we are simply exitting and hoping that
433
     * the device will clean up its values.
434
     */
435
85.9M
    if (pinfo->gray_index < num_components &&
436
85.8M
        (!pinfo->dither_grays || pinfo->dither_grays != (pinfo->max_gray + 1)))
437
0
            return;
438
85.9M
    if ((num_components > 1 || pinfo->gray_index != 0) &&
439
63.2k
        (!pinfo->dither_colors || pinfo->dither_colors != (pinfo->max_color + 1)))
440
0
        return;
441
    /*
442
     * If dither_grays or dither_colors is not a power of two then we assume
443
     * that the device is not separable.  In theory this not a requirement
444
     * but it has been true for all of the devices that we have seen so far.
445
     * This assumption also makes the logic in the next section easier.
446
     */
447
85.9M
    if (!is_power_of_two(pinfo->dither_grays)
448
85.9M
                    || !is_power_of_two(pinfo->dither_colors))
449
0
        return;
450
    /*
451
     * Use the encode_color routine to try to verify that the device is
452
     * separable and to determine the shift count, etc. for each colorant.
453
     */
454
85.9M
    color_index = dev_proc(dev, encode_color)(dev, colorants);
455
85.9M
    if (color_index != 0)
456
85.0M
        return;    /* Exit if zero colorants produce a non zero index */
457
1.76M
    for (i = 0; i < num_components; i++) {
458
        /* Check this colorant = max with all others = 0 */
459
2.26M
        for (j = 0; j < num_components; j++)
460
1.32M
            colorants[j] = 0;
461
944k
        colorants[i] = gx_max_color_value;
462
944k
        color_index = dev_proc(dev, encode_color)(dev, colorants);
463
944k
        if (color_index == 0)  /* If no bits then we have a problem */
464
1
            return;
465
944k
        if (color_index & current_bits)  /* Check for overlapping bits */
466
0
            return;
467
944k
        current_bits |= color_index;
468
944k
        comp_mask[i] = color_index;
469
        /* Determine the shift count for the colorant */
470
2.46M
        for (j = 0; (color_index & 1) == 0 && color_index != 0; j++)
471
1.51M
            color_index >>= 1;
472
944k
        comp_shift[i] = j;
473
        /* Determine the bit count for the colorant */
474
3.31M
        for (j = 0; color_index != 0; j++) {
475
2.37M
            if ((color_index & 1) == 0) /* check for non-consecutive bits */
476
0
                return;
477
2.37M
            color_index >>= 1;
478
2.37M
        }
479
944k
        comp_bits[i] = j;
480
        /*
481
         * We could verify that the bit count matches the dither_grays or
482
         * dither_colors values, but this is not really required unless we
483
         * are halftoning.  Thus we are allowing for non equal colorant sizes.
484
         */
485
        /* Check for overlap with other colorant if they are all maxed */
486
2.26M
        for (j = 0; j < num_components; j++)
487
1.32M
            colorants[j] = gx_max_color_value;
488
944k
        colorants[i] = 0;
489
944k
        color_index = dev_proc(dev, encode_color)(dev, colorants);
490
944k
        if (color_index & comp_mask[i])  /* Check for overlapping bits */
491
0
            return;
492
944k
    }
493
    /* If we get to here then the device is very likely to be separable. */
494
817k
    pinfo->separable_and_linear = GX_CINFO_SEP_LIN;
495
1.76M
    for (i = 0; i < num_components; i++) {
496
944k
        pinfo->comp_shift[i] = comp_shift[i];
497
944k
        pinfo->comp_bits[i] = comp_bits[i];
498
944k
        pinfo->comp_mask[i] = comp_mask[i];
499
944k
    }
500
    /*
501
     * The 'gray_index' value allows one colorant to have a different number
502
     * of shades from the remainder.  Since the default macros only guess at
503
     * an appropriate value, we are setting its value based upon the data that
504
     * we just determined.  Note:  In some cases the macros set max_gray to 0
505
     * and dither_grays to 1.  This is not valid so ignore this case.
506
     */
507
817k
    for (i = 0; i < num_components; i++) {
508
817k
        int dither = 1 << comp_bits[i];
509
510
817k
        if (pinfo->dither_grays != 1 && dither == pinfo->dither_grays) {
511
817k
            pinfo->gray_index = i;
512
817k
            break;
513
817k
        }
514
817k
    }
515
817k
}
516
#undef is_power_of_two
517
518
/*
519
 * This routine attempts to determine if a device's encode_color procedure
520
 * produces values that are in keeping with "the standard encoding".
521
 * i.e. that given by pdf14_encode_color.
522
 *
523
 * It works by first checking to see if we are separable_and_linear. If not
524
 * we cannot hope to be the standard encoding.
525
 *
526
 * Then, we check to see if we are a dev device - if so, we must be
527
 * compatible.
528
 *
529
 * Failing that it checks to see if the encoding uses the appropriate
530
 * bit ranges for each individual color.
531
 *
532
 * If those (quick) tests pass, then we try the slower test of checking
533
 * the encodings. We can do this far faster than an exhaustive check, by
534
 * relying on the separability and linearity - we only need to check 256
535
 * possible values.
536
 *
537
 * The one tricky section there is to avoid the special case for
538
 * gx_no_color_index_value (which can occur when we have a 32bit
539
 * gx_color_index type, and a 4 component device, such as cmyk).
540
 * We allow the encoding to be off in the lower bits for that case.
541
 */
542
void check_device_compatible_encoding(gx_device *dev)
543
136M
{
544
136M
    gx_device_color_info * pinfo = &(dev->color_info);
545
136M
    int num_components = pinfo->num_components;
546
136M
    gx_color_index mul, color_index;
547
136M
    int i, j;
548
136M
    gx_color_value colorants[GX_DEVICE_COLOR_MAX_COMPONENTS];
549
136M
    bool deep = device_is_deep(dev);
550
551
136M
    if (pinfo->separable_and_linear == GX_CINFO_UNKNOWN_SEP_LIN)
552
42.6M
        check_device_separable(dev);
553
136M
    if (pinfo->separable_and_linear != GX_CINFO_SEP_LIN)
554
136M
        return;
555
556
48.9k
    if (dev_proc(dev, ret_devn_params)(dev) != NULL) {
557
        /* We know all devn devices are compatible. */
558
33.3k
        pinfo->separable_and_linear = GX_CINFO_SEP_LIN_STANDARD;
559
33.3k
        return;
560
33.3k
    }
561
562
    /* Do the superficial quick checks */
563
53.6k
    for (i = 0; i < num_components; i++) {
564
38.0k
        int shift = (num_components-1-i)*(8<<deep);
565
38.0k
        if (pinfo->comp_shift[i] != shift)
566
0
            goto bad;
567
38.0k
        if (pinfo->comp_bits[i] != 8<<deep)
568
0
            goto bad;
569
38.0k
        if (pinfo->comp_mask[i] != ((gx_color_index)(deep ? 65535 : 255))<<shift)
570
0
            goto bad;
571
38.0k
    }
572
573
    /* OK, now we are going to be slower. */
574
15.5k
    mul = 0;
575
53.6k
    for (i = 0; i < num_components; i++) {
576
38.0k
        mul = (mul<<(8<<deep)) | 1;
577
38.0k
    }
578
    /* In the deep case, we don't exhaustively test */
579
3.99M
    for (i = 0; i < 255; i++) {
580
13.6M
        for (j = 0; j < num_components; j++)
581
9.70M
            colorants[j] = i*257;
582
3.97M
        color_index = dev_proc(dev, encode_color)(dev, colorants);
583
3.97M
        if (color_index != i*mul*(deep ? 257 : 1) && (i*mul*(deep ? 257 : 1) != gx_no_color_index_value))
584
0
            goto bad;
585
3.97M
    }
586
    /* If we reach here, then every value matched, except possibly the last one.
587
     * We'll allow that to differ just in the lowest bits. */
588
15.5k
    if ((color_index | mul) != 255*mul*(deep ? 257 : 1))
589
0
        goto bad;
590
591
15.5k
    pinfo->separable_and_linear = GX_CINFO_SEP_LIN_STANDARD;
592
15.5k
    return;
593
0
bad:
594
0
    pinfo->separable_and_linear = GX_CINFO_SEP_LIN_NON_STANDARD;
595
0
}
596
597
int gx_default_no_copy_alpha_hl_color(gx_device * dev, const byte * data, int data_x, int raster, gx_bitmap_id id, int x, int y, int width, int height, const gx_drawing_color *pdcolor, int depth);
598
599
/* Fill in NULL procedures in a device procedure record. */
600
void
601
gx_device_fill_in_procs(register gx_device * dev)
602
203M
{
603
203M
    fill_dev_proc(dev, open_device, gx_default_open_device);
604
203M
    fill_dev_proc(dev, get_initial_matrix, gx_default_get_initial_matrix);
605
203M
    fill_dev_proc(dev, sync_output, gx_default_sync_output);
606
203M
    fill_dev_proc(dev, output_page, gx_default_output_page);
607
203M
    fill_dev_proc(dev, close_device, gx_default_close_device);
608
    /* see below for map_rgb_color */
609
203M
    fill_dev_proc(dev, map_color_rgb, gx_default_map_color_rgb);
610
    /* NOT fill_rectangle */
611
203M
    fill_dev_proc(dev, copy_mono, gx_default_copy_mono);
612
203M
    fill_dev_proc(dev, copy_color, gx_default_copy_color);
613
203M
    fill_dev_proc(dev, get_params, gx_default_get_params);
614
203M
    fill_dev_proc(dev, put_params, gx_default_put_params);
615
    /* see below for map_cmyk_color */
616
203M
    fill_dev_proc(dev, get_page_device, gx_default_get_page_device);
617
203M
    fill_dev_proc(dev, get_alpha_bits, gx_default_get_alpha_bits);
618
203M
    fill_dev_proc(dev, copy_alpha, gx_default_copy_alpha);
619
203M
    fill_dev_proc(dev, fill_path, gx_default_fill_path);
620
203M
    fill_dev_proc(dev, stroke_path, gx_default_stroke_path);
621
203M
    fill_dev_proc(dev, fill_mask, gx_default_fill_mask);
622
203M
    fill_dev_proc(dev, fill_trapezoid, gx_default_fill_trapezoid);
623
203M
    fill_dev_proc(dev, fill_parallelogram, gx_default_fill_parallelogram);
624
203M
    fill_dev_proc(dev, fill_triangle, gx_default_fill_triangle);
625
203M
    fill_dev_proc(dev, draw_thin_line, gx_default_draw_thin_line);
626
203M
    fill_dev_proc(dev, get_alpha_bits, gx_default_get_alpha_bits);
627
203M
    fill_dev_proc(dev, strip_tile_rectangle, gx_default_strip_tile_rectangle);
628
203M
    fill_dev_proc(dev, strip_copy_rop2, gx_default_strip_copy_rop2);
629
203M
    fill_dev_proc(dev, strip_tile_rect_devn, gx_default_strip_tile_rect_devn);
630
203M
    fill_dev_proc(dev, get_clipping_box, gx_default_get_clipping_box);
631
203M
    fill_dev_proc(dev, begin_typed_image, gx_default_begin_typed_image);
632
203M
    fill_dev_proc(dev, get_bits_rectangle, gx_default_get_bits_rectangle);
633
203M
    fill_dev_proc(dev, composite, gx_default_composite);
634
203M
    fill_dev_proc(dev, get_hardware_params, gx_default_get_hardware_params);
635
203M
    fill_dev_proc(dev, text_begin, gx_default_text_begin);
636
637
203M
    set_dev_proc(dev, encode_color, get_encode_color(dev));
638
203M
    if (dev->color_info.num_components == 3)
639
33.1M
        set_dev_proc(dev, map_rgb_color, dev_proc(dev, encode_color));
640
203M
    if (dev->color_info.num_components == 4)
641
50.1M
        set_dev_proc(dev, map_cmyk_color, dev_proc(dev, encode_color));
642
643
203M
    if (colors_are_separable_and_linear(&dev->color_info)) {
644
36.8M
        fill_dev_proc(dev, encode_color, gx_default_encode_color);
645
36.8M
        fill_dev_proc(dev, map_cmyk_color, gx_default_encode_color);
646
36.8M
        fill_dev_proc(dev, map_rgb_color, gx_default_encode_color);
647
166M
    } else {
648
        /* if it isn't set now punt */
649
166M
        fill_dev_proc(dev, encode_color, gx_error_encode_color);
650
166M
        fill_dev_proc(dev, map_cmyk_color, gx_error_encode_color);
651
166M
        fill_dev_proc(dev, map_rgb_color, gx_error_encode_color);
652
166M
    }
653
654
    /*
655
     * Fill in the color mapping procedures and the component index
656
     * assignment procedure if they have not been provided by the client.
657
     *
658
     * Because it is difficult to provide default encoding procedures
659
     * that handle level inversion, this code needs to check both
660
     * the number of components and the polarity of color model.
661
     */
662
203M
    switch (dev->color_info.num_components) {
663
119M
    case 1:     /* DeviceGray or DeviceInvertGray */
664
        /*
665
         * If not gray then the device must provide the color
666
         * mapping procs.
667
         */
668
119M
        if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE) {
669
119M
            fill_dev_proc( dev,
670
119M
                       get_color_mapping_procs,
671
119M
                       gx_default_DevGray_get_color_mapping_procs );
672
119M
        } else
673
4.98k
            fill_dev_proc(dev, get_color_mapping_procs, gx_error_get_color_mapping_procs);
674
119M
        fill_dev_proc( dev,
675
119M
                       get_color_comp_index,
676
119M
                       gx_default_DevGray_get_color_comp_index );
677
119M
        break;
678
679
33.1M
    case 3:
680
33.1M
        if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE) {
681
33.1M
            fill_dev_proc( dev,
682
33.1M
                       get_color_mapping_procs,
683
33.1M
                       gx_default_DevRGB_get_color_mapping_procs );
684
33.1M
            fill_dev_proc( dev,
685
33.1M
                       get_color_comp_index,
686
33.1M
                       gx_default_DevRGB_get_color_comp_index );
687
33.1M
        } else {
688
0
            fill_dev_proc(dev, get_color_mapping_procs, gx_error_get_color_mapping_procs);
689
0
            fill_dev_proc(dev, get_color_comp_index, gx_error_get_color_comp_index);
690
0
        }
691
33.1M
        break;
692
693
50.1M
    case 4:
694
50.1M
        fill_dev_proc(dev, get_color_mapping_procs, gx_default_DevCMYK_get_color_mapping_procs);
695
50.1M
        fill_dev_proc(dev, get_color_comp_index, gx_default_DevCMYK_get_color_comp_index);
696
50.1M
        break;
697
88.1k
    default:    /* Unknown color model - set error handlers */
698
88.1k
        if (dev_proc(dev, get_color_mapping_procs) == NULL) {
699
0
            fill_dev_proc(dev, get_color_mapping_procs, gx_error_get_color_mapping_procs);
700
0
            fill_dev_proc(dev, get_color_comp_index, gx_error_get_color_comp_index);
701
0
        }
702
203M
    }
703
704
203M
    set_dev_proc(dev, decode_color, get_decode_color(dev));
705
203M
    fill_dev_proc(dev, get_profile, gx_default_get_profile);
706
203M
    fill_dev_proc(dev, set_graphics_type_tag, gx_default_set_graphics_type_tag);
707
708
203M
    fill_dev_proc(dev, fill_rectangle_hl_color, gx_default_fill_rectangle_hl_color);
709
203M
    fill_dev_proc(dev, include_color_space, gx_default_include_color_space);
710
203M
    fill_dev_proc(dev, fill_linear_color_scanline, gx_default_fill_linear_color_scanline);
711
203M
    fill_dev_proc(dev, fill_linear_color_trapezoid, gx_default_fill_linear_color_trapezoid);
712
203M
    fill_dev_proc(dev, fill_linear_color_triangle, gx_default_fill_linear_color_triangle);
713
203M
    fill_dev_proc(dev, update_spot_equivalent_colors, gx_default_update_spot_equivalent_colors);
714
203M
    fill_dev_proc(dev, ret_devn_params, gx_default_ret_devn_params);
715
203M
    fill_dev_proc(dev, fillpage, gx_default_fillpage);
716
203M
    fill_dev_proc(dev, copy_alpha_hl_color, gx_default_no_copy_alpha_hl_color);
717
718
203M
    fill_dev_proc(dev, begin_transparency_group, gx_default_begin_transparency_group);
719
203M
    fill_dev_proc(dev, end_transparency_group, gx_default_end_transparency_group);
720
721
203M
    fill_dev_proc(dev, begin_transparency_mask, gx_default_begin_transparency_mask);
722
203M
    fill_dev_proc(dev, end_transparency_mask, gx_default_end_transparency_mask);
723
203M
    fill_dev_proc(dev, discard_transparency_layer, gx_default_discard_transparency_layer);
724
725
203M
    fill_dev_proc(dev, push_transparency_state, gx_default_push_transparency_state);
726
203M
    fill_dev_proc(dev, pop_transparency_state, gx_default_pop_transparency_state);
727
728
203M
    fill_dev_proc(dev, put_image, gx_default_put_image);
729
730
203M
    fill_dev_proc(dev, dev_spec_op, gx_default_dev_spec_op);
731
203M
    fill_dev_proc(dev, copy_planes, gx_default_copy_planes);
732
203M
    fill_dev_proc(dev, process_page, gx_default_process_page);
733
203M
    fill_dev_proc(dev, transform_pixel_region, gx_default_transform_pixel_region);
734
203M
    fill_dev_proc(dev, fill_stroke_path, gx_default_fill_stroke_path);
735
203M
    fill_dev_proc(dev, lock_pattern, gx_default_lock_pattern);
736
203M
}
737
738
739
int
740
gx_default_open_device(gx_device * dev)
741
749k
{
742
    /* Initialize the separable status if not known. */
743
749k
    check_device_separable(dev);
744
749k
    return 0;
745
749k
}
746
747
/* Get the initial matrix for a device with inverted Y. */
748
/* This includes essentially all printers and displays. */
749
/* Supports LeadingEdge, but no margins or viewports */
750
void
751
gx_default_get_initial_matrix(gx_device * dev, register gs_matrix * pmat)
752
95.5M
{
753
    /* NB this device has no paper margins */
754
95.5M
    double fs_res = dev->HWResolution[0] / 72.0;
755
95.5M
    double ss_res = dev->HWResolution[1] / 72.0;
756
757
95.5M
    switch(dev->LeadingEdge & LEADINGEDGE_MASK) {
758
0
    case 1: /* 90 degrees */
759
0
        pmat->xx = 0;
760
0
        pmat->xy = -ss_res;
761
0
        pmat->yx = -fs_res;
762
0
        pmat->yy = 0;
763
0
        pmat->tx = (float)dev->width;
764
0
        pmat->ty = (float)dev->height;
765
0
        break;
766
0
    case 2: /* 180 degrees */
767
0
        pmat->xx = -fs_res;
768
0
        pmat->xy = 0;
769
0
        pmat->yx = 0;
770
0
        pmat->yy = ss_res;
771
0
        pmat->tx = (float)dev->width;
772
0
        pmat->ty = 0;
773
0
        break;
774
0
    case 3: /* 270 degrees */
775
0
        pmat->xx = 0;
776
0
        pmat->xy = ss_res;
777
0
        pmat->yx = fs_res;
778
0
        pmat->yy = 0;
779
0
        pmat->tx = 0;
780
0
        pmat->ty = 0;
781
0
        break;
782
0
    default:
783
95.5M
    case 0:
784
95.5M
        pmat->xx = fs_res;
785
95.5M
        pmat->xy = 0;
786
95.5M
        pmat->yx = 0;
787
95.5M
        pmat->yy = -ss_res;
788
95.5M
        pmat->tx = 0;
789
95.5M
        pmat->ty = (float)dev->height;
790
        /****** tx/y is WRONG for devices with ******/
791
        /****** arbitrary initial matrix ******/
792
95.5M
        break;
793
95.5M
    }
794
95.5M
}
795
/* Get the initial matrix for a device with upright Y. */
796
/* This includes just a few printers and window systems. */
797
void
798
gx_upright_get_initial_matrix(gx_device * dev, register gs_matrix * pmat)
799
3.94M
{
800
3.94M
    pmat->xx = dev->HWResolution[0] / 72.0; /* x_pixels_per_inch */
801
3.94M
    pmat->xy = 0;
802
3.94M
    pmat->yx = 0;
803
3.94M
    pmat->yy = dev->HWResolution[1] / 72.0; /* y_pixels_per_inch */
804
    /****** tx/y is WRONG for devices with ******/
805
    /****** arbitrary initial matrix ******/
806
3.94M
    pmat->tx = 0;
807
3.94M
    pmat->ty = 0;
808
3.94M
}
809
810
int
811
gx_default_sync_output(gx_device * dev) /* lgtm [cpp/useless-expression] */
812
1.46M
{
813
1.46M
    return 0;
814
1.46M
}
815
816
int
817
gx_default_output_page(gx_device * dev, int num_copies, int flush)
818
147k
{
819
147k
    int code = dev_proc(dev, sync_output)(dev);
820
821
147k
    if (code >= 0)
822
147k
        code = gx_finish_output_page(dev, num_copies, flush);
823
147k
    return code;
824
147k
}
825
826
int
827
gx_default_close_device(gx_device * dev)
828
1.18M
{
829
1.18M
    return 0;
830
1.18M
}
831
832
gx_device *
833
gx_default_get_page_device(gx_device * dev)
834
2.30M
{
835
2.30M
    return NULL;
836
2.30M
}
837
gx_device *
838
gx_page_device_get_page_device(gx_device * dev)
839
30.5M
{
840
30.5M
    return dev;
841
30.5M
}
842
843
int
844
gx_default_get_alpha_bits(gx_device * dev, graphics_object_type type)
845
92.3M
{
846
92.3M
    return (type == go_text ? dev->color_info.anti_alias.text_bits :
847
92.3M
            dev->color_info.anti_alias.graphics_bits);
848
92.3M
}
849
850
void
851
gx_default_get_clipping_box(gx_device * dev, gs_fixed_rect * pbox)
852
126M
{
853
126M
    pbox->p.x = 0;
854
126M
    pbox->p.y = 0;
855
126M
    pbox->q.x = int2fixed(dev->width);
856
126M
    pbox->q.y = int2fixed(dev->height);
857
126M
}
858
void
859
gx_get_largest_clipping_box(gx_device * dev, gs_fixed_rect * pbox)
860
715
{
861
715
    pbox->p.x = min_fixed;
862
715
    pbox->p.y = min_fixed;
863
715
    pbox->q.x = max_fixed;
864
715
    pbox->q.y = max_fixed;
865
715
}
866
867
int
868
gx_no_composite(gx_device * dev, gx_device ** pcdev,
869
                        const gs_composite_t * pcte,
870
                        gs_gstate * pgs, gs_memory_t * memory,
871
                        gx_device *cdev)
872
0
{
873
0
    return_error(gs_error_unknownerror);  /* not implemented */
874
0
}
875
int
876
gx_default_composite(gx_device * dev, gx_device ** pcdev,
877
                             const gs_composite_t * pcte,
878
                             gs_gstate * pgs, gs_memory_t * memory,
879
                             gx_device *cdev)
880
12.3M
{
881
12.3M
    return pcte->type->procs.create_default_compositor
882
12.3M
        (pcte, pcdev, dev, pgs, memory);
883
12.3M
}
884
int
885
gx_null_composite(gx_device * dev, gx_device ** pcdev,
886
                          const gs_composite_t * pcte,
887
                          gs_gstate * pgs, gs_memory_t * memory,
888
                          gx_device *cdev)
889
1.16k
{
890
1.16k
    *pcdev = dev;
891
1.16k
    return 0;
892
1.16k
}
893
894
/*
895
 * Default handler for creating a compositor device when writing the clist. */
896
int
897
gx_default_composite_clist_write_update(const gs_composite_t *pcte, gx_device * dev,
898
                gx_device ** pcdev, gs_gstate * pgs, gs_memory_t * mem)
899
756k
{
900
756k
    *pcdev = dev;   /* Do nothing -> return the same device */
901
756k
    return 0;
902
756k
}
903
904
/* Default handler for adjusting a compositor's CTM. */
905
int
906
gx_default_composite_adjust_ctm(gs_composite_t *pcte, int x0, int y0, gs_gstate *pgs)
907
100M
{
908
100M
    return 0;
909
100M
}
910
911
/*
912
 * Default check for closing compositor.
913
 */
914
gs_compositor_closing_state
915
gx_default_composite_is_closing(const gs_composite_t *this, gs_composite_t **pcte, gx_device *dev)
916
0
{
917
0
    return COMP_ENQUEUE;
918
0
}
919
920
/*
921
 * Default check whether a next operation is friendly to the compositor.
922
 */
923
bool
924
gx_default_composite_is_friendly(const gs_composite_t *this, byte cmd0, byte cmd1)
925
1.49M
{
926
1.49M
    return false;
927
1.49M
}
928
929
/*
930
 * Default handler for updating the clist device when reading a compositing
931
 * device.
932
 */
933
int
934
gx_default_composite_clist_read_update(gs_composite_t *pxcte, gx_device * cdev,
935
                gx_device * tdev, gs_gstate * pgs, gs_memory_t * mem)
936
100M
{
937
100M
    return 0;     /* Do nothing */
938
100M
}
939
940
/*
941
 * Default handler for get_cropping returns no cropping.
942
 */
943
int
944
gx_default_composite_get_cropping(const gs_composite_t *pxcte, int *ry, int *rheight,
945
                                  int cropping_min, int cropping_max)
946
756k
{
947
756k
    return 0;     /* No cropping. */
948
756k
}
949
950
int
951
gx_default_initialize_device(gx_device *dev)
952
0
{
953
0
    return 0;
954
0
}
955
956
int
957
gx_default_dev_spec_op(gx_device *pdev, int dev_spec_op, void *data, int size)
958
540M
{
959
540M
    switch(dev_spec_op) {
960
0
        case gxdso_form_begin:
961
0
        case gxdso_form_end:
962
39.7k
        case gxdso_pattern_can_accum:
963
39.7k
        case gxdso_pattern_start_accum:
964
39.7k
        case gxdso_pattern_finish_accum:
965
1.25M
        case gxdso_pattern_load:
966
44.2M
        case gxdso_pattern_shading_area:
967
45.2M
        case gxdso_pattern_is_cpath_accum:
968
45.2M
        case gxdso_pattern_handles_clip_path:
969
45.2M
        case gxdso_is_pdf14_device:
970
109M
        case gxdso_supports_devn:
971
109M
        case gxdso_supports_hlcolor:
972
109M
        case gxdso_supports_saved_pages:
973
109M
        case gxdso_needs_invariant_palette:
974
110M
        case gxdso_supports_iccpostrender:
975
110M
        case gxdso_supports_alpha:
976
111M
        case gxdso_pdf14_sep_device:
977
112M
        case gxdso_supports_pattern_transparency:
978
112M
        case gxdso_overprintsim_state:
979
112M
        case gxdso_skip_icc_component_validation:
980
112M
            return 0;
981
243
        case gxdso_pattern_shfill_doesnt_need_path:
982
243
            return (dev_proc(pdev, fill_path) == gx_default_fill_path);
983
11.9M
        case gxdso_is_std_cmyk_1bit:
984
11.9M
            return (dev_proc(pdev, map_cmyk_color) == cmyk_1bit_map_cmyk_color);
985
3
        case gxdso_interpolate_antidropout:
986
3
            return pdev->color_info.use_antidropout_downscaler;
987
1.00M
        case gxdso_interpolate_threshold:
988
1.00M
            if ((pdev->color_info.num_components == 1 &&
989
360k
                 pdev->color_info.max_gray < 15) ||
990
761k
                (pdev->color_info.num_components > 1 &&
991
642k
                 pdev->color_info.max_color < 15)) {
992
                /* If we are a limited color device (i.e. we are halftoning)
993
                 * then only interpolate if we are upscaling by at least 4 */
994
574k
                return 4;
995
574k
            }
996
428k
            return 0; /* Otherwise no change */
997
3.91M
        case gxdso_get_dev_param:
998
3.91M
            {
999
3.91M
                dev_param_req_t *request = (dev_param_req_t *)data;
1000
3.91M
                return gx_default_get_param(pdev, request->Param, request->list);
1001
1.00M
            }
1002
2.17M
        case gxdso_current_output_device:
1003
2.17M
            {
1004
2.17M
                *(gx_device **)data = pdev;
1005
2.17M
                return 0;
1006
1.00M
            }
1007
56.0k
        case gxdso_copy_color_is_fast:
1008
56.0k
            return (dev_proc(pdev, copy_color) != gx_default_copy_color);
1009
2.63M
        case gxdso_is_encoding_direct:
1010
2.63M
            if (pdev->color_info.depth != 8 * pdev->color_info.num_components)
1011
0
                return 0;
1012
2.63M
            return (dev_proc(pdev, encode_color) == gx_default_encode_color ||
1013
2.63M
                    dev_proc(pdev, encode_color) == gx_default_rgb_map_rgb_color);
1014
        /* Just ignore information about events */
1015
0
        case gxdso_event_info:
1016
0
            return 0;
1017
122M
        case gxdso_overprint_active:
1018
122M
            return 0;
1019
506k
        case gxdso_overprint_adjust:
1020
506k
            return 0;
1021
0
        case gxdso_set_srcgtag:
1022
0
            rc_decrement(pdev->srcgtag,"default dev_spec_op");
1023
0
            pdev->srcgtag = data;
1024
0
            rc_increment(pdev->srcgtag);
1025
0
            return 0;
1026
540M
    }
1027
540M
    return_error(gs_error_undefined);
1028
540M
}
1029
1030
int
1031
gx_default_fill_rectangle_hl_color(gx_device *pdev,
1032
    const gs_fixed_rect *rect,
1033
    const gs_gstate *pgs, const gx_drawing_color *pdcolor,
1034
    const gx_clip_path *pcpath)
1035
6
{
1036
6
    return_error(gs_error_rangecheck);
1037
6
}
1038
1039
int
1040
gx_default_include_color_space(gx_device *pdev, gs_color_space *cspace,
1041
        const byte *res_name, int name_length)
1042
0
{
1043
0
    return 0;
1044
0
}
1045
1046
/*
1047
 * If a device wants to determine an equivalent color for its spot colors then
1048
 * it needs to implement this method.  See comments at the start of
1049
 * src/gsequivc.c.
1050
 */
1051
int
1052
gx_default_update_spot_equivalent_colors(gx_device *pdev, const gs_gstate * pgs, const gs_color_space *pcs)
1053
3.96k
{
1054
3.96k
    return 0;
1055
3.96k
}
1056
1057
/*
1058
 * If a device wants to determine implement support for spot colors then
1059
 * it needs to implement this method.
1060
 */
1061
gs_devn_params *
1062
gx_default_ret_devn_params(gx_device *pdev)
1063
128M
{
1064
128M
    return NULL;
1065
128M
}
1066
1067
int
1068
gx_default_process_page(gx_device *dev, gx_process_page_options_t *options)
1069
0
{
1070
0
    gs_int_rect rect;
1071
0
    int code = 0;
1072
0
    void *buffer = NULL;
1073
1074
    /* Possible future improvements in here could be given by us dividing the
1075
     * page up into n chunks, and spawning a thread per chunk to do the
1076
     * process_fn call on. n could be given by NumRenderingThreads. This
1077
     * would give us multi-core advantages even without clist. */
1078
0
    if (options->init_buffer_fn) {
1079
0
        code = options->init_buffer_fn(options->arg, dev, dev->memory, dev->width, dev->height, &buffer);
1080
0
        if (code < 0)
1081
0
            return code;
1082
0
    }
1083
1084
0
    rect.p.x = 0;
1085
0
    rect.p.y = 0;
1086
0
    rect.q.x = dev->width;
1087
0
    rect.q.y = dev->height;
1088
0
    if (options->process_fn)
1089
0
        code = options->process_fn(options->arg, dev, dev, &rect, buffer);
1090
0
    if (code >= 0 && options->output_fn)
1091
0
        code = options->output_fn(options->arg, dev, buffer);
1092
1093
0
    if (options->free_buffer_fn)
1094
0
        options->free_buffer_fn(options->arg, dev, dev->memory, buffer);
1095
1096
0
    return code;
1097
0
}
1098
1099
int
1100
gx_default_begin_transparency_group(gx_device *dev, const gs_transparency_group_params_t *ptgp, const gs_rect *pbbox, gs_gstate *pgs, gs_memory_t *mem)
1101
0
{
1102
0
    return 0;
1103
0
}
1104
1105
int
1106
gx_default_end_transparency_group(gx_device *dev, gs_gstate *pgs)
1107
0
{
1108
0
    return 0;
1109
0
}
1110
1111
int
1112
gx_default_begin_transparency_mask(gx_device *dev, const gx_transparency_mask_params_t *ptgp, const gs_rect *pbbox, gs_gstate *pgs, gs_memory_t *mem)
1113
0
{
1114
0
    return 0;
1115
0
}
1116
1117
int
1118
gx_default_end_transparency_mask(gx_device *dev, gs_gstate *pgs)
1119
0
{
1120
0
    return 0;
1121
0
}
1122
1123
int
1124
gx_default_discard_transparency_layer(gx_device *dev, gs_gstate *pgs)
1125
0
{
1126
0
    return 0;
1127
0
}
1128
1129
int
1130
gx_default_push_transparency_state(gx_device *dev, gs_gstate *pgs)
1131
0
{
1132
0
    return 0;
1133
0
}
1134
1135
int
1136
gx_default_pop_transparency_state(gx_device *dev, gs_gstate *pgs)
1137
0
{
1138
0
    return 0;
1139
0
}
1140
1141
int
1142
gx_default_put_image(gx_device *dev, gx_device *mdev, const byte **buffers, int num_chan, int x, int y, int width, int height, int row_stride, int alpha_plane_index, int tag_plane_index)
1143
23.4k
{
1144
23.4k
    return_error(gs_error_undefined);
1145
23.4k
}
1146
1147
int
1148
gx_default_no_copy_alpha_hl_color(gx_device * dev, const byte * data, int data_x, int raster, gx_bitmap_id id, int x, int y, int width, int height, const gx_drawing_color *pdcolor, int depth)
1149
0
{
1150
0
    return_error(gs_error_undefined);
1151
0
}
1152
1153
int
1154
gx_default_copy_planes(gx_device *dev, const byte *data, int data_x, int raster, gx_bitmap_id id, int x, int y, int width, int height, int plane_height)
1155
0
{
1156
0
    return_error(gs_error_undefined);
1157
0
}
1158
1159
/* ---------------- Default per-instance procedures ---------------- */
1160
1161
int
1162
gx_default_install(gx_device * dev, gs_gstate * pgs)
1163
611k
{
1164
611k
    return 0;
1165
611k
}
1166
1167
int
1168
gx_default_begin_page(gx_device * dev, gs_gstate * pgs)
1169
803k
{
1170
803k
    return 0;
1171
803k
}
1172
1173
int
1174
gx_default_end_page(gx_device * dev, int reason, gs_gstate * pgs)
1175
979k
{
1176
979k
    return (reason != 2 ? 1 : 0);
1177
979k
}
1178
1179
void
1180
gx_default_set_graphics_type_tag(gx_device *dev, gs_graphics_type_tag_t graphics_type_tag)
1181
3.51M
{
1182
    /* set the tag but carefully preserve GS_DEVICE_ENCODES_TAGS */
1183
3.51M
    dev->graphics_type_tag = (dev->graphics_type_tag & GS_DEVICE_ENCODES_TAGS) | graphics_type_tag;
1184
3.51M
}
1185
1186
/* ---------------- Device subclassing procedures ---------------- */
1187
1188
/* Non-obvious code. The 'dest_procs' is the 'procs' memory occupied by the original device that we decided to subclass,
1189
 * 'src_procs' is the newly allocated piece of memory, to which we have already copied the content of the
1190
 * original device (including the procs), prototype is the device structure prototype for the subclassing device.
1191
 * Here we copy the methods from the prototype to the original device procs memory *but* if the original (src_procs)
1192
 * device had a NULL method, we make the new device procs have a NULL method too.
1193
 * The reason for ths is ugly, there are some places in the graphics library which explicitly check for
1194
 * a device having a NULL method and take different code paths depending on the result.
1195
 * Now in general we expect subclassing devices to implement *every* method, so if we didn't copy
1196
 * over NULL methods present in the original source device then the code path could be inappropriate for
1197
 * that underlying (now subclassed) device.
1198
 */
1199
/* November 10th 2017 Restored the original behaviour of the device methods, they should now never be NULL.
1200
 * Howwever, there are still places in the code which take different code paths if the device method is (now)
1201
 * the default device method, rather than a device-specific method.
1202
 * So instead of checking for NULL, we now need to check against the default implementation, and *NOT* copy the
1203
 * prototype (subclass device) method if the original device had the default implementation.
1204
 * I suspect a combination of forwarding and subclassing devices will not work properly for this reason.
1205
 */
1206
int gx_copy_device_procs(gx_device *dest, const gx_device *src, const gx_device *pprototype)
1207
32.2k
{
1208
32.2k
    gx_device prototype = *pprototype;
1209
1210
    /* In the new (as of 2021) world, the prototype does not contain
1211
     * device procs. We need to call the 'initialize_device_procs'
1212
     * function to properly populate the procs array. We can't write to
1213
     * the const prototype pointer we are passed in, so copy it to a
1214
     * local block, and initialize that instead, */
1215
32.2k
    prototype.initialize_device_procs(&prototype);
1216
    /* Fill in missing entries with the global defaults */
1217
32.2k
    gx_device_fill_in_procs(&prototype);
1218
1219
32.2k
    if (dest->initialize_device_procs == NULL)
1220
0
       dest->initialize_device_procs = prototype.initialize_device_procs;
1221
1222
32.2k
    set_dev_proc(dest, initialize_device, dev_proc(&prototype, initialize_device));
1223
32.2k
    set_dev_proc(dest, open_device, dev_proc(&prototype, open_device));
1224
32.2k
    set_dev_proc(dest, get_initial_matrix, dev_proc(&prototype, get_initial_matrix));
1225
32.2k
    set_dev_proc(dest, sync_output, dev_proc(&prototype, sync_output));
1226
32.2k
    set_dev_proc(dest, output_page, dev_proc(&prototype, output_page));
1227
32.2k
    set_dev_proc(dest, close_device, dev_proc(&prototype, close_device));
1228
32.2k
    set_dev_proc(dest, map_rgb_color, dev_proc(&prototype, map_rgb_color));
1229
32.2k
    set_dev_proc(dest, map_color_rgb, dev_proc(&prototype, map_color_rgb));
1230
32.2k
    set_dev_proc(dest, fill_rectangle, dev_proc(&prototype, fill_rectangle));
1231
32.2k
    set_dev_proc(dest, copy_mono, dev_proc(&prototype, copy_mono));
1232
32.2k
    set_dev_proc(dest, copy_color, dev_proc(&prototype, copy_color));
1233
32.2k
    set_dev_proc(dest, get_params, dev_proc(&prototype, get_params));
1234
32.2k
    set_dev_proc(dest, put_params, dev_proc(&prototype, put_params));
1235
32.2k
    set_dev_proc(dest, map_cmyk_color, dev_proc(&prototype, map_cmyk_color));
1236
32.2k
    set_dev_proc(dest, get_page_device, dev_proc(&prototype, get_page_device));
1237
32.2k
    set_dev_proc(dest, get_alpha_bits, dev_proc(&prototype, get_alpha_bits));
1238
32.2k
    set_dev_proc(dest, copy_alpha, dev_proc(&prototype, copy_alpha));
1239
32.2k
    set_dev_proc(dest, fill_path, dev_proc(&prototype, fill_path));
1240
32.2k
    set_dev_proc(dest, stroke_path, dev_proc(&prototype, stroke_path));
1241
32.2k
    set_dev_proc(dest, fill_trapezoid, dev_proc(&prototype, fill_trapezoid));
1242
32.2k
    set_dev_proc(dest, fill_parallelogram, dev_proc(&prototype, fill_parallelogram));
1243
32.2k
    set_dev_proc(dest, fill_triangle, dev_proc(&prototype, fill_triangle));
1244
32.2k
    set_dev_proc(dest, draw_thin_line, dev_proc(&prototype, draw_thin_line));
1245
32.2k
    set_dev_proc(dest, strip_tile_rectangle, dev_proc(&prototype, strip_tile_rectangle));
1246
32.2k
    set_dev_proc(dest, get_clipping_box, dev_proc(&prototype, get_clipping_box));
1247
32.2k
    set_dev_proc(dest, begin_typed_image, dev_proc(&prototype, begin_typed_image));
1248
32.2k
    set_dev_proc(dest, get_bits_rectangle, dev_proc(&prototype, get_bits_rectangle));
1249
32.2k
    set_dev_proc(dest, composite, dev_proc(&prototype, composite));
1250
32.2k
    set_dev_proc(dest, get_hardware_params, dev_proc(&prototype, get_hardware_params));
1251
32.2k
    set_dev_proc(dest, text_begin, dev_proc(&prototype, text_begin));
1252
32.2k
    set_dev_proc(dest, discard_transparency_layer, dev_proc(&prototype, discard_transparency_layer));
1253
32.2k
    set_dev_proc(dest, get_color_mapping_procs, dev_proc(&prototype, get_color_mapping_procs));
1254
32.2k
    set_dev_proc(dest, get_color_comp_index, dev_proc(&prototype, get_color_comp_index));
1255
32.2k
    set_dev_proc(dest, encode_color, dev_proc(&prototype, encode_color));
1256
32.2k
    set_dev_proc(dest, decode_color, dev_proc(&prototype, decode_color));
1257
32.2k
    set_dev_proc(dest, fill_rectangle_hl_color, dev_proc(&prototype, fill_rectangle_hl_color));
1258
32.2k
    set_dev_proc(dest, include_color_space, dev_proc(&prototype, include_color_space));
1259
32.2k
    set_dev_proc(dest, fill_linear_color_scanline, dev_proc(&prototype, fill_linear_color_scanline));
1260
32.2k
    set_dev_proc(dest, fill_linear_color_trapezoid, dev_proc(&prototype, fill_linear_color_trapezoid));
1261
32.2k
    set_dev_proc(dest, fill_linear_color_triangle, dev_proc(&prototype, fill_linear_color_triangle));
1262
32.2k
    set_dev_proc(dest, update_spot_equivalent_colors, dev_proc(&prototype, update_spot_equivalent_colors));
1263
32.2k
    set_dev_proc(dest, ret_devn_params, dev_proc(&prototype, ret_devn_params));
1264
32.2k
    set_dev_proc(dest, fillpage, dev_proc(&prototype, fillpage));
1265
32.2k
    set_dev_proc(dest, push_transparency_state, dev_proc(&prototype, push_transparency_state));
1266
32.2k
    set_dev_proc(dest, pop_transparency_state, dev_proc(&prototype, pop_transparency_state));
1267
32.2k
    set_dev_proc(dest, dev_spec_op, dev_proc(&prototype, dev_spec_op));
1268
32.2k
    set_dev_proc(dest, get_profile, dev_proc(&prototype, get_profile));
1269
32.2k
    set_dev_proc(dest, strip_copy_rop2, dev_proc(&prototype, strip_copy_rop2));
1270
32.2k
    set_dev_proc(dest, strip_tile_rect_devn, dev_proc(&prototype, strip_tile_rect_devn));
1271
32.2k
    set_dev_proc(dest, process_page, dev_proc(&prototype, process_page));
1272
32.2k
    set_dev_proc(dest, transform_pixel_region, dev_proc(&prototype, transform_pixel_region));
1273
32.2k
    set_dev_proc(dest, fill_stroke_path, dev_proc(&prototype, fill_stroke_path));
1274
32.2k
    set_dev_proc(dest, lock_pattern, dev_proc(&prototype, lock_pattern));
1275
1276
    /*
1277
     * We absolutely must set the 'set_graphics_type_tag' to the default subclass one
1278
     * even if the subclassed device is using the default. This is because the
1279
     * default implementation sets a flag in the device structure, and if we
1280
     * copy the default method, we'll end up setting the flag in the subclassing device
1281
     * instead of the subclassed device!
1282
     */
1283
32.2k
    set_dev_proc(dest, set_graphics_type_tag, dev_proc(&prototype, set_graphics_type_tag));
1284
1285
    /* These are the routines whose existence is checked against the default at
1286
     * some point in the code. The code path differs when the device implements a
1287
     * method other than the default, so the subclassing device needs to ensure that
1288
     * if the subclassed device has one of these methods set to the default, we
1289
     * do not overwrite the default method.
1290
     */
1291
32.2k
    if (dev_proc(src, fill_mask) != gx_default_fill_mask)
1292
20.1k
        set_dev_proc(dest, fill_mask, dev_proc(&prototype, fill_mask));
1293
32.2k
    if (dev_proc(src, begin_transparency_group) != gx_default_begin_transparency_group)
1294
0
        set_dev_proc(dest, begin_transparency_group, dev_proc(&prototype, begin_transparency_group));
1295
32.2k
    if (dev_proc(src, end_transparency_group) != gx_default_end_transparency_group)
1296
0
        set_dev_proc(dest, end_transparency_group, dev_proc(&prototype, end_transparency_group));
1297
32.2k
    if (dev_proc(src, put_image) != gx_default_put_image)
1298
0
        set_dev_proc(dest, put_image, dev_proc(&prototype, put_image));
1299
32.2k
    if (dev_proc(src, copy_planes) != gx_default_copy_planes)
1300
0
        set_dev_proc(dest, copy_planes, dev_proc(&prototype, copy_planes));
1301
32.2k
    if (dev_proc(src, copy_alpha_hl_color) != gx_default_no_copy_alpha_hl_color)
1302
0
        set_dev_proc(dest, copy_alpha_hl_color, dev_proc(&prototype, copy_alpha_hl_color));
1303
1304
32.2k
    return 0;
1305
32.2k
}
1306
1307
int gx_device_subclass(gx_device *dev_to_subclass, gx_device *new_prototype, unsigned int private_data_size)
1308
32.3k
{
1309
32.3k
    gx_device *child_dev;
1310
32.3k
    void *psubclass_data;
1311
32.3k
    gs_memory_struct_type_t *a_std = NULL, *b_std = NULL;
1312
32.3k
    int dynamic = dev_to_subclass->stype_is_dynamic;
1313
32.3k
    char *ptr, *ptr1;
1314
1315
    /* If this happens we are stuffed, as there is no way to get hold
1316
     * of the original device's stype structure, which means we cannot
1317
     * allocate a replacement structure. Abort if so.
1318
     * Also abort if the new_prototype device struct is too large.
1319
     */
1320
32.3k
    if (!dev_to_subclass->stype ||
1321
32.2k
        dev_to_subclass->stype->ssize < new_prototype->params_size)
1322
6
        return_error(gs_error_VMerror);
1323
1324
    /* We make a 'stype' structure for our new device, and copy the old stype into it
1325
     * This means our new device will always have the 'stype_is_dynamic' flag set
1326
     */
1327
32.2k
    a_std = (gs_memory_struct_type_t *)
1328
32.2k
        gs_alloc_bytes_immovable(dev_to_subclass->memory->non_gc_memory, sizeof(*a_std),
1329
32.2k
                                 "gs_device_subclass(stype)");
1330
32.2k
    if (!a_std)
1331
0
        return_error(gs_error_VMerror);
1332
32.2k
    *a_std = *dev_to_subclass->stype;
1333
32.2k
    a_std->ssize = dev_to_subclass->params_size;
1334
1335
32.2k
    if (!dynamic) {
1336
29.3k
        b_std = (gs_memory_struct_type_t *)
1337
29.3k
            gs_alloc_bytes_immovable(dev_to_subclass->memory->non_gc_memory, sizeof(*b_std),
1338
29.3k
                                     "gs_device_subclass(stype)");
1339
29.3k
        if (!b_std) {
1340
0
            gs_free_const_object(dev_to_subclass->memory->non_gc_memory, a_std, "gs_device_subclass(stype)");
1341
0
            return_error(gs_error_VMerror);
1342
0
        }
1343
29.3k
    }
1344
1345
    /* Allocate a device structure for the new child device */
1346
32.2k
    child_dev = gs_alloc_struct_immovable(dev_to_subclass->memory->stable_memory, gx_device, a_std,
1347
32.2k
                                        "gs_device_subclass(device)");
1348
32.2k
    if (child_dev == 0) {
1349
0
        gs_free_const_object(dev_to_subclass->memory->non_gc_memory, a_std, "gs_device_subclass(stype)");
1350
0
        gs_free_const_object(dev_to_subclass->memory->non_gc_memory, b_std, "gs_device_subclass(stype)");
1351
0
        return_error(gs_error_VMerror);
1352
0
    }
1353
1354
    /* Make sure all methods are filled in, note this won't work for a forwarding device
1355
     * so forwarding devices will have to be filled in before being subclassed. This doesn't fill
1356
     * in the fill_rectangle proc, that gets done in the ultimate device's open proc.
1357
     */
1358
32.2k
    gx_device_fill_in_procs(dev_to_subclass);
1359
32.2k
    memcpy(child_dev, dev_to_subclass, dev_to_subclass->stype->ssize);
1360
32.2k
    child_dev->stype = a_std;
1361
32.2k
    child_dev->stype_is_dynamic = 1;
1362
1363
    /* At this point, the only counted reference to the child is from its parent, and we need it to use the right allocator */
1364
32.2k
    rc_init(child_dev, dev_to_subclass->memory->stable_memory, 1);
1365
1366
32.2k
    psubclass_data = (void *)gs_alloc_bytes(dev_to_subclass->memory->non_gc_memory, private_data_size, "subclass memory for subclassing device");
1367
32.2k
    if (psubclass_data == 0){
1368
0
        gs_free_const_object(dev_to_subclass->memory->non_gc_memory, b_std, "gs_device_subclass(stype)");
1369
        /* We *don't* want to run the finalize routine. This would free the stype and
1370
         * properly handle the icc_struct and PageList, but for devices with a custom
1371
         * finalize (eg psdcmyk) it might also free memory it had allocated, and we're
1372
         * still pointing at that memory in the parent.
1373
         */
1374
0
        a_std->finalize = NULL;
1375
0
        gs_set_object_type(dev_to_subclass->memory->stable_memory, child_dev, a_std);
1376
0
        gs_free_object(dev_to_subclass->memory->stable_memory, child_dev, "free subclass memory for subclassing device");
1377
0
        gs_free_const_object(dev_to_subclass->memory->non_gc_memory, a_std, "gs_device_subclass(stype)");
1378
0
        return_error(gs_error_VMerror);
1379
0
    }
1380
32.2k
    memset(psubclass_data, 0x00, private_data_size);
1381
1382
32.2k
    gx_copy_device_procs(dev_to_subclass, child_dev, new_prototype);
1383
32.2k
    dev_to_subclass->finalize = new_prototype->finalize;
1384
32.2k
    dev_to_subclass->dname = new_prototype->dname;
1385
32.2k
    if (dev_to_subclass->icc_struct)
1386
32.2k
        rc_increment(dev_to_subclass->icc_struct);
1387
32.2k
    if (dev_to_subclass->PageList)
1388
32.2k
        rc_increment(dev_to_subclass->PageList);
1389
32.2k
    if (dev_to_subclass->NupControl)
1390
32.2k
        rc_increment(dev_to_subclass->NupControl);
1391
1392
32.2k
    dev_to_subclass->page_procs = new_prototype->page_procs;
1393
32.2k
    gx_subclass_fill_in_page_procs(dev_to_subclass);
1394
1395
    /* In case the new device we're creating has already been initialised, copy
1396
     * its additional data.
1397
     */
1398
32.2k
    ptr = ((char *)dev_to_subclass) + sizeof(gx_device);
1399
32.2k
    ptr1 = ((char *)new_prototype) + sizeof(gx_device);
1400
32.2k
    memcpy(ptr, ptr1, new_prototype->params_size - sizeof(gx_device));
1401
1402
    /* If the original device's stype structure was dynamically allocated, we need
1403
     * to 'fixup' the contents, it's procs need to point to the new device's procs
1404
     * for instance.
1405
     */
1406
32.2k
    if (dynamic) {
1407
2.93k
        if (new_prototype->stype) {
1408
2.93k
            b_std = (gs_memory_struct_type_t *)dev_to_subclass->stype;
1409
2.93k
            *b_std = *new_prototype->stype;
1410
2.93k
            b_std->ssize = a_std->ssize;
1411
2.93k
            dev_to_subclass->stype_is_dynamic = 1;
1412
2.93k
        } else {
1413
0
            gs_free_const_object(child_dev->memory->non_gc_memory, dev_to_subclass->stype,
1414
0
                             "unsubclass");
1415
0
            dev_to_subclass->stype = NULL;
1416
0
            b_std = (gs_memory_struct_type_t *)new_prototype->stype;
1417
0
            dev_to_subclass->stype_is_dynamic = 0;
1418
0
        }
1419
2.93k
    }
1420
29.3k
    else {
1421
29.3k
        *b_std = *new_prototype->stype;
1422
29.3k
        b_std->ssize = a_std->ssize;
1423
29.3k
        dev_to_subclass->stype_is_dynamic = 1;
1424
29.3k
    }
1425
32.2k
    dev_to_subclass->stype = b_std;
1426
    /* We have to patch up the "type" parameters that the memory manage/garbage
1427
     * collector will use, as well.
1428
     */
1429
32.2k
    gs_set_object_type(child_dev->memory, dev_to_subclass, b_std);
1430
1431
32.2k
    dev_to_subclass->subclass_data = psubclass_data;
1432
32.2k
    dev_to_subclass->child = child_dev;
1433
32.2k
    if (child_dev->parent) {
1434
0
        dev_to_subclass->parent = child_dev->parent;
1435
0
        child_dev->parent->child = dev_to_subclass;
1436
0
    }
1437
32.2k
    if (child_dev->child) {
1438
0
        child_dev->child->parent = child_dev;
1439
0
    }
1440
32.2k
    child_dev->parent = dev_to_subclass;
1441
1442
32.2k
    return 0;
1443
32.2k
}
1444
1445
void gx_device_unsubclass(gx_device *dev)
1446
0
{
1447
0
    generic_subclass_data *psubclass_data;
1448
0
    gx_device *parent, *child;
1449
0
    gs_memory_struct_type_t *a_std = 0, *b_std = 0;
1450
0
    int dynamic, ref_count;
1451
0
    gs_memory_t *rcmem;
1452
1453
    /* This should not happen... */
1454
0
    if (!dev)
1455
0
        return;
1456
1457
0
    ref_count = dev->rc.ref_count;
1458
0
    rcmem = dev->rc.memory;
1459
1460
0
    child = dev->child;
1461
0
    psubclass_data = (generic_subclass_data *)dev->subclass_data;
1462
0
    parent = dev->parent;
1463
0
    dynamic = dev->stype_is_dynamic;
1464
1465
    /* We need to account for the fact that we are removing ourselves from
1466
     * the device chain after a clist device has been pushed, due to a
1467
     * compositor action. Since we patched the clist 'composite'
1468
     * method (and target device) when it was pushed.
1469
     * A point to note; we *don't* want to change the forwarding device's
1470
     * 'target', because when we copy the child up to replace 'this' device
1471
     * we do still want the forwarding device to point here. NB its the *child*
1472
     * device that goes away.
1473
     */
1474
0
    if (psubclass_data != NULL && psubclass_data->forwarding_dev != NULL && psubclass_data->saved_compositor_method)
1475
0
        psubclass_data->forwarding_dev->procs.composite = psubclass_data->saved_compositor_method;
1476
1477
    /* If ths device's stype is dynamically allocated, keep a copy of it
1478
     * in case we might need it.
1479
     */
1480
0
    if (dynamic) {
1481
0
        a_std = (gs_memory_struct_type_t *)dev->stype;
1482
0
        if (child)
1483
0
            *a_std = *child->stype;
1484
0
    }
1485
1486
    /* If ths device has any private storage, free it now */
1487
0
    if (psubclass_data)
1488
0
        gs_free_object(dev->memory->non_gc_memory, psubclass_data, "gx_device_unsubclass");
1489
1490
    /* Copy the child device into ths device's memory */
1491
0
    if (child) {
1492
0
        b_std = (gs_memory_struct_type_t *)dev->stype;
1493
0
        rc_decrement(dev->icc_struct, "unsubclass device");
1494
0
        rc_increment(child->icc_struct);
1495
0
        memcpy(dev, child, child->stype->ssize);
1496
        /* Patch back the 'stype' in the memory manager */
1497
0
        gs_set_object_type(child->memory, dev, b_std);
1498
1499
0
        dev->stype = b_std;
1500
        /* The reference count of the subclassing device may have been
1501
         * changed (eg graphics states pointing to it) after we subclassed
1502
         * the device. We need to ensure that we do not overwrite this
1503
         * when we copy back the subclassed device.
1504
         */
1505
0
        dev->rc.ref_count = ref_count;
1506
0
        dev->rc.memory = rcmem;
1507
1508
        /* If we have a chain of devices, make sure the chain beyond the
1509
         * device we're unsubclassing doesn't get broken, we need to
1510
         * detach the lower chain and reattach it at the new highest level.
1511
         */
1512
0
        if (child->child)
1513
0
            child->child->parent = dev;
1514
0
        child->parent->child = child->child;
1515
0
    }
1516
1517
    /* How can we have a subclass device with no child ? Simples; when we
1518
     * hit the end of job restore, the devices are not freed in device
1519
     * chain order. To make sure we don't end up following stale pointers,
1520
     * when a device is freed we remove it from the chain and update
1521
     * any dangling pointers to NULL. When we later free the remaining
1522
     * devices it's possible that their child pointer can then be NULL.
1523
     */
1524
0
    if (child) {
1525
        /* We cannot afford to free the child device if its stype is not
1526
         * dynamic because we can't 'null' the finalise routine, and we
1527
         * cannot permit the device to be finalised because we have copied
1528
         * it up one level, not discarded it. (This shouldn't happen! Child
1529
         * devices are always created with a dynamic stype.) If this ever
1530
         * happens garbage collecton will eventually clean up the memory.
1531
         */
1532
0
        if (child->stype_is_dynamic) {
1533
            /* Make sure that nothing will try to follow the device chain,
1534
             * just security here. */
1535
0
            child->parent = NULL;
1536
0
            child->child = NULL;
1537
1538
            /* We *don't* want to run the finalize routine. This would free
1539
             * the stype and properly handle the icc_struct and PageList,
1540
             * but for devices with a custom finalize (eg psdcmyk) it might
1541
             * also free memory it had allocated, and we're still pointing
1542
             * at that memory in the parent. The indirection through a
1543
             * variable is just to get rid of const warnings.
1544
             */
1545
0
            b_std = (gs_memory_struct_type_t *)child->stype;
1546
0
            gs_free_const_object(dev->memory->non_gc_memory, b_std, "gs_device_unsubclass(stype)");
1547
            /* Make this into a generic device */
1548
0
            child->stype = &st_device;
1549
0
            child->stype_is_dynamic = false;
1550
1551
            /* We can't simply discard the child device, because there may be references to it elsewhere,
1552
               but equally, we really don't want it doing anything, so set the procs so actions are just discarded.
1553
             */
1554
0
            gx_copy_device_procs(child, (gx_device *)&gs_null_device, (gx_device *)&gs_null_device);
1555
1556
            /* Having changed the stype, we need to make sure the memory
1557
             * manager uses it. It keeps a copy in its own data structure,
1558
             * and would use that copy, which would mean it would call the
1559
             * finalize routine that we just patched out.
1560
             */
1561
0
            gs_set_object_type(dev->memory->stable_memory, child, child->stype);
1562
0
            child->finalize = NULL;
1563
            /* Now (finally) free the child memory */
1564
0
            rc_decrement(child, "gx_device_unsubclass(device)");
1565
0
        }
1566
0
    }
1567
0
    dev->parent = parent;
1568
1569
    /* If this device has a dynamic stype, we wnt to keep using it, but we copied
1570
     * the stype pointer from the child when we copied the rest of the device. So
1571
     * we update the stype pointer with the saved pointer to this device's stype.
1572
     */
1573
0
    if (dynamic) {
1574
0
        dev->stype = a_std;
1575
0
        dev->stype_is_dynamic = 1;
1576
0
    } else {
1577
0
        dev->stype_is_dynamic = 0;
1578
0
    }
1579
0
}
1580
1581
int gx_update_from_subclass(gx_device *dev)
1582
220k
{
1583
220k
    if (!dev->child)
1584
0
        return 0;
1585
1586
220k
    memcpy(&dev->color_info, &dev->child->color_info, sizeof(gx_device_color_info));
1587
220k
    memcpy(&dev->cached_colors, &dev->child->cached_colors, sizeof(gx_device_cached_colors_t));
1588
220k
    dev->max_fill_band = dev->child->max_fill_band;
1589
220k
    dev->width = dev->child->width;
1590
220k
    dev->height = dev->child->height;
1591
220k
    dev->pad = dev->child->pad;
1592
220k
    dev->log2_align_mod = dev->child->log2_align_mod;
1593
220k
    dev->max_fill_band = dev->child->max_fill_band;
1594
220k
    dev->num_planar_planes = dev->child->num_planar_planes;
1595
220k
    dev->LeadingEdge = dev->child->LeadingEdge;
1596
220k
    memcpy(&dev->ImagingBBox, &dev->child->ImagingBBox, sizeof(dev->child->ImagingBBox));
1597
220k
    dev->ImagingBBox_set = dev->child->ImagingBBox_set;
1598
220k
    memcpy(&dev->MediaSize, &dev->child->MediaSize, sizeof(dev->child->MediaSize));
1599
220k
    memcpy(&dev->HWResolution, &dev->child->HWResolution, sizeof(dev->child->HWResolution));
1600
220k
    memcpy(&dev->Margins, &dev->child->Margins, sizeof(dev->child->Margins));
1601
220k
    memcpy(&dev->HWMargins, &dev->child->HWMargins, sizeof(dev->child->HWMargins));
1602
220k
    dev->FirstPage = dev->child->FirstPage;
1603
220k
    dev->LastPage = dev->child->LastPage;
1604
220k
    dev->PageCount = dev->child->PageCount;
1605
220k
    dev->ShowpageCount = dev->child->ShowpageCount;
1606
220k
    dev->NumCopies = dev->child->NumCopies;
1607
220k
    dev->NumCopies_set = dev->child->NumCopies_set;
1608
220k
    dev->IgnoreNumCopies = dev->child->IgnoreNumCopies;
1609
220k
    dev->UseCIEColor = dev->child->UseCIEColor;
1610
220k
    dev->LockSafetyParams= dev->child->LockSafetyParams;
1611
220k
    dev->band_offset_x = dev->child->band_offset_y;
1612
220k
    dev->sgr = dev->child->sgr;
1613
220k
    dev->MaxPatternBitmap = dev->child->MaxPatternBitmap;
1614
220k
    dev->page_uses_transparency = dev->child->page_uses_transparency;
1615
220k
    memcpy(&dev->space_params, &dev->child->space_params, sizeof(gdev_space_params));
1616
220k
    dev->graphics_type_tag = dev->child->graphics_type_tag;
1617
1618
220k
    return 0;
1619
220k
}
1620
1621
int gx_subclass_composite(gx_device *dev, gx_device **pcdev, const gs_composite_t *pcte,
1622
    gs_gstate *pgs, gs_memory_t *memory, gx_device *cdev)
1623
0
{
1624
0
    pdf14_clist_device *p14dev;
1625
0
    generic_subclass_data *psubclass_data;
1626
0
    int code = 0;
1627
1628
0
    p14dev = (pdf14_clist_device *)dev;
1629
0
    psubclass_data = (generic_subclass_data *)p14dev->target->subclass_data;
1630
1631
0
    set_dev_proc(dev, composite, psubclass_data->saved_compositor_method);
1632
1633
0
    if (gs_is_pdf14trans_compositor(pcte) != 0 && strncmp(dev->dname, "pdf14clist", 10) == 0) {
1634
0
        const gs_pdf14trans_t * pdf14pct = (const gs_pdf14trans_t *) pcte;
1635
1636
0
        switch (pdf14pct->params.pdf14_op) {
1637
0
            case PDF14_POP_DEVICE:
1638
0
                {
1639
0
                    pdf14_clist_device *p14dev = (pdf14_clist_device *)dev;
1640
0
                    gx_device *subclass_device;
1641
1642
0
                    p14dev->target->color_info = p14dev->saved_target_color_info;
1643
0
                    if (p14dev->target->child) {
1644
0
                        p14dev->target->child->color_info = p14dev->saved_target_color_info;
1645
1646
0
                        set_dev_proc(p14dev->target->child, encode_color, p14dev->saved_target_encode_color);
1647
0
                        set_dev_proc(p14dev->target->child, decode_color, p14dev->saved_target_decode_color);
1648
0
                        set_dev_proc(p14dev->target->child, get_color_mapping_procs, p14dev->saved_target_get_color_mapping_procs);
1649
0
                        set_dev_proc(p14dev->target->child, get_color_comp_index, p14dev->saved_target_get_color_comp_index);
1650
0
                    }
1651
1652
0
                    pgs->get_cmap_procs = p14dev->save_get_cmap_procs;
1653
0
                    gx_set_cmap_procs(pgs, p14dev->target);
1654
1655
0
                    subclass_device = p14dev->target;
1656
0
                    p14dev->target = p14dev->target->child;
1657
1658
0
                    code = dev_proc(dev, composite)(dev, pcdev, pcte, pgs, memory, cdev);
1659
1660
0
                    p14dev->target = subclass_device;
1661
1662
                    /* We return 0, rather than 1, as we have not created
1663
                     * a new compositor that wraps dev. */
1664
0
                    if (code == 1)
1665
0
                        code = 0;
1666
0
                    return code;
1667
0
                }
1668
0
                break;
1669
0
            default:
1670
0
                code = dev_proc(dev, composite)(dev, pcdev, pcte, pgs, memory, cdev);
1671
0
                break;
1672
0
        }
1673
0
    } else {
1674
0
        code = dev_proc(dev, composite)(dev, pcdev, pcte, pgs, memory, cdev);
1675
0
    }
1676
0
    set_dev_proc(dev, composite, gx_subclass_composite);
1677
0
    return code;
1678
0
}
1679
1680
typedef enum
1681
{
1682
    transform_pixel_region_portrait,
1683
    transform_pixel_region_landscape,
1684
    transform_pixel_region_skew
1685
} transform_pixel_region_posture;
1686
1687
typedef struct gx_default_transform_pixel_region_state_s gx_default_transform_pixel_region_state_t;
1688
1689
typedef int (gx_default_transform_pixel_region_render_fn)(gx_device *dev, gx_default_transform_pixel_region_state_t *state, const unsigned char **buffer, int data_x, gx_cmapper_t *cmapper, const gs_gstate *pgs);
1690
1691
struct gx_default_transform_pixel_region_state_s
1692
{
1693
    gs_memory_t *mem;
1694
    gx_dda_fixed_point pixels;
1695
    gx_dda_fixed_point rows;
1696
    gs_int_rect clip;
1697
    int w;
1698
    int h;
1699
    int spp;
1700
    transform_pixel_region_posture posture;
1701
    gs_logical_operation_t lop;
1702
    byte *line;
1703
    gx_default_transform_pixel_region_render_fn *render;
1704
};
1705
1706
static void
1707
get_portrait_y_extent(gx_default_transform_pixel_region_state_t *state, int *iy, int *ih)
1708
6.23M
{
1709
6.23M
    fixed y0, y1;
1710
6.23M
    gx_dda_fixed row = state->rows.y;
1711
1712
6.23M
    y0 = dda_current(row);
1713
6.23M
    dda_next(row);
1714
6.23M
    y1 = dda_current(row);
1715
1716
6.23M
    if (y1 < y0) {
1717
486k
        fixed t = y1; y1 = y0; y0 = t;
1718
486k
    }
1719
1720
6.23M
    *iy = fixed2int_pixround_perfect(y0);
1721
6.23M
    *ih = fixed2int_pixround_perfect(y1) - *iy;
1722
6.23M
}
1723
1724
static void
1725
get_landscape_x_extent(gx_default_transform_pixel_region_state_t *state, int *ix, int *iw)
1726
10.2k
{
1727
10.2k
    fixed x0, x1;
1728
10.2k
    gx_dda_fixed row = state->rows.x;
1729
1730
10.2k
    x0 = dda_current(row);
1731
10.2k
    dda_next(row);
1732
10.2k
    x1 = dda_current(row);
1733
1734
10.2k
    if (x1 < x0) {
1735
0
        fixed t = x1; x1 = x0; x0 = t;
1736
0
    }
1737
1738
10.2k
    *ix = fixed2int_pixround_perfect(x0);
1739
10.2k
    *iw = fixed2int_pixround_perfect(x1) - *ix;
1740
10.2k
}
1741
1742
static void
1743
get_skew_extents(gx_default_transform_pixel_region_state_t *state, fixed *w, fixed *h)
1744
8.45k
{
1745
8.45k
    fixed x0, x1, y0, y1;
1746
8.45k
    gx_dda_fixed_point row = state->rows;
1747
1748
8.45k
    x0 = dda_current(row.x);
1749
8.45k
    y0 = dda_current(row.y);
1750
8.45k
    dda_next(row.x);
1751
8.45k
    dda_next(row.y);
1752
8.45k
    x1 = dda_current(row.x);
1753
8.45k
    y1 = dda_current(row.y);
1754
1755
8.45k
    *w = x1-x0;
1756
8.45k
    *h = y1-y0;
1757
8.45k
}
1758
1759
static int
1760
transform_pixel_region_render_portrait(gx_device *dev, gx_default_transform_pixel_region_state_t *state, const unsigned char **buffer, int data_x, gx_cmapper_t *cmapper, const gs_gstate *pgs)
1761
3.07M
{
1762
3.07M
    gs_logical_operation_t lop = state->lop;
1763
3.07M
    gx_dda_fixed_point pnext;
1764
3.07M
    int vci, vdi;
1765
3.07M
    int irun;     /* int x/rrun */
1766
3.07M
    int w = state->w;
1767
3.07M
    int h = state->h;
1768
3.07M
    int spp = state->spp;
1769
3.07M
    const byte *data = buffer[0] + data_x * spp;
1770
3.07M
    const byte *bufend = NULL;
1771
3.07M
    int code = 0;
1772
3.07M
    const byte *run = NULL;
1773
3.07M
    int k;
1774
3.07M
    gx_color_value *conc = &cmapper->conc[0];
1775
3.07M
    int to_rects;
1776
3.07M
    gx_cmapper_fn *mapper = cmapper->set_color;
1777
3.07M
    int minx, maxx;
1778
1779
3.07M
    if (h == 0)
1780
0
        return 0;
1781
1782
    /* Clip on Y */
1783
3.07M
    get_portrait_y_extent(state, &vci, &vdi);
1784
3.07M
    if (vci < state->clip.p.y)
1785
97.7k
        vdi += vci - state->clip.p.y, vci = state->clip.p.y;
1786
3.07M
    if (vci+vdi > state->clip.q.y)
1787
47.3k
        vdi = state->clip.q.y - vci;
1788
3.07M
    if (vdi <= 0)
1789
1.79M
        return 0;
1790
1791
1.27M
    pnext = state->pixels;
1792
1.27M
    dda_translate(pnext.x,  (-fixed_epsilon));
1793
1.27M
    irun = fixed2int_var_rounded(dda_current(pnext.x));
1794
1.27M
    if_debug5m('b', dev->memory, "[b]y=%d data_x=%d w=%d xt=%f yt=%f\n",
1795
1.27M
               vci, data_x, w, fixed2float(dda_current(pnext.x)), fixed2float(dda_current(pnext.y)));
1796
1.27M
    to_rects = (dev->color_info.depth != spp*8);
1797
1.27M
    if (to_rects == 0) {
1798
1.05M
        if (dev_proc(dev, dev_spec_op)(dev, gxdso_copy_color_is_fast, NULL, 0) <= 0)
1799
999k
            to_rects = 1;
1800
1.05M
    }
1801
1802
1.27M
    minx = state->clip.p.x;
1803
1.27M
    maxx = state->clip.q.x;
1804
1.27M
    bufend = data + w * spp;
1805
1.27M
    if (to_rects) {
1806
137M
        while (data < bufend) {
1807
            /* Find the length of the next run. It will either end when we hit
1808
             * the end of the source data, or when the pixel data differs. */
1809
136M
            run = data + spp;
1810
531M
            while (1) {
1811
531M
                dda_next(pnext.x);
1812
531M
                if (run >= bufend)
1813
1.22M
                    break;
1814
529M
                if (memcmp(run, data, spp))
1815
135M
                    break;
1816
394M
                run += spp;
1817
394M
            }
1818
            /* So we have a run of pixels from data to run that are all the same. */
1819
            /* This needs to be sped up */
1820
528M
            for (k = 0; k < spp; k++) {
1821
392M
                conc[k] = gx_color_value_from_byte(data[k]);
1822
392M
            }
1823
136M
            mapper(cmapper);
1824
            /* Fill the region between irun and fixed2int_var_rounded(pnext.x) */
1825
136M
            {
1826
136M
                int xi = irun;
1827
136M
                int wi = (irun = fixed2int_var_rounded(dda_current(pnext.x))) - xi;
1828
1829
136M
                if (wi < 0)
1830
10.0k
                    xi += wi, wi = -wi;
1831
136M
                if (xi < minx)
1832
32.1k
                    wi += xi - minx, xi = minx;
1833
136M
                if (xi + wi > maxx)
1834
89.3k
                    wi = maxx - xi;
1835
136M
                if (wi > 0)
1836
127M
                    code = gx_fill_rectangle_device_rop(xi, vci, wi, vdi,
1837
136M
                                                        &cmapper->devc, dev, lop);
1838
136M
            }
1839
136M
            if (code < 0)
1840
0
                goto err;
1841
136M
            data = run;
1842
136M
        }
1843
1.22M
    } else {
1844
55.5k
        int pending_left = irun;
1845
55.5k
        int pending_right;
1846
55.5k
        byte *out;
1847
55.5k
        int depth = spp;
1848
55.5k
        if (state->line == NULL) {
1849
461
            state->line = gs_alloc_bytes(state->mem,
1850
461
                                         (size_t)dev->width * depth,
1851
461
                                         "image line");
1852
461
            if (state->line == NULL)
1853
0
                return gs_error_VMerror;
1854
461
        }
1855
55.5k
        out = state->line;
1856
1857
55.5k
        if (minx < 0)
1858
0
            minx = 0;
1859
55.5k
        if (maxx > dev->width)
1860
0
            maxx = dev->width;
1861
1862
55.5k
        if (pending_left < minx)
1863
214
            pending_left = minx;
1864
55.3k
        else if (pending_left > maxx)
1865
1
            pending_left = maxx;
1866
55.5k
        pending_right = pending_left;
1867
1868
6.10M
        while (data < bufend) {
1869
            /* Find the length of the next run. It will either end when we hit
1870
             * the end of the source data, or when the pixel data differs. */
1871
6.04M
            run = data + spp;
1872
28.3M
            while (1) {
1873
28.3M
                dda_next(pnext.x);
1874
28.3M
                if (run >= bufend)
1875
55.5k
                    break;
1876
28.3M
                if (memcmp(run, data, spp))
1877
5.99M
                    break;
1878
22.3M
                run += spp;
1879
22.3M
            }
1880
            /* So we have a run of pixels from data to run that are all the same. */
1881
            /* This needs to be sped up */
1882
18.7M
            for (k = 0; k < spp; k++) {
1883
12.6M
                conc[k] = gx_color_value_from_byte(data[k]);
1884
12.6M
            }
1885
6.04M
            mapper(cmapper);
1886
            /* Fill the region between irun and fixed2int_var_rounded(pnext.x) */
1887
6.04M
            {
1888
6.04M
                int xi = irun;
1889
6.04M
                int wi = (irun = fixed2int_var_rounded(dda_current(pnext.x))) - xi;
1890
1891
6.04M
                if (wi < 0)
1892
0
                    xi += wi, wi = -wi;
1893
1894
6.04M
                if (xi < minx)
1895
214
                    wi += xi - minx, xi = minx;
1896
6.04M
                if (xi + wi > maxx)
1897
230k
                    wi = maxx - xi;
1898
1899
6.04M
                if (wi > 0) {
1900
5.53M
                    if (color_is_pure(&cmapper->devc)) {
1901
5.53M
                        gx_color_index color = cmapper->devc.colors.pure;
1902
5.53M
                        int xii = xi * spp;
1903
1904
5.53M
                        if (pending_left > xi)
1905
0
                            pending_left = xi;
1906
5.53M
                        else
1907
5.53M
                            pending_right = xi + wi;
1908
27.2M
                        do {
1909
                            /* Excuse the double shifts below, that's to stop the
1910
                             * C compiler complaining if the color index type is
1911
                             * 32 bits. */
1912
27.2M
                            switch(depth)
1913
27.2M
                            {
1914
0
                            case 8: out[xii++] = ((color>>28)>>28) & 0xff;
1915
0
                            case 7: out[xii++] = ((color>>24)>>24) & 0xff;
1916
0
                            case 6: out[xii++] = ((color>>24)>>16) & 0xff;
1917
0
                            case 5: out[xii++] = ((color>>24)>>8) & 0xff;
1918
0
                            case 4: out[xii++] = (color>>24) & 0xff;
1919
11.8M
                            case 3: out[xii++] = (color>>16) & 0xff;
1920
11.8M
                            case 2: out[xii++] = (color>>8) & 0xff;
1921
27.2M
                            case 1: out[xii++] = color & 0xff;
1922
27.2M
                            }
1923
27.2M
                        } while (--wi != 0);
1924
5.53M
                    } else {
1925
0
                        if (pending_left != pending_right) {
1926
0
                            code = dev_proc(dev, copy_color)(dev, out, pending_left, 0, 0, pending_left, vci, pending_right - pending_left, vdi);
1927
0
                            if (code < 0)
1928
0
                                goto err;
1929
0
                        }
1930
0
                        pending_left = pending_right = xi + (pending_left > xi ? 0 : wi);
1931
0
                        code = gx_fill_rectangle_device_rop(xi, vci, wi, vdi,
1932
0
                                                            &cmapper->devc, dev, lop);
1933
0
                    }
1934
5.53M
                }
1935
6.04M
                if (code < 0)
1936
0
                    goto err;
1937
6.04M
            }
1938
6.04M
            data = run;
1939
6.04M
        }
1940
55.5k
        if (pending_left != pending_right) {
1941
55.5k
            code = dev_proc(dev, copy_color)(dev, out, pending_left, 0, 0, pending_left, vci, pending_right - pending_left, vdi);
1942
55.5k
            if (code < 0)
1943
0
                goto err;
1944
55.5k
        }
1945
55.5k
    }
1946
1.27M
    return 1;
1947
    /* Save position if error, in case we resume. */
1948
0
err:
1949
0
    buffer[0] = run;
1950
0
    return code;
1951
1.27M
}
1952
1953
static int
1954
transform_pixel_region_render_landscape(gx_device *dev, gx_default_transform_pixel_region_state_t *state, const unsigned char **buffer, int data_x, gx_cmapper_t *cmapper, const gs_gstate *pgs)
1955
5.14k
{
1956
5.14k
    gs_logical_operation_t lop = state->lop;
1957
5.14k
    gx_dda_fixed_point pnext;
1958
5.14k
    int vci, vdi;
1959
5.14k
    int irun;     /* int x/rrun */
1960
5.14k
    int w = state->w;
1961
5.14k
    int h = state->h;
1962
5.14k
    int spp = state->spp;
1963
5.14k
    const byte *data = buffer[0] + data_x * spp;
1964
5.14k
    const byte *bufend = NULL;
1965
5.14k
    int code = 0;
1966
5.14k
    const byte *run;
1967
5.14k
    int k;
1968
5.14k
    gx_color_value *conc = &cmapper->conc[0];
1969
5.14k
    int to_rects;
1970
5.14k
    gx_cmapper_fn *mapper = cmapper->set_color;
1971
5.14k
    int miny, maxy;
1972
1973
5.14k
    if (h == 0)
1974
0
        return 0;
1975
1976
    /* Clip on X */
1977
5.14k
    get_landscape_x_extent(state, &vci, &vdi);
1978
5.14k
    if (vci < state->clip.p.x)
1979
0
        vdi += vci - state->clip.p.x, vci = state->clip.p.x;
1980
5.14k
    if (vci+vdi > state->clip.q.x)
1981
0
        vdi = state->clip.q.x - vci;
1982
5.14k
    if (vdi <= 0)
1983
795
        return 0;
1984
1985
4.35k
    pnext = state->pixels;
1986
4.35k
    dda_translate(pnext.x,  (-fixed_epsilon));
1987
4.35k
    irun = fixed2int_var_rounded(dda_current(pnext.y));
1988
4.35k
    if_debug5m('b', dev->memory, "[b]y=%d data_x=%d w=%d xt=%f yt=%f\n",
1989
4.35k
               vci, data_x, w, fixed2float(dda_current(pnext.x)), fixed2float(dda_current(pnext.y)));
1990
4.35k
    to_rects = (dev->color_info.depth != spp*8);
1991
4.35k
    if (to_rects == 0) {
1992
4.35k
        if (dev_proc(dev, dev_spec_op)(dev, gxdso_copy_color_is_fast, NULL, 0) <= 0)
1993
4.35k
            to_rects = 1;
1994
4.35k
    }
1995
1996
4.35k
    miny = state->clip.p.y;
1997
4.35k
    maxy = state->clip.q.y;
1998
4.35k
    bufend = data + w * spp;
1999
25.8k
    while (data < bufend) {
2000
        /* Find the length of the next run. It will either end when we hit
2001
         * the end of the source data, or when the pixel data differs. */
2002
21.4k
        run = data + spp;
2003
21.5k
        while (1) {
2004
21.5k
            dda_next(pnext.y);
2005
21.5k
            if (run >= bufend)
2006
4.35k
                break;
2007
17.1k
            if (memcmp(run, data, spp))
2008
17.1k
                break;
2009
68
            run += spp;
2010
68
        }
2011
        /* So we have a run of pixels from data to run that are all the same. */
2012
        /* This needs to be sped up */
2013
85.8k
        for (k = 0; k < spp; k++) {
2014
64.4k
            conc[k] = gx_color_value_from_byte(data[k]);
2015
64.4k
        }
2016
21.4k
        mapper(cmapper);
2017
        /* Fill the region between irun and fixed2int_var_rounded(pnext.y) */
2018
21.4k
        {              /* 90 degree rotated rectangle */
2019
21.4k
            int yi = irun;
2020
21.4k
            int hi = (irun = fixed2int_var_rounded(dda_current(pnext.y))) - yi;
2021
2022
21.4k
            if (hi < 0)
2023
21.4k
                yi += hi, hi = -hi;
2024
21.4k
            if (yi < miny)
2025
4.20k
                hi += yi - miny, yi = miny;
2026
21.4k
            if (yi + hi > maxy)
2027
4.17k
                hi = maxy - yi;
2028
21.4k
            if (hi > 0)
2029
16.7k
                code = gx_fill_rectangle_device_rop(vci, yi, vdi, hi,
2030
21.4k
                                                    &cmapper->devc, dev, lop);
2031
21.4k
        }
2032
21.4k
        if (code < 0)
2033
0
            goto err;
2034
21.4k
        data = run;
2035
21.4k
    }
2036
4.35k
    return 1;
2037
    /* Save position if error, in case we resume. */
2038
0
err:
2039
0
    buffer[0] = run;
2040
0
    return code;
2041
4.35k
}
2042
2043
static int
2044
transform_pixel_region_render_skew(gx_device *dev, gx_default_transform_pixel_region_state_t *state, const unsigned char **buffer, int data_x, gx_cmapper_t *cmapper, const gs_gstate *pgs)
2045
8.45k
{
2046
8.45k
    gs_logical_operation_t lop = state->lop;
2047
8.45k
    gx_dda_fixed_point pnext;
2048
8.45k
    fixed xprev, yprev;
2049
8.45k
    fixed pdyx, pdyy;   /* edge of parallelogram */
2050
8.45k
    int w = state->w;
2051
8.45k
    int h = state->h;
2052
8.45k
    int spp = state->spp;
2053
8.45k
    const byte *data = buffer[0] + data_x * spp;
2054
8.45k
    fixed xpos;     /* x ditto */
2055
8.45k
    fixed ypos;     /* y ditto */
2056
8.45k
    const byte *bufend = data + w * spp;
2057
8.45k
    int code = 0;
2058
8.45k
    int k;
2059
8.45k
    byte initial_run[GX_DEVICE_COLOR_MAX_COMPONENTS] = { 0 };
2060
8.45k
    const byte *prev = &initial_run[0];
2061
8.45k
    gx_cmapper_fn *mapper = cmapper->set_color;
2062
8.45k
    gx_color_value *conc = &cmapper->conc[0];
2063
2064
8.45k
    if (h == 0)
2065
0
        return 0;
2066
8.45k
    pnext = state->pixels;
2067
8.45k
    get_skew_extents(state, &pdyx, &pdyy);
2068
8.45k
    dda_translate(pnext.x,  (-fixed_epsilon));
2069
8.45k
    xprev = dda_current(pnext.x);
2070
8.45k
    yprev = dda_current(pnext.y);
2071
8.45k
    if_debug4m('b', dev->memory, "[b]y=? data_x=%d w=%d xt=%f yt=%f\n",
2072
8.45k
               data_x, w, fixed2float(xprev), fixed2float(yprev));
2073
8.45k
    initial_run[0] = ~data[0];  /* Force intial setting */
2074
2.18M
    while (data < bufend) {
2075
2.17M
        dda_next(pnext.x);
2076
2.17M
        dda_next(pnext.y);
2077
2.17M
        xpos = dda_current(pnext.x);
2078
2.17M
        ypos = dda_current(pnext.y);
2079
2080
2.17M
        if (memcmp(prev, data, spp) != 0)
2081
1.00M
        {
2082
            /* This needs to be sped up */
2083
3.12M
            for (k = 0; k < spp; k++) {
2084
2.11M
                conc[k] = gx_color_value_from_byte(data[k]);
2085
2.11M
            }
2086
1.00M
            mapper(cmapper);
2087
1.00M
        }
2088
        /* Fill the region between */
2089
        /* xprev/yprev and xpos/ypos */
2090
        /* Parallelogram */
2091
2.17M
        code = (*dev_proc(dev, fill_parallelogram))
2092
2.17M
                    (dev, xprev, yprev, xpos - xprev, ypos - yprev, pdyx, pdyy,
2093
2.17M
                     &cmapper->devc, lop);
2094
2.17M
        xprev = xpos;
2095
2.17M
        yprev = ypos;
2096
2.17M
        if (code < 0)
2097
0
            goto err;
2098
2.17M
        prev = data;
2099
2.17M
        data += spp;
2100
2.17M
    }
2101
8.45k
    return 1;
2102
    /* Save position if error, in case we resume. */
2103
0
err:
2104
    /* Only set buffer[0] if we've managed to set prev to something valid. */
2105
0
    if (prev != &initial_run[0]) buffer[0] = prev;
2106
0
    return code;
2107
8.45k
}
2108
2109
static int
2110
gx_default_transform_pixel_region_begin(gx_device *dev, int w, int h, int spp,
2111
                             const gx_dda_fixed_point *pixels, const gx_dda_fixed_point *rows,
2112
                             const gs_int_rect *clip, gs_logical_operation_t lop,
2113
                             gx_default_transform_pixel_region_state_t **statep)
2114
144k
{
2115
144k
    gx_default_transform_pixel_region_state_t *state;
2116
144k
    gs_memory_t *mem = dev->memory->non_gc_memory;
2117
2118
144k
    *statep = state = (gx_default_transform_pixel_region_state_t *)gs_alloc_bytes(mem, sizeof(gx_default_transform_pixel_region_state_t), "gx_default_transform_pixel_region_state_t");
2119
144k
    if (state == NULL)
2120
0
        return gs_error_VMerror;
2121
144k
    state->mem = mem;
2122
144k
    state->rows = *rows;
2123
144k
    state->pixels = *pixels;
2124
144k
    state->clip = *clip;
2125
144k
    state->w = w;
2126
144k
    state->h = h;
2127
144k
    state->spp = spp;
2128
144k
    state->lop = lop;
2129
144k
    state->line = NULL;
2130
2131
    /* FIXME: Consider sheers here too. Probably happens rarely enough not to be worth it. */
2132
144k
    if (rows->x.step.dQ == 0 && rows->x.step.dR == 0 && pixels->y.step.dQ == 0 && pixels->y.step.dR == 0)
2133
143k
        state->posture = transform_pixel_region_portrait;
2134
765
    else if (rows->y.step.dQ == 0 && rows->y.step.dR == 0 && pixels->x.step.dQ == 0 && pixels->x.step.dR == 0)
2135
585
        state->posture = transform_pixel_region_landscape;
2136
180
    else
2137
180
        state->posture = transform_pixel_region_skew;
2138
2139
144k
    if (state->posture == transform_pixel_region_portrait)
2140
143k
        state->render = transform_pixel_region_render_portrait;
2141
765
    else if (state->posture == transform_pixel_region_landscape)
2142
585
        state->render = transform_pixel_region_render_landscape;
2143
180
    else
2144
180
        state->render = transform_pixel_region_render_skew;
2145
2146
144k
    return 0;
2147
144k
}
2148
2149
static void
2150
step_to_next_line(gx_default_transform_pixel_region_state_t *state)
2151
3.16M
{
2152
3.16M
    fixed x = dda_current(state->rows.x);
2153
3.16M
    fixed y = dda_current(state->rows.y);
2154
2155
3.16M
    dda_next(state->rows.x);
2156
3.16M
    dda_next(state->rows.y);
2157
3.16M
    x = dda_current(state->rows.x) - x;
2158
3.16M
    y = dda_current(state->rows.y) - y;
2159
3.16M
    dda_translate(state->pixels.x, x);
2160
3.16M
    dda_translate(state->pixels.y, y);
2161
3.16M
}
2162
2163
static int
2164
gx_default_transform_pixel_region_data_needed(gx_device *dev, gx_default_transform_pixel_region_state_t *state)
2165
3.16M
{
2166
3.16M
    if (state->posture == transform_pixel_region_portrait) {
2167
3.15M
        int iy, ih;
2168
2169
3.15M
        get_portrait_y_extent(state, &iy, &ih);
2170
2171
3.15M
        if (iy + ih < state->clip.p.y || iy >= state->clip.q.y) {
2172
            /* Skip this line. */
2173
80.0k
            step_to_next_line(state);
2174
80.0k
            return 0;
2175
80.0k
        }
2176
3.15M
    } else if (state->posture == transform_pixel_region_landscape) {
2177
5.14k
        int ix, iw;
2178
2179
5.14k
        get_landscape_x_extent(state, &ix, &iw);
2180
2181
5.14k
        if (ix + iw < state->clip.p.x || ix >= state->clip.q.x) {
2182
            /* Skip this line. */
2183
0
            step_to_next_line(state);
2184
0
            return 0;
2185
0
        }
2186
5.14k
    }
2187
2188
3.08M
    return 1;
2189
3.16M
}
2190
2191
static int
2192
gx_default_transform_pixel_region_process_data(gx_device *dev, gx_default_transform_pixel_region_state_t *state, const unsigned char **buffer, int data_x, gx_cmapper_t *cmapper, const gs_gstate *pgs)
2193
3.08M
{
2194
3.08M
    int ret = state->render(dev, state, buffer, data_x, cmapper, pgs);
2195
2196
3.08M
    step_to_next_line(state);
2197
3.08M
    return ret;
2198
3.08M
}
2199
2200
static int
2201
gx_default_transform_pixel_region_end(gx_device *dev, gx_default_transform_pixel_region_state_t *state)
2202
144k
{
2203
144k
    if (state) {
2204
144k
        gs_free_object(state->mem, state->line, "image line");
2205
144k
        gs_free_object(state->mem, state, "gx_default_transform_pixel_region_state_t");
2206
144k
    }
2207
144k
    return 0;
2208
144k
}
2209
2210
int
2211
gx_default_transform_pixel_region(gx_device *dev,
2212
                       transform_pixel_region_reason reason,
2213
                       transform_pixel_region_data *data)
2214
6.54M
{
2215
6.54M
    gx_default_transform_pixel_region_state_t *state = (gx_default_transform_pixel_region_state_t *)data->state;
2216
2217
6.54M
    switch (reason)
2218
6.54M
    {
2219
144k
    case transform_pixel_region_begin:
2220
144k
        return gx_default_transform_pixel_region_begin(dev, data->u.init.w, data->u.init.h, data->u.init.spp, data->u.init.pixels, data->u.init.rows, data->u.init.clip, data->u.init.lop, (gx_default_transform_pixel_region_state_t **)&data->state);
2221
3.16M
    case transform_pixel_region_data_needed:
2222
3.16M
        return gx_default_transform_pixel_region_data_needed(dev, state);
2223
3.08M
    case transform_pixel_region_process_data:
2224
3.08M
        return gx_default_transform_pixel_region_process_data(dev, state, data->u.process_data.buffer, data->u.process_data.data_x, data->u.process_data.cmapper, data->u.process_data.pgs);
2225
144k
    case transform_pixel_region_end:
2226
144k
        data->state = NULL;
2227
144k
        return gx_default_transform_pixel_region_end(dev, state);
2228
0
    default:
2229
0
        return gs_error_unknownerror;
2230
6.54M
    }
2231
6.54M
}