~ubuntu-branches/ubuntu/jaunty/ghostscript/jaunty-updates

« back to all changes in this revision

Viewing changes to src/gdevm24.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2009-01-20 16:40:45 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090120164045-lnfhi0n30o5lwhwa
Tags: 8.64.dfsg.1~svn9377-0ubuntu1
* New upstream release (SVN rev 9377)
   o Fixes many bugs concerning PDF rendering, to make the PDF printing
     workflow correctly working.
   o Fixes long-standing bugs in many drivers, like input paper tray and
     duplex options not working for the built-in PCL 4, 5, 5c, 5e, and
     6/XL drivers, PDF input not working for bjc600, bjc800, and cups
     output devices, several options not working and uninitialized
     memory with cups output device.
   o Merged nearly all patches of the Ubuntu and Debian packages upstream.
   o Fixes LP: #317810, LP: #314439, LP: #314018.
* debian/patches/03_libpaper_support.dpatch,
  debian/patches/11_gs-cjk_font_glyph_handling_fix.dpatch,
  debian/patches/12_gs-cjk_vertical_writing_metrics_fix.dpatch,
  debian/patches/13_gs-cjk_cjkps_examples.dpatch,
  debian/patches/20_bbox_segv_fix.dpatch,
  debian/patches/21_brother_7x0_gdi_fix.dpatch,
  debian/patches/22_epsn_margin_workaround.dpatch,
  debian/patches/24_gs_man_fix.dpatch,
  debian/patches/25_toolbin_insecure_tmp_usage_fix.dpatch,
  debian/patches/26_assorted_script_fixes.dpatch,
  debian/patches/29_gs_css_fix.dpatch,
  debian/patches/30_ps2pdf_man_improvement.dpatch,
  debian/patches/31_fix-gc-sigbus.dpatch,
  debian/patches/34_ftbfs-on-hurd-fix.dpatch,
  debian/patches/35_disable_libcairo.dpatch,
  debian/patches/38_pxl-duplex.dpatch,
  debian/patches/39_pxl-resolution.dpatch,
  debian/patches/42_gs-init-ps-delaybind-fix.dpatch,
  debian/patches/45_bjc600-bjc800-pdf-input.dpatch,
  debian/patches/48_cups-output-device-pdf-duplex-uninitialized-memory-fix.dpatch,
  debian/patches/50_lips4-floating-point-exception.dpatch,
  debian/patches/52_cups-device-logging.dpatch,
  debian/patches/55_pcl-input-slot-fix.dpatch,
  debian/patches/57_pxl-input-slot-fix.dpatch,
  debian/patches/60_pxl-cups-driver-pdf.dpatch,
  debian/patches/62_onebitcmyk-pdf.dpatch,
  debian/patches/65_too-big-temp-files-1.dpatch,
  debian/patches/67_too-big-temp-files-2.dpatch,
  debian/patches/70_take-into-account-data-in-stream-buffer-before-refill.dpatch:
  Removed, applied upstream.
* debian/patches/01_docdir_fix_for_debian.dpatch,
  debian/patches/02_gs_man_fix_debian.dpatch,
  debian/patches/01_docdir-fix-for-debian.dpatch,
  debian/patches/02_docdir-fix-for-debian.dpatch: Renamed patches to
  make merging with Debian easier.
* debian/patches/32_improve-handling-of-media-size-changes-from-gv.dpatch, 
  debian/patches/33_bad-params-to-xinitimage-on-large-bitmaps.dpatch:
  regenerated for new source directory structure.
* debian/rules: Corrected paths to remove cidfmap (it is in Resource/Init/
  in GS 8.64) and to install headers (source paths are psi/ and base/ now).
* debian/rules: Remove all fontmaps, as DeFoMa replaces them.
* debian/local/pdftoraster/pdftoraster.c,
  debian/local/pdftoraster/pdftoraster.convs, debian/rules: Removed
  added pdftoraster filter and use the one which comes with Ghostscript.
* debian/ghostscript.links: s/8.63/8.64/

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2001-2006 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, modified
8
 
   or distributed except as expressly authorized under the terms of that
9
 
   license.  Refer to licensing information at http://www.artifex.com/
10
 
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11
 
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12
 
*/
13
 
/* $Id: gdevm24.c 8539 2008-02-22 20:18:08Z leonardo $ */
14
 
