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.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 // We can jam a couple alternate transfer function forms into skcms_TransferFunction,
55 // including those matching the general forms of the SMPTE ST 2084 PQ function or HLG.
56 //
57 // PQish:
58 // max(A + B|encoded|^C, 0)
59 // linear = sign(encoded) * (------------------------) ^ F
60 // D + E|encoded|^C
61 SKCMS_API bool skcms_TransferFunction_makePQish(skcms_TransferFunction*,
62 float A, float B, float C,
63 float D, float E, float F);
64 // HLGish:
65 // { sign(encoded) * ( (R|encoded|)^G ) when 0 <= |encoded| <= 1/R
66 // linear = { sign(encoded) * ( e^(a(|encoded|-c)) + b ) when 1/R < |encoded|
67 SKCMS_API bool skcms_TransferFunction_makeHLGish(skcms_TransferFunction*,
68 float R, float G,
69 float a, float b, float c);
70
71 // PQ mapping encoded [0,1] to linear [0,1].
skcms_TransferFunction_makePQ(skcms_TransferFunction * tf)72 static inline bool skcms_TransferFunction_makePQ(skcms_TransferFunction* tf) {
73 return skcms_TransferFunction_makePQish(tf, -107/128.0f, 1.0f, 32/2523.0f
74 , 2413/128.0f, -2392/128.0f, 8192/1305.0f);
75 }
76 // HLG mapping encoded [0,1] to linear [0,12].
skcms_TransferFunction_makeHLG(skcms_TransferFunction * tf)77 static inline bool skcms_TransferFunction_makeHLG(skcms_TransferFunction* tf) {
78 return skcms_TransferFunction_makeHLGish(tf, 2.0f, 2.0f
79 , 1/0.17883277f, 0.28466892f, 0.55991073f);
80 }
81
82 // Unified representation of 'curv' or 'para' tag data, or a 1D table from 'mft1' or 'mft2'
83 typedef union skcms_Curve {
84 struct {
85 uint32_t alias_of_table_entries;
86 skcms_TransferFunction parametric;
87 };
88 struct {
89 uint32_t table_entries;
90 const uint8_t* table_8;
91 const uint8_t* table_16;
92 };
93 } skcms_Curve;
94
95 typedef struct skcms_A2B {
96 // Optional: N 1D curves, followed by an N-dimensional CLUT.
97 // If input_channels == 0, these curves and CLUT are skipped,
98 // Otherwise, input_channels must be in [1, 4].
99 uint32_t input_channels;
100 skcms_Curve input_curves[4];
101 uint8_t grid_points[4];
102 const uint8_t* grid_8;
103 const uint8_t* grid_16;
104
105 // Optional: 3 1D curves, followed by a color matrix.
106 // If matrix_channels == 0, these curves and matrix are skipped,
107 // Otherwise, matrix_channels must be 3.
108 uint32_t matrix_channels;
109 skcms_Curve matrix_curves[3];
110 skcms_Matrix3x4 matrix;
111
112 // Required: 3 1D curves. Always present, and output_channels must be 3.
113 uint32_t output_channels;
114 skcms_Curve output_curves[3];
115 } skcms_A2B;
116
117 typedef struct skcms_ICCProfile {
118 const uint8_t* buffer;
119
120 uint32_t size;
121 uint32_t data_color_space;
122 uint32_t pcs;
123 uint32_t tag_count;
124
125 // skcms_Parse() will set commonly-used fields for you when possible:
126
127 // If we can parse red, green and blue transfer curves from the profile,
128 // trc will be set to those three curves, and has_trc will be true.
129 bool has_trc;
130 skcms_Curve trc[3];
131
132 // If this profile's gamut can be represented by a 3x3 transform to XYZD50,
133 // skcms_Parse() sets toXYZD50 to that transform and has_toXYZD50 to true.
134 bool has_toXYZD50;
135 skcms_Matrix3x3 toXYZD50;
136
137 // If the profile has a valid A2B0 tag, skcms_Parse() sets A2B to that data,
138 // and has_A2B to true.
139 bool has_A2B;
140 skcms_A2B A2B;
141 } skcms_ICCProfile;
142
143 // The sRGB color profile is so commonly used that we offer a canonical skcms_ICCProfile for it.
144 SKCMS_API const skcms_ICCProfile* skcms_sRGB_profile(void);
145 // Ditto for XYZD50, the most common profile connection space.
146 SKCMS_API const skcms_ICCProfile* skcms_XYZD50_profile(void);
147
148 SKCMS_API const skcms_TransferFunction* skcms_sRGB_TransferFunction(void);
149 SKCMS_API const skcms_TransferFunction* skcms_sRGB_Inverse_TransferFunction(void);
150 SKCMS_API const skcms_TransferFunction* skcms_Identity_TransferFunction(void);
151
152 // Practical equality test for two skcms_ICCProfiles.
153 // The implementation is subject to change, but it will always try to answer
154 // "can I substitute A for B?" and "can I skip transforming from A to B?".
155 SKCMS_API bool skcms_ApproximatelyEqualProfiles(const skcms_ICCProfile* A,
156 const skcms_ICCProfile* B);
157
158 // Practical test that answers: Is curve roughly the inverse of inv_tf? Typically used by passing
159 // the inverse of a known parametric transfer function (like sRGB), to determine if a particular
160 // curve is very close to sRGB.
161 SKCMS_API bool skcms_AreApproximateInverses(const skcms_Curve* curve,
162 const skcms_TransferFunction* inv_tf);
163
164 // Similar to above, answering the question for all three TRC curves of the given profile. Again,
165 // passing skcms_sRGB_InverseTransferFunction as inv_tf will answer the question:
166 // "Does this profile have a transfer function that is very close to sRGB?"
167 SKCMS_API bool skcms_TRCs_AreApproximateInverse(const skcms_ICCProfile* profile,
168 const skcms_TransferFunction* inv_tf);
169
170 // Parse an ICC profile and return true if possible, otherwise return false.
171 // The buffer is not copied, it must remain valid as long as the skcms_ICCProfile
172 // will be used.
173 SKCMS_API bool skcms_Parse(const void*, size_t, skcms_ICCProfile*);
174
175 SKCMS_API bool skcms_ApproximateCurve(const skcms_Curve* curve,
176 skcms_TransferFunction* approx,
177 float* max_error);
178
179 typedef struct skcms_ICCTag {
180 uint32_t signature;
181 uint32_t type;
182 uint32_t size;
183 const uint8_t* buf;
184 } skcms_ICCTag;
185
186 SKCMS_API void skcms_GetTagByIndex (const skcms_ICCProfile*, uint32_t idx, skcms_ICCTag*);
187 SKCMS_API bool skcms_GetTagBySignature(const skcms_ICCProfile*, uint32_t sig, skcms_ICCTag*);
188
189 // These are common ICC signature values
190 enum {
191 // data_color_space
192 skcms_Signature_CMYK = 0x434D594B,
193 skcms_Signature_Gray = 0x47524159,
194 skcms_Signature_RGB = 0x52474220,
195
196 // pcs
197 skcms_Signature_Lab = 0x4C616220,
198 skcms_Signature_XYZ = 0x58595A20,
199 };
200
201 typedef enum skcms_PixelFormat {
202 skcms_PixelFormat_A_8,
203 skcms_PixelFormat_A_8_,
204 skcms_PixelFormat_G_8,
205 skcms_PixelFormat_G_8_,
206 skcms_PixelFormat_RGBA_8888_Palette8,
207 skcms_PixelFormat_BGRA_8888_Palette8,
208
209 skcms_PixelFormat_RGB_565,
210 skcms_PixelFormat_BGR_565,
211
212 skcms_PixelFormat_ABGR_4444,
213 skcms_PixelFormat_ARGB_4444,
214
215 skcms_PixelFormat_RGB_888,
216 skcms_PixelFormat_BGR_888,
217 skcms_PixelFormat_RGBA_8888,
218 skcms_PixelFormat_BGRA_8888,
219
220 skcms_PixelFormat_RGBA_1010102,
221 skcms_PixelFormat_BGRA_1010102,
222
223 skcms_PixelFormat_RGB_161616LE, // Little-endian. Pointers must be 16-bit aligned.
224 skcms_PixelFormat_BGR_161616LE,
225 skcms_PixelFormat_RGBA_16161616LE,
226 skcms_PixelFormat_BGRA_16161616LE,
227
228 skcms_PixelFormat_RGB_161616BE, // Big-endian. Pointers must be 16-bit aligned.
229 skcms_PixelFormat_BGR_161616BE,
230 skcms_PixelFormat_RGBA_16161616BE,
231 skcms_PixelFormat_BGRA_16161616BE,
232
233 skcms_PixelFormat_RGB_hhh_Norm, // 1-5-10 half-precision float in [0,1]
234 skcms_PixelFormat_BGR_hhh_Norm, // Pointers must be 16-bit aligned.
235 skcms_PixelFormat_RGBA_hhhh_Norm,
236 skcms_PixelFormat_BGRA_hhhh_Norm,
237
238 skcms_PixelFormat_RGB_hhh, // 1-5-10 half-precision float.
239 skcms_PixelFormat_BGR_hhh, // Pointers must be 16-bit aligned.
240 skcms_PixelFormat_RGBA_hhhh,
241 skcms_PixelFormat_BGRA_hhhh,
242
243 skcms_PixelFormat_RGB_fff, // 1-8-23 single-precision float (the normal kind).
244 skcms_PixelFormat_BGR_fff, // Pointers must be 32-bit aligned.
245 skcms_PixelFormat_RGBA_ffff,
246 skcms_PixelFormat_BGRA_ffff,
247 } skcms_PixelFormat;
248
249 // We always store any alpha channel linearly. In the chart below, tf-1() is the inverse
250 // transfer function for the given color profile (applying the transfer function linearizes).
251
252 // We treat opaque as a strong requirement, not just a performance hint: we will ignore
253 // any source alpha and treat it as 1.0, and will make sure that any destination alpha
254 // channel is filled with the equivalent of 1.0.
255
256 // We used to offer multiple types of premultiplication, but now just one, PremulAsEncoded.
257 // This is the premul you're probably used to working with.
258
259 typedef enum skcms_AlphaFormat {
260 skcms_AlphaFormat_Opaque, // alpha is always opaque
261 // tf-1(r), tf-1(g), tf-1(b), 1.0
262 skcms_AlphaFormat_Unpremul, // alpha and color are unassociated
263 // tf-1(r), tf-1(g), tf-1(b), a
264 skcms_AlphaFormat_PremulAsEncoded, // premultiplied while encoded
265 // tf-1(r)*a, tf-1(g)*a, tf-1(b)*a, a
266 } skcms_AlphaFormat;
267
268 // Convert npixels pixels from src format and color profile to dst format and color profile
269 // and return true, otherwise return false. It is safe to alias dst == src if dstFmt == srcFmt.
270 SKCMS_API bool skcms_Transform(const void* src,
271 skcms_PixelFormat srcFmt,
272 skcms_AlphaFormat srcAlpha,
273 const skcms_ICCProfile* srcProfile,
274 void* dst,
275 skcms_PixelFormat dstFmt,
276 skcms_AlphaFormat dstAlpha,
277 const skcms_ICCProfile* dstProfile,
278 size_t npixels);
279
280 // As skcms_Transform(), supporting srcFmts with a palette.
281 SKCMS_API bool skcms_TransformWithPalette(const void* src,
282 skcms_PixelFormat srcFmt,
283 skcms_AlphaFormat srcAlpha,
284 const skcms_ICCProfile* srcProfile,
285 void* dst,
286 skcms_PixelFormat dstFmt,
287 skcms_AlphaFormat dstAlpha,
288 const skcms_ICCProfile* dstProfile,
289 size_t npixels,
290 const void* palette);
291
292 // If profile can be used as a destination in skcms_Transform, return true. Otherwise, attempt to
293 // rewrite it with approximations where reasonable. If successful, return true. If no reasonable
294 // approximation exists, leave the profile unchanged and return false.
295 SKCMS_API bool skcms_MakeUsableAsDestination(skcms_ICCProfile* profile);
296
297 // If profile can be used as a destination with a single parametric transfer function (ie for
298 // rasterization), return true. Otherwise, attempt to rewrite it with approximations where
299 // reasonable. If successful, return true. If no reasonable approximation exists, leave the
300 // profile unchanged and return false.
301 SKCMS_API bool skcms_MakeUsableAsDestinationWithSingleCurve(skcms_ICCProfile* profile);
302
303 SKCMS_API bool skcms_PrimariesToXYZD50(float rx, float ry,
304 float gx, float gy,
305 float bx, float by,
306 float wx, float wy,
307 skcms_Matrix3x3* toXYZD50);
308
309 // Utilities for programmatically constructing profiles
skcms_Init(skcms_ICCProfile * p)310 static inline void skcms_Init(skcms_ICCProfile* p) {
311 memset(p, 0, sizeof(*p));
312 p->data_color_space = skcms_Signature_RGB;
313 p->pcs = skcms_Signature_XYZ;
314 }
315
skcms_SetTransferFunction(skcms_ICCProfile * p,const skcms_TransferFunction * tf)316 static inline void skcms_SetTransferFunction(skcms_ICCProfile* p,
317 const skcms_TransferFunction* tf) {
318 p->has_trc = true;
319 for (int i = 0; i < 3; ++i) {
320 p->trc[i].table_entries = 0;
321 p->trc[i].parametric = *tf;
322 }
323 }
324
skcms_SetXYZD50(skcms_ICCProfile * p,const skcms_Matrix3x3 * m)325 static inline void skcms_SetXYZD50(skcms_ICCProfile* p, const skcms_Matrix3x3* m) {
326 p->has_toXYZD50 = true;
327 p->toXYZD50 = *m;
328 }
329
330 #ifdef __cplusplus
331 }
332 #endif
333