Coverage Report

Created: 2026-08-08 08:00

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