/* 24-bit-per-pixel "memory" (stored bitmap) device */
15
 
#include "memory_.h"
16
 
#include "gx.h"
17
 
#include "gxdevice.h"
18
 
#include "gxdevmem.h"           /* semi-public definitions */
19
 
#include "gdevmem.h"            /* private definitions */
20
 
#include "vdtrace.h"
21
 
 
22
 
#define mem_true24_strip_copy_rop mem_gray8_rgb24_strip_copy_rop
23
 
 
24
 
/*
25
 
 * Define whether to use the library's memset.
26
 
 */
27
 
/*#define USE_MEMSET*/
28
 
/*
29
 
 * Define whether to use memcpy for very wide fills.  We thought this
30
 
 * made a big difference, but it turned out to be an artifact of the
31
 
 * profiler.
32
 
 */
33
 
/*#define USE_MEMCPY*/
34
 
 
35
 
/* Define debugging statistics. */
36
 
#ifdef DEBUG
37
 
struct stats_mem24_s {
38
 
    long
39
 
        fill, fwide, fgray[101], fsetc, fcolor[101], fnarrow[5],
40
 
        fprevc[257];
41
 
    double ftotal;
42
 
} stats_mem24;
43
 
static int prev_count = 0;
44
 
static uint prev_colors[256];
45
 
# define INCR(v) (++(stats_mem24.v))
46
 
#else
47
 
# define INCR(v) DO_NOTHING
48
 
#endif
49
 
 
50
 
 
51
 
/* ================ Standard (byte-oriented) device ================ */
52
 
 
53
 
#undef chunk
54
 
#define chunk byte
55
 
 
56
 
/* Procedures */
57
 
declare_mem_procs(mem_true24_copy_mono, mem_true24_copy_color, mem_true24_fill_rectangle);
58
 
static dev_proc_copy_alpha(mem_true24_copy_alpha);
59
 
 
60
 
/* The device descriptor. */
61
 
const gx_device_memory mem_true24_device =
62
 
mem_full_alpha_device("image24", 24, 0, mem_open,
63
 
                 gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb,
64
 
     mem_true24_copy_mono, mem_true24_copy_color, mem_true24_fill_rectangle,
65
 
                      gx_default_map_cmyk_color, mem_true24_copy_alpha,
66
 
                 gx_default_strip_tile_rectangle, mem_true24_strip_copy_rop,
67
 
                      mem_get_bits_rectangle);
68
 
 
69
 
/* Convert x coordinate to byte offset in scan line. */
70
 
#undef x_to_byte
71
 
#define x_to_byte(x) ((x) * 3)
72
 
 
73
 
/* Unpack a color into its bytes. */
74
 
#define declare_unpack_color(r, g, b, color)\
75
 
        byte r = (byte)(color >> 16);\
76
 
        byte g = (byte)((uint)color >> 8);\
77
 
        byte b = (byte)color
78
 
/* Put a 24-bit color into the bitmap. */
79
 
#define put3(ptr, r, g, b)\
80
 
        (ptr)[0] = r, (ptr)[1] = g, (ptr)[2] = b
81
 
/* Put 4 bytes of color into the bitmap. */
82
 
#define putw(ptr, wxyz)\
83
 
        *(bits32 *)(ptr) = (wxyz)
84
 
/* Load the 3-word 24-bit-color cache. */
85
 
/* Free variables: [m]dev, rgbr, gbrg, brgb. */
86
 
#if arch_is_big_endian
87
 
#  define set_color24_cache(crgb, r, g, b)\
88
 
        mdev->color24.rgbr = rgbr = ((bits32)(crgb) << 8) | (r),\
89
 
        mdev->color24.gbrg = gbrg = (rgbr << 8) | (g),\
90
 
        mdev->color24.brgb = brgb = (gbrg << 8) | (b),\
91
 
        mdev->color24.rgb = (crgb)
92
 
#else
93
 
#  define set_color24_cache(crgb, r, g, b)\
94
 
        mdev->color24.rgbr = rgbr =\
95
 
                ((bits32)(r) << 24) | ((bits32)(b) << 16) |\
96
 
                ((bits16)(g) << 8) | (r),\
97
 
        mdev->color24.brgb = brgb = (rgbr << 8) | (b),\
