Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/base/gxclimag.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2022 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.,  1305 Grant Avenue - Suite 200, Novato,
13
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14
*/
15
16
17
/* Higher-level image operations for band lists */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "string_.h"    /* for strcmp */
21
#include "gx.h"
22
#include "gserrors.h"
23
#include "gscspace.h"
24
#include "gscdefs.h"            /* for image type table */
25
#include "gxarith.h"
26
#include "gxcspace.h"
27
#include "gxpcolor.h"
28
#include "gxdevice.h"
29
#include "gxdevmem.h"           /* must precede gxcldev.h */
30
#include "gxcldev.h"
31
#include "gxclpath.h"
32
#include "gxfmap.h"
33
#include "gxiparam.h"
34
#include "gxpath.h"
35
#include "stream.h"
36
#include "strimpl.h"            /* for sisparam.h */
37
#include "sisparam.h"
38
#include "gxcomp.h"
39
#include "gsserial.h"
40
#include "gxdhtserial.h"
41
#include "gsptype1.h"
42
#include "gsicc_manage.h"
43
#include "gsicc_cache.h"
44
#include "gxdevsop.h"
45
#include "gscindex.h"
46
#include "gsicc_cms.h"
47
#include "gximdecode.h"
48
49
extern_gx_image_type_table();
50
51
/* Define whether we should use high-level images. */
52
/* (See below for additional restrictions.) */
53
static const bool USE_HL_IMAGES = true;
54
55
/* Forward references */
56
static int cmd_put_set_data_x(gx_device_clist_writer * cldev,
57
                               gx_clist_state * pcls, int data_x);
58
static bool check_rect_for_trivial_clip(
59
    const gx_clip_path *pcpath,  /* May be NULL, clip to evaluate */
60
    int px, int py, int qx, int qy  /* corners of box to test */
61
);
62
63
static bool
64
palette_has_color(const gs_color_space *pcs, const gs_pixel_image_t * const pim)
65
0
{
66
0
    gs_color_space *pbcs = pcs->base_space;
67
0
    gs_color_space_index base_type = gs_color_space_get_index(pbcs);
68
0
    bool ((*is_neutral)(void*, int));
69
0
    int bps = pim->BitsPerComponent;
70
0
    int num_entries = 1 << bps;
71
0
    int k;
72
0
    byte psrc[4];
73
74
0
    switch(base_type) {
75
76
0
    case gs_color_space_index_DeviceGray:
77
0
    case gs_color_space_index_CIEA:
78
0
        return false;
79
0
        break;
80
81
0
    case gs_color_space_index_DeviceRGB:
82
0
    case gs_color_space_index_CIEABC:
83
0
    case gs_color_space_index_CIEDEF:
84
0
        is_neutral = &gsicc_mcm_monitor_rgb;
85
0
        break;
86
87
0
    case gs_color_space_index_DeviceCMYK:
88
0
    case gs_color_space_index_CIEDEFG:
89
0
        is_neutral = &gsicc_mcm_monitor_cmyk;
90
0
        break;
91
92
0
     case gs_color_space_index_DevicePixel:
93
0
     case gs_color_space_index_DeviceN:
94
0
     case gs_color_space_index_Separation:
95
0
     case gs_color_space_index_Indexed:
96
0
     case gs_color_space_index_Pattern:
97
0
        return true;
98
0
        break;
99
100
0
     case gs_color_space_index_ICC:
101
0
        switch(pbcs->cmm_icc_profile_data->data_cs) {
102
0
        case gsRGB:
103
0
            is_neutral = &gsicc_mcm_monitor_rgb;
104
0
            break;
105
106
0
        case gsCMYK:
107
0
            is_neutral = &gsicc_mcm_monitor_cmyk;
108
0
            break;
109
110
0
        case gsCIELAB:
111
0
            is_neutral = &gsicc_mcm_monitor_lab;
112
0
            break;
113
114
0
        default:
115
0
            return true;
116
0
        }
117
0
        break;
118
0
     default:
119
0
        return true;
120
0
    }
121
    /* Now go through the palette with the check color function */
122
0
    for (k = 0; k < num_entries; k++) {
123
0
        (void)gs_cspace_indexed_lookup_bytes(pcs, (float) k, psrc); /* this always returns 0 */
124
0
        if (!is_neutral(psrc, 1)) {
125
            /* Has color end this now */
126
0
            return true;
127
0
        }
128
0
    }
129
    /* Must not have color */
130
0
    return false;
131
0
}
132
133
134
/* ------ Driver procedures ------ */
135
136
int
137
clist_fill_mask(gx_device * dev,
138
                const byte * data, int data_x, int raster, gx_bitmap_id id,
139
                int rx, int ry, int rwidth, int rheight,
140
                const gx_drawing_color * pdcolor, int depth,
141
                gs_logical_operation_t lop, const gx_clip_path * pcpath)
142
11.2M
{
143
11.2M
    gx_device_clist_writer * const cdev =
144
11.2M
        &((gx_device_clist *)dev)->writer;
145
11.2M
    const byte *orig_data = data;       /* for writing tile */
146
11.2M
    int orig_data_x = data_x;   /* ditto */
147
11.2M
    int orig_x = rx;            /* ditto */
148
11.2M
    int orig_width = rwidth;    /* ditto */
149
11.2M
    int orig_height = rheight;  /* ditto */
150
11.2M
    int y0;
151
11.2M
    byte copy_op =
152
11.2M
        (depth > 1 ? cmd_op_copy_color_alpha :
153
11.2M
         cmd_op_copy_mono_planes);  /* Plane not needed here */
154
11.2M
    bool slow_rop =
155
11.2M
        cmd_slow_rop(dev, lop_know_S_0(lop), pdcolor) ||
156
11.2M
        cmd_slow_rop(dev, lop_know_S_1(lop), pdcolor);
157
11.2M
    cmd_rects_enum_t re;
158
159
    /* If depth > 1, this call will be translated to a copy_alpha call. */
160
    /* if the target device can't perform copy_alpha, exit now. */
161
11.2M
    if (depth > 1 && (cdev->disable_mask & clist_disable_copy_alpha) != 0)
162
0
        return_error(gs_error_unknownerror);
163
164
11.2M
    crop_copy(cdev, data, data_x, raster, id, rx, ry, rwidth, rheight);
165
11.2M
    if (rwidth <= 0 || rheight <= 0)
166
1.96M
        return 0;
167
9.26M
    y0 = ry;                    /* must do after fit_copy */
168
169
    /* If non-trivial clipping & complex clipping disabled, default */
170
    /* Also default for uncached bitmap or non-default lop; */
171
    /* We could handle more RasterOp cases here directly, but it */
172
    /* doesn't seem worth the trouble right now. */
173
    /* Lastly, the command list will translate calls with depth > 1 to */
174
    /* copy_alpha calls, so the device color must be pure */
175
9.26M
    if (((cdev->disable_mask & clist_disable_complex_clip) &&
176
9.26M
         !check_rect_for_trivial_clip(pcpath, rx, ry, rx + rwidth, ry + rheight)) ||
177
9.26M
        gs_debug_c('`') || id == gx_no_bitmap_id || lop != lop_default ||
178
9.26M
        (depth > 1 && !color_writes_pure(pdcolor, lop))
179
9.26M
        )
180
36.8k
  copy:
181
36.8k
        return gx_default_fill_mask(dev, data, data_x, raster, id,
182
36.8k
                                    rx, ry, rwidth, rheight, pdcolor, depth,
183
36.8k
                                    lop, pcpath);
184
185
9.24M
    if (cmd_check_clip_path(cdev, pcpath))
186
29.1k
        cmd_clear_known(cdev, clip_path_known);
187
9.24M
    if (cdev->permanent_error < 0)
188
0
      return (cdev->permanent_error);
189
    /* If needed, update the trans_bbox */
190
9.24M
    if (cdev->pdf14_needed) {
191
2.61M
        gs_int_rect bbox;
192
193
2.61M
        bbox.p.x = rx;
194
2.61M
        bbox.q.x = rx + rwidth - 1;
195
2.61M
        bbox.p.y = ry;
196
2.61M
        bbox.q.y = ry + rheight - 1;
197
198
2.61M
        clist_update_trans_bbox(cdev, &bbox);
199
2.61M
    }
200
9.24M
    RECT_ENUM_INIT(re, ry, rheight);
201
13.1M
    do {
202
13.1M
        int code;
203
13.1M
        ulong offset_temp;
204
205
13.1M
        RECT_STEP_INIT(re);
206
13.1M
        code = cmd_update_lop(cdev, re.pcls, lop);
207
13.1M
        if (code < 0)
208
0
            return code;
209
13.1M
        if (depth > 1 && !re.pcls->color_is_alpha) {
210
0
            byte *dp;
211
212
0
            code = set_cmd_put_op(&dp, cdev, re.pcls, cmd_opv_set_copy_alpha, 1);
213
0
            if (code < 0)
214
0
                return code;
215
0
            re.pcls->color_is_alpha = 1;
216
0
        }
217
13.1M
        code = cmd_do_write_unknown(cdev, re.pcls, clip_path_known);
218
13.1M
        if (code >= 0)
219
13.1M
            code = cmd_do_enable_clip(cdev, re.pcls, pcpath != NULL);
220
13.1M
        if (code < 0)
221
0
            return code;
222
13.1M
        code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re,
223
13.1M
                                     devn_not_tile_fill);
224
13.1M
        if (code == gs_error_unregistered)
225
0
            return code;
226
13.1M
        if (depth > 1 && code >= 0)
227
0
            code = cmd_set_color1(cdev, re.pcls, pdcolor->colors.pure);
228
13.1M
        if (code < 0)
229
44
            return code;
230
13.1M
        re.pcls->color_usage.slow_rop |= slow_rop;
231
        /* Put it in the cache if possible. */
232
13.1M
        if (!cls_has_tile_id(cdev, re.pcls, id, offset_temp)) {
233
12.5M
            gx_strip_bitmap tile;
234
235
12.5M
            tile.data = (byte *) orig_data;     /* actually const */
236
12.5M
            tile.raster = raster;
237
12.5M
            tile.size.x = tile.rep_width = orig_width;
238
12.5M
            tile.size.y = tile.rep_height = orig_height;
239
12.5M
            tile.rep_shift = tile.shift = 0;
240
12.5M
            tile.id = id;
241
12.5M
            tile.num_planes = 1;
242
12.5M
            code = clist_change_bits(cdev, re.pcls, &tile, depth);
243
12.5M
            if (code < 0) {
244
                /* Something went wrong; just copy the bits. */
245
124
                goto copy;
246
124
            }
247
12.5M
        }
248
13.1M
        {
249
13.1M
            gx_cmd_rect rect;
250
13.1M
            int rsize;
251
13.1M
            byte op = copy_op + cmd_copy_use_tile;
252
253
            /* Output a command to copy the entire character. */
254
            /* It will be truncated properly per band. */
255
13.1M
            rect.x = orig_x, rect.y = y0;
256
13.1M
            rect.width = orig_width, rect.height = re.yend - y0;
257
13.1M
            rsize = 1 + cmd_sizexy(rect);
258
13.1M
            if (depth == 1) rsize = rsize + cmd_sizew(0);  /* need planar_height 0 setting */
259
13.1M
            code = (orig_data_x ?
260
13.1M
                    cmd_put_set_data_x(cdev, re.pcls, orig_data_x) : 0);
261
13.1M
            if (code >= 0) {
262
13.1M
                byte *dp;
263
264
13.1M
                code = set_cmd_put_op(&dp, cdev, re.pcls, op, rsize);
265
                /*
266
                 * The following conditional is unnecessary: the two
267
                 * statements inside it should go outside the
268
                 * HANDLE_RECT.  They are here solely to pacify
269
                 * stupid compilers that don't understand that dp
270
                 * will always be set if control gets past the
271
                 * HANDLE_RECT.
272
                 */
273
13.1M
                if (code >= 0) {
274
13.1M
                    dp++;
275
13.1M
                    if (depth == 1) {
276
13.1M
                        cmd_putw(0, &dp);
277
13.1M
                    }
278
13.1M
                    cmd_putxy(rect, &dp);
279
13.1M
                }
280
13.1M
            }
281
13.1M
            if (code < 0)
282
0
                return code;
283
13.1M
            re.pcls->rect = rect;
284
13.1M
        }
285
13.1M
    } while ((re.y += re.height) < re.yend);
286
9.24M
    return 0;
287
9.24M
}
288
289
/* ------ Bitmap image driver procedures ------ */
290
291
/* Define the structure for keeping track of progress through an image. */
292
typedef struct clist_image_enum_s {
293
    gx_image_enum_common;
294
    /* Arguments of begin_image */
295
    gs_pixel_image_t image;     /* only uses Width, Height, Interpolate */
296
    gx_drawing_color dcolor;    /* only pure right now */
297
    gs_int_rect rect;
298
    const gs_gstate *pgs;
299
    const gx_clip_path *pcpath;
300
    /* Set at creation time */
301
    gs_image_format_t format;
302
    gs_int_point support;       /* extra source pixels for interpolation */
303
    int bits_per_plane;         /* bits per pixel per plane */
304
    gs_matrix matrix;           /* image space -> device space */
305
    bool uses_color;
306
    bool masked;
307
    clist_color_space_t color_space;
308
    int ymin, ymax;
309
    gx_color_usage_t color_usage;
310
    /* begin_image command prepared & ready to output */
311
    /****** SIZE COMPUTATION IS WRONG, TIED TO gximage.c, gsmatrix.c ******/
312
    byte begin_image_command[3 +
313
                            /* Width, Height */
314
                            2 * cmd_sizew_max +
315
                            /* ImageMatrix */
316
                            1 + 6 * sizeof(float) +
317
                            /* Decode */
318
                            (GS_IMAGE_MAX_COMPONENTS + 3) / 4 +
319
                              GS_IMAGE_MAX_COMPONENTS * 2 * sizeof(float) +
320
                            /* MaskColors */
321
                            GS_IMAGE_MAX_COMPONENTS * cmd_sizew_max +
322
                            /* rect */
323
                            4 * cmd_sizew_max];
324
    int begin_image_command_length;
325
    /* Updated dynamically */
326
    int y;
327
    bool color_map_is_known;
328
    bool monitor_color;
329
    image_decode_t decode;
330
    byte *buffer;  /* needed for unpacking during monitoring */
331
} clist_image_enum;
332
gs_private_st_suffix_add4(st_clist_image_enum, clist_image_enum,
333
                          "clist_image_enum", clist_image_enum_enum_ptrs,
334
                          clist_image_enum_reloc_ptrs, st_gx_image_enum_common,
335
                          pgs, pcpath, color_space.space, buffer);
