Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/base/gsbitops.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2021 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.,  1305 Grant Avenue - Suite 200, Novato,
13
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14
*/
15
16
17
/* Bitmap filling, copying, and transforming operations */
18
#include "stdio_.h"
19
#include "memory_.h"
20
#include "gdebug.h"
21
#include "gserrors.h"
22
#include "gstypes.h"
23
#include "gsbittab.h"
24
#include "gxbitops.h"
25
#include "gxcindex.h"
26
27
/* ---------------- Bit-oriented operations ---------------- */
28
29
/* Define masks for little-endian operation. */
30
/* masks[i] has the first i bits off and the rest on. */
31
#if !ARCH_IS_BIG_ENDIAN
32
const bits16 mono_copy_masks[17] = {
33
    0xffff, 0xff7f, 0xff3f, 0xff1f,
34
    0xff0f, 0xff07, 0xff03, 0xff01,
35
    0xff00, 0x7f00, 0x3f00, 0x1f00,
36
    0x0f00, 0x0700, 0x0300, 0x0100,
37
    0x0000
38
};
39
const bits32 mono_fill_masks[33] = {
40
#define mask(n)\
41
  (((bits32)~0xff | (0xff >> (n & 7))) << (n & -8))
42
    mask( 0),mask( 1),mask( 2),mask( 3),mask( 4),mask( 5),mask( 6),mask( 7),
43
    mask( 8),mask( 9),mask(10),mask(11),mask(12),mask(13),mask(14),mask(15),
44
    mask(16),mask(17),mask(18),mask(19),mask(20),mask(21),mask(22),mask(23),
45
    mask(24),mask(25),mask(26),mask(27),mask(28),mask(29),mask(30),mask(31),
46
    0
47
#undef mask
48
};
49
#endif
50
51
/* Fill a rectangle of bits with an 8x1 pattern. */
52
/* The pattern argument must consist of the pattern in every byte, */
53
/* e.g., if the desired pattern is 0xaa, the pattern argument must */
54
/* have the value 0xaaaa (if ints are short) or 0xaaaaaaaa. */
55
#undef chunk
56
191M
#define chunk mono_fill_chunk
57
#undef mono_masks
58
163M
#define mono_masks mono_fill_masks
59
void
60
bits_fill_rectangle(byte * dest, int dest_bit, uint draster,
61
                    mono_fill_chunk pattern, int width_bits, int height)
