summaryrefslogtreecommitdiff
path: root/base/gxoprect.c
blob: 6b63e98571f9de9ca3ff04b936b28da7d867f7c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* generic (very slow) overprint fill rectangle implementation */

#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsutil.h"             /* for gs_next_ids */
#include "gxdevice.h"
#include "gsdevice.h"
#include "gxgetbit.h"
#include "gxoprect.h"
#include "gsbitops.h"

/*
 * Unpack a scanline for a depth < 8. In this case we know the depth is
 * divisor of 8 and thus a power of 2, which implies that 8 / depth is
 * also a power of 2.
 */
static void
unpack_scanline_lt8(
    gx_color_index *    destp,
    const byte *        srcp,
    int                 src_offset,
    int                 width,
    int                 depth )
{
    byte                buff = 0;
    int                 i = 0, shift = 8 - depth, p_per_byte = 8 / depth;

    /* exit early if nothing to do */
    if (width == 0)
        return;

    /* skip over src_offset */
    if (src_offset >= p_per_byte) {
        srcp += src_offset / p_per_byte;
        src_offset &= (p_per_byte - 1);
    }
    if (src_offset > 0) {
        buff = *srcp++ << (src_offset * depth);
        i = src_offset;
        width += src_offset;
    }

    /* process the interesting part of the scanline */
    for (; i < width; i++, buff <<= depth) {
        if ((i & (p_per_byte - 1)) == 0)
            buff = *srcp++;
        *destp++ = buff >> shift;
    }
}

/*
 * Pack a scanline for a depth of < 8. Note that data prior to dest_offset
 * and any data beyond the width must be left undisturbed.
 */
static void
pack_scanline_lt8(
    const gx_color_index *  srcp,
    byte *                  destp,
    int                     dest_offset,
    int                     width,
    int                     depth )
{
    byte                    buff = 0;
    int                     i = 0, p_per_byte = 8 / depth;

    /* exit early if nothing to do */
    if (width == 0)
        return;

    /* skip over dest_offset */
    if (dest_offset >= p_per_byte) {
        destp += dest_offset / p_per_byte;
        dest_offset &= (p_per_byte - 1);
    }
    if (dest_offset > 0) {
        buff = *destp++ >> (8 - dest_offset * depth);
        i = dest_offset;
        width += dest_offset;
    }

    /* process the interesting part of the scanline */
    for (; i < width; i++) {
        buff = (buff << depth) | *srcp++;
        if ((i & (p_per_byte - 1)) == p_per_byte - 1)
            *destp++ = buff;
    }
    if ((i &= (p_per_byte - 1)) != 0) {
        int     shift = depth * (p_per_byte - i);
        int     mask = (1 << shift) - 1;

        *destp = (*destp & mask) | (buff << shift);
    }
}

/*
 * Unpack a scanline for a depth >= 8. In this case, the depth must be
 * a multiple of 8.
 */
static void
unpack_scanline_ge8(
    gx_color_index *    destp,
    const byte *        srcp,
    int                 src_offset,
    int                 width,
    int                 depth )
{
    gx_color_index      buff = 0;
    int                 i, j, bytes_per_p = depth >> 3;

    /* skip over src_offset */
    srcp += src_offset * bytes_per_p;

    /* process the interesting part of the scanline */
    width *= bytes_per_p;
    for (i = 0, j = 0; i < width; i++) {
        buff = (buff << 8) | *srcp++;
        if (++j == bytes_per_p) {
            *destp++ = buff;
            buff = 0;
            j = 0;
        }
    }
}

/*
 * Pack a scanline for depth >= 8.
 */
static void
pack_scanline_ge8(
    const gx_color_index *  srcp,
    byte *                  destp,
    int                     dest_offset,
    int                     width,
    int                     depth )
{
    gx_color_index          buff = 0;
    int                     i, j, bytes_per_p = depth >> 3;
    int                     shift = depth - 8;

    /* skip over dest_offset */
    destp += dest_offset;

    /* process the interesting part of the scanline */
    width *= bytes_per_p;
    for (i = 0, j = bytes_per_p - 1; i < width; i++, buff <<= 8) {
        if (++j == bytes_per_p) {
            buff = *srcp++;
            j = 0;
        }
        *destp++ = buff >> shift;
    }
}

