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