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