Debian Codegraph @

modules/skcms/src/skcms_public.h

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