Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gxclrect.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
/* Rectangle-oriented command writing for command list */
18
#include "gx.h"
19
#include "gserrors.h"
20
#include "gsutil.h"   /* for gs_next_ids */
21
#include "gxdevice.h"
22
#include "gxdevmem.h"   /* must precede gxcldev.h */
23
#include "gxcldev.h"
24
#include "gxclpath.h"
25
#include "gxdevsop.h"
26
27
/* ---------------- Writing utilities ---------------- */
28
29
#define cmd_set_rect(rect)\
30
82.9M
  ((rect).x = x, (rect).y = y,\
31
82.9M
   (rect).width = width, (rect).height = height)
32
33
/* Write a rectangle. */
34
static int
35
cmd_size_rect(register const gx_cmd_rect * prect)
36
27.0M
{
37
27.0M
    return
38
27.0M
        cmd_sizew(prect->x) + cmd_sizew(prect->y) +
39
27.0M
        cmd_sizew(prect->width) + cmd_sizew(prect->height);
40
27.0M
}
41
static inline byte *
42
cmd_put_rect(const gx_cmd_rect * prect, byte * dp)
43
23.5M
{
44
23.5M
    cmd_putw(prect->x, &dp);
45
23.5M
    cmd_putw(prect->y, &dp);
46
23.5M
    cmd_putw(prect->width, &dp);
47
23.5M
    cmd_putw(prect->height, &dp);
48
23.5M
    return dp;
49
23.5M
}
50
51
int
52
cmd_write_rect_hl_cmd(gx_device_clist_writer * cldev, gx_clist_state * pcls,
53
                      int op, int x, int y, int width, int height,
54
                      bool extended_command)
55
22.1M
{
56
22.1M
    byte *dp;
57
22.1M
    int code;
58
22.1M
    int rcsize;
59
60
22.1M
    cmd_set_rect(pcls->rect);
61
22.1M
    if (extended_command) {
62
6.07k
        rcsize = 2 + cmd_size_rect(&pcls->rect);
63
6.07k
        code = set_cmd_put_extended_op(&dp, cldev, pcls, op, rcsize);
64
6.07k
        dp += 2;
65
22.1M
    } else {
66
22.1M
        rcsize = 1 + cmd_size_rect(&pcls->rect);
67
22.1M
        code = set_cmd_put_op(&dp, cldev, pcls, op, rcsize);
68
22.1M
        dp += 1;
69
22.1M
    }
70
22.1M
    if (code < 0)
71
0
        return code;
72
22.1M
    if_debug5m('L', cldev->memory, "[L]  rect hl r%d:%d,%d,%d,%d\n",
73
22.1M
               rcsize - 1, pcls->rect.x, pcls->rect.y,
74
22.1M
               pcls->rect.width, pcls->rect.height);
75
22.1M
    cmd_put_rect(&pcls->rect, dp);
76
22.1M
    return 0;
77
22.1M
}
78
79
int
80
cmd_write_rect_cmd(gx_device_clist_writer * cldev, gx_clist_state * pcls,
81
                   int op, int x, int y, int width, int height)
82
42.9M
{
83
42.9M
    int dx = x - pcls->rect.x;
84
42.9M
    int dy = y - pcls->rect.y;
85
42.9M
    int dwidth = width - pcls->rect.width;
86
42.9M
    int dheight = height - pcls->rect.height;
87
42.9M
    byte *dp;
88
42.9M
    int code;
89
90
42.9M
#define check_range_xy(rmin, rmax)\
91
52.1M
  ((unsigned)(dx - rmin) <= (rmax - rmin) &&\
92
52.1M
   (unsigned)(dy - rmin) <= (rmax - rmin))
93
42.9M
#define check_range_w(rmin, rmax)\
94
97.3M
  ((unsigned)(dwidth - rmin) <= (rmax - rmin))
95
42.9M
#define check_ranges(rmin, rmax)\
96
42.9M
  (check_range_xy(rmin, rmax) && check_range_w(rmin, rmax) &&\
97
7.24M
   (unsigned)(dheight - rmin) <= (rmax - rmin))
98
42.9M
    cmd_set_rect(pcls->rect);
99
42.9M
    if (dheight == 0 && check_range_w(cmd_min_dw_tiny, cmd_max_dw_tiny) &&
100
37.6M
        check_range_xy(cmd_min_dxy_tiny, cmd_max_dxy_tiny)
101
42.9M
        ) {
102
35.7M
        byte op_tiny = op + 0x20 + dwidth - cmd_min_dw_tiny;
103
104
35.7M
        if (dx == width - dwidth && dy == 0) {
105
24.8M
            code = set_cmd_put_op(&dp, cldev, pcls, op_tiny + 8, 1);
106
24.8M
            if (code < 0)
107
0
                return code;
108
24.8M
        } else {
109
10.8M
            code = set_cmd_put_op(&dp, cldev, pcls, op_tiny, 2);
110
10.8M
            if (code < 0)
111
0
                return code;
112
10.8M
            dp[1] = (dx << 4) + dy - (cmd_min_dxy_tiny * 0x11);
113
10.8M
        }
114
35.7M
    }
115
14.0M
#define rmin cmd_min_short
116
7.24M
#define rmax cmd_max_short
117
7.24M
    else if (check_ranges(rmin, rmax)) {
118
5.03M
        int dh = dheight - cmd_min_dxy_tiny;
119
120
5.03M
        if ((unsigned)dh <= cmd_max_dxy_tiny - cmd_min_dxy_tiny &&
121
5.00M
            dh != 0 && dy == 0
122
5.03M
            ) {
123
3.03M
            op += dh;
124
3.03M
            code = set_cmd_put_op(&dp, cldev, pcls, op + 0x10, 3);
125
3.03M
            if (code < 0)
126
0
                return code;
127
3.03M
            if_debug3m('L', cldev->memory, "    rs2:%d,%d,0,%d\n",
128
3.03M
                       dx, dwidth, dheight);
129
3.03M
        } else {
130
1.99M
            code = set_cmd_put_op(&dp, cldev, pcls, op + 0x10, 5);
131
1.99M
            if (code < 0)
132
0
                return code;
133
1.99M
            if_debug4m('L', cldev->memory, "    rs4:%d,%d,%d,%d\n",
134
1.99M
                       dx, dwidth, dy, dheight);
135
1.99M
            dp[3] = dy - rmin;
136
1.99M
            dp[4] = dheight - rmin;
137
1.99M
        }
138
5.03M
        dp[1] = dx - rmin;
139
5.03M
        dp[2] = dwidth - rmin;
140
5.03M
    }
141
2.20M
#undef rmin
142
2.20M
#undef rmax
143
2.20M
    else if (dy >= -2 && dy <= 1 && dheight >= -2 && dheight <= 1 &&
144
801k
             (dy + dheight) != -4
145
2.20M
        ) {
146
801k
        int rcsize = 1 + cmd_sizew(x) + cmd_sizew(width);
147
148
801k
        code = set_cmd_put_op(&dp, cldev, pcls,
149
801k
                              op + ((dy + 2) << 2) + dheight + 2, rcsize);
150
801k
        if (code < 0)
151
0
            return code;
152
801k
        ++dp;
153
801k
        cmd_put2w(x, width, &dp);
154
1.40M
    } else {
155
1.40M
        int rcsize = 1 + cmd_size_rect(&pcls->rect);
156
157
1.40M
        code = set_cmd_put_op(&dp, cldev, pcls, op, rcsize);
158
1.40M
        if (code < 0)
159
0
            return code;
160
1.40M
        if_debug5m('L', cldev->memory, "[L]  r%d:%d,%d,%d,%d\n",
161
1.40M
                   rcsize - 1, dx, dwidth, dy, dheight);
162
1.40M
        cmd_put_rect(&pcls->rect, dp + 1);
163
1.40M
    }
164
42.9M
    return 0;
165
42.9M
}
166
167
/* Put out a fill or tile rectangle command for fillpage. */
168
int
169
cmd_write_page_rect_cmd(gx_device_clist_writer * cldev, int op)
170
538k
{
171
    /* Since cmd_write_rect_cmd always writes 0x0 rectangle as a tiny one,
172
       here we write a "big" rectangle with the size 0x0.
173
       The clist reader must handle this case especially.
174
     */
175
538k
    int rcsize = 1 + 4 * cmd_sizew(0);
176
538k
    byte *dp;
177
538k
    int x = 0, y = 0, width = 0, height = 0; /* For cmd_set_rect */
178
538k
    gx_clist_state *pcls1;
179
538k
    int code;
180
181
538k
    if_debug0m('L', cldev->memory, "[L]fillpage beg\n");
182
538k
    code = set_cmd_put_all_op(&dp, cldev, op, rcsize);
183
538k
    if (code < 0)
184
0
        return code;
185
17.7M
    for (pcls1 = cldev->states; pcls1 < cldev->states + cldev->nbands; pcls1++)
186
17.2M
        cmd_set_rect(pcls1->rect);
187
538k
    ++dp;
188
538k
    cmd_putw(0, &dp);
189
538k
    cmd_putw(0, &dp);
190
538k
    cmd_putw(0, &dp);
191
538k
    cmd_putw(0, &dp);
192
538k
    if_debug0m('L', cldev->memory, "[L]fillpage end\n");
193
538k
    return 0;
194
538k
}
195
196
static inline byte *
197
cmd_put_frac31_color(gx_device_clist_writer * cldev, const frac31 *c, byte *dp)
198
57.1M
{
199
57.1M
    uchar num_components = cldev->clist_color_info.num_components;
200
57.1M
    uchar j;
201
202
281M
    for (j = 0; j < num_components; j++)
203
224M
        dp = cmd_put_frac31(c[j], dp);
204
57.1M
    return dp;
205
57.1M
}
206
207
static inline int
208
cmd_size_frac31_color(gx_device_clist_writer * cldev, const frac31 *c)
209
57.1M
{
210
57.1M
    uchar j;
211
57.1M
    int s = 0;
212
57.1M
    uchar num_components = cldev->clist_color_info.num_components;
213
214
281M
    for (j = 0; j < num_components; j++)
215
224M
        s += cmd_size_frac31(c[j]);
216
57.1M
    return s;
217
57.1M
}
218
219
static int
220
cmd_write_trapezoid_cmd(gx_device_clist_writer * cldev, gx_clist_state * pcls,
221
                    int op,  const gs_fixed_edge *left, const gs_fixed_edge *right,
222
                    fixed ybot, fixed ytop, int options,
223
                    const gs_fill_attributes *fa,
224
                    const frac31 *c0, const frac31 *c1,
225
                    const frac31 *c2, const frac31 *c3)
