Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/base/gdevdflt.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2025 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
18.2M
{
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
18.2M
    if (dev->color_info.polarity == GX_CINFO_POLARITY_UNKNOWN)
58
0
        dev->color_info.polarity = new_polarity;
59
18.2M
}
60
61
static gx_color_index
62
(*get_encode_color(gx_device *dev))(gx_device *, const gx_color_value *)
63
188M
{
64
188M
    dev_proc_encode_color(*encode_proc);
65
66
    /* use encode_color if it has been provided */
67
188M
    if ((encode_proc = dev_proc(dev, encode_color)) == 0) {
68
18.2M
        if (dev->color_info.num_components == 1                          &&
69
18.2M
            dev_proc(dev, map_rgb_color) != 0) {
70
12.3M
            set_cinfo_polarity(dev, GX_CINFO_POLARITY_ADDITIVE);
71
12.3M
            encode_proc = gx_backwards_compatible_gray_encode;
72
12.3M
        } else  if ( (dev->color_info.num_components == 3    )           &&
73
5.91M
             (encode_proc = dev_proc(dev, map_rgb_color)) != 0  )
74
69.8k
            set_cinfo_polarity(dev, GX_CINFO_POLARITY_ADDITIVE);
75
5.84M
        else if ( dev->color_info.num_components == 4                    &&
76
5.84M
                 (encode_proc = dev_proc(dev, map_cmyk_color)) != 0   )
77
0
            set_cinfo_polarity(dev, GX_CINFO_POLARITY_SUBTRACTIVE);
78
18.2M
    }
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
188M
    if (encode_proc == 0) {
92
5.84M
        if (dev->color_info.num_components == 1 && dev->color_info.depth != 0) {
93
5.84M
            set_cinfo_polarity(dev, GX_CINFO_POLARITY_ADDITIVE);
94
5.84M
            if (dev->color_info.max_gray == (1 << dev->color_info.depth) - 1)
95
5.84M
                encode_proc = gx_default_gray_fast_encode;
96
0
            else
97
0
                encode_proc = gx_default_gray_encode;
98
5.84M
            dev->color_info.separable_and_linear = GX_CINFO_SEP_LIN;
99
5.84M
        } 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
5.84M
    }
111
112
188M
    return encode_proc;
113
188M
}
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
18.2M
{
130
18.2M
    frac                            cm_comp_fracs[3];
131
18.2M
    int                             i;
132
18.2M
    const gx_device                *cmdev;
133
18.2M
    const gx_cm_color_map_procs    *cmprocs;
134
135
18.2M
    if ( dev->color_info.num_components != 3                   ||
136
18.2M
         dev->color_info.polarity != GX_CINFO_POLARITY_ADDITIVE  )
137
18.2M
        return false;
138
139
69.8k
    cmprocs = dev_proc(dev, get_color_mapping_procs)(dev, &cmdev);
140
141
    /* check the values 1/4, 1/3, and 3/4 */
142
69.8k
    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
69.8k
    cm_comp_fracs[0] -= frac_1 / 4;
146
69.8k
    cm_comp_fracs[1] -= frac_1 / 3;
147
69.8k
    cm_comp_fracs[2] -= 3 * frac_1 / 4;
148
69.8k
    for ( i = 0;
149
279k
           i < 3                            &&
150
279k
           -frac_1 / 100 < cm_comp_fracs[i] &&
151
279k
           cm_comp_fracs[i] < frac_1 / 100;
152
209k
          i++ )
153
209k
        ;
154
69.8k
    return i == 3;
155
18.2M
}
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
188M
{
282
    /* if a method has already been provided, use it */
283
188M
    if (dev_proc(dev, decode_color) != 0)
284
170M
        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
18.2M
    if (dev_proc(dev, map_color_rgb) != 0) {
293
294
        /* if the device has a DeviceRGB color model, use map_color_rgb */
295
18.2M
        if (is_like_DeviceRGB(dev))
296
69.8k
            return dev_proc(dev, map_color_rgb);
297
298
        /* If separable ande linear then use default */
299
18.2M
        if (colors_are_separable_and_linear(&dev->color_info))
300
5.84M
            return &gx_default_decode_color;
301
302
        /* gray devices can be handled based on their polarity */
303
12.3M
        if ( dev->color_info.num_components == 1 &&
304
12.3M
             dev->color_info.gray_index == 0       )
305
12.3M
            return dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE
306
12.3M
                       ? &gx_default_1_add_decode_color
307
12.3M
                       : &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
67.9k
{
350
67.9k
    int i;
351
67.9k
    byte gray_index = dev->color_info.gray_index;
352
67.9k
    gx_color_value max_gray = dev->color_info.max_gray;
353
67.9k
    gx_color_value max_color = dev->color_info.max_color;
354
67.9k
    int num_components = dev->color_info.num_components;
355
356
612k
#define comp_bits (dev->color_info.comp_bits)
357
306k
#define comp_mask (dev->color_info.comp_mask)
358
851k
#define comp_shift (dev->color_info.comp_shift)
359
67.9k
    comp_shift[num_components - 1] = 0;
360
306k
    for ( i = num_components - 1 - 1; i >= 0; i-- ) {
361
238k
        comp_shift[i] = comp_shift[i + 1] +
362
238k
            ( i == gray_index ? ilog2(max_gray + 1) : ilog2(max_color + 1) );
363
238k
    }
364
374k
    for ( i = 0; i < num_components; i++ ) {
365
306k
        comp_bits[i] = ( i == gray_index ?
366
28.6k
                         ilog2(max_gray + 1) :
367
306k
                         ilog2(max_color + 1) );
368
306k
        comp_mask[i] = (((gx_color_index)1 << comp_bits[i]) - 1)
369
306k
                                               << comp_shift[i];
370
306k
    }
371
67.9k
#undef comp_bits
372
67.9k
#undef comp_mask
373
67.9k
#undef comp_shift
374
67.9k
}
375
376
/* Determine if a number is a power of two.  Works only for integers. */
377
203M
#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.97M
{
383
3.97M
    if ((float)pdev->color_info.depth / (float)pdev->color_info.num_components >= 8)
384
581k
        return true;
385
3.39M
    return false;
386
3.97M
}
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
203M
{
411
203M
    int i, j;
412
203M
    gx_device_color_info * pinfo = &(dev->color_info);
413
203M
    int num_components = pinfo->num_components;
414
203M
    byte comp_shift[GX_DEVICE_COLOR_MAX_COMPONENTS];
415
203M
    byte comp_bits[GX_DEVICE_COLOR_MAX_COMPONENTS];
416
203M
    gx_color_index comp_mask[GX_DEVICE_COLOR_MAX_COMPONENTS];
417
203M
    gx_color_index color_index;
418
203M
    gx_color_index current_bits = 0;
419
203M
    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
203M
    if (pinfo->separable_and_linear != GX_CINFO_UNKNOWN_SEP_LIN)
423
123M
        return;
424
    /* If there is not an encode_color_routine then we cannot proceed. */
425
80.0M
    if (dev_proc(dev, encode_color) == NULL)
426
12.3M
        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
67.6M
    if (pinfo->gray_index < num_components &&
433
67.6M
        (!pinfo->dither_grays || pinfo->dither_grays != (pinfo->max_gray + 1)))
434
0
            return;
435
67.6M
    if ((num_components > 1 || pinfo->gray_index != 0) &&
436
67.6M
        (!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
67.6M
    if (!is_power_of_two(pinfo->dither_grays)
445
67.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
67.6M
    color_index = dev_proc(dev, encode_color)(dev, colorants);
452
67.6M
    if (color_index != 0)
453
67.0M
        return;    /* Exit if zero colorants produce a non zero index */
454
1.35M
    for (i = 0; i < num_components; i++) {
455
        /* Check this colorant = max with all others = 0 */
456
1.93M
        for (j = 0; j < num_components; j++)
457
1.18M
            colorants[j] = 0;
458
749k
        colorants[i] = gx_max_color_value;
459
749k
        color_index = dev_proc(dev, encode_color)(dev, colorants);
460
749k
        if (color_index == 0)  /* If no bits then we have a problem */
461
1
            return;
462
749k
        if (color_index & current_bits)  /* Check for overlapping bits */
463
0
            return;
464
749k
        current_bits |= color_index;
465
749k
        comp_mask[i] = color_index;
466
        /* Determine the shift count for the colorant */
467
2.51M
        for (j = 0; (color_index & 1) == 0 && color_index != 0; j++)
468
1.76M
            color_index >>= 1;
469
749k
        comp_shift[i] = j;
470
        /* Determine the bit count for the colorant */
471
3.18M
        for (j = 0; color_index != 0; j++) {
472
2.43M
            if ((color_index & 1) == 0) /* check for non-consecutive bits */
473
0
                return;
474
2.43M
            color_index >>= 1;
475
2.43M
        }
476
749k
        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
1.93M
        for (j = 0; j < num_components; j++)
484
1.18M
            colorants[j] = gx_max_color_value;
485
749k
        colorants[i] = 0;
486
749k
        color_index = dev_proc(dev, encode_color)(dev, colorants);
487
749k
        if (color_index & comp_mask[i])  /* Check for overlapping bits */
488
0
            return;
489
749k
    }
490
    /* If we get to here then the device is very likely to be separable. */
491
602k
    pinfo->separable_and_linear = GX_CINFO_SEP_LIN;
492
1.35M
    for (i = 0; i < num_components; i++) {
493
749k
        pinfo->comp_shift[i] = comp_shift[i];
494
749k
        pinfo->comp_bits[i] = comp_bits[i];
495
749k
        pinfo->comp_mask[i] = comp_mask[i];
496
749k
    }
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
602k
    for (i = 0; i < num_components; i++) {
505
602k
        int dither = 1 << comp_bits[i];
506
507
602k
        if (pinfo->dither_grays != 1 && dither == pinfo->dither_grays) {
508
602k
            pinfo->gray_index = i;
509
602k
            break;
510
602k
        }
511
602k
    }
512
602k
}
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
157M
{
541
157M
    gx_device_color_info * pinfo = &(dev->color_info);
542
157M
    int num_components = pinfo->num_components;
543
157M
    gx_color_index mul, color_index;
544
157M
    int i, j;
545
157M
    gx_color_value colorants[GX_DEVICE_COLOR_MAX_COMPONENTS];
546
157M
    bool deep = device_is_deep(dev);
547
548
157M
    if (pinfo->separable_and_linear == GX_CINFO_UNKNOWN_SEP_LIN)
549
33.6M
        check_device_separable(dev);
550
157M
    if (pinfo->separable_and_linear != GX_CINFO_SEP_LIN)
551
157M
        return;
552
553
46.4k
    if (dev_proc(dev, ret_devn_params)(dev) != NULL) {
554
        /* We know all devn devices are compatible. */
555
21.6k
        pinfo->separable_and_linear = GX_CINFO_SEP_LIN_STANDARD;
556
21.6k
        return;
557
21.6k
    }
558
559
    /* Do the superficial quick checks */
560
77.3k
    for (i = 0; i < num_components; i++) {
561
52.5k
        int shift = (num_components-1-i)*(8<<deep);
562
52.5k
        if (pinfo->comp_shift[i] != shift)
563
0
            goto bad;
564
52.5k
        if (pinfo->comp_bits[i] != 8<<deep)
565
0
            goto bad;
566
52.5k
        if (pinfo->comp_mask[i] != ((gx_color_index)(deep ? 65535 : 255))<<shift)
567
0
            goto bad;
568
52.5k
    }
569
570
    /* OK, now we are going to be slower. */
571
24.7k
    mul = 0;
572
77.3k
    for (i = 0; i < num_components; i++) {
573
52.5k
        mul = (mul<<(8<<deep)) | 1;
574
52.5k
    }
575
    /* In the deep case, we don't exhaustively test */
576
6.34M
    for (i = 0; i < 255; i++) {
577
19.7M
        for (j = 0; j < num_components; j++)
578
13.4M
            colorants[j] = i*257;
579
6.32M
        color_index = dev_proc(dev, encode_color)(dev, colorants);
580
6.32M
        if (color_index != i*mul*(deep ? 257 : 1) && (i*mul*(deep ? 257 : 1) != gx_no_color_index_value))
581
0
            goto bad;
582
6.32M
    }
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
24.7k
    if ((color_index | mul) != 255*mul*(deep ? 257 : 1))
586
0
        goto bad;
587
588
24.7k
    pinfo->separable_and_linear = GX_CINFO_SEP_LIN_STANDARD;
589
24.7k
    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
188M
{
600
188M
    fill_dev_proc(dev, open_device, gx_default_open_device);
601
188M
    fill_dev_proc(dev, get_initial_matrix, gx_default_get_initial_matrix);
602
188M
    fill_dev_proc(dev, sync_output, gx_default_sync_output);
603
188M
    fill_dev_proc(dev, output_page, gx_default_output_page);
604
188M
    fill_dev_proc(dev, close_device, gx_default_close_device);
605
    /* see below for map_rgb_color */
606
188M
    fill_dev_proc(dev, map_color_rgb, gx_default_map_color_rgb);
607
    /* NOT fill_rectangle */
608
188M
    fill_dev_proc(dev, copy_mono, gx_default_copy_mono);
609
188M
    fill_dev_proc(dev, copy_color, gx_default_copy_color);
610
188M
    fill_dev_proc(dev, get_params, gx_default_get_params);
611
188M
    fill_dev_proc(dev, put_params, gx_default_put_params);
612
    /* see below for map_cmyk_color */
613
188M
    fill_dev_proc(dev, get_page_device, gx_default_get_page_device);
614
188M
    fill_dev_proc(dev, get_alpha_bits, gx_default_get_alpha_bits);
615
188M
    fill_dev_proc(dev, copy_alpha, gx_default_copy_alpha);
616
188M
    fill_dev_proc(dev, fill_path, gx_default_fill_path);
617
188M
    fill_dev_proc(dev, stroke_path, gx_default_stroke_path);
618
188M
    fill_dev_proc(dev, fill_mask, gx_default_fill_mask);
619
188M
    fill_dev_proc(dev, fill_trapezoid, gx_default_fill_trapezoid);
620
188M
    fill_dev_proc(dev, fill_parallelogram, gx_default_fill_parallelogram);
621
188M
    fill_dev_proc(dev, fill_triangle, gx_default_fill_triangle);
622
188M
    fill_dev_proc(dev, draw_thin_line, gx_default_draw_thin_line);
623
188M
    fill_dev_proc(dev, get_alpha_bits, gx_default_get_alpha_bits);
624
188M
    fill_dev_proc(dev, strip_tile_rectangle, gx_default_strip_tile_rectangle);
625
188M
    fill_dev_proc(dev, strip_copy_rop2, gx_default_strip_copy_rop2);
626
188M
    fill_dev_proc(dev, strip_tile_rect_devn, gx_default_strip_tile_rect_devn);
627
188M
    fill_dev_proc(dev, get_clipping_box, gx_default_get_clipping_box);
628
188M
    fill_dev_proc(dev, begin_typed_image, gx_default_begin_typed_image);
629
188M
    fill_dev_proc(dev, get_bits_rectangle, gx_default_get_bits_rectangle);
630
188M
    fill_dev_proc(dev, composite, gx_default_composite);
631
188M
    fill_dev_proc(dev, get_hardware_params, gx_default_get_hardware_params);
632
188M
    fill_dev_proc(dev, text_begin, gx_default_text_begin);
633
634
188M
    set_dev_proc(dev, encode_color, get_encode_color(dev));
635
188M
    if (dev->color_info.num_components == 3)
636
53.2M
        set_dev_proc(dev, map_rgb_color, dev_proc(dev, encode_color));
637
188M
    if (dev->color_info.num_components == 4)
638
55.9M
        set_dev_proc(dev, map_cmyk_color, dev_proc(dev, encode_color));
639
640
188M
    if (colors_are_separable_and_linear(&dev->color_info)) {
641
57.2M
        fill_dev_proc(dev, encode_color, gx_default_encode_color);
642
57.2M
        fill_dev_proc(dev, map_cmyk_color, gx_default_encode_color);
643
57.2M
        fill_dev_proc(dev, map_rgb_color, gx_default_encode_color);
644
131M
    } else {
645
        /* if it isn't set now punt */
646
131M
        fill_dev_proc(dev, encode_color, gx_error_encode_color);
647
131M
        fill_dev_proc(dev, map_cmyk_color, gx_error_encode_color);
648
131M
        fill_dev_proc(dev, map_rgb_color, gx_error_encode_color);
649
131M
    }
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
188M
    switch (dev->color_info.num_components) {
660
79.5M
    case 1:     /* DeviceGray or DeviceInvertGray */
661
        /*
662
         * If not gray then the device must provide the color
663
         * mapping procs.
664
         */
665
79.5M
        if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE) {
666
79.5M
            fill_dev_proc( dev,
667
79.5M
                       get_color_mapping_procs,
668
79.5M
                       gx_default_DevGray_get_color_mapping_procs );
669
79.5M
        } else
670
6.36k
            fill_dev_proc(dev, get_color_mapping_procs, gx_error_get_color_mapping_procs);
671
79.5M
        fill_dev_proc( dev,
672
79.5M
                       get_color_comp_index,
673
79.5M
                       gx_default_DevGray_get_color_comp_index );
674
79.5M
        break;
675
676
53.2M
    case 3:
677
53.2M
        if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE) {
678
53.2M
            fill_dev_proc( dev,
679
53.2M
                       get_color_mapping_procs,
680
53.2M
                       gx_default_DevRGB_get_color_mapping_procs );
681
53.2M
            fill_dev_proc( dev,
682
53.2M
                       get_color_comp_index,
683
53.2M
                       gx_default_DevRGB_get_color_comp_index );
684
53.2M
        } 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
53.2M
        break;
689
690
55.9M
    case 4:
691
55.9M
        fill_dev_proc(dev, get_color_mapping_procs, gx_default_DevCMYK_get_color_mapping_procs);
692
55.9M
        fill_dev_proc(dev, get_color_comp_index, gx_default_DevCMYK_get_color_comp_index);
693
55.9M
        break;
694
54.0k
    default:    /* Unknown color model - set error handlers */
695
54.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
188M
    }
700
701
188M
    set_dev_proc(dev, decode_color, get_decode_color(dev));
702
188M
    fill_dev_proc(dev, get_profile, gx_default_get_profile);
703
188M
    fill_dev_proc(dev, set_graphics_type_tag, gx_default_set_graphics_type_tag);
704
705
188M
    fill_dev_proc(dev, fill_rectangle_hl_color, gx_default_fill_rectangle_hl_color);
706
188M
    fill_dev_proc(dev, include_color_space, gx_default_include_color_space);
707
188M
    fill_dev_proc(dev, fill_linear_color_scanline, gx_default_fill_linear_color_scanline);
708
188M
    fill_dev_proc(dev, fill_linear_color_trapezoid, gx_default_fill_linear_color_trapezoid);
709
188M
    fill_dev_proc(dev, fill_linear_color_triangle, gx_default_fill_linear_color_triangle);
710
188M
    fill_dev_proc(dev, update_spot_equivalent_colors, gx_default_update_spot_equivalent_colors);
711
188M
    fill_dev_proc(dev, ret_devn_params, gx_default_ret_devn_params);
712
188M
    fill_dev_proc(dev, fillpage, gx_default_fillpage);
713
188M
    fill_dev_proc(dev, copy_alpha_hl_color, gx_default_no_copy_alpha_hl_color);
714
715
188M
    fill_dev_proc(dev, begin_transparency_group, gx_default_begin_transparency_group);
716
188M
    fill_dev_proc(dev, end_transparency_group, gx_default_end_transparency_group);
717
718
188M
    fill_dev_proc(dev, begin_transparency_mask, gx_default_begin_transparency_mask);
719
188M
    fill_dev_proc(dev, end_transparency_mask, gx_default_end_transparency_mask);
720
188M
    fill_dev_proc(dev, discard_transparency_layer, gx_default_discard_transparency_layer);
721
722
188M
    fill_dev_proc(dev, push_transparency_state, gx_default_push_transparency_state);
723
188M
    fill_dev_proc(dev, pop_transparency_state, gx_default_pop_transparency_state);
724
725
188M
    fill_dev_proc(dev, put_image, gx_default_put_image);
726
727
188M
    fill_dev_proc(dev, dev_spec_op, gx_default_dev_spec_op);
728
188M
    fill_dev_proc(dev, copy_planes, gx_default_copy_planes);
729
188M
    fill_dev_proc(dev, process_page, gx_default_process_page);
730
188M
    fill_dev_proc(dev, transform_pixel_region, gx_default_transform_pixel_region);
731
188M
    fill_dev_proc(dev, fill_stroke_path, gx_default_fill_stroke_path);
732
188M
    fill_dev_proc(dev, lock_pattern, gx_default_lock_pattern);
733
188M
}
734
735
736
int
737
gx_default_open_device(gx_device * dev)
738
522k
{
739
    /* Initialize the separable status if not known. */
740
522k
    check_device_separable(dev);
741
522k
    return 0;
742
522k
}
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
89.7M
{
750
    /* NB this device has no paper margins */
751
89.7M
    double fs_res = dev->HWResolution[0] / 72.0;
752
89.7M
    double ss_res = dev->HWResolution[1] / 72.0;
753
754
89.7M
    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
89.7M
    case 0:
781
89.7M
        pmat->xx = fs_res;
782
89.7M
        pmat->xy = 0;
783
89.7M
        pmat->yx = 0;
784
89.7M
        pmat->yy = -ss_res;
785
89.7M
        pmat->tx = 0;
786
89.7M
        pmat->ty = (float)dev->height;
787
        /****** tx/y is WRONG for devices with ******/
788
        /****** arbitrary initial matrix ******/
789
89.7M
        break;
790
89.7M
    }
791
89.7M
}
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.30M
{
810
1.30M
    return 0;
811
1.30M
}
812
813
int
814
gx_default_output_page(gx_device * dev, int num_copies, int flush)
815
16
{
816
16
    int code = dev_proc(dev, sync_output)(dev);
817
818
16
    if (code >= 0)
819
16
        code = gx_finish_output_page(dev, num_copies, flush);
820
16
    return code;
821
16
}
822
823
int
824
gx_default_close_device(gx_device * dev)
825
946k
{
826
946k
    return 0;
827
946k
}
828
829
gx_device *
830
gx_default_get_page_device(gx_device * dev)
831
577k
{
832
577k
    return NULL;
833
577k
}
834
gx_device *
835
gx_page_device_get_page_device(gx_device * dev)
836
39.4M
{
837
39.4M
    return dev;
838
39.4M
}
839
840
int
841
gx_default_get_alpha_bits(gx_device * dev, graphics_object_type type)
842
94.3M
{
843
94.3M
    return (type == go_text ? dev->color_info.anti_alias.text_bits :
844
94.3M
            dev->color_info.anti_alias.graphics_bits);
845
94.3M
}
846
847
void
848
gx_default_get_clipping_box(gx_device * dev, gs_fixed_rect * pbox)
849
86.4M
{
850
86.4M
    pbox->p.x = 0;
851
86.4M
    pbox->p.y = 0;
852
86.4M
    pbox->q.x = int2fixed(dev->width);
853
86.4M
    pbox->q.y = int2fixed(dev->height);
854
86.4M
}
855
void
856
gx_get_largest_clipping_box(gx_device * dev, gs_fixed_rect * pbox)
857
693
{
858
693
    pbox->p.x = min_fixed;
859
693
    pbox->p.y = min_fixed;
860
693
    pbox->q.x = max_fixed;
861
693
    pbox->q.y = max_fixed;
862
693
}
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
26.4M
{
878
26.4M
    return pcte->type->procs.create_default_compositor
879
26.4M
        (pcte, pcdev, dev, pgs, memory);
880
26.4M
}
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
0
{
887
0
    *pcdev = dev;
888
0
    return 0;
889
0
}
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
968k
{
897
968k
    *pcdev = dev;   /* Do nothing -> return the same device */
898
968k
    return 0;
899
968k
}
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
179M
{
905
179M
    return 0;
906
179M
}
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
2.94M
{
923
2.94M
    return false;
924
2.94M
}
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
179M
{
934
179M
    return 0;     /* Do nothing */
935
179M
}
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
968k
{
944
968k
    return 0;     /* No cropping. */
945
968k
}
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
379M
{
956
379M
    switch(dev_spec_op) {
957
0
        case gxdso_form_begin:
958
0
        case gxdso_form_end:
959
23.0k
        case gxdso_pattern_can_accum:
960
23.0k
        case gxdso_pattern_start_accum:
961
23.0k
        case gxdso_pattern_finish_accum:
962
152k
        case gxdso_pattern_load:
963
3.87M
        case gxdso_pattern_shading_area:
964
4.87M
        case gxdso_pattern_is_cpath_accum:
965
4.87M
        case gxdso_pattern_handles_clip_path:
966
4.91M
        case gxdso_is_pdf14_device:
967
134M
        case gxdso_supports_devn:
968
134M
        case gxdso_supports_hlcolor:
969
134M
        case gxdso_supports_saved_pages:
970
134M
        case gxdso_needs_invariant_palette:
971
134M
        case gxdso_supports_iccpostrender:
972
135M
        case gxdso_supports_alpha:
973
136M
        case gxdso_pdf14_sep_device:
974
138M
        case gxdso_supports_pattern_transparency:
975
138M
        case gxdso_overprintsim_state:
976
138M
        case gxdso_skip_icc_component_validation:
977
138M
            return 0;
978
365
        case gxdso_pattern_shfill_doesnt_need_path:
979
365
            return (dev_proc(pdev, fill_path) == gx_default_fill_path);
980
26.3M
        case gxdso_is_std_cmyk_1bit:
981
26.3M
            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.55M
        case gxdso_interpolate_threshold:
985
1.55M
            if ((pdev->color_info.num_components == 1 &&
986
1.55M
                 pdev->color_info.max_gray < 15) ||
987
1.55M
                (pdev->color_info.num_components > 1 &&
988
1.29M
                 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
719k
                return 4;
992
719k
            }
993
838k
            return 0; /* Otherwise no change */
994
4.55M
        case gxdso_get_dev_param:
995
4.55M
            {
996
4.55M
                dev_param_req_t *request = (dev_param_req_t *)data;
997
4.55M
                return gx_default_get_param(pdev, request->Param, request->list);
998
1.55M
            }
999
2.61M
        case gxdso_current_output_device:
1000
2.61M
            {
1001
2.61M
                *(gx_device **)data = pdev;
1002
2.61M
                return 0;
1003
1.55M
            }
1004
72.0k
        case gxdso_copy_color_is_fast:
1005
72.0k
            return (dev_proc(pdev, copy_color) != gx_default_copy_color);
1006
5.92M
        case gxdso_is_encoding_direct:
1007
5.92M
            if (pdev->color_info.depth != 8 * pdev->color_info.num_components)
1008
0
                return 0;
1009
5.92M
            return (dev_proc(pdev, encode_color) == gx_default_encode_color ||
1010
5.92M
                    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
17.7M
        case gxdso_overprint_active:
1015
17.7M
            return 0;
1016
379M
    }
1017
379M
    return_error(gs_error_undefined);
1018
379M
}
1019
1020
int
1021
gx_default_fill_rectangle_hl_color(gx_device *pdev,
1022
    const gs_fixed_rect *rect,
1023
    const gs_gstate *pgs, const gx_drawing_color *pdcolor,
1024
    const gx_clip_path *pcpath)
1025
6
{
1026
6
    return_error(gs_error_rangecheck);
1027
6
}
1028
1029
int
1030
gx_default_include_color_space(gx_device *pdev, gs_color_space *cspace,
1031
        const byte *res_name, int name_length)
1032
0
{
1033
0
    return 0;
1034
0
}
1035
1036
/*
1037
 * If a device wants to determine an equivalent color for its spot colors then
1038
 * it needs to implement this method.  See comments at the start of
1039
 * src/gsequivc.c.
1040
 */
1041
int
1042
gx_default_update_spot_equivalent_colors(gx_device *pdev, const gs_gstate * pgs, const gs_color_space *pcs)
1043
16.9k
{
1044
16.9k
    return 0;
1045
16.9k
}
1046
1047
/*
1048
 * If a device wants to determine implement support for spot colors then
1049
 * it needs to implement this method.
1050
 */
1051
gs_devn_params *
1052
gx_default_ret_devn_params(gx_device *pdev)
1053
149M
{
1054
149M
    return NULL;
1055
149M
}
1056
1057
int
1058
gx_default_process_page(gx_device *dev, gx_process_page_options_t *options)
1059
0
{
1060
0
    gs_int_rect rect;
1061
0
    int code = 0;
1062
0
    void *buffer = NULL;
1063
1064
    /* Possible future improvements in here could be given by us dividing the
1065
     * page up into n chunks, and spawning a thread per chunk to do the
1066
     * process_fn call on. n could be given by NumRenderingThreads. This
1067
     * would give us multi-core advantages even without clist. */
1068
0
    if (options->init_buffer_fn) {
1069
0
        code = options->init_buffer_fn(options->arg, dev, dev->memory, dev->width, dev->height, &buffer);
1070
0
        if (code < 0)
1071
0
            return code;
1072
0
    }
1073
1074
0
    rect.p.x = 0;
1075
0
    rect.p.y = 0;
1076
0
    rect.q.x = dev->width;
1077
0
    rect.q.y = dev->height;
1078
0
    if (options->process_fn)
1079
0
        code = options->process_fn(options->arg, dev, dev, &rect, buffer);
1080
0
    if (code >= 0 && options->output_fn)
1081
0
        code = options->output_fn(options->arg, dev, buffer);
1082
1083
0
    if (options->free_buffer_fn)
1084
0
        options->free_buffer_fn(options->arg, dev, dev->memory, buffer);
1085
1086
0
    return code;
1087
0
}
1088
1089
int
1090
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)
1091
0
{
1092
0
    return 0;
1093
0
}
1094
1095
int
1096
gx_default_end_transparency_group(gx_device *dev, gs_gstate *pgs)
1097
0
{
1098
0
    return 0;
1099
0
}
1100
1101
int
1102
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)
1103
0
{
1104
0
    return 0;
1105
0
}
1106
1107
int
1108
gx_default_end_transparency_mask(gx_device *dev, gs_gstate *pgs)
1109
0
{
1110
0
    return 0;
1111
0
}
1112
1113
int
1114
gx_default_discard_transparency_layer(gx_device *dev, gs_gstate *pgs)
1115
0
{
1116
0
    return 0;
1117
0
}
1118
1119
int
1120
gx_default_push_transparency_state(gx_device *dev, gs_gstate *pgs)
1121
0
{
1122
0
    return 0;
1123
0
}
1124
1125
int
1126
gx_default_pop_transparency_state(gx_device *dev, gs_gstate *pgs)
1127
0
{
1128
0
    return 0;
1129
0
}
1130
1131
int
1132
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)
1133
46.4k
{
1134
46.4k
    return_error(gs_error_undefined);
1135
46.4k
}
1136
1137
int
1138
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)
1139
0
{
1140
0
    return_error(gs_error_undefined);
1141
0
}
1142
1143
int
1144
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)
1145
0
{
1146
0
    return_error(gs_error_undefined);
1147
0
}
1148
1149
/* ---------------- Default per-instance procedures ---------------- */
1150
1151
int
1152
gx_default_install(gx_device * dev, gs_gstate * pgs)
1153
766k
{
1154
766k
    return 0;
1155
766k
}
1156
1157
int
1158
gx_default_begin_page(gx_device * dev, gs_gstate * pgs)
1159
985k
{
1160
985k
    return 0;
1161
985k
}
1162
1163
int
1164
gx_default_end_page(gx_device * dev, int reason, gs_gstate * pgs)
1165
1.15M
{
1166
1.15M
    return (reason != 2 ? 1 : 0);
1167
1.15M
}
1168
1169
void
1170
gx_default_set_graphics_type_tag(gx_device *dev, gs_graphics_type_tag_t graphics_type_tag)
1171
4.90M
{
1172
    /* set the tag but carefully preserve GS_DEVICE_ENCODES_TAGS */
1173
4.90M
    dev->graphics_type_tag = (dev->graphics_type_tag & GS_DEVICE_ENCODES_TAGS) | graphics_type_tag;
1174
4.90M
}
1175
1176
/* ---------------- Device subclassing procedures ---------------- */
1177
1178
/* Non-obvious code. The 'dest_procs' is the 'procs' memory occupied by the original device that we decided to subclass,
1179
 * 'src_procs' is the newly allocated piece of memory, to which we have already copied the content of the
1180
 * original device (including the procs), prototype is the device structure prototype for the subclassing device.
1181
 * Here we copy the methods from the prototype to the original device procs memory *but* if the original (src_procs)
1182
 * device had a NULL method, we make the new device procs have a NULL method too.
1183
 * The reason for ths is ugly, there are some places in the graphics library which explicitly check for
1184
 * a device having a NULL method and take different code paths depending on the result.
1185
 * Now in general we expect subclassing devices to implement *every* method, so if we didn't copy
1186
 * over NULL methods present in the original source device then the code path could be inappropriate for
1187
 * that underlying (now subclassed) device.
1188
 */
