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