62
81.4M
{
63
81.4M
    uint bit;
64
81.4M
    chunk right_mask;
65
81.4M
    int line_count = height;
66
81.4M
    chunk *ptr;
67
81.4M
    int last_bit;
68
69
81.4M
#define FOR_EACH_LINE(stat)\
70
167M
        do { stat } while ( inc_ptr(ptr, draster), --line_count )
71
72
81.4M
    dest += (dest_bit >> 3) & -chunk_align_bytes;
73
81.4M
    ptr = (chunk *) dest;
74
81.4M
    bit = dest_bit & chunk_align_bit_mask;
75
81.4M
    last_bit = width_bits + bit - (chunk_bits + 1);
76
77
81.4M
    if (last_bit < 0) {   /* <=1 chunk */
78
53.1M
        set_mono_thin_mask(right_mask, width_bits, bit);
79
53.1M
        if (pattern == 0)
80
37.7M
            FOR_EACH_LINE(*ptr &= ~right_mask;);
81
15.4M
        else if (pattern == (mono_fill_chunk)(-1))
82
3.74M
            FOR_EACH_LINE(*ptr |= right_mask;);
83
11.7M
        else
84
11.7M
            FOR_EACH_LINE(
85
53.1M
                *ptr = (*ptr & ~right_mask) | (pattern & right_mask); );
86
53.1M
    } else {
87
28.3M
        chunk mask;
88
28.3M
        int last = last_bit >> chunk_log2_bits;
89
90
28.3M
        set_mono_left_mask(mask, bit);
91
28.3M
        set_mono_right_mask(right_mask, (last_bit & chunk_bit_mask) + 1);
92
28.3M
        switch (last) {
93
19.5M
            case 0:   /* 2 chunks */
94
19.5M
                if (pattern == 0)
95
9.22M
                    FOR_EACH_LINE(*ptr &= ~mask; ptr[1] &= ~right_mask;);
96
10.3M
                else if (pattern == (mono_fill_chunk)(-1))
97
793k
                    FOR_EACH_LINE(*ptr |= mask; ptr[1] |= right_mask;);
98
9.51M
                else
99
9.51M
                    FOR_EACH_LINE(
100
19.5M
                        *ptr = (*ptr & ~mask) | (pattern & mask);
101
19.5M
                        ptr[1] = (ptr[1] & ~right_mask) | (pattern & right_mask); );
102
19.5M
                break;
103
2.97M
            case 1:   /* 3 chunks */
104
2.97M
                if (pattern == 0)
105
926k
                    FOR_EACH_LINE( *ptr &= ~mask;
106
2.97M
                                   ptr[1] = 0;
107
2.97M
                                   ptr[2] &= ~right_mask; );
108
2.04M
                else if (pattern == (mono_fill_chunk)(-1))
109
177k
                    FOR_EACH_LINE( *ptr |= mask;
110
2.04M
                                   ptr[1] = ~(chunk) 0;
111
2.04M
                                   ptr[2] |= right_mask; );
112
1.86M
                else
113
1.86M
                    FOR_EACH_LINE( *ptr = (*ptr & ~mask) | (pattern & mask);
114
2.97M
                                    ptr[1] = pattern;
115
2.97M
                                    ptr[2] = (ptr[2] & ~right_mask) | (pattern & right_mask); );
116
2.97M
                break;
117
5.80M
            default:{   /* >3 chunks */
118
5.80M
                    uint byte_count = (last_bit >> 3) & -chunk_bytes;
119
120
5.80M
                    if (pattern == 0)
121
1.52M
                        FOR_EACH_LINE( *ptr &= ~mask;
122
5.80M
                                       memset(ptr + 1, 0, byte_count);
123
5.80M
                                       ptr[last + 1] &= ~right_mask; );
124
4.27M
                    else if (pattern == (mono_fill_chunk)(-1))
125
522k
                        FOR_EACH_LINE( *ptr |= mask;
126
4.27M
                                       memset(ptr + 1, 0xff, byte_count);
127
4.27M
                                       ptr[last + 1] |= right_mask; );
128
3.75M
                    else
129
3.75M
                        FOR_EACH_LINE(
130
5.80M
                                *ptr = (*ptr & ~mask) | (pattern & mask);
131
5.80M
                                memset(ptr + 1, (byte) pattern, byte_count);
132
5.80M
                                ptr[last + 1] = (ptr[last + 1] & ~right_mask) |
133
5.80M
                                                (pattern & right_mask);   );
134
5.80M
                }
135
28.3M
        }
136
28.3M
    }
137
81.4M
#undef FOR_EACH_LINE
138
81.4M
}
139
140
/*
141
 * Similar to bits_fill_rectangle, but with an additional source mask.
142
 * The src_mask variable is 1 for those bits of the original that are
143
 * to be retained. The mask argument must consist of the requisite value
144
 * in every byte, in the same manner as the pattern.
145
 */
146
void
147
bits_fill_rectangle_masked(byte * dest, int dest_bit, uint draster,
148
                    mono_fill_chunk pattern, mono_fill_chunk src_mask,
149
                    int width_bits, int height)
