Coverage Report

Created: 2025-08-28 07:06

/src/ghostpdl/base/gdevplnx.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2025 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Plane extraction device */
18
#include "gx.h"
19
#include "gserrors.h"
20
#include "gsbitops.h"
21
#include "gsrop.h"    /* for logical op access */
22
#include "gsstruct.h"
23
#include "gsutil.h"
24
#include "gxdcolor.h"
25
#include "gxcmap.h"   /* requires gxdcolor.h */
26
#include "gxdevice.h"
27
#include "gxdevmem.h"
28
#include "gxdither.h"
29
#include "gxgetbit.h"
30
#include "gxiparam.h"
31
#include "gxgstate.h"
32
#include "gsstate.h"
33
#include "gdevplnx.h"
34
35
/* Define the size of the locally allocated bitmap buffers. */
36
#define COPY_COLOR_BUF_SIZE 100
37
#define TILE_RECTANGLE_BUF_SIZE 100
38
#define COPY_ROP_SOURCE_BUF_SIZE 100
39
#define COPY_ROP_TEXTURE_BUF_SIZE 100
40
41
/* GC procedures */
42
static
43
0
ENUM_PTRS_WITH(device_plane_extract_enum_ptrs, gx_device_plane_extract *edev)
44
0
    ENUM_PREFIX(st_device_forward, 1);
45
0
case 0: ENUM_RETURN(gx_device_enum_ptr(edev->target));
46
0
ENUM_PTRS_END
47
0
static RELOC_PTRS_WITH(device_plane_extract_reloc_ptrs, gx_device_plane_extract *edev)
48
0
{
49
0
    RELOC_PREFIX(st_device_forward);
50
0
    edev->plane_dev = gx_device_reloc_ptr(edev->plane_dev, gcst);
51
0
}
52
0
RELOC_PTRS_END
53
public_st_device_plane_extract();
54
55
/* Driver procedures */
56
static dev_proc_open_device(plane_open_device);
57
static dev_proc_fill_rectangle(plane_fill_rectangle);
58
static dev_proc_copy_mono(plane_copy_mono);
59
static dev_proc_copy_color(plane_copy_color);
60
static dev_proc_copy_alpha(plane_copy_alpha);
61
static dev_proc_fill_path(plane_fill_path);
62
static dev_proc_stroke_path(plane_stroke_path);
63
static dev_proc_fill_mask(plane_fill_mask);
64
static dev_proc_fill_parallelogram(plane_fill_parallelogram);
65
static dev_proc_fill_triangle(plane_fill_triangle);
66
static dev_proc_strip_tile_rectangle(plane_strip_tile_rectangle);
67
static dev_proc_strip_copy_rop2(plane_strip_copy_rop2);
68
static dev_proc_begin_typed_image(plane_begin_typed_image);
69
static dev_proc_get_bits_rectangle(plane_get_bits_rectangle);
70
71
/* Device prototype */
72
static void
73
plane_initialize_device_procs(gx_device *dev)
74
0
{
75
0
    set_dev_proc(dev, open_device, plane_open_device);
76
0
    set_dev_proc(dev, fill_rectangle, plane_fill_rectangle);
77
0
    set_dev_proc(dev, copy_mono, plane_copy_mono);
78
0
    set_dev_proc(dev, copy_color, plane_copy_color);
79
0
    set_dev_proc(dev, copy_alpha, plane_copy_alpha);
80
0
    set_dev_proc(dev, fill_path, plane_fill_path);
81
0
    set_dev_proc(dev, stroke_path, plane_stroke_path);
82
0
    set_dev_proc(dev, fill_mask, plane_fill_mask);
83
0
    set_dev_proc(dev, fill_parallelogram, plane_fill_parallelogram);
84
0
    set_dev_proc(dev, fill_triangle, plane_fill_triangle);
85
0
    set_dev_proc(dev, strip_tile_rectangle, plane_strip_tile_rectangle);
86
0
    set_dev_proc(dev, strip_copy_rop2, plane_strip_copy_rop2);
87
0
    set_dev_proc(dev, begin_typed_image, plane_begin_typed_image);
88
0
    set_dev_proc(dev, get_bits_rectangle, plane_get_bits_rectangle);
89
0
    set_dev_proc(dev, composite, gx_no_composite); /* WRONG */
90
91
    /* Ideally the following would be initialized to the defaults
92
     * automatically, but this does not currently work. */
93
0
    set_dev_proc(dev, close_device, gx_default_close_device);
94
0
    set_dev_proc(dev, fill_trapezoid, gx_default_fill_trapezoid);
95
0
    set_dev_proc(dev, draw_thin_line, gx_default_draw_thin_line);
96
0
    set_dev_proc(dev, text_begin, gx_default_text_begin);
97
0
    set_dev_proc(dev, fill_rectangle_hl_color, gx_default_fill_rectangle_hl_color);
98
0
    set_dev_proc(dev, include_color_space, gx_default_include_color_space);
99
0
    set_dev_proc(dev, fill_linear_color_scanline, gx_default_fill_linear_color_scanline);
100
0
    set_dev_proc(dev, fill_linear_color_trapezoid, gx_default_fill_linear_color_trapezoid);
101
0
    set_dev_proc(dev, fill_linear_color_triangle, gx_default_fill_linear_color_triangle);
102
0
    set_dev_proc(dev, update_spot_equivalent_colors, gx_default_update_spot_equivalent_colors);
103
0
    set_dev_proc(dev, ret_devn_params, gx_default_ret_devn_params);
104
0
    set_dev_proc(dev, fillpage, gx_default_fillpage);
105
0
    set_dev_proc(dev, strip_tile_rect_devn, gx_default_strip_tile_rect_devn);
106
0
    set_dev_proc(dev, copy_alpha_hl_color, gx_default_copy_alpha_hl_color);
107
0
}
108
109
static const gx_device_plane_extract gs_plane_extract_device = {
110
    std_device_std_body(gx_device_plane_extract,
111
                        plane_initialize_device_procs, "plane_extract",
112
                        0, 0, 72, 72),
113
    { 0 },
114
    /* device-specific members */
115
    NULL,       /* target */
116
    NULL,       /* plane_dev */
117
    { 0 },        /* plane */
118
    0,          /* plane_white */
119
    0,          /* plane_mask */
120
    0,          /* plane_dev_is_memory */
121
    1 /*true*/        /* any_marks */
122
};
123
124
/* ---------------- Utilities ---------------- */
125
126
/* Extract the selected plane from a color (gx_color_index). */
127
#define COLOR_PIXEL(edev, color)\
128
0
  ( ((color) >> (edev)->plane.shift) & (edev)->plane_mask )
