1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2018 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker #pragma once
9*c8dee2aaSAndroid Build Coastguard Worker
10*c8dee2aaSAndroid Build Coastguard Worker // skcms_public.h contains the entire public API for skcms.
11*c8dee2aaSAndroid Build Coastguard Worker
12*c8dee2aaSAndroid Build Coastguard Worker #ifndef SKCMS_API
13*c8dee2aaSAndroid Build Coastguard Worker #define SKCMS_API
14*c8dee2aaSAndroid Build Coastguard Worker #endif
15*c8dee2aaSAndroid Build Coastguard Worker
16*c8dee2aaSAndroid Build Coastguard Worker #include <stdbool.h>
17*c8dee2aaSAndroid Build Coastguard Worker #include <stddef.h>
18*c8dee2aaSAndroid Build Coastguard Worker #include <stdint.h>
19*c8dee2aaSAndroid Build Coastguard Worker #include <string.h>
20*c8dee2aaSAndroid Build Coastguard Worker
21*c8dee2aaSAndroid Build Coastguard Worker #ifdef __cplusplus
22*c8dee2aaSAndroid Build Coastguard Worker extern "C" {
23*c8dee2aaSAndroid Build Coastguard Worker #endif
24*c8dee2aaSAndroid Build Coastguard Worker
25*c8dee2aaSAndroid Build Coastguard Worker // A row-major 3x3 matrix (ie vals[row][col])
26*c8dee2aaSAndroid Build Coastguard Worker typedef struct skcms_Matrix3x3 {
27*c8dee2aaSAndroid Build Coastguard Worker float vals[3][3];
28*c8dee2aaSAndroid Build Coastguard Worker } skcms_Matrix3x3;
29*c8dee2aaSAndroid Build Coastguard Worker
30*c8dee2aaSAndroid Build Coastguard Worker // It is _not_ safe to alias the pointers to invert in-place.
31*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_Matrix3x3_invert(const skcms_Matrix3x3*, skcms_Matrix3x3*);
32*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API skcms_Matrix3x3 skcms_Matrix3x3_concat(const skcms_Matrix3x3*, const skcms_Matrix3x3*);
33*c8dee2aaSAndroid Build Coastguard Worker
34*c8dee2aaSAndroid Build Coastguard Worker // A row-major 3x4 matrix (ie vals[row][col])
35*c8dee2aaSAndroid Build Coastguard Worker typedef struct skcms_Matrix3x4 {
36*c8dee2aaSAndroid Build Coastguard Worker float vals[3][4];
37*c8dee2aaSAndroid Build Coastguard Worker } skcms_Matrix3x4;
38*c8dee2aaSAndroid Build Coastguard Worker
39*c8dee2aaSAndroid Build Coastguard Worker // A transfer function mapping encoded values to linear values,
40*c8dee2aaSAndroid Build Coastguard Worker // represented by this 7-parameter piecewise function:
41*c8dee2aaSAndroid Build Coastguard Worker //
42*c8dee2aaSAndroid Build Coastguard Worker // linear = sign(encoded) * (c*|encoded| + f) , 0 <= |encoded| < d
43*c8dee2aaSAndroid Build Coastguard Worker // = sign(encoded) * ((a*|encoded| + b)^g + e), d <= |encoded|
44*c8dee2aaSAndroid Build Coastguard Worker //
45*c8dee2aaSAndroid Build Coastguard Worker // (A simple gamma transfer function sets g to gamma and a to 1.)
46*c8dee2aaSAndroid Build Coastguard Worker typedef struct skcms_TransferFunction {
47*c8dee2aaSAndroid Build Coastguard Worker float g, a,b,c,d,e,f;
48*c8dee2aaSAndroid Build Coastguard Worker } skcms_TransferFunction;
49*c8dee2aaSAndroid Build Coastguard Worker
50*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API float skcms_TransferFunction_eval (const skcms_TransferFunction*, float);
51*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_TransferFunction_invert(const skcms_TransferFunction*,
52*c8dee2aaSAndroid Build Coastguard Worker skcms_TransferFunction*);
53*c8dee2aaSAndroid Build Coastguard Worker
54*c8dee2aaSAndroid Build Coastguard Worker typedef enum skcms_TFType {
55*c8dee2aaSAndroid Build Coastguard Worker skcms_TFType_Invalid,
56*c8dee2aaSAndroid Build Coastguard Worker skcms_TFType_sRGBish,
57*c8dee2aaSAndroid Build Coastguard Worker skcms_TFType_PQish,
58*c8dee2aaSAndroid Build Coastguard Worker skcms_TFType_HLGish,
59*c8dee2aaSAndroid Build Coastguard Worker skcms_TFType_HLGinvish,
60*c8dee2aaSAndroid Build Coastguard Worker } skcms_TFType;
61*c8dee2aaSAndroid Build Coastguard Worker
62*c8dee2aaSAndroid Build Coastguard Worker // Identify which kind of transfer function is encoded in an skcms_TransferFunction
63*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API skcms_TFType skcms_TransferFunction_getType(const skcms_TransferFunction*);
64*c8dee2aaSAndroid Build Coastguard Worker
65*c8dee2aaSAndroid Build Coastguard Worker // We can jam a couple alternate transfer function forms into skcms_TransferFunction,
66*c8dee2aaSAndroid Build Coastguard Worker // including those matching the general forms of the SMPTE ST 2084 PQ function or HLG.
67*c8dee2aaSAndroid Build Coastguard Worker //
68*c8dee2aaSAndroid Build Coastguard Worker // PQish:
69*c8dee2aaSAndroid Build Coastguard Worker // max(A + B|encoded|^C, 0)
70*c8dee2aaSAndroid Build Coastguard Worker // linear = sign(encoded) * (------------------------) ^ F
71*c8dee2aaSAndroid Build Coastguard Worker // D + E|encoded|^C
72*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_TransferFunction_makePQish(skcms_TransferFunction*,
73*c8dee2aaSAndroid Build Coastguard Worker float A, float B, float C,
74*c8dee2aaSAndroid Build Coastguard Worker float D, float E, float F);
75*c8dee2aaSAndroid Build Coastguard Worker // HLGish:
76*c8dee2aaSAndroid Build Coastguard Worker // { K * sign(encoded) * ( (R|encoded|)^G ) when 0 <= |encoded| <= 1/R
77*c8dee2aaSAndroid Build Coastguard Worker // linear = { K * sign(encoded) * ( e^(a(|encoded|-c)) + b ) when 1/R < |encoded|
78*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_TransferFunction_makeScaledHLGish(skcms_TransferFunction*,
79*c8dee2aaSAndroid Build Coastguard Worker float K, float R, float G,
80*c8dee2aaSAndroid Build Coastguard Worker float a, float b, float c);
81*c8dee2aaSAndroid Build Coastguard Worker
82*c8dee2aaSAndroid Build Coastguard Worker // Compatibility shim with K=1 for old callers.
skcms_TransferFunction_makeHLGish(skcms_TransferFunction * fn,float R,float G,float a,float b,float c)83*c8dee2aaSAndroid Build Coastguard Worker static inline bool skcms_TransferFunction_makeHLGish(skcms_TransferFunction* fn,
84*c8dee2aaSAndroid Build Coastguard Worker float R, float G,
85*c8dee2aaSAndroid Build Coastguard Worker float a, float b, float c) {
86*c8dee2aaSAndroid Build Coastguard Worker return skcms_TransferFunction_makeScaledHLGish(fn, 1.0f, R,G, a,b,c);
87*c8dee2aaSAndroid Build Coastguard Worker }
88*c8dee2aaSAndroid Build Coastguard Worker
89*c8dee2aaSAndroid Build Coastguard Worker // PQ mapping encoded [0,1] to linear [0,1].
skcms_TransferFunction_makePQ(skcms_TransferFunction * tf)90*c8dee2aaSAndroid Build Coastguard Worker static inline bool skcms_TransferFunction_makePQ(skcms_TransferFunction* tf) {
91*c8dee2aaSAndroid Build Coastguard Worker return skcms_TransferFunction_makePQish(tf, -107/128.0f, 1.0f, 32/2523.0f
92*c8dee2aaSAndroid Build Coastguard Worker , 2413/128.0f, -2392/128.0f, 8192/1305.0f);
93*c8dee2aaSAndroid Build Coastguard Worker }
94*c8dee2aaSAndroid Build Coastguard Worker // HLG mapping encoded [0,1] to linear [0,12].
skcms_TransferFunction_makeHLG(skcms_TransferFunction * tf)95*c8dee2aaSAndroid Build Coastguard Worker static inline bool skcms_TransferFunction_makeHLG(skcms_TransferFunction* tf) {
96*c8dee2aaSAndroid Build Coastguard Worker return skcms_TransferFunction_makeHLGish(tf, 2.0f, 2.0f
97*c8dee2aaSAndroid Build Coastguard Worker , 1/0.17883277f, 0.28466892f, 0.55991073f);
98*c8dee2aaSAndroid Build Coastguard Worker }
99*c8dee2aaSAndroid Build Coastguard Worker
100*c8dee2aaSAndroid Build Coastguard Worker // Is this an ordinary sRGB-ish transfer function, or one of the HDR forms we support?
101*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_TransferFunction_isSRGBish(const skcms_TransferFunction*);
102*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_TransferFunction_isPQish (const skcms_TransferFunction*);
103*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_TransferFunction_isHLGish (const skcms_TransferFunction*);
104*c8dee2aaSAndroid Build Coastguard Worker
105*c8dee2aaSAndroid Build Coastguard Worker // Unified representation of 'curv' or 'para' tag data, or a 1D table from 'mft1' or 'mft2'
106*c8dee2aaSAndroid Build Coastguard Worker typedef union skcms_Curve {
107*c8dee2aaSAndroid Build Coastguard Worker struct {
108*c8dee2aaSAndroid Build Coastguard Worker // this needs to line up with alias_of_table_entries so we can tell if there are or
109*c8dee2aaSAndroid Build Coastguard Worker // are not table entries. If this is 0, this struct is a parametric function,
110*c8dee2aaSAndroid Build Coastguard Worker // otherwise it's a table entry.
111*c8dee2aaSAndroid Build Coastguard Worker uint32_t alias_of_table_entries;
112*c8dee2aaSAndroid Build Coastguard Worker skcms_TransferFunction parametric;
113*c8dee2aaSAndroid Build Coastguard Worker };
114*c8dee2aaSAndroid Build Coastguard Worker struct {
115*c8dee2aaSAndroid Build Coastguard Worker uint32_t table_entries;
116*c8dee2aaSAndroid Build Coastguard Worker const uint8_t* table_8;
117*c8dee2aaSAndroid Build Coastguard Worker const uint8_t* table_16;
118*c8dee2aaSAndroid Build Coastguard Worker };
119*c8dee2aaSAndroid Build Coastguard Worker } skcms_Curve;
120*c8dee2aaSAndroid Build Coastguard Worker
121*c8dee2aaSAndroid Build Coastguard Worker // Complex transforms between device space (A) and profile connection space (B):
122*c8dee2aaSAndroid Build Coastguard Worker // A2B: device -> [ "A" curves -> CLUT ] -> [ "M" curves -> matrix ] -> "B" curves -> PCS
123*c8dee2aaSAndroid Build Coastguard Worker // B2A: device <- [ "A" curves <- CLUT ] <- [ "M" curves <- matrix ] <- "B" curves <- PCS
124*c8dee2aaSAndroid Build Coastguard Worker
125*c8dee2aaSAndroid Build Coastguard Worker typedef struct skcms_A2B {
126*c8dee2aaSAndroid Build Coastguard Worker // Optional: N 1D "A" curves, followed by an N-dimensional CLUT.
127*c8dee2aaSAndroid Build Coastguard Worker // If input_channels == 0, these curves and CLUT are skipped,
128*c8dee2aaSAndroid Build Coastguard Worker // Otherwise, input_channels must be in [1, 4].
129*c8dee2aaSAndroid Build Coastguard Worker skcms_Curve input_curves[4];
130*c8dee2aaSAndroid Build Coastguard Worker const uint8_t* grid_8;
131*c8dee2aaSAndroid Build Coastguard Worker const uint8_t* grid_16;
132*c8dee2aaSAndroid Build Coastguard Worker uint32_t input_channels;
133*c8dee2aaSAndroid Build Coastguard Worker uint8_t grid_points[4];
134*c8dee2aaSAndroid Build Coastguard Worker
135*c8dee2aaSAndroid Build Coastguard Worker // Optional: 3 1D "M" curves, followed by a color matrix.
136*c8dee2aaSAndroid Build Coastguard Worker // If matrix_channels == 0, these curves and matrix are skipped,
137*c8dee2aaSAndroid Build Coastguard Worker // Otherwise, matrix_channels must be 3.
138*c8dee2aaSAndroid Build Coastguard Worker skcms_Curve matrix_curves[3];
139*c8dee2aaSAndroid Build Coastguard Worker skcms_Matrix3x4 matrix;
140*c8dee2aaSAndroid Build Coastguard Worker uint32_t matrix_channels;
141*c8dee2aaSAndroid Build Coastguard Worker
142*c8dee2aaSAndroid Build Coastguard Worker // Required: 3 1D "B" curves. Always present, and output_channels must be 3.
143*c8dee2aaSAndroid Build Coastguard Worker uint32_t output_channels; // list first to pack with matrix_channels
144*c8dee2aaSAndroid Build Coastguard Worker skcms_Curve output_curves[3];
145*c8dee2aaSAndroid Build Coastguard Worker } skcms_A2B;
146*c8dee2aaSAndroid Build Coastguard Worker
147*c8dee2aaSAndroid Build Coastguard Worker typedef struct skcms_B2A {
148*c8dee2aaSAndroid Build Coastguard Worker // Required: 3 1D "B" curves. Always present, and input_channels must be 3.
149*c8dee2aaSAndroid Build Coastguard Worker skcms_Curve input_curves[3];
150*c8dee2aaSAndroid Build Coastguard Worker uint32_t input_channels;
151*c8dee2aaSAndroid Build Coastguard Worker
152*c8dee2aaSAndroid Build Coastguard Worker // Optional: a color matrix, followed by 3 1D "M" curves.
153*c8dee2aaSAndroid Build Coastguard Worker // If matrix_channels == 0, this matrix and these curves are skipped,
154*c8dee2aaSAndroid Build Coastguard Worker // Otherwise, matrix_channels must be 3.
155*c8dee2aaSAndroid Build Coastguard Worker uint32_t matrix_channels; // list first to pack with input_channels
156*c8dee2aaSAndroid Build Coastguard Worker skcms_Curve matrix_curves[3];
157*c8dee2aaSAndroid Build Coastguard Worker skcms_Matrix3x4 matrix;
158*c8dee2aaSAndroid Build Coastguard Worker
159*c8dee2aaSAndroid Build Coastguard Worker // Optional: an N-dimensional CLUT, followed by N 1D "A" curves.
160*c8dee2aaSAndroid Build Coastguard Worker // If output_channels == 0, this CLUT and these curves are skipped,
161*c8dee2aaSAndroid Build Coastguard Worker // Otherwise, output_channels must be in [1, 4].
162*c8dee2aaSAndroid Build Coastguard Worker skcms_Curve output_curves[4];
163*c8dee2aaSAndroid Build Coastguard Worker const uint8_t* grid_8;
164*c8dee2aaSAndroid Build Coastguard Worker const uint8_t* grid_16;
165*c8dee2aaSAndroid Build Coastguard Worker uint8_t grid_points[4];
166*c8dee2aaSAndroid Build Coastguard Worker uint32_t output_channels;
167*c8dee2aaSAndroid Build Coastguard Worker } skcms_B2A;
168*c8dee2aaSAndroid Build Coastguard Worker
169*c8dee2aaSAndroid Build Coastguard Worker typedef struct skcms_CICP {
170*c8dee2aaSAndroid Build Coastguard Worker uint8_t color_primaries;
171*c8dee2aaSAndroid Build Coastguard Worker uint8_t transfer_characteristics;
172*c8dee2aaSAndroid Build Coastguard Worker uint8_t matrix_coefficients;
173*c8dee2aaSAndroid Build Coastguard Worker uint8_t video_full_range_flag;
174*c8dee2aaSAndroid Build Coastguard Worker } skcms_CICP;
175*c8dee2aaSAndroid Build Coastguard Worker
176*c8dee2aaSAndroid Build Coastguard Worker typedef struct skcms_ICCProfile {
177*c8dee2aaSAndroid Build Coastguard Worker const uint8_t* buffer;
178*c8dee2aaSAndroid Build Coastguard Worker
179*c8dee2aaSAndroid Build Coastguard Worker uint32_t size;
180*c8dee2aaSAndroid Build Coastguard Worker uint32_t data_color_space;
181*c8dee2aaSAndroid Build Coastguard Worker uint32_t pcs;
182*c8dee2aaSAndroid Build Coastguard Worker uint32_t tag_count;
183*c8dee2aaSAndroid Build Coastguard Worker
184*c8dee2aaSAndroid Build Coastguard Worker // skcms_Parse() will set commonly-used fields for you when possible:
185*c8dee2aaSAndroid Build Coastguard Worker
186*c8dee2aaSAndroid Build Coastguard Worker // If we can parse red, green and blue transfer curves from the profile,
187*c8dee2aaSAndroid Build Coastguard Worker // trc will be set to those three curves, and has_trc will be true.
188*c8dee2aaSAndroid Build Coastguard Worker skcms_Curve trc[3];
189*c8dee2aaSAndroid Build Coastguard Worker
190*c8dee2aaSAndroid Build Coastguard Worker // If this profile's gamut can be represented by a 3x3 transform to XYZD50,
191*c8dee2aaSAndroid Build Coastguard Worker // skcms_Parse() sets toXYZD50 to that transform and has_toXYZD50 to true.
192*c8dee2aaSAndroid Build Coastguard Worker skcms_Matrix3x3 toXYZD50;
193*c8dee2aaSAndroid Build Coastguard Worker
194*c8dee2aaSAndroid Build Coastguard Worker // If the profile has a valid A2B0 or A2B1 tag, skcms_Parse() sets A2B to
195*c8dee2aaSAndroid Build Coastguard Worker // that data, and has_A2B to true. skcms_ParseWithA2BPriority() does the
196*c8dee2aaSAndroid Build Coastguard Worker // same following any user-provided prioritization of A2B0, A2B1, or A2B2.
197*c8dee2aaSAndroid Build Coastguard Worker skcms_A2B A2B;
198*c8dee2aaSAndroid Build Coastguard Worker
199*c8dee2aaSAndroid Build Coastguard Worker // If the profile has a valid B2A0 or B2A1 tag, skcms_Parse() sets B2A to
200*c8dee2aaSAndroid Build Coastguard Worker // that data, and has_B2A to true. skcms_ParseWithA2BPriority() does the
201*c8dee2aaSAndroid Build Coastguard Worker // same following any user-provided prioritization of B2A0, B2A1, or B2A2.
202*c8dee2aaSAndroid Build Coastguard Worker skcms_B2A B2A;
203*c8dee2aaSAndroid Build Coastguard Worker
204*c8dee2aaSAndroid Build Coastguard Worker // If the profile has a valid CICP tag, skcms_Parse() sets CICP to that data,
205*c8dee2aaSAndroid Build Coastguard Worker // and has_CICP to true.
206*c8dee2aaSAndroid Build Coastguard Worker skcms_CICP CICP;
207*c8dee2aaSAndroid Build Coastguard Worker
208*c8dee2aaSAndroid Build Coastguard Worker bool has_trc;
209*c8dee2aaSAndroid Build Coastguard Worker bool has_toXYZD50;
210*c8dee2aaSAndroid Build Coastguard Worker bool has_A2B;
211*c8dee2aaSAndroid Build Coastguard Worker bool has_B2A;
212*c8dee2aaSAndroid Build Coastguard Worker bool has_CICP;
213*c8dee2aaSAndroid Build Coastguard Worker } skcms_ICCProfile;
214*c8dee2aaSAndroid Build Coastguard Worker
215*c8dee2aaSAndroid Build Coastguard Worker // The sRGB color profile is so commonly used that we offer a canonical skcms_ICCProfile for it.
216*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API const skcms_ICCProfile* skcms_sRGB_profile(void);
217*c8dee2aaSAndroid Build Coastguard Worker // Ditto for XYZD50, the most common profile connection space.
218*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API const skcms_ICCProfile* skcms_XYZD50_profile(void);
219*c8dee2aaSAndroid Build Coastguard Worker
220*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API const skcms_TransferFunction* skcms_sRGB_TransferFunction(void);
221*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API const skcms_TransferFunction* skcms_sRGB_Inverse_TransferFunction(void);
222*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API const skcms_TransferFunction* skcms_Identity_TransferFunction(void);
223*c8dee2aaSAndroid Build Coastguard Worker
224*c8dee2aaSAndroid Build Coastguard Worker // Practical equality test for two skcms_ICCProfiles.
225*c8dee2aaSAndroid Build Coastguard Worker // The implementation is subject to change, but it will always try to answer
226*c8dee2aaSAndroid Build Coastguard Worker // "can I substitute A for B?" and "can I skip transforming from A to B?".
227*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_ApproximatelyEqualProfiles(const skcms_ICCProfile* A,
228*c8dee2aaSAndroid Build Coastguard Worker const skcms_ICCProfile* B);
229*c8dee2aaSAndroid Build Coastguard Worker
230*c8dee2aaSAndroid Build Coastguard Worker // Practical test that answers: Is curve roughly the inverse of inv_tf? Typically used by passing
231*c8dee2aaSAndroid Build Coastguard Worker // the inverse of a known parametric transfer function (like sRGB), to determine if a particular
232*c8dee2aaSAndroid Build Coastguard Worker // curve is very close to sRGB.
233*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_AreApproximateInverses(const skcms_Curve* curve,
234*c8dee2aaSAndroid Build Coastguard Worker const skcms_TransferFunction* inv_tf);
235*c8dee2aaSAndroid Build Coastguard Worker
236*c8dee2aaSAndroid Build Coastguard Worker // Similar to above, answering the question for all three TRC curves of the given profile. Again,
237*c8dee2aaSAndroid Build Coastguard Worker // passing skcms_sRGB_InverseTransferFunction as inv_tf will answer the question:
238*c8dee2aaSAndroid Build Coastguard Worker // "Does this profile have a transfer function that is very close to sRGB?"
239*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_TRCs_AreApproximateInverse(const skcms_ICCProfile* profile,
240*c8dee2aaSAndroid Build Coastguard Worker const skcms_TransferFunction* inv_tf);
241*c8dee2aaSAndroid Build Coastguard Worker
242*c8dee2aaSAndroid Build Coastguard Worker // Parse an ICC profile and return true if possible, otherwise return false.
243*c8dee2aaSAndroid Build Coastguard Worker // Selects an A2B profile (if present) according to priority list (each entry 0-2).
244*c8dee2aaSAndroid Build Coastguard Worker // The buffer is not copied; it must remain valid as long as the skcms_ICCProfile will be used.
245*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_ParseWithA2BPriority(const void*, size_t,
246*c8dee2aaSAndroid Build Coastguard Worker const int priority[], int priorities,
247*c8dee2aaSAndroid Build Coastguard Worker skcms_ICCProfile*);
248*c8dee2aaSAndroid Build Coastguard Worker
skcms_Parse(const void * buf,size_t len,skcms_ICCProfile * profile)249*c8dee2aaSAndroid Build Coastguard Worker static inline bool skcms_Parse(const void* buf, size_t len, skcms_ICCProfile* profile) {
250*c8dee2aaSAndroid Build Coastguard Worker // For continuity of existing user expectations,
251*c8dee2aaSAndroid Build Coastguard Worker // prefer A2B0 (perceptual) over A2B1 (relative colormetric), and ignore A2B2 (saturation).
252*c8dee2aaSAndroid Build Coastguard Worker const int priority[] = {0,1};
253*c8dee2aaSAndroid Build Coastguard Worker return skcms_ParseWithA2BPriority(buf, len,
254*c8dee2aaSAndroid Build Coastguard Worker priority, sizeof(priority)/sizeof(*priority),
255*c8dee2aaSAndroid Build Coastguard Worker profile);
256*c8dee2aaSAndroid Build Coastguard Worker }
257*c8dee2aaSAndroid Build Coastguard Worker
258*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_ApproximateCurve(const skcms_Curve* curve,
259*c8dee2aaSAndroid Build Coastguard Worker skcms_TransferFunction* approx,
260*c8dee2aaSAndroid Build Coastguard Worker float* max_error);
261*c8dee2aaSAndroid Build Coastguard Worker
262*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_GetCHAD(const skcms_ICCProfile*, skcms_Matrix3x3*);
263*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_GetWTPT(const skcms_ICCProfile*, float xyz[3]);
264*c8dee2aaSAndroid Build Coastguard Worker
265*c8dee2aaSAndroid Build Coastguard Worker // Returns the number of channels of input data that are expected on the "A" side of the profile.
266*c8dee2aaSAndroid Build Coastguard Worker // This is useful for image codecs, where the image data and the accompanying profile might have
267*c8dee2aaSAndroid Build Coastguard Worker // conflicting data shapes. In some cases, the result is unclear or invalid. In that case, the
268*c8dee2aaSAndroid Build Coastguard Worker // function will return a negative value to signal an error.
269*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API int skcms_GetInputChannelCount(const skcms_ICCProfile*);
270*c8dee2aaSAndroid Build Coastguard Worker
271*c8dee2aaSAndroid Build Coastguard Worker // These are common ICC signature values
272*c8dee2aaSAndroid Build Coastguard Worker enum {
273*c8dee2aaSAndroid Build Coastguard Worker // common data_color_space values
274*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_CMYK = 0x434D594B,
275*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_Gray = 0x47524159,
276*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_RGB = 0x52474220,
277*c8dee2aaSAndroid Build Coastguard Worker
278*c8dee2aaSAndroid Build Coastguard Worker // pcs (or data_color_space)
279*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_Lab = 0x4C616220,
280*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_XYZ = 0x58595A20,
281*c8dee2aaSAndroid Build Coastguard Worker
282*c8dee2aaSAndroid Build Coastguard Worker // other, less common data_color_space values
283*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_CIELUV = 0x4C757620,
284*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_YCbCr = 0x59436272,
285*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_CIEYxy = 0x59787920,
286*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_HSV = 0x48535620,
287*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_HLS = 0x484C5320,
288*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_CMY = 0x434D5920,
289*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_2CLR = 0x32434C52,
290*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_3CLR = 0x33434C52,
291*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_4CLR = 0x34434C52,
292*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_5CLR = 0x35434C52,
293*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_6CLR = 0x36434C52,
294*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_7CLR = 0x37434C52,
295*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_8CLR = 0x38434C52,
296*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_9CLR = 0x39434C52,
297*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_10CLR = 0x41434C52,
298*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_11CLR = 0x42434C52,
299*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_12CLR = 0x43434C52,
300*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_13CLR = 0x44434C52,
301*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_14CLR = 0x45434C52,
302*c8dee2aaSAndroid Build Coastguard Worker skcms_Signature_15CLR = 0x46434C52,
303*c8dee2aaSAndroid Build Coastguard Worker };
304*c8dee2aaSAndroid Build Coastguard Worker
305*c8dee2aaSAndroid Build Coastguard Worker typedef enum skcms_PixelFormat {
306*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_A_8,
307*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_A_8_,
308*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_G_8,
309*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_G_8_,
310*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_GA_88, // Grayscale with alpha.
311*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_GA_88_,
312*c8dee2aaSAndroid Build Coastguard Worker
313*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGB_565,
314*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGR_565,
315*c8dee2aaSAndroid Build Coastguard Worker
316*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_ABGR_4444,
317*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_ARGB_4444,
318*c8dee2aaSAndroid Build Coastguard Worker
319*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGB_888,
320*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGR_888,
321*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGBA_8888,
322*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGRA_8888,
323*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGBA_8888_sRGB, // Automatic sRGB encoding / decoding.
324*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGRA_8888_sRGB, // (Generally used with linear transfer functions.)
325*c8dee2aaSAndroid Build Coastguard Worker
326*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGBA_1010102,
327*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGRA_1010102,
328*c8dee2aaSAndroid Build Coastguard Worker
329*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGB_161616LE, // Little-endian. Pointers must be 16-bit aligned.
330*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGR_161616LE,
331*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGBA_16161616LE,
332*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGRA_16161616LE,
333*c8dee2aaSAndroid Build Coastguard Worker
334*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGB_161616BE, // Big-endian. Pointers must be 16-bit aligned.
335*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGR_161616BE,
336*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGBA_16161616BE,
337*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGRA_16161616BE,
338*c8dee2aaSAndroid Build Coastguard Worker
339*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGB_hhh_Norm, // 1-5-10 half-precision float in [0,1]
340*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGR_hhh_Norm, // Pointers must be 16-bit aligned.
341*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGBA_hhhh_Norm,
342*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGRA_hhhh_Norm,
343*c8dee2aaSAndroid Build Coastguard Worker
344*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGB_hhh, // 1-5-10 half-precision float.
345*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGR_hhh, // Pointers must be 16-bit aligned.
346*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGBA_hhhh,
347*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGRA_hhhh,
348*c8dee2aaSAndroid Build Coastguard Worker
349*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGB_fff, // 1-8-23 single-precision float (the normal kind).
350*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGR_fff, // Pointers must be 32-bit aligned.
351*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGBA_ffff,
352*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGRA_ffff,
353*c8dee2aaSAndroid Build Coastguard Worker
354*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGB_101010x_XR, // Note: This is located here to signal no clamping.
355*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGR_101010x_XR, // Compatible with MTLPixelFormatBGR10_XR.
356*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_RGBA_10101010_XR, // Note: This is located here to signal no clamping.
357*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat_BGRA_10101010_XR, // Compatible with MTLPixelFormatBGRA10_XR.
358*c8dee2aaSAndroid Build Coastguard Worker } skcms_PixelFormat;
359*c8dee2aaSAndroid Build Coastguard Worker
360*c8dee2aaSAndroid Build Coastguard Worker // We always store any alpha channel linearly. In the chart below, tf-1() is the inverse
361*c8dee2aaSAndroid Build Coastguard Worker // transfer function for the given color profile (applying the transfer function linearizes).
362*c8dee2aaSAndroid Build Coastguard Worker
363*c8dee2aaSAndroid Build Coastguard Worker // We treat opaque as a strong requirement, not just a performance hint: we will ignore
364*c8dee2aaSAndroid Build Coastguard Worker // any source alpha and treat it as 1.0, and will make sure that any destination alpha
365*c8dee2aaSAndroid Build Coastguard Worker // channel is filled with the equivalent of 1.0.
366*c8dee2aaSAndroid Build Coastguard Worker
367*c8dee2aaSAndroid Build Coastguard Worker // We used to offer multiple types of premultiplication, but now just one, PremulAsEncoded.
368*c8dee2aaSAndroid Build Coastguard Worker // This is the premul you're probably used to working with.
369*c8dee2aaSAndroid Build Coastguard Worker
370*c8dee2aaSAndroid Build Coastguard Worker typedef enum skcms_AlphaFormat {
371*c8dee2aaSAndroid Build Coastguard Worker skcms_AlphaFormat_Opaque, // alpha is always opaque
372*c8dee2aaSAndroid Build Coastguard Worker // tf-1(r), tf-1(g), tf-1(b), 1.0
373*c8dee2aaSAndroid Build Coastguard Worker skcms_AlphaFormat_Unpremul, // alpha and color are unassociated
374*c8dee2aaSAndroid Build Coastguard Worker // tf-1(r), tf-1(g), tf-1(b), a
375*c8dee2aaSAndroid Build Coastguard Worker skcms_AlphaFormat_PremulAsEncoded, // premultiplied while encoded
376*c8dee2aaSAndroid Build Coastguard Worker // tf-1(r)*a, tf-1(g)*a, tf-1(b)*a, a
377*c8dee2aaSAndroid Build Coastguard Worker } skcms_AlphaFormat;
378*c8dee2aaSAndroid Build Coastguard Worker
379*c8dee2aaSAndroid Build Coastguard Worker // Convert npixels pixels from src format and color profile to dst format and color profile
380*c8dee2aaSAndroid Build Coastguard Worker // and return true, otherwise return false. It is safe to alias dst == src if dstFmt == srcFmt.
381*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_Transform(const void* src,
382*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat srcFmt,
383*c8dee2aaSAndroid Build Coastguard Worker skcms_AlphaFormat srcAlpha,
384*c8dee2aaSAndroid Build Coastguard Worker const skcms_ICCProfile* srcProfile,
385*c8dee2aaSAndroid Build Coastguard Worker void* dst,
386*c8dee2aaSAndroid Build Coastguard Worker skcms_PixelFormat dstFmt,
387*c8dee2aaSAndroid Build Coastguard Worker skcms_AlphaFormat dstAlpha,
388*c8dee2aaSAndroid Build Coastguard Worker const skcms_ICCProfile* dstProfile,
389*c8dee2aaSAndroid Build Coastguard Worker size_t npixels);
390*c8dee2aaSAndroid Build Coastguard Worker
391*c8dee2aaSAndroid Build Coastguard Worker // If profile can be used as a destination in skcms_Transform, return true. Otherwise, attempt to
392*c8dee2aaSAndroid Build Coastguard Worker // rewrite it with approximations where reasonable. If successful, return true. If no reasonable
393*c8dee2aaSAndroid Build Coastguard Worker // approximation exists, leave the profile unchanged and return false.
394*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_MakeUsableAsDestination(skcms_ICCProfile* profile);
395*c8dee2aaSAndroid Build Coastguard Worker
396*c8dee2aaSAndroid Build Coastguard Worker // If profile can be used as a destination with a single parametric transfer function (ie for
397*c8dee2aaSAndroid Build Coastguard Worker // rasterization), return true. Otherwise, attempt to rewrite it with approximations where
398*c8dee2aaSAndroid Build Coastguard Worker // reasonable. If successful, return true. If no reasonable approximation exists, leave the
399*c8dee2aaSAndroid Build Coastguard Worker // profile unchanged and return false.
400*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_MakeUsableAsDestinationWithSingleCurve(skcms_ICCProfile* profile);
401*c8dee2aaSAndroid Build Coastguard Worker
402*c8dee2aaSAndroid Build Coastguard Worker // Returns a matrix to adapt XYZ color from given the whitepoint to D50.
403*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_AdaptToXYZD50(float wx, float wy,
404*c8dee2aaSAndroid Build Coastguard Worker skcms_Matrix3x3* toXYZD50);
405*c8dee2aaSAndroid Build Coastguard Worker
406*c8dee2aaSAndroid Build Coastguard Worker // Returns a matrix to convert RGB color into XYZ adapted to D50, given the
407*c8dee2aaSAndroid Build Coastguard Worker // primaries and whitepoint of the RGB model.
408*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API bool skcms_PrimariesToXYZD50(float rx, float ry,
409*c8dee2aaSAndroid Build Coastguard Worker float gx, float gy,
410*c8dee2aaSAndroid Build Coastguard Worker float bx, float by,
411*c8dee2aaSAndroid Build Coastguard Worker float wx, float wy,
412*c8dee2aaSAndroid Build Coastguard Worker skcms_Matrix3x3* toXYZD50);
413*c8dee2aaSAndroid Build Coastguard Worker
414*c8dee2aaSAndroid Build Coastguard Worker // Call before your first call to skcms_Transform() to skip runtime CPU detection.
415*c8dee2aaSAndroid Build Coastguard Worker SKCMS_API void skcms_DisableRuntimeCPUDetection(void);
416*c8dee2aaSAndroid Build Coastguard Worker
417*c8dee2aaSAndroid Build Coastguard Worker // Utilities for programmatically constructing profiles
skcms_Init(skcms_ICCProfile * p)418*c8dee2aaSAndroid Build Coastguard Worker static inline void skcms_Init(skcms_ICCProfile* p) {
419*c8dee2aaSAndroid Build Coastguard Worker memset(p, 0, sizeof(*p));
420*c8dee2aaSAndroid Build Coastguard Worker p->data_color_space = skcms_Signature_RGB;
421*c8dee2aaSAndroid Build Coastguard Worker p->pcs = skcms_Signature_XYZ;
422*c8dee2aaSAndroid Build Coastguard Worker }
423*c8dee2aaSAndroid Build Coastguard Worker
skcms_SetTransferFunction(skcms_ICCProfile * p,const skcms_TransferFunction * tf)424*c8dee2aaSAndroid Build Coastguard Worker static inline void skcms_SetTransferFunction(skcms_ICCProfile* p,
425*c8dee2aaSAndroid Build Coastguard Worker const skcms_TransferFunction* tf) {
426*c8dee2aaSAndroid Build Coastguard Worker p->has_trc = true;
427*c8dee2aaSAndroid Build Coastguard Worker for (int i = 0; i < 3; ++i) {
428*c8dee2aaSAndroid Build Coastguard Worker p->trc[i].table_entries = 0;
429*c8dee2aaSAndroid Build Coastguard Worker p->trc[i].parametric = *tf;
430*c8dee2aaSAndroid Build Coastguard Worker }
431*c8dee2aaSAndroid Build Coastguard Worker }
432*c8dee2aaSAndroid Build Coastguard Worker
skcms_SetXYZD50(skcms_ICCProfile * p,const skcms_Matrix3x3 * m)433*c8dee2aaSAndroid Build Coastguard Worker static inline void skcms_SetXYZD50(skcms_ICCProfile* p, const skcms_Matrix3x3* m) {
434*c8dee2aaSAndroid Build Coastguard Worker p->has_toXYZD50 = true;
435*c8dee2aaSAndroid Build Coastguard Worker p->toXYZD50 = *m;
436*c8dee2aaSAndroid Build Coastguard Worker }
437*c8dee2aaSAndroid Build Coastguard Worker
438*c8dee2aaSAndroid Build Coastguard Worker #ifdef __cplusplus
439*c8dee2aaSAndroid Build Coastguard Worker }
440*c8dee2aaSAndroid Build Coastguard Worker #endif
441