Coverage Report

Created: 2026-10-28 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gxclrast.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
/* Command list interpreter/rasterizer */
18
#include "memory_.h"
19
#include "gx.h"
20
#include "gp.h"                 /* for gp_fmode_rb */
21
#include "gpcheck.h"
22
#include "gserrors.h"
23
#include "gscdefs.h"            /* for image type table */
24
#include "gsbitops.h"
25
#include "gsparams.h"
26
#include "gsstate.h"            /* (should only be gs_gstate) */
27
#include "gstrans.h"    /* for gs_is_pdf14trans_compositor */
28
#include "gxdcolor.h"
29
#include "gxdevice.h"
30
#include "gscoord.h"            /* requires gsmatrix.h */
31
#include "gsdevice.h"           /* for gs_deviceinitialmatrix */
32
#include "gsiparm4.h"
33
#include "gxdevmem.h"           /* must precede gxcldev.h */
34
#include "gxcldev.h"
35
#include "gxclpath.h"
36
#include "gxcmap.h"
37
#include "gxcolor2.h"
38
#include "gxcspace.h"           /* for gs_color_space_type */
39
#include "gxdhtres.h"
40
#include "gxgetbit.h"
41
#include "gxpaint.h"            /* for gx_fill/stroke_params */
42
#include "gxpcolor.h"
43
#include "gxhttile.h"
44
#include "gxiparam.h"
45
#include "gximask.h"
46
#include "gzpath.h"
47
#include "gzcpath.h"
48
#include "gzacpath.h"
49
#include "stream.h"
50
#include "strimpl.h"
51
#include "gxcomp.h"
52
#include "gsserial.h"
53
#include "gxdhtserial.h"
54
#include "gzht.h"
55
#include "gxshade.h"
56
#include "gxshade4.h"
57
#include "gsicc_manage.h"
58
#include "gsicc.h"
59
60
extern_gx_device_halftone_list();
61
extern_gx_image_type_table();
62
63
/* We need color space types for constructing temporary color spaces. */
64
extern const gs_color_space_type gs_color_space_type_Indexed;
65
66
/* Print a bitmap for tracing */
67
#ifdef DEBUG
68
static void
69
cmd_print_bits(gs_memory_t *mem, const byte * data, int width, int height, int raster)
70
{
71
    int i, j;
72
73
    dmlprintf3(mem, "[L]width=%d, height=%d, raster=%d\n",
74
              width, height, raster);
75
    for (i = 0; i < height; i++) {
76
        const byte *row = data + i * raster;
77
78
        dmlprintf(mem, "[L]");
79
        for (j = 0; j < raster; j++)
80
            dmprintf1(mem, " %02x", row[j]);
81
        dmputc(mem, '\n');
82
    }
83
}
84
#else
85
#  define cmd_print_bits(mem, data, width, height, raster) DO_NOTHING
86
#endif
87
88
/* Get a variable-length integer operand. */
89
#define cmd_getw(var, p)\
90
964M
  BEGIN\
91
964M
    if ( *p < 0x80 ) var = *p++;\
92
964M
    else { const byte *_cbp; var = cmd_get_w(p, &_cbp); p = _cbp; }\
93
964M
  END
94
95
static long
96
cmd_get_w(const byte * p, const byte ** rp)
97
795M
{
98
795M
    int val = *p++ & 0x7f;
99
795M
    int shift = 7;
100
101
1.51G
    for (; val |= (int)(*p & 0x7f) << shift, *p++ > 0x7f; shift += 7);
102
795M
    *rp = p;
103
795M
    return val;
104
795M
}
105
106
/* Get a variable-length fractional operand. */
107
#define cmd_getfrac(var, p)\
108
6.73M
  BEGIN\
109
6.73M
    if ( !(*p & 1) ) var = (*p++) << 24;\
110
6.73M
    else { const byte *_cbp; var = cmd_get_frac31(p, &_cbp); p = _cbp; }\
111
6.73M
  END
112
static frac31
113
cmd_get_frac31(const byte * p, const byte ** rp)
114
2.44M
{
115
2.44M
    frac31 val = (*p++ & 0xFE) << 24;
116
2.44M
    int shift = 24 - 7;
117
118
2.44M
    for (; val |= (frac31)(*p & 0xFE) << shift, *p++ & 1; shift -= 7);
119
2.44M
    *rp = p;
120
2.44M
    return val;
121
2.44M
}
122
123
/*
124
 * Define the structure for keeping track of the command reading buffer.
125
 *
126
 * The ptr member is only used for passing the current pointer to, and
127
 * receiving an updated pointer from, commands implemented as separate
128
 * procedures: normally it is kept in a register.
129
 */
130
typedef struct command_buf_s {
131
    byte *data;                 /* actual buffer, guaranteed aligned */
132
    uint size;
133
    const byte *ptr;            /* next byte to be read (see above) */
134
    const byte *warn_limit;     /* refill warning point */
135
    const byte *end;            /* byte just beyond valid data */
136
    stream *s;                  /* for refilling buffer */
137
    int end_status;
138
} command_buf_t;
139
140
/* Set the end of a command buffer. */
141
static void
142
set_cb_end(command_buf_t *pcb, const byte *end)
143
39.1M
{
144
39.1M
    pcb->end = end;
145
39.1M
    pcb->warn_limit = pcb->data + (pcb->size - cmd_largest_size + 1);
146
39.1M
    if ( pcb->warn_limit > pcb->end )
147
2.12M
        pcb->warn_limit = pcb->end;     /**** This is dangerous. Other places ****/
148
                                        /**** assume that the limit is a soft ****/
149
                                        /**** limit and should check 'end'    ****/
150
39.1M
}
151
152
static inline void
153
advance_buffer(command_buf_t *pcb, const byte *cbp)
154
37.0M
{
155
#ifdef DEBUG
156
    stream_state *st = pcb->s->state;
157
158
    top_up_offset_map(st, pcb->data, cbp, pcb->end);
159
#endif
160
37.0M
    memmove(pcb->data, cbp, pcb->end - cbp);
161
37.0M
}
162
163
static inline void
164
next_is_skip(command_buf_t *pcb)
165
44.6k
{
166
#ifdef DEBUG
167
    stream_state *st = pcb->s->state;
168
169
    offset_map_next_data_out_of_band(st);
170
#endif
171
44.6k
}
172
173
/* Read more data into a command buffer. */
174
static int
175
top_up_cbuf(command_buf_t *pcb, const byte **pcbp)
176
46.7M
{
177
46.7M
    uint nread;
178
46.7M
    const byte *cbp = *pcbp;
179
46.7M
    byte *cb_top = pcb->data + (pcb->end - cbp);
180
181
46.7M
    if (cbp < pcb->data || cbp > pcb->end) {
182
0
        errprintf(pcb->s->memory, "Clist I/O error: cbp outside of buffer\n");
183
0
        return (gs_error_ioerror);
184
0
    }
185
186
46.7M
    if (seofp(pcb->s)) {
187
        /* Can't use offset_map, because s_close resets s->state. Don't top up. */
188
9.69M
        pcb->end_status = pcb->s->end_status;
189
9.69M
        return 0;
190
9.69M
    }
191
37.0M
    advance_buffer(pcb, cbp);
192
37.0M
    nread = pcb->end - cb_top;
193
37.0M
    pcb->end_status = sgets(pcb->s, cb_top, nread, &nread);
194
37.0M
    if ( nread == 0 ) {
195
        /* No data for this band at all. */
196
115
        if (cb_top >= pcb->end) {
197
            /* should not happen */
198
0
            *pcbp = pcb->data;
199
0
            pcb->data[0] = cmd_opv_end_run;
200
0
            return_error(gs_error_ioerror);
201
0
        }
202
115
        *cb_top = cmd_opv_end_run;
203
115
        nread = 1;
204
115
    }
205
37.0M
    set_cb_end(pcb, cb_top + nread);
206
37.0M
    process_interrupts(pcb->s->memory);
207
37.0M
    *pcbp = pcb->data;
208
37.0M
    return 0;
209
37.0M
}
210
211
/* Read data from the command buffer and stream. */
212
/* From the command_buffer pcb, read rsize bytes to ptr, starting from cbp.
213
 * Return the new value for pcb->ptr. */
214
static const byte *
215
cmd_read_data(command_buf_t *pcb, byte *ptr, uint rsize, const byte *cbp)
216
14.2M
{
217
14.2M
    if (pcb->end - cbp >= rsize) {
218
14.2M
        memmove(ptr, cbp, rsize);
219
14.2M
        return cbp + rsize;
220
14.2M
    } else {
221
36.6k
        uint cleft = pcb->end - cbp;
222
36.6k
        uint rleft = rsize - cleft;
223
224
36.6k
        memmove(ptr, cbp, cleft);
225
36.6k
        sgets(pcb->s, ptr + cleft, rleft, &rleft);
226
#ifdef DEBUG
227
        {
228
            stream_state *st = pcb->s->state;
229
230
            adjust_offset_map_for_skipped_data(st, (uint)(pcb->end - pcb->data), rleft);
231
        }
232
#endif
233
36.6k
        return pcb->end;
234
36.6k
    }
235
14.2M
}
236
237
/* Read a fixed-size value from the command buffer. */
238
static inline const byte *
239
cmd_copy_value(void *pvar, int var_size, const byte *cbp)
240
2.00M
{
241
2.00M
    memcpy(pvar, cbp, var_size);
242
2.00M
    return cbp + var_size;
243
2.00M
}
244
#define cmd_get_value(var, cbp)\
245
2.00M
  cbp = cmd_copy_value(&var, sizeof(var), cbp)
246
247
/*
248
 * Define a buffer structure to hold a serialized halftone. This is
249
 * used only if the serialized halftone is too large to fit into
250
 * the command buffer.
251
 */
252
typedef struct ht_buff_s {
253
    uint    ht_size, read_size;
254
    byte *  pcurr;
255
    byte *  pbuff;
256
} ht_buff_t;
257
258
/*
259
 * Render one band to a specified target device.  Note that if
260
 * action == setup, target may be 0.
261
 */
262
static int read_set_tile_size(command_buf_t *pcb, tile_slot *bits, bool for_pattern);
263
static int read_set_bits(command_buf_t *pcb, tile_slot *bits,
264
                          int compress, gx_clist_state *pcls,
265
                          gx_strip_bitmap *tile, tile_slot **pslot,
266
                          gx_device_clist_reader *cdev, gs_memory_t *mem);
267
static int read_set_misc2(command_buf_t *pcb, gs_gstate *pgs,
268
                           segment_notes *pnotes);
269
static int read_set_color_space(command_buf_t *pcb, gs_gstate *pgs,
270
                                 gx_device_clist_reader *cdev, gs_memory_t *mem);
271
static int read_begin_image(command_buf_t *pcb, gs_image_common_t *pic,
272
                             gs_color_space *pcs);
273
static int read_put_params(command_buf_t *pcb, gs_gstate *pgs,
274
                            gx_device_clist_reader *cdev,
275
                            gs_memory_t *mem);
276
static int read_composite(command_buf_t *pcb, gs_memory_t *mem, gs_composite_t **ppcomp);
277
static int apply_composite(gx_device_clist_reader *cdev, gs_gstate *pgs,
278
                                   gs_memory_t *mem, gs_composite_t *pcomp,
279
                                   int x0, int y0, gx_device **ptarget);
280
static int read_alloc_ht_buff(ht_buff_t *, uint, gs_memory_t *);
281
static int read_ht_segment(ht_buff_t *, command_buf_t *, gs_gstate *,
282
                            gx_device *, gs_memory_t *);
283
284
static const byte *cmd_read_rect(int, gx_cmd_rect *, const byte *);
285
static const byte *cmd_read_short_bits(command_buf_t *pcb, byte *data, int tot_bytes,
286
                                        int width_bytes, int height,
287
                                        uint raster, const byte *cbp);
288
static int cmd_select_map(cmd_map_index, cmd_map_contents,
289
                           gs_gstate *, int **,
290
                           frac **, uint *, gs_memory_t *);
291
static int cmd_create_dev_ht(gx_device_halftone **, gs_memory_t *);
292
static int cmd_resize_halftone(gx_device_halftone **, uint,
293
                                gs_memory_t *);
294
static int clist_decode_segment(gx_path *, int, fixed[6],
295
                                 gs_fixed_point *, int, int,
296
                                 segment_notes);
297
static int clist_do_polyfill(gx_device *, gx_path *,
298
                              const gx_drawing_color *,
299
                              gs_logical_operation_t);
300
301
static inline void
302
enqueue_compositor(gs_composite_t **ppcomp_first, gs_composite_t **ppcomp_last, gs_composite_t *pcomp)
303
143M
{
304
143M
    if (*ppcomp_last == NULL) {
305
9.37M
        pcomp->prev = pcomp->next = NULL;
306
9.37M
        *ppcomp_last = *ppcomp_first = pcomp;
307
134M
    } else {
308
134M
        (*ppcomp_last)->next = pcomp;
309
134M
        pcomp->prev = *ppcomp_last;
310
134M
        pcomp->next = NULL;
311
134M
        *ppcomp_last = pcomp;
312
134M
    }
313
143M
}
314
315
#if 0 /* Appears unused - keep for a while. */
316
static inline gs_composite_t *
317
dequeue_last_compositor(gs_composite_t **ppcomp_first, gs_composite_t **ppcomp_last)
318
{
319
    gs_composite_t *pcomp = *ppcomp_last;
320
321
    if (*ppcomp_first == *ppcomp_last)
322
        *ppcomp_first = *ppcomp_last = NULL;
323
    else {
324
        *ppcomp_last = (*ppcomp_last)->prev;
325
        pcomp->prev = NULL;
326
        (*ppcomp_last)->next = NULL;
327
    }
328
    return pcomp;
329
}
330
331
static inline gs_composite_t *
332
dequeue_first_compositor(gs_composite_t **ppcomp_first, gs_composite_t **ppcomp_last)
333
{
334
    gs_composite_t *pcomp = *ppcomp_first;
335
336
    if (*ppcomp_first == *ppcomp_last)
337
        *ppcomp_first = *ppcomp_last = NULL;
338
    else {
339
        *ppcomp_first = (*ppcomp_first)->next;
340
        pcomp->next = NULL;
341
        (*ppcomp_last)->prev = NULL;
342
    }
343
    return pcomp;
344
}
345
#endif
346
347
static inline int
348
dequeue_compositor(gs_composite_t **ppcomp_first, gs_composite_t **ppcomp_last, gs_composite_t *pcomp)
349
143M
{
350
143M
    if (*ppcomp_last == *ppcomp_first) {
351
9.37M
        if (*ppcomp_last == pcomp) {
352
9.37M
            *ppcomp_last = *ppcomp_first = NULL;
353
9.37M
            return 0;
354
9.37M
        } else
355
0
            return_error(gs_error_unregistered); /* Must not happen. */
356
134M
    } else {
357
134M
        gs_composite_t *pcomp_next = pcomp->next, *pcomp_prev = pcomp->prev;
358
359
134M
        if (*ppcomp_last == pcomp)
360
6.20M
            *ppcomp_last = pcomp->prev;
361
127M
        else
362
127M
            pcomp_next->prev = pcomp_prev;
363
134M
        if (*ppcomp_first == pcomp)
364
127M
            *ppcomp_first = pcomp->next;
365
6.20M
        else
366
6.20M
            pcomp_prev->next = pcomp_next;
367
134M
        pcomp->next = pcomp->prev = NULL;
368
134M
        return 0;
369
134M
    }
370
143M
}
371
372
static inline void
373
free_compositor(gs_composite_t *pcomp, gs_memory_t *mem)
374
9.53M
{
375
9.53M
    gs_free_object(mem, pcomp, "free_compositor");
376
9.53M
}
377
378
static inline bool
379
is_null_compositor_op(const byte *cbp, int *length)
380
79.1M
{
381
79.1M
    if (cbp[0] == cmd_opv_end_run) {
382
71.2M
        *length = 1;
383
71.2M
        return true;
384
71.2M
    }
385
7.91M
    return false;
386
79.1M
}
387
388
static int
389
execute_compositor_queue(gx_device_clist_reader *cdev, gx_device **target, gx_device **tdev, gs_gstate *pgs,
390
                         gs_composite_t **ppcomp_first, gs_composite_t **ppcomp_last, gs_composite_t *pcomp_from,
391
                         int x0, int y0, gs_memory_t *mem, bool idle)
392
7.33M
{
393
141M
    while (pcomp_from != NULL) {
394
133M
        gs_composite_t *pcomp = pcomp_from;
395
133M
        int code;
396
397
133M
        pcomp_from = pcomp->next;
398
133M
        code = dequeue_compositor(ppcomp_first, ppcomp_last, pcomp);
399
133M
        if (code < 0)
400
0
            return code;
401
133M
        pcomp->idle |= idle;
402
133M
        code = apply_composite(cdev, pgs, mem, pcomp, x0, y0, target); /* Releases the compositor. */
403
133M
        if (code < 0)
404
15
            return code;
405
133M
        *tdev = *target;
406
133M
    }
407
7.33M
    return 0;
408
7.33M
}
409
410
static void
411
mark_as_idle(gs_composite_t *pcomp_start, gs_composite_t *pcomp_end)
412
1.61M
{
413
1.61M
    gs_composite_t *pcomp = pcomp_start;
414
415
3.80M
    while (pcomp != NULL) {
416
3.80M
        pcomp->idle = true;
417
3.80M
        if (pcomp == pcomp_end)
418
1.61M
            break;
419
2.18M
        pcomp = pcomp->next;
420
2.18M
    }
421
1.61M
}
422
423
static inline int
424
drop_compositor_queue(gs_composite_t **ppcomp_first, gs_composite_t **ppcomp_last,
425
                      gs_composite_t *pcomp_from, gs_memory_t *mem, int x0, int y0,
426
                      gs_gstate *pgs)
427
111k
{
428
111k
    gs_composite_t *pcomp;
429
430
636k
    do {
431
636k
        int code;
432
433
636k
        pcomp = *ppcomp_last;
434
636k
        if (pcomp == NULL)
435
0
            return 0;
436
636k
        dequeue_compositor(ppcomp_first, ppcomp_last, *ppcomp_last);
437
636k
        code = pcomp->type->procs.adjust_ctm(pcomp, x0, y0, pgs);
438
636k
        if (code < 0)
439
0
            return code;
440
636k
        free_compositor(pcomp, mem);
441
636k
    } while (pcomp != pcomp_from);
442
111k
    return 0;
443
111k
}
444
445
static int
446
read_set_misc_map(byte cb, command_buf_t *pcb, gs_gstate *pgs, gs_memory_t *mem)
447
5.21M
{
448
5.21M
    const byte *cbp = pcb->ptr;
449
5.21M
    frac *mdata;
450
5.21M
    int *pcomp_num;
451
5.21M
    uint count = 0;   /* quiet compiler */
452
5.21M
    cmd_map_contents cont =
453
5.21M
        (cmd_map_contents)(cb & 0x30) >> 4;
454
5.21M
    int code;
455
456
5.21M
    code = cmd_select_map(cb & 0xf, cont,
457
5.21M
                          pgs,
458
5.21M
                          &pcomp_num,
459
5.21M
                          &mdata, &count, mem);
460
461
5.21M
    if (code < 0)
462
0
        return code;
463
    /* Get component number if relevant */
464
5.21M
    if (pcomp_num == NULL)
465
5.21M
        cbp++;
466
636
    else {
467
636
        *pcomp_num = (int) *cbp++;
468
636
        if_debug1m('L', mem, " comp_num=%d", *pcomp_num);
469
636
    }
470
5.21M
    if (cont == cmd_map_other) {
471
4.27M
        cbp = cmd_read_data(pcb, (byte *)mdata, count, cbp);
472
473
#ifdef DEBUG
474
        if (gs_debug_c('L')) {
475
            uint i;
476
477
            for (i = 0; i < count / sizeof(*mdata); ++i)
478
                dmprintf1(mem, " 0x%04x", mdata[i]);
479
            dmputc(mem, '\n');
480
        }
481
    } else {
482
        if_debug0m('L', mem, " none\n");
483
#endif
484
4.27M
    }
485
    /* Recompute the effective transfer, */
486
    /* in case this was a transfer map. */
487
5.21M
    gx_gstate_set_effective_xfer(pgs);
488
5.21M
    pcb->ptr = cbp;
489
5.21M
    return 0;
490
5.21M
}
491
492
#ifdef DEBUG
493
void clist_debug_op(gs_memory_t *mem, const unsigned char *cbp)
494
{
495
    unsigned char op = *cbp++;
496
    const char *const *sub = cmd_sub_op_names[op >> 4];
497
    if (op == cmd_opv_extend) {
498
        unsigned char op2 = *cbp;
499
        if (cmd_extend_op_names[op2])
500
            dmlprintf1(mem, " %s", cmd_extend_op_names[op2]);
501
        else
502
            dmlprintf1(mem, " ?0x%02x?", (int)op2);
503
    } else if (sub)
504
        dmlprintf1(mem, " %s", sub[op & 0xf]);
505
    else
506
        dmlprintf2(mem, " %s %d", cmd_op_names[op >> 4], op & 0xf);
507
}
508
#endif
509
510
int
511
clist_playback_band(clist_playback_action playback_action, /* lgtm [cpp/use-of-goto] */
512
                    gx_device_clist_reader *cdev, stream *s,
513
                    gx_device *target, int x0, int y0,
514
                    gs_memory_t * mem)