98
 
        mdev->color24.gbrg = gbrg = (brgb << 8) | (g),\
99
 
        mdev->color24.rgb = (crgb)
100
 
#endif
101
 
 
102
 
/* Fill a rectangle with a color. */
103
 
static int
104
 
mem_true24_fill_rectangle(gx_device * dev,
105
 
                          int x, int y, int w, int h, gx_color_index color)
106
 
{
107
 
    gx_device_memory * const mdev = (gx_device_memory *)dev;
108
 
    declare_unpack_color(r, g, b, color);
109
 
    declare_scan_ptr(dest);
110
 
 
111
 
    if_debug4('b', "[b]device y=%d h=%d x=%d w=%d\n", y + mdev->band_y, h, x, w);
112
 
    /*
113
 
     * In order to avoid testing w > 0 and h > 0 twice, we defer
114
 
     * executing setup_rect, and use fit_fill_xywh instead of
115
 
     * fit_fill.
116
 
     */
117
 
    fit_fill_xywh(dev, x, y, w, h);
118
 
    INCR(fill);
119
 
#ifdef DEBUG
120
 
    stats_mem24.ftotal += w;
121
 
#endif
122
 
    if (w >= 5) {
123
 
        if (h <= 0)
124
 
            return 0;
125
 
        INCR(fwide);
126
 
        setup_rect(dest);
127
 
        if (r == g && r == b) {
128
 
#ifndef USE_MEMSET
129
 
            /* We think we can do better than the library's memset.... */
130
 
            int bcntm7 = w * 3 - 7;
131
 
            register bits32 cword = color | (color << 24);
132
 
 
133
 
            INCR(fgray[min(w, 100)]);
134
 
            while (h-- > 0) {
135
 
                register byte *pptr = dest;
136
 
                byte *limit = pptr + bcntm7;
137
 
 
138
 
                /* We want to store full words, but we have to */
139
 
                /* guarantee that they are word-aligned. */
140
 
                switch (x & 3) {
141
 
                    case 3:
142
 
                        *pptr++ = (byte) cword;
143
 
                    case 2:
144
 
                        *pptr++ = (byte) cword;
145
 
                    case 1:
146
 
                        *pptr++ = (byte) cword;
147
 
                    case 0:;
148
 
                }
149
 
                /* Even with w = 5, we always store at least */
150
 
                /* 3 full words, regardless of the starting x. */
151
 
                *(bits32 *) pptr =
152
 
                    ((bits32 *) pptr)[1] =
153
 
                    ((bits32 *) pptr)[2] = cword;
154
 
                pptr += 12;
155
 
                while (pptr < limit) {
156
 
                    *(bits32 *) pptr =
157
 
                        ((bits32 *) pptr)[1] = cword;
158
 
                    pptr += 8;
159
 
                }
160
 
                switch ((int)(pptr - limit)) {
161
 
                    case 0:
162
 
                        pptr[6] = (byte) cword;
163
 
                    case 1:
164
 
                        pptr[5] = (byte) cword;
165
 
                    case 2:
166
 
                        pptr[4] = (byte) cword;
167
 
                    case 3:
168
 
                        *(bits32 *) pptr = cword;
169
 
                        break;
170
 
                    case 4:
171
 
                        pptr[2] = (byte) cword;
172
 
                    case 5:
173
 
                        pptr[1] = (byte) cword;
174
 
                    case 6:
175
 
                        pptr[0] = (byte) cword;
176
 
                    case 7:;
177
 
                }
178
 
                inc_ptr(dest, draster);
179
 
            }
180
 
#else
181
 
            int bcnt = w * 3;
182
 
 
183
 
            INCR(fgray[min(w, 100)]);
184
 
            while (h-- > 0) {
185
 
                memset(dest, r, bcnt);
186
 
                inc_ptr(dest, draster);
187
 
            }
188
 
#endif
189
 
        } else {
190
 
            int x3 = -x & 3, ww = w - x3;       /* we know ww >= 2 */
191
 
            bits32 rgbr, gbrg, brgb;
192
 
 
193
 
            if (mdev->color24.rgb == color) {
194
 
                rgbr = mdev->color24.rgbr;
195
 
                gbrg = mdev->color24.gbrg;
196
 
                brgb = mdev->color24.brgb;
197
 
            } else {
198
 
                INCR(fsetc);
199
 
                set_color24_cache(color, r, g, b);
200
 
            }
201
 
#ifdef DEBUG
202
 
            {
203
 
                int ci;
204
 
                for (ci = 0; ci < prev_count; ++ci)
205
 
                    if (prev_colors[ci] == color)
206
 
                        break;
207
 
                INCR(fprevc[ci]);
208
 
                if (ci == prev_count) {
209
 
                    if (ci < countof(prev_colors))
210
 
                        ++prev_count;
211
 
                    else
212
 
                        --ci;
213
 
                }
214
 
                if (ci)
215
 
                    memmove(&prev_colors[1], &prev_colors[0],
216
 
                            ci * sizeof(prev_colors[0]));
217
 
                prev_colors[0] = color;
218
 
            }
219
 
#endif
220
 
            INCR(fcolor[min(w, 100)]);
221
 
            while (h-- > 0) {
222
 
                register byte *pptr = dest;
223
 
                int w1 = ww;
224
 
 
225
 
                switch (x3) {
226
 
                    case 1:
227
 
                        put3(pptr, r, g, b);
228
 
                        pptr += 3;
229
 
                        break;
230
 
                    case 2:
231
 
                        pptr[0] = r;
232
 
                        pptr[1] = g;
233
 
                        putw(pptr + 2, brgb);
234
 
                        pptr += 6;
235
 
                        break;
236
 
                    case 3:
237
 
                        pptr[0] = r;
238
 
                        putw(pptr + 1, gbrg);
239
 
                        putw(pptr + 5, brgb);
240
 
                        pptr += 9;
241
 
                        break;
242
 
                    case 0:
243
 
                        ;
244
 
                }
245
 
#ifdef USE_MEMCPY
246
 
                /*
247
 
                 * For very wide fills, it's most efficient to fill a few
248
 
                 * pixels and then use memcpy to fill the rest.
249
 
                 */
250
 
                if (w1 > 16) {
251
 
#define PUTW4(ptr, w)\
252
 
  BEGIN\
253
 
    putw(ptr, w); putw((ptr)+12, w); putw((ptr)+24, w); putw((ptr)+36, w);\
254
 
  END
255
 
                    PUTW4(pptr, rgbr);
256
 
                    PUTW4(pptr + 4, gbrg);
257
 
                    PUTW4(pptr + 8, brgb);
258
 
#undef PUTW4
259
 
                    if (w1 > 64) {
260
 
                        memcpy(pptr + 48, pptr, 48);
261
 
                        memcpy(pptr + 96, pptr, 96);
262
 
                        for (pptr += 192; (w1 -= 64) >= 64; pptr += 192)
263
 
                            memcpy(pptr, pptr - 192, 192);
264
 
                    } else
265
 
                        pptr += 48;
266
 
                    for (; (w1 -= 16) >= 16; pptr += 48)
267
 
                        memcpy(pptr, pptr - 48, 48);
268
 
                }
269
 
#endif
270
 
                while (w1 >= 4) {
271
 
                    putw(pptr, rgbr);
272
 
                    putw(pptr + 4, gbrg);
273
 
                    putw(pptr + 8, brgb);
274
 
                    pptr += 12;
275
 
                    w1 -= 4;
276
 
                }
277
 
                switch (w1) {
278
 
                    case 1:
279
 
                        put3(pptr, r, g, b);
280
 
                        break;
281
 
                    case 2:
282
 
                        putw(pptr, rgbr);
283
 
                        pptr[4] = g;
284
 
                        pptr[5] = b;
285
 
                        break;
286
 
                    case 3:
287
 
                        putw(pptr, rgbr);
288
 
                        putw(pptr + 4, gbrg);
289
 
                        pptr[8] = b;
290
 
                        break;
291
 
                    case 0:
292
 
                        ;
293
 
                }
294
 
                inc_ptr(dest, draster);
295
 
            }
296
 
        }
297
 
    } else if (h > 0) {         /* w < 5 */
298
 
        INCR(fnarrow[max(w, 0)]);
299
 
        setup_rect(dest);
300
 
        switch (w) {
301
 
            case 4:
302
 
                do {
303
 
                    dest[9] = dest[6] = dest[3] = dest[0] = r;
304
 
                    dest[10] = dest[7] = dest[4] = dest[1] = g;
305
 
                    dest[11] = dest[8] = dest[5] = dest[2] = b;
306
 
                    inc_ptr(dest, draster);
307
 
                }
308
 
                while (--h);
309
 
                break;
310
 
            case 3:
311
 
                do {
312
 
                    dest[6] = dest[3] = dest[0] = r;
313
 
                    dest[7] = dest[4] = dest[1] = g;
314
 
                    dest[8] = dest[5] = dest[2] = b;
315
 
                    inc_ptr(dest, draster);
316
 
                }
317
 
                while (--h);
318
 
                break;
319
 
            case 2:
320
 
                do {
321
 
                    dest[3] = dest[0] = r;
322
 
                    dest[4] = dest[1] = g;
323
 
                    dest[5] = dest[2] = b;
324
 
                    inc_ptr(dest, draster);
325
 
                }
326
 
                while (--h);
327
 
                break;
328
 
            case 1:
329
 
                do {
330
 
                    dest[0] = r, dest[1] = g, dest[2] = b;
331
 
                    inc_ptr(dest, draster);
332
 
                }
333
 
                while (--h);
334
 
                break;
335
 
            case 0:
336
 
            default:
337
 
                ;
338
 
        }
339
 
    }
340
 
    return 0;
341
 
}
342
 
 
343
 
