Coverage Report

Created: 2025-06-10 07:19

/src/ghostpdl/base/gdevdbit.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
/* Default device bitmap copying implementation */
17
#include "gx.h"
18
#include "gpcheck.h"
19
#include "gserrors.h"
20
#include "gsbittab.h"
21
#include "gsrect.h"
22
#include "gsropt.h"
23
#include "gxdcolor.h"
24
#include "gxdevice.h"
25
#include "gxdevmem.h"
26
#include "gdevmem.h"
27
#include "gxgetbit.h"
28
#undef mdev
29
#include "gxcpath.h"
30
31
/* Implement copy_mono by filling lots of small rectangles. */
32
/* This is very inefficient, but it works as a default. */
33
int
34
gx_default_copy_mono(gx_device * dev, const byte * data,
35
            int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
36
                     gx_color_index zero, gx_color_index one)
37
2.67k
{
38
2.67k
    bool invert;
39
2.67k
    gx_color_index color;
40
2.67k
    gx_device_color devc;
41
42
2.67k
    if (!data)
43
0
        return gs_throw_code(gs_error_unknownerror);
44
2.67k
    fit_copy(dev, data, dx, raster, id, x, y, w, h);
45
2.67k
    if (!data)
46
0
        return gs_throw_code(gs_error_unknownerror);
47
2.67k
    if (one != gx_no_color_index) {
48
2.21k
        invert = false;
49
2.21k
        color = one;
50
2.21k
        if (zero != gx_no_color_index) {
51
0
            int code = (*dev_proc(dev, fill_rectangle))
52
0
            (dev, x, y, w, h, zero);
53
54
0
            if (code < 0)
55
0
                return code;
56
0
        }
57
2.21k
    } else {
58
462
        invert = true;
59
462
        color = zero;
60
462
    }
61
2.67k
    if (!data)
62
0
        return gs_throw_code(gs_error_unknownerror);
63
2.67k
    set_nonclient_dev_color(&devc, color);
64
2.67k
    if (!data)
65
0
        return gs_throw_code(gs_error_unknownerror);
66
2.67k
    return gx_dc_default_fill_masked
67
2.67k
        (&devc, data, dx, raster, id, x, y, w, h, dev, rop3_T, invert);
68
2.67k
}
69
70
/* Implement copy_color by filling lots of small rectangles. */
71
/* This is very inefficient, but it works as a default. */
72
int
73
gx_default_copy_color(gx_device * dev, const byte * data,
74
                      int dx, int raster, gx_bitmap_id id,
75
                      int x, int y, int w, int h)
76
169k
{
77
169k
    int depth = dev->color_info.depth;
78
169k
    byte mask;
79
80
169k
    dev_proc_fill_rectangle((*fill));
81
169k
    const byte *row;
82
169k
    int iy;
83
84
169k
    if (depth == 1)
85
160k
        return (*dev_proc(dev, copy_mono)) (dev, data, dx, raster, id,
86
160k
                                            x, y, w, h,
87
160k
                                    (gx_color_index) 0, (gx_color_index) 1);
88
169k
    fit_copy(dev, data, dx, raster, id, x, y, w, h);
89
8.44k
    fill = dev_proc(dev, fill_rectangle);
90
8.44k
    mask = (byte) ((1 << depth) - 1);
91
16.8k
    for (row = data, iy = 0; iy < h; row += raster, ++iy) {
92
8.44k
        int ix;
93
8.44k
        gx_color_index c0 = gx_no_color_index;
94
8.44k
        const byte *ptr = row + ((dx * depth) >> 3);
95
8.44k
        int i0;
96
97
1.80M
        for (i0 = ix = 0; ix < w; ++ix) {
98
1.80M
            gx_color_index color;
99
100
1.80M
            if (depth >= 8) {
101
1.80M
                color = *ptr++;
102
1.80M
                switch (depth) {
103
0
                    case 64:
104
0
                        color = (color << 8) + *ptr++;
105
0
                    case 56:
106
0
                        color = (color << 8) + *ptr++;
107
0
                    case 48:
108
0
                        color = (color << 8) + *ptr++;
109
0
                    case 40:
110
0
                        color = (color << 8) + *ptr++;
111
0
                    case 32:
112
0
                        color = (color << 8) + *ptr++;
113
1.80M
                    case 24:
114
1.80M
                        color = (color << 8) + *ptr++;
115
1.80M
                    case 16:
116
1.80M
                        color = (color << 8) + *ptr++;
117
1.80M
                }
118
1.80M
            } else {
119
0
                uint dbit = (-(ix + dx + 1) * depth) & 7;
120
121
0
                color = (*ptr >> dbit) & mask;
122
0
                if (dbit == 0)
123
0
                    ptr++;
124
0
            }
125
1.80M
            if (color != c0) {
126
26.3k
                if (ix > i0) {
127
17.8k
                    int code = (*fill)
128
17.8k
                    (dev, i0 + x, iy + y, ix - i0, 1, c0);
129
130
17.8k
                    if (code < 0)
131
0
                        return code;
132
17.8k
                }
133
26.3k
                c0 = color;
134
26.3k
                i0 = ix;
135
26.3k
            }
136
1.80M
        }
137
8.44k
        if (ix > i0) {
138
8.44k
            int code = (*fill) (dev, i0 + x, iy + y, ix - i0, 1, c0);
139
140
8.44k
            if (code < 0)
141
0
                return code;
142
8.44k
        }
143
8.44k
    }
144
8.44k
    return 0;
145
8.44k
}
146
147
int
148
gx_no_copy_alpha(gx_device * dev, const byte * data, int data_x,
149
           int raster, gx_bitmap_id id, int x, int y, int width, int height,
150
                 gx_color_index color, int depth)
