gxdht.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Copyright (C) 1995, 1996, 1997, 1999, 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gxdht.h,v 1.9 2005/03/14 18:08:37 dan Exp $ */
  14. /* Definition of device halftones */
  15. #ifndef gxdht_INCLUDED
  16. # define gxdht_INCLUDED
  17. #include "gsrefct.h"
  18. #include "gsmatrix.h"
  19. #include "gxarith.h" /* for igcd */
  20. #include "gxhttype.h"
  21. #include "gscspace.h"
  22. #include "gxcindex.h"
  23. #include "gxfrac.h"
  24. /*
  25. * We represent a halftone tile as a rectangular super-cell consisting of
  26. * multiple copies of a multi-cell whose corners lie on integral
  27. * coordinates, which in turn is a parallelogram (normally square) array of
  28. * basic parallelogram (normally square) cells whose corners lie on rational
  29. * coordinates.
  30. *
  31. * Let T be the aspect ratio (ratio of physical pixel height to physical
  32. * pixel width), which is abs(xx/yy) for portrait devices and abs(yx/xy) for
  33. * landscape devices. We characterize the basic cell by four rational
  34. * numbers U(') = M(')/R(') and V(') = N(')/R(') where R(') is positive, at
  35. * least one of U and V (and the corresponding one of U' and V') is
  36. * non-zero, and U' is approximately U/T and V' is approximately V*T; these
  37. * numbers define the vertices of the basic cell at device space coordinates
  38. * (0,0), (U,V), (U-V',U'+V), and (-V',U'); then the multi-cell is defined
  39. * similarly by M(') and N('). From these definitions, the basic cell has
  40. * an area of B = U*U' + V*V' = (M*M' + N*N') / R*R' pixels, and the
  41. * multi-cell has an area of C = B * R*R' = M*M' + N*N' pixels.
  42. *
  43. * If the coefficients of the default device transformation matrix are xx,
  44. * xy, yx, and yy, then U and V are related to the frequency F and the angle
  45. * A by:
  46. * P = 72 / F;
  47. * U = P * (xx * cos(A) + yx * sin(A));
  48. * V = P * (xy * cos(A) + yy * sin(A)).
  49. *
  50. * We can tile the plane with any rectangular super-cell that consists of
  51. * repetitions of the multi-cell and whose corners coincide with multi-cell
  52. * coordinates (0,0). We observe that for any integers i, j such that i*N -
  53. * j*M' = 0, a multi-cell corner lies on the X axis at W = i*M + j*N';
  54. * similarly, if i'*M - j'*N' = 0, a corner lies on the Y axis at W' = i'*N
  55. * + j'*M'. Then the super-cell occupies Z = W * W' pixels, consisting of Z
  56. * / C multi-cells or Z / B basic cells. The trick in all this is to find
  57. * values of F and A that aren't too far from the requested ones, and that
  58. * yield a manageably small value for Z.
  59. *
  60. * Note that the super-cell only has to be so large because we want to use
  61. * it directly to tile the plane. In fact, we can decompose it into W' / D
  62. * horizontal strips of width W and height D, shifted horizontally with
  63. * respect to each other by S pixels, where we compute S by finding h and k
  64. * such that h*N - k*M' = D and then S = h*M + k*N'. The halftone setup
  65. * routines only generate a single strip of samples, and let
  66. * gx_ht_construct_spot_order construct the rest. If W' is large, we
  67. * actually keep only one strip, and let the strip_tile_rectangle routines
  68. * do the shifting at rendering time.
  69. */
  70. typedef struct gx_ht_cell_params_s {
  71. /* Defining values. M * M1 != 0 or N * N1 != 0; R > 0, R1 > 0. */
  72. /* R and D are short rather than ushort so that we don't get */
  73. /* unsigned results from arithmetic. */
  74. short M, N, R;
  75. short M1, N1, R1;
  76. /* Derived values. */
  77. ulong C;
  78. short D, D1;
  79. uint W, W1;
  80. int S;
  81. } gx_ht_cell_params_t;
  82. /* Compute the derived values from the defining values. */
  83. void gx_compute_cell_values(gx_ht_cell_params_t *);
  84. /*
  85. * The whitening order is represented by a pair of arrays.
  86. * The levels array contains an integer (an index into the bit_data array)
  87. * for each distinct halftone level, indicating how many pixels should be
  88. * whitened for that level; levels[0] = 0, levels[i] <= levels[i+1], and
  89. * levels[num_levels-1] <= num_bits. The bit_data array contains data to
  90. * specify which bits should be set for each level: it has several
  91. * different representations depending on space/time tradeoffs.
  92. *
  93. * The default bit_data representation is an (offset,mask) pair for each
  94. * pixel in the tile. bits[i].offset is the (properly aligned) byte index
  95. * of a pixel in the tile; bits[i].mask is the mask to be or'ed into this
  96. * byte and following ones. This is arranged so it will work properly on
  97. * either big- or little-endian machines, and with different mask widths.
  98. */
  99. /*
  100. * The mask width must be at least as wide as uint,
  101. * and must not be wider than the width implied by align_bitmap_mod.
  102. */
  103. typedef uint ht_mask_t;
  104. #define ht_mask_bits (sizeof(ht_mask_t) * 8)
  105. typedef struct gx_ht_bit_s {
  106. uint offset;
  107. ht_mask_t mask;
  108. } gx_ht_bit;
  109. /* During sampling, bits[i].mask is used to hold a normalized sample value. */
  110. typedef ht_mask_t ht_sample_t;
  111. /* The following awkward expression avoids integer overflow. */
  112. #define max_ht_sample (ht_sample_t)(((1 << (ht_mask_bits - 2)) - 1) * 2 + 1)
  113. #ifndef wts_screen_t_DEFINED
  114. # define wts_screen_t_DEFINED
  115. typedef struct wts_screen_s wts_screen_t;
  116. #endif
  117. #ifndef gs_wts_screen_enum_t_DEFINED
  118. # define gs_wts_screen_enum_t_DEFINED
  119. typedef struct gs_wts_screen_enum_s gs_wts_screen_enum_t;
  120. #endif
  121. /*
  122. * Define the internal representation of a halftone order.
  123. * Note that it may include a cached transfer function.
  124. *
  125. * Halftone orders exist in two slightly different configurations, strip and
  126. * complete. In a complete order, shift = 0 and full_height = height; in a
  127. * strip order, shift != 0 and full_height is the height of a fully expanded
  128. * halftone made up of enough shifted strip copies to get back to a zero
  129. * shift. In other words, full_height is a cached value, but it is an
  130. * important one, since it is the modulus used for computing the
  131. * tile-relative phase. Requirements:
  132. * width > 0, height > 0, multiple > 0
  133. * raster >= bitmap_raster(width)
  134. * 0 <= shift < width
  135. * num_bits = width * height
  136. * For complete orders:
  137. * full_height = height
  138. * For strip orders:
  139. * full_height = height * width / gcd(width, shift)
  140. * Note that during the sampling of a complete spot halftone, these
  141. * invariants may be violated; in particular, it is possible that shift != 0
  142. * and height < full_height, even though num_bits and num_levels reflect the
  143. * full height. In this case, the invariant is restored (by resetting
  144. * shift and height) when sampling is finished. However, we must save the
  145. * original height and shift values used for sampling, since sometimes we
  146. * run the "finishing" routines more than once. (This is ugly, but it's
  147. * too hard to fix.)
  148. *
  149. * See gxbitmap.h for more details about strip halftones.
  150. */
  151. typedef struct gx_ht_cache_s gx_ht_cache;
  152. #ifndef gx_ht_order_DEFINED
  153. # define gx_ht_order_DEFINED
  154. typedef struct gx_ht_order_s gx_ht_order;
  155. #endif
  156. #ifndef gx_ht_tile_DEFINED
  157. # define gx_ht_tile_DEFINED
  158. typedef struct gx_ht_tile_s gx_ht_tile;
  159. #endif
  160. typedef struct gx_ht_order_procs_s {
  161. /* Define the size of each element of bit_data. */
  162. uint bit_data_elt_size;
  163. /* Construct the order from the threshold array. */
  164. /* Note that for 16-bit threshold values, */
  165. /* each value is 2 bytes in big-endian order (Adobe spec). */
  166. int (*construct_order)(gx_ht_order *order, const byte *thresholds);
  167. /* Return the (x,y) coordinate of an element of bit_data. */
  168. int (*bit_index)(const gx_ht_order *order, uint index,
  169. gs_int_point *ppt);
  170. /* Update a halftone cache tile to match this order. */
  171. int (*render)(gx_ht_tile *tile, int new_bit_level,
  172. const gx_ht_order *order);
  173. /* Draw a halftone shade into a 1 bit deep buffer. */
  174. /* Note: this is a tentative design for a new method. I may not
  175. keep it. */
  176. int (*draw)(gx_ht_order *order, frac shade,
  177. byte *data, int data_raster,
  178. int x, int y, int w, int h);
  179. } gx_ht_order_procs_t;
  180. /*
  181. * Define the procedure vectors for the supported implementations
  182. * (in gxhtbit.c).
  183. */
  184. extern const gx_ht_order_procs_t ht_order_procs_table[2];
  185. #define ht_order_procs_default ht_order_procs_table[0] /* bit_data is gx_ht_bit[] */
  186. #define ht_order_procs_short ht_order_procs_table[1] /* bit_data is ushort[] */
  187. /* For screen/spot halftones, we must record additional parameters. */
  188. typedef struct gx_ht_order_screen_params_s {
  189. gs_matrix matrix; /* CTM when the function was sampled */
  190. ulong max_size; /* max bitmap size */
  191. } gx_ht_order_screen_params_t;
  192. struct gx_ht_order_s {
  193. gx_ht_cell_params_t params; /* parameters defining the cells */
  194. gs_wts_screen_enum_t *wse;
  195. wts_screen_t *wts; /* if non-NULL, then rest of the structure is irrelevant */
  196. ushort width;
  197. ushort height;
  198. ushort raster;
  199. ushort shift;
  200. ushort orig_height;
  201. ushort orig_shift;
  202. uint full_height;
  203. uint num_levels; /* = levels size */
  204. uint num_bits; /* = countof(bit_data) = width * height */
  205. const gx_ht_order_procs_t *procs;
  206. gs_memory_t *data_memory; /* for levels and bit_data, may be 0 */
  207. uint *levels;
  208. void *bit_data;
  209. gx_ht_cache *cache; /* cache to use */
  210. gx_transfer_map *transfer; /* TransferFunction or 0 */
  211. gx_ht_order_screen_params_t screen_params;
  212. };
  213. #define ht_order_is_complete(porder)\
  214. ((porder)->shift == 0)
  215. #define ht_order_full_height(porder)\
  216. ((porder)->shift == 0 ? (porder)->height :\
  217. (porder)->width / igcd((porder)->width, (porder)->shift) *\
  218. (porder)->height)
  219. /* We only export st_ht_order for use in st_screen_enum. */
  220. extern_st(st_ht_order);
  221. #define public_st_ht_order() /* in gsht.c */\
  222. gs_public_st_composite(st_ht_order, gx_ht_order, "gx_ht_order",\
  223. ht_order_enum_ptrs, ht_order_reloc_ptrs)
  224. #define st_ht_order_max_ptrs 4
  225. /*
  226. * Define a device halftone. This consists of one or more orders.
  227. * If components = 0, then order is the only current halftone screen
  228. * (set by setscreen, Type 1 sethalftone, Type 3 sethalftone, or
  229. * Type 5 sethalftone with only a Default). Otherwise, order is the
  230. * gray or black screen (for gray/RGB or CMYK devices respectively),
  231. * and components is an array of gx_ht_order_components parallel to
  232. * the components of the client halftone (set by setcolorscreen or
  233. * Type 5 sethalftone).
  234. *
  235. * Multi-component halftone orders may be required even in Level 1 systems,
  236. * because they are needed for setcolorscreen.
  237. *
  238. * NOTE: it is assumed that all subsidiary structures of device halftones
  239. * (the components array, and the bit_data, levels, cache, and transfer
  240. * members of any gx_ht_orders, both the default order and any component
  241. * orders) are allocated with the same allocator as the device halftone
  242. * itself.
  243. */
  244. typedef struct gx_ht_order_component_s {
  245. gx_ht_order corder;
  246. int comp_number;
  247. int cname;
  248. } gx_ht_order_component;
  249. #define private_st_ht_order_component() /* in gsht.c */\
  250. gs_private_st_ptrs_add0(st_ht_order_component, gx_ht_order_component,\
  251. "gx_ht_order_component", ht_order_component_enum_ptrs,\
  252. ht_order_component_reloc_ptrs, st_ht_order, corder)
  253. #define st_ht_order_component_max_ptrs st_ht_order_max_ptrs
  254. /* We only export st_ht_order_component_element for use in banding. */
  255. extern_st(st_ht_order_component_element);
  256. #define public_st_ht_order_comp_element() /* in gsht.c */\
  257. gs_public_st_element(st_ht_order_component_element, gx_ht_order_component,\
  258. "gx_ht_order_component[]", ht_order_element_enum_ptrs,\
  259. ht_order_element_reloc_ptrs, st_ht_order_component)
  260. #ifndef gx_device_halftone_DEFINED
  261. # define gx_device_halftone_DEFINED
  262. typedef struct gx_device_halftone_s gx_device_halftone;
  263. #endif
  264. /*
  265. * Device Halftone Structure definition. See comments before
  266. * gx_imager_dev_ht_install() for more information on this structure and its
  267. * fields.
  268. */
  269. struct gx_device_halftone_s {
  270. gx_ht_order order; /* must be first, for subclassing */
  271. rc_header rc;
  272. gs_id id; /* the id changes whenever the data change */
  273. /*
  274. * We have to keep the halftone type so that we can pass it
  275. * through the band list for gx_imager_dev_ht_install.
  276. */
  277. gs_halftone_type type;
  278. gx_ht_order_component *components;
  279. uint num_comp; /* Number of components in the halftone */
  280. uint num_dev_comp; /* Number of process color model components */
  281. /* The following are computed from the above. */
  282. int lcm_width, lcm_height; /* LCM of primary color tile sizes, */
  283. /* max_int if overflowed */
  284. };
  285. extern_st(st_device_halftone);
  286. #define public_st_device_halftone() /* in gsht.c */\
  287. gs_public_st_ptrs_add1(st_device_halftone, gx_device_halftone,\
  288. "gx_device_halftone", device_halftone_enum_ptrs,\
  289. device_halftone_reloc_ptrs, st_ht_order, order, components)
  290. #define st_device_halftone_max_ptrs (st_ht_order_max_ptrs + 1)
  291. /* Complete a halftone order defined by a threshold array. */
  292. void gx_ht_complete_threshold_order(gx_ht_order *porder);
  293. /* Release a gx_device_halftone by freeing its components. */
  294. /* (Don't free the gx_device_halftone itself.) */
  295. void gx_device_halftone_release(gx_device_halftone * pdht, gs_memory_t * mem);
  296. #endif /* gxdht_INCLUDED */