Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/base/gdevplnx.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 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
    uint full_size = width_raster * height;
309
310
0
    pts->edev = edev;
311
0
    pts->data = data, pts->data_x = data_x, pts->raster = raster;
312
0
    pts->width = width, pts->height = height;
313
0
    pts->dest_x = 0;
314
0
    if (full_size <= buffer_size) {
315
0
        pts->buffer.data = local_buffer;
316
0
        pts->buffer.size = buffer_size;
317
0
        pts->buffer.raster = width_raster;
318
0
        pts->buffer.on_heap = false;
319
0
        pts->size.x = width, pts->size.y = height;
320
0
    } else if (partial_ok) {
321
0
        pts->buffer.data = local_buffer;
322
0
        pts->buffer.size = buffer_size;
323
0
        pts->buffer.on_heap = false;
324
0
        if (buffer_size >= width_raster) {
325
0
            pts->buffer.raster = width_raster;
326
0
            pts->size.x = width;
327
0
            pts->size.y = buffer_size / width_raster;
328
0
        } else {
329
0
            pts->buffer.raster = buffer_size & -align_bitmap_mod;
330
0
            pts->size.x =
331
0
                pts->buffer.raster * (8 / edev->plane_dev->color_info.depth);
332
0
            pts->size.y = 1;
333
0
        }
334
0
    } else {
335
0
        pts->buffer.data =
336
0
            gs_alloc_bytes(edev->memory, full_size, "begin_tiling");
337
0
        if (!pts->buffer.data)
338
0
            return_error(gs_error_VMerror);
339
0
        pts->buffer.size = full_size;
340
0
        pts->buffer.raster = width_raster;
341
0
        pts->buffer.on_heap = true;
342
0
        pts->size.x = width, pts->size.y = height;
343
0
    }
344
0
    pts->buffer.raster = width_raster;
345
0
    pts->offset.x = pts->offset.y = 0;
346
0
    pts->per_tile_width = pts->size.x;
347
0
    return pts->buffer.size < full_size;
348
0
}
349
350
/*
351
 * Advance to the next tile.  Return true if there are more tiles to do.
352
 */
353
static bool
354
next_tile(tiling_state_t *pts)
355
0
{
356
0
    if ((pts->offset.x += pts->size.x) >= pts->width) {
357
0
        if ((pts->offset.y += pts->size.y) >= pts->height)
358
0
            return false;
359
0
        pts->offset.x = 0;
360
0
        pts->size.x = pts->per_tile_width;
361
0
        if (pts->offset.y + pts->size.y >= pts->height)
362
0
            pts->size.y = pts->height - pts->offset.y;
363
0
    } else if (pts->offset.x + pts->size.x >= pts->width)
364
0
        pts->size.x = pts->width - pts->offset.x;
365
0
    return true;
366
0
}
367
368
/*
369
 * Finish tiling by freeing the buffer if necessary.
370
 */
371
static void
372
end_tiling(tiling_state_t *pts)
373
0
{
374
0
    if (pts->buffer.on_heap)
375
0
        gs_free_object(pts->edev->memory, pts->buffer.data, "end_tiling");
376
0
}
377
378
/* ---------------- Initialization ---------------- */
379
380
int
381
plane_device_init(gx_device_plane_extract *edev, gx_device *target,
382
    gx_device *plane_dev, const gx_render_plane_t *render_plane, bool clear)
383
0
{
384
0
    int code;
385
    /* Check for compatibility of the plane specification. */
386
0
    if (render_plane->depth > plane_dev->color_info.depth)
387
0
        return_error(gs_error_rangecheck);
388
0
    code = gx_device_init((gx_device *)edev,
389
0
                          (const gx_device *)&gs_plane_extract_device,
390
0
                          edev->memory, true);
391
0
    if (code < 0)
392
0
        return code;
393
0
    check_device_separable((gx_device *)edev);
394
0
    gx_device_forward_fill_in_procs((gx_device_forward *)edev);
395
0
    gx_device_set_target((gx_device_forward *)edev, target);
396
0
    gx_device_copy_params((gx_device *)edev, target);
397
0
    edev->plane_dev = plane_dev;
398
0
    gx_device_retain(plane_dev, true);
399
0
    edev->plane = *render_plane;
400
0
    plane_open_device((gx_device *)edev);
401
0
    if (clear) {
402
0
        dev_proc(plane_dev, fill_rectangle)
403
0
            (plane_dev, 0, 0, plane_dev->width, plane_dev->height,
404
0
             edev->plane_white);
405
0
        edev->any_marks = false;
406
0
    }
407
0
    return 0;
408
0
}
409
410
/* ---------------- Driver procedures ---------------- */
411
412
static int
413
plane_open_device(gx_device *dev)
414
0
{
415
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
416
0
    gx_device * const plane_dev = edev->plane_dev;
417
0
    int plane_depth = plane_dev->color_info.depth;
418
0
    const gdev_mem_functions *fns =
419
0
                               gdev_mem_functions_for_bits(plane_depth);
420
421
0
    edev->plane_white = gx_device_white(plane_dev);
422
0
    edev->plane_mask = (1 << plane_depth) - 1;
423
0
    edev->plane_dev_is_memory = fns != NULL &&
424
0
                     dev_proc(plane_dev, copy_color) == fns->copy_color;
425
    /* We don't set or clear any_marks here: see ...init above. */
426
0
    return 0;
427
0
}
428
429
static int
430
plane_fill_rectangle(gx_device *dev,
431
    int x, int y, int w, int h, gx_color_index color)