150
173k
{
151
173k
    uint bit;
152
173k
    chunk right_mask;
153
173k
    int line_count = height;
154
173k
    chunk *ptr;
155
173k
    int last_bit;
156
157
173k
#define FOR_EACH_LINE(stat)\
158
173k
        do { stat } while ( inc_ptr(ptr, draster), --line_count )
159
160
173k
    dest += (dest_bit >> 3) & -chunk_align_bytes;
161
173k
    ptr = (chunk *) dest;
162
173k
    bit = dest_bit & chunk_align_bit_mask;
163
173k
    last_bit = width_bits + bit - (chunk_bits + 1);
164
165
173k
    if (last_bit < 0) {   /* <=1 chunk */
166
170k
        set_mono_thin_mask(right_mask, width_bits, bit);
167
170k
        right_mask &= ~src_mask;
168
170k
        if (pattern == 0)
169
6.05k
            FOR_EACH_LINE(*ptr &= ~right_mask;);
170
164k
        else if (pattern == (mono_fill_chunk)(-1))
171
23.3k
            FOR_EACH_LINE(*ptr |= right_mask;);
172
140k
        else
173
140k
            FOR_EACH_LINE(
174
170k
                *ptr = (*ptr & ~right_mask) | (pattern & right_mask); );
175
170k
    } else {
176
3.61k
        chunk mask;
177
3.61k
        int last = last_bit >> chunk_log2_bits;
178
179
3.61k
        set_mono_left_mask(mask, bit);
180
3.61k
        set_mono_right_mask(right_mask, (last_bit & chunk_bit_mask) + 1);
181
3.61k
        mask &= ~src_mask;
182
3.61k
        right_mask &= ~src_mask;
183
3.61k
        switch (last) {
184
2.96k
            case 0:   /* 2 chunks */
185
2.96k
                if (pattern == 0)
186
14
                    FOR_EACH_LINE(*ptr &= ~mask; ptr[1] &= ~right_mask;);
187
2.95k
                else if (pattern == (mono_fill_chunk)(-1))
188
0
                    FOR_EACH_LINE(*ptr |= mask; ptr[1] |= right_mask;);
189
2.95k
                else
190
2.95k
                    FOR_EACH_LINE(
191
2.96k
                        *ptr = (*ptr & ~mask) | (pattern & mask);
192
2.96k
                        ptr[1] = (ptr[1] & ~right_mask) | (pattern & right_mask); );
193
2.96k
                break;
194
264
            case 1:   /* 3 chunks */
195
264
                if (pattern == 0)
196
6
                    FOR_EACH_LINE( *ptr &= ~mask;
197
264
                                   ptr[1] &= src_mask;
198
264
                                   ptr[2] &= ~right_mask; );
199
258
                else if (pattern == (mono_fill_chunk)(-1))
200
0
                    FOR_EACH_LINE( *ptr |= mask;
201
258
                                   ptr[1] |= ~src_mask;
202
258
                                   ptr[2] |= right_mask; );
203
258
                else
204
258
                    FOR_EACH_LINE( *ptr = (*ptr & ~mask) | (pattern & mask);
205
264
                                    ptr[1] =(ptr[1] & src_mask) | pattern;
206
264
                                    ptr[2] = (ptr[2] & ~right_mask) | (pattern & right_mask); );
207
264
                break;
208
381
            default:{   /* >3 chunks */
209
381
                    int     i;
210
211
381
                    if (pattern == 0)
212
111
                        FOR_EACH_LINE( *ptr++ &= ~mask;
213
381
                                       for (i = 0; i < last; i++)
214
381
                                           *ptr++ &= src_mask;
215
381
                                       *ptr &= ~right_mask; );
216
270
                    else if (pattern == (mono_fill_chunk)(-1))
217
0
                        FOR_EACH_LINE( *ptr++ |= mask;
218
270
                                       for (i = 0; i < last; i++)
219
270
                                           *ptr++ |= ~src_mask;
220
270
                                        *ptr |= right_mask; );
221
270
                    else
222
270
                        FOR_EACH_LINE(
223
                            /* note: we know (pattern & ~src_mask) == pattern */
224
381
                            *ptr = (*ptr & ~mask) | (pattern & mask);
225
381
                            ++ptr;
226
381
                            for (i = 0; i < last; i++, ptr++)
227
381
                                *ptr = (*ptr & src_mask) | pattern;
228
381
                            *ptr = (*ptr & ~right_mask) | (pattern & right_mask); );
229
381
                }
230
3.61k
        }
231
3.61k
    }
232
173k
#undef FOR_EACH_LINE
233
173k
}
234
235
/* Replicate a bitmap horizontally in place. */
236
void
237
bits_replicate_horizontally(byte * data, uint width, uint height,
238
                 uint raster, uint replicated_width, uint replicated_raster)