226
79.1M
{
227
79.1M
    byte *dp;
228
79.1M
    int rcsize;
229
79.1M
    int code;
230
79.1M
    int colors_mask = 0;
231
232
79.1M
    rcsize = 1 + cmd_sizew(left->start.x) + cmd_sizew(left->start.y)
233
79.1M
               + cmd_sizew(left->end.x) + cmd_sizew(left->end.y)
234
79.1M
               + cmd_sizew(right->start.x) + cmd_sizew(right->start.y)
235
79.1M
               + cmd_sizew(right->end.x) + cmd_sizew(right->end.y)
236
79.1M
               + cmd_sizew(options);
237
238
79.1M
    if (!(options & 4))
239
61.1M
        rcsize += cmd_sizew(ybot) + cmd_sizew(ytop);
240
79.1M
    if (options & 2) {
241
19.5M
        rcsize += cmd_sizew(fa->clip->p.x) + cmd_sizew(fa->clip->p.y)
242
19.5M
                + cmd_sizew(fa->clip->q.x) + cmd_sizew(fa->clip->q.y);
243
19.5M
        if (c0 != NULL) {
244
19.5M
            colors_mask += 1;
245
19.5M
            rcsize += cmd_size_frac31_color(cldev, c0);
246
19.5M
        }
247
19.5M
        if (c1 != NULL) {
248
19.5M
            colors_mask += 2;
249
19.5M
            rcsize += cmd_size_frac31_color(cldev, c1);
250
19.5M
        }
251
19.5M
        if (c2 != NULL) {
252
18.0M
            colors_mask += 4;
253
18.0M
            rcsize += cmd_size_frac31_color(cldev, c2);
254
18.0M
        }
255
19.5M
        if (c3 != NULL) {
256
0
            colors_mask += 8;
257
0
            rcsize += cmd_size_frac31_color(cldev, c3);
258
0
        }
259
19.5M
        rcsize += cmd_sizew(colors_mask);
260
19.5M
    }
261
79.1M
    code = set_cmd_put_op(&dp, cldev, pcls, op, rcsize);
262
79.1M
    if (code < 0)
263
0
        return code;
264
79.1M
    dp++;
265
79.1M
    cmd_putw(left->start.x, &dp);
266
79.1M
    cmd_putw(left->start.y, &dp);
267
79.1M
    cmd_putw(left->end.x, &dp);
268
79.1M
    cmd_putw(left->end.y, &dp);
269
79.1M
    cmd_putw(right->start.x, &dp);
270
79.1M
    cmd_putw(right->start.y, &dp);
271
79.1M
    cmd_putw(right->end.x, &dp);
272
79.1M
    cmd_putw(right->end.y, &dp);
273
79.1M
    cmd_putw(options, &dp);
274
79.1M
    if (!(options & 4)) {
275
61.1M
        cmd_putw(ybot, &dp);
276
61.1M
        cmd_putw(ytop, &dp);
277
61.1M
    }
278
79.1M
    if_debug6m('L', cldev->memory, "    t%d:%ld,%ld,%ld,%ld   %ld\n",
279
79.1M
               rcsize - 1, (long int)left->start.x, (long int)left->start.y,
280
79.1M
               (long int)left->end.x, (long int)left->end.y, (long int)ybot);
281
79.1M
    if_debug6m('L', cldev->memory, "    t%ld,%ld,%ld,%ld   %ld   o=%d\n",
282
79.1M
               (long int)right->start.x, (long int)right->start.y,
283
79.1M
               (long int)right->end.x, (long int)right->end.y, (long int)ytop,
284
79.1M
               options);
285
79.1M
    if (options & 2) {
286
19.5M
        cmd_putw(fa->clip->p.x, &dp);
287
19.5M
        cmd_putw(fa->clip->p.y, &dp);
288
19.5M
        cmd_putw(fa->clip->q.x, &dp);
289
19.5M
        cmd_putw(fa->clip->q.y, &dp);
290
19.5M
        cmd_putw(colors_mask, &dp);
291
19.5M
        if (c0 != NULL)
292
19.5M
            dp = cmd_put_frac31_color(cldev, c0, dp);
293
19.5M
        if (c1 != NULL)
294
19.5M
            dp = cmd_put_frac31_color(cldev, c1, dp);
295
19.5M
        if (c2 != NULL)
296
18.0M
            dp = cmd_put_frac31_color(cldev, c2, dp);
297
19.5M
        if (c3 != NULL)
298
0
            cmd_put_frac31_color(cldev, c3, dp);
299
19.5M
    }
300
79.1M
    return 0;
301
79.1M
}
302
303
/* ---------------- Driver procedures ---------------- */
304
305
int
306
clist_fillpage(gx_device * dev, gs_gstate *pgs, gx_drawing_color *pdcolor)
307
538k
{
308
538k
    gx_device_clist * const cldev = (gx_device_clist *)dev;
309
538k
    gx_device_clist_writer * const cdev = &(cldev->writer);
310
538k
    gx_clist_state * pcls;
311
538k
    int code;
312
313
    /* flush previous contents */
314
538k
    if ((code = clist_close_writer_and_init_reader(cldev)) < 0)
315
0
        return code;
316
538k
    if ((code = clist_finish_page(dev, true)) < 0)
317
0
        return code;
318
319
538k
    pcls = cdev->states; /* Use any. */
320
321
538k
    code = cmd_put_drawing_color(cdev, pcls, pdcolor, NULL, devn_not_tile_fill);
322
538k
    if (code >= 0)
323
538k
        code = cmd_write_page_rect_cmd(cdev, cmd_op_fill_rect);
324
538k
    return code;
325
538k
}
326
327
int
328
clist_fill_rectangle(gx_device * dev, int rx, int ry, int rwidth, int rheight,
329
                     gx_color_index color)
330
37.6M
{
331
37.6M
    gx_device_clist_writer * const cdev =
332
37.6M
        &((gx_device_clist *)dev)->writer;
333
37.6M
    int code;
334
37.6M
    cmd_rects_enum_t re;
335
37.6M
    gx_color_usage_bits color_usage;
336
337
37.6M
    crop_fill(cdev, rx, ry, rwidth, rheight);
338
37.6M
    if (rwidth <= 0 || rheight <= 0)
339
4.95k
        return 0;
340
37.6M
    if (cdev->permanent_error < 0)
341
0
      return (cdev->permanent_error);
342
    /* If needed, update the trans_bbox */
343
37.6M
    if (cdev->pdf14_needed) {
344
32.5M
        gs_int_rect bbox;
345
346
32.5M
        bbox.p.x = rx;
347
32.5M
        bbox.q.x = rx + rwidth - 1;
348
32.5M
        bbox.p.y = ry;
349
32.5M
        bbox.q.y = ry + rheight - 1;
350
351
32.5M
        clist_update_trans_bbox(cdev, &bbox);
352
32.5M
    }
353
37.6M
    RECT_ENUM_INIT(re, ry, rheight);
354
37.6M
    color_usage = gx_color_index2usage(dev, color);
355
37.9M
    do {
356
37.9M
        RECT_STEP_INIT(re);
357
37.9M
        re.pcls->color_usage.or |= color_usage;
358
37.9M
        code = cmd_disable_lop(cdev, re.pcls);
359
37.9M
        if (code >= 0 && color != re.pcls->colors[1]) {
360
27.2M
            code = cmd_put_color(cdev, re.pcls, &clist_select_color1,
361
27.2M
                                 color, &re.pcls->colors[1]);
362
27.2M
        }
363
37.9M
        if (code >= 0) {
364
37.9M
            code = cmd_write_rect_cmd(cdev, re.pcls, cmd_op_fill_rect, rx, re.y,
365
37.9M
                                      rwidth, re.height);
366
37.9M
        }
367
37.9M
        if (code < 0)
368
0
            return code;
369
37.9M
        re.y += re.height;
370
37.9M
    } while (re.y < re.yend);
371
37.6M
    return 0;
372
37.6M
}
373
374
/* This is used in fills from devn color types */
375
int
376
clist_fill_rectangle_hl_color(gx_device *dev, const gs_fixed_rect *rect,
377
    const gs_gstate *pgs, const gx_drawing_color *pdcolor,
378
    const gx_clip_path *pcpath)
379
22.1M
{
380
22.1M
    gx_device_clist_writer * const cdev =
381
22.1M
        &((gx_device_clist *)dev)->writer;
382
22.1M
    int code;
383
22.1M
    int rx, ry, rwidth, rheight;
384
22.1M
    cmd_rects_enum_t re;
385
22.1M
    gx_color_usage_bits color_usage = cmd_drawing_color_usage(cdev, pdcolor);
386
387
22.1M
    rx = fixed2int(rect->p.x);
388
22.1M
    ry = fixed2int(rect->p.y);
389
22.1M
    rwidth = fixed2int(rect->q.x) - rx;
390
22.1M
    rheight = fixed2int(rect->q.y) - ry;
391
392
22.1M
    crop_fill(cdev, rx, ry, rwidth, rheight);
393
22.1M
    if (rwidth <= 0 || rheight <= 0)
394
1.52k
        return 0;
395
22.1M
    if (cdev->permanent_error < 0)
396
0
      return (cdev->permanent_error);
397
    /* If needed, update the trans_bbox */
398
22.1M
    if (cdev->pdf14_needed) {
399
17.9M
        gs_int_rect bbox;
400
401
17.9M
        bbox.p.x = rx;
402
17.9M
        bbox.q.x = rx + rwidth - 1;
403
17.9M
        bbox.p.y = ry;
404
17.9M
        bbox.q.y = ry + rheight - 1;
405
406
17.9M
        clist_update_trans_bbox(cdev, &bbox);
407
17.9M
    }
408
22.1M
    RECT_ENUM_INIT(re, ry, rheight);
409
22.1M
    do {
410
22.1M
        RECT_STEP_INIT(re);
411
22.1M
        re.pcls->color_usage.or |= color_usage;
412
22.1M
        code = cmd_disable_lop(cdev, re.pcls);
413
22.1M
        code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re,
414
22.1M
                                     devn_not_tile_fill);
415
22.1M
        if (code >= 0) {
416
22.1M
            code = cmd_write_rect_hl_cmd(cdev, re.pcls, cmd_op_fill_rect_hl,
417
22.1M
                                         rx, re.y, rwidth, re.height, false);
418
22.1M
        }
419
22.1M
        if (code < 0)
420
0
            return code;
421
22.1M
        re.y += re.height;
422
22.1M
    } while (re.y < re.yend);
423
22.1M
    return 0;
424
22.1M
}
425
426
static inline int
427
clist_write_fill_trapezoid(gx_device * dev,
428
    const gs_fixed_edge *left, const gs_fixed_edge *right,
429
    fixed ybot, fixed ytop, int options,
430
    const gx_drawing_color *pdcolor, gs_logical_operation_t lop,
431
    const gs_fill_attributes *fa,
432
    const frac31 *c0, const frac31 *c1,
433
    const frac31 *c2, const frac31 *c3)
