home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ghostscr / gdevmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-04  |  26.7 KB  |  886 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevmem.c */
  21. /* "Memory" (stored bitmap) device for Ghostscript library. */
  22. #include "memory_.h"
  23. #include "gs.h"
  24. #include "arch.h"
  25. #include "gxbitmap.h"
  26. #include "gsmatrix.h"            /* for gxdevice.h */
  27. #include "gxdevice.h"
  28. #include "gxdevmem.h"
  29.  
  30. typedef struct gx_device_s gx_device;
  31.  
  32. /*
  33.    The obvious representation for a "memory" device is simply a
  34.    contiguous bitmap stored in something like the PostScript
  35.    representation, i.e., each scan line (in left-to-right order), padded
  36.    to a byte or word boundary, followed immediately by the next one.
  37.    Unfortunately, we can't use this representation, for two reasons:
  38.  
  39.     - On PCs with segmented architectures, there is no way to
  40.       obtain a contiguous block of storage larger than 64K bytes,
  41.       which isn't big enough for a full-screen bitmap, even in
  42.       monochrome.
  43.  
  44.     - The representation of strings in the Ghostscript
  45.       interpreter limits the size of a string to 64K-1 bytes,
  46.       which means we can't simply use a string for the contents
  47.       of a memory device.
  48.  
  49.    We get around the former problem by representing a memory device
  50.    as an array of strings: each string holds one scan line.
  51.    We get around the latter problem by making the client read out the
  52.    contents of a memory device bitmap in pieces.
  53. */
  54.  
  55. /* ------ Generic macros ------ */
  56.  
  57. /* Macro for declaring the essential device procedures. */
  58. #define declare_mem_map_procs(map_rgb_color, map_color_rgb)\
  59.   private dev_proc_map_rgb_color(map_rgb_color);\
  60.   private dev_proc_map_color_rgb(map_color_rgb)
  61. #define declare_mem_procs(copy_mono, copy_color, fill_rectangle)\
  62.   private dev_proc_copy_mono(copy_mono);\
  63.   private dev_proc_copy_color(copy_color);\
  64.   private dev_proc_fill_rectangle(fill_rectangle)
  65.  
  66. /* Macro for generating the procedure record in the device descriptor */
  67. #define mem_procs(map_rgb_color, map_color_rgb, copy_mono, copy_color, fill_rectangle)\
  68. {    mem_open,\
  69.     mem_get_initial_matrix,\
  70.     gx_default_sync_output,\
  71.     gx_default_output_page,\
  72.     gx_default_close_device,\
  73.     map_rgb_color,            /* differs */\
  74.     map_color_rgb,            /* differs */\
  75.     fill_rectangle,            /* differs */\
  76.     gx_default_tile_rectangle,\
  77.     copy_mono,            /* differs */\
  78.     copy_color,            /* differs */\
  79.     gx_default_draw_line,\
  80.     gx_default_fill_trapezoid,\
  81.     gx_default_tile_trapezoid\
  82. }
  83.  
  84. /* Macro for generating the device descriptor */
  85. /* The "& 15" in max_value is bogus, to keep certain compilers */
  86. /* from complaining about a left shift by 32. */
  87. #define max_value(depth) (depth > 8 ? 255 : (1 << (depth & 15)) - 1)
  88. #define mem_device(name, depth, procs)\
  89. {    sizeof(gx_device_memory),\
  90.     &procs,            /* differs */\
  91.     name,            /* differs */\
  92.     0, 0,            /* x and y extent (filled in) */\
  93.     1, 1,            /* density (irrelevant) */\
  94.     (depth > 1),        /* has_color */\
  95.     max_value(depth),    /* max_rgb */\
  96.     depth,            /* depth differs */\
  97.     0,            /* not open yet */\
  98.     identity_matrix_body,    /* initial matrix (filled in) */\
  99.     0,            /* raster (filled in) */\
  100.     (byte *)0,        /* base (filled in) */\
  101.     (byte **)0,        /* line_ptrs (filled in by 'open') */\
  102.     mem_no_fault_proc,    /* default bring_in_proc */\
  103.     0,            /* invert (filled in for mono) */\
  104.     0, (byte *)0        /* palette (filled in for color) */\
  105. }
  106.  
  107. /* Macro for casting gx_device argument */
  108. #define mdev ((gx_device_memory *)dev)
  109.  
  110. /* Macro for rectangle arguments (x,y,w,h) */
  111. #define check_rect()\
  112.     if ( w <= 0 || h <= 0 ) return 0;\
  113.     if ( x < 0 || x > mdev->width - w || y < 0 || y > mdev->height - h )\
  114.         return -1
  115.  
  116. /* Macros for processing bitmaps in the largest possible chunks. */
  117. /* Since bits within a byte are always stored big-endian, */
  118. /* we can only use chunks larger than a byte on big-endian machines. */
  119. /* Note that we use type uint for register variables holding a chunk: */
  120. /* for this reason, we only use larger chunks if !ints_are_short. */
  121. #if !big_endian || ints_are_short
  122. #  define log2_chunk_bits 3
  123. #  define chunk byte
  124. #else
  125. #  define log2_chunk_bits 5
  126. #  define chunk ulong
  127. #endif
  128. /* Now generic macros defined in terms of the above. */
  129. #define chunk_bits (1<<log2_chunk_bits)
  130. #define chunk_bytes (chunk_bits/8)
  131. #define chunk_bit_mask (chunk_bits-1)
  132. #define chunk_hi_bit ((chunk)1<<(chunk_bits-1))
  133. #define chunk_all_bits ((chunk_hi_bit<<1)-1)
  134. #define chunk_hi_bits(n) (chunk_all_bits-(chunk_all_bits>>(n)))
  135.  
  136. /* Macros for scan line access. */
  137. /* x_to_byte is different for each number of bits per pixel. */
  138. /* Note that these macros depend on the definition of scan_chunk: */
  139. /* each procedure that uses the scanning macros should #define */
  140. /* (not typedef) scan_chunk as either chunk or byte. */
  141. #define scan_chunk_bytes sizeof(scan_chunk)
  142. #define log2_scan_chunk_bytes (scan_chunk_bytes >> 1)    /* works for 1,2,4 */
  143. #define declare_scan_line(line,ptr)\
  144.     byte **line; register scan_chunk *ptr
  145. #define declare_scan_ptr(line,ptr,offset)\
  146.     byte **line; scan_chunk *ptr; int offset
  147. #define setup_scan_ptr(line,ptr,offset)\
  148.     ptr = (scan_chunk *)((*line) + (offset))
  149. #define setup_scan(line,ptr,offset)\
  150.     line = mdev->line_ptrs + (y);\
  151.     setup_scan_ptr(line,ptr,offset)
  152. #define next_scan_line(line,ptr,offset)\
  153.     ++line; setup_scan_ptr(line,ptr,offset)
  154. #define setup_rect(line,ptr,offset)\
  155.     offset = x_to_byte(x) & -scan_chunk_bytes;\
  156.     setup_scan(line,ptr,offset)
  157.  
  158. /* ------ Generic code ------ */
  159.  
  160. /* Compute the size of the bitmap storage, */
  161. /* including the space for the scan line index. */
  162. /* Note that scan lines are padded to a multiple of 4 bytes. */
  163. ulong
  164. gx_device_memory_bitmap_size(gx_device_memory *dev)
  165. {    unsigned raster =
  166.         ((mdev->width * mdev->bits_per_color_pixel + 31) >> 5) << 2;
  167.     mdev->raster = raster;
  168.     return (ulong)mdev->height * (raster + sizeof(byte *));
  169. }
  170.  
  171. /* 'Open' the memory device by creating the index table if needed. */
  172. private int
  173. mem_open(gx_device *dev)
  174. {
  175. #ifdef __MSDOS__            /* ****** NOTA BENE ****** */
  176. #  include <dos.h>
  177. #  define make_huge_ptr(ptr)\
  178.     ((byte huge *)MK_FP(FP_SEG(ptr), 0) + FP_OFF(ptr))
  179.     byte huge *scan_line = make_huge_ptr(mdev->base);
  180. #else                    /* ****** ****** */
  181.     byte *scan_line = mdev->base;
  182. #endif                    /* ****** ****** */
  183.     byte **pptr = (byte **)(scan_line + (ulong)mdev->height * mdev->raster);
  184.     byte **pend = pptr + mdev->height;
  185.     mdev->line_ptrs = pptr;
  186.     while ( pptr != pend )
  187.        {    *pptr++ = (byte *)scan_line;
  188.         scan_line += mdev->raster;
  189.        }    
  190.     return 0;
  191. }
  192.  
  193. /* Return the initial transformation matrix */
  194. void
  195. mem_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  196. {    *pmat = mdev->initial_matrix;
  197. }
  198.  
  199. /* Test whether a device is a memory device */
  200. int
  201. gs_device_is_memory(gx_device *dev)
  202. {    /* We can't just compare the procs, or even an individual proc, */
  203.     /* because we might be tracing.  Compare the device name, */
  204.     /* and hope for the best. */
  205.     char *name = dev->name;
  206.     int i;
  207.     for ( i = 0; i < 6; i++ )
  208.       if ( name[i] != "image("[i] ) return 0;
  209.     return 1;
  210. }
  211.  
  212. /* Compute the number of data bytes per scan line. */
  213. /* Note that this does not include the padding. */
  214. int
  215. mem_bytes_per_scan_line(gx_device_memory *dev)
  216. {    return (dev->width * dev->bits_per_color_pixel + 7) >> 3;
  217. }
  218.  
  219. /* Copy one or more scan lines to a client. */
  220. #undef scan_chunk
  221. #define scan_chunk byte
  222. int
  223. mem_copy_scan_lines(gx_device_memory *dev, int start_y, byte *str, uint size)
  224. {    declare_scan_ptr(src_line, src, offset);
  225.     uint bytes_per_line = mem_bytes_per_scan_line(dev);
  226.     byte *dest = str;
  227.     int y = start_y;
  228.     uint count = min(size / bytes_per_line, dev->height - y);
  229.     while ( (*dev->bring_in_proc)(dev, 0, y, bytes_per_line, count, 0) < 0 )
  230.        {    /* We can only split in Y, not in X. */
  231.         uint part = count >> 1;
  232.         uint part_size = part * bytes_per_line;
  233.         mem_copy_scan_lines(dev, y, dest, part_size);
  234.         dest += part_size;
  235.         y += part;
  236.         count -= part;
  237.        }
  238.     setup_scan(src_line, src, 0);
  239.     while ( count-- != 0 )
  240.        {    memcpy(dest, src, bytes_per_line);
  241.         next_scan_line(src_line, src, 0);
  242.         dest += bytes_per_line;
  243.         y++;
  244.        }
  245.     return y - start_y;
  246. }
  247.  
  248. /* ------ Page fault recovery ------ */
  249.  
  250. /* Default (no-fault) bring_in_proc */
  251. private int
  252. mem_no_fault_proc(gx_device_memory *dev,
  253.   int x, int y, int w, int h, int writing)
  254. {    return 0;
  255. }
  256.  
  257. /* Recover from bring_in_proc failure in fill_rectangle */
  258. #define check_fault_fill(byte_x, byte_count)\
  259.   if ( mdev->bring_in_proc != mem_no_fault_proc )\
  260.    {    int fault = (*mdev->bring_in_proc)(mdev, byte_x, y, byte_count, h, 1);\
  261.     if ( fault < 0 )\
  262.         return mem_fill_recover(dev, x, y, w, h, color, fault);\
  263.    }
  264. private int
  265. mem_fill_recover(gx_device *dev, int x, int y, int w, int h,
  266.   gx_color_index color, int fault)
  267. {    int nx = x, nw = w, ny = y, nh = h;
  268.     switch ( fault )
  269.        {
  270.     case mem_fault_split_X:
  271.         nx += (nw >>= 1), w -= nw;
  272.         break;
  273.     case mem_fault_split_Y:
  274.         ny += (nh >>= 1), h -= nh;
  275.         break;
  276.     default:
  277.         return fault;
  278.        }
  279.     (*dev->procs->fill_rectangle)(dev, x, y, w, h, color);
  280.     return (*dev->procs->fill_rectangle)(dev, nx, ny, nw, nh, color);
  281. }
  282.  
  283. /* Recover from bring_in_proc failure in copy_mono */
  284. #define check_fault_copy_mono(byte_x, byte_count)\
  285.   if ( mdev->bring_in_proc != mem_no_fault_proc )\
  286.    {    int fault = (*mdev->bring_in_proc)(mdev, byte_x, y, byte_count, h, 1);\
  287.     if ( fault < 0 )\
  288.         return mem_copy_mono_recover(dev, base, sourcex, raster,\
  289.                 x, y, w, h, zero, one, fault);\
  290.    }
  291. private int
  292. mem_copy_mono_recover(gx_device *dev, byte *base, int sourcex, int raster,
  293.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one,
  294.   int fault)
  295. {    int nx = x, nw = w, ny = y, nh = h;
  296.     switch ( fault )
  297.        {
  298.     case mem_fault_split_X:
  299.         nx += (nw >>= 1), w -= nw;
  300.         break;
  301.     case mem_fault_split_Y:
  302.         ny += (nh >>= 1), h -= nh;
  303.         break;
  304.     default:
  305.         return fault;
  306.        }
  307.     (*dev->procs->copy_mono)(dev, base, sourcex, raster,
  308.                  x, y, w, h, zero, one);
  309.     return (*dev->procs->copy_mono)(dev, base, sourcex, raster,
  310.                     nx, ny, nw, nh, zero, one);
  311. }
  312.  
  313. /* Recover from bring_in_proc failure in copy_color */
  314. #define check_fault_copy_color(byte_x, byte_count)\
  315.   if ( mdev->bring_in_proc != mem_no_fault_proc )\
  316.    {    int fault = (*mdev->bring_in_proc)(mdev, byte_x, y, byte_count, h, 1);\
  317.     if ( fault < 0 )\
  318.         return mem_copy_color_recover(dev, base, sourcex, raster,\
  319.                 x, y, w, h, fault);\
  320.    }
  321. private int
  322. mem_copy_color_recover(gx_device *dev, byte *base, int sourcex, int raster,
  323.   int x, int y, int w, int h, int fault)
  324. {    int nx = x, nw = w, ny = y, nh = h;
  325.     switch ( fault )
  326.        {
  327.     case mem_fault_split_X:
  328.         nx += (nw >>= 1), w -= nw;
  329.         break;
  330.     case mem_fault_split_Y:
  331.         ny += (nh >>= 1), h -= nh;
  332.         break;
  333.     default:
  334.         return fault;
  335.        }
  336.     (*dev->procs->copy_color)(dev, base, sourcex, raster,
  337.                  x, y, w, h);
  338.     return (*dev->procs->copy_color)(dev, base, sourcex, raster,
  339.                      nx, ny, nw, nh);
  340. }
  341.  
  342. /* ------ Monochrome ------ */
  343.  
  344. /* Procedures */
  345. declare_mem_procs(mem_mono_copy_mono, mem_mono_copy_color, mem_mono_fill_rectangle);
  346.  
  347. /* The device descriptor. */
  348. private gx_device_procs mem_mono_procs =
  349.   mem_procs(gx_default_map_rgb_color, gx_default_map_color_rgb,
  350.     mem_mono_copy_mono, mem_mono_copy_color, mem_mono_fill_rectangle);
  351.  
  352. /* The instance is public. */
  353. gx_device_memory mem_mono_device =
  354.   mem_device("image(mono)", 1, mem_mono_procs);
  355.  
  356. /* Convert x coordinate to byte offset in scan line. */
  357. #define x_to_byte(x) ((x) >> 3)
  358.  
  359. /* Fill a rectangle with a color. */
  360. #undef scan_chunk
  361. #define scan_chunk chunk
  362. private int
  363. mem_mono_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  364.   gx_color_index color)
  365. {    uint bit;
  366.     chunk right_mask;
  367.     byte fill;
  368.     declare_scan_ptr(dest_line, dest, offset);
  369.     check_fault_fill(x >> 3, ((x + w + 7) >> 3) - (x >> 3));
  370.     check_rect();
  371.     setup_rect(dest_line, dest, offset);
  372. #define write_loop(stat)\
  373.  { int line_count = h;\
  374.    declare_scan_line(ptr_line, ptr);\
  375.    ptr_line = dest_line;\
  376.    setup_scan_ptr(ptr_line, ptr, offset);\
  377.    do { stat; next_scan_line(ptr_line, ptr, offset); }\
  378.    while ( --line_count );\
  379.  }
  380. #define write_partial(msk)\
  381.    if ( fill ) write_loop(*ptr |= msk)\
  382.    else write_loop(*ptr &= ~msk)
  383.     switch ( color )
  384.        {
  385.     case 0: fill = mdev->invert; break;
  386.     case 1: fill = ~mdev->invert; break;
  387.     case gx_no_color_index: return 0;        /* transparent */
  388.     default: return -1;        /* invalid */
  389.        }
  390.     bit = x & chunk_bit_mask;
  391.     if ( bit + w <= chunk_bits )
  392.        {    /* Only one word */
  393.         right_mask = chunk_hi_bits(w) >> bit;
  394.        }
  395.     else
  396.        {    int byte_count;
  397.         if ( bit )
  398.            {    chunk mask = chunk_all_bits >> bit;
  399.             write_partial(mask);
  400.             offset += chunk_bytes;
  401.             w += bit - chunk_bits;
  402.            }
  403.         right_mask = chunk_hi_bits(w & chunk_bit_mask);
  404.         if ( (byte_count = (w >> 3) & -chunk_bytes) != 0 )
  405.            {    write_loop(memset(ptr, fill, byte_count));
  406.             offset += byte_count;
  407.            }
  408.        }
  409.     if ( right_mask )
  410.         write_partial(right_mask);
  411.     return 0;
  412. }
  413.  
  414. /* Copy a monochrome bitmap. */
  415. #undef scan_chunk
  416. #define scan_chunk chunk
  417. private int
  418. mem_mono_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
  419.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  420. {    chunk *line;
  421.     int sleft, dleft;
  422.     uint mask, rmask;
  423.     uint invert, zmask, omask;
  424.     declare_scan_ptr(dest_line, dest, offset);
  425. #define izero (int)zero
  426. #define ione (int)one
  427.     if ( ione == izero )        /* vacuous case */
  428.         return mem_mono_fill_rectangle(dev, x, y, w, h, zero);
  429.     check_fault_copy_mono(x >> 3, ((x + w + 7) >> 3) - (x >> 3));
  430.     check_rect();
  431.     setup_rect(dest_line, dest, offset);
  432.     line = (chunk *)base + (sourcex >> log2_chunk_bits);
  433.     sleft = chunk_bits - (sourcex & chunk_bit_mask);
  434.     dleft = chunk_bits - (x & chunk_bit_mask);
  435.     mask = chunk_all_bits >> (chunk_bits - dleft);
  436.     if ( w < dleft )
  437.         mask -= mask >> w;
  438.     else
  439.         rmask = chunk_hi_bits((w - dleft) & chunk_bit_mask);
  440. /* Macros for writing partial chunks. */
  441. /* bits has already been inverted by xor'ing with invert. */
  442. #define write_chunk_masked(ptr, bits, mask)\
  443.   *ptr = ((bits | ~mask | zmask) & *ptr | (bits & mask & omask))
  444. #define write_chunk(ptr, bits)\
  445.   *ptr = ((bits | zmask) & *ptr | (bits & omask))
  446.     if ( mdev->invert )
  447.        {    if ( izero != (int)gx_no_color_index ) zero ^= 1;
  448.         if ( ione != (int)gx_no_color_index ) one ^= 1;
  449.        }
  450.     invert = (izero == 1 || ione == 0 ? -1 : 0);
  451.     zmask = (izero == 0 || ione == 0 ? 0 : -1);
  452.     omask = (izero == 1 || ione == 1 ? -1 : 0);
  453. #undef izero
  454. #undef ione
  455.     if ( sleft == dleft )        /* optimize the aligned case */
  456.        {    w -= dleft;
  457.         while ( --h >= 0 )
  458.            {    register chunk *bptr = line;
  459.             int count = w;
  460.             register chunk *optr = dest;
  461.             register uint bits = *bptr ^ invert;    /* first partial chunk */
  462.             write_chunk_masked(optr, bits, mask);
  463.             /* Do full chunks. */
  464.             while ( (count -= chunk_bits) >= 0 )
  465.                {    bits = *++bptr ^ invert;
  466.                 ++optr;
  467.                 write_chunk(optr, bits);
  468.                }
  469.             /* Do last chunk */
  470.             if ( count > -chunk_bits )
  471.                {    bits = *++bptr ^ invert;
  472.                 ++optr;
  473.                 write_chunk_masked(optr, bits, rmask);
  474.                }
  475.             next_scan_line(dest_line, dest, offset);
  476.             line = (chunk *)((byte *)line + raster);
  477.            }
  478.        }
  479.     else
  480.        {    int skew = (sleft - dleft) & chunk_bit_mask;
  481.         int cskew = chunk_bits - skew;
  482.         while ( --h >= 0 )
  483.            {    chunk *bptr = line;
  484.             int count = w;
  485.             chunk *optr = dest;
  486.             register int bits;
  487.             /* Do the first partial chunk */
  488.             if ( sleft >= dleft )
  489.                {    bits = *bptr >> skew;
  490.                }
  491.             else /* ( sleft < dleft ) */
  492.                {    bits = *bptr++ << cskew;
  493.                 if ( count > sleft )
  494.                     bits += *bptr >> skew;
  495.                }
  496.             bits ^= invert;
  497.             write_chunk_masked(optr, bits, mask);
  498.             count -= dleft;
  499.             optr++;
  500.             /* Do full chunks. */
  501.             while ( count >= chunk_bits )
  502.                {    bits = *bptr++ << cskew;
  503.                 bits += *bptr >> skew;
  504.                 bits ^= invert;
  505.                 write_chunk(optr, bits);
  506.                 count -= chunk_bits;
  507.                 optr++;
  508.                }
  509.             /* Do last chunk */
  510.             if ( count > 0 )
  511.                {    bits = *bptr++ << cskew;
  512.                 if ( count > skew ) bits += *bptr >> skew;
  513.                 bits ^= invert;
  514.                 write_chunk_masked(optr, bits, rmask);
  515.                }
  516.             next_scan_line(dest_line, dest, offset);
  517.             line = (chunk *)((byte *)line + raster);
  518.            }
  519.        }
  520.     return 0;
  521. }
  522.  
  523. /* Copy a "color" bitmap.  Since "color" is the same as monochrome, */
  524. /* this just reduces to copying a monochrome bitmap. */
  525. private int
  526. mem_mono_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
  527.   int x, int y, int w, int h)
  528. {    return mem_mono_copy_mono(dev, base, sourcex, raster, x, y, w, h,
  529.       (gx_color_index)0, (gx_color_index)1);
  530. }
  531.  
  532. /* ------ Color (mapped or true) ------ */
  533.  
  534. /* Copy a rectangle of bytes from a source to a destination. */
  535. #undef scan_chunk
  536. #define scan_chunk byte
  537. private int
  538. copy_byte_rect(gx_device *dev, byte *source, int sraster,
  539.   int offset, int y, int w, int h)
  540. {    declare_scan_line(dest_line, dest);
  541.     setup_scan(dest_line, dest, offset);
  542.     while ( h-- > 0 )
  543.        {    memcpy(dest, source, w);
  544.         source += sraster;
  545.         next_scan_line(dest_line, dest, offset);
  546.        }
  547.     return 0;
  548. }
  549.  
  550. /* ------ Mapped 8-bit color ------ */
  551.  
  552. /* Procedures */
  553. declare_mem_map_procs(mem_mapped_map_rgb_color, mem_mapped_map_color_rgb);
  554. declare_mem_procs(mem_mapped_copy_mono, mem_mapped_copy_color, mem_mapped_fill_rectangle);
  555.  
  556. /* The device descriptor. */
  557. private gx_device_procs mem_mapped_procs =
  558.   mem_procs(mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  559.     mem_mapped_copy_mono, mem_mapped_copy_color, mem_mapped_fill_rectangle);
  560.  
  561. /* The instance is public. */
  562. gx_device_memory mem_mapped_color_device =
  563.   mem_device("image(8)", 8, mem_mapped_procs);
  564.  
  565. /* Convert x coordinate to byte offset in scan line. */
  566. #undef x_to_byte
  567. #define x_to_byte(x) (x)
  568.  
  569. /* Map a r-g-b color to a color index. */
  570. /* This requires searching the palette. */
  571. private gx_color_index
  572. mem_mapped_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  573. {    register byte *pptr = mdev->palette;
  574.     int cnt = mdev->palette_size;
  575.     byte *which;
  576.     int best = 256*3;
  577.     while ( cnt-- > 0 )
  578.        {    register int diff = *pptr - r;
  579.         if ( diff < 0 ) diff = -diff;
  580.         if ( diff < best )    /* quick rejection */
  581.            {    int dg = pptr[1] - g;
  582.             if ( dg < 0 ) dg = -dg;
  583.             if ( (diff += dg) < best )    /* quick rejection */
  584.                {    int db = pptr[2] - b;
  585.                 if ( db < 0 ) db = -db;
  586.                 if ( (diff += db) < best )
  587.                     which = pptr, best = diff;
  588.                }
  589.            }
  590.         pptr += 3;
  591.        }
  592.     return (gx_color_index)((which - mdev->palette) / 3);
  593. }
  594.  
  595. /* Map a color index to a r-g-b color. */
  596. private int
  597. mem_mapped_map_color_rgb(gx_device *dev, gx_color_index color, ushort *prgb)
  598. {    byte *pptr = mdev->palette + (int)color * 3;
  599.     prgb[0] = pptr[0];
  600.     prgb[1] = pptr[1];
  601.     prgb[2] = pptr[2];
  602.     return 0;
  603. }
  604.  
  605. /* Fill a rectangle with a color. */
  606. private int
  607. mem_mapped_fill_rectangle(gx_device *dev,
  608.   int x, int y, int w, int h, gx_color_index color)
  609. {    declare_scan_ptr(dest_line, dest, offset);
  610.     check_fault_fill(x, w);
  611.     setup_rect(dest_line, dest, offset);
  612.     while ( h-- > 0 )
  613.        {    memset(dest, (byte)color, w);
  614.         next_scan_line(dest_line, dest, offset);
  615.        }
  616.     return 0;
  617. }
  618.  
  619. /* Copy a monochrome bitmap. */
  620. private int
  621. mem_mapped_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
  622.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  623. {    byte *line;
  624.     int first_bit;
  625.     declare_scan_ptr(dest_line, dest, offset);
  626.     check_fault_copy_mono(x, w);
  627.     setup_rect(dest_line, dest, offset);
  628.     line = base + (sourcex >> 3);
  629.     first_bit = 0x80 >> (sourcex & 7);
  630.     while ( h-- > 0 )
  631.        {    register byte *pptr = dest;
  632.         byte *sptr = line;
  633.         register int sbyte = *sptr++;
  634.         register int bit = first_bit;
  635.         int count = w;
  636.         do
  637.            {    if ( sbyte & bit )
  638.                {    if ( one != gx_no_color_index )
  639.                   *pptr = (byte)one;
  640.                }
  641.             else
  642.                {    if ( zero != gx_no_color_index )
  643.                   *pptr = (byte)zero;
  644.                }
  645.             if ( (bit >>= 1) == 0 )
  646.                 bit = 0x80, sbyte = *sptr++;
  647.             pptr++;
  648.            }
  649.         while ( --count > 0 );
  650.         line += raster;
  651.         next_scan_line(dest_line, dest, offset);
  652.        }
  653.     return 0;
  654. }
  655.  
  656. /* Copy a color bitmap. */
  657. private int
  658. mem_mapped_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
  659.   int x, int y, int w, int h)
  660. {    check_fault_copy_color(x, w);
  661.     check_rect();
  662.     return copy_byte_rect(dev, base + x_to_byte(sourcex), raster,
  663.         x_to_byte(x), y, x_to_byte(w), h);
  664. }
  665.  
  666. /* ------ True (24- or 32-bit) color ------ */
  667.  
  668. /* Procedures */
  669. declare_mem_map_procs(mem_true_map_rgb_color, mem_true_map_color_rgb);
  670.  
  671. /* The device descriptor. */
  672. #define mem_true_procs(copy_mono, copy_color, fill_rectangle)\
  673.   mem_procs(mem_true_map_rgb_color, mem_true_map_color_rgb,\
  674.     copy_mono, copy_color, fill_rectangle)
  675.  
  676. /* The instance is public. */
  677. #define mem_true_color_device(name, depth, procs)\
  678. {    sizeof(gx_device_memory),\
  679.     &procs,                /* differs */\
  680.     name,                /* differs */\
  681.     0, 0,            /* x and y extent (filled in) */\
  682.     1, 1,            /* density (irrelevant) */\
  683.     1, 255, depth,            /* depth differs */\
  684.     0,            /* not open yet */\
  685.     identity_matrix_body,    /* initial matrix (filled in) */\
  686.     0,            /* raster (filled in) */\
  687.     (byte *)0,        /* base (filled in) */\
  688.     (byte **)0,        /* line_ptrs (filled in by 'open') */\
  689.     mem_no_fault_proc,    /* default bring_in_proc */\
  690.     0,            /* invert (unused) */\
  691.     0, (byte *)0        /* palette (unused) */\
  692. }
  693.  
  694. /* We want the bytes of a color always to be in the order -,r,g,b, */
  695. /* but we want to manipulate colors as longs.  This requires careful */
  696. /* handling to be byte-order independent. */
  697. #define color_byte(cx,i) (((byte *)&(cx))[i])
  698.  
  699. /* Map a r-g-b color to a color index. */
  700. private gx_color_index
  701. mem_true_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  702. {    gx_color_index color = 0;
  703.     color_byte(color, 1) = r;
  704.     color_byte(color, 2) = g;
  705.     color_byte(color, 3) = b;
  706.     return color;
  707. }
  708.  
  709. /* Map a color index to a r-g-b color. */
  710. private int
  711. mem_true_map_color_rgb(gx_device *dev, gx_color_index color, ushort *prgb)
  712. {    prgb[0] = color_byte(color, 1);
  713.     prgb[1] = color_byte(color, 2);
  714.     prgb[2] = color_byte(color, 3);
  715.     return 0;
  716. }
  717.  
  718. /* ------ 24-bit color ------ */
  719. /* 24-bit takes less space than 32-bit, but is slower. */
  720.  
  721. /* Procedures */
  722. declare_mem_procs(mem_true24_copy_mono, mem_true24_copy_color, mem_true24_fill_rectangle);
  723.  
  724. /* The device descriptor. */
  725. private gx_device_procs mem_true24_procs =
  726.   mem_true_procs(mem_true24_copy_mono, mem_true24_copy_color,
  727.     mem_true24_fill_rectangle);
  728. gx_device_memory mem_true24_color_device =
  729.   mem_device("image(24)", 24, mem_true24_procs);
  730.  
  731. /* Convert x coordinate to byte offset in scan line. */
  732. #undef x_to_byte
  733. #define x_to_byte(x) ((x) * 3)
  734.  
  735. /* Unpack a color into its bytes. */
  736. #define declare_unpack_color(r, g, b, color)\
  737.     byte r = color_byte(color, 1);\
  738.     byte g = color_byte(color, 2);\
  739.     byte b = color_byte(color, 3)
  740. #define put3(ptr, r, g, b)\
  741.     *ptr++ = r, *ptr++ = g, *ptr++ = b
  742.  
  743. /* Fill a rectangle with a color. */
  744. private int
  745. mem_true24_fill_rectangle(gx_device *dev,
  746.   int x, int y, int w, int h, gx_color_index color)
  747. {    declare_unpack_color(r, g, b, color);
  748.     declare_scan_ptr(dest_line, dest, offset);
  749.     check_fault_fill(x * 3, w * 3);
  750.     setup_rect(dest_line, dest, offset);
  751.     while ( h-- > 0 )
  752.        {    register int cnt = w;
  753.         register byte *pptr = dest;
  754.         do { put3(pptr, r, g, b); } while ( --cnt > 0 );
  755.         next_scan_line(dest_line, dest, offset);
  756.        }
  757.     return 0;
  758. }
  759.  
  760. /* Copy a monochrome bitmap. */
  761. private int
  762. mem_true24_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
  763.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  764. {    byte *line;
  765.     int first_bit;
  766.     declare_unpack_color(r0, g0, b0, zero);
  767.     declare_unpack_color(r1, g1, b1, one);
  768.     declare_scan_ptr(dest_line, dest, offset);
  769.     check_fault_copy_mono(x * 3, w * 3);
  770.     setup_rect(dest_line, dest, offset);
  771.     line = base + (sourcex >> 3);
  772.     first_bit = 0x80 >> (sourcex & 7);
  773.     while ( h-- > 0 )
  774.        {    register byte *pptr = dest;
  775.         byte *sptr = line;
  776.         register int sbyte = *sptr++;
  777.         register int bit = first_bit;
  778.         int count = w;
  779.         do
  780.            {    if ( sbyte & bit )
  781.                {    if ( one != gx_no_color_index )
  782.                   put3(pptr, r1, g1, b1);
  783.                }
  784.             else
  785.                {    if ( zero != gx_no_color_index )
  786.                   put3(pptr, r0, g0, b0);
  787.                }
  788.             if ( (bit >>= 1) == 0 )
  789.                 bit = 0x80, sbyte = *sptr++;
  790.            }
  791.         while ( --count > 0 );
  792.         line += raster;
  793.         next_scan_line(dest_line, dest, offset);
  794.        }
  795.     return 0;
  796. }
  797.  
  798. /* Copy a color bitmap. */
  799. private int
  800. mem_true24_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
  801.   int x, int y, int w, int h)
  802. {    check_fault_copy_color(x * 3, w * 3);
  803.     check_rect();
  804.     return copy_byte_rect(dev, base + x_to_byte(sourcex), raster,
  805.         x_to_byte(x), y, x_to_byte(w), h);
  806. }
  807.  
  808. /* ------ 32-bit color ------ */
  809.  
  810. /* Procedures */
  811. declare_mem_procs(mem_true32_copy_mono, mem_true32_copy_color, mem_true32_fill_rectangle);
  812.  
  813. /* The device descriptor. */
  814. private gx_device_procs mem_true32_procs =
  815.   mem_true_procs(mem_true32_copy_mono, mem_true32_copy_color,
  816.     mem_true32_fill_rectangle);
  817. gx_device_memory mem_true32_color_device =
  818.   mem_device("image(32)", 32, mem_true32_procs);
  819.  
  820. /* Convert x coordinate to byte offset in scan line. */
  821. #undef x_to_byte
  822. #define x_to_byte(x) ((x) << 2)
  823.  
  824. /* Fill a rectangle with a color. */
  825. private int
  826. mem_true32_fill_rectangle(gx_device *dev,
  827.   int x, int y, int w, int h, gx_color_index color)
  828. {    declare_scan_ptr(dest_line, dest, offset);
  829.     check_fault_fill(x << 2, w << 2);
  830.     setup_rect(dest_line, dest, offset);
  831.     while ( h-- > 0 )
  832.        {    gx_color_index *pptr = (gx_color_index *)dest;
  833.         int cnt = w;
  834.         do { *pptr++ = color; } while ( --cnt > 0 );
  835.         next_scan_line(dest_line, dest, offset);
  836.        }
  837.     return 0;
  838. }
  839.  
  840. /* Copy a monochrome bitmap. */
  841. private int
  842. mem_true32_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
  843.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  844. {    byte *line;
  845.     int first_bit;
  846.     declare_scan_ptr(dest_line, dest, offset);
  847.     check_fault_copy_mono(x << 2, w << 2);
  848.     setup_rect(dest_line, dest, offset);
  849.     line = base + (sourcex >> 3);
  850.     first_bit = 0x80 >> (sourcex & 7);
  851.     while ( h-- > 0 )
  852.        {    register gx_color_index *pptr = (gx_color_index *)dest;
  853.         byte *sptr = line;
  854.         register int sbyte = *sptr++;
  855.         register int bit = first_bit;
  856.         int count = w;
  857.         do
  858.            {    if ( sbyte & bit )
  859.                {    if ( one != gx_no_color_index )
  860.                   *pptr = one;
  861.                }
  862.             else
  863.                {    if ( zero != gx_no_color_index )
  864.                   *pptr = zero;
  865.                }
  866.             if ( (bit >>= 1) == 0 )
  867.                 bit = 0x80, sbyte = *sptr++;
  868.             pptr++;
  869.            }
  870.         while ( --count > 0 );
  871.         line += raster;
  872.         next_scan_line(dest_line, dest, offset);
  873.        }
  874.     return 0;
  875. }
  876.  
  877. /* Copy a color bitmap. */
  878. private int
  879. mem_true32_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
  880.   int x, int y, int w, int h)
  881. {    check_fault_copy_color(x << 2, w << 2);
  882.     check_rect();
  883.     return copy_byte_rect(dev, base + x_to_byte(sourcex), raster,
  884.         x_to_byte(x), y, x_to_byte(w), h);
  885. }
  886.