432
0
{
433
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
434
0
    gx_device * const plane_dev = edev->plane_dev;
435
0
    gx_color_index pixel = COLOR_PIXEL(edev, color);
436
437
0
    if (pixel != edev->plane_white)
438
0
        edev->any_marks = true;
439
0
    else if (!edev->any_marks)
440
0
        return 0;
441
0
    return dev_proc(plane_dev, fill_rectangle)
442
0
        (plane_dev, x, y, w, h, pixel);
443
0
}
444
445
static int
446
plane_copy_mono(gx_device *dev,
447
    const byte *data, int data_x, int raster, gx_bitmap_id id,
448
    int x, int y, int w, int h,
449
    gx_color_index color0, gx_color_index color1)
450
0
{
451
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
452
0
    gx_device * const plane_dev = edev->plane_dev;
453
0
    gx_color_index pixel0 = TRANS_COLOR_PIXEL(edev, color0);
454
0
    gx_color_index pixel1 = TRANS_COLOR_PIXEL(edev, color1);
455
456
0
    if (pixel0 == pixel1)
457
0
        return plane_fill_rectangle(dev, x, y, w, h, color0);
458
0
    if ((pixel0 == edev->plane_white || pixel0 == gx_no_color_index) &&
459
0
        (pixel1 == edev->plane_white || pixel1 == gx_no_color_index)) {
460
        /* This operation will only write white. */
461
0
        if (!edev->any_marks)
462
0
            return 0;
463
0
    } else
464
0
        edev->any_marks = true;
465
0
    return dev_proc(plane_dev, copy_mono)
466
0
        (plane_dev, data, data_x, raster, id, x, y, w, h, pixel0, pixel1);
467
0
}
468
469
static int
470
plane_copy_color(gx_device *dev,
471
    const byte *data, int data_x, int raster, gx_bitmap_id id,
472
    int x, int y, int w, int h)
473
0
{
474
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
475
0
    gx_device * const plane_dev = edev->plane_dev;
476
0
    tiling_state_t state;
477
0
    long buf[COPY_COLOR_BUF_SIZE / sizeof(long)];
478
0
    int code;
479
480
0
    if (edev->plane_dev_is_memory) {
481
        /* Reduce the source directly into the plane device. */
482
0
        gx_device_memory * const mdev = (gx_device_memory *)plane_dev;
483
484
0
        fit_copy(edev, data, data_x, raster, id, x, y, w, h);
485
0
        code = begin_tiling(&state, edev, data, data_x, raster, w, h,
486
0
                            scan_line_base(mdev, y), max_uint, false);
487
0
        if (code < 0)
488
0
            return code;
489
0
        state.dest_x = x;
490
0
        state.buffer.raster = mdev->raster;
491
0
        extract_partial_tile(&state);
492
0
        end_tiling(&state);
493
0
        edev->any_marks = true;
494
0
        return 0;
495
0
    }
496
0
    code = begin_tiling(&state, edev, data, data_x, raster,
497
0
                        w, h, (byte *)buf, sizeof(buf), true);
498
0
    if (code < 0)
499
0
        return code;
500
0
    do {
501
0
        extract_partial_tile(&state);
502
0
        code = dev_proc(plane_dev, copy_color)
503
0
            (plane_dev, state.buffer.data, 0, state.buffer.raster,
504
0
             gx_no_bitmap_id, x + state.offset.x, y + state.offset.y,
505
0
             state.size.x, state.size.y);
506
0
    } while (code >= 0 && next_tile(&state));
507
0
    end_tiling(&state);
508
0
    edev->any_marks = true;
509
0
    return code;
510
0
}
511
512
static int
513
plane_copy_alpha(gx_device *dev, const byte *data, int data_x,
514
    int raster, gx_bitmap_id id, int x, int y, int w, int h,
515
    gx_color_index color, int depth)
