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]. |
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]. |
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 | // Is this an ordinary sRGB-ish transfer function, or one of the HDR forms we support? |
83 | SKCMS_API bool skcms_TransferFunction_isSRGBish(const skcms_TransferFunction*); |
84 | SKCMS_API bool skcms_TransferFunction_isPQish (const skcms_TransferFunction*); |
85 | SKCMS_API bool skcms_TransferFunction_isHLGish (const skcms_TransferFunction*); |
86 | |
87 | // Unified representation of 'curv' or 'para' tag data, or a 1D table from 'mft1' or 'mft2' |
88 | typedef union skcms_Curve { |
89 | struct { |
90 | uint32_t alias_of_table_entries; |
91 | skcms_TransferFunction parametric; |
92 | }; |
93 | struct { |
94 | uint32_t table_entries; |
95 | const uint8_t* table_8; |
96 | const uint8_t* table_16; |
97 | }; |
98 | } skcms_Curve; |
99 | |
100 | typedef struct skcms_A2B { |
101 | // Optional: N 1D curves, followed by an N-dimensional CLUT. |
102 | // If input_channels == 0, these curves and CLUT are skipped, |
103 | // Otherwise, input_channels must be in [1, 4]. |
104 | uint32_t input_channels; |
105 | skcms_Curve input_curves[4]; |
106 | uint8_t grid_points[4]; |
107 | const uint8_t* grid_8; |
108 | const uint8_t* grid_16; |
109 | |
110 | // Optional: 3 1D curves, followed by a color matrix. |
111 | // If matrix_channels == 0, these curves and matrix are skipped, |
112 | // Otherwise, matrix_channels must be 3. |
113 | uint32_t matrix_channels; |
114 | skcms_Curve matrix_curves[3]; |
115 | skcms_Matrix3x4 matrix; |
116 | |
117 | // Required: 3 1D curves. Always present, and output_channels must be 3. |
118 | uint32_t output_channels; |
119 | skcms_Curve output_curves[3]; |
120 | } skcms_A2B; |
121 | |
122 | typedef struct skcms_ICCProfile { |
123 | const uint8_t* buffer; |
124 | |
125 | uint32_t size; |
126 | uint32_t data_color_space; |
127 | uint32_t pcs; |
128 | uint32_t tag_count; |
129 | |
130 | // skcms_Parse() will set commonly-used fields for you when possible: |
131 | |
132 | // If we can parse red, green and blue transfer curves from the profile, |
133 | // trc will be set to those three curves, and has_trc will be true. |
134 | bool has_trc; |
135 | skcms_Curve trc[3]; |
136 | |
137 | // If this profile's gamut can be represented by a 3x3 transform to XYZD50, |
138 | // skcms_Parse() sets toXYZD50 to that transform and has_toXYZD50 to true. |
139 | bool has_toXYZD50; |
140 | skcms_Matrix3x3 toXYZD50; |
141 | |
142 | // If the profile has a valid A2B0 tag, skcms_Parse() sets A2B to that data, |
143 | // and has_A2B to true. |
144 | bool has_A2B; |
145 | skcms_A2B A2B; |
146 | } skcms_ICCProfile; |
147 | |
148 | // The sRGB color profile is so commonly used that we offer a canonical skcms_ICCProfile for it. |
149 | SKCMS_API const skcms_ICCProfile* skcms_sRGB_profile(void); |
150 | // Ditto for XYZD50, the most common profile connection space. |
151 | SKCMS_API const skcms_ICCProfile* skcms_XYZD50_profile(void); |
152 | |
153 | SKCMS_API const skcms_TransferFunction* skcms_sRGB_TransferFunction(void); |
154 | SKCMS_API const skcms_TransferFunction* skcms_sRGB_Inverse_TransferFunction(void); |
155 | SKCMS_API const skcms_TransferFunction* skcms_Identity_TransferFunction(void); |
156 | |
157 | // Practical equality test for two skcms_ICCProfiles. |
158 | // The implementation is subject to change, but it will always try to answer |
159 | // "can I substitute A for B?" and "can I skip transforming from A to B?". |
160 | SKCMS_API bool skcms_ApproximatelyEqualProfiles(const skcms_ICCProfile* A, |
161 | const skcms_ICCProfile* B); |
162 | |
163 | // Practical test that answers: Is curve roughly the inverse of inv_tf? Typically used by passing |
164 | // the inverse of a known parametric transfer function (like sRGB), to determine if a particular |
165 | // curve is very close to sRGB. |
166 | SKCMS_API bool skcms_AreApproximateInverses(const skcms_Curve* curve, |
167 | const skcms_TransferFunction* inv_tf); |
168 | |
169 | // Similar to above, answering the question for all three TRC curves of the given profile. Again, |
170 | // passing skcms_sRGB_InverseTransferFunction as inv_tf will answer the question: |
171 | // "Does this profile have a transfer function that is very close to sRGB?" |
172 | SKCMS_API bool skcms_TRCs_AreApproximateInverse(const skcms_ICCProfile* profile, |
173 | const skcms_TransferFunction* inv_tf); |
174 | |
175 | // Parse an ICC profile and return true if possible, otherwise return false. |
176 | // The buffer is not copied, it must remain valid as long as the skcms_ICCProfile |
177 | // will be used. |
178 | SKCMS_API bool skcms_Parse(const void*, size_t, skcms_ICCProfile*); |
179 | |
180 | SKCMS_API bool skcms_ApproximateCurve(const skcms_Curve* curve, |
181 | skcms_TransferFunction* approx, |
182 | float* max_error); |
183 | |
184 | SKCMS_API bool skcms_GetCHAD(const skcms_ICCProfile*, skcms_Matrix3x3*); |
185 | SKCMS_API bool skcms_GetWTPT(const skcms_ICCProfile*, float xyz[3]); |
186 | |
187 | // These are common ICC signature values |
188 | enum { |
189 | // data_color_space |
190 | skcms_Signature_CMYK = 0x434D594B, |
191 | skcms_Signature_Gray = 0x47524159, |
192 | skcms_Signature_RGB = 0x52474220, |
193 | |
194 | // pcs |
195 | skcms_Signature_Lab = 0x4C616220, |
196 | skcms_Signature_XYZ = 0x58595A20, |
197 | }; |
198 | |
199 | typedef enum skcms_PixelFormat { |
200 | skcms_PixelFormat_A_8, |
201 | skcms_PixelFormat_A_8_, |
202 | skcms_PixelFormat_G_8, |
203 | skcms_PixelFormat_G_8_, |
204 | skcms_PixelFormat_RGBA_8888_Palette8, |
205 | skcms_PixelFormat_BGRA_8888_Palette8, |
206 | |
207 | skcms_PixelFormat_RGB_565, |
208 | skcms_PixelFormat_BGR_565, |
209 | |
210 | skcms_PixelFormat_ABGR_4444, |
211 | skcms_PixelFormat_ARGB_4444, |
212 | |
213 | skcms_PixelFormat_RGB_888, |
214 | skcms_PixelFormat_BGR_888, |
215 | skcms_PixelFormat_RGBA_8888, |
216 | skcms_PixelFormat_BGRA_8888, |
217 | skcms_PixelFormat_RGBA_8888_sRGB, // Automatic sRGB encoding / decoding. |
218 | skcms_PixelFormat_BGRA_8888_sRGB, // (Generally used with linear transfer functions.) |
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 | // Returns a matrix to adapt XYZ color from given the whitepoint to D50. |
304 | SKCMS_API bool skcms_AdaptToXYZD50(float wx, float wy, |
305 | skcms_Matrix3x3* toXYZD50); |
306 | |
307 | // Returns a matrix to convert RGB color into XYZ adapted to D50, given the |
308 | // primaries and whitepoint of the RGB model. |
309 | SKCMS_API bool skcms_PrimariesToXYZD50(float rx, float ry, |
310 | float gx, float gy, |
311 | float bx, float by, |
312 | float wx, float wy, |
313 | skcms_Matrix3x3* toXYZD50); |
314 | |
315 | // Call before your first call to skcms_Transform() to skip runtime CPU detection. |
316 | SKCMS_API void skcms_DisableRuntimeCPUDetection(void); |
317 | |
318 | // Utilities for programmatically constructing profiles |
319 | static inline void skcms_Init(skcms_ICCProfile* p) { |
320 | memset(p, 0, sizeof(*p)); |
321 | p->data_color_space = skcms_Signature_RGB; |
322 | p->pcs = skcms_Signature_XYZ; |
323 | } |
324 | |
325 | static inline void skcms_SetTransferFunction(skcms_ICCProfile* p, |
326 | const skcms_TransferFunction* tf) { |
327 | p->has_trc = true; |
328 | for (int i = 0; i < 3; ++i) { |
329 | p->trc[i].table_entries = 0; |
330 | p->trc[i].parametric = *tf; |
331 | } |
332 | } |
333 | |
334 | static inline void skcms_SetXYZD50(skcms_ICCProfile* p, const skcms_Matrix3x3* m) { |
335 | p->has_toXYZD50 = true; |
336 | p->toXYZD50 = *m; |
337 | } |
338 | |
339 | #ifdef __cplusplus |
340 | } |
341 | #endif |
342 | |