home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GZHT.H < prev    next >
C/C++ Source or Header  |  1994-07-27  |  9KB  |  222 lines

  1. /* Copyright (C) 1989, 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. /* gzht.h */
  20. /* Private halftone representation for Ghostscript */
  21. /* Requires gxdevice.h, gxdcolor.h */
  22. #include "gxht.h"
  23.  
  24. /*
  25.  * The whitening order is represented by a pair of arrays.
  26.  * The levels array contains an integer (an index into the bits array)
  27.  * for each distinct halftone level, indicating how many pixels should be
  28.  * whitened for that level; levels[0] = 0, levels[i] <= levels[i+1], and
  29.  * levels[num_levels-1] <= num_bits.
  30.  * The bits array contains an (offset,mask) pair for each pixel in the tile.
  31.  * bits[i].offset is the (properly aligned) byte index of a pixel
  32.  * in the tile; bits[i].mask is the mask to be or'ed into this byte and
  33.  * following ones.  This is arranged so it will work properly on
  34.  * either big- or little-endian machines, and with different mask widths.
  35.  *
  36.  * During sampling, bits[i].mask is used to hold a normalized sample value.
  37.  */
  38. /* The mask width must be at least as wide as uint, */
  39. /* and must not be wider than the width implied by align_bitmap_mod. */
  40. typedef uint ht_mask_t;
  41. #define ht_mask_bits (sizeof(ht_mask_t) * 8)
  42. typedef struct gx_ht_bit_s {
  43.     uint offset;
  44.     ht_mask_t mask;
  45. } gx_ht_bit;
  46.  
  47. /* Define the internal representation of a halftone order. */
  48. /* Note that it includes a cached phase value, computed from */
  49. /* the halftone phase in the graphics state. */
  50. typedef struct gx_ht_cache_s gx_ht_cache;
  51. typedef struct gx_ht_order_s {
  52.     ushort width;
  53.     ushort height;
  54.     ushort raster;
  55.     uint num_levels;        /* = levels size */
  56.     uint num_bits;            /* = width * height = bits size */
  57.     uint *levels;
  58.     gx_ht_bit *bits;
  59.     gs_int_point phase;        /* negated gstate phase mod */
  60.                     /* tile width/height */
  61.     gx_ht_cache *cache;        /* cache to use, 0 means pgs->ht_cache */
  62. } gx_ht_order;
  63. /* We only export st_ht_order for use in st_screen_enum. */
  64. extern_st(st_ht_order);
  65. #define public_st_ht_order()    /* in gsht.c */\
  66.   gs_public_st_ptrs3(st_ht_order, gx_ht_order, "gx_ht_order",\
  67.     ht_order_enum_ptrs, ht_order_reloc_ptrs, levels, bits, cache)
  68. #define st_ht_order_max_ptrs 3
  69.  
  70. /* Procedures for constructing halftone orders */
  71. int    gx_ht_init_order(P5(gx_ht_order *, uint, uint, uint, gs_memory_t *));
  72. void    gx_ht_construct_spot_order(P3(gx_ht_order *, uint, uint));
  73. void    gx_ht_construct_threshold_order(P2(gx_ht_order *, const byte *));
  74. void    gx_ht_construct_bits(P1(gx_ht_order *));
  75.  
  76. /*
  77.  * Define a device halftone.  This consists of one or more orders.
  78.  * If components = 0, then order is the only current halftone screen
  79.  * (set by setscreen, Type 1 sethalftone, Type 3 sethalftone, or
  80.  * Type 5 sethalftone with only a Default).  Otherwise, order is the
  81.  * gray or black screen (for gray/RGB or CMYK devices respectively),
  82.  * and components is an array of gx_ht_order_components parallel to
  83.  * the components of the client halftone (set by setcolorscreen or
  84.  * Type 5 sethalftone).
  85.  */
  86. typedef struct gx_ht_order_component_s {
  87.     gx_ht_order corder;
  88.     gs_ht_separation_name cname;
  89. } gx_ht_order_component;
  90. #define private_st_ht_order_component()    /* in gsht.c */\
  91.   gs_private_st_ptrs_add0(st_ht_order_component, gx_ht_order_component,\
  92.     "gx_ht_order_component", ht_order_component_enum_ptrs,\
  93.      ht_order_component_reloc_ptrs, ht_order_enum_ptrs, ht_order_reloc_ptrs,\
  94.      corder)
  95. #define st_ht_order_component_max_ptrs st_ht_order_max_ptrs
  96. #define private_st_ht_order_comp_element() /* in gsht.c */\
  97.   gs_private_st_element(st_ht_order_component_element, gx_ht_order_component,\
  98.     "gx_ht_order_component[]", ht_order_element_enum_ptrs,\
  99.     ht_order_element_reloc_ptrs, st_ht_order_component)
  100.  
  101. #ifndef gx_device_halftone_DEFINED
  102. #  define gx_device_halftone_DEFINED
  103. typedef struct gx_device_halftone_s gx_device_halftone;
  104. #endif
  105.  
  106. struct gx_device_halftone_s {
  107.     gx_ht_order order;
  108.     uint color_indices[4];
  109.     gx_ht_order_component *components;
  110.     uint num_comp;
  111. };
  112. extern_st(st_device_halftone);
  113. #define public_st_device_halftone() /* in gsht.c */\
  114.   gs_public_st_ptrs_add1(st_device_halftone, gx_device_halftone,\
  115.     "gx_device_halftone", device_halftone_enum_ptrs,\
  116.     device_halftone_reloc_ptrs, ht_order_enum_ptrs, ht_order_reloc_ptrs,\
  117.     order, components)
  118. #define st_device_halftone_max_ptrs (st_ht_order_max_ptrs + 1)
  119.  
  120. /* Halftone enumeration structure */
  121. struct gs_screen_enum_s {
  122.     gs_halftone halftone;    /* supplied by client */
  123.     gx_ht_order order;
  124.     gs_matrix mat;        /* for mapping device x,y to rotated cell */
  125.     int x, y;
  126.     int strip, shift;
  127.     gs_state *pgs;
  128.     /* We allocate a couple of slots here for the convenience of */
  129.     /* the interpreter's sethalftone operator; they aren't used */
  130.     /* at the library level. */
  131.     gx_device_halftone *dev_ht;
  132.     int comp_index;
  133. };
  134. #define private_st_gs_screen_enum() /* in gsht1.c */\
  135.   gs_private_st_composite(st_gs_screen_enum, gs_screen_enum,\
  136.     "gs_screen_enum", screen_enum_enum_ptrs, screen_enum_reloc_ptrs)
  137. /*    order.levels, order.bits, pgs)*/
  138.  
  139. /* Prepare a device halftone for installation, but don't install it. */
  140. int    gs_sethalftone_prepare(P3(gs_state *, gs_halftone *,
  141.                   gx_device_halftone *));
  142.  
  143. /* Update the phase cache in the graphics state */
  144. void    gx_ht_set_phase(P1(gs_state *));
  145.  
  146. /*
  147.  * We don't want to remember all the values of the halftone screen,
  148.  * because they would take up space proportional to P^3, where P is
  149.  * the number of pixels in a cell.  Instead, we pick some number N of
  150.  * patterns to cache.  Each cache slot covers a range of (P+1)/N
  151.  * different gray levels: we "slide" the contents of the slot back and
  152.  * forth within this range by incrementally adding and dropping 1-bits.
  153.  * N>=0 (obviously); N<=P+1 (likewise); also, so that we can simplify things
  154.  * by preallocating the bookkeeping information for the cache, we define
  155.  * a constant max_cached_tiles which is an a priori maximum value for N.
  156.  *
  157.  * Note that the raster for each tile must be a multiple of bitmap_align_mod,
  158.  * to satisfy the copy_mono device routine, even though a multiple of
  159.  * sizeof(ht_mask_t) would otherwise be sufficient.
  160.  */
  161.  
  162. typedef struct gx_ht_tile_s {
  163.     int level;            /* the cached gray level, i.e. */
  164.                     /* the number of spots whitened, */
  165.                     /* or -1 if the cache is empty */
  166.     gx_tile_bitmap tile;        /* the currently rendered tile */
  167. } gx_ht_tile;
  168. struct gx_ht_cache_s {
  169.     /* The following are set when the cache is created. */
  170.     byte *bits;            /* the base of the bits */
  171.     uint bits_size;            /* the space available for bits */
  172.     gx_ht_tile *tiles;        /* the base of the tiles */
  173.     uint num_tiles;            /* the number of tiles allocated */
  174.     /* The following are reset each time the cache is initialized */
  175.     /* for a new screen. */
  176.     gx_ht_order order;        /* the cached order vector */
  177.     int num_cached;            /* actual # of cached tiles */
  178.     int levels_per_tile;        /* # of levels per cached tile */
  179.     gx_bitmap_id base_id;        /* the base id, to which */
  180.                     /* we add the halftone level */
  181. };
  182. #define private_st_ht_cache()    /* in gxht.c */\
  183.   gs_private_st_ptrs4(st_ht_cache, gx_ht_cache, "ht cache",\
  184.     ht_cache_enum_ptrs, ht_cache_reloc_ptrs,\
  185.     bits, tiles, order.levels, order.bits)
  186.  
  187. /* Compute a fractional color for dithering, the correctly rounded */
  188. /* quotient f * max_gx_color_value / maxv. */
  189. #define frac_color_(f, maxv)\
  190.   (gx_color_value)(((f) * (0xffffL * 2) + maxv) / (maxv * 2))
  191. extern const gx_color_value _ds *fc_color_quo[8];
  192. #define fractional_color(f, maxv)\
  193.   ((maxv) <= 7 ? fc_color_quo[maxv][f] : frac_color_(f, maxv))
  194.  
  195. /* ------ Halftone cache procedures ------ */
  196.  
  197. /* Allocate a halftone cache. */
  198. extern const uint
  199.     ht_cache_default_max_tiles,
  200.     ht_cache_default_max_bits;
  201. gx_ht_cache *gx_ht_alloc_cache(P3(gs_memory_t *, uint, uint));
  202.  
  203. /* Clear a halftone cache. */
  204. #define gx_ht_clear_cache(pcache)\
  205.   ((pcache)->order.levels = 0, (pcache)->order.bits = 0)
  206.  
  207. /* Initialize a halftone cache with a given order. */
  208. void gx_ht_init_cache(P2(gx_ht_cache *, const gx_ht_order *));
  209.  
  210. /* Install a halftone in the graphics state. */
  211. int gx_ht_install(P3(gs_state *,
  212.              const gs_halftone *, const gx_device_halftone *));
  213.  
  214. /* Make the cache order current, and return whether */
  215. /* there is room for all possible tiles in the cache. */
  216. bool gx_check_tile_cache(P1(gs_state *));
  217.  
  218. /* Determine whether a given (width, y, height) might fit into a */
  219. /* single tile. If so, return the byte offset of the appropriate row */
  220. /* from the beginning of the tile; if not, return -1. */
  221. int gx_check_tile_size(P4(gs_state *pgs, int w, int y, int h));
  222.