xref: /plan9/sys/src/cmd/gs/src/gsdcolor.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1996, 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: gsdcolor.h,v 1.14 2004/03/16 01:25:19 dan Exp $ */
18 /* Device color representation for drivers */
19 
20 #ifndef gsdcolor_INCLUDED
21 #  define gsdcolor_INCLUDED
22 
23 #include "gsccolor.h"
24 #include "gxarith.h"		/* for imod */
25 #include "gxbitmap.h"
26 #include "gxhttile.h"
27 #include "gxcindex.h"
28 #include "gxwts.h"
29 
30 #ifndef gx_device_color_DEFINED
31 #  define gx_device_color_DEFINED
32 typedef struct gx_device_color_s gx_device_color;
33 #endif
34 
35 #ifndef gx_device_saved_color_DEFINED
36 #  define gx_device_saved_color_DEFINED
37 typedef struct gx_device_color_saved_s  gx_device_color_saved;
38 #endif
39 
40 #ifndef gx_device_halftone_DEFINED
41 #  define gx_device_halftone_DEFINED
42 typedef struct gx_device_halftone_s gx_device_halftone;
43 #endif
44 
45 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
46  * The definitions in the following section of the file are the only
47  * ones that should be used by read-only clients such as implementors
48  * of high-level driver functions.
49  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
50 
51 /*
52  * A device color consists of a base color and an optional (tiled) mask.
53  * The base color may be a pure color, a binary halftone, or a colored
54  * bitmap (color halftone or colored Pattern).  The mask is used for
55  * both colored and uncolored Patterns.
56  */
57 
58 /* Accessing a pure color. */
59 #define gx_dc_is_pure(pdc)\
60   ((pdc)->type == gx_dc_type_pure)
61 #define gx_dc_writes_pure(pdc, lop)\
62   (gx_dc_is_pure(pdc) && lop_no_S_is_T(lop))
63 #define gx_dc_pure_color(pdc)\
64   ((pdc)->colors.pure)
65 
66 /* Accessing the phase of a halftone. */
67 #define gx_dc_phase(pdc)\
68   ((pdc)->phase)
69 
70 /* Accessing a binary halftone. */
71 #define gx_dc_is_binary_halftone(pdc)\
72   ((pdc)->type == gx_dc_type_ht_binary)
73 #define gx_dc_binary_tile(pdc)\
74   (&(pdc)->colors.binary.b_tile->tiles)
75 #define gx_dc_binary_color0(pdc)\
76   ((pdc)->colors.binary.color[0])
77 #define gx_dc_binary_color1(pdc)\
78   ((pdc)->colors.binary.color[1])
79 
80 /* Accessing a colored halftone. */
81 #define gx_dc_is_colored_halftone(pdc)\
82   ((pdc)->type == gx_dc_type_ht_colored)
83 
84 /*
85  * Test device colors for equality.  Testing for equality is done
86  * for determining when cache values, etc. can be used.  Thus these
87  * routines should err toward false responses if there is any question
88  * about the equality of the two device colors.
89  */
90 bool gx_device_color_equal(const gx_device_color *pdevc1,
91 			   const gx_device_color *pdevc2);
92 
93 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
94  * The definitions in the following section of the file, plus the ones
95  * just above, are the only ones that should be used by clients that
96  * set as well as read device colors.
97  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
98 
99 #define color_is_set(pdc)\
100   ((pdc)->type != gx_dc_type_none)
101 #define color_unset(pdc)\
102   ((pdc)->type = gx_dc_type_none)
103 
104 #define gx_dc_is_null(pdc)\
105   ((pdc)->type == gx_dc_type_null)
106 #define color_is_null(pdc) gx_dc_is_null(pdc)
107 #define color_set_null(pdc)\
108   ((pdc)->type = gx_dc_type_null)
109 
110 #define color_is_pure(pdc) gx_dc_is_pure(pdc)
111 #define color_writes_pure(pdc, lop) gx_dc_writes_pure(pdc, lop)
112 /*
113  * Used to define 'pure' (solid - without halftoning or patterns) colors.
114  * This macro assumes the colorspace and client color information is already
115  * defined in the device color strucTure.  If not then see the next macro.
116  */
117 #define color_set_pure(pdc, color)\
118   ((pdc)->colors.pure = (color),\
119    (pdc)->type = gx_dc_type_pure)
120 /*
121  * Used to create special case device colors for which the colorspace
122  * and client colors are not already contained in the device color.
123  */
124 #define set_nonclient_dev_color(pdc, color)\
125     color_set_pure(pdc, color);\
126     (pdc)->ccolor_valid = false
127 
128 /* Set the phase to an offset from the tile origin. */
129 #define color_set_phase(pdc, px, py)\
130   ((pdc)->phase.x = (px),\
131    (pdc)->phase.y = (py))
132 /* Set the phase from the halftone phase in a graphics state. */
133 #define color_set_phase_mod(pdc, px, py, tw, th)\
134   color_set_phase(pdc, imod(-(px), tw), imod(-(py), th))
135 
136 #define color_is_binary_halftone(pdc) gx_dc_is_binary_halftone(pdc)
137 #define color_set_binary_halftone_component(pdc, ht, index, color0, color1, level)\
138   ((pdc)->colors.binary.b_ht = (ht),\
139    (pdc)->colors.binary.b_index = (index),\
140    (pdc)->colors.binary.color[0] = (color0),\
141    (pdc)->colors.binary.color[1] = (color1),\
142    (pdc)->colors.binary.b_level = (level),\
143    (pdc)->type = gx_dc_type_ht_binary)
144 #define color_set_binary_halftone(pdc, ht, color0, color1, level)\
145   color_set_binary_halftone_component(pdc, ht, -1, color0, color1, level)
146 #define color_set_binary_tile(pdc, color0, color1, tile)\
147   ((pdc)->colors.binary.b_ht = 0,\
148    (pdc)->colors.binary.color[0] = (color0),\
149    (pdc)->colors.binary.color[1] = (color1),\
150    (pdc)->colors.binary.b_index = -1, /* irrelevant */\
151    (pdc)->colors.binary.b_tile = (tile),\
152    (pdc)->type = gx_dc_type_ht_binary)
153 
154 #define color_is_colored_halftone(pdc) gx_dc_is_colored_halftone(pdc)
155 #define _color_set_c(pdc, i, b, l)\
156   ((pdc)->colors.colored.c_base[i] = (b),\
157    (pdc)->colors.colored.c_level[i] = (l))
158 
159 /* Some special clients set the individual components separately. */
160 void gx_complete_halftone(gx_device_color *pdevc, int num_comps,
161                           gx_device_halftone *pdht);
162 
163 /* Note that color_set_null_pattern doesn't set mask.ccolor. */
164 #define color_set_null_pattern(pdc)\
165  ((pdc)->mask.id = gx_no_bitmap_id,\
166   (pdc)->mask.m_tile = 0,\
167   (pdc)->colors.pattern.p_tile = 0,\
168   (pdc)->type = gx_dc_type_pattern)
169 
170 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
171  * The remaining definitions are internal ones that are included in this
172  * file only because C's abstraction mechanisms aren't strong enough to
173  * allow us to keep them separate and still have in-line access to the
174  * commonly used members.
175  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
176 
177 /* Define opaque types for objects referenced by device colors. */
178 
179 #ifndef gx_ht_tile_DEFINED
180 #  define gx_ht_tile_DEFINED
181 typedef struct gx_ht_tile_s gx_ht_tile;
182 #endif
183 
184 #ifndef gx_color_tile_DEFINED
185 #  define gx_color_tile_DEFINED
186 typedef struct gx_color_tile_s gx_color_tile;
187 #endif
188 
189 /*
190  * The device color in the graphics state is computed from client color
191  * specifications, and kept current through changes in transfer function,
192  * device, and (if relevant) halftone phase.
193  * (gx_set_dev_color sets the device color if needed.)
194  * For binary halftones (and eventually colored halftones as well),
195  * the bitmaps are only cached, so internal clients (the painting operators)
196  * must call gx_color_load to ensure that the bitmap is available.
197  * Device color elements set by gx_color_load are marked with @ below.
198  *
199  * Base colors are represented as follows:
200  *
201  *      Pure color (gx_dc_pure):
202  *              colors.pure = the color;
203  *      Binary halftone (gx_dc_ht_binary):
204  *              colors.binary.b_ht = the device halftone;
205  *		colors.binary.b_index = -1 if b_ht is the halftone,
206  *		  otherwise an index in b_ht.components
207  *              colors.binary.color[0] = the color for 0s (darker);
208  *              colors.binary.color[1] = the color for 1s (lighter);
209  *              colors.binary.b_level = the number of pixels to lighten,
210  *                0 < halftone_level < P, the number of pixels in the tile;
211  *      @       colors.binary.b_tile points to an entry in the binary
212  *                tile cache.
213  *      Colored halftone (gx_dc_ht_colored):
214  *              colors.colored.c_ht = the device halftone;
215  *              colors.colored.c_level[0..N-1] = the halftone levels,
216  *                like b_level;
217  *              colors.colored.c_base[0..N-1] = the base colors;
218  *                N = the device color_info.num_components
219  *		    (3 for RGB devices, 4 for CMYK devices, ? for DeviceN);
220  *                0 <= c_level[i] < P;
221  *                0 <= c_base[i] <= dither_rgb;
222  *              colors.colored.alpha = the opacity.
223  *		colors.colored.plane_mask: bit 2^i = 1 iff c_level[i] != 0
224  *      Colored PatternType 1 pattern (gx_dc_pattern):
225  *              (mask is also set, see below)
226  *      @       colors.pattern.p_tile points to a gx_color_tile in
227  *                the pattern cache, or is NULL for a null pattern.
228  *
229  * The phase element is used for all colors except pure ones.  It holds the
230  * negative of the graphics state halftone phase, modulo the halftone tile
231  * size.
232  *
233  * The ccolor element is used for all kinds of patterns.  It is needed for
234  * rendering the pattern.
235  *
236  * The mask elements of a device color are only used for PatternType 1
237  * patterns:
238  *      Non-pattern:
239  *              mask is unused.
240  *      Pattern:
241  *              mask.id gives the ID of the pattern (and its mask);
242  *              mask.m_phase holds the negative of the graphics state
243  *                halftone phase;
244  *      @       mask.m_tile points to a gx_color_tile in the pattern cache,
245  *                or is NULL for a pattern that doesn't require a mask.
246  *                (The 'bits' of the tile are not accessed.)
247  *                For colored patterns requiring a mask, p_tile and
248  *                mask.m_tile point to the same cache entry.
249  * For masked colors, gx_set_dev_color replaces the type with a different
250  * type that applies the mask when painting.  These types are not defined
251  * here, because they are only used in Level 2.
252  */
253 
254 /* Define the (opaque) type for device color types. */
255 /* The name is an unfortunate anachronism. */
256 typedef struct gx_device_color_type_s gx_device_color_type_t;
257 typedef const gx_device_color_type_t *gx_device_color_type;
258 
259 struct gx_device_color_s {
260     /*
261      * Since some compilers don't allow static initialization of a
262      * union, we put the type first.
263      */
264     gx_device_color_type type;
265     /*
266      * See the comment above for descriptions of the members.  We use
267      * b_, c_, and p_ member names because some old compilers don't
268      * allow the same name to be used for two different structure
269      * members even when it's unambiguous.
270      */
271     union _c {
272 	gx_color_index pure;
273 	struct _bin {
274 	    const gx_device_halftone *b_ht;
275 	    gx_color_index color[2];
276 	    uint b_level;
277 	    int b_index;
278 	    gx_ht_tile *b_tile;
279 	} binary;
280 	struct _col {
281 	    gx_device_halftone *c_ht; /* non-const for setting cache ptr */
282 	    ushort num_components;
283 	    byte c_base[GX_DEVICE_COLOR_MAX_COMPONENTS];
284 	    uint c_level[GX_DEVICE_COLOR_MAX_COMPONENTS];
285 	    ushort /*gx_color_value */ alpha;
286 #if GX_DEVICE_COLOR_MAX_COMPONENTS <= ARCH_SIZEOF_SHORT * 8
287 	    ushort plane_mask;
288 #else
289 #if GX_DEVICE_COLOR_MAX_COMPONENTS <= ARCH_SIZEOF_INT * 8
290 	    uint plane_mask;
291 #else
292 	    gx_color_index plane_mask;
293 #endif
294 #endif
295 	} colored;
296 	struct _wts {
297 	    const gx_device_halftone *w_ht;
298 	    wts_screen_sample_t levels[GX_DEVICE_COLOR_MAX_COMPONENTS];
299 	    ushort num_components;
300 
301 	    /* plane_mask and base_color would be an optimization */
302 	    gx_color_index plane_vector[GX_DEVICE_COLOR_MAX_COMPONENTS];
303 	} wts;
304 	struct _pat {
305 	    gx_color_tile *p_tile;
306 	} /*(colored) */ pattern;
307     } colors;
308     gs_int_point phase;
309     /*
310      * This flag indicates if the paint values in ccolor are valid.  They
311      * are valid for most cases.  However there are some special cases
312      * in which a device color is initialized to specific values (usually
313      * black or white) instead of being created from a color space and
314      * color values.
315      */
316     bool ccolor_valid;
317     /*
318      * 'ccolor' (a "client color") is valid iff 'ccolor_valid' is true.
319      * For non-pattern colors, it contains the original paint values.
320      * For pattern colors, it contains information required for remapping
321      * the pattern.
322      */
323     gs_client_color ccolor;
324 
325     struct _mask {
326 	struct mp_ {
327 	    short x, y;
328 	} m_phase;
329 	gx_bitmap_id id;
330 	gx_color_tile *m_tile;
331     } mask;
332 };
333 
334 /*extern_st(st_device_color); *//* in gxdcolor.h */
335 #define public_st_device_color() /* in gxcmap.c */\
336   gs_public_st_composite(st_device_color, gx_device_color, "gx_device_color",\
337     device_color_enum_ptrs, device_color_reloc_ptrs)
338 #define st_device_color_max_ptrs (st_client_color_max_ptrs + 2)
339 
340 /*
341  * For the command list, it is useful to record the most recent device
342  * color placed in a band, so as to avoid sending unnecessary
343  * information. The following structure is used for that purpose. It is
344  * created by the save_dc method, and can be utilized by the write
345  * method. It should otherwise be considered opaque, though it is
346  * guarranteed not to contain pointers to allocated memory (and thus does
347  * not interact with the GC code for containing structures).
348  *
349  * The reason a structure distinct from the device color itself is used
350  * for this purpose is related to an anomally involving reference to
351  * device halftones. The gx_device_halftone structure is reference
352  * counted, but a long standing (and not easily removable) convention
353  * in the code states that only reference from imager (graphic) states
354  * to halftones are counted; reference from device colors are not. The
355  * pointer to a halftone in a saved device color may, therefore,
356  * become a dangling pointer. This does not occur in other uses of a
357  * device color, because a color can only be usable when the hafltone
358  * it references is the current halftone in some imager state.
359  *
360  * Because halftones are large and seldom changed, they are always sent
361  * as "all bands" commands. Individual device colors, by contrast, are
362  * usually written just for the bands that make use of them. The
363  * distinction between these two cases can only be handled by command
364  * list writer code itself, so this structure does not involve the
365  * halftone. If the halftone changes, however, the write method should
366  * be passed a null pointer for the saved color operand; this will
367  * ensure the the full device color information is written.
368  *
369  * Currently patterns cannot be passed through the command list,
370  * however vector devices need to save a color for comparing
371  * it with another color, which appears later.
372  * We provide a minimal support, which is necessary
373  * for the current implementation of pdfwrite.
374  * It is not sufficient for restoring the pattern from the saved color.
375  */
376 
377 struct gx_device_color_saved_s {
378     gx_device_color_type    type;
379     union _svc {
380         gx_color_index  pure;
381         struct _svbin {
382             gx_color_index  b_color[2];
383             uint            b_level;
384             int             b_index;
385         }               binary;
386         struct _svcol {
387             byte    c_base[GX_DEVICE_COLOR_MAX_COMPONENTS];
388             uint    c_level[GX_DEVICE_COLOR_MAX_COMPONENTS];
389             ushort  alpha;
390         }               colored;
391         struct _swts {
392             wts_screen_sample_t levels[GX_DEVICE_COLOR_MAX_COMPONENTS];
393         }               wts;
394 	struct _pattern {
395 	    gs_id id;
396 	    gs_int_point phase;
397 	}		pattern;
398 	struct _pattern2 {
399 	    gs_id id;
400 	}		pattern2;
401     }                       colors;
402     gs_int_point            phase;
403 };
404 
405 
406 /*
407  * Define the standard device color types.
408  * We define them here as pointers to the real types only because a few
409  * C compilers don't allow declaring externs with abstract struct types;
410  * we redefine them as macros in gxdcolor.h where the concrete type for
411  * gx_device_color_procs is available.
412  * We spell out the definition of gx_device_color type because some
413  * C compilers can't handle the typedef correctly.
414  */
415 #ifndef gx_dc_type_none
416 extern const gx_device_color_type_t *const gx_dc_type_none;	/* gxdcolor.c */
417 #endif
418 #ifndef gx_dc_type_null
419 extern const gx_device_color_type_t *const gx_dc_type_null;	/* gxdcolor.c */
420 #endif
421 #ifndef gx_dc_type_pure
422 extern const gx_device_color_type_t *const gx_dc_type_pure;	/* gxdcolor.c */
423 #endif
424 		/*
425 		 * We don't declare gx_dc_pattern here, so as not to create
426 		 * a spurious external reference in Level 1 systems.
427 		 */
428 #ifndef gx_dc_type_pattern
429 /*extern const gx_device_color_type_t * const gx_dc_type_pattern; *//* gspcolor.c */
430 #endif
431 #ifndef gx_dc_type_ht_binary
432 extern const gx_device_color_type_t *const gx_dc_type_ht_binary;	/* gxht.c */
433 #endif
434 #ifndef gx_dc_type_ht_colored
435 extern const gx_device_color_type_t *const gx_dc_type_ht_colored;	/* gxcht.c */
436 #endif
437 #ifndef gx_dc_type_ht_colored
438 extern const gx_device_color_type_t *const gx_dc_type_wts;	/* gxwts.c */
439 #endif
440 
441 #endif /* gsdcolor_INCLUDED */
442