516
0
{
517
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
518
0
    gx_device * const plane_dev = edev->plane_dev;
519
0
    gx_color_index pixel = COLOR_PIXEL(edev, color);
520
521
0
    if (pixel != edev->plane_white)
522
0
        edev->any_marks = true;
523
0
    else if (!edev->any_marks)
524
0
        return 0;
525
0
    return dev_proc(plane_dev, copy_alpha)
526
0
        (plane_dev, data, data_x, raster, id, x, y, w, h, pixel, depth);
527
0
}
528
529
static int
530
plane_fill_path(gx_device *dev,
531
    const gs_gstate *pgs, gx_path *ppath,
532
    const gx_fill_params *params,
533
    const gx_drawing_color *pdevc, const gx_clip_path *pcpath)
534
0
{
535
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
536
0
    gx_device * const plane_dev = edev->plane_dev;
537
0
    gs_logical_operation_t lop_orig =
538
0
        gs_current_logical_op((const gs_gstate *)pgs);
539
0
    gs_logical_operation_t lop = lop_orig;
540
0
    gx_device_color dcolor;
541
542
0
    switch (reduce_drawing_color(&dcolor, edev, pdevc, &lop)) {
543
0
    case REDUCE_SKIP:
544
0
        return 0;
545
0
    case REDUCE_DRAW: {
546
0
        gs_gstate lopgs;
547
0
        const gs_gstate *pgs_draw = pgs;
548
549
0
        if (lop != lop_orig) {
550
0
            lopgs = *pgs;
551
0
            gs_set_logical_op((gs_gstate *)&lopgs, lop);
552
0
            pgs_draw = &lopgs;
553
0
        }
554
0
        return dev_proc(plane_dev, fill_path)
555
0
            (plane_dev, pgs_draw, ppath, params, &dcolor, pcpath);
556
0
    }
557
0
    default /*REDUCE_FAILED*/:
558
0
        return gx_default_fill_path(dev, pgs, ppath, params, pdevc, pcpath);
559
0
    }
560
0
}
561
562
static int
563
plane_stroke_path(gx_device *dev,
564
    const gs_gstate *pgs, gx_path *ppath,
565
    const gx_stroke_params *params,
566
    const gx_drawing_color *pdevc, const gx_clip_path *pcpath)
567
0
{
568
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
569
0
    gx_device * const plane_dev = edev->plane_dev;
570
0
    gs_logical_operation_t lop_orig =
571
0
        gs_current_logical_op((const gs_gstate *)pgs);
572
0
    gs_logical_operation_t lop = lop_orig;
573
0
    gx_device_color dcolor;
574
575
0
    switch (reduce_drawing_color(&dcolor, edev, pdevc, &lop)) {
576
0
    case REDUCE_SKIP:
577
0
        return 0;
578
0
    case REDUCE_DRAW: {
579
0
        gs_gstate lopgs;
580
0
        const gs_gstate *pgs_draw = pgs;
581
582
0
        if (lop != lop_orig) {
583
0
            lopgs = *pgs;
584
0
            gs_set_logical_op((gs_gstate *)&lopgs, lop);
585
0
            pgs_draw = &lopgs;
586
0
        }
587
0
        return dev_proc(plane_dev, stroke_path)
588
0
            (plane_dev, pgs_draw, ppath, params, &dcolor, pcpath);
589
0
    }
590
0
    default /*REDUCE_FAILED*/:
591
0
        return gx_default_stroke_path(dev, pgs, ppath, params, pdevc, pcpath);
592
0
    }
593
0
}
594
595
static int
596
plane_fill_mask(gx_device *dev,
597
    const byte *data, int data_x, int raster, gx_bitmap_id id,
598
    int x, int y, int w, int h,
599
    const gx_drawing_color *pdcolor, int depth,
600
    gs_logical_operation_t lop, const gx_clip_path *pcpath)
601
0
{
602
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
603
0
    gx_device * const plane_dev = edev->plane_dev;
604
0
    gx_device_color dcolor;
605
606
0
    switch (reduce_drawing_color(&dcolor, edev, pdcolor, &lop)) {
607
0
    case REDUCE_SKIP:
608
0
        return 0;
609
0
    case REDUCE_DRAW:
610
0
        return dev_proc(plane_dev, fill_mask)
611
0
            (plane_dev, data, data_x, raster, gx_no_bitmap_id, x, y, w, h,
612
0
             &dcolor, depth, lop, pcpath);
613
0
    default /*REDUCE_FAILED*/:
614
0
        return gx_default_fill_mask(dev, data, data_x, raster, gx_no_bitmap_id,
615
0
                                    x, y, w, h, &dcolor, depth, lop, pcpath);
616
0
    }
617
0
}
618
619
static int
620
plane_fill_parallelogram(gx_device * dev,
621
    fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
622
    const gx_drawing_color * pdcolor, gs_logical_operation_t lop)