129
/* Do the same if the color might be transparent. */
130
#define TRANS_COLOR_PIXEL(edev, color)\
131
0
 ((color) == gx_no_color_index ? gx_no_color_index : COLOR_PIXEL(edev, color))
132
133
/*
134
 * Reduce the drawing color to one for the selected plane.
135
 * All we care about is whether the drawing operation should be skipped.
136
 */
137
typedef enum {
138
    REDUCE_SKIP,
139
    REDUCE_DRAW,
140
    REDUCE_FAILED     /* couldn't reduce */
141
} reduced_color_t;
142
#define REDUCE_PURE(edev, pixel)\
143
0
  ((pixel) == (edev)->plane_white && !(edev)->any_marks ?  REDUCE_SKIP :\
144
0
   ((edev)->any_marks = true, REDUCE_DRAW))
145
static reduced_color_t
146
reduce_drawing_color(gx_device_color *ppdc, gx_device_plane_extract *edev,
147
                     const gx_drawing_color *pdevc,
148
                     gs_logical_operation_t *plop)
149
0
{
150
0
    reduced_color_t reduced;
151
152
0
    if (gx_dc_is_pure(pdevc)) {
153
0
        gx_color_index pixel = COLOR_PIXEL(edev, gx_dc_pure_color(pdevc));
154
155
0
        set_nonclient_dev_color(ppdc, pixel);
156
0
        reduced = REDUCE_PURE(edev, pixel);
157
0
    } else if (gx_dc_is_binary_halftone(pdevc)) {
158
0
        gx_color_index pixel0 =
159
0
            TRANS_COLOR_PIXEL(edev, gx_dc_binary_color0(pdevc));
160
0
        gx_color_index pixel1 =
161
0
            TRANS_COLOR_PIXEL(edev, gx_dc_binary_color1(pdevc));
162
163
0
        if (pixel0 == pixel1) {
164
0
            set_nonclient_dev_color(ppdc, pixel0);
165
0
            reduced = REDUCE_PURE(edev, pixel0);
166
0
        } else {
167
0
            *ppdc = *pdevc;
168
0
            ppdc->colors.binary.color[0] = pixel0;
169
0
            ppdc->colors.binary.color[1] = pixel1;
170
0
            edev->any_marks = true;
171
0
            reduced = REDUCE_DRAW;
172
0
        }
173
0
    } else if (color_is_colored_halftone(pdevc)) {
174
0
        int plane = edev->plane.index;
175
0
        int i;
176
177
0
        *ppdc = *pdevc;
178
0
        for (i = 0; i < countof(ppdc->colors.colored.c_base); ++i)
179
0
            if (i != edev->plane.index) {
180
0
                ppdc->colors.colored.c_base[i] = 0;
181
0
                ppdc->colors.colored.c_level[i] = 0;
182
0
            }
183
0
        ppdc->colors.colored.plane_mask &= 1 << plane;
184
0
        if (ppdc->colors.colored.c_level[plane] == 0) {
185
0
            gx_devn_reduce_colored_halftone(ppdc, (gx_device *)edev);
186
0
            ppdc->colors.pure = COLOR_PIXEL(edev, ppdc->colors.pure);
187
0
            reduced = REDUCE_PURE(edev, gx_dc_pure_color(ppdc));
188
0
        } else {
189
0
            gx_devn_reduce_colored_halftone(ppdc, (gx_device *)edev);
190
0
            ppdc->colors.binary.color[0] =
191
0
                COLOR_PIXEL(edev, ppdc->colors.binary.color[0]);
192
0
            ppdc->colors.binary.color[1] =
193
0
                COLOR_PIXEL(edev, ppdc->colors.binary.color[1]);
194
0
            gx_color_load(ppdc, NULL, (gx_device *)edev);
195
0
            edev->any_marks = true;
196
0
            reduced = REDUCE_DRAW;
197
0
        }
198
0
    } else
199
0
        return REDUCE_FAILED;   /* can't handle it */
200
0
    if (*plop & lop_T_transparent) {
201
        /*
202
         * If the logical operation invokes transparency for the texture, we
203
         * must do some extra work, since a color that was originally opaque
204
         * may become transparent (white) if reduced to a single plane.  If
205
         * RasterOp transparency were calculated before halftoning, life
206
         * would be easy: we would simply turn off texture transparency in
207
         * the logical operation iff the original (not reduced) color was
208
         * not white.  Unfortunately, RasterOp transparency is calculated
209
         * after halftoning.  (This is arguably wrong, but it's how we've
210
         * defined it.)  Therefore, if transparency is involved with a
211
         * white color or a halftone that can include white, we must keep
212
         * the entire pixel together for the RasterOp.
213
         */
214
0
        gx_color_index white = gx_device_white((gx_device *)edev);
215
216
        /*
217
         * Given that we haven't failed, the only possible colors at this
218
         * point are pure or binary halftone.
219
         */
220
0
        if (gx_dc_is_pure(ppdc)) {
221
0
            if (gx_dc_pure_color(pdevc) != white)
222
0
                *plop &= ~lop_T_transparent;
223
0
            else if (!gx_dc_is_pure(pdevc))
224
0
                return REDUCE_FAILED;
225
0
        } else {
226
0
            if (gx_dc_binary_color0(pdevc) != white &&
227
0
                gx_dc_binary_color1(pdevc) != white) {
228
0
                *plop &= ~lop_T_transparent;
229
0
            } else
230
0
                return REDUCE_FAILED;
231
0
        }
232
0
    }
233
0
    return reduced;
234
0
}
235
236
/*
237
 * Set up to create the plane-extracted bitmap corresponding to a
238
 * source or halftone pixmap.  If the bitmap doesn't fit in the locally
239
 * allocated buffer, we may either do the operation in pieces, or allocate
240
 * a buffer on the heap.  The control structure is:
241
 *  begin_tiling(&state, ...);
242
 *  do {
243
 *      extract_partial_tile(&state);
244
 *      ... process tile in buffer ...
245
 *  } while (next_tile(&state));
246
 *  end_tiling(&state);
247
 * If partial_ok is false, there is only a single tile, so the do ... while
248
 * is not used.
249
 */