/*
 * Perform the fill rectangle operation for a non-separable color encoding
 * that requires overprint support. This situation requires that colors be
 * decoded, modified, and re-encoded. These steps must be performed per
 * output pixel, so there is no hope of achieving good performance.
 * Consequently, only minimal performance optimizations are applied below.
 *
 * The overprint device structure is known only in gsovr.c, and thus is not
 * available here. The required information from the overprint device is,
 * therefore, provided via explicit operands.  The device operand points to
 * the target of the overprint compositor device, not the compositor device
 * itself. The drawn_comps bit array and the memory descriptor pointer are
 * also provided explicitly as operands.
 *
 * Returns 0 on success, < 0 in the event of an error.
 */
int
gx_overprint_generic_fill_rectangle(
    gx_device *             tdev,
    gx_color_index          drawn_comps,
    int                     x,
    int                     y,
    int                     w,
    int                     h,
    gx_color_index          color,
    gs_memory_t *           mem )
{
    gx_color_value          src_cvals[GX_DEVICE_COLOR_MAX_COMPONENTS];
    gx_color_index *        pcolor_buff = 0;
    byte *                  gb_buff = 0;
    gs_get_bits_params_t    gb_params;
    gs_int_rect             gb_rect;
    int                     depth = tdev->color_info.depth;
    int                     bit_x, start_x, end_x, raster, code;
    void                    (*unpack_proc)( gx_color_index *,
                                            const byte *,
                                            int, int, int );
    void                    (*pack_proc)( const gx_color_index *,
                                          byte *,
                                          int, int, int );

    fit_fill(tdev, x, y, w, h);
    bit_x = x * depth;
    start_x = bit_x & ~(8 * align_bitmap_mod - 1);
    end_x = bit_x + w * depth;

    /* select the appropriate pack/unpack routines */
    if (depth >= 8) {
        unpack_proc = unpack_scanline_ge8;
        pack_proc = pack_scanline_ge8;
    } else {
        unpack_proc = unpack_scanline_lt8;
        pack_proc = pack_scanline_lt8;
    }

    /* decode the source color */
    if ((code = dev_proc(tdev, decode_color)(tdev, color, src_cvals)) < 0)
        return code;

    /* allocate space for a scanline of color indices */
    pcolor_buff = (gx_color_index *)
                      gs_alloc_bytes( mem,
                                      w *  ARCH_SIZEOF_COLOR_INDEX,
                                      "overprint generic fill rectangle" );
    if (pcolor_buff == 0)
        return gs_note_error(gs_error_VMerror);

    /* allocate a buffer for the returned data */
    raster = bitmap_raster(end_x - start_x);
    gb_buff = gs_alloc_bytes(mem, raster, "overprint generic fill rectangle");
    if (gb_buff == 0) {
        gs_free_object( mem,
                        pcolor_buff,
                        "overprint generic fill rectangle" );
        return gs_note_error(gs_error_VMerror);
    }

    /*
     * Initialize the get_bits parameters. The selection of options is
     * based on the following logic:
     *
     *  - Overprint is only defined with respect to components of the
     *    process color model, so the retrieved information must be kept
     *    in that color model. The gx_bitmap_format_t bitfield regards
     *    this as the native color space.
     *
     *  - Overprinting and alpha compositing don't mix, so there is no
     *    reason to retrieve the alpha information.
     *
     *  - Data should be returned in the depth of the process color
     *    model. Though this depth could be specified explicitly, there
     *    is little reason to do so.
     *
     *  - Though overprint is much more easily implemented with planar
     *    data, there is no planar version of the copy_color method to
     *    send the modified data back to device. Hence, we must retrieve
     *    data in chunky form.
     *
     *  - It is not possible to modify the raster data "in place", as
     *    doing so would bypass any other forwarding devices currently
     *    in the device "stack" (e.g.: a bounding box device). Hence,
     *    we must work with a copy of the data, which is passed to the
     *    copy_color method at the end of fill_rectangle operation.
     *
     *  - Though we only require data for those planes that will not be
     *    modified, there is no benefit to returning less than the full
     *    data for each pixel if the color encoding is not separable.
     *    Since this routine will be used only for encodings that are
     *    not separable, we might as well ask for full information.
     *
     *  - Though no particular alignment and offset are required, it is
     *    useful to make the copy operation as fast as possible. Ideally
     *    we would calculate an offset so that the data achieves optimal
     *    alignment. Alas, some of the devices work much more slowly if
     *    anything but GB_OFFSET_0 is specified, so that is what we use.
     */
    gb_params.options =  GB_COLORS_NATIVE
                       | GB_ALPHA_NONE
                       | GB_DEPTH_ALL
                       | GB_PACKING_CHUNKY
                       | GB_RETURN_COPY
                       | GB_ALIGN_STANDARD
                       | GB_OFFSET_0
                       | GB_RASTER_STANDARD;
    gb_params.x_offset = 0;     /* for consistency */
    gb_params.data[0] = gb_buff;
    gb_params.raster = raster;

    gb_rect.p.x = x;
    gb_rect.q.x = x + w;

    /* process each scanline separately */
    while (h-- > 0 && code >= 0) {
        gx_color_index *    cp = pcolor_buff;
        int                 i;

        gb_rect.p.y = y++;
        gb_rect.q.y = y;
        code = dev_proc(tdev, get_bits_rectangle)( tdev,
                                                   &gb_rect,
                                                   &gb_params );
        if (code < 0)
            break;
        unpack_proc(pcolor_buff, gb_buff, 0, w, depth);
        for (i = 0; i < w; i++, cp++) {
            gx_color_index  comps;
            int             j;
            gx_color_value  dest_cvals[GX_DEVICE_COLOR_MAX_COMPONENTS];

            if ((code = dev_proc(tdev, decode_color)(tdev, *cp, dest_cvals)) < 0)
                break;
            for (j = 0, comps = drawn_comps; comps != 0; ++j, comps >>= 1) {
                    if ((comps & 0x1) != 0)
                        dest_cvals[j] = src_cvals[j];
            }
            *cp = dev_proc(tdev, encode_color)(tdev, dest_cvals);
        }
        pack_proc(pcolor_buff, gb_buff, 0, w, depth);
        code = dev_proc(tdev, copy_color)( tdev,
                                           gb_buff,
                                           0,
                                           raster,
                                           gs_no_bitmap_id,
                                           x, y - 1, w, 1 );
    }

    gs_free_object( mem,
                    gb_buff,
                    "overprint generic fill rectangle" );
    gs_free_object( mem,
                    pcolor_buff,
                    "overprint generic fill rectangle" );

    return code;
}

