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