250
typedef struct tiling_state_s {
251
        /* Save the original operands. */
252
    const gx_device_plane_extract *edev;
253
    const byte *data;
254
    int data_x;
255
    uint raster;
256
    int width, height;
257
    int dest_x;     /* only for copy_color, defaults to 0 */
258
        /* Define the (aligned) buffer for doing the operation. */
259
    struct tsb_ {
260
        byte *data;
261
        uint size;
262
        uint raster;
263
        bool on_heap;
264
    } buffer;
265
        /* Record the current tile available for processing. */
266
        /* The client may read these out. */
267
    gs_int_point offset;
268
    gs_int_point size;
269
        /* Record private tiling parameters. */
270
    int per_tile_width;
271
} tiling_state_t;
272
273
/*
274
 * Extract the plane's data from one subrectangle of a source tile.
275
 */
276
static inline int /* ignore the return value */
277
extract_partial_tile(const tiling_state_t *pts)
278
0
{
279
0
    const gx_device_plane_extract * const edev = pts->edev;
280
0
    bits_plane_t dest, source;
281
282
0
    dest.data.write = pts->buffer.data + pts->offset.y * pts->buffer.raster;
283
0
    dest.raster = pts->buffer.raster;
284
0
    dest.depth = edev->plane.depth;
285
0
    dest.x = pts->dest_x;
286
287
0
    source.data.read = pts->data + pts->offset.y * pts->raster;
288
0
    source.raster = pts->raster;
289
0
    source.depth = edev->color_info.depth;
290
0
    source.x = pts->data_x + pts->offset.x;
291
292
0
    bits_extract_plane(&dest, &source, edev->plane.shift,
293
0
                       pts->size.x, pts->size.y);
294
0
    return 0;
295
0
}
296
297
/*
298
 * Set up to start (possibly) tiling.  Return 0 if the entire tile fit,
299
 * 1 if a partial tile fit, or a negative error code.
300
 */
301
static int
302
begin_tiling(tiling_state_t *pts, gx_device_plane_extract *edev,
303
    const byte *data, int data_x, uint raster, int width, int height,
304
    byte *local_buffer, uint buffer_size, bool partial_ok)
305
0
{
306
0
    uint width_raster =
307
0
        bitmap_raster(width * edev->plane_dev->color_info.depth);
308
0
    int64_t full_size;
309
310
0
    if (check_64bit_multiply(width_raster, height, &full_size) != 0)
311
0
        return gs_note_error(gs_error_undefinedresult);
312
313
0
    pts->edev = edev;
314
0
    pts->data = data, pts->data_x = data_x, pts->raster = raster;
315
0
    pts->width = width, pts->height = height;
316
0
    pts->dest_x = 0;
317
0
    if (full_size <= buffer_size) {
318
0
        pts->buffer.data = local_buffer;
319
0
        pts->buffer.size = buffer_size;
320
0
        pts->buffer.raster = width_raster;
321
0
        pts->buffer.on_heap = false;
322
0
        pts->size.x = width, pts->size.y = height;
323
0
    } else if (partial_ok) {
324
0
        pts->buffer.data = local_buffer;
325
0
        pts->buffer.size = buffer_size;
326
0
        pts->buffer.on_heap = false;
327
0
        if (buffer_size >= width_raster) {
328
0
            pts->buffer.raster = width_raster;
329
0
            pts->size.x = width;
330
0
            pts->size.y = buffer_size / width_raster;
331
0
        } else {
332
0
            pts->buffer.raster = buffer_size & -align_bitmap_mod;
333
0
            pts->size.x =
334
0
                pts->buffer.raster * (8 / edev->plane_dev->color_info.depth);
335
0
            pts->size.y = 1;
336
0
        }
337
0
    } else {
338
0
        pts->buffer.data =
339
0
            gs_alloc_bytes(edev->memory, full_size, "begin_tiling");
340
0
        if (!pts->buffer.data)
341
0
            return_error(gs_error_VMerror);
342
0
        pts->buffer.size = full_size;
343
0
        pts->buffer.raster = width_raster;
344
0
        pts->buffer.on_heap = true;
345
0
        pts->size.x = width, pts->size.y = height;
346
0
    }
347
0
    pts->buffer.raster = width_raster;
348
0
    pts->offset.x = pts->offset.y = 0;
349
0
    pts->per_tile_width = pts->size.x;
350
0
    return pts->buffer.size < full_size;
351
0
}
352
353
/*
354
 * Advance to the next tile.  Return true if there are more tiles to do.
355
 */
356
static bool
357
next_tile(tiling_state_t *pts)
358
0
{
359
0
    if ((pts->offset.x += pts->size.x) >= pts->width) {
360
0
        if ((pts->offset.y += pts->size.y) >= pts->height)
361
0
            return false;
362
0
        pts->offset.x = 0;
363
0
        pts->size.x = pts->per_tile_width;
364
0
        if (pts->offset.y + pts->size.y >= pts->height)
365
0
            pts->size.y = pts->height - pts->offset.y;
366
0
    } else if (pts->offset.x + pts->size.x >= pts->width)
367
0
        pts->size.x = pts->width - pts->offset.x;
368
0
    return true;
369
0
}
370
371
/*
372
 * Finish tiling by freeing the buffer if necessary.
373
 */
374
static void
375
end_tiling(tiling_state_t *pts)
376
0
{
377
0
    if (pts->buffer.on_heap)
378
0
        gs_free_object(pts->edev->memory, pts->buffer.data, "end_tiling");
379
0
}
380
381
/* ---------------- Initialization ---------------- */
382
383
int
384
plane_device_init(gx_device_plane_extract *edev, gx_device *target,
385
    gx_device *plane_dev, const gx_render_plane_t *render_plane, bool clear)
386
0
{
387
0
    int code;
388
    /* Check for compatibility of the plane specification. */
389
0
    if (render_plane->depth > plane_dev->color_info.depth)
390
0
        return_error(gs_error_rangecheck);
391
0
    code = gx_device_init((gx_device *)edev,
392
0
                          (const gx_device *)&gs_plane_extract_device,
393
0
                          edev->memory, true);
394
0
    if (code < 0)
395
0
        return code;
396
0
    check_device_separable((gx_device *)edev);
397
0
    gx_device_forward_fill_in_procs((gx_device_forward *)edev);
398
0
    gx_device_set_target((gx_device_forward *)edev, target);
399
0
    gx_device_copy_params((gx_device *)edev, target);
400
0
    edev->plane_dev = plane_dev;
401
0
    gx_device_retain(plane_dev, true);
402
0
    edev->plane = *render_plane;
403
0
    plane_open_device((gx_device *)edev);
404
0
    if (clear) {
405
0
        dev_proc(plane_dev, fill_rectangle)
406
0
            (plane_dev, 0, 0, plane_dev->width, plane_dev->height,
407
0
             edev->plane_white);
408
0
        edev->any_marks = false;
409
0
    }
410
0
    return 0;
411
0
}
412
413
/* ---------------- Driver procedures ---------------- */
414
415
static int
416
plane_open_device(gx_device *dev)
417
0
{
418
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
419
0
    gx_device * const plane_dev = edev->plane_dev;
420
0
    int plane_depth = plane_dev->color_info.depth;
421
0
    const gdev_mem_functions *fns =
422
0
                               gdev_mem_functions_for_bits(plane_depth);
423
424
0
    edev->plane_white = gx_device_white(plane_dev);
425
0
    edev->plane_mask = (1 << plane_depth) - 1;
426
0
    edev->plane_dev_is_memory = fns != NULL &&
427
0
                     dev_proc(plane_dev, copy_color) == fns->copy_color;
428
    /* We don't set or clear any_marks here: see ...init above. */
429
0
    return 0;
430
0
}
431
432
static int
433
plane_fill_rectangle(gx_device *dev,
434
    int x, int y, int w, int h, gx_color_index color)