/*
 * Replication of 2 and 4 bit patterns to fill a mem_mono_chunk.
 */
static mono_fill_chunk fill_pat_2[4] = {
    mono_fill_make_pattern(0x00), mono_fill_make_pattern(0x55),
    mono_fill_make_pattern(0xaa), mono_fill_make_pattern(0xff)
};

static mono_fill_chunk fill_pat_4[16] = {
    mono_fill_make_pattern(0x00), mono_fill_make_pattern(0x11),
    mono_fill_make_pattern(0x22), mono_fill_make_pattern(0x33),
    mono_fill_make_pattern(0x44), mono_fill_make_pattern(0x55),
    mono_fill_make_pattern(0x66), mono_fill_make_pattern(0x77),
    mono_fill_make_pattern(0x88), mono_fill_make_pattern(0x99),
    mono_fill_make_pattern(0xaa), mono_fill_make_pattern(0xbb),
    mono_fill_make_pattern(0xcc), mono_fill_make_pattern(0xdd),
    mono_fill_make_pattern(0xee), mono_fill_make_pattern(0xff)
};

/*
 * Replicate a color or mask as required to fill a mem_mono_fill_chunk.
 * This is possible if (8 * sizeof(mono_fill_chunk)) % depth == 0.
 * Since sizeof(mono_fill_chunk) is a power of 2, this will be the case
 * if depth is a power of 2 and depth <= 8 * sizeof(mono_fill_chunk).
 */