336
337
static image_enum_proc_plane_data(clist_image_plane_data);
338
static image_enum_proc_end_image(clist_image_end_image);
339
static const gx_image_enum_procs_t clist_image_enum_procs =
340
{
341
    clist_image_plane_data, clist_image_end_image
342
};
343
344
/* data_size is number of bytes per component, width is number of pixels in the row. */
345
static bool
346
row_has_color(byte *data_ptr, clist_image_enum *pie_c, int data_size, int width)
347
0
{
348
0
    clist_color_space_t pclcs = pie_c->color_space;
349
0
    bool ((*is_neutral)(void*, int));
350
0
    int step_size = data_size * pie_c->decode.spp;
351
0
    byte *ptr;
352
0
    bool is_mono;
353
0
    int k;
354
355
0
    if (pclcs.icc_info.is_lab) {
356
0
        is_neutral = &gsicc_mcm_monitor_lab;
357
0
    } else {
358
0
        switch(pclcs.icc_info.icc_num_components) {
359
0
        case 3:
360
0
            is_neutral = &gsicc_mcm_monitor_rgb;
361
0
            break;
362
0
        case 4:
363
0
            is_neutral = &gsicc_mcm_monitor_cmyk;
364
0
            break;
365
0
        default:
366
0
            return true;
367
0
        }
368
0
    }
369
    /* Now go through the raster line and determine if we have any color. */
370
0
    ptr = data_ptr;
371
0
    for (k = 0; k < width; k++) {
372
0
        is_mono = is_neutral(ptr, data_size);
373
0
        if (!is_mono) {
374
0
            return true;
375
0
        }
376
0
        ptr += step_size;
377
0
    }
378
0
    return false;
379
0
}
380
381
/* Forward declarations */
382
static bool image_band_box(gx_device * dev, const clist_image_enum * pie,
383
                            int y, int h, gs_int_rect * pbox);
384
static int begin_image_command(byte *buf, uint buf_size,
385
                                const gs_image_common_t *pic);
386
static int cmd_image_plane_data(gx_device_clist_writer * cldev,
387
                                 gx_clist_state * pcls,
388
                                 const gx_image_plane_t * planes,
389
                                 const gx_image_enum_common_t * pie,
390
                                 uint bytes_per_plane,
391
                                 const uint * offsets, int dx, int h);
392
static int cmd_image_plane_data_mon(gx_device_clist_writer * cldev,
393
                                 gx_clist_state * pcls,
394
                                 const gx_image_plane_t * planes,
395
                                 const gx_image_enum_common_t * pie,
396
                                 uint bytes_per_plane,
397
                                 const uint * offsets, int dx, int h,
398
                                 bool *found_color);
399
static uint clist_image_unknowns(gx_device *dev,
400
                                  const clist_image_enum *pie);
401
static int write_image_end_all(gx_device *dev,
402
                                const clist_image_enum *pie);
403
404
/*
405
 * Since currently we are limited to writing a single subrectangle of the
406
 * image for each band, images that are rotated by angles other than
407
 * multiples of 90 degrees may wind up writing many copies of the data.
408
 * Eventually we will fix this by breaking up the image into multiple
409
 * subrectangles, but for now, don't use the high-level approach if it would
410
 * cause the data to explode because of this.
411
 */
412
static bool
413
image_matrix_ok_to_band(const gs_matrix * pmat)
414
30.9k
{
415
30.9k
    double t;
416
    /* Detecting a downscale when it's really noscale upsets some
417
     * customers code, so we add a fudge factor in here. This may
418
     * cause us to allow the use of high level images for some downscales
419
     * that are *nearly* noscales, but our code will cope with that. */
420
30.9k
    float one = (float)(1.0 - 1e-5);
421
422
    /* Don't band if the matrix is (nearly) singular. */
423
30.9k
    if (fabs(pmat->xx * pmat->yy - pmat->xy * pmat->yx) < 0.001)
424
453
        return false;
425
    /* If it's portrait, then we encode it if not a downscale */
426
30.4k
    if (is_xxyy(pmat))
427
26.7k
        return (fabs(pmat->xx) >= one) && (fabs(pmat->yy) >= one);
428
    /* If it's landscape, then we encode it if not a downscale */
429
3.69k
    if (is_xyyx(pmat))
430
337
        return (fabs(pmat->xy) >= one) && (fabs(pmat->yx) >= one);
431
    /* Skewed, so do more expensive downscale test */
432
3.36k
    if ((pmat->xx * pmat->xx + pmat->xy * pmat->xy < one) ||
433
3.36k
        (pmat->yx * pmat->yx + pmat->yy * pmat->yy < one))
434
1.26k
        return false;
435
    /* Otherwise only encode it if it doesn't rotate too much */
436
2.10k
    t = (fabs(pmat->xx) + fabs(pmat->yy)) /
437
2.10k
        (fabs(pmat->xy) + fabs(pmat->yx));
438
2.10k
    return (t < 0.2 || t > 5);
439
3.36k
}
440
441
/* Start processing an image. */
442
int
443
clist_begin_typed_image(gx_device * dev, const gs_gstate * pgs,
444
                        const gs_matrix * pmat, const gs_image_common_t * pic,
445
                        const gs_int_rect * prect, const gx_drawing_color * pdcolor,
446
                        const gx_clip_path * pcpath, gs_memory_t * mem,
447
                        gx_image_enum_common_t ** pinfo)