435
0
{
436
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
437
0
    gx_device * const plane_dev = edev->plane_dev;
438
0
    gx_color_index pixel = COLOR_PIXEL(edev, color);
439
440
0
    if (pixel != edev->plane_white)
441
0
        edev->any_marks = true;
442
0
    else if (!edev->any_marks)
443
0
        return 0;
444
0
    return dev_proc(plane_dev, fill_rectangle)
445
0
        (plane_dev, x, y, w, h, pixel);
446
0
}
447
448
static int
449
plane_copy_mono(gx_device *dev,
450
    const byte *data, int data_x, int raster, gx_bitmap_id id,
451
    int x, int y, int w, int h,
452
    gx_color_index color0, gx_color_index color1)
453
0
{
454
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
455
0
    gx_device * const plane_dev = edev->plane_dev;
456
0
    gx_color_index pixel0 = TRANS_COLOR_PIXEL(edev, color0);
457
0
    gx_color_index pixel1 = TRANS_COLOR_PIXEL(edev, color1);
458
459
0
    if (pixel0 == pixel1)
460
0
        return plane_fill_rectangle(dev, x, y, w, h, color0);
461
0
    if ((pixel0 == edev->plane_white || pixel0 == gx_no_color_index) &&
462
0
        (pixel1 == edev->plane_white || pixel1 == gx_no_color_index)) {
463
        /* This operation will only write white. */
464
0
        if (!edev->any_marks)
465
0
            return 0;
466
0
    } else
467
0
        edev->any_marks = true;
468
0
    return dev_proc(plane_dev, copy_mono)
469
0
        (plane_dev, data, data_x, raster, id, x, y, w, h, pixel0, pixel1);
470
0
}
471
472
static int
473
plane_copy_color(gx_device *dev,
474
    const byte *data, int data_x, int raster, gx_bitmap_id id,
475
    int x, int y, int w, int h)
476
0
{
477
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
478
0
    gx_device * const plane_dev = edev->plane_dev;
479
0
    tiling_state_t state;
480
0
    long buf[COPY_COLOR_BUF_SIZE / sizeof(long)];
481
0
    int code;
482
483
0
    if (edev->plane_dev_is_memory) {
484
        /* Reduce the source directly into the plane device. */
485
0
        gx_device_memory * const mdev = (gx_device_memory *)plane_dev;
486
487
0
        fit_copy(edev, data, data_x, raster, id, x, y, w, h);
488
0
        code = begin_tiling(&state, edev, data, data_x, raster, w, h,
489
0
                            scan_line_base(mdev, y), max_uint, false);
490
0
        if (code < 0)
491
0
            return code;
492
0
        state.dest_x = x;
493
0
        state.buffer.raster = mdev->raster;
494
0
        extract_partial_tile(&state);
495
0
        end_tiling(&state);
496
0
        edev->any_marks = true;
497
0
        return 0;
498
0
    }
499
0
    code = begin_tiling(&state, edev, data, data_x, raster,
500
0
                        w, h, (byte *)buf, sizeof(buf), true);
501
0
    if (code < 0)
502
0
        return code;
503
0
    do {
504
0
        extract_partial_tile(&state);
505
0
        code = dev_proc(plane_dev, copy_color)
506
0
            (plane_dev, state.buffer.data, 0, state.buffer.raster,
507
0
             gx_no_bitmap_id, x + state.offset.x, y + state.offset.y,
508
0
             state.size.x, state.size.y);
509
0
    } while (code >= 0 && next_tile(&state));
510
0
    end_tiling(&state);
511
0
    edev->any_marks = true;
512
0
    return code;
513
0
}
514
515
static int
516
plane_copy_alpha(gx_device *dev, const byte *data, int data_x,
517
    int raster, gx_bitmap_id id, int x, int y, int w, int h,
518
    gx_color_index color, int depth)
519
0
{
520
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
521
0
    gx_device * const plane_dev = edev->plane_dev;
522
0
    gx_color_index pixel = COLOR_PIXEL(edev, color);
523
524
0
    if (pixel != edev->plane_white)
525
0
        edev->any_marks = true;
526
0
    else if (!edev->any_marks)
527
0
        return 0;
528
0
    return dev_proc(plane_dev, copy_alpha)
529
0
        (plane_dev, data, data_x, raster, id, x, y, w, h, pixel, depth);
530
0
}
531
532
static int
533
plane_fill_path(gx_device *dev,
534
    const gs_gstate *pgs, gx_path *ppath,
535
    const gx_fill_params *params,
536
    const gx_drawing_color *pdevc, const gx_clip_path *pcpath)
537
0
{
538
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
539
0
    gx_device * const plane_dev = edev->plane_dev;
540
0
    gs_logical_operation_t lop_orig =
541
0
        gs_current_logical_op((const gs_gstate *)pgs);
542
0
    gs_logical_operation_t lop = lop_orig;
543
0
    gx_device_color dcolor;
544
545
0
    switch (reduce_drawing_color(&dcolor, edev, pdevc, &lop)) {
546
0
    case REDUCE_SKIP:
547
0
        return 0;
548
0
    case REDUCE_DRAW: {
549
0
        gs_gstate lopgs;
550
0
        const gs_gstate *pgs_draw = pgs;
551
552
0
        if (lop != lop_orig) {
553
0
            lopgs = *pgs;
554
0
            gs_set_logical_op((gs_gstate *)&lopgs, lop);
555
0
            pgs_draw = &lopgs;
556
0
        }
557
0
        return dev_proc(plane_dev, fill_path)
558
0
            (plane_dev, pgs_draw, ppath, params, &dcolor, pcpath);
559
0
    }
560
0
    default /*REDUCE_FAILED*/:
561
0
        return gx_default_fill_path(dev, pgs, ppath, params, pdevc, pcpath);
562
0
    }
563
0
}
564
565
static int
566
plane_stroke_path(gx_device *dev,
567
    const gs_gstate *pgs, gx_path *ppath,
568
    const gx_stroke_params *params,
569
    const gx_drawing_color *pdevc, const gx_clip_path *pcpath)