515
2.14M
{
516
2.14M
    byte *cbuf_storage;
517
2.14M
    command_buf_t cbuf;
518
    /* data_bits is for short copy_* bits and copy_* compressed, */
519
    /* must be aligned */
520
2.14M
    byte *data_bits = 0;
521
2.14M
    const byte *cbp;
522
2.14M
    int dev_depth;              /* May vary due to compositing devices */
523
2.14M
    int dev_depth_bytes;
524
2.14M
    int odd_delta_shift;
525
2.14M
    int num_zero_bytes;
526
2.14M
    gx_device *tdev;
527
2.14M
    gx_clist_state state;
528
2.14M
    gx_color_index *set_colors;
529
2.14M
    gx_device_color *set_dev_colors;
530
2.14M
    tile_slot *state_slot;
531
2.14M
    gx_strip_bitmap state_tile; /* parameters for reading tiles */
532
2.14M
    tile_slot tile_bits;        /* parameters of current tile */
533
2.14M
    gs_int_point tile_phase;
534
2.14M
    gx_path path;
535
2.14M
    bool in_path;
536
2.14M
    gs_fixed_point ppos;
537
2.14M
    gx_clip_path clip_path;
538
2.14M
    bool use_clip;
539
2.14M
    gx_clip_path *pcpath;
540
2.14M
    gx_device_cpath_accum clip_accum;
541
2.14M
    gs_fixed_rect target_box;
542
2.14M
    struct _cas {
543
2.14M
        bool lop_enabled;
544
2.14M
        gx_device_color dcolor;
545
2.14M
        gs_fixed_point fa_save;
546
2.14M
    } clip_save;
547
2.14M
    bool in_clip = false;
548
2.14M
    gs_gstate gs_gstate;
549
2.14M
    gx_device_color fill_color = { 0 };
550
2.14M
    gx_device_color stroke_color = { 0 };
551
2.14M
    float dash_pattern[cmd_max_dash];
552
2.14M
    gx_fill_params fill_params;
553
2.14M
    gx_stroke_params stroke_params;
554
#ifdef DEBUG
555
    gs_halftone_type halftone_type;
556
#endif
557
2.14M
    union im_ {
558
2.14M
        gs_image_common_t c;
559
2.14M
        gs_data_image_t d;
560
2.14M
        gs_image1_t i1;
561
2.14M
        gs_image4_t i4;
562
2.14M
    } image;
563
2.14M
    gs_int_rect image_rect;
564
2.14M
    gs_color_space *pcs = NULL;
565
2.14M
    gx_image_enum_common_t *image_info;
566
2.14M
    gx_image_plane_t planes[32];
567
2.14M
    uint data_height;
568
2.14M
    uint data_size;
569
2.14M
    byte *data_on_heap;
570
2.14M
    fixed vs[6];
571
2.14M
    segment_notes notes;
572
2.14M
    int data_x;
573
2.14M
    int code = 0;
574
2.14M
    ht_buff_t  ht_buff;
575
2.14M
    gx_device *const orig_target = target;
576
2.14M
    gx_device_clip clipper_dev;
577
2.14M
    bool clipper_dev_open = false;
578
2.14M
    patch_fill_state_t pfs;
579
2.14M
    int op = 0;
580
2.14M
    int plane_height = 0;
581
582
#ifdef DEBUG
583
    stream_state *st = s->state; /* Save because s_close resets s->state. */
584
#endif
585
2.14M
    gs_composite_t *pcomp_first = NULL, *pcomp_last = NULL;
586
2.14M
    tile_slot bits;             /* parameters for reading bits */
587
588
    /* pad the cbuf data area a bit (just in case) */
589
2.14M
    if ((cbuf_storage = gs_alloc_bytes(mem, cbuf_size + sizeof(double),
590
2.14M
                               "clist_playback_band(cbuf_storage)")) == NULL) {
591
0
        return_error(gs_error_VMerror);
592
0
    }
593
2.14M
    cbuf.data = (byte *)cbuf_storage;
594
2.14M
    cbuf.size = cbuf_size;
595
2.14M
    cbuf.s = s;
596
2.14M
    cbuf.end_status = 0;
597
2.14M
    set_cb_end(&cbuf, cbuf.data + cbuf.size);
598
2.14M
    cbp = cbuf.end;
599
600
2.14M
    pfs.dev = NULL; /* Indicate "not initialized". */
601
2.14M
    memset(&ht_buff, 0, sizeof(ht_buff));
602
603
    /* The following initializations are to quiet gcc warnings. */
604
2.14M
    memset(&bits, 0, sizeof(bits));
605
2.14M
    memset(&tile_bits, 0, sizeof(tile_bits));
606
2.14M
    memset(&clip_save, 0, sizeof(clip_save));
607
2.14M
    memset(&state_slot, 0, sizeof(state_slot));
608
2.14M
    ppos.x = ppos.y = 0;
609
610
2.14M
in:                             /* Initialize for a new page. */
611
2.14M
    tdev = target;
612
2.14M
    set_colors = state.colors;
613
2.14M
    set_dev_colors = state.tile_color_devn;
614
2.14M
    use_clip = false;
615
2.14M
    pcpath = NULL;
616
2.14M
    if (clipper_dev_open)
617
0
        gx_destroy_clip_device_on_stack(&clipper_dev);
618
2.14M
    clipper_dev_open = false;
619
2.14M
    notes = sn_none;
620
2.14M
    data_x = 0;
621
2.14M
    {
622
2.14M
        static const gx_clist_state cls_initial = { cls_initial_values };
623
624
2.14M
        state = cls_initial;
625
2.14M
    }
626
2.14M
    state_tile.id = gx_no_bitmap_id;
627
2.14M
    state_tile.shift = state_tile.rep_shift = 0;
628
2.14M
    state_tile.size.x = state_tile.size.y = 0;
629
2.14M
    state_tile.num_planes = 1;
630
2.14M
    tile_phase.x = x0;
631
2.14M
    tile_phase.y = y0;
632
2.14M
    gx_path_init_local(&path, mem);
633
2.14M
    in_path = false;
634
    /*
635
     * Initialize the clipping region to the full page.
636
     * (Since we also initialize use_clip to false, this is arbitrary.)
637
     */
638
2.14M
    {
639
2.14M
        gs_fixed_rect cbox;
640
641
2.14M
        gx_cpath_init_local(&clip_path, mem);
642
2.14M
        cbox.p.x = 0;
643
2.14M
        cbox.p.y = 0;
644
2.14M
        cbox.q.x = cdev->width;
645
2.14M
        cbox.q.y = cdev->height;
646
2.14M
        gx_cpath_from_rectangle(&clip_path, &cbox);
647
2.14M
    }
648
2.14M
    if (target != 0)
649
2.14M
        (*dev_proc(target, get_clipping_box))(target, &target_box);
650
2.14M
    memset(&gs_gstate, 0, sizeof(gs_gstate));
651
2.14M
    GS_STATE_INIT_VALUES_CLIST((&gs_gstate));
652
2.14M
    code = gs_gstate_initialize(&gs_gstate, mem);
653
2.14M
    if (code < 0)
654
0
        goto out;
655
2.14M
    gs_gstate.device = tdev;
656
2.14M
    gs_gstate.view_clip = NULL; /* Avoid issues in pdf14 fill stroke */
657
2.14M
    gs_gstate.clip_path = &clip_path;
658
2.14M
    pcs = gs_cspace_new_DeviceGray(mem);
659
2.14M
    if (pcs == NULL) {
660
0
        code = gs_note_error(gs_error_VMerror);
661
0
        goto out;
662
0
    }
663
2.14M
    code = pcs->type->install_cspace(pcs, &gs_gstate);
664
2.14M
    if (code < 0) {
665
0
        rc_decrement(pcs, "clist_playback_band");
666
0
        goto out;
667
0
    }
668
2.14M
    if (gs_gstate.icc_manager != NULL) {
669
2.14M
        gs_gstate.icc_manager->srcgtag_profile = cdev->srcgtag;
670
2.14M
        rc_increment(cdev->srcgtag);
671
2.14M
    }
672
673
2.14M
    gs_gstate.color[0].color_space = pcs; /* we already have one ref */
674
2.14M
    gs_gstate.color[1].color_space = pcs;
675
2.14M
    rc_increment_cs(pcs); /* increment for second ref */
676
    /* Initialize client color and device color */
677
2.14M
    gs_gstate.color[0].ccolor =
678
2.14M
        gs_alloc_struct(mem, gs_client_color, &st_client_color, "clist_playback_band");
679
2.14M
    gs_gstate.color[1].ccolor =
680
2.14M
        gs_alloc_struct(mem, gs_client_color, &st_client_color, "clist_playback_band");
681
2.14M
    gs_gstate.color[0].dev_color =
682
2.14M
        gs_alloc_struct(mem, gx_device_color, &st_device_color, "clist_playback_band");
683
2.14M
    gs_gstate.color[1].dev_color =
684
2.14M
        gs_alloc_struct(mem, gx_device_color, &st_device_color, "clist_playback_band");
685
2.14M
    if (gs_gstate.color[0].ccolor == 0 || gs_gstate.color[0].dev_color == 0 ||
686
2.14M
        gs_gstate.color[1].ccolor == 0 || gs_gstate.color[1].dev_color == 0
687
2.14M
        ) {
688
0
        gs_free_object(mem, gs_gstate.color[0].ccolor, "clist_playback_band");
689
0
        gs_free_object(mem, gs_gstate.color[1].ccolor, "clist_playback_band");
690
0
        gs_free_object(mem, gs_gstate.color[0].dev_color, "clist_playback_band");
691
0
        gs_free_object(mem, gs_gstate.color[1].dev_color, "clist_playback_band");
692
0
        if (clipper_dev_open)
693
0
            gx_destroy_clip_device_on_stack(&clipper_dev);
694
0
        return_error(gs_error_VMerror);
695
0
    }
696
2.14M
    gs_gstate.color[0].color_space->pclient_color_space_data =
697
2.14M
        pcs->pclient_color_space_data;
698
2.14M
    cs_full_init_color(gs_gstate.color[0].ccolor, pcs);
699
2.14M
    gx_unset_dev_color(&gs_gstate);
700
701
2.14M
    gs_gstate.color[1].color_space->pclient_color_space_data =
702
2.14M
        pcs->pclient_color_space_data;
703
2.14M
    cs_full_init_color(gs_gstate.color[1].ccolor, pcs);
704
2.14M
    gx_unset_dev_color(&gs_gstate);
705
706
    /* Remove the ICC link cache and replace with the device link cache
707
       so that we share the cache across bands */
708
2.14M
    rc_decrement(gs_gstate.icc_link_cache,"clist_playback_band");
709
2.14M
    gs_gstate.icc_link_cache = cdev->icc_cache_cl;
710
    /* Need to lock during the increment of the link cache */
711
2.14M
    gx_monitor_enter(cdev->icc_cache_cl->lock);
712
2.14M
    rc_increment(cdev->icc_cache_cl);
713
2.14M
    gx_monitor_leave(cdev->icc_cache_cl->lock); /* let everyone run */
714
2.14M
    if (code < 0)
715
0
        goto out;
716
717
2.14M
    gs_gstate.line_params.dash.pattern = dash_pattern;
718
2.14M
    if (tdev != 0) {
719
2.14M
        gx_set_cmap_procs(&gs_gstate, tdev);
720
2.14M
    }
721
2.14M
    gx_gstate_setscreenphase(&gs_gstate, -x0, -y0, gs_color_select_all);
722
#ifdef DEBUG
723
    halftone_type = ht_type_none;
724
#endif
725
2.14M
    fill_color.ccolor_valid = false;
726
2.14M
    color_unset(&fill_color);
727
2.14M
    data_bits = gs_alloc_bytes(mem, data_bits_size,
728
2.14M
                               "clist_playback_band(data_bits)");
729
2.14M
    if (data_bits == 0) {
730
0
        code = gs_note_error(gs_error_VMerror);
731
0
        goto out;
732
0
    }
733
347M
    while (code >= 0) {
734
347M
        int compress;
735
347M
        int depth = 0x7badf00d; /* Initialize against indeterminizm. */
736
347M
        int raster = 0x7badf00d; /* Initialize against indeterminizm. */
737
347M
        byte *source = NULL;  /* Initialize against indeterminizm. */
738
347M
        gx_color_index colors[2];
739
347M
        gx_color_index *pcolor;
740
347M
        gx_device_color *pdcolor = NULL;
741
347M
        gs_logical_operation_t log_op;
742
743
        /* Make sure the buffer contains a full command. */
744
347M
        if (cbp >= cbuf.warn_limit) {
745
3.27M
            if (cbuf.end_status < 0) {  /* End of file or error. */
746
16.9k
                if (cbp >= cbuf.end) {
747
0
                    code = (cbuf.end_status == EOFC ? 0 :
748
0
                            gs_note_error(gs_error_ioerror));
749
0
                    break;
750
0
                }
751
3.25M
            } else {
752
3.25M
                code = top_up_cbuf(&cbuf, &cbp);
753
3.25M
                if (code < 0)
754
0
                    goto top_up_failed;
755
3.25M
            }
756
3.27M
        }
757
347M
        op = *cbp++;
758
#ifdef DEBUG
759
        if (gs_debug_c('L')) {
760
            int64_t offset = clist_file_offset(st, cbp - 1 - cbuf.data);
761
762
            dmlprintf1(mem, "[L] %"PRIu64":", offset);
763
            clist_debug_op(mem, cbp-1);
764
        }
765
#endif
766
347M
        switch (op >> 4) {
767
41.0M
            case cmd_op_misc >> 4:
768
41.0M
                switch (op) {
769
7.50M
                    case cmd_opv_end_run:
770
7.50M
                        if_debug0m('L', mem, "\n");
771
7.50M
                        continue;
772
3.38k
                    case cmd_opv_set_tile_size:
773
3.38k
                        cbuf.ptr = cbp;
774
3.38k
                        code = read_set_tile_size(&cbuf, &tile_bits,
775
3.38k
                                    gx_device_is_pattern_clist((gx_device *)cdev));
776
3.38k
                        cbp = cbuf.ptr;
777
3.38k
                        if (code < 0)
778
0
                            goto out;
779
3.38k
                        continue;
780
30.1k
                    case cmd_opv_set_tile_phase:
781
30.1k
                        cmd_getw(state.tile_phase.x, cbp);
782
30.1k
                        cmd_getw(state.tile_phase.y, cbp);
783
30.1k
                        if_debug2m('L', mem, " (%d,%d)\n",
784
30.1k
                                   state.tile_phase.x,
785
30.1k
                                   state.tile_phase.y);
786
30.1k
                        goto set_tile_phase;
787
89.4k
                    case cmd_opv_set_screen_phaseT:
788
89.4k
                        cmd_getw(state.screen_phase[gs_color_select_texture].x, cbp);
789
89.4k
                        cmd_getw(state.screen_phase[gs_color_select_texture].y, cbp);
790
89.4k
                        if_debug2m('L', mem, " (%d,%d)\n",
791
89.4k
                                   state.screen_phase[0].x,
792
89.4k
                                   state.screen_phase[gs_color_select_texture].y);
793
89.4k
                        gx_gstate_setscreenphase(&gs_gstate,
794
89.4k
                                                 state.screen_phase[gs_color_select_texture].x - x0,
795
89.4k
                                                 state.screen_phase[gs_color_select_texture].y - y0,
796
89.4k
                                                 gs_color_select_texture);
797
89.4k
                        continue;
798
0
                    case cmd_opv_set_screen_phaseS:
799
0
                        cmd_getw(state.screen_phase[gs_color_select_source].x, cbp);
800
0
                        cmd_getw(state.screen_phase[gs_color_select_source].y, cbp);
801
0
                        if_debug2m('L', mem, " (%d,%d)\n",
802
0
                                   state.screen_phase[gs_color_select_source].x,
803
0
                                   state.screen_phase[gs_color_select_source].y);
804
0
                        gx_gstate_setscreenphase(&gs_gstate,
805
0
                                                 state.screen_phase[gs_color_select_source].x - x0,
806
0
                                                 state.screen_phase[gs_color_select_source].y - y0,
807
0
                                                 gs_color_select_source);
808
0
                        continue;
809
13.1k
                    case cmd_opv_set_tile_bits:
810
13.1k
                        bits = tile_bits;
811
13.1k
                        compress = 0;
812
8.14M
                      stb:
813
8.14M
                        cbuf.ptr = cbp;
814
8.14M
                        code = read_set_bits(&cbuf, &bits, compress,
815
8.14M
                                             &state, &state_tile, &state_slot,
816
8.14M
                                             cdev, mem);
817
8.14M
                        cbp = cbuf.ptr;
818
8.14M
                        if (code < 0)
819
0
                            goto out;
820
8.14M
                        goto stp;
821
8.14M
                    case cmd_opv_set_bits:
822
8.13M
do_opv_set_bits:
823
8.13M
                        compress = *cbp & 3;
824
8.13M
                        bits.head.depth = *cbp++ >> 2;
825
8.13M
                        cmd_getw(bits.width, cbp);
826
8.13M
                        cmd_getw(bits.height, cbp);
827
8.13M
                        if (op == cmd_opv_set_bits_planar)
828
8.13M
                            cmd_getw(bits.num_planes, cbp);
829
8.13M
                        else
830
8.13M
                            bits.num_planes = 1;
831
8.13M
                        if_debug5m('L', mem, " compress=%d depth=%d size=(%d,%d) planes=%d",
832
8.13M
                                   compress, bits.head.depth,
833
8.13M
                                   bits.width, bits.height, bits.num_planes);
834
8.13M
                        bits.raster =
835
8.13M
                            bitmap_raster(bits.width * bits.head.depth);
836
8.13M
                        bits.x_reps = bits.y_reps = 1;
837
8.13M
                        bits.shift = bits.rep_shift = 0;
838
8.13M
                        goto stb;
839
177k
                    case cmd_opv_set_tile_color:
840
177k
                        set_colors = state.tile_colors;
841
177k
                        if_debug0m('L', mem, "\n");
842
177k
                        continue;
843
5.47M
                    case cmd_opv_set_misc:
844
5.47M
                        {
845
5.47M
                            uint cb = *cbp++;
846
847
5.47M
                            switch (cb >> 6) {
848
712k
                                case cmd_set_misc_lop >> 6:
849
712k
                                    cmd_getw(state.lop, cbp);
850
712k
                                    state.lop = (state.lop << 6) + (cb & 0x3f);
851
712k
                                    if_debug1m('L', mem, " lop=0x%x\n", state.lop);
852
712k
                                    if (state.lop_enabled)
853
524k
                                        gs_gstate.log_op = state.lop;
854
712k
                                    break;
855
296k
                                case cmd_set_misc_data_x >> 6:
856
296k
                                    if (cb & 0x20)
857
296k
                                        cmd_getw(data_x, cbp);
858
296k
                                    else
859
296k
                                        data_x = 0;
860
296k
                                    data_x = (data_x << 5) + (cb & 0x1f);
861
296k
                                    if_debug1m('L', mem, " data_x=%d\n", data_x);
862
296k
                                    break;
863
4.46M
                                case cmd_set_misc_map >> 6:
864
4.46M
                                    cbuf.ptr = cbp;
865
4.46M
                                    code = read_set_misc_map(cb, &cbuf, &gs_gstate, mem);
866
4.46M
                                    if (code < 0)
867
0
                                        goto out;
868
4.46M
                                    cbp = cbuf.ptr;
869
4.46M
                                    break;
870
0
                                case cmd_set_misc_halftone >> 6: {
871
0
                                    uint num_comp;
872
#ifdef DEBUG
873
                                    halftone_type = cb & 0x3f;
874
#endif
875
0
                                    cmd_getw(num_comp, cbp);
876
#ifdef DEBUG
877
                                    if_debug2m('L', mem, " halftone type=%d num_comp=%u\n",
878
                                               halftone_type, num_comp);
879
#endif
880
0
                                    code = cmd_resize_halftone(
881
0
                                                        &gs_gstate.dev_ht[HT_OBJTYPE_DEFAULT],
882
0
                                                        num_comp, mem);
883
0
                                    if (code < 0)
884
0
                                        goto out;
885
0
                                    break;
886
0
                                }
887
0
                                default:
888
0
                                    goto bad_op;
889
5.47M
                            }
890
5.47M
                        }
891
5.47M
                        continue;
892
5.47M
                    case cmd_opv_enable_lop:
893
188k
                        state.lop_enabled = true;
894
188k
                        gs_gstate.log_op = state.lop;
895
188k
                        if_debug0m('L', mem, "\n");
896
188k
                        continue;
897
92.4k
                    case cmd_opv_disable_lop:
898
92.4k
                        state.lop_enabled = false;
899
92.4k
                        gs_gstate.log_op = lop_default;
900
92.4k
                        if_debug0m('L', mem, "\n");
901
92.4k
                        continue;
902
2.14M
                    case cmd_opv_end_page:
903
2.14M
                        if_debug0m('L', mem, "\n");
904
                        /*
905
                         * Do end-of-page cleanup, then reinitialize if
906
                         * there are more pages to come.
907
                         */
908
2.14M
                        goto out;
909
0
                    case cmd_opv_delta_color0:
910
0
                        pcolor = &set_colors[0];
911
0
                        goto delta2_c;
912
17.1M
                    case cmd_opv_delta_color1:
913
17.1M
                        pcolor = &set_colors[1];
914
17.1M
                      delta2_c:set_colors = state.colors;
915
                        /* See comments for cmd_put_color() in gxclutil.c. */
916
17.1M
                        {
917
17.1M
                            gx_color_index delta = 0;
918
17.1M
                            uint data;
919
920
17.1M
                            dev_depth = (tdev->color_info.depth <= 8*sizeof(gx_color_index) ?
921
17.1M
                                         tdev->color_info.depth : 8*sizeof(gx_color_index));
922
17.1M
                            dev_depth_bytes = (dev_depth + 7) >> 3;
923
17.1M
                            switch (dev_depth_bytes) {
924
                                /* For cases with an even number of bytes */
925
0
                                case 8:
926
0
                                    data = *cbp++;
927
0
                                    delta = (((gx_color_index)
928
0
                                        ((data & 0xf0) << 4) + (data & 0x0f)) << 24) << 24;
929
                                    /* fall through */
930
0
                                case 6:
931
0
                                    data = *cbp++;
932
0
                                    delta |= (((gx_color_index)
933
0
                                        ((data & 0xf0) << 4) + (data & 0x0f)) << 16) << 16;
934
                                    /* fall through */
935
9.46M
                                case 4:
936
9.46M
                                    data = *cbp++;
937
9.46M
                                    delta |= ((gx_color_index)
938
9.46M
                                        ((data & 0xf0) << 4) + (data & 0x0f)) << 16;
939
                                    /* fall through */
940
9.46M
                                case 2:
941
9.46M
                                    data = *cbp++;
942
9.46M
                                    delta |= ((gx_color_index)
943
9.46M
                                        ((data & 0xf0) << 4) + (data & 0x0f));
944
9.46M
                                    break;
945
                                /* For cases with an odd number of bytes */
946
0
                                case 7:
947
0
                                    data = *cbp++;
948
0
                                    delta = ((gx_color_index)
949
0
                                        ((data & 0xf0) << 4) + (data & 0x0f)) << 16;
950
                                    /* fall through */
951
0
                                case 5:
952
0
                                    data = *cbp++;
953
0
                                    delta |= ((gx_color_index)
954
0
                                        ((data & 0xf0) << 4) + (data & 0x0f));
955
                                    /* fall through */
956
7.70M
                                case 3:
957
7.70M
                                    data = *cbp++;
958
7.70M
                                    odd_delta_shift = (dev_depth_bytes - 3) * 8;
959
7.70M
                                    delta |= ((gx_color_index)
960
7.70M
                                        ((data & 0xe0) << 3) + (data & 0x1f)) << odd_delta_shift;
961
7.70M
                                    data = *cbp++;
962
7.70M
                                    delta |= ((gx_color_index) ((data & 0xf8) << 2) + (data & 0x07))
963
7.70M
                                                        << (odd_delta_shift + 11);
964
17.1M
                            }
965
17.1M
                            *pcolor += delta - cmd_delta_offsets[dev_depth_bytes];
966
17.1M
                        }
967
17.1M
                        if (sizeof(*pcolor) <= sizeof(ulong))
968
17.1M
                            if_debug1m('L', mem, " 0x%lx\n", (ulong)*pcolor);
969
0
                        else
970
17.1M
                            if_debug2m('L', mem, " 0x%8lx%08lx\n",
971
17.1M
                                       (ulong)(*pcolor >> 8*(sizeof(*pcolor) - sizeof(ulong))), (ulong)*pcolor);
972
17.1M
                        continue;
973
0
                    case cmd_opv_set_copy_color:
974
0
                        state.color_is_alpha = 0;
975
0
                        if_debug0m('L', mem, "\n");
976
0
                        continue;
977
0
                    case cmd_opv_set_copy_alpha:
978
0
                        state.color_is_alpha = 1;
979
0
                        if_debug0m('L', mem, "\n");
980
0
                        continue;
981
0
                    default:
982
0
                        goto bad_op;
983
41.0M
                }
984
                /*NOTREACHED */
985
97.9k
            case cmd_op_set_color0 >> 4:
986
97.9k
                pcolor = &set_colors[0];
987
97.9k
                goto set_color;
988
16.0M
            case cmd_op_set_color1 >> 4:
989
16.0M
                pcolor = &set_colors[1];
990
16.0M
              set_color:set_colors = state.colors;
991
                /*
992
                 * We have a special case for gx_no_color_index. If the low
993
                 * order 4 bits are "cmd_no_color_index" then we really
994
                 * have a value of gx_no_color_index.  Otherwise the these
995
                 * bits indicate the number of low order zero bytes in the
996
                 * value.  See comments for cmd_put_color() in gxclutil.c.
997
                 */
998
16.0M
                num_zero_bytes = op & 0x0f;
999
1000
16.0M
                if (num_zero_bytes == cmd_no_color_index)
1001
26
                    *pcolor = gx_no_color_index;
1002
16.0M
                else {
1003
16.0M
                    gx_color_index color = 0;
1004
1005
16.0M
                    dev_depth = (tdev->color_info.depth < 8*sizeof(gx_color_index) ?
1006
16.0M
                                 tdev->color_info.depth : 8*sizeof(gx_color_index));
1007
16.0M
                    dev_depth_bytes = (dev_depth + 7) >> 3;
1008
16.0M
                    switch (dev_depth_bytes - num_zero_bytes) {
1009
0
                        case 8:
1010
0
                            color = (gx_color_index) * cbp++;
1011
0
                        case 7:
1012
0
                            color = (color << 8) | (gx_color_index) * cbp++;
1013
0
                        case 6:
1014
0
                            color = (color << 8) | (gx_color_index) * cbp++;
1015
0
                        case 5:
1016
0
                            color = (color << 8) | (gx_color_index) * cbp++;
1017
845k
                        case 4:
1018
845k
                            color = (color << 8) | (gx_color_index) * cbp++;
1019
2.55M
                        case 3:
1020
2.55M
                            color = (color << 8) | (gx_color_index) * cbp++;
1021
3.10M
                        case 2:
1022
3.10M
                            color = (color << 8) | (gx_color_index) * cbp++;
1023
15.3M
                        case 1:
1024
15.3M
                            color = (color << 8) | (gx_color_index) * cbp++;
1025
16.0M
                        default:
1026
16.0M
                            break;
1027
16.0M
                    }
1028
16.0M
                    color <<= num_zero_bytes * 8;
1029
16.0M
                    *pcolor = color;
1030
16.0M
                }
1031
16.0M
                if (sizeof(*pcolor) <= sizeof(ulong))
1032
16.0M
                    if_debug1m('L', mem, " 0x%lx\n", (ulong)*pcolor);
1033
0
                else
1034
16.0M
                    if_debug2m('L', mem, " 0x%8lx%08lx\n",
1035
16.0M
                               (ulong)(*pcolor >> 8*(sizeof(*pcolor) - sizeof(ulong))), (ulong)*pcolor);
1036
16.0M
                continue;
1037
4.15M
            case cmd_op_fill_rect >> 4:
1038
4.29M
            case cmd_op_tile_rect >> 4:
1039
4.29M
                cbp = cmd_read_rect(op, &state.rect, cbp);
1040
4.29M
                break;
1041
4.65M
            case cmd_op_fill_rect_short >> 4:
1042
5.81M
            case cmd_op_tile_rect_short >> 4:
1043
5.81M
                state.rect.x += *cbp + cmd_min_short;
1044
5.81M
                state.rect.width += cbp[1] + cmd_min_short;
1045
5.81M
                if (op & 0xf) {
1046
3.63M
                    state.rect.height += (op & 0xf) + cmd_min_dxy_tiny;
1047
3.63M
                    cbp += 2;
1048
3.63M
                } else {
1049
2.18M
                    state.rect.y += cbp[2] + cmd_min_short;
1050
2.18M
                    state.rect.height += cbp[3] + cmd_min_short;
1051
2.18M
                    cbp += 4;
1052
2.18M
                }
1053
5.81M
                break;
1054
41.6M
            case cmd_op_fill_rect_tiny >> 4:
1055
43.2M
            case cmd_op_tile_rect_tiny >> 4:
1056
43.2M
                if (op & 8)
1057
30.4M
                    state.rect.x += state.rect.width;
1058
12.7M
                else {
1059
12.7M
                    int txy = *cbp++;
1060
1061
12.7M
                    state.rect.x += (txy >> 4) + cmd_min_dxy_tiny;
1062
12.7M
                    state.rect.y += (txy & 0xf) + cmd_min_dxy_tiny;
1063
12.7M
                }
1064
43.2M
                state.rect.width += (op & 7) + cmd_min_dw_tiny;
1065
43.2M
                break;
1066
24.1M
            case cmd_op_copy_mono_planes >> 4:
1067
24.1M
                cmd_getw(plane_height, cbp);
1068
24.1M
                if (plane_height == 0) {
1069
                    /* We are doing a copy mono */
1070
24.1M
                    depth = 1;
1071
24.1M
                } else {
1072
6.91k
                    depth = tdev->color_info.depth;
1073
6.91k
                }
1074
24.1M
                if_debug1m('L', mem, " plane_height=0x%x", plane_height);
1075
24.1M
                goto copy;
1076
2.56M
            case cmd_op_copy_color_alpha >> 4:
1077
2.56M
                if (state.color_is_alpha) {
1078
0
                    if (!(op & 8))
1079
0
                        depth = *cbp++;
1080
0
                } else
1081
2.56M
                    depth = tdev->color_info.depth;
1082
2.56M
                plane_height = 0;
1083
26.6M
              copy:cmd_getw(state.rect.x, cbp);
1084
26.6M
                cmd_getw(state.rect.y, cbp);
1085
26.6M
                if (op & cmd_copy_use_tile) {   /* Use the current "tile". */
1086
#ifdef DEBUG
1087
                    if (state_slot->index != state.tile_index) {
1088
                        mlprintf2(mem, "state_slot->index = %d, state.tile_index = %d!\n",
1089
                                  state_slot->index,
1090
                                  state.tile_index);
1091
                        code = gs_note_error(gs_error_ioerror);
1092
                        goto out;
1093
                    }
1094
#endif
1095
23.4M
                    depth = state_slot->head.depth;
1096
23.4M
                    state.rect.width = state_slot->width;
1097
23.4M
                    state.rect.height = state_slot->height;
1098
23.4M
                    if (state.rect.y + state.rect.height > cdev->height)
1099
27.4k
                        state.rect.height = cdev->height - state.rect.y; /* clamp as writer did */
1100
23.4M
                    raster = state_slot->raster;
1101
23.4M
                    source = (byte *) (state_slot + 1);
1102
23.4M
                } else {        /* Read width, height, bits. */
1103
                    /* depth was set already. */
1104
3.26M
                    uint width_bits, width_bytes;
1105
3.26M
                    uint bytes;
1106
3.26M
                    uchar planes = 1;
1107
3.26M
                    uint plane_depth = depth;
1108
3.26M
                    uint pln;
1109
3.26M
                    byte compression = op & 3;
1110
3.26M
                    uint out_bytes;
1111
1112
3.26M
                    cmd_getw(state.rect.width, cbp);
1113
3.26M
                    cmd_getw(state.rect.height, cbp);
1114
3.26M
                    if (plane_height != 0) {
1115
6.91k
                        planes = tdev->color_info.num_components;
1116
6.91k
                        plane_depth /= planes;
1117
6.91k
                    }
1118
3.26M
                    width_bits = state.rect.width * plane_depth;
1119
3.26M
                    bytes = clist_bitmap_bytes(width_bits,
1120
3.26M
                                               state.rect.height,
1121
3.26M
                                               op & 3, &width_bytes,
1122
3.26M
                                               (uint *)&raster);
1123
3.26M
                    if (planes > 1) {
1124
6.91k
                        out_bytes = raster * state.rect.height;
1125
6.91k
                        plane_height = state.rect.height;
1126
3.25M
                    } else {
1127
3.25M
                        out_bytes = bytes;
1128
3.25M
                    }
1129
                    /* copy_mono and copy_color/alpha */
1130
                    /* ensure that the bits will fit in a single buffer, */
1131
                    /* even after decompression if compressed. */
1132
#ifdef DEBUG
1133
                    if (planes * out_bytes > data_bits_size) {
1134
                        mlprintf6(mem, "bitmap size exceeds buffer!  width=%d raster=%d height=%d\n    file pos %"PRId64" buf pos %d/%d\n",
1135
                                  state.rect.width, raster,
1136
                                  state.rect.height,
1137
                                  stell(s), (int)(cbp - cbuf.data),
1138
                                  (int)(cbuf.end - cbuf.data));
1139
                        code = gs_note_error(gs_error_ioerror);
1140
                        goto out;
1141
                    }
1142
#endif
1143
6.54M
                    for (pln = 0; pln < planes; pln++) {
1144
3.28M
                        byte *plane_bits = data_bits + pln * plane_height * raster;
1145
1146
                        /* Fill the cbuf if needed to get the data for the plane */
1147
3.28M
                        if (cbp + out_bytes >= cbuf.warn_limit) {
1148
50.7k
                            code = top_up_cbuf(&cbuf, &cbp);
1149
50.7k
                            if (code < 0)
1150
0
                                goto top_up_failed;
1151
50.7k
                        }
1152
3.28M
                        if (pln)
1153
20.7k
                            compression = *cbp++;
1154
1155
3.28M
                        if (compression == cmd_compress_const) {
1156
0
                            cbp = cmd_read_data(&cbuf, plane_bits, 1, cbp);
1157
0
                            if (width_bytes > 0 && state.rect.height > 0)
1158
0
                                memset(plane_bits+1, *plane_bits, width_bytes * state.rect.height - 1);
1159
0
                            if (pln == 0)
1160
0
                                source = data_bits;
1161
1162
3.28M
                        } else if (compression) {       /* Decompress the image data. */
1163
696
                            stream_cursor_read r;
1164
696
                            stream_cursor_write w;
1165
1166
                            /* We don't know the data length a priori, */
1167
                            /* so to be conservative, we read */
1168
                            /* the uncompressed size. */
1169
696
                            uint cleft = cbuf.end - cbp;
1170
1171
696
                            if (cleft < bytes  && !cbuf.end_status) {
1172
0
                                uint nread = cbuf_size - cleft;
1173
1174
0
                                advance_buffer(&cbuf, cbp);
1175
0
                                cbuf.end_status = sgets(s, cbuf.data + cleft, nread, &nread);
1176
0
                                set_cb_end(&cbuf, cbuf.data + cleft + nread);
1177
0
                                cbp = cbuf.data;
1178
0
                            }
1179
696
                            r.ptr = cbp - 1;
1180
696
                            r.limit = cbuf.end - 1;
1181
696
                            w.ptr = plane_bits - 1;
1182
696
                            w.limit = w.ptr + data_bits_size;
1183
696
                            switch (compression) {
1184
0
                                case cmd_compress_rle:
1185
0
                                    {
1186
0
                                        stream_RLD_state sstate;
1187
1188
0
                                        clist_rld_init(&sstate);
1189
                                        /* The process procedure can't fail. */
1190
0
                                        (*s_RLD_template.process)
1191
0
                                            ((stream_state *)&sstate, &r, &w, true);
1192
0
                                    }
1193
0
                                    break;
1194
696
                                case cmd_compress_cfe:
1195
696
                                    {
1196
696
                                        stream_CFD_state sstate;
1197
1198
696
                                        clist_cfd_init(&sstate,
1199
696
                                        width_bytes << 3 /*state.rect.width */ ,
1200
696
                                                       state.rect.height, mem);
1201
                                        /* The process procedure can't fail. */
1202
696
                                        (*s_CFD_template.process)
1203
696
                                            ((stream_state *)&sstate, &r, &w, true);
1204
696
                                        (*s_CFD_template.release)
1205
696
                                            ((stream_state *)&sstate);
1206
696
                                    }
1207
696
                                    break;
1208
0
                                default:
1209
0
                                    goto bad_op;
1210
696
                            }
1211
696
                            cbp = r.ptr + 1;
1212
696
                            if (pln == 0)
1213
696
                                source = data_bits;
1214
3.28M
                        } else if ((state.rect.height > 1 && width_bytes != raster) ||
1215
3.18M
                                   (plane_height != 0)) {
1216
131k
                            cbp = cmd_read_short_bits(&cbuf, plane_bits, bytes, width_bytes,
1217
131k
                                                      state.rect.height, raster, cbp);
1218
131k
                            if (pln == 0)
1219
110k
                                source = data_bits;
1220
3.15M
                        } else {
1221
                            /* Never used for planar data */
1222
3.15M
                            cbp = cmd_read_data(&cbuf, cbuf.data, bytes, cbp);
1223
3.15M
                            source = cbuf.data;
1224
3.15M
                        }
1225
3.28M
                    }
1226
#ifdef DEBUG
1227
                    if (gs_debug_c('L')) {
1228
                        dmprintf2(mem, " depth=%d, data_x=%d\n",
1229
                                  depth, data_x);
1230
                        cmd_print_bits(mem, source, state.rect.width,
1231
                                       state.rect.height, raster);
1232
                    }
1233
#endif
1234
3.26M
                }
1235
26.6M
                break;
1236
26.6M
            case cmd_op_delta_tile_index >> 4:
1237
5.32M
                state.tile_index += (int)(op & 0xf) - 8;
1238
5.32M
                goto sti;
1239
9.52M
            case cmd_op_set_tile_index >> 4:
1240
9.52M
                state.tile_index =
1241
9.52M
                    ((op & 0xf) << 8) + *cbp++;
1242
14.8M
              sti:state_slot =
1243
14.8M
                    (tile_slot *) (cdev->cache_chunk->data +
1244
14.8M
                                 cdev->tile_table[state.tile_index].offset);
1245
14.8M
                if_debug2m('L', mem, " index=%u offset=%lu\n",
1246
14.8M
                           state.tile_index,
1247
14.8M
                           cdev->tile_table[state.tile_index].offset);
1248
14.8M
                state_tile.data = (byte *) (state_slot + 1);
1249
22.9M
              stp:state_tile.size.x = state_slot->width;
1250
22.9M
                state_tile.size.y = state_slot->height;
1251
22.9M
                state_tile.raster = state_slot->raster;
1252
22.9M
                state_tile.rep_width = state_tile.size.x /
1253
22.9M
                    state_slot->x_reps;
1254
22.9M
                state_tile.rep_height = state_tile.size.y /
1255
22.9M
                    state_slot->y_reps;
1256
22.9M
                state_tile.rep_shift = state_slot->rep_shift;
1257
22.9M
                state_tile.shift = state_slot->shift;
1258
22.9M
                state_tile.id = state_slot->id;
1259
22.9M
                state_tile.num_planes = state_slot->num_planes;
1260
23.0M
set_tile_phase:
1261
23.0M
                if (state_tile.size.x)
1262
23.0M
                    tile_phase.x =
1263
23.0M
                        (state.tile_phase.x + x0) % state_tile.size.x;
1264
                /*
1265
                 * The true tile height for shifted tiles is not
1266
                 * size.y: see gxbitmap.h for the computation.
1267
                 */
1268
23.0M
                if (state_tile.size.y) {
1269
23.0M
                    int full_height;
1270
1271
23.0M
                    if (state_tile.shift == 0)
1272
23.0M
                        full_height = state_tile.size.y;
1273
0
                    else
1274
0
                        full_height = state_tile.rep_height *
1275
0
                                    (state_tile.rep_width /
1276
0
                                     igcd(state_tile.rep_shift,
1277
0
                                          state_tile.rep_width));
1278
23.0M
                    tile_phase.y = (state.tile_phase.y + y0) % full_height;
1279
23.0M
                }
1280
23.0M
                continue;
1281
59.1M
            case cmd_op_misc2 >> 4:
1282
59.1M
                switch (op) {
1283
0
                    case cmd_opv_set_bits_planar:
1284
0
                        goto do_opv_set_bits;
1285
104k
                    case cmd_opv_set_fill_adjust:
1286
104k
                        cmd_get_value(gs_gstate.fill_adjust.x, cbp);
1287
104k
                        cmd_get_value(gs_gstate.fill_adjust.y, cbp);
1288
104k
                        if_debug2m('L', mem, " (%g,%g)\n",
1289
104k
                                   fixed2float(gs_gstate.fill_adjust.x),
1290
104k
                                   fixed2float(gs_gstate.fill_adjust.y));
1291
104k
                        continue;
1292
4.45M
                    case cmd_opv_set_ctm:
1293
4.45M
                        {
1294
4.45M
                            gs_matrix mat;
1295
1296
4.45M
                            cbp = cmd_read_matrix(&mat, cbp);
1297
4.45M
                            if_debug6m('L', mem, " [%g %g %g %g %g %g]\n",
1298
4.45M
                                       mat.xx, mat.xy, mat.yx, mat.yy,
1299
4.45M
                                       mat.tx, mat.ty);
1300
4.45M
                            mat.tx -= x0;
1301
4.45M
                            mat.ty -= y0;
1302
4.45M
                            gs_gstate_setmatrix(&gs_gstate, &mat);
1303
4.45M
                        }
1304
4.45M
                        continue;
1305
1.54M
                    case cmd_opv_set_misc2:
1306
1.54M
                        cbuf.ptr = cbp;
1307
1.54M
                        code = read_set_misc2(&cbuf, &gs_gstate, &notes);
1308
1.54M
                        cbp = cbuf.ptr;
1309
1.54M
                        if (code < 0)
1310
0
                            goto out;
1311
1.54M
                        continue;
1312
1.54M
                    case cmd_opv_set_dash:
1313
228k
                        {
1314
228k
                            int nb = *cbp++;
1315
228k
                            int n = nb & 0x3f;
1316
228k
                            float dot_length, offset;
1317
1318
228k
                            cmd_get_value(dot_length, cbp);
1319
228k
                            cmd_get_value(offset, cbp);
1320
228k
                            memcpy(dash_pattern, cbp, n * sizeof(float));
1321
1322
228k
                            gx_set_dash(&gs_gstate.line_params.dash,
1323
228k
                                        dash_pattern, n, offset,
1324
228k
                                        NULL);
1325
228k
                            gx_set_dash_adapt(&gs_gstate.line_params.dash,
1326
228k
                                              (nb & 0x80) != 0);
1327
228k
                            gx_set_dot_length(&gs_gstate.line_params,
1328
228k
                                              dot_length,
1329
228k
                                              (nb & 0x40) != 0);
1330
#ifdef DEBUG
1331
                            if (gs_debug_c('L')) {
1332
                                int i;
1333
1334
                                dmprintf4(mem, " dot=%g(mode %d) adapt=%d offset=%g [",
1335
                                         dot_length,
1336
                                         (nb & 0x40) != 0,
1337
                                         (nb & 0x80) != 0, offset);
1338
                                for (i = 0; i < n; ++i)
1339
                                    dmprintf1(mem, "%g ", dash_pattern[i]);
1340
                                dmputs(mem, "]\n");
1341
                            }
1342
#endif
1343
228k
                            cbp += n * sizeof(float);
1344
228k
                        }
1345
228k
                        break;
1346
7.67k
                    case cmd_opv_enable_clip:
1347
7.67k
                        pcpath = (use_clip ? &clip_path : NULL);
1348
7.67k
                        if (pcpath) {
1349
375
                            code = gx_cpath_ensure_path_list(pcpath);
1350
375
                            if (code < 0)
1351
0
                                goto out;
1352
375
                        }
1353
7.67k
                        if (clipper_dev_open)
1354
0
                            gx_destroy_clip_device_on_stack(&clipper_dev);
1355
7.67k
                        clipper_dev_open = false;
1356
7.67k
                        if_debug0m('L', mem, "\n");
1357
7.67k
                        break;
1358
11.4k
                    case cmd_opv_disable_clip:
1359
11.4k
                        pcpath = NULL;
1360
11.4k
                        if (clipper_dev_open)
1361
11
                            gx_destroy_clip_device_on_stack(&clipper_dev);
1362
11.4k
                        clipper_dev_open = false;
1363
11.4k
                        if_debug0m('L', mem, "\n");
1364
11.4k
                        break;
1365
3.25M
                    case cmd_opv_begin_clip:
1366
3.25M
                        pcpath = NULL;
1367
3.25M
                        if (clipper_dev_open)
1368
1.07k
                            gx_destroy_clip_device_on_stack(&clipper_dev);
1369
3.25M
                        clipper_dev_open = false;
1370
3.25M
                        in_clip = true;
1371
3.25M
                        if_debug0m('L', mem, "\n");
1372
3.25M
                        code = gx_cpath_reset(&clip_path);
1373
3.25M
                        if (code < 0)
1374
0
                            goto out;
1375
3.25M
                        gx_cpath_accum_begin(&clip_accum, mem, false);
1376
3.25M
                        gx_cpath_accum_set_cbox(&clip_accum,
1377
3.25M
                                                &target_box);
1378
3.25M
                        tdev = (gx_device *)&clip_accum;
1379
3.25M
                        clip_save.lop_enabled = state.lop_enabled;
1380
3.25M
                        clip_save.dcolor = fill_color;
1381
3.25M
                        clip_save.fa_save.x = gs_gstate.fill_adjust.x;
1382
3.25M
                        clip_save.fa_save.y = gs_gstate.fill_adjust.y;
1383
3.25M
                        cmd_getw(gs_gstate.fill_adjust.x, cbp);
1384
3.25M
                        cmd_getw(gs_gstate.fill_adjust.y, cbp);
1385
                        /* temporarily set a solid color */
1386
3.25M
                        color_set_pure(&fill_color, (gx_color_index)1);
1387
3.25M
                        state.lop_enabled = false;
1388
3.25M
                        gs_gstate.log_op = lop_default;
1389
3.25M
                        break;
1390
3.25M
                    case cmd_opv_end_clip:
1391
3.25M
                        if_debug0m('L', mem, "\n");
1392
3.25M
                        gx_cpath_accum_end(&clip_accum, &clip_path);
1393
3.25M
                        tdev = target;
1394
                        /*
1395
                         * If the entire band falls within the clip
1396
                         * path, no clipping is needed.
1397
                         */
1398
3.25M
                        {
1399
3.25M
                            gs_fixed_rect cbox;
1400
1401
3.25M
                            gx_cpath_inner_box(&clip_path, &cbox);
1402
3.25M
                            use_clip =
1403
3.25M
                                !(cbox.p.x <= target_box.p.x &&
1404
2.33M
                                  cbox.q.x >= target_box.q.x &&
1405
1.14M
                                  cbox.p.y <= target_box.p.y &&
1406
1.14M
                                  cbox.q.y >= target_box.q.y);
1407
3.25M
                        }
1408
3.25M
                        pcpath = (use_clip ? &clip_path : NULL);
1409
3.25M
                        if (pcpath) {
1410
2.10M
                            code = gx_cpath_ensure_path_list(pcpath);
1411
2.10M
                            if (code < 0)
1412
0
                                goto out;
1413
2.10M
                        }
1414
3.25M
                        if (clipper_dev_open)
1415
0
                            gx_destroy_clip_device_on_stack(&clipper_dev);
1416
3.25M
                        clipper_dev_open = false;
1417
3.25M
                        state.lop_enabled = clip_save.lop_enabled;
1418
3.25M
                        gs_gstate.log_op =
1419
3.25M
                            (state.lop_enabled ? state.lop :
1420
3.25M
                             lop_default);
1421
3.25M
                        fill_color = clip_save.dcolor;
1422
                        /* restore the fill_adjust if it was changed by begin_clip */
1423
3.25M
                        gs_gstate.fill_adjust.x = clip_save.fa_save.x;
1424
3.25M
                        gs_gstate.fill_adjust.y = clip_save.fa_save.y;
1425
3.25M
                        in_clip = false;
1426
3.25M
                        break;
1427
338k
                    case cmd_opv_set_color_space:
1428
338k
                        cbuf.ptr = cbp;
1429
338k
                        code = read_set_color_space(&cbuf, &gs_gstate, cdev, mem);
1430
338k
                        pcs = gs_gstate.color[0].color_space;
1431
338k
                        cbp = cbuf.ptr;
1432
338k
                        if (code < 0) {
1433
0
                            if (code == gs_error_rangecheck)
1434
0
                                goto bad_op;
1435
0
                            goto out;
1436
0
                        }
1437
338k
                        break;
1438
11.5M
                    case cmd_op_fill_rect_hl:
1439
11.5M
                        {
1440
11.5M
                            gs_fixed_rect rect_hl;
1441
1442
11.5M
                            cbp = cmd_read_rect(op & 0xf0, &state.rect, cbp);
1443
11.5M
                            if (fill_color.type != gx_dc_type_devn) {
1444
0
                                if_debug0m('L', mem, "hl rect fill without devn color\n");
1445
0
                                code = gs_note_error(gs_error_typecheck);
1446
0
                                goto out;
1447
0
                            }
1448
11.5M
                            if_debug4m('L', mem, " x=%d y=%d w=%d h=%d\n",
1449
11.5M
                                       state.rect.x, state.rect.y,
1450
11.5M
                                       state.rect.width,state.rect.height);
1451
11.5M
                            rect_hl.p.x = int2fixed(state.rect.x - x0);
1452
11.5M
                            rect_hl.p.y = int2fixed(state.rect.y - y0);
1453
11.5M
                            rect_hl.q.x = int2fixed(state.rect.width) + rect_hl.p.x;
1454
11.5M
                            rect_hl.q.y = int2fixed(state.rect.height) + rect_hl.p.y;
1455
11.5M
                            code = dev_proc(tdev, fill_rectangle_hl_color) (tdev,
1456
11.5M
                                                        &rect_hl, NULL,
1457
11.5M
                                                        &fill_color, NULL);
1458
11.5M
                        }
1459
0
                        continue;
1460
348k
                    case cmd_opv_begin_image_rect:
1461
348k
                        cbuf.ptr = cbp;
1462
348k
                        code = read_begin_image(&cbuf, &image.c, pcs);
1463
348k
                        cbp = cbuf.ptr;
1464
348k
                        if (code < 0)
1465
0
                            goto out;
1466
348k
                        {
1467
348k
                            uint diff;
1468
1469
348k
                            cmd_getw(image_rect.p.x, cbp);
1470
348k
                            cmd_getw(image_rect.p.y, cbp);
1471
348k
                            cmd_getw(diff, cbp);
1472
348k
                            image_rect.q.x = image.d.Width - diff;
1473
348k
                            cmd_getw(diff, cbp);
1474
348k
                            image_rect.q.y = image.d.Height - diff;
1475
348k
                            if_debug4m('L', mem, " rect=(%d,%d),(%d,%d)",
1476
348k
                                       image_rect.p.x, image_rect.p.y,
1477
348k
                                       image_rect.q.x, image_rect.q.y);
1478
348k
                        }
1479
348k
                        goto ibegin;
1480
105k
                    case cmd_opv_begin_image:
1481
105k
                        cbuf.ptr = cbp;
1482
105k
                        code = read_begin_image(&cbuf, &image.c, pcs);
1483
105k
                        cbp = cbuf.ptr;
1484
105k
                        if (code < 0)
1485
0
                            goto out;
1486
105k
                        image_rect.p.x = 0;
1487
105k
                        image_rect.p.y = 0;
1488
105k
                        image_rect.q.x = image.d.Width;
1489
105k
                        image_rect.q.y = image.d.Height;
1490
105k
                        if_debug2m('L', mem, " size=(%d,%d)",
1491
105k
                                  image.d.Width, image.d.Height);
1492
453k
ibegin:                 if_debug0m('L', mem, "\n");
1493
453k
                        {
1494
                            /* Processing an image operation */
1495
453k
                            dev_proc(tdev, set_graphics_type_tag)(tdev, GS_IMAGE_TAG);/* FIXME: what about text bitmaps? */
1496
453k
                            image.i4.override_in_smask = 0;
1497
453k
                            code = (*dev_proc(tdev, begin_typed_image))
1498
453k
                                (tdev, &gs_gstate, NULL,
1499
453k
                                 (const gs_image_common_t *)&image,
1500
453k
                                 &image_rect, &fill_color, pcpath, mem,
1501
453k
                                 &image_info);
1502
453k
                        }
1503
453k
                        if (code < 0)
1504
15
                            goto out;
1505
453k
                        break;
1506
453k
                    case cmd_opv_image_plane_data:
1507
0
                        cmd_getw(data_height, cbp);
1508
0
                        if (data_height == 0) {
1509
0
                            if_debug0m('L', mem, " done image\n");
1510
0
                            code = gx_image_end(image_info, true);
1511
0
                            if (code < 0)
1512
0
                                goto out;
1513
0
                            continue;
1514
0
                        }
1515
0
                        {
1516
0
                            uint flags;
1517
0
                            int plane;
1518
0
                            uint raster1 = 0xbaadf00d; /* Initialize against indeterminizm. */
1519
1520
0
                            cmd_getw(flags, cbp);
1521
0
                            for (plane = 0;
1522
0
                                 plane < image_info->num_planes;
1523
0
                                 ++plane, flags >>= 1) {
1524
0
                                if (flags & 1) {
1525
0
                                    if (cbuf.end - cbp <
1526
0
                                        2 * cmd_max_intsize(sizeof(uint))) {
1527
0
                                        code = top_up_cbuf(&cbuf, &cbp);
1528
0
                                        if (code < 0)
1529
0
                                            goto top_up_failed;
1530
0
                                    }
1531
0
                                    cmd_getw(planes[plane].raster, cbp);
1532
0
                                    if ((raster1 = planes[plane].raster) != 0)
1533
0
                                        cmd_getw(data_x, cbp);
1534
0
                                } else {
1535
0
                                    planes[plane].raster = raster1;
1536
0
                                }
1537
0
                                planes[plane].data_x = data_x;
1538
0
                            }
1539
0
                        }
1540
0
                        goto idata;
1541
2.38M
                    case cmd_opv_image_data:
1542
2.38M
                        cmd_getw(data_height, cbp);
1543
2.38M
                        if (data_height == 0) {
1544
453k
                            if_debug0m('L', mem, " done image\n");
1545
453k
                            code = gx_image_end(image_info, true);
1546
453k
                            if (code < 0)
1547
0
                                goto out;
1548
453k
                            continue;
1549
453k
                        }
1550
1.92M
                        {
1551
1.92M
                            uint bytes_per_plane;
1552
1.92M
                            int plane;
1553
1554
1.92M
                            cmd_getw(bytes_per_plane, cbp);
1555
1.92M
                            if_debug2m('L', mem, " height=%u raster=%u\n",
1556
1.92M
                                       data_height, bytes_per_plane);
1557
1.92M
                            for (plane = 0;
1558
3.85M
                                 plane < image_info->num_planes;
1559
1.93M
                                 ++plane
1560
1.93M
                                 ) {
1561
1.93M
                                planes[plane].data_x = data_x;
1562
1.93M
                                planes[plane].raster = bytes_per_plane;
1563
1.93M
                            }
1564
1.92M
                        }
1565
1.92M
idata:                  data_size = 0;
1566
1.92M
                        {
1567
1.92M
                            int plane;
1568
1569
3.85M
                            for (plane = 0; plane < image_info->num_planes;
1570
1.93M
                                 ++plane)
1571
1.93M
                                data_size += planes[plane].raster;
1572
1.92M
                        }
1573
1.92M
                        data_size *= data_height;
1574
1.92M
                        data_on_heap = 0;
1575
1.92M
                        if (cbuf.end - cbp < data_size) {
1576
456k
                            code = top_up_cbuf(&cbuf, &cbp);
1577
456k
                            if (code < 0)
1578
0
                                goto top_up_failed;
1579
456k
                        }
1580
1.92M
                        if (cbuf.end - cbp >= data_size) {
1581
1.88M
                            planes[0].data = cbp;
1582
1.88M
                            cbp += data_size;
1583
1.88M
                        } else {
1584
44.6k
                            uint cleft = cbuf.end - cbp;
1585
44.6k
                            uint rleft = data_size - cleft;
1586
44.6k
                            byte *rdata;
1587
1588
44.6k
                            if (data_size > cbuf.end - cbuf.data) {
1589
                                /* Allocate a separate buffer. */
1590
44.6k
                                rdata = data_on_heap =
1591
44.6k
                                    gs_alloc_bytes(mem, data_size,
1592
44.6k
                                                   "clist image_data");
1593
44.6k
                                if (rdata == 0) {
1594
0
                                    code = gs_note_error(gs_error_VMerror);
1595
0
                                    goto out;
1596
0
                                }
1597
44.6k
                            } else
1598
0
                                rdata = cbuf.data;
1599
44.6k
                            memmove(rdata, cbp, cleft);
1600
44.6k
                            if (data_on_heap)
1601
44.6k
                                next_is_skip(&cbuf);
1602
44.6k
                            if (sgets(s, rdata + cleft, rleft, &rleft) < 0) {
1603
0
                                code = gs_note_error(gs_error_unregistered); /* Must not happen. */
1604
0
                                goto out;
1605
0
                            }
1606
44.6k
                            planes[0].data = rdata;
1607
44.6k
                            cbp = cbuf.end;     /* force refill */
1608
44.6k
                        }
1609
1.92M
                        {
1610
1.92M
                            int plane;
1611
1.92M
                            const byte *data = planes[0].data;
1612
1613
1.92M
                            for (plane = 0;
1614
3.85M
                                 plane < image_info->num_planes;
1615
1.93M
                                 ++plane
1616
1.93M
                                 ) {
1617
1.93M
                                if (planes[plane].raster == 0)
1618
0
                                    planes[plane].data = 0;
1619
1.93M
                                else {
1620
1.93M
                                    planes[plane].data = data;
1621
1.93M
                                    data += planes[plane].raster *
1622
1.93M
                                        data_height;
1623
1.93M
                                }
1624
1.93M
                            }
1625
1.92M
                        }
1626
#ifdef DEBUG
1627
                        if (gs_debug_c('L')) {
1628
                            int plane;
1629
1630
                            for (plane = 0; plane < image_info->num_planes;
1631
                                 ++plane)
1632
                                if (planes[plane].data != 0)
1633
                                    cmd_print_bits(mem,
1634
                                                   planes[plane].data,
1635
                                                   image_rect.q.x -
1636
                                                   image_rect.p.x,
1637
                                                   data_height,
1638
                                                   planes[plane].raster);
1639
                        }
1640
#endif
1641
1.92M
                        code = gx_image_plane_data(image_info, planes,
1642
1.92M
                                                   data_height);
1643
1.92M
                        if (code < 0)
1644
0
                            gx_image_end(image_info, false);
1645
1.92M
                        if (data_on_heap)
1646
44.6k
                            gs_free_object(mem, data_on_heap,
1647
1.92M
                                           "clist image_data");
1648
1.92M
                        data_x = 0;
1649
1.92M
                        if (code < 0)
1650
0
                            goto out;
1651
1.92M
                        continue;
1652
31.5M
                    case cmd_opv_extend:
1653
31.5M
                        switch (*cbp++) {
1654
0
                            case cmd_opv_ext_put_params:
1655
0
                                if_debug0m('L', mem, "put_params\n");
1656
0
                                cbuf.ptr = cbp;
1657
0
                                code = read_put_params(&cbuf, &gs_gstate,
1658
0
                                                        cdev, mem);
1659
0
                                cbp = cbuf.ptr;
1660
0
                                if (code > 0)
1661
0
                                    break; /* empty list */
1662
0
                                if (code < 0)
1663
0
                                    goto out;
1664
0
                                if (playback_action == playback_action_setup)
1665
0
                                    goto out;
1666
0
                                break;
1667
6.49M
                            case cmd_opv_ext_composite:
1668
6.49M
                                if_debug0m('L', mem, " ext_composite\n");
1669
6.49M
                                cbuf.ptr = cbp;
1670
                                /*
1671
                                 * The screen phase may have been changed during
1672
                                 * the processing of masked images.
1673
                                 */
1674
6.49M
                                gx_gstate_setscreenphase(&gs_gstate,
1675
6.49M
                                            -x0, -y0, gs_color_select_all);
1676
6.49M
                                cbp -= 2; /* Step back to simplify the cycle invariant below. */
1677
228M
                                for (;;) {
1678
                                    /* This internal loop looks ahead for compositor commands and
1679
                                       copies them into a temporary queue. Compositors, which do not paint something,
1680
                                       are marked as idle and later executed with a reduced functionality
1681
                                       for reducing time and memory expense. */
1682
228M
                                    int len;
1683
1684
228M
                                    if (cbp >= cbuf.warn_limit) {
1685
163k
                                        code = top_up_cbuf(&cbuf, &cbp);
1686
163k
                                        if (code < 0)
1687
0
                                            goto out;
1688
163k
                                    }
1689
#ifdef DEBUG
1690
                                    if (gs_debug_c('L')) {
1691
                                       long offset = (long)clist_file_offset(st, cbp - cbuf.data);
1692
1693
                                       dmlprintf1(mem, "[L]  %ld:", offset);
1694
                                       clist_debug_op(mem, cbp);
1695
                                       dmlprintf(mem, "\n");
1696
                                    }
1697
#endif
1698
228M
                                    if (cbp[0] == cmd_opv_extend && cbp[1] == cmd_opv_ext_composite) {
1699
148M
                                        gs_composite_t *pcomp, *pcomp_opening;
1700
148M
                                        gs_compositor_closing_state closing_state;
1701
1702
148M
                                        cbuf.ptr = cbp += 2;
1703
148M
                                        code = read_composite(&cbuf, mem, &pcomp);
1704
148M
                                        if (code < 0)
1705
0
                                            goto out;
1706
148M
                                        cbp = cbuf.ptr;
1707
148M
                                        if (pcomp == NULL)
1708
0
                                            continue;
1709
148M
                                        if (gs_is_pdf14trans_compositor(pcomp) &&
1710
38.3M
                                            playback_action == playback_action_render_no_pdf14) {
1711
                                            /* free the compositor object */
1712
5.54M
                                            gs_free_object(mem, pcomp, "read_composite");
1713
5.54M
                                            pcomp = NULL;
1714
5.54M
                                            continue;
1715
5.54M
                                        }
1716
143M
                                        pcomp_opening = pcomp_last;
1717
143M
                                        closing_state = pcomp->type->procs.is_closing(pcomp, &pcomp_opening, tdev);
1718
143M
                                        switch(closing_state)
1719
143M
                                        {
1720
0
                                        default:
1721
0
                                            code = (int)closing_state;
1722
0
                                            if (code >= 0)
1723
0
                                                code = gs_note_error(gs_error_unregistered); /* Must not happen. */
1724
0
                                            goto out;
1725
131M
                                        case COMP_ENQUEUE:
1726
                                            /* Enqueue. */
1727
131M
                                            enqueue_compositor(&pcomp_first, &pcomp_last, pcomp);
1728
131M
                                            break;
1729
0
                                        case COMP_EXEC_IDLE:
1730
                                            /* Execute idle. */
1731
0
                                            enqueue_compositor(&pcomp_first, &pcomp_last, pcomp);
1732
0
                                            code = execute_compositor_queue(cdev, &target, &tdev,
1733
0
                                                &gs_gstate, &pcomp_first, &pcomp_last, pcomp_opening, x0, y0, mem, true);
1734
0
                                            if (code < 0)
1735
0
                                                goto out;
1736
0
                                            break;
1737
846k
                                        case COMP_EXEC_QUEUE:
1738
                                            /* The opening command was executed. Execute the queue. */
1739
846k
                                            enqueue_compositor(&pcomp_first, &pcomp_last, pcomp);
1740
846k
                                            code = execute_compositor_queue(cdev, &target, &tdev,
1741
846k
                                                &gs_gstate, &pcomp_first, &pcomp_last, pcomp_first, x0, y0, mem, false);
1742
846k
                                            if (code < 0)
1743
3
                                                goto out;
1744
846k
                                            break;
1745
846k
                                        case COMP_REPLACE_PREV:
1746
                                            /* Replace last compositors. */
1747
0
                                            code = execute_compositor_queue(cdev, &target, &tdev,
1748
0
                                                &gs_gstate, &pcomp_first, &pcomp_last, pcomp_opening, x0, y0, mem, true);
1749
0
                                            if (code < 0)
1750
0
                                                goto out;
1751
0
                                            enqueue_compositor(&pcomp_first, &pcomp_last, pcomp);
1752
0
                                            break;
1753
8.89M
                                        case COMP_REPLACE_CURR:
1754
                                            /* Replace specific compositor. */
1755
8.89M
                                            code = dequeue_compositor(&pcomp_first, &pcomp_last, pcomp_opening);
1756
8.89M
                                            if (code < 0)
1757
0
                                                goto out;
1758
8.89M
                                            enqueue_compositor(&pcomp_first, &pcomp_last, pcomp);
1759
8.89M
                                            free_compositor(pcomp_opening, mem);
1760
8.89M
                                            break;
1761
111k
                                        case COMP_DROP_QUEUE:
1762
                                            /* Annihilate the last compositors. */
1763
111k
                                            enqueue_compositor(&pcomp_first, &pcomp_last, pcomp);
1764
111k
                                            code = drop_compositor_queue(&pcomp_first, &pcomp_last, pcomp_opening, mem, x0, y0, &gs_gstate);
1765
111k
                                            if (code < 0)
1766
0
                                                goto out;
1767
111k
                                            break;
1768
1.61M
                                        case COMP_MARK_IDLE:
1769
                                            /* Mark as idle. */
1770
1.61M
                                            enqueue_compositor(&pcomp_first, &pcomp_last, pcomp);
1771
1.61M
                                            mark_as_idle(pcomp_opening, pcomp);
1772
143M
                                        }
1773
143M
                                    } else if (is_null_compositor_op(cbp, &len)) {
1774
71.2M
                                        cbuf.ptr = cbp += len;
1775
71.2M
                                    } else if (cbp[0] == cmd_opv_end_page) {
1776
                                        /* End page, drop the queue. */
1777
1.68M
                                        code = execute_compositor_queue(cdev, &target, &tdev,
1778
1.68M
                                                &gs_gstate, &pcomp_first, &pcomp_last, pcomp_first, x0, y0, mem, true);
1779
1.68M
                                        if (code < 0)
1780
10
                                            goto out;
1781
1.68M
                                        break;
1782
6.22M
                                    } else if (pcomp_last != NULL &&
1783
5.58M
                                            pcomp_last->type->procs.is_friendly(pcomp_last, cbp[0], cbp[1])) {
1784
                                        /* Immediately execute friendly commands
1785
                                           inside the compositor lookahead loop.
1786
                                           Currently there are few friendly commands for the pdf14 compositor only
1787
                                           due to the logic defined in c_pdf14trans_is_friendly.
1788
                                           This code duplicates some code portions from the main loop,
1789
                                           but we have no better idea with no slowdown to the main loop.
1790
                                         */
1791
1.42M
                                        uint cb;
1792
1793
1.42M
                                        switch (*cbp++) {
1794
669k
                                            case cmd_opv_extend:
1795
669k
                                                switch (*cbp++) {
1796
269k
                                                    case cmd_opv_ext_put_halftone:
1797
269k
                                                        {
1798
269k
                                                            uint    ht_size;
1799
1800
269k
                                                            enc_u_getw(ht_size, cbp);
1801
269k
                                                            code = read_alloc_ht_buff(&ht_buff, ht_size, mem);
1802
269k
                                                            if (code < 0)
1803
0
                                                                goto out;
1804
269k
                                                        }
1805
269k
                                                        break;
1806
400k
                                                    case cmd_opv_ext_put_ht_seg:
1807
400k
                                                        cbuf.ptr = cbp;
1808
400k
                                                        code = read_ht_segment(&ht_buff, &cbuf,
1809
400k
                                                                               &gs_gstate, tdev,
1810
400k
                                                                               mem);
1811
400k
                                                        cbp = cbuf.ptr;
1812
400k
                                                        if (code < 0)
1813
0
                                                            goto out;
1814
400k
                                                        break;
1815
400k
                                                    default:
1816
0
                                                        code = gs_note_error(gs_error_unregistered); /* Must not happen. */
1817
0
                                                        goto out;
1818
669k
                                                }
1819
669k
                                                break;
1820
751k
                                            case cmd_opv_set_misc:
1821
751k
                                                cb = *cbp++;
1822
751k
                                                switch (cb >> 6) {
1823
751k
                                                    case cmd_set_misc_map >> 6:
1824
751k
                                                        cbuf.ptr = cbp;
1825
751k
                                                        code = read_set_misc_map(cb, &cbuf, &gs_gstate, mem);
1826
751k
                                                        if (code < 0)
1827
0
                                                            goto out;
1828
751k
                                                        cbp = cbuf.ptr;
1829
751k
                                                        break;
1830
0
                                                    default:
1831
0
                                                        code = gs_note_error(gs_error_unregistered); /* Must not happen. */
1832
0
                                                        goto out;
1833
751k
                                                }
1834
751k
                                                break;
1835
751k
                                            default:
1836
0
                                                code = gs_note_error(gs_error_unregistered); /* Must not happen. */
1837
0
                                                goto out;
1838
1.42M
                                        }
1839
4.80M
                                    } else {
1840
                                        /* A drawing command, execute entire queue. */
1841
4.80M
                                        code = execute_compositor_queue(cdev, &target, &tdev,
1842
4.80M
                                            &gs_gstate, &pcomp_first, &pcomp_last, pcomp_first, x0, y0, mem, false);
1843
4.80M
                                        if (code < 0)
1844
2
                                            goto out;
1845
4.80M
                                        break;
1846
4.80M
                                    }
1847
228M
                                }
1848
6.49M
                                if (pcomp_last != NULL) {
1849
0
                                    code = gs_note_error(gs_error_unregistered);
1850
0
                                    goto out;
1851
0
                                }
1852
6.49M
                                break;
1853
6.49M
                            case cmd_opv_ext_put_halftone:
1854
628k
                                {
1855
628k
                                    uint    ht_size;
1856
1857
628k
                                    if_debug0m('L', mem, " ext_put_halftone\n");
1858
628k
                                    enc_u_getw(ht_size, cbp);
1859
628k
                                    code = read_alloc_ht_buff(&ht_buff, ht_size, mem);
1860
628k
                                    if (code < 0)
1861
0
                                        goto out;
1862
628k
                                }
1863
628k
                                break;
1864
925k
                            case cmd_opv_ext_put_ht_seg:
1865
925k
                                if_debug0m('L', mem, " ext_put_ht_seg\n");
1866
925k
                                cbuf.ptr = cbp;
1867
925k
                                code = read_ht_segment(&ht_buff, &cbuf,
1868
925k
                                                       &gs_gstate, tdev,
1869
925k
                                                       mem);
1870
925k
                                cbp = cbuf.ptr;
1871
925k
                                if (code < 0)
1872
0
                                    goto out;
1873
925k
                                break;
1874
925k
                            case cmd_opv_ext_set_color_is_devn:
1875
0
                                state.color_is_devn = true;
1876
0
                                if_debug0m('L', mem, " ext_set_color_is_devn\n");
1877
0
                                break;
1878
0
                            case cmd_opv_ext_unset_color_is_devn:
1879
0
                                state.color_is_devn = false;
1880
0
                                if_debug0m('L', mem, " ext_unset_color_is_devn\n");
1881
0
                                break;
1882
248
                            case cmd_opv_ext_tile_rect_hl:
1883
                                /* Strip tile with devn colors */
1884
248
                                cbp = cmd_read_rect(op & 0xf0, &state.rect, cbp);
1885
248
                                if_debug4m('L', mem, " x=%d y=%d w=%d h=%d\n",
1886
248
                                           state.rect.x, state.rect.y,
1887
248
                                           state.rect.width,state.rect.height);
1888
248
                                code = (*dev_proc(tdev, strip_tile_rect_devn))
1889
248
                                    (tdev, &state_tile,
1890
248
                                     state.rect.x - x0, state.rect.y - y0,
1891
248
                                     state.rect.width, state.rect.height,
1892
248
                                     &(state.tile_color_devn[0]),
1893
248
                                     &(state.tile_color_devn[1]),
1894
248
                                     tile_phase.x, tile_phase.y);
1895
248
                                break;
1896
22.6M
                            case cmd_opv_ext_put_fill_dcolor:
1897
22.6M
                                pdcolor = &fill_color;
1898
22.6M
                                goto load_dcolor;
1899
871k
                            case cmd_opv_ext_put_stroke_dcolor:
1900
871k
                                pdcolor = &stroke_color;
1901
871k
                                goto load_dcolor;
1902
248
                            case cmd_opv_ext_put_tile_devn_color0:
1903
248
                                pdcolor = &set_dev_colors[0];
1904
248
                                goto load_dcolor;
1905
248
                            case cmd_opv_ext_put_tile_devn_color1:
1906
248
                                pdcolor = &set_dev_colors[1];
1907
23.5M
                    load_dcolor:{
1908
23.5M
                                    uint    color_size;
1909
23.5M
                                    int left, offset, l;
1910
23.5M
                                    const gx_device_color_type_t *  pdct;
1911
23.5M
                                    byte type_and_flag = *cbp++;
1912
23.5M
                                    byte is_continuation = type_and_flag & 0x80;
1913
1914
23.5M
                                    if_debug0m('L', mem, " cmd_opv_ext_put_drawing_color\n");
1915
23.5M
                                    pdct = gx_get_dc_type_from_index(type_and_flag & 0x7F);
1916
23.5M
                                    if (pdct == 0) {
1917
0
                                        code = gs_note_error(gs_error_rangecheck);
1918
0
                                        goto out;
1919
0
                                    }
1920
23.5M
                                    offset = 0;
1921
23.5M
                                    if (is_continuation)
1922
23.5M
                                        enc_u_getw(offset, cbp);
1923
23.5M
                                    enc_u_getw(color_size, cbp);
1924
23.5M
                                    left = color_size;
1925
23.5M
                                    if (!left) {
1926
                                        /* We still need to call pdct->read because it may change dev_color.type -
1927
                                           see gx_dc_null_read.*/
1928
510k
                                        code = pdct->read(pdcolor, &gs_gstate,
1929
510k
                                                          pdcolor, tdev, offset,
1930
510k
                                                          cbp, 0, mem, x0, y0);
1931
510k
                                        if (code < 0)
1932
0
                                            goto out;
1933
510k
                                    }
1934
76.9M
                                    while (left) {
1935
53.3M
                                        if (cbuf.warn_limit - cbp < (int)left) {  /* cbp can be past warn_limit */
1936
31.9M
                                            code = top_up_cbuf(&cbuf, &cbp);
1937
31.9M
                                            if (code < 0)
1938
0
                                                goto top_up_failed;
1939
31.9M
                                        }
1940
53.3M
                                        l = min(left, cbuf.end - cbp);
1941
53.3M
                                        code = pdct->read(pdcolor, &gs_gstate,
1942
53.3M
                                                          pdcolor, tdev, offset,
1943
53.3M
                                                          cbp, l, mem, x0, y0);
1944
53.3M
                                        if (code < 0)
1945
0
                                            goto out;
1946
53.3M
                                        l = code;
1947
53.3M
                                        cbp += l;
1948
53.3M
                                        offset += l;
1949
53.3M
                                        left -= l;
1950
53.3M
                                    }
1951
23.5M
                                    code = gx_color_load(pdcolor, &gs_gstate,
1952
23.5M
                                                         tdev);
1953
23.5M
                                    if (code < 0)
1954
0
                                        goto out;
1955
23.5M
                                }
1956
23.5M
                                break;
1957
23.5M
                            default:
1958
0
                                goto bad_op;
1959
31.5M
                        }
1960
31.5M
                        break;
1961
31.5M
                    default:
1962
0
                        goto bad_op;
1963
59.1M
                }
1964
39.1M
                continue;
1965
53.4M
            case cmd_op_segment >> 4:
1966
53.4M
                {
1967
53.4M
                    int i;
1968
53.4M
                    static const byte op_num_operands[] = {
1969
53.4M
                        cmd_segment_op_num_operands_values
1970
53.4M
                    };
1971
53.4M
                  rgapto:
1972
53.4M
                    if (!in_path) {
1973
8.30M
                        ppos.x = int2fixed(state.rect.x);
1974
8.30M
                        ppos.y = int2fixed(state.rect.y);
1975
8.30M
                        if_debug2m('L', mem, " (%d,%d)", state.rect.x,
1976
8.30M
                                   state.rect.y);
1977
8.30M
                        notes = sn_none;
1978
8.30M
                        in_path = true;
1979
8.30M
                    }
1980
142M
                    for (i = 0; i < op_num_operands[op & 0xf]; ++i) {
1981
89.0M
                        fixed v;
1982
89.0M
                        int b = *cbp;
1983
1984
89.0M
                        switch (b >> 5) {
1985
25.7M
                            case 0:
1986
47.6M
                            case 1:
1987
47.6M
                                vs[i++] =
1988
47.6M
                                    ((fixed) ((b ^ 0x20) - 0x20) << 13) +
1989
47.6M
                                    ((int)cbp[1] << 5) + (cbp[2] >> 3);
1990
47.6M
                                if_debug1m('L', mem, " %g", fixed2float(vs[i - 1]));
1991
47.6M
                                cbp += 2;
1992
47.6M
                                v = (int)((*cbp & 7) ^ 4) - 4;
1993
47.6M
                                break;
1994
13.9M
                            case 2:
1995
26.6M
                            case 3:
1996
26.6M
                                v = (b ^ 0x60) - 0x20;
1997
26.6M
                                break;
1998
2.46M
                            case 4:
1999
4.76M
                            case 5:
2000
                                /*
2001
                                 * Without the following cast, C's
2002
                                 * brain-damaged coercion rules cause the
2003
                                 * result to be considered unsigned, and not
2004
                                 * sign-extended on machines where
2005
                                 * sizeof(long) > sizeof(int).
2006
                                 */
2007
4.76M
                                v = (((b ^ 0xa0) - 0x20) << 8) + (int)*++cbp;
2008
4.76M
                                break;
2009
8.49M
                            case 6:
2010
8.49M
                                v = (b ^ 0xd0) - 0x10;
2011
8.49M
                                vs[i] =
2012
8.49M
          (((ufixed)v << 8) + cbp[1]) << (_fixed_shift - 2);
2013
8.49M
                                if_debug1m('L', mem, " %g", fixed2float(vs[i]));
2014
8.49M
                                cbp += 2;
2015
8.49M
                                continue;
2016
1.54M
                            default /*case 7 */ :
2017
1.54M
                                v = (int)(*++cbp ^ 0x80) - 0x80;
2018
3.09M
                                for (b = 0; b < sizeof(fixed) - 3; ++b)
2019
1.54M
                                    v = (v << 8) + *++cbp;
2020
1.54M
                                break;
2021
89.0M
                        }
2022
80.6M
                        cbp += 3;
2023
                        /* Absent the cast in the next statement, */
2024
                        /* the Borland C++ 4.5 compiler incorrectly */
2025
                        /* sign-extends the result of the shift. */
2026
80.6M
                        vs[i] = ((ufixed)v << 16) + (uint) (cbp[-2] << 8) + cbp[-1];
2027
80.6M
                        if_debug1m('L', mem, " %g", fixed2float(vs[i]));
2028
80.6M
                    }
2029
53.4M
                    if_debug0m('L', mem, "\n");
2030
53.4M
                    code = clist_decode_segment(&path, op, vs, &ppos,
2031
53.4M
                                                x0, y0, notes);
2032
53.4M
                    if (code < 0)
2033
0
                        goto out;
2034
53.4M
                }
2035
53.4M
                continue;
2036
82.4M
            case cmd_op_path >> 4:
2037
82.4M
                if (op == cmd_opv_rgapto)
2038
0
                    goto rgapto;
2039
82.4M
                else if (op == cmd_opv_lock_pattern) {
2040
2.95k
                    gs_id id;
2041
2.95k
                    int lock = *cbp++;
2042
2.95k
                    cmd_get_value(id, cbp);
2043
2.95k
                    if_debug2m('L', mem, "id=0x%lx, lock=%d\n", id, lock);
2044
                    /* We currently lock the pattern in all the bands, even in ones
2045
                     * where we haven't used the pattern. This can cause the following
2046
                     * call to return with 'undefined' because the pattern is not
2047
                     * found. Just swallow this error and continue. */
2048
2.95k
                    code = gx_pattern_cache_entry_set_lock(&gs_gstate, id, lock);
2049
2.95k
                    if (code == gs_error_undefined)
2050
0
                        code = 0;
2051
2.95k
                    if (code < 0)
2052
0
                        goto out;
2053
2.95k
                    continue;
2054
82.4M
                } else {
2055
82.4M
                    gx_path fpath;
2056
82.4M
                    gx_path *ppath = &path;
2057
2058
82.4M
                    if_debug0m('L', mem, "\n");
2059
                    /* if in clip, flatten path first */
2060
82.4M
                    if (in_clip) {
2061
1.17M
                        gx_path_init_local(&fpath, mem);
2062
1.17M
                        code = gx_path_add_flattened_accurate(&path, &fpath,
2063
1.17M
                                             gs_currentflat_inline(&gs_gstate),
2064
1.17M
                                             gs_gstate.accurate_curves);
2065
1.17M
                        if (code < 0)
2066
0
                            goto out;
2067
1.17M
                        ppath = &fpath;
2068
1.17M
                    }
2069
82.4M
                    switch (op) {
2070
5.75M
                        case cmd_opv_fill:
2071
5.75M
                            fill_params.rule = gx_rule_winding_number;
2072
5.75M
                            goto fill;
2073
297k
                        case cmd_opv_eofill:
2074
297k
                            fill_params.rule = gx_rule_even_odd;
2075
6.04M
                        fill:
2076
6.04M
                            fill_params.adjust = gs_gstate.fill_adjust;
2077
6.04M
                            fill_params.flatness = gs_gstate.flatness;
2078
6.04M
                            code = (*dev_proc(tdev, fill_path))(tdev, &gs_gstate, ppath,
2079
6.04M
                                                                &fill_params, &fill_color, pcpath);
2080
6.04M
                            break;
2081
120k
                        case cmd_opv_fill_stroke:
2082
120k
                            fill_params.rule = gx_rule_winding_number;
2083
120k
                            goto fill_stroke;
2084
25.9k
                        case cmd_opv_eofill_stroke:
2085
25.9k
                            fill_params.rule = gx_rule_even_odd;
2086
146k
                        fill_stroke:
2087
146k
                            fill_params.adjust = gs_gstate.fill_adjust;
2088
146k
                            fill_params.flatness = gs_gstate.flatness;
2089
146k
                            stroke_params.flatness = gs_gstate.flatness;
2090
146k
                            stroke_params.traditional = false;
2091
146k
                            code = (*dev_proc(tdev, fill_stroke_path))(tdev, &gs_gstate, ppath,
2092
146k
                                                                &fill_params, &fill_color,
2093
146k
                                                                &stroke_params, &stroke_color, pcpath);
2094
146k
                            break;
2095
6.14M
                        case cmd_opv_stroke:
2096
6.14M
                            stroke_params.flatness = gs_gstate.flatness;
2097
6.14M
                            stroke_params.traditional = false;
2098
6.14M
                            code = (*dev_proc(tdev, stroke_path))
2099
6.14M
                                                       (tdev, &gs_gstate,
2100
6.14M
                                                       ppath, &stroke_params,
2101
6.14M
                                                       &stroke_color, pcpath);
2102
6.14M
                            break;
2103
397k
                        case cmd_opv_polyfill:
2104
397k
                            code = clist_do_polyfill(tdev, ppath, &fill_color,
2105
397k
                                                     gs_gstate.log_op);
2106
397k
                            break;
2107
69.7M
                        case cmd_opv_fill_trapezoid:
2108
69.7M
                            {
2109
69.7M
                                gs_fixed_edge left, right;
2110
69.7M
                                fixed ybot, ytop;
2111
69.7M
                                int options, swap_axes, wh;
2112
69.7M
                                fixed x0f;
2113
69.7M
                                fixed y0f;
2114
69.7M
                                gx_device *ttdev = tdev;
2115
2116
69.7M
                                if (pcpath != NULL && !clipper_dev_open) {
2117
2.27k
                                    gx_make_clip_device_on_stack(&clipper_dev, pcpath, tdev);
2118
2.27k
                                    clipper_dev_open = true;
2119
2.27k
                                }
2120
69.7M
                                if (clipper_dev_open)
2121
45.4M
                                    ttdev = (gx_device *)&clipper_dev;
2122
                                /* Note that if we have transparency present, the clipper device may need to have
2123
                                   its color information updated to be synced up with the target device.
2124
                                   This can occur if we had fills of a path first with a transparency mask to get
2125
                                   an XPS opacity followed by a fill with a transparency group. This occurs in
2126
                                   the XPS gradient code */
2127
69.7M
                                if (tdev->color_info.num_components != ttdev->color_info.num_components){
2128
                                    /* Reset the clipper device color information. Only worry about
2129
                                       the information that is used in the trap code */
2130
0
                                    ttdev->color_info.num_components = tdev->color_info.num_components;
2131
0
                                    ttdev->color_info.depth = tdev->color_info.depth;
2132
0
                                    ttdev->color_info.polarity = tdev->color_info.polarity;
2133
0
                                    memcpy(&(ttdev->color_info.comp_bits),&(tdev->color_info.comp_bits),GX_DEVICE_COLOR_MAX_COMPONENTS);
2134
0
                                    memcpy(&(ttdev->color_info.comp_shift),&(tdev->color_info.comp_shift),GX_DEVICE_COLOR_MAX_COMPONENTS);
2135
0
                                }
2136
69.7M
                                cmd_getw(left.start.x, cbp);
2137
69.7M
                                cmd_getw(left.start.y, cbp);
2138
69.7M
                                cmd_getw(left.end.x, cbp);
2139
69.7M
                                cmd_getw(left.end.y, cbp);
2140
69.7M
                                cmd_getw(right.start.x, cbp);
2141
69.7M
                                cmd_getw(right.start.y, cbp);
2142
69.7M
                                cmd_getw(right.end.x, cbp);
2143
69.7M
                                cmd_getw(right.end.y, cbp);
2144
69.7M
                                cmd_getw(options, cbp);
2145
69.7M
                                if (!(options & 4)) {
2146
69.1M
                                    cmd_getw(ybot, cbp);
2147
69.1M
                                    cmd_getw(ytop, cbp);
2148
69.1M
                                } else
2149
613k
                                    ytop = ybot = 0; /* Unused, but quiet gcc warning. */
2150
69.7M
                                swap_axes = options & 1;
2151
69.7M
                                wh = swap_axes ? tdev->width : tdev->height;
2152
69.7M
                                x0f = int2fixed(swap_axes ? y0 : x0);
2153
69.7M
                                y0f = int2fixed(swap_axes ? x0 : y0);
2154
69.7M
                                left.start.x -= x0f;
2155
69.7M
                                left.start.y -= y0f;
2156
69.7M
                                left.end.x -= x0f;
2157
69.7M
                                left.end.y -= y0f;
2158
69.7M
                                right.start.x -= x0f;
2159
69.7M
                                right.start.y -= y0f;
2160
69.7M
                                right.end.x -= x0f;
2161
69.7M
                                right.end.y -= y0f;
2162
69.7M
                                if (options & 2) {
2163
932k
                                    uchar num_components = tdev->color_info.num_components;
2164
932k
                                    frac31 c[4][GX_DEVICE_COLOR_MAX_COMPONENTS], *cc[4];
2165
932k
                                    byte colors_mask, i, j, m = 1;
2166
932k
                                    gs_fill_attributes fa;
2167
932k
                                    gs_fixed_rect clip;
2168
932k
                                    fixed hh = int2fixed(swap_axes ? target->width : target->height);
2169
2170
932k
                                    if (cbuf.end - cbp < 5 * cmd_max_intsize(sizeof(frac31))) {
2171
1.12k
                                        code = top_up_cbuf(&cbuf, &cbp);
2172
1.12k
                                        if (code < 0)
2173
0
                                            goto top_up_failed;
2174
1.12k
                                    }
2175
932k
                                    cmd_getw(clip.p.x, cbp);
2176
932k
                                    cmd_getw(clip.p.y, cbp);
2177
932k
                                    cmd_getw(clip.q.x, cbp);
2178
932k
                                    cmd_getw(clip.q.y, cbp);
2179
932k
                                    clip.p.x -= x0f;
2180
932k
                                    clip.p.y -= y0f;
2181
932k
                                    clip.q.x -= x0f;
2182
932k
                                    clip.q.y -= y0f;
2183
932k
                                    if (clip.p.y < 0)
2184
285k
                                        clip.p.y = 0;
2185
932k
                                    if (clip.q.y > hh)
2186
523k
                                        clip.q.y = hh;
2187
932k
                                    fa.clip = &clip;
2188
932k
                                    fa.swap_axes = swap_axes;
2189
932k
                                    fa.ht = NULL;
2190
932k
                                    fa.lop = lop_default; /* fgixme: gs_gstate.log_op; */
2191
932k
                                    fa.ystart = ybot - y0f;
2192
932k
                                    fa.yend = ytop - y0f;
2193
932k
                                    cmd_getw(colors_mask, cbp);
2194
4.66M
                                    for (i = 0; i < 4; i++, m <<= 1) {
2195
3.72M
                                        if (colors_mask & m) {
2196
2.47M
                                            if (cbuf.end - cbp < num_components * cmd_max_intsize(sizeof(frac31))) {
2197
1.65k
                                                code = top_up_cbuf(&cbuf, &cbp);
2198
1.65k
                                                if (code < 0)
2199
0
                                                    goto top_up_failed;
2200
1.65k
                                            }
2201
2.47M
                                            cc[i] = c[i];
2202
5.84M
                                            for (j = 0; j < num_components; j++)
2203
3.36M
                                                cmd_getfrac(c[i][j], cbp);
2204
2.47M
                                        } else
2205
1.25M
                                            cc[i] = NULL;
2206
3.72M
                                    }
2207
932k
                                    if (options & 4) {
2208
613k
#                                       if 1 /* Disable to debug gx_fill_triangle_small. */
2209
613k
                                        code = dev_proc(ttdev, fill_linear_color_triangle)(ttdev, &fa,
2210
613k
                                                        &left.start, &left.end, &right.start,
2211
613k
                                                        cc[0], cc[1], cc[2]);
2212
#                                       else
2213
                                        code = 0;
2214
#                                       endif
2215
613k
                                        if (code == 0) {
2216
                                            /* The target device didn't fill the trapezoid and
2217
                                               requests a decomposition. Subdivide into smaller triangles : */
2218
0
                                            if (pfs.dev == NULL)
2219
0
                                                code = gx_init_patch_fill_state_for_clist(tdev, &pfs, mem);
2220
0
                                            if (code >= 0) {
2221
0
                                                pfs.dev = ttdev;
2222
0
                                                pfs.rect = clip; /* fixme: eliminate 'clip'. */
2223
0
                                                fa.pfs = &pfs;
2224
0
                                                code = gx_fill_triangle_small(ttdev, &fa,
2225
0
                                                            &left.start, &left.end, &right.start,
2226
0
                                                            cc[0], cc[1], cc[2]);
2227
0
                                            }
2228
0
                                        }
2229
613k
                                    } else {
2230
318k
                                        code = dev_proc(ttdev, fill_linear_color_trapezoid)(ttdev, &fa,
2231
318k
                                                        &left.start, &left.end, &right.start, &right.end,
2232
318k
                                                        cc[0], cc[1], cc[2], cc[3]);
2233
318k
                                        if (code == 0) {
2234
                                            /* Fixme : The target device didn't fill the trapezoid and
2235
                                               requests a decomposition.
2236
                                               Currently we never call it with 4 colors (see gxshade6.c)
2237
                                               and 2 colors must not return 0 - see comment to
2238
                                               dev_t_proc_fill_linear_color_trapezoid in gxdevcli.c .
2239
                                               Must not happen. */
2240
0
                                            code = gs_note_error(gs_error_unregistered);
2241
0
                                        }
2242
318k
                                    }
2243
932k
                                } else
2244
68.8M
                                    code = gx_default_fill_trapezoid(ttdev, &left, &right,
2245
68.8M
                                        max(ybot - y0f, fixed_half),
2246
68.8M
                                        min(ytop - y0f, int2fixed(wh)), swap_axes,
2247
68.8M
                                        &fill_color, gs_gstate.log_op);
2248
69.7M
                            }
2249
69.7M
                           break;
2250
69.7M
                        default:
2251
0
                            goto bad_op;
2252
82.4M
                    }
2253
82.4M
                    if (ppath != &path)
2254
1.17M
                        gx_path_free(ppath, "clist_render_band");
2255
82.4M
                }
2256
82.4M
                if (in_path) {  /* path might be empty! */
2257
8.30M
                    state.rect.x = fixed2int_var(ppos.x);
2258
8.30M
                    state.rect.y = fixed2int_var(ppos.y);
2259
8.30M
                    in_path = false;
2260
8.30M
                }
2261
82.4M
                gx_path_free(&path, "clist_render_band");
2262
82.4M
                gx_path_init_local(&path, mem);
2263
82.4M
                if (code < 0)
2264
2
                    goto out;
2265
82.4M
                continue;
2266
82.4M
            default:
2267
0
              bad_op:mlprintf5(mem, "Bad op %02x band y0 = %d file pos %"PRId64" buf pos %d/%d\n",
2268
0
                 op, y0, stell(s), (int)(cbp - cbuf.data), (int)(cbuf.end - cbuf.data));
2269
0
                {
2270
0
                    const byte *pp;
2271
2272
0
                    for (pp = cbuf.data; pp < cbuf.end; pp += 10) {
2273
0
                        dmlprintf1(mem, "%4d:", (int)(pp - cbuf.data));
2274
0
                        dmprintf10(mem, " %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2275
0
                                  pp[0], pp[1], pp[2], pp[3], pp[4],
2276
0
                                  pp[5], pp[6], pp[7], pp[8], pp[9]);
2277
0
                    }
2278
0
                }
2279
0
                code = gs_note_error(gs_error_Fatal);
2280
0
                goto out;
2281
347M
        }
2282
347M
        if_debug4m('L', mem, " x=%d y=%d w=%d h=%d\n",
2283
80.0M
                  state.rect.x, state.rect.y, state.rect.width,
2284
80.0M
                  state.rect.height);
2285
80.0M
        switch (op >> 4) {
2286
4.15M
            case cmd_op_fill_rect >> 4:
2287
4.15M
                if (state.rect.width == 0 && state.rect.height == 0 &&
2288
2.11M
                    state.rect.x == 0 && state.rect.y == 0) {
2289
                    /* FIXME: This test should be unnecessary. Bug 692076
2290
                     * is open pending a proper fix. */
2291
2.11M
                    code = (dev_proc(tdev, fillpage) == NULL ? 0 :
2292
2.11M
                            (*dev_proc(tdev, fillpage))(tdev, &gs_gstate,
2293
2.11M
                                                        &fill_color));
2294
2.11M
                    break;
2295
2.11M
                }
2296
6.69M
            case cmd_op_fill_rect_short >> 4:
2297
48.3M
            case cmd_op_fill_rect_tiny >> 4:
2298
48.3M
                if (!state.lop_enabled) {
2299
48.3M
                    code = (*dev_proc(tdev, fill_rectangle))
2300
48.3M
                        (tdev, state.rect.x - x0, state.rect.y - y0,
2301
48.3M
                         state.rect.width, state.rect.height,
2302
48.3M
                         state.colors[1]);
2303
48.3M
                    break;
2304
48.3M
                }
2305
0
                source = NULL;
2306
0
                data_x = 0;
2307
0
                raster = 0;
2308
0
                colors[0] = colors[1] = state.colors[1];
2309
0
                log_op = state.lop;
2310
0
                pcolor = colors;
2311
0
         do_rop:code = (*dev_proc(tdev, strip_copy_rop2))
2312
0
                                (tdev, source, data_x, raster, gx_no_bitmap_id,
2313
0
                                 pcolor, &state_tile,
2314
0
                                 (state.tile_colors[0] == gx_no_color_index &&
2315
0
                                  state.tile_colors[1] == gx_no_color_index ?
2316
0
                                  NULL : state.tile_colors),
2317
0
                                 state.rect.x - x0, state.rect.y - y0,
2318
0
                                 state.rect.width - data_x, state.rect.height,
2319
0
                                 tile_phase.x, tile_phase.y, log_op,
2320
0
                                 plane_height);
2321
0
                     plane_height = 0;
2322
0
                data_x = 0;
2323
0
                break;
2324
143k
            case cmd_op_tile_rect >> 4:
2325
143k
                if (state.rect.width == 0 && state.rect.height == 0 &&
2326
0
                    state.rect.x == 0 && state.rect.y == 0) {
2327
0
                    code = (*dev_proc(tdev, fillpage))(tdev, &gs_gstate, &fill_color);
2328
0
                    break;
2329
0
                }
2330
1.30M
            case cmd_op_tile_rect_short >> 4:
2331
2.85M
            case cmd_op_tile_rect_tiny >> 4:
2332
                /* Currently we don't use lop with strip_tile_rectangle. */
2333
2.85M
                code = (*dev_proc(tdev, strip_tile_rectangle))
2334
2.85M
                    (tdev, &state_tile,
2335
2.85M
                     state.rect.x - x0, state.rect.y - y0,
2336
2.85M
                     state.rect.width, state.rect.height,
2337
2.85M
                     state.tile_colors[0], state.tile_colors[1],
2338
2.85M
                     tile_phase.x, tile_phase.y);
2339
2.85M
                break;
2340
24.1M
            case cmd_op_copy_mono_planes >> 4:
2341
24.1M
                if (state.lop_enabled) {
2342
0
                    pcolor = state.colors;
2343
0
                    log_op = state.lop;
2344
0
                    goto do_rop;
2345
0
                }
2346
24.1M
                if ((op & cmd_copy_use_tile) || pcpath != NULL) {       /*
2347
                                                                         * This call of copy_mono originated as a call
2348
                                                                         * of fill_mask.
2349
                                                                         */
2350
23.4M
                    code = gx_image_fill_masked
2351
23.4M
                        (tdev, source, data_x, raster, gx_no_bitmap_id,
2352
23.4M
                         state.rect.x - x0, state.rect.y - y0,
2353
23.4M
                         state.rect.width - data_x, state.rect.height,
2354
23.4M
                         &fill_color, 1, gs_gstate.log_op, pcpath);
2355
23.4M
                } else {
2356
694k
                    if (plane_height == 0) {
2357
687k
                        code = (*dev_proc(tdev, copy_mono))
2358
687k
                             (tdev, source, data_x, raster, gx_no_bitmap_id,
2359
687k
                              state.rect.x - x0, state.rect.y - y0,
2360
687k
                              state.rect.width - data_x, state.rect.height,
2361
687k
                              state.colors[0], state.colors[1]);
2362
687k
                    } else {
2363
6.91k
                        code = (*dev_proc(tdev, copy_planes))
2364
6.91k
                             (tdev, source, data_x, raster, gx_no_bitmap_id,
2365
6.91k
                              state.rect.x - x0, state.rect.y - y0,
2366
6.91k
                              state.rect.width - data_x, state.rect.height,
2367
6.91k
                              plane_height);
2368
6.91k
                    }
2369
694k
                }
2370
24.1M
                plane_height = 0;
2371
24.1M
                data_x = 0;
2372
24.1M
                break;
2373
2.56M
            case cmd_op_copy_color_alpha >> 4:
2374
2.56M
                if (state.color_is_alpha) {
2375
/****** CAN'T DO ROP WITH ALPHA ******/
2376
0
                    if (state.color_is_devn &&
2377
0
                        dev_proc(tdev, copy_alpha_hl_color) != gx_default_no_copy_alpha_hl_color) { /* FIXME */
2378
0
                        code = (*dev_proc(tdev, copy_alpha_hl_color))
2379
0
                            (tdev, source, data_x, raster, gx_no_bitmap_id,
2380
0
                             state.rect.x - x0, state.rect.y - y0,
2381
0
                             state.rect.width - data_x, state.rect.height,
2382
0
                             &fill_color, depth);
2383
0
                    } else {
2384
0
                        code = (*dev_proc(tdev, copy_alpha))
2385
0
                            (tdev, source, data_x, raster, gx_no_bitmap_id,
2386
0
                             state.rect.x - x0, state.rect.y - y0,
2387
0
                             state.rect.width - data_x, state.rect.height,
2388
0
                             state.colors[1], depth);
2389
0
                    }
2390
2.56M
                } else {
2391
2.56M
                    if (state.lop_enabled) {
2392
0
                        pcolor = NULL;
2393
0
                        log_op = state.lop;
2394
0
                        goto do_rop;
2395
0
                    }
2396
2.56M
                    code = (*dev_proc(tdev, copy_color))
2397
2.56M
                        (tdev, source, data_x, raster, gx_no_bitmap_id,
2398
2.56M
                         state.rect.x - x0, state.rect.y - y0,
2399
2.56M
                         state.rect.width - data_x, state.rect.height);
2400
2.56M
                }
2401
2.56M
                data_x = 0;
2402
2.56M
                break;
2403
0
            default:            /* can't happen */
2404
0
                goto bad_op;
2405
80.0M
        }
2406
80.0M
    }
2407
    /* Clean up before we exit. */
2408
2.14M
  out:
2409
2410
2.14M
    if(gs_gstate.icc_manager != NULL) {
2411
2.14M
        rc_decrement(gs_gstate.icc_manager->srcgtag_profile, "clist_playback_band");
2412
2.14M
        gs_gstate.icc_manager->srcgtag_profile = NULL;
2413
2.14M
    }
2414
2.14M
    if (ht_buff.pbuff != 0) {
2415
0
        gs_free_object(mem, ht_buff.pbuff, "clist_playback_band(ht_buff)");
2416
0
        ht_buff.pbuff = 0;
2417
0
        ht_buff.pcurr = 0;
2418
0
    }
2419
2.14M
    ht_buff.ht_size = 0;
2420
2.14M
    ht_buff.read_size = 0;
2421
2422
2.14M
    if (pcomp_last != NULL) {
2423
0
        int code1 = drop_compositor_queue(&pcomp_first, &pcomp_last, NULL, mem, x0, y0, &gs_gstate);
2424
2425
0
        if (code == 0)
2426
0
            code = code1;
2427
0
    }
2428
2.14M
    gx_cpath_free(&clip_path, "clist_render_band exit");
2429
2.14M
    gx_path_free(&path, "clist_render_band exit");
2430
2.14M
    if (gs_gstate.pattern_cache != NULL) {
2431
63.1k
        gx_pattern_cache_free(gs_gstate.pattern_cache);
2432
63.1k
        gs_gstate.pattern_cache = NULL;
2433
63.1k
    }
2434
    /* Free the client color and device colors allocated upon entry */
2435
2.14M
    gs_free_object(mem, gs_gstate.color[0].ccolor, "clist_playback_band");
2436
2.14M
    gs_free_object(mem, gs_gstate.color[1].ccolor, "clist_playback_band");
2437
2.14M
    gs_free_object(mem, gs_gstate.color[0].dev_color, "clist_playback_band");
2438
2.14M
    gs_free_object(mem, gs_gstate.color[1].dev_color, "clist_playback_band");
2439
2.14M
    gs_gstate.color[0].ccolor = gs_gstate.color[1].ccolor = NULL;
2440
2.14M
    gs_gstate.color[0].dev_color = gs_gstate.color[1].dev_color = NULL;
2441
2442
    /* The imager state release will decrement the icc link cache.  To avoid
2443
       race conditions lock the cache */
2444
2.14M
    gx_monitor_enter(cdev->icc_cache_cl->lock);
2445
2.14M
    gs_gstate_release(&gs_gstate);
2446
2.14M
    gx_monitor_leave(cdev->icc_cache_cl->lock); /* done with increment, let everyone run */
2447
2.14M
    gs_free_object(mem, data_bits, "clist_playback_band(data_bits)");
2448
2.14M
    if (target != orig_target) {
2449
1.09M
        if (target->rc.ref_count != 1) {
2450
            /* This can occur if we are coming from a pattern clist that
2451
               includes transparency.  In this case, we do not want to
2452
               free the compositor since it is really the main target that
2453
               we are tiling into.  i.e. the tile itself does not have
2454
               a pdf14 device, but rather we push a trans group, draw and
2455
               then pop the group to properly blend */
2456
0
            rc_decrement(target, "gxclrast(target compositor)");
2457
1.09M
        } else {
2458
            /* Ref count was 1. close the device and then free it */
2459
1.09M
            if (target->is_open)
2460
310
                dev_proc(target, close_device)(target);
2461
1.09M
            gs_free_object(target->memory, target, "gxclrast discard compositor");
2462
1.09M
        }
2463
1.09M
        target = orig_target;
2464
1.09M
    }
2465
2.14M
    if (code < 0) {
2466
32
        if (pfs.dev != NULL)
2467
0
            term_patch_fill_state(&pfs);
2468
32
        rc_decrement(gs_gstate.color[0].color_space, "clist_playback_band");
2469
32
        rc_decrement(gs_gstate.color[1].color_space, "clist_playback_band");
2470
32
        gs_free_object(mem, cbuf_storage, "clist_playback_band(cbuf_storage)");
2471
32
        gx_cpath_free(&clip_path, "clist_playback_band");
2472
32
        if (pcpath != &clip_path)
2473
20
            gx_cpath_free(pcpath, "clist_playback_band");
2474
32
        if (clipper_dev_open)
2475
0
            gx_destroy_clip_device_on_stack(&clipper_dev);
2476
32
        return_error(code);
2477
32
    }
2478
    /* Check whether we have more pages to process. */
2479
2.14M
    if ((playback_action != playback_action_setup &&
2480
2.14M
        (cbp < cbuf.end || !seofp(s)) && (op != cmd_opv_end_page) )
2481
2.14M
        )
2482
0
        goto in;
2483
2.14M
    if (pfs.dev != NULL)
2484
0
        term_patch_fill_state(&pfs);
2485
2.14M
    rc_decrement(gs_gstate.color[0].color_space, "clist_playback_band");
2486
2.14M
    rc_decrement(gs_gstate.color[1].color_space, "clist_playback_band");
2487
2.14M
    gs_free_object(mem, cbuf_storage, "clist_playback_band(cbuf_storage)");
2488
2.14M
    gx_cpath_free(&clip_path, "clist_playback_band");
2489
2.14M
    if (pcpath != &clip_path)
2490
1.81M
        gx_cpath_free(pcpath, "clist_playback_band");
2491
2.14M
    if (clipper_dev_open)
2492
1.18k
        gx_destroy_clip_device_on_stack(&clipper_dev);
2493
2.14M
    return code;
2494
0
top_up_failed:
2495
0
    gx_cpath_free(&clip_path, "clist_playback_band");
2496
0
    if (pcpath != &clip_path)
2497
0
        gx_cpath_free(pcpath, "clist_playback_band");
2498
0
    if (clipper_dev_open)
2499
0
        gx_destroy_clip_device_on_stack(&clipper_dev);
2500
0
    return code;
2501
2.14M
}
2502
2503
/* ---------------- Individual commands ---------------- */
2504
2505
/*
2506
 * These single-use procedures implement a few large individual commands,
2507
 * primarily for readability but also to avoid overflowing compilers'
2508
 * optimization limits.  They all take the command buffer as their first
2509
 * parameter (pcb), assume that the current buffer pointer is in pcb->ptr,
2510
 * and update it there.
2511
 */