448
31.0k
{
449
31.0k
    const gs_pixel_image_t * const pim = (const gs_pixel_image_t *)pic;
450
31.0k
    gx_device_clist_writer * const cdev =
451
31.0k
        &((gx_device_clist *)dev)->writer;
452
31.0k
    clist_image_enum *pie = 0;
453
31.0k
    int base_index;
454
31.0k
    bool indexed;
455
31.0k
    bool masked = false;
456
31.0k
    bool has_alpha = false;
457
31.0k
    int num_components;
458
31.0k
    int bits_per_pixel;
459
31.0k
    bool uses_color;
460
31.0k
    bool varying_depths = false;
461
31.0k
    gs_matrix mat;
462
31.0k
    gs_rect sbox, dbox;
463
31.0k
    gs_image_format_t format;
464
31.0k
    gx_color_usage_bits color_usage = 0;
465
31.0k
    int code;
466
31.0k
    bool mask_use_hl;
467
31.0k
    clist_icc_color_t icc_zero_init = { 0 };
468
31.0k
    cmm_profile_t *src_profile;
469
31.0k
    cmm_srcgtag_profile_t *srcgtag_profile;
470
31.0k
    gsicc_rendering_intents_t renderingintent = pgs->renderingintent;
471
31.0k
    gsicc_blackptcomp_t blackptcomp = pgs->blackptcomp;
472
31.0k
    gsicc_rendering_param_t stored_rendering_cond;
473
31.0k
    gsicc_rendering_param_t dev_render_cond;
474
31.0k
    gs_gstate *pgs_nonconst = (gs_gstate*) pgs;
475
31.0k
    bool intent_changed = false;
476
31.0k
    bool bp_changed = false;
477
31.0k
    cmm_dev_profile_t *dev_profile = NULL;
478
31.0k
    cmm_profile_t *gs_output_profile;
479
31.0k
    bool is_planar_dev = dev->is_planar;
480
31.0k
    bool render_is_valid;
481
31.0k
    int csi;
482
31.0k
    gx_clip_path *lpcpath = NULL;
483
484
    /* We can only handle a limited set of image types. */
485
31.0k
    switch ((gs_debug_c('`') ? -1 : pic->type->index)) {
486
30.9k
    case 1:
487
30.9k
        masked = ((const gs_image1_t *)pim)->ImageMask;
488
30.9k
        has_alpha = ((const gs_image1_t *)pim)->Alpha != 0;
489
        /* fall through */
490
30.9k
    case 4:
491
30.9k
        if (pmat == 0)
492
30.9k
            break;
493
45
    default:
494
45
        goto use_default;
495
31.0k
    }
496
30.9k
    format = pim->format;
497
    /* See above for why we allocate the enumerator as immovable. */
498
30.9k
    pie = gs_alloc_struct_immovable(mem, clist_image_enum,
499
30.9k
                                    &st_clist_image_enum,
500
30.9k
                                    "clist_begin_typed_image");
501
30.9k
    if (pie == 0)
502
1
        return_error(gs_error_VMerror);
503
30.9k
#ifdef PACIFY_VALGRIND
504
    /* The following memset is required to avoid a valgrind warning
505
     * in:
506
     *   gs -I./gs/lib -sOutputFile=out.pgm -dMaxBitmap=10000
507
     *      -sDEVICE=pgmraw -r300 -Z: -sDEFAULTPAPERSIZE=letter
508
     *      -dNOPAUSE -dBATCH -K2000000 -dClusterJob -dJOBSERVER
509
     *      tests_private/ps/ps3cet/11-14.PS
510
     * Setting the individual elements of the structure directly is
511
     * not enough, which leads me to believe that we are writing the
512
     * entire struct out, padding and all.
513
     */
514
30.9k
    memset(&pie->color_space.icc_info, 0, sizeof(pie->color_space.icc_info));
515
30.9k
#endif
516
30.9k
    pie->memory = mem;
517
30.9k
    pie->buffer = NULL;
518
30.9k
    pie->masked = masked;
519
30.9k
    *pinfo = (gx_image_enum_common_t *) pie;
520
    /* num_planes and plane_depths[] are set later, */
521
    /* by gx_image_enum_common_init. */
522
30.9k
    if (masked) {
523
9.94k
        base_index = gs_color_space_index_DeviceGray;   /* arbitrary */
524
9.94k
        indexed = false;
525
9.94k
        num_components = 1;
526
9.94k
        uses_color = true;
527
        /* cmd_put_drawing_color handles color_usage */
528
21.0k
    } else {
529
21.0k
        const gs_color_space *pcs = pim->ColorSpace;
530
531
21.0k
        base_index = gs_color_space_get_index(pcs);
532
21.0k
        if (base_index == gs_color_space_index_Indexed) {
533
1.42k
            const gs_color_space *pbcs =
534
1.42k
                gs_color_space_indexed_base_space(pcs);
535
536
1.42k
            indexed = true;
537
1.42k
            base_index = gs_color_space_get_index(pbcs);
538
1.42k
            num_components = 1;
539
19.5k
        } else {
540
19.5k
            indexed = false;
541
19.5k
            num_components = gs_color_space_num_components(pcs);
542
19.5k
        }
543
21.0k
        uses_color = pim->CombineWithColor &&
544
21.0k
                    (rop3_uses_T(pgs->log_op) || rop3_uses_S(pgs->log_op));
545
21.0k
    }
546
30.9k
    code = gx_image_enum_common_init((gx_image_enum_common_t *) pie,
547
30.9k
                                     (const gs_data_image_t *) pim,
548
30.9k
                                     &clist_image_enum_procs, dev,
549
30.9k
                                     num_components, format);
550
30.9k
    {
551
30.9k
        int i;
552
553
30.9k
        for (i = 1; i < pie->num_planes; ++i)
554
8
            varying_depths |= pie->plane_depths[i] != pie->plane_depths[0];
555
30.9k
    }
556
557
    /* Now, check to see if we can't handle this as a high level image. */
558
30.9k
    if (code < 0)
559
0
        goto use_default;
560
30.9k
    if (!USE_HL_IMAGES) /* Always use the default. */
561
0
        goto use_default;
562
30.9k
    if (cdev->disable_mask & clist_disable_hl_image)
563
0
        goto use_default;
564
30.9k
    if (cdev->image_enum_id != gs_no_id) /* Can't handle nested images */
565
0
        goto use_default;
566
30.9k
    if (base_index > gs_color_space_index_DeviceCMYK &&
567
30.9k
        base_index != gs_color_space_index_ICC)
568
        /****** Can only handle Gray, RGB, CMYK and ICC ******/
569
28
        goto use_default;
570
30.9k
    if (has_alpha)
571
        /****** CAN'T HANDLE IMAGES WITH ALPHA YET ******/
572
0
        goto use_default;
573
30.9k
    if (varying_depths)
574
        /****** CAN'T HANDLE IMAGES WITH IRREGULAR DEPTHS ******/
575
0
        goto use_default;
576
30.9k
    if ((code = gs_matrix_invert(&pim->ImageMatrix, &mat)) < 0 ||
577
30.9k
        (code = gs_matrix_multiply(&mat, &ctm_only(pgs), &mat)) < 0 ||
578
30.9k
        !(cdev->disable_mask & clist_disable_nonrect_hl_image ?
579
0
          (is_xxyy(&mat) || is_xyyx(&mat)) :
580
30.9k
          image_matrix_ok_to_band(&mat)))
581
6.68k
        goto use_default;
582
583
24.2k
    mask_use_hl =
584
24.2k
        masked && ( gx_dc_is_pattern1_color(pdcolor) || gx_dc_is_pure(pdcolor) );
585
24.2k
    if (!mask_use_hl && uses_color && !gx_dc_is_pure(pdcolor) &&
586
24.2k
             !gx_dc_is_pattern1_color_clist_based(pdcolor))
587
        /* Only add in masks that are pure or pattern or pattern trans types */
588
8.41k
        goto use_default;
589
590
    /* We've passed the tests; code it as a high level image */
591
15.8k
    {
592
15.8k
        int bytes_per_plane, bytes_per_row;
593
594
15.8k
        bits_per_pixel = pim->BitsPerComponent * num_components;
595
15.8k
        pie->decode.bps = bits_per_pixel/num_components;
596
15.8k
        pie->decode.spp = num_components;
597
15.8k
        pie->image = *pim;
598
15.8k
        pie->dcolor = *pdcolor;
599
15.8k
        if (prect)
600
0
            pie->rect = *prect;
601
15.8k
        else {
602
15.8k
            pie->rect.p.x = 0, pie->rect.p.y = 0;
603
15.8k
            pie->rect.q.x = pim->Width, pie->rect.q.y = pim->Height;
604
15.8k
        }
605
15.8k
        pie->pgs = pgs;
606
607
15.8k
        if (pcpath) {
608
15.8k
            lpcpath = gx_cpath_alloc(mem, "clist_begin_typed_image(lpcpath)");
609
15.8k
            if (!lpcpath) {
610
0
                goto use_default;
611
0
            }
612
15.8k
            code = gx_cpath_copy(pcpath, lpcpath);
613
15.8k
            if (code < 0) {
614
0
                goto use_default;
615
0
            }
616
15.8k
        }
617
15.8k
        pie->pcpath = lpcpath;
618
619
15.8k
        pie->buffer = NULL;
620
15.8k
        pie->format = format;
621
15.8k
        pie->bits_per_plane = bits_per_pixel / pie->num_planes;
622
15.8k
        pie->matrix = mat;
623
15.8k
        pie->uses_color = uses_color;
624
15.8k
        if (masked) {
625
298
            pie->color_space.byte1 = 0;  /* arbitrary */
626
298
            pie->color_space.icc_info = icc_zero_init;
627
298
            pie->color_space.space = 0;
628
298
            pie->color_space.id = gs_no_id;
629
15.5k
        } else {
630
            /* Check for presence of ICC profiles in standard Device Color Spaces
631
               This can happen if a default space was initialized. It should
632
               typically have assigned to it one of the default ICC profiles */
633
15.5k
            if (indexed) {
634
1.19k
                if (pim->ColorSpace->base_space->cmm_icc_profile_data) {
635
1.19k
                    base_index = gs_color_space_index_ICC;
636
1.19k
                }
637
14.3k
            } else {
638
14.3k
                if (pim->ColorSpace->cmm_icc_profile_data) {
639
14.3k
                    base_index = gs_color_space_index_ICC;
640
14.3k
                }
641
14.3k
            }
642
15.5k
            pie->color_space.byte1 = (base_index << 4) |
643
15.5k
                (indexed ? (pim->ColorSpace->params.indexed.use_proc ? 12 : 8) : 0);
644
15.5k
            pie->color_space.id =
645
15.5k
                (pie->color_space.space = pim->ColorSpace)->id;
646
            /* Get the hash code of the ICC space */
647
15.5k
            if ( base_index == gs_color_space_index_ICC ) {
648
15.5k
                code = dev_proc(dev, get_profile)(dev,  &dev_profile);
649
15.5k
                gsicc_extract_profile(dev->graphics_type_tag, dev_profile,
650
15.5k
                                      &(gs_output_profile),
651
15.5k
                                      (&(dev_render_cond)));
652
15.5k
                if (!indexed) {
653
14.3k
                    src_profile = pim->ColorSpace->cmm_icc_profile_data;
654
14.3k
                } else {
655
1.19k
                    src_profile =
656
1.19k
                        pim->ColorSpace->base_space->cmm_icc_profile_data;
657
1.19k
                }
658
                /* Initialize the rendering conditions to what we currently
659
                   have before we may blow them away with what is set in
660
                   the srcgtag information */
661
15.5k
                stored_rendering_cond.graphics_type_tag = GS_IMAGE_TAG;
662
15.5k
                stored_rendering_cond.override_icc =
663
15.5k
                                dev_render_cond.override_icc;
664
15.5k
                stored_rendering_cond.preserve_black =
665
15.5k
                                dev_render_cond.preserve_black;
666
15.5k
                stored_rendering_cond.cmm = gsCMM_DEFAULT;  /* Unless spec. below */
667
                /* We may need to do some substitions for the source profile */
668
15.5k
                if (pgs->icc_manager->srcgtag_profile != NULL) {
669
0
                    srcgtag_profile = pgs->icc_manager->srcgtag_profile;
670
0
                    if (src_profile->data_cs == gsRGB) {
671
0
                        if (srcgtag_profile->rgb_profiles[gsSRC_IMAGPRO] != NULL) {
672
                            /* We only do this replacement depending upon the
673
                               ICC override setting for this object and the
674
                               original color space of this object */
675
0
                            csi = gsicc_get_default_type(src_profile);
676
0
                            if (srcgtag_profile->rgb_rend_cond[gsSRC_IMAGPRO].override_icc ||
677
0
                                csi == gs_color_space_index_DeviceRGB) {
678
0
                                src_profile =
679
0
                                    srcgtag_profile->rgb_profiles[gsSRC_IMAGPRO];
680
0
                                pgs_nonconst->renderingintent =
681
0
                                    srcgtag_profile->rgb_rend_cond[gsSRC_IMAGPRO].rendering_intent;
682
0
                                pgs_nonconst->blackptcomp =
683
0
                                    srcgtag_profile->rgb_rend_cond[gsSRC_IMAGPRO].black_point_comp;
684
0
                                stored_rendering_cond =
685
0
                                    srcgtag_profile->rgb_rend_cond[gsSRC_IMAGPRO];
686
0
                            }
687
0
                        } else {
688
                            /* A possible do not use CM case */
689
0
                            stored_rendering_cond.cmm =
690
0
                                srcgtag_profile->rgb_rend_cond[gsSRC_IMAGPRO].cmm;
691
0
                        }
692
0
                    } else if (src_profile->data_cs == gsCMYK) {
693
0
                        if (srcgtag_profile->cmyk_profiles[gsSRC_IMAGPRO] != NULL) {
694
0
                            csi = gsicc_get_default_type(src_profile);
695
0
                            if (srcgtag_profile->cmyk_rend_cond[gsSRC_IMAGPRO].override_icc ||
696
0
                                csi == gs_color_space_index_DeviceCMYK) {
697
0
                                src_profile =
698
0
                                    srcgtag_profile->cmyk_profiles[gsSRC_IMAGPRO];
699
0
                                pgs_nonconst->renderingintent =
700
0
                                    srcgtag_profile->cmyk_rend_cond[gsSRC_IMAGPRO].rendering_intent;
701
0
                                pgs_nonconst->blackptcomp =
702
0
                                    srcgtag_profile->cmyk_rend_cond[gsSRC_IMAGPRO].black_point_comp;
703
0
                                stored_rendering_cond =
704
0
                                    srcgtag_profile->cmyk_rend_cond[gsSRC_IMAGPRO];
705
0
                            }
706
0
                        } else {
707
                            /* A possible do not use CM case */
708
0
                            stored_rendering_cond.cmm =
709
0
                                srcgtag_profile->cmyk_rend_cond[gsSRC_IMAGPRO].cmm;
710
0
                        }
711
0
                    }
712
0
                }
713
                /* If the device RI is set and we are not  setting the RI from
714
                   the source structure, then override any RI specified in the
715
                   document by the RI specified in the device */
716
15.5k
                if (!(pgs_nonconst->renderingintent & gsRI_OVERRIDE)) {  /* was set by source? */
717
                    /* No it was not.  See if we should override with the
718
                       device setting */
719
15.5k
                    if (dev_render_cond.rendering_intent != gsRINOTSPECIFIED) {
720
0
                        pgs_nonconst->renderingintent =
721
0
                                        dev_render_cond.rendering_intent;
722
0
                        }
723
15.5k
                }
724
                /* We have a similar issue to deal with with respect to the
725
                   black point.  */
726
15.5k
                if (!(pgs_nonconst->blackptcomp & gsBP_OVERRIDE)) {
727
15.5k
                    if (dev_render_cond.black_point_comp != gsBPNOTSPECIFIED) {
728
0
                        pgs_nonconst->blackptcomp =
729
0
                                            dev_render_cond.black_point_comp;
730
0
                    }
731
15.5k
                }
732
15.5k
                if (renderingintent != pgs_nonconst->renderingintent)
733
0
                    intent_changed = true;
734
15.5k
                if (blackptcomp != pgs_nonconst->blackptcomp)
735
0
                    bp_changed = true;
736
                /* Set for the rendering param structure also */
737
15.5k
                stored_rendering_cond.rendering_intent =
738
15.5k
                                                pgs_nonconst->renderingintent;
739
15.5k
                stored_rendering_cond.black_point_comp =
740
15.5k
                                                pgs_nonconst->blackptcomp;
741
15.5k
                stored_rendering_cond.graphics_type_tag = GS_IMAGE_TAG;
742
15.5k
                if (!(src_profile->hash_is_valid)) {
743
9
                    int64_t hash;
744
9
                    gsicc_get_icc_buff_hash(src_profile->buffer, &hash,
745
9
                                            src_profile->buffer_size);
746
9
                    src_profile->hashcode = hash;
747
9
                    src_profile->hash_is_valid = true;
748
9
                }
749
15.5k
                pie->color_space.icc_info.icc_hash = src_profile->hashcode;
750
15.5k
                pie->color_space.icc_info.icc_num_components =
751
15.5k
                    src_profile->num_comps;
752
15.5k
                pie->color_space.icc_info.is_lab = src_profile->islab;
753
15.5k
                pie->color_space.icc_info.default_match = src_profile->default_match;
754
15.5k
                pie->color_space.icc_info.data_cs = src_profile->data_cs;
755
15.5k
                src_profile->rend_cond = stored_rendering_cond;
756
15.5k
                render_is_valid = src_profile->rend_is_valid;
757
15.5k
                src_profile->rend_is_valid = true;
758
15.5k
                clist_icc_addentry(cdev, src_profile->hashcode, src_profile);
759
15.5k
                src_profile->rend_is_valid = render_is_valid;
760
15.5k
            } else {
761
0
                pie->color_space.icc_info = icc_zero_init;
762
0
            }
763
15.5k
        }
764
15.8k
        pie->y = pie->rect.p.y;
765
        /* Image row has to fit in cmd writer's buffer */
766
15.8k
        bytes_per_plane =
767
15.8k
            (pim->Width * pie->bits_per_plane + 7) >> 3;
768
15.8k
        bytes_per_row = bytes_per_plane * pie->num_planes;
769
15.8k
        bytes_per_row = max(bytes_per_row, 1);
770
15.8k
        if (cmd_largest_size + bytes_per_row > cdev->cend - cdev->cbuf)
771
0
            goto use_default;
772
15.8k
    }
773
15.8k
    if (pim->Interpolate) {
774
0
        pie->support.x = pie->support.y = MAX_ISCALE_SUPPORT + 1;
775
15.8k
    } else {
776
15.8k
        pie->support.x = pie->support.y = 0;
777
15.8k
    }
778
15.8k
    sbox.p.x = pie->rect.p.x - pie->support.x;
779
15.8k
    sbox.p.y = pie->rect.p.y - pie->support.y;
780
15.8k
    sbox.q.x = pie->rect.q.x + pie->support.x;
781
15.8k
    sbox.q.y = pie->rect.q.y + pie->support.y;
782
15.8k
    gs_bbox_transform(&sbox, &mat, &dbox);
783
784
15.8k
    if (cdev->disable_mask & clist_disable_complex_clip)
785
0
        if (!check_rect_for_trivial_clip(lpcpath,
786
0
                                (int)floor(dbox.p.x), (int)floor(dbox.p.y),
787
0
                                (int)ceil(dbox.q.x), (int)ceil(dbox.q.y)))
788
0
            goto use_default;
789
790
    /* If we are going out to a halftone device and the size of the stored
791
       image at device resolution and color space is going to be smaller,
792
       go ahead and do the default handler. This occurs only for planar
793
       devices where if we prerender we will end up doing the fast theshold
794
       halftone and going out as copy_planes commands into the clist.
795
       There is already a test above with regard to the posture so that
796
       we are only doing portrait or landscape cases if we are here.  Only
797
       question is penum->image_parent_type == gs_image_type1 */
798
15.8k
    if (dev_profile == NULL) {
799
298
        gsicc_rendering_param_t temp_render_cond;
800
298
        code = dev_proc(dev, get_profile)(dev,  &dev_profile);
801
298
        if (code < 0)
802
0
            return code;
803
298
        gsicc_extract_profile(dev->graphics_type_tag, dev_profile,
804
298
                                              &(gs_output_profile),
805
298
                                              &(temp_render_cond));
806
298
    }
807
    /* Decide if we need to do any monitoring of the colors.  Note that multiple source
808
       (planes) is treated as color */
809
15.8k
    pie->decode.unpack = NULL;
810
15.8k
    if (dev_profile->pageneutralcolor && pie->color_space.icc_info.data_cs != gsGRAY) {
811
        /* If it is an index image, then check the pallete only */
812
0
        if (!indexed) {
813
0
            pie->monitor_color = true;
814
            /* Set up the unpacking proc for monitoring */
815
0
            get_unpack_proc((gx_image_enum_common_t*) pie, &(pie->decode),
816
0
                             pim->format, pim->Decode);
817
0
            get_map(&(pie->decode), pim->format, pim->Decode);
818
0
            if (pie->decode.unpack == NULL) {
819
                /* If we cant unpack, then end monitoring now. Treat as has color */
820
0
                dev_profile->pageneutralcolor = false;
821
0
                code = gsicc_mcm_end_monitor(pgs->icc_link_cache, dev);
822
0
                if (code < 0)
823
0
                    return code;
824
0
            } else {
825
                /* We need to allocate the buffer for unpacking during monitoring.
826
                    This is mainly for the 12bit case */
827
0
                int bsize = ((pie->decode.bps > 8 ? (pim->Width) * 2 : pim->Width) + 15) * num_components;
828
0
                pie->buffer = gs_alloc_bytes(mem, bsize, "image buffer");
829
0
                if (pie->buffer == 0) {
830
0
                    gs_free_object(mem, pie, "clist_begin_typed_image");
831
0
                    *pinfo = NULL;
832
0
                    return_error(gs_error_VMerror);
833
0
                }
834
0
            }
835
0
        } else {
836
0
            pie->monitor_color = false;
837
            /* Check the Palette here */
838
0
            if (palette_has_color(pim->ColorSpace, pim)) {
839
                /* Has color.  We are done monitoring */
840
0
                dev_profile->pageneutralcolor = false;
841
0
                code = gsicc_mcm_end_monitor(pgs->icc_link_cache, dev);
842
0
                if (code < 0)
843
0
                    return code;
844
0
            }
845
0
        }
846
15.8k
    } else {
847
15.8k
        pie->monitor_color = false;
848
15.8k
    }
849
15.8k
    if (gx_device_must_halftone(dev) && pim->BitsPerComponent == 8 && !masked &&
850
15.8k
        (dev->color_info.num_components == 1 || is_planar_dev) &&
851
15.8k
        dev_profile->prebandthreshold) {
852
9
        int dev_width = (int)(ceil(dbox.q.x) - floor(dbox.p.x));
853
9
        int dev_height = (int)(ceil(dbox.q.y) - floor(dbox.p.y));
854
855
9
        int src_size = pim->Height *
856
9
                       bitmap_raster(pim->Width * pim->BitsPerComponent *
857
9
                                     num_components);
858
9
        int des_size = dev_height * bitmap_raster(dev_width *
859
9
                                                  dev->color_info.depth);
860
9
        if (src_size > des_size)
861
0
            goto use_default;
862
9
    }
863
    /* Create the begin_image command. */
864
15.8k
    if ((pie->begin_image_command_length =
865
15.8k
         begin_image_command(pie->begin_image_command,
866
15.8k
                             sizeof(pie->begin_image_command), pic)) < 0)
867
1
        goto use_default;
868
15.8k
    if (!masked) {
869
        /*
870
         * Calculate (conservatively) the set of colors that this image
871
         * might generate.  For single-component images we can sample
872
         * this. We generate all the possible colors now; otherwise,
873
         * we assume that any color might be generated.  It is possible
874
         * to do better than this, but we won't bother unless there's
875
         * evidence that it's worthwhile.
876
         */
877
15.5k
        gx_color_usage_bits all = gx_color_usage_all(cdev);
878
879
15.5k
        if (num_components > 1)
880
8.01k
            color_usage = all;
881
7.53k
        else {
882
7.53k
            const gs_color_space *pcs = pim->ColorSpace;
883
7.53k
            cs_proc_remap_color((*remap_color)) = pcs->type->remap_color;
884
7.53k
            gs_client_color cc;
885
7.53k
            gx_drawing_color dcolor;
886
7.53k
            int i;
887
7.53k
            int max_value = indexed ? pcs->params.indexed.hival : 1;
888
889
250k
            for (i = 0; i <= max_value; ++i) {
890
                /* Enumerate the indexed colors, or just Black (DeviceGray = 0) */
891
243k
                cc.paint.values[0] = (double)i;
892
243k
                code = remap_color(&cc, pcs, &dcolor, pgs, dev,
893
243k
                            gs_color_select_source);
894
243k
                if (code < 0)
895
0
                    break;
896
243k
                color_usage |= cmd_drawing_color_usage(cdev, &dcolor);
897
243k
            }
898
7.53k
            if (code < 0)
899
0
                goto use_default;
900
7.53k
        }
901
15.5k
    }
902
15.8k
    pie->color_usage.or = color_usage;
903
15.8k
    pie->color_usage.slow_rop =
904
15.8k
        cmd_slow_rop(dev, pgs->log_op, (uses_color ? pdcolor : NULL));
905
15.8k
    pie->color_map_is_known = false;
906
    /*
907
     * Calculate a (slightly conservative) Y bounding interval for the image
908
     * in device space.
909
     */
910
15.8k
    {
911
15.8k
        int y0 = (int)floor(dbox.p.y - 0.51);   /* adjust + rounding slop */
912
15.8k
        int y1 = (int)ceil(dbox.q.y + 0.51);    /* ditto */
913
914
15.8k
        if (lpcpath) {
915
15.8k
            gs_fixed_rect obox;
916
15.8k
            gx_cpath_outer_box(lpcpath, &obox);
917
15.8k
            pie->ymin = max(0, max(y0, fixed2int(obox.p.y)));
918
15.8k
            pie->ymax = min(min(y1, fixed2int(obox.q.y)), dev->height);
919
15.8k
        } else {
920
0
            pie->ymin = max(y0, 0);
921
0
            pie->ymax = min(y1, dev->height);
922
0
        }
923
15.8k
    }
924
925
    /*
926
     * Make sure the CTM, color space, and clipping region (and, for
927
     * masked images or images with CombineWithColor, the current color)
928
     * are known at the time of the begin_image command.
929
     */
930
15.8k
    cmd_clear_known(cdev, clist_image_unknowns(dev, pie) | begin_image_known);
931
    /* Because the rendering intent may be driven by the source color
932
       settings we may have needed to overide the intent.  Need to break the const
933
       on the pgs here for this and reset back */
934
15.8k
    if (intent_changed)
935
0
        pgs_nonconst->renderingintent = renderingintent;
936
15.8k
    if (bp_changed)
937
0
        pgs_nonconst->blackptcomp = blackptcomp;
938
939
15.8k
    cdev->image_enum_id = pie->id;
940
15.8k
    return 0;
941
    /*
942
     * We couldn't handle the image.  It is up to the caller to use the default
943
     * algorithms, which break the image up into rectangles or small pixmaps.
944
     * If we are doing the PDF14 transparency device then we want to make sure we do
945
     * NOT use the target device.  In this case we return -1.
946
     */
947
15.1k
use_default:
948
15.1k
    if (pie != NULL)
949
15.1k
        gs_free_object(mem, pie->buffer, "clist_begin_typed_image");
950
15.1k
    gs_free_object(mem, pie, "clist_begin_typed_image");
951
15.1k
    *pinfo = NULL;
952
953
15.1k
    if (lpcpath != NULL)
954
1
        gx_cpath_free(lpcpath, "clist_begin_typed_image(lpcpath)");
955
956
15.1k
    if (pgs->has_transparency){
957
4.50k
        return -1;
958
10.6k
    } else {
959
10.6k
        return gx_default_begin_typed_image(dev, pgs, pmat, pic, prect,
960
10.6k
                                            pdcolor, pcpath, mem, pinfo);
961
10.6k
    }
962
15.1k
}
963
964
/* Error cleanup for clist_image_plane_data. */
965
static inline int
966
clist_image_plane_data_retry_cleanup(gx_device *dev, clist_image_enum *pie, int yh_used, int code)
967
0
{
968
0
    gx_device_clist_writer * const cdev =
969
0
        &((gx_device_clist *)dev)->writer;
970
0
971
0
    ++cdev->ignore_lo_mem_warnings;
972
0
    {
973
0
        code = write_image_end_all(dev, pie);
974
0
    }
975
0
    --cdev->ignore_lo_mem_warnings;
976
0
    /* Update sub-rect */
977
0
    if (!pie->image.Interpolate)
978
0
        pie->rect.p.y += yh_used;  /* interpolate & mem recovery currently incompat */
979
0
    return code;
980
0
}
981
982
/* Process the next piece of an image. */
983
static int
984
clist_image_plane_data(gx_image_enum_common_t * info,
985
                       const gx_image_plane_t * planes, int yh,
986
                       int *rows_used)
