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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
|
// qcms
// Copyright (C) 2009 Mozilla Foundation
// Copyright (C) 1998-2007 Marti Maria
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use std::{
convert::{TryInto, TryFrom},
sync::atomic::AtomicBool,
sync::Arc,
};
use crate::{
double_to_s15Fixed16Number,
transform::{set_rgb_colorants, PrecacheOuput},
};
use crate::{matrix::Matrix, s15Fixed16Number, s15Fixed16Number_to_float, Intent, Intent::*};
pub static SUPPORTS_ICCV4: AtomicBool = AtomicBool::new(cfg!(feature = "iccv4-enabled"));
pub const RGB_SIGNATURE: u32 = 0x52474220;
pub const GRAY_SIGNATURE: u32 = 0x47524159;
pub const XYZ_SIGNATURE: u32 = 0x58595A20;
pub const LAB_SIGNATURE: u32 = 0x4C616220;
pub const CMYK_SIGNATURE: u32 = 0x434D594B; // 'CMYK'
/// A color profile
#[derive(Default, Debug)]
pub struct Profile {
pub(crate) class_type: u32,
pub(crate) color_space: u32,
pub(crate) pcs: u32,
pub(crate) rendering_intent: Intent,
pub(crate) redColorant: XYZNumber,
pub(crate) blueColorant: XYZNumber,
pub(crate) greenColorant: XYZNumber,
// "TRC" is EOTF, e.g. gamma->linear transfer function.
// Because ICC profiles are phrased as decodings to the xyzd50-linear PCS.
pub(crate) redTRC: Option<Box<curveType>>,
pub(crate) blueTRC: Option<Box<curveType>>,
pub(crate) greenTRC: Option<Box<curveType>>,
pub(crate) grayTRC: Option<Box<curveType>>,
pub(crate) A2B0: Option<Box<lutType>>,
pub(crate) B2A0: Option<Box<lutType>>,
pub(crate) mAB: Option<Box<lutmABType>>,
pub(crate) mBA: Option<Box<lutmABType>>,
pub(crate) chromaticAdaption: Option<Matrix>,
pub(crate) precache_output: Option<Arc<PrecacheOuput>>,
is_srgb: bool,
}
#[derive(Debug, Default)]
#[allow(clippy::upper_case_acronyms)]
pub(crate) struct lutmABType {
pub num_in_channels: u8,
pub num_out_channels: u8,
// 16 is the upperbound, actual is 0..num_in_channels.
pub num_grid_points: [u8; 16],
pub e00: s15Fixed16Number,
pub e01: s15Fixed16Number,
pub e02: s15Fixed16Number,
pub e03: s15Fixed16Number,
pub e10: s15Fixed16Number,
pub e11: s15Fixed16Number,
pub e12: s15Fixed16Number,
pub e13: s15Fixed16Number,
pub e20: s15Fixed16Number,
pub e21: s15Fixed16Number,
pub e22: s15Fixed16Number,
pub e23: s15Fixed16Number,
// reversed elements (for mBA)
pub reversed: bool,
pub clut_table: Option<Vec<f32>>,
pub a_curves: [Option<Box<curveType>>; MAX_CHANNELS],
pub b_curves: [Option<Box<curveType>>; MAX_CHANNELS],
pub m_curves: [Option<Box<curveType>>; MAX_CHANNELS],
}
#[derive(Clone, Debug)]
pub(crate) enum curveType {
Curve(Vec<uInt16Number>), // len=0 => Linear, len=1 => Gamma(v[0]), _ => lut
/// The ICC parametricCurveType is specified in terms of s15Fixed16Number,
/// so it's possible to use this variant to specify greater precision than
/// any raw ICC profile could
Parametric(Vec<f32>),
}
type uInt16Number = u16;
/* should lut8Type and lut16Type be different types? */
#[derive(Debug)]
pub(crate) struct lutType {
// used by lut8Type/lut16Type (mft2) only
pub num_input_channels: u8,
pub num_output_channels: u8,
pub num_clut_grid_points: u8,
pub e00: s15Fixed16Number,
pub e01: s15Fixed16Number,
pub e02: s15Fixed16Number,
pub e10: s15Fixed16Number,
pub e11: s15Fixed16Number,
pub e12: s15Fixed16Number,
pub e20: s15Fixed16Number,
pub e21: s15Fixed16Number,
pub e22: s15Fixed16Number,
pub num_input_table_entries: u16,
pub num_output_table_entries: u16,
pub input_table: Vec<f32>,
pub clut_table: Vec<f32>,
pub output_table: Vec<f32>,
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Default)]
#[allow(clippy::upper_case_acronyms)]
pub struct XYZNumber {
pub X: s15Fixed16Number,
pub Y: s15Fixed16Number,
pub Z: s15Fixed16Number,
}
/// A color in the CIE xyY color space
/* the names for the following two types are sort of ugly */
#[repr(C)]
#[derive(Copy, Clone)]
#[allow(clippy::upper_case_acronyms)]
pub struct qcms_CIE_xyY {
pub x: f64,
pub y: f64,
pub Y: f64,
}
/// A more convenient type for specifying primaries and white points where
/// luminosity is irrelevant
struct qcms_chromaticity {
x: f64,
y: f64,
}
impl qcms_chromaticity {
const D65: Self = Self {
x: 0.3127,
y: 0.3290,
};
}
impl From<qcms_chromaticity> for qcms_CIE_xyY {
fn from(qcms_chromaticity { x, y }: qcms_chromaticity) -> Self {
Self { x, y, Y: 1.0 }
}
}
/// a set of CIE_xyY values that can use to describe the primaries of a color space
#[repr(C)]
#[derive(Copy, Clone)]
#[allow(clippy::upper_case_acronyms)]
pub struct qcms_CIE_xyYTRIPLE {
pub red: qcms_CIE_xyY,
pub green: qcms_CIE_xyY,
pub blue: qcms_CIE_xyY,
}
struct Tag {
signature: u32,
offset: u32,
size: u32,
}
/* It might be worth having a unified limit on content controlled
* allocation per profile. This would remove the need for many
* of the arbitrary limits that we used */
type TagIndex = [Tag];
/* a wrapper around the memory that we are going to parse
* into a qcms_profile */
struct MemSource<'a> {
buf: &'a [u8],
valid: bool,
invalid_reason: Option<&'static str>,
}
pub type uInt8Number = u8;
#[inline]
fn uInt8Number_to_float(a: uInt8Number) -> f32 {
a as f32 / 255.0
}
#[inline]
fn uInt16Number_to_float(a: uInt16Number) -> f32 {
a as f32 / 65535.0
}
fn invalid_source(mem: &mut MemSource, reason: &'static str) {
mem.valid = false;
mem.invalid_reason = Some(reason);
}
fn read_u32(mem: &mut MemSource, offset: usize) -> u32 {
let val = mem.buf.get(offset..offset + 4);
if let Some(val) = val {
let val = val.try_into().unwrap();
u32::from_be_bytes(val)
} else {
invalid_source(mem, "Invalid offset");
0
}
}
fn read_u16(mem: &mut MemSource, offset: usize) -> u16 {
let val = mem.buf.get(offset..offset + 2);
if let Some(val) = val {
let val = val.try_into().unwrap();
u16::from_be_bytes(val)
} else {
invalid_source(mem, "Invalid offset");
0
}
}
fn read_u8(mem: &mut MemSource, offset: usize) -> u8 {
let val = mem.buf.get(offset);
if let Some(val) = val {
*val
} else {
invalid_source(mem, "Invalid offset");
0
}
}
fn read_s15Fixed16Number(mem: &mut MemSource, offset: usize) -> s15Fixed16Number {
read_u32(mem, offset) as s15Fixed16Number
}
fn read_uInt8Number(mem: &mut MemSource, offset: usize) -> uInt8Number {
read_u8(mem, offset)
}
fn read_uInt16Number(mem: &mut MemSource, offset: usize) -> uInt16Number {
read_u16(mem, offset)
}
pub fn write_u32(mem: &mut [u8], offset: usize, value: u32) {
// we use get() and expect() instead of [..] so there's only one call to panic
// instead of two
mem.get_mut(offset..offset + std::mem::size_of_val(&value))
.expect("OOB")
.copy_from_slice(&value.to_be_bytes());
}
pub fn write_u16(mem: &mut [u8], offset: usize, value: u16) {
// we use get() and expect() instead of [..] so there's only one call to panic
// intead of two
mem.get_mut(offset..offset + std::mem::size_of_val(&value))
.expect("OOB")
.copy_from_slice(&value.to_be_bytes());
}
/* An arbitrary 4MB limit on profile size */
pub(crate) const MAX_PROFILE_SIZE: usize = 1024 * 1024 * 4;
const MAX_TAG_COUNT: u32 = 1024;
fn check_CMM_type_signature(_src: &mut MemSource) {
//uint32_t CMM_type_signature = read_u32(src, 4);
//TODO: do the check?
}
fn check_profile_version(src: &mut MemSource) {
/*
uint8_t major_revision = read_u8(src, 8 + 0);
uint8_t minor_revision = read_u8(src, 8 + 1);
*/
let reserved1: u8 = read_u8(src, (8 + 2) as usize);
let reserved2: u8 = read_u8(src, (8 + 3) as usize);
/* Checking the version doesn't buy us anything
if (major_revision != 0x4) {
if (major_revision > 0x2)
invalid_source(src, "Unsupported major revision");
if (minor_revision > 0x40)
invalid_source(src, "Unsupported minor revision");
}
*/
if reserved1 != 0 || reserved2 != 0 {
invalid_source(src, "Invalid reserved bytes");
};
}
const INPUT_DEVICE_PROFILE: u32 = 0x73636e72; // 'scnr'
pub const DISPLAY_DEVICE_PROFILE: u32 = 0x6d6e7472; // 'mntr'
const OUTPUT_DEVICE_PROFILE: u32 = 0x70727472; // 'prtr'
const DEVICE_LINK_PROFILE: u32 = 0x6c696e6b; // 'link'
const COLOR_SPACE_PROFILE: u32 = 0x73706163; // 'spac'
const ABSTRACT_PROFILE: u32 = 0x61627374; // 'abst'
const NAMED_COLOR_PROFILE: u32 = 0x6e6d636c; // 'nmcl'
fn read_class_signature(profile: &mut Profile, mem: &mut MemSource) {
profile.class_type = read_u32(mem, 12);
match profile.class_type {
DISPLAY_DEVICE_PROFILE
| INPUT_DEVICE_PROFILE
| OUTPUT_DEVICE_PROFILE
| COLOR_SPACE_PROFILE => {}
_ => {
invalid_source(mem, "Invalid Profile/Device Class signature");
}
};
}
fn read_color_space(profile: &mut Profile, mem: &mut MemSource) {
profile.color_space = read_u32(mem, 16);
match profile.color_space {
RGB_SIGNATURE | GRAY_SIGNATURE => {}
#[cfg(feature = "cmyk")]
CMYK_SIGNATURE => {}
_ => {
invalid_source(mem, "Unsupported colorspace");
}
};
}
fn read_pcs(profile: &mut Profile, mem: &mut MemSource) {
profile.pcs = read_u32(mem, 20);
match profile.pcs {
XYZ_SIGNATURE | LAB_SIGNATURE => {}
_ => {
invalid_source(mem, "Unsupported pcs");
}
};
}
fn read_tag_table(_profile: &mut Profile, mem: &mut MemSource) -> Vec<Tag> {
let count = read_u32(mem, 128);
if count > MAX_TAG_COUNT {
invalid_source(mem, "max number of tags exceeded");
return Vec::new();
}
let mut index = Vec::with_capacity(count as usize);
for i in 0..count {
let tag_start = (128 + 4 + 4 * i * 3) as usize;
let offset = read_u32(mem, tag_start + 4);
if offset as usize > mem.buf.len() {
invalid_source(mem, "tag points beyond the end of the buffer");
}
index.push(Tag {
signature: read_u32(mem, tag_start),
offset,
size: read_u32(mem, tag_start + 8),
});
}
index
}
/// Checks a profile for obvious inconsistencies and returns
/// true if the profile looks bogus and should probably be
/// ignored.
#[no_mangle]
pub extern "C" fn qcms_profile_is_bogus(profile: &mut Profile) -> bool {
let mut sum: [f32; 3] = [0.; 3];
let mut target: [f32; 3] = [0.; 3];
let mut tolerance: [f32; 3] = [0.; 3];
let rX: f32;
let rY: f32;
let rZ: f32;
let gX: f32;
let gY: f32;
let gZ: f32;
let bX: f32;
let bY: f32;
let bZ: f32;
let negative: bool;
let mut i: u32;
// We currently only check the bogosity of RGB profiles
if profile.color_space != RGB_SIGNATURE {
return false;
}
if profile.A2B0.is_some()
|| profile.B2A0.is_some()
|| profile.mAB.is_some()
|| profile.mBA.is_some()
{
return false;
}
rX = s15Fixed16Number_to_float(profile.redColorant.X);
rY = s15Fixed16Number_to_float(profile.redColorant.Y);
rZ = s15Fixed16Number_to_float(profile.redColorant.Z);
gX = s15Fixed16Number_to_float(profile.greenColorant.X);
gY = s15Fixed16Number_to_float(profile.greenColorant.Y);
gZ = s15Fixed16Number_to_float(profile.greenColorant.Z);
bX = s15Fixed16Number_to_float(profile.blueColorant.X);
bY = s15Fixed16Number_to_float(profile.blueColorant.Y);
bZ = s15Fixed16Number_to_float(profile.blueColorant.Z);
// Sum the values; they should add up to something close to white
sum[0] = rX + gX + bX;
sum[1] = rY + gY + bY;
sum[2] = rZ + gZ + bZ;
// Build our target vector (see mozilla bug 460629)
target[0] = 0.96420;
target[1] = 1.00000;
target[2] = 0.82491;
// Our tolerance vector - Recommended by Chris Murphy based on
// conversion from the LAB space criterion of no more than 3 in any one
// channel. This is similar to, but slightly more tolerant than Adobe's
// criterion.
tolerance[0] = 0.02;
tolerance[1] = 0.02;
tolerance[2] = 0.04;
// Compare with our tolerance
i = 0;
while i < 3 {
if !(sum[i as usize] - tolerance[i as usize] <= target[i as usize]
&& sum[i as usize] + tolerance[i as usize] >= target[i as usize])
{
return true;
}
i += 1
}
if false {
negative = (rX < 0.)
|| (rY < 0.)
|| (rZ < 0.)
|| (gX < 0.)
|| (gY < 0.)
|| (gZ < 0.)
|| (bX < 0.)
|| (bY < 0.)
|| (bZ < 0.);
} else {
// Chromatic adaption to D50 can result in negative XYZ, but the white
// point D50 tolerance test has passed. Accept negative values herein.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=498245#c18 onwards
// for discussion about whether profile XYZ can or cannot be negative,
// per the spec. Also the https://bugzil.la/450923 user report.
// Also: https://bugzil.la/1799391 and https://bugzil.la/1792469
negative = false; // bogus
}
if negative {
return true;
}
// All Good
false
}
pub const TAG_bXYZ: u32 = 0x6258595a;
pub const TAG_gXYZ: u32 = 0x6758595a;
pub const TAG_rXYZ: u32 = 0x7258595a;
pub const TAG_rTRC: u32 = 0x72545243;
pub const TAG_bTRC: u32 = 0x62545243;
pub const TAG_gTRC: u32 = 0x67545243;
pub const TAG_kTRC: u32 = 0x6b545243;
pub const TAG_A2B0: u32 = 0x41324230;
pub const TAG_B2A0: u32 = 0x42324130;
pub const TAG_CHAD: u32 = 0x63686164;
fn find_tag(index: &TagIndex, tag_id: u32) -> Option<&Tag> {
for t in index {
if t.signature == tag_id {
return Some(t);
}
}
None
}
pub const XYZ_TYPE: u32 = 0x58595a20; // 'XYZ '
pub const CURVE_TYPE: u32 = 0x63757276; // 'curv'
pub const PARAMETRIC_CURVE_TYPE: u32 = 0x70617261; // 'para'
pub const LUT16_TYPE: u32 = 0x6d667432; // 'mft2'
pub const LUT8_TYPE: u32 = 0x6d667431; // 'mft1'
pub const LUT_MAB_TYPE: u32 = 0x6d414220; // 'mAB '
pub const LUT_MBA_TYPE: u32 = 0x6d424120; // 'mBA '
pub const CHROMATIC_TYPE: u32 = 0x73663332; // 'sf32'
fn read_tag_s15Fixed16ArrayType(src: &mut MemSource, tag: &Tag) -> Matrix {
let mut matrix: Matrix = Matrix { m: [[0.; 3]; 3] };
let offset: u32 = tag.offset;
let type_0: u32 = read_u32(src, offset as usize);
// Check mandatory type signature for s16Fixed16ArrayType
if type_0 != CHROMATIC_TYPE {
invalid_source(src, "unexpected type, expected \'sf32\'");
}
for i in 0..=8 {
matrix.m[(i / 3) as usize][(i % 3) as usize] = s15Fixed16Number_to_float(
read_s15Fixed16Number(src, (offset + 8 + (i * 4) as u32) as usize),
);
}
matrix
}
fn read_tag_XYZType(src: &mut MemSource, index: &TagIndex, tag_id: u32) -> XYZNumber {
let mut num = XYZNumber { X: 0, Y: 0, Z: 0 };
let tag = find_tag(&index, tag_id);
if let Some(tag) = tag {
let offset: u32 = tag.offset;
let type_0: u32 = read_u32(src, offset as usize);
if type_0 != XYZ_TYPE {
invalid_source(src, "unexpected type, expected XYZ");
}
num.X = read_s15Fixed16Number(src, (offset + 8) as usize);
num.Y = read_s15Fixed16Number(src, (offset + 12) as usize);
num.Z = read_s15Fixed16Number(src, (offset + 16) as usize)
} else {
invalid_source(src, "missing xyztag");
}
num
}
// Read the tag at a given offset rather then the tag_index.
// This method is used when reading mAB tags where nested curveType are
// present that are not part of the tag_index.
fn read_curveType(src: &mut MemSource, offset: u32, len: &mut u32) -> Option<Box<curveType>> {
const COUNT_TO_LENGTH: [u32; 5] = [1, 3, 4, 5, 7]; //PARAMETRIC_CURVE_TYPE
let type_0: u32 = read_u32(src, offset as usize);
let count: u32;
if type_0 != CURVE_TYPE && type_0 != PARAMETRIC_CURVE_TYPE {
invalid_source(src, "unexpected type, expected CURV or PARA");
return None;
}
if type_0 == CURVE_TYPE {
count = read_u32(src, (offset + 8) as usize);
//arbitrary
if count > 40000 {
invalid_source(src, "curve size too large");
return None;
}
let mut table = Vec::with_capacity(count as usize);
for i in 0..count {
table.push(read_u16(src, (offset + 12 + i * 2) as usize));
}
*len = 12 + count * 2;
Some(Box::new(curveType::Curve(table)))
} else {
count = read_u16(src, (offset + 8) as usize) as u32;
if count > 4 {
invalid_source(src, "parametric function type not supported.");
return None;
}
let mut params = Vec::with_capacity(count as usize);
for i in 0..COUNT_TO_LENGTH[count as usize] {
params.push(s15Fixed16Number_to_float(read_s15Fixed16Number(
src,
(offset + 12 + i * 4) as usize,
)));
}
*len = 12 + COUNT_TO_LENGTH[count as usize] * 4;
if count == 1 || count == 2 {
/* we have a type 1 or type 2 function that has a division by 'a' */
let a: f32 = params[1];
if a == 0.0 {
invalid_source(src, "parametricCurve definition causes division by zero");
}
}
Some(Box::new(curveType::Parametric(params)))
}
}
fn read_tag_curveType(
src: &mut MemSource,
index: &TagIndex,
tag_id: u32,
) -> Option<Box<curveType>> {
let tag = find_tag(index, tag_id);
if let Some(tag) = tag {
let mut len: u32 = 0;
return read_curveType(src, tag.offset, &mut len);
} else {
invalid_source(src, "missing curvetag");
}
None
}
const MAX_LUT_SIZE: u32 = 500000; // arbitrary
const MAX_CHANNELS: usize = 10; // arbitrary
fn read_nested_curveType(
src: &mut MemSource,
curveArray: &mut [Option<Box<curveType>>; MAX_CHANNELS],
num_channels: u8,
curve_offset: u32,
) {
let mut channel_offset: u32 = 0;
#[allow(clippy::needless_range_loop)]
for i in 0..usize::from(num_channels) {
let mut tag_len: u32 = 0;
curveArray[i] = read_curveType(src, curve_offset + channel_offset, &mut tag_len);
if curveArray[i].is_none() {
invalid_source(src, "invalid nested curveType curve");
break;
} else {
channel_offset += tag_len;
// 4 byte aligned
if tag_len % 4 != 0 {
channel_offset += 4 - tag_len % 4
}
}
}
}
/* See section 10.10 for specs */
fn read_tag_lutmABType(src: &mut MemSource, tag: &Tag) -> Option<Box<lutmABType>> {
let offset: u32 = tag.offset;
let mut clut_size: u32 = 1;
let type_0: u32 = read_u32(src, offset as usize);
if type_0 != LUT_MAB_TYPE && type_0 != LUT_MBA_TYPE {
return None;
}
let num_in_channels = read_u8(src, (offset + 8) as usize);
let num_out_channels = read_u8(src, (offset + 9) as usize);
if num_in_channels > 10 || num_out_channels > 10 {
return None;
}
// We require 3in/out channels since we only support RGB->XYZ (or RGB->LAB)
// XXX: If we remove this restriction make sure that the number of channels
// is less or equal to the maximum number of mAB curves in qcmsint.h
// also check for clut_size overflow. Also make sure it's != 0
if num_in_channels != 3 || num_out_channels != 3 {
return None;
}
// some of this data is optional and is denoted by a zero offset
// we also use this to track their existance
let mut a_curve_offset = read_u32(src, (offset + 28) as usize);
let mut clut_offset = read_u32(src, (offset + 24) as usize);
let mut m_curve_offset = read_u32(src, (offset + 20) as usize);
let mut matrix_offset = read_u32(src, (offset + 16) as usize);
let mut b_curve_offset = read_u32(src, (offset + 12) as usize);
// Convert offsets relative to the tag to relative to the profile
// preserve zero for optional fields
if a_curve_offset != 0 {
a_curve_offset += offset
}
if clut_offset != 0 {
clut_offset += offset
}
if m_curve_offset != 0 {
m_curve_offset += offset
}
if matrix_offset != 0 {
matrix_offset += offset
}
if b_curve_offset != 0 {
b_curve_offset += offset
}
if clut_offset != 0 {
debug_assert!(num_in_channels == 3);
// clut_size can not overflow since lg(256^num_in_channels) = 24 bits.
for i in 0..u32::from(num_in_channels) {
clut_size *= read_u8(src, (clut_offset + i) as usize) as u32;
if clut_size == 0 {
invalid_source(src, "bad clut_size");
}
}
} else {
clut_size = 0
}
// 24bits * 3 won't overflow either
clut_size *= num_out_channels as u32;
if clut_size > MAX_LUT_SIZE {
return None;
}
let mut lut = Box::new(lutmABType::default());
if clut_offset != 0 {
for i in 0..usize::from(num_in_channels) {
lut.num_grid_points[i] = read_u8(src, clut_offset as usize + i);
if lut.num_grid_points[i] == 0 {
invalid_source(src, "bad grid_points");
}
}
}
// Reverse the processing of transformation elements for mBA type.
lut.reversed = type_0 == LUT_MBA_TYPE;
lut.num_in_channels = num_in_channels;
lut.num_out_channels = num_out_channels;
#[allow(clippy::identity_op, clippy::erasing_op)]
if matrix_offset != 0 {
// read the matrix if we have it
lut.e00 = read_s15Fixed16Number(src, (matrix_offset + (4 * 0) as u32) as usize); // the caller checks that this doesn't happen
lut.e01 = read_s15Fixed16Number(src, (matrix_offset + (4 * 1) as u32) as usize);
lut.e02 = read_s15Fixed16Number(src, (matrix_offset + (4 * 2) as u32) as usize);
lut.e10 = read_s15Fixed16Number(src, (matrix_offset + (4 * 3) as u32) as usize);
lut.e11 = read_s15Fixed16Number(src, (matrix_offset + (4 * 4) as u32) as usize);
lut.e12 = read_s15Fixed16Number(src, (matrix_offset + (4 * 5) as u32) as usize);
lut.e20 = read_s15Fixed16Number(src, (matrix_offset + (4 * 6) as u32) as usize);
lut.e21 = read_s15Fixed16Number(src, (matrix_offset + (4 * 7) as u32) as usize);
lut.e22 = read_s15Fixed16Number(src, (matrix_offset + (4 * 8) as u32) as usize);
lut.e03 = read_s15Fixed16Number(src, (matrix_offset + (4 * 9) as u32) as usize);
lut.e13 = read_s15Fixed16Number(src, (matrix_offset + (4 * 10) as u32) as usize);
lut.e23 = read_s15Fixed16Number(src, (matrix_offset + (4 * 11) as u32) as usize)
}
if a_curve_offset != 0 {
read_nested_curveType(src, &mut lut.a_curves, num_in_channels, a_curve_offset);
}
if m_curve_offset != 0 {
read_nested_curveType(src, &mut lut.m_curves, num_out_channels, m_curve_offset);
}
if b_curve_offset != 0 {
read_nested_curveType(src, &mut lut.b_curves, num_out_channels, b_curve_offset);
} else {
invalid_source(src, "B curves required");
}
if clut_offset != 0 {
let clut_precision = read_u8(src, (clut_offset + 16) as usize);
let mut clut_table = Vec::with_capacity(clut_size as usize);
if clut_precision == 1 {
for i in 0..clut_size {
clut_table.push(uInt8Number_to_float(read_uInt8Number(
src,
(clut_offset + 20 + i) as usize,
)));
}
lut.clut_table = Some(clut_table);
} else if clut_precision == 2 {
for i in 0..clut_size {
clut_table.push(uInt16Number_to_float(read_uInt16Number(
src,
(clut_offset + 20 + i * 2) as usize,
)));
}
lut.clut_table = Some(clut_table);
} else {
invalid_source(src, "Invalid clut precision");
}
}
if !src.valid {
return None;
}
Some(lut)
}
fn read_tag_lutType(src: &mut MemSource, tag: &Tag) -> Option<Box<lutType>> {
let offset: u32 = tag.offset;
let type_0: u32 = read_u32(src, offset as usize);
let num_input_table_entries: u16;
let num_output_table_entries: u16;
let input_offset: u32;
let entry_size: usize;
if type_0 == LUT8_TYPE {
num_input_table_entries = 256u16;
num_output_table_entries = 256u16;
entry_size = 1;
input_offset = 48
} else if type_0 == LUT16_TYPE {
num_input_table_entries = read_u16(src, (offset + 48) as usize);
num_output_table_entries = read_u16(src, (offset + 50) as usize);
// these limits come from the spec
if !(2..=4096).contains(&num_input_table_entries)
|| !(2..=4096).contains(&num_output_table_entries)
{
invalid_source(src, "Bad channel count");
return None;
}
entry_size = 2;
input_offset = 52
} else {
debug_assert!(false);
invalid_source(src, "Unexpected lut type");
return None;
}
let in_chan = read_u8(src, (offset + 8) as usize);
let out_chan = read_u8(src, (offset + 9) as usize);
if !(in_chan == 3 || in_chan == 4) || out_chan != 3 {
invalid_source(src, "CLUT only supports RGB and CMYK");
return None;
}
let grid_points = read_u8(src, (offset + 10) as usize);
let clut_size = match (grid_points as u32).checked_pow(in_chan as u32) {
Some(clut_size) => clut_size,
_ => {
invalid_source(src, "CLUT size overflow");
return None;
}
};
match clut_size {
1..=MAX_LUT_SIZE => {} // OK
0 => {
invalid_source(src, "CLUT must not be empty.");
return None;
}
_ => {
invalid_source(src, "CLUT too large");
return None;
}
}
let e00 = read_s15Fixed16Number(src, (offset + 12) as usize);
let e01 = read_s15Fixed16Number(src, (offset + 16) as usize);
let e02 = read_s15Fixed16Number(src, (offset + 20) as usize);
let e10 = read_s15Fixed16Number(src, (offset + 24) as usize);
let e11 = read_s15Fixed16Number(src, (offset + 28) as usize);
let e12 = read_s15Fixed16Number(src, (offset + 32) as usize);
let e20 = read_s15Fixed16Number(src, (offset + 36) as usize);
let e21 = read_s15Fixed16Number(src, (offset + 40) as usize);
let e22 = read_s15Fixed16Number(src, (offset + 44) as usize);
let mut input_table = Vec::with_capacity((num_input_table_entries * in_chan as u16) as usize);
for i in 0..(num_input_table_entries * in_chan as u16) {
if type_0 == LUT8_TYPE {
input_table.push(uInt8Number_to_float(read_uInt8Number(
src,
(offset + input_offset) as usize + i as usize * entry_size,
)))
} else {
input_table.push(uInt16Number_to_float(read_uInt16Number(
src,
(offset + input_offset) as usize + i as usize * entry_size,
)))
}
}
let clut_offset = ((offset + input_offset) as usize
+ (num_input_table_entries as i32 * in_chan as i32) as usize * entry_size)
as u32;
let mut clut_table = Vec::with_capacity((clut_size * out_chan as u32) as usize);
for i in 0..clut_size * out_chan as u32 {
if type_0 == LUT8_TYPE {
clut_table.push(uInt8Number_to_float(read_uInt8Number(
src,
clut_offset as usize + i as usize * entry_size,
)));
} else if type_0 == LUT16_TYPE {
clut_table.push(uInt16Number_to_float(read_uInt16Number(
src,
clut_offset as usize + i as usize * entry_size,
)));
}
}
let output_offset =
(clut_offset as usize + (clut_size * out_chan as u32) as usize * entry_size) as u32;
let mut output_table =
Vec::with_capacity((num_output_table_entries * out_chan as u16) as usize);
for i in 0..num_output_table_entries as i32 * out_chan as i32 {
if type_0 == LUT8_TYPE {
output_table.push(uInt8Number_to_float(read_uInt8Number(
src,
output_offset as usize + i as usize * entry_size,
)))
} else {
output_table.push(uInt16Number_to_float(read_uInt16Number(
src,
output_offset as usize + i as usize * entry_size,
)))
}
}
Some(Box::new(lutType {
num_input_table_entries,
num_output_table_entries,
num_input_channels: in_chan,
num_output_channels: out_chan,
num_clut_grid_points: grid_points,
e00,
e01,
e02,
e10,
e11,
e12,
e20,
e21,
e22,
input_table,
clut_table,
output_table,
}))
}
fn read_rendering_intent(profile: &mut Profile, src: &mut MemSource) {
let intent = read_u32(src, 64);
profile.rendering_intent = match intent {
x if x == Perceptual as u32 => Perceptual,
x if x == RelativeColorimetric as u32 => RelativeColorimetric,
x if x == Saturation as u32 => Saturation,
x if x == AbsoluteColorimetric as u32 => AbsoluteColorimetric,
_ => {
invalid_source(src, "unknown rendering intent");
Intent::default()
}
};
}
fn profile_create() -> Box<Profile> {
Box::new(Profile::default())
}
/* build sRGB gamma table */
/* based on cmsBuildParametricGamma() */
#[allow(clippy::many_single_char_names)]
fn build_sRGB_gamma_table(num_entries: i32) -> Vec<u16> {
/* taken from lcms: Build_sRGBGamma() */
let gamma: f64 = 2.4;
let a: f64 = 1.0 / 1.055;
let b: f64 = 0.055 / 1.055;
let c: f64 = 1.0 / 12.92;
let d: f64 = 0.04045;
build_trc_table(
num_entries,
// IEC 61966-2.1 (sRGB)
// Y = (aX + b)^Gamma | X >= d
// Y = cX | X < d
|x| {
if x >= d {
let e: f64 = a * x + b;
if e > 0. {
e.powf(gamma)
} else {
0.
}
} else {
c * x
}
},
)
}
/// eotf: electro-optical transfer characteristic function, maps from [0, 1]
/// in non-linear (voltage) space to [0, 1] in linear (optical) space. Should
/// generally be a concave up function.
fn build_trc_table(num_entries: i32, eotf: impl Fn(f64) -> f64) -> Vec<u16> {
let mut table = Vec::with_capacity(num_entries as usize);
for i in 0..num_entries {
let x: f64 = i as f64 / (num_entries - 1) as f64;
let y: f64 = eotf(x);
let mut output: f64;
// Saturate -- this could likely move to a separate function
output = y * 65535.0 + 0.5;
if output > 65535.0 {
output = 65535.0
}
if output < 0.0 {
output = 0.0
}
table.push(output.floor() as u16);
}
table
}
fn curve_from_table(table: &[u16]) -> Box<curveType> {
Box::new(curveType::Curve(table.to_vec()))
}
pub fn float_to_u8Fixed8Number(a: f32) -> u16 {
if a > 255.0 + 255.0 / 256f32 {
0xffffu16
} else if a < 0.0 {
0u16
} else {
(a * 256.0 + 0.5).floor() as u16
}
}
fn curve_from_gamma(gamma: f32) -> Box<curveType> {
Box::new(curveType::Curve(vec![float_to_u8Fixed8Number(gamma)]))
}
fn identity_curve() -> Box<curveType> {
Box::new(curveType::Curve(Vec::new()))
}
/* from lcms: cmsWhitePointFromTemp */
/* tempK must be >= 4000. and <= 25000.
* Invalid values of tempK will return
* (x,y,Y) = (-1.0, -1.0, -1.0)
* similar to argyll: icx_DTEMP2XYZ() */
fn white_point_from_temp(temp_K: i32) -> qcms_CIE_xyY {
let mut white_point: qcms_CIE_xyY = qcms_CIE_xyY {
x: 0.,
y: 0.,
Y: 0.,
};
// No optimization provided.
let T = temp_K as f64; // Square
let T2 = T * T; // Cube
let T3 = T2 * T;
// For correlated color temperature (T) between 4000K and 7000K:
let x = if (4000.0..=7000.0).contains(&T) {
-4.6070 * (1E9 / T3) + 2.9678 * (1E6 / T2) + 0.09911 * (1E3 / T) + 0.244063
} else if T > 7000.0 && T <= 25000.0 {
-2.0064 * (1E9 / T3) + 1.9018 * (1E6 / T2) + 0.24748 * (1E3 / T) + 0.237040
} else {
// or for correlated color temperature (T) between 7000K and 25000K:
// Invalid tempK
white_point.x = -1.0;
white_point.y = -1.0;
white_point.Y = -1.0;
debug_assert!(false, "invalid temp");
return white_point;
};
// Obtain y(x)
let y = -3.000 * (x * x) + 2.870 * x - 0.275;
// wave factors (not used, but here for futures extensions)
// let M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
// let M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
// Fill white_point struct
white_point.x = x;
white_point.y = y;
white_point.Y = 1.0;
white_point
}
#[no_mangle]
pub extern "C" fn qcms_white_point_sRGB() -> qcms_CIE_xyY {
white_point_from_temp(6504)
}
/// See [Rec. ITU-T H.273 (12/2016)](https://www.itu.int/rec/T-REC-H.273-201612-I/en) Table 2
/// Values 0, 3, 13–21, 23–255 are all reserved so all map to the same variant
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ColourPrimaries {
/// For future use by ITU-T | ISO/IEC
Reserved,
/// Rec. ITU-R BT.709-6<br />
/// Rec. ITU-R BT.1361-0 conventional colour gamut system and extended colour gamut system (historical)<br />
/// IEC 61966-2-1 sRGB or sYCC IEC 61966-2-4<br />
/// Society of Motion Picture and Television Engineers (MPTE) RP 177 (1993) Annex B<br />
Bt709 = 1,
/// Unspecified<br />
/// Image characteristics are unknown or are determined by the application.
Unspecified = 2,
/// Rec. ITU-R BT.470-6 System M (historical)<br />
/// United States National Television System Committee 1953 Recommendation for transmission standards for color television<br />
/// United States Federal Communications Commission (2003) Title 47 Code of Federal Regulations 73.682 (a) (20)<br />
Bt470M = 4,
/// Rec. ITU-R BT.470-6 System B, G (historical) Rec. ITU-R BT.601-7 625<br />
/// Rec. ITU-R BT.1358-0 625 (historical)<br />
/// Rec. ITU-R BT.1700-0 625 PAL and 625 SECAM<br />
Bt470Bg = 5,
/// Rec. ITU-R BT.601-7 525<br />
/// Rec. ITU-R BT.1358-1 525 or 625 (historical) Rec. ITU-R BT.1700-0 NTSC<br />
/// SMPTE 170M (2004)<br />
/// (functionally the same as the value 7)<br />
Bt601 = 6,
/// SMPTE 240M (1999) (historical) (functionally the same as the value 6)<br />
Smpte240 = 7,
/// Generic film (colour filters using Illuminant C)<br />
Generic_film = 8,
/// Rec. ITU-R BT.2020-2<br />
/// Rec. ITU-R BT.2100-0<br />
Bt2020 = 9,
/// SMPTE ST 428-1<br />
/// (CIE 1931 XYZ as in ISO 11664-1)<br />
Xyz = 10,
/// SMPTE RP 431-2 (2011)<br />
Smpte431 = 11,
/// SMPTE EG 432-1 (2010)<br />
Smpte432 = 12,
/// EBU Tech. 3213-E (1975)<br />
Ebu3213 = 22,
}
impl From<u8> for ColourPrimaries {
fn from(value: u8) -> Self {
match value {
0 | 3 | 13..=21 | 23..=255 => Self::Reserved,
1 => Self::Bt709,
2 => Self::Unspecified,
4 => Self::Bt470M,
5 => Self::Bt470Bg,
6 => Self::Bt601,
7 => Self::Smpte240,
8 => Self::Generic_film,
9 => Self::Bt2020,
10 => Self::Xyz,
11 => Self::Smpte431,
12 => Self::Smpte432,
22 => Self::Ebu3213,
}
}
}
#[test]
fn colour_primaries() {
for value in 0..=u8::MAX {
match ColourPrimaries::from(value) {
ColourPrimaries::Reserved => {}
variant => assert_eq!(value, variant as u8),
}
}
}
impl From<ColourPrimaries> for qcms_CIE_xyYTRIPLE {
fn from(value: ColourPrimaries) -> Self {
let red;
let green;
let blue;
match value {
ColourPrimaries::Reserved => panic!("CP={} is reserved", value as u8),
ColourPrimaries::Bt709 => {
green = qcms_chromaticity { x: 0.300, y: 0.600 };
blue = qcms_chromaticity { x: 0.150, y: 0.060 };
red = qcms_chromaticity { x: 0.640, y: 0.330 };
}
ColourPrimaries::Unspecified => panic!("CP={} is unspecified", value as u8),
ColourPrimaries::Bt470M => {
green = qcms_chromaticity { x: 0.21, y: 0.71 };
blue = qcms_chromaticity { x: 0.14, y: 0.08 };
red = qcms_chromaticity { x: 0.67, y: 0.33 };
}
ColourPrimaries::Bt470Bg => {
green = qcms_chromaticity { x: 0.29, y: 0.60 };
blue = qcms_chromaticity { x: 0.15, y: 0.06 };
red = qcms_chromaticity { x: 0.64, y: 0.33 };
}
ColourPrimaries::Bt601 | ColourPrimaries::Smpte240 => {
green = qcms_chromaticity { x: 0.310, y: 0.595 };
blue = qcms_chromaticity { x: 0.155, y: 0.070 };
red = qcms_chromaticity { x: 0.630, y: 0.340 };
}
ColourPrimaries::Generic_film => {
green = qcms_chromaticity { x: 0.243, y: 0.692 };
blue = qcms_chromaticity { x: 0.145, y: 0.049 };
red = qcms_chromaticity { x: 0.681, y: 0.319 };
}
ColourPrimaries::Bt2020 => {
green = qcms_chromaticity { x: 0.170, y: 0.797 };
blue = qcms_chromaticity { x: 0.131, y: 0.046 };
red = qcms_chromaticity { x: 0.708, y: 0.292 };
}
ColourPrimaries::Xyz => {
green = qcms_chromaticity { x: 0.0, y: 1.0 };
blue = qcms_chromaticity { x: 0.0, y: 0.0 };
red = qcms_chromaticity { x: 1.0, y: 0.0 };
}
// These two share primaries, but have distinct white points
ColourPrimaries::Smpte431 | ColourPrimaries::Smpte432 => {
green = qcms_chromaticity { x: 0.265, y: 0.690 };
blue = qcms_chromaticity { x: 0.150, y: 0.060 };
red = qcms_chromaticity { x: 0.680, y: 0.320 };
}
ColourPrimaries::Ebu3213 => {
green = qcms_chromaticity { x: 0.295, y: 0.605 };
blue = qcms_chromaticity { x: 0.155, y: 0.077 };
red = qcms_chromaticity { x: 0.630, y: 0.340 };
}
}
Self {
red: red.into(),
green: green.into(),
blue: blue.into(),
}
}
}
impl ColourPrimaries {
fn white_point(self) -> qcms_CIE_xyY {
match self {
Self::Reserved => panic!("CP={} is reserved", self as u8),
Self::Bt709
| Self::Bt470Bg
| Self::Bt601
| Self::Smpte240
| Self::Bt2020
| Self::Smpte432
| Self::Ebu3213 => qcms_chromaticity::D65,
Self::Unspecified => panic!("CP={} is unspecified", self as u8),
Self::Bt470M => qcms_chromaticity { x: 0.310, y: 0.316 },
Self::Generic_film => qcms_chromaticity { x: 0.310, y: 0.316 },
Self::Xyz => qcms_chromaticity {
x: 1. / 3.,
y: 1. / 3.,
},
Self::Smpte431 => qcms_chromaticity { x: 0.314, y: 0.351 },
}
.into()
}
}
/// See [Rec. ITU-T H.273 (12/2016)](https://www.itu.int/rec/T-REC-H.273-201612-I/en) Table 3
/// Values 0, 3, 19–255 are all reserved so all map to the same variant
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TransferCharacteristics {
/// For future use by ITU-T | ISO/IEC
Reserved,
/// Rec. ITU-R BT.709-6<br />
/// Rec. ITU-R BT.1361-0 conventional colour gamut system (historical)<br />
/// (functionally the same as the values 6, 14 and 15) <br />
Bt709 = 1,
/// Image characteristics are unknown or are determined by the application.<br />
Unspecified = 2,
/// Rec. ITU-R BT.470-6 System M (historical)<br />
/// United States National Television System Committee 1953 Recommendation for transmission standards for color television<br />
/// United States Federal Communications Commission (2003) Title 47 Code of Federal Regulations 73.682 (a) (20)<br />
/// Rec. ITU-R BT.1700-0 625 PAL and 625 SECAM<br />
Bt470M = 4,
/// Rec. ITU-R BT.470-6 System B, G (historical)<br />
Bt470Bg = 5,
/// Rec. ITU-R BT.601-7 525 or 625<br />
/// Rec. ITU-R BT.1358-1 525 or 625 (historical)<br />
/// Rec. ITU-R BT.1700-0 NTSC SMPTE 170M (2004)<br />
/// (functionally the same as the values 1, 14 and 15)<br />
Bt601 = 6,
/// SMPTE 240M (1999) (historical)<br />
Smpte240 = 7,
/// Linear transfer characteristics<br />
Linear = 8,
/// Logarithmic transfer characteristic (100:1 range)<br />
Log_100 = 9,
/// Logarithmic transfer characteristic (100 * Sqrt( 10 ) : 1 range)<br />
Log_100_sqrt10 = 10,
/// IEC 61966-2-4<br />
Iec61966 = 11,
/// Rec. ITU-R BT.1361-0 extended colour gamut system (historical)<br />
Bt_1361 = 12,
/// IEC 61966-2-1 sRGB or sYCC<br />
Srgb = 13,
/// Rec. ITU-R BT.2020-2 (10-bit system)<br />
/// (functionally the same as the values 1, 6 and 15)<br />
Bt2020_10bit = 14,
/// Rec. ITU-R BT.2020-2 (12-bit system)<br />
/// (functionally the same as the values 1, 6 and 14)<br />
Bt2020_12bit = 15,
/// SMPTE ST 2084 for 10-, 12-, 14- and 16-bitsystems<br />
/// Rec. ITU-R BT.2100-0 perceptual quantization (PQ) system<br />
Smpte2084 = 16,
/// SMPTE ST 428-1<br />
Smpte428 = 17,
/// ARIB STD-B67<br />
/// Rec. ITU-R BT.2100-0 hybrid log- gamma (HLG) system<br />
Hlg = 18,
}
#[test]
fn transfer_characteristics() {
for value in 0..=u8::MAX {
match TransferCharacteristics::from(value) {
TransferCharacteristics::Reserved => {}
variant => assert_eq!(value, variant as u8),
}
}
}
impl From<u8> for TransferCharacteristics {
fn from(value: u8) -> Self {
match value {
0 | 3 | 19..=255 => Self::Reserved,
1 => Self::Bt709,
2 => Self::Unspecified,
4 => Self::Bt470M,
5 => Self::Bt470Bg,
6 => Self::Bt601,
7 => Self::Smpte240, // unimplemented
8 => Self::Linear,
9 => Self::Log_100,
10 => Self::Log_100_sqrt10,
11 => Self::Iec61966, // unimplemented
12 => Self::Bt_1361, // unimplemented
13 => Self::Srgb,
14 => Self::Bt2020_10bit,
15 => Self::Bt2020_12bit,
16 => Self::Smpte2084,
17 => Self::Smpte428, // unimplemented
18 => Self::Hlg,
}
}
}
impl TryFrom<TransferCharacteristics> for curveType {
type Error = ();
/// See [ICC.1:2010](https://www.color.org/specification/ICC1v43_2010-12.pdf)
/// See [Rec. ITU-R BT.2100-2](https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.2100-2-201807-I!!PDF-E.pdf)
fn try_from(value: TransferCharacteristics) -> Result<Self, Self::Error> {
const NUM_TRC_TABLE_ENTRIES: i32 = 1024;
Ok(match value {
TransferCharacteristics::Reserved => panic!("TC={} is reserved", value as u8),
TransferCharacteristics::Bt709
| TransferCharacteristics::Bt601
| TransferCharacteristics::Bt2020_10bit
| TransferCharacteristics::Bt2020_12bit => {
// The opto-electronic transfer characteristic function (OETF)
// as defined in ITU-T H.273 table 3, row 1:
//
// V = (α * Lc^0.45) − (α − 1) for 1 >= Lc >= β
// V = 4.500 * Lc for β > Lc >= 0
//
// Inverting gives the electro-optical transfer characteristic
// function (EOTF) which can be represented as ICC
// parametricCurveType with 4 parameters (ICC.1:2010 Table 5).
// Converting between the two (Lc ↔︎ Y, V ↔︎ X):
//
// Y = (a * X + b)^g for (X >= d)
// Y = c * X for (X < d)
//
// g, a, b, c, d can then be defined in terms of α and β:
//
// g = 1 / 0.45
// a = 1 / α
// b = 1 - α
// c = 1 / 4.500
// d = 4.500 * β
//
// α and β are determined by solving the piecewise equations to
// ensure continuity of both value and slope at the value β.
// We use the values specified for 10-bit systems in
// https://www.itu.int/rec/R-REC-BT.2020-2-201510-I Table 4
// since this results in the similar values as available ICC
// profiles after converting to s15Fixed16Number, providing us
// good test coverage.
type Float = f32;
const alpha: Float = 1.099;
const beta: Float = 0.018;
const linear_coef: Float = 4.500;
const pow_exp: Float = 0.45;
const g: Float = 1. / pow_exp;
const a: Float = 1. / alpha;
const b: Float = 1. - a;
const c: Float = 1. / linear_coef;
const d: Float = linear_coef * beta;
curveType::Parametric(vec![g, a, b, c, d])
}
TransferCharacteristics::Unspecified => panic!("TC={} is unspecified", value as u8),
TransferCharacteristics::Bt470M => *curve_from_gamma(2.2),
TransferCharacteristics::Bt470Bg => *curve_from_gamma(2.8),
TransferCharacteristics::Smpte240 => return Err(()),
TransferCharacteristics::Linear => *curve_from_gamma(1.),
TransferCharacteristics::Log_100 => {
// See log_100_transfer_characteristics() for derivation
// The opto-electronic transfer characteristic function (OETF)
// as defined in ITU-T H.273 table 3, row 9:
//
// V = 1.0 + Log10(Lc) ÷ 2 for 1 >= Lc >= 0.01
// V = 0.0 for 0.01 > Lc >= 0
//
// Inverting this to give the EOTF required for the profile gives
//
// Lc = 10^(2*V - 2) for 1 >= V >= 0
let table = build_trc_table(NUM_TRC_TABLE_ENTRIES, |v| 10f64.powf(2. * v - 2.));
curveType::Curve(table)
}
TransferCharacteristics::Log_100_sqrt10 => {
// The opto-electronic transfer characteristic function (OETF)
// as defined in ITU-T H.273 table 3, row 10:
//
// V = 1.0 + Log10(Lc) ÷ 2.5 for 1 >= Lc >= Sqrt(10) ÷ 1000
// V = 0.0 for Sqrt(10) ÷ 1000 > Lc >= 0
//
// Inverting this to give the EOTF required for the profile gives
//
// Lc = 10^(2.5*V - 2.5) for 1 >= V >= 0
let table = build_trc_table(NUM_TRC_TABLE_ENTRIES, |v| 10f64.powf(2.5 * v - 2.5));
curveType::Curve(table)
}
TransferCharacteristics::Iec61966 => return Err(()),
TransferCharacteristics::Bt_1361 => return Err(()),
TransferCharacteristics::Srgb => {
// Should we prefer this or curveType::Parametric?
curveType::Curve(build_sRGB_gamma_table(NUM_TRC_TABLE_ENTRIES))
}
TransferCharacteristics::Smpte2084 => {
// Despite using Lo rather than Lc, H.273 gives the OETF:
//
// V = ( ( c1 + c2 * (Lo)^n ) ÷ ( 1 + c3 * (Lo)^n ) )^m
const c1: f64 = 0.8359375;
const c2: f64 = 18.8515625;
const c3: f64 = 18.6875;
const m: f64 = 78.84375;
const n: f64 = 0.1593017578125;
// Inverting this to give the EOTF required for the profile
// (and confirmed by Rec. ITU-R BT.2100-2, Table 4) gives
//
// Y = ( max[( X^(1/m) - c1 ), 0] ÷ ( c2 - c3 * X^(1/m) ) )^(1/n)
let table = build_trc_table(NUM_TRC_TABLE_ENTRIES, |x| {
((x.powf(1. / m) - c1).max(0.) / (c2 - c3 * x.powf(1. / m))).powf(1. / n)
});
curveType::Curve(table)
}
TransferCharacteristics::Smpte428 => return Err(()),
TransferCharacteristics::Hlg => {
// The opto-electronic transfer characteristic function (OETF)
// as defined in ITU-T H.273 table 3, row 18:
//
// V = a * Ln(12 * Lc - b) + c for 1 >= Lc > 1 ÷ 12
// V = Sqrt(3) * Lc^0.5 for 1 ÷ 12 >= Lc >= 0
const a: f64 = 0.17883277;
const b: f64 = 0.28466892;
const c: f64 = 0.55991073;
// Inverting this to give the EOTF required for the profile
// (and confirmed by Rec. ITU-R BT.2100-2, Table 4) gives
//
// Y = (X^2) / 3 for 0 <= X <= 0.5
// Y = ((e^((X-c)/a))+b)/12 for 0.5 < X <= 1
let table = build_trc_table(NUM_TRC_TABLE_ENTRIES, |x| {
if x <= 0.5 {
let y1 = x.powf(2.) / 3.;
assert!((0. ..=1. / 12.).contains(&y1));
y1
} else {
(std::f64::consts::E.powf((x - c) / a) + b) / 12.
}
});
curveType::Curve(table)
}
})
}
}
#[cfg(test)]
fn check_transfer_characteristics(cicp: TransferCharacteristics, icc_path: &str) {
let mut cicp_out = [0u8; crate::transform::PRECACHE_OUTPUT_SIZE];
let mut icc_out = [0u8; crate::transform::PRECACHE_OUTPUT_SIZE];
let cicp_tc = curveType::try_from(cicp).unwrap();
let icc = Profile::new_from_path(icc_path).unwrap();
let icc_tc = icc.redTRC.as_ref().unwrap();
eprintln!("cicp_tc: {:?}", cicp_tc);
eprintln!("icc_tc: {:?}", icc_tc);
crate::transform_util::compute_precache(icc_tc, &mut icc_out);
crate::transform_util::compute_precache(&cicp_tc, &mut cicp_out);
let mut off_by_one = 0;
for i in 0..cicp_out.len() {
match (cicp_out[i] as i16) - (icc_out[i] as i16) {
0 => {}
1 | -1 => {
off_by_one += 1;
}
_ => assert_eq!(cicp_out[i], icc_out[i], "difference at index {}", i),
}
}
eprintln!("{} / {} off by one", off_by_one, cicp_out.len());
}
#[test]
fn srgb_transfer_characteristics() {
check_transfer_characteristics(TransferCharacteristics::Srgb, "sRGB_lcms.icc");
}
#[test]
fn bt709_transfer_characteristics() {
check_transfer_characteristics(TransferCharacteristics::Bt709, "ITU-709.icc");
}
#[test]
fn bt2020_10bit_transfer_characteristics() {
check_transfer_characteristics(TransferCharacteristics::Bt2020_10bit, "ITU-2020.icc");
}
#[test]
fn bt2020_12bit_transfer_characteristics() {
check_transfer_characteristics(TransferCharacteristics::Bt2020_12bit, "ITU-2020.icc");
}
impl Profile {
//XXX: it would be nice if we had a way of ensuring
// everything in a profile was initialized regardless of how it was created
//XXX: should this also be taking a black_point?
/* similar to CGColorSpaceCreateCalibratedRGB */
pub fn new_rgb_with_table(
white_point: qcms_CIE_xyY,
primaries: qcms_CIE_xyYTRIPLE,
table: &[u16],
) -> Option<Box<Profile>> {
let mut profile = profile_create();
//XXX: should store the whitepoint
if !set_rgb_colorants(&mut profile, white_point, primaries) {
return None;
}
profile.redTRC = Some(curve_from_table(table));
profile.blueTRC = Some(curve_from_table(table));
profile.greenTRC = Some(curve_from_table(table));
profile.class_type = DISPLAY_DEVICE_PROFILE;
profile.rendering_intent = Perceptual;
profile.color_space = RGB_SIGNATURE;
profile.pcs = XYZ_TYPE;
Some(profile)
}
pub fn new_sRGB() -> Box<Profile> {
let D65 = qcms_white_point_sRGB();
let table = build_sRGB_gamma_table(1024);
let mut srgb = Profile::new_rgb_with_table(
D65,
qcms_CIE_xyYTRIPLE::from(ColourPrimaries::Bt709),
&table,
)
.unwrap();
srgb.is_srgb = true;
srgb
}
/// Returns true if this profile is sRGB
pub fn is_sRGB(&self) -> bool {
self.is_srgb
}
pub(crate) fn new_sRGB_parametric() -> Box<Profile> {
let primaries = qcms_CIE_xyYTRIPLE::from(ColourPrimaries::Bt709);
let white_point = qcms_white_point_sRGB();
let mut profile = profile_create();
set_rgb_colorants(&mut profile, white_point, primaries);
let curve = Box::new(curveType::Parametric(vec![
2.4,
1. / 1.055,
0.055 / 1.055,
1. / 12.92,
0.04045,
]));
profile.redTRC = Some(curve.clone());
profile.blueTRC = Some(curve.clone());
profile.greenTRC = Some(curve);
profile.class_type = DISPLAY_DEVICE_PROFILE;
profile.rendering_intent = Perceptual;
profile.color_space = RGB_SIGNATURE;
profile.pcs = XYZ_TYPE;
profile.is_srgb = true;
profile
}
pub(crate) fn new_displayP3() -> Box<Profile> {
let primaries = qcms_CIE_xyYTRIPLE::from(ColourPrimaries::Smpte432);
let white_point = qcms_white_point_sRGB();
let mut profile = profile_create();
set_rgb_colorants(&mut profile, white_point, primaries);
let curve = Box::new(curveType::Parametric(vec![
2.4,
1. / 1.055,
0.055 / 1.055,
1. / 12.92,
0.04045,
]));
profile.redTRC = Some(curve.clone());
profile.blueTRC = Some(curve.clone());
profile.greenTRC = Some(curve);
profile.class_type = DISPLAY_DEVICE_PROFILE;
profile.rendering_intent = Perceptual;
profile.color_space = RGB_SIGNATURE;
profile.pcs = XYZ_TYPE;
profile.is_srgb = false;
profile
}
/// Create a new profile with D50 adopted white and identity transform functions
pub fn new_XYZD50() -> Box<Profile> {
let mut profile = profile_create();
profile.redColorant.X = double_to_s15Fixed16Number(1.);
profile.redColorant.Y = double_to_s15Fixed16Number(0.);
profile.redColorant.Z = double_to_s15Fixed16Number(0.);
profile.greenColorant.X = double_to_s15Fixed16Number(0.);
profile.greenColorant.Y = double_to_s15Fixed16Number(1.);
profile.greenColorant.Z = double_to_s15Fixed16Number(0.);
profile.blueColorant.X = double_to_s15Fixed16Number(0.);
profile.blueColorant.Y = double_to_s15Fixed16Number(0.);
profile.blueColorant.Z = double_to_s15Fixed16Number(1.);
profile.redTRC = Some(identity_curve());
profile.blueTRC = Some(identity_curve());
profile.greenTRC = Some(identity_curve());
profile.class_type = DISPLAY_DEVICE_PROFILE;
profile.rendering_intent = Perceptual;
profile.color_space = RGB_SIGNATURE;
profile.pcs = XYZ_TYPE;
profile
}
pub fn new_cicp(cp: ColourPrimaries, tc: TransferCharacteristics) -> Option<Box<Profile>> {
let mut profile = profile_create();
//XXX: should store the whitepoint
if !set_rgb_colorants(&mut profile, cp.white_point(), qcms_CIE_xyYTRIPLE::from(cp)) {
return None;
}
let curve = curveType::try_from(tc).ok()?;
profile.redTRC = Some(Box::new(curve.clone()));
profile.blueTRC = Some(Box::new(curve.clone()));
profile.greenTRC = Some(Box::new(curve));
profile.class_type = DISPLAY_DEVICE_PROFILE;
profile.rendering_intent = Perceptual;
profile.color_space = RGB_SIGNATURE;
profile.pcs = XYZ_TYPE;
profile.is_srgb = (cp, tc) == (ColourPrimaries::Bt709, TransferCharacteristics::Srgb);
Some(profile)
}
pub fn new_gray_with_gamma(gamma: f32) -> Box<Profile> {
let mut profile = profile_create();
profile.grayTRC = Some(curve_from_gamma(gamma));
profile.class_type = DISPLAY_DEVICE_PROFILE;
profile.rendering_intent = Perceptual;
profile.color_space = GRAY_SIGNATURE;
profile.pcs = XYZ_TYPE;
profile
}
pub fn new_rgb_with_gamma_set(
white_point: qcms_CIE_xyY,
primaries: qcms_CIE_xyYTRIPLE,
redGamma: f32,
greenGamma: f32,
blueGamma: f32,
) -> Option<Box<Profile>> {
let mut profile = profile_create();
//XXX: should store the whitepoint
if !set_rgb_colorants(&mut profile, white_point, primaries) {
return None;
}
profile.redTRC = Some(curve_from_gamma(redGamma));
profile.blueTRC = Some(curve_from_gamma(blueGamma));
profile.greenTRC = Some(curve_from_gamma(greenGamma));
profile.class_type = DISPLAY_DEVICE_PROFILE;
profile.rendering_intent = Perceptual;
profile.color_space = RGB_SIGNATURE;
profile.pcs = XYZ_TYPE;
Some(profile)
}
pub fn new_from_path(file: &str) -> Option<Box<Profile>> {
Profile::new_from_slice(&std::fs::read(file).ok()?, false)
}
pub fn new_from_slice(mem: &[u8], curves_only: bool) -> Option<Box<Profile>> {
let length: u32;
let mut source: MemSource = MemSource {
buf: mem,
valid: false,
invalid_reason: None,
};
let index;
source.valid = true;
let src: &mut MemSource = &mut source;
if mem.len() < 4 {
return None;
}
length = read_u32(src, 0);
if length as usize <= mem.len() {
// shrink the area that we can read if appropriate
src.buf = &src.buf[0..length as usize];
} else {
return None;
}
/* ensure that the profile size is sane so it's easier to reason about */
if src.buf.len() <= 64 || src.buf.len() >= MAX_PROFILE_SIZE {
return None;
}
let mut profile = profile_create();
check_CMM_type_signature(src);
check_profile_version(src);
read_class_signature(&mut profile, src);
read_rendering_intent(&mut profile, src);
read_color_space(&mut profile, src);
read_pcs(&mut profile, src);
//TODO read rest of profile stuff
if !src.valid {
return None;
}
index = read_tag_table(&mut profile, src);
if !src.valid || index.is_empty() {
return None;
}
if let Some(chad) = find_tag(&index, TAG_CHAD) {
profile.chromaticAdaption = Some(read_tag_s15Fixed16ArrayType(src, chad))
} else {
profile.chromaticAdaption = None; //Signal the data is not present
}
if profile.class_type == DISPLAY_DEVICE_PROFILE
|| profile.class_type == INPUT_DEVICE_PROFILE
|| profile.class_type == OUTPUT_DEVICE_PROFILE
|| profile.class_type == COLOR_SPACE_PROFILE
{
if profile.color_space == RGB_SIGNATURE {
if !curves_only {
if let Some(A2B0) = find_tag(&index, TAG_A2B0) {
let lut_type = read_u32(src, A2B0.offset as usize);
if lut_type == LUT8_TYPE || lut_type == LUT16_TYPE {
profile.A2B0 = read_tag_lutType(src, A2B0)
} else if lut_type == LUT_MAB_TYPE {
profile.mAB = read_tag_lutmABType(src, A2B0)
}
}
if let Some(B2A0) = find_tag(&index, TAG_B2A0) {
let lut_type = read_u32(src, B2A0.offset as usize);
if lut_type == LUT8_TYPE || lut_type == LUT16_TYPE {
profile.B2A0 = read_tag_lutType(src, B2A0)
} else if lut_type == LUT_MBA_TYPE {
profile.mBA = read_tag_lutmABType(src, B2A0)
}
}
}
if find_tag(&index, TAG_rXYZ).is_some() || curves_only {
profile.redColorant = read_tag_XYZType(src, &index, TAG_rXYZ);
profile.greenColorant = read_tag_XYZType(src, &index, TAG_gXYZ);
profile.blueColorant = read_tag_XYZType(src, &index, TAG_bXYZ)
}
if !src.valid {
return None;
}
if find_tag(&index, TAG_rTRC).is_some() || curves_only {
profile.redTRC = read_tag_curveType(src, &index, TAG_rTRC);
profile.greenTRC = read_tag_curveType(src, &index, TAG_gTRC);
profile.blueTRC = read_tag_curveType(src, &index, TAG_bTRC);
if profile.redTRC.is_none()
|| profile.blueTRC.is_none()
|| profile.greenTRC.is_none()
{
return None;
}
}
} else if profile.color_space == GRAY_SIGNATURE {
profile.grayTRC = read_tag_curveType(src, &index, TAG_kTRC);
profile.grayTRC.as_ref()?;
} else if profile.color_space == CMYK_SIGNATURE {
if let Some(A2B0) = find_tag(&index, TAG_A2B0) {
let lut_type = read_u32(src, A2B0.offset as usize);
if lut_type == LUT8_TYPE || lut_type == LUT16_TYPE {
profile.A2B0 = read_tag_lutType(src, A2B0)
} else if lut_type == LUT_MBA_TYPE {
profile.mAB = read_tag_lutmABType(src, A2B0)
}
}
} else {
debug_assert!(false, "read_color_space protects against entering here");
return None;
}
} else {
return None;
}
if !src.valid {
return None;
}
Some(profile)
}
/// Precomputes the information needed for this profile to be
/// used as the output profile when constructing a `Transform`.
pub fn precache_output_transform(&mut self) {
crate::transform::qcms_profile_precache_output_transform(self);
}
}
|