static mono_fill_chunk
replicate_color(int depth, mono_fill_chunk color)
{
    switch (depth) {

      case 1:
        color = (mono_fill_chunk)(-(int)color); break;

      case 2:
        color = fill_pat_2[color]; break;

      case 4:
        color = fill_pat_4[color]; break;

      case 8:
        color= mono_fill_make_pattern(color); break;

#if mono_fill_chunk_bytes > 2
      case 16:
        color = (color << 16) | color;
        /* fall through */
#endif
#if mono_fill_chunk_bytes > 4
      case 32:
        color = (color << 32) | color;
        break;
#endif
    }

    return color;
}

/*
 * Perform the fill rectangle operation for a separable color encoding
 * that requires overprint support.
 *
 * This is handled via two separate cases. If
 *
 *    (8 * sizeof(mono_fill_chunk)) % tdev->color_info.depth = 0,
 *
 * then is possible to work via the masked analog of the bits_fill_rectangle
 * procedure, bits_fill_rectangle_masked. This requires that both the
 * color and component mask be replicated sufficiently to fill the
 * mono_fill_chunk. The somewhat elaborate set-up aside, the resulting
 * algorithm is about as efficient as can be achieved when using
 * get_bits_rectangle. More efficient algorithms require overprint to be
 * implemented in the target device itself.
 *
 * If the condition is not satisfied, a simple byte-wise algorithm is
 * used. This requires minimal setup but is not efficient, as it works in
 * units that are too small. More efficient methods are possible in this
 * case, but the required setup for a general depth is excessive (even
 * with the restriction that depth % 8 = 0). Hence, efficiency for these
 * cases is better addressed by direct implementation of overprint for
 * memory devices.
 *
 * For both cases, the color and retain_mask values passed to this
 * procedure are expected to be already swapped as required for a byte-
 * oriented bitmap. This consideration affects only little-endian
 * machines. For those machines, if depth > 9 the color passed to these
 * two procedures will not be the same as that passed to
 * gx_overprint_generic_fill_rectangle.
 *
 * Returns 0 on success, < 0 in the event of an error.
 */
int
gx_overprint_sep_fill_rectangle_1(
    gx_device *             tdev,
    gx_color_index          retain_mask,    /* already swapped */
    int                     x,
    int                     y,
    int                     w,
    int                     h,
    gx_color_index          color,          /* already swapped */
    gs_memory_t *           mem )
{
    byte *                  gb_buff = 0;
    gs_get_bits_params_t    gb_params;
    gs_int_rect             gb_rect;
    int                     code = 0, bit_w, depth = tdev->color_info.depth;
    int                     raster;
    mono_fill_chunk         rep_color, rep_mask;

    fit_fill(tdev, x, y, w, h);
    bit_w = w * depth;

    /* set up replicated color and retain mask */
    if (depth < 8 * sizeof(mono_fill_chunk)) {
        rep_color = replicate_color(depth, (mono_fill_chunk)color);
        rep_mask = replicate_color(depth, (mono_fill_chunk)retain_mask);
    } else {
        rep_color = (mono_fill_chunk)color;
        rep_mask = (mono_fill_chunk)retain_mask;
    }

    /* allocate a buffer for the returned data */
    raster = bitmap_raster(w * depth);
    gb_buff = gs_alloc_bytes(mem, raster, "overprint sep fill rectangle 1");
    if (gb_buff == 0)
        return gs_note_error(gs_error_VMerror);

    /*
     * Initialize the get_bits parameters. The selection of options is
     * the same as that for gx_overprint_generic_fill_rectangle (above).
     */
    gb_params.options =  GB_COLORS_NATIVE
                       | GB_ALPHA_NONE
                       | GB_DEPTH_ALL
                       | GB_PACKING_CHUNKY
                       | GB_RETURN_COPY
                       | GB_ALIGN_STANDARD
                       | GB_OFFSET_0
                       | GB_RASTER_STANDARD;
    gb_params.x_offset = 0;     /* for consistency */
    gb_params.data[0] = gb_buff;
    gb_params.raster = raster;

    gb_rect.p.x = x;
    gb_rect.q.x = x + w;

    /* process each scanline separately */
    while (h-- > 0 && code >= 0) {
        gb_rect.p.y = y++;
        gb_rect.q.y = y;
        code = dev_proc(tdev, get_bits_rectangle)( tdev,
                                                   &gb_rect,
                                                   &gb_params );
        if (code < 0)
            break;
        bits_fill_rectangle_masked( gb_buff,
                                    0,
                                    raster,
                                    rep_color,
                                    rep_mask,
                                    bit_w,
                                    1 );
        code = dev_proc(tdev, copy_color)( tdev,
                                           gb_buff,
                                           0,
                                           raster,
                                           gs_no_bitmap_id,
                                           x, y - 1, w, 1 );
    }

    gs_free_object( mem,
                    gb_buff,
                    "overprint generic fill rectangle" );

    return code;
}