/* Copy a monochrome bitmap. */
344
 
static int
345
 
mem_true24_copy_mono(gx_device * dev,
346
 
               const byte * base, int sourcex, int sraster, gx_bitmap_id id,
347
 
        int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
348
 
{
349
 
    gx_device_memory * const mdev = (gx_device_memory *)dev;
350
 
    const byte *line;
351
 
    int sbit;
352
 
    int first_bit;
353
 
 
354
 
    declare_scan_ptr(dest);
355
 
 
356
 
    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
357
 
    setup_rect(dest);
358
 
    line = base + (sourcex >> 3);
359
 
    sbit = sourcex & 7;
360
 
    first_bit = 0x80 >> sbit;
361
 
    if (zero != gx_no_color_index) {    /* Loop for halftones or inverted masks */
362
 
        /* (never used). */
363
 
        declare_unpack_color(r0, g0, b0, zero);
364
 
        declare_unpack_color(r1, g1, b1, one);
365
 
        while (h-- > 0) {
366
 
            register byte *pptr = dest;
367
 
            const byte *sptr = line;
368
 
            register int sbyte = *sptr++;
369
 
            register int bit = first_bit;
370
 
            int count = w;
371
 
 
372
 
            do {
373
 
                if (sbyte & bit) {
374
 
                    if (one != gx_no_color_index) {
375
 
                        put3(pptr, r1, g1, b1);
376
 
                        vd_pixel(int2fixed((pptr - mdev->line_ptrs[y]) / 3), int2fixed(y), RGB(r1, g1, b1));
377
 
                    }
378
 
                } else {
379
 
                    put3(pptr, r0, g0, b0);
380
 
                    vd_pixel(int2fixed((pptr - mdev->line_ptrs[y]) / 3), int2fixed(y), RGB(r0, g0, b0));
381
 
                }
382
 
                pptr += 3;
383
 
                if ((bit >>= 1) == 0)
384
 
                    bit = 0x80, sbyte = *sptr++;
385
 
            }
386
 
            while (--count > 0);
387
 
            line += sraster;
388
 
            inc_ptr(dest, draster);
389
 
        }
390
 
    } else if (one != gx_no_color_index) {      /* Loop for character and pattern masks. */
391
 
        /* This is used heavily. */
392
 
        declare_unpack_color(r1, g1, b1, one);
393
 
        int first_mask = first_bit << 1;
394
 
        int first_count, first_skip;
395
 
 
396
 
        if (sbit + w > 8)
397
 
            first_mask -= 1,
398
 
                first_count = 8 - sbit;
399
 
        else
400
 
            first_mask -= first_mask >> w,
401
 
                first_count = w;
402
 
        first_skip = first_count * 3;
403
 
        while (h-- > 0) {
404
 
            register byte *pptr = dest;
405
 
            const byte *sptr = line;
406
 
            register int sbyte = *sptr++ & first_mask;
407
 
            int count = w - first_count;
408
 
 
409
 
            if (sbyte) {
410
 
                register int bit = first_bit;
411
 
 
412
 
                do {
413
 
                    if (sbyte & bit)
414
 
                        put3(pptr, r1, g1, b1);
415
 
                    pptr += 3;
416
 
                }
417
 
                while ((bit >>= 1) & first_mask);
418
 
            } else
419
 
                pptr += first_skip;
420
 
            while (count >= 8) {
421
 
                sbyte = *sptr++;
422
 
                if (sbyte & 0xf0) {
423
 
                    if (sbyte & 0x80)
424
 
                        put3(pptr, r1, g1, b1);
425
 
                    if (sbyte & 0x40)
426
 
                        put3(pptr + 3, r1, g1, b1);
427
 
                    if (sbyte & 0x20)
428
 
                        put3(pptr + 6, r1, g1, b1);
429
 
                    if (sbyte & 0x10)
430
 
                        put3(pptr + 9, r1, g1, b1);
431
 
                }
432
 
                if (sbyte & 0xf) {
433
 
                    if (sbyte & 8)
434
 
                        put3(pptr + 12, r1, g1, b1);
435
 
                    if (sbyte & 4)
436
 
                        put3(pptr + 15, r1, g1, b1);
437
 
                    if (sbyte & 2)
438
 
                        put3(pptr + 18, r1, g1, b1);
439
 
                    if (sbyte & 1)
440
 
                        put3(pptr + 21, r1, g1, b1);
441
 
                }
442
 
                pptr += 24;
443
 
                count -= 8;
444
 
            }
445
 
            if (count > 0) {
446
 
                register int bit = 0x80;
447
 
 
448
 
                sbyte = *sptr++;
449
 
                do {
450
 
                    if (sbyte & bit)
451
 
                        put3(pptr, r1, g1, b1);
452
 
                    pptr += 3;
453
 
                    bit >>= 1;
454
 
                }
455
 
                while (--count > 0);
456
 
            }
457
 
            line += sraster;
458
 
            inc_ptr(dest, draster);
459
 
        }
460
 
    }
461
 
    return 0;
462
 
}
463
 
 
464
 
/* Copy a color bitmap. */
465
 
static int
466
 
mem_true24_copy_color(gx_device * dev,
467
 
               const byte * base, int sourcex, int sraster, gx_bitmap_id id,
468
 
                      int x, int y, int w, int h)
469
 
{
470
 
    gx_device_memory * const mdev = (gx_device_memory *)dev;
471
 
 
472
 
    if_debug1('w', "[w]device y=%d:\n", y + mdev->band_y); /* See siscale.c about 'w'. */
473
 
    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
474
 
    mem_copy_byte_rect(mdev, base, sourcex, sraster, x, y, w, h);
475
 
    return 0;
476
 
}
477
 
 
478
 
/* Copy an alpha map. */
479
 
static int
480
 
mem_true24_copy_alpha(gx_device * dev, const byte * base, int sourcex,
481
 
                   int sraster, gx_bitmap_id id, int x, int y, int w, int h,
482
 
                      gx_color_index color, int depth)
483
 
{
484
 
    gx_device_memory * const mdev = (gx_device_memory *)dev;
485
 
    const byte *line;
486
 
 
487
 
    declare_scan_ptr(dest);
488
 
    declare_unpack_color(r, g, b, color);
489
 
 
490
 
    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
491
 
    setup_rect(dest);
492
 
    line = base;
493
 
    while (h-- > 0) {
494
 
        register byte *pptr = dest;
495
 
        int sx;
496
 
 
497
 
        for (sx = sourcex; sx < sourcex + w; ++sx, pptr += 3) {
498
 
            int alpha2, alpha;
499
 
 
500
 
            if (depth == 2)     /* map 0 - 3 to 0 - 15 */
501
 
                alpha =
502
 
                    ((line[sx >> 2] >> ((3 - (sx & 3)) << 1)) & 3) * 5;
503
 
            else
504
 
                alpha2 = line[sx >> 1],
505
 
                    alpha = (sx & 1 ? alpha2 & 0xf : alpha2 >> 4);
506
 
            if (alpha == 15) {  /* Just write the new color. */
507
 
                put3(pptr, r, g, b);
508
 
            } else if (alpha != 0) {    /* Blend RGB values. */
509
 
#define make_shade(old, clr, alpha, amax) \
510
 
  (old) + (((int)(clr) - (int)(old)) * (alpha) / (amax))
511
 
                pptr[0] = make_shade(pptr[0], r, alpha, 15);
512
 
                pptr[1] = make_shade(pptr[1], g, alpha, 15);
513
 
                pptr[2] = make_shade(pptr[2], b, alpha, 15);
514
 
#undef make_shade
515
 
            }
516
 
        }
517
 
        line += sraster;
518
 
        inc_ptr(dest, draster);
519
 
    }
520
 
    return 0;
521
 
}
522
 
 
523
 
/* ================ "Word"-oriented device ================ */
524
 
 
525
 
/* Note that on a big-endian machine, this is the same as the */
526
 
/* standard byte-oriented-device. */
527
 
 
528
 
#if !arch_is_big_endian
529
 
 
530
 
/* Procedures */
531
 
declare_mem_procs(mem24_word_copy_mono, mem24_word_copy_color, mem24_word_fill_rectangle);
532
 
 
533
 
/* Here is the device descriptor. */
534
 
const gx_device_memory mem_true24_word_device =
535
 
mem_full_device("image24w", 24, 0, mem_open,
536
 
                gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb,
537
 
     mem24_word_copy_mono, mem24_word_copy_color, mem24_word_fill_rectangle,
538
 
                gx_default_map_cmyk_color, gx_default_strip_tile_rectangle,
539
 
                gx_no_strip_copy_rop, mem_word_get_bits_rectangle);
540
 
 
541
 
/* Fill a rectangle with a color. */
542
 
static int
543
 
mem24_word_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
544
 
                          gx_color_index color)