623
0
{
624
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
625
0
    gx_device * const plane_dev = edev->plane_dev;
626
0
    gx_device_color dcolor;
627
628
0
    switch (reduce_drawing_color(&dcolor, edev, pdcolor, &lop)) {
629
0
    case REDUCE_SKIP:
630
0
        return 0;
631
0
    case REDUCE_DRAW:
632
0
        return dev_proc(plane_dev, fill_parallelogram)
633
0
            (plane_dev, px, py, ax, ay, bx, by, &dcolor, lop);
634
0
    default /*REDUCE_FAILED*/:
635
0
        return gx_default_fill_parallelogram(dev, px, py, ax, ay, bx, by,
636
0
                                             pdcolor, lop);
637
0
    }
638
0
}
639
640
static int
641
plane_fill_triangle(gx_device * dev,
642
    fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
643
    const gx_drawing_color * pdcolor, gs_logical_operation_t lop)
644
0
{
645
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
646
0
    gx_device * const plane_dev = edev->plane_dev;
647
0
    gx_device_color dcolor;
648
649
0
    switch (reduce_drawing_color(&dcolor, edev, pdcolor, &lop)) {
650
0
    case REDUCE_SKIP:
651
0
        return 0;
652
0
    case REDUCE_DRAW:
653
0
        return dev_proc(plane_dev, fill_triangle)
654
0
            (plane_dev, px, py, ax, ay, bx, by, &dcolor, lop);
655
0
    default /*REDUCE_FAILED*/:
656
0
        return gx_default_fill_triangle(dev, px, py, ax, ay, bx, by,
657
0
                                        pdcolor, lop);
658
0
    }
659
0
}
660
661
static int
662
plane_strip_tile_rectangle(gx_device *dev,
663
    const gx_strip_bitmap *tiles, int x, int y, int w, int h,
664
    gx_color_index color0, gx_color_index color1,
665
    int phase_x, int phase_y)
666
0
{
667
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
668
0
    gx_device * const plane_dev = edev->plane_dev;
669
0
    gx_color_index pixel0 = TRANS_COLOR_PIXEL(edev, color0);
670
0
    gx_color_index pixel1 = TRANS_COLOR_PIXEL(edev, color1);
671
672
0
    if (pixel0 == pixel1) {
673
0
        if (pixel0 != gx_no_color_index)
674
0
            return plane_fill_rectangle(dev, x, y, w, h, color0);
675
        /* The tile is a pixmap rather than a bitmap. */
676
        /* We should use the default implementation if it is small.... */
677
0
        {
678
0
            gx_strip_bitmap plane_tile;
679
0
            tiling_state_t state;
680
0
            long buf[TILE_RECTANGLE_BUF_SIZE / sizeof(long)];
681
0
            int code = begin_tiling(&state, edev, tiles->data, 0, tiles->raster,
682
0
                        tiles->size.x, tiles->size.y,
683
0
                                (byte *)buf, sizeof(buf), false);
684
685
0
            if (code < 0)
686
0
                return gx_default_strip_tile_rectangle(dev, tiles, x, y, w, h,
687
0
                                        color0, color1, phase_x, phase_y);
688
0
            extract_partial_tile(&state);
689
0
            plane_tile = *tiles;
690
0
            plane_tile.data = state.buffer.data;
691
0
            plane_tile.raster = state.buffer.raster;
692
0
            plane_tile.id = gx_no_bitmap_id;
693
0
            code = dev_proc(plane_dev, strip_tile_rectangle)
694
0
                (plane_dev, &plane_tile, x, y, w, h, pixel0, pixel1,
695
0
                 phase_x, phase_y);
696
0
            end_tiling(&state);
697
0
            edev->any_marks = true;
698
0
            return code;
699
0
        }
700
0
    }
701
0
    if ((pixel0 == edev->plane_white || pixel0 == gx_no_color_index) &&
702
0
        (pixel1 == edev->plane_white || pixel1 == gx_no_color_index)) {
703
        /* This operation will only write white. */
704
0
        if (!edev->any_marks)
705
0
            return 0;
706
0
    } else
707
0
        edev->any_marks = true;
708
0
    return dev_proc(plane_dev, strip_tile_rectangle)
709
0
        (plane_dev, tiles, x, y, w, h, pixel0, pixel1, phase_x, phase_y);
710
0
}
711
712
static int
713
plane_strip_copy_rop2(gx_device *dev,
714
    const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
715
    const gx_color_index *scolors,
716
    const gx_strip_bitmap *textures, const gx_color_index *tcolors,
717
    int x, int y, int w, int h,
718
    int phase_x, int phase_y, gs_logical_operation_t lop,
719
    uint plane_height)
