gzht.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gzht.h,v 1.4 2000/10/22 03:15:26 lpd Exp $ */
  16. /* Internal procedures for halftones */
  17. /* Requires gxdevice.h, gxdcolor.h */
  18. #ifndef gzht_INCLUDED
  19. # define gzht_INCLUDED
  20. #include "gscsel.h"
  21. #include "gxht.h"
  22. #include "gxfmap.h"
  23. #include "gxdht.h"
  24. #include "gxhttile.h"
  25. /* Sort a sampled halftone order by sample value. */
  26. void gx_sort_ht_order(P2(gx_ht_bit *, uint));
  27. /* (Internal) procedures for constructing halftone orders. */
  28. int gx_ht_alloc_ht_order(P8(gx_ht_order * porder, uint width, uint height,
  29. uint num_levels, uint num_bits, uint strip_shift,
  30. const gx_ht_order_procs_t *procs,
  31. gs_memory_t * mem));
  32. int gx_ht_alloc_order(P6(gx_ht_order * porder, uint width, uint height,
  33. uint strip_shift, uint num_levels, gs_memory_t *mem));
  34. int gx_ht_alloc_threshold_order(P5(gx_ht_order * porder, uint width,
  35. uint height, uint num_levels,
  36. gs_memory_t * mem));
  37. int gx_ht_alloc_client_order(P6(gx_ht_order * porder, uint width, uint height,
  38. uint num_levels, uint num_bits, gs_memory_t * mem));
  39. void gx_ht_construct_spot_order(P1(gx_ht_order *));
  40. int gx_ht_construct_threshold_order(P2(gx_ht_order *, const byte *));
  41. void gx_ht_construct_bit(P3(gx_ht_bit * bit, int width, int bit_num));
  42. void gx_ht_construct_bits(P1(gx_ht_order *));
  43. /* Halftone enumeration structure */
  44. struct gs_screen_enum_s {
  45. gs_halftone halftone; /* supplied by client */
  46. gx_ht_order order;
  47. gs_matrix mat; /* for mapping device x,y to rotated cell */
  48. gs_matrix mat_inv; /* the inversion of mat */
  49. int x, y;
  50. int strip, shift;
  51. gs_state *pgs;
  52. };
  53. #define private_st_gs_screen_enum() /* in gshtscr.c */\
  54. gs_private_st_composite(st_gs_screen_enum, gs_screen_enum,\
  55. "gs_screen_enum", screen_enum_enum_ptrs, screen_enum_reloc_ptrs)
  56. /* order.levels, order.bits, pgs) */
  57. /* Prepare a device halftone for installation, but don't install it. */
  58. int gs_sethalftone_prepare(P3(gs_state *, gs_halftone *,
  59. gx_device_halftone *));
  60. /* Allocate and initialize a spot screen. */
  61. /* This is the first half of gs_screen_init_accurate/memory. */
  62. int gs_screen_order_alloc(P2(gx_ht_order *, gs_memory_t *));
  63. int gs_screen_order_init_memory(P5(gx_ht_order *, const gs_state *,
  64. gs_screen_halftone *, bool, gs_memory_t *));
  65. #define gs_screen_order_init(porder, pgs, phsp, accurate)\
  66. gs_screen_order_init_memory(porder, pgs, phsp, accurate, pgs->memory)
  67. /* Prepare to sample a spot screen. */
  68. /* This is the second half of gs_screen_init_accurate/memory. */
  69. int gs_screen_enum_init_memory(P5(gs_screen_enum *, const gx_ht_order *,
  70. gs_state *, const gs_screen_halftone *,
  71. gs_memory_t *));
  72. #define gs_screen_enum_init(penum, porder, pgs, phsp)\
  73. gs_screen_enum_init_memory(penum, porder, pgs, phsp, pgs->memory)
  74. /* Process an entire screen plane. */
  75. int gx_ht_process_screen_memory(P5(gs_screen_enum * penum, gs_state * pgs,
  76. gs_screen_halftone * phsp, bool accurate,
  77. gs_memory_t * mem));
  78. #define gx_ht_process_screen(penum, pgs, phsp, accurate)\
  79. gx_ht_process_screen_memory(penum, pgs, phsp, accurate, pgs->memory)
  80. /*
  81. * We don't want to remember all the values of the halftone screen,
  82. * because they would take up space proportional to P^3, where P is
  83. * the number of pixels in a cell. Instead, we pick some number N of
  84. * patterns to cache. Each cache slot covers a range of (P+1)/N
  85. * different gray levels: we "slide" the contents of the slot back and
  86. * forth within this range by incrementally adding and dropping 1-bits.
  87. * N>=0 (obviously); N<=P+1 (likewise); also, so that we can simplify things
  88. * by preallocating the bookkeeping information for the cache, we define
  89. * a constant max_cached_tiles which is an a priori maximum value for N.
  90. *
  91. * Note that the raster for each tile must be a multiple of bitmap_align_mod,
  92. * to satisfy the copy_mono device routine, even though a multiple of
  93. * sizeof(ht_mask_t) would otherwise be sufficient.
  94. */
  95. struct gx_ht_cache_s {
  96. /* The following are set when the cache is created. */
  97. byte *bits; /* the base of the bits */
  98. uint bits_size; /* the space available for bits */
  99. gx_ht_tile *ht_tiles; /* the base of the tiles */
  100. uint num_tiles; /* the number of tiles allocated */
  101. /* The following are reset each time the cache is initialized */
  102. /* for a new screen. */
  103. gx_ht_order order; /* the cached order vector */
  104. int num_cached; /* actual # of cached tiles */
  105. int levels_per_tile; /* # of levels per cached tile */
  106. int tiles_fit; /* -1 if not determined, 0 if no fit, */
  107. /* 1 if fit */
  108. gx_bitmap_id base_id; /* the base id, to which */
  109. /* we add the halftone level */
  110. gx_ht_tile *(*render_ht)(P2(gx_ht_cache *, int)); /* rendering procedure */
  111. };
  112. /* We don't mark from the tiles pointer, and we relocate the tiles en masse. */
  113. #define private_st_ht_tiles() /* in gxht.c */\
  114. gs_private_st_composite(st_ht_tiles, gx_ht_tile, "ht tiles",\
  115. ht_tiles_enum_ptrs, ht_tiles_reloc_ptrs)
  116. #define private_st_ht_cache() /* in gxht.c */\
  117. gs_private_st_ptrs_add2(st_ht_cache, gx_ht_cache, "ht cache",\
  118. ht_cache_enum_ptrs, ht_cache_reloc_ptrs,\
  119. st_ht_order, order, bits, ht_tiles)
  120. /* Compute a fractional color for dithering, the correctly rounded */
  121. /* quotient f * max_gx_color_value / maxv. */
  122. #define frac_color_(f, maxv)\
  123. (gx_color_value)(((f) * (0xffffL * 2) + maxv) / (maxv * 2))
  124. extern const gx_color_value *const fc_color_quo[8];
  125. #define fractional_color(f, maxv)\
  126. ((maxv) <= 7 ? fc_color_quo[maxv][f] : frac_color_(f, maxv))
  127. /* ------ Halftone cache procedures ------ */
  128. /* Allocate/free a halftone cache. */
  129. uint gx_ht_cache_default_tiles(P0());
  130. uint gx_ht_cache_default_bits(P0());
  131. gx_ht_cache *gx_ht_alloc_cache(P3(gs_memory_t *, uint, uint));
  132. void gx_ht_free_cache(P2(gs_memory_t *, gx_ht_cache *));
  133. /* Clear a halftone cache. */
  134. #define gx_ht_clear_cache(pcache)\
  135. ((pcache)->order.levels = 0, (pcache)->order.bit_data = 0,\
  136. (pcache)->ht_tiles[0].tiles.data = 0)
  137. /* Initialize a halftone cache with a given order. */
  138. void gx_ht_init_cache(P2(gx_ht_cache *, const gx_ht_order *));
  139. /* Make the cache order current, and return whether */
  140. /* there is room for all possible tiles in the cache. */
  141. bool gx_check_tile_cache(P1(const gs_imager_state *));
  142. /* Determine whether a given (width, y, height) might fit into a */
  143. /* single tile. If so, return the byte offset of the appropriate row */
  144. /* from the beginning of the tile, and set *ppx to the x phase offset */
  145. /* within the tile; if not, return -1. */
  146. int gx_check_tile_size(P6(const gs_imager_state * pis, int w, int y, int h,
  147. gs_color_select_t select, int *ppx));
  148. /* Make a given level current in a halftone cache. */
  149. #define gx_render_ht(pcache, b_level)\
  150. ((pcache)->render_ht(pcache, b_level))
  151. /* ------ Device halftone management ------ */
  152. /* Release a gx_ht_order by freeing its components. */
  153. /* (Don't free the gx_device_halftone itself.) */
  154. void gx_ht_order_release(P3(gx_ht_order * porder, gs_memory_t * mem, bool free_cache));
  155. /*
  156. * Install a device halftone in an imager state. Note that this does not
  157. * read or update the client halftone. There is a special check for pdht ==
  158. * pis->dev_ht, for the benefit of the band rendering code.
  159. */
  160. int gx_imager_dev_ht_install(P4(gs_imager_state * pis,
  161. const gx_device_halftone * pdht,
  162. gs_halftone_type type,
  163. const gx_device * dev));
  164. /*
  165. * Install a new halftone in the graphics state. Note that we copy the top
  166. * level of the gs_halftone and the gx_device_halftone, and take ownership
  167. * of any substructures.
  168. */
  169. int gx_ht_install(P3(gs_state *, const gs_halftone *,
  170. const gx_device_halftone *));
  171. /* Reestablish the effective transfer functions, taking into account */
  172. /* any overrides from halftone dictionaries. */
  173. /* Some compilers object to names longer than 31 characters.... */
  174. void gx_imager_set_effective_xfer(P1(gs_imager_state * pis));
  175. void gx_set_effective_transfer(P1(gs_state * pgs));
  176. #endif /* gzht_INCLUDED */