987
656k
{
988
656k
    gx_device *dev = info->dev;
989
656k
    gx_device_clist_writer * const cdev =
990
656k
        &((gx_device_clist *)dev)->writer;
991
656k
    clist_image_enum *pie = (clist_image_enum *) info;
992
656k
    gs_rect sbox, dbox;
993
656k
    int y_orig = pie->y;
994
656k
    int yh_used = min(yh, pie->rect.q.y - y_orig);
995
656k
    int y0, y1;
996
656k
    int ry, rheight;
997
656k
    int code;
998
656k
    cmd_rects_enum_t re;
999
656k
    bool found_color = false;
1000
1001
#ifdef DEBUG
1002
    if (pie->id != cdev->image_enum_id) {
1003
        lprintf2("end_image id = %lu != clist image id = %lu!\n",
1004
                 (ulong) pie->id, (ulong) cdev->image_enum_id);
1005
        *rows_used = 0;
1006
        return_error(gs_error_Fatal);
1007
    }
1008
#endif
1009
    /****** CAN'T HANDLE VARYING data_x VALUES YET ******/
1010
656k
    {
1011
656k
        int i;
1012
1013
658k
        for (i = 1; i < info->num_planes; ++i)
1014
1.82k
            if (planes[i].data_x != planes[0].data_x) {
1015
0
                *rows_used = 0;
1016
0
                return_error(gs_error_rangecheck);
1017
0
            }
1018
656k
    }
1019
656k
    sbox.p.x = pie->rect.p.x - pie->support.x;
1020
656k
    sbox.p.y = (y0 = y_orig) - pie->support.y;
1021
656k
    sbox.q.x = pie->rect.q.x + pie->support.x;
1022
656k
    sbox.q.y = (y1 = pie->y += yh_used) + pie->support.y;
1023
656k
    code = gs_bbox_transform(&sbox, &pie->matrix, &dbox);
1024
656k
    if (code < 0)
1025
0
        return code;
1026
    /*
1027
     * In order to keep the band list consistent, we must write out
1028
     * the image data in precisely those bands whose begin_image
1029
     * Y range includes the respective image scan lines.  Because of
1030
     * rounding, we must expand the dbox by a little extra, and then
1031
     * use image_band_box to calculate the precise range for each band.
1032
     * This is slow, but we don't see any faster way to do it in the
1033
     * general case.
1034
     */
1035
656k
    {
1036
656k
        int ry0 = (int)floor(dbox.p.y) - 2;
1037
656k
        int ry1 = (int)ceil(dbox.q.y) + 2;
1038
656k
        int band_height0 = cdev->page_band_height;
1039
1040
        /*
1041
         * Make sure we don't go into any bands beyond the Y range
1042
         * determined at begin_image time.
1043
         */
1044
656k
        if (ry0 < pie->ymin)
1045
47.5k
            ry0 = pie->ymin;
1046
656k
        if (ry1 > pie->ymax)
1047
14.0k
            ry1 = pie->ymax;
1048
        /*
1049
         * If the image extends off the page in the Y direction,
1050
         * we may have ry0 > ry1.  Check for this here.
1051
         */
1052
656k
        if (ry0 >= ry1)
1053
35.4k
            goto done;
1054
        /* Expand the range out to band boundaries. */
1055
620k
        ry = ry0 / band_height0 * band_height0;
1056
620k
        rheight = min(ROUND_UP(ry1, band_height0), dev->height) - ry;
1057
620k
    }
1058
1059
620k
    if (cdev->permanent_error < 0)
1060
0
      return (cdev->permanent_error);
1061
    /* If needed, update the trans_bbox */
1062
620k
    if (cdev->pdf14_needed) {
1063
370k
        gs_int_rect bbox;
1064
1065
370k
        bbox.p.x = (int)floor(dbox.p.x);
1066
370k
        bbox.q.x = (int)ceil(dbox.q.x);
1067
370k
        bbox.p.y = pie->ymin;
1068
370k
        bbox.q.y = pie->ymax;
1069
1070
370k
        clist_update_trans_bbox(cdev, &bbox);
1071
370k
    }
1072
    /* Make sure clip_path for the cdev is not stale -- update from image_enum */
1073
620k
    cdev->clip_path = NULL;
1074
620k
    cmd_check_clip_path(cdev, pie->pcpath);
1075
1076
620k
    RECT_ENUM_INIT(re, ry, rheight);
1077
894k
    do {
1078
894k
        gs_int_rect ibox;
1079
894k
        gs_int_rect entire_box;
1080
1081
894k
        RECT_STEP_INIT(re);
1082
        /*
1083
         * Just transmit the subset of the data that intersects this band.
1084
         * Note that y and height always define a complete band.
1085
         */
1086
1087
894k
        if (!image_band_box(dev, pie, re.y, re.height, &ibox))
1088
2.28k
            continue;
1089
        /*
1090
         * The transmitted subrectangle has to be computed at the time
1091
         * we write the begin_image command; this in turn controls how
1092
         * much of each scan line we write out.
1093
         */
1094
891k
        {
1095
891k
            int band_ymax = min(re.band_end, pie->ymax);
1096
891k
            int band_ymin = max(re.band_end - re.band_height, pie->ymin);
1097
1098
891k
            if (!image_band_box(dev, pie, band_ymin,
1099
891k
                                band_ymax - band_ymin, &entire_box))
1100
0
                continue;
1101
891k
        }
1102
1103
891k
        re.pcls->color_usage.or |= pie->color_usage.or;
1104
891k
        re.pcls->color_usage.slow_rop |= pie->color_usage.slow_rop;
1105
1106
        /* Write out begin_image & its preamble for this band */
1107
891k
        if (!(re.pcls->known & begin_image_known)) {
1108
149k
            gs_logical_operation_t lop = pie->pgs->log_op;
1109
149k
            byte *dp;
1110
149k
            byte *bp = pie->begin_image_command +
1111
149k
                pie->begin_image_command_length;
1112
149k
            uint len;
1113
149k
            byte image_op = cmd_opv_begin_image;
1114
1115
            /* Make sure the gs_gstate is up to date. */
1116
149k
            code = (pie->color_map_is_known ? 0 :
1117
149k
                    cmd_put_color_mapping(cdev, pie->pgs));
1118
149k
            pie->color_map_is_known = true;
1119
149k
            if (code >= 0) {
1120
149k
                uint want_known = ctm_known | clip_path_known |
1121
149k
                            op_bm_tk_known | ais_known |
1122
149k
                            fill_alpha_known | stroke_alpha_known | fill_adjust_known |
1123
149k
                            (pie->color_space.id == gs_no_id ? 0 :
1124
149k
                                                     color_space_known);
1125
1126
149k
                code = cmd_do_write_unknown(cdev, re.pcls, want_known);
1127
149k
            }
1128
149k
            if (code >= 0)
1129
149k
                code = cmd_do_enable_clip(cdev, re.pcls, pie->pcpath != NULL);
1130
149k
            if (code >= 0)
1131
149k
                code = cmd_update_lop(cdev, re.pcls, lop);
1132
149k
            if (code < 0)
1133
0
                return code;
1134
            /* Does the result of this image depend upon the current color in the
1135
             * graphics state? If so, we need to send it. */
1136
149k
            if (pie->uses_color) {
1137
                /* We want to write the color taking into account the entire image so */
1138
                /* we set re.rect_nbands from pie->ymin and pie->ymax so that we will */
1139
                /* make the decision to write 'all_bands' the same for the whole image */
1140
                /* This is slightly more efficient, and is required for patterns with */
1141
                /* transparency that push the group at the begin_image step.          */
1142
273
                re.rect_nbands = ((pie->ymax + re.band_height - 1) / re.band_height) -
1143
273
                                 ((pie->ymin) / re.band_height);
1144
273
                code = cmd_put_drawing_color(cdev, re.pcls, &pie->dcolor,
1145
273
                                             &re, devn_not_tile_fill);
1146
273
                if (code < 0)
1147
0
                    return code;
1148
273
                if (!pie->masked) {
1149
                    /* In PS and PDF, masked == uses_color. In PCL, due to rops, we can
1150
                     * have a non-imagemask image that relies on the current graphics
1151
                     * color. C303.BIN page 20 has an example of this. Normally the above
1152
                     * call the cmd_put_drawing_color will have sent through the halftone
1153
                     * phase, but we can be in the situation where the current drawing
1154
                     * color is pure (so no phase is sent), but the colors in the image
1155
                     * are not (so a phase must be sent). Accordingly, we catch that
1156
                     * here. */
1157
0
                    if (pie->pgs->screen_phase[gs_color_select_texture].x != re.pcls->screen_phase[gs_color_select_texture].x ||
1158
0
                        pie->pgs->screen_phase[gs_color_select_texture].y != re.pcls->screen_phase[gs_color_select_texture].y) {
1159
0
                        code = cmd_set_screen_phase_generic(cdev, re.pcls,
1160
0
                                                            pie->pgs->screen_phase[gs_color_select_texture].x,
1161
0
                                                            pie->pgs->screen_phase[gs_color_select_texture].y,
1162
0
                                                            gs_color_select_texture, true);
1163
0
                        if (code < 0)
1164
0
                            return code;
1165
0
                    }
1166
0
                    if (pie->pgs->screen_phase[gs_color_select_source].x != re.pcls->screen_phase[gs_color_select_source].x ||
1167
0
                        pie->pgs->screen_phase[gs_color_select_source].y != re.pcls->screen_phase[gs_color_select_source].y) {
1168
0
                        code = cmd_set_screen_phase_generic(cdev, re.pcls,
1169
0
                                                            pie->pgs->screen_phase[gs_color_select_source].x,
1170
0
                                                            pie->pgs->screen_phase[gs_color_select_source].y,
1171
0
                                                            gs_color_select_source, true);
1172
0
                        if (code < 0)
1173
0
                            return code;
1174
0
                    }
1175
0
                }
1176
149k
            } else if (0 != re.pcls->tile_phase.x || 0 != re.pcls->tile_phase.y) {
1177
0
                code = cmd_set_tile_phase(cdev, re.pcls, 0, 0);
1178
0
                if (code < 0)
1179
0
                    return code;
1180
0
            }
1181
149k
            if (entire_box.p.x != 0 || entire_box.p.y != 0 ||
1182
149k
                entire_box.q.x != pie->image.Width ||
1183
149k
                entire_box.q.y != pie->image.Height
1184
149k
                ) {
1185
147k
                image_op = cmd_opv_begin_image_rect;
1186
147k
                cmd_put2w(entire_box.p.x, entire_box.p.y, &bp);
1187
147k
                cmd_put2w(pie->image.Width - entire_box.q.x,
1188
147k
                          pie->image.Height - entire_box.q.y, &bp);
1189
147k
                }
1190
149k
            len = bp - pie->begin_image_command;
1191
149k
            code =
1192
149k
                set_cmd_put_op(&dp, cdev, re.pcls, image_op, 1 + len);
1193
149k
            if (code < 0)
1194
0
                return code;
1195
149k
            memcpy(dp + 1, pie->begin_image_command, len);
1196
1197
            /* Mark band's begin_image as known */
1198
149k
            re.pcls->known |= begin_image_known;
1199
149k
        }
1200
1201
        /*
1202
         * The data that we write out must use the X values set by
1203
         * begin_image, which may cover a larger interval than the ones
1204
         * actually needed for these particular scan lines if the image is
1205
         * rotated.
1206
         */
1207
891k
        {
1208
            /*
1209
             * image_band_box ensures that b{x,y}{0,1} fall within
1210
             * pie->rect.
1211
             */
1212
891k
            int bx0 = entire_box.p.x, bx1 = entire_box.q.x;
1213
891k
            int by0 = ibox.p.y, by1 = ibox.q.y;
1214
891k
            int bpp = pie->bits_per_plane;
1215
891k
            int num_planes = pie->num_planes;
1216
891k
            uint offsets[GS_IMAGE_MAX_COMPONENTS];
1217
891k
            int i, iy, ih, xskip, xoff, nrows;
1218
891k
            uint bytes_per_plane, bytes_per_row, rows_per_cmd;
1219
1220
891k
            if (by0 < y0)
1221
690k
                by0 = y0;
1222
891k
            if (by1 > y1)
1223
694k
                by1 = y1;
1224
            /*
1225
             * Make sure we're skipping an integral number of pixels, by
1226
             * truncating the initial X coordinate to the next lower
1227
             * value that is an exact multiple of a byte.
1228
             */
1229
891k
            xoff = bx0 - pie->rect.p.x;
1230
891k
            xskip = xoff & -(int)"\001\010\004\010\002\010\004\010"[bpp & 7];
1231
1.78M
            for (i = 0; i < num_planes; ++i)
1232
893k
                offsets[i] =
1233
893k
                    (by0 - y0) * planes[i].raster + ((xskip * bpp) >> 3);
1234
891k
            bytes_per_plane = ((bx1 - (pie->rect.p.x + xskip)) * bpp + 7) >> 3;
1235
891k
            bytes_per_row = bytes_per_plane * pie->num_planes;
1236
891k
            rows_per_cmd =
1237
891k
                (data_bits_size - cmd_largest_size) / max(bytes_per_row, 1);
1238
1239
891k
            if (rows_per_cmd == 0) {
1240
                /* The reader will have to buffer a row separately. */
1241
18.5k
                rows_per_cmd = 1;
1242
18.5k
            }
1243
891k
            if (pie->monitor_color) {
1244
0
                for (iy = by0, ih = by1 - by0; ih > 0; iy += nrows, ih -= nrows) {
1245
0
                    nrows = min(ih, rows_per_cmd);
1246
0
                    if (!found_color) {
1247
0
                        code = cmd_image_plane_data_mon(cdev, re.pcls, planes, info,
1248
0
                                                    bytes_per_plane, offsets,
1249
0
                                                    xoff - xskip, nrows,
1250
0
                                                    &found_color);
1251
0
                        if (found_color) {
1252
                            /* Has color.  We are done monitoring */
1253
0
                            cmm_dev_profile_t *dev_profile;
1254
0
                            code = dev_proc(dev, get_profile)(dev,  &dev_profile);
1255
0
                            dev_profile->pageneutralcolor = false;
1256
0
                            code |= gsicc_mcm_end_monitor(pie->pgs->icc_link_cache, dev);
1257
0
                            pie->monitor_color = false;
1258
0
                        }
1259
0
                    } else {
1260
0
                        code = cmd_image_plane_data(cdev, re.pcls, planes, info,
1261
0
                                                    bytes_per_plane, offsets,
1262
0
                                                    xoff - xskip, nrows);
1263
0
                    }
1264
0
                    if (code < 0)
1265
0
                        return code;
1266
0
                    for (i = 0; i < num_planes; ++i)
1267
0
                        offsets[i] += planes[i].raster * nrows;
1268
0
                }
1269
891k
            } else {
1270
1.68M
                for (iy = by0, ih = by1 - by0; ih > 0; iy += nrows, ih -= nrows) {
1271
788k
                    nrows = min(ih, rows_per_cmd);
1272
788k
                    code = cmd_image_plane_data(cdev, re.pcls, planes, info,
1273
788k
                                                bytes_per_plane, offsets,
1274
788k
                                                xoff - xskip, nrows);
1275
788k
                    if (code < 0)
1276
0
                        return code;
1277
1.57M
                    for (i = 0; i < num_planes; ++i)
1278
790k
                        offsets[i] += planes[i].raster * nrows;
1279
788k
                }
1280
891k
            }
1281
891k
        }
1282
894k
    } while ((re.y += re.height) < re.yend);
1283
656k
 done:
1284
656k
    *rows_used = pie->y - y_orig;
1285
656k
    return pie->y >= pie->rect.q.y;
1286
620k
}
1287
1288
/* Clean up by releasing the buffers. */
1289
static int
1290
clist_image_end_image(gx_image_enum_common_t * info, bool draw_last)
1291
15.8k
{
1292
15.8k
    gx_device *dev = info->dev;
1293
15.8k
    gx_device_clist_writer * const cdev =
1294
15.8k
        &((gx_device_clist *)dev)->writer;
1295
15.8k
    clist_image_enum *pie = (clist_image_enum *) info;
1296
15.8k
    int code;
1297
1298
#ifdef DEBUG
1299
    if (pie->id != cdev->image_enum_id) {
1300
        lprintf2("end_image id = %lu != clist image id = %lu!\n",
1301
                 (ulong) pie->id, (ulong) cdev->image_enum_id);
1302
        return_error(gs_error_Fatal);
1303
    }
1304
#endif
1305
15.8k
    code = write_image_end_all(dev, pie);
1306
15.8k
    cdev->image_enum_id = gs_no_id;
1307
15.8k
    gx_cpath_free((gx_clip_path *)pie->pcpath, "clist_image_end_image(pie->pcpath)");
1308
15.8k
    cdev->clip_path = NULL;
1309
15.8k
    cdev->clip_path_id = gs_no_id;
1310
15.8k
    gx_image_free_enum(&info);
1311
15.8k
    return code;
1312
15.8k
}
1313
1314
/* Create a compositor device. */
1315
int
1316
clist_composite(gx_device * dev,
1317
                        gx_device ** pcdev, const gs_composite_t * pcte,
1318
                        gs_gstate * pgs, gs_memory_t * mem, gx_device *cldev)