434
65.5M
{
435
65.5M
    gx_device_clist_writer * const cdev =
436
65.5M
        &((gx_device_clist *)dev)->writer;
437
65.5M
    int code;
438
65.5M
    cmd_rects_enum_t re;
439
65.5M
    int ry, rheight, rx, rxe;
440
65.5M
    bool swap_axes = (options & 1);
441
442
65.5M
    if (options & 4) {
443
4.66M
        if (swap_axes) {
444
0
            rx =  fixed2int(max(min(min(left->start.y, left->end.y), right->start.y), fa->clip->p.y));
445
0
            rxe =  fixed2int_ceiling(min(max(max(left->start.y, left->end.y), right->start.y), fa->clip->q.y));
446
0
            ry = fixed2int(max(min(min(left->start.x, left->end.x), right->start.x), fa->clip->p.x));
447
0
            rheight = fixed2int_ceiling(min(max(max(left->start.x, left->end.x), right->start.x), fa->clip->q.x)) - ry;
448
4.66M
        } else {
449
4.66M
            rx = fixed2int(max(min(min(left->start.x, left->end.x), right->start.x), fa->clip->p.x));
450
4.66M
            rxe = fixed2int_ceiling(min(max(max(left->start.x, left->end.x), right->start.x), fa->clip->q.x));
451
4.66M
            ry = fixed2int(max(min(min(left->start.y, left->end.y), right->start.y), fa->clip->p.y));
452
4.66M
            rheight = fixed2int_ceiling(min(max(max(left->start.y, left->end.y), right->start.y), fa->clip->q.y)) - ry;
453
4.66M
        }
454
60.8M
    } else {
455
        /* fixme: this may give a significant overestimation,
456
           so the command will be written to many bands.
457
           Would like to know a better range by X
458
           with computing intersections of sides with ybot, ytop. */
459
60.8M
        if (swap_axes) {
460
3.10M
            rx = fixed2int(ybot);
461
3.10M
            rxe = fixed2int_ceiling(ytop);
462
3.10M
            ry = fixed2int(min(left->start.x, left->end.x));
463
3.10M
            rheight = fixed2int_ceiling(max(right->start.x, right->end.x)) - ry;
464
57.7M
        } else {
465
57.7M
            rx = fixed2int(min(left->start.x, left->end.x));
466
57.7M
            rxe = fixed2int_ceiling(max(right->start.x, right->end.x));
467
57.7M
            ry = fixed2int(ybot);
468
57.7M
            rheight = fixed2int_ceiling(ytop) - ry;
469
57.7M
        }
470
60.8M
    }
471
65.5M
    crop_fill_y(cdev, ry, rheight);
472
65.5M
    if (rheight <= 0)
473
2.20M
        return 0;
474
63.3M
    if (cdev->permanent_error < 0)
475
0
        return (cdev->permanent_error);
476
    /* If needed, update the trans_bbox */
477
63.3M
    if (cdev->pdf14_needed) {
478
54
        gs_int_rect bbox;
479
480
54
        bbox.p.x = rx;
481
54
        bbox.q.x = rxe;
482
54
        bbox.p.y = ry;
483
54
        bbox.q.y = ry + rheight;
484
485
54
        clist_update_trans_bbox(cdev, &bbox);
486
54
    }
487
63.3M
    RECT_ENUM_INIT(re, ry, rheight);
488
79.1M
    do {
489
79.1M
        RECT_STEP_INIT(re);
490
79.1M
        if (pdcolor != NULL) {
491
59.5M
            code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re,
492
59.5M
                                         devn_not_tile_fill);
493
59.5M
            if (code == gs_error_unregistered)
494
0
                return code;
495
59.5M
            if (code < 0) {
496
                /* Something went wrong, use the default implementation. */
497
132
                return gx_default_fill_trapezoid(dev, left, right, ybot, ytop, swap_axes, pdcolor, lop);
498
132
            }
499
59.5M
            code = cmd_update_lop(cdev, re.pcls, lop);
500
59.5M
        } else {
501
            /* Even with pdcolor NULL, we may still have colors packed in
502
               c0, c1, c2 or c3 */
503
19.5M
            re.pcls->color_usage.or |= gx_color_usage_all(cdev);
504
19.5M
            code = 0;
505
19.5M
        }
506
79.1M
        if (code >= 0) {
507
            /* Don't want to shorten the trapezoid by the band boundary,
508
               keeping in mind a further optimization with writing same data to all bands. */
509
79.1M
            code = cmd_write_trapezoid_cmd(cdev, re.pcls, cmd_opv_fill_trapezoid, left, right,
510
79.1M
                                      ybot, ytop, options, fa, c0, c1, c2, c3);
511
79.1M
        }
512
79.1M
        if (code < 0)
513
0
            return code;
514
79.1M
        re.y += re.height;
515
79.1M
    } while (re.y < re.yend);
516
63.3M
    return 0;
517
63.3M
}
518
519
int
520
clist_fill_trapezoid(gx_device * dev,
521
    const gs_fixed_edge *left, const gs_fixed_edge *right,
522
    fixed ybot, fixed ytop, bool swap_axes,
523
    const gx_drawing_color *pdcolor, gs_logical_operation_t lop)
524
59.8M
{
525
59.8M
    return clist_write_fill_trapezoid(dev, left, right,
526
59.8M
        ybot, ytop, swap_axes, pdcolor, lop, NULL, NULL, NULL, NULL, NULL);
527
59.8M
}
528
529
int
530
clist_fill_linear_color_trapezoid(gx_device * dev, const gs_fill_attributes *fa,
531
        const gs_fixed_point *p0, const gs_fixed_point *p1,
532
        const gs_fixed_point *p2, const gs_fixed_point *p3,
533
        const frac31 *c0, const frac31 *c1,
534
        const frac31 *c2, const frac31 *c3)
535
960k
{
536
960k
    gs_fixed_edge left, right;
537
960k
    int code;
538
539
960k
    left.start = *p0;
540
960k
    left.end = *p1;
541
960k
    right.start = *p2;
542
960k
    right.end = *p3;
543
960k
    code = clist_write_fill_trapezoid(dev, &left, &right,
544
960k
        fa->ystart, fa->yend, fa->swap_axes | 2, NULL, fa->lop, fa, c0, c1, c2, c3);
545
960k
    if (code < 0)
546
0
        return code;
547
    /* NOTE : The return value 0 for the fill_linear_color_trapezoid method means
548
       that the device requests a further decomposition of the trapezoid.
549
       Currently we have no interface for checking whether the target device
550
       can handle a trapezoid without an attempt to fill it.
551
       Therefore the clist writer device must always return 1,
552
       and the clist reader must perform a further decomposition
553
       if the target device requests it.
554
     */
555
960k
    return 1;
556
960k
}
557
558
int
559
clist_fill_linear_color_triangle(gx_device * dev, const gs_fill_attributes *fa,
560
        const gs_fixed_point *p0, const gs_fixed_point *p1,
561
        const gs_fixed_point *p2,
562
        const frac31 *c0, const frac31 *c1, const frac31 *c2)
563
4.66M
{
564
4.66M
    gs_fixed_edge left, right;
565
4.66M
    int code;
566
567
4.66M
    left.start = *p0;
568
4.66M
    left.end = *p1;
569
4.66M
    right.start = *p2;
570
4.66M
    right.end.x = right.end.y = 0; /* unused. */
571
572
4.66M
    code = clist_write_fill_trapezoid(dev, &left, &right,
573
4.66M
        0, 0, fa->swap_axes | 2 | 4, NULL, fa->lop, fa, c0, c1, c2, NULL);
574
4.66M
    if (code < 0)
575
0
        return code;
576
    /* NOTE : The return value 0 for the fill_linear_color_triangle method means
577
       that the device requests a further decomposition of the trapezoid.
578
       Currently we have no interface for checking whether the target device
579
       can handle a trapezoid without an attempt to fill it.
580
       Therefore the clist writer device must always return 1,
581
       and the clist reader must perform a further decomposition
582
       if the target device requests it.
583
     */
584
4.66M
    return 1;
585
4.66M
}
586
587
extern dev_proc_open_device(pattern_clist_open_device);
588
extern dev_proc_dev_spec_op(pattern_accum_dev_spec_op);
589
590
int
591
clist_dev_spec_op(gx_device *pdev, int dev_spec_op, void *data, int size)
592
386M
{
593
386M
    gx_device_clist_common * const cdev = &((gx_device_clist *)pdev)->common;
594
595
386M
    if (dev_spec_op == gxdso_pattern_handles_clip_path)
596
1.05k
        return 1;
597
386M
    if (dev_spec_op == gxdso_pattern_shfill_doesnt_need_path)
598
560
        return 1;
599
386M
    if (dev_spec_op == gxdso_copy_alpha_disabled) {
600
0
        gx_device_clist_writer * const cdev = &((gx_device_clist *)pdev)->writer;
601
0
        return (cdev->disable_mask & clist_disable_copy_alpha) != 0;
602
0
    }
603
386M
    if (dev_spec_op == gxdso_supports_devn
604
367M
     || dev_spec_op == gxdso_skip_icc_component_validation) {
605
18.9M
        cmm_dev_profile_t *dev_profile;
606
18.9M
        int code;
607
18.9M
        code = dev_proc(cdev, get_profile)((gx_device*) cdev, &dev_profile);
608
18.9M
        if (code == 0) {
609
18.9M
            return dev_profile->supports_devn;
610
18.9M
        } else {
611
0
            return 0;
612
0
        }
613
18.9M
    }
614
367M
    if (dev_spec_op == gxdso_restrict_bbox) {
615
0
        gx_device_clist_writer *cwdev = &((gx_device_clist *)pdev)->writer;
616
0
        gs_int_rect *ibox = (gs_int_rect *)data;
617
0
        if (ibox->p.y < cwdev->cropping_min)
618
0
            ibox->p.y = cwdev->cropping_min;
619
0
        if (ibox->q.y > cwdev->cropping_max)
620
0
            ibox->q.y = cwdev->cropping_max;
621
0
        return 0;
622
0
    }
623
367M
    if (dev_spec_op == gxdso_is_clist_device)
624
9.18k
        return 1;
625
367M
    if (dev_spec_op == gxdso_overprint_active) {
626
83.6M
        gx_device_clist_writer* cwdev = &((gx_device_clist*)pdev)->writer;
627
83.6M
        if (cwdev->op_fill_active || cwdev->op_stroke_active)
628
10.8k
            return 1;
629
        /* Fall through to pass through cases. */
630
83.6M
    }
631
632
367M
    if (dev_spec_op == gxdso_overprint_adjust) {
633
378k
        gx_device_clist_writer *cwdev = &((gx_device_clist *)pdev)->writer;
634
378k
        if (data == NULL || size != sizeof(gx_overprint_adjust_t))
635
0
            return gs_error_unknownerror;
636
378k
        ((gx_overprint_adjust_t *)data)->op_was_active |= (cwdev->op_fill_active || cwdev->op_stroke_active);
637
        /* Fall through to pass through cases. ick. */
638
378k
    }
639
    /* This is a horrible hack. Accumulator devices have their procs
640
     * overriden by clist ones in gdev_prn_open. */
641
367M
    if (strncmp(pdev->dname, "pdf14-accum-", 12) == 0) {
642
6.77M
        return pdf14_accum_dev_spec_op(pdev, dev_spec_op, data, size);
643
6.77M
    }
644
    /* forward to the appropriate super class */
645
360M
    if (cdev->orig_spec_op)
646
360M
        return cdev->orig_spec_op(pdev, dev_spec_op, data, size);
647
3.22k
    if (dev_proc(cdev, open_device) == pattern_clist_open_device)
648
3.22k
        return pattern_accum_dev_spec_op(pdev, dev_spec_op, data, size);
649
0
    return gx_default_dev_spec_op(pdev, dev_spec_op, data, size);
650
3.22k
}
651
652
/* Based heavily off of clist_strip_tile_rectangle */
653
int
654
clist_strip_tile_rect_devn(gx_device * dev, const gx_strip_bitmap * tile,
655
                           int rx, int ry, int rwidth, int rheight,
656
                           const gx_drawing_color *pdcolor0,
657
                           const gx_drawing_color *pdcolor1, int px, int py)
