1 /*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #pragma once
9
10 // skcms_public.h contains the entire public API for skcms.
11
12 #ifndef SKCMS_API
13 #define SKCMS_API
14 #endif
15
16 #include <stdbool.h>
17 #include <stddef.h>
18 #include <stdint.h>
19 #include <string.h>
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 // A row-major 3x3 matrix (ie vals[row][col])
26 typedef struct skcms_Matrix3x3 {
27 float vals[3][3];
28 } skcms_Matrix3x3;
29
30 // It is _not_ safe to alias the pointers to invert in-place.
31 SKCMS_API bool skcms_Matrix3x3_invert(const skcms_Matrix3x3*, skcms_Matrix3x3*);
32 SKCMS_API skcms_Matrix3x3 skcms_Matrix3x3_concat(const skcms_Matrix3x3*, const skcms_Matrix3x3*);
33
34 // A row-major 3x4 matrix (ie vals[row][col])
35 typedef struct skcms_Matrix3x4 {
36 float vals[3][4];
37 } skcms_Matrix3x4;
38
39 // A transfer function mapping encoded values to linear values,
40 // represented by this 7-parameter piecewise function:
41 //
42 // linear = sign(encoded) * (c*|encoded| + f) , 0 <= |encoded| < d
43 // = sign(encoded) * ((a*|encoded| + b)^g + e), d <= |encoded|
44 //
45 // (A simple gamma transfer function sets g to gamma and a to 1.)
46 typedef struct skcms_TransferFunction {
47 float g, a,b,c,d,e,f;
48 } skcms_TransferFunction;
49
50 SKCMS_API float skcms_TransferFunction_eval (const skcms_TransferFunction*, float);
51 SKCMS_API bool skcms_TransferFunction_invert(const skcms_TransferFunction*,
52 skcms_TransferFunction*);
53
54 typedef enum skcms_TFType {
55 skcms_TFType_Invalid,
56 skcms_TFType_sRGBish,
57 skcms_TFType_PQish,
58 skcms_TFType_HLGish,
59 skcms_TFType_HLGinvish,
60 } skcms_TFType;
61
62 // Identify which kind of transfer function is encoded in an skcms_TransferFunction
63 SKCMS_API skcms_TFType skcms_TransferFunction_getType(const skcms_TransferFunction*);
64
65 // We can jam a couple alternate transfer function forms into skcms_TransferFunction,
66 // including those matching the general forms of the SMPTE ST 2084 PQ function or HLG.
67 //
68 // PQish:
69 // max(A + B|encoded|^C, 0)
70 // linear = sign(encoded) * (------------------------) ^ F
71 // D + E|encoded|^C
72 SKCMS_API bool skcms_TransferFunction_makePQish(skcms_TransferFunction*,
73 float A, float B, float C,
74 float D, float E, float F);
75 // HLGish:
76 // { K * sign(encoded) * ( (R|encoded|)^G ) when 0 <= |encoded| <= 1/R
77 // linear = { K * sign(encoded) * ( e^(a(|encoded|-c)) + b ) when 1/R < |encoded|
78 SKCMS_API bool skcms_TransferFunction_makeScaledHLGish(skcms_TransferFunction*,
79 float K, float R, float G,
80 float a, float b, float c);
81
82 // Compatibility shim with K=1 for old callers.
skcms_TransferFunction_makeHLGish(skcms_TransferFunction * fn,float R,float G,float a,float b,float c)83 static inline bool skcms_TransferFunction_makeHLGish(skcms_TransferFunction* fn,
84 float R, float G,
85 float a, float b, float c) {
86 return skcms_TransferFunction_makeScaledHLGish(fn, 1.0f, R,G, a,b,c);
87 }
88
89 // PQ mapping encoded [0,1] to linear [0,1].
skcms_TransferFunction_makePQ(skcms_TransferFunction * tf)90 static inline bool skcms_TransferFunction_makePQ(skcms_TransferFunction* tf) {
91 return skcms_TransferFunction_makePQish(tf, -107/128.0f, 1.0f, 32/2523.0f
92 , 2413/128.0f, -2392/128.0f, 8192/1305.0f);
93 }
94 // HLG mapping encoded [0,1] to linear [0,12].
skcms_TransferFunction_makeHLG(skcms_TransferFunction * tf)95 static inline bool skcms_TransferFunction_makeHLG(skcms_TransferFunction* tf) {
96 return skcms_TransferFunction_makeHLGish(tf, 2.0f, 2.0f
97 , 1/0.17883277f, 0.28466892f, 0.55991073f);
98 }
99
100 // Is this an ordinary sRGB-ish transfer function, or one of the HDR forms we support?
101 SKCMS_API bool skcms_TransferFunction_isSRGBish(const skcms_TransferFunction*);
102 SKCMS_API bool skcms_TransferFunction_isPQish (const skcms_TransferFunction*);
103 SKCMS_API bool skcms_TransferFunction_isHLGish (const skcms_TransferFunction*);
104
105 // Unified representation of 'curv' or 'para' tag data, or a 1D table from 'mft1' or 'mft2'
106 typedef union skcms_Curve {
107 struct {
108 // this needs to line up with alias_of_table_entries so we can tell if there are or
109 // are not table entries. If this is 0, this struct is a parametric function,
110 // otherwise it's a table entry.
111 uint32_t alias_of_table_entries;
112 skcms_TransferFunction parametric;
113 };
114 struct {
115 uint32_t table_entries;
116 const uint8_t* table_8;
117 const uint8_t* table_16;
118 };
119 } skcms_Curve;
120
121 // Complex transforms between device space (A) and profile connection space (B):
122 // A2B: device -> [ "A" curves -> CLUT ] -> [ "M" curves -> matrix ] -> "B" curves -> PCS
123 // B2A: device <- [ "A" curves <- CLUT ] <- [ "M" curves <- matrix ] <- "B" curves <- PCS
124
125 typedef struct skcms_A2B {
126 // Optional: N 1D "A" curves, followed by an N-dimensional CLUT.
127 // If input_channels == 0, these curves and CLUT are skipped,
128 // Otherwise, input_channels must be in [1, 4].
129 skcms_Curve input_curves[4];
130 const uint8_t* grid_8;
131 const uint8_t* grid_16;
132 uint32_t input_channels;
133 uint8_t grid_points[4];
134
135 // Optional: 3 1D "M" curves, followed by a color matrix.
136 // If matrix_channels == 0, these curves and matrix are skipped,
137 // Otherwise, matrix_channels must be 3.
138 skcms_Curve matrix_curves[3];
139 skcms_Matrix3x4 matrix;
140 uint32_t matrix_channels;
141
142 // Required: 3 1D "B" curves. Always present, and output_channels must be 3.
143 uint32_t output_channels; // list first to pack with matrix_channels
144 skcms_Curve output_curves[3];
145 } skcms_A2B;
146
147 typedef struct skcms_B2A {
148 // Required: 3 1D "B" curves. Always present, and input_channels must be 3.
149 skcms_Curve input_curves[3];
150 uint32_t input_channels;
151
152 // Optional: a color matrix, followed by 3 1D "M" curves.
153 // If matrix_channels == 0, this matrix and these curves are skipped,
154 // Otherwise, matrix_channels must be 3.
155 uint32_t matrix_channels; // list first to pack with input_channels
156 skcms_Curve matrix_curves[3];
157 skcms_Matrix3x4 matrix;
158
159 // Optional: an N-dimensional CLUT, followed by N 1D "A" curves.
160 // If output_channels == 0, this CLUT and these curves are skipped,
161 // Otherwise, output_channels must be in [1, 4].
162 skcms_Curve output_curves[4];
163 const uint8_t* grid_8;
164 const uint8_t* grid_16;
165 uint8_t grid_points[4];
166 uint32_t output_channels;
167 } skcms_B2A;
168
169 typedef struct skcms_CICP {
170 uint8_t color_primaries;
171 uint8_t transfer_characteristics;
172 uint8_t matrix_coefficients;
173 uint8_t video_full_range_flag;
174 } skcms_CICP;
175
176 typedef struct skcms_ICCProfile {
177 const uint8_t* buffer;
178
179 uint32_t size;
180 uint32_t data_color_space;
181 uint32_t pcs;
182 uint32_t tag_count;
183
184 // skcms_Parse() will set commonly-used fields for you when possible:
185
186 // If we can parse red, green and blue transfer curves from the profile,
187 // trc will be set to those three curves, and has_trc will be true.
188 skcms_Curve trc[3];
189
190 // If this profile's gamut can be represented by a 3x3 transform to XYZD50,
191 // skcms_Parse() sets toXYZD50 to that transform and has_toXYZD50 to true.
192 skcms_Matrix3x3 toXYZD50;
193
194 // If the profile has a valid A2B0 or A2B1 tag, skcms_Parse() sets A2B to
195 // that data, and has_A2B to true. skcms_ParseWithA2BPriority() does the
196 // same following any user-provided prioritization of A2B0, A2B1, or A2B2.
197 skcms_A2B A2B;
198
199 // If the profile has a valid B2A0 or B2A1 tag, skcms_Parse() sets B2A to
200 // that data, and has_B2A to true. skcms_ParseWithA2BPriority() does the
201 // same following any user-provided prioritization of B2A0, B2A1, or B2A2.
202 skcms_B2A B2A;
203
204 // If the profile has a valid CICP tag, skcms_Parse() sets CICP to that data,
205 // and has_CICP to true.
206 skcms_CICP CICP;
207
208 bool has_trc;
209 bool has_toXYZD50;
210 bool has_A2B;
211 bool has_B2A;
212 bool has_CICP;
213 } skcms_ICCProfile;
214
215 // The sRGB color profile is so commonly used that we offer a canonical skcms_ICCProfile for it.
216 SKCMS_API const skcms_ICCProfile* skcms_sRGB_profile(void);
217 // Ditto for XYZD50, the most common profile connection space.
218 SKCMS_API const skcms_ICCProfile* skcms_XYZD50_profile(void);
219
220 SKCMS_API const skcms_TransferFunction* skcms_sRGB_TransferFunction(void);
221 SKCMS_API const skcms_TransferFunction* skcms_sRGB_Inverse_TransferFunction(void);
222 SKCMS_API const skcms_TransferFunction* skcms_Identity_TransferFunction(void);
223
224 // Practical equality test for two skcms_ICCProfiles.
225 // The implementation is subject to change, but it will always try to answer
226 // "can I substitute A for B?" and "can I skip transforming from A to B?".
227 SKCMS_API bool skcms_ApproximatelyEqualProfiles(const skcms_ICCProfile* A,
228 const skcms_ICCProfile* B);
229
230 // Practical test that answers: Is curve roughly the inverse of inv_tf? Typically used by passing
231 // the inverse of a known parametric transfer function (like sRGB), to determine if a particular
232 // curve is very close to sRGB.
233 SKCMS_API bool skcms_AreApproximateInverses(const skcms_Curve* curve,
234 const skcms_TransferFunction* inv_tf);
235
236 // Similar to above, answering the question for all three TRC curves of the given profile. Again,
237 // passing skcms_sRGB_InverseTransferFunction as inv_tf will answer the question:
238 // "Does this profile have a transfer function that is very close to sRGB?"
239 SKCMS_API bool skcms_TRCs_AreApproximateInverse(const skcms_ICCProfile* profile,
240 const skcms_TransferFunction* inv_tf);
241
242 // Parse an ICC profile and return true if possible, otherwise return false.
243 // Selects an A2B profile (if present) according to priority list (each entry 0-2).
244 // The buffer is not copied; it must remain valid as long as the skcms_ICCProfile will be used.
245 SKCMS_API bool skcms_ParseWithA2BPriority(const void*, size_t,
246 const int priority[], int priorities,
247 skcms_ICCProfile*);
248
skcms_Parse(const void * buf,size_t len,skcms_ICCProfile * profile)249 static inline bool skcms_Parse(const void* buf, size_t len, skcms_ICCProfile* profile) {
250 // For continuity of existing user expectations,
251 // prefer A2B0 (perceptual) over A2B1 (relative colormetric), and ignore A2B2 (saturation).
252 const int priority[] = {0,1};
253 return skcms_ParseWithA2BPriority(buf, len,
254 priority, sizeof(priority)/sizeof(*priority),
255 profile);
256 }
257
258 SKCMS_API bool skcms_ApproximateCurve(const skcms_Curve* curve,
259 skcms_TransferFunction* approx,
260 float* max_error);
261
262 SKCMS_API bool skcms_GetCHAD(const skcms_ICCProfile*, skcms_Matrix3x3*);
263 SKCMS_API bool skcms_GetWTPT(const skcms_ICCProfile*, float xyz[3]);
264
265 // Returns the number of channels of input data that are expected on the "A" side of the profile.
266 // This is useful for image codecs, where the image data and the accompanying profile might have
267 // conflicting data shapes. In some cases, the result is unclear or invalid. In that case, the
268 // function will return a negative value to signal an error.
269 SKCMS_API int skcms_GetInputChannelCount(const skcms_ICCProfile*);
270
271 // These are common ICC signature values
272 enum {
273 // common data_color_space values
274 skcms_Signature_CMYK = 0x434D594B,
275 skcms_Signature_Gray = 0x47524159,
276 skcms_Signature_RGB = 0x52474220,
277
278 // pcs (or data_color_space)
279 skcms_Signature_Lab = 0x4C616220,
280 skcms_Signature_XYZ = 0x58595A20,
281
282 // other, less common data_color_space values
283 skcms_Signature_CIELUV = 0x4C757620,
284 skcms_Signature_YCbCr = 0x59436272,
285 skcms_Signature_CIEYxy = 0x59787920,
286 skcms_Signature_HSV = 0x48535620,
287 skcms_Signature_HLS = 0x484C5320,
288 skcms_Signature_CMY = 0x434D5920,
289 skcms_Signature_2CLR = 0x32434C52,
290 skcms_Signature_3CLR = 0x33434C52,
291 skcms_Signature_4CLR = 0x34434C52,
292 skcms_Signature_5CLR = 0x35434C52,
293 skcms_Signature_6CLR = 0x36434C52,
294 skcms_Signature_7CLR = 0x37434C52,
295 skcms_Signature_8CLR = 0x38434C52,
296 skcms_Signature_9CLR = 0x39434C52,
297 skcms_Signature_10CLR = 0x41434C52,
298 skcms_Signature_11CLR = 0x42434C52,
299 skcms_Signature_12CLR = 0x43434C52,
300 skcms_Signature_13CLR = 0x44434C52,
301 skcms_Signature_14CLR = 0x45434C52,
302 skcms_Signature_15CLR = 0x46434C52,
303 };
304
305 typedef enum skcms_PixelFormat {
306 skcms_PixelFormat_A_8,
307 skcms_PixelFormat_A_8_,
308 skcms_PixelFormat_G_8,
309 skcms_PixelFormat_G_8_,
310 skcms_PixelFormat_GA_88, // Grayscale with alpha.
311 skcms_PixelFormat_GA_88_,
312
313 skcms_PixelFormat_RGB_565,
314 skcms_PixelFormat_BGR_565,
315
316 skcms_PixelFormat_ABGR_4444,
317 skcms_PixelFormat_ARGB_4444,
318
319 skcms_PixelFormat_RGB_888,
320 skcms_PixelFormat_BGR_888,
321 skcms_PixelFormat_RGBA_8888,
322 skcms_PixelFormat_BGRA_8888,
323 skcms_PixelFormat_RGBA_8888_sRGB, // Automatic sRGB encoding / decoding.
324 skcms_PixelFormat_BGRA_8888_sRGB, // (Generally used with linear transfer functions.)
325
326 skcms_PixelFormat_RGBA_1010102,
327 skcms_PixelFormat_BGRA_1010102,
328
329 skcms_PixelFormat_RGB_161616LE, // Little-endian. Pointers must be 16-bit aligned.
330 skcms_PixelFormat_BGR_161616LE,
331 skcms_PixelFormat_RGBA_16161616LE,
332 skcms_PixelFormat_BGRA_16161616LE,
333
334 skcms_PixelFormat_RGB_161616BE, // Big-endian. Pointers must be 16-bit aligned.
335 skcms_PixelFormat_BGR_161616BE,
336 skcms_PixelFormat_RGBA_16161616BE,
337 skcms_PixelFormat_BGRA_16161616BE,
338
339 skcms_PixelFormat_RGB_hhh_Norm, // 1-5-10 half-precision float in [0,1]
340 skcms_PixelFormat_BGR_hhh_Norm, // Pointers must be 16-bit aligned.
341 skcms_PixelFormat_RGBA_hhhh_Norm,
342 skcms_PixelFormat_BGRA_hhhh_Norm,
343
344 skcms_PixelFormat_RGB_hhh, // 1-5-10 half-precision float.
345 skcms_PixelFormat_BGR_hhh, // Pointers must be 16-bit aligned.
346 skcms_PixelFormat_RGBA_hhhh,
347 skcms_PixelFormat_BGRA_hhhh,
348
349 skcms_PixelFormat_RGB_fff, // 1-8-23 single-precision float (the normal kind).
350 skcms_PixelFormat_BGR_fff, // Pointers must be 32-bit aligned.
351 skcms_PixelFormat_RGBA_ffff,
352 skcms_PixelFormat_BGRA_ffff,
353
354 skcms_PixelFormat_RGB_101010x_XR, // Note: This is located here to signal no clamping.
355 skcms_PixelFormat_BGR_101010x_XR, // Compatible with MTLPixelFormatBGR10_XR.
356 skcms_PixelFormat_RGBA_10101010_XR, // Note: This is located here to signal no clamping.
357 skcms_PixelFormat_BGRA_10101010_XR, // Compatible with MTLPixelFormatBGRA10_XR.
358 } skcms_PixelFormat;
359
360 // We always store any alpha channel linearly. In the chart below, tf-1() is the inverse
361 // transfer function for the given color profile (applying the transfer function linearizes).
362
363 // We treat opaque as a strong requirement, not just a performance hint: we will ignore
364 // any source alpha and treat it as 1.0, and will make sure that any destination alpha
365 // channel is filled with the equivalent of 1.0.
366
367 // We used to offer multiple types of premultiplication, but now just one, PremulAsEncoded.
368 // This is the premul you're probably used to working with.
369
370 typedef enum skcms_AlphaFormat {
371 skcms_AlphaFormat_Opaque, // alpha is always opaque
372 // tf-1(r), tf-1(g), tf-1(b), 1.0
373 skcms_AlphaFormat_Unpremul, // alpha and color are unassociated
374 // tf-1(r), tf-1(g), tf-1(b), a
375 skcms_AlphaFormat_PremulAsEncoded, // premultiplied while encoded
376 // tf-1(r)*a, tf-1(g)*a, tf-1(b)*a, a
377 } skcms_AlphaFormat;
378
379 // Convert npixels pixels from src format and color profile to dst format and color profile
380 // and return true, otherwise return false. It is safe to alias dst == src if dstFmt == srcFmt.
381 SKCMS_API bool skcms_Transform(const void* src,
382 skcms_PixelFormat srcFmt,
383 skcms_AlphaFormat srcAlpha,
384 const skcms_ICCProfile* srcProfile,
385 void* dst,
386 skcms_PixelFormat dstFmt,
387 skcms_AlphaFormat dstAlpha,
388 const skcms_ICCProfile* dstProfile,
389 size_t npixels);
390
391 // If profile can be used as a destination in skcms_Transform, return true. Otherwise, attempt to
392 // rewrite it with approximations where reasonable. If successful, return true. If no reasonable
393 // approximation exists, leave the profile unchanged and return false.
394 SKCMS_API bool skcms_MakeUsableAsDestination(skcms_ICCProfile* profile);
395
396 // If profile can be used as a destination with a single parametric transfer function (ie for
397 // rasterization), return true. Otherwise, attempt to rewrite it with approximations where
398 // reasonable. If successful, return true. If no reasonable approximation exists, leave the
399 // profile unchanged and return false.
400 SKCMS_API bool skcms_MakeUsableAsDestinationWithSingleCurve(skcms_ICCProfile* profile);
401
402 // Returns a matrix to adapt XYZ color from given the whitepoint to D50.
403 SKCMS_API bool skcms_AdaptToXYZD50(float wx, float wy,
404 skcms_Matrix3x3* toXYZD50);
405
406 // Returns a matrix to convert RGB color into XYZ adapted to D50, given the
407 // primaries and whitepoint of the RGB model.
408 SKCMS_API bool skcms_PrimariesToXYZD50(float rx, float ry,
409 float gx, float gy,
410 float bx, float by,
411 float wx, float wy,
412 skcms_Matrix3x3* toXYZD50);
413
414 // Call before your first call to skcms_Transform() to skip runtime CPU detection.
415 SKCMS_API void skcms_DisableRuntimeCPUDetection(void);
416
417 // Utilities for programmatically constructing profiles
skcms_Init(skcms_ICCProfile * p)418 static inline void skcms_Init(skcms_ICCProfile* p) {
419 memset(p, 0, sizeof(*p));
420 p->data_color_space = skcms_Signature_RGB;
421 p->pcs = skcms_Signature_XYZ;
422 }
423
skcms_SetTransferFunction(skcms_ICCProfile * p,const skcms_TransferFunction * tf)424 static inline void skcms_SetTransferFunction(skcms_ICCProfile* p,
425 const skcms_TransferFunction* tf) {
426 p->has_trc = true;
427 for (int i = 0; i < 3; ++i) {
428 p->trc[i].table_entries = 0;
429 p->trc[i].parametric = *tf;
430 }
431 }
432
skcms_SetXYZD50(skcms_ICCProfile * p,const skcms_Matrix3x3 * m)433 static inline void skcms_SetXYZD50(skcms_ICCProfile* p, const skcms_Matrix3x3* m) {
434 p->has_toXYZD50 = true;
435 p->toXYZD50 = *m;
436 }
437
438 #ifdef __cplusplus
439 }
440 #endif
441