1319
282k
{
1320
282k
    byte * dp;
1321
282k
    uint size = 0, size_dummy;
1322
282k
    gx_device_clist_writer * const cdev =
1323
282k
                    &((gx_device_clist *)dev)->writer;
1324
282k
    int ry, rheight, cropping_op;
1325
282k
    int band_height = cdev->page_info.band_params.BandHeight;
1326
282k
    int last_band = cdev->nbands - 1;
1327
282k
    int first_band = 0, no_of_bands = cdev->nbands;
1328
282k
    int code = pcte->type->procs.write(pcte, 0, &size, cdev);
1329
282k
    int temp_cropping_min, temp_cropping_max;
1330
282k
    int newdev;
1331
1332
282k
    CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1333
1334
    /* determine the amount of space required */
1335
282k
    if (code < 0 && code != gs_error_rangecheck)
1336
0
        return code;
1337
282k
    size += 2 + 1;      /* 2 bytes for the command code, one for the id */
1338
1339
    /* Create a compositor device for clist writing (if needed) */
1340
282k
    code = pcte->type->procs.clist_compositor_write_update(pcte, dev,
1341
282k
                                                        pcdev, pgs, mem);
1342
282k
    if (code < 0)
1343
0
        return code;
1344
282k
    newdev = code == 1;
1345
1346
282k
    CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1347
1348
282k
    code = pcte->type->procs.get_cropping(pcte, &ry, &rheight, cdev->cropping_min, cdev->cropping_max);
1349
1350
282k
    CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1351
1352
282k
    if (code < 0)
1353
0
        return code;
1354
1355
282k
    cropping_op = code;
1356
282k
    code = 0;
1357
1358
282k
    if (cropping_op == PUSHCROP || cropping_op == SAMEAS_PUSHCROP_BUTNOPUSH) {
1359
27.1k
        first_band = ry / band_height;
1360
27.1k
        last_band = (ry + rheight - 1) / band_height;
1361
255k
    } else if (cropping_op == POPCROP || cropping_op == CURRBANDS) {
1362
26.6k
        first_band = cdev->cropping_min / band_height;
1363
26.6k
        last_band = (cdev->cropping_max - 1) / band_height;
1364
26.6k
    }
1365
1366
282k
    if (last_band - first_band > no_of_bands * 2 / 3) {
1367
        /* Covering many bands, so write "all bands" command for shorter clist. */
1368
247k
        cropping_op = ALLBANDS;
1369
247k
    }
1370
1371
    /* Using 'v' here instead of 'L' since this is used almost exclusively with
1372
       the transparency code */
1373
1374
#ifdef DEBUG
1375
    if (gs_debug_c('v')) {
1376
1377
        if(cropping_op != 0) {
1378
1379
           dmprintf2(dev->memory, "[v] cropping_op = %d. Total number of bands is %d \n",
1380
                     cropping_op, no_of_bands);
1381
           dmprintf2(dev->memory, "[v]  Writing out from band %d through band %d \n",
1382
                     first_band, last_band);
1383
1384
        } else {
1385
1386
           dmprintf1(dev->memory, "[v] cropping_op = %d. Writing out to all bands \n",
1387
                     cropping_op);
1388
1389
        }
1390
    }
1391
#endif
1392
1393
282k
    if (cropping_op == ALLBANDS) {
1394
        /* overprint applies to all bands */
1395
250k
        size_dummy = size;
1396
250k
        code = set_cmd_put_all_extended_op(& dp,
1397
250k
                                   (gx_device_clist_writer *)dev,
1398
250k
                                   cmd_opv_ext_composite,
1399
250k
                                   size );
1400
250k
        if (code < 0)
1401
4
            return code;
1402
1403
        /* insert the compositor identifier */
1404
250k
        dp[2] = pcte->type->comp_id;
1405
1406
        /* serialize the remainder of the compositor */
1407
250k
        if ((code = pcte->type->procs.write(pcte, dp + 3, &size_dummy, cdev)) < 0)
1408
0
            ((gx_device_clist_writer *)dev)->cnext = dp;
1409
1410
250k
        if (code >= 0 && newdev)
1411
2.72k
            code = 1; /* Return 1 to indicate we created a new device. */
1412
250k
        return code;
1413
250k
    }
1414
31.4k
    if (cropping_op == PUSHCROP) {
1415
15.7k
        code = clist_writer_push_cropping(cdev, ry, rheight);
1416
15.7k
        if (code < 0)
1417
0
            return code;
1418
15.7k
    }
1419
31.4k
    if (cropping_op == SAMEAS_PUSHCROP_BUTNOPUSH) {
1420
        /* Set the range even though it is not pushed until the group occurs
1421
           This occurs only when we had blend changes with a group push */
1422
24
        temp_cropping_min = max(cdev->cropping_min, ry);
1423
24
        temp_cropping_max = min(cdev->cropping_max, ry + rheight);
1424
31.4k
    } else {
1425
31.4k
        temp_cropping_min = cdev->cropping_min;
1426
31.4k
        temp_cropping_max = cdev->cropping_max;
1427
31.4k
    }
1428
    /* Adjust the lower and upper bound to allow for image gridfitting changing boundaries */
1429
31.4k
    if (temp_cropping_min > 0)
1430
30.7k
        temp_cropping_min--;
1431
31.4k
    if (temp_cropping_max < dev->height - 1)
1432
30.0k
        temp_cropping_max++;
1433
31.4k
    if (temp_cropping_min < temp_cropping_max) {
1434
        /* The pdf14 compositor could be applied
1435
           only to bands covered by the pcte->params.bbox. */
1436
31.4k
        cmd_rects_enum_t re;
1437
1438
31.4k
        RECT_ENUM_INIT(re, temp_cropping_min, temp_cropping_max - temp_cropping_min);
1439
453k
        do {
1440
453k
            RECT_STEP_INIT(re);
1441
453k
            code = set_cmd_put_extended_op(&dp, cdev, re.pcls, cmd_opv_ext_composite, size);
1442
453k
            if (code >= 0) {
1443
453k
                size_dummy = size;
1444
453k
                dp[2] = pcte->type->comp_id;
1445
453k
                code = pcte->type->procs.write(pcte, dp + 3, &size_dummy, cdev);
1446
453k
            }
1447
453k
            if (code < 0)
1448
0
                return code;
1449
453k
        } while ((re.y += re.height) < re.yend);
1450
31.4k
    }
1451
31.4k
    if (cropping_op == POPCROP) {
1452
15.7k
        code = clist_writer_pop_cropping(cdev);
1453
15.7k
        if (code < 0)
1454
0
            return code;
1455
15.7k
    }
1456
1457
31.4k
    if (newdev)
1458
0
        code = 1; /* Return 1 to indicate we created a new device. */
1459
1460
31.4k
    return code;
1461
31.4k
}
1462
1463
/* ------ Utilities ------ */
1464
1465
/* Add a command to set data_x. */
1466
static int
1467
cmd_put_set_data_x(gx_device_clist_writer * cldev, gx_clist_state * pcls,
1468
                   int data_x)
