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