239
971k
{
240
    /* The current algorithm is extremely inefficient! */
241
971k
    const byte *orig_row = data + (height - 1) * raster;
242
971k
    byte *tile_row = data + (height - 1) * replicated_raster;
243
971k
    uint y;
244
245
971k
    if (!(width & 7)) {
246
12.8k
        uint src_bytes = width >> 3;
247
12.8k
        uint dest_bytes = replicated_width >> 3;
248
249
526k
        for (y = height; y-- > 0;
250
513k
             orig_row -= raster, tile_row -= replicated_raster
251
513k
             ) {
252
513k
            uint move = src_bytes;
253
513k
            const byte *from = orig_row;
254
513k
            byte *to = tile_row + dest_bytes - src_bytes;
255
256
513k
            memmove(to, from, move);
257
1.02M
            while (to - tile_row >= move) {
258
513k
                from = to;
259
513k
                to -= move;
260
513k
                memmove(to, from, move);
261
513k
                move <<= 1;
262
513k
            }
263
513k
            if (to != tile_row)
264
513k
                memmove(tile_row, to, to - tile_row);
265
513k
        }
266
958k
    } else {
267
        /*
268
         * This algorithm is inefficient, but probably not worth improving.
269
         */
270
958k
        uint bit_count = width & (uint)(-(int)width);  /* lowest bit: 1, 2, or 4 */
271
958k
        uint left_mask = (0xff00 >> bit_count) & 0xff;
272
273
13.3M
        for (y = height; y-- > 0;
274
12.4M
             orig_row -= raster, tile_row -= replicated_raster
275
12.4M
             ) {
276
12.4M
            uint sx;
277
278
189M
            for (sx = width; sx > 0;) {
279
177M
                uint bits, dx;
280
281
177M
                sx -= bit_count;
282
177M
                bits = (orig_row[sx >> 3] << (sx & 7)) & left_mask;
283
11.3G
                for (dx = sx + replicated_width; dx >= width;) {
284
11.1G
                    byte *dp;
285
11.1G
                    int dbit;
286
287
11.1G
                    dx -= width;
288
11.1G
                    dbit = dx & 7;
289
11.1G
                    dp = tile_row + (dx >> 3);
290
11.1G
                    *dp = (*dp & ~(left_mask >> dbit)) | (bits >> dbit);
291
11.1G
                }
292
177M
            }
293
12.4M
        }
294
958k
    }
295
971k
}
296
297
/* Replicate a bitmap vertically in place. */
298
void
299
bits_replicate_vertically(byte * data, uint height, uint raster,
300
                          uint replicated_height)
301
693
{
302
693
    byte *dest = data;
303
693
    uint h = replicated_height;
304
693
    uint size = raster * height;
305
306
1.38k
    while (h > height) {
307
693
        memcpy(dest + size, dest, size);
308
693
        dest += size;
309
693
        h -= height;
310
693
    }
311
693
}
312
313
/* Find the bounding box of a bitmap. */
314
/* Assume bits beyond the width are zero. */
315
void
316
bits_bounding_box(const byte * data, uint height, uint raster,
317
                  gs_int_rect * pbox)