1469
0
{
1470
0
    byte *dp;
1471
0
    int code;
1472
1473
0
    if (data_x > 0x1f) {
1474
0
        int dx_msb = data_x >> 5;
1475
1476
0
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_misc,
1477
0
                              2 + cmd_size_w(dx_msb));
1478
0
        if (code >= 0) {
1479
0
            dp[1] = cmd_set_misc_data_x + 0x20 + (data_x & 0x1f);
1480
0
            cmd_put_w(dx_msb, dp + 2);
1481
0
        }
1482
0
    } else {
1483
0
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_misc, 2);
1484
0
        if (code >= 0)
1485
0
            dp[1] = cmd_set_misc_data_x + data_x;
1486
0
    }
1487
0
    return code;
1488
0
}
1489
1490
/* Add commands to represent a full (device) halftone. */
1491
int
1492
cmd_put_halftone(gx_device_clist_writer * cldev, const gx_device_halftone * pdht)
1493
16.8k
{
1494
16.8k
    uint    ht_size = 0, req_size;
1495
16.8k
    byte *  dp;
1496
16.8k
    byte *  dp0 = 0;
1497
16.8k
    byte *  pht_buff = 0;
1498
16.8k
    int     code = gx_ht_write(pdht, (gx_device *)cldev, 0, &ht_size);
1499
1500
    /*
1501
     * Determine the required size, and if necessary allocate a buffer.
1502
     *
1503
     * The full serialized representation consists of:
1504
     *  command code (2 bytes)
1505
     *  length of serialized halftone (enc_u_sizew(ht_size)
1506
     *  one or more halfton segments, which consist of:
1507
     *    command code (2 bytes)
1508
     *    segment size (enc_u_sizew(seg_size) (seg_size < cbuf_ht_seg_max_size)
1509
     *    the serialized halftone segment (seg_size)
1510
     *
1511
     * Serialized halftones may be larger than the command buffer, so it
1512
     * is sent in segments. The cmd_opv_extend/cmd_opv_ext_put_halftone
1513
     * combination indicates that a device halftone is being sent, and
1514
     * provides the length of the entire halftone. This is followed by
1515
     * one or more cmd_opv_extend/cmd_opv_ext_ht_seg commands, which
1516
     * convey the segments of the serialized hafltone. The reader can
1517
     * identify the final segment by adding segment lengths.
1518
     *
1519
     * This complexity is hidden from the serialization code. If the
1520
     * halftone is larger than a single halftone buffer, we allocate a
1521
     * buffer to hold the entire representation, and divided into
1522
     * segments in this routine.
1523
     */
1524
16.8k
    if (code < 0 && code != gs_error_rangecheck)
1525
0
        return code;
1526
16.8k
    req_size = 2 + enc_u_sizew(ht_size);
1527
1528
    /* output the "put halftone" command */
1529
16.8k
    if ((code = set_cmd_put_all_extended_op(&dp, cldev, cmd_opv_ext_put_halftone, req_size)) < 0)
1530
2
        return code;
1531
16.8k
    dp += 2;
1532
16.8k
    enc_u_putw(ht_size, dp);
1533
1534
    /* see if a separate allocated buffer is required */
1535
16.8k
    if (ht_size > cbuf_ht_seg_max_size) {
1536
6.39k
        pht_buff = gs_alloc_bytes( cldev->bandlist_memory,
1537
6.39k
                                   ht_size,
1538
6.39k
                                   "cmd_put_halftone" );
1539
6.39k
        if (pht_buff == 0)
1540
6
            return_error(gs_error_VMerror);
1541
10.4k
    } else {
1542
        /* send the only segment command */
1543
10.4k
        req_size += ht_size;
1544
10.4k
        code = set_cmd_put_all_extended_op(&dp, cldev, cmd_opv_ext_put_ht_seg, req_size);
1545
10.4k
        if (code < 0)
1546
0
            return code;
1547
10.4k
        dp0 = dp;
1548
10.4k
        dp += 2;
1549
10.4k
        enc_u_putw(ht_size, dp);
1550
10.4k
        pht_buff = dp;
1551
10.4k
    }
1552
1553
    /* serialize the halftone */
1554
16.8k
    code = gx_ht_write(pdht, (gx_device *)cldev, pht_buff, &ht_size);
1555
16.8k
    if (code < 0) {
1556
0
        if (ht_size > cbuf_ht_seg_max_size)
1557
0
            gs_free_object( cldev->bandlist_memory,
1558
0
                            pht_buff,
1559
0
                            "cmd_put_halftone" );
1560
0
        else
1561
0
            cldev->cnext = dp0;
1562
0
        return code;
1563
0
    }
1564
1565
    /*
1566
     * If the halftone fit into a single command buffer, we are done.
1567
     * Otherwise, process the individual segments.
1568
     *
1569
     * If bandlist memory is exhausted while processing the segments,
1570
     * we do not make any attempt to recover the partially submitted
1571
     * halftone. The reader will discard any partially sent hafltone
1572
     * when it receives the next cmd_opv_extend/
1573
     * cmd_opv_ext_put_halftone combination.
1574
     */
1575
16.8k
    if (ht_size > cbuf_ht_seg_max_size) {
1576
6.39k
        byte *  pbuff = pht_buff;
1577
1578
21.1k
        while (ht_size > 0 && code >= 0) {
1579
14.7k
            int     seg_size, tmp_size;
1580
1581
14.7k
            seg_size = ( ht_size > cbuf_ht_seg_max_size ? cbuf_ht_seg_max_size
1582
14.7k
                                                        : ht_size );
1583
14.7k
            tmp_size = 2 + enc_u_sizew(seg_size) + seg_size;
1584
14.7k
            code = set_cmd_put_all_extended_op(&dp, cldev, cmd_opv_ext_put_ht_seg, tmp_size);
1585
14.7k
            if (code >= 0) {
1586
14.7k
                dp += 2;
1587
14.7k
                enc_u_putw(seg_size, dp);
1588
14.7k
                memcpy(dp, pbuff, seg_size);
1589
14.7k
                ht_size -= seg_size;
1590
14.7k
                pbuff += seg_size;
1591
14.7k
            }
1592
14.7k
        }
1593
6.39k
        gs_free_object( cldev->bandlist_memory, pht_buff, "cmd_put_halftone");
1594
6.39k
        pht_buff = 0;
1595
6.39k
    }
1596
1597
16.8k
    if (code >= 0)
1598
16.8k
        cldev->device_halftone_id = pdht->id;
1599
1600
16.8k
    return code;
1601
16.8k
}
1602
1603
/* Write out any necessary color mapping data. */
1604
int
1605
cmd_put_color_mapping(gx_device_clist_writer * cldev,
1606
                      const gs_gstate * pgs)
1607
16.0k
{
1608
16.0k
    int code;
1609
16.0k
    const gx_device_halftone *pdht = gx_select_dev_ht(pgs);
1610
1611
    /* Put out the halftone, if present, and target is not contone. */
1612
16.0k
    if (pdht && pdht->id != cldev->device_halftone_id && !device_is_contone(cldev->target)) {
1613
1.65k
        code = cmd_put_halftone(cldev, pdht);
1614
1.65k
        if (code < 0)
1615
8
            return code;
1616
1.64k
        cldev->device_halftone_id = pdht->id;
1617
1.64k
    }
1618
    /* Put the under color removal and black generation functions */
1619
16.0k
    code = cmd_put_color_map(cldev, cmd_map_black_generation,
1620
16.0k
                                 0, pgs->black_generation,
1621
16.0k
                                 &cldev->black_generation_id);
1622
16.0k
    if (code < 0)
1623
0
        return code;
1624
16.0k
    code = cmd_put_color_map(cldev, cmd_map_undercolor_removal,
1625
16.0k
                                 0, pgs->undercolor_removal,
1626
16.0k
                                 &cldev->undercolor_removal_id);
1627
16.0k
    if (code < 0)
1628
0
        return code;
1629
    /* Now put out the transfer functions. */
1630
16.0k
    {
1631
16.0k
        uint which = 0;
1632
16.0k
        bool send_default_comp = false;
1633
16.0k
        int i;
1634
16.0k
        gs_id default_comp_id, xfer_ids[4];
1635
1636
        /*
1637
         * Determine the ids for the transfer functions that we currently
1638
         * have in the set_transfer structure.  The halftone xfer funcs
1639
         * are sent in cmd_put_halftone.
1640
         */
1641
16.0k
#define get_id(pgs, color, color_num) \
1642
48.2k
    ((pgs->set_transfer.color != NULL && pgs->set_transfer.color_num >= 0) \
1643
48.2k
        ? pgs->set_transfer.color->id\
1644
48.2k
        : pgs->set_transfer.gray->id)
1645
1646
16.0k
        xfer_ids[0] = get_id(pgs, red, red_component_num);
1647
16.0k
        xfer_ids[1] = get_id(pgs, green, green_component_num);
1648
16.0k
        xfer_ids[2] = get_id(pgs, blue, blue_component_num);
1649
16.0k
        xfer_ids[3] = default_comp_id = pgs->set_transfer.gray->id;
1650
16.0k
#undef get_id
1651
1652
80.3k
        for (i = 0; i < countof(cldev->transfer_ids); ++i) {
1653
64.2k
            if (xfer_ids[i] != cldev->transfer_ids[i])
1654
21.3k
                which |= 1 << i;
1655
64.2k
            if (xfer_ids[i] == default_comp_id &&
1656
64.2k
                cldev->transfer_ids[i] != default_comp_id)
1657
21.3k
                send_default_comp = true;
1658
64.2k
        }
1659
        /* There are 3 cases for transfer functions: nothing to write, */
1660
        /* a single function, and multiple functions. */
1661
16.0k
        if (which == 0)
1662
10.7k
            return 0;
1663
        /*
1664
         * Send default transfer function if changed or we need it for a
1665
         * component
1666
         */
1667
5.34k
        if (send_default_comp || cldev->transfer_ids[0] != default_comp_id) {
1668
5.34k
            gs_id dummy = gs_no_id;
1669
1670
5.34k
            code = cmd_put_color_map(cldev, cmd_map_transfer, 0,
1671
5.34k
                pgs->set_transfer.gray, &dummy);
1672
5.34k
            if (code < 0)
1673
0
                return code;
1674
            /* Sending a default will force all xfers to default */
1675
26.7k
            for (i = 0; i < countof(cldev->transfer_ids); ++i)
1676
21.3k
                cldev->transfer_ids[i] = default_comp_id;
1677
5.34k
        }
1678
        /* Send any transfer functions which have changed */
1679
5.34k
        if (cldev->transfer_ids[0] != xfer_ids[0]) {
1680
5
            code = cmd_put_color_map(cldev, cmd_map_transfer_0,
1681
5
                        pgs->set_transfer.red_component_num,
1682
5
                        pgs->set_transfer.red, &cldev->transfer_ids[0]);
1683
5
            if (code < 0)
1684
0
                return code;
1685
5
        }
1686
5.34k
        if (cldev->transfer_ids[1] != xfer_ids[1]) {
1687
5
            code = cmd_put_color_map(cldev, cmd_map_transfer_1,
1688
5
                        pgs->set_transfer.green_component_num,
1689
5
                        pgs->set_transfer.green, &cldev->transfer_ids[1]);
1690
5
            if (code < 0)
1691
0
                return code;
1692
5
        }
1693
5.34k
        if (cldev->transfer_ids[2] != xfer_ids[2]) {
1694
5
            code = cmd_put_color_map(cldev, cmd_map_transfer_2,
1695
5
                        pgs->set_transfer.blue_component_num,
1696
5
                        pgs->set_transfer.blue, &cldev->transfer_ids[2]);
1697
5
            if (code < 0)
1698
0
                return code;
1699
5
        }
1700
5.34k
    }
1701
1702
5.34k
    return 0;
1703
5.34k
}
1704
1705
/*
1706
 * Compute the subrectangle of an image that intersects a band;
1707
 * return false if it is empty.
1708
 * It is OK for this to be too large; in fact, with the present
1709
 * algorithm, it will be quite a bit too large if the transformation isn't
1710
 * well-behaved ("well-behaved" meaning either xy = yx = 0 or xx = yy = 0).
1711
 */
