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