151
0
{
152
0
    return_error(gs_error_unknownerror);
153
0
}
154
155
/* Currently we really should only be here if the target device is planar
156
   AND it supports devn colors AND is 8 or 16 bit.  For example tiffsep
157
   and psdcmyk may make use of this if AA is enabled.  It is basically
158
   designed for devices that need more than 64 bits for color support
159
160
   So that I can follow things and  make it readable for future generations,
161
   I am not using the macro nightmare that default_copy_alpha uses. */
162
int
163
gx_default_copy_alpha_hl_color(gx_device * dev, const byte * data, int data_x,
164
           int raster, gx_bitmap_id id, int x, int y, int width, int height,
165
                      const gx_drawing_color *pdcolor, int depth)
166
0
{
167
0
    const byte *row_alpha;
168
0
    gs_memory_t *mem = dev->memory;
169
0
    int bpp = dev->color_info.depth;
170
0
    uchar ncomps = dev->color_info.num_components;
171
0
    uint out_raster;
172
0
    int code = 0;
173
0
    gx_color_value src_cv[GS_CLIENT_COLOR_MAX_COMPONENTS];
174
0
    gx_color_value curr_cv[GS_CLIENT_COLOR_MAX_COMPONENTS];
175
0
    gx_color_value blend_cv[GS_CLIENT_COLOR_MAX_COMPONENTS];
176
0
    int ry;
177
0
    uchar k, j;
178
0
    gs_get_bits_params_t gb_params;
179
0
    byte *src_planes[GS_CLIENT_COLOR_MAX_COMPONENTS];
180
0
    gs_int_rect gb_rect;
181
0
    int byte_depth;
182
0
    int shift, word_width;
183
0
    gx_color_value *composite;
184
0
    byte *gb_buff;
185
0
    int x_curr, w_curr, gb_buff_start;
186
187
0
    byte_depth = bpp / ncomps;
188
0
    shift = 16 - byte_depth;
189
0
    word_width = byte_depth >> 3;
190
191
0
    fit_copy(dev, data, data_x, raster, id, x, y, width, height);
192
0
    row_alpha = data;
193
0
    out_raster = bitmap_raster(width * byte_depth);
194
0
    gb_buff = gs_alloc_bytes(mem, out_raster * ncomps, "copy_alpha_hl_color(gb_buff)");
195
0
    if (gb_buff == 0) {
196
0
        code = gs_note_error(gs_error_VMerror);
197
0
        return code;
198
0
    }
199
0
    for (k = 0; k < ncomps; k++) {
200
0
        src_cv[k] = pdcolor->colors.devn.values[k];
201
0
    }
202
    /* Initialize the get_bits parameters. Here we just get a plane at a  time. */
203
0
    gb_params.options =  GB_COLORS_NATIVE
204
0
                       | GB_ALPHA_NONE
205
0
                       | GB_DEPTH_ALL
206
0
                       | GB_PACKING_PLANAR
207
0
                       | GB_RETURN_COPY
208
0
                       | GB_ALIGN_STANDARD
209
0
                       | GB_OFFSET_0
210
0
                       | GB_RASTER_STANDARD
211
0
                       | GB_SELECT_PLANES;
212
0
    gb_rect.p.x = x;
213
0
    gb_rect.q.x = x + width;
214
0
    for (ry = y; ry < y + height; row_alpha += raster, ++ry) {
215
0
        int sx, rx;
216
217
0
        gb_rect.p.y = ry;
218
0
        gb_rect.q.y = ry+1;
219
0
        for (k = 0; k < ncomps; k++) {
220
            /* First set the params to zero for all planes except the one we want */
221
            /* I am not sure why get_bits_rectangle for the planar device can
222
               not hand back the data in a proper planar form.  To get the
223
               individual planes seems that I need to jump through some hoops
224
               here */
225
0
            for (j = 0; j < ncomps; j++)
226
0
                gb_params.data[j] = 0;
227
0
            gb_params.data[k] = gb_buff + k * out_raster;
228
0
            code = dev_proc(dev, get_bits_rectangle) (dev, &gb_rect,
229
0
                                                      &gb_params);
230
0
            src_planes[k] = gb_params.data[k];
231
0
            if (code < 0) {
232
0
                gs_free_object(mem, gb_buff, "copy_alpha_hl_color");
233
0
                return code;
234
0
            }
235
0
        }
236
        /* At this point we have to carry around some additional variables
237
           so that we can handle any buffer flushes due to alpha == 0 values.
238
           See below why this is needed */
239
0
        x_curr = x;
240
0
        w_curr = 0;
241
0
        gb_buff_start = 0;
242
0
        for (sx = data_x, rx = x; sx < data_x + width; ++sx, ++rx) {
243
0
            int alpha2, alpha;
244
245
0
            w_curr += 1;
246
0
            switch (depth)
247
0
            {
248
0
            case 2:
249
0
                alpha = ((row_alpha[sx >> 2] >> ((3 - (sx & 3)) << 1)) & 3) * 85;
250
0
                break;
251
0
            case 4:
252
0
                alpha2 = row_alpha[sx >> 1];
253
0
                alpha = (sx & 1 ? alpha2 & 0xf : alpha2 >> 4) * 17;
254
0
                break;
255
0
            case 8:
256
0
                alpha = row_alpha[sx];
257
0
                break;
258
0
            default:
259
0
                return_error(gs_error_rangecheck);
260
0
            }
261
262
0
            if (alpha == 0) {
263
                /* With alpha 0 we want to avoid writing out this value.
264
                 * While it is true that writting it out leaves the color
265
                 * unchanged,  any device that's watching what pixels are
266
                 * written (such as the pattern tile devices) may have problems.
267
                 * As in gx_default_copy_alpha the right thing to do is to write
268
                 * out what we have so far and then continue to collect when we
269
                 * get back to non zero alpha.  */
270
0
                code = dev_proc(dev, copy_planes)(dev, &(gb_buff[gb_buff_start]),
271
0
                                                  0, out_raster, gs_no_bitmap_id,
272
0
                                                  x_curr, ry, w_curr-1, 1, 1);
273
0
                if (code < 0) {
274
0
                    gs_free_object(mem, gb_buff, "copy_alpha_hl_color");
275
0
                    return code;
276
0
                }
277
                /* reset ourselves */
278
0
                gb_buff_start = gb_buff_start + w_curr * word_width;
279
0
                w_curr = 0;
280
0
                x_curr = rx + 1;
281
0
            } else {
282
0
                if (alpha == 255) {
283
                    /* Just use the new color. */
284
0
                    composite = &(src_cv[0]);
285
0
                } else {
286
                    /* We need to do the weighting by the alpha value */
287
0
                    alpha += (alpha>>7); /* Expand from 0..255->0..256 */
288
                    /* First get the old color */
289
0
                    for (k = 0; k < ncomps; k++) {
290
                        /* We only have 8 and 16 bit depth to worry about.
291
                           However, this stuff should really be done with
292
                           the device encode/decode procedure. */
293
0
                        byte *ptr = ((src_planes[k]) + (sx - data_x) * word_width);
294
0
                        curr_cv[k] = 0;
295
0
                        switch (word_width) {
296
0
                            case 2:
297
0
                                curr_cv[k] += (*ptr++ << 8);
298
0
                                curr_cv[k] += *ptr;
299
0
                                break;
300
0
                            case 1:
301
0
                                curr_cv[k] += *ptr;
302
0
                                curr_cv[k] += curr_cv[k] << 8;
303
0
                        }
304
                        /* Now compute the new color which is a blend of
305
                           the old and the new */
306
0
                        blend_cv[k] =  ((curr_cv[k]<<8) +
307
0
                                        (((long) src_cv[k] - (long) curr_cv[k]) * alpha))>>8;
308
0
                        composite = &(blend_cv[0]);
309
0
                    }
310
0
                }
311
                /* Update our plane data buffers.  Just reuse the current one */
312
0
                for (k = 0; k < ncomps; k++) {
313
0
                    byte *ptr = ((src_planes[k]) + (sx - data_x) * word_width);
314
0
                    switch (word_width) {
315
0
                        case 2:
316
0
                            *ptr++ = composite[k] >> 8;
317
0
                        case 1:
318
0
                            *ptr++ = composite[k] >> shift;
319
0
                    }
320
0
                }
321
0
            } /* else on alpha != 0 */
322
0
        } /* loop on x */
323
        /* Flush what ever we have left.  We may only have a partial due to
324
           the presence of alpha = 0 values */
325
0
        code = dev_proc(dev, copy_planes)(dev, &(gb_buff[gb_buff_start]),
326
0
                                          0, out_raster, gs_no_bitmap_id,
327
0
                                          x_curr, ry, w_curr, 1, 1);
328
0
    } /* loop on y */
329
0
    gs_free_object(mem, gb_buff, "copy_alpha_hl_color");
330
0
    return code;
331
0
}
332
333
int
334
gx_default_copy_alpha(gx_device * dev, const byte * data, int data_x,
335
           int raster, gx_bitmap_id id, int x, int y, int width, int height,
336
                      gx_color_index color, int depth)