658
5.83k
{
659
5.83k
    gx_device_clist_writer * const cdev =
660
5.83k
        &((gx_device_clist *)dev)->writer;
661
5.83k
    int depth = 1;
662
5.83k
    int code;
663
5.83k
    cmd_rects_enum_t re;
664
5.83k
    gx_color_usage_bits color_usage = cmd_drawing_color_usage(cdev, pdcolor0);
665
666
5.83k
    crop_fill(cdev, rx, ry, rwidth, rheight);
667
5.83k
    if (rwidth <= 0 || rheight <= 0)
668
0
        return 0;
669
5.83k
    if (cdev->permanent_error < 0)
670
0
      return (cdev->permanent_error);
671
    /* If needed, update the trans_bbox */
672
5.83k
    if (cdev->pdf14_needed) {
673
539
        gs_int_rect bbox;
674
675
539
        bbox.p.x = rx;
676
539
        bbox.q.x = rx + rwidth - 1;
677
539
        bbox.p.y = ry;
678
539
        bbox.q.y = ry + rheight - 1;
679
680
539
        clist_update_trans_bbox(cdev, &bbox);
681
539
    }
682
5.83k
    color_usage |= cmd_drawing_color_usage(cdev, pdcolor1);
683
5.83k
    RECT_ENUM_INIT(re, ry, rheight);
684
6.07k
    do {
685
6.07k
        ulong offset_temp;
686
687
6.07k
        RECT_STEP_INIT(re);
688
6.07k
        re.pcls->color_usage.or |= color_usage;
689
6.07k
        code = cmd_disable_lop(cdev, re.pcls);
690
6.07k
        if (code < 0)
691
0
            return code;
692
        /* Change the tile if needed */
693
6.07k
        if (!cls_has_tile_id(cdev, re.pcls, tile->id, offset_temp)) {
694
28
            if (tile->id != gx_no_bitmap_id) {
695
28
                code = clist_change_tile(cdev, re.pcls, tile, depth);
696
28
                if (code < 0)
697
0
                {
698
0
                    if (code == gs_error_limitcheck)
699
0
                    {
700
0
                        code = gx_default_strip_tile_rect_devn(dev, tile,
701
0
                                                               rx, re.y, rwidth, re.height,
702
0
                                                               pdcolor0, pdcolor1,
703
0
                                                               px, py);
704
0
                        if (code <= 0)
705
0
                            goto loop;
706
0
                    }
707
0
                    return code;
708
0
                }
709
28
            } else {
710
0
                return_error(gs_error_unregistered);
711
0
            }
712
28
        }
713
6.07k
        code = 0;
714
        /* Write out the devn colors */
715
6.07k
        code = cmd_put_drawing_color(cdev, re.pcls, pdcolor0, &re,
716
6.07k
                                     devn_tile0);
717
6.07k
        code = cmd_put_drawing_color(cdev, re.pcls, pdcolor1, &re,
718
6.07k
                                     devn_tile1);
719
        /* Set the tile phase */
720
6.07k
        if (px != re.pcls->tile_phase.x || py != re.pcls->tile_phase.y) {
721
5.49k
            if (code >= 0)
722
5.49k
                code = cmd_set_tile_phase(cdev, re.pcls, px, py);
723
5.49k
        }
724
        /* Write out the actually command to fill with the devn colors */
725
6.07k
        if (code >= 0) {
726
6.07k
            code = cmd_write_rect_hl_cmd(cdev, re.pcls,
727
6.07k
                                         cmd_opv_ext_tile_rect_hl, rx, re.y,
728
6.07k
                                         rwidth, re.height, true);
729
6.07k
        }
730
6.07k
        if (code < 0)
731
0
            return code;
732
6.07k
loop:
733
6.07k
        re.y += re.height;
734
6.07k
    } while (re.y < re.yend);
735
5.83k
    return 0;
736
5.83k
}
737
738
int
739
clist_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tile,
740
                           int rx, int ry, int rwidth, int rheight,
741
               gx_color_index color0, gx_color_index color1, int px, int py)
742
2.14M
{
743
2.14M
    gx_device_clist_writer * const cdev =
744
2.14M
        &((gx_device_clist *)dev)->writer;
745
2.14M
    int depth =
746
2.14M
        (color1 == gx_no_color_index && color0 == gx_no_color_index ?
747
2.14M
         cdev->clist_color_info.depth : 1);
748
2.14M
    gx_color_usage_bits color_usage =
749
2.14M
        (color1 == gx_no_color_index && color0 == gx_no_color_index ?
750
         /* We can't know what colors will be used: assume the worst. */
751
165
         gx_color_usage_all(dev) :
752
2.14M
         (color0 == gx_no_color_index ? 0 : gx_color_index2usage(dev, color0)) |
753
2.14M
         (color1 == gx_no_color_index ? 0 : gx_color_index2usage(dev, color1)));
754
2.14M
    int code;
755
2.14M
    cmd_rects_enum_t re;
756
757
2.14M
    crop_fill(cdev, rx, ry, rwidth, rheight);
758
2.14M
    if (rwidth <= 0 || rheight <= 0)
759
0
        return 0;
760
2.14M
    if (cdev->permanent_error < 0)
761
0
        return (cdev->permanent_error);
762
    /* If needed, update the trans_bbox */
763
2.14M
    if (cdev->pdf14_needed) {
764
3.78k
        gs_int_rect bbox;
765
766
3.78k
        bbox.p.x = rx;
767
3.78k
        bbox.q.x = rx + rwidth - 1;
768
3.78k
        bbox.p.y = ry;
769
3.78k
        bbox.q.y = ry + rheight - 1;
770
771
3.78k
        clist_update_trans_bbox(cdev, &bbox);
772
3.78k
    }
773
2.14M
    RECT_ENUM_INIT(re, ry, rheight);
774
2.15M
    do {
775
2.15M
        ulong offset_temp;
776
777
2.15M
        RECT_STEP_INIT(re);
778
2.15M
        re.pcls->color_usage.or |= color_usage;
779
2.15M
        code = cmd_disable_lop(cdev, re.pcls);
780
2.15M
        if (code < 0)
781
0
            return code;
782
2.15M
        if (!cls_has_tile_id(cdev, re.pcls, tile->id, offset_temp)) {
783
585k
            if (tile->id != gx_no_bitmap_id) {
784
585k
                code = clist_change_tile(cdev, re.pcls, tile, depth);
785
585k
            } else
786
0
                code = -1; /* Force the default implementation. Should never happen. */
787
585k
            if (code < 0) {
788
                /* ok if gx_default... does retries internally: */
789
                /* it's self-sufficient */
790
0
                code = gx_default_strip_tile_rectangle(dev, tile,
791
0
                                                       rx, re.y, rwidth, re.height,
792
0
                                                       color0, color1,
793
0
                                                       px, py);
794
0
                if (code < 0)
795
0
                    return code;
796
0
                goto endr;
797
0
            }
798
585k
        }
799
2.15M
        code = 0;
800
2.15M
        if (color0 != re.pcls->tile_colors[0] || color1 != re.pcls->tile_colors[1])
801
129k
            code = cmd_set_tile_colors(cdev, re.pcls, color0, color1);
802
2.15M
        if (px != re.pcls->tile_phase.x || py != re.pcls->tile_phase.y) {
803
26.6k
            if (code >= 0)
804
26.6k
                code = cmd_set_tile_phase(cdev, re.pcls, px, py);
805
26.6k
        }
806
2.15M
        if (code >= 0)
807
2.15M
            code = cmd_write_rect_cmd(cdev, re.pcls, cmd_op_tile_rect, rx, re.y,
808
2.15M
                                      rwidth, re.height);
809
2.15M
        if (code < 0)
810
0
            return code;
811
2.15M
endr:;
812
2.15M
        re.y += re.height;
813
2.15M
    } while (re.y < re.yend);
814
2.14M
    return 0;
815
2.14M
}
816
817
int
818
clist_copy_mono(gx_device * dev,
819
                const byte * data, int data_x, int raster, gx_bitmap_id id,
820
                int rx, int ry, int rwidth, int rheight,
821
                gx_color_index color0, gx_color_index color1)
822
487k
{
823
487k
    gx_device_clist_writer * const cdev =
824
487k
        &((gx_device_clist *)dev)->writer;
825
487k
    int y0;
826
487k
    gx_bitmap_id orig_id = id;
827
487k
    gx_color_usage_bits color_usage =
828
487k
        (color0 == gx_no_color_index ? 0 : gx_color_index2usage(dev, color0)) |
829
487k
        (color1 == gx_no_color_index ? 0 : gx_color_index2usage(dev, color1));
830
487k
    cmd_rects_enum_t re;
831
832
487k
    fit_copy(dev, data, data_x, raster, id, rx, ry, rwidth, rheight);
833
483k
    y0 = ry;
834
483k
    if (cdev->permanent_error < 0)
835
0
      return (cdev->permanent_error);
836
    /* If needed, update the trans_bbox */
837
483k
    if (cdev->pdf14_needed) {
838
9.54k
        gs_int_rect bbox;
839
840
9.54k
        bbox.p.x = rx;
841
9.54k
        bbox.q.x = rx + rwidth - 1;
842
9.54k
        bbox.p.y = ry;
843
9.54k
        bbox.q.y = ry + rheight - 1;
844
845
9.54k
        clist_update_trans_bbox(cdev, &bbox);
846
9.54k
    }
847
483k
    RECT_ENUM_INIT(re, ry, rheight);
848
506k
    do {
849
506k
        int dx = data_x & 7;
850
506k
        int w1 = dx + rwidth;
851
506k
        const byte *row = data + (re.y - y0) * raster + (data_x >> 3);
852
506k
        int code;
853
854
506k
        RECT_STEP_INIT(re);
855
506k
        re.pcls->color_usage.or |= color_usage;
856
506k
        code = cmd_disable_lop(cdev, re.pcls);
857
506k
        if (code >= 0)
858
506k
            code = cmd_disable_clip(cdev, re.pcls);
859
506k
        if (color0 != re.pcls->colors[0] && code >= 0)
860
1.78k
            code = cmd_set_color0(cdev, re.pcls, color0);
861
506k
        if (color1 != re.pcls->colors[1] && code >= 0)
862
13.9k
            code = cmd_set_color1(cdev, re.pcls, color1);
863
506k
        if (code < 0)
864
0
            return code;
865
        /* Don't bother to check for a possible cache hit: */
866
        /* tile_rectangle and fill_mask handle those cases. */
867
513k
copy:{
868
513k
        gx_cmd_rect rect;
869
513k
        int rsize;
870
513k
        byte op = (byte) cmd_op_copy_mono_planes;
871
513k
        byte *dp;
872
513k
        uint csize;
873
513k
        uint compress;
874
513k
        int code;
875
876
513k
        rect.x = rx, rect.y = re.y;
877
513k
        rect.width = w1, rect.height = re.height;
878
513k
        rsize = (dx ? 3 : 1) + cmd_size_rect(&rect) + cmd_sizew(0); /* planar_height 0 */
879
513k
        code = cmd_put_bits(cdev, re.pcls, row, w1, re.height, raster,
880
513k
                            rsize, (orig_id == gx_no_bitmap_id ?
881
513k
                                    1 << cmd_compress_rle :
882
513k
                                    cmd_mask_compress_any),
883
513k
                            &dp, &csize);
884
513k
        if (code < 0 && !(code == gs_error_limitcheck))
885
0
            return code;
886
513k
        compress = (uint)code;
887
513k
        if (code < 0) {
888
            /* The bitmap was too large; split up the transfer. */
889
7.23k
            if (re.height > 1) {
890
                /*
891
                 * Split the transfer by reducing the height.
892
                 * See the comment above FOR_RECTS in gxcldev.h.
893
                 */
894
7.23k
                re.height >>= 1;
895
7.23k
                goto copy;
896
7.23k
            } else {
897
                /* Split a single (very long) row. */
898
0
                int w2 = w1 >> 1;
899
900
0
                code = clist_copy_mono(dev, row, dx,
901
0
                                       raster, gx_no_bitmap_id, rx, re.y,
902
0
                                       w2, 1, color0, color1);
903
0
                if (code >= 0)
904
0
                    code = clist_copy_mono(dev, row, dx + w2,
905
0
                                           raster, gx_no_bitmap_id,
906
0
                                           rx + w2, re.y,
907
0
                                           w1 - w2, 1, color0, color1);
908
0
                if (code < 0)
909
0
                    return code;
910
0
                continue;
911
0
            }
912
7.23k
        }
913
506k
        op += compress;
914
506k
        if (dx) {
915
88.8k
            if_debug0m('L', dev->memory,
916
88.8k
                       "[L] fake end_run: really set_misc_data_x[2]\n");
917
88.8k
            *dp++ = cmd_count_op(cmd_opv_set_misc, 2, dev->memory);
918
88.8k
            *dp++ = cmd_set_misc_data_x + dx;
919
88.8k
        }
920
#ifdef DEBUG
921
        if (gs_debug_c('L')) {
922
            const char *const *sub = cmd_sub_op_names[op >> 4];
923
924
            if (sub)
925
                dmlprintf1(dev->memory, "[L] fake end_run: really %s\n", sub[op & 0xf]);
926
            else
927
                dmlprintf2(dev->memory, "[L] fake end_run: really %s %d\n", cmd_op_names[op >> 4], op & 0xf);
928
        }
929
#endif
930
506k
        *dp++ = cmd_count_op(op, csize, dev->memory);
931
        /* Store the plane_height */
932
506k
        cmd_putw(0, &dp);
933
506k
        cmd_put2w(rx, re.y, &dp);
934
506k
        cmd_put2w(w1, re.height, &dp);
935
506k
        re.pcls->rect = rect;
936
506k
        }
937
506k
    } while ((re.y += re.height) < re.yend);
938
483k
    return 0;
939
483k
}
940
941
/* The code duplication between this and clist_copy_mono needs to be removed */
942
int
943
clist_copy_planes(gx_device * dev,
944
                  const byte * data, int data_x, int raster, gx_bitmap_id id,
945
                  int rx, int ry, int rwidth, int rheight, int plane_height)
