home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVMEM.H < prev    next >
Text File  |  1994-07-27  |  12KB  |  271 lines

  1. /* Copyright (C) 1991, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevmem.h */
  20. /* Private definitions for memory devices. */
  21.  
  22. /*
  23.    The representation for a "memory" device is simply a
  24.    contiguous bitmap stored in something like the PostScript
  25.    representation, i.e., each scan line (in left-to-right order), padded
  26.    to a multiple of bitmap_align_mod bytes, followed immediately by
  27.    the next one.
  28.  
  29.    The representation of strings in the interpreter limits
  30.    the size of a string to 64K-1 bytes, which means we can't simply use
  31.    a string for the contents of a memory device.
  32.    We get around this problem by making the client read out the
  33.    contents of a memory device bitmap in pieces.
  34.  
  35.    On 80x86 PCs running in 16-bit mode, there may be no way to
  36.    obtain a contiguous block of storage larger than 64K bytes,
  37.    which typically isn't big enough for a full-screen bitmap.
  38.    We take the following compromise position: if the PC is running in
  39.    native mode (pseudo-segmenting), we limit the bitmap to 64K;
  40.    if the PC is running in protected mode (e.g., under MS Windows),
  41.    we assume that blocks larger than 64K have sequential segment numbers,
  42.    and that the client arranges things so that an individual scan line,
  43.    the scan line pointer table, and any single call on a drawing routine
  44.    do not cross a segment boundary.
  45.  
  46.    Even though the scan lines are stored contiguously, we store a table
  47.    of their base addresses, because indexing into it is faster than
  48.    the multiplication that would otherwise be needed.
  49. */
  50.  
  51. /* ------ Generic macros ------ */
  52.  
  53. /* Macro for declaring the essential device procedures. */
  54. dev_proc_get_initial_matrix(mem_get_initial_matrix);
  55. dev_proc_close_device(mem_close);
  56. #define declare_mem_map_procs(map_rgb_color, map_color_rgb)\
  57.   private dev_proc_map_rgb_color(map_rgb_color);\
  58.   private dev_proc_map_color_rgb(map_color_rgb)
  59. #define declare_mem_procs(copy_mono, copy_color, fill_rectangle)\
  60.   private dev_proc_copy_mono(copy_mono);\
  61.   private dev_proc_copy_color(copy_color);\
  62.   private dev_proc_fill_rectangle(fill_rectangle)
  63.  
  64. /* The following are used for all except planar devices. */
  65. dev_proc_open_device(mem_open);
  66. dev_proc_get_bits(mem_get_bits);
  67. /* The following are used for the non-true-color devices. */
  68. dev_proc_map_rgb_color(mem_mapped_map_rgb_color);
  69. dev_proc_map_color_rgb(mem_mapped_map_color_rgb);
  70.  
  71. /*
  72.  * Macro for generating the device descriptor.
  73.  * Various compilers have problems with the obvious definition
  74.  * for max_value, namely:
  75.  *    (depth >= 8 ? 255 : (1 << depth) - 1)
  76.  * I tried changing (1 << depth) to (1 << (depth & 15)) to forestall bogus
  77.  * error messages about invalid shift counts, but the H-P compiler chokes
  78.  * on this.  Since the only values of depth we ever plan to support are
  79.  * powers of 2 (and 24), we just go ahead and enumerate them.
  80.  */
  81. #define max_value_gray(rgb_depth, gray_depth)\
  82.   (gray_depth ? (1 << gray_depth) - 1 : max_value_rgb(rgb_depth, 0))
  83. #define max_value_rgb(rgb_depth, gray_depth)\
  84.   (rgb_depth >= 8 ? 255 : rgb_depth == 4 ? 15 : rgb_depth == 2 ? 3 :\
  85.    rgb_depth == 1 ? 1 : (1 << gray_depth) - 1)
  86. #define mem_full_device(name, rgb_depth, gray_depth, open, map_rgb_color, map_color_rgb, copy_mono, copy_color, fill_rectangle, get_bits, map_cmyk_color)\
  87. {    sizeof(gx_device_memory),\
  88.     0,            /* &...dev.std_procs */\
  89.     name,            /* differs */\
  90.     0, 0,            /* x and y extent (filled in) */\
  91.     72, 72,            /* density (makes initclip come out right) */\
  92.     no_margins,        /* margins */\
  93.        {    (rgb_depth ? 3 : 0) + (gray_depth ? 1 : 0),    /* num_components */\
  94.         rgb_depth + gray_depth,    /* depth */\
  95.         max_value_gray(rgb_depth, gray_depth),    /* max_gray */\
  96.         max_value_rgb(rgb_depth, gray_depth),    /* max_rgb */\
  97.         max_value_gray(rgb_depth, gray_depth) + 1, /* dither_gray */\
  98.         max_value_rgb(rgb_depth, gray_depth) + 1, /* dither_color */\
  99.        },\
  100.     dev_init_misc,\
  101.     {    open,            /* differs */\
  102.         mem_get_initial_matrix,\
  103.         gx_default_sync_output,\
  104.         gx_default_output_page,\
  105.         mem_close,\
  106.         map_rgb_color,        /* differs */\
  107.         map_color_rgb,        /* differs */\
  108.         fill_rectangle,        /* differs */\
  109.         gx_default_tile_rectangle,\
  110.         copy_mono,        /* differs */\
  111.         copy_color,        /* differs */\
  112.         gx_default_draw_line,\
  113.         get_bits,        /* differs */\
  114.         gx_default_get_params,\
  115.         gx_default_put_params,\
  116.         map_cmyk_color,        /* differs */\
  117.         gx_forward_get_xfont_procs,\
  118.         gx_forward_get_xfont_device,\
  119.         gx_default_map_rgb_alpha_color,\
  120.         gx_forward_get_page_device\
  121.     },\
  122.     0,            /* target */\
  123.     { identity_matrix_body },    /* initial matrix (filled in) */\
  124.     0,            /* raster (filled in) */\
  125.     true,            /* foreign_bits (default) */\
  126.     (byte *)0,        /* base (filled in) */\
  127.     (byte **)0,        /* line_ptrs (filled in by mem_open) */\
  128.     0,            /* bitmap_memory */\
  129.     0,            /* invert (filled in for mono) */\
  130.     { (byte *)0, 0 },    /* palette (filled in for color) */\
  131.     { gx_no_color_index }    /* color24 */\
  132. }
  133. #define mem_device(name, rgb_depth, gray_depth, map_rgb_color, map_color_rgb, copy_mono, copy_color, fill_rectangle)\
  134.   mem_full_device(name, rgb_depth, gray_depth, mem_open, map_rgb_color, map_color_rgb, copy_mono, copy_color, fill_rectangle, mem_get_bits, gx_default_map_cmyk_color)
  135.  
  136. /* Macro for casting gx_device argument */
  137. #define mdev ((gx_device_memory *)dev)
  138.  
  139. /*
  140.  * Macros for processing bitmaps in the largest possible chunks.
  141.  * Bits within a byte are always stored big-endian;
  142.  * bytes are likewise stored in left-to-right order, i.e., big-endian.
  143.  * Note that this is the format used for the source of copy_mono.
  144.  * It used to be the case that bytes were stored in the natural
  145.  * platform order, and the client had force them into big-endian order
  146.  * by calling gdev_mem_ensure_byte_order, but this no longer necessary.
  147.  *
  148.  * Note that we use type uint for register variables holding a chunk:
  149.  * for this reason, the chunk size cannot be larger than uint.
  150.  */
  151. /* Generic macros for chunk accessing. */
  152. #define cbytes(ct) size_of(ct)    /* sizeof may be unsigned */
  153. #  define chunk_bytes cbytes(chunk)
  154. /* The clog2_bytes macro assumes that ints are 2, 4, or 8 bytes in size. */
  155. #define clog2_bytes(ct) (size_of(ct) == 8 ? 3 : size_of(ct)>>1)
  156. #  define chunk_log2_bytes clog2_bytes(chunk)
  157. #define cbits(ct) (size_of(ct)*8)    /* sizeof may be unsigned */
  158. #  define chunk_bits cbits(chunk)
  159. #define clog2_bits(ct) (clog2_bytes(ct)+3)
  160. #  define chunk_log2_bits clog2_bits(chunk)
  161. #define cbit_mask(ct) (cbits(ct)-1)
  162. #  define chunk_bit_mask cbit_mask(chunk)
  163. #define calign_bytes(ct)\
  164.   (sizeof(ct) == 1 ? 1:\
  165.    sizeof(ct) == sizeof(short) ? arch_align_short_mod :\
  166.    sizeof(ct) == sizeof(int) ? arch_align_int_mod: arch_align_long_mod)
  167. #  define chunk_align_bytes calign_bytes(chunk)
  168. #define calign_bit_mask(ct) (calign_bytes(ct)*8-1)
  169. #  define chunk_align_bit_mask calign_bit_mask(chunk)
  170. /*
  171.  * The obvious definition for cmask is:
  172.  *    #define cmask(ct) ((ct)~(ct)0)
  173.  * but this doesn't work on the VAX/VMS compiler, which fails to truncate
  174.  * the value to 16 bits when ct is ushort.
  175.  * Instead, we have to generate the mask with no extra 1-bits.
  176.  * We can't do this in the obvious way:
  177.  *    #define cmask(ct) ((1 << (size_of(ct) * 8)) - 1)
  178.  * because some compilers won't allow a shift of the full type size.
  179.  * Instead, we have to do something really awkward:
  180.  */
  181. #define cmask(ct) ((ct) (((((ct)1 << (size_of(ct)*8-2)) - 1) << 2) + 3))
  182. #  define chunk_all_bits cmask(chunk)
  183. /*
  184.  * The obvious definition for chi_bits is:
  185.  *    #define chi_bits(ct,n) (cmask(ct)-(cmask(ct)>>(n)))
  186.  * but this doesn't work on the DEC/MIPS compilers.
  187.  * Instead, we have to restrict chi_bits to only working for values of n
  188.  * between 0 and cbits(ct)-1, and use
  189.  */
  190. #define chi_bits(ct,n) (ct)(~(ct)1 << (cbits(ct)-1 - (n)))
  191. #  define chunk_hi_bits(n) chi_bits(chunk,n)
  192.  
  193. /* Define whether this is a machine where chunks are long, */
  194. /* but the machine can't shift a long by its full width. */
  195. #define arch_cant_shift_full_chunk\
  196.   (arch_is_big_endian && !arch_ints_are_short && !arch_can_shift_full_long)
  197.  
  198. /*
  199.  * Macro for adding an offset to a pointer.
  200.  * This isn't just pointer arithmetic, because of the segmenting
  201.  * considerations discussed above.
  202.  * Note that this only works for byte * (or char *) pointers!
  203.  */
  204. #  define byte_ptr_add(base, offset)\
  205.      ((byte *)((byte huge *)(base) + (offset)))
  206.  
  207. /*
  208.  * Macros for scan line access.
  209.  * x_to_byte is different for each number of bits per pixel.
  210.  * Note that these macros depend on the definition of chunk:
  211.  * each procedure that uses the scanning macros should #define
  212.  * (not typedef) chunk as either uint or byte.
  213.  */
  214. #define declare_scan_ptr(ptr)    declare_scan_ptr_as(ptr, chunk *)
  215. #define declare_scan_ptr_as(ptr,ptype)\
  216.     register ptype ptr; uint draster
  217. #define inc_chunk_ptr(ptr,delta)\
  218.     ptr = (chunk *)((byte *)ptr + (delta))
  219. #define setup_rect(ptr)   setup_rect_as(ptr, chunk *)
  220. #define setup_rect_as(ptr,ptype)\
  221.     draster = mdev->raster;\
  222.     ptr = (ptype)(scan_line_base(mdev, y) +\
  223.         (x_to_byte(x) & -chunk_align_bytes))
  224.  
  225. /* Define macros for setting up left- and right-end masks. */
  226. /* These are used for monobit operations, and for filling */
  227. /* with 2- and 4-bit-per-pixel patterns. */
  228.  
  229. /* Define the chunk size for monobit operations. */
  230. #if arch_is_big_endian
  231. #  define mono_chunk uint
  232. #  define mono_chunk_bytes arch_sizeof_int
  233. #  define set_mono_right_mask(var, w)\
  234.     var = ((w) == chunk_bits ? chunk_all_bits : chunk_hi_bits(w))
  235. /*
  236.  * We have to split the following statement because of a bug in the Xenix C
  237.  * compiler (it produces a signed rather than an unsigned shift if we don't
  238.  * split).
  239.  */
  240. #  define set_mono_thin_mask(var, w, bit)\
  241.     set_mono_right_mask(var, w), var >>= (bit)
  242. /*
  243.  * We have to split the following statement in two because of a bug
  244.  * in the DEC VAX/VMS C compiler.
  245.  */
  246. #  define set_mono_left_mask(var, bit)\
  247.     var = chunk_all_bits, var >>= (bit)
  248. #else
  249. #  define mono_chunk bits16
  250. #  define mono_chunk_bytes 2
  251. extern const bits16 gdev_mem_swapped_left_masks[17];
  252. #  define set_mono_right_mask(var, w)\
  253.     var = ~gdev_mem_swapped_left_masks[w]
  254. #  define set_mono_thin_mask(var, w, bit)\
  255.     var = ~gdev_mem_swapped_left_masks[(w) + (bit)] &\
  256.         gdev_mem_swapped_left_masks[bit]
  257. #  define set_mono_left_mask(var, bit)\
  258.     var = gdev_mem_swapped_left_masks[bit]
  259. #endif
  260.  
  261. /* ------ Implementations ------ */
  262.  
  263. extern const gx_device_memory far_data mem_mono_device;
  264. extern const gx_device_memory far_data mem_mapped2_color_device;
  265. extern const gx_device_memory far_data mem_mapped4_color_device;
  266. extern const gx_device_memory far_data mem_mapped8_color_device;
  267. extern const gx_device_memory far_data mem_true16_color_device;
  268. extern const gx_device_memory far_data mem_true24_color_device;
  269. extern const gx_device_memory far_data mem_true32_color_device;
  270. extern const gx_device_memory far_data mem_planar_device;
  271.