570
0
{
571
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
572
0
    gx_device * const plane_dev = edev->plane_dev;
573
0
    gs_logical_operation_t lop_orig =
574
0
        gs_current_logical_op((const gs_gstate *)pgs);
575
0
    gs_logical_operation_t lop = lop_orig;
576
0
    gx_device_color dcolor;
577
578
0
    switch (reduce_drawing_color(&dcolor, edev, pdevc, &lop)) {
579
0
    case REDUCE_SKIP:
580
0
        return 0;
581
0
    case REDUCE_DRAW: {
582
0
        gs_gstate lopgs;
583
0
        const gs_gstate *pgs_draw = pgs;
584
585
0
        if (lop != lop_orig) {
586
0
            lopgs = *pgs;
587
0
            gs_set_logical_op((gs_gstate *)&lopgs, lop);
588
0
            pgs_draw = &lopgs;
589
0
        }
590
0
        return dev_proc(plane_dev, stroke_path)
591
0
            (plane_dev, pgs_draw, ppath, params, &dcolor, pcpath);
592
0
    }
593
0
    default /*REDUCE_FAILED*/:
594
0
        return gx_default_stroke_path(dev, pgs, ppath, params, pdevc, pcpath);
595
0
    }
596
0
}
597
598
static int
599
plane_fill_mask(gx_device *dev,
600
    const byte *data, int data_x, int raster, gx_bitmap_id id,
601
    int x, int y, int w, int h,
602
    const gx_drawing_color *pdcolor, int depth,
603
    gs_logical_operation_t lop, const gx_clip_path *pcpath)
604
0
{
605
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
606
0
    gx_device * const plane_dev = edev->plane_dev;
607
0
    gx_device_color dcolor;
608
609
0
    switch (reduce_drawing_color(&dcolor, edev, pdcolor, &lop)) {
610
0
    case REDUCE_SKIP:
611
0
        return 0;
612
0
    case REDUCE_DRAW:
613
0
        return dev_proc(plane_dev, fill_mask)
614
0
            (plane_dev, data, data_x, raster, gx_no_bitmap_id, x, y, w, h,
615
0
             &dcolor, depth, lop, pcpath);
616
0
    default /*REDUCE_FAILED*/:
617
0
        return gx_default_fill_mask(dev, data, data_x, raster, gx_no_bitmap_id,
618
0
                                    x, y, w, h, &dcolor, depth, lop, pcpath);
619
0
    }
620
0
}
621
622
static int
623
plane_fill_parallelogram(gx_device * dev,
624
    fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
625
    const gx_drawing_color * pdcolor, gs_logical_operation_t lop)
626
0
{
627
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
628
0
    gx_device * const plane_dev = edev->plane_dev;
629
0
    gx_device_color dcolor;
630
631
0
    switch (reduce_drawing_color(&dcolor, edev, pdcolor, &lop)) {
632
0
    case REDUCE_SKIP:
633
0
        return 0;
634
0
    case REDUCE_DRAW:
635
0
        return dev_proc(plane_dev, fill_parallelogram)
636
0
            (plane_dev, px, py, ax, ay, bx, by, &dcolor, lop);
637
0
    default /*REDUCE_FAILED*/:
638
0
        return gx_default_fill_parallelogram(dev, px, py, ax, ay, bx, by,
639
0
                                             pdcolor, lop);
640
0
    }
641
0
}
642
643
static int
644
plane_fill_triangle(gx_device * dev,
645
    fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
646
    const gx_drawing_color * pdcolor, gs_logical_operation_t lop)
647
0
{
648
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
649
0
    gx_device * const plane_dev = edev->plane_dev;
650
0
    gx_device_color dcolor;
651
652
0
    switch (reduce_drawing_color(&dcolor, edev, pdcolor, &lop)) {
653
0
    case REDUCE_SKIP:
654
0
        return 0;
655
0
    case REDUCE_DRAW:
656
0
        return dev_proc(plane_dev, fill_triangle)
657
0
            (plane_dev, px, py, ax, ay, bx, by, &dcolor, lop);
658
0
    default /*REDUCE_FAILED*/:
659
0
        return gx_default_fill_triangle(dev, px, py, ax, ay, bx, by,
660
0
                                        pdcolor, lop);
661
0
    }
662
0
}
663
664
static int
665
plane_strip_tile_rectangle(gx_device *dev,
666
    const gx_strip_bitmap *tiles, int x, int y, int w, int h,
667
    gx_color_index color0, gx_color_index color1,
668
    int phase_x, int phase_y)
669
0
{
670
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
671
0
    gx_device * const plane_dev = edev->plane_dev;
672
0
    gx_color_index pixel0 = TRANS_COLOR_PIXEL(edev, color0);
673
0
    gx_color_index pixel1 = TRANS_COLOR_PIXEL(edev, color1);
674
675
0
    if (pixel0 == pixel1) {
676
0
        if (pixel0 != gx_no_color_index)
677
0
            return plane_fill_rectangle(dev, x, y, w, h, color0);
678
        /* The tile is a pixmap rather than a bitmap. */
679
        /* We should use the default implementation if it is small.... */
680
0
        {
681
0
            gx_strip_bitmap plane_tile;
682
0
            tiling_state_t state;
683
0
            long buf[TILE_RECTANGLE_BUF_SIZE / sizeof(long)];
684
0
            int code = begin_tiling(&state, edev, tiles->data, 0, tiles->raster,
685
0
                        tiles->size.x, tiles->size.y,
686
0
                                (byte *)buf, sizeof(buf), false);
687
688
0
            if (code < 0)
689
0
                return gx_default_strip_tile_rectangle(dev, tiles, x, y, w, h,
690
0
                                        color0, color1, phase_x, phase_y);
691
0
            extract_partial_tile(&state);
692
0
            plane_tile = *tiles;
693
0
            plane_tile.data = state.buffer.data;
694
0
            plane_tile.raster = state.buffer.raster;
695
0
            plane_tile.id = gx_no_bitmap_id;
696
0
            code = dev_proc(plane_dev, strip_tile_rectangle)
697
0
                (plane_dev, &plane_tile, x, y, w, h, pixel0, pixel1,
698
0
                 phase_x, phase_y);
699
0
            end_tiling(&state);
700
0
            edev->any_marks = true;
701
0
            return code;
702
0
        }
703
0
    }
704
0
    if ((pixel0 == edev->plane_white || pixel0 == gx_no_color_index) &&
705
0
        (pixel1 == edev->plane_white || pixel1 == gx_no_color_index)) {
706
        /* This operation will only write white. */
707
0
        if (!edev->any_marks)
708
0
            return 0;
709
0
    } else
710
0
        edev->any_marks = true;
711
0
    return dev_proc(plane_dev, strip_tile_rectangle)
712
0
        (plane_dev, tiles, x, y, w, h, pixel0, pixel1, phase_x, phase_y);
713
0
}
714
715
static int
716
plane_strip_copy_rop2(gx_device *dev,
717
    const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
718
    const gx_color_index *scolors,
719
    const gx_strip_bitmap *textures, const gx_color_index *tcolors,
720
    int x, int y, int w, int h,
721
    int phase_x, int phase_y, gs_logical_operation_t lop,
722
    uint plane_height)