337
0
{       /* This might be called with depth = 1.... */
338
0
    if (depth == 1)
339
0
        return (*dev_proc(dev, copy_mono)) (dev, data, data_x, raster, id,
340
0
                                            x, y, width, height,
341
0
                                            gx_no_color_index, color);
342
    /*
343
     * Simulate alpha by weighted averaging of RGB values.
344
     * This is very slow, but functionally correct.
345
     */
346
0
    {
347
0
        const byte *row;
348
0
        gs_memory_t *mem = dev->memory;
349
0
        int bpp = dev->color_info.depth;
350
0
        uchar ncomps = dev->color_info.num_components;
351
0
        uint in_size = gx_device_raster_chunky(dev, false);
352
0
        byte *lin;
353
0
        uint out_size;
354
0
        byte *lout;
355
0
        int code = 0;
356
0
        gx_color_value color_cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
357
0
        int ry, lx;
358
0
        gs_int_rect rect;
359
360
0
        fit_copy(dev, data, data_x, raster, id, x, y, width, height);
361
0
        row = data;
362
0
        out_size = bitmap_raster(width * bpp);
363
0
        lin = gs_alloc_bytes(mem, in_size, "copy_alpha(lin)");
364
0
        lout = gs_alloc_bytes(mem, out_size, "copy_alpha(lout)");
365
0
        if (lin == 0 || lout == 0) {
366
0
            code = gs_note_error(gs_error_VMerror);
367
0
            goto out;
368
0
        }
369
0
        (*dev_proc(dev, decode_color)) (dev, color, color_cv);
370
0
        rect.p.x = 0;
371
0
        rect.q.x = dev->width;
372
0
        for (ry = y; ry < y + height; row += raster, ++ry) {
373
0
            byte *line;
374
0
            int sx, rx;
375
376
0
            byte *l_dptr = lout;
377
0
            int l_dbit = 0;
378
0
            byte l_dbyte = ((l_dbit) ? (byte)(*(l_dptr) & (0xff00 >> (l_dbit))) : 0);
379
0
            int l_xprev = x;
380
0
            gs_get_bits_params_t params;
381
382
0
            params.options = (GB_ALIGN_STANDARD |
383
0
                              (GB_RETURN_COPY | GB_RETURN_POINTER) |
384
0
                              GB_OFFSET_0 |
385
0
                              GB_RASTER_STANDARD | GB_PACKING_CHUNKY |
386
0
                              GB_COLORS_NATIVE | GB_ALPHA_NONE);
387
0
            params.x_offset = 0;
388
0
            params.raster = bitmap_raster(dev->width * dev->color_info.depth);
389
0
            params.data[0] = lin;
390
0
            rect.p.y = ry;
391
0
            rect.q.y = ry+1;
392
0
            code = (*dev_proc(dev, get_bits_rectangle))(dev, &rect,
393
0
                                                        &params);
394
0
            if (code < 0)
395
0
                break;
396
0
            line = params.data[0];
397
0
            lx = x;
398
0
            for (sx = data_x, rx = x; sx < data_x + width; ++sx, ++rx) {
399
0
                gx_color_index previous = gx_no_color_index;
400
0
                gx_color_index composite;
401
0
                int alpha2, alpha;
402
403
0
                switch(depth)
404
0
                {
405
0
                case 2:
406
                    /* map 0 - 3 to 0 - 15 */
407
0
                    alpha = ((row[sx >> 2] >> ((3 - (sx & 3)) << 1)) & 3) * 85;
408
0
                    break;
409
0
                case 4:
410
0
                    alpha2 = row[sx >> 1],
411
0
                        alpha = (sx & 1 ? alpha2 & 0xf : alpha2 >> 4) * 17;
412
0
                    break;
413
0
                case 8:
414
0
                    alpha = row[sx];
415
0
                    break;
416
0
                default:
417
0
                    return_error(gs_error_rangecheck);
418
0
                }
419
0
              blend:
420
0
                if (alpha == 0) {
421
                    /* Previously the code used to just write out the previous
422
                     * colour when the alpha was 0, but that's wrong. It leaves
423
                     * the underlying colour unchanged, but has the effect of
424
                     * making this pixel appear solid in any device that's
425
                     * watching what pixels are written (such as the pattern
426
                     * tile devices). The right thing to do is to write out
427
                     * the buffered accumulator, and skip over any pixels that
428
                     * are completely clear. */
429
0
                    if (rx > l_xprev ) {
430
0
                        sample_store_flush(l_dptr, l_dbit, l_dbyte);
431
0
                        code = (*dev_proc(dev, copy_color))
432
0
                          (dev, lout, l_xprev - (lx), out_size,
433
0
                           gx_no_bitmap_id, l_xprev, ry, (rx) - l_xprev, 1);
434
0
                        if ( code < 0 )
435
0
                          return code;
436
0
                    }
437
0
                    l_dptr = lout;
438
0
                    l_dbit = 0;
439
0
                    l_dbyte = (l_dbit ? (byte)(*l_dptr & (0xff00 >> l_dbit)) : 0);
440
0
                    l_xprev = rx+1;
441
0
                    lx = rx+1;
442
0
                } else {
443
0
                    if (alpha == 255) { /* Just write the new color. */
444
0
                        composite = color;
445
0
                    } else {
446
0
                        gx_color_value cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
447
0
                        uchar i;
448
0
                        int alpha2 = alpha + (alpha>>7);
449
450
0
                        if (previous == gx_no_color_index) { /* Extract the old color. */
451
0
                            if (bpp < 8) {
452
0
                                const uint bit = rx * bpp;
453
0
                                const byte *src = line + (bit >> 3);
454
455
0
                                previous =
456
0
                                    (*src >> (8 - ((bit & 7) + bpp))) &
457
0
                                    ((1 << bpp) - 1);
458
0
                            } else {
459
0
                                const byte *src = line + (rx * (bpp >> 3));
460
461
0
                                previous = 0;
462
0
                                switch (bpp >> 3) {
463
0
                                    case 8:
464
0
                                        previous += (gx_color_index) * src++
465
0
                                            << SAMPLE_BOUND_SHIFT(previous, 56);
466
0
                                    case 7:
467
0
                                        previous += (gx_color_index) * src++
468
0
                                            << SAMPLE_BOUND_SHIFT(previous, 48);
469
0
                                    case 6:
470
0
                                        previous += (gx_color_index) * src++
471
0
                                            << SAMPLE_BOUND_SHIFT(previous, 40);
472
0
                                    case 5:
473
0
                                        previous += (gx_color_index) * src++
474
0
                                            << SAMPLE_BOUND_SHIFT(previous, 32);
475
0
                                    case 4:
476
0
                                        previous += (gx_color_index) * src++ << 24;
477
0
                                    case 3:
478
0
                                        previous += (gx_color_index) * src++ << 16;
479
0
                                    case 2:
480
0
                                        previous += (gx_color_index) * src++ << 8;
481
0
                                    case 1:
482
0
                                        previous += *src++;
483
0
                                }
484
0
                            }
485
0
                        }
486
0
                        (*dev_proc(dev, decode_color)) (dev, previous, cv);
487
#if ARCH_INTS_ARE_SHORT
488
#  define b_int long
489
#else
490
0
#  define b_int int
491
0
#endif
492
0
#define make_shade(old, clr, alpha) \
493
0
  (((((b_int)(old))<<8) + (((b_int)(clr) - (b_int)(old)) * (alpha)))>>8)
494
0
                        for (i=0; i<ncomps; i++)
495
0
                            cv[i] = make_shade(cv[i], color_cv[i], alpha2);
496
0
#undef b_int
497
0
#undef make_shade
498
0
                        composite =
499
0
                            (*dev_proc(dev, encode_color)) (dev, cv);
500
0
                        if (composite == gx_no_color_index) { /* The device can't represent this color. */
501
                            /* Move the alpha value towards 0 or 1. */
502
0
                            if (alpha == 127) /* move 1/2 towards 1 */
503
0
                                ++alpha;
504
0
                            alpha = (alpha & 128) | (alpha >> 1);
505
0
                            goto blend;
506
0
                        }
507
0
                    }
508
0
                    if (sizeof(composite) > 4) {
509
0
                        if (sample_store_next64(composite, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
510
0
                            return_error(gs_error_rangecheck);
511
0
                    }
512
0
                    else {
513
0
                        if (sample_store_next32(composite, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
514
0
                            return_error(gs_error_rangecheck);
515
0
                    }
516
0
                }
517
0
            }
518
0
            if ( rx > l_xprev ) {
519
0
                sample_store_flush(l_dptr, l_dbit, l_dbyte);
520
0
                code = (*dev_proc(dev, copy_color))
521
0
                  (dev, lout, l_xprev - lx, out_size,
522
0
                   gx_no_bitmap_id, l_xprev, ry, rx - l_xprev, 1);
523
0
                if (code < 0)
524
0
                    return code;
525
0
            }
526
0
        }
527
0
      out:gs_free_object(mem, lout, "copy_alpha(lout)");
528
0
        gs_free_object(mem, lin, "copy_alpha(lin)");
529
0
        return code;
530
0
    }
531
0
}
532
533
int
534
gx_default_fill_mask(gx_device * orig_dev,
535
                     const byte * data, int dx, int raster, gx_bitmap_id id,
536
                     int x, int y, int w, int h,
537
                     const gx_drawing_color * pdcolor, int depth,
538
                     gs_logical_operation_t lop, const gx_clip_path * pcpath)
539
2.76M
{
540
2.76M
    gx_device *dev = orig_dev;
541
2.76M
    gx_device_clip cdev;
542
2.76M
    int code = 0;
543
544
2.76M
    if (w == 0 || h == 0)
545
49
        return 0;
546
547
2.76M
    if (pcpath != 0)
548
121k
    {
549
121k
        gs_fixed_rect rect;
550
121k
        int tmp;
551
552
121k
        rect.p.x = int2fixed(x);
553
121k
        rect.p.y = int2fixed(y);
554
121k
        rect.q.x = int2fixed(x+w);
555
121k
        rect.q.y = int2fixed(y+h);
556
121k
        dev = gx_make_clip_device_on_stack_if_needed(&cdev, pcpath, dev, &rect);
557
121k
        if (dev == NULL)
558
453
            return 0;
559
        /* Clip region if possible */
560
120k
        tmp = fixed2int(rect.p.x);
561
120k
        if (tmp > x)
562
185
        {
563
185
            dx += tmp-x;
564
185
            x = tmp;
565
185
        }
566
120k
        tmp = fixed2int(rect.q.x);
567
120k
        if (tmp < x+w)
568
413
            w = tmp-x;
569
120k
        tmp = fixed2int(rect.p.y);
570
120k
        if (tmp > y)
571
334
        {
572
334
            data += (tmp-y) * raster;
573
334
            y = tmp;
574
334
        }
575
120k
        tmp = fixed2int(rect.q.y);
576
120k
        if (tmp < y+h)
577
877
            h = tmp-y;
578
120k
    }
579
2.76M
    if (depth > 1) {
580
        /****** CAN'T DO ROP OR HALFTONE WITH ALPHA ******/
581
0
        code = (*dev_proc(dev, copy_alpha))
582
0
            (dev, data, dx, raster, id, x, y, w, h,
583
0
             gx_dc_pure_color(pdcolor), depth);
584
0
    } else
585
2.76M
        code = pdcolor->type->fill_masked(pdcolor, data, dx, raster, id,
586
2.76M
                                          x, y, w, h, dev, lop, false);
587
2.76M
    if (dev != orig_dev)
588
418
        gx_destroy_clip_device_on_stack(&cdev);
589
2.76M
    return code;
590
2.76M
}
591
592
/* Default implementation of strip_tile_rect_devn.  With the current design
593
   only devices that support devn color will be making use of this
594
   procedure and those are planar devices.  So we have an implemenation
595
   for planar devices and not a default implemenetation at this time. */
596
int
597
gx_default_strip_tile_rect_devn(gx_device * dev, const gx_strip_bitmap * tiles,
598
   int x, int y, int w, int h, const gx_drawing_color * pdcolor0,
599
   const gx_drawing_color * pdcolor1, int px, int py)
600
0
{
601
0
    int width = tiles->size.x;
602
0
    int height = tiles->size.y;
603
0
    int raster = tiles->raster;
604
0
    int rwidth = tiles->rep_width;
605
0
    int rheight = tiles->rep_height;
606
0
    int shift = tiles->shift;
607
0
    int code;
608
609
0
    if (rwidth == 0 || rheight == 0)
610
0
        return_error(gs_error_unregistered); /* Must not happen. */
611
0
    fit_fill_xy(dev, x, y, w, h);
612
0
    if (w == 0 || h == 0)
613
0
        return 0;
614
615
    /* Loop over as many tiles as we need vertically */
616
0
    while (1) {
617
0
        int xoff = (shift == 0 ? px :
618
0
                                 px + (y + py) / rheight * tiles->rep_shift);
619
        /* irx = x offset within the tile to start copying from */
620
0
        int irx = ((rwidth & (rwidth - 1)) == 0 ?  /* power of 2 */
621
0
                   (x + xoff) & (rwidth - 1) :
622
0
                   (x + xoff) % rwidth);
623
        /* ry = y offset within the tile to start copying from */
624
0
        int ry = ((rheight & (rheight - 1)) == 0 ?  /* power of 2 */
625
0
                  (y + py) & (rheight - 1) :
626
0
                  (y + py) % rheight);
627
        /* icw = how many bits we can copy before we run out of tile and need to loop */
628
0
        int icw = width - irx;
629
        /* ch = how many rows we can copy before we run out of tile and need to loop */
630
0
        int ch = height - ry;
631
0
        byte *row0 = tiles->data + ry * raster;
632
633
        /* Loop per line */
634
0
        do {
635
0
            byte *row = row0;
636
0
            int w2 = w;
637
0
            int left_in_tile = icw;
638
0
            int runlen = 0;
639
0
            int bit = 1<<((7-irx) & 7);
640
0
            int x2 = x;
641
0
            if (left_in_tile > w2)
642
0
                left_in_tile = w2;
643
644
            /* Loop per row */
645
0
            while (w2) {
646
0
                while (left_in_tile && ((bit & *row) == 0)) {
647
0
                    bit >>= 1;
648
0
                    if (bit == 0)
649
0
                        bit = 128, row++;
650
0
                    left_in_tile--;
651
0
                    runlen++;
652
0
                }
653
0
                if (runlen) {
654
0
                    gs_fixed_rect rect;
655
0
                    rect.p.x = int2fixed(x2);
656
0
                    rect.p.y = int2fixed(y);
657
0
                    rect.q.x = rect.p.x + int2fixed(runlen);
658
0
                    rect.q.y = rect.p.y + int2fixed(1);
659
                    /* Pure colours are only used to signal transparency */
660
0
                    if (pdcolor0->type != gx_dc_type_pure) {
661
0
                        code = dev_proc(dev, fill_rectangle_hl_color)(dev, &rect, NULL, pdcolor0, NULL);
662
0
                        if (code < 0)
663
0
                            return code;
664
0
                    }
665
0
                    x2 += runlen;
666
0
                    w2 -= runlen;
667
0
                    runlen = 0;
668
0
                }
669
0
                while (left_in_tile && ((bit & *row) != 0)) {
670
0
                    bit >>= 1;
671
0
                    if (bit == 0)
672
0
                        bit = 128, row++;
673
0
                    left_in_tile--;
674
0
                    runlen++;
675
0
                }
676
0
                if (runlen) {
677
0
                    gs_fixed_rect rect;
678
0
                    rect.p.x = int2fixed(x2);
679
0
                    rect.p.y = int2fixed(y);
680
0
                    rect.q.x = rect.p.x + int2fixed(runlen);
681
0
                    rect.q.y = rect.p.y + int2fixed(1);
682
                    /* Pure colours are only used to signal transparency */
683
0
                    if (pdcolor1->type != gx_dc_type_pure) {
684
0
                        code = dev_proc(dev, fill_rectangle_hl_color)(dev, &rect, NULL, pdcolor1, NULL);
685
0
                        if (code < 0)
686
0
                            return code;
687
0
                    }
688
0
                    x2 += runlen;
689
0
                    w2 -= runlen;
690
0
                    runlen = 0;
691
0
                }
692
0
                if (left_in_tile == 0) {
693
0
                    left_in_tile = rwidth;
694
0
                    if (left_in_tile > w2)
695
0
                        left_in_tile = w2;
696
0
                }
697
0
            }
698
699
            /* That's a line complete. */
700
0
            y++;
701
0
            if (--h == 0)
702
0
                return 0;
703
0
            row0 += raster;
704
0
        } while (--ch != 0); /* Until we finish a tile vertically */
705
0
    }
706
0
}
707
708
/* Default implementation of strip_tile_rectangle */
709
int
710
gx_default_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
711
   int x, int y, int w, int h, gx_color_index color0, gx_color_index color1,
712
                                int px, int py)
713
27.9k
{       /* Fill the rectangle in chunks. */
714
27.9k
    int width = tiles->size.x;
715
27.9k
    int height = tiles->size.y;
716
27.9k
    int raster = tiles->raster;
717
27.9k
    int rwidth = tiles->rep_width;
718
27.9k
    int rheight = tiles->rep_height;
719
27.9k
    int shift = tiles->shift;
720
27.9k
    gs_id tile_id = tiles->id;
721
722
27.9k
    if (rwidth == 0 || rheight == 0)
723
0
        return_error(gs_error_unregistered); /* Must not happen. */
724
27.9k
    fit_fill_xy(dev, x, y, w, h);
725
726
#ifdef DEBUG
727
    if (gs_debug_c('t')) {
728
        int ptx, pty;
729
        const byte *ptp = tiles->data;
730
731
        dmlprintf4(dev->memory, "[t]tile %dx%d raster=%d id=%lu;",
732
                   tiles->size.x, tiles->size.y, tiles->raster, tiles->id);
733
        dmlprintf6(dev->memory, " x,y=%d,%d w,h=%d,%d p=%d,%d\n",
734
                   x, y, w, h, px, py);
735
        dmlputs(dev->memory, "");
736
        for (pty = 0; pty < tiles->size.y; pty++) {
737
            dmprintf(dev->memory, "   ");
738
            for (ptx = 0; ptx < tiles->raster; ptx++)
739
                dmprintf1(dev->memory, "%3x", *ptp++);
740
        }
741
        dmputc(dev->memory, '\n');
742
    }
743
#endif
744
745
27.9k
    {       /*
746
                                 * Note: we can't do the following computations until after
747
                                 * the fit_fill_xy.
748
                                 */
749
27.9k
        int xoff =
750
27.9k
        (shift == 0 ? px :
751
27.9k
         px + (y + py) / rheight * tiles->rep_shift);
752
27.9k
        int irx = ((rwidth & (rwidth - 1)) == 0 ?  /* power of 2 */
753
0
                   (x + xoff) & (rwidth - 1) :
754
27.9k
                   (x + xoff) % rwidth);
755
27.9k
        int ry = ((rheight & (rheight - 1)) == 0 ?  /* power of 2 */
756
0
                  (y + py) & (rheight - 1) :
757
27.9k
                  (y + py) % rheight);
758
27.9k
        int icw = width - irx;
759
27.9k
        int ch = height - ry;
760
27.9k
        byte *row = tiles->data + ry * raster;
761
762
27.9k
        dev_proc_copy_mono((*proc_mono));
763
27.9k
        dev_proc_copy_color((*proc_color));
764
27.9k
        dev_proc_copy_planes((*proc_planes));
765
27.9k
        int code = 0;
766
767
27.9k
        if (color0 == gx_no_color_index && color1 == gx_no_color_index) {
768
15.3k
            if (tiles->num_planes > 1) {
769
0
                proc_mono = 0;
770
0
                proc_color = 0;
771
0
                proc_planes = dev_proc(dev, copy_planes);
772
15.3k
            } else {
773
15.3k
                proc_planes = 0;
774
15.3k
                proc_color = dev_proc(dev, copy_color);
775
15.3k
                proc_mono = 0;
776
15.3k
            }
777
15.3k
        } else {
778
12.6k
            proc_planes = 0;
779
12.6k
            proc_color = 0;
780
12.6k
            proc_mono = dev_proc(dev, copy_mono);
781
12.6k
        }
782
783
61.7k
#define GX_DEFAULT_COPY_TILE(dev, srcx, tx, ty, tw, th, tid) do {\
784
61.7k
                if_debug6m('t', (dev)->memory, "   copy id=%lu sx=%d => x=%d y=%d w=%d h=%d\n", tid, srcx, tx, ty, tw, th);\
785
61.7k
                if (tiles->num_planes > 1) {\
786
0
                    if (proc_planes)\
787
0
                        code = (*proc_planes)(dev, row, srcx, raster, tid, tx, ty, tw, th, height);\
788
61.7k
                } else {\
789
61.7k
                    if (proc_color != 0) {\
790
43.7k
                        code = (*proc_color)(dev, row, srcx, raster, tid, tx, ty, tw, th);\
791
43.7k
                    } else {\
792
18.0k
                        if (proc_mono)\
793
39.3k
                            code = (*proc_mono)(dev, row, srcx, raster, tid, tx, ty, tw, th, color0, color1);\
794
18.0k
                         else code = 0;\
795
18.0k
                    }\
796
61.7k
                }\
797
61.7k
                if (code < 0) return_error(code);\
798
61.7k
                } while (0);
799
800
801
27.9k
        if (ch >= h) {   /* Shallow operation */
802
26.7k
            if (icw >= w) { /* Just one (partial) tile to transfer. */
803
11.9k
                GX_DEFAULT_COPY_TILE(dev, irx, x, y, w, h, (w == width && h == height ? tile_id : gs_no_bitmap_id));
804
14.8k
            } else {
805
14.8k
                int ex = x + w;
806
14.8k
                int fex = ex - width;
807
14.8k
                int cx = x + icw;
808
14.8k
                ulong id = (h == height ? tile_id : gs_no_bitmap_id);
809
810
14.8k
                GX_DEFAULT_COPY_TILE(dev, irx, x, y, icw, h, gs_no_bitmap_id);
811
28.5k
                while (cx <= fex) {
812
13.7k
                    GX_DEFAULT_COPY_TILE(dev, 0, cx, y, width, h, id);
813
13.7k
                    cx += width;
814
13.7k
                }
815
14.8k
                if (cx < ex) {
816
14.7k
                    GX_DEFAULT_COPY_TILE(dev, 0, cx, y, ex - cx, h, gs_no_bitmap_id);
817
14.7k
                }
818
14.8k
            }
819
26.7k
        } else if (icw >= w && shift == 0) {
820
            /* Narrow operation, no shift */
821
1.20k
            int ey = y + h;
822
1.20k
            int fey = ey - height;
823
1.20k
            int cy = y + ch;
824
1.20k
            ulong id = (w == width ? tile_id : gs_no_bitmap_id);
825
826
1.20k
            GX_DEFAULT_COPY_TILE(dev, irx, x, y, w, ch, (ch == height ? id : gs_no_bitmap_id));
827
1.20k
            row = tiles->data;
828
1.34k
            do {
829
1.34k
                ch = (cy > fey ? ey - cy : height);
830
1.34k
                GX_DEFAULT_COPY_TILE(dev, irx, x, cy, w, ch,
831
1.34k
                          (ch == height ? id : gs_no_bitmap_id));
832
1.34k
            }
833
1.34k
            while ((cy += ch) < ey);
834
1.20k
        } else {
835
            /* Full operation.  If shift != 0, some scan lines */
836
            /* may be narrow.  We could test shift == 0 in advance */
837
            /* and use a slightly faster loop, but right now */
838
            /* we don't bother. */
839
2
            int ex = x + w, ey = y + h;
840
2
            int fex = ex - width, fey = ey - height;
841
2
            int cx, cy;
842
843
143
            for (cy = y;;) {
844
143
                ulong id = (ch == height ? tile_id : gs_no_bitmap_id);
845
846
143
                if (icw >= w) {
847
0
                    GX_DEFAULT_COPY_TILE(dev, irx, x, cy, w, ch,
848
0
                              (w == width ? id : gs_no_bitmap_id));
849
143
                } else {
850
143
                    GX_DEFAULT_COPY_TILE(dev, irx, x, cy, icw, ch, gs_no_bitmap_id);
851
143
                    cx = x + icw;
852
3.90k
                    while (cx <= fex) {
853
3.76k
                        GX_DEFAULT_COPY_TILE(dev, 0, cx, cy, width, ch, id);
854
3.76k
                        cx += width;
855
3.76k
                    }
856
143
                    if (cx < ex) {
857
143
                        GX_DEFAULT_COPY_TILE(dev, 0, cx, cy, ex - cx, ch, gs_no_bitmap_id);
858
143
                    }
859
143
                }
860
143
                if ((cy += ch) >= ey)
861
2
                    break;
862
141
                ch = (cy > fey ? ey - cy : height);
863
141
                if ((irx += shift) >= rwidth)
864
0
                    irx -= rwidth;
865
141
                icw = width - irx;
866
141
                row = tiles->data;
867
141
            }
868
2
        }
869
27.9k
#undef GX_DEFAULT_COPY_TILE
870
27.9k
    }
871
27.9k
    return 0;
872
27.9k
}
873
874
int
875
gx_no_strip_copy_rop2(gx_device * dev,
876
             const byte * sdata, int sourcex, uint sraster, gx_bitmap_id id,
877
                     const gx_color_index * scolors,
878
           const gx_strip_bitmap * textures, const gx_color_index * tcolors,
879
                     int x, int y, int width, int height,
880
                     int phase_x, int phase_y, gs_logical_operation_t lop,
881
                     uint planar_height)
882
0
{
883
0
    return_error(gs_error_unknownerror);  /* not implemented */
884
0
}
885
886
/* ---------------- Unaligned copy operations ---------------- */
887
888
/*
889
 * Implementing unaligned operations in terms of the standard aligned
890
 * operations requires adjusting the bitmap origin and/or the raster to be
891
 * aligned.  Adjusting the origin is simple; adjusting the raster requires
892
 * doing the operation one scan line at a time.
893
 */
894
int
895
gx_copy_mono_unaligned(gx_device * dev, const byte * data,
896
            int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
897
                       gx_color_index zero, gx_color_index one)
898
0
{
899
0
    dev_proc_copy_mono((*copy_mono)) = dev_proc(dev, copy_mono);
900
0
    uint offset = ALIGNMENT_MOD(data, align_bitmap_mod);
901
0
    int step = raster & (align_bitmap_mod - 1);
902
903
    /* Adjust the origin. */
904
0
    data -= offset;
905
0
    dx += offset << 3;
906
907
    /* Adjust the raster. */
908
0
    if (!step) {   /* No adjustment needed. */
909
0
        return (*copy_mono) (dev, data, dx, raster, id,
910
0
                             x, y, w, h, zero, one);
911
0
    }
912
    /* Do the transfer one scan line at a time. */
913
0
    {
914
0
        const byte *p = data;
915
0
        int d = dx;
916
0
        int code = 0;
917
0
        int i;
918
919
0
        for (i = 0; i < h && code >= 0;
920
0
             ++i, p += raster - step, d += step << 3
921
0
            )
922
0
            code = (*copy_mono) (dev, p, d, raster, gx_no_bitmap_id,
923
0
                                 x, y + i, w, 1, zero, one);
924
0
        return code;
925
0
    }
926
0
}
927
928
int
929
gx_copy_color_unaligned(gx_device * dev, const byte * data,
930
                        int data_x, int raster, gx_bitmap_id id,
931
                        int x, int y, int width, int height)
932
0
{
933
0
    dev_proc_copy_color((*copy_color)) = dev_proc(dev, copy_color);
934
0
    int depth = dev->color_info.depth;
935
0
    uint offset = (uint) (data - (const byte *)0) & (align_bitmap_mod - 1);
936
0
    int step = raster & (align_bitmap_mod - 1);
937
938
    /*
939
     * Adjust the origin.
940
     * We have to do something very special for 24-bit data,
941
     * because that is the only depth that doesn't divide
942
     * align_bitmap_mod exactly.  In particular, we need to find
943
     * M*B + R == 0 mod 3, where M is align_bitmap_mod, R is the
944
     * offset value just calculated, and B is an integer unknown;
945
     * the new value of offset will be M*B + R.
946
     */
947
0
    if (depth == 24)
948
0
        offset += (offset % 3) *
949
0
            (align_bitmap_mod * (3 - (align_bitmap_mod % 3)));
950
0
    data -= offset;
951
0
    data_x += (offset << 3) / depth;
952
953
    /* Adjust the raster. */
954
0
    if (!step) {   /* No adjustment needed. */
955
0
        return (*copy_color) (dev, data, data_x, raster, id,
956
0
                              x, y, width, height);
957
0
    }
958
    /* Do the transfer one scan line at a time. */
959
0
    {
960
0
        const byte *p = data;
961
0
        int d = data_x;
962
0
        int dstep = (step << 3) / depth;
963
0
        int code = 0;
964
0
        int i;
965
966
0
        for (i = 0; i < height && code >= 0;
967
0
             ++i, p += raster - step, d += dstep
968
0
            )
969
0
            code = (*copy_color) (dev, p, d, raster, gx_no_bitmap_id,
970
0
                                  x, y + i, width, 1);
971
0
        return code;
972
0
    }
973
0
}
974
975
int
976
gx_copy_alpha_unaligned(gx_device * dev, const byte * data, int data_x,
977
           int raster, gx_bitmap_id id, int x, int y, int width, int height,
978
                        gx_color_index color, int depth)
979
0
{
980
0
    dev_proc_copy_alpha((*copy_alpha)) = dev_proc(dev, copy_alpha);
981
0
    uint offset = (uint) (data - (const byte *)0) & (align_bitmap_mod - 1);
982
0
    int step = raster & (align_bitmap_mod - 1);
983
984
    /* Adjust the origin. */
985
0
    data -= offset;
986
0
    data_x += (offset << 3) / depth;
987
988
    /* Adjust the raster. */
989
0
    if (!step) {   /* No adjustment needed. */
990
0
        return (*copy_alpha) (dev, data, data_x, raster, id,
991
0
                              x, y, width, height, color, depth);
992
0
    }
993
    /* Do the transfer one scan line at a time. */
994
0
    {
995
0
        const byte *p = data;
996
0
        int d = data_x;
997
0
        int dstep = (step << 3) / depth;
998
0
        int code = 0;
999
0
        int i;
1000
1001
0
        for (i = 0; i < height && code >= 0;
1002
0
             ++i, p += raster - step, d += dstep
1003
0
            )
1004
0
            code = (*copy_alpha) (dev, p, d, raster, gx_no_bitmap_id,
1005
0
                                  x, y + i, width, 1, color, depth);
1006
0
        return code;
1007
0
    }
1008
0
}