318
1.92M
{
319
1.92M
    register const ulong *lp;
320
1.92M
    static const byte first_1[16] = {
321
1.92M
        4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0
322
1.92M
    };
323
1.92M
    static const byte last_1[16] = {
324
1.92M
        0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4
325
1.92M
    };
326
327
    /* Count trailing blank rows. */
328
    /* Since the raster is a multiple of sizeof(long), */
329
    /* we don't need to scan by bytes, only by longs. */
330
331
1.92M
    lp = (const ulong *)(data + raster * height);
332
46.8M
    while ((const byte *)lp > data && !lp[-1])
333
44.9M
        --lp;
334
1.92M
    if ((const byte *)lp == data) {
335
156k
        pbox->p.x = pbox->q.x = pbox->p.y = pbox->q.y = 0;
336
156k
        return;
337
156k
    }
338
1.76M
    pbox->q.y = height = ((const byte *)lp - data + raster - 1) / raster;
339
340
    /* Count leading blank rows. */
341
342
1.76M
    lp = (const ulong *)data;
343
41.8M
    while (!*lp)
344
40.0M
        ++lp;
345
1.76M
    {
346
1.76M
        uint n = ((const byte *)lp - data) / raster;
347
348
1.76M
        pbox->p.y = n;
349
1.76M
        if (n)
350
1.76M
            height -= n, data += n * raster;
351
1.76M
    }
352
353
    /* Find the left and right edges. */
354
    /* We know that the first and last rows are non-blank. */
355
356
1.76M
    {
357
1.76M
        uint raster_longs = raster >> ARCH_LOG2_SIZEOF_LONG;
358
1.76M
        uint left = raster_longs - 1, right = 0;
359
1.76M
        ulong llong = 0, rlong = 0;
360
1.76M
        const byte *q;
361
1.76M
        uint h, n;
362
363
38.4M
        for (q = data, h = height; h-- > 0; q += raster) { /* Work from the left edge by longs. */
364
36.6M
            for (lp = (const ulong *)q, n = 0;
365
38.2M
                 n < left && !*lp; lp++, n++
366
36.6M
                );
367
36.6M
            if (n < left)
368
251k
                left = n, llong = *lp;
369
36.4M
            else
370
36.4M
                llong |= *lp;
371
            /* Work from the right edge by longs. */
372
36.6M
            for (lp = (const ulong *)(q + raster - sizeof(long)),
373
36.6M
                 n = raster_longs - 1;
374
375
49.4M
                 n > right && !*lp; lp--, n--
376
36.6M
                );
377
36.6M
            if (n > right)
378
63.8k
                right = n, rlong = *lp;
379
36.6M
            else
380
36.6M
                rlong |= *lp;
381
36.6M
        }
382
383
        /* Do binary subdivision on edge longs.  We assume that */
384
        /* sizeof(long) = 4 or 8. */
385
#if ARCH_SIZEOF_LONG > 8
386
        Error_longs_are_too_large();
387
#endif
388
389
#if ARCH_IS_BIG_ENDIAN
390
#  define last_bits(n) ((1L << (n)) - 1)
391
#  define shift_out_last(x,n) ((x) >>= (n))
392
#  define right_justify_last(x,n) DO_NOTHING
393
#else
394
10.5M
#  define last_bits(n) (-1L << ((ARCH_SIZEOF_LONG * 8) - (n)))
395
7.06M
#  define shift_out_last(x,n) ((x) <<= (n))
396
3.53M
#  define right_justify_last(x,n) (x) >>= ((ARCH_SIZEOF_LONG * 8) - (n))
397
1.76M
#endif
398
399
1.76M
        left <<= ARCH_LOG2_SIZEOF_LONG + 3;
400
1.76M
#if ARCH_SIZEOF_LONG == 8
401
1.76M
        if (llong & ~last_bits(32))
402
1.73M
            shift_out_last(llong, 32);
403
33.9k
        else
404
33.9k
            left += 32;
405
1.76M
#endif
406
1.76M
        if (llong & ~last_bits(16))
407
1.53M
            shift_out_last(llong, 16);
408
227k
        else
409
227k
            left += 16;
410
1.76M
        if (llong & ~last_bits(8))
411
684k
            shift_out_last(llong, 8);
412
1.08M
        else
413
1.08M
            left += 8;
414
1.76M
        right_justify_last(llong, 8);
415
1.76M
        if (llong & 0xf0)
416
1.19M
            left += first_1[(byte) llong >> 4];
417
573k
        else
418
573k
            left += first_1[(byte) llong] + 4;
419
420
1.76M
        right <<= ARCH_LOG2_SIZEOF_LONG + 3;
421
1.76M
#if ARCH_SIZEOF_LONG == 8
422
1.76M
        if (!(rlong & last_bits(32)))
423
1.42M
            shift_out_last(rlong, 32);
424
337k
        else
425
337k
            right += 32;
426
1.76M
#endif
427
1.76M
        if (!(rlong & last_bits(16)))
428
788k
            shift_out_last(rlong, 16);
429
976k
        else
430
976k
            right += 16;
431
1.76M
        if (!(rlong & last_bits(8)))
432
893k
            shift_out_last(rlong, 8);
433
871k
        else
434
871k
            right += 8;
435
1.76M
        right_justify_last(rlong, 8);
436
1.76M
        if (!(rlong & 0xf))
437
965k
            right += last_1[(byte) rlong >> 4];
438
799k
        else
439
799k
            right += last_1[(uint) rlong & 0xf] + 4;
440
441
1.76M
        pbox->p.x = left;
442
1.76M
        pbox->q.x = right;
443
1.76M
    }
444
1.76M
}
445
446
/* Extract a plane from a pixmap. */
447
int
448
bits_extract_plane(const bits_plane_t *dest /*write*/,
449
    const bits_plane_t *source /*read*/, int shift, int width, int height)