2512
2513
static int
2514
read_set_tile_size(command_buf_t *pcb, tile_slot *bits, bool for_pattern)
2515
3.38k
{
2516
3.38k
    const byte *cbp = pcb->ptr;
2517
3.38k
    uint rep_width, rep_height;
2518
3.38k
    uint pdepth;
2519
3.38k
    byte bd = *cbp++;
2520
2521
3.38k
    bits->head.depth = cmd_code_to_depth(bd);
2522
3.38k
    if (for_pattern)
2523
3.38k
        cmd_getw(bits->id, cbp);
2524
3.38k
    cmd_getw(rep_width, cbp);
2525
3.38k
    cmd_getw(rep_height, cbp);
2526
3.38k
    if (bd & 0x20) {
2527
3.34k
        cmd_getw(bits->x_reps, cbp);
2528
3.34k
        bits->width = rep_width * bits->x_reps;
2529
3.34k
    } else {
2530
38
        bits->x_reps = 1;
2531
38
        bits->width = rep_width;
2532
38
    }
2533
3.38k
    if (bd & 0x40) {
2534
1.00k
        cmd_getw(bits->y_reps, cbp);
2535
1.00k
        bits->height = rep_height * bits->y_reps;
2536
2.37k
    } else {
2537
2.37k
        bits->y_reps = 1;
2538
2.37k
        bits->height = rep_height;
2539
2.37k
    }
2540
3.38k
    if (bd & 0x80)
2541
3.38k
        cmd_getw(bits->rep_shift, cbp);
2542
3.38k
    else
2543
3.38k
        bits->rep_shift = 0;
2544
3.38k
    if (bd & 0x10)
2545
0
        bits->num_planes = *cbp++;
2546
3.38k
    else
2547
3.38k
        bits->num_planes = 1;
2548
3.38k
    if_debug7('L', " depth=%d size=(%d,%d), rep_size=(%d,%d), rep_shift=%d, num_planes=%d\n",
2549
3.38k
              bits->head.depth, bits->width,
2550
3.38k
              bits->height, rep_width,
2551
3.38k
              rep_height, bits->rep_shift, bits->num_planes);
2552
3.38k
    bits->shift =
2553
3.38k
        (bits->rep_shift == 0 ? 0 :
2554
3.38k
         (bits->rep_shift * (bits->height / rep_height)) % rep_width);
2555
3.38k
    pdepth = bits->head.depth;
2556
3.38k
    if (bits->num_planes != 1)
2557
0
        pdepth /= bits->num_planes;
2558
3.38k
    bits->raster = bitmap_raster(bits->width * pdepth);
2559
3.38k
    pcb->ptr = cbp;
2560
3.38k
    return 0;
2561
3.38k
}
2562
2563
static int
2564
read_set_bits(command_buf_t *pcb, tile_slot *bits, int compress,
2565
              gx_clist_state *pcls, gx_strip_bitmap *tile, tile_slot **pslot,
2566
              gx_device_clist_reader *cdev, gs_memory_t *mem)