946
13.2k
{
947
13.2k
    gx_device_clist_writer * const cdev =
948
13.2k
        &((gx_device_clist *)dev)->writer;
949
13.2k
    int y0;
950
13.2k
    gx_bitmap_id orig_id = id;
951
13.2k
    cmd_rects_enum_t re;
952
13.2k
    int bpc = dev->color_info.depth / dev->color_info.num_components;
953
13.2k
    int pix_in_byte = bpc <= 8 ? 8/bpc : 1;
954
13.2k
    int byte_in_pix = bpc >= 8 ? bpc/8 : 1;
955
956
13.2k
    if (rwidth <= 0 || rheight <= 0)
957
0
        return 0;
958
959
13.2k
    fit_copy(dev, data, data_x, raster, id, rx, ry, rwidth, rheight);
960
961
13.2k
    y0 = ry;
962
13.2k
    if (cdev->permanent_error < 0)
963
0
      return (cdev->permanent_error);
964
965
#ifdef DEBUG
966
    if (plane_height == 0) {
967
        dprintf("clist_copy_planes called with plane_height == 0.\n");
968
    }
969
#endif
970
971
    /* If needed, update the trans_bbox */
972
13.2k
    if (cdev->pdf14_needed) {
973
2.45k
        gs_int_rect bbox;
974
975
2.45k
        bbox.p.x = rx;
976
2.45k
        bbox.q.x = rx + rwidth - 1;
977
2.45k
        bbox.p.y = ry;
978
2.45k
        bbox.q.y = ry + rheight - 1;
979
980
2.45k
        clist_update_trans_bbox(cdev, &bbox);
981
2.45k
    }
982
983
13.2k
    RECT_ENUM_INIT(re, ry, rheight);
984
13.2k
    do {
985
13.2k
        int code;
986
13.2k
        gx_cmd_rect rect;
987
13.2k
        int rsize;
988
13.2k
        byte *dp, *dp2;
989
13.2k
        uint csize;
990
13.2k
        byte op = (byte) cmd_op_copy_mono_planes;
991
13.2k
        int dx = data_x % pix_in_byte;
992
13.2k
        int w1 = dx + rwidth;
993
13.2k
        const byte *row = data + (re.y - y0) * raster + (data_x * byte_in_pix / pix_in_byte);
994
13.2k
        int bytes_row = ((w1*bpc+7)/8 + 7) & ~7;
995
13.2k
        int maxheight = data_bits_size / bytes_row / cdev->color_info.num_components;
996
13.2k
        int plane;
997
998
13.2k
        RECT_STEP_INIT(re);
999
13.2k
        code = cmd_disable_lop(cdev, re.pcls);
1000
13.2k
        if (code >= 0)
1001
13.2k
            code = cmd_disable_clip(cdev, re.pcls);
1002
13.2k
        if (code < 0)
1003
0
            return code;
1004
        /* Don't bother to check for a possible cache hit: */
1005
        /* tile_rectangle and fill_mask handle those cases. */
1006
1007
        /* We require that in the copy_planes case all the planes fit into
1008
         * the data_bits area (for the copy_planes call by the clist reader) */
1009
13.2k
        if (re.height > maxheight)
1010
0
            re.height = maxheight;
1011
1012
13.2k
        if (re.height == 0) {
1013
            /* Not even a single row fits. Revert to a single row and split it in two recursively */
1014
0
            int w2 = w1 >> 1;
1015
1016
0
            re.height = 1;
1017
0
            code = clist_copy_planes(dev, row, dx, raster,
1018
0
                                     gx_no_bitmap_id, rx, re.y,
1019
0
                                     w2, 1, plane_height);
1020
0
            if (code >= 0)
1021
0
                code = clist_copy_planes(dev, row, dx + w2,
1022
0
                                         raster, gx_no_bitmap_id,
1023
0
                                         rx + w2, re.y,
1024
0
                                         w1 - w2, 1, plane_height);
1025
0
            if (code < 0)
1026
0
                return code;
1027
0
            continue;
1028
0
        }
1029
1030
        /* 0x100 fudge is arbitrary, but the BufferSpace is large w.r.t. cbuf size so it doesn't matter */
1031
13.2k
        if ((cdev->cend - cdev->cnext) < 0x100 + ((long)cdev->color_info.num_components * re.height * bytes_row))
1032
5
            cmd_write_buffer(cdev, cmd_opv_end_run); /* Insure that all planes fit in the bufferspace */
1033
1034
13.2k
        rect.x = rx, rect.y = re.y;
1035
13.2k
        rect.width = w1, rect.height = re.height;
1036
13.2k
        rsize = (dx ? 3 : 1) + cmd_size_rect(&rect) + cmd_sizew(plane_height);
1037
        /* Copy the 0th plane - this is the one the op goes in. */
1038
13.2k
        code = cmd_put_bits(cdev, re.pcls, row, w1*bpc, re.height, raster,
1039
13.2k
                            rsize, (bpc == 1 ? (orig_id == gx_no_bitmap_id ?
1040
8.59k
                                                1 << cmd_compress_rle :
1041
8.59k
                                                cmd_mask_compress_any) : 0),
1042
13.2k
                            &dp, &csize);
1043
13.2k
        if (code < 0)
1044
0
            continue;
1045
        /* Write the command header out now, in case the following
1046
         * cmd_put_bits fill the buffer up. */
1047
13.2k
        dp2 = dp;
1048
13.2k
        if (dx) {
1049
3.01k
            if_debug0m('L', cdev->memory,
1050
3.01k
                       "[L] fake end_run: really set_misc_data_x[2]\n");
1051
3.01k
            *dp2++ = cmd_count_op(cmd_opv_set_misc, 2, cdev->memory);
1052
3.01k
            *dp2++ = cmd_set_misc_data_x + dx;
1053
3.01k
        }
1054
#ifdef DEBUG
1055
        if (gs_debug_c('L')) {
1056
            const char *const *sub = cmd_sub_op_names[(op+code) >> 4];
1057
1058
            if (sub)
1059
                dmlprintf1(cdev->memory, "[L] fake end_run: really %s\n", sub[(op+code) & 0xf]);
1060
            else
1061
                dmlprintf2(cdev->memory, "[L] fake end_run: really %s %d\n", cmd_op_names[(op+code) >> 4], (op+code) & 0xf);
1062
        }
1063
#endif
1064
13.2k
        *dp2++ = cmd_count_op(op + code, csize, cdev->memory);
1065
13.2k
        cmd_putw(plane_height, &dp2);
1066
13.2k
        cmd_put2w(rx, re.y, &dp2);
1067
13.2k
        cmd_put2w(w1, re.height, &dp2);
1068
52.8k
        for (plane = 1; plane < cdev->color_info.num_components && (code >= 0); plane++)
1069
39.6k
        {
1070
39.6k
            byte *dummy_dp;
1071
39.6k
            uint csize2;
1072
            /* Copy subsequent planes - 1 byte header used to send the
1073
             * compression type. */
1074
39.6k
            code = cmd_put_bits(cdev, re.pcls,
1075
39.6k
                                row + plane_height * raster * plane,
1076
39.6k
                                w1*bpc, re.height, raster, 1,
1077
39.6k
                                (bpc == 1 ?
1078
25.7k
                                 (orig_id == gx_no_bitmap_id ?
1079
25.7k
                                  1 << cmd_compress_rle :
1080
25.7k
                                  cmd_mask_compress_any) : 0),
1081
39.6k
                                &dummy_dp, &csize2);
1082
39.6k
            if (code >= 0)
1083
39.6k
                *dummy_dp = code;
1084
1085
39.6k
            csize += csize2;
1086
39.6k
        }
1087
13.2k
        if (code < 0 && !(code == gs_error_limitcheck))
1088
0
            return code;
1089
1090
13.2k
        re.pcls->rect = rect;
1091
13.2k
    } while ((re.y += re.height) < re.yend);
1092
13.2k
    return 0;
1093
13.2k
}
1094
1095
int
1096
clist_copy_color(gx_device * dev,
1097
                 const byte * data, int data_x, int raster, gx_bitmap_id id,
1098
                 int rx, int ry, int rwidth, int rheight)