1712
16.2k
#define I_FLOOR(x) ((int)floor(x))
1713
16.2k
#define I_CEIL(x) ((int)ceil(x))
1714
static void
1715
box_merge_point(gs_int_rect * pbox, double x, double y)
1716
8.10k
{
1717
8.10k
    int t;
1718
1719
8.10k
    if ((t = I_FLOOR(x)) < pbox->p.x)
1720
4.56k
        pbox->p.x = t;
1721
8.10k
    if ((t = I_CEIL(x)) > pbox->q.x)
1722
2.00k
        pbox->q.x = t;
1723
8.10k
    if ((t = I_FLOOR(y)) < pbox->p.y)
1724
2.08k
        pbox->p.y = t;
1725
8.10k
    if ((t = I_CEIL(y)) > pbox->q.y)
1726
2.27k
        pbox->q.y = t;
1727
8.10k
}
1728
static bool
1729
image_band_box(gx_device * dev, const clist_image_enum * pie, int y, int h,
1730
               gs_int_rect * pbox)
1731
1.78M
{
1732
1.78M
    fixed by0 = int2fixed(y);
1733
1.78M
    fixed by1 = int2fixed(y + h);
1734
1.78M
    int
1735
1.78M
        px = pie->rect.p.x, py = pie->rect.p.y,
1736
1.78M
        qx = pie->rect.q.x, qy = pie->rect.q.y;
1737
1.78M
    gs_fixed_rect cbox;         /* device clipping box */
1738
1.78M
    gs_rect bbox;               /* cbox intersected with band */
1739
1740
    /* Intersect the device clipping box and the band. */
1741
1.78M
    (*dev_proc(dev, get_clipping_box)) (dev, &cbox);
1742
    /* The fixed_half here is to allow for adjustment. */
1743
1.78M
    bbox.p.x = fixed2float(cbox.p.x - fixed_half);
1744
1.78M
    bbox.q.x = fixed2float(cbox.q.x + fixed_half);
1745
1.78M
    bbox.p.y = fixed2float(max(cbox.p.y, by0) - fixed_half);
1746
1.78M
    bbox.q.y = fixed2float(min(cbox.q.y, by1) + fixed_half);
1747
    /* Limit the box further if possible (because of a clipping path) */
1748
1.78M
    if (bbox.p.y < pie->ymin)
1749
370k
        bbox.p.y = pie->ymin;
1750
1.78M
    if (bbox.q.y > pie->ymax)
1751
140k
        bbox.q.y = pie->ymax;
1752
#ifdef DEBUG
1753
    if (gs_debug_c('b')) {
1754
        dmlprintf6(dev->memory, "[b]band box for (%d,%d),(%d,%d), band (%d,%d) =>\n",
1755
                   px, py, qx, qy, y, y + h);
1756
        dmlprintf10(dev->memory, "      (%g,%g),(%g,%g), matrix=[%g %g %g %g %g %g]\n",
1757
                    bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y,
1758
                    pie->matrix.xx, pie->matrix.xy, pie->matrix.yx,
1759
                    pie->matrix.yy, pie->matrix.tx, pie->matrix.ty);
1760
    }
1761
#endif
1762
1.78M
    if (is_xxyy(&pie->matrix) || is_xyyx(&pie->matrix)) {
1763
        /*
1764
         * The inverse transform of the band is a rectangle aligned with
1765
         * the coordinate axes, so we can just intersect it with the
1766
         * image subrectangle.
1767
         */
1768
1.78M
        gs_rect ibox;           /* bbox transformed back to image space */
1769
1770
1.78M
        if (gs_bbox_transform_inverse(&bbox, &pie->matrix, &ibox) < 0)
1771
0
            return false;
1772
1.78M
        pbox->p.x = max(px, I_FLOOR(ibox.p.x));
1773
1.78M
        pbox->q.x = min(qx, I_CEIL(ibox.q.x));
1774
1.78M
        pbox->p.y = max(py, I_FLOOR(ibox.p.y));
1775
1.78M
        pbox->q.y = min(qy, I_CEIL(ibox.q.y));
1776
1.78M
    } else {
1777
        /*
1778
         * The inverse transform of the band is not aligned with the
1779
         * axes, i.e., is a general parallelogram.  To compute an exact
1780
         * bounding box, we need to find the intersections of this
1781
         * parallelogram with the image subrectangle.
1782
         *
1783
         * There is probably a much more efficient way to do this
1784
         * computation, but we don't know what it is.
1785
         */
1786
2.25k
        gs_point rect[4];
1787
2.25k
        gs_point corners[5];
1788
2.25k
        int i;
1789
1790
        /* Store the corners of the image rectangle. */
1791
2.25k
        rect[0].x = rect[3].x = px;
1792
2.25k
        rect[1].x = rect[2].x = qx;
1793
2.25k
        rect[0].y = rect[1].y = py;
1794
2.25k
        rect[2].y = rect[3].y = qy;
1795
        /*
1796
         * Compute the corners of the clipped band in image space.  If
1797
         * the matrix is singular or an overflow occurs, the result will
1798
         * be nonsense: in this case, there isn't anything useful we
1799
         * can do, so return an empty intersection.
1800
         */
1801
2.25k
        if (gs_point_transform_inverse(bbox.p.x, bbox.p.y, &pie->matrix,
1802
2.25k
                                       &corners[0]) < 0 ||
1803
2.25k
            gs_point_transform_inverse(bbox.q.x, bbox.p.y, &pie->matrix,
1804
2.25k
                                       &corners[1]) < 0 ||
1805
2.25k
            gs_point_transform_inverse(bbox.q.x, bbox.q.y, &pie->matrix,
1806
2.25k
                                       &corners[2]) < 0 ||
1807
2.25k
            gs_point_transform_inverse(bbox.p.x, bbox.q.y, &pie->matrix,
1808
2.25k
                                       &corners[3]) < 0
1809
2.25k
            ) {
1810
0
            if_debug0m('b', dev->memory,
1811
0
                       "[b]can't inverse-transform a band corner!\n");
1812
0
            return false;
1813
0
        }
1814
2.25k
        corners[4] = corners[0];
1815
2.25k
        pbox->p.x = qx, pbox->p.y = qy;
1816
2.25k
        pbox->q.x = px, pbox->q.y = py;
1817
        /*
1818
         * We iterate over both the image rectangle and the band
1819
         * parallelogram in a single loop for convenience, even though
1820
         * there is no coupling between the two.
1821
         */
1822
11.2k
        for (i = 0; i < 4; ++i) {
1823
9.02k
            gs_point pa, pt;
1824
9.02k
            double dx, dy;
1825
1826
            /* Check the image corner for being inside the band. */
1827
9.02k
            pa = rect[i];
1828
9.02k
            gs_point_transform(pa.x, pa.y, &pie->matrix, &pt);
1829
9.02k
            if (pt.x >= bbox.p.x && pt.x <= bbox.q.x &&
1830
9.02k
                pt.y >= bbox.p.y && pt.y <= bbox.q.y
1831
9.02k
                )
1832
768
                box_merge_point(pbox, pa.x, pa.y);
1833
            /* Check the band corner for being inside the image. */
1834
9.02k
            pa = corners[i];
1835
9.02k
            if (pa.x >= px && pa.x <= qx && pa.y >= py && pa.y <= qy)
1836
2.76k
                box_merge_point(pbox, pa.x, pa.y);
1837
            /* Check for intersections of band edges with image edges. */
1838
9.02k
            dx = corners[i + 1].x - pa.x;
1839
9.02k
            dy = corners[i + 1].y - pa.y;
1840
9.02k
#define in_range(t, tc, p, q)\
1841
32.9k
  (0 <= t && t <= 1 && (t = tc) >= p && t <= q)
1842
9.02k
            if (dx != 0) {
1843
7.50k
                double t = (px - pa.x) / dx;
1844
1845
7.50k
                if_debug3m('b', dev->memory, "   (px) t=%g => (%d,%g)\n",
1846
7.50k
                           t, px, pa.y + t * dy);
1847
7.50k
                if (in_range(t, pa.y + t * dy, py, qy))
1848
510
                    box_merge_point(pbox, (double) px, t);
1849
7.50k
                t = (qx - pa.x) / dx;
1850
7.50k
                if_debug3m('b', dev->memory, "   (qx) t=%g => (%d,%g)\n",
1851
7.50k
                           t, qx, pa.y + t * dy);
1852
7.50k
                if (in_range(t, pa.y + t * dy, py, qy))
1853
312
                    box_merge_point(pbox, (double) qx, t);
1854
7.50k
            }
1855
9.02k
            if (dy != 0) {
1856
8.96k
                double t = (py - pa.y) / dy;
1857
1858
8.96k
                if_debug3m('b', dev->memory, "   (py) t=%g => (%g,%d)\n",
1859
8.96k
                           t, pa.x + t * dx, py);
1860
8.96k
                if (in_range(t, pa.x + t * dx, px, qx))
1861
472
                    box_merge_point(pbox, t, (double) py);
1862
8.96k
                t = (qy - pa.y) / dy;
1863
8.96k
                if_debug3m('b', dev->memory, "   (qy) t=%g => (%g,%d)\n",
1864
8.96k
                           t, pa.x + t * dx, qy);
1865
8.96k
                if (in_range(t, pa.x + t * dx, px, qx))
1866
3.27k
                    box_merge_point(pbox, t, (double) qy);
1867
8.96k
            }
1868
9.02k
#undef in_range
1869
9.02k
        }
1870
2.25k
    }
1871
1.78M
    if_debug4m('b', dev->memory, "    => (%d,%d),(%d,%d)\n",
1872
1.78M
               pbox->p.x, pbox->p.y, pbox->q.x, pbox->q.y);
1873
    /*
1874
     * If necessary, add pixels around the edges so we will have
1875
     * enough information to do interpolation.
1876
     */
1877
1.78M
    if ((pbox->p.x -= pie->support.x) < pie->rect.p.x)
1878
0
        pbox->p.x = pie->rect.p.x;
1879
1.78M
    if ((pbox->p.y -= pie->support.y) < pie->rect.p.y)
1880
0
        pbox->p.y = pie->rect.p.y;
1881
1.78M
    if ((pbox->q.x += pie->support.x) > pie->rect.q.x)
1882
0
        pbox->q.x = pie->rect.q.x;
1883
1.78M
    if ((pbox->q.y += pie->support.y) > pie->rect.q.y)
1884
0
        pbox->q.y = pie->rect.q.y;
1885
1.78M
    return (pbox->p.x < pbox->q.x && pbox->p.y < pbox->q.y);
1886
1.78M
}
1887
1888
inline static bool
1889
icc_info_notequal(clist_icc_color_t info1, clist_icc_color_t info2)
1890
1.10k
{
1891
1.10k
    if (info1.data_cs != info2.data_cs || info1.default_match != info2.default_match ||
1892
1.10k
        info1.icc_num_components != info2.icc_num_components || info1.is_lab != info2.is_lab ||
1893
1.10k
        info1.icc_hash != info2.icc_hash)
1894
0
        return true;
1895
1.10k
    else
1896
1.10k
        return false;
1897
1.10k
}
1898
1899
/* Determine which image-related properties are unknown */
1900
static uint     /* mask of unknown properties(see pcls->known) */
1901
clist_image_unknowns(gx_device *dev, const clist_image_enum *pie)
1902
15.8k
{
1903
15.8k
    gx_device_clist_writer * const cdev =
1904
15.8k
        &((gx_device_clist *)dev)->writer;
1905
15.8k
    const gs_gstate *const pgs = pie->pgs;
1906
15.8k
    uint unknown = 0;
1907
1908
    /*
1909
     * Determine if the CTM, color space, fill_adjust and clipping region,
1910
     * (and, for masked images or images with CombineWithColor, the current
1911
     * color) are unknown. Set the device state in anticipation of the
1912
     * values becoming known.
1913
     */
1914
15.8k
    if (cdev->gs_gstate.ctm.xx != pgs->ctm.xx ||
1915
15.8k
        cdev->gs_gstate.ctm.xy != pgs->ctm.xy ||
1916
15.8k
        cdev->gs_gstate.ctm.yx != pgs->ctm.yx ||
1917
15.8k
        cdev->gs_gstate.ctm.yy != pgs->ctm.yy ||
1918
15.8k
        cdev->gs_gstate.ctm.tx != pgs->ctm.tx ||
1919
15.8k
        cdev->gs_gstate.ctm.ty != pgs->ctm.ty
1920
15.8k
        ) {
1921
7.18k
        unknown |= ctm_known;
1922
7.18k
        cdev->gs_gstate.ctm = pgs->ctm;
1923
7.18k
    }
1924
15.8k
    if (pie->color_space.id == gs_no_id) { /* masked image */
1925
298
        cdev->color_space.space = 0; /* for GC */
1926
15.5k
    } else {                    /* not masked */
1927
15.5k
        if (cdev->color_space.id != pie->color_space.id ||
1928
15.5k
            cdev->color_space.space != pie->color_space.space ||
1929
15.5k
            icc_info_notequal(cdev->color_space.icc_info, pie->color_space.icc_info)) {
1930
14.4k
            unknown |= color_space_known;
1931
14.4k
            cdev->color_space.space = pie->color_space.space;
1932
14.4k
            cdev->color_space = pie->color_space;
1933
14.4k
            memcpy(&(cdev->color_space.icc_info), &(pie->color_space.icc_info), sizeof(clist_icc_color_t));
1934
14.4k
        }
1935
15.5k
    }
1936
15.8k
    if (cdev->gs_gstate.fill_adjust.x != pgs->fill_adjust.x ||
1937
15.8k
        cdev->gs_gstate.fill_adjust.y != pgs->fill_adjust.y) {
1938
340
        unknown |= fill_adjust_known;
1939
340
        cdev->gs_gstate.fill_adjust = pgs->fill_adjust;
1940
340
    }
1941
15.8k
    if (cmd_check_clip_path(cdev, pie->pcpath))
1942
15.8k
        unknown |= clip_path_known;
1943
    /*
1944
     * Note: overprint and overprint_mode are implemented via a compositor
1945
     * device, which is passed separately through the command list. Hence,
1946
     * though both parameters are passed in the state as well, this usually
1947
     * has no effect.
1948
     */
1949
15.8k
    if (cdev->gs_gstate.overprint != pgs->overprint ||
1950
15.8k
        cdev->gs_gstate.overprint_mode != pgs->overprint_mode ||
1951
15.8k
        cdev->gs_gstate.blend_mode != pgs->blend_mode ||
1952
15.8k
        cdev->gs_gstate.text_knockout != pgs->text_knockout ||
1953
15.8k
        cdev->gs_gstate.renderingintent != pgs->renderingintent) {
1954
6.13k
        unknown |= op_bm_tk_known;
1955
6.13k
        cdev->gs_gstate.overprint = pgs->overprint;
1956
6.13k
        cdev->gs_gstate.overprint_mode = pgs->overprint_mode;
1957
6.13k
        cdev->gs_gstate.blend_mode = pgs->blend_mode;
1958
6.13k
        cdev->gs_gstate.text_knockout = pgs->text_knockout;
1959
6.13k
        cdev->gs_gstate.renderingintent = pgs->renderingintent;
1960
6.13k
    }
1961
15.8k
    if (cdev->gs_gstate.alphaisshape != pgs->alphaisshape) {
1962
28
        unknown |= ais_known;
1963
28
        cdev->gs_gstate.alphaisshape = pgs->alphaisshape;
1964
28
    }
1965
15.8k
    if (cdev->gs_gstate.strokeconstantalpha != pgs->strokeconstantalpha) {
1966
0
        unknown |= stroke_alpha_known;
1967
0
        cdev->gs_gstate.strokeconstantalpha = pgs->strokeconstantalpha;
1968
0
    }
1969
15.8k
    if (cdev->gs_gstate.fillconstantalpha != pgs->fillconstantalpha) {
1970
21
        unknown |= fill_alpha_known;
1971
21
        cdev->gs_gstate.fillconstantalpha = pgs->fillconstantalpha;
1972
21
    }
1973
15.8k
    return unknown;
1974
15.8k
}
1975
1976
/* Construct the begin_image command. */
1977
static int
1978
begin_image_command(byte *buf, uint buf_size, const gs_image_common_t *pic)
1979
15.8k
{
1980
15.8k
    int i;
1981
15.8k
    stream s;
1982
15.8k
    const gs_color_space *ignore_pcs;
1983
15.8k
    int code;
1984
1985
63.6k
    for (i = 0; i < gx_image_type_table_count; ++i)
1986
63.6k
        if (gx_image_type_table[i] == pic->type)
1987
15.8k
            break;
1988
15.8k
    if (i >= gx_image_type_table_count)
1989
0
        return_error(gs_error_rangecheck);
1990
15.8k
    s_init(&s, NULL);
1991
15.8k
    swrite_string(&s, buf, buf_size);
1992
15.8k
    sputc(&s, (byte)i);
1993
15.8k
    code = pic->type->sput(pic, &s, &ignore_pcs);
1994
15.8k
    return (code < 0 ? code : stell(&s));
1995
15.8k
}
1996
1997
/* Write data for a partial image. */
1998
static int
1999
cmd_image_plane_data(gx_device_clist_writer * cldev, gx_clist_state * pcls,
2000
                     const gx_image_plane_t * planes,
2001
                     const gx_image_enum_common_t * pie,
2002
                     uint bytes_per_plane, const uint * offsets,
2003
                     int dx, int h)