2567
8.14M
{
2568
8.14M
    const byte *cbp = pcb->ptr;
2569
8.14M
    uint rep_width = bits->width / bits->x_reps;
2570
8.14M
    uint rep_height = bits->height / bits->y_reps;
2571
8.14M
    uint index;
2572
8.14M
    ulong offset;
2573
8.14M
    uint width_bits;
2574
8.14M
    uint width_bytes;
2575
8.14M
    uint raster;
2576
8.14M
    uint bytes;
2577
8.14M
    byte *data;
2578
8.14M
    tile_slot *slot;
2579
8.14M
    uint depth = bits->head.depth;
2580
2581
8.14M
    if (bits->num_planes != 1)
2582
0
        depth /= bits->num_planes;
2583
8.14M
    width_bits = rep_width * depth;
2584
2585
8.14M
    bytes = clist_bitmap_bytes(width_bits, rep_height * bits->num_planes,
2586
8.14M
                               compress |
2587
8.14M
                               (rep_width < bits->width ?
2588
8.13M
                                decompress_spread : 0) |
2589
8.14M
                               decompress_elsewhere,
2590
8.14M
                               &width_bytes,
2591
8.14M
                               (uint *)&raster);
2592
2593
8.14M
    cmd_getw(index, cbp);
2594
8.14M
    cmd_getw(offset, cbp);
2595
8.14M
    if_debug2m('L', mem, " index=%d offset=%lu\n", index, offset);
2596
8.14M
    pcls->tile_index = index;
2597
8.14M
    cdev->tile_table[pcls->tile_index].offset = offset;
2598
8.14M
    slot = (tile_slot *)(cdev->cache_chunk->data + offset);
2599
8.14M
    *pslot = slot;
2600
8.14M
    *slot = *bits;
2601
8.14M
    tile->data = data = (byte *)(slot + 1);
2602
#ifdef DEBUG
2603
    slot->index = pcls->tile_index;
2604
#endif
2605
8.14M
    if (compress == cmd_compress_const) {
2606
0
        cbp = cmd_read_data(pcb, data, 1, cbp);
2607
0
        if (width_bytes > 0 && rep_height > 0)
2608
0
            memset(data+1, *data, width_bytes * rep_height - 1);
2609
8.14M
    } else if (compress) {
2610
        /*
2611
         * Decompress the image data.  We'd like to share this code with the
2612
         * similar code in copy_*, but right now we don't see how.
2613
         */
2614
1.44M
        stream_cursor_read r;
2615
1.44M
        stream_cursor_write w;
2616
        /*
2617
         * We don't know the data length a priori, so to be conservative, we
2618
         * read the uncompressed size.
2619
         */
2620
1.44M
        uint cleft = pcb->end - cbp;
2621
2622
1.44M
        if (cleft < bytes && !pcb->end_status) {
2623
13.2k
            uint nread = cbuf_size - cleft;
2624
2625
13.2k
            advance_buffer(pcb, cbp);
2626
13.2k
            pcb->end_status = sgets(pcb->s, pcb->data + cleft, nread, &nread);
2627
13.2k
            set_cb_end(pcb, pcb->data + cleft + nread);
2628
13.2k
            cbp = pcb->data;
2629
13.2k
        }
2630
1.44M
        r.ptr = cbp - 1;
2631
1.44M
        r.limit = pcb->end - 1;
2632
1.44M
        w.ptr = data - 1;
2633
1.44M
        w.limit = w.ptr + bytes;
2634
1.44M
        switch (compress) {
2635
0
        case cmd_compress_rle:
2636
0
            {
2637
0
                stream_RLD_state sstate;
2638
2639
0
                clist_rld_init(&sstate);
2640
0
                (*s_RLD_template.process)
2641
0
                    ((stream_state *)&sstate, &r, &w, true);
2642
0
            }
2643
0
            break;
2644
1.44M
        case cmd_compress_cfe:
2645
1.44M
            {
2646
1.44M
                stream_CFD_state sstate;
2647
2648
1.44M
                clist_cfd_init(&sstate,
2649
1.44M
                               width_bytes << 3 /*width_bits */ ,
2650
1.44M
                               rep_height, mem);
2651
1.44M
                (*s_CFD_template.process)
2652
1.44M
                    ((stream_state *)&sstate, &r, &w, true);
2653
1.44M
                (*s_CFD_template.release)
2654
1.44M
                    ((stream_state *)&sstate);
2655
1.44M
            }
2656
1.44M
            break;
2657
0
        default:
2658
0
            return_error(gs_error_unregistered);
2659
1.44M
        }
2660
1.44M
        cbp = r.ptr + 1;
2661
6.70M
    } else if (rep_height * bits->num_planes > 1 && width_bytes != bits->raster) {
2662
6.65M
        cbp = cmd_read_short_bits(pcb, data, bytes,
2663
6.65M
                                  width_bytes, rep_height * bits->num_planes,
2664
6.65M
                                  bits->raster, cbp);
2665
6.65M
    } else {
2666
47.0k
        cbp = cmd_read_data(pcb, data, bytes, cbp);
2667
47.0k
    }
2668
8.14M
    if (bits->width > rep_width)
2669
13.1k
        bits_replicate_horizontally(data,
2670
13.1k
                                    rep_width * depth, rep_height * bits->num_planes,
2671
13.1k
                                    bits->raster,
2672
13.1k
                                    bits->width * depth,
2673
13.1k
                                    bits->raster);
2674
8.14M
    if (bits->height > rep_height)
2675
2.04k
        bits_replicate_vertically(data,
2676
2.04k
                                  rep_height, bits->raster,
2677
2.04k
                                  bits->height);
2678
#ifdef DEBUG
2679
    if (gs_debug_c('L'))
2680
        cmd_print_bits(mem, data, bits->width, bits->height, bits->raster);
2681
#endif
2682
8.14M
    pcb->ptr = cbp;
2683
8.14M
    return 0;
2684
8.14M
}
2685
2686
/* if necessary, allocate a buffer to hold a serialized halftone */
2687
static int
2688
read_alloc_ht_buff(ht_buff_t * pht_buff, uint ht_size, gs_memory_t * mem)
2689
897k
{
2690
    /* free the existing buffer, if any (usually none) */
2691
897k
    if (pht_buff->pbuff != 0) {
2692
0
        gs_free_object(mem, pht_buff->pbuff, "read_alloc_ht_buff");
2693
0
        pht_buff->pbuff = 0;
2694
0
    }
2695
2696
    /*
2697
     * If the serialized halftone fits in the command buffer, no
2698
     * additional buffer is required.
2699
     */
2700
897k
    if (ht_size > cbuf_ht_seg_max_size) {
2701
423k
        pht_buff->pbuff = gs_alloc_bytes(mem, ht_size, "read_alloc_ht_buff");
2702
423k
        if (pht_buff->pbuff == 0)
2703
0
            return_error(gs_error_VMerror);
2704
423k
    }
2705
897k
    pht_buff->ht_size = ht_size;
2706
897k
    pht_buff->read_size = 0;
2707
897k
    pht_buff->pcurr = pht_buff->pbuff;
2708
897k
    return 0;
2709
897k
}
2710
2711
/* read a halftone segment; if it is the final segment, build the halftone */
2712
static int
2713
read_ht_segment(
2714
    ht_buff_t *                 pht_buff,
2715
    command_buf_t *             pcb,
2716
    gs_gstate *           pgs,
2717
    gx_device *                 dev,
2718
    gs_memory_t *               mem )