1099
2.93M
{
1100
2.93M
    gx_device_clist_writer * const cdev =
1101
2.93M
        &((gx_device_clist *)dev)->writer;
1102
2.93M
    int depth = cdev->clist_color_info.depth; /* Could be different than target
1103
                                                 if 1.4 device */
1104
2.93M
    int y0;
1105
2.93M
    int data_x_bit;
1106
    /* We can't know what colors will be used: assume the worst. */
1107
2.93M
    gx_color_usage_bits all = gx_color_usage_all(cdev);
1108
2.93M
    cmd_rects_enum_t re;
1109
1110
2.93M
    fit_copy(dev, data, data_x, raster, id, rx, ry, rwidth, rheight);
1111
2.93M
    y0 = ry;
1112
2.93M
    data_x_bit = data_x * depth;
1113
2.93M
    if (cdev->permanent_error < 0)
1114
0
      return (cdev->permanent_error);
1115
    /* If needed, update the trans_bbox */
1116
2.93M
    if (cdev->pdf14_needed) {
1117
0
        gs_int_rect bbox;
1118
1119
0
        bbox.p.x = rx;
1120
0
        bbox.q.x = rx + rwidth - 1;
1121
0
        bbox.p.y = ry;
1122
0
        bbox.q.y = ry + rheight - 1;
1123
1124
0
        clist_update_trans_bbox(cdev, &bbox);
1125
0
    }
1126
2.93M
    RECT_ENUM_INIT(re, ry, rheight);
1127
2.93M
    do {
1128
2.93M
        int dx = (data_x_bit & 7) / depth;
1129
2.93M
        int w1 = dx + rwidth;
1130
2.93M
        const byte *row = data + (re.y - y0) * raster + (data_x_bit >> 3);
1131
2.93M
        int code;
1132
1133
2.93M
        RECT_STEP_INIT(re);
1134
2.93M
        re.pcls->color_usage.or |= all;
1135
1136
2.93M
        code = cmd_disable_lop(cdev, re.pcls);
1137
2.93M
        if (code >= 0)
1138
2.93M
            code = cmd_disable_clip(cdev, re.pcls);
1139
2.93M
        if (code < 0)
1140
0
            return code;
1141
2.93M
        if (re.pcls->color_is_alpha) {
1142
0
            byte *dp;
1143
1144
0
            code =
1145
0
                set_cmd_put_op(&dp, cdev, re.pcls, cmd_opv_set_copy_color, 1);
1146
0
            if (code < 0)
1147
0
                return code;
1148
0
            re.pcls->color_is_alpha = 0;
1149
0
        }
1150
2.93M
copy:{
1151
2.93M
            gx_cmd_rect rect;
1152
2.93M
            int rsize;
1153
2.93M
            byte op = (byte) cmd_op_copy_color_alpha;
1154
2.93M
            byte *dp;
1155
2.93M
            uint csize;
1156
2.93M
            uint compress;
1157
1158
2.93M
            rect.x = rx, rect.y = re.y;
1159
2.93M
            rect.width = w1, rect.height = re.height;
1160
2.93M
            rsize = (dx ? 3 : 1) + cmd_size_rect(&rect);
1161
2.93M
            code = cmd_put_bits(cdev, re.pcls, row, w1 * depth,
1162
2.93M
                                re.height, raster, rsize,
1163
2.93M
                                1 << cmd_compress_rle, &dp, &csize);
1164
2.93M
            if (code < 0 && !(code == gs_error_limitcheck))
1165
0
                return code;
1166
2.93M
            compress = (uint)code;
1167
2.93M
            if (code < 0) {
1168
                /* The bitmap was too large; split up the transfer. */
1169
0
                if (re.height > 1) {
1170
                    /* Split the transfer by reducing the height.
1171
                     * See the comment above FOR_RECTS in gxcldev.h.
1172
                     */
1173
0
                    re.height >>= 1;
1174
0
                    goto copy;
1175
0
                } else {
1176
                    /* Split a single (very long) row. */
1177
0
                    int w2 = w1 >> 1;
1178
1179
0
                    code = clist_copy_color(dev, row, dx,
1180
0
                                            raster, gx_no_bitmap_id,
1181
0
                                            rx, re.y, w2, 1);
1182
0
                    if (code >= 0)
1183
0
                        code = clist_copy_color(dev, row, dx + w2,
1184
0
                                                raster, gx_no_bitmap_id,
1185
0
                                                rx + w2, re.y, w1 - w2, 1);
1186
0
                    if (code < 0)
1187
0
                        return code;
1188
0
                    continue;
1189
0
                }
1190
0
            }
1191
2.93M
            op += compress;
1192
2.93M
            if (dx) {
1193
172k
                if_debug0m('L', dev->memory,
1194
172k
                           "[L] fake end_run: really set_misc_data_x[2]\n");
1195
172k
                *dp++ = cmd_count_op(cmd_opv_set_misc, 2, dev->memory);
1196
172k
                *dp++ = cmd_set_misc_data_x + dx;
1197
172k
            }
1198
#ifdef DEBUG
1199
            if (gs_debug_c('L')) {
1200
                const char *const *sub = cmd_sub_op_names[op >> 4];
1201
1202
                if (sub)
1203
                    dmlprintf1(dev->memory, "[L] fake end_run: really %s\n", sub[op & 0xf]);
1204
                else
1205
                    dmlprintf2(dev->memory, "[L] fake end_run: really %s %d\n", cmd_op_names[op >> 4], op & 0xf);
1206
            }
1207
#endif
1208
2.93M
            *dp++ = cmd_count_op(op, csize, dev->memory);
1209
2.93M
            cmd_put2w(rx, re.y, &dp);
1210
2.93M
            cmd_put2w(w1, re.height, &dp);
1211
2.93M
            re.pcls->rect = rect;
1212
2.93M
        }
1213
2.93M
    } while ((re.y += re.height) < re.yend);
1214
2.93M
    return 0;
1215
2.93M
}
1216
1217
/* Patterned after clist_copy_alpha and sharing a command with
1218
   cmd_op_copy_color_alpha.  This was done due to avoid difficult
1219
   to follow code in the read back logic in gxclrast.c.  Due to the
1220
   recursive nature of this call, it would be a bit painful to do much
1221
   sharing between clist_copy_alpha_hl_color and clist_copy_alpha */
1222
int
1223
clist_copy_alpha_hl_color(gx_device * dev, const byte * data, int data_x,
1224
           int raster, gx_bitmap_id id, int rx, int ry, int rwidth, int rheight,
1225
                 const gx_drawing_color *pdcolor, int depth)
1226
0
{
1227
0
    gx_device_clist_writer * const cdev =
1228
0
        &((gx_device_clist *)dev)->writer;
1229
1230
0
    int log2_depth = ilog2(depth);
1231
0
    int y0;
1232
0
    int data_x_bit;
1233
0
    cmd_rects_enum_t re;
1234
0
    gx_color_usage_bits all = gx_color_usage_all(cdev);
1235
1236
    /* If the target can't perform copy_alpha, exit now */
1237
0
    if (depth > 1 && (cdev->disable_mask & clist_disable_copy_alpha) != 0)
1238
0
        return_error(gs_error_unknownerror);
1239
1240
0
    fit_copy(dev, data, data_x, raster, id, rx, ry, rwidth, rheight);
1241
0
    y0 = ry;
1242
0
    data_x_bit = data_x << log2_depth;
1243
0
    if (cdev->permanent_error < 0)
1244
0
      return (cdev->permanent_error);
1245
    /* If needed, update the trans_bbox */
1246
0
    if (cdev->pdf14_needed) {
1247
0
        gs_int_rect bbox;
1248
1249
0
        bbox.p.x = rx;
1250
0
        bbox.q.x = rx + rwidth - 1;
1251
0
        bbox.p.y = ry;
1252
0
        bbox.q.y = ry + rheight - 1;
1253
1254
0
        clist_update_trans_bbox(cdev, &bbox);
1255
0
    }
1256
0
    RECT_ENUM_INIT(re, ry, rheight);
1257
0
    do {
1258
0
        int dx = (data_x_bit & 7) >> log2_depth;
1259
0
        int w1 = dx + rwidth;
1260
0
        const byte *row = data + (re.y - y0) * raster + (data_x_bit >> 3);
1261
0
        int code;
1262
1263
0
        RECT_STEP_INIT(re);
1264
0
        re.pcls->color_usage.or = all;
1265
0
        code = cmd_disable_lop(cdev, re.pcls);
1266
0
        if (code >= 0)
1267
0
            code = cmd_disable_clip(cdev, re.pcls);
1268
0
        if (code < 0)
1269
0
            return code;
1270
0
        if (!re.pcls->color_is_alpha) {
1271
0
            byte *dp;
1272
1273
0
            code = set_cmd_put_op(&dp, cdev, re.pcls, cmd_opv_set_copy_alpha, 1);
1274
0
            if (code < 0)
1275
0
                return code;
1276
0
            re.pcls->color_is_alpha = 1;
1277
0
        }
1278
        /* Set extended command for overload of copy_color_alpha with devn type */
1279
0
        if (!re.pcls->color_is_devn) {
1280
0
            byte *dp;
1281
1282
0
            code = set_cmd_put_extended_op(&dp, cdev, re.pcls, cmd_opv_ext_set_color_is_devn, 2);
1283
0
            dp += 2;
1284
0
            if (code < 0)
1285
0
                  return code;
1286
0
            re.pcls->color_is_alpha = 1;
1287
0
        }
1288
        /* Set the color */
1289
0
        code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re, devn_not_tile_fill);
1290
0
copy:{
1291
0
            gx_cmd_rect rect;
1292
0
            int rsize;
1293
0
            byte op = (byte) cmd_op_copy_color_alpha;
1294
0
            byte *dp;
1295
0
            uint csize;
1296
0
            uint compress;
1297
1298
0
            rect.x = rx, rect.y = re.y;
1299
0
            rect.width = w1, rect.height = re.height;
1300
0
            rsize = (dx ? 4 : 2) + cmd_size_rect(&rect);
1301
0
            code = cmd_put_bits(cdev, re.pcls, row, w1 << log2_depth,
1302
0
                                re.height, raster, rsize,
1303
0
                                1 << cmd_compress_rle, &dp, &csize);
1304
0
            if (code < 0 && !(code == gs_error_limitcheck))
1305
0
                return code;
1306
0
            compress = (uint)code;
1307
0
            if (code < 0) {
1308
                /* The bitmap was too large; split up the transfer. */
1309
0
                if (re.height > 1) {
1310
                    /* Split the transfer by reducing the height.
1311
                     * See the comment above FOR_RECTS in gxcldev.h.
1312
                     */
1313
0
                    re.height >>= 1;
1314
0
                    goto copy;
1315
0
                } else {
1316
                    /* Split a single (very long) row. */
1317
0
                    int w2 = w1 >> 1;
1318
1319
0
                    code = clist_copy_alpha_hl_color(dev, row, dx,
1320
0
                                            raster, gx_no_bitmap_id, rx, re.y,
1321
0
                                            w2, 1, pdcolor, depth);
1322
0
                    if (code >= 0)
1323
0
                            code = clist_copy_alpha_hl_color(dev, row, dx + w2,
1324
0
                                                    raster, gx_no_bitmap_id,
1325
0
                                                    rx + w2, re.y, w1 - w2, 1,
1326
0
                                                    pdcolor, depth);
1327
0
                    if (code < 0)
1328
0
                        return code;
1329
0
                    continue;
1330
0
                }
1331
0
            }
1332
0
            op += compress;
1333
0
            if (dx) {
1334
0
                if_debug0m('L', dev->memory,
1335
0
                           "[L] fake end_run: really set_misc_data_x[2]\n");
1336
0
                *dp++ = cmd_count_op(cmd_opv_set_misc, 2, dev->memory);
1337
0
                *dp++ = cmd_set_misc_data_x + dx;
1338
0
            }
1339
#ifdef DEBUG
1340
            if (gs_debug_c('L')) {
1341
                const char *const *sub = cmd_sub_op_names[op >> 4];
1342
1343
                if (sub)
1344
                    dmlprintf1(dev->memory, "[L] fake end_run: really %s\n", sub[op & 0xf]);
1345
                else
1346
                    dmlprintf2(dev->memory, "[L] fake end_run: really %s %d\n", cmd_op_names[op >> 4], op & 0xf);
1347
            }
1348
#endif
1349
0
            *dp++ = cmd_count_op(op, csize, dev->memory);
1350
0
            *dp++ = depth;
1351
0
            cmd_put2w(rx, re.y, &dp);
1352
0
            cmd_put2w(w1, re.height, &dp);
1353
0
            re.pcls->rect = rect;
1354
0
        }
1355
0
    } while ((re.y += re.height) < re.yend);
