summaryrefslogtreecommitdiff
path: root/base/gdevm8.c
blob: 7387c6d6c6a4dff380add3006fea0588dec64ff2 (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
/* 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.
*/

/* 8-bit-per-pixel "memory" (stored bitmap) device */
#include "memory_.h"
#include "gx.h"
#include "gxdevice.h"
#include "gxdevmem.h"           /* semi-public definitions */
#include "gdevmem.h"            /* private definitions */

#define mem_gray8_strip_copy_rop2 mem_gray8_rgb24_strip_copy_rop2

/* ================ Standard (byte-oriented) device ================ */

#undef chunk
#define chunk byte

/* Procedures */
declare_mem_procs(mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle);

/* The device descriptor. */
const gx_device_memory mem_mapped8_device =
    mem_device("image8", 8, 0, mem_dev_initialize_device_procs);

const gdev_mem_functions gdev_mem_fns_8 =
{
    mem_mapped_map_rgb_color,
    mem_mapped_map_color_rgb,
    mem_mapped8_fill_rectangle,
    mem_mapped8_copy_mono,
    mem_mapped8_copy_color,
    gx_default_copy_alpha,
    gx_default_strip_tile_rectangle,
    mem_gray8_strip_copy_rop2,
    mem_get_bits_rectangle
};

/* Convert x coordinate to byte offset in scan line. */
#undef x_to_byte
#define x_to_byte(x) (x)

/* Fill a rectangle with a color. */
static int
mem_mapped8_fill_rectangle(gx_device * dev,
                           int x, int y, int w, int h, gx_color_index color)
{
    gx_device_memory * const mdev = (gx_device_memory *)dev;

    fit_fill(dev, x, y, w, h);
    bytes_fill_rectangle(scan_line_base(mdev, y) + x, mdev->raster,
                         (byte) color, w, h);
    return 0;
}

/* Copy a monochrome bitmap. */
/* We split up this procedure because of limitations in the bcc32 compiler. */
static void mapped8_copy01(chunk *, const byte *, int, int, uint,
                            int, int, byte, byte);
static void mapped8_copyN1(chunk *, const byte *, int, int, uint,
                            int, int, byte);
static void mapped8_copy0N(chunk *, const byte *, int, int, uint,
                            int, int, byte);
static int
mem_mapped8_copy_mono(gx_device * dev,
               const byte * base, int sourcex, int sraster, gx_bitmap_id id,
        int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
{
    gx_device_memory * const mdev = (gx_device_memory *)dev;
    const byte *line;
    int first_bit;

    declare_scan_ptr(dest);
    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
    setup_rect(dest);
    line = base + (sourcex >> 3);
    first_bit = sourcex & 7;
#define is_color(c) ((int)(c) != (int)gx_no_color_index)
    if (is_color(one)) {
        if (is_color(zero))
            mapped8_copy01(dest, line, first_bit, sraster, draster,
                           w, h, (byte) zero, (byte) one);
        else
            mapped8_copyN1(dest, line, first_bit, sraster, draster,
                           w, h, (byte) one);
    } else if (is_color(zero))
        mapped8_copy0N(dest, line, first_bit, sraster, draster,
                       w, h, (byte) zero);
#undef is_color
    return 0;
}
/* Halftone coloring */
static void
mapped8_copy01(chunk * dest, const byte * line, int first_bit,
               int sraster, uint draster, int w, int h, byte b0, byte b1)
{
    while ( h-- > 0 ) {
        register byte *pptr = dest;
        const byte *sptr = line;
        register int sbyte = *sptr++;
        register uint bit;
        int count = w + first_bit - 8; /* How many bits after first byte */
        if (count >= 0) {
            switch (first_bit) {
                case 0:
                    goto enter0;
                case 1:
                    goto enter1;
                case 2:
                    goto enter2;
                case 3:
                    goto enter3;
                case 4:
                    goto enter4;
                case 5:
                    goto enter5;
                case 6:
                    goto enter6;
                default:
                    goto enter7;
            }
            do {
                sbyte = *sptr++;
                /* In true gs fashion: Do not be tempted to replace the
                 * following lines with: *pptr++ = (condition ? b1 : b0);
                 * without good reason as gcc on ARM takes 4 cycles to do that
                 * rather than 3 cycles to do the form here. */
                enter0: if (sbyte & 128) *pptr++ = b1; else *pptr++ = b0;
                enter1: if (sbyte &  64) *pptr++ = b1; else *pptr++ = b0;
                enter2: if (sbyte &  32) *pptr++ = b1; else *pptr++ = b0;
                enter3: if (sbyte &  16) *pptr++ = b1; else *pptr++ = b0;
                enter4: if (sbyte &   8) *pptr++ = b1; else *pptr++ = b0;
                enter5: if (sbyte &   4) *pptr++ = b1; else *pptr++ = b0;
                enter6: if (sbyte &   2) *pptr++ = b1; else *pptr++ = b0;
                enter7: if (sbyte &   1) *pptr++ = b1; else *pptr++ = b0;
                count -= 8;
            } while (count >= 0);
            bit = 128;
            count += 8;
            if (count > 0)
                sbyte = *sptr++;
        } else {
            /* Less than 1 byte to do */
            bit = 0x80>>first_bit;
            count += 8-first_bit;
        }
        while (count > 0) {
            if (sbyte & bit) *pptr++ = b1; else *pptr++ = b0;
            bit >>= 1;
            count--;
        }
        line += sraster;
        inc_ptr(dest, draster);
    }
}
/* Stenciling */
static void
mapped8_copyN1(chunk * dest, const byte * line, int first_bit,
               int sraster, uint draster, int w, int h, byte b1)
{
    while ( h-- > 0 ) {
        register byte *pptr = dest;
        const byte *sptr = line;
        register int sbyte = *sptr++;
        register uint bit;
        int count = w + first_bit - 8; /* How many bits after first byte */
        if (count >= 0) {
            switch (first_bit) {
                case 0:
                    goto enter0;
                case 1:
                    goto enter1;
                case 2:
                    goto enter2;
                case 3:
                    goto enter3;
                case 4:
                    goto enter4;
                case 5:
                    goto enter5;
                case 6:
                    goto enter6;
                default:
                    goto enter7;
            }
            do {
                sbyte = *sptr++;
                enter0: if (sbyte & 128)
                            *pptr = b1;
                        pptr++;
                enter1: if (sbyte & 64)
                            *pptr = b1;
                        pptr++;
                enter2: if (sbyte & 32)
                            *pptr = b1;
                        pptr++;
                enter3: if (sbyte & 16)
                            *pptr = b1;
                        pptr++;
                enter4: if (sbyte & 8)
                            *pptr = b1;
                        pptr++;
                enter5: if (sbyte & 4)
                            *pptr = b1;
                        pptr++;
                enter6: if (sbyte & 2)
                            *pptr = b1;
                        pptr++;
                enter7: if (sbyte & 1)
                            *pptr = b1;
                        pptr++;
                count -= 8;
            } while (count >= 0);
            bit = 128;
            count += 8;
            if (count > 0)
                /* read the byte containing the trailing bits */
                sbyte = *sptr++;
        } else {
            /* Less than 1 byte to do */
            bit = 0x80>>first_bit;
            count += 8-first_bit;
        }
        while (count > 0) {
            if (sbyte & bit) *pptr = b1;
            pptr++;
            bit >>= 1;
            count--;
        }
        line += sraster;
        inc_ptr(dest, draster);
    }
}
/* Reverse stenciling */
static void
mapped8_copy0N(chunk * dest, const byte * line, int first_bit,
               int sraster, uint draster, int w, int h, byte b0)
{
    while ( h-- > 0 ) {
        register byte *pptr = dest;
        const byte *sptr = line;
        register int sbyte = *sptr++;
        register uint bit;
        int count = w + first_bit - 8; /* How many bits after first byte */
        if (count >= 0) {
            switch (first_bit) {
                case 0:
                    goto enter0;
                case 1:
                    goto enter1;
                case 2:
                    goto enter2;
                case 3:
                    goto enter3;
                case 4:
                    goto enter4;
                case 5:
                    goto enter5;
                case 6:
                    goto enter6;
                default:
                    goto enter7;
            }
            do {
                enter0: if (!(sbyte & 128))
                            *pptr = b0;
                        pptr++;
                enter1: if (!(sbyte & 64))
                           *pptr = b0;
                        pptr++;
                enter2: if (!(sbyte & 32))
                            *pptr = b0;
                        pptr++;
                enter3: if (!(sbyte & 16))
                            *pptr = b0;
                        pptr++;
                enter4: if (!(sbyte & 8))
                            *pptr = b0;
                        pptr++;
                enter5: if (!(sbyte & 4))
                            *pptr = b0;
                        pptr++;
                enter6: if (!(sbyte & 2))
                            *pptr = b0;
                        pptr++;
                enter7: if (!(sbyte & 1))
                            *pptr = b0;
                        pptr++;
                sbyte = *sptr++;
                count -= 8;
            } while (count >= 0);
            bit = 128;
            count += 8;
        } else {
            /* Less than 1 byte to do */
            bit = 0x80>>first_bit;
            count += 8-first_bit;
        }
        while (count > 0) {
            if (!(sbyte & bit)) *pptr = b0;
            pptr++;
            bit >>= 1;
            count--;
        }
        line += sraster;
        inc_ptr(dest, draster);
    }
}

/* Copy a color bitmap. */
static int
mem_mapped8_copy_color(gx_device * dev,
               const byte * base, int sourcex, int sraster, gx_bitmap_id id,
                       int x, int y, int w, int h)
{
    gx_device_memory * const mdev = (gx_device_memory *)dev;

    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
    mem_copy_byte_rect(mdev, base, sourcex, sraster, x, y, w, h);
    return 0;
}

/* ================ "Word"-oriented device ================ */

/* Note that on a big-endian machine, this is the same as the */
/* standard byte-oriented-device. */

#if !ARCH_IS_BIG_ENDIAN

/* Procedures */
declare_mem_procs(mem8_word_copy_mono, mem8_word_copy_color, mem8_word_fill_rectangle);

/* Here is the device descriptor. */
const gx_device_memory mem_mapped8_word_device =
    mem_device("image8w", 8, 0, mem_word_dev_initialize_device_procs);

const gdev_mem_functions gdev_mem_fns_8w =
{
    gx_default_rgb_map_rgb_color,
    gx_default_rgb_map_color_rgb,
    mem8_word_fill_rectangle,
    mem8_word_copy_mono,
    mem8_word_copy_color,
    gx_default_copy_alpha,
    gx_default_strip_tile_rectangle,
    gx_no_strip_copy_rop2,
    mem_word_get_bits_rectangle
};

/* Fill a rectangle with a color. */
static int
mem8_word_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
                         gx_color_index color)
{
    gx_device_memory * const mdev = (gx_device_memory *)dev;
    byte *base;
    uint raster;

    fit_fill(dev, x, y, w, h);
    base = scan_line_base(mdev, y);
    raster = mdev->raster;
    mem_swap_byte_rect(base, raster, x << 3, w << 3, h, true);
    bytes_fill_rectangle(base + x, raster, (byte) color, w, h);
    mem_swap_byte_rect(base, raster, x << 3, w << 3, h, true);
    return 0;
}

/* Copy a bitmap. */
static int
mem8_word_copy_mono(gx_device * dev,
               const byte * base, int sourcex, int sraster, gx_bitmap_id id,
        int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
{
    gx_device_memory * const mdev = (gx_device_memory *)dev;
    byte *row;
    uint raster;
    bool store;

    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
    row = scan_line_base(mdev, y);
    raster = mdev->raster;
    store = (zero != gx_no_color_index && one != gx_no_color_index);
    mem_swap_byte_rect(row, raster, x << 3, w << 3, h, store);
    mem_mapped8_copy_mono(dev, base, sourcex, sraster, id,
                          x, y, w, h, zero, one);
    mem_swap_byte_rect(row, raster, x << 3, w << 3, h, false);
    return 0;
}

/* Copy a color bitmap. */
static int
mem8_word_copy_color(gx_device * dev,
               const byte * base, int sourcex, int sraster, gx_bitmap_id id,
                     int x, int y, int w, int h)
{
    gx_device_memory * const mdev = (gx_device_memory *)dev;
    byte *row;
    uint raster;

    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
    row = scan_line_base(mdev, y);
    raster = mdev->raster;
    mem_swap_byte_rect(row, raster, x << 3, w << 3, h, true);
    mem_copy_byte_rect(mdev, base, sourcex, sraster, x, y, w, h);
    mem_swap_byte_rect(row, raster, x << 3, w << 3, h, false);
    return 0;
}

#endif /* !ARCH_IS_BIG_ENDIAN */