2719
1.32M
{
2720
1.32M
    const byte *                cbp = pcb->ptr;
2721
1.32M
    const byte *                pbuff = 0;
2722
1.32M
    uint                        ht_size = pht_buff->ht_size, seg_size;
2723
1.32M
    int                         code = 0;
2724
2725
    /* get the segment size; refill command buffer if necessary */
2726
1.32M
    enc_u_getw(seg_size, cbp);
2727
1.32M
    if (pcb->warn_limit - cbp < (int)seg_size) { /* cbp can be past warn_limit */
2728
432k
        code = top_up_cbuf(pcb, &cbp);
2729
432k
        if (code < 0)
2730
0
            return code;
2731
432k
        if (pcb->end - cbp < (int)seg_size) {
2732
0
            emprintf(mem, " *** ht segment size doesn't fit in buffer ***\n");
2733
0
            return_error(gs_error_unknownerror);
2734
0
        }
2735
432k
    }
2736
2737
1.32M
    if (pht_buff->pbuff == 0) {
2738
        /* if not separate buffer, must be only one segment */
2739
474k
        if (seg_size != ht_size)
2740
0
            return_error(gs_error_unknownerror);
2741
474k
        pbuff = cbp;
2742
850k
    } else {
2743
850k
        if (seg_size + pht_buff->read_size > pht_buff->ht_size)
2744
0
            return_error(gs_error_unknownerror);
2745
850k
        memcpy(pht_buff->pcurr, cbp, seg_size);
2746
850k
        pht_buff->pcurr += seg_size;
2747
850k
        if ((pht_buff->read_size += seg_size) == ht_size)
2748
423k
            pbuff = pht_buff->pbuff;
2749
850k
    }
2750
2751
    /* if everything has been read, convert back to a halftone */
2752
1.32M
    if (pbuff != 0) {
2753
897k
        code = gx_ht_read_and_install(pgs, dev, pbuff, ht_size, mem);
2754
2755
        /* release any buffered information */
2756
897k
        if (pht_buff->pbuff != 0) {
2757
423k
            gs_free_object(mem, pht_buff->pbuff, "read_alloc_ht_buff");
2758
423k
            pht_buff->pbuff = 0;
2759
423k
            pht_buff->pcurr = 0;
2760
423k
        }
2761
897k
        pht_buff->ht_size = 0;
2762
897k
        pht_buff->read_size = 0;
2763
897k
    }
2764
2765
    /* update the command buffer ponter */
2766
1.32M
    pcb->ptr = cbp + seg_size;
2767
2768
1.32M
    return code;
2769
1.32M
}
2770
2771
static int
2772
read_set_misc2(command_buf_t *pcb, gs_gstate *pgs, segment_notes *pnotes)
2773
1.54M
{
2774
1.54M
    const byte *cbp = pcb->ptr;
2775
1.54M
    uint mask, cb;
2776
2777
1.54M
    if_debug0m('L', pgs->memory, "\n");
2778
1.54M
    cmd_getw(mask, cbp);
2779
1.54M
    if (mask & cap_join_known) {
2780
232k
        cb = *cbp++;
2781
232k
        pgs->line_params.start_cap = (gs_line_cap)((cb >> 3) & 7);
2782
232k
        pgs->line_params.join = (gs_line_join)(cb & 7);
2783
232k
        if_debug2m('L', pgs->memory, "[L]      start_cap=%d join=%d\n",
2784
232k
                   pgs->line_params.start_cap, pgs->line_params.join);
2785
232k
        cb = *cbp++;
2786
232k
        pgs->line_params.end_cap = (gs_line_cap)((cb >> 3) & 7);
2787
232k
        pgs->line_params.dash_cap = (gs_line_cap)(cb & 7);
2788
232k
        if_debug2m('L', pgs->memory, "[L]      end_cap=%d dash_cap=%d\n",
2789
232k
                   pgs->line_params.end_cap, pgs->line_params.dash_cap);
2790
232k
    }
2791
1.54M
    if (mask & cj_ac_sa_known) {
2792
104k
        cb = *cbp++;
2793
104k
        pgs->line_params.curve_join = ((cb >> 2) & 7) - 1;
2794
104k
        pgs->accurate_curves = (cb & 2) != 0;
2795
104k
        pgs->stroke_adjust = cb & 1;
2796
104k
        if_debug3m('L', pgs->memory, "[L]      CJ=%d AC=%d SA=%d\n",
2797
104k
                   pgs->line_params.curve_join, pgs->accurate_curves,
2798
104k
                   pgs->stroke_adjust);
2799
104k
    }
2800
1.54M
    if (mask & flatness_known) {
2801
116k
        cmd_get_value(pgs->flatness, cbp);
2802
116k
        if_debug1m('L', pgs->memory, "[L]      flatness=%g\n", pgs->flatness);
2803
116k
    }
2804
1.54M
    if (mask & line_width_known) {
2805
646k
        float width;
2806
2807
646k
        cmd_get_value(width, cbp);
2808
646k
        if_debug1m('L', pgs->memory, "[L]      line_width=%g\n", width);
2809
646k
        gx_set_line_width(&pgs->line_params, width);
2810
646k
    }
2811
1.54M
    if (mask & miter_limit_known) {
2812
19.5k
        float limit;
2813
2814
19.5k
        cmd_get_value(limit, cbp);
2815
19.5k
        if_debug1m('L', pgs->memory, "[L]      miter_limit=%g\n", limit);
2816
19.5k
        gx_set_miter_limit(&pgs->line_params, limit);
2817
19.5k
    }
2818
1.54M
    if (mask & op_bm_tk_known) {
2819
933k
        cb = *cbp++;
2820
933k
        pgs->blend_mode = cb >> 3;
2821
933k
        pgs->text_knockout = cb & 1;
2822
        /* the following usually have no effect; see gxclpath.c */
2823
933k
        cb = *cbp++;
2824
933k
        pgs->overprint_mode = (cb >> 2) & 1;
2825
933k
        pgs->stroke_overprint = (cb >> 1) & 1;
2826
933k
        pgs->overprint = cb & 1;
2827
933k
        cb = *cbp++;
2828
933k
        pgs->renderingintent = cb;
2829
933k
        if_debug6m('L', pgs->memory, "[L]      BM=%d TK=%d OPM=%d OP=%d op=%d RI=%d\n",
2830
933k
                   pgs->blend_mode, pgs->text_knockout, pgs->overprint_mode,
2831
933k
                   pgs->stroke_overprint, pgs->overprint, pgs->renderingintent);
2832
933k
    }
2833
1.54M
    if (mask & segment_notes_known) {
2834
734
        cb = *cbp++;
2835
734
        *pnotes = (segment_notes)(cb & 0x3f);
2836
734
        if_debug1m('L', pgs->memory, "[L]      notes=%d\n", *pnotes);
2837
734
    }
2838
1.54M
    if (mask & ais_known) {
2839
61.7k
        cmd_get_value(pgs->alphaisshape, cbp);
2840
61.7k
        if_debug1m('L', pgs->memory, "[L]      alphaisshape=%d\n", pgs->alphaisshape);
2841
61.7k
    }
2842
1.54M
    if (mask & stroke_alpha_known) {
2843
221k
        cmd_get_value(pgs->strokeconstantalpha, cbp);
2844
221k
        if_debug1m('L', pgs->memory, "[L]      strokeconstantalpha=%g\n", pgs->strokeconstantalpha);
2845
221k
    }
2846
1.54M
    if (mask & fill_alpha_known) {
2847
268k
        cmd_get_value(pgs->fillconstantalpha, cbp);
2848
268k
        if_debug1m('L', pgs->memory, "[L]      fillconstantalpha=%u\n", (uint)(pgs->fillconstantalpha));
2849
268k
    }
2850
1.54M
    pcb->ptr = cbp;
2851
1.54M
    return 0;
2852
1.54M
}
2853
2854
static int
2855
read_set_color_space(command_buf_t *pcb, gs_gstate *pgs,
2856
                     gx_device_clist_reader *cdev, gs_memory_t *mem)