int
gx_overprint_sep_fill_rectangle_2(
    gx_device *             tdev,
    gx_color_index          retain_mask,    /* already swapped */
    int                     x,
    int                     y,
    int                     w,
    int                     h,
    gx_color_index          color,          /* already swapped */
    gs_memory_t *           mem )
{
    byte *                  gb_buff = 0;
    gs_get_bits_params_t    gb_params;
    gs_int_rect             gb_rect;
    int                     code = 0, byte_w, raster;
    int                     byte_depth = tdev->color_info.depth >> 3;
    byte *                  pcolor;
    byte *                  pmask;

    fit_fill(tdev, x, y, w, h);
    byte_w = w * byte_depth;

    /* set up color and retain mask pointers */
    pcolor = (byte *)&color;
    pmask = (byte *)&retain_mask;
#if ARCH_IS_BIG_ENDIAN
    pcolor += ARCH_SIZEOF_COLOR_INDEX - byte_depth;
    pmask += ARCH_SIZEOF_COLOR_INDEX - byte_depth;
#endif

    /* allocate a buffer for the returned data */
    raster = bitmap_raster(w * (byte_depth << 3));
    gb_buff = gs_alloc_bytes(mem, raster, "overprint sep fill rectangle 2");
    if (gb_buff == 0)
        return gs_note_error(gs_error_VMerror);

    /*
     * Initialize the get_bits parameters. The selection of options is
     * the same as that for gx_overprint_generic_fill_rectangle (above).
     */
    gb_params.options =  GB_COLORS_NATIVE
                       | GB_ALPHA_NONE
                       | GB_DEPTH_ALL
                       | GB_PACKING_CHUNKY
                       | GB_RETURN_COPY
                       | GB_ALIGN_STANDARD
                       | GB_OFFSET_0
                       | GB_RASTER_STANDARD;
    gb_params.x_offset = 0;     /* for consistency */
    gb_params.data[0] = gb_buff;
    gb_params.raster = raster;

    gb_rect.p.x = x;
    gb_rect.q.x = x + w;

    /* process each scanline separately */
    while (h-- > 0 && code >= 0) {
        int     i, j;
        byte *  cp = gb_buff;

        gb_rect.p.y = y++;
        gb_rect.q.y = y;
        code = dev_proc(tdev, get_bits_rectangle)( tdev,
                                                   &gb_rect,
                                                   &gb_params );
        if (code < 0)
            break;
        for (i = 0, j = 0; i < byte_w; i++, cp++) {
            *cp = (*cp & pmask[j]) | pcolor[j];
            if (++j == byte_depth)
                j = 0;
        }
        code = dev_proc(tdev, copy_color)( tdev,
                                           gb_buff,
                                           0,
                                           raster,
                                           gs_no_bitmap_id,
                                           x, y - 1, w, 1 );
    }

    gs_free_object( mem,
                    gb_buff,
                    "overprint generic fill rectangle" );

    return code;
}