720
0
{
721
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
722
0
    gx_device * const plane_dev = edev->plane_dev;
723
0
    gs_rop3_t rop = lop_sanitize(lop);
724
0
    struct crp_ {
725
0
        gx_color_index pixels[2];
726
0
        gx_color_index *colors;
727
0
        tiling_state_t state;
728
0
    } source, texture;
729
0
    long sbuf[COPY_ROP_SOURCE_BUF_SIZE / sizeof(long)];
730
0
    long tbuf[COPY_ROP_TEXTURE_BUF_SIZE / sizeof(long)];
731
0
    const byte *plane_source;
732
0
    uint plane_raster = 0xbaadf00d; /* Initialize against indeterminizm. */
733
0
    gx_strip_bitmap plane_texture;
734
0
    const gx_strip_bitmap *plane_textures = NULL;
735
0
    int code;
736
737
0
    if (!rop3_uses_S(rop)) {
738
0
        sdata = 0;
739
0
        source.colors = 0;
740
0
    } else if (scolors) {
741
0
        source.pixels[0] = COLOR_PIXEL(edev, scolors[0]);
742
0
        source.pixels[1] = COLOR_PIXEL(edev, scolors[1]);
743
0
        if (source.pixels[0] == source.pixels[1])
744
0
            sdata = 0;
745
0
        source.colors = source.pixels;
746
0
    }
747
0
    else
748
0
        source.colors = 0;
749
0
    if (!rop3_uses_T(rop)) {
750
0
        textures = 0;
751
0
        texture.colors = 0;
752
0
    } else if (tcolors) {
753
0
        texture.pixels[0] = COLOR_PIXEL(edev, tcolors[0]);
754
0
        texture.pixels[1] = COLOR_PIXEL(edev, tcolors[1]);
755
0
        if (texture.pixels[0] == texture.pixels[1])
756
0
            textures = 0;
757
0
        texture.colors = texture.pixels;
758
0
    }
759
0
    else
760
0
        texture.colors = 0;
761
0
    if (sdata) {
762
0
        code = begin_tiling(&source.state, edev, sdata, sourcex, sraster, w, y,
763
0
                            (byte *)sbuf, sizeof(sbuf), true);
764
0
        if (code < 0)
765
0
            return gx_default_strip_copy_rop2(dev, sdata, sourcex, sraster, id,
766
0
                                              scolors, textures, tcolors,
767
0
                                              x, y, w, h, phase_x, phase_y, rop,
768
0
                                              plane_height);
769
0
        plane_source = source.state.buffer.data;
770
0
        plane_raster = source.state.buffer.raster;
771
0
    } else
772
0
        plane_source = 0;
773
0
    if (textures) {
774
0
        code = begin_tiling(&texture.state, edev, textures->data, 0,
775
0
                            textures->raster, textures->size.x,
776
0
                            textures->size.y, (byte *)tbuf, sizeof(tbuf),
777
0
                            false);
778
0
        if (code < 0) {
779
0
            if (plane_source)
780
0
                end_tiling(&source.state);
781
0
            return code;
782
0
        }
783
0
        plane_texture = *textures;
784
0
        plane_texture.data = texture.state.buffer.data;
785
0
        plane_texture.raster = texture.state.buffer.raster;
786
0
        plane_textures = &plane_texture;
787
0
    }
788
0
    if (textures)
789
0
        extract_partial_tile(&texture.state);
790
0
    do {
791
0
        if (sdata)
792
0
            extract_partial_tile(&source.state);
793
0
        code = dev_proc(plane_dev, strip_copy_rop2)
794
0
            (plane_dev, plane_source, sourcex, plane_raster, gx_no_bitmap_id,
795
0
             source.colors, plane_textures, texture.colors,
796
0
             x, y, w, h, phase_x, phase_y, rop, plane_height);
797
0
    } while (code >= 0 && sdata && next_tile(&source.state));
798
0
    if (textures)
799
0
        end_tiling(&texture.state);
800
0
    if (sdata)
801
0
        end_tiling(&source.state);
802
0
    return code;
803
0
}
804
805
/* ---------------- Images ---------------- */
806
807
/* Define the state for image rendering. */
808
typedef struct plane_image_enum_s {
809
    gx_image_enum_common;
810
    gx_image_enum_common_t *info; /* plane device enumerator */
811
    gs_gstate *pgs_image; /* modified gs_gstate state */
812
} plane_image_enum_t;
813
/* Note that we include the pgs_image which is 'bytes' type (not gs_gstate) */
814
/* It still needs to be traced so that a GC won't free it prematurely.      */
815
gs_private_st_suffix_add2(st_plane_image_enum, plane_image_enum_t,
816
  "plane_image_enum_t", plane_image_enum_enum_ptrs,
817
  plane_image_enum_reloc_ptrs, st_gx_image_enum_common, info, pgs_image);
