Coverage Report

Created: 2026-08-08 08:00

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