450
0
{
451
0
    int source_depth = source->depth;
452
0
    int source_bit = source->x * source_depth;
453
0
    const byte *source_row = source->data.read + (source_bit >> 3);
454
0
    int dest_depth = dest->depth;
455
0
    uint plane_mask = (1 << dest_depth) - 1;
456
0
    int dest_bit = dest->x * dest_depth;
457
0
    byte *dest_row = dest->data.write + (dest_bit >> 3);
458
0
    enum {
459
0
        EXTRACT_SLOW = 0,
460
0
        EXTRACT_4_TO_1,
461
0
        EXTRACT_32_TO_8
462
0
    } loop_case = EXTRACT_SLOW;
463
0
    int y;
464
465
0
    source_bit &= 7;
466
0
    dest_bit &= 7;
467
    /* Check for the fast CMYK cases. */
468
0
    if (!(source_bit | dest_bit)) {
469
0
        switch (source_depth) {
470
0
        case 4:
471
0
            loop_case =
472
0
                (dest_depth == 1 && !(source->raster & 3) &&
473
0
                 !(source->x & 1) ? EXTRACT_4_TO_1 :
474
0
                 EXTRACT_SLOW);
475
0
            break;
476
0
        case 32:
477
0
            if (dest_depth == 8 && !(shift & 7)) {
478
0
                loop_case = EXTRACT_32_TO_8;
479
0
                source_row += 3 - (shift >> 3);
480
0
            }
481
0
            break;
482
0
        }
483
0
    }
484
0
    for (y = 0; y < height;
485
0
         ++y, source_row += source->raster, dest_row += dest->raster
486
0
        ) {
487
0
        int x;
488
489
0
        switch (loop_case) {
490
0
        case EXTRACT_4_TO_1: {
491
0
            const byte *src = source_row;
492
0
            byte *dst = dest_row;
493
494
            /* Do groups of 8 pixels. */
495
0
            for (x = width; x >= 8; src += 4, x -= 8) {
496
0
                bits32 sword =
497
0
                    (*(const bits32 *)src >> shift) & 0x11111111;
498
499
0
                *dst++ =
500
0
                    byte_acegbdfh_to_abcdefgh[(
501
#if ARCH_IS_BIG_ENDIAN
502
                    (sword >> 21) | (sword >> 14) | (sword >> 7) | sword
503
#else
504
0
                    (sword << 3) | (sword >> 6) | (sword >> 15) | (sword >> 24)
505
0
#endif
506
0
                                        ) & 0xff];
507
0
            }
508
0
            if (x) {
509
                /* Do the final 1-7 pixels. */
510
0
                uint test = 0x10 << shift, store = 0x80;
511
512
0
                do {
513
0
                    *dst = (*src & test ? *dst | store : *dst & ~store);
514
0
                    if (test >= 0x10)
515
0
                        test >>= 4;
516
0
                    else
517
0
                        test <<= 4, ++src;
518
0
                    store >>= 1;
519
0
                } while (--x > 0);
520
0
            }
521
0
            break;
522
0
        }
523
0
        case EXTRACT_32_TO_8: {
524
0
            const byte *src = source_row;
525
0
            byte *dst = dest_row;
526
527
0
            for (x = width; x > 0; src += 4, --x)
528
0
                *dst++ = *src;
529
0
            break;
530
0
        }
531
0
        default: {
532
0
            const byte *sptr = source_row;
533
0
            int sbit = source_bit;
534
0
            byte *dptr = dest_row;
535
0
            int dbit = dest_bit;
536
0
            byte dbbyte = (dbit ? (byte)(*dptr & (0xff00 >> dbit)) : 0);
537
538
0
            dbbyte = (dbit ? (byte)(*dptr & (0xff00 >> dbit)) : 0);
539
0
            for (x = width; x > 0; --x) {
540
0
                gx_color_index color;
541
0
                uint pixel;
542
543
0
                if (sizeof(color) > 4) {
544
0
                    if (sample_load_next64((uint64_t *)&color, &sptr, &sbit, source_depth) < 0)
545
0
                        return_error(gs_error_rangecheck);
546
0
                }
547
0
                else {
548
0
                    if (sample_load_next32((uint32_t *)&color, &sptr, &sbit, source_depth) < 0)
549
0
                        return_error(gs_error_rangecheck);
550
0
                }
551
0
                pixel = (color >> shift) & plane_mask;
552
0
                if (sample_store_next8(pixel, &dptr, &dbit, dest_depth, &dbbyte) < 0)
553
0
                    return_error(gs_error_rangecheck);
554
0
            }
555
0
            sample_store_flush(dptr, dbit, dbbyte);
556
0
        }
557
0
        }
558
0
    }
559
0
    return 0;
560
0
}
561
562
/* Expand a plane into a pixmap. */
563
int
564
bits_expand_plane(const bits_plane_t *dest /*write*/,
565
    const bits_plane_t *source /*read*/, int shift, int width, int height)
