• 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 //            { 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 SKCMS_API bool skcms_GetCHAD(const skcms_ICCProfile*, skcms_Matrix3x3*);
180 SKCMS_API bool skcms_GetWTPT(const skcms_ICCProfile*, float xyz[3]);
181 
182 // These are common ICC signature values
183 enum {
184     // data_color_space
185     skcms_Signature_CMYK = 0x434D594B,
186     skcms_Signature_Gray = 0x47524159,
187     skcms_Signature_RGB  = 0x52474220,
188 
189     // pcs
190     skcms_Signature_Lab  = 0x4C616220,
191     skcms_Signature_XYZ  = 0x58595A20,
192 };
193 
194 typedef enum skcms_PixelFormat {
195     skcms_PixelFormat_A_8,
196     skcms_PixelFormat_A_8_,
197     skcms_PixelFormat_G_8,
198     skcms_PixelFormat_G_8_,
199     skcms_PixelFormat_RGBA_8888_Palette8,
200     skcms_PixelFormat_BGRA_8888_Palette8,
201 
202     skcms_PixelFormat_RGB_565,
203     skcms_PixelFormat_BGR_565,
204 
205     skcms_PixelFormat_ABGR_4444,
206     skcms_PixelFormat_ARGB_4444,
207 
208     skcms_PixelFormat_RGB_888,
209     skcms_PixelFormat_BGR_888,
210     skcms_PixelFormat_RGBA_8888,
211     skcms_PixelFormat_BGRA_8888,
212 
213     skcms_PixelFormat_RGBA_1010102,
214     skcms_PixelFormat_BGRA_1010102,
215 
216     skcms_PixelFormat_RGB_161616LE,     // Little-endian.  Pointers must be 16-bit aligned.
217     skcms_PixelFormat_BGR_161616LE,
218     skcms_PixelFormat_RGBA_16161616LE,
219     skcms_PixelFormat_BGRA_16161616LE,
220 
221     skcms_PixelFormat_RGB_161616BE,     // Big-endian.  Pointers must be 16-bit aligned.
222     skcms_PixelFormat_BGR_161616BE,
223     skcms_PixelFormat_RGBA_16161616BE,
224     skcms_PixelFormat_BGRA_16161616BE,
225 
226     skcms_PixelFormat_RGB_hhh_Norm,   // 1-5-10 half-precision float in [0,1]
227     skcms_PixelFormat_BGR_hhh_Norm,   // Pointers must be 16-bit aligned.
228     skcms_PixelFormat_RGBA_hhhh_Norm,
229     skcms_PixelFormat_BGRA_hhhh_Norm,
230 
231     skcms_PixelFormat_RGB_hhh,        // 1-5-10 half-precision float.
232     skcms_PixelFormat_BGR_hhh,        // Pointers must be 16-bit aligned.
233     skcms_PixelFormat_RGBA_hhhh,
234     skcms_PixelFormat_BGRA_hhhh,
235 
236     skcms_PixelFormat_RGB_fff,        // 1-8-23 single-precision float (the normal kind).
237     skcms_PixelFormat_BGR_fff,        // Pointers must be 32-bit aligned.
238     skcms_PixelFormat_RGBA_ffff,
239     skcms_PixelFormat_BGRA_ffff,
240 } skcms_PixelFormat;
241 
242 // We always store any alpha channel linearly.  In the chart below, tf-1() is the inverse
243 // transfer function for the given color profile (applying the transfer function linearizes).
244 
245 // We treat opaque as a strong requirement, not just a performance hint: we will ignore
246 // any source alpha and treat it as 1.0, and will make sure that any destination alpha
247 // channel is filled with the equivalent of 1.0.
248 
249 // We used to offer multiple types of premultiplication, but now just one, PremulAsEncoded.
250 // This is the premul you're probably used to working with.
251 
252 typedef enum skcms_AlphaFormat {
253     skcms_AlphaFormat_Opaque,          // alpha is always opaque
254                                        //   tf-1(r),   tf-1(g),   tf-1(b),   1.0
255     skcms_AlphaFormat_Unpremul,        // alpha and color are unassociated
256                                        //   tf-1(r),   tf-1(g),   tf-1(b),   a
257     skcms_AlphaFormat_PremulAsEncoded, // premultiplied while encoded
258                                        //   tf-1(r)*a, tf-1(g)*a, tf-1(b)*a, a
259 } skcms_AlphaFormat;
260 
261 // Convert npixels pixels from src format and color profile to dst format and color profile
262 // and return true, otherwise return false.  It is safe to alias dst == src if dstFmt == srcFmt.
263 SKCMS_API bool skcms_Transform(const void*             src,
264                                skcms_PixelFormat       srcFmt,
265                                skcms_AlphaFormat       srcAlpha,
266                                const skcms_ICCProfile* srcProfile,
267                                void*                   dst,
268                                skcms_PixelFormat       dstFmt,
269                                skcms_AlphaFormat       dstAlpha,
270                                const skcms_ICCProfile* dstProfile,
271                                size_t                  npixels);
272 
273 // As skcms_Transform(), supporting srcFmts with a palette.
274 SKCMS_API bool skcms_TransformWithPalette(const void*             src,
275                                           skcms_PixelFormat       srcFmt,
276                                           skcms_AlphaFormat       srcAlpha,
277                                           const skcms_ICCProfile* srcProfile,
278                                           void*                   dst,
279                                           skcms_PixelFormat       dstFmt,
280                                           skcms_AlphaFormat       dstAlpha,
281                                           const skcms_ICCProfile* dstProfile,
282                                           size_t                  npixels,
283                                           const void*             palette);
284 
285 // If profile can be used as a destination in skcms_Transform, return true. Otherwise, attempt to
286 // rewrite it with approximations where reasonable. If successful, return true. If no reasonable
287 // approximation exists, leave the profile unchanged and return false.
288 SKCMS_API bool skcms_MakeUsableAsDestination(skcms_ICCProfile* profile);
289 
290 // If profile can be used as a destination with a single parametric transfer function (ie for
291 // rasterization), return true. Otherwise, attempt to rewrite it with approximations where
292 // reasonable. If successful, return true. If no reasonable approximation exists, leave the
293 // profile unchanged and return false.
294 SKCMS_API bool skcms_MakeUsableAsDestinationWithSingleCurve(skcms_ICCProfile* profile);
295 
296 // Returns a matrix to adapt XYZ color from given the whitepoint to D50.
297 SKCMS_API bool skcms_AdaptToXYZD50(float wx, float wy,
298                                    skcms_Matrix3x3* toXYZD50);
299 
300 // Returns a matrix to convert RGB color into XYZ adapted to D50, given the
301 // primaries and whitepoint of the RGB model.
302 SKCMS_API bool skcms_PrimariesToXYZD50(float rx, float ry,
303                                        float gx, float gy,
304                                        float bx, float by,
305                                        float wx, float wy,
306                                        skcms_Matrix3x3* toXYZD50);
307 
308 // Utilities for programmatically constructing profiles
skcms_Init(skcms_ICCProfile * p)309 static inline void skcms_Init(skcms_ICCProfile* p) {
310     memset(p, 0, sizeof(*p));
311     p->data_color_space = skcms_Signature_RGB;
312     p->pcs = skcms_Signature_XYZ;
313 }
314 
skcms_SetTransferFunction(skcms_ICCProfile * p,const skcms_TransferFunction * tf)315 static inline void skcms_SetTransferFunction(skcms_ICCProfile* p,
316                                              const skcms_TransferFunction* tf) {
317     p->has_trc = true;
318     for (int i = 0; i < 3; ++i) {
319         p->trc[i].table_entries = 0;
320         p->trc[i].parametric = *tf;
321     }
322 }
323 
skcms_SetXYZD50(skcms_ICCProfile * p,const skcms_Matrix3x3 * m)324 static inline void skcms_SetXYZD50(skcms_ICCProfile* p, const skcms_Matrix3x3* m) {
325     p->has_toXYZD50 = true;
326     p->toXYZD50 = *m;
327 }
328 
329 #ifdef __cplusplus
330 }
331 #endif
332