Debian Codegraph @

modules/skcms/skcms.cc

raw
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 #include "src/skcms_public.h" // NO_G3_REWRITE
9 #include "src/skcms_internals.h" // NO_G3_REWRITE
10 #include "src/skcms_Transform.h" // NO_G3_REWRITE
11 #include <assert.h>
12 #include <float.h>
13 #include <limits.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #if defined(__ARM_NEON)
18 #include <arm_neon.h>
19 #elif defined(__SSE__)
20 #include <immintrin.h>
21
22 #if defined(__clang__)
23 // That #include <immintrin.h> is usually enough, but Clang's headers
24 // "helpfully" skip including the whole kitchen sink when _MSC_VER is
25 // defined, because lots of programs on Windows would include that and
26 // it'd be a lot slower. But we want all those headers included so we
27 // can use their features after runtime checks later.
28 #include <smmintrin.h>
29 #include <avxintrin.h>
30 #include <avx2intrin.h>
31 #include <avx512fintrin.h>
32 #include <avx512dqintrin.h>
33 #endif
34 #endif
35
36 using namespace skcms_private;
37
38 static bool sAllowRuntimeCPUDetection = true;
39
40 void skcms_DisableRuntimeCPUDetection() {
41 sAllowRuntimeCPUDetection = false;
42 }
43
44 static float log2f_(float x) {
45 // The first approximation of log2(x) is its exponent 'e', minus 127.
46 int32_t bits;
47 memcpy(&bits, &x, sizeof(bits));
48
49 float e = (float)bits * (1.0f / (1<<23));
50
51 // If we use the mantissa too we can refine the error signficantly.
52 int32_t m_bits = (bits & 0x007fffff) | 0x3f000000;
53 float m;
54 memcpy(&m, &m_bits, sizeof(m));
55
56 return (e - 124.225514990f
57 - 1.498030302f*m
58 - 1.725879990f/(0.3520887068f + m));
59 }
60 static float logf_(float x) {
61 const float ln2 = 0.69314718f;
62 return ln2*log2f_(x);
63 }
64
65 static float exp2f_(float x) {
66 if (x > 128.0f) {
67 return INFINITY_;
68 } else if (x < -127.0f) {
69 return 0.0f;
70 }
71 float fract = x - floorf_(x);
72
73 float fbits = (1.0f * (1<<23)) * (x + 121.274057500f
74 - 1.490129070f*fract
75 + 27.728023300f/(4.84252568f - fract));
76
77 // Before we cast fbits to int32_t, check for out of range values to pacify UBSAN.
78 // INT_MAX is not exactly representable as a float, so exclude it as effectively infinite.
79 // Negative values are effectively underflow - we'll end up returning a (different) negative
80 // value, which makes no sense. So clamp to zero.
81 if (fbits >= (float)INT_MAX) {
82 return INFINITY_;
83 } else if (fbits < 0) {
84 return 0;
85 }
86
87 int32_t bits = (int32_t)fbits;
88 memcpy(&x, &bits, sizeof(x));
89 return x;
90 }
91
92 // Not static, as it's used by some test tools.
93 float powf_(float x, float y) {
94 if (x <= 0.f) {
95 return 0.f;
96 }
97 if (x == 1.f) {
98 return 1.f;
99 }
100 return exp2f_(log2f_(x) * y);
101 }
102
103 static float expf_(float x) {
104 const float log2_e = 1.4426950408889634074f;
105 return exp2f_(log2_e * x);
106 }
107
108 static float fmaxf_(float x, float y) { return x > y ? x : y; }
109 static float fminf_(float x, float y) { return x < y ? x : y; }
110
111 static bool isfinitef_(float x) { return 0 == x*0; }
112
113 static float minus_1_ulp(float x) {
114 int32_t bits;
115 memcpy(&bits, &x, sizeof(bits));
116 bits = bits - 1;
117 memcpy(&x, &bits, sizeof(bits));
118 return x;
119 }
120
121 // Most transfer functions we work with are sRGBish.
122 // For exotic HDR transfer functions, we encode them using a tf.g that makes no sense,
123 // and repurpose the other fields to hold the parameters of the HDR functions.
124 struct TF_PQish { float A,B,C,D,E,F; };
125 struct TF_HLGish { float R,G,a,b,c,K_minus_1; };
126 // We didn't originally support a scale factor K for HLG, and instead just stored 0 in
127 // the unused `f` field of skcms_TransferFunction for HLGish and HLGInvish transfer functions.
128 // By storing f=K-1, those old unusued f=0 values now mean K=1, a noop scale factor.
129
130 static float TFKind_marker(skcms_TFType kind) {
131 // We'd use different NaNs, but those aren't guaranteed to be preserved by WASM.
132 return -(float)kind;
133 }
134
135 static skcms_TFType classify(const skcms_TransferFunction& tf, TF_PQish* pq = nullptr
136 , TF_HLGish* hlg = nullptr) {
137 if (tf.g < 0) {
138 // Negative "g" is mapped to enum values; large negative are for sure invalid.
139 if (tf.g < -128) {
140 return skcms_TFType_Invalid;
141 }
142 int enum_g = -static_cast<int>(tf.g);
143 // Non-whole "g" values are invalid as well.
144 if (static_cast<float>(-enum_g) != tf.g) {
145 return skcms_TFType_Invalid;
146 }
147 // TODO: soundness checks for PQ/HLG like we do for sRGBish?
148 switch (enum_g) {
149 case skcms_TFType_PQish:
150 if (pq) {
151 memcpy(pq , &tf.a, sizeof(*pq ));
152 }
153 return skcms_TFType_PQish;
154 case skcms_TFType_HLGish:
155 if (hlg) {
156 memcpy(hlg, &tf.a, sizeof(*hlg));
157 }
158 return skcms_TFType_HLGish;
159 case skcms_TFType_HLGinvish:
160 if (hlg) {
161 memcpy(hlg, &tf.a, sizeof(*hlg));
162 }
163 return skcms_TFType_HLGinvish;
164 case skcms_TFType_PQ:
165 if (tf.b != 0.f || tf.c != 0.f || tf.d != 0.f || tf.e != 0.f || tf.f != 0.f) {
166 return skcms_TFType_Invalid;
167 }
168 return skcms_TFType_PQ;
169 case skcms_TFType_HLG:
170 if (tf.d != 0.f || tf.e != 0.f || tf.f != 0.f) {
171 return skcms_TFType_Invalid;
172 }
173 return skcms_TFType_HLG;
174 }
175 return skcms_TFType_Invalid;
176 }
177
178 // Basic soundness checks for sRGBish transfer functions.
179 if (isfinitef_(tf.a + tf.b + tf.c + tf.d + tf.e + tf.f + tf.g)
180 // a,c,d,g should be non-negative to make any sense.
181 && tf.a >= 0
182 && tf.c >= 0
183 && tf.d >= 0
184 && tf.g >= 0
185 // Raising a negative value to a fractional tf->g produces complex numbers.
186 && tf.a * tf.d + tf.b >= 0) {
187 return skcms_TFType_sRGBish;
188 }
189
190 return skcms_TFType_Invalid;
191 }
192
193 skcms_TFType skcms_TransferFunction_getType(const skcms_TransferFunction* tf) {
194 return classify(*tf);
195 }
196 bool skcms_TransferFunction_isSRGBish(const skcms_TransferFunction* tf) {
197 return classify(*tf) == skcms_TFType_sRGBish;
198 }
199 bool skcms_TransferFunction_isPQish(const skcms_TransferFunction* tf) {
200 return classify(*tf) == skcms_TFType_PQish;
201 }
202 bool skcms_TransferFunction_isHLGish(const skcms_TransferFunction* tf) {
203 return classify(*tf) == skcms_TFType_HLGish;
204 }
205 bool skcms_TransferFunction_isPQ(const skcms_TransferFunction* tf) {
206 return classify(*tf) == skcms_TFType_PQ;
207 }
208 bool skcms_TransferFunction_isHLG(const skcms_TransferFunction* tf) {
209 return classify(*tf) == skcms_TFType_HLG;
210 }
211
212 bool skcms_TransferFunction_makePQish(skcms_TransferFunction* tf,
213 float A, float B, float C,
214 float D, float E, float F) {
215 *tf = { TFKind_marker(skcms_TFType_PQish), A,B,C,D,E,F };
216 assert(skcms_TransferFunction_isPQish(tf));
217 return true;
218 }
219
220 bool skcms_TransferFunction_makeScaledHLGish(skcms_TransferFunction* tf,
221 float K, float R, float G,
222 float a, float b, float c) {
223 *tf = { TFKind_marker(skcms_TFType_HLGish), R,G, a,b,c, K-1.0f };
224 assert(skcms_TransferFunction_isHLGish(tf));
225 return true;
226 }
227
228 void skcms_TransferFunction_makePQ(
229 skcms_TransferFunction* tf,
230 float hdr_reference_white_luminance) {
231 *tf = { TFKind_marker(skcms_TFType_PQ),
232 hdr_reference_white_luminance,
233 0.f,0.f,0.f,0.f,0.f };
234 assert(skcms_TransferFunction_isPQ(tf));
235 }
236
237 void skcms_TransferFunction_makeHLG(
238 skcms_TransferFunction* tf,
239 float hdr_reference_white_luminance,
240 float peak_luminance,
241 float system_gamma) {
242 *tf = { TFKind_marker(skcms_TFType_HLG),
243 hdr_reference_white_luminance,
244 peak_luminance,
245 system_gamma,
246 0.f, 0.f, 0.f };
247 assert(skcms_TransferFunction_isHLG(tf));
248 }
249
250 float skcms_TransferFunction_eval(const skcms_TransferFunction* tf, float x) {
251 float sign = x < 0 ? -1.0f : 1.0f;
252 x *= sign;
253
254 TF_PQish pq;
255 TF_HLGish hlg;
256 switch (classify(*tf, &pq, &hlg)) {
257 case skcms_TFType_Invalid: break;
258
259 case skcms_TFType_HLG: {
260 const float a = 0.17883277f;
261 const float b = 0.28466892f;
262 const float c = 0.55991073f;
263 return sign * (x <= 0.5f ? x*x/3.f : (expf_((x-c)/a) + b) / 12.f);
264 }
265
266 case skcms_TFType_HLGish: {
267 const float K = hlg.K_minus_1 + 1.0f;
268 return K * sign * (x*hlg.R <= 1 ? powf_(x*hlg.R, hlg.G)
269 : expf_((x-hlg.c)*hlg.a) + hlg.b);
270 }
271
272 // skcms_TransferFunction_invert() inverts R, G, and a for HLGinvish so this math is fast.
273 case skcms_TFType_HLGinvish: {
274 const float K = hlg.K_minus_1 + 1.0f;
275 x /= K;
276 return sign * (x <= 1 ? hlg.R * powf_(x, hlg.G)
277 : hlg.a * logf_(x - hlg.b) + hlg.c);
278 }
279
280 case skcms_TFType_sRGBish:
281 return sign * (x < tf->d ? tf->c * x + tf->f
282 : powf_(tf->a * x + tf->b, tf->g) + tf->e);
283
284 case skcms_TFType_PQ: {
285 const float c1 = 107 / 128.f;
286 const float c2 = 2413 / 128.f;
287 const float c3 = 2392 / 128.f;
288 const float m1 = 1305 / 8192.f;
289 const float m2 = 2523 / 32.f;
290 const float p = powf_(x, 1.f / m2);
291 return powf_((p - c1) / (c2 - c3 * p), 1.f / m1);
292 }
293
294 case skcms_TFType_PQish:
295 return sign *
296 powf_((pq.A + pq.B * powf_(x, pq.C)) / (pq.D + pq.E * powf_(x, pq.C)), pq.F);
297 }
298 return 0;
299 }
300
301
302 static float eval_curve(const skcms_Curve* curve, float x) {
303 if (curve->table_entries == 0) {
304 return skcms_TransferFunction_eval(&curve->parametric, x);
305 }
306
307 float ix = fmaxf_(0, fminf_(x, 1)) * static_cast<float>(curve->table_entries - 1);
308 int lo = (int) ix ,
309 hi = (int)(float)minus_1_ulp(ix + 1.0f);
310 float t = ix - (float)lo;
311
312 float l, h;
313 if (curve->table_8) {
314 l = curve->table_8[lo] * (1/255.0f);
315 h = curve->table_8[hi] * (1/255.0f);
316 } else {
317 uint16_t be_l, be_h;
318 memcpy(&be_l, curve->table_16 + 2*lo, 2);
319 memcpy(&be_h, curve->table_16 + 2*hi, 2);
320 uint16_t le_l = ((be_l << 8) | (be_l >> 8)) & 0xffff;
321 uint16_t le_h = ((be_h << 8) | (be_h >> 8)) & 0xffff;
322 l = le_l * (1/65535.0f);
323 h = le_h * (1/65535.0f);
324 }
325 return l + (h-l)*t;
326 }
327
328 float skcms_MaxRoundtripError(const skcms_Curve* curve, const skcms_TransferFunction* inv_tf) {
329 uint32_t N = curve->table_entries > 256 ? curve->table_entries : 256;
330 const float dx = 1.0f / static_cast<float>(N - 1);
331 float err = 0;
332 for (uint32_t i = 0; i < N; i++) {
333 float x = static_cast<float>(i) * dx,
334 y = eval_curve(curve, x);
335 err = fmaxf_(err, fabsf_(x - skcms_TransferFunction_eval(inv_tf, y)));
336 }
337 return err;
338 }
339
340 bool skcms_AreApproximateInverses(const skcms_Curve* curve, const skcms_TransferFunction* inv_tf) {
341 return skcms_MaxRoundtripError(curve, inv_tf) < (1/512.0f);
342 }
343
344 // Additional ICC signature values that are only used internally
345 enum {
346 // File signature
347 skcms_Signature_acsp = 0x61637370,
348
349 // Tag signatures
350 skcms_Signature_rTRC = 0x72545243,
351 skcms_Signature_gTRC = 0x67545243,
352 skcms_Signature_bTRC = 0x62545243,
353 skcms_Signature_kTRC = 0x6B545243,
354
355 skcms_Signature_rXYZ = 0x7258595A,
356 skcms_Signature_gXYZ = 0x6758595A,
357 skcms_Signature_bXYZ = 0x6258595A,
358
359 skcms_Signature_A2B0 = 0x41324230,
360 skcms_Signature_B2A0 = 0x42324130,
361
362 skcms_Signature_CHAD = 0x63686164,
363 skcms_Signature_WTPT = 0x77747074,
364
365 skcms_Signature_CICP = 0x63696370,
366
367 // Type signatures
368 skcms_Signature_curv = 0x63757276,
369 skcms_Signature_mft1 = 0x6D667431,
370 skcms_Signature_mft2 = 0x6D667432,
371 skcms_Signature_mAB = 0x6D414220,
372 skcms_Signature_mBA = 0x6D424120,
373 skcms_Signature_para = 0x70617261,
374 skcms_Signature_sf32 = 0x73663332,
375 // XYZ is also a PCS signature, so it's defined in skcms.h
376 // skcms_Signature_XYZ = 0x58595A20,
377 };
378
379 static uint16_t read_big_u16(const uint8_t* ptr) {
380 uint16_t be;
381 memcpy(&be, ptr, sizeof(be));
382 #if defined(_MSC_VER)
383 return _byteswap_ushort(be);
384 #else
385 return __builtin_bswap16(be);
386 #endif
387 }
388
389 static uint32_t read_big_u32(const uint8_t* ptr) {
390 uint32_t be;
391 memcpy(&be, ptr, sizeof(be));
392 #if defined(_MSC_VER)
393 return _byteswap_ulong(be);
394 #else
395 return __builtin_bswap32(be);
396 #endif
397 }
398
399 static int32_t read_big_i32(const uint8_t* ptr) {
400 return (int32_t)read_big_u32(ptr);
401 }
402
403 static float read_big_fixed(const uint8_t* ptr) {
404 return static_cast<float>(read_big_i32(ptr)) * (1.0f / 65536.0f);
405 }
406
407 // Maps to an in-memory profile so that fields line up to the locations specified
408 // in ICC.1:2010, section 7.2
409 typedef struct {
410 uint8_t size [ 4];
411 uint8_t cmm_type [ 4];
412 uint8_t version [ 4];
413 uint8_t profile_class [ 4];
414 uint8_t data_color_space [ 4];
415 uint8_t pcs [ 4];
416 uint8_t creation_date_time [12];
417 uint8_t signature [ 4];
418 uint8_t platform [ 4];
419 uint8_t flags [ 4];
420 uint8_t device_manufacturer [ 4];
421 uint8_t device_model [ 4];
422 uint8_t device_attributes [ 8];
423 uint8_t rendering_intent [ 4];
424 uint8_t illuminant_X [ 4];
425 uint8_t illuminant_Y [ 4];
426 uint8_t illuminant_Z [ 4];
427 uint8_t creator [ 4];
428 uint8_t profile_id [16];
429 uint8_t reserved [28];
430 uint8_t tag_count [ 4]; // Technically not part of header, but required
431 } header_Layout;
432
433 typedef struct {
434 uint8_t signature [4];
435 uint8_t offset [4];
436 uint8_t size [4];
437 } tag_Layout;
438
439 static const tag_Layout* get_tag_table(const skcms_ICCProfile* profile) {
440 return (const tag_Layout*)(profile->buffer + SAFE_SIZEOF(header_Layout));
441 }
442
443 // s15Fixed16ArrayType is technically variable sized, holding N values. However, the only valid
444 // use of the type is for the CHAD tag that stores exactly nine values.
445 typedef struct {
446 uint8_t type [ 4];
447 uint8_t reserved [ 4];
448 uint8_t values [36];
449 } sf32_Layout;
450
451 bool skcms_GetCHAD(const skcms_ICCProfile* profile, skcms_Matrix3x3* m) {
452 skcms_ICCTag tag;
453 if (!skcms_GetTagBySignature(profile, skcms_Signature_CHAD, &tag)) {
454 return false;
455 }
456
457 if (tag.type != skcms_Signature_sf32 || tag.size < SAFE_SIZEOF(sf32_Layout)) {
458 return false;
459 }
460
461 const sf32_Layout* sf32Tag = (const sf32_Layout*)tag.buf;
462 const uint8_t* values = sf32Tag->values;
463 for (int r = 0; r < 3; ++r)
464 for (int c = 0; c < 3; ++c, values += 4) {
465 m->vals[r][c] = read_big_fixed(values);
466 }
467 return true;
468 }
469
470 // XYZType is technically variable sized, holding N XYZ triples. However, the only valid uses of
471 // the type are for tags/data that store exactly one triple.
472 typedef struct {
473 uint8_t type [4];
474 uint8_t reserved [4];
475 uint8_t X [4];
476 uint8_t Y [4];
477 uint8_t Z [4];
478 } XYZ_Layout;
479
480 static bool read_tag_xyz(const skcms_ICCTag* tag, float* x, float* y, float* z) {
481 if (tag->type != skcms_Signature_XYZ || tag->size < SAFE_SIZEOF(XYZ_Layout)) {
482 return false;
483 }
484
485 const XYZ_Layout* xyzTag = (const XYZ_Layout*)tag->buf;
486
487 *x = read_big_fixed(xyzTag->X);
488 *y = read_big_fixed(xyzTag->Y);
489 *z = read_big_fixed(xyzTag->Z);
490 return true;
491 }
492
493 bool skcms_GetWTPT(const skcms_ICCProfile* profile, float xyz[3]) {
494 skcms_ICCTag tag;
495 return skcms_GetTagBySignature(profile, skcms_Signature_WTPT, &tag) &&
496 read_tag_xyz(&tag, &xyz[0], &xyz[1], &xyz[2]);
497 }
498
499 static int data_color_space_channel_count(uint32_t data_color_space) {
500 switch (data_color_space) {
501 case skcms_Signature_CMYK: return 4;
502 case skcms_Signature_Gray: return 1;
503 case skcms_Signature_RGB: return 3;
504 case skcms_Signature_Lab: return 3;
505 case skcms_Signature_XYZ: return 3;
506 case skcms_Signature_CIELUV: return 3;
507 case skcms_Signature_YCbCr: return 3;
508 case skcms_Signature_CIEYxy: return 3;
509 case skcms_Signature_HSV: return 3;
510 case skcms_Signature_HLS: return 3;
511 case skcms_Signature_CMY: return 3;
512 case skcms_Signature_2CLR: return 2;
513 case skcms_Signature_3CLR: return 3;
514 case skcms_Signature_4CLR: return 4;
515 case skcms_Signature_5CLR: return 5;
516 case skcms_Signature_6CLR: return 6;
517 case skcms_Signature_7CLR: return 7;
518 case skcms_Signature_8CLR: return 8;
519 case skcms_Signature_9CLR: return 9;
520 case skcms_Signature_10CLR: return 10;
521 case skcms_Signature_11CLR: return 11;
522 case skcms_Signature_12CLR: return 12;
523 case skcms_Signature_13CLR: return 13;
524 case skcms_Signature_14CLR: return 14;
525 case skcms_Signature_15CLR: return 15;
526 default: return -1;
527 }
528 }
529
530 int skcms_GetInputChannelCount(const skcms_ICCProfile* profile) {
531 int a2b_count = 0;
532 if (profile->has_A2B) {
533 a2b_count = profile->A2B.input_channels != 0
534 ? static_cast<int>(profile->A2B.input_channels)
535 : 3;
536 }
537
538 skcms_ICCTag tag;
539 int trc_count = 0;
540 if (skcms_GetTagBySignature(profile, skcms_Signature_kTRC, &tag)) {
541 trc_count = 1;
542 } else if (profile->has_trc) {
543 trc_count = 3;
544 }
545
546 int dcs_count = data_color_space_channel_count(profile->data_color_space);
547
548 if (dcs_count < 0) {
549 return -1;
550 }
551
552 if (a2b_count > 0 && a2b_count != dcs_count) {
553 return -1;
554 }
555 if (trc_count > 0 && trc_count != dcs_count) {
556 return -1;
557 }
558
559 return dcs_count;
560 }
561
562 static bool read_to_XYZD50(const skcms_ICCTag* rXYZ, const skcms_ICCTag* gXYZ,
563 const skcms_ICCTag* bXYZ, skcms_Matrix3x3* toXYZ) {
564 return read_tag_xyz(rXYZ, &toXYZ->vals[0][0], &toXYZ->vals[1][0], &toXYZ->vals[2][0]) &&
565 read_tag_xyz(gXYZ, &toXYZ->vals[0][1], &toXYZ->vals[1][1], &toXYZ->vals[2][1]) &&
566 read_tag_xyz(bXYZ, &toXYZ->vals[0][2], &toXYZ->vals[1][2], &toXYZ->vals[2][2]);
567 }
568
569 typedef struct {
570 uint8_t type [4];
571 uint8_t reserved_a [4];
572 uint8_t function_type [2];
573 uint8_t reserved_b [2];
574 uint8_t variable [1/*variable*/]; // 1, 3, 4, 5, or 7 s15.16, depending on function_type
575 } para_Layout;
576
577 static bool read_curve_para(const uint8_t* buf, uint32_t size,
578 skcms_Curve* curve, uint32_t* curve_size) {
579 if (size < SAFE_FIXED_SIZE(para_Layout)) {
580 return false;
581 }
582
583 const para_Layout* paraTag = (const para_Layout*)buf;
584
585 enum { kG = 0, kGAB = 1, kGABC = 2, kGABCD = 3, kGABCDEF = 4 };
586 uint16_t function_type = read_big_u16(paraTag->function_type);
587 if (function_type > kGABCDEF) {
588 return false;
589 }
590
591 static const uint32_t curve_bytes[] = { 4, 12, 16, 20, 28 };
592 if (size < SAFE_FIXED_SIZE(para_Layout) + curve_bytes[function_type]) {
593 return false;
594 }
595
596 if (curve_size) {
597 *curve_size = SAFE_FIXED_SIZE(para_Layout) + curve_bytes[function_type];
598 }
599
600 curve->table_entries = 0;
601 curve->parametric.a = 1.0f;
602 curve->parametric.b = 0.0f;
603 curve->parametric.c = 0.0f;
604 curve->parametric.d = 0.0f;
605 curve->parametric.e = 0.0f;
606 curve->parametric.f = 0.0f;
607 curve->parametric.g = read_big_fixed(paraTag->variable);
608
609 switch (function_type) {
610 case kGAB:
611 curve->parametric.a = read_big_fixed(paraTag->variable + 4);
612 curve->parametric.b = read_big_fixed(paraTag->variable + 8);
613 if (curve->parametric.a == 0) {
614 return false;
615 }
616 curve->parametric.d = -curve->parametric.b / curve->parametric.a;
617 break;
618 case kGABC:
619 curve->parametric.a = read_big_fixed(paraTag->variable + 4);
620 curve->parametric.b = read_big_fixed(paraTag->variable + 8);
621 curve->parametric.e = read_big_fixed(paraTag->variable + 12);
622 if (curve->parametric.a == 0) {
623 return false;
624 }
625 curve->parametric.d = -curve->parametric.b / curve->parametric.a;
626 curve->parametric.f = curve->parametric.e;
627 break;
628 case kGABCD:
629 curve->parametric.a = read_big_fixed(paraTag->variable + 4);
630 curve->parametric.b = read_big_fixed(paraTag->variable + 8);
631 curve->parametric.c = read_big_fixed(paraTag->variable + 12);
632 curve->parametric.d = read_big_fixed(paraTag->variable + 16);
633 break;
634 case kGABCDEF:
635 curve->parametric.a = read_big_fixed(paraTag->variable + 4);
636 curve->parametric.b = read_big_fixed(paraTag->variable + 8);
637 curve->parametric.c = read_big_fixed(paraTag->variable + 12);
638 curve->parametric.d = read_big_fixed(paraTag->variable + 16);
639 curve->parametric.e = read_big_fixed(paraTag->variable + 20);
640 curve->parametric.f = read_big_fixed(paraTag->variable + 24);
641 break;
642 }
643 return skcms_TransferFunction_isSRGBish(&curve->parametric);
644 }
645
646 typedef struct {
647 uint8_t type [4];
648 uint8_t reserved [4];
649 uint8_t value_count [4];
650 uint8_t variable [1/*variable*/]; // value_count, 8.8 if 1, uint16 (n*65535) if > 1
651 } curv_Layout;
652
653 // See https://crbug.com/492744328 for how this was determined.
654 static const uint32_t kMaxTableEntries = 1 << 24; // 16,777,216
655
656 static bool read_curve_curv(const uint8_t* buf, uint32_t size,
657 skcms_Curve* curve, uint32_t* curve_size) {
658 if (size < SAFE_FIXED_SIZE(curv_Layout)) {
659 return false;
660 }
661
662 const curv_Layout* curvTag = (const curv_Layout*)buf;
663
664 uint32_t value_count = read_big_u32(curvTag->value_count);
665 if (size < SAFE_FIXED_SIZE(curv_Layout) + value_count * SAFE_SIZEOF(uint16_t)) {
666 return false;
667 }
668
669 if (curve_size) {
670 *curve_size = SAFE_FIXED_SIZE(curv_Layout) + value_count * SAFE_SIZEOF(uint16_t);
671 }
672
673 if (value_count < 2) {
674 curve->table_entries = 0;
675 curve->parametric.a = 1.0f;
676 curve->parametric.b = 0.0f;
677 curve->parametric.c = 0.0f;
678 curve->parametric.d = 0.0f;
679 curve->parametric.e = 0.0f;
680 curve->parametric.f = 0.0f;
681 if (value_count == 0) {
682 // Empty tables are a shorthand for an identity curve
683 curve->parametric.g = 1.0f;
684 } else {
685 // Single entry tables are a shorthand for simple gamma
686 curve->parametric.g = read_big_u16(curvTag->variable) * (1.0f / 256.0f);
687 }
688 return true;
689 }
690 if (value_count > kMaxTableEntries) {
691 return false;
692 }
693 curve->table_8 = nullptr;
694 curve->table_16 = curvTag->variable;
695 curve->table_entries = value_count;
696 return true;
697 }
698
699 // Parses both curveType and parametricCurveType data. Ensures that at most 'size' bytes are read.
700 // If curve_size is not nullptr, writes the number of bytes used by the curve in (*curve_size).
701 static bool read_curve(const uint8_t* buf, uint32_t size,
702 skcms_Curve* curve, uint32_t* curve_size) {
703 if (!buf || size < 4 || !curve) {
704 return false;
705 }
706
707 uint32_t type = read_big_u32(buf);
708 if (type == skcms_Signature_para) {
709 return read_curve_para(buf, size, curve, curve_size);
710 } else if (type == skcms_Signature_curv) {
711 return read_curve_curv(buf, size, curve, curve_size);
712 }
713
714 return false;
715 }
716
717 // mft1 and mft2 share a large chunk of data
718 typedef struct {
719 uint8_t type [ 4];
720 uint8_t reserved_a [ 4];
721 uint8_t input_channels [ 1];
722 uint8_t output_channels [ 1];
723 uint8_t grid_points [ 1];
724 uint8_t reserved_b [ 1];
725 uint8_t matrix [36];
726 } mft_CommonLayout;
727
728 typedef struct {
729 mft_CommonLayout common [1];
730
731 uint8_t variable [1/*variable*/];
732 } mft1_Layout;
733
734 typedef struct {
735 mft_CommonLayout common [1];
736
737 uint8_t input_table_entries [2];
738 uint8_t output_table_entries [2];
739 uint8_t variable [1/*variable*/];
740 } mft2_Layout;
741
742 static bool read_mft_common(const mft_CommonLayout* mftTag, skcms_A2B* a2b) {
743 // MFT matrices are applied before the first set of curves, but must be identity unless the
744 // input is PCSXYZ. We don't support PCSXYZ profiles, so we ignore this matrix. Note that the
745 // matrix in skcms_A2B is applied later in the pipe, so supporting this would require another
746 // field/flag.
747 a2b->matrix_channels = 0;
748 a2b-> input_channels = mftTag-> input_channels[0];
749 a2b->output_channels = mftTag->output_channels[0];
750
751 // We require exactly three (ie XYZ/Lab/RGB) output channels
752 if (a2b->output_channels != ARRAY_COUNT(a2b->output_curves)) {
753 return false;
754 }
755 // We require at least one, and no more than four (ie CMYK) input channels
756 if (a2b->input_channels < 1 || a2b->input_channels > ARRAY_COUNT(a2b->input_curves)) {
757 return false;
758 }
759
760 for (uint32_t i = 0; i < a2b->input_channels; ++i) {
761 a2b->grid_points[i] = mftTag->grid_points[0];
762 }
763 // The grid only makes sense with at least two points along each axis
764 if (a2b->grid_points[0] < 2) {
765 return false;
766 }
767 return true;
768 }
769
770 // All as the A2B version above, except where noted.
771 static bool read_mft_common(const mft_CommonLayout* mftTag, skcms_B2A* b2a) {
772 // Same as A2B.
773 b2a->matrix_channels = 0;
774 b2a-> input_channels = mftTag-> input_channels[0];
775 b2a->output_channels = mftTag->output_channels[0];
776
777
778 // For B2A, exactly 3 input channels (XYZ) and 3 (RGB) or 4 (CMYK) output channels.
779 if (b2a->input_channels != ARRAY_COUNT(b2a->input_curves)) {
780 return false;
781 }
782 if (b2a->output_channels < 3 || b2a->output_channels > ARRAY_COUNT(b2a->output_curves)) {
783 return false;
784 }
785
786 // Same as A2B.
787 for (uint32_t i = 0; i < b2a->input_channels; ++i) {
788 b2a->grid_points[i] = mftTag->grid_points[0];
789 }
790 if (b2a->grid_points[0] < 2) {
791 return false;
792 }
793 return true;
794 }
795
796 template <typename A2B_or_B2A>
797 static bool init_tables(const uint8_t* table_base, uint64_t max_tables_len, uint32_t byte_width,
798 uint32_t input_table_entries, uint32_t output_table_entries,
799 A2B_or_B2A* out) {
800 // byte_width is 1 or 2, [input|output]_table_entries are in [2, 4096], so no overflow
801 uint32_t byte_len_per_input_table = input_table_entries * byte_width;
802 uint32_t byte_len_per_output_table = output_table_entries * byte_width;
803
804 // [input|output]_channels are <= 4, so still no overflow
805 uint32_t byte_len_all_input_tables = out->input_channels * byte_len_per_input_table;
806 uint32_t byte_len_all_output_tables = out->output_channels * byte_len_per_output_table;
807
808 uint64_t grid_size = out->output_channels * byte_width;
809 for (uint32_t axis = 0; axis < out->input_channels; ++axis) {
810 grid_size *= out->grid_points[axis];
811 }
812
813 if (max_tables_len < byte_len_all_input_tables + grid_size + byte_len_all_output_tables) {
814 return false;
815 }
816
817 for (uint32_t i = 0; i < out->input_channels; ++i) {
818 out->input_curves[i].table_entries = input_table_entries;
819 if (byte_width == 1) {
820 out->input_curves[i].table_8 = table_base + i * byte_len_per_input_table;
821 out->input_curves[i].table_16 = nullptr;
822 } else {
823 out->input_curves[i].table_8 = nullptr;
824 out->input_curves[i].table_16 = table_base + i * byte_len_per_input_table;
825 }
826 }
827
828 if (byte_width == 1) {
829 out->grid_8 = table_base + byte_len_all_input_tables;
830 out->grid_16 = nullptr;
831 } else {
832 out->grid_8 = nullptr;
833 out->grid_16 = table_base + byte_len_all_input_tables;
834 }
835
836 const uint8_t* output_table_base = table_base + byte_len_all_input_tables + grid_size;
837 for (uint32_t i = 0; i < out->output_channels; ++i) {
838 out->output_curves[i].table_entries = output_table_entries;
839 if (byte_width == 1) {
840 out->output_curves[i].table_8 = output_table_base + i * byte_len_per_output_table;
841 out->output_curves[i].table_16 = nullptr;
842 } else {
843 out->output_curves[i].table_8 = nullptr;
844 out->output_curves[i].table_16 = output_table_base + i * byte_len_per_output_table;
845 }
846 }
847
848 return true;
849 }
850
851 template <typename A2B_or_B2A>
852 static bool read_tag_mft1(const skcms_ICCTag* tag, A2B_or_B2A* out) {
853 if (tag->size < SAFE_FIXED_SIZE(mft1_Layout)) {
854 return false;
855 }
856
857 const mft1_Layout* mftTag = (const mft1_Layout*)tag->buf;
858 if (!read_mft_common(mftTag->common, out)) {
859 return false;
860 }
861
862 uint32_t input_table_entries = 256;
863 uint32_t output_table_entries = 256;
864
865 return init_tables(mftTag->variable, tag->size - SAFE_FIXED_SIZE(mft1_Layout), 1,
866 input_table_entries, output_table_entries, out);
867 }
868
869 template <typename A2B_or_B2A>
870 static bool read_tag_mft2(const skcms_ICCTag* tag, A2B_or_B2A* out) {
871 if (tag->size < SAFE_FIXED_SIZE(mft2_Layout)) {
872 return false;
873 }
874
875 const mft2_Layout* mftTag = (const mft2_Layout*)tag->buf;
876 if (!read_mft_common(mftTag->common, out)) {
877 return false;
878 }
879
880 uint32_t input_table_entries = read_big_u16(mftTag->input_table_entries);
881 uint32_t output_table_entries = read_big_u16(mftTag->output_table_entries);
882
883 // ICC spec mandates that 2 <= table_entries <= 4096
884 if (input_table_entries < 2 || input_table_entries > 4096 ||
885 output_table_entries < 2 || output_table_entries > 4096) {
886 return false;
887 }
888
889 return init_tables(mftTag->variable, tag->size - SAFE_FIXED_SIZE(mft2_Layout), 2,
890 input_table_entries, output_table_entries, out);
891 }
892
893 static bool read_curves(const uint8_t* buf, uint32_t size, uint32_t curve_offset,
894 uint32_t num_curves, skcms_Curve* curves) {
895 for (uint32_t i = 0; i < num_curves; ++i) {
896 if (curve_offset > size) {
897 return false;
898 }
899
900 uint32_t curve_bytes;
901 if (!read_curve(buf + curve_offset, size - curve_offset, &curves[i], &curve_bytes)) {
902 return false;
903 }
904
905 if (curve_bytes > UINT32_MAX - 3) {
906 return false;
907 }
908 curve_bytes = (curve_bytes + 3) & ~3U;
909
910 uint64_t new_offset_64 = (uint64_t)curve_offset + curve_bytes;
911 curve_offset = (uint32_t)new_offset_64;
912 if (new_offset_64 != curve_offset) {
913 return false;
914 }
915 }
916
917 return true;
918 }
919
920 // mAB and mBA tags use the same encoding, including color lookup tables.
921 typedef struct {
922 uint8_t type [ 4];
923 uint8_t reserved_a [ 4];
924 uint8_t input_channels [ 1];
925 uint8_t output_channels [ 1];
926 uint8_t reserved_b [ 2];
927 uint8_t b_curve_offset [ 4];
928 uint8_t matrix_offset [ 4];
929 uint8_t m_curve_offset [ 4];
930 uint8_t clut_offset [ 4];
931 uint8_t a_curve_offset [ 4];
932 } mAB_or_mBA_Layout;
933
934 typedef struct {
935 uint8_t grid_points [16];
936 uint8_t grid_byte_width [ 1];
937 uint8_t reserved [ 3];
938 uint8_t variable [1/*variable*/];
939 } CLUT_Layout;
940
941 static bool read_tag_mab(const skcms_ICCTag* tag, skcms_A2B* a2b, bool pcs_is_xyz) {
942 if (tag->size < SAFE_SIZEOF(mAB_or_mBA_Layout)) {
943 return false;
944 }
945
946 const mAB_or_mBA_Layout* mABTag = (const mAB_or_mBA_Layout*)tag->buf;
947
948 a2b->input_channels = mABTag->input_channels[0];
949 a2b->output_channels = mABTag->output_channels[0];
950
951 // We require exactly three (ie XYZ/Lab/RGB) output channels
952 if (a2b->output_channels != ARRAY_COUNT(a2b->output_curves)) {
953 return false;
954 }
955 // We require no more than four (ie CMYK) input channels
956 if (a2b->input_channels > ARRAY_COUNT(a2b->input_curves)) {
957 return false;
958 }
959
960 uint32_t b_curve_offset = read_big_u32(mABTag->b_curve_offset);
961 uint32_t matrix_offset = read_big_u32(mABTag->matrix_offset);
962 uint32_t m_curve_offset = read_big_u32(mABTag->m_curve_offset);
963 uint32_t clut_offset = read_big_u32(mABTag->clut_offset);
964 uint32_t a_curve_offset = read_big_u32(mABTag->a_curve_offset);
965
966 // "B" curves must be present
967 if (0 == b_curve_offset) {
968 return false;
969 }
970
971 if (!read_curves(tag->buf, tag->size, b_curve_offset, a2b->output_channels,
972 a2b->output_curves)) {
973 return false;
974 }
975
976 // "M" curves and Matrix must be used together
977 if (0 != m_curve_offset) {
978 if (0 == matrix_offset) {
979 return false;
980 }
981 a2b->matrix_channels = a2b->output_channels;
982 if (!read_curves(tag->buf, tag->size, m_curve_offset, a2b->matrix_channels,
983 a2b->matrix_curves)) {
984 return false;
985 }
986
987 // Read matrix, which is stored as a row-major 3x3, followed by the fourth column
988 if (tag->size < matrix_offset + 12 * SAFE_SIZEOF(uint32_t)) {
989 return false;
990 }
991 float encoding_factor = pcs_is_xyz ? (65535 / 32768.0f) : 1.0f;
992 const uint8_t* mtx_buf = tag->buf + matrix_offset;
993 a2b->matrix.vals[0][0] = encoding_factor * read_big_fixed(mtx_buf + 0);
994 a2b->matrix.vals[0][1] = encoding_factor * read_big_fixed(mtx_buf + 4);
995 a2b->matrix.vals[0][2] = encoding_factor * read_big_fixed(mtx_buf + 8);
996 a2b->matrix.vals[1][0] = encoding_factor * read_big_fixed(mtx_buf + 12);
997 a2b->matrix.vals[1][1] = encoding_factor * read_big_fixed(mtx_buf + 16);
998 a2b->matrix.vals[1][2] = encoding_factor * read_big_fixed(mtx_buf + 20);
999 a2b->matrix.vals[2][0] = encoding_factor * read_big_fixed(mtx_buf + 24);
1000 a2b->matrix.vals[2][1] = encoding_factor * read_big_fixed(mtx_buf + 28);
1001 a2b->matrix.vals[2][2] = encoding_factor * read_big_fixed(mtx_buf + 32);
1002 a2b->matrix.vals[0][3] = encoding_factor * read_big_fixed(mtx_buf + 36);
1003 a2b->matrix.vals[1][3] = encoding_factor * read_big_fixed(mtx_buf + 40);
1004 a2b->matrix.vals[2][3] = encoding_factor * read_big_fixed(mtx_buf + 44);
1005 } else {
1006 if (0 != matrix_offset) {
1007 return false;
1008 }
1009 a2b->matrix_channels = 0;
1010 }
1011
1012 // "A" curves and CLUT must be used together
1013 if (0 != a_curve_offset) {
1014 if (0 == clut_offset) {
1015 return false;
1016 }
1017 if (!read_curves(tag->buf, tag->size, a_curve_offset, a2b->input_channels,
1018 a2b->input_curves)) {
1019 return false;
1020 }
1021
1022 if (tag->size < clut_offset + SAFE_FIXED_SIZE(CLUT_Layout)) {
1023 return false;
1024 }
1025 const CLUT_Layout* clut = (const CLUT_Layout*)(tag->buf + clut_offset);
1026
1027 if (clut->grid_byte_width[0] == 1) {
1028 a2b->grid_8 = clut->variable;
1029 a2b->grid_16 = nullptr;
1030 } else if (clut->grid_byte_width[0] == 2) {
1031 a2b->grid_8 = nullptr;
1032 a2b->grid_16 = clut->variable;
1033 } else {
1034 return false;
1035 }
1036
1037 uint64_t grid_size = a2b->output_channels * clut->grid_byte_width[0]; // the payload
1038 for (uint32_t i = 0; i < a2b->input_channels; ++i) {
1039 a2b->grid_points[i] = clut->grid_points[i];
1040 // The grid only makes sense with at least two points along each axis
1041 if (a2b->grid_points[i] < 2) {
1042 return false;
1043 }
1044 grid_size *= a2b->grid_points[i];
1045 }
1046 if (tag->size < clut_offset + SAFE_FIXED_SIZE(CLUT_Layout) + grid_size) {
1047 return false;
1048 }
1049 } else {
1050 if (0 != clut_offset) {
1051 return false;
1052 }
1053
1054 // If there is no CLUT, the number of input and output channels must match
1055 if (a2b->input_channels != a2b->output_channels) {
1056 return false;
1057 }
1058
1059 // Zero out the number of input channels to signal that we're skipping this stage
1060 a2b->input_channels = 0;
1061 }
1062
1063 return true;
1064 }
1065
1066 // Exactly the same as read_tag_mab(), except where there are comments.
1067 // TODO: refactor the two to eliminate common code?
1068 static bool read_tag_mba(const skcms_ICCTag* tag, skcms_B2A* b2a, bool pcs_is_xyz) {
1069 if (tag->size < SAFE_SIZEOF(mAB_or_mBA_Layout)) {
1070 return false;
1071 }
1072
1073 const mAB_or_mBA_Layout* mBATag = (const mAB_or_mBA_Layout*)tag->buf;
1074
1075 b2a->input_channels = mBATag->input_channels[0];
1076 b2a->output_channels = mBATag->output_channels[0];
1077
1078 // Require exactly 3 inputs (XYZ) and 3 (RGB) or 4 (CMYK) outputs.
1079 if (b2a->input_channels != ARRAY_COUNT(b2a->input_curves)) {
1080 return false;
1081 }
1082 if (b2a->output_channels < 3 || b2a->output_channels > ARRAY_COUNT(b2a->output_curves)) {
1083 return false;
1084 }
1085
1086 uint32_t b_curve_offset = read_big_u32(mBATag->b_curve_offset);
1087 uint32_t matrix_offset = read_big_u32(mBATag->matrix_offset);
1088 uint32_t m_curve_offset = read_big_u32(mBATag->m_curve_offset);
1089 uint32_t clut_offset = read_big_u32(mBATag->clut_offset);
1090 uint32_t a_curve_offset = read_big_u32(mBATag->a_curve_offset);
1091
1092 if (0 == b_curve_offset) {
1093 return false;
1094 }
1095
1096 // "B" curves are our inputs, not outputs.
1097 if (!read_curves(tag->buf, tag->size, b_curve_offset, b2a->input_channels,
1098 b2a->input_curves)) {
1099 return false;
1100 }
1101
1102 if (0 != m_curve_offset) {
1103 if (0 == matrix_offset) {
1104 return false;
1105 }
1106 // Matrix channels is tied to input_channels (3), not output_channels.
1107 b2a->matrix_channels = b2a->input_channels;
1108
1109 if (!read_curves(tag->buf, tag->size, m_curve_offset, b2a->matrix_channels,
1110 b2a->matrix_curves)) {
1111 return false;
1112 }
1113
1114 if (tag->size < matrix_offset + 12 * SAFE_SIZEOF(uint32_t)) {
1115 return false;
1116 }
1117 float encoding_factor = pcs_is_xyz ? (32768 / 65535.0f) : 1.0f; // TODO: understand
1118 const uint8_t* mtx_buf = tag->buf + matrix_offset;
1119 b2a->matrix.vals[0][0] = encoding_factor * read_big_fixed(mtx_buf + 0);
1120 b2a->matrix.vals[0][1] = encoding_factor * read_big_fixed(mtx_buf + 4);
1121 b2a->matrix.vals[0][2] = encoding_factor * read_big_fixed(mtx_buf + 8);
1122 b2a->matrix.vals[1][0] = encoding_factor * read_big_fixed(mtx_buf + 12);
1123 b2a->matrix.vals[1][1] = encoding_factor * read_big_fixed(mtx_buf + 16);
1124 b2a->matrix.vals[1][2] = encoding_factor * read_big_fixed(mtx_buf + 20);
1125 b2a->matrix.vals[2][0] = encoding_factor * read_big_fixed(mtx_buf + 24);
1126 b2a->matrix.vals[2][1] = encoding_factor * read_big_fixed(mtx_buf + 28);
1127 b2a->matrix.vals[2][2] = encoding_factor * read_big_fixed(mtx_buf + 32);
1128 b2a->matrix.vals[0][3] = encoding_factor * read_big_fixed(mtx_buf + 36);
1129 b2a->matrix.vals[1][3] = encoding_factor * read_big_fixed(mtx_buf + 40);
1130 b2a->matrix.vals[2][3] = encoding_factor * read_big_fixed(mtx_buf + 44);
1131 } else {
1132 if (0 != matrix_offset) {
1133 return false;
1134 }
1135 b2a->matrix_channels = 0;
1136 }
1137
1138 if (0 != a_curve_offset) {
1139 if (0 == clut_offset) {
1140 return false;
1141 }
1142
1143 // "A" curves are our output, not input.
1144 if (!read_curves(tag->buf, tag->size, a_curve_offset, b2a->output_channels,
1145 b2a->output_curves)) {
1146 return false;
1147 }
1148
1149 if (tag->size < clut_offset + SAFE_FIXED_SIZE(CLUT_Layout)) {
1150 return false;
1151 }
1152 const CLUT_Layout* clut = (const CLUT_Layout*)(tag->buf + clut_offset);
1153
1154 if (clut->grid_byte_width[0] == 1) {
1155 b2a->grid_8 = clut->variable;
1156 b2a->grid_16 = nullptr;
1157 } else if (clut->grid_byte_width[0] == 2) {
1158 b2a->grid_8 = nullptr;
1159 b2a->grid_16 = clut->variable;
1160 } else {
1161 return false;
1162 }
1163
1164 uint64_t grid_size = b2a->output_channels * clut->grid_byte_width[0];
1165 for (uint32_t i = 0; i < b2a->input_channels; ++i) {
1166 b2a->grid_points[i] = clut->grid_points[i];
1167 if (b2a->grid_points[i] < 2) {
1168 return false;
1169 }
1170 grid_size *= b2a->grid_points[i];
1171 }
1172 if (tag->size < clut_offset + SAFE_FIXED_SIZE(CLUT_Layout) + grid_size) {
1173 return false;
1174 }
1175 } else {
1176 if (0 != clut_offset) {
1177 return false;
1178 }
1179
1180 if (b2a->input_channels != b2a->output_channels) {
1181 return false;
1182 }
1183
1184 // Zero out *output* channels to skip this stage.
1185 b2a->output_channels = 0;
1186 }
1187 return true;
1188 }
1189
1190 // If you pass f, we'll fit a possibly-non-zero value for *f.
1191 // If you pass nullptr, we'll assume you want *f to be treated as zero.
1192 static int fit_linear(const skcms_Curve* curve, int N, float tol,
1193 float* c, float* d, float* f = nullptr) {
1194 assert(N > 1);
1195 // We iteratively fit the first points to the TF's linear piece.
1196 // We want the cx + f line to pass through the first and last points we fit exactly.
1197 //
1198 // As we walk along the points we find the minimum and maximum slope of the line before the
1199 // error would exceed our tolerance. We stop when the range [slope_min, slope_max] becomes
1200 // emtpy, when we definitely can't add any more points.
1201 //
1202 // Some points' error intervals may intersect the running interval but not lie fully
1203 // within it. So we keep track of the last point we saw that is a valid end point candidate,
1204 // and once the search is done, back up to build the line through *that* point.
1205 const float dx = 1.0f / static_cast<float>(N - 1);
1206
1207 int lin_points = 1;
1208
1209 float f_zero = 0.0f;
1210 if (f) {
1211 *f = eval_curve(curve, 0);
1212 } else {
1213 f = &f_zero;
1214 }
1215
1216
1217 float slope_min = -INFINITY_;
1218 float slope_max = +INFINITY_;
1219 for (int i = 1; i < N; ++i) {
1220 float x = static_cast<float>(i) * dx;
1221 float y = eval_curve(curve, x);
1222
1223 float slope_max_i = (y + tol - *f) / x,
1224 slope_min_i = (y - tol - *f) / x;
1225 if (slope_max_i < slope_min || slope_max < slope_min_i) {
1226 // Slope intervals would no longer overlap.
1227 break;
1228 }
1229 slope_max = fminf_(slope_max, slope_max_i);
1230 slope_min = fmaxf_(slope_min, slope_min_i);
1231
1232 float cur_slope = (y - *f) / x;
1233 if (slope_min <= cur_slope && cur_slope <= slope_max) {
1234 lin_points = i + 1;
1235 *c = cur_slope;
1236 }
1237 }
1238
1239 // Set D to the last point that met our tolerance.
1240 *d = static_cast<float>(lin_points - 1) * dx;
1241 return lin_points;
1242 }
1243
1244 // If this skcms_Curve holds an identity table, rewrite it as an identity skcms_TransferFunction.
1245 static void canonicalize_identity(skcms_Curve* curve) {
1246 if (curve->table_entries && curve->table_entries <= (uint32_t)INT_MAX) {
1247 int N = (int)curve->table_entries;
1248
1249 float c = 0.0f, d = 0.0f, f = 0.0f;
1250 if (N == fit_linear(curve, N, 1.0f/static_cast<float>(2*N), &c,&d,&f)
1251 && c == 1.0f
1252 && f == 0.0f) {
1253 curve->table_entries = 0;
1254 curve->table_8 = nullptr;
1255 curve->table_16 = nullptr;
1256 curve->parametric = skcms_TransferFunction{1,1,0,0,0,0,0};
1257 }
1258 }
1259 }
1260
1261 static bool read_a2b(const skcms_ICCTag* tag, skcms_A2B* a2b, bool pcs_is_xyz) {
1262 bool ok = false;
1263 if (tag->type == skcms_Signature_mft1) { ok = read_tag_mft1(tag, a2b); }
1264 if (tag->type == skcms_Signature_mft2) { ok = read_tag_mft2(tag, a2b); }
1265 if (tag->type == skcms_Signature_mAB ) { ok = read_tag_mab(tag, a2b, pcs_is_xyz); }
1266 if (!ok) {
1267 return false;
1268 }
1269
1270 if (a2b->input_channels > 0) { canonicalize_identity(a2b->input_curves + 0); }
1271 if (a2b->input_channels > 1) { canonicalize_identity(a2b->input_curves + 1); }
1272 if (a2b->input_channels > 2) { canonicalize_identity(a2b->input_curves + 2); }
1273 if (a2b->input_channels > 3) { canonicalize_identity(a2b->input_curves + 3); }
1274
1275 if (a2b->matrix_channels > 0) { canonicalize_identity(a2b->matrix_curves + 0); }
1276 if (a2b->matrix_channels > 1) { canonicalize_identity(a2b->matrix_curves + 1); }
1277 if (a2b->matrix_channels > 2) { canonicalize_identity(a2b->matrix_curves + 2); }
1278
1279 if (a2b->output_channels > 0) { canonicalize_identity(a2b->output_curves + 0); }
1280 if (a2b->output_channels > 1) { canonicalize_identity(a2b->output_curves + 1); }
1281 if (a2b->output_channels > 2) { canonicalize_identity(a2b->output_curves + 2); }
1282
1283 return true;
1284 }
1285
1286 static bool read_b2a(const skcms_ICCTag* tag, skcms_B2A* b2a, bool pcs_is_xyz) {
1287 bool ok = false;
1288 if (tag->type == skcms_Signature_mft1) { ok = read_tag_mft1(tag, b2a); }
1289 if (tag->type == skcms_Signature_mft2) { ok = read_tag_mft2(tag, b2a); }
1290 if (tag->type == skcms_Signature_mBA ) { ok = read_tag_mba(tag, b2a, pcs_is_xyz); }
1291 if (!ok) {
1292 return false;
1293 }
1294
1295 if (b2a->input_channels > 0) { canonicalize_identity(b2a->input_curves + 0); }
1296 if (b2a->input_channels > 1) { canonicalize_identity(b2a->input_curves + 1); }
1297 if (b2a->input_channels > 2) { canonicalize_identity(b2a->input_curves + 2); }
1298
1299 if (b2a->matrix_channels > 0) { canonicalize_identity(b2a->matrix_curves + 0); }
1300 if (b2a->matrix_channels > 1) { canonicalize_identity(b2a->matrix_curves + 1); }
1301 if (b2a->matrix_channels > 2) { canonicalize_identity(b2a->matrix_curves + 2); }
1302
1303 if (b2a->output_channels > 0) { canonicalize_identity(b2a->output_curves + 0); }
1304 if (b2a->output_channels > 1) { canonicalize_identity(b2a->output_curves + 1); }
1305 if (b2a->output_channels > 2) { canonicalize_identity(b2a->output_curves + 2); }
1306 if (b2a->output_channels > 3) { canonicalize_identity(b2a->output_curves + 3); }
1307
1308 return true;
1309 }
1310
1311 typedef struct {
1312 uint8_t type [4];
1313 uint8_t reserved [4];
1314 uint8_t color_primaries [1];
1315 uint8_t transfer_characteristics [1];
1316 uint8_t matrix_coefficients [1];
1317 uint8_t video_full_range_flag [1];
1318 } CICP_Layout;
1319
1320 static bool read_cicp(const skcms_ICCTag* tag, skcms_CICP* cicp) {
1321 if (tag->type != skcms_Signature_CICP || tag->size < SAFE_SIZEOF(CICP_Layout)) {
1322 return false;
1323 }
1324
1325 const CICP_Layout* cicpTag = (const CICP_Layout*)tag->buf;
1326
1327 cicp->color_primaries = cicpTag->color_primaries[0];
1328 cicp->transfer_characteristics = cicpTag->transfer_characteristics[0];
1329 cicp->matrix_coefficients = cicpTag->matrix_coefficients[0];
1330 cicp->video_full_range_flag = cicpTag->video_full_range_flag[0];
1331 return true;
1332 }
1333
1334 void skcms_GetTagByIndex(const skcms_ICCProfile* profile, uint32_t idx, skcms_ICCTag* tag) {
1335 if (!profile || !profile->buffer || !tag) { return; }
1336 if (idx > profile->tag_count) { return; }
1337 const tag_Layout* tags = get_tag_table(profile);
1338 tag->signature = read_big_u32(tags[idx].signature);
1339 tag->size = read_big_u32(tags[idx].size);
1340 tag->buf = read_big_u32(tags[idx].offset) + profile->buffer;
1341 tag->type = read_big_u32(tag->buf);
1342 }
1343
1344 bool skcms_GetTagBySignature(const skcms_ICCProfile* profile, uint32_t sig, skcms_ICCTag* tag) {
1345 if (!profile || !profile->buffer || !tag) { return false; }
1346 const tag_Layout* tags = get_tag_table(profile);
1347 for (uint32_t i = 0; i < profile->tag_count; ++i) {
1348 if (read_big_u32(tags[i].signature) == sig) {
1349 tag->signature = sig;
1350 tag->size = read_big_u32(tags[i].size);
1351 tag->buf = read_big_u32(tags[i].offset) + profile->buffer;
1352 tag->type = read_big_u32(tag->buf);
1353 return true;
1354 }
1355 }
1356 return false;
1357 }
1358
1359 static bool usable_as_src(const skcms_ICCProfile* profile) {
1360 return profile->has_A2B
1361 || (profile->has_trc && profile->has_toXYZD50);
1362 }
1363
1364 bool skcms_ParseWithA2BPriority(const void* buf, size_t len,
1365 const int priority[], const int priorities,
1366 skcms_ICCProfile* profile) {
1367 static_assert(SAFE_SIZEOF(header_Layout) == 132, "need to update header code");
1368
1369 if (!profile) {
1370 return false;
1371 }
1372 memset(profile, 0, SAFE_SIZEOF(*profile));
1373
1374 if (len < SAFE_SIZEOF(header_Layout)) {
1375 return false;
1376 }
1377
1378 // Byte-swap all header fields
1379 const header_Layout* header = (const header_Layout*)buf;
1380 profile->buffer = (const uint8_t*)buf;
1381 profile->size = read_big_u32(header->size);
1382 uint32_t version = read_big_u32(header->version);
1383 profile->data_color_space = read_big_u32(header->data_color_space);
1384 profile->pcs = read_big_u32(header->pcs);
1385 uint32_t signature = read_big_u32(header->signature);
1386 float illuminant_X = read_big_fixed(header->illuminant_X);
1387 float illuminant_Y = read_big_fixed(header->illuminant_Y);
1388 float illuminant_Z = read_big_fixed(header->illuminant_Z);
1389 profile->tag_count = read_big_u32(header->tag_count);
1390
1391 // Validate signature, size (smaller than buffer, large enough to hold tag table),
1392 // and major version
1393 uint64_t tag_table_size = profile->tag_count * SAFE_SIZEOF(tag_Layout);
1394 if (signature != skcms_Signature_acsp ||
1395 profile->size > len ||
1396 profile->size < SAFE_SIZEOF(header_Layout) + tag_table_size ||
1397 (version >> 24) > 4) {
1398 return false;
1399 }
1400
1401 // Validate that illuminant is D50 white
1402 if (fabsf_(illuminant_X - 0.9642f) > 0.0100f ||
1403 fabsf_(illuminant_Y - 1.0000f) > 0.0100f ||
1404 fabsf_(illuminant_Z - 0.8249f) > 0.0100f) {
1405 return false;
1406 }
1407
1408 // Validate that all tag entries have sane offset + size
1409 const tag_Layout* tags = get_tag_table(profile);
1410 for (uint32_t i = 0; i < profile->tag_count; ++i) {
1411 uint32_t tag_offset = read_big_u32(tags[i].offset);
1412 uint32_t tag_size = read_big_u32(tags[i].size);
1413 uint64_t tag_end = (uint64_t)tag_offset + (uint64_t)tag_size;
1414 if (tag_size < 4 || tag_end > profile->size) {
1415 return false;
1416 }
1417 }
1418
1419 if (profile->pcs != skcms_Signature_XYZ && profile->pcs != skcms_Signature_Lab) {
1420 return false;
1421 }
1422
1423 bool pcs_is_xyz = profile->pcs == skcms_Signature_XYZ;
1424
1425 // Pre-parse commonly used tags.
1426 skcms_ICCTag kTRC;
1427 if (profile->data_color_space == skcms_Signature_Gray &&
1428 skcms_GetTagBySignature(profile, skcms_Signature_kTRC, &kTRC)) {
1429 if (!read_curve(kTRC.buf, kTRC.size, &profile->trc[0], nullptr)) {
1430 // Malformed tag
1431 return false;
1432 }
1433 profile->trc[1] = profile->trc[0];
1434 profile->trc[2] = profile->trc[0];
1435 profile->has_trc = true;
1436
1437 if (pcs_is_xyz) {
1438 profile->toXYZD50.vals[0][0] = illuminant_X;
1439 profile->toXYZD50.vals[1][1] = illuminant_Y;
1440 profile->toXYZD50.vals[2][2] = illuminant_Z;
1441 profile->has_toXYZD50 = true;
1442 }
1443 } else {
1444 skcms_ICCTag rTRC, gTRC, bTRC;
1445 if (skcms_GetTagBySignature(profile, skcms_Signature_rTRC, &rTRC) &&
1446 skcms_GetTagBySignature(profile, skcms_Signature_gTRC, &gTRC) &&
1447 skcms_GetTagBySignature(profile, skcms_Signature_bTRC, &bTRC)) {
1448 if (!read_curve(rTRC.buf, rTRC.size, &profile->trc[0], nullptr) ||
1449 !read_curve(gTRC.buf, gTRC.size, &profile->trc[1], nullptr) ||
1450 !read_curve(bTRC.buf, bTRC.size, &profile->trc[2], nullptr)) {
1451 // Malformed TRC tags
1452 return false;
1453 }
1454 profile->has_trc = true;
1455 }
1456
1457 skcms_ICCTag rXYZ, gXYZ, bXYZ;
1458 if (skcms_GetTagBySignature(profile, skcms_Signature_rXYZ, &rXYZ) &&
1459 skcms_GetTagBySignature(profile, skcms_Signature_gXYZ, &gXYZ) &&
1460 skcms_GetTagBySignature(profile, skcms_Signature_bXYZ, &bXYZ)) {
1461 if (!read_to_XYZD50(&rXYZ, &gXYZ, &bXYZ, &profile->toXYZD50)) {
1462 // Malformed XYZ tags
1463 return false;
1464 }
1465 profile->has_toXYZD50 = true;
1466 }
1467 }
1468
1469 for (int i = 0; i < priorities; i++) {
1470 // enum { perceptual, relative_colormetric, saturation }
1471 if (priority[i] < 0 || priority[i] > 2) {
1472 return false;
1473 }
1474 uint32_t sig = skcms_Signature_A2B0 + static_cast<uint32_t>(priority[i]);
1475 skcms_ICCTag tag;
1476 if (skcms_GetTagBySignature(profile, sig, &tag)) {
1477 if (!read_a2b(&tag, &profile->A2B, pcs_is_xyz)) {
1478 // Malformed A2B tag
1479 return false;
1480 }
1481 profile->has_A2B = true;
1482 break;
1483 }
1484 }
1485
1486 for (int i = 0; i < priorities; i++) {
1487 // enum { perceptual, relative_colormetric, saturation }
1488 if (priority[i] < 0 || priority[i] > 2) {
1489 return false;
1490 }
1491 uint32_t sig = skcms_Signature_B2A0 + static_cast<uint32_t>(priority[i]);
1492 skcms_ICCTag tag;
1493 if (skcms_GetTagBySignature(profile, sig, &tag)) {
1494 if (!read_b2a(&tag, &profile->B2A, pcs_is_xyz)) {
1495 // Malformed B2A tag
1496 return false;
1497 }
1498 profile->has_B2A = true;
1499 break;
1500 }
1501 }
1502
1503 skcms_ICCTag cicp_tag;
1504 if (skcms_GetTagBySignature(profile, skcms_Signature_CICP, &cicp_tag)) {
1505 if (!read_cicp(&cicp_tag, &profile->CICP)) {
1506 // Malformed CICP tag
1507 return false;
1508 }
1509 profile->has_CICP = true;
1510 }
1511
1512 return usable_as_src(profile);
1513 }
1514
1515
1516 const skcms_ICCProfile* skcms_sRGB_profile() {
1517 static const skcms_ICCProfile sRGB_profile = {
1518 nullptr, // buffer, moot here
1519
1520 0, // size, moot here
1521 skcms_Signature_RGB, // data_color_space
1522 skcms_Signature_XYZ, // pcs
1523 0, // tag count, moot here
1524
1525 // We choose to represent sRGB with its canonical transfer function,
1526 // and with its canonical XYZD50 gamut matrix.
1527 { // the 3 trc curves
1528 {{0, {2.4f, (float)(1/1.055), (float)(0.055/1.055), (float)(1/12.92), 0.04045f, 0, 0}}},
1529 {{0, {2.4f, (float)(1/1.055), (float)(0.055/1.055), (float)(1/12.92), 0.04045f, 0, 0}}},
1530 {{0, {2.4f, (float)(1/1.055), (float)(0.055/1.055), (float)(1/12.92), 0.04045f, 0, 0}}},
1531 },
1532
1533 {{ // 3x3 toXYZD50 matrix
1534 { 0.436065674f, 0.385147095f, 0.143066406f },
1535 { 0.222488403f, 0.716873169f, 0.060607910f },
1536 { 0.013916016f, 0.097076416f, 0.714096069f },
1537 }},
1538
1539 { // an empty A2B
1540 { // input_curves
1541 {{0, {0,0, 0,0,0,0,0}}},
1542 {{0, {0,0, 0,0,0,0,0}}},
1543 {{0, {0,0, 0,0,0,0,0}}},
1544 {{0, {0,0, 0,0,0,0,0}}},
1545 },
1546 nullptr, // grid_8
1547 nullptr, // grid_16
1548 0, // input_channels
1549 {0,0,0,0}, // grid_points
1550
1551 { // matrix_curves
1552 {{0, {0,0, 0,0,0,0,0}}},
1553 {{0, {0,0, 0,0,0,0,0}}},
1554 {{0, {0,0, 0,0,0,0,0}}},
1555 },
1556 {{ // matrix (3x4)
1557 { 0,0,0,0 },
1558 { 0,0,0,0 },
1559 { 0,0,0,0 },
1560 }},
1561 0, // matrix_channels
1562
1563 0, // output_channels
1564 { // output_curves
1565 {{0, {0,0, 0,0,0,0,0}}},
1566 {{0, {0,0, 0,0,0,0,0}}},
1567 {{0, {0,0, 0,0,0,0,0}}},
1568 },
1569 },
1570
1571 { // an empty B2A
1572 { // input_curves
1573 {{0, {0,0, 0,0,0,0,0}}},
1574 {{0, {0,0, 0,0,0,0,0}}},
1575 {{0, {0,0, 0,0,0,0,0}}},
1576 },
1577 0, // input_channels
1578
1579 0, // matrix_channels
1580 { // matrix_curves
1581 {{0, {0,0, 0,0,0,0,0}}},
1582 {{0, {0,0, 0,0,0,0,0}}},
1583 {{0, {0,0, 0,0,0,0,0}}},
1584 },
1585 {{ // matrix (3x4)
1586 { 0,0,0,0 },
1587 { 0,0,0,0 },
1588 { 0,0,0,0 },
1589 }},
1590
1591 { // output_curves
1592 {{0, {0,0, 0,0,0,0,0}}},
1593 {{0, {0,0, 0,0,0,0,0}}},
1594 {{0, {0,0, 0,0,0,0,0}}},
1595 {{0, {0,0, 0,0,0,0,0}}},
1596 },
1597 nullptr, // grid_8
1598 nullptr, // grid_16
1599 {0,0,0,0}, // grid_points
1600 0, // output_channels
1601 },
1602
1603 { 0, 0, 0, 0 }, // an empty CICP
1604
1605 true, // has_trc
1606 true, // has_toXYZD50
1607 false, // has_A2B
1608 false, // has B2A
1609 false, // has_CICP
1610 };
1611 return &sRGB_profile;
1612 }
1613
1614 const skcms_ICCProfile* skcms_XYZD50_profile() {
1615 // Just like sRGB above, but with identity transfer functions and toXYZD50 matrix.
1616 static const skcms_ICCProfile XYZD50_profile = {
1617 nullptr, // buffer, moot here
1618
1619 0, // size, moot here
1620 skcms_Signature_RGB, // data_color_space
1621 skcms_Signature_XYZ, // pcs
1622 0, // tag count, moot here
1623
1624 { // the 3 trc curves
1625 {{0, {1,1, 0,0,0,0,0}}},
1626 {{0, {1,1, 0,0,0,0,0}}},
1627 {{0, {1,1, 0,0,0,0,0}}},
1628 },
1629
1630 {{ // 3x3 toXYZD50 matrix
1631 { 1,0,0 },
1632 { 0,1,0 },
1633 { 0,0,1 },
1634 }},
1635
1636 { // an empty A2B
1637 { // input_curves
1638 {{0, {0,0, 0,0,0,0,0}}},
1639 {{0, {0,0, 0,0,0,0,0}}},
1640 {{0, {0,0, 0,0,0,0,0}}},
1641 {{0, {0,0, 0,0,0,0,0}}},
1642 },
1643 nullptr, // grid_8
1644 nullptr, // grid_16
1645 0, // input_channels
1646 {0,0,0,0}, // grid_points
1647
1648 { // matrix_curves
1649 {{0, {0,0, 0,0,0,0,0}}},
1650 {{0, {0,0, 0,0,0,0,0}}},
1651 {{0, {0,0, 0,0,0,0,0}}},
1652 },
1653 {{ // matrix (3x4)
1654 { 0,0,0,0 },
1655 { 0,0,0,0 },
1656 { 0,0,0,0 },
1657 }},
1658 0, // matrix_channels
1659
1660 0, // output_channels
1661 { // output_curves
1662 {{0, {0,0, 0,0,0,0,0}}},
1663 {{0, {0,0, 0,0,0,0,0}}},
1664 {{0, {0,0, 0,0,0,0,0}}},
1665 },
1666 },
1667
1668 { // an empty B2A
1669 { // input_curves
1670 {{0, {0,0, 0,0,0,0,0}}},
1671 {{0, {0,0, 0,0,0,0,0}}},
1672 {{0, {0,0, 0,0,0,0,0}}},
1673 },
1674 0, // input_channels
1675
1676 0, // matrix_channels
1677 { // matrix_curves
1678 {{0, {0,0, 0,0,0,0,0}}},
1679 {{0, {0,0, 0,0,0,0,0}}},
1680 {{0, {0,0, 0,0,0,0,0}}},
1681 },
1682 {{ // matrix (3x4)
1683 { 0,0,0,0 },
1684 { 0,0,0,0 },
1685 { 0,0,0,0 },
1686 }},
1687
1688 { // output_curves
1689 {{0, {0,0, 0,0,0,0,0}}},
1690 {{0, {0,0, 0,0,0,0,0}}},
1691 {{0, {0,0, 0,0,0,0,0}}},
1692 {{0, {0,0, 0,0,0,0,0}}},
1693 },
1694 nullptr, // grid_8
1695 nullptr, // grid_16
1696 {0,0,0,0}, // grid_points
1697 0, // output_channels
1698 },
1699
1700 { 0, 0, 0, 0 }, // an empty CICP
1701
1702 true, // has_trc
1703 true, // has_toXYZD50
1704 false, // has_A2B
1705 false, // has B2A
1706 false, // has_CICP
1707 };
1708
1709 return &XYZD50_profile;
1710 }
1711
1712 const skcms_TransferFunction* skcms_sRGB_TransferFunction() {
1713 return &skcms_sRGB_profile()->trc[0].parametric;
1714 }
1715
1716 const skcms_TransferFunction* skcms_sRGB_Inverse_TransferFunction() {
1717 static const skcms_TransferFunction sRGB_inv =
1718 {0.416666657f, 1.137283325f, -0.0f, 12.920000076f, 0.003130805f, -0.054969788f, -0.0f};
1719 return &sRGB_inv;
1720 }
1721
1722 const skcms_TransferFunction* skcms_Identity_TransferFunction() {
1723 static const skcms_TransferFunction identity = {1,1,0,0,0,0,0};
1724 return &identity;
1725 }
1726
1727 const uint8_t skcms_252_random_bytes[] = {
1728 8, 179, 128, 204, 253, 38, 134, 184, 68, 102, 32, 138, 99, 39, 169, 215,
1729 119, 26, 3, 223, 95, 239, 52, 132, 114, 74, 81, 234, 97, 116, 244, 205, 30,
1730 154, 173, 12, 51, 159, 122, 153, 61, 226, 236, 178, 229, 55, 181, 220, 191,
1731 194, 160, 126, 168, 82, 131, 18, 180, 245, 163, 22, 246, 69, 235, 252, 57,
1732 108, 14, 6, 152, 240, 255, 171, 242, 20, 227, 177, 238, 96, 85, 16, 211,
1733 70, 200, 149, 155, 146, 127, 145, 100, 151, 109, 19, 165, 208, 195, 164,
1734 137, 254, 182, 248, 64, 201, 45, 209, 5, 147, 207, 210, 113, 162, 83, 225,
1735 9, 31, 15, 231, 115, 37, 58, 53, 24, 49, 197, 56, 120, 172, 48, 21, 214,
1736 129, 111, 11, 50, 187, 196, 34, 60, 103, 71, 144, 47, 203, 77, 80, 232,
1737 140, 222, 250, 206, 166, 247, 139, 249, 221, 72, 106, 27, 199, 117, 54,
1738 219, 135, 118, 40, 79, 41, 251, 46, 93, 212, 92, 233, 148, 28, 121, 63,
1739 123, 158, 105, 59, 29, 42, 143, 23, 0, 107, 176, 87, 104, 183, 156, 193,
1740 189, 90, 188, 65, 190, 17, 198, 7, 186, 161, 1, 124, 78, 125, 170, 133,
1741 174, 218, 67, 157, 75, 101, 89, 217, 62, 33, 141, 228, 25, 35, 91, 230, 4,
1742 2, 13, 73, 86, 167, 237, 84, 243, 44, 185, 66, 130, 110, 150, 142, 216, 88,
1743 112, 36, 224, 136, 202, 76, 94, 98, 175, 213
1744 };
1745
1746 bool skcms_ApproximatelyEqualProfiles(const skcms_ICCProfile* A, const skcms_ICCProfile* B) {
1747 // Test for exactly equal profiles first.
1748 if (A == B || 0 == memcmp(A,B, sizeof(skcms_ICCProfile))) {
1749 return true;
1750 }
1751
1752 // For now this is the essentially the same strategy we use in test_only.c
1753 // for our skcms_Transform() smoke tests:
1754 // 1) transform A to XYZD50
1755 // 2) transform B to XYZD50
1756 // 3) return true if they're similar enough
1757 // Our current criterion in 3) is maximum 1 bit error per XYZD50 byte.
1758
1759 // skcms_252_random_bytes are 252 of a random shuffle of all possible bytes.
1760 // 252 is evenly divisible by 3 and 4. Only 192, 10, 241, and 43 are missing.
1761
1762 // We want to allow otherwise equivalent profiles tagged as grayscale and RGB
1763 // to be treated as equal. But CMYK profiles are a totally different ballgame.
1764 const auto CMYK = skcms_Signature_CMYK;
1765 if ((A->data_color_space == CMYK) != (B->data_color_space == CMYK)) {
1766 return false;
1767 }
1768
1769 // Interpret as RGB_888 if data color space is RGB or GRAY, RGBA_8888 if CMYK.
1770 // TODO: working with RGBA_8888 either way is probably fastest.
1771 skcms_PixelFormat fmt = skcms_PixelFormat_RGB_888;
1772 size_t npixels = 84;
1773 if (A->data_color_space == skcms_Signature_CMYK) {
1774 fmt = skcms_PixelFormat_RGBA_8888;
1775 npixels = 63;
1776 }
1777
1778 // TODO: if A or B is a known profile (skcms_sRGB_profile, skcms_XYZD50_profile),
1779 // use pre-canned results and skip that skcms_Transform() call?
1780 uint8_t dstA[252],
1781 dstB[252];
1782 if (!skcms_Transform(
1783 skcms_252_random_bytes, fmt, skcms_AlphaFormat_Unpremul, A,
1784 dstA, skcms_PixelFormat_RGB_888, skcms_AlphaFormat_Unpremul, skcms_XYZD50_profile(),
1785 npixels)) {
1786 return false;
1787 }
1788 if (!skcms_Transform(
1789 skcms_252_random_bytes, fmt, skcms_AlphaFormat_Unpremul, B,
1790 dstB, skcms_PixelFormat_RGB_888, skcms_AlphaFormat_Unpremul, skcms_XYZD50_profile(),
1791 npixels)) {
1792 return false;
1793 }
1794
1795 // TODO: make sure this final check has reasonable codegen.
1796 for (size_t i = 0; i < 252; i++) {
1797 if (abs((int)dstA[i] - (int)dstB[i]) > 1) {
1798 return false;
1799 }
1800 }
1801 return true;
1802 }
1803
1804 bool skcms_TRCs_AreApproximateInverse(const skcms_ICCProfile* profile,
1805 const skcms_TransferFunction* inv_tf) {
1806 if (!profile || !profile->has_trc) {
1807 return false;
1808 }
1809
1810 return skcms_AreApproximateInverses(&profile->trc[0], inv_tf) &&
1811 skcms_AreApproximateInverses(&profile->trc[1], inv_tf) &&
1812 skcms_AreApproximateInverses(&profile->trc[2], inv_tf);
1813 }
1814
1815 static bool is_zero_to_one(float x) {
1816 return 0 <= x && x <= 1;
1817 }
1818
1819 typedef struct { float vals[3]; } skcms_Vector3;
1820
1821 static skcms_Vector3 mv_mul(const skcms_Matrix3x3* m, const skcms_Vector3* v) {
1822 skcms_Vector3 dst = {{0,0,0}};
1823 for (int row = 0; row < 3; ++row) {
1824 dst.vals[row] = m->vals[row][0] * v->vals[0]
1825 + m->vals[row][1] * v->vals[1]
1826 + m->vals[row][2] * v->vals[2];
1827 }
1828 return dst;
1829 }
1830
1831 bool skcms_AdaptToXYZD50(float wx, float wy,
1832 skcms_Matrix3x3* toXYZD50) {
1833 if (!is_zero_to_one(wx) || !is_zero_to_one(wy) ||
1834 !toXYZD50) {
1835 return false;
1836 }
1837
1838 // Assumes that Y is 1.0f.
1839 skcms_Vector3 wXYZ = { { wx / wy, 1, (1 - wx - wy) / wy } };
1840
1841 // Now convert toXYZ matrix to toXYZD50.
1842 skcms_Vector3 wXYZD50 = { { 0.96422f, 1.0f, 0.82521f } };
1843
1844 // Calculate the chromatic adaptation matrix. We will use the Bradford method, thus
1845 // the matrices below. The Bradford method is used by Adobe and is widely considered
1846 // to be the best.
1847 skcms_Matrix3x3 xyz_to_lms = {{
1848 { 0.8951f, 0.2664f, -0.1614f },
1849 { -0.7502f, 1.7135f, 0.0367f },
1850 { 0.0389f, -0.0685f, 1.0296f },
1851 }};
1852 skcms_Matrix3x3 lms_to_xyz = {{
1853 { 0.9869929f, -0.1470543f, 0.1599627f },
1854 { 0.4323053f, 0.5183603f, 0.0492912f },
1855 { -0.0085287f, 0.0400428f, 0.9684867f },
1856 }};
1857
1858 skcms_Vector3 srcCone = mv_mul(&xyz_to_lms, &wXYZ);
1859 skcms_Vector3 dstCone = mv_mul(&xyz_to_lms, &wXYZD50);
1860
1861 *toXYZD50 = {{
1862 { dstCone.vals[0] / srcCone.vals[0], 0, 0 },
1863 { 0, dstCone.vals[1] / srcCone.vals[1], 0 },
1864 { 0, 0, dstCone.vals[2] / srcCone.vals[2] },
1865 }};
1866 *toXYZD50 = skcms_Matrix3x3_concat(toXYZD50, &xyz_to_lms);
1867 *toXYZD50 = skcms_Matrix3x3_concat(&lms_to_xyz, toXYZD50);
1868
1869 return true;
1870 }
1871
1872 bool skcms_PrimariesToXYZD50(float rx, float ry,
1873 float gx, float gy,
1874 float bx, float by,
1875 float wx, float wy,
1876 skcms_Matrix3x3* toXYZD50) {
1877 if (!is_zero_to_one(rx) || !is_zero_to_one(ry) ||
1878 !is_zero_to_one(gx) || !is_zero_to_one(gy) ||
1879 !is_zero_to_one(bx) || !is_zero_to_one(by) ||
1880 !is_zero_to_one(wx) || !is_zero_to_one(wy) ||
1881 !toXYZD50) {
1882 return false;
1883 }
1884
1885 // First, we need to convert xy values (primaries) to XYZ.
1886 skcms_Matrix3x3 primaries = {{
1887 { rx, gx, bx },
1888 { ry, gy, by },
1889 { 1 - rx - ry, 1 - gx - gy, 1 - bx - by },
1890 }};
1891 skcms_Matrix3x3 primaries_inv;
1892 if (!skcms_Matrix3x3_invert(&primaries, &primaries_inv)) {
1893 return false;
1894 }
1895
1896 // Assumes that Y is 1.0f.
1897 skcms_Vector3 wXYZ = { { wx / wy, 1, (1 - wx - wy) / wy } };
1898 skcms_Vector3 XYZ = mv_mul(&primaries_inv, &wXYZ);
1899
1900 skcms_Matrix3x3 toXYZ = {{
1901 { XYZ.vals[0], 0, 0 },
1902 { 0, XYZ.vals[1], 0 },
1903 { 0, 0, XYZ.vals[2] },
1904 }};
1905 toXYZ = skcms_Matrix3x3_concat(&primaries, &toXYZ);
1906
1907 skcms_Matrix3x3 DXtoD50;
1908 if (!skcms_AdaptToXYZD50(wx, wy, &DXtoD50)) {
1909 return false;
1910 }
1911
1912 *toXYZD50 = skcms_Matrix3x3_concat(&DXtoD50, &toXYZ);
1913 return true;
1914 }
1915
1916
1917 bool skcms_Matrix3x3_invert(const skcms_Matrix3x3* src, skcms_Matrix3x3* dst) {
1918 double a00 = src->vals[0][0],
1919 a01 = src->vals[1][0],
1920 a02 = src->vals[2][0],
1921 a10 = src->vals[0][1],
1922 a11 = src->vals[1][1],
1923 a12 = src->vals[2][1],
1924 a20 = src->vals[0][2],
1925 a21 = src->vals[1][2],
1926 a22 = src->vals[2][2];
1927
1928 double b0 = a00*a11 - a01*a10,
1929 b1 = a00*a12 - a02*a10,
1930 b2 = a01*a12 - a02*a11,
1931 b3 = a20,
1932 b4 = a21,
1933 b5 = a22;
1934
1935 double determinant = b0*b5
1936 - b1*b4
1937 + b2*b3;
1938
1939 if (determinant == 0) {
1940 return false;
1941 }
1942
1943 double invdet = 1.0 / determinant;
1944 if (invdet > +FLT_MAX || invdet < -FLT_MAX || !isfinitef_((float)invdet)) {
1945 return false;
1946 }
1947
1948 b0 *= invdet;
1949 b1 *= invdet;
1950 b2 *= invdet;
1951 b3 *= invdet;
1952 b4 *= invdet;
1953 b5 *= invdet;
1954
1955 dst->vals[0][0] = (float)( a11*b5 - a12*b4 );
1956 dst->vals[1][0] = (float)( a02*b4 - a01*b5 );
1957 dst->vals[2][0] = (float)( + b2 );
1958 dst->vals[0][1] = (float)( a12*b3 - a10*b5 );
1959 dst->vals[1][1] = (float)( a00*b5 - a02*b3 );
1960 dst->vals[2][1] = (float)( - b1 );
1961 dst->vals[0][2] = (float)( a10*b4 - a11*b3 );
1962 dst->vals[1][2] = (float)( a01*b3 - a00*b4 );
1963 dst->vals[2][2] = (float)( + b0 );
1964
1965 for (int r = 0; r < 3; ++r)
1966 for (int c = 0; c < 3; ++c) {
1967 if (!isfinitef_(dst->vals[r][c])) {
1968 return false;
1969 }
1970 }
1971 return true;
1972 }
1973
1974 skcms_Matrix3x3 skcms_Matrix3x3_concat(const skcms_Matrix3x3* A, const skcms_Matrix3x3* B) {
1975 skcms_Matrix3x3 m = { { { 0,0,0 },{ 0,0,0 },{ 0,0,0 } } };
1976 for (int r = 0; r < 3; r++)
1977 for (int c = 0; c < 3; c++) {
1978 m.vals[r][c] = A->vals[r][0] * B->vals[0][c]
1979 + A->vals[r][1] * B->vals[1][c]
1980 + A->vals[r][2] * B->vals[2][c];
1981 }
1982 return m;
1983 }
1984
1985 #if defined(__clang__)
1986 [[clang::no_sanitize("float-divide-by-zero")]] // Checked for by classify() on the way out.
1987 #endif
1988 bool skcms_TransferFunction_invert(const skcms_TransferFunction* src, skcms_TransferFunction* dst) {
1989 TF_PQish pq;
1990 TF_HLGish hlg;
1991 switch (classify(*src, &pq, &hlg)) {
1992 case skcms_TFType_Invalid: return false;
1993 case skcms_TFType_PQ: return false;
1994 case skcms_TFType_HLG: return false;
1995 case skcms_TFType_sRGBish: break; // handled below
1996
1997 case skcms_TFType_PQish:
1998 *dst = { TFKind_marker(skcms_TFType_PQish), -pq.A, pq.D, 1.0f/pq.F
1999 , pq.B, -pq.E, 1.0f/pq.C};
2000 return true;
2001
2002 case skcms_TFType_HLGish:
2003 *dst = { TFKind_marker(skcms_TFType_HLGinvish), 1.0f/hlg.R, 1.0f/hlg.G
2004 , 1.0f/hlg.a, hlg.b, hlg.c
2005 , hlg.K_minus_1 };
2006 return true;
2007
2008 case skcms_TFType_HLGinvish:
2009 *dst = { TFKind_marker(skcms_TFType_HLGish), 1.0f/hlg.R, 1.0f/hlg.G
2010 , 1.0f/hlg.a, hlg.b, hlg.c
2011 , hlg.K_minus_1 };
2012 return true;
2013 }
2014
2015 assert (classify(*src) == skcms_TFType_sRGBish);
2016
2017 // We're inverting this function, solving for x in terms of y.
2018 // y = (cx + f) x < d
2019 // (ax + b)^g + e x ≥ d
2020 // The inverse of this function can be expressed in the same piecewise form.
2021 skcms_TransferFunction inv = {0,0,0,0,0,0,0};
2022
2023 // We'll start by finding the new threshold inv.d.
2024 // In principle we should be able to find that by solving for y at x=d from either side.
2025 // (If those two d values aren't the same, it's a discontinuous transfer function.)
2026 float d_l = src->c * src->d + src->f,
2027 d_r = powf_(src->a * src->d + src->b, src->g) + src->e;
2028 if (fabsf_(d_l - d_r) > 1/512.0f) {
2029 return false;
2030 }
2031 inv.d = d_l; // TODO(mtklein): better in practice to choose d_r?
2032
2033 // When d=0, the linear section collapses to a point. We leave c,d,f all zero in that case.
2034 if (inv.d > 0) {
2035 // Inverting the linear section is pretty straightfoward:
2036 // y = cx + f
2037 // y - f = cx
2038 // (1/c)y - f/c = x
2039 inv.c = 1.0f/src->c;
2040 inv.f = -src->f/src->c;
2041 }
2042
2043 // The interesting part is inverting the nonlinear section:
2044 // y = (ax + b)^g + e.
2045 // y - e = (ax + b)^g
2046 // (y - e)^1/g = ax + b
2047 // (y - e)^1/g - b = ax
2048 // (1/a)(y - e)^1/g - b/a = x
2049 //
2050 // To make that fit our form, we need to move the (1/a) term inside the exponentiation:
2051 // let k = (1/a)^g
2052 // (1/a)( y - e)^1/g - b/a = x
2053 // (ky - ke)^1/g - b/a = x
2054
2055 float k = powf_(src->a, -src->g); // (1/a)^g == a^-g
2056 inv.g = 1.0f / src->g;
2057 inv.a = k;
2058 inv.b = -k * src->e;
2059 inv.e = -src->b / src->a;
2060
2061 // We need to enforce the same constraints here that we do when fitting a curve,
2062 // a >= 0 and ad+b >= 0. These constraints are checked by classify(), so they're true
2063 // of the source function if we're here.
2064
2065 // Just like when fitting the curve, there's really no way to rescue a < 0.
2066 if (inv.a < 0) {
2067 return false;
2068 }
2069 // On the other hand we can rescue an ad+b that's gone slightly negative here.
2070 if (inv.a * inv.d + inv.b < 0) {
2071 inv.b = -inv.a * inv.d;
2072 }
2073
2074 // That should usually make classify(inv) == sRGBish true, but there are a couple situations
2075 // where we might still fail here, like non-finite parameter values.
2076 if (classify(inv) != skcms_TFType_sRGBish) {
2077 return false;
2078 }
2079
2080 assert (inv.a >= 0);
2081 assert (inv.a * inv.d + inv.b >= 0);
2082
2083 // Now in principle we're done.
2084 // But to preserve the valuable invariant inv(src(1.0f)) == 1.0f, we'll tweak
2085 // e or f of the inverse, depending on which segment contains src(1.0f).
2086 float s = skcms_TransferFunction_eval(src, 1.0f);
2087 if (!isfinitef_(s)) {
2088 return false;
2089 }
2090
2091 float sign = s < 0 ? -1.0f : 1.0f;
2092 s *= sign;
2093 if (s < inv.d) {
2094 inv.f = 1.0f - sign * inv.c * s;
2095 } else {
2096 inv.e = 1.0f - sign * powf_(inv.a * s + inv.b, inv.g);
2097 }
2098
2099 *dst = inv;
2100 return classify(*dst) == skcms_TFType_sRGBish;
2101 }
2102
2103 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
2104
2105 // From here below we're approximating an skcms_Curve with an skcms_TransferFunction{g,a,b,c,d,e,f}:
2106 //
2107 // tf(x) = cx + f x < d
2108 // tf(x) = (ax + b)^g + e x ≥ d
2109 //
2110 // When fitting, we add the additional constraint that both pieces meet at d:
2111 //
2112 // cd + f = (ad + b)^g + e
2113 //
2114 // Solving for e and folding it through gives an alternate formulation of the non-linear piece:
2115 //
2116 // tf(x) = cx + f x < d
2117 // tf(x) = (ax + b)^g - (ad + b)^g + cd + f x ≥ d
2118 //
2119 // Our overall strategy is then:
2120 // For a couple tolerances,
2121 // - fit_linear(): fit c,d,f iteratively to as many points as our tolerance allows
2122 // - invert c,d,f
2123 // - fit_nonlinear(): fit g,a,b using Gauss-Newton given those inverted c,d,f
2124 // (and by constraint, inverted e) to the inverse of the table.
2125 // Return the parameters with least maximum error.
2126 //
2127 // To run Gauss-Newton to find g,a,b, we'll also need the gradient of the residuals
2128 // of round-trip f_inv(x), the inverse of the non-linear piece of f(x).
2129 //
2130 // let y = Table(x)
2131 // r(x) = x - f_inv(y)
2132 //
2133 // ∂r/∂g = ln(ay + b)*(ay + b)^g
2134 // - ln(ad + b)*(ad + b)^g
2135 // ∂r/∂a = yg(ay + b)^(g-1)
2136 // - dg(ad + b)^(g-1)
2137 // ∂r/∂b = g(ay + b)^(g-1)
2138 // - g(ad + b)^(g-1)
2139
2140 // Return the residual of roundtripping skcms_Curve(x) through f_inv(y) with parameters P,
2141 // and fill out the gradient of the residual into dfdP.
2142 static float rg_nonlinear(float x,
2143 const skcms_Curve* curve,
2144 const skcms_TransferFunction* tf,
2145 float dfdP[3]) {
2146 const float y = eval_curve(curve, x);
2147
2148 const float g = tf->g, a = tf->a, b = tf->b,
2149 c = tf->c, d = tf->d, f = tf->f;
2150
2151 const float Y = fmaxf_(a*y + b, 0.0f),
2152 D = a*d + b;
2153 assert (D >= 0);
2154
2155 // The gradient.
2156 dfdP[0] = logf_(Y)*powf_(Y, g)
2157 - logf_(D)*powf_(D, g);
2158 dfdP[1] = y*g*powf_(Y, g-1)
2159 - d*g*powf_(D, g-1);
2160 dfdP[2] = g*powf_(Y, g-1)
2161 - g*powf_(D, g-1);
2162
2163 // The residual.
2164 const float f_inv = powf_(Y, g)
2165 - powf_(D, g)
2166 + c*d + f;
2167 return x - f_inv;
2168 }
2169
2170 static bool gauss_newton_step(const skcms_Curve* curve,
2171 skcms_TransferFunction* tf,
2172 float x0, float dx, int N) {
2173 // We'll sample x from the range [x0,x1] (both inclusive) N times with even spacing.
2174 //
2175 // Let P = [ tf->g, tf->a, tf->b ] (the three terms that we're adjusting).
2176 //
2177 // We want to do P' = P + (Jf^T Jf)^-1 Jf^T r(P),
2178 // where r(P) is the residual vector
2179 // and Jf is the Jacobian matrix of f(), ∂r/∂P.
2180 //
2181 // Let's review the shape of each of these expressions:
2182 // r(P) is [N x 1], a column vector with one entry per value of x tested
2183 // Jf is [N x 3], a matrix with an entry for each (x,P) pair
2184 // Jf^T is [3 x N], the transpose of Jf
2185 //
2186 // Jf^T Jf is [3 x N] * [N x 3] == [3 x 3], a 3x3 matrix,
2187 // and so is its inverse (Jf^T Jf)^-1
2188 // Jf^T r(P) is [3 x N] * [N x 1] == [3 x 1], a column vector with the same shape as P
2189 //
2190 // Our implementation strategy to get to the final ∆P is
2191 // 1) evaluate Jf^T Jf, call that lhs
2192 // 2) evaluate Jf^T r(P), call that rhs
2193 // 3) invert lhs
2194 // 4) multiply inverse lhs by rhs
2195 //
2196 // This is a friendly implementation strategy because we don't have to have any
2197 // buffers that scale with N, and equally nice don't have to perform any matrix
2198 // operations that are variable size.
2199 //
2200 // Other implementation strategies could trade this off, e.g. evaluating the
2201 // pseudoinverse of Jf ( (Jf^T Jf)^-1 Jf^T ) directly, then multiplying that by
2202 // the residuals. That would probably require implementing singular value
2203 // decomposition, and would create a [3 x N] matrix to be multiplied by the
2204 // [N x 1] residual vector, but on the upside I think that'd eliminate the
2205 // possibility of this gauss_newton_step() function ever failing.
2206
2207 // 0) start off with lhs and rhs safely zeroed.
2208 skcms_Matrix3x3 lhs = {{ {0,0,0}, {0,0,0}, {0,0,0} }};
2209 skcms_Vector3 rhs = { {0,0,0} };
2210
2211 // 1,2) evaluate lhs and evaluate rhs
2212 // We want to evaluate Jf only once, but both lhs and rhs involve Jf^T,
2213 // so we'll have to update lhs and rhs at the same time.
2214 for (int i = 0; i < N; i++) {
2215 float x = x0 + static_cast<float>(i)*dx;
2216
2217 float dfdP[3] = {0,0,0};
2218 float resid = rg_nonlinear(x,curve,tf, dfdP);
2219
2220 for (int r = 0; r < 3; r++) {
2221 for (int c = 0; c < 3; c++) {
2222 lhs.vals[r][c] += dfdP[r] * dfdP[c];
2223 }
2224 rhs.vals[r] += dfdP[r] * resid;
2225 }
2226 }
2227
2228 // If any of the 3 P parameters are unused, this matrix will be singular.
2229 // Detect those cases and fix them up to indentity instead, so we can invert.
2230 for (int k = 0; k < 3; k++) {
2231 if (lhs.vals[0][k]==0 && lhs.vals[1][k]==0 && lhs.vals[2][k]==0 &&
2232 lhs.vals[k][0]==0 && lhs.vals[k][1]==0 && lhs.vals[k][2]==0) {
2233 lhs.vals[k][k] = 1;
2234 }
2235 }
2236
2237 // 3) invert lhs
2238 skcms_Matrix3x3 lhs_inv;
2239 if (!skcms_Matrix3x3_invert(&lhs, &lhs_inv)) {
2240 return false;
2241 }
2242
2243 // 4) multiply inverse lhs by rhs
2244 skcms_Vector3 dP = mv_mul(&lhs_inv, &rhs);
2245 tf->g += dP.vals[0];
2246 tf->a += dP.vals[1];
2247 tf->b += dP.vals[2];
2248 return isfinitef_(tf->g) && isfinitef_(tf->a) && isfinitef_(tf->b);
2249 }
2250
2251 static float max_roundtrip_error_checked(const skcms_Curve* curve,
2252 const skcms_TransferFunction* tf_inv) {
2253 skcms_TransferFunction tf;
2254 if (!skcms_TransferFunction_invert(tf_inv, &tf) || skcms_TFType_sRGBish != classify(tf)) {
2255 return INFINITY_;
2256 }
2257
2258 skcms_TransferFunction tf_inv_again;
2259 if (!skcms_TransferFunction_invert(&tf, &tf_inv_again)) {
2260 return INFINITY_;
2261 }
2262
2263 return skcms_MaxRoundtripError(curve, &tf_inv_again);
2264 }
2265
2266 // Fit the points in [L,N) to the non-linear piece of tf, or return false if we can't.
2267 static bool fit_nonlinear(const skcms_Curve* curve, int L, int N, skcms_TransferFunction* tf) {
2268 // This enforces a few constraints that are not modeled in gauss_newton_step()'s optimization.
2269 auto fixup_tf = [tf]() {
2270 // a must be non-negative. That ensures the function is monotonically increasing.
2271 // We don't really know how to fix up a if it goes negative.
2272 if (tf->a < 0) {
2273 return false;
2274 }
2275 // ad+b must be non-negative. That ensures we don't end up with complex numbers in powf.
2276 // We feel just barely not uneasy enough to tweak b so ad+b is zero in this case.
2277 if (tf->a * tf->d + tf->b < 0) {
2278 tf->b = -tf->a * tf->d;
2279 }
2280 assert (tf->a >= 0 &&
2281 tf->a * tf->d + tf->b >= 0);
2282
2283 // cd+f must be ~= (ad+b)^g+e. That ensures the function is continuous. We keep e as a free
2284 // parameter so we can guarantee this.
2285 tf->e = tf->c*tf->d + tf->f
2286 - powf_(tf->a*tf->d + tf->b, tf->g);
2287
2288 return isfinitef_(tf->e);
2289 };
2290
2291 if (!fixup_tf()) {
2292 return false;
2293 }
2294
2295 // No matter where we start, dx should always represent N even steps from 0 to 1.
2296 const float dx = 1.0f / static_cast<float>(N-1);
2297
2298 skcms_TransferFunction best_tf = *tf;
2299 float best_max_error = INFINITY_;
2300
2301 // Need this or several curves get worse... *sigh*
2302 float init_error = max_roundtrip_error_checked(curve, tf);
2303 if (init_error < best_max_error) {
2304 best_max_error = init_error;
2305 best_tf = *tf;
2306 }
2307
2308 // As far as we can tell, 1 Gauss-Newton step won't converge, and 3 steps is no better than 2.
2309 for (int j = 0; j < 8; j++) {
2310 if (!gauss_newton_step(curve, tf, static_cast<float>(L)*dx, dx, N-L) || !fixup_tf()) {
2311 *tf = best_tf;
2312 return isfinitef_(best_max_error);
2313 }
2314
2315 float max_error = max_roundtrip_error_checked(curve, tf);
2316 if (max_error < best_max_error) {
2317 best_max_error = max_error;
2318 best_tf = *tf;
2319 }
2320 }
2321
2322 *tf = best_tf;
2323 return isfinitef_(best_max_error);
2324 }
2325
2326 bool skcms_ApproximateCurve(const skcms_Curve* curve,
2327 skcms_TransferFunction* approx,
2328 float* max_error) {
2329 if (!curve || !approx || !max_error) {
2330 return false;
2331 }
2332
2333 if (curve->table_entries == 0) {
2334 // No point approximating an skcms_TransferFunction with an skcms_TransferFunction!
2335 return false;
2336 }
2337
2338 if (curve->table_entries == 1 || curve->table_entries > kMaxTableEntries) {
2339 // We need at least two points, and must put some reasonable cap on the maximum number.
2340 return false;
2341 }
2342
2343 int N = (int)curve->table_entries;
2344 const float dx = 1.0f / static_cast<float>(N - 1);
2345
2346 *max_error = INFINITY_;
2347 const float kTolerances[] = { 1.5f / 65535.0f, 1.0f / 512.0f };
2348 for (int t = 0; t < ARRAY_COUNT(kTolerances); t++) {
2349 skcms_TransferFunction tf,
2350 tf_inv;
2351
2352 // It's problematic to fit curves with non-zero f, so always force it to zero explicitly.
2353 tf.f = 0.0f;
2354 int L = fit_linear(curve, N, kTolerances[t], &tf.c, &tf.d);
2355
2356 if (L == N) {
2357 // If the entire data set was linear, move the coefficients to the nonlinear portion
2358 // with G == 1. This lets use a canonical representation with d == 0.
2359 tf.g = 1;
2360 tf.a = tf.c;
2361 tf.b = tf.f;
2362 tf.c = tf.d = tf.e = tf.f = 0;
2363 } else if (L == N - 1) {
2364 // Degenerate case with only two points in the nonlinear segment. Solve directly.
2365 tf.g = 1;
2366 tf.a = (eval_curve(curve, static_cast<float>(N-1)*dx) -
2367 eval_curve(curve, static_cast<float>(N-2)*dx))
2368 / dx;
2369 tf.b = eval_curve(curve, static_cast<float>(N-2)*dx)
2370 - tf.a * static_cast<float>(N-2)*dx;
2371 tf.e = 0;
2372 } else {
2373 // Start by guessing a gamma-only curve through the midpoint.
2374 int mid = (L + N) / 2;
2375 float mid_x = static_cast<float>(mid) / static_cast<float>(N - 1);
2376 float mid_y = eval_curve(curve, mid_x);
2377 tf.g = log2f_(mid_y) / log2f_(mid_x);
2378 tf.a = 1;
2379 tf.b = 0;
2380 tf.e = tf.c*tf.d + tf.f
2381 - powf_(tf.a*tf.d + tf.b, tf.g);
2382
2383
2384 if (!skcms_TransferFunction_invert(&tf, &tf_inv) ||
2385 !fit_nonlinear(curve, L,N, &tf_inv)) {
2386 continue;
2387 }
2388
2389 // We fit tf_inv, so calculate tf to keep in sync.
2390 // fit_nonlinear() should guarantee invertibility.
2391 if (!skcms_TransferFunction_invert(&tf_inv, &tf)) {
2392 assert(false);
2393 continue;
2394 }
2395 }
2396
2397 // We'd better have a sane, sRGB-ish TF by now.
2398 // Other non-Bad TFs would be fine, but we know we've only ever tried to fit sRGBish;
2399 // anything else is just some accident of math and the way we pun tf.g as a type flag.
2400 // fit_nonlinear() should guarantee this, but the special cases may fail this test.
2401 if (skcms_TFType_sRGBish != classify(tf)) {
2402 continue;
2403 }
2404
2405 // We find our error by roundtripping the table through tf_inv.
2406 //
2407 // (The most likely use case for this approximation is to be inverted and
2408 // used as the transfer function for a destination color space.)
2409 //
2410 // We've kept tf and tf_inv in sync above, but we can't guarantee that tf is
2411 // invertible, so re-verify that here (and use the new inverse for testing).
2412 // fit_nonlinear() should guarantee this, but the special cases that don't use
2413 // it may fail this test.
2414 if (!skcms_TransferFunction_invert(&tf, &tf_inv)) {
2415 continue;
2416 }
2417
2418 float err = skcms_MaxRoundtripError(curve, &tf_inv);
2419 if (*max_error > err) {
2420 *max_error = err;
2421 *approx = tf;
2422 }
2423 }
2424 return isfinitef_(*max_error);
2425 }
2426
2427 enum class CpuType { Baseline, HSW, SKX };
2428
2429 static CpuType cpu_type() {
2430 #if defined(SKCMS_PORTABLE) || !defined(__x86_64__) || defined(SKCMS_FORCE_BASELINE)
2431 return CpuType::Baseline;
2432 #elif defined(SKCMS_FORCE_HSW)
2433 return CpuType::HSW;
2434 #elif defined(SKCMS_FORCE_SKX)
2435 return CpuType::SKX;
2436 #else
2437 static const CpuType type = []{
2438 if (!sAllowRuntimeCPUDetection) {
2439 return CpuType::Baseline;
2440 }
2441 // See http://www.sandpile.org/x86/cpuid.htm
2442
2443 // First, a basic cpuid(1) lets us check prerequisites for HSW, SKX.
2444 uint32_t eax, ebx, ecx, edx;
2445 __asm__ __volatile__("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
2446 : "0"(1), "2"(0));
2447 if ((edx & (1u<<25)) && // SSE
2448 (edx & (1u<<26)) && // SSE2
2449 (ecx & (1u<< 0)) && // SSE3
2450 (ecx & (1u<< 9)) && // SSSE3
2451 (ecx & (1u<<12)) && // FMA (N.B. not used, avoided even)
2452 (ecx & (1u<<19)) && // SSE4.1
2453 (ecx & (1u<<20)) && // SSE4.2
2454 (ecx & (1u<<26)) && // XSAVE
2455 (ecx & (1u<<27)) && // OSXSAVE
2456 (ecx & (1u<<28)) && // AVX
2457 (ecx & (1u<<29))) { // F16C
2458
2459 // Call cpuid(7) to check for AVX2 and AVX-512 bits.
2460 __asm__ __volatile__("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
2461 : "0"(7), "2"(0));
2462 // eax from xgetbv(0) will tell us whether XMM, YMM, and ZMM state is saved.
2463 uint32_t xcr0, dont_need_edx;
2464 __asm__ __volatile__("xgetbv" : "=a"(xcr0), "=d"(dont_need_edx) : "c"(0));
2465
2466 if ((xcr0 & (1u<<1)) && // XMM register state saved?
2467 (xcr0 & (1u<<2)) && // YMM register state saved?
2468 (ebx & (1u<<5))) { // AVX2
2469 // At this point we're at least HSW. Continue checking for SKX.
2470 if ((xcr0 & (1u<< 5)) && // Opmasks state saved?
2471 (xcr0 & (1u<< 6)) && // First 16 ZMM registers saved?
2472 (xcr0 & (1u<< 7)) && // High 16 ZMM registers saved?
2473 (ebx & (1u<<16)) && // AVX512F
2474 (ebx & (1u<<17)) && // AVX512DQ
2475 (ebx & (1u<<28)) && // AVX512CD
2476 (ebx & (1u<<30)) && // AVX512BW
2477 (ebx & (1u<<31))) { // AVX512VL
2478 return CpuType::SKX;
2479 }
2480 return CpuType::HSW;
2481 }
2482 }
2483 return CpuType::Baseline;
2484 }();
2485 return type;
2486 #endif
2487 }
2488
2489 static bool tf_is_gamma(const skcms_TransferFunction& tf) {
2490 return tf.g > 0 && tf.a == 1 &&
2491 tf.b == 0 && tf.c == 0 && tf.d == 0 && tf.e == 0 && tf.f == 0;
2492 }
2493
2494 struct OpAndArg {
2495 Op op;
2496 const void* arg;
2497 };
2498
2499 static OpAndArg select_curve_op(const skcms_Curve* curve, int channel) {
2500 struct OpType {
2501 Op sGamma, sRGBish, PQish, HLGish, HLGinvish, table;
2502 };
2503 static constexpr OpType kOps[] = {
2504 { Op::gamma_r, Op::tf_r, Op::pq_r, Op::hlg_r, Op::hlginv_r, Op::table_r },
2505 { Op::gamma_g, Op::tf_g, Op::pq_g, Op::hlg_g, Op::hlginv_g, Op::table_g },
2506 { Op::gamma_b, Op::tf_b, Op::pq_b, Op::hlg_b, Op::hlginv_b, Op::table_b },
2507 { Op::gamma_a, Op::tf_a, Op::pq_a, Op::hlg_a, Op::hlginv_a, Op::table_a },
2508 };
2509 const auto& op = kOps[channel];
2510
2511 if (curve->table_entries == 0) {
2512 const OpAndArg noop = { Op::load_a8/*doesn't matter*/, nullptr };
2513
2514 const skcms_TransferFunction& tf = curve->parametric;
2515
2516 if (tf_is_gamma(tf)) {
2517 return tf.g != 1 ? OpAndArg{op.sGamma, &tf}
2518 : noop;
2519 }
2520
2521 switch (classify(tf)) {
2522 case skcms_TFType_Invalid: return noop;
2523 // TODO(https://issues.skia.org/issues/420956739): Consider adding
2524 // support for PQ and HLG. Generally any code that goes through this
2525 // path would also want tone mapping too.
2526 case skcms_TFType_PQ: return noop;
2527 case skcms_TFType_HLG: return noop;
2528 case skcms_TFType_sRGBish: return OpAndArg{op.sRGBish, &tf};
2529 case skcms_TFType_PQish: return OpAndArg{op.PQish, &tf};
2530 case skcms_TFType_HLGish: return OpAndArg{op.HLGish, &tf};
2531 case skcms_TFType_HLGinvish: return OpAndArg{op.HLGinvish, &tf};
2532 }
2533 }
2534 // The table_* ops make this assumption.
2535 assert(curve->table_entries <= kMaxTableEntries);
2536 return OpAndArg{op.table, curve};
2537 }
2538
2539 // Returns negative if any of the curves are malformed.
2540 static int select_curve_ops(const skcms_Curve* curves, int numChannels, OpAndArg* ops) {
2541 // We process the channels in reverse order, yielding ops in ABGR order.
2542 // (Working backwards allows us to fuse trailing B+G+R ops into a single RGB op.)
2543 int cursor = 0;
2544 for (int index = numChannels; index-- > 0; ) {
2545 if (curves[index].table_entries > kMaxTableEntries) {
2546 return -1;
2547 }
2548 ops[cursor] = select_curve_op(&curves[index], index);
2549 if (ops[cursor].arg) {
2550 ++cursor;
2551 }
2552 }
2553
2554 // Identify separate B+G+R ops and fuse them into a single RGB op.
2555 if (cursor >= 3) {
2556 struct FusableOps {
2557 Op r, g, b, rgb;
2558 };
2559 static constexpr FusableOps kFusableOps[] = {
2560 {Op::gamma_r, Op::gamma_g, Op::gamma_b, Op::gamma_rgb},
2561 {Op::tf_r, Op::tf_g, Op::tf_b, Op::tf_rgb},
2562 {Op::pq_r, Op::pq_g, Op::pq_b, Op::pq_rgb},
2563 {Op::hlg_r, Op::hlg_g, Op::hlg_b, Op::hlg_rgb},
2564 {Op::hlginv_r, Op::hlginv_g, Op::hlginv_b, Op::hlginv_rgb},
2565 };
2566
2567 int posR = cursor - 1;
2568 int posG = cursor - 2;
2569 int posB = cursor - 3;
2570 for (const FusableOps& fusableOp : kFusableOps) {
2571 if (ops[posR].op == fusableOp.r &&
2572 ops[posG].op == fusableOp.g &&
2573 ops[posB].op == fusableOp.b &&
2574 (0 == memcmp(ops[posR].arg, ops[posG].arg, sizeof(skcms_TransferFunction))) &&
2575 (0 == memcmp(ops[posR].arg, ops[posB].arg, sizeof(skcms_TransferFunction)))) {
2576 // Fuse the three matching ops into one.
2577 ops[posB].op = fusableOp.rgb;
2578 cursor -= 2;
2579 break;
2580 }
2581 }
2582 }
2583
2584 return cursor;
2585 }
2586
2587 static size_t bytes_per_pixel(skcms_PixelFormat fmt) {
2588 switch (fmt >> 1) { // ignore rgb/bgr
2589 case skcms_PixelFormat_A_8 >> 1: return 1;
2590 case skcms_PixelFormat_G_8 >> 1: return 1;
2591 case skcms_PixelFormat_GA_88 >> 1: return 2;
2592 case skcms_PixelFormat_ABGR_4444 >> 1: return 2;
2593 case skcms_PixelFormat_RGB_565 >> 1: return 2;
2594 case skcms_PixelFormat_RGB_888 >> 1: return 3;
2595 case skcms_PixelFormat_RGBA_8888 >> 1: return 4;
2596 case skcms_PixelFormat_RGBA_8888_sRGB >> 1: return 4;
2597 case skcms_PixelFormat_RGBA_1010102 >> 1: return 4;
2598 case skcms_PixelFormat_RGB_101010x_XR >> 1: return 4;
2599 case skcms_PixelFormat_RGB_161616LE >> 1: return 6;
2600 case skcms_PixelFormat_RGBA_10101010_XR >> 1: return 8;
2601 case skcms_PixelFormat_RGBA_16161616LE >> 1: return 8;
2602 case skcms_PixelFormat_RGB_161616BE >> 1: return 6;
2603 case skcms_PixelFormat_RGBA_16161616BE >> 1: return 8;
2604 case skcms_PixelFormat_RGB_hhh_Norm >> 1: return 6;
2605 case skcms_PixelFormat_RGBA_hhhh_Norm >> 1: return 8;
2606 case skcms_PixelFormat_RGB_hhh >> 1: return 6;
2607 case skcms_PixelFormat_RGBA_hhhh >> 1: return 8;
2608 case skcms_PixelFormat_RGB_fff >> 1: return 12;
2609 case skcms_PixelFormat_RGBA_ffff >> 1: return 16;
2610 }
2611 assert(false);
2612 return 0;
2613 }
2614
2615 // See ITU-T H.273 Table 3 for the full list of codes.
2616 const uint8_t kTransferCicpIdPQ = 16;
2617 const uint8_t kTransferCicpIdHLG = 18;
2618
2619 static bool has_cicp_pq_trc(const skcms_ICCProfile* profile) {
2620 return profile->has_CICP
2621 && profile->CICP.transfer_characteristics == kTransferCicpIdPQ;
2622 }
2623
2624 static bool has_cicp_hlg_trc(const skcms_ICCProfile* profile) {
2625 return profile->has_CICP
2626 && profile->CICP.transfer_characteristics == kTransferCicpIdHLG;
2627 }
2628
2629 // Set tf to be the PQ transfer function, scaled such that 1.0 will map to 10,000 / 203.
2630 static void set_reference_pq_ish_trc(skcms_TransferFunction* tf) {
2631 // Initialize such that 1.0 maps to 1.0.
2632 skcms_TransferFunction_makePQish(tf,
2633 -107/128.0f, 1.0f, 32/2523.0f, 2413/128.0f, -2392/128.0f, 8192/1305.0f);
2634
2635 // Distribute scaling factor W by scaling A and B with X ^ (1/F):
2636 // ((A + Bx^C) / (D + Ex^C))^F * W = ((A + Bx^C) / (D + Ex^C) * W^(1/F))^F
2637 // See https://crbug.com/1058580#c32 for discussion.
2638 const float w = 10000.0f / 203.0f;
2639 const float ws = powf_(w, 1.0f / tf->f);
2640 tf->a = ws * tf->a;
2641 tf->b = ws * tf->b;
2642 }
2643
2644 // Set tf to be the HLG inverse OETF, scaled such that 1.0 will map to 1.0.
2645 // While this is one version of HLG, there are many others. A better version
2646 // would be to use the 1,000 nit reference version, but that will require
2647 // adding opt-optical transform support.
2648 static void set_sdr_hlg_ish_trc(skcms_TransferFunction* tf) {
2649 skcms_TransferFunction_makeHLGish(tf,
2650 2.0f, 2.0f, 1/0.17883277f, 0.28466892f, 0.55991073f);
2651 tf->f = 1.0f / 12.0f - 1.0f;
2652 }
2653
2654 static bool prep_for_destination(const skcms_ICCProfile* profile,
2655 skcms_Matrix3x3* fromXYZD50,
2656 skcms_TransferFunction* invR,
2657 skcms_TransferFunction* invG,
2658 skcms_TransferFunction* invB,
2659 bool* dst_using_B2A,
2660 bool* dst_using_hlg_ootf) {
2661 const bool has_xyzd50 =
2662 profile->has_toXYZD50 &&
2663 skcms_Matrix3x3_invert(&profile->toXYZD50, fromXYZD50);
2664 *dst_using_B2A = false;
2665 *dst_using_hlg_ootf = false;
2666
2667 // CICP-specified PQ or HLG transfer functions take precedence.
2668 // TODO: Add the ability to parse CICP primaries to not require
2669 // the XYZD50 matrix.
2670 if (has_cicp_pq_trc(profile) && has_xyzd50) {
2671 skcms_TransferFunction trc_pq;
2672 set_reference_pq_ish_trc(&trc_pq);
2673 skcms_TransferFunction_invert(&trc_pq, invR);
2674 skcms_TransferFunction_invert(&trc_pq, invG);
2675 skcms_TransferFunction_invert(&trc_pq, invB);
2676 return true;
2677 }
2678 if (has_cicp_hlg_trc(profile) && has_xyzd50) {
2679 skcms_TransferFunction trc_hlg;
2680 set_sdr_hlg_ish_trc(&trc_hlg);
2681 skcms_TransferFunction_invert(&trc_hlg, invR);
2682 skcms_TransferFunction_invert(&trc_hlg, invG);
2683 skcms_TransferFunction_invert(&trc_hlg, invB);
2684 *dst_using_hlg_ootf = true;
2685 return true;
2686 }
2687
2688 // Then prefer the B2A transformation.
2689 // skcms_Transform() supports B2A destinations.
2690 if (profile->has_B2A) {
2691 *dst_using_B2A = true;
2692 return true;
2693 }
2694
2695 // Finally use parametric transfer functions.
2696 // TODO: Reject non sRGB-ish transfer functions here.
2697 return has_xyzd50
2698 && profile->has_trc
2699 && profile->trc[0].table_entries == 0
2700 && profile->trc[1].table_entries == 0
2701 && profile->trc[2].table_entries == 0
2702 && skcms_TransferFunction_invert(&profile->trc[0].parametric, invR)
2703 && skcms_TransferFunction_invert(&profile->trc[1].parametric, invG)
2704 && skcms_TransferFunction_invert(&profile->trc[2].parametric, invB);
2705 }
2706
2707 bool skcms_Transform(const void* src,
2708 skcms_PixelFormat srcFmt,
2709 skcms_AlphaFormat srcAlpha,
2710 const skcms_ICCProfile* srcProfile,
2711 void* dst,
2712 skcms_PixelFormat dstFmt,
2713 skcms_AlphaFormat dstAlpha,
2714 const skcms_ICCProfile* dstProfile,
2715 size_t nz) {
2716 const size_t dst_bpp = bytes_per_pixel(dstFmt),
2717 src_bpp = bytes_per_pixel(srcFmt);
2718 // Let's just refuse if the request is absurdly big.
2719 if (nz * dst_bpp > INT_MAX || nz * src_bpp > INT_MAX) {
2720 return false;
2721 }
2722 int n = (int)nz;
2723
2724 // Null profiles default to sRGB. Passing null for both is handy when doing format conversion.
2725 if (!srcProfile) {
2726 srcProfile = skcms_sRGB_profile();
2727 }
2728 if (!dstProfile) {
2729 dstProfile = skcms_sRGB_profile();
2730 }
2731
2732 // We can't transform in place unless the PixelFormats are the same size.
2733 if (dst == src && dst_bpp != src_bpp) {
2734 return false;
2735 }
2736 // TODO: more careful alias rejection (like, dst == src + 1)?
2737
2738 Op program[32];
2739 const void* context[32];
2740
2741 Op* ops = program;
2742 const void** contexts = context;
2743
2744 auto add_op = [&](Op o) {
2745 *ops++ = o;
2746 *contexts++ = nullptr;
2747 };
2748
2749 auto add_op_ctx = [&](Op o, const void* c) {
2750 *ops++ = o;
2751 *contexts++ = c;
2752 };
2753
2754 auto add_curve_ops = [&](const skcms_Curve* curves, int numChannels) -> bool {
2755 OpAndArg oa[4];
2756 assert(numChannels <= ARRAY_COUNT(oa));
2757
2758 int numOps = select_curve_ops(curves, numChannels, oa);
2759 if (numOps < 0) {
2760 return false;
2761 }
2762
2763 for (int i = 0; i < numOps; ++i) {
2764 add_op_ctx(oa[i].op, oa[i].arg);
2765 }
2766 return true;
2767 };
2768
2769 // If the source has a TRC that is specified by CICP and not the TRC
2770 // entries, then store it here for future use.
2771 skcms_TransferFunction src_cicp_trc;
2772
2773 // These are always parametric curves of some sort.
2774 skcms_Curve dst_curves[3];
2775 dst_curves[0].table_entries =
2776 dst_curves[1].table_entries =
2777 dst_curves[2].table_entries = 0;
2778
2779 // This will store the XYZD50 to destination gamut conversion matrix, if it is needed.
2780 skcms_Matrix3x3 dst_from_xyz;
2781
2782 // This will store the full source to destination gamut conversion matrix, if it is needed.
2783 skcms_Matrix3x3 dst_from_src;
2784
2785 switch (srcFmt >> 1) {
2786 default: return false;
2787 case skcms_PixelFormat_A_8 >> 1: add_op(Op::load_a8); break;
2788 case skcms_PixelFormat_G_8 >> 1: add_op(Op::load_g8); break;
2789 case skcms_PixelFormat_GA_88 >> 1: add_op(Op::load_ga88); break;
2790 case skcms_PixelFormat_ABGR_4444 >> 1: add_op(Op::load_4444); break;
2791 case skcms_PixelFormat_RGB_565 >> 1: add_op(Op::load_565); break;
2792 case skcms_PixelFormat_RGB_888 >> 1: add_op(Op::load_888); break;
2793 case skcms_PixelFormat_RGBA_8888 >> 1: add_op(Op::load_8888); break;
2794 case skcms_PixelFormat_RGBA_1010102 >> 1: add_op(Op::load_1010102); break;
2795 case skcms_PixelFormat_RGB_101010x_XR >> 1: add_op(Op::load_101010x_XR); break;
2796 case skcms_PixelFormat_RGBA_10101010_XR >> 1: add_op(Op::load_10101010_XR); break;
2797 case skcms_PixelFormat_RGB_161616LE >> 1: add_op(Op::load_161616LE); break;
2798 case skcms_PixelFormat_RGBA_16161616LE >> 1: add_op(Op::load_16161616LE); break;
2799 case skcms_PixelFormat_RGB_161616BE >> 1: add_op(Op::load_161616BE); break;
2800 case skcms_PixelFormat_RGBA_16161616BE >> 1: add_op(Op::load_16161616BE); break;
2801 case skcms_PixelFormat_RGB_hhh_Norm >> 1: add_op(Op::load_hhh); break;
2802 case skcms_PixelFormat_RGBA_hhhh_Norm >> 1: add_op(Op::load_hhhh); break;
2803 case skcms_PixelFormat_RGB_hhh >> 1: add_op(Op::load_hhh); break;
2804 case skcms_PixelFormat_RGBA_hhhh >> 1: add_op(Op::load_hhhh); break;
2805 case skcms_PixelFormat_RGB_fff >> 1: add_op(Op::load_fff); break;
2806 case skcms_PixelFormat_RGBA_ffff >> 1: add_op(Op::load_ffff); break;
2807
2808 case skcms_PixelFormat_RGBA_8888_sRGB >> 1:
2809 add_op(Op::load_8888);
2810 add_op_ctx(Op::tf_rgb, skcms_sRGB_TransferFunction());
2811 break;
2812 }
2813 if (srcFmt == skcms_PixelFormat_RGB_hhh_Norm ||
2814 srcFmt == skcms_PixelFormat_RGBA_hhhh_Norm) {
2815 add_op(Op::clamp);
2816 }
2817 if (srcFmt & 1) {
2818 add_op(Op::swap_rb);
2819 }
2820 skcms_ICCProfile gray_dst_profile;
2821 switch (dstFmt >> 1) {
2822 case skcms_PixelFormat_G_8:
2823 case skcms_PixelFormat_GA_88:
2824 // When transforming to gray, stop at XYZ (by setting toXYZ to identity), then transform
2825 // luminance (Y) by the destination transfer function.
2826 gray_dst_profile = *dstProfile;
2827 skcms_SetXYZD50(&gray_dst_profile, &skcms_XYZD50_profile()->toXYZD50);
2828 dstProfile = &gray_dst_profile;
2829 break;
2830 default:
2831 break;
2832 }
2833
2834 if (srcProfile->data_color_space == skcms_Signature_CMYK) {
2835 // Photoshop creates CMYK images as inverse CMYK.
2836 // These happen to be the only ones we've _ever_ seen.
2837 add_op(Op::invert);
2838 // With CMYK, ignore the alpha type, to avoid changing K or conflating CMY with K.
2839 srcAlpha = skcms_AlphaFormat_Unpremul;
2840 }
2841
2842 if (srcAlpha == skcms_AlphaFormat_Opaque) {
2843 add_op(Op::force_opaque);
2844 } else if (srcAlpha == skcms_AlphaFormat_PremulAsEncoded) {
2845 add_op(Op::unpremul);
2846 }
2847
2848 if (dstProfile != srcProfile) {
2849
2850 // Track whether or not the A2B or B2A transforms are used. the CICP
2851 // values take precedence over A2B and B2A.
2852 bool src_using_A2B = false;
2853 bool src_using_hlg_ootf = false;
2854 bool dst_using_B2A = false;
2855 bool dst_using_hlg_ootf = false;
2856
2857 if (!prep_for_destination(dstProfile,
2858 &dst_from_xyz,
2859 &dst_curves[0].parametric,
2860 &dst_curves[1].parametric,
2861 &dst_curves[2].parametric,
2862 &dst_using_B2A,
2863 &dst_using_hlg_ootf)) {
2864 return false;
2865 }
2866
2867 if (has_cicp_pq_trc(srcProfile) && srcProfile->has_toXYZD50) {
2868 set_reference_pq_ish_trc(&src_cicp_trc);
2869 add_op_ctx(Op::pq_rgb, &src_cicp_trc);
2870 } else if (has_cicp_hlg_trc(srcProfile) && srcProfile->has_toXYZD50) {
2871 src_using_hlg_ootf = true;
2872 set_sdr_hlg_ish_trc(&src_cicp_trc);
2873 add_op_ctx(Op::hlg_rgb, &src_cicp_trc);
2874 } else if (srcProfile->has_A2B) {
2875 src_using_A2B = true;
2876 if (srcProfile->A2B.input_channels) {
2877 if (!add_curve_ops(srcProfile->A2B.input_curves,
2878 (int)srcProfile->A2B.input_channels)) {
2879 return false;
2880 }
2881 add_op(Op::clamp);
2882 add_op_ctx(Op::clut_A2B, &srcProfile->A2B);
2883 }
2884
2885 if (srcProfile->A2B.matrix_channels == 3) {
2886 if (!add_curve_ops(srcProfile->A2B.matrix_curves, /*numChannels=*/3)) {
2887 return false;
2888 }
2889
2890 static const skcms_Matrix3x4 I = {{
2891 {1,0,0,0},
2892 {0,1,0,0},
2893 {0,0,1,0},
2894 }};
2895 if (0 != memcmp(&I, &srcProfile->A2B.matrix, sizeof(I))) {
2896 add_op_ctx(Op::matrix_3x4, &srcProfile->A2B.matrix);
2897 }
2898 }
2899
2900 if (srcProfile->A2B.output_channels == 3) {
2901 if (!add_curve_ops(srcProfile->A2B.output_curves, /*numChannels=*/3)) {
2902 return false;
2903 }
2904 }
2905
2906 if (srcProfile->pcs == skcms_Signature_Lab) {
2907 add_op(Op::lab_to_xyz);
2908 }
2909
2910 } else if (srcProfile->has_trc && srcProfile->has_toXYZD50) {
2911 if (!add_curve_ops(srcProfile->trc, /*numChannels=*/3)) {
2912 return false;
2913 }
2914 } else {
2915 return false;
2916 }
2917
2918 // A2B sources are in XYZD50 by now, but TRC sources are still in their original gamut.
2919 assert (srcProfile->has_A2B || srcProfile->has_toXYZD50);
2920
2921 if (dst_using_B2A) {
2922 // B2A needs its input in XYZD50, so transform TRC sources now.
2923 if (!src_using_A2B) {
2924 add_op_ctx(Op::matrix_3x3, &srcProfile->toXYZD50);
2925 // Apply the HLG OOTF in XYZD50 space, if needed.
2926 if (src_using_hlg_ootf) {
2927 add_op(Op::hlg_ootf_scale);
2928 }
2929 }
2930
2931 if (dstProfile->pcs == skcms_Signature_Lab) {
2932 add_op(Op::xyz_to_lab);
2933 }
2934
2935 if (dstProfile->B2A.input_channels == 3) {
2936 if (!add_curve_ops(dstProfile->B2A.input_curves, /*numChannels=*/3)) {
2937 return false;
2938 }
2939 }
2940
2941 if (dstProfile->B2A.matrix_channels == 3) {
2942 static const skcms_Matrix3x4 I = {{
2943 {1,0,0,0},
2944 {0,1,0,0},
2945 {0,0,1,0},
2946 }};
2947 if (0 != memcmp(&I, &dstProfile->B2A.matrix, sizeof(I))) {
2948 add_op_ctx(Op::matrix_3x4, &dstProfile->B2A.matrix);
2949 }
2950
2951 if (!add_curve_ops(dstProfile->B2A.matrix_curves, /*numChannels=*/3)) {
2952 return false;
2953 }
2954 }
2955
2956 if (dstProfile->B2A.output_channels) {
2957 add_op(Op::clamp);
2958 add_op_ctx(Op::clut_B2A, &dstProfile->B2A);
2959
2960 if (!add_curve_ops(dstProfile->B2A.output_curves,
2961 (int)dstProfile->B2A.output_channels)) {
2962 return false;
2963 }
2964 }
2965 } else {
2966 // This is a TRC destination.
2967
2968 // Transform to the destination gamut.
2969 if (src_using_hlg_ootf != dst_using_hlg_ootf) {
2970 // If just the src or the dst has an HLG OOTF then we will apply the OOTF in XYZD50
2971 // space. If both the src and dst has an HLG OOTF then they will cancel.
2972 if (!src_using_A2B) {
2973 add_op_ctx(Op::matrix_3x3, &srcProfile->toXYZD50);
2974 }
2975 if (src_using_hlg_ootf) {
2976 add_op(Op::hlg_ootf_scale);
2977 }
2978 if (dst_using_hlg_ootf) {
2979 add_op(Op::hlginv_ootf_scale);
2980 }
2981 add_op_ctx(Op::matrix_3x3, &dst_from_xyz);
2982 } else if (src_using_A2B) {
2983 // If the source is A2B then we are already in XYZD50. Just apply the xyz->dst
2984 // matrix.
2985 add_op_ctx(Op::matrix_3x3, &dst_from_xyz);
2986 } else {
2987 const skcms_Matrix3x3* to_xyz = &srcProfile->toXYZD50;
2988 // There's a chance the source and destination gamuts are identical,
2989 // in which case we can skip the gamut transform.
2990 if (0 != memcmp(&dstProfile->toXYZD50, to_xyz, sizeof(skcms_Matrix3x3))) {
2991 // Concat the entire gamut transform into dst_from_src.
2992 dst_from_src = skcms_Matrix3x3_concat(&dst_from_xyz, to_xyz);
2993 add_op_ctx(Op::matrix_3x3, &dst_from_src);
2994 }
2995 }
2996
2997 // Encode back to dst RGB using its parametric transfer functions.
2998 OpAndArg oa[3];
2999 int numOps = select_curve_ops(dst_curves, /*numChannels=*/3, oa);
3000 // All dst_curves should be parametric, not table-based, so select_curve_ops should
3001 // not fail.
3002 assert(numOps >= 0);
3003 for (int index = 0; index < numOps; ++index) {
3004 assert(oa[index].op != Op::table_r &&
3005 oa[index].op != Op::table_g &&
3006 oa[index].op != Op::table_b &&
3007 oa[index].op != Op::table_a);
3008 add_op_ctx(oa[index].op, oa[index].arg);
3009 }
3010 }
3011 }
3012
3013 // Clamp here before premul to make sure we're clamping to normalized values _and_ gamut,
3014 // not just to values that fit in [0,1].
3015 //
3016 // E.g. r = 1.1, a = 0.5 would fit fine in fixed point after premul (ra=0.55,a=0.5),
3017 // but would be carrying r > 1, which is really unexpected for downstream consumers.
3018 if (dstFmt < skcms_PixelFormat_RGB_hhh) {
3019 add_op(Op::clamp);
3020 }
3021
3022 if (dstProfile->data_color_space == skcms_Signature_CMYK) {
3023 // Photoshop creates CMYK images as inverse CMYK.
3024 // These happen to be the only ones we've _ever_ seen.
3025 add_op(Op::invert);
3026
3027 // CMYK has no alpha channel, so make sure dstAlpha is a no-op.
3028 dstAlpha = skcms_AlphaFormat_Unpremul;
3029 }
3030
3031 if (dstAlpha == skcms_AlphaFormat_Opaque) {
3032 add_op(Op::force_opaque);
3033 } else if (dstAlpha == skcms_AlphaFormat_PremulAsEncoded) {
3034 add_op(Op::premul);
3035 }
3036 if (dstFmt & 1) {
3037 add_op(Op::swap_rb);
3038 }
3039 switch (dstFmt >> 1) {
3040 default: return false;
3041 case skcms_PixelFormat_A_8 >> 1: add_op(Op::store_a8); break;
3042 case skcms_PixelFormat_G_8 >> 1: add_op(Op::store_g8); break;
3043 case skcms_PixelFormat_GA_88 >> 1: add_op(Op::store_ga88); break;
3044 case skcms_PixelFormat_ABGR_4444 >> 1: add_op(Op::store_4444); break;
3045 case skcms_PixelFormat_RGB_565 >> 1: add_op(Op::store_565); break;
3046 case skcms_PixelFormat_RGB_888 >> 1: add_op(Op::store_888); break;
3047 case skcms_PixelFormat_RGBA_8888 >> 1: add_op(Op::store_8888); break;
3048 case skcms_PixelFormat_RGBA_1010102 >> 1: add_op(Op::store_1010102); break;
3049 case skcms_PixelFormat_RGB_161616LE >> 1: add_op(Op::store_161616LE); break;
3050 case skcms_PixelFormat_RGBA_16161616LE >> 1: add_op(Op::store_16161616LE); break;
3051 case skcms_PixelFormat_RGB_161616BE >> 1: add_op(Op::store_161616BE); break;
3052 case skcms_PixelFormat_RGBA_16161616BE >> 1: add_op(Op::store_16161616BE); break;
3053 case skcms_PixelFormat_RGB_hhh_Norm >> 1: add_op(Op::store_hhh); break;
3054 case skcms_PixelFormat_RGBA_hhhh_Norm >> 1: add_op(Op::store_hhhh); break;
3055 case skcms_PixelFormat_RGB_101010x_XR >> 1: add_op(Op::store_101010x_XR); break;
3056 case skcms_PixelFormat_RGBA_10101010_XR >> 1: add_op(Op::store_10101010_XR); break;
3057 case skcms_PixelFormat_RGB_hhh >> 1: add_op(Op::store_hhh); break;
3058 case skcms_PixelFormat_RGBA_hhhh >> 1: add_op(Op::store_hhhh); break;
3059 case skcms_PixelFormat_RGB_fff >> 1: add_op(Op::store_fff); break;
3060 case skcms_PixelFormat_RGBA_ffff >> 1: add_op(Op::store_ffff); break;
3061
3062 case skcms_PixelFormat_RGBA_8888_sRGB >> 1:
3063 add_op_ctx(Op::tf_rgb, skcms_sRGB_Inverse_TransferFunction());
3064 add_op(Op::store_8888);
3065 break;
3066 }
3067
3068 assert(ops <= program + ARRAY_COUNT(program));
3069 assert(contexts <= context + ARRAY_COUNT(context));
3070
3071 auto run = baseline::run_program;
3072 switch (cpu_type()) {
3073 case CpuType::SKX:
3074 #if !defined(SKCMS_DISABLE_SKX)
3075 run = skx::run_program;
3076 break;
3077 #endif
3078
3079 case CpuType::HSW:
3080 #if !defined(SKCMS_DISABLE_HSW)
3081 run = hsw::run_program;
3082 break;
3083 #endif
3084
3085 case CpuType::Baseline:
3086 break;
3087 }
3088
3089 run(program, context, ops - program, (const char*)src, (char*)dst, n, src_bpp,dst_bpp);
3090 return true;
3091 }
3092
3093 static void assert_usable_as_destination(const skcms_ICCProfile* profile) {
3094 #if defined(NDEBUG)
3095 (void)profile;
3096 #else
3097 skcms_Matrix3x3 fromXYZD50;
3098 skcms_TransferFunction invR, invG, invB;
3099 bool useB2A = false;
3100 bool useHlgOotf = false;
3101 assert(prep_for_destination(profile, &fromXYZD50, &invR, &invG, &invB, &useB2A, &useHlgOotf));
3102 #endif
3103 }
3104
3105 bool skcms_MakeUsableAsDestination(skcms_ICCProfile* profile) {
3106 if (!profile->has_B2A) {
3107 skcms_Matrix3x3 fromXYZD50;
3108 if (!profile->has_trc || !profile->has_toXYZD50
3109 || !skcms_Matrix3x3_invert(&profile->toXYZD50, &fromXYZD50)) {
3110 return false;
3111 }
3112
3113 skcms_TransferFunction tf[3];
3114 for (int i = 0; i < 3; i++) {
3115 skcms_TransferFunction inv;
3116 if (profile->trc[i].table_entries == 0
3117 && skcms_TransferFunction_invert(&profile->trc[i].parametric, &inv)) {
3118 tf[i] = profile->trc[i].parametric;
3119 continue;
3120 }
3121
3122 float max_error;
3123 // Parametric curves from skcms_ApproximateCurve() are guaranteed to be invertible.
3124 if (!skcms_ApproximateCurve(&profile->trc[i], &tf[i], &max_error)) {
3125 return false;
3126 }
3127 }
3128
3129 for (int i = 0; i < 3; ++i) {
3130 profile->trc[i].table_entries = 0;
3131 profile->trc[i].parametric = tf[i];
3132 }
3133 }
3134 assert_usable_as_destination(profile);
3135 return true;
3136 }
3137
3138 bool skcms_MakeUsableAsDestinationWithSingleCurve(skcms_ICCProfile* profile) {
3139 // Call skcms_MakeUsableAsDestination() with B2A disabled;
3140 // on success that'll return a TRC/XYZ profile with three skcms_TransferFunctions.
3141 skcms_ICCProfile result = *profile;
3142 result.has_B2A = false;
3143 if (!skcms_MakeUsableAsDestination(&result)) {
3144 return false;
3145 }
3146
3147 // Of the three, pick the transfer function that best fits the other two.
3148 int best_tf = 0;
3149 float min_max_error = INFINITY_;
3150 for (int i = 0; i < 3; i++) {
3151 skcms_TransferFunction inv;
3152 if (!skcms_TransferFunction_invert(&result.trc[i].parametric, &inv)) {
3153 return false;
3154 }
3155
3156 float err = 0;
3157 for (int j = 0; j < 3; ++j) {
3158 err = fmaxf_(err, skcms_MaxRoundtripError(&profile->trc[j], &inv));
3159 }
3160 if (min_max_error > err) {
3161 min_max_error = err;
3162 best_tf = i;
3163 }
3164 }
3165
3166 for (int i = 0; i < 3; i++) {
3167 result.trc[i].parametric = result.trc[best_tf].parametric;
3168 }
3169
3170 *profile = result;
3171 assert_usable_as_destination(profile);
3172 return true;
3173 }