818
819
/*
820
 * Reduce drawing colors returned by color mapping.  Note that these
821
 * assume that the call of reduce_drawing_color will not fail:
822
 * plane_begin_typed_image must ensure this.
823
 *
824
 * In the gs_gstate passed to these procedures, the client data is
825
 * the plane_image_enum_t.
826
 */
827
828
static void
829
plane_cmap_gray(frac gray, gx_device_color * pdc,
830
    const gs_gstate *pgs_image, gx_device *dev, gs_color_select_t select)
831
0
{
832
0
    const plane_image_enum_t *ppie =
833
0
        (const plane_image_enum_t *)pgs_image->client_data;
834
0
    gx_device_plane_extract * const edev =
835
0
        (gx_device_plane_extract *)ppie->dev;
836
0
    gs_logical_operation_t lop = gs_current_logical_op_inline(pgs_image);
837
0
    gx_device_color dcolor;
838
839
0
    gx_remap_concrete_gray(gray, &dcolor, ppie->pgs,
840
0
                           (gx_device *)edev, select);
841
0
    reduce_drawing_color(pdc, edev, &dcolor, &lop);
842
0
}
843
static void
844
plane_cmap_rgb(frac r, frac g, frac b, gx_device_color * pdc,
845
    const gs_gstate *pgs_image, gx_device *dev, gs_color_select_t select)
846
0
{
847
0
    const plane_image_enum_t *ppie =
848
0
        (const plane_image_enum_t *)pgs_image->client_data;
849
0
    gx_device_plane_extract * const edev =
850
0
        (gx_device_plane_extract *)ppie->dev;
851
0
    gs_logical_operation_t lop = gs_current_logical_op_inline(pgs_image);
852
0
    gx_device_color dcolor;
853
854
0
    gx_remap_concrete_rgb(r, g, b, &dcolor, ppie->pgs,
855
0
                          (gx_device *)edev, select);
856
0
    reduce_drawing_color(pdc, edev, &dcolor, &lop);
857
0
}
858
static void
859
plane_cmap_cmyk(frac c, frac m, frac y, frac k, gx_device_color * pdc,
860
    const gs_gstate *pgs_image, gx_device *dev, gs_color_select_t select,
861
    const gs_color_space *source_pcs)
862
0
{
863
0
    const plane_image_enum_t *ppie =
864
0
        (const plane_image_enum_t *)pgs_image->client_data;
865
0
    gx_device_plane_extract * const edev =
866
0
        (gx_device_plane_extract *)ppie->dev;
867
0
    gs_logical_operation_t lop = gs_current_logical_op_inline(pgs_image);
868
0
    gx_device_color dcolor;
869
870
0
    gx_remap_concrete_cmyk(c, m, y, k, &dcolor, ppie->pgs,
871
0
                           (gx_device *)edev, select, NULL);
872
0
    reduce_drawing_color(pdc, edev, &dcolor, &lop);
873
0
}
874
static bool
875
plane_cmap_is_halftoned(const gs_gstate *pgs_image, gx_device *dev)
876
0
{
877
0
    return false;
878
0
}
879
880
static const gx_color_map_procs plane_color_map_procs = {
881
    plane_cmap_gray, plane_cmap_rgb, plane_cmap_cmyk,
882
    NULL, NULL, plane_cmap_is_halftoned
883
};
884
static const gx_color_map_procs *
885
plane_get_cmap_procs(const gs_gstate *pgs, const gx_device *dev)
886
0
{
887
0
    return &plane_color_map_procs;
888
0
}
889
890
/* Define the image processing procedures. */
891
static image_enum_proc_plane_data(plane_image_plane_data);
892
static image_enum_proc_end_image(plane_image_end_image);
893
static const gx_image_enum_procs_t plane_image_enum_procs = {
894
    plane_image_plane_data, plane_image_end_image
895
};
896
897
static int
898
plane_begin_typed_image(gx_device * dev,
899
                        const gs_gstate * pgs, const gs_matrix * pmat,
900
                   const gs_image_common_t * pic, const gs_int_rect * prect,
901
              const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
902
                      gs_memory_t * memory, gx_image_enum_common_t ** pinfo)