1189
/* November 10th 2017 Restored the original behaviour of the device methods, they should now never be NULL.
1190
 * Howwever, there are still places in the code which take different code paths if the device method is (now)
1191
 * the default device method, rather than a device-specific method.
1192
 * So instead of checking for NULL, we now need to check against the default implementation, and *NOT* copy the
1193
 * prototype (subclass device) method if the original device had the default implementation.
1194
 * I suspect a combination of forwarding and subclassing devices will not work properly for this reason.
1195
 */
1196
int gx_copy_device_procs(gx_device *dest, const gx_device *src, const gx_device *pprototype)
1197
31.4k
{
1198
31.4k
    gx_device prototype = *pprototype;
1199
1200
    /* In the new (as of 2021) world, the prototype does not contain
1201
     * device procs. We need to call the 'initialize_device_procs'
1202
     * function to properly populate the procs array. We can't write to
1203
     * the const prototype pointer we are passed in, so copy it to a
1204
     * local block, and initialize that instead, */
1205
31.4k
    prototype.initialize_device_procs(&prototype);
1206
    /* Fill in missing entries with the global defaults */
1207
31.4k
    gx_device_fill_in_procs(&prototype);
1208
1209
31.4k
    if (dest->initialize_device_procs == NULL)
1210
0
       dest->initialize_device_procs = prototype.initialize_device_procs;
1211
1212
31.4k
    set_dev_proc(dest, initialize_device, dev_proc(&prototype, initialize_device));
1213
31.4k
    set_dev_proc(dest, open_device, dev_proc(&prototype, open_device));
1214
31.4k
    set_dev_proc(dest, get_initial_matrix, dev_proc(&prototype, get_initial_matrix));
1215
31.4k
    set_dev_proc(dest, sync_output, dev_proc(&prototype, sync_output));
1216
31.4k
    set_dev_proc(dest, output_page, dev_proc(&prototype, output_page));
1217
31.4k
    set_dev_proc(dest, close_device, dev_proc(&prototype, close_device));
1218
31.4k
    set_dev_proc(dest, map_rgb_color, dev_proc(&prototype, map_rgb_color));
1219
31.4k
    set_dev_proc(dest, map_color_rgb, dev_proc(&prototype, map_color_rgb));
1220
31.4k
    set_dev_proc(dest, fill_rectangle, dev_proc(&prototype, fill_rectangle));
1221
31.4k
    set_dev_proc(dest, copy_mono, dev_proc(&prototype, copy_mono));
1222
31.4k
    set_dev_proc(dest, copy_color, dev_proc(&prototype, copy_color));
1223
31.4k
    set_dev_proc(dest, get_params, dev_proc(&prototype, get_params));
1224
31.4k
    set_dev_proc(dest, put_params, dev_proc(&prototype, put_params));
1225
31.4k
    set_dev_proc(dest, map_cmyk_color, dev_proc(&prototype, map_cmyk_color));
1226
31.4k
    set_dev_proc(dest, get_page_device, dev_proc(&prototype, get_page_device));
1227
31.4k
    set_dev_proc(dest, get_alpha_bits, dev_proc(&prototype, get_alpha_bits));
1228
31.4k
    set_dev_proc(dest, copy_alpha, dev_proc(&prototype, copy_alpha));
1229
31.4k
    set_dev_proc(dest, fill_path, dev_proc(&prototype, fill_path));
1230
31.4k
    set_dev_proc(dest, stroke_path, dev_proc(&prototype, stroke_path));
1231
31.4k
    set_dev_proc(dest, fill_trapezoid, dev_proc(&prototype, fill_trapezoid));
1232
31.4k
    set_dev_proc(dest, fill_parallelogram, dev_proc(&prototype, fill_parallelogram));
1233
31.4k
    set_dev_proc(dest, fill_triangle, dev_proc(&prototype, fill_triangle));
1234
31.4k
    set_dev_proc(dest, draw_thin_line, dev_proc(&prototype, draw_thin_line));
1235
31.4k
    set_dev_proc(dest, strip_tile_rectangle, dev_proc(&prototype, strip_tile_rectangle));
1236
31.4k
    set_dev_proc(dest, get_clipping_box, dev_proc(&prototype, get_clipping_box));
1237
31.4k
    set_dev_proc(dest, begin_typed_image, dev_proc(&prototype, begin_typed_image));
1238
31.4k
    set_dev_proc(dest, get_bits_rectangle, dev_proc(&prototype, get_bits_rectangle));
1239
31.4k
    set_dev_proc(dest, composite, dev_proc(&prototype, composite));
1240
31.4k
    set_dev_proc(dest, get_hardware_params, dev_proc(&prototype, get_hardware_params));
1241
31.4k
    set_dev_proc(dest, text_begin, dev_proc(&prototype, text_begin));
1242
31.4k
    set_dev_proc(dest, discard_transparency_layer, dev_proc(&prototype, discard_transparency_layer));
1243
31.4k
    set_dev_proc(dest, get_color_mapping_procs, dev_proc(&prototype, get_color_mapping_procs));
1244
31.4k
    set_dev_proc(dest, get_color_comp_index, dev_proc(&prototype, get_color_comp_index));
1245
31.4k
    set_dev_proc(dest, encode_color, dev_proc(&prototype, encode_color));
1246
31.4k
    set_dev_proc(dest, decode_color, dev_proc(&prototype, decode_color));
1247
31.4k
    set_dev_proc(dest, fill_rectangle_hl_color, dev_proc(&prototype, fill_rectangle_hl_color));
1248
31.4k
    set_dev_proc(dest, include_color_space, dev_proc(&prototype, include_color_space));
1249
31.4k
    set_dev_proc(dest, fill_linear_color_scanline, dev_proc(&prototype, fill_linear_color_scanline));
1250
31.4k
    set_dev_proc(dest, fill_linear_color_trapezoid, dev_proc(&prototype, fill_linear_color_trapezoid));
1251
31.4k
    set_dev_proc(dest, fill_linear_color_triangle, dev_proc(&prototype, fill_linear_color_triangle));
1252
31.4k
    set_dev_proc(dest, update_spot_equivalent_colors, dev_proc(&prototype, update_spot_equivalent_colors));
1253
31.4k
    set_dev_proc(dest, ret_devn_params, dev_proc(&prototype, ret_devn_params));
1254
31.4k
    set_dev_proc(dest, fillpage, dev_proc(&prototype, fillpage));
1255
31.4k
    set_dev_proc(dest, push_transparency_state, dev_proc(&prototype, push_transparency_state));
1256
31.4k
    set_dev_proc(dest, pop_transparency_state, dev_proc(&prototype, pop_transparency_state));
1257
31.4k
    set_dev_proc(dest, dev_spec_op, dev_proc(&prototype, dev_spec_op));
1258
31.4k
    set_dev_proc(dest, get_profile, dev_proc(&prototype, get_profile));
1259
31.4k
    set_dev_proc(dest, strip_copy_rop2, dev_proc(&prototype, strip_copy_rop2));
1260
31.4k
    set_dev_proc(dest, strip_tile_rect_devn, dev_proc(&prototype, strip_tile_rect_devn));
1261
31.4k
    set_dev_proc(dest, process_page, dev_proc(&prototype, process_page));
1262
31.4k
    set_dev_proc(dest, transform_pixel_region, dev_proc(&prototype, transform_pixel_region));
1263
31.4k
    set_dev_proc(dest, fill_stroke_path, dev_proc(&prototype, fill_stroke_path));
1264
31.4k
    set_dev_proc(dest, lock_pattern, dev_proc(&prototype, lock_pattern));
1265
1266
    /*
1267
     * We absolutely must set the 'set_graphics_type_tag' to the default subclass one
1268
     * even if the subclassed device is using the default. This is because the
1269
     * default implementation sets a flag in the device structure, and if we
1270
     * copy the default method, we'll end up setting the flag in the subclassing device
1271
     * instead of the subclassed device!
1272
     */
1273
31.4k
    set_dev_proc(dest, set_graphics_type_tag, dev_proc(&prototype, set_graphics_type_tag));
1274
1275
    /* These are the routines whose existence is checked against the default at
1276
     * some point in the code. The code path differs when the device implements a
1277
     * method other than the default, so the subclassing device needs to ensure that
1278
     * if the subclassed device has one of these methods set to the default, we
1279
     * do not overwrite the default method.
1280
     */
1281
31.4k
    if (dev_proc(src, fill_mask) != gx_default_fill_mask)
1282
22.2k
        set_dev_proc(dest, fill_mask, dev_proc(&prototype, fill_mask));
1283
31.4k
    if (dev_proc(src, begin_transparency_group) != gx_default_begin_transparency_group)
1284
0
        set_dev_proc(dest, begin_transparency_group, dev_proc(&prototype, begin_transparency_group));
1285
31.4k
    if (dev_proc(src, end_transparency_group) != gx_default_end_transparency_group)
1286
0
        set_dev_proc(dest, end_transparency_group, dev_proc(&prototype, end_transparency_group));
1287
31.4k
    if (dev_proc(src, put_image) != gx_default_put_image)
1288
0
        set_dev_proc(dest, put_image, dev_proc(&prototype, put_image));
1289
31.4k
    if (dev_proc(src, copy_planes) != gx_default_copy_planes)
1290
0
        set_dev_proc(dest, copy_planes, dev_proc(&prototype, copy_planes));
1291
31.4k
    if (dev_proc(src, copy_alpha_hl_color) != gx_default_no_copy_alpha_hl_color)
1292
0
        set_dev_proc(dest, copy_alpha_hl_color, dev_proc(&prototype, copy_alpha_hl_color));
1293
1294
31.4k
    return 0;
1295
31.4k
}
1296
1297
int gx_device_subclass(gx_device *dev_to_subclass, gx_device *new_prototype, unsigned int private_data_size)
1298
31.4k
{
1299
31.4k
    gx_device *child_dev;
1300
31.4k
    void *psubclass_data;
1301
31.4k
    gs_memory_struct_type_t *a_std = NULL, *b_std = NULL;
1302
31.4k
    int dynamic = dev_to_subclass->stype_is_dynamic;
1303
31.4k
    char *ptr, *ptr1;
1304
1305
    /* If this happens we are stuffed, as there is no way to get hold
1306
     * of the original device's stype structure, which means we cannot
1307
     * allocate a replacement structure. Abort if so.
1308
     * Also abort if the new_prototype device struct is too large.
1309
     */
1310
31.4k
    if (!dev_to_subclass->stype ||
1311
31.4k
        dev_to_subclass->stype->ssize < new_prototype->params_size)
1312
6
        return_error(gs_error_VMerror);
1313
1314
    /* We make a 'stype' structure for our new device, and copy the old stype into it
1315
     * This means our new device will always have the 'stype_is_dynamic' flag set
1316
     */
1317
31.4k
    a_std = (gs_memory_struct_type_t *)
1318
31.4k
        gs_alloc_bytes_immovable(dev_to_subclass->memory->non_gc_memory, sizeof(*a_std),
1319
31.4k
                                 "gs_device_subclass(stype)");
1320
31.4k
    if (!a_std)
1321
0
        return_error(gs_error_VMerror);
1322
31.4k
    *a_std = *dev_to_subclass->stype;
1323
31.4k
    a_std->ssize = dev_to_subclass->params_size;
1324
1325
31.4k
    if (!dynamic) {
1326
31.4k
        b_std = (gs_memory_struct_type_t *)
1327
31.4k
            gs_alloc_bytes_immovable(dev_to_subclass->memory->non_gc_memory, sizeof(*b_std),
1328
31.4k
                                     "gs_device_subclass(stype)");
1329
31.4k
        if (!b_std) {
1330
0
            gs_free_const_object(dev_to_subclass->memory->non_gc_memory, a_std, "gs_device_subclass(stype)");
1331
0
            return_error(gs_error_VMerror);
1332
0
        }
1333
31.4k
    }
1334
1335
    /* Allocate a device structure for the new child device */
1336
31.4k
    child_dev = gs_alloc_struct_immovable(dev_to_subclass->memory->stable_memory, gx_device, a_std,
1337
31.4k
                                        "gs_device_subclass(device)");
1338
31.4k
    if (child_dev == 0) {
1339
0
        gs_free_const_object(dev_to_subclass->memory->non_gc_memory, a_std, "gs_device_subclass(stype)");
1340
0
        gs_free_const_object(dev_to_subclass->memory->non_gc_memory, b_std, "gs_device_subclass(stype)");
1341
0
        return_error(gs_error_VMerror);
1342
0
    }
1343
1344
    /* Make sure all methods are filled in, note this won't work for a forwarding device
1345
     * so forwarding devices will have to be filled in before being subclassed. This doesn't fill
1346
     * in the fill_rectangle proc, that gets done in the ultimate device's open proc.
1347
     */
1348
31.4k
    gx_device_fill_in_procs(dev_to_subclass);
1349
31.4k
    memcpy(child_dev, dev_to_subclass, dev_to_subclass->stype->ssize);
1350
31.4k
    child_dev->stype = a_std;
1351
31.4k
    child_dev->stype_is_dynamic = 1;
1352
1353
    /* At this point, the only counted reference to the child is from its parent, and we need it to use the right allocator */
1354
31.4k
    rc_init(child_dev, dev_to_subclass->memory->stable_memory, 1);
1355
1356
31.4k
    psubclass_data = (void *)gs_alloc_bytes(dev_to_subclass->memory->non_gc_memory, private_data_size, "subclass memory for subclassing device");
1357
31.4k
    if (psubclass_data == 0){
1358
0
        gs_free_const_object(dev_to_subclass->memory->non_gc_memory, b_std, "gs_device_subclass(stype)");
1359
        /* We *don't* want to run the finalize routine. This would free the stype and
1360
         * properly handle the icc_struct and PageList, but for devices with a custom
1361
         * finalize (eg psdcmyk) it might also free memory it had allocated, and we're
1362
         * still pointing at that memory in the parent.
1363
         */
1364
0
        a_std->finalize = NULL;
1365
0
        gs_set_object_type(dev_to_subclass->memory->stable_memory, child_dev, a_std);
1366
0
        gs_free_object(dev_to_subclass->memory->stable_memory, child_dev, "free subclass memory for subclassing device");
1367
0
        gs_free_const_object(dev_to_subclass->memory->non_gc_memory, a_std, "gs_device_subclass(stype)");
1368
0
        return_error(gs_error_VMerror);
1369
0
    }
1370
31.4k
    memset(psubclass_data, 0x00, private_data_size);
1371
1372
31.4k
    gx_copy_device_procs(dev_to_subclass, child_dev, new_prototype);
1373
31.4k
    dev_to_subclass->finalize = new_prototype->finalize;
1374
31.4k
    dev_to_subclass->dname = new_prototype->dname;
1375
31.4k
    if (dev_to_subclass->icc_struct)
1376
31.4k
        rc_increment(dev_to_subclass->icc_struct);
1377
31.4k
    if (dev_to_subclass->PageList)
1378
31.4k
        rc_increment(dev_to_subclass->PageList);
1379
31.4k
    if (dev_to_subclass->NupControl)
1380
31.4k
        rc_increment(dev_to_subclass->NupControl);
1381
1382
31.4k
    dev_to_subclass->page_procs = new_prototype->page_procs;
1383
31.4k
    gx_subclass_fill_in_page_procs(dev_to_subclass);
1384
1385
    /* In case the new device we're creating has already been initialised, copy
1386
     * its additional data.
1387
     */
1388
31.4k
    ptr = ((char *)dev_to_subclass) + sizeof(gx_device);
1389
31.4k
    ptr1 = ((char *)new_prototype) + sizeof(gx_device);
1390
31.4k
    memcpy(ptr, ptr1, new_prototype->params_size - sizeof(gx_device));
1391
1392
    /* If the original device's stype structure was dynamically allocated, we need
1393
     * to 'fixup' the contents, it's procs need to point to the new device's procs
1394
     * for instance.
1395
     */
1396
31.4k
    if (dynamic) {
1397
0
        if (new_prototype->stype) {
1398
0
            b_std = (gs_memory_struct_type_t *)dev_to_subclass->stype;
1399
0
            *b_std = *new_prototype->stype;
1400
0
            b_std->ssize = a_std->ssize;
1401
0
            dev_to_subclass->stype_is_dynamic = 1;
1402
0
        } else {
1403
0
            gs_free_const_object(child_dev->memory->non_gc_memory, dev_to_subclass->stype,
1404
0
                             "unsubclass");
1405
0
            dev_to_subclass->stype = NULL;
1406
0
            b_std = (gs_memory_struct_type_t *)new_prototype->stype;
1407
0
            dev_to_subclass->stype_is_dynamic = 0;
1408
0
        }
1409
0
    }
1410
31.4k
    else {
1411
31.4k
        *b_std = *new_prototype->stype;
1412
31.4k
        b_std->ssize = a_std->ssize;
1413
31.4k
        dev_to_subclass->stype_is_dynamic = 1;
1414
31.4k
    }
1415
31.4k
    dev_to_subclass->stype = b_std;
1416
    /* We have to patch up the "type" parameters that the memory manage/garbage
1417
     * collector will use, as well.
1418
     */
1419
31.4k
    gs_set_object_type(child_dev->memory, dev_to_subclass, b_std);
1420
1421
31.4k
    dev_to_subclass->subclass_data = psubclass_data;
1422
31.4k
    dev_to_subclass->child = child_dev;
1423
31.4k
    if (child_dev->parent) {
1424
0
        dev_to_subclass->parent = child_dev->parent;
1425
0
        child_dev->parent->child = dev_to_subclass;
1426
0
    }
1427
31.4k
    if (child_dev->child) {
1428
0
        child_dev->child->parent = child_dev;
1429
0
    }
1430
31.4k
    child_dev->parent = dev_to_subclass;
1431
1432
31.4k
    return 0;
1433
31.4k
}
1434
1435
void gx_device_unsubclass(gx_device *dev)
1436
0
{
1437
0
    generic_subclass_data *psubclass_data;
1438
0
    gx_device *parent, *child;
1439
0
    gs_memory_struct_type_t *a_std = 0, *b_std = 0;
1440
0
    int dynamic, ref_count;
1441
0
    gs_memory_t *rcmem;
1442
1443
    /* This should not happen... */
1444
0
    if (!dev)
1445
0
        return;
1446
1447
0
    ref_count = dev->rc.ref_count;
1448
0
    rcmem = dev->rc.memory;
1449
1450
0
    child = dev->child;
1451
0
    psubclass_data = (generic_subclass_data *)dev->subclass_data;
1452
0
    parent = dev->parent;
1453
0
    dynamic = dev->stype_is_dynamic;
1454
1455
    /* We need to account for the fact that we are removing ourselves from
1456
     * the device chain after a clist device has been pushed, due to a
1457
     * compositor action. Since we patched the clist 'composite'
1458
     * method (and target device) when it was pushed.
1459
     * A point to note; we *don't* want to change the forwarding device's
1460
     * 'target', because when we copy the child up to replace 'this' device
1461
     * we do still want the forwarding device to point here. NB its the *child*
1462
     * device that goes away.
1463
     */
1464
0
    if (psubclass_data != NULL && psubclass_data->forwarding_dev != NULL && psubclass_data->saved_compositor_method)
1465
0
        psubclass_data->forwarding_dev->procs.composite = psubclass_data->saved_compositor_method;
1466
1467
    /* If ths device's stype is dynamically allocated, keep a copy of it
1468
     * in case we might need it.
1469
     */
1470
0
    if (dynamic) {
1471
0
        a_std = (gs_memory_struct_type_t *)dev->stype;
1472
0
        if (child)
1473
0
            *a_std = *child->stype;
1474
0
    }
1475
1476
    /* If ths device has any private storage, free it now */
1477
0
    if (psubclass_data)
1478
0
        gs_free_object(dev->memory->non_gc_memory, psubclass_data, "gx_device_unsubclass");
1479
1480
    /* Copy the child device into ths device's memory */
1481
0
    if (child) {
1482
0
        b_std = (gs_memory_struct_type_t *)dev->stype;
1483
0
        rc_decrement(dev->icc_struct, "unsubclass device");
1484
0
        rc_increment(child->icc_struct);
1485
0
        memcpy(dev, child, child->stype->ssize);
1486
        /* Patch back the 'stype' in the memory manager */
1487
0
        gs_set_object_type(child->memory, dev, b_std);
1488
1489
0
        dev->stype = b_std;
1490
        /* The reference count of the subclassing device may have been
1491
         * changed (eg graphics states pointing to it) after we subclassed
1492
         * the device. We need to ensure that we do not overwrite this
1493
         * when we copy back the subclassed device.
1494
         */
1495
0
        dev->rc.ref_count = ref_count;
1496
0
        dev->rc.memory = rcmem;
1497
1498
        /* If we have a chain of devices, make sure the chain beyond the
1499
         * device we're unsubclassing doesn't get broken, we need to
1500
         * detach the lower chain and reattach it at the new highest level.
1501
         */
1502
0
        if (child->child)
1503
0
            child->child->parent = dev;
1504
0
        child->parent->child = child->child;
1505
0
    }
1506
1507
    /* How can we have a subclass device with no child ? Simples; when we
1508
     * hit the end of job restore, the devices are not freed in device
1509
     * chain order. To make sure we don't end up following stale pointers,
1510
     * when a device is freed we remove it from the chain and update
1511
     * any dangling pointers to NULL. When we later free the remaining
1512
     * devices it's possible that their child pointer can then be NULL.
1513
     */
1514
0
    if (child) {
1515
        /* We cannot afford to free the child device if its stype is not
1516
         * dynamic because we can't 'null' the finalise routine, and we
1517
         * cannot permit the device to be finalised because we have copied
1518
         * it up one level, not discarded it. (This shouldn't happen! Child
1519
         * devices are always created with a dynamic stype.) If this ever
1520
         * happens garbage collecton will eventually clean up the memory.
1521
         */
1522
0
        if (child->stype_is_dynamic) {
1523
            /* Make sure that nothing will try to follow the device chain,
1524
             * just security here. */
1525
0
            child->parent = NULL;
1526
0
            child->child = NULL;
1527
1528
            /* We *don't* want to run the finalize routine. This would free
1529
             * the stype and properly handle the icc_struct and PageList,
1530
             * but for devices with a custom finalize (eg psdcmyk) it might
1531
             * also free memory it had allocated, and we're still pointing
1532
             * at that memory in the parent. The indirection through a
1533
             * variable is just to get rid of const warnings.
1534
             */
1535
0
            b_std = (gs_memory_struct_type_t *)child->stype;
1536
0
            gs_free_const_object(dev->memory->non_gc_memory, b_std, "gs_device_unsubclass(stype)");
1537
            /* Make this into a generic device */
1538
0
            child->stype = &st_device;
1539
0
            child->stype_is_dynamic = false;
1540
1541
            /* We can't simply discard the child device, because there may be references to it elsewhere,
1542
               but equally, we really don't want it doing anything, so set the procs so actions are just discarded.
1543
             */
1544
0
            gx_copy_device_procs(child, (gx_device *)&gs_null_device, (gx_device *)&gs_null_device);
1545
1546
            /* Having changed the stype, we need to make sure the memory
1547
             * manager uses it. It keeps a copy in its own data structure,
1548
             * and would use that copy, which would mean it would call the
1549
             * finalize routine that we just patched out.
1550
             */
1551
0
            gs_set_object_type(dev->memory->stable_memory, child, child->stype);
1552
0
            child->finalize = NULL;
1553
            /* Now (finally) free the child memory */
1554
0
            rc_decrement(child, "gx_device_unsubclass(device)");
1555
0
        }
1556
0
    }
1557
0
    dev->parent = parent;
1558
1559
    /* If this device has a dynamic stype, we wnt to keep using it, but we copied
1560
     * the stype pointer from the child when we copied the rest of the device. So
1561
     * we update the stype pointer with the saved pointer to this device's stype.
1562
     */
1563
0
    if (dynamic) {
1564
0
        dev->stype = a_std;
1565
0
        dev->stype_is_dynamic = 1;
1566
0
    } else {
1567
0
        dev->stype_is_dynamic = 0;
1568
0
    }
1569
0
}
1570
1571
int gx_update_from_subclass(gx_device *dev)
1572
177k
{
1573
177k
    if (!dev->child)
1574
0
        return 0;
1575
1576
177k
    memcpy(&dev->color_info, &dev->child->color_info, sizeof(gx_device_color_info));
1577
177k
    memcpy(&dev->cached_colors, &dev->child->cached_colors, sizeof(gx_device_cached_colors_t));
1578
177k
    dev->max_fill_band = dev->child->max_fill_band;
1579
177k
    dev->width = dev->child->width;
1580
177k
    dev->height = dev->child->height;
1581
177k
    dev->pad = dev->child->pad;
1582
177k
    dev->log2_align_mod = dev->child->log2_align_mod;
1583
177k
    dev->max_fill_band = dev->child->max_fill_band;
1584
177k
    dev->num_planar_planes = dev->child->num_planar_planes;
1585
177k
    dev->LeadingEdge = dev->child->LeadingEdge;
1586
177k
    memcpy(&dev->ImagingBBox, &dev->child->ImagingBBox, sizeof(dev->child->ImagingBBox));
1587
177k
    dev->ImagingBBox_set = dev->child->ImagingBBox_set;
1588
177k
    memcpy(&dev->MediaSize, &dev->child->MediaSize, sizeof(dev->child->MediaSize));
1589
177k
    memcpy(&dev->HWResolution, &dev->child->HWResolution, sizeof(dev->child->HWResolution));
1590
177k
    memcpy(&dev->Margins, &dev->child->Margins, sizeof(dev->child->Margins));
1591
177k
    memcpy(&dev->HWMargins, &dev->child->HWMargins, sizeof(dev->child->HWMargins));
1592
177k
    dev->FirstPage = dev->child->FirstPage;
1593
177k
    dev->LastPage = dev->child->LastPage;
1594
177k
    dev->PageCount = dev->child->PageCount;
1595
177k
    dev->ShowpageCount = dev->child->ShowpageCount;
1596
177k
    dev->NumCopies = dev->child->NumCopies;
1597
177k
    dev->NumCopies_set = dev->child->NumCopies_set;
1598
177k
    dev->IgnoreNumCopies = dev->child->IgnoreNumCopies;
1599
177k
    dev->UseCIEColor = dev->child->UseCIEColor;
1600
177k
    dev->LockSafetyParams= dev->child->LockSafetyParams;
1601
177k
    dev->band_offset_x = dev->child->band_offset_y;
1602
177k
    dev->sgr = dev->child->sgr;
1603
177k
    dev->MaxPatternBitmap = dev->child->MaxPatternBitmap;
1604
177k
    dev->page_uses_transparency = dev->child->page_uses_transparency;
1605
177k
    memcpy(&dev->space_params, &dev->child->space_params, sizeof(gdev_space_params));
1606
177k
    dev->graphics_type_tag = dev->child->graphics_type_tag;
1607
1608
177k
    return 0;
1609
177k
}
1610
1611
int gx_subclass_composite(gx_device *dev, gx_device **pcdev, const gs_composite_t *pcte,
1612
    gs_gstate *pgs, gs_memory_t *memory, gx_device *cdev)