2004
788k
{
2005
788k
    int data_x = planes[0].data_x + dx;
2006
788k
    uint nbytes = bytes_per_plane * pie->num_planes * h;
2007
788k
    uint len = 1 + cmd_size2w(h, bytes_per_plane) + nbytes;
2008
788k
    byte *dp;
2009
788k
    uint offset = 0;
2010
788k
    int plane, i;
2011
788k
    int code;
2012
2013
788k
    if (data_x) {
2014
0
        code = cmd_put_set_data_x(cldev, pcls, data_x);
2015
0
        if (code < 0)
2016
0
            return code;
2017
0
        offset = ((data_x & ~7) * cldev->clist_color_info.depth) >> 3;
2018
0
    }
2019
788k
    code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_image_data, len);
2020
788k
    if (code < 0)
2021
0
        return code;
2022
788k
    dp++;
2023
788k
    cmd_put2w(h, bytes_per_plane, &dp);
2024
1.57M
    for (plane = 0; plane < pie->num_planes; ++plane)
2025
2.24M
        for (i = 0; i < h; ++i) {
2026
1.45M
            memcpy(dp,
2027
1.45M
                   planes[plane].data + i * planes[plane].raster +
2028
1.45M
                   offsets[plane] + offset,
2029
1.45M
                   bytes_per_plane);
2030
1.45M
            dp += bytes_per_plane;
2031
1.45M
        }
2032
788k
    return 0;
2033
788k
}
2034
2035
/* Write data for a partial image with color monitor. */
2036
static int
2037
cmd_image_plane_data_mon(gx_device_clist_writer * cldev, gx_clist_state * pcls,
2038
                     const gx_image_plane_t * planes,
2039
                     const gx_image_enum_common_t * pie,
2040
                     uint bytes_per_plane, const uint * offsets,
2041
                     int dx, int h, bool *found_color)
2042
0
{
2043
0
    clist_image_enum *pie_c = (clist_image_enum *) pie;
2044
0
    int data_x = planes[0].data_x + dx;
2045
0
    uint nbytes = bytes_per_plane * pie->num_planes * h;
2046
0
    uint len = 1 + cmd_size2w(h, bytes_per_plane) + nbytes;
2047
0
    byte *dp;
2048
0
    uint offset = 0;
2049
0
    int plane, i;
2050
0
    int code;
2051
0
    int width = pie_c->rect.q.x - pie_c->rect.p.x;
2052
0
    int dsize = (((width + (planes[0]).data_x) * pie_c->decode.spp *
2053
0
        pie_c->decode.bps / pie->num_planes + 7) >> 3);
2054
0
    int data_size = pie_c->decode.spread / pie->num_planes;
2055
2056
0
    *found_color = false;
2057
2058
0
    if (data_x) {
2059
0
        code = cmd_put_set_data_x(cldev, pcls, data_x);
2060
0
        if (code < 0)
2061
0
            return code;
2062
0
        offset = ((data_x & ~7) * cldev->clist_color_info.depth) >> 3;
2063
0
    }
2064
0
    code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_image_data, len);
2065
0
    if (code < 0)
2066
0
        return code;
2067
0
    dp++;
2068
2069
0
    cmd_put2w(h, bytes_per_plane, &dp);
2070
2071
0
    for (i = 0; i < h; ++i) {
2072
0
        if (!(*found_color)) {
2073
            /* Here we need to unpack and actually look at the image data
2074
               to see if we have any non-neutral colors */
2075
0
            int pdata_x;
2076
0
            byte *data_ptr =  (byte *)(planes[0].data + i * planes[0].raster + offsets[0] + offset);
2077
0
            byte *buffer = (byte *)(*pie_c->decode.unpack)(pie_c->buffer, &pdata_x,
2078
0
                                     data_ptr, 0, dsize, pie_c->decode.map,
2079
0
                pie_c->decode.spread, pie_c->decode.spp);
2080
2081
0
            for (plane = 1; plane < pie->num_planes; ++plane) {
2082
                /* unpack planes after the first (if any), relying on spread to place the */
2083
                /* data at the correct spacing, with the buffer start adjusted for each plane */
2084
0
                data_ptr = (byte *)(planes[plane].data + i * planes[plane].raster + offsets[plane] + offset);
2085
0
                (*pie_c->decode.unpack)(pie_c->buffer + (data_size * plane), &pdata_x, data_ptr, 0,
2086
0
                    dsize, pie_c->decode.map, pie_c->decode.spread, pie_c->decode.spp);
2087
0
            }
2088
0
            if (row_has_color(buffer, pie_c, data_size, width)) {
2089
                /* Has color.  We are done monitoring */
2090
0
                *found_color = true;
2091
0
            }
2092
0
        }
2093
        /* Now copy the plane data into the clist buffer */
2094
0
        for (plane = 0; plane < pie->num_planes; ++plane) {
2095
0
            memcpy(dp, planes[plane].data + i * planes[plane].raster +
2096
0
                   offsets[plane] + offset, bytes_per_plane);
2097
0
            dp += bytes_per_plane;
2098
0
        }
2099
0
    }
2100
0
    return 0;
2101
0
}
2102
2103
/* Write image_end commands into all bands */
2104
static int      /* ret 0 ok, else -ve error status */
2105
write_image_end_all(gx_device *dev, const clist_image_enum *pie)
2106
15.8k
{
2107
15.8k
    gx_device_clist_writer * const cdev =
2108
15.8k
        &((gx_device_clist *)dev)->writer;
2109
15.8k
    int code;
2110
15.8k
    int ry = pie->ymin;
2111
15.8k
    int rheight = pie->ymax - ry;
2112
15.8k
    cmd_rects_enum_t re;
2113
2114
    /*
2115
     * We need to check specially for images lying entirely outside the
2116
     * page, since the RECT writing logic doesn't do this.
2117
     */
2118
15.8k
    if (pie->ymax < 0 || ry >= dev->height)
2119
284
        return 0;
2120
15.5k
    if (cdev->permanent_error < 0)
2121
0
      return (cdev->permanent_error);
2122
15.5k
    RECT_ENUM_INIT(re, ry, rheight);
2123
223k
    do {
2124
223k
        byte *dp;
2125
2126
223k
        RECT_STEP_INIT(re);
2127
223k
        if (re.pcls->known & begin_image_known) {
2128
149k
            if_debug1m('L', dev->memory, "[L]image_end for band %d\n", re.band);
2129
149k
            code = set_cmd_put_op(&dp, cdev, re.pcls, cmd_opv_image_data, 2);
2130
149k
            if (code < 0)
2131
0
                return code;
2132
149k
            dp[1] = 0;      /* EOD */
2133
149k
            re.pcls->known ^= begin_image_known;
2134
149k
        }
2135
223k
    } while ((re.y += re.height) < re.yend);
2136
    /* Make sure to clean up the buffer if we were monitoring */
2137
15.5k
    if (pie->buffer != NULL) {
2138
0
        gs_free_object(pie->memory, pie->buffer, "write_image_end_all");
2139
0
    }
2140
15.5k
    return 0;
2141
15.5k
}
2142
2143
/*
2144
 * Compare a rectangle vs. clip path.  Return true if there is no clipping
2145
 * path, if the rectangle is unclipped, or if the clipping path is a
2146
 * rectangle and intersects the given rectangle.
2147
 */
2148
static bool
2149
check_rect_for_trivial_clip(
2150
    const gx_clip_path *pcpath, /* May be NULL, clip to evaluate */
2151
    int px, int py, int qx, int qy      /* corners of box to test */
2152
)
2153
0
{
2154
0
    gs_fixed_rect obox;
2155
0
    gs_fixed_rect imgbox;
2156
2157
0
    if (!pcpath)
2158
0
        return true;
2159
2160
0
    imgbox.p.x = int2fixed(px);
2161
0
    imgbox.p.y = int2fixed(py);
2162
0
    imgbox.q.x = int2fixed(qx);
2163
0
    imgbox.q.y = int2fixed(qy);
2164
0
    if (gx_cpath_includes_rectangle(pcpath,
2165
0
                                    imgbox.p.x, imgbox.p.y,
2166
0
                                    imgbox.q.x, imgbox.q.y))
2167
0
        return true;
2168
2169
0
    return (gx_cpath_outer_box(pcpath, &obox) /* cpath is rectangle */ &&
2170
0
            obox.p.x <= imgbox.q.x && obox.q.x >= imgbox.p.x &&
2171
0
            obox.p.y <= imgbox.q.y && obox.q.y >= imgbox.p.y );
2172
0
}