903
0
{
904
    /*
905
     * For images, we intercept the gs_gstate's cmap_procs and apply
906
     * reduce_drawing_color to the colors as they are returned to the image
907
     * processing code.  For reasons explained above, we can't do this in
908
     * some cases of RasterOp that include transparency.
909
     */
910
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
911
0
    gs_logical_operation_t lop = gs_current_logical_op((const gs_gstate *)pgs);
912
0
    const gs_pixel_image_t *pim;
913
0
    plane_image_enum_t *info = 0;
914
0
    gs_gstate *pgs_image = 0;
915
0
    gx_device_color dcolor;
916
0
    bool uses_color = false;
917
0
    int code;
918
919
    /* We can only handle a limited set of image types. */
920
0
    switch (pic->type->index) {
921
0
    case 1: {
922
0
        const gs_image1_t * const pim1 = (const gs_image1_t *)pic;
923
924
0
        if (pim1->Alpha != gs_image_alpha_none)
925
0
            goto fail;
926
0
        uses_color = pim1->ImageMask;
927
0
        break;
928
0
        }
929
0
    case 3:
930
0
    case 4:
931
0
        break;
932
0
    default:
933
0
        goto fail;
934
0
    }
935
0
    pim = (const gs_pixel_image_t *)pic;
936
0
    lop = lop_sanitize(lop);
937
0
    if (uses_color || (pim->CombineWithColor && lop_uses_T(lop))) {
938
0
        if (reduce_drawing_color(&dcolor, edev, pdcolor, &lop) ==
939
0
            REDUCE_FAILED)
940
0
            goto fail;
941
0
    } else {
942
        /*
943
         * The drawing color won't be used, but if RasterOp is involved,
944
         * it may still be accessed in some anomalous cases.
945
         */
946
0
        set_nonclient_dev_color(&dcolor, (gx_color_index)0);
947
0
    }
948
0
    info = gs_alloc_struct(memory, plane_image_enum_t, &st_plane_image_enum,
949
0
                           "plane_image_begin_typed(info)");
950
0
    pgs_image = gs_gstate_copy(pgs, memory);
951
0
    if (pgs_image == 0 || info == 0)
952
0
        goto fail;
953
0
    pgs_image->client_data = info;
954
0
    pgs_image->get_cmap_procs = plane_get_cmap_procs;
955
0
    code = dev_proc(edev->plane_dev, begin_typed_image)
956
0
        (edev->plane_dev, pgs_image, pmat, pic, prect,
957
0
         &dcolor, pcpath, memory, &info->info);
958
0
    if (code < 0)
959
0
        goto fail;
960
0
    *((gx_image_enum_common_t *)info) = *info->info;
961
0
    info->procs = &plane_image_enum_procs;
962
0
    info->dev = (gx_device *)edev;
963
0
    info->id = gs_next_ids(memory, 1);
964
0
    info->memory = memory;
965
0
    info->pgs = pgs;
966
0
    info->pgs_level = pgs->level;
967
0
    info->pgs_image = pgs_image;
968
0
    *pinfo = (gx_image_enum_common_t *)info;
969
0
    return code;
970
0
fail:
971
0
    gs_free_object(memory, pgs_image, "plane_image_begin_typed(pgs_image)");
972
0
    gs_free_object(memory, info, "plane_image_begin_typed(info)");
973
0
    return gx_default_begin_typed_image(dev, pgs, pmat, pic, prect,
974
0
                                        pdcolor, pcpath, memory, pinfo);
975
0
}
976
977
static int
978
plane_image_plane_data(gx_image_enum_common_t * info,
979
                       const gx_image_plane_t * planes, int height,
980
                       int *rows_used)
981
0
{
982
0
    plane_image_enum_t * const ppie = (plane_image_enum_t *)info;
983
984
0
    if (info->pgs!= NULL && info->pgs->level < info->pgs_level)
985
0
        return_error(gs_error_undefinedresult);
986
987
0
    return gx_image_plane_data_rows(ppie->info, planes, height, rows_used);
988
0
}
989
990
static int
991
plane_image_end_image(gx_image_enum_common_t * info, bool draw_last)
992
0
{
993
0
    plane_image_enum_t * const ppie = (plane_image_enum_t *)info;
994
0
    int code = gx_image_end(ppie->info, draw_last);
995
996
0
    ppie->pgs_image->client_data = NULL;       /* this isn't a complete client_data struct */
997
0
    gs_free_object(ppie->memory, ppie->pgs_image,
998
0
                   "plane_image_end_image(pgs_image)");
999
0
    gx_image_free_enum(&info);
1000
0
    return code;
1001
0
}
1002
1003
/* ---------------- Reading back bits ---------------- */
1004
1005
static int
1006
plane_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
1007
                         gs_get_bits_params_t * params)