1613
0
{
1614
0
    pdf14_clist_device *p14dev;
1615
0
    generic_subclass_data *psubclass_data;
1616
0
    int code = 0;
1617
1618
0
    p14dev = (pdf14_clist_device *)dev;
1619
0
    psubclass_data = (generic_subclass_data *)p14dev->target->subclass_data;
1620
1621
0
    set_dev_proc(dev, composite, psubclass_data->saved_compositor_method);
1622
1623
0
    if (gs_is_pdf14trans_compositor(pcte) != 0 && strncmp(dev->dname, "pdf14clist", 10) == 0) {
1624
0
        const gs_pdf14trans_t * pdf14pct = (const gs_pdf14trans_t *) pcte;
1625
1626
0
        switch (pdf14pct->params.pdf14_op) {
1627
0
            case PDF14_POP_DEVICE:
1628
0
                {
1629
0
                    pdf14_clist_device *p14dev = (pdf14_clist_device *)dev;
1630
0
                    gx_device *subclass_device;
1631
1632
0
                    p14dev->target->color_info = p14dev->saved_target_color_info;
1633
0
                    if (p14dev->target->child) {
1634
0
                        p14dev->target->child->color_info = p14dev->saved_target_color_info;
1635
1636
0
                        set_dev_proc(p14dev->target->child, encode_color, p14dev->saved_target_encode_color);
1637
0
                        set_dev_proc(p14dev->target->child, decode_color, p14dev->saved_target_decode_color);
1638
0
                        set_dev_proc(p14dev->target->child, get_color_mapping_procs, p14dev->saved_target_get_color_mapping_procs);
1639
0
                        set_dev_proc(p14dev->target->child, get_color_comp_index, p14dev->saved_target_get_color_comp_index);
1640
0
                    }
1641
1642
0
                    pgs->get_cmap_procs = p14dev->save_get_cmap_procs;
1643
0
                    gx_set_cmap_procs(pgs, p14dev->target);
1644
1645
0
                    subclass_device = p14dev->target;
1646
0
                    p14dev->target = p14dev->target->child;
1647
1648
0
                    code = dev_proc(dev, composite)(dev, pcdev, pcte, pgs, memory, cdev);
1649
1650
0
                    p14dev->target = subclass_device;
1651
1652
                    /* We return 0, rather than 1, as we have not created
1653
                     * a new compositor that wraps dev. */
1654
0
                    if (code == 1)
1655
0
                        code = 0;
1656
0
                    return code;
1657
0
                }
1658
0
                break;
1659
0
            default:
1660
0
                code = dev_proc(dev, composite)(dev, pcdev, pcte, pgs, memory, cdev);
1661
0
                break;
1662
0
        }
1663
0
    } else {
1664
0
        code = dev_proc(dev, composite)(dev, pcdev, pcte, pgs, memory, cdev);
1665
0
    }
1666
0
    set_dev_proc(dev, composite, gx_subclass_composite);
1667
0
    return code;
1668
0
}
1669
1670
typedef enum
1671
{
1672
    transform_pixel_region_portrait,
1673
    transform_pixel_region_landscape,
1674
    transform_pixel_region_skew
1675
} transform_pixel_region_posture;
1676
1677
typedef struct gx_default_transform_pixel_region_state_s gx_default_transform_pixel_region_state_t;
1678
1679
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);
1680
1681
struct gx_default_transform_pixel_region_state_s
1682
{
1683
    gs_memory_t *mem;
1684
    gx_dda_fixed_point pixels;
1685
    gx_dda_fixed_point rows;
1686
    gs_int_rect clip;
1687
    int w;
1688
    int h;
1689
    int spp;
1690
    transform_pixel_region_posture posture;
1691
    gs_logical_operation_t lop;
1692
    byte *line;
1693
    gx_default_transform_pixel_region_render_fn *render;
1694
};
1695
1696
static void
1697
get_portrait_y_extent(gx_default_transform_pixel_region_state_t *state, int *iy, int *ih)
1698
7.55M
{
1699
7.55M
    fixed y0, y1;
1700
7.55M
    gx_dda_fixed row = state->rows.y;
1701
1702
7.55M
    y0 = dda_current(row);
1703
7.55M
    dda_next(row);
1704
7.55M
    y1 = dda_current(row);
1705
1706
7.55M
    if (y1 < y0) {
1707
2.29M
        fixed t = y1; y1 = y0; y0 = t;
1708
2.29M
    }
1709
1710
7.55M
    *iy = fixed2int_pixround_perfect(y0);
1711
7.55M
    *ih = fixed2int_pixround_perfect(y1) - *iy;
1712
7.55M
}
1713
1714
static void
1715
get_landscape_x_extent(gx_default_transform_pixel_region_state_t *state, int *ix, int *iw)
1716
10.6k
{
1717
10.6k
    fixed x0, x1;
1718
10.6k
    gx_dda_fixed row = state->rows.x;
1719
1720
10.6k
    x0 = dda_current(row);
1721
10.6k
    dda_next(row);
1722
10.6k
    x1 = dda_current(row);
1723
1724
10.6k
    if (x1 < x0) {
1725
294
        fixed t = x1; x1 = x0; x0 = t;
1726
294
    }
1727
1728
10.6k
    *ix = fixed2int_pixround_perfect(x0);
1729
10.6k
    *iw = fixed2int_pixround_perfect(x1) - *ix;
1730
10.6k
}
1731
1732
static void
1733
get_skew_extents(gx_default_transform_pixel_region_state_t *state, fixed *w, fixed *h)
1734
19.3k
{
1735
19.3k
    fixed x0, x1, y0, y1;
1736
19.3k
    gx_dda_fixed_point row = state->rows;
1737
1738
19.3k
    x0 = dda_current(row.x);
1739
19.3k
    y0 = dda_current(row.y);
1740
19.3k
    dda_next(row.x);
1741
19.3k
    dda_next(row.y);
1742
19.3k
    x1 = dda_current(row.x);
1743
19.3k
    y1 = dda_current(row.y);
1744
1745
19.3k
    *w = x1-x0;
1746
19.3k
    *h = y1-y0;
1747
19.3k
}
1748
1749
static int
1750
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)
1751
3.67M
{
1752
3.67M
    gs_logical_operation_t lop = state->lop;
1753
3.67M
    gx_dda_fixed_point pnext;
1754
3.67M
    int vci, vdi;
1755
3.67M
    int irun;     /* int x/rrun */
1756
3.67M
    int w = state->w;
1757
3.67M
    int h = state->h;
1758
3.67M
    int spp = state->spp;
1759
3.67M
    const byte *data = buffer[0] + data_x * spp;
1760
3.67M
    const byte *bufend = NULL;
1761
3.67M
    int code = 0;
1762
3.67M
    const byte *run = NULL;
1763
3.67M
    int k;
1764
3.67M
    gx_color_value *conc = &cmapper->conc[0];
1765
3.67M
    int to_rects;
1766
3.67M
    gx_cmapper_fn *mapper = cmapper->set_color;
1767
3.67M
    int minx, maxx;
1768
1769
3.67M
    if (h == 0)
1770
0
        return 0;
1771
1772
    /* Clip on Y */
1773
3.67M
    get_portrait_y_extent(state, &vci, &vdi);
1774
3.67M
    if (vci < state->clip.p.y)
1775
219k
        vdi += vci - state->clip.p.y, vci = state->clip.p.y;
1776
3.67M
    if (vci+vdi > state->clip.q.y)
1777
97.1k
        vdi = state->clip.q.y - vci;
1778
3.67M
    if (vdi <= 0)
1779
1.36M
        return 0;
1780
1781
2.31M
    pnext = state->pixels;
1782
2.31M
    dda_translate(pnext.x,  (-fixed_epsilon));
1783
2.31M
    irun = fixed2int_var_rounded(dda_current(pnext.x));
1784
2.31M
    if_debug5m('b', dev->memory, "[b]y=%d data_x=%d w=%d xt=%f yt=%f\n",
1785
2.31M
               vci, data_x, w, fixed2float(dda_current(pnext.x)), fixed2float(dda_current(pnext.y)));
1786
2.31M
    to_rects = (dev->color_info.depth != spp*8);
1787
2.31M
    if (to_rects == 0) {
1788
2.30M
        if (dev_proc(dev, dev_spec_op)(dev, gxdso_copy_color_is_fast, NULL, 0) <= 0)
1789
2.23M
            to_rects = 1;
1790
2.30M
    }
1791
1792
2.31M
    minx = state->clip.p.x;
1793
2.31M
    maxx = state->clip.q.x;
1794
2.31M
    bufend = data + w * spp;
1795
2.31M
    if (to_rects) {
1796
312M
        while (data < bufend) {
1797
            /* Find the length of the next run. It will either end when we hit
1798
             * the end of the source data, or when the pixel data differs. */
1799
310M
            run = data + spp;
1800
1.02G
            while (1) {
1801
1.02G
                dda_next(pnext.x);
1802
1.02G
                if (run >= bufend)
1803
2.24M
                    break;
1804
1.02G
                if (memcmp(run, data, spp))
1805
307M
                    break;
1806
719M
                run += spp;
1807
719M
            }
1808
            /* So we have a run of pixels from data to run that are all the same. */
1809
            /* This needs to be sped up */
1810
1.22G
            for (k = 0; k < spp; k++) {
1811
910M
                conc[k] = gx_color_value_from_byte(data[k]);
1812
910M
            }
1813
310M
            mapper(cmapper);
1814
            /* Fill the region between irun and fixed2int_var_rounded(pnext.x) */
1815
310M
            {
1816
310M
                int xi = irun;
1817
310M
                int wi = (irun = fixed2int_var_rounded(dda_current(pnext.x))) - xi;
1818
1819
310M
                if (wi < 0)
1820
11.9k
                    xi += wi, wi = -wi;
1821
310M
                if (xi < minx)
1822
99.8k
                    wi += xi - minx, xi = minx;
1823
310M
                if (xi + wi > maxx)
1824
303k
                    wi = maxx - xi;
1825
310M
                if (wi > 0)
1826
297M
                    code = gx_fill_rectangle_device_rop(xi, vci, wi, vdi,
1827
310M
                                                        &cmapper->devc, dev, lop);
1828
310M
            }
1829
310M
            if (code < 0)
1830
0
                goto err;
1831
310M
            data = run;
1832
310M
        }
1833
2.24M
    } else {
1834
70.6k
        int pending_left = irun;
1835
70.6k
        int pending_right;
1836
70.6k
        byte *out;
1837
70.6k
        int depth = spp;
1838
70.6k
        if (state->line == NULL) {
1839
577
            state->line = gs_alloc_bytes(state->mem,
1840
577
                                         (size_t)dev->width * depth,
1841
577
                                         "image line");
1842
577
            if (state->line == NULL)
1843
0
                return gs_error_VMerror;
1844
577
        }
1845
70.6k
        out = state->line;
1846
1847
70.6k
        if (minx < 0)
1848
0
            minx = 0;
1849
70.6k
        if (maxx > dev->width)
1850
0
            maxx = dev->width;
1851
1852
70.6k
        if (pending_left < minx)
1853
231
            pending_left = minx;
1854
70.4k
        else if (pending_left > maxx)
1855
1
            pending_left = maxx;
1856
70.6k
        pending_right = pending_left;
1857
1858
10.8M
        while (data < bufend) {
1859
            /* Find the length of the next run. It will either end when we hit
1860
             * the end of the source data, or when the pixel data differs. */
1861
10.7M
            run = data + spp;
1862
36.9M
            while (1) {
1863
36.9M
                dda_next(pnext.x);
1864
36.9M
                if (run >= bufend)
1865
70.6k
                    break;
1866
36.8M
                if (memcmp(run, data, spp))
1867
10.7M
                    break;
1868
26.1M
                run += spp;
1869
26.1M
            }
1870
            /* So we have a run of pixels from data to run that are all the same. */
1871
            /* This needs to be sped up */
1872
34.2M
            for (k = 0; k < spp; k++) {
1873
23.4M
                conc[k] = gx_color_value_from_byte(data[k]);
1874
23.4M
            }
1875
10.7M
            mapper(cmapper);
1876
            /* Fill the region between irun and fixed2int_var_rounded(pnext.x) */
1877
10.7M
            {
1878
10.7M
                int xi = irun;
1879
10.7M
                int wi = (irun = fixed2int_var_rounded(dda_current(pnext.x))) - xi;
1880
1881
10.7M
                if (wi < 0)
1882
166
                    xi += wi, wi = -wi;
1883
1884
10.7M
                if (xi < minx)
1885
454
                    wi += xi - minx, xi = minx;
1886
10.7M
                if (xi + wi > maxx)
1887
153k
                    wi = maxx - xi;
1888
1889
10.7M
                if (wi > 0) {
1890
10.2M
                    if (color_is_pure(&cmapper->devc)) {
1891
10.2M
                        gx_color_index color = cmapper->devc.colors.pure;
1892
10.2M
                        int xii = xi * spp;
1893
1894
10.2M
                        if (pending_left > xi)
1895
166
                            pending_left = xi;
1896
10.2M
                        else
1897
10.2M
                            pending_right = xi + wi;
1898
42.6M
                        do {
1899
                            /* Excuse the double shifts below, that's to stop the
1900
                             * C compiler complaining if the color index type is
1901
                             * 32 bits. */
1902
42.6M
                            switch(depth)
1903
42.6M
                            {
1904
0
                            case 8: out[xii++] = ((color>>28)>>28) & 0xff;
1905
0
                            case 7: out[xii++] = ((color>>24)>>24) & 0xff;
1906
0
                            case 6: out[xii++] = ((color>>24)>>16) & 0xff;
1907
0
                            case 5: out[xii++] = ((color>>24)>>8) & 0xff;
1908
0
                            case 4: out[xii++] = (color>>24) & 0xff;
1909
21.2M
                            case 3: out[xii++] = (color>>16) & 0xff;
1910
21.2M
                            case 2: out[xii++] = (color>>8) & 0xff;
1911
42.6M
                            case 1: out[xii++] = color & 0xff;
1912
42.6M
                            }
1913
42.6M
                        } while (--wi != 0);
1914
10.2M
                    } else {
1915
0
                        if (pending_left != pending_right) {
1916
0
                            code = dev_proc(dev, copy_color)(dev, out, pending_left, 0, 0, pending_left, vci, pending_right - pending_left, vdi);
1917
0
                            if (code < 0)
1918
0
                                goto err;
1919
0
                        }
1920
0
                        pending_left = pending_right = xi + (pending_left > xi ? 0 : wi);
1921
0
                        code = gx_fill_rectangle_device_rop(xi, vci, wi, vdi,
1922
0
                                                            &cmapper->devc, dev, lop);
1923
0
                    }
1924
10.2M
                }
1925
10.7M
                if (code < 0)
1926
0
                    goto err;
1927
10.7M
            }
1928
10.7M
            data = run;
1929
10.7M
        }
1930
70.6k
        if (pending_left != pending_right) {
1931
70.6k
            code = dev_proc(dev, copy_color)(dev, out, pending_left, 0, 0, pending_left, vci, pending_right - pending_left, vdi);
1932
70.6k
            if (code < 0)
1933
0
                goto err;
1934
70.6k
        }
1935
70.6k
    }
1936
2.31M
    return 1;
1937
    /* Save position if error, in case we resume. */
1938
0
err:
1939
0
    buffer[0] = run;
1940
0
    return code;
1941
2.31M
}
1942
1943
static int
1944
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)
1945
5.30k
{
1946
5.30k
    gs_logical_operation_t lop = state->lop;
1947
5.30k
    gx_dda_fixed_point pnext;
1948
5.30k
    int vci, vdi;
1949
5.30k
    int irun;     /* int x/rrun */
1950
5.30k
    int w = state->w;
1951
5.30k
    int h = state->h;
1952
5.30k
    int spp = state->spp;
1953
5.30k
    const byte *data = buffer[0] + data_x * spp;
1954
5.30k
    const byte *bufend = NULL;
1955
5.30k
    int code = 0;
1956
5.30k
    const byte *run;
1957
5.30k
    int k;
1958
5.30k
    gx_color_value *conc = &cmapper->conc[0];
1959
5.30k
    int to_rects;
1960
5.30k
    gx_cmapper_fn *mapper = cmapper->set_color;
1961
5.30k
    int miny, maxy;
1962
1963
5.30k
    if (h == 0)
1964
0
        return 0;
1965
1966
    /* Clip on X */
1967
5.30k
    get_landscape_x_extent(state, &vci, &vdi);
1968
5.30k
    if (vci < state->clip.p.x)
1969
0
        vdi += vci - state->clip.p.x, vci = state->clip.p.x;
1970
5.30k
    if (vci+vdi > state->clip.q.x)
1971
0
        vdi = state->clip.q.x - vci;
1972
5.30k
    if (vdi <= 0)
1973
795
        return 0;
1974
1975
4.50k
    pnext = state->pixels;
1976
4.50k
    dda_translate(pnext.x,  (-fixed_epsilon));
1977
4.50k
    irun = fixed2int_var_rounded(dda_current(pnext.y));
1978
4.50k
    if_debug5m('b', dev->memory, "[b]y=%d data_x=%d w=%d xt=%f yt=%f\n",
1979
4.50k
               vci, data_x, w, fixed2float(dda_current(pnext.x)), fixed2float(dda_current(pnext.y)));
1980
4.50k
    to_rects = (dev->color_info.depth != spp*8);
1981
4.50k
    if (to_rects == 0) {
1982
4.50k
        if (dev_proc(dev, dev_spec_op)(dev, gxdso_copy_color_is_fast, NULL, 0) <= 0)
1983
4.49k
            to_rects = 1;
1984
4.50k
    }
1985
1986
4.50k
    miny = state->clip.p.y;
1987
4.50k
    maxy = state->clip.q.y;
1988
4.50k
    bufend = data + w * spp;
1989
26.9k
    while (data < bufend) {
1990
        /* Find the length of the next run. It will either end when we hit
1991
         * the end of the source data, or when the pixel data differs. */
1992
22.4k
        run = data + spp;
1993
22.5k
        while (1) {
1994
22.5k
            dda_next(pnext.y);
1995
22.5k
            if (run >= bufend)
1996
4.50k
                break;
1997
18.0k
            if (memcmp(run, data, spp))
1998
17.9k
                break;
1999
87
            run += spp;
2000
87
        }
2001
        /* So we have a run of pixels from data to run that are all the same. */
2002
        /* This needs to be sped up */
2003
89.7k
        for (k = 0; k < spp; k++) {
2004
67.2k
            conc[k] = gx_color_value_from_byte(data[k]);
2005
67.2k
        }
2006
22.4k
        mapper(cmapper);
2007
        /* Fill the region between irun and fixed2int_var_rounded(pnext.y) */
2008
22.4k
        {              /* 90 degree rotated rectangle */
2009
22.4k
            int yi = irun;
2010
22.4k
            int hi = (irun = fixed2int_var_rounded(dda_current(pnext.y))) - yi;
2011
2012
22.4k
            if (hi < 0)
2013
21.4k
                yi += hi, hi = -hi;
2014
22.4k
            if (yi < miny)
2015
4.34k
                hi += yi - miny, yi = miny;
2016
22.4k
            if (yi + hi > maxy)
2017
4.31k
                hi = maxy - yi;
2018
22.4k
            if (hi > 0)
2019
17.5k
                code = gx_fill_rectangle_device_rop(vci, yi, vdi, hi,
2020
22.4k
                                                    &cmapper->devc, dev, lop);
2021
22.4k
        }
2022
22.4k
        if (code < 0)
2023
0
            goto err;
2024
22.4k
        data = run;
2025
22.4k
    }
2026
4.50k
    return 1;
2027
    /* Save position if error, in case we resume. */
2028
0
err:
2029
0
    buffer[0] = run;
2030
0
    return code;
2031
4.50k
}
2032
2033
static int
2034
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)
2035
19.3k
{
2036
19.3k
    gs_logical_operation_t lop = state->lop;
2037
19.3k
    gx_dda_fixed_point pnext;
2038
19.3k
    fixed xprev, yprev;
2039
19.3k
    fixed pdyx, pdyy;   /* edge of parallelogram */
2040
19.3k
    int w = state->w;
2041
19.3k
    int h = state->h;
2042
19.3k
    int spp = state->spp;
2043
19.3k
    const byte *data = buffer[0] + data_x * spp;
2044
19.3k
    fixed xpos;     /* x ditto */
2045
19.3k
    fixed ypos;     /* y ditto */
2046
19.3k
    const byte *bufend = data + w * spp;
2047
19.3k
    int code = 0;
2048
19.3k
    int k;
2049
19.3k
    byte initial_run[GX_DEVICE_COLOR_MAX_COMPONENTS] = { 0 };
2050
19.3k
    const byte *prev = &initial_run[0];
2051
19.3k
    gx_cmapper_fn *mapper = cmapper->set_color;
2052
19.3k
    gx_color_value *conc = &cmapper->conc[0];
2053
2054
19.3k
    if (h == 0)
2055
0
        return 0;
2056
19.3k
    pnext = state->pixels;
2057
19.3k
    get_skew_extents(state, &pdyx, &pdyy);
2058
19.3k
    dda_translate(pnext.x,  (-fixed_epsilon));
2059
19.3k
    xprev = dda_current(pnext.x);
2060
19.3k
    yprev = dda_current(pnext.y);
2061
19.3k
    if_debug4m('b', dev->memory, "[b]y=? data_x=%d w=%d xt=%f yt=%f\n",
2062
19.3k
               data_x, w, fixed2float(xprev), fixed2float(yprev));
2063
19.3k
    initial_run[0] = ~data[0];  /* Force intial setting */
2064
3.60M
    while (data < bufend) {
2065
3.58M
        dda_next(pnext.x);
2066
3.58M
        dda_next(pnext.y);
2067
3.58M
        xpos = dda_current(pnext.x);
2068
3.58M
        ypos = dda_current(pnext.y);
2069
2070
3.58M
        if (memcmp(prev, data, spp) != 0)
2071
1.56M
        {
2072
            /* This needs to be sped up */
2073
5.31M
            for (k = 0; k < spp; k++) {
2074
3.74M
                conc[k] = gx_color_value_from_byte(data[k]);
2075
3.74M
            }
2076
1.56M
            mapper(cmapper);
2077
1.56M
        }
2078
        /* Fill the region between */
2079
        /* xprev/yprev and xpos/ypos */
2080
        /* Parallelogram */
2081
3.58M
        code = (*dev_proc(dev, fill_parallelogram))
2082
3.58M
                    (dev, xprev, yprev, xpos - xprev, ypos - yprev, pdyx, pdyy,
2083
3.58M
                     &cmapper->devc, lop);
2084
3.58M
        xprev = xpos;
2085
3.58M
        yprev = ypos;
2086
3.58M
        if (code < 0)
2087
0
            goto err;
2088
3.58M
        prev = data;
2089
3.58M
        data += spp;
2090
3.58M
    }
2091
19.3k
    return 1;
2092
    /* Save position if error, in case we resume. */
2093
0
err:
2094
    /* Only set buffer[0] if we've managed to set prev to something valid. */
2095
0
    if (prev != &initial_run[0]) buffer[0] = prev;
2096
0
    return code;
2097
19.3k
}
2098
2099
static int
2100
gx_default_transform_pixel_region_begin(gx_device *dev, int w, int h, int spp,
2101
                             const gx_dda_fixed_point *pixels, const gx_dda_fixed_point *rows,
2102
                             const gs_int_rect *clip, gs_logical_operation_t lop,
2103
                             gx_default_transform_pixel_region_state_t **statep)