723
0
{
724
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
725
0
    gx_device * const plane_dev = edev->plane_dev;
726
0
    gs_rop3_t rop = lop_sanitize(lop);
727
0
    struct crp_ {
728
0
        gx_color_index pixels[2];
729
0
        gx_color_index *colors;
730
0
        tiling_state_t state;
731
0
    } source, texture;
732
0
    long sbuf[COPY_ROP_SOURCE_BUF_SIZE / sizeof(long)];
733
0
    long tbuf[COPY_ROP_TEXTURE_BUF_SIZE / sizeof(long)];
734
0
    const byte *plane_source;
735
0
    uint plane_raster = 0xbaadf00d; /* Initialize against indeterminizm. */
736
0
    gx_strip_bitmap plane_texture;
737
0
    const gx_strip_bitmap *plane_textures = NULL;
738
0
    int code;
739
740
0
    if (!rop3_uses_S(rop)) {
741
0
        sdata = 0;
742
0
        source.colors = 0;
743
0
    } else if (scolors) {
744
0
        source.pixels[0] = COLOR_PIXEL(edev, scolors[0]);
745
0
        source.pixels[1] = COLOR_PIXEL(edev, scolors[1]);
746
0
        if (source.pixels[0] == source.pixels[1])
747
0
            sdata = 0;
748
0
        source.colors = source.pixels;
749
0
    }
750
0
    else
751
0
        source.colors = 0;
752
0
    if (!rop3_uses_T(rop)) {
753
0
        textures = 0;
754
0
        texture.colors = 0;
755
0
    } else if (tcolors) {
756
0
        texture.pixels[0] = COLOR_PIXEL(edev, tcolors[0]);
757
0
        texture.pixels[1] = COLOR_PIXEL(edev, tcolors[1]);
758
0
        if (texture.pixels[0] == texture.pixels[1])
759
0
            textures = 0;
760
0
        texture.colors = texture.pixels;
761
0
    }
762
0
    else
763
0
        texture.colors = 0;
764
0
    if (sdata) {
765
0
        code = begin_tiling(&source.state, edev, sdata, sourcex, sraster, w, y,
766
0
                            (byte *)sbuf, sizeof(sbuf), true);
767
0
        if (code < 0)
768
0
            return gx_default_strip_copy_rop2(dev, sdata, sourcex, sraster, id,
769
0
                                              scolors, textures, tcolors,
770
0
                                              x, y, w, h, phase_x, phase_y, rop,
771
0
                                              plane_height);
772
0
        plane_source = source.state.buffer.data;
773
0
        plane_raster = source.state.buffer.raster;
774
0
    } else
775
0
        plane_source = 0;
776
0
    if (textures) {
777
0
        code = begin_tiling(&texture.state, edev, textures->data, 0,
778
0
                            textures->raster, textures->size.x,
779
0
                            textures->size.y, (byte *)tbuf, sizeof(tbuf),
780
0
                            false);
781
0
        if (code < 0) {
782
0
            if (plane_source)
783
0
                end_tiling(&source.state);
784
0
            return code;
785
0
        }
786
0
        plane_texture = *textures;
787
0
        plane_texture.data = texture.state.buffer.data;
788
0
        plane_texture.raster = texture.state.buffer.raster;
789
0
        plane_textures = &plane_texture;
790
0
    }
791
0
    if (textures)
792
0
        extract_partial_tile(&texture.state);
793
0
    do {
794
0
        if (sdata)
795
0
            extract_partial_tile(&source.state);
796
0
        code = dev_proc(plane_dev, strip_copy_rop2)
797
0
            (plane_dev, plane_source, sourcex, plane_raster, gx_no_bitmap_id,
798
0
             source.colors, plane_textures, texture.colors,
799
0
             x, y, w, h, phase_x, phase_y, rop, plane_height);
800
0
    } while (code >= 0 && sdata && next_tile(&source.state));
801
0
    if (textures)
802
0
        end_tiling(&texture.state);
803
0
    if (sdata)
804
0
        end_tiling(&source.state);
805
0
    return code;
806
0
}
807
808
/* ---------------- Images ---------------- */
809
810
/* Define the state for image rendering. */
811
typedef struct plane_image_enum_s {
812
    gx_image_enum_common;
813
    gx_image_enum_common_t *info; /* plane device enumerator */
814
    gs_gstate *pgs_image; /* modified gs_gstate state */
815
} plane_image_enum_t;
816
/* Note that we include the pgs_image which is 'bytes' type (not gs_gstate) */
817
/* It still needs to be traced so that a GC won't free it prematurely.      */
818
gs_private_st_suffix_add2(st_plane_image_enum, plane_image_enum_t,
819
  "plane_image_enum_t", plane_image_enum_enum_ptrs,
820
  plane_image_enum_reloc_ptrs, st_gx_image_enum_common, info, pgs_image);
821
822
/*
823
 * Reduce drawing colors returned by color mapping.  Note that these
824
 * assume that the call of reduce_drawing_color will not fail:
825
 * plane_begin_typed_image must ensure this.
826
 *
827
 * In the gs_gstate passed to these procedures, the client data is
828
 * the plane_image_enum_t.
829
 */
830
831
static void
832
plane_cmap_gray(frac gray, gx_device_color * pdc,
833
    const gs_gstate *pgs_image, gx_device *dev, gs_color_select_t select)