1008
0
{
1009
0
    gx_device_plane_extract * const edev = (gx_device_plane_extract *)dev;
1010
0
    gx_device * const plane_dev = edev->plane_dev;
1011
0
    int plane_index = edev->plane.index;
1012
0
    gs_get_bits_options_t options = params->options;
1013
0
    gs_get_bits_params_t plane_params;
1014
0
    uchar plane;
1015
0
    int code;
1016
1017
    /*
1018
     * The only real option that this device supports is single-plane
1019
     * retrieval.  However, for the default case of RasterOp, it must be
1020
     * able to return chunky pixels in which the other components are
1021
     * arbitrary (but might as well be zero).
1022
     */
1023
0
    if ((options & GB_PACKING_PLANAR) && (options & GB_SELECT_PLANES)) {
1024
0
        if (params->data[plane_index] == 0)
1025
0
            return gx_default_get_bits_rectangle(dev, prect, params);
1026
        /* If the caller wants any other plane(s), punt. */
1027
0
        for (plane = 0; plane < dev->color_info.num_components; ++plane)
1028
0
            if (plane != plane_index && params->data[plane] != 0)
1029
0
                return gx_default_get_bits_rectangle(dev, prect, params);
1030
        /* Pass the request on to the plane device. */
1031
0
        plane_params = *params;
1032
0
        plane_params.options =
1033
0
            (options & ~(GB_PACKING_ALL | GB_SELECT_PLANES)) |
1034
0
            GB_PACKING_CHUNKY;
1035
0
        plane_params.data[0] = params->data[plane_index];
1036
0
        code = dev_proc(plane_dev, get_bits_rectangle)
1037
0
            (plane_dev, prect, &plane_params);
1038
0
        if (code >= 0) {
1039
0
            *params = plane_params;
1040
0
            params->options = (params->options & ~GB_PACKING_ALL) |
1041
0
                (GB_PACKING_PLANAR | GB_SELECT_PLANES);
1042
0
            params->data[plane_index] = params->data[0];
1043
0
            for (plane = 0; plane < dev->color_info.num_components; ++plane)
1044
0
                if (plane != plane_index)
1045
0
                    params->data[plane] = 0;
1046
0
        }
1047
0
    } else if (!(~options & (GB_COLORS_NATIVE | GB_ALPHA_NONE |
1048
0
                             GB_PACKING_CHUNKY | GB_RETURN_COPY |
1049
0
                             GB_ALIGN_STANDARD | GB_OFFSET_0 |
1050
0
                             GB_RASTER_STANDARD))) {
1051
        /* Expand the plane into chunky pixels. */
1052
0
        bits_plane_t dest, source;
1053
1054
0
        dest.data.write = params->data[0];
1055
0
        dest.raster =
1056
0
            bitmap_raster((prect->q.x - prect->p.x) * dev->color_info.depth);
1057
0
        dest.depth = edev->color_info.depth;
1058
0
        dest.x = 0;
1059
1060
        /* not source.data, source.raster, source.x */
1061
0
        source.depth = plane_dev->color_info.depth;
1062
1063
0
        plane_params = *params;
1064
0
        plane_params.options = options &=
1065
0
            (~(GB_COLORS_ALL | GB_ALPHA_ALL | GB_PACKING_ALL |
1066
0
               GB_RETURN_ALL | GB_ALIGN_ALL | GB_OFFSET_ALL | GB_RASTER_ALL) |
1067
0
             GB_COLORS_NATIVE | GB_ALPHA_NONE | GB_PACKING_CHUNKY |
1068
             /* Try for a pointer return the first time. */
1069
0
             GB_RETURN_POINTER |
1070
0
             GB_ALIGN_STANDARD |
1071
0
             (GB_OFFSET_0 | GB_OFFSET_ANY) |
1072
0
             (GB_RASTER_STANDARD | GB_RASTER_ANY));
1073
0
        plane_params.raster = gx_device_raster(plane_dev, true);
1074
0
        code = dev_proc(plane_dev, get_bits_rectangle)
1075
0
            (plane_dev, prect, &plane_params);
1076
0
        if (code >= 0) {
1077
            /* Success, expand the plane into pixels. */
1078
0
            source.data.read = plane_params.data[0];
1079
0
            source.raster = plane_params.raster;
1080
0
            source.x = params->x_offset;
1081
0
            code = bits_expand_plane(&dest, &source, edev->plane.shift,
1082
0
                                     prect->q.x - prect->p.x,
1083
0
                                     prect->q.y - prect->p.y);
1084
0
        }
1085
0
        params->options = (options & ~GB_RETURN_POINTER) | GB_RETURN_COPY;
1086
0
    } else
1087
0
        return gx_default_get_bits_rectangle(dev, prect, params);
1088
0
    return code;
1089
0
}