566
0
{
567
    /*
568
     * Eventually we will optimize this just like bits_extract_plane.
569
     */
570
0
    int source_depth = source->depth;
571
0
    int source_bit = source->x * source_depth;
572
0
    const byte *source_row = source->data.read + (source_bit >> 3);
573
0
    int dest_depth = dest->depth;
574
0
    int dest_bit = dest->x * dest_depth;
575
0
    byte *dest_row = dest->data.write + (dest_bit >> 3);
576
0
    enum {
577
0
        EXPAND_SLOW = 0,
578
0
        EXPAND_1_TO_4,
579
0
        EXPAND_8_TO_32
580
0
    } loop_case = EXPAND_SLOW;
581
0
    int y;
582
583
0
    source_bit &= 7;
584
    /* Check for the fast CMYK cases. */
585
0
    if (!(source_bit || (dest_bit & 31) || (dest->raster & 3))) {
586
0
        switch (dest_depth) {
587
0
        case 4:
588
0
            if (source_depth == 1)
589
0
                loop_case = EXPAND_1_TO_4;
590
0
            break;
591
0
        case 32:
592
0
            if (source_depth == 8 && !(shift & 7))
593
0
                loop_case = EXPAND_8_TO_32;
594
0
            break;
595
0
        }
596
0
    }
597
0
    dest_bit &= 7;
598
0
    switch (loop_case) {
599
600
0
    case EXPAND_8_TO_32: {
601
#if ARCH_IS_BIG_ENDIAN
602
#  define word_shift (shift)
603
#else
604
0
        int word_shift = 24 - shift;
605
0
#endif
606
0
        for (y = 0; y < height;
607
0
             ++y, source_row += source->raster, dest_row += dest->raster
608
0
             ) {
609
0
            int x;
610
0
            const byte *src = source_row;
611
0
            bits32 *dst = (bits32 *)dest_row;
612
613
0
            for (x = width; x > 0; --x)
614
0
                *dst++ = (bits32)(*src++) << word_shift;
615
0
        }
616
0
#undef word_shift
617
0
    }
618
0
        break;
619
620
0
    case EXPAND_1_TO_4:
621
0
    default:
622
0
        for (y = 0; y < height;
623
0
             ++y, source_row += source->raster, dest_row += dest->raster
624
0
             ) {
625
0
            int x;
626
0
            const byte *sptr = source_row;
627
0
            int sbit = source_bit;
628
0
            byte *dptr = dest_row;
629
0
            int dbit = dest_bit;
630
0
            byte dbbyte = (dbit ? (byte)(*dptr & (0xff00 >> dbit)) : 0);
631
632
0
            dbbyte = (dbit ? (byte)(*dptr & (0xff00 >> dbit)) : 0);
633
0
            for (x = width; x > 0; --x) {
634
0
                uint color;
635
0
                gx_color_index pixel;
636
637
0
                if (sample_load_next8(&color, &sptr, &sbit, source_depth) < 0)
638
0
                    return_error(gs_error_rangecheck);
639
640
0
                pixel = (gx_color_index)color << shift;
641
0
                if (sizeof(pixel) > 4) {
642
0
                    if (sample_store_next64(pixel, &dptr, &dbit, dest_depth, &dbbyte) < 0)
643
0
                        return_error(gs_error_rangecheck);
644
0
                }
645
0
                else {
646
0
                    if (sample_store_next32(pixel, &dptr, &dbit, dest_depth, &dbbyte) < 0)
647
0
                        return_error(gs_error_rangecheck);
648
0
                }
649
0
            }
650
0
            sample_store_flush(dptr, dbit, dbbyte);
651
0
        }
652
0
        break;
653
654
0
    }
655
0
    return 0;
656
0
}
657
658
/* ---------------- Byte-oriented operations ---------------- */
659
660
/* Fill a rectangle of bytes. */
661
void
662
bytes_fill_rectangle(byte * dest, uint raster,
663
                     byte value, int width_bytes, int height)