834
0
{
835
0
    const plane_image_enum_t *ppie =
836
0
        (const plane_image_enum_t *)pgs_image->client_data;
837
0
    gx_device_plane_extract * const edev =
838
0
        (gx_device_plane_extract *)ppie->dev;
839
0
    gs_logical_operation_t lop = gs_current_logical_op_inline(pgs_image);
840
0
    gx_device_color dcolor;
841
842
0
    gx_remap_concrete_gray(gray, &dcolor, ppie->pgs,
843
0
                           (gx_device *)edev, select);
844
0
    reduce_drawing_color(pdc, edev, &dcolor, &lop);
845
0
}
846
static void
847
plane_cmap_rgb(frac r, frac g, frac b, gx_device_color * pdc,
848
    const gs_gstate *pgs_image, gx_device *dev, gs_color_select_t select)
849
0
{
850
0
    const plane_image_enum_t *ppie =
851
0
        (const plane_image_enum_t *)pgs_image->client_data;
852
0
    gx_device_plane_extract * const edev =
853
0
        (gx_device_plane_extract *)ppie->dev;
854
0
    gs_logical_operation_t lop = gs_current_logical_op_inline(pgs_image);
855
0
    gx_device_color dcolor;
856
857
0
    gx_remap_concrete_rgb(r, g, b, &dcolor, ppie->pgs,
858
0
                          (gx_device *)edev, select);
859
0
    reduce_drawing_color(pdc, edev, &dcolor, &lop);
860
0
}
861
static void
862
plane_cmap_cmyk(frac c, frac m, frac y, frac k, gx_device_color * pdc,
863
    const gs_gstate *pgs_image, gx_device *dev, gs_color_select_t select,
864
    const gs_color_space *source_pcs)
865
0
{
866
0
    const plane_image_enum_t *ppie =
867
0
        (const plane_image_enum_t *)pgs_image->client_data;
868
0
    gx_device_plane_extract * const edev =
869
0
        (gx_device_plane_extract *)ppie->dev;
870
0
    gs_logical_operation_t lop = gs_current_logical_op_inline(pgs_image);
871
0
    gx_device_color dcolor;
872
873
0
    gx_remap_concrete_cmyk(c, m, y, k, &dcolor, ppie->pgs,
874
0
                           (gx_device *)edev, select, NULL);
875
0
    reduce_drawing_color(pdc, edev, &dcolor, &lop);
876
0
}
877
static bool
878
plane_cmap_is_halftoned(const gs_gstate *pgs_image, gx_device *dev)
879
0
{
880
0
    return false;
881
0
}
882
883
static const gx_color_map_procs plane_color_map_procs = {
884
    plane_cmap_gray, plane_cmap_rgb, plane_cmap_cmyk,
885
    NULL, NULL, plane_cmap_is_halftoned
886
};
887
static const gx_color_map_procs *
888
plane_get_cmap_procs(const gs_gstate *pgs, const gx_device *dev)
889
0
{
890
0
    return &plane_color_map_procs;
891
0
}
892
893
/* Define the image processing procedures. */
894
static image_enum_proc_plane_data(plane_image_plane_data);
895
static image_enum_proc_end_image(plane_image_end_image);
896
static const gx_image_enum_procs_t plane_image_enum_procs = {
897
    plane_image_plane_data, plane_image_end_image
898
};
899
900
static int
901
plane_begin_typed_image(gx_device * dev,
902
                        const gs_gstate * pgs, const gs_matrix * pmat,
903
                   const gs_image_common_t * pic, const gs_int_rect * prect,
904
              const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
905
                      gs_memory_t * memory, gx_image_enum_common_t ** pinfo)
906
0
{
907
    /*
908
     * For images, we intercept the gs_gstate's cmap_procs and apply
909
     * reduce_drawing_color to the colors as they are returned to the image
910
     * processing code.  For reasons explained above, we can't do this in
911
     * some cases of RasterOp that include transparency.
912
     */
913
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
914
0
    gs_logical_operation_t lop = gs_current_logical_op((const gs_gstate *)pgs);
915
0
    const gs_pixel_image_t *pim;
916
0
    plane_image_enum_t *info = 0;
917
0
    gs_gstate *pgs_image = 0;
918
0
    gx_device_color dcolor;
919
0
    bool uses_color = false;
920
0
    int code;
921
922
    /* We can only handle a limited set of image types. */
923
0
    switch (pic->type->index) {
924
0
    case 1: {
925
0
        const gs_image1_t * const pim1 = (const gs_image1_t *)pic;
926
927
0
        if (pim1->Alpha != gs_image_alpha_none)
928
0
            goto fail;
929
0
        uses_color = pim1->ImageMask;
930
0
        break;
931
0
        }
932
0
    case 3:
933
0
    case 4:
934
0
        break;
935
0
    default:
936
0
        goto fail;
937
0
    }
938
0
    pim = (const gs_pixel_image_t *)pic;
939
0
    lop = lop_sanitize(lop);
940
0
    if (uses_color || (pim->CombineWithColor && lop_uses_T(lop))) {
941
0
        if (reduce_drawing_color(&dcolor, edev, pdcolor, &lop) ==
942
0
            REDUCE_FAILED)
943
0
            goto fail;
944
0
    } else {
945
        /*
946
         * The drawing color won't be used, but if RasterOp is involved,
947
         * it may still be accessed in some anomalous cases.
948
         */
949
0
        set_nonclient_dev_color(&dcolor, (gx_color_index)0);
950
0
    }
951
0
    info = gs_alloc_struct(memory, plane_image_enum_t, &st_plane_image_enum,
952
0
                           "plane_image_begin_typed(info)");
953
0
    pgs_image = gs_gstate_copy(pgs, memory);
954
0
    if (pgs_image == 0 || info == 0)
955
0
        goto fail;
956
0
    pgs_image->client_data = info;
957
0
    pgs_image->get_cmap_procs = plane_get_cmap_procs;
958
0
    code = dev_proc(edev->plane_dev, begin_typed_image)
959
0
        (edev->plane_dev, pgs_image, pmat, pic, prect,
960
0
         &dcolor, pcpath, memory, &info->info);
961
0
    if (code < 0)
962
0
        goto fail;
963
0
    *((gx_image_enum_common_t *)info) = *info->info;
964
0
    info->procs = &plane_image_enum_procs;
965
0
    info->dev = (gx_device *)edev;
966
0
    info->id = gs_next_ids(memory, 1);
967
0
    info->memory = memory;
968
0
    info->pgs = pgs;
969
0
    info->pgs_level = pgs->level;
970
0
    info->pgs_image = pgs_image;
971
0
    *pinfo = (gx_image_enum_common_t *)info;
972
0
    return code;
973
0
fail:
974
0
    gs_free_object(memory, pgs_image, "plane_image_begin_typed(pgs_image)");
975
0
    gs_free_object(memory, info, "plane_image_begin_typed(info)");
976
0
    return gx_default_begin_typed_image(dev, pgs, pmat, pic, prect,
977
0
                                        pdcolor, pcpath, memory, pinfo);
978
0
}
979
980
static int
981
plane_image_plane_data(gx_image_enum_common_t * info,
982
                       const gx_image_plane_t * planes, int height,
983
                       int *rows_used)