1356
0
    return 0;
1357
0
}
1358
1359
int
1360
clist_copy_alpha(gx_device * dev, const byte * data, int data_x,
1361
           int raster, gx_bitmap_id id, int rx, int ry, int rwidth, int rheight,
1362
                 gx_color_index color, int depth)
1363
0
{
1364
0
    gx_device_clist_writer * const cdev =
1365
0
        &((gx_device_clist *)dev)->writer;
1366
    /* I don't like copying the entire body of clist_copy_color */
1367
    /* just to change 2 arguments and 1 opcode, */
1368
    /* but I don't see any alternative that doesn't require */
1369
    /* another level of procedure call even in the common case. */
1370
0
    int log2_depth = ilog2(depth);
1371
0
    int y0;
1372
0
    int data_x_bit;
1373
0
    cmd_rects_enum_t re;
1374
0
    gx_color_usage_bits color_usage = gx_color_index2usage(dev, color);
1375
1376
    /* If the target can't perform copy_alpha, exit now */
1377
0
    if (depth > 1 && (cdev->disable_mask & clist_disable_copy_alpha) != 0)
1378
0
        return_error(gs_error_unknownerror);
1379
1380
0
    fit_copy(dev, data, data_x, raster, id, rx, ry, rwidth, rheight);
1381
0
    y0 = ry;
1382
0
    data_x_bit = data_x << log2_depth;
1383
0
    if (cdev->permanent_error < 0)
1384
0
      return (cdev->permanent_error);
1385
    /* If needed, update the trans_bbox */
1386
0
    if (cdev->pdf14_needed) {
1387
0
        gs_int_rect bbox;
1388
1389
0
        bbox.p.x = rx;
1390
0
        bbox.q.x = rx + rwidth - 1;
1391
0
        bbox.p.y = ry;
1392
0
        bbox.q.y = ry + rheight - 1;
1393
1394
0
        clist_update_trans_bbox(cdev, &bbox);
1395
0
    }
1396
0
    RECT_ENUM_INIT(re, ry, rheight);
1397
0
    do {
1398
0
        int dx = (data_x_bit & 7) >> log2_depth;
1399
0
        int w1 = dx + rwidth;
1400
0
        const byte *row = data + (re.y - y0) * raster + (data_x_bit >> 3);
1401
0
        int code;
1402
1403
0
        RECT_STEP_INIT(re);
1404
0
        re.pcls->color_usage.or |= color_usage;
1405
0
        code = cmd_disable_lop(cdev, re.pcls);
1406
0
        if (code >= 0)
1407
0
            code = cmd_disable_clip(cdev, re.pcls);
1408
0
        if (code < 0)
1409
0
            return code;
1410
0
        if (!re.pcls->color_is_alpha) {
1411
0
            byte *dp;
1412
1413
0
            code = set_cmd_put_op(&dp, cdev, re.pcls, cmd_opv_set_copy_alpha, 1);
1414
0
            if (code < 0)
1415
0
                return code;
1416
0
            re.pcls->color_is_alpha = 1;
1417
0
        }
1418
0
        if (re.pcls->color_is_devn) {
1419
0
            byte *dp;
1420
1421
0
            code = set_cmd_put_extended_op(&dp, cdev, re.pcls, cmd_opv_ext_unset_color_is_devn, 1);
1422
0
            if (code < 0)
1423
0
                return code;
1424
0
            dp++;
1425
0
            re.pcls->color_is_alpha = 1;
1426
0
        }
1427
0
        if (color != re.pcls->colors[1]) {
1428
0
            code = cmd_set_color1(cdev, re.pcls, color);
1429
0
            if (code < 0)
1430
0
                return code;
1431
0
        }
1432
0
copy:{
1433
0
            gx_cmd_rect rect;
1434
0
            int rsize;
1435
0
            byte op = (byte) cmd_op_copy_color_alpha;
1436
0
            byte *dp;
1437
0
            uint csize;
1438
0
            uint compress;
1439
1440
0
            rect.x = rx, rect.y = re.y;
1441
0
            rect.width = w1, rect.height = re.height;
1442
0
            rsize = (dx ? 4 : 2) + cmd_size_rect(&rect);
1443
0
            code = cmd_put_bits(cdev, re.pcls, row, w1 << log2_depth,
1444
0
                                re.height, raster, rsize,
1445
0
                                1 << cmd_compress_rle, &dp, &csize);
1446
0
            if (code < 0 && !(code == gs_error_limitcheck))
1447
0
                return code;
1448
0
            compress = (uint)code;
1449
0
            if (code < 0) {
1450
                /* The bitmap was too large; split up the transfer. */
1451
0
                if (re.height > 1) {
1452
                    /* Split the transfer by reducing the height.
1453
                     * See the comment above FOR_RECTS in gxcldev.h.
1454
                     */
1455
0
                    re.height >>= 1;
1456
0
                    goto copy;
1457
0
                } else {
1458
                    /* Split a single (very long) row. */
1459
0
                    int w2 = w1 >> 1;
1460
1461
0
                    code = clist_copy_alpha(dev, row, dx,
1462
0
                                            raster, gx_no_bitmap_id, rx, re.y,
1463
0
                                            w2, 1, color, depth);
1464
0
                    if (code >= 0)
1465
0
                        code = clist_copy_alpha(dev, row, dx + w2,
1466
0
                                                raster, gx_no_bitmap_id,
1467
0
                                                rx + w2, re.y, w1 - w2, 1,
1468
0
                                                color, depth);
1469
0
                    if (code < 0)
1470
0
                        return code;
1471
0
                    continue;
1472
0
                }
1473
0
            }
1474
0
            op += compress;
1475
0
            if (dx) {
1476
0
                if_debug0m('L', dev->memory,
1477
0
                           "[L] fake end_run: really set_misc_data_x[2]\n");
1478
0
                *dp++ = cmd_count_op(cmd_opv_set_misc, 2, dev->memory);
1479
0
                *dp++ = cmd_set_misc_data_x + dx;
1480
0
            }
1481
#ifdef DEBUG
1482
            if (gs_debug_c('L')) {
1483
                const char *const *sub = cmd_sub_op_names[op >> 4];
1484
1485
                if (sub)
1486
                    dmlprintf1(dev->memory, "[L] fake end_run: really %s\n", sub[op & 0xf]);
1487
                else
1488
                    dmlprintf2(dev->memory, "[L] fake end_run: really %s %d\n", cmd_op_names[op >> 4], op & 0xf);
1489
            }
1490
#endif
1491
0
            *dp++ = cmd_count_op(op, csize, dev->memory);
1492
0
            *dp++ = depth;
1493
0
            cmd_put2w(rx, re.y, &dp);
1494
0
            cmd_put2w(w1, re.height, &dp);
1495
0
            re.pcls->rect = rect;
1496
0
        }
1497
0
    } while ((re.y += re.height) < re.yend);
1498
0
    return 0;
1499
0
}
1500
1501
int
1502
clist_strip_copy_rop2(gx_device * dev,
1503
             const byte * sdata, int sourcex, uint sraster, gx_bitmap_id id,
1504
                     const gx_color_index * scolors,
1505
           const gx_strip_bitmap * textures, const gx_color_index * tcolors,
1506
                     int rx, int ry, int rwidth, int rheight,
1507
                     int phase_x, int phase_y, gs_logical_operation_t lop,
1508
                     uint planar_height)
