-
Notifications
You must be signed in to change notification settings - Fork 0
/
skcms.cc
2960 lines (2567 loc) · 107 KB
/
skcms.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "src/skcms_public.h" // NO_G3_REWRITE
#include "src/skcms_internals.h" // NO_G3_REWRITE
#include "src/skcms_Transform.h" // NO_G3_REWRITE
#include <assert.h>
#include <float.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#if defined(__ARM_NEON)
#include <arm_neon.h>
#elif defined(__SSE__)
#include <immintrin.h>
#if defined(__clang__)
// That #include <immintrin.h> is usually enough, but Clang's headers
// "helpfully" skip including the whole kitchen sink when _MSC_VER is
// defined, because lots of programs on Windows would include that and
// it'd be a lot slower. But we want all those headers included so we
// can use their features after runtime checks later.
#include <smmintrin.h>
#include <avxintrin.h>
#include <avx2intrin.h>
#include <avx512fintrin.h>
#include <avx512dqintrin.h>
#endif
#endif
using namespace skcms_private;
static bool sAllowRuntimeCPUDetection = true;
void skcms_DisableRuntimeCPUDetection() {
sAllowRuntimeCPUDetection = false;
}
static float log2f_(float x) {
// The first approximation of log2(x) is its exponent 'e', minus 127.
int32_t bits;
memcpy(&bits, &x, sizeof(bits));
float e = (float)bits * (1.0f / (1<<23));
// If we use the mantissa too we can refine the error signficantly.
int32_t m_bits = (bits & 0x007fffff) | 0x3f000000;
float m;
memcpy(&m, &m_bits, sizeof(m));
return (e - 124.225514990f
- 1.498030302f*m
- 1.725879990f/(0.3520887068f + m));
}
static float logf_(float x) {
const float ln2 = 0.69314718f;
return ln2*log2f_(x);
}
static float exp2f_(float x) {
if (x > 128.0f) {
return INFINITY_;
} else if (x < -127.0f) {
return 0.0f;
}
float fract = x - floorf_(x);
float fbits = (1.0f * (1<<23)) * (x + 121.274057500f
- 1.490129070f*fract
+ 27.728023300f/(4.84252568f - fract));
// Before we cast fbits to int32_t, check for out of range values to pacify UBSAN.
// INT_MAX is not exactly representable as a float, so exclude it as effectively infinite.
// Negative values are effectively underflow - we'll end up returning a (different) negative
// value, which makes no sense. So clamp to zero.
if (fbits >= (float)INT_MAX) {
return INFINITY_;
} else if (fbits < 0) {
return 0;
}
int32_t bits = (int32_t)fbits;
memcpy(&x, &bits, sizeof(x));
return x;
}
// Not static, as it's used by some test tools.
float powf_(float x, float y) {
if (x <= 0.f) {
return 0.f;
}
if (x == 1.f) {
return 1.f;
}
return exp2f_(log2f_(x) * y);
}
static float expf_(float x) {
const float log2_e = 1.4426950408889634074f;
return exp2f_(log2_e * x);
}
static float fmaxf_(float x, float y) { return x > y ? x : y; }
static float fminf_(float x, float y) { return x < y ? x : y; }
static bool isfinitef_(float x) { return 0 == x*0; }
static float minus_1_ulp(float x) {
int32_t bits;
memcpy(&bits, &x, sizeof(bits));
bits = bits - 1;
memcpy(&x, &bits, sizeof(bits));
return x;
}
// Most transfer functions we work with are sRGBish.
// For exotic HDR transfer functions, we encode them using a tf.g that makes no sense,
// and repurpose the other fields to hold the parameters of the HDR functions.
struct TF_PQish { float A,B,C,D,E,F; };
struct TF_HLGish { float R,G,a,b,c,K_minus_1; };
// We didn't originally support a scale factor K for HLG, and instead just stored 0 in
// the unused `f` field of skcms_TransferFunction for HLGish and HLGInvish transfer functions.
// By storing f=K-1, those old unusued f=0 values now mean K=1, a noop scale factor.
static float TFKind_marker(skcms_TFType kind) {
// We'd use different NaNs, but those aren't guaranteed to be preserved by WASM.
return -(float)kind;
}
static skcms_TFType classify(const skcms_TransferFunction& tf, TF_PQish* pq = nullptr
, TF_HLGish* hlg = nullptr) {
if (tf.g < 0) {
// Negative "g" is mapped to enum values; large negative are for sure invalid.
if (tf.g < -128) {
return skcms_TFType_Invalid;
}
int enum_g = -static_cast<int>(tf.g);
// Non-whole "g" values are invalid as well.
if (static_cast<float>(-enum_g) != tf.g) {
return skcms_TFType_Invalid;
}
// TODO: soundness checks for PQ/HLG like we do for sRGBish?
switch (enum_g) {
case skcms_TFType_PQish:
if (pq) {
memcpy(pq , &tf.a, sizeof(*pq ));
}
return skcms_TFType_PQish;
case skcms_TFType_HLGish:
if (hlg) {
memcpy(hlg, &tf.a, sizeof(*hlg));
}
return skcms_TFType_HLGish;
case skcms_TFType_HLGinvish:
if (hlg) {
memcpy(hlg, &tf.a, sizeof(*hlg));
}
return skcms_TFType_HLGinvish;
}
return skcms_TFType_Invalid;
}
// Basic soundness checks for sRGBish transfer functions.
if (isfinitef_(tf.a + tf.b + tf.c + tf.d + tf.e + tf.f + tf.g)
// a,c,d,g should be non-negative to make any sense.
&& tf.a >= 0
&& tf.c >= 0
&& tf.d >= 0
&& tf.g >= 0
// Raising a negative value to a fractional tf->g produces complex numbers.
&& tf.a * tf.d + tf.b >= 0) {
return skcms_TFType_sRGBish;
}
return skcms_TFType_Invalid;
}
skcms_TFType skcms_TransferFunction_getType(const skcms_TransferFunction* tf) {
return classify(*tf);
}
bool skcms_TransferFunction_isSRGBish(const skcms_TransferFunction* tf) {
return classify(*tf) == skcms_TFType_sRGBish;
}
bool skcms_TransferFunction_isPQish(const skcms_TransferFunction* tf) {
return classify(*tf) == skcms_TFType_PQish;
}
bool skcms_TransferFunction_isHLGish(const skcms_TransferFunction* tf) {
return classify(*tf) == skcms_TFType_HLGish;
}
bool skcms_TransferFunction_makePQish(skcms_TransferFunction* tf,
float A, float B, float C,
float D, float E, float F) {
*tf = { TFKind_marker(skcms_TFType_PQish), A,B,C,D,E,F };
assert(skcms_TransferFunction_isPQish(tf));
return true;
}
bool skcms_TransferFunction_makeScaledHLGish(skcms_TransferFunction* tf,
float K, float R, float G,
float a, float b, float c) {
*tf = { TFKind_marker(skcms_TFType_HLGish), R,G, a,b,c, K-1.0f };
assert(skcms_TransferFunction_isHLGish(tf));
return true;
}
float skcms_TransferFunction_eval(const skcms_TransferFunction* tf, float x) {
float sign = x < 0 ? -1.0f : 1.0f;
x *= sign;
TF_PQish pq;
TF_HLGish hlg;
switch (classify(*tf, &pq, &hlg)) {
case skcms_TFType_Invalid: break;
case skcms_TFType_HLGish: {
const float K = hlg.K_minus_1 + 1.0f;
return K * sign * (x*hlg.R <= 1 ? powf_(x*hlg.R, hlg.G)
: expf_((x-hlg.c)*hlg.a) + hlg.b);
}
// skcms_TransferFunction_invert() inverts R, G, and a for HLGinvish so this math is fast.
case skcms_TFType_HLGinvish: {
const float K = hlg.K_minus_1 + 1.0f;
x /= K;
return sign * (x <= 1 ? hlg.R * powf_(x, hlg.G)
: hlg.a * logf_(x - hlg.b) + hlg.c);
}
case skcms_TFType_sRGBish:
return sign * (x < tf->d ? tf->c * x + tf->f
: powf_(tf->a * x + tf->b, tf->g) + tf->e);
case skcms_TFType_PQish:
return sign *
powf_((pq.A + pq.B * powf_(x, pq.C)) / (pq.D + pq.E * powf_(x, pq.C)), pq.F);
}
return 0;
}
static float eval_curve(const skcms_Curve* curve, float x) {
if (curve->table_entries == 0) {
return skcms_TransferFunction_eval(&curve->parametric, x);
}
float ix = fmaxf_(0, fminf_(x, 1)) * static_cast<float>(curve->table_entries - 1);
int lo = (int) ix ,
hi = (int)(float)minus_1_ulp(ix + 1.0f);
float t = ix - (float)lo;
float l, h;
if (curve->table_8) {
l = curve->table_8[lo] * (1/255.0f);
h = curve->table_8[hi] * (1/255.0f);
} else {
uint16_t be_l, be_h;
memcpy(&be_l, curve->table_16 + 2*lo, 2);
memcpy(&be_h, curve->table_16 + 2*hi, 2);
uint16_t le_l = ((be_l << 8) | (be_l >> 8)) & 0xffff;
uint16_t le_h = ((be_h << 8) | (be_h >> 8)) & 0xffff;
l = le_l * (1/65535.0f);
h = le_h * (1/65535.0f);
}
return l + (h-l)*t;
}
float skcms_MaxRoundtripError(const skcms_Curve* curve, const skcms_TransferFunction* inv_tf) {
uint32_t N = curve->table_entries > 256 ? curve->table_entries : 256;
const float dx = 1.0f / static_cast<float>(N - 1);
float err = 0;
for (uint32_t i = 0; i < N; i++) {
float x = static_cast<float>(i) * dx,
y = eval_curve(curve, x);
err = fmaxf_(err, fabsf_(x - skcms_TransferFunction_eval(inv_tf, y)));
}
return err;
}
bool skcms_AreApproximateInverses(const skcms_Curve* curve, const skcms_TransferFunction* inv_tf) {
return skcms_MaxRoundtripError(curve, inv_tf) < (1/512.0f);
}
// Additional ICC signature values that are only used internally
enum {
// File signature
skcms_Signature_acsp = 0x61637370,
// Tag signatures
skcms_Signature_rTRC = 0x72545243,
skcms_Signature_gTRC = 0x67545243,
skcms_Signature_bTRC = 0x62545243,
skcms_Signature_kTRC = 0x6B545243,
skcms_Signature_rXYZ = 0x7258595A,
skcms_Signature_gXYZ = 0x6758595A,
skcms_Signature_bXYZ = 0x6258595A,
skcms_Signature_A2B0 = 0x41324230,
skcms_Signature_B2A0 = 0x42324130,
skcms_Signature_CHAD = 0x63686164,
skcms_Signature_WTPT = 0x77747074,
skcms_Signature_CICP = 0x63696370,
// Type signatures
skcms_Signature_curv = 0x63757276,
skcms_Signature_mft1 = 0x6D667431,
skcms_Signature_mft2 = 0x6D667432,
skcms_Signature_mAB = 0x6D414220,
skcms_Signature_mBA = 0x6D424120,
skcms_Signature_para = 0x70617261,
skcms_Signature_sf32 = 0x73663332,
// XYZ is also a PCS signature, so it's defined in skcms.h
// skcms_Signature_XYZ = 0x58595A20,
};
static uint16_t read_big_u16(const uint8_t* ptr) {
uint16_t be;
memcpy(&be, ptr, sizeof(be));
#if defined(_MSC_VER)
return _byteswap_ushort(be);
#else
return __builtin_bswap16(be);
#endif
}
static uint32_t read_big_u32(const uint8_t* ptr) {
uint32_t be;
memcpy(&be, ptr, sizeof(be));
#if defined(_MSC_VER)
return _byteswap_ulong(be);
#else
return __builtin_bswap32(be);
#endif
}
static int32_t read_big_i32(const uint8_t* ptr) {
return (int32_t)read_big_u32(ptr);
}
static float read_big_fixed(const uint8_t* ptr) {
return static_cast<float>(read_big_i32(ptr)) * (1.0f / 65536.0f);
}
// Maps to an in-memory profile so that fields line up to the locations specified
// in ICC.1:2010, section 7.2
typedef struct {
uint8_t size [ 4];
uint8_t cmm_type [ 4];
uint8_t version [ 4];
uint8_t profile_class [ 4];
uint8_t data_color_space [ 4];
uint8_t pcs [ 4];
uint8_t creation_date_time [12];
uint8_t signature [ 4];
uint8_t platform [ 4];
uint8_t flags [ 4];
uint8_t device_manufacturer [ 4];
uint8_t device_model [ 4];
uint8_t device_attributes [ 8];
uint8_t rendering_intent [ 4];
uint8_t illuminant_X [ 4];
uint8_t illuminant_Y [ 4];
uint8_t illuminant_Z [ 4];
uint8_t creator [ 4];
uint8_t profile_id [16];
uint8_t reserved [28];
uint8_t tag_count [ 4]; // Technically not part of header, but required
} header_Layout;
typedef struct {
uint8_t signature [4];
uint8_t offset [4];
uint8_t size [4];
} tag_Layout;
static const tag_Layout* get_tag_table(const skcms_ICCProfile* profile) {
return (const tag_Layout*)(profile->buffer + SAFE_SIZEOF(header_Layout));
}
// s15Fixed16ArrayType is technically variable sized, holding N values. However, the only valid
// use of the type is for the CHAD tag that stores exactly nine values.
typedef struct {
uint8_t type [ 4];
uint8_t reserved [ 4];
uint8_t values [36];
} sf32_Layout;
bool skcms_GetCHAD(const skcms_ICCProfile* profile, skcms_Matrix3x3* m) {
skcms_ICCTag tag;
if (!skcms_GetTagBySignature(profile, skcms_Signature_CHAD, &tag)) {
return false;
}
if (tag.type != skcms_Signature_sf32 || tag.size < SAFE_SIZEOF(sf32_Layout)) {
return false;
}
const sf32_Layout* sf32Tag = (const sf32_Layout*)tag.buf;
const uint8_t* values = sf32Tag->values;
for (int r = 0; r < 3; ++r)
for (int c = 0; c < 3; ++c, values += 4) {
m->vals[r][c] = read_big_fixed(values);
}
return true;
}
// XYZType is technically variable sized, holding N XYZ triples. However, the only valid uses of
// the type are for tags/data that store exactly one triple.
typedef struct {
uint8_t type [4];
uint8_t reserved [4];
uint8_t X [4];
uint8_t Y [4];
uint8_t Z [4];
} XYZ_Layout;
static bool read_tag_xyz(const skcms_ICCTag* tag, float* x, float* y, float* z) {
if (tag->type != skcms_Signature_XYZ || tag->size < SAFE_SIZEOF(XYZ_Layout)) {
return false;
}
const XYZ_Layout* xyzTag = (const XYZ_Layout*)tag->buf;
*x = read_big_fixed(xyzTag->X);
*y = read_big_fixed(xyzTag->Y);
*z = read_big_fixed(xyzTag->Z);
return true;
}
bool skcms_GetWTPT(const skcms_ICCProfile* profile, float xyz[3]) {
skcms_ICCTag tag;
return skcms_GetTagBySignature(profile, skcms_Signature_WTPT, &tag) &&
read_tag_xyz(&tag, &xyz[0], &xyz[1], &xyz[2]);
}
static int data_color_space_channel_count(uint32_t data_color_space) {
switch (data_color_space) {
case skcms_Signature_CMYK: return 4;
case skcms_Signature_Gray: return 1;
case skcms_Signature_RGB: return 3;
case skcms_Signature_Lab: return 3;
case skcms_Signature_XYZ: return 3;
case skcms_Signature_CIELUV: return 3;
case skcms_Signature_YCbCr: return 3;
case skcms_Signature_CIEYxy: return 3;
case skcms_Signature_HSV: return 3;
case skcms_Signature_HLS: return 3;
case skcms_Signature_CMY: return 3;
case skcms_Signature_2CLR: return 2;
case skcms_Signature_3CLR: return 3;
case skcms_Signature_4CLR: return 4;
case skcms_Signature_5CLR: return 5;
case skcms_Signature_6CLR: return 6;
case skcms_Signature_7CLR: return 7;
case skcms_Signature_8CLR: return 8;
case skcms_Signature_9CLR: return 9;
case skcms_Signature_10CLR: return 10;
case skcms_Signature_11CLR: return 11;
case skcms_Signature_12CLR: return 12;
case skcms_Signature_13CLR: return 13;
case skcms_Signature_14CLR: return 14;
case skcms_Signature_15CLR: return 15;
default: return -1;
}
}
int skcms_GetInputChannelCount(const skcms_ICCProfile* profile) {
int a2b_count = 0;
if (profile->has_A2B) {
a2b_count = profile->A2B.input_channels != 0
? static_cast<int>(profile->A2B.input_channels)
: 3;
}
skcms_ICCTag tag;
int trc_count = 0;
if (skcms_GetTagBySignature(profile, skcms_Signature_kTRC, &tag)) {
trc_count = 1;
} else if (profile->has_trc) {
trc_count = 3;
}
int dcs_count = data_color_space_channel_count(profile->data_color_space);
if (dcs_count < 0) {
return -1;
}
if (a2b_count > 0 && a2b_count != dcs_count) {
return -1;
}
if (trc_count > 0 && trc_count != dcs_count) {
return -1;
}
return dcs_count;
}
static bool read_to_XYZD50(const skcms_ICCTag* rXYZ, const skcms_ICCTag* gXYZ,
const skcms_ICCTag* bXYZ, skcms_Matrix3x3* toXYZ) {
return read_tag_xyz(rXYZ, &toXYZ->vals[0][0], &toXYZ->vals[1][0], &toXYZ->vals[2][0]) &&
read_tag_xyz(gXYZ, &toXYZ->vals[0][1], &toXYZ->vals[1][1], &toXYZ->vals[2][1]) &&
read_tag_xyz(bXYZ, &toXYZ->vals[0][2], &toXYZ->vals[1][2], &toXYZ->vals[2][2]);
}
typedef struct {
uint8_t type [4];
uint8_t reserved_a [4];
uint8_t function_type [2];
uint8_t reserved_b [2];
uint8_t variable [1/*variable*/]; // 1, 3, 4, 5, or 7 s15.16, depending on function_type
} para_Layout;
static bool read_curve_para(const uint8_t* buf, uint32_t size,
skcms_Curve* curve, uint32_t* curve_size) {
if (size < SAFE_FIXED_SIZE(para_Layout)) {
return false;
}
const para_Layout* paraTag = (const para_Layout*)buf;
enum { kG = 0, kGAB = 1, kGABC = 2, kGABCD = 3, kGABCDEF = 4 };
uint16_t function_type = read_big_u16(paraTag->function_type);
if (function_type > kGABCDEF) {
return false;
}
static const uint32_t curve_bytes[] = { 4, 12, 16, 20, 28 };
if (size < SAFE_FIXED_SIZE(para_Layout) + curve_bytes[function_type]) {
return false;
}
if (curve_size) {
*curve_size = SAFE_FIXED_SIZE(para_Layout) + curve_bytes[function_type];
}
curve->table_entries = 0;
curve->parametric.a = 1.0f;
curve->parametric.b = 0.0f;
curve->parametric.c = 0.0f;
curve->parametric.d = 0.0f;
curve->parametric.e = 0.0f;
curve->parametric.f = 0.0f;
curve->parametric.g = read_big_fixed(paraTag->variable);
switch (function_type) {
case kGAB:
curve->parametric.a = read_big_fixed(paraTag->variable + 4);
curve->parametric.b = read_big_fixed(paraTag->variable + 8);
if (curve->parametric.a == 0) {
return false;
}
curve->parametric.d = -curve->parametric.b / curve->parametric.a;
break;
case kGABC:
curve->parametric.a = read_big_fixed(paraTag->variable + 4);
curve->parametric.b = read_big_fixed(paraTag->variable + 8);
curve->parametric.e = read_big_fixed(paraTag->variable + 12);
if (curve->parametric.a == 0) {
return false;
}
curve->parametric.d = -curve->parametric.b / curve->parametric.a;
curve->parametric.f = curve->parametric.e;
break;
case kGABCD:
curve->parametric.a = read_big_fixed(paraTag->variable + 4);
curve->parametric.b = read_big_fixed(paraTag->variable + 8);
curve->parametric.c = read_big_fixed(paraTag->variable + 12);
curve->parametric.d = read_big_fixed(paraTag->variable + 16);
break;
case kGABCDEF:
curve->parametric.a = read_big_fixed(paraTag->variable + 4);
curve->parametric.b = read_big_fixed(paraTag->variable + 8);
curve->parametric.c = read_big_fixed(paraTag->variable + 12);
curve->parametric.d = read_big_fixed(paraTag->variable + 16);
curve->parametric.e = read_big_fixed(paraTag->variable + 20);
curve->parametric.f = read_big_fixed(paraTag->variable + 24);
break;
}
return skcms_TransferFunction_isSRGBish(&curve->parametric);
}
typedef struct {
uint8_t type [4];
uint8_t reserved [4];
uint8_t value_count [4];
uint8_t variable [1/*variable*/]; // value_count, 8.8 if 1, uint16 (n*65535) if > 1
} curv_Layout;
static bool read_curve_curv(const uint8_t* buf, uint32_t size,
skcms_Curve* curve, uint32_t* curve_size) {
if (size < SAFE_FIXED_SIZE(curv_Layout)) {
return false;
}
const curv_Layout* curvTag = (const curv_Layout*)buf;
uint32_t value_count = read_big_u32(curvTag->value_count);
if (size < SAFE_FIXED_SIZE(curv_Layout) + value_count * SAFE_SIZEOF(uint16_t)) {
return false;
}
if (curve_size) {
*curve_size = SAFE_FIXED_SIZE(curv_Layout) + value_count * SAFE_SIZEOF(uint16_t);
}
if (value_count < 2) {
curve->table_entries = 0;
curve->parametric.a = 1.0f;
curve->parametric.b = 0.0f;
curve->parametric.c = 0.0f;
curve->parametric.d = 0.0f;
curve->parametric.e = 0.0f;
curve->parametric.f = 0.0f;
if (value_count == 0) {
// Empty tables are a shorthand for an identity curve
curve->parametric.g = 1.0f;
} else {
// Single entry tables are a shorthand for simple gamma
curve->parametric.g = read_big_u16(curvTag->variable) * (1.0f / 256.0f);
}
} else {
curve->table_8 = nullptr;
curve->table_16 = curvTag->variable;
curve->table_entries = value_count;
}
return true;
}
// Parses both curveType and parametricCurveType data. Ensures that at most 'size' bytes are read.
// If curve_size is not nullptr, writes the number of bytes used by the curve in (*curve_size).
static bool read_curve(const uint8_t* buf, uint32_t size,
skcms_Curve* curve, uint32_t* curve_size) {
if (!buf || size < 4 || !curve) {
return false;
}
uint32_t type = read_big_u32(buf);
if (type == skcms_Signature_para) {
return read_curve_para(buf, size, curve, curve_size);
} else if (type == skcms_Signature_curv) {
return read_curve_curv(buf, size, curve, curve_size);
}
return false;
}
// mft1 and mft2 share a large chunk of data
typedef struct {
uint8_t type [ 4];
uint8_t reserved_a [ 4];
uint8_t input_channels [ 1];
uint8_t output_channels [ 1];
uint8_t grid_points [ 1];
uint8_t reserved_b [ 1];
uint8_t matrix [36];
} mft_CommonLayout;
typedef struct {
mft_CommonLayout common [1];
uint8_t variable [1/*variable*/];
} mft1_Layout;
typedef struct {
mft_CommonLayout common [1];
uint8_t input_table_entries [2];
uint8_t output_table_entries [2];
uint8_t variable [1/*variable*/];
} mft2_Layout;
static bool read_mft_common(const mft_CommonLayout* mftTag, skcms_A2B* a2b) {
// MFT matrices are applied before the first set of curves, but must be identity unless the
// input is PCSXYZ. We don't support PCSXYZ profiles, so we ignore this matrix. Note that the
// matrix in skcms_A2B is applied later in the pipe, so supporting this would require another
// field/flag.
a2b->matrix_channels = 0;
a2b-> input_channels = mftTag-> input_channels[0];
a2b->output_channels = mftTag->output_channels[0];
// We require exactly three (ie XYZ/Lab/RGB) output channels
if (a2b->output_channels != ARRAY_COUNT(a2b->output_curves)) {
return false;
}
// We require at least one, and no more than four (ie CMYK) input channels
if (a2b->input_channels < 1 || a2b->input_channels > ARRAY_COUNT(a2b->input_curves)) {
return false;
}
for (uint32_t i = 0; i < a2b->input_channels; ++i) {
a2b->grid_points[i] = mftTag->grid_points[0];
}
// The grid only makes sense with at least two points along each axis
if (a2b->grid_points[0] < 2) {
return false;
}
return true;
}
// All as the A2B version above, except where noted.
static bool read_mft_common(const mft_CommonLayout* mftTag, skcms_B2A* b2a) {
// Same as A2B.
b2a->matrix_channels = 0;
b2a-> input_channels = mftTag-> input_channels[0];
b2a->output_channels = mftTag->output_channels[0];
// For B2A, exactly 3 input channels (XYZ) and 3 (RGB) or 4 (CMYK) output channels.
if (b2a->input_channels != ARRAY_COUNT(b2a->input_curves)) {
return false;
}
if (b2a->output_channels < 3 || b2a->output_channels > ARRAY_COUNT(b2a->output_curves)) {
return false;
}
// Same as A2B.
for (uint32_t i = 0; i < b2a->input_channels; ++i) {
b2a->grid_points[i] = mftTag->grid_points[0];
}
if (b2a->grid_points[0] < 2) {
return false;
}
return true;
}
template <typename A2B_or_B2A>
static bool init_tables(const uint8_t* table_base, uint64_t max_tables_len, uint32_t byte_width,
uint32_t input_table_entries, uint32_t output_table_entries,
A2B_or_B2A* out) {
// byte_width is 1 or 2, [input|output]_table_entries are in [2, 4096], so no overflow
uint32_t byte_len_per_input_table = input_table_entries * byte_width;
uint32_t byte_len_per_output_table = output_table_entries * byte_width;
// [input|output]_channels are <= 4, so still no overflow
uint32_t byte_len_all_input_tables = out->input_channels * byte_len_per_input_table;
uint32_t byte_len_all_output_tables = out->output_channels * byte_len_per_output_table;
uint64_t grid_size = out->output_channels * byte_width;
for (uint32_t axis = 0; axis < out->input_channels; ++axis) {
grid_size *= out->grid_points[axis];
}
if (max_tables_len < byte_len_all_input_tables + grid_size + byte_len_all_output_tables) {
return false;
}
for (uint32_t i = 0; i < out->input_channels; ++i) {
out->input_curves[i].table_entries = input_table_entries;
if (byte_width == 1) {
out->input_curves[i].table_8 = table_base + i * byte_len_per_input_table;
out->input_curves[i].table_16 = nullptr;
} else {
out->input_curves[i].table_8 = nullptr;
out->input_curves[i].table_16 = table_base + i * byte_len_per_input_table;
}
}
if (byte_width == 1) {
out->grid_8 = table_base + byte_len_all_input_tables;
out->grid_16 = nullptr;
} else {
out->grid_8 = nullptr;
out->grid_16 = table_base + byte_len_all_input_tables;
}
const uint8_t* output_table_base = table_base + byte_len_all_input_tables + grid_size;
for (uint32_t i = 0; i < out->output_channels; ++i) {
out->output_curves[i].table_entries = output_table_entries;
if (byte_width == 1) {
out->output_curves[i].table_8 = output_table_base + i * byte_len_per_output_table;
out->output_curves[i].table_16 = nullptr;
} else {
out->output_curves[i].table_8 = nullptr;
out->output_curves[i].table_16 = output_table_base + i * byte_len_per_output_table;
}
}
return true;
}
template <typename A2B_or_B2A>
static bool read_tag_mft1(const skcms_ICCTag* tag, A2B_or_B2A* out) {
if (tag->size < SAFE_FIXED_SIZE(mft1_Layout)) {
return false;
}
const mft1_Layout* mftTag = (const mft1_Layout*)tag->buf;
if (!read_mft_common(mftTag->common, out)) {
return false;
}
uint32_t input_table_entries = 256;
uint32_t output_table_entries = 256;
return init_tables(mftTag->variable, tag->size - SAFE_FIXED_SIZE(mft1_Layout), 1,
input_table_entries, output_table_entries, out);
}
template <typename A2B_or_B2A>
static bool read_tag_mft2(const skcms_ICCTag* tag, A2B_or_B2A* out) {
if (tag->size < SAFE_FIXED_SIZE(mft2_Layout)) {
return false;
}
const mft2_Layout* mftTag = (const mft2_Layout*)tag->buf;
if (!read_mft_common(mftTag->common, out)) {
return false;
}
uint32_t input_table_entries = read_big_u16(mftTag->input_table_entries);
uint32_t output_table_entries = read_big_u16(mftTag->output_table_entries);
// ICC spec mandates that 2 <= table_entries <= 4096
if (input_table_entries < 2 || input_table_entries > 4096 ||
output_table_entries < 2 || output_table_entries > 4096) {
return false;
}
return init_tables(mftTag->variable, tag->size - SAFE_FIXED_SIZE(mft2_Layout), 2,
input_table_entries, output_table_entries, out);
}
static bool read_curves(const uint8_t* buf, uint32_t size, uint32_t curve_offset,
uint32_t num_curves, skcms_Curve* curves) {
for (uint32_t i = 0; i < num_curves; ++i) {
if (curve_offset > size) {
return false;
}
uint32_t curve_bytes;
if (!read_curve(buf + curve_offset, size - curve_offset, &curves[i], &curve_bytes)) {
return false;
}
if (curve_bytes > UINT32_MAX - 3) {
return false;
}
curve_bytes = (curve_bytes + 3) & ~3U;
uint64_t new_offset_64 = (uint64_t)curve_offset + curve_bytes;
curve_offset = (uint32_t)new_offset_64;
if (new_offset_64 != curve_offset) {
return false;
}
}
return true;
}
// mAB and mBA tags use the same encoding, including color lookup tables.
typedef struct {
uint8_t type [ 4];
uint8_t reserved_a [ 4];
uint8_t input_channels [ 1];
uint8_t output_channels [ 1];
uint8_t reserved_b [ 2];
uint8_t b_curve_offset [ 4];
uint8_t matrix_offset [ 4];
uint8_t m_curve_offset [ 4];
uint8_t clut_offset [ 4];
uint8_t a_curve_offset [ 4];
} mAB_or_mBA_Layout;
typedef struct {
uint8_t grid_points [16];
uint8_t grid_byte_width [ 1];
uint8_t reserved [ 3];
uint8_t variable [1/*variable*/];
} CLUT_Layout;
static bool read_tag_mab(const skcms_ICCTag* tag, skcms_A2B* a2b, bool pcs_is_xyz) {
if (tag->size < SAFE_SIZEOF(mAB_or_mBA_Layout)) {
return false;
}
const mAB_or_mBA_Layout* mABTag = (const mAB_or_mBA_Layout*)tag->buf;
a2b->input_channels = mABTag->input_channels[0];
a2b->output_channels = mABTag->output_channels[0];
// We require exactly three (ie XYZ/Lab/RGB) output channels
if (a2b->output_channels != ARRAY_COUNT(a2b->output_curves)) {
return false;
}
// We require no more than four (ie CMYK) input channels
if (a2b->input_channels > ARRAY_COUNT(a2b->input_curves)) {
return false;
}
uint32_t b_curve_offset = read_big_u32(mABTag->b_curve_offset);
uint32_t matrix_offset = read_big_u32(mABTag->matrix_offset);
uint32_t m_curve_offset = read_big_u32(mABTag->m_curve_offset);
uint32_t clut_offset = read_big_u32(mABTag->clut_offset);
uint32_t a_curve_offset = read_big_u32(mABTag->a_curve_offset);
// "B" curves must be present
if (0 == b_curve_offset) {
return false;
}
if (!read_curves(tag->buf, tag->size, b_curve_offset, a2b->output_channels,
a2b->output_curves)) {
return false;
}
// "M" curves and Matrix must be used together
if (0 != m_curve_offset) {
if (0 == matrix_offset) {
return false;
}
a2b->matrix_channels = a2b->output_channels;
if (!read_curves(tag->buf, tag->size, m_curve_offset, a2b->matrix_channels,
a2b->matrix_curves)) {
return false;
}
// Read matrix, which is stored as a row-major 3x3, followed by the fourth column
if (tag->size < matrix_offset + 12 * SAFE_SIZEOF(uint32_t)) {
return false;
}
float encoding_factor = pcs_is_xyz ? (65535 / 32768.0f) : 1.0f;
const uint8_t* mtx_buf = tag->buf + matrix_offset;
a2b->matrix.vals[0][0] = encoding_factor * read_big_fixed(mtx_buf + 0);
a2b->matrix.vals[0][1] = encoding_factor * read_big_fixed(mtx_buf + 4);
a2b->matrix.vals[0][2] = encoding_factor * read_big_fixed(mtx_buf + 8);
a2b->matrix.vals[1][0] = encoding_factor * read_big_fixed(mtx_buf + 12);
a2b->matrix.vals[1][1] = encoding_factor * read_big_fixed(mtx_buf + 16);
a2b->matrix.vals[1][2] = encoding_factor * read_big_fixed(mtx_buf + 20);
a2b->matrix.vals[2][0] = encoding_factor * read_big_fixed(mtx_buf + 24);
a2b->matrix.vals[2][1] = encoding_factor * read_big_fixed(mtx_buf + 28);
a2b->matrix.vals[2][2] = encoding_factor * read_big_fixed(mtx_buf + 32);
a2b->matrix.vals[0][3] = encoding_factor * read_big_fixed(mtx_buf + 36);
a2b->matrix.vals[1][3] = encoding_factor * read_big_fixed(mtx_buf + 40);
a2b->matrix.vals[2][3] = encoding_factor * read_big_fixed(mtx_buf + 44);
} else {
if (0 != matrix_offset) {
return false;
}
a2b->matrix_channels = 0;
}
// "A" curves and CLUT must be used together
if (0 != a_curve_offset) {
if (0 == clut_offset) {
return false;
}
if (!read_curves(tag->buf, tag->size, a_curve_offset, a2b->input_channels,
a2b->input_curves)) {
return false;
}
if (tag->size < clut_offset + SAFE_FIXED_SIZE(CLUT_Layout)) {
return false;
}
const CLUT_Layout* clut = (const CLUT_Layout*)(tag->buf + clut_offset);
if (clut->grid_byte_width[0] == 1) {
a2b->grid_8 = clut->variable;
a2b->grid_16 = nullptr;
} else if (clut->grid_byte_width[0] == 2) {
a2b->grid_8 = nullptr;
a2b->grid_16 = clut->variable;
} else {
return false;
}
uint64_t grid_size = a2b->output_channels * clut->grid_byte_width[0]; // the payload
for (uint32_t i = 0; i < a2b->input_channels; ++i) {
a2b->grid_points[i] = clut->grid_points[i];
// The grid only makes sense with at least two points along each axis
if (a2b->grid_points[i] < 2) {
return false;
}
grid_size *= a2b->grid_points[i];
}
if (tag->size < clut_offset + SAFE_FIXED_SIZE(CLUT_Layout) + grid_size) {
return false;
}
} else {
if (0 != clut_offset) {
return false;
}
// If there is no CLUT, the number of input and output channels must match
if (a2b->input_channels != a2b->output_channels) {
return false;
}
// Zero out the number of input channels to signal that we're skipping this stage
a2b->input_channels = 0;