2104
267k
{
2105
267k
    gx_default_transform_pixel_region_state_t *state;
2106
267k
    gs_memory_t *mem = dev->memory->non_gc_memory;
2107
2108
267k
    *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");
2109
267k
    if (state == NULL)
2110
0
        return gs_error_VMerror;
2111
267k
    state->mem = mem;
2112
267k
    state->rows = *rows;
2113
267k
    state->pixels = *pixels;
2114
267k
    state->clip = *clip;
2115
267k
    state->w = w;
2116
267k
    state->h = h;
2117
267k
    state->spp = spp;
2118
267k
    state->lop = lop;
2119
267k
    state->line = NULL;
2120
2121
    /* FIXME: Consider sheers here too. Probably happens rarely enough not to be worth it. */
2122
267k
    if (rows->x.step.dQ == 0 && rows->x.step.dR == 0 && pixels->y.step.dQ == 0 && pixels->y.step.dR == 0)
2123
265k
        state->posture = transform_pixel_region_portrait;
2124
1.19k
    else if (rows->y.step.dQ == 0 && rows->y.step.dR == 0 && pixels->x.step.dQ == 0 && pixels->x.step.dR == 0)
2125
609
        state->posture = transform_pixel_region_landscape;
2126
585
    else
2127
585
        state->posture = transform_pixel_region_skew;
2128
2129
267k
    if (state->posture == transform_pixel_region_portrait)
2130
265k
        state->render = transform_pixel_region_render_portrait;
2131
1.19k
    else if (state->posture == transform_pixel_region_landscape)
2132
609
        state->render = transform_pixel_region_render_landscape;
2133
585
    else
2134
585
        state->render = transform_pixel_region_render_skew;
2135
2136
267k
    return 0;
2137
267k
}
2138
2139
static void
2140
step_to_next_line(gx_default_transform_pixel_region_state_t *state)
2141
3.90M
{
2142
3.90M
    fixed x = dda_current(state->rows.x);
2143
3.90M
    fixed y = dda_current(state->rows.y);
2144
2145
3.90M
    dda_next(state->rows.x);
2146
3.90M
    dda_next(state->rows.y);
2147
3.90M
    x = dda_current(state->rows.x) - x;
2148
3.90M
    y = dda_current(state->rows.y) - y;
2149
3.90M
    dda_translate(state->pixels.x, x);
2150
3.90M
    dda_translate(state->pixels.y, y);
2151
3.90M
}
2152
2153
static int
2154
gx_default_transform_pixel_region_data_needed(gx_device *dev, gx_default_transform_pixel_region_state_t *state)
2155
3.90M
{
2156
3.90M
    if (state->posture == transform_pixel_region_portrait) {
2157
3.87M
        int iy, ih;
2158
2159
3.87M
        get_portrait_y_extent(state, &iy, &ih);
2160
2161
3.87M
        if (iy + ih < state->clip.p.y || iy >= state->clip.q.y) {
2162
            /* Skip this line. */
2163
202k
            step_to_next_line(state);
2164
202k
            return 0;
2165
202k
        }
2166
3.87M
    } else if (state->posture == transform_pixel_region_landscape) {
2167
5.30k
        int ix, iw;
2168
2169
5.30k
        get_landscape_x_extent(state, &ix, &iw);
2170
2171
5.30k
        if (ix + iw < state->clip.p.x || ix >= state->clip.q.x) {
2172
            /* Skip this line. */
2173
0
            step_to_next_line(state);
2174
0
            return 0;
2175
0
        }
2176
5.30k
    }
2177
2178
3.70M
    return 1;
2179
3.90M
}
2180
2181
static int
2182
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)
2183
3.70M
{
2184
3.70M
    int ret = state->render(dev, state, buffer, data_x, cmapper, pgs);
2185
2186
3.70M
    step_to_next_line(state);
2187
3.70M
    return ret;
2188
3.70M
}
2189
2190
static int
2191
gx_default_transform_pixel_region_end(gx_device *dev, gx_default_transform_pixel_region_state_t *state)
2192
267k
{
2193
267k
    if (state) {
2194
267k
        gs_free_object(state->mem, state->line, "image line");
2195
267k
        gs_free_object(state->mem, state, "gx_default_transform_pixel_region_state_t");
2196
267k
    }
2197
267k
    return 0;
2198
267k
}
2199
2200
int
2201
gx_default_transform_pixel_region(gx_device *dev,
2202
                       transform_pixel_region_reason reason,
2203
                       transform_pixel_region_data *data)
2204
8.13M
{
2205
8.13M
    gx_default_transform_pixel_region_state_t *state = (gx_default_transform_pixel_region_state_t *)data->state;
2206
2207
8.13M
    switch (reason)
2208
8.13M
    {
2209
267k
    case transform_pixel_region_begin:
2210
267k
        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);
2211
3.90M
    case transform_pixel_region_data_needed:
2212
3.90M
        return gx_default_transform_pixel_region_data_needed(dev, state);
2213
3.70M
    case transform_pixel_region_process_data:
2214
3.70M
        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);
2215
267k
    case transform_pixel_region_end:
2216
267k
        data->state = NULL;
2217
267k
        return gx_default_transform_pixel_region_end(dev, state);
2218
0
    default:
2219
0
        return gs_error_unknownerror;
2220
8.13M
    }
2221
8.13M
}