2857
338k
{
2858
338k
    const byte *cbp = pcb->ptr;
2859
338k
    byte b = *cbp++;
2860
338k
    int index = b >> 4;
2861
338k
    gs_color_space *pcs;
2862
338k
    int code = 0;
2863
338k
    cmm_profile_t *picc_profile;
2864
338k
    clist_icc_color_t icc_information;
2865
2866
338k
    if_debug3m('L', mem, " %d%s%s\n", index,
2867
338k
               (b & 8 ? " (indexed)" : ""),
2868
338k
               (b & 4 ? "(proc)" : ""));
2869
    /* They all store the ICC information.  Even if it is NULL
2870
       it is used in the ICC case to avoid reading from the
2871
       serialized profile data which is stored elsewhere in the
2872
       clist.  Hence we avoid jumping around in the file. */
2873
338k
    memcpy(&icc_information, cbp, sizeof(clist_icc_color_t));
2874
338k
    cbp = cbp + sizeof(clist_icc_color_t);
2875
338k
    switch (index) {
2876
0
    case gs_color_space_index_DeviceGray:
2877
0
        pcs = gs_cspace_new_DeviceGray(mem);
2878
0
        break;
2879
0
    case gs_color_space_index_DeviceRGB:
2880
0
        pcs = gs_cspace_new_DeviceRGB(mem);
2881
0
        break;
2882
0
    case gs_color_space_index_DeviceCMYK:
2883
0
        pcs = gs_cspace_new_DeviceCMYK(mem);
2884
0
        break;
2885
338k
    case gs_color_space_index_ICC:
2886
        /* build the color space object */
2887
338k
        code = gs_cspace_build_ICC(&pcs, NULL, mem);
2888
        /* Don't bother getting the ICC stuff from the clist yet */
2889
338k
        picc_profile = gsicc_profile_new(NULL, cdev->memory, NULL, 0);
2890
338k
        if (picc_profile == NULL)
2891
0
            return gs_rethrow(-1, "Failed to find ICC profile during clist read");
2892
338k
        picc_profile->num_comps = icc_information.icc_num_components;
2893
338k
        picc_profile->hashcode = icc_information.icc_hash;
2894
338k
        picc_profile->hash_is_valid = true;
2895
338k
        picc_profile->islab = icc_information.is_lab;
2896
338k
        picc_profile->default_match = icc_information.default_match;
2897
338k
        picc_profile->data_cs = icc_information.data_cs;
2898
        /* Store the clist reader address in the profile
2899
           structure so that we can get to the buffer
2900
           data if we really neeed it.  Ideally, we
2901
           will use a cached link and only access this once. */
2902
338k
        picc_profile->dev = (gx_device*) cdev;
2903
        /* Assign it to the colorspace */
2904
338k
        code = gsicc_set_gscs_profile(pcs, picc_profile, mem);
2905
        /* And we no longer need our reference to the profile */
2906
338k
        gsicc_adjust_profile_rc(picc_profile, -1, "read_set_color_space");
2907
338k
        break;
2908
0
    default:
2909
0
        code = gs_note_error(gs_error_rangecheck);      /* others are NYI */
2910
0
        goto out;
2911
338k
    }
2912
2913
338k
    if (pcs == NULL) {
2914
0
        code = gs_note_error(gs_error_VMerror);
2915
0
        goto out;
2916
0
    }
2917
2918
338k
    if (b & 8) {
2919
15.6k
        bool use_proc = (b & 4) != 0;
2920
15.6k
        int hival;
2921
15.6k
        int num_values;
2922
15.6k
        byte *data;
2923
15.6k
        uint data_size;
2924
15.6k
        gs_color_space *pcs_indexed;
2925
2926
15.6k
        pcs_indexed = gs_cspace_alloc(mem, &gs_color_space_type_Indexed);
2927
15.6k
        if (pcs_indexed == 0) {
2928
0
            rc_decrement_cs(pcs, "read_set_color_space");
2929
0
            code = gs_note_error(gs_error_VMerror);
2930
0
            goto out;
2931
0
        }
2932
15.6k
        pcs_indexed->base_space = pcs;
2933
15.6k
        pcs = pcs_indexed;
2934
15.6k
        pcs->params.indexed.use_proc = 0;
2935
15.6k
        pcs->params.indexed.lookup.table.data = 0;
2936
15.6k
        pcs->params.indexed.lookup.table.size = 0;
2937
15.6k
        cmd_getw(hival, cbp);
2938
15.6k
        pcs->params.indexed.n_comps = gs_color_space_num_components(pcs->base_space);
2939
15.6k
        num_values = (hival + 1) * pcs->params.indexed.n_comps;
2940
15.6k
        if (use_proc) {
2941
0
            gs_indexed_map *map;
2942
2943
0
            code = alloc_indexed_map(&map, num_values, mem, "indexed map");
2944
0
            if (code < 0) {
2945
0
                rc_decrement_cs(pcs, "read_set_color_space");
2946
0
                goto out;
2947
0
            }
2948
0
            map->proc.lookup_index = lookup_indexed_map;
2949
0
            pcs->params.indexed.lookup.map = map;
2950
0
            data = (byte *)map->values;
2951
0
            data_size = num_values * sizeof(map->values[0]);
2952
15.6k
        } else {
2953
15.6k
            byte *table = gs_alloc_string(mem, num_values, "color_space indexed table");
2954
2955
15.6k
            if (table == 0) {
2956
0
                code = gs_note_error(gs_error_VMerror);
2957
0
                rc_decrement_cs(pcs, "read_set_color_space");
2958
0
                goto out;
2959
0
            }
2960
15.6k
            pcs->params.indexed.lookup.table.data = table;
2961
15.6k
            pcs->params.indexed.lookup.table.size = num_values;
2962
15.6k
            data = table;
2963
15.6k
            data_size = num_values;
2964
15.6k
        }
2965
15.6k
        cbp = cmd_read_data(pcb, data, data_size, cbp);
2966
15.6k
        pcs->params.indexed.hival = hival;
2967
15.6k
        pcs->params.indexed.use_proc = use_proc;
2968
15.6k
    }
2969
2970
    /* Release reference to old color space before installing new one. */
2971
338k
    if (pgs->color[0].color_space != NULL)
2972
338k
        rc_decrement_only_cs(pgs->color[0].color_space, "read_set_color_space");
2973
338k
    pgs->color[0].color_space = pcs;
2974
338k
out:
2975
338k
    pcb->ptr = cbp;
2976
338k
    return code;
2977
338k
}
2978
2979
static int
2980
read_begin_image(command_buf_t *pcb, gs_image_common_t *pic,
2981
                 gs_color_space *pcs)
