1 #ifndef MUPDF_FITZ_COLOR_H
2 #define MUPDF_FITZ_COLOR_H
3 
4 #include "mupdf/fitz/system.h"
5 #include "mupdf/fitz/context.h"
6 #include "mupdf/fitz/store.h"
7 
8 #if FZ_ENABLE_ICC
9 /**
10 	Opaque type for an ICC Profile.
11 */
12 typedef struct fz_icc_profile fz_icc_profile;
13 #endif
14 
15 /**
16 	Describes a given colorspace.
17 */
18 typedef struct fz_colorspace fz_colorspace;
19 
20 /**
21 	Pixmaps represent a set of pixels for a 2 dimensional region of
22 	a plane. Each pixel has n components per pixel. The components
23 	are in the order process-components, spot-colors, alpha, where
24 	there can be 0 of any of those types. The data is in
25 	premultiplied alpha when rendering, but non-premultiplied for
26 	colorspace conversions and rescaling.
27 */
28 typedef struct fz_pixmap fz_pixmap;
29 
30 /* Color handling parameters: rendering intent, overprint, etc. */
31 
32 enum
33 {
34 	/* Same order as needed by lcms */
35 	FZ_RI_PERCEPTUAL,
36 	FZ_RI_RELATIVE_COLORIMETRIC,
37 	FZ_RI_SATURATION,
38 	FZ_RI_ABSOLUTE_COLORIMETRIC,
39 };
40 
41 typedef struct
42 {
43 	uint8_t ri;	/* rendering intent */
44 	uint8_t bp;	/* black point compensation */
45 	uint8_t op;	/* overprinting */
46 	uint8_t opm;	/* overprint mode */
47 }  fz_color_params;
48 
49 extern const fz_color_params fz_default_color_params;
50 
51 /**
52 	Map from (case sensitive) rendering intent string to enumeration
53 	value.
54 */
55 int fz_lookup_rendering_intent(const char *name);
56 
57 /**
58 	Map from enumerated rendering intent to string.
59 
60 	The returned string is static and therefore must not be freed.
61 */
62 const char *fz_rendering_intent_name(int ri);
63 
64 /**
65 	The maximum number of colorants available in any given
66 	color/colorspace (not including alpha).
67 
68 	Changing this value will alter the amount of memory being used
69 	(both stack and heap space), but not hugely. Speed should
70 	(largely) be determined by the number of colors actually used.
71 */
72 enum { FZ_MAX_COLORS = 32 };
73 
74 enum fz_colorspace_type
75 {
76 	FZ_COLORSPACE_NONE,
77 	FZ_COLORSPACE_GRAY,
78 	FZ_COLORSPACE_RGB,
79 	FZ_COLORSPACE_BGR,
80 	FZ_COLORSPACE_CMYK,
81 	FZ_COLORSPACE_LAB,
82 	FZ_COLORSPACE_INDEXED,
83 	FZ_COLORSPACE_SEPARATION,
84 };
85 
86 enum
87 {
88 	FZ_COLORSPACE_IS_DEVICE = 1,
89 	FZ_COLORSPACE_IS_ICC = 2,
90 	FZ_COLORSPACE_HAS_CMYK = 4,
91 	FZ_COLORSPACE_HAS_SPOTS = 8,
92 	FZ_COLORSPACE_HAS_CMYK_AND_SPOTS = 4|8,
93 };
94 
95 /**
96 	Creates a new colorspace instance and returns a reference.
97 
98 	No internal checking is done that the colorspace type (e.g.
99 	CMYK) matches with the flags (e.g. FZ_COLORSPACE_HAS_CMYK) or
100 	colorant count (n) or name.
101 
102 	The reference should be dropped when it is finished with.
103 
104 	Colorspaces are immutable once created (with the exception of
105 	setting up colorant names for separation spaces).
106 */
107 fz_colorspace *fz_new_colorspace(fz_context *ctx, enum fz_colorspace_type type, int flags, int n, const char *name);
108 
109 /**
110 	Increment the reference count for the colorspace.
111 
112 	Returns the same pointer. Never throws an exception.
113 */
114 fz_colorspace *fz_keep_colorspace(fz_context *ctx, fz_colorspace *colorspace);
115 
116 /**
117 	Drops a reference to the colorspace.
118 
119 	When the reference count reaches zero, the colorspace is
120 	destroyed.
121 */
122 void fz_drop_colorspace(fz_context *ctx, fz_colorspace *colorspace);
123 
124 /**
125 	Create an indexed colorspace.
126 
127 	The supplied lookup table is high palette entries long. Each
128 	entry is n bytes long, where n is given by the number of
129 	colorants in the base colorspace, one byte per colorant.
130 
131 	Ownership of lookup is passed it; it will be freed on
132 	destruction, so must be heap allocated.
133 
134 	The colorspace will keep an additional reference to the base
135 	colorspace that will be dropped on destruction.
136 
137 	The returned reference should be dropped when it is finished
138 	with.
139 
140 	Colorspaces are immutable once created.
141 */
142 fz_colorspace *fz_new_indexed_colorspace(fz_context *ctx, fz_colorspace *base, int high, unsigned char *lookup);
143 
144 /**
145 	Create a colorspace from an ICC profile supplied in buf.
146 
147 	Limited checking is done to ensure that the colorspace type is
148 	appropriate for the supplied ICC profile.
149 
150 	An additional reference is taken to buf, which will be dropped
151 	on destruction. Ownership is NOT passed in.
152 
153 	The returned reference should be dropped when it is finished
154 	with.
155 
156 	Colorspaces are immutable once created.
157 */
158 fz_colorspace *fz_new_icc_colorspace(fz_context *ctx, enum fz_colorspace_type type, int flags, const char *name, fz_buffer *buf);
159 
160 
161 /**
162 	Create a calibrated gray colorspace.
163 
164 	The returned reference should be dropped when it is finished
165 	with.
166 
167 	Colorspaces are immutable once created.
168 */
169 fz_colorspace *fz_new_cal_gray_colorspace(fz_context *ctx, float wp[3], float bp[3], float gamma);
170 
171 /**
172 	Create a calibrated rgb colorspace.
173 
174 	The returned reference should be dropped when it is finished
175 	with.
176 
177 	Colorspaces are immutable once created.
178 */
179 fz_colorspace *fz_new_cal_rgb_colorspace(fz_context *ctx, float wp[3], float bp[3], float gamma[3], float matrix[9]);
180 
181 /**
182 	Query the type of colorspace.
183 */
184 enum fz_colorspace_type fz_colorspace_type(fz_context *ctx, fz_colorspace *cs);
185 
186 /**
187 	Query the name of a colorspace.
188 
189 	The returned string has the same lifespan as the colorspace
190 	does. Caller should not free it.
191 */
192 const char *fz_colorspace_name(fz_context *ctx, fz_colorspace *cs);
193 
194 /**
195 	Query the number of colorants in a colorspace.
196 */
197 int fz_colorspace_n(fz_context *ctx, fz_colorspace *cs);
198 
199 /**
200 	True for CMYK, Separation and DeviceN colorspaces.
201 */
202 int fz_colorspace_is_subtractive(fz_context *ctx, fz_colorspace *cs);
203 
204 /**
205 	True if DeviceN color space has only colorants from the CMYK set.
206 */
207 int fz_colorspace_device_n_has_only_cmyk(fz_context *ctx, fz_colorspace *cs);
208 
209 /**
210 	True if DeviceN color space has cyan magenta yellow or black as
211 	one of its colorants.
212 */
213 int fz_colorspace_device_n_has_cmyk(fz_context *ctx, fz_colorspace *cs);
214 
215 /**
216 	Tests for particular types of colorspaces
217 */
218 int fz_colorspace_is_gray(fz_context *ctx, fz_colorspace *cs);
219 int fz_colorspace_is_rgb(fz_context *ctx, fz_colorspace *cs);
220 int fz_colorspace_is_cmyk(fz_context *ctx, fz_colorspace *cs);
221 int fz_colorspace_is_lab(fz_context *ctx, fz_colorspace *cs);
222 int fz_colorspace_is_indexed(fz_context *ctx, fz_colorspace *cs);
223 int fz_colorspace_is_device_n(fz_context *ctx, fz_colorspace *cs);
224 int fz_colorspace_is_device(fz_context *ctx, fz_colorspace *cs);
225 int fz_colorspace_is_device_gray(fz_context *ctx, fz_colorspace *cs);
226 int fz_colorspace_is_device_cmyk(fz_context *ctx, fz_colorspace *cs);
227 int fz_colorspace_is_lab_icc(fz_context *ctx, fz_colorspace *cs);
228 
229 /**
230 	Check to see that a colorspace is appropriate to be used as
231 	a blending space (i.e. only grey, rgb or cmyk).
232 */
233 int fz_is_valid_blend_colorspace(fz_context *ctx, fz_colorspace *cs);
234 
235 /**
236 	Retrieve global default colorspaces.
237 
238 	These return borrowed references that should not be dropped,
239 	unless they are kept first.
240 */
241 fz_colorspace *fz_device_gray(fz_context *ctx);
242 fz_colorspace *fz_device_rgb(fz_context *ctx);
243 fz_colorspace *fz_device_bgr(fz_context *ctx);
244 fz_colorspace *fz_device_cmyk(fz_context *ctx);
245 fz_colorspace *fz_device_lab(fz_context *ctx);
246 
247 /**
248 	Assign a name for a given colorant in a colorspace.
249 
250 	Used while initially setting up a colorspace. The string is
251 	copied into local storage, so need not be retained by the
252 	caller.
253 */
254 void fz_colorspace_name_colorant(fz_context *ctx, fz_colorspace *cs, int n, const char *name);
255 
256 /**
257 	Retrieve a the name for a colorant.
258 
259 	Returns a pointer with the same lifespan as the colorspace.
260 */
261 const char *fz_colorspace_colorant(fz_context *ctx, fz_colorspace *cs, int n);
262 
263 /* Color conversion */
264 
265 /**
266 	Clamp the samples in a color to the correct ranges for a
267 	given colorspace.
268 */
269 void fz_clamp_color(fz_context *ctx, fz_colorspace *cs, const float *in, float *out);
270 
271 /**
272 	Convert color values sv from colorspace ss into colorvalues dv
273 	for colorspace ds, via an optional intervening space is,
274 	respecting the given color_params.
275 */
276 void fz_convert_color(fz_context *ctx, fz_colorspace *ss, const float *sv, fz_colorspace *ds, float *dv, fz_colorspace *is, fz_color_params params);
277 
278 /* Default (fallback) colorspace handling */
279 
280 /**
281 	Structure to hold default colorspaces.
282 */
283 typedef struct
284 {
285 	int refs;
286 	fz_colorspace *gray;
287 	fz_colorspace *rgb;
288 	fz_colorspace *cmyk;
289 	fz_colorspace *oi;
290 }  fz_default_colorspaces;
291 
292 /**
293 	Create a new default colorspace structure with values inherited
294 	from the context, and return a reference to it.
295 
296 	These can be overridden using fz_set_default_xxxx.
297 
298 	These should not be overridden while more than one caller has
299 	the reference for fear of race conditions.
300 
301 	The caller should drop this reference once finished with it.
302 */
303 fz_default_colorspaces *fz_new_default_colorspaces(fz_context *ctx);
304 
305 /**
306 	Keep an additional reference to the default colorspaces
307 	structure.
308 
309 	Never throws exceptions.
310 */
311 fz_default_colorspaces* fz_keep_default_colorspaces(fz_context *ctx, fz_default_colorspaces *default_cs);
312 
313 /**
314 	Drop a reference to the default colorspaces structure. When the
315 	reference count reaches 0, the references it holds internally
316 	to the underlying colorspaces will be dropped, and the structure
317 	will be destroyed.
318 
319 	Never throws exceptions.
320 */
321 void fz_drop_default_colorspaces(fz_context *ctx, fz_default_colorspaces *default_cs);
322 
323 /**
324 	Returns a reference to a newly cloned default colorspaces
325 	structure.
326 
327 	The new clone may safely be altered without fear of race
328 	conditions as the caller is the only reference holder.
329 */
330 fz_default_colorspaces *fz_clone_default_colorspaces(fz_context *ctx, fz_default_colorspaces *base);
331 
332 /**
333 	Retrieve default colorspaces (typically page local).
334 
335 	If default_cs is non NULL, the default is retrieved from there,
336 	otherwise the global default is retrieved.
337 
338 	These return borrowed references that should not be dropped,
339 	unless they are kept first.
340 */
341 fz_colorspace *fz_default_gray(fz_context *ctx, const fz_default_colorspaces *default_cs);
342 fz_colorspace *fz_default_rgb(fz_context *ctx, const fz_default_colorspaces *default_cs);
343 fz_colorspace *fz_default_cmyk(fz_context *ctx, const fz_default_colorspaces *default_cs);
344 fz_colorspace *fz_default_output_intent(fz_context *ctx, const fz_default_colorspaces *default_cs);
345 
346 /**
347 	Set new defaults within the default colorspace structure.
348 
349 	New references are taken to the new default, and references to
350 	the old defaults dropped.
351 
352 	Never throws exceptions.
353 */
354 void fz_set_default_gray(fz_context *ctx, fz_default_colorspaces *default_cs, fz_colorspace *cs);
355 void fz_set_default_rgb(fz_context *ctx, fz_default_colorspaces *default_cs, fz_colorspace *cs);
356 void fz_set_default_cmyk(fz_context *ctx, fz_default_colorspaces *default_cs, fz_colorspace *cs);
357 void fz_set_default_output_intent(fz_context *ctx, fz_default_colorspaces *default_cs, fz_colorspace *cs);
358 
359 /* Implementation details: subject to change. */
360 
361 struct fz_colorspace
362 {
363 	fz_key_storable key_storable;
364 	enum fz_colorspace_type type;
365 	int flags;
366 	int n;
367 	char *name;
368 	union {
369 #if FZ_ENABLE_ICC
370 		struct {
371 			fz_buffer *buffer;
372 			unsigned char md5[16];
373 			fz_icc_profile *profile;
374 		} icc;
375 #endif
376 		struct {
377 			fz_colorspace *base;
378 			int high;
379 			unsigned char *lookup;
380 		} indexed;
381 		struct {
382 			fz_colorspace *base;
383 			void (*eval)(fz_context *ctx, void *tint, const float *s, int sn, float *d, int dn);
384 			void (*drop)(fz_context *ctx, void *tint);
385 			void *tint;
386 			char *colorant[FZ_MAX_COLORS];
387 		} separation;
388 	} u;
389 };
390 
391 void fz_drop_colorspace_imp(fz_context *ctx, fz_storable *cs_);
392 
393 #endif
394