984
0
{
985
0
    plane_image_enum_t * const ppie = (plane_image_enum_t *)info;
986
987
0
    if (info->pgs!= NULL && info->pgs->level < info->pgs_level)
988
0
        return_error(gs_error_undefinedresult);
989
990
0
    return gx_image_plane_data_rows(ppie->info, planes, height, rows_used);
991
0
}
992
993
static int
994
plane_image_end_image(gx_image_enum_common_t * info, bool draw_last)
995
0
{
996
0
    plane_image_enum_t * const ppie = (plane_image_enum_t *)info;
997
0
    int code = gx_image_end(ppie->info, draw_last);
998
999
0
    ppie->pgs_image->client_data = NULL;       /* this isn't a complete client_data struct */
1000
0
    gs_free_object(ppie->memory, ppie->pgs_image,
1001
0
                   "plane_image_end_image(pgs_image)");
1002
0
    gx_image_free_enum(&info);
1003
0
    return code;
1004
0
}
1005
1006
/* ---------------- Reading back bits ---------------- */
1007
1008
static int
1009
plane_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
1010
                         gs_get_bits_params_t * params)
1011
0
{
1012
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
1013
0
    gx_device * const plane_dev = edev->plane_dev;
1014
0
    int plane_index = edev->plane.index;
1015
0
    gs_get_bits_options_t options = params->options;
1016
0
    gs_get_bits_params_t plane_params;
1017
0
    uchar plane;
1018
0
    int code;
1019
1020
    /*
1021
     * The only real option that this device supports is single-plane
1022
     * retrieval.  However, for the default case of RasterOp, it must be
1023
     * able to return chunky pixels in which the other components are
1024
     * arbitrary (but might as well be zero).
1025
     */
1026
0
    if ((options & GB_PACKING_PLANAR) && (options & GB_SELECT_PLANES)) {
1027
0
        if (params->data[plane_index] == 0)
1028
0
            return gx_default_get_bits_rectangle(dev, prect, params);
1029
        /* If the caller wants any other plane(s), punt. */
1030
0
        for (plane = 0; plane < dev->color_info.num_components; ++plane)
1031
0
            if (plane != plane_index && params->data[plane] != 0)
1032
0
                return gx_default_get_bits_rectangle(dev, prect, params);
1033
        /* Pass the request on to the plane device. */
1034
0
        plane_params = *params;
1035
0
        plane_params.options =
1036
0
            (options & ~(GB_PACKING_ALL | GB_SELECT_PLANES)) |
1037
0
            GB_PACKING_CHUNKY;
1038
0
        plane_params.data[0] = params->data[plane_index];
1039
0
        code = dev_proc(plane_dev, get_bits_rectangle)
1040
0
            (plane_dev, prect, &plane_params);
1041
0
        if (code >= 0) {
1042
0
            *params = plane_params;
1043
0
            params->options = (params->options & ~GB_PACKING_ALL) |
1044
0
                (GB_PACKING_PLANAR | GB_SELECT_PLANES);
1045
0
            params->data[plane_index] = params->data[0];
1046
0
            for (plane = 0; plane < dev->color_info.num_components; ++plane)
1047
0
                if (plane != plane_index)
1048
0
                    params->data[plane] = 0;
1049
0
        }
1050
0
    } else if (!(~options & (GB_COLORS_NATIVE | GB_ALPHA_NONE |
1051
0
                             GB_PACKING_CHUNKY | GB_RETURN_COPY |
1052
0
                             GB_ALIGN_STANDARD | GB_OFFSET_0 |
1053
0
                             GB_RASTER_STANDARD))) {
1054
        /* Expand the plane into chunky pixels. */
1055
0
        bits_plane_t dest, source;
1056
1057
0
        dest.data.write = params->data[0];
1058
0
        dest.raster =
1059
0
            bitmap_raster((prect->q.x - prect->p.x) * dev->color_info.depth);
1060
0
        dest.depth = edev->color_info.depth;
1061
0
        dest.x = 0;
1062
1063
        /* not source.data, source.raster, source.x */
1064
0
        source.depth = plane_dev->color_info.depth;
1065
1066
0
        plane_params = *params;
1067
0
        plane_params.options = options &=
1068
0
            (~(GB_COLORS_ALL | GB_ALPHA_ALL | GB_PACKING_ALL |
1069
0
               GB_RETURN_ALL | GB_ALIGN_ALL | GB_OFFSET_ALL | GB_RASTER_ALL) |
1070
0
             GB_COLORS_NATIVE | GB_ALPHA_NONE | GB_PACKING_CHUNKY |
1071
             /* Try for a pointer return the first time. */
1072
0
             GB_RETURN_POINTER |
1073
0
             GB_ALIGN_STANDARD |
1074
0
             (GB_OFFSET_0 | GB_OFFSET_ANY) |
1075
0
             (GB_RASTER_STANDARD | GB_RASTER_ANY));
1076
0
        plane_params.raster = gx_device_raster(plane_dev, true);
1077
0
        code = dev_proc(plane_dev, get_bits_rectangle)
1078
0
            (plane_dev, prect, &plane_params);
1079
0
        if (code >= 0) {
1080
            /* Success, expand the plane into pixels. */
1081
0
            source.data.read = plane_params.data[0];
1082
0
            source.raster = plane_params.raster;
1083
0
            source.x = params->x_offset;
1084
0
            code = bits_expand_plane(&dest, &source, edev->plane.shift,
1085
0
                                     prect->q.x - prect->p.x,
1086
0
                                     prect->q.y - prect->p.y);
1087
0
        }
1088
0
        params->options = (options & ~GB_RETURN_POINTER) | GB_RETURN_COPY;
1089
0
    } else
1090
0
        return gx_default_get_bits_rectangle(dev, prect, params);
1091
0
    return code;
1092
0
}