664
9.89M
{
665
41.8M
    while (height-- > 0) {
666
31.9M
        memset(dest, value, width_bytes);
667
31.9M
        dest += raster;
668
31.9M
    }
669
9.89M
}
670
671
/* Copy a rectangle of bytes. */
672
void
673
bytes_copy_rectangle(byte * dest, uint dest_raster,
674
             const byte * src, uint src_raster, int width_bytes, int height)
675
10.7M
{
676
21.4M
    while (height-- > 0) {
677
10.7M
        memcpy(dest, src, width_bytes);
678
10.7M
        src += src_raster;
679
10.7M
        dest += dest_raster;
680
10.7M
    }
681
10.7M
}
682
683
int
684
bytes_rectangle_is_const(const byte * src, uint src_raster,
685
                         int width_bytes, int height)
686
1.26k
{
687
1.26k
    int i;
688
1.26k
    char c;
689
690
1.26k
    if (width_bytes == 0 || height == 0)
691
0
        return -1;
692
1.26k
    c = *src;
693
8.86k
    while (height-- > 0) {
694
8.17k
        const byte *s = src;
695
8.17k
        src += src_raster;
696
495k
        for (i = width_bytes; i > 0; i--)
697
488k
            if (*s++ != c)
698
573
                return -1;
699
8.17k
    }
700
690
    return c;
701
1.26k
}
702
703
/* Copy a rectangle of bytes zeroing any padding bytes. */
704
void
705
bytes_copy_rectangle_zero_padding(byte * dest, uint dest_raster,
706
             const byte * src, uint src_raster, int width_bytes, int height)
707
6.13M
{
708
6.13M
    int padlen = dest_raster;
709
6.13M
    if (padlen < 0)
710
0
        padlen = -padlen;
711
6.13M
    padlen -= width_bytes;
712
6.13M
    if (padlen == 0)
713
4.77M
    {
714
59.3M
        while (height-- > 0) {
715
54.6M
            memcpy(dest, src, width_bytes);
716
54.6M
            src += src_raster;
717
54.6M
            dest += dest_raster;
718
54.6M
        }
719
4.77M
    } else {
720
25.9M
        while (height-- > 0) {
721
24.5M
            memcpy(dest, src, width_bytes);
722
24.5M
            memset(dest+width_bytes, 0, padlen);
723
24.5M
            src += src_raster;
724
24.5M
            dest += dest_raster;
725
24.5M
        }
726
1.36M
    }
727
6.13M
}