1509
0
{
1510
0
    gx_device_clist_writer * const cdev =
1511
0
        &((gx_device_clist *)dev)->writer;
1512
0
    gs_rop3_t rop = lop_rop(lop);
1513
0
    gx_strip_bitmap tile_with_id;
1514
0
    const gx_strip_bitmap *tiles = textures;
1515
0
    int y0;
1516
    /* Compute the set of possible colors that this operation can generate. */
1517
0
    gx_color_usage_bits all = gx_color_usage_all(cdev);
1518
0
    bool subtractive = dev->color_info.num_components >= 4; /****** HACK ******/
1519
0
    gx_color_usage_bits S =
1520
0
        (scolors ? gx_color_index2usage(dev, scolors[0] | scolors[1]) : sdata ? all : 0);
1521
0
    gx_color_usage_bits T =
1522
0
        (tcolors ? gx_color_index2usage(dev, tcolors[0] | tcolors[1]) : textures ? all : 0);
1523
0
    gs_rop3_t color_rop =
1524
0
        (subtractive ? byte_reverse_bits[rop ^ 0xff] : rop);
1525
0
    bool slow_rop;
1526
0
    cmd_rects_enum_t re;
1527
1528
0
    if (scolors != 0 && scolors[0] != scolors[1]) {
1529
0
        crop_fill(cdev, rx, ry, rwidth, rheight);
1530
0
    } else {
1531
0
        crop_copy(cdev, sdata, sourcex, sraster, id, rx, ry, rwidth, rheight);
1532
0
    }
1533
0
    if (rwidth <= 0 || rheight <= 0)
1534
0
        return 0;
1535
1536
    /* If needed, update the trans_bbox */
1537
0
    if (cdev->pdf14_needed) {
1538
0
        gs_int_rect bbox;
1539
1540
0
        bbox.p.x = rx;
1541
0
        bbox.q.x = rx + rwidth - 1;
1542
0
        bbox.p.y = ry;
1543
0
        bbox.q.y = ry + rheight - 1;
1544
1545
0
        clist_update_trans_bbox(cdev, &bbox);
1546
0
    }
1547
    /*
1548
     * On CMYK devices, RasterOps must be executed with complete pixels
1549
     * if the operation involves the destination.
1550
     * This is because the black plane interacts with the other planes
1551
     * in the conversion between RGB and CMYK.  Check for this now.
1552
     */
1553
0
    {
1554
0
        gs_rop3_t rop_used = rop;
1555
1556
0
        if (scolors && (scolors[0] == scolors[1]))
1557
0
            rop_used = (scolors[0] == gx_device_black(dev) ?
1558
0
                        rop3_know_S_0(rop_used) :
1559
0
                        scolors[0] == gx_device_white(dev) ?
1560
0
                        rop3_know_S_1(rop_used) : rop_used);
1561
0
        if (tcolors && (tcolors[0] == tcolors[1]))
1562
0
            rop_used = (tcolors[0] == gx_device_black(dev) ?
1563
0
                        rop3_know_T_0(rop_used) :
1564
0
                        tcolors[0] == gx_device_white(dev) ?
1565
0
                        rop3_know_T_1(rop_used) : rop_used);
1566
0
        slow_rop = !(rop == rop3_0 || rop == rop3_1 ||
1567
0
                     rop == rop3_S || rop == rop3_T);
1568
0
    }
1569
0
    y0 = ry;
1570
0
    if (cdev->permanent_error < 0)
1571
0
      return (cdev->permanent_error);
1572
    /* If needed, update the trans_bbox */
1573
0
    if (cdev->pdf14_needed) {
1574
0
        gs_int_rect bbox;
1575
1576
0
        bbox.p.x = rx;
1577
0
        bbox.q.x = rx + rwidth - 1;
1578
0
        bbox.p.y = ry;
1579
0
        bbox.q.y = ry + rheight - 1;
1580
1581
0
        clist_update_trans_bbox(cdev, &bbox);
1582
0
    }
1583
0
    RECT_ENUM_INIT(re, ry, rheight);
1584
0
    do {
1585
0
        const byte *row = sdata + (re.y - y0) * sraster;
1586
0
        int code;
1587
1588
0
        RECT_STEP_INIT(re);
1589
        /* rops are defined to work on rop_operands, which are typically 32
1590
         * bits (unsigned long). We need to use gx_color_usage_bits, which
1591
         * are usually 64 bits. So, the following funky bit of code; although
1592
         * this is an if rather than a #if (as sizeof cannot be evaluated at
1593
         * preprocess time), it should resolve properly at compile time. */
1594
0
        if (sizeof(rop_operand) >= sizeof(gx_color_usage_bits)) {
1595
0
            gx_color_usage_bits D = re.pcls->color_usage.or;
1596
0
            re.pcls->color_usage.or =
1597
0
                    ((rop_proc_table[color_rop])((rop_operand)D, (rop_operand)S, (rop_operand)T) & all) | D;
1598
0
        } else if (2*sizeof(rop_operand) >= sizeof(gx_color_usage_bits)) {
1599
0
            rop_operand D;
1600
0
            D = (rop_operand)re.pcls->color_usage.or;
1601
0
            re.pcls->color_usage.or |=
1602
0
                    ((rop_proc_table[color_rop])(D, (rop_operand)S, (rop_operand)T) & all);
1603
0
#define DOWN(x) ((rop_operand)(((x)>>(4*sizeof(rop_operand)))>>(4*sizeof(rop_operand))))
1604
0
#define UP(x)   ((((gx_color_usage_bits)(x))<<(4*sizeof(rop_operand)))<<(4*sizeof(rop_operand)))
1605
0
            D = DOWN(re.pcls->color_usage.or);
1606
0
            re.pcls->color_usage.or |=
1607
0
                    UP((rop_proc_table[color_rop])(D, DOWN(S), DOWN(T))) & all;
1608
0
#undef DOWN
1609
0
#undef UP
1610
0
        }
1611
0
        re.pcls->color_usage.slow_rop |= slow_rop;
1612
0
        if (rop3_uses_T(rop)) {
1613
0
            if (tcolors == 0 || tcolors[0] != tcolors[1]) {
1614
0
                ulong offset_temp;
1615
1616
0
                if (!cls_has_tile_id(cdev, re.pcls, tiles->id, offset_temp)) {
1617
                    /* Change tile.  If there is no id, generate one. */
1618
0
                    if (tiles->id == gx_no_bitmap_id) {
1619
0
                        tile_with_id = *tiles;
1620
0
                        tile_with_id.id = gs_next_ids(dev->memory, 1);
1621
0
                        tiles = &tile_with_id;
1622
0
                    }
1623
0
                    code = clist_change_bits(cdev, re.pcls, tiles,
1624
0
                                            (tcolors != 0 ? 1 :
1625
0
                                                 cdev->clist_color_info.depth));
1626
0
                    if (code < 0 && !(code == gs_error_limitcheck))
1627
0
                        return code;
1628
0
                    if (code < 0) {
1629
                        /*
1630
                         * The error is a limitcheck: we have a tile that
1631
                         * is too big to fit in the command reading buffer.
1632
                         * For now, just divide up the transfer into scan
1633
                         * lines.  (If a single scan line won't fit, punt.)
1634
                         * Eventually, we'll need a way to transfer the tile
1635
                         * in pieces.
1636
                         */
1637
0
                        uint rep_height = tiles->rep_height;
1638
0
                        gs_id ids;
1639
0
                        gx_strip_bitmap line_tile;
1640
0
                        int data_shift = 0, phase_shift = 0, raster;
1641
0
                        int new_phase = phase_x;
1642
0
                        int tile_space_phase;
1643
0
                        int iy;
1644
1645
0
                        if (rep_height == 1 ||
1646
                            /****** CAN'T HANDLE SHIFT YET ******/
1647
0
                            tiles->rep_shift != 0
1648
0
                            )
1649
0
                            return code;
1650
                        /*
1651
                         * Allocate enough fake IDs, since the inner call on
1652
                         * clist_strip_copy_rop will need them anyway.
1653
                         */
1654
0
                        ids = gs_next_ids(dev->memory, min(re.height, rep_height));
1655
0
                        line_tile = *tiles;
1656
0
                        line_tile.size.y = 1;
1657
0
                        line_tile.rep_height = 1;
1658
0
                        raster = line_tile.raster;
1659
0
                        if (line_tile.num_planes > 0)
1660
0
                            line_tile.raster *= tiles->size.y;
1661
                        /* The rasterizer assumes tile phase relatively to the rectangle origin,
1662
                           (see x_offset in gdevmr8n.c), so compute "the tile phase in the tile space"
1663
                           with same expression : */
1664
0
                        tile_space_phase = (rx + phase_x) % tiles->rep_width;
1665
0
                        if (tile_space_phase + rwidth <= tiles->rep_width) {
1666
                            /* Narrow the tile to prevent buffer overflow - bug 689588.
1667
                               Note currently we don't narrow "wrapped" tiles (because bug 689588 doesn't need) :
1668
                               when tile_space_phase < rep_width && tile_space_phase + rwidth > rep_width,
1669
                               each line to be converted into 2 ones.
1670
                            */
1671
0
                            int depth = cdev->clist_color_info.depth;
1672
1673
0
                            if (line_tile.num_planes > 0)
1674
0
                                depth /= line_tile.num_planes;
1675
#         if 0
1676
                            /* Align bitmap data : */
1677
                            data_shift = ((tile_space_phase * depth) >> (log2_align_bitmap_mod + 3)) << log2_align_bitmap_mod;
1678
#         else
1679
0
                            data_shift = tile_space_phase * depth / 8; /* No bitmap data alignment because we'll only write it to clist.  */
1680
0
#         endif
1681
0
                            phase_shift = data_shift * 8 / depth;
1682
0
                            line_tile.rep_width = rwidth + (tile_space_phase - phase_shift);
1683
                            /* Normally line_tile.raster must account trailing row alignment bytes and
1684
                               to be computed as bitmap_raster(line_tile.rep_width * depth);
1685
                               but we can't apply it here because the trailing alignment bytes may be absent
1686
                               due to data_shift. We believe it is not harmful because we just write the data to clist,
1687
                               and because the bitmap height is 1.
1688
                               The clist reader must provide the trailing bytes if the rasterizer needs them.
1689
                             */
1690
0
                            if (line_tile.num_planes <= 1)
1691
0
                                line_tile.raster = (line_tile.rep_width * depth + 7) / 8;
1692
0
                            line_tile.size.x = line_tile.rep_width;
1693
0
                            line_tile.shift = 0;
1694
0
                            new_phase = (tile_space_phase - phase_shift - rx % line_tile.rep_width);
1695
                            /* Provide a positive phase for clist reader : */
1696
0
                            new_phase = (new_phase + line_tile.rep_width) % line_tile.rep_width;
1697
0
                        }
1698
0
                        for (iy = 0; iy < re.height; ++iy) {
1699
0
                            line_tile.data = tiles->data + raster *
1700
0
                                ((re.y + iy + phase_y) % rep_height) + data_shift;
1701
0
                            line_tile.id = ids + (iy % rep_height);
1702
0
                            code = clist_strip_copy_rop2(dev,
1703
0
                                    (sdata == 0 ? 0 : row + iy * sraster),
1704
0
                                    sourcex, sraster,
1705
0
                                    gx_no_bitmap_id, scolors,
1706
0
                                    &line_tile, tcolors,
1707
0
                                    rx, re.y + iy, rwidth, 1,
1708
0
                                    new_phase, 0, lop, planar_height);
1709
0
                            if (code < 0)
1710
0
                                return code;
1711
0
                        }
1712
0
                        continue;
1713
0
                    }
1714
0
                }
1715
0
                if (((phase_x != re.pcls->tile_phase.x) && (tiles->rep_width > 1)) ||
1716
0
                    ((phase_y != re.pcls->tile_phase.y) && (tiles->rep_height > 1))) {
1717
0
                    code = cmd_set_tile_phase(cdev, re.pcls, phase_x,
1718
0
                                              phase_y);
1719
0
                    if (code < 0)
1720
0
                        return code;
1721
0
                }
1722
0
            }
1723
            /* Set the tile colors. */
1724
0
            code =
1725
0
                (tcolors != 0 ?
1726
0
                 cmd_set_tile_colors(cdev, re.pcls, tcolors[0], tcolors[1]) :
1727
0
                 cmd_set_tile_colors(cdev, re.pcls, gx_no_color_index,
1728
0
                                     gx_no_color_index));
1729
0
            if (code < 0)
1730
0
                return code;
1731
0
        }
1732
0
        code = 0;
1733
0
        if (lop != re.pcls->lop)
1734
0
            code = cmd_set_lop(cdev, re.pcls, lop);
1735
0
        if (code >= 0)
1736
0
            code = cmd_enable_lop(cdev, re.pcls);
1737
0
        if (code < 0)
1738
0
            return code;
1739
        /* Set lop_enabled to -1 so that fill_rectangle / copy_* */
1740
        /* won't attempt to set it to 0. */
1741
0
        re.pcls->lop_enabled = -1;
1742
0
        if (scolors != NULL) {
1743
0
            if (scolors[0] == scolors[1])
1744
0
                code = clist_fill_rectangle(dev, rx, re.y, rwidth, re.height,
1745
0
                                            scolors[1]);
1746
0
            else
1747
0
                code = clist_copy_mono(dev, row, sourcex, sraster, id,
1748
0
                                       rx, re.y, rwidth, re.height,
1749
0
                                       scolors[0], scolors[1]);
1750
0
        } else if (planar_height) {
1751
0
            code = clist_copy_planes(dev, row, sourcex, sraster, id,
1752
0
                                     rx, re.y, rwidth, re.height,
1753
0
                                     planar_height);
1754
0
        } else {
1755
0
            code = clist_copy_color(dev, row, sourcex, sraster, id,
1756
0
                                    rx, re.y, rwidth, re.height);
1757
0
        }
1758
0
        re.pcls->lop_enabled = 1;
1759
0
        if (code < 0)
1760
0
            return code;
1761
0
    } while ((re.y += re.height) < re.yend);
1762
0
    return 0;
1763
0
}