2982
453k
{
2983
453k
    uint index = *(pcb->ptr)++;
2984
453k
    const gx_image_type_t *image_type = gx_image_type_table[index];
2985
453k
    stream s;
2986
453k
    int code;
2987
2988
    /* This is sloppy, but we don't have enough information to do better. */
2989
453k
    code = top_up_cbuf(pcb, &pcb->ptr);
2990
453k
    if (code < 0)
2991
0
        return code;
2992
453k
    s_init(&s, NULL);
2993
453k
    sread_string(&s, pcb->ptr, pcb->end - pcb->ptr);
2994
453k
    code = image_type->sget(pic, &s, pcs);
2995
453k
    pcb->ptr = sbufptr(&s);
2996
453k
    pic->imagematrices_are_untrustworthy = 0;
2997
453k
    return code;
2998
453k
}
2999
3000
static int
3001
read_put_params(command_buf_t *pcb, gs_gstate *pgs,
3002
                gx_device_clist_reader *cdev, gs_memory_t *mem)
3003
0
{
3004
0
    const byte *cbp = pcb->ptr;
3005
0
    gs_c_param_list param_list;
3006
0
    uint cleft;
3007
0
    uint rleft;
3008
0
    bool alloc_data_on_heap = false;
3009
0
    byte *param_buf;
3010
0
    uint param_length;
3011
0
    int code;
3012
3013
0
    cmd_get_value(param_length, cbp);
3014
0
    if_debug1m('L', mem, " length=%d\n", param_length);
3015
0
    if (param_length == 0) {
3016
0
        code = 1;               /* empty list */
3017
0
        goto out;
3018
0
    }
3019
3020
    /* Make sure entire serialized param list is in cbuf */
3021
    /* + force void* alignment */
3022
0
    code = top_up_cbuf(pcb, &cbp);
3023
0
    if (code < 0)
3024
0
        return code;
3025
0
    if (pcb->end - cbp >= param_length) {
3026
0
        param_buf = (byte *)cbp;
3027
0
        cbp += param_length;
3028
0
    } else {
3029
        /* NOTE: param_buf must be maximally aligned */
3030
0
        param_buf = gs_alloc_bytes(mem, param_length,
3031
0
                                   "clist put_params");
3032
0
        if (param_buf == 0) {
3033
0
            code = gs_note_error(gs_error_VMerror);
3034
0
            goto out;
3035
0
        }
3036
0
        alloc_data_on_heap = true;
3037
0
        cleft = pcb->end - cbp;
3038
0
        rleft = param_length - cleft;
3039
0
        memmove(param_buf, cbp, cleft);
3040
0
        next_is_skip(pcb);
3041
0
        pcb->end_status = sgets(pcb->s, param_buf + cleft, rleft, &rleft);
3042
0
        cbp = pcb->end;  /* force refill */
3043
0
    }
3044
3045
    /*
3046
     * Create a gs_c_param_list & expand into it.
3047
     * NB that gs_c_param_list doesn't copy objects into
3048
     * it, but rather keeps *pointers* to what's passed.
3049
     * That's OK because the serialized format keeps enough
3050
     * space to hold expanded versions of the structures,
3051
     * but this means we cannot deallocate source buffer
3052
     * until the gs_c_param_list is deleted.
3053
     */
3054
0
    gs_c_param_list_write(&param_list, mem);
3055
0
    code = gs_param_list_unserialize
3056
0
        ( (gs_param_list *)&param_list, param_buf );
3057
0
    if (code >= 0 && code != param_length)
3058
0
        code = gs_error_unknownerror;  /* must match */
3059
0
    if (code >= 0) {
3060
0
        gs_c_param_list_read(&param_list);
3061
0
        code = gs_gstate_putdeviceparams(pgs, (gx_device *)cdev,
3062
0
                                         (gs_param_list *)&param_list);
3063
0
    }
3064
0
    gs_c_param_list_release(&param_list);
3065
0
    if (alloc_data_on_heap)
3066
0
        gs_free_object(mem, param_buf, "clist put_params");
3067
3068
0
out:
3069
0
    pcb->ptr = cbp;
3070
0
    return code;
3071
0
}
3072
3073
/*
3074
 * Read a "composite" command, and execute the command.
3075
 *
3076
 * This code assumes that a the largest create compositor command,
3077
 * including the compositor name size, is smaller than the data buffer
3078
 * size. This assumption is inherent in the basic design of the coding
3079
 * and the de-serializer interface, as no length field is provided.
3080
 *
3081
 * At the time of this writing, no compositor violates this assumption.
3082
 * The largest composite is currently 1275 bytes, while the command
3083
 * data buffer is 4096 bytes.
3084
 *
3085
 * In the event that this assumption is violated, a change in the encoding
3086
 * would be called for.
3087
 *
3088
 * See comment in gdevp14.c c_pdf14trans_read PDF14_BEGIN_TRANS_MASK case.
3089
 */
3090
extern_gs_find_compositor();
3091
3092
static int
3093
read_composite(
3094
    command_buf_t *pcb,  gs_memory_t *mem, gs_composite_t **ppcomp)
