xref: /plan9/sys/src/cmd/gs/src/gxdcolor.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1993, 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: gxdcolor.h,v 1.9 2002/08/29 00:11:30 dan Exp $ */
18 /* Device color representation for Ghostscript */
19 
20 #ifndef gxdcolor_INCLUDED
21 #  define gxdcolor_INCLUDED
22 
23 #include "gscsel.h"
24 #include "gsdcolor.h"
25 #include "gsropt.h"
26 #include "gsstruct.h"		/* for extern_st, GC procs */
27 
28 /* Define opaque types. */
29 
30 #ifndef gx_device_DEFINED
31 #  define gx_device_DEFINED
32 typedef struct gx_device_s gx_device;
33 
34 #endif
35 
36 /*
37  * Define a source structure for RasterOp.
38  */
39 typedef struct gx_rop_source_s {
40     const byte *sdata;
41     int sourcex;
42     uint sraster;
43     gx_bitmap_id id;
44     gx_color_index scolors[2];
45     bool use_scolors;
46 } gx_rop_source_t;
47 
48 /*
49  * Note that the following definition depends on the gx_color_index for
50  * black, which may not be 0.  Clients must check this and construct
51  * a different null source if necessary.
52  */
53 #define gx_rop_no_source_body(black_pixel)\
54   NULL, 0, 0, gx_no_bitmap_id, {black_pixel, black_pixel}, true
55 #define gx_rop_source_set_color(prs, pixel)\
56   ((prs)->scolors[0] = (prs)->scolors[1] = (pixel))
57 void gx_set_rop_no_source(const gx_rop_source_t **psource,
58 			  gx_rop_source_t *pno_source, gx_device *dev);
59 #define set_rop_no_source(source, no_source, dev)\
60   gx_set_rop_no_source(&(source), &(no_source), dev)
61 
62 /*
63  * Define the device color structure per se.
64  */
65 
66 /* The typedef is in gsdcolor.h. */
67 /*typedef struct gx_device_color_type_s gx_device_color_type_t; */
68 struct gx_device_color_type_s {
69 
70     /*
71      * In order to simplify memory management, we use a union, but since
72      * different variants may have different pointer tracing procedures,
73      * we have to define a separate GC structure type for each variant.
74      */
75 
76     gs_memory_type_ptr_t stype;
77 
78     /*
79      * Accessors.
80      *
81      * The "save_dc" method fills in a gx_device_color_saved structure
82      * for the operand device color. This is may be used with the
83      * "write" and "read" methods (see below) to minimize command list
84      * size.
85      *
86      * The "get_dev_halftone" method returns a pointer to the device
87      * halftone used by the current color, or NULL if there is no such
88      * halftone (i.e.: the device color is a pure color).
89      *
90      * The "get_phase" returns true if the device color contains phase
91      * information, and sets *pphase to the appropriate value. Halftones
92      * that do not use the color information return false.
93      */
94 #define dev_color_proc_save_dc(proc)\
95   void proc(const gx_device_color * pdevc, gx_device_color_saved * psdc)
96 			 dev_color_proc_save_dc((*save_dc));
97 
98 #define dev_color_proc_get_dev_halftone(proc)\
99   const gx_device_halftone * proc(const gx_device_color * pdevc)
100 			 dev_color_proc_get_dev_halftone((*get_dev_halftone));
101 
102 #define dev_color_proc_get_phase(proc)\
103   bool proc(const gx_device_color * pdevc, gs_int_point * pphase)
104 			dev_color_proc_get_phase((*get_phase));
105 
106     /*
107      * If necessary and possible, load the halftone or Pattern cache
108      * with the rendering of this color.
109      */
110 
111 #define dev_color_proc_load(proc)\
112   int proc(gx_device_color *pdevc, const gs_imager_state *pis,\
113     gx_device *dev, gs_color_select_t select)
114                          dev_color_proc_load((*load));
115 
116     /*
117      * Fill a rectangle with the color.
118      * We pass the device separately so that pattern fills can
119      * substitute a tiled mask clipping device.
120      */
121 
122 #define dev_color_proc_fill_rectangle(proc)\
123   int proc(const gx_device_color *pdevc, int x, int y, int w, int h,\
124     gx_device *dev, gs_logical_operation_t lop, const gx_rop_source_t *source)
125                          dev_color_proc_fill_rectangle((*fill_rectangle));
126 
127     /*
128      * Fill a masked region with a color.  Nearly all device colors
129      * use the default implementation, which simply parses the mask
130      * into rectangles and calls fill_rectangle.  Note that in this
131      * case there is no RasterOp source: the mask is the source.
132      */
133 
134 #define dev_color_proc_fill_masked(proc)\
135   int proc(const gx_device_color *pdevc, const byte *data, int data_x,\
136     int raster, gx_bitmap_id id, int x, int y, int w, int h,\
137     gx_device *dev, gs_logical_operation_t lop, bool invert)
138                          dev_color_proc_fill_masked((*fill_masked));
139 
140     /*
141      * Test whether this color is equal to another.
142      */
143 
144 #define dev_color_proc_equal(proc)\
145   bool proc(const gx_device_color *pdevc1, const gx_device_color *pdevc2)
146                          dev_color_proc_equal((*equal));
147 
148     /*
149      * Serialize and deserialize a device color.
150      *
151      * The "write" routine converts a device color into a string for
152      * writing to the command list. *psize is the amount of space
153      * available. If the saved color and the current color are the same,
154      * the routine sets *psize to 0 and returns 1. Otherwise, if *psize
155      * is large enough, the procedure sets *psize to the amount actually
156      * used and returns 0. If *psize is too small and no other problem
157      * is detected, *psize is set to the amount required and
158      * gs_error_rangecheck is returned. If some other error is detected,
159      * *psize is left unchanged and the error code is returned.
160      *
161      * The "read" routine converts the string representation back into
162      * the full device color structure. The value returned is the number
163      * of bytes actually read, or < 0 in the event of an error.
164      *
165      * As with any instance of virtual serialization, the command list
166      * code must include its own identifier of the color space type in
167      * the command list, so as to know which read routine to call. The
168      * procedures gx_dc_get_type_code and gx_dc_get_type_from_code are
169      * provided to support this operation.
170      *
171      * For the write operation, psdc points to the saved version of the
172      * color previously stored for a particular band. When the band is
173      * rendered this will be the current device color just before the
174      * color being serialized is read. This information can be used to
175      * make encoding more efficient, and to discard unnecessary color
176      * setting operations. To avoid any optimization, set psdc to be a
177      * null pointer.
178      *
179      * Note that the caller is always responsible for serializing and
180      * transmitting the device halftone, if this is required. Because
181      * device halftones change infrequently, they are transmitted as
182      * "all bands" commands. This is only possible if they are serialized
183      * separately, which is why they cannot be handled by these methods.
184      *
185      * The first device color serialized after the halftone has been
186      * changed should always contain complete information; i.e.: psdc
187      * should be set to a null pointer. This is necessary as the command
188      * list reader may have reset its device color when the halftone is
189      * changed, so informaition from the prior device color will no
190      * longer be available.
191      *
192      * For the read and method, the imager state is passed as an operand,
193      * which allows the routine to access the current device halftone
194      * (always required). Also passed in a pointer to the existing device
195      * color, as this is not part of the imager state. If the writer was
196      * passed a non-null psdc operand, *prior_devc must reflect the
197      * information contained in *psdc.
198      *
199      * NB: For the read method, pdevc and prior_devc may and usually
200      *     will be the same. Code implementing this method must be able
201      *     to handle this situation.
202      *
203      * The device is provided as an operand for both routines to pass
204      * color model information. This allows more compact encoding of
205      * various pieces of information, in particular color indices.
206      */
207 #define dev_color_proc_write(proc)\
208   int proc(const gx_device_color *pdevc, const gx_device_color_saved *psdc,\
209     const gx_device * dev, byte *data, uint *psize)
210 			dev_color_proc_write((*write));
211 
212 #define dev_color_proc_read(proc)\
213   int proc(gx_device_color *pdevc, const gs_imager_state * pis,\
214     const gx_device_color *prior_devc, const gx_device * dev,\
215     const byte *data, uint size, gs_memory_t *mem)
216 			dev_color_proc_read((*read));
217 
218     /*
219      * Identify which color model components have non-zero intensities in
220      * a device color. If this is the case, set the (1 << i)'th bit of
221      * *pcomp_bits to 1; otherwise set it to 0. This method is used to
222      * support PDF's overprint mode. The *pcomp_bits value is known to be
223      * large enough for the number of device color components, and should
224      * be initialized to 0 by the client.
225      *
226      * Returns 0 except for shading and/or color tiling patterns, for
227      * which  1 is returned. For those two "colors", lower level device
228      * colors must be examined to determine the desired information. This
229      * is not a problem for shading colors, as overprint mode does not
230      * apply to them. It is potentially a problem for colored tiling
231      * patterns, but the situations in which it is a problem other, long-
232      * standing implementation difficulties for patterns would also be a
233      * problem.
234      *
235      * Returns of < 0 indicate an error, and shouldn't be possible.
236      */
237 #define dev_color_proc_get_nonzero_comps(proc)\
238   int proc(const gx_device_color * pdevc, const gx_device * dev,\
239     gx_color_index * pcomp_bits)
240                          dev_color_proc_get_nonzero_comps((*get_nonzero_comps));
241 };
242 
243 /* Define the default implementation of fill_masked. */
244 dev_color_proc_fill_masked(gx_dc_default_fill_masked);
245 
246 extern_st(st_device_color);
247 /* public_st_device_color() is defined in gsdcolor.h */
248 
249 /* Define the standard device color types. */
250 /* See gsdcolor.h for details. */
251 extern const gx_device_color_type_t
252 #define gx_dc_type_none (&gx_dc_type_data_none)
253       gx_dc_type_data_none,	/* gxdcolor.c */
254 #define gx_dc_type_null (&gx_dc_type_data_null)
255       gx_dc_type_data_null,	/* gxdcolor.c */
256 #define gx_dc_type_pure (&gx_dc_type_data_pure)
257       gx_dc_type_data_pure,	/* gxdcolor.c */
258 /*#define gx_dc_type_pattern (&gx_dc_type_data_pattern) */
259 						/*gx_dc_type_data_pattern, *//* gspcolor.c */
260 #define gx_dc_type_ht_binary (&gx_dc_type_data_ht_binary)
261       gx_dc_type_data_ht_binary,	/* gxht.c */
262 #define gx_dc_type_ht_colored (&gx_dc_type_data_ht_colored)
263       gx_dc_type_data_ht_colored,	/* gxcht.c */
264 #define gx_dc_type_wts (&gx_dc_type_data_wts)
265       gx_dc_type_data_wts;	/* gxwts.c */
266 
267 /* the following are exported for the benefit of gsptype1.c */
268 extern  dev_color_proc_get_nonzero_comps(gx_dc_pure_get_nonzero_comps);
269 extern  dev_color_proc_get_nonzero_comps(gx_dc_ht_binary_get_nonzero_comps);
270 extern  dev_color_proc_get_nonzero_comps(gx_dc_ht_colored_get_nonzero_comps);
271 
272 /* convert between color types and color type indices */
273 extern int gx_get_dc_type_index(const gx_device_color *);
274 extern const gx_device_color_type_t * gx_get_dc_type_from_index(int);
275 
276 /* the two canonical "get_phase" methods */
277 extern  dev_color_proc_get_phase(gx_dc_no_get_phase);
278 extern  dev_color_proc_get_phase(gx_dc_ht_get_phase);
279 
280 
281 #define gs_color_writes_pure(pgs)\
282   color_writes_pure((pgs)->dev_color, (pgs)->log_op)
283 
284 /* Set up device color 1 for writing into a mask cache */
285 /* (e.g., the character cache). */
286 void gx_set_device_color_1(gs_state * pgs);
287 
288 /* Remap the color if necessary. */
289 int gx_remap_color(gs_state *);
290 
291 #define gx_set_dev_color(pgs)\
292   if ( !color_is_set((pgs)->dev_color) )\
293    { int code_dc = gx_remap_color(pgs);\
294      if ( code_dc != 0 ) return code_dc;\
295    }
296 
297 /* Indicate that the device color needs remapping. */
298 #define gx_unset_dev_color(pgs)\
299   color_unset((pgs)->dev_color)
300 
301 /* Load the halftone cache in preparation for drawing. */
302 #define gx_color_load_select(pdevc, pis, dev, select)\
303   (*(pdevc)->type->load)(pdevc, pis, dev, select)
304 #define gx_color_load(pdevc, pis, dev)\
305   gx_color_load_select(pdevc, pis, dev, gs_color_select_texture)
306 #define gs_state_color_load(pgs)\
307   gx_color_load((pgs)->dev_color, (const gs_imager_state *)(pgs),\
308 		(pgs)->device)
309 
310 /* Fill a rectangle with a color. */
311 #define gx_device_color_fill_rectangle(pdevc, x, y, w, h, dev, lop, source)\
312   (*(pdevc)->type->fill_rectangle)(pdevc, x, y, w, h, dev, lop, source)
313 #define gx_fill_rectangle_device_rop(x, y, w, h, pdevc, dev, lop)\
314   gx_device_color_fill_rectangle(pdevc, x, y, w, h, dev, lop, NULL)
315 #define gx_fill_rectangle_rop(x, y, w, h, pdevc, lop, pgs)\
316   gx_fill_rectangle_device_rop(x, y, w, h, pdevc, (pgs)->device, lop)
317 #define gx_fill_rectangle(x, y, w, h, pdevc, pgs)\
318   gx_fill_rectangle_rop(x, y, w, h, pdevc, (pgs)->log_op, pgs)
319 
320 /*
321  * Utilities to write/read color indices. Currently, a very simple mechanism
322  * is used, much simpler than that used by other command-list writers. This
323  * should be sufficient for most situations.
324  *
325  * The operand set and return values are those of the device color write/read
326  * routines.
327  */
328 extern  int     gx_dc_write_color( gx_color_index       color,
329                                    const gx_device *    dev,
330                                    byte *               pdata,
331                                    uint *               psize );
332 
333 extern  int     gx_dc_read_color( gx_color_index *  pcolor,
334                                   const gx_device * dev,
335                                   const byte *      pdata,
336                                   int               size );
337 
338 #endif /* gxdcolor_INCLUDED */
339