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