3095
148M
{
3096
148M
    const byte *                cbp = pcb->ptr;
3097
148M
    int                         comp_id = 0, code = 0;
3098
148M
    const gs_composite_type_t * pcomp_type = 0;
3099
3100
    /* fill the command buffer (see comment above) */
3101
148M
    if (pcb->end - cbp < MAX_CLIST_COMPOSITOR_SIZE + sizeof(comp_id)) {
3102
9.94M
        code = top_up_cbuf(pcb, &cbp);
3103
9.94M
        if (code < 0)
3104
0
            return code;
3105
9.94M
    }
3106
3107
    /* find the appropriate compositor method vector */
3108
148M
    comp_id = *cbp++;
3109
148M
    if ((pcomp_type = gs_find_compositor(comp_id)) == 0)
3110
0
        return_error(gs_error_unknownerror);
3111
3112
    /* de-serialize the compositor */
3113
148M
    code = pcomp_type->procs.read(ppcomp, cbp, pcb->end - cbp, mem);
3114
3115
    /* If we read more than the maximum expected, return a rangecheck error */
3116
148M
    if ( code > MAX_CLIST_COMPOSITOR_SIZE )
3117
0
        return_error(gs_error_rangecheck);
3118
3119
148M
    if (code > 0)
3120
148M
        cbp += code;
3121
148M
    pcb->ptr = cbp;
3122
148M
    return code;
3123
148M
}
3124
3125
static int apply_composite(gx_device_clist_reader *cdev, gs_gstate *pgs,
3126
                                   gs_memory_t *mem, gs_composite_t *pcomp,
3127
                                   int x0, int y0, gx_device **ptarget)
3128
133M
{
3129
133M
    gx_device *tdev = *ptarget;
3130
133M
    int code;
3131
3132
133M
    code = pcomp->type->procs.adjust_ctm(pcomp, x0, y0, pgs);
3133
133M
    if (code < 0)
3134
0
        goto exit;
3135
    /*
3136
     * Apply the compositor to the target device; note that this may
3137
     * change the target device.
3138
     */
3139
133M
    code = dev_proc(tdev, composite)(tdev, &tdev, pcomp, pgs, mem, (gx_device*) cdev);
3140
133M
    if (code == 1) {
3141
        /* A new compositor was created that wrapped tdev. This should
3142
         * be our new target. */
3143
1.09M
        *ptarget = tdev;
3144
1.09M
        code = 0;
3145
1.09M
    }
3146
133M
    if (code < 0)
3147
15
        goto exit;
3148
3149
    /* Perform any updates for the clist device required */
3150
133M
    code = pcomp->type->procs.clist_compositor_read_update(pcomp,
3151
133M
                                        (gx_device *)cdev, tdev, pgs, mem);
3152
133M
exit:
3153
    /* free the compositor object */
3154
133M
    gs_free_object(mem, pcomp, "read_composite");
3155
3156
133M
    return code;
3157
133M
}
3158
3159
/* ---------------- Utilities ---------------- */
3160
3161
/* Read and unpack a short bitmap */
3162
/*
3163
 * The 'raster' in the dest buffer may be larger than the 'width_bytes'
3164
 * in the src, so after reading we memmove data down to the proper
3165
 * alignment from the last line backwards.
3166
 * THIS RELIES on width_bytes <= raster to work.
3167
 */
3168
static const byte *
3169
cmd_read_short_bits(command_buf_t *pcb, byte *data, int tot_bytes,
3170
                    int width_bytes, int height, uint raster, const byte *cbp)
3171
6.78M
{
3172
    /* Note the following may read from the file past the end of the buffer */
3173
    /* leaving cbp at pcb->end. No further reading using cbp can be done    */
3174
    /* without top_up_cbuf to reload the buffer.                            */
3175
6.78M
    cbp = cmd_read_data(pcb, data, tot_bytes, cbp);
3176
3177
    /* if needed, adjust buffer contents for dest raster > width_bytes */
3178
6.78M
    if (width_bytes < raster) {
3179
6.78M
        const byte *pdata = data /*src*/ + width_bytes * height;
3180
6.78M
        byte *udata = data /*dest*/ + height * raster;
3181
3182
109M
        while (--height > 0) { /* don't need to move the first line to itself */
3183
103M
            udata -= raster, pdata -= width_bytes;
3184
103M
            switch (width_bytes) {
3185
0
                default:
3186
0
                    memmove(udata, pdata, width_bytes);
3187
0
                    break;
3188
363k
                case 6:
3189
363k
                    udata[5] = pdata[5];
3190
1.11M
                case 5:
3191
1.11M
                    udata[4] = pdata[4];
3192
3.74M
                case 4:
3193
3.74M
                    udata[3] = pdata[3];
3194
20.8M
                case 3:
3195
20.8M
                    udata[2] = pdata[2];
3196
82.1M
                case 2:
3197
82.1M
                    udata[1] = pdata[1];
3198
103M
                case 1:
3199
103M
                    udata[0] = pdata[0];
3200
103M
                case 0:;            /* shouldn't happen */
3201
103M
            }
3202
103M
        }
3203
6.78M
    }
3204
6.78M
    return cbp;
3205
6.78M
}
3206
3207
/* Read a rectangle. */
3208
static const byte *
3209
cmd_read_rect(int op, gx_cmd_rect * prect, const byte * cbp)
3210
15.8M
{
3211
15.8M
    cmd_getw(prect->x, cbp);
3212
15.8M
    if (op & 0xf)
3213
879k
        prect->y += ((op >> 2) & 3) - 2;
3214
14.9M
    else {
3215
14.9M
        cmd_getw(prect->y, cbp);
3216
14.9M
    }
3217
15.8M
    cmd_getw(prect->width, cbp);
3218
15.8M
    if (op & 0xf)
3219
879k
        prect->height += (op & 3) - 2;
3220
14.9M
    else {
3221
14.9M
        cmd_getw(prect->height, cbp);
3222
14.9M
    }
3223
15.8M
    return cbp;
3224
15.8M
}
3225
3226
/*
3227
 * Select a map for loading with data.
3228
 *
3229
 * This routine has three outputs:
3230
 *   *pmdata - points to the map data.
3231
 *   *pcomp_num - points to a component number if the map is a transfer
3232
 *               map which has been set via the setcolortransfer operator.
3233
 *               A. value of NULL indicates that no component number is to
3234
 *               be sent for this map.
3235
 *   *pcount - the size of the map (in bytes).
3236
 */
3237
static int
3238
cmd_select_map(cmd_map_index map_index, cmd_map_contents cont,
3239
               gs_gstate * pgs, int ** pcomp_num, frac ** pmdata,
3240
               uint * pcount, gs_memory_t * mem)
3241
5.21M
{
3242
5.21M
    gx_transfer_map *map;
3243
5.21M
    gx_transfer_map **pmap;
3244
5.21M
    const char *cname;
3245
3246
5.21M
    *pcomp_num = NULL;          /* Only used for color transfer maps */
3247
5.21M
    switch (map_index) {
3248
1.72M
        case cmd_map_transfer:
3249
1.72M
            if_debug0m('L', mem, " transfer");
3250
1.72M
            rc_unshare_struct(pgs->set_transfer.gray, gx_transfer_map,
3251
1.72M
                &st_transfer_map, mem, return_error(gs_error_VMerror),
3252
1.72M
                "cmd_select_map(default_transfer)");
3253
1.72M
            map = pgs->set_transfer.gray;
3254
            /* Release all current maps */
3255
1.72M
            rc_decrement(pgs->set_transfer.red, "cmd_select_map(red)");
3256
1.72M
            pgs->set_transfer.red = NULL;
3257
1.72M
            pgs->set_transfer.red_component_num = -1;
3258
1.72M
            rc_decrement(pgs->set_transfer.green, "cmd_select_map(green)");
3259
1.72M
            pgs->set_transfer.green = NULL;
3260
1.72M
            pgs->set_transfer.green_component_num = -1;
3261
1.72M
            rc_decrement(pgs->set_transfer.blue, "cmd_select_map(blue)");
3262
1.72M
            pgs->set_transfer.blue = NULL;
3263
1.72M
            pgs->set_transfer.blue_component_num = -1;
3264
1.72M
            goto transfer2;
3265
212
        case cmd_map_transfer_0:
3266
212
            pmap = &pgs->set_transfer.red;
3267
212
            *pcomp_num = &pgs->set_transfer.red_component_num;
3268
212
            goto transfer1;
3269
212
        case cmd_map_transfer_1:
3270
212
            pmap = &pgs->set_transfer.green;
3271
212
            *pcomp_num = &pgs->set_transfer.green_component_num;
3272
212
            goto transfer1;
3273
212
        case cmd_map_transfer_2:
3274
212
            pmap = &pgs->set_transfer.blue;
3275
212
            *pcomp_num = &pgs->set_transfer.blue_component_num;
3276
212
            goto transfer1;
3277
0
        case cmd_map_transfer_3:
3278
0
            pmap = &pgs->set_transfer.gray;
3279
0
            *pcomp_num = &pgs->set_transfer.gray_component_num;
3280
636
transfer1:  if_debug1m('L', mem, " transfer[%d]", (int)(map_index - cmd_map_transfer_0));
3281
636
            rc_unshare_struct(*pmap, gx_transfer_map, &st_transfer_map, mem,
3282
636
                return_error(gs_error_VMerror), "cmd_select_map(transfer)");
3283
636
            map = *pmap;
3284
3285
1.72M
transfer2:  if (cont != cmd_map_other) {
3286
936k
                gx_set_identity_transfer(map);
3287
936k
                *pmdata = 0;
3288
936k
                *pcount = 0;
3289
936k
                return 0;
3290
936k
            }
3291
789k
            break;
3292
1.74M
        case cmd_map_black_generation:
3293
1.74M
            if_debug0m('L', mem, " black generation");
3294
1.74M
            pmap = &pgs->black_generation;
3295
1.74M
            cname = "cmd_select_map(black generation)";
3296
1.74M
            goto alloc;
3297
1.74M
        case cmd_map_undercolor_removal:
3298
1.74M
            if_debug0m('L', mem, " undercolor removal");
3299
1.74M
            pmap = &pgs->undercolor_removal;
3300
1.74M
            cname = "cmd_select_map(undercolor removal)";
3301
3.49M
alloc:      if (cont == cmd_map_none) {
3302
0
                rc_decrement(*pmap, cname);
3303
0
                *pmap = 0;
3304
0
                *pmdata = 0;
3305
0
                *pcount = 0;
3306
0
                return 0;
3307
0
            }
3308
3.49M
            rc_unshare_struct(*pmap, gx_transfer_map, &st_transfer_map,
3309
3.49M
                              mem, return_error(gs_error_VMerror), cname);
3310
3.49M
            map = *pmap;
3311
3.49M
            if (cont == cmd_map_identity) {
3312
0
                gx_set_identity_transfer(map);
3313
0
                *pmdata = 0;
3314
0
                *pcount = 0;
3315
0
                return 0;
3316
0
            }
3317
3.49M
            break;
3318
3.49M
        default:
3319
0
            *pmdata = 0;
3320
0
            return 0;
3321
5.21M
    }
3322
4.27M
    map->proc = gs_mapped_transfer;
3323
4.27M
    *pmdata = map->values;
3324
4.27M
    *pcount = sizeof(map->values);
3325
4.27M
    return 0;
3326
5.21M
}
3327
3328
/* Create a device halftone for the imager if necessary. */
3329
static int
3330
cmd_create_dev_ht(gx_device_halftone **ppdht, gs_memory_t *mem)
3331
0
{
3332
0
    gx_device_halftone *pdht = *ppdht;
3333
3334
0
    if (pdht == 0) {
3335
0
        rc_header rc;
3336
3337
0
        rc_alloc_struct_1(pdht, gx_device_halftone, &st_device_halftone, mem,
3338
0
                          return_error(gs_error_VMerror),
3339
0
                          "cmd_create_dev_ht");
3340
0
        rc = pdht->rc;
3341
0
        memset(pdht, 0, sizeof(*pdht));
3342
0
        pdht->rc = rc;
3343
0
        *ppdht = pdht;
3344
0
    }
3345
0
    return 0;
3346
0
}
3347
3348
/* Resize the halftone components array if necessary. */
3349
static int
3350
cmd_resize_halftone(gx_device_halftone **ppdht, uint num_comp,
3351
                    gs_memory_t * mem)
3352
0
{
3353
0
    int code = cmd_create_dev_ht(ppdht, mem);
3354
0
    gx_device_halftone *pdht = *ppdht;
3355
3356
0
    if (code < 0)
3357
0
        return code;
3358
0
    if (num_comp != pdht->num_comp) {
3359
0
        gx_ht_order_component *pcomp;
3360
3361
        /*
3362
         * We must be careful not to shrink or free the components array
3363
         * before releasing any relevant elements.
3364
         */
3365
0
        if (num_comp < pdht->num_comp) {
3366
0
            uint i;
3367
3368
            /* Don't release the default order. */
3369
0
            for (i = pdht->num_comp; i-- > num_comp;)
3370
0
                if (pdht->components[i].corder.bit_data != pdht->order.bit_data)
3371
0
                    gx_ht_order_release(&pdht->components[i].corder, mem, true);
3372
0
            if (num_comp == 0) {
3373
0
                gs_free_object(mem, pdht->components, "cmd_resize_halftone");
3374
0
                pcomp = 0;
3375
0
            } else {
3376
0
                pcomp = gs_resize_object(mem, pdht->components, num_comp,
3377
0
                                         "cmd_resize_halftone");
3378
0
                if (pcomp == 0) {
3379
0
                    pdht->num_comp = num_comp;  /* attempt consistency */
3380
0
                    return_error(gs_error_VMerror);
3381
0
                }
3382
0
            }
3383
0
        } else {
3384
            /* num_comp > pdht->num_comp */
3385
0
            if (pdht->num_comp == 0)
3386
0
                pcomp = gs_alloc_struct_array(mem, num_comp,
3387
0
                                              gx_ht_order_component,
3388
0
                                              &st_ht_order_component_element,
3389
0
                                              "cmd_resize_halftone");
3390
0
            else
3391
0
                pcomp = gs_resize_object(mem, pdht->components, num_comp,
3392
0
                                         "cmd_resize_halftone");
3393
0
            if (pcomp == 0)
3394
0
                return_error(gs_error_VMerror);
3395
0
            memset(&pcomp[pdht->num_comp], 0,
3396
0
                   sizeof(*pcomp) * (num_comp - pdht->num_comp));
3397
0
        }
3398
0
        pdht->num_comp = num_comp;
3399
0
        pdht->components = pcomp;
3400
0
    }
3401
0
    return 0;
3402
0
}
3403
3404
/* ------ Path operations ------ */
3405
3406
/* Decode a path segment. */
3407
static int
3408
clist_decode_segment(gx_path * ppath, int op, fixed vs[6],
3409
                 gs_fixed_point * ppos, int x0, int y0, segment_notes notes)
3410
53.4M
{
3411
53.4M
    fixed px = ppos->x - int2fixed(x0);
3412
53.4M
    fixed py = ppos->y - int2fixed(y0);
3413
53.4M
    int code;
3414
3415
69.8M
#define A vs[0]
3416
53.4M
#define B vs[1]
3417
53.4M
#define C vs[2]
3418
53.4M
#define D vs[3]
3419
53.4M
#define E vs[4]
3420
53.4M
#define F vs[5]
3421
3422
53.4M
    switch (op) {
3423
8.07M
        case cmd_opv_rmoveto:
3424
8.07M
            code = gx_path_add_point(ppath, px += A, py += B);
3425
8.07M
            break;
3426
6.24M
        case cmd_opv_rlineto:
3427
6.24M
            code = gx_path_add_line_notes(ppath, px += A, py += B, notes);
3428
6.24M
            break;
3429
0
        case cmd_opv_rgapto:
3430
0
            code = gx_path_add_gap_notes(ppath, px += A, py += B, notes);
3431
0
            break;
3432
7.91M
        case cmd_opv_hlineto:
3433
7.91M
            code = gx_path_add_line_notes(ppath, px += A, py, notes);
3434
7.91M
            break;
3435
10.5M
        case cmd_opv_vlineto:
3436
10.5M
            code = gx_path_add_line_notes(ppath, px, py += A, notes);
3437
10.5M
            break;
3438
1.83M
        case cmd_opv_rmlineto:
3439
1.83M
            if ((code = gx_path_add_point(ppath, px += A, py += B)) < 0)
3440
0
                break;
3441
1.83M
            code = gx_path_add_line_notes(ppath, px += C, py += D, notes);
3442
1.83M
            break;
3443
728k
        case cmd_opv_rm2lineto:
3444
728k
            if ((code = gx_path_add_point(ppath, px += A, py += B)) < 0 ||
3445
728k
                (code = gx_path_add_line_notes(ppath, px += C, py += D,
3446
728k
                                               notes)) < 0
3447
728k
                )
3448
0
                break;
3449
728k
            code = gx_path_add_line_notes(ppath, px += E, py += F, notes);
3450
728k
            break;
3451
292k
        case cmd_opv_rm3lineto:
3452
292k
            if ((code = gx_path_add_point(ppath, px += A, py += B)) < 0 ||
3453
292k
                (code = gx_path_add_line_notes(ppath, px += C, py += D,
3454
292k
                                               notes)) < 0 ||
3455
292k
                (code = gx_path_add_line_notes(ppath, px += E, py += F,
3456
292k
                                               notes)) < 0
3457
292k
                )
3458
0
                break;
3459
292k
            code = gx_path_add_line_notes(ppath, px -= C, py -= D, notes);
3460
292k
            break;
3461
11.1M
        case cmd_opv_rrcurveto: /* a b c d e f => a b a+c b+d a+c+e b+d+f */
3462
11.1M
rrc:        E += (C += A);
3463
11.1M
            F += (D += B);
3464
13.6M
curve:      code = gx_path_add_curve_notes(ppath, px + A, py + B,
3465
13.6M
                                           px + C, py + D,
3466
13.6M
                                           px + E, py + F, notes);
3467
13.6M
            px += E, py += F;
3468
13.6M
            break;
3469
1.01M
        case cmd_opv_hvcurveto: /* a b c d => a 0 a+b c a+b c+d */
3470
1.04M
hvc:        F = C + D, D = C, E = C = A + B, B = 0;
3471
1.04M
            goto curve;
3472
1.03M
        case cmd_opv_vhcurveto: /* a b c d => 0 a b a+c b+d a+c */
3473
1.08M
vhc:        E = B + D, F = D = A + C, C = B, B = A, A = 0;
3474
1.08M
            goto curve;
3475
131k
        case cmd_opv_nrcurveto: /* a b c d => 0 0 a b a+c b+d */
3476
131k
            F = B + D, E = A + C, D = B, C = A, B = A = 0;
3477
131k
            goto curve;
3478
168k
        case cmd_opv_rncurveto: /* a b c d => a b a+c b+d a+c b+d */
3479
168k
            F = D += B, E = C += A;
3480
168k
            goto curve;
3481
54.7k
        case cmd_opv_vqcurveto: /* a b => VH a b TS(a,b) TS(b,a) */
3482
54.7k
            if ((A ^ B) < 0)
3483
16.3k
                C = -B, D = -A;
3484
38.4k
            else
3485
38.4k
                C = B, D = A;
3486
54.7k
            goto vhc;
3487
24.9k
        case cmd_opv_hqcurveto: /* a b => HV a TS(a,b) b TS(b,a) */
3488
24.9k
            if ((A ^ B) < 0)
3489
8.10k
                D = -A, C = B, B = -B;
3490
16.8k
            else
3491
16.8k
                D = A, C = B;
3492
24.9k
            goto hvc;
3493
74.5k
        case cmd_opv_scurveto: /* (a b c d e f) => */
3494
74.5k
            {
3495
74.5k
                fixed a = A, b = B;
3496
3497
                /* See gxclpath.h for details on the following. */
3498
74.5k
                if (A == 0) {
3499
                    /* Previous curve was vh or vv */
3500
70.1k
                    A = E - C, B = D - F, C = C - a, D = b - D, E = a, F = -b;
3501
70.1k
                } else {
3502
                    /* Previous curve was hv or hh */
3503
4.48k
                    A = C - E, B = F - D, C = a - C, D = D - b, E = -a, F = b;
3504
4.48k
                }
3505
74.5k
            }
3506
74.5k
            goto rrc;
3507
4.21M
        case cmd_opv_closepath:
3508
4.21M
            if ((code = gx_path_close_subpath(ppath)) < 0)
3509
4.21M
                return code;;
3510
4.21M
            if ((code = gx_path_current_point(ppath, (gs_fixed_point *) vs)) < 0)
3511
4.21M
                return code;;
3512
4.21M
            px = A, py = B;
3513
4.21M
            break;
3514
0
        default:
3515
0
            return_error(gs_error_rangecheck);
3516
53.4M
    }
3517
53.4M
#undef A
3518
53.4M
#undef B
3519
53.4M
#undef C
3520
53.4M
#undef D
3521
53.4M
#undef E
3522
53.4M
#undef F
3523
53.4M
    ppos->x = px + int2fixed(x0);
3524
53.4M
    ppos->y = py + int2fixed(y0);
3525
53.4M
    return code;
3526
53.4M
}
3527
3528
/*
3529
 * Execute a polyfill -- either a fill_parallelogram or a fill_triangle.
3530
 *
3531
 * Note that degenerate parallelograms or triangles may collapse into
3532
 * a single line or point.  We must check for this so we don't try to
3533
 * access non-existent segments.
3534
 */
3535
static int
3536
clist_do_polyfill(gx_device *dev, gx_path *ppath,
3537
                  const gx_drawing_color *pdcolor,
3538
                  gs_logical_operation_t lop)
3539
397k
{
3540
397k
    const subpath *psub = ppath->first_subpath;
3541
397k
    const segment *pseg1;
3542
397k
    const segment *pseg2;
3543
397k
    int code;
3544
3545
397k
    if (psub && (pseg1 = psub->next) != 0 && (pseg2 = pseg1->next) != 0) {
3546
397k
        fixed px = psub->pt.x, py = psub->pt.y;
3547
397k
        fixed ax = pseg1->pt.x - px, ay = pseg1->pt.y - py;
3548
397k
        fixed bx, by;
3549
        /*
3550
         * We take advantage of the fact that the parameter lists for
3551
         * fill_parallelogram and fill_triangle are identical.
3552
         */
3553
397k
        dev_proc_fill_parallelogram((*fill));
3554
3555
        /* close_path of 3 point triangle adds 4th point, detected here.*/
3556
        /* close_path on parallelogram adds 5th point also ignored. */
3557
397k
        if (pseg2->next && !(px == pseg2->next->pt.x && py == pseg2->next->pt.y)) {
3558
            /* Parallelogram */
3559
368k
            fill = dev_proc(dev, fill_parallelogram);
3560
368k
            bx = pseg2->pt.x - pseg1->pt.x;
3561
368k
            by = pseg2->pt.y - pseg1->pt.y;
3562
368k
        } else {
3563
            /* Triangle */
3564
28.8k
            fill = dev_proc(dev, fill_triangle);
3565
28.8k
            bx = pseg2->pt.x - px;
3566
28.8k
            by = pseg2->pt.y - py;
3567
28.8k
        }
3568
397k
        code = fill(dev, px, py, ax, ay, bx, by, pdcolor, lop);
3569
397k
    } else
3570
0
        code = 0;
3571
397k
    gx_path_new(ppath);
3572
397k
    return code;
3573
397k
}