xref: /plan9/sys/src/cmd/gs/src/gzht.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gzht.h,v 1.13 2004/08/04 19:36:12 stefan Exp $ */
18 /* Internal procedures for halftones */
19 /* Requires gxdevice.h, gxdcolor.h */
20 
21 #ifndef gzht_INCLUDED
22 #  define gzht_INCLUDED
23 
24 #include "gscsel.h"
25 #include "gxht.h"
26 #include "gxfmap.h"
27 #include "gxdht.h"
28 #include "gxhttile.h"
29 
30 /* Sort a sampled halftone order by sample value. */
31 void gx_sort_ht_order(gx_ht_bit *, uint);
32 
33 /* (Internal) procedures for constructing halftone orders. */
34 int gx_ht_alloc_ht_order(gx_ht_order * porder, uint width, uint height,
35 			 uint num_levels, uint num_bits, uint strip_shift,
36 			 const gx_ht_order_procs_t *procs,
37 			 gs_memory_t * mem);
38 int gx_ht_alloc_order(gx_ht_order * porder, uint width, uint height,
39 		      uint strip_shift, uint num_levels, gs_memory_t *mem);
40 int gx_ht_alloc_threshold_order(gx_ht_order * porder, uint width,
41 				uint height, uint num_levels,
42 				gs_memory_t * mem);
43 int gx_ht_alloc_client_order(gx_ht_order * porder, uint width, uint height,
44 			     uint num_levels, uint num_bits, gs_memory_t * mem);
45 void gx_ht_construct_spot_order(gx_ht_order *);
46 int gx_ht_construct_threshold_order(gx_ht_order *, const byte *);
47 void gx_ht_construct_bit(gx_ht_bit * bit, int width, int bit_num);
48 void gx_ht_construct_bits(gx_ht_order *);
49 
50 /* Halftone enumeration structure */
51 struct gs_screen_enum_s {
52     gs_halftone halftone;	/* supplied by client */
53     gx_ht_order order;
54     gs_matrix mat;		/* for mapping device x,y to rotated cell */
55     gs_matrix mat_inv;		/* the inversion of mat */
56     int x, y;
57     int strip, shift;
58     gs_state *pgs;
59 };
60 
61 #define private_st_gs_screen_enum() /* in gshtscr.c */\
62   gs_private_st_composite(st_gs_screen_enum, gs_screen_enum,\
63     "gs_screen_enum", screen_enum_enum_ptrs, screen_enum_reloc_ptrs)
64 /*    order.levels, order.bits, pgs) */
65 
66 /* Prepare a device halftone for installation, but don't install it. */
67 int gs_sethalftone_prepare(gs_state *, gs_halftone *,
68 			   gx_device_halftone *);
69 
70 /* Allocate and initialize a spot screen. */
71 /* This is the first half of gs_screen_init_accurate/memory. */
72 int gs_screen_order_alloc(gx_ht_order *, gs_memory_t *);
73 int gs_screen_order_init_memory(gx_ht_order *, const gs_state *,
74 				gs_screen_halftone *, bool, gs_memory_t *);
75 
76 #define gs_screen_order_init(porder, pgs, phsp, accurate)\
77   gs_screen_order_init_memory(porder, pgs, phsp, accurate, pgs->memory)
78 
79 /* Prepare to sample a spot screen. */
80 /* This is the second half of gs_screen_init_accurate/memory. */
81 int gs_screen_enum_init_memory(gs_screen_enum *, const gx_ht_order *,
82 			       gs_state *, const gs_screen_halftone *,
83 			       gs_memory_t *);
84 
85 #define gs_screen_enum_init(penum, porder, pgs, phsp)\
86   gs_screen_enum_init_memory(penum, porder, pgs, phsp, pgs->memory)
87 
88 /* Process an entire screen plane. */
89 int gx_ht_process_screen_memory(gs_screen_enum * penum, gs_state * pgs,
90 				gs_screen_halftone * phsp, bool accurate,
91 				gs_memory_t * mem);
92 
93 #define gx_ht_process_screen(penum, pgs, phsp, accurate)\
94   gx_ht_process_screen_memory(penum, pgs, phsp, accurate, pgs->memory)
95 
96 /*
97  * We don't want to remember all the values of the halftone screen,
98  * because they would take up space proportional to P^3, where P is
99  * the number of pixels in a cell.  Instead, we pick some number N of
100  * patterns to cache.  Each cache slot covers a range of (P+1)/N
101  * different gray levels: we "slide" the contents of the slot back and
102  * forth within this range by incrementally adding and dropping 1-bits.
103  * N>=0 (obviously); N<=P+1 (likewise); also, so that we can simplify things
104  * by preallocating the bookkeeping information for the cache, we define
105  * a constant max_cached_tiles which is an a priori maximum value for N.
106  *
107  * Note that the raster for each tile must be a multiple of bitmap_align_mod,
108  * to satisfy the copy_mono device routine, even though a multiple of
109  * sizeof(ht_mask_t) would otherwise be sufficient.
110  */
111 
112 struct gx_ht_cache_s {
113     /* The following are set when the cache is created. */
114     byte *bits;			/* the base of the bits */
115     uint bits_size;		/* the space available for bits */
116     gx_ht_tile *ht_tiles;	/* the base of the tiles */
117     uint num_tiles;		/* the number of tiles allocated */
118     /* The following are reset each time the cache is initialized */
119     /* for a new screen. */
120     gx_ht_order order;		/* the cached order vector */
121     int num_cached;		/* actual # of cached tiles */
122     int levels_per_tile;	/* # of levels per cached tile */
123     int tiles_fit;		/* -1 if not determined, 0 if no fit, */
124 				/* 1 if fit */
125     gx_bitmap_id base_id;	/* the base id, to which */
126 				/* we add the halftone level */
127     gx_ht_tile *(*render_ht)(gx_ht_cache *, int); /* rendering procedure */
128 };
129 
130 /* Define the sizes of the halftone cache. */
131 #define max_cached_tiles_HUGE 5000	/* not used */
132 #define max_ht_bits_HUGE 1000000	/* not used */
133 #define max_cached_tiles_LARGE 577
134 #define max_ht_bits_LARGE 100000
135 #define max_cached_tiles_SMALL 25
136 #define max_ht_bits_SMALL 1000
137 
138 /* Define the size of the halftone tile cache. */
139 #define max_tile_bytes_LARGE 4096
140 #define max_tile_bytes_SMALL 512
141 #if arch_small_memory
142 #  define max_tile_cache_bytes max_tile_bytes_SMALL
143 #else
144 #  define max_tile_cache_bytes\
145      (gs_debug_c('.') ? max_tile_bytes_SMALL : max_tile_bytes_LARGE)
146 #endif
147 
148 /* We don't mark from the tiles pointer, and we relocate the tiles en masse. */
149 #define private_st_ht_tiles()	/* in gxht.c */\
150   gs_private_st_composite(st_ht_tiles, gx_ht_tile, "ht tiles",\
151     ht_tiles_enum_ptrs, ht_tiles_reloc_ptrs)
152 #define private_st_ht_cache()	/* in gxht.c */\
153   gs_private_st_ptrs_add2(st_ht_cache, gx_ht_cache, "ht cache",\
154     ht_cache_enum_ptrs, ht_cache_reloc_ptrs,\
155     st_ht_order, order, bits, ht_tiles)
156 
157 /* Compute a fractional color for dithering, the correctly rounded */
158 /* quotient f * max_gx_color_value / maxv. */
159 #define frac_color_(f, maxv)\
160   (gx_color_value)(((f) * (0xffffL * 2) + maxv) / (maxv * 2))
161 extern const gx_color_value *const fc_color_quo[8];
162 
163 #define fractional_color(f, maxv)\
164   ((maxv) <= 7 ? fc_color_quo[maxv][f] : frac_color_(f, maxv))
165 
166 /* ------ Halftone cache procedures ------ */
167 
168 /* Allocate/free a halftone cache. */
169 uint gx_ht_cache_default_tiles(void);
170 uint gx_ht_cache_default_bits(void);
171 gx_ht_cache *gx_ht_alloc_cache(gs_memory_t *, uint, uint);
172 void gx_ht_free_cache(gs_memory_t *, gx_ht_cache *);
173 
174 /* Clear a halftone cache. */
175 #define gx_ht_clear_cache(pcache)\
176   ((pcache)->order.levels = 0, (pcache)->order.bit_data = 0,\
177    (pcache)->ht_tiles[0].tiles.data = 0)
178 
179 /* Initialize a halftone cache with a given order. */
180 void gx_ht_init_cache(const gs_memory_t *mem, gx_ht_cache *, const gx_ht_order *);
181 
182 /* Check whether the tile cache corresponds to the current order */
183 bool gx_check_tile_cache_current(const gs_imager_state * pis);
184 
185 /* Make the cache order current, and return whether */
186 /* there is room for all possible tiles in the cache. */
187 bool gx_check_tile_cache(const gs_imager_state *);
188 
189 /* Determine whether a given (width, y, height) might fit into a */
190 /* single tile. If so, return the byte offset of the appropriate row */
191 /* from the beginning of the tile, and set *ppx to the x phase offset */
192 /* within the tile; if not, return -1. */
193 int gx_check_tile_size(const gs_imager_state * pis, int w, int y, int h,
194 		       gs_color_select_t select, int *ppx);
195 
196 /* Make a given level current in a halftone cache. */
197 #define gx_render_ht(pcache, b_level)\
198   ((pcache)->render_ht(pcache, b_level))
199 
200 /* ------ Device halftone management ------ */
201 
202 /* Release a gx_ht_order by freeing its components. */
203 /* (Don't free the gx_device_halftone itself.) */
204 void gx_ht_order_release(gx_ht_order * porder, gs_memory_t * mem, bool free_cache);
205 
206 /*
207  * Install a device halftone in an imager state.  Note that this does not
208  * read or update the client halftone.
209  */
210 int gx_imager_dev_ht_install(gs_imager_state * pis,
211 			     gx_device_halftone * pdht,
212 			     gs_halftone_type type,
213 			     const gx_device * dev);
214 
215 /*
216  * Install a new halftone in the graphics state.  Note that we copy the top
217  * level of the gs_halftone and the gx_device_halftone, and take ownership
218  * of any substructures.
219  */
220 int gx_ht_install(gs_state *, const gs_halftone *, gx_device_halftone *);
221 
222 /* Reestablish the effective transfer functions, taking into account */
223 /* any overrides from halftone dictionaries. */
224 /* Some compilers object to names longer than 31 characters.... */
225 void gx_imager_set_effective_xfer(gs_imager_state * pis);
226 void gx_set_effective_transfer(gs_state * pgs);
227 
228 /*
229  * This routine will take a color name (defined by a ptr and size) and
230  * check if this is a valid colorant name for the current device.  If
231  * so then the device's colorant number is returned.
232  *
233  * Two other checks are also made.  If the name is "Default" then a value
234  * of GX_DEVICE_COLOR_MAX_COMPONENTS is returned.  This is done to
235  * simplify the handling of default halftones.
236  *
237  * If the halftone type is colorscreen or multiple colorscreen, then we
238  * also check for Red/Cyan, Green/Magenta, Blue/Yellow, and Gray/Black
239  * component name pairs.  This is done since the setcolorscreen and
240  * sethalftone types 2 and 4 imply the dual name sets.
241  *
242  * A negative value is returned if the color name is not found.
243  */
244 int gs_color_name_component_number(gx_device * dev, const char * pname,
245 				int name_size, int halftonetype);
246 /*
247  * See gs_color_name_component_number for main description.
248  *
249  * This version converts a name index value into a string and size and
250  * then call gs_color_name_component_number.
251  */
252 int gs_cname_to_colorant_number(gs_state * pgs, byte * pname, uint name_size,
253 				 int halftonetype);
254 #endif /* gzht_INCLUDED */
255