545
 
{
546
 
    gx_device_memory * const mdev = (gx_device_memory *)dev;
547
 
    byte *base;
548
 
    uint raster;
549
 
 
550
 
    fit_fill(dev, x, y, w, h);
551
 
    base = scan_line_base(mdev, y);
552
 
    raster = mdev->raster;
553
 
    mem_swap_byte_rect(base, raster, x * 24, w * 24, h, true);
554
 
    mem_true24_fill_rectangle(dev, x, y, w, h, color);
555
 
    mem_swap_byte_rect(base, raster, x * 24, w * 24, h, false);
556
 
    return 0;
557
 
}
558
 
 
559
 
/* Copy a bitmap. */
560
 
static int
561
 
mem24_word_copy_mono(gx_device * dev,
562
 
               const byte * base, int sourcex, int sraster, gx_bitmap_id id,
563
 
        int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
564
 
{
565
 
    gx_device_memory * const mdev = (gx_device_memory *)dev;
566
 
    byte *row;
567
 
    uint raster;
568
 
    bool store;
569
 
 
570
 
    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
571
 
    row = scan_line_base(mdev, y);
572
 
    raster = mdev->raster;
573
 
    store = (zero != gx_no_color_index && one != gx_no_color_index);
574
 
    mem_swap_byte_rect(row, raster, x * 24, w * 24, h, store);
575
 
    mem_true24_copy_mono(dev, base, sourcex, sraster, id,
576
 
                         x, y, w, h, zero, one);
577
 
    mem_swap_byte_rect(row, raster, x * 24, w * 24, h, false);
578
 
    return 0;
579
 
}
580
 
 
581
 
/* Copy a color bitmap. */
582
 
static int
583
 
mem24_word_copy_color(gx_device * dev,
584
 
               const byte * base, int sourcex, int sraster, gx_bitmap_id id,
585
 
                      int x, int y, int w, int h)
586
 
{
587
 
    gx_device_memory * const mdev = (gx_device_memory *)dev;
588
 
    byte *row;
589
 
    uint raster;
590
 
 
591
 
    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
592
 
    row = scan_line_base(mdev, y);
593
 
    raster = mdev->raster;
594
 
    mem_swap_byte_rect(row, raster, x * 24, w * 24, h, true);
595
 
    bytes_copy_rectangle(row + x * 3, raster, base + sourcex * 3, sraster,
596
 
                         w * 3, h);
597
 
    mem_swap_byte_rect(row, raster, x * 24, w * 24, h, false);
598
 
    return 0;
599
 
}
600
 
 
601
 
#endif /* !arch_is_big_endian */