oximedia-calibrate 0.1.8

Professional color calibration and matching tools for OxiMedia
Documentation
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
//! ICC profile parsing and representation.
//!
//! This module provides tools for parsing and representing ICC color profiles.

use crate::error::{CalibrationError, CalibrationResult};
use crate::{Illuminant, Matrix3x3};
use serde::{Deserialize, Serialize};

/// ICC profile version.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum IccProfileVersion {
    /// ICC v2.
    V2,
    /// ICC v4.
    V4,
    /// ICC v5 / iccMAX.
    V5IccMax,
}

// ── iccMAX tag type codes ──────────────────────────────────────────────────────

/// iccMAX `sfloat32Array` type signature.
const SIG_SF32: u32 = 0x7366_3332; // 'sf32'
/// iccMAX `spectralDataType` type signature.
const SIG_SPEC: u32 = 0x7370_6563; // 'spec'
/// iccMAX `multiProcessElements` type signature.
const SIG_MPE: u32 = 0x6D706566; // 'mpef'

/// iccMAX spectral tag signatures recognised by this parser.
const TAG_BRDF_SPECTRAL: u32 = 0x6272_6466; // 'brdf'

/// iccMAX float tag for custom float data.
const TAG_FLOAT_LUT: u32 = 0x666C_7574; // 'flut'

/// An iccMAX (ICC v5) tag payload.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum IccMaxTag {
    /// Spectral data from an `spec`-type tag.
    SpectralData(SpectralData),
    /// Float LUT sampled from an `sf32`-type tag.
    FloatLut(Vec<f32>),
    /// Multi-process elements placeholder (stores raw byte count only).
    MultiProcessElements(usize),
}

/// Spectral data extracted from an iccMAX spectral tag.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SpectralData {
    /// Starting wavelength in nanometres.
    pub start_nm: f32,
    /// Ending wavelength in nanometres.
    pub end_nm: f32,
    /// Number of spectral samples.
    pub sample_count: u16,
    /// Spectral values (one per sample, linear scale).
    pub values: Vec<f32>,
}

/// An ICC v5 / iccMAX profile (superset of `IccProfile`).
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct IccMaxProfile {
    /// The underlying ICC v4-compatible fields (header + tag table).
    pub base: IccProfile,
    /// Spectral PCS data, if a `spec`-type tag was found.
    pub spectral_pcs: Option<SpectralData>,
    /// Float LUT values from an `sf32`-type tag, if found.
    pub float_lut: Option<Vec<f32>>,
    /// All recognised iccMAX tags.
    pub tags: Vec<IccMaxTag>,
}

/// ICC color profile.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct IccProfile {
    /// Profile description.
    pub description: String,
    /// Profile version.
    pub version: IccProfileVersion,
    /// Color space to XYZ transformation matrix.
    pub to_xyz_matrix: Matrix3x3,
    /// XYZ to color space transformation matrix.
    pub from_xyz_matrix: Matrix3x3,
    /// Profile white point.
    pub white_point: Illuminant,
    /// Profile creation date (Unix timestamp).
    pub creation_date: u64,
}

// ── ICC binary constants ───────────────────────────────────────────────────────

const HEADER_SIZE: usize = 128;
const TAG_ENTRY_SIZE: usize = 12;

// Four-character-code helpers as u32 big-endian
const SIG_ACSP: u32 = 0x6163_7370; // 'acsp'
const SIG_MNTR: u32 = 0x6D6E_7472; // 'mntr'
const SIG_RGB_: u32 = 0x5247_4220; // 'RGB '
const SIG_XYZ_: u32 = 0x5859_5A20; // 'XYZ '
const SIG_XYZ_TYPE: u32 = 0x5859_5A20; // XYZType signature
const SIG_DESC: u32 = 0x6465_7363; // 'desc'
const SIG_MLUC: u32 = 0x6D6C_7563; // 'mluc'
const SIG_WTPT: u32 = 0x7774_7074; // 'wtpt'
const SIG_RXYZ: u32 = 0x7258_595A; // 'rXYZ'
const SIG_GXYZ: u32 = 0x6758_595A; // 'gXYZ'
const SIG_BXYZ: u32 = 0x6258_595A; // 'bXYZ'

const VERSION_V2: u32 = 0x0210_0000;
const VERSION_V4: u32 = 0x0400_0000;
const VERSION_V5: u32 = 0x0500_0000;

/// D50 PCS illuminant XYZ values written in ICC headers (fixed canonical values).
const D50_PCS_X: f64 = 0.964_2;
const D50_PCS_Y: f64 = 1.0;
const D50_PCS_Z: f64 = 0.824_9;

// ── s15Fixed16 helpers ─────────────────────────────────────────────────────────

#[allow(clippy::cast_possible_wrap)]
fn f64_to_s15f16(v: f64) -> i32 {
    (v * 65536.0).round() as i32
}

fn s15f16_to_f64(raw: i32) -> f64 {
    f64::from(raw) / 65536.0
}

// ── u32 / u16 big-endian read helpers ─────────────────────────────────────────

fn read_u32_be(data: &[u8], offset: usize) -> CalibrationResult<u32> {
    if offset + 4 > data.len() {
        return Err(CalibrationError::IccParseError(format!(
            "offset {offset} out of bounds (len={})",
            data.len()
        )));
    }
    Ok(u32::from_be_bytes([
        data[offset],
        data[offset + 1],
        data[offset + 2],
        data[offset + 3],
    ]))
}

fn read_i32_be(data: &[u8], offset: usize) -> CalibrationResult<i32> {
    if offset + 4 > data.len() {
        return Err(CalibrationError::IccParseError(format!(
            "offset {offset} out of bounds (len={})",
            data.len()
        )));
    }
    Ok(i32::from_be_bytes([
        data[offset],
        data[offset + 1],
        data[offset + 2],
        data[offset + 3],
    ]))
}

fn read_u16_be(data: &[u8], offset: usize) -> CalibrationResult<u16> {
    if offset + 2 > data.len() {
        return Err(CalibrationError::IccParseError(format!(
            "offset {offset} out of bounds (len={})",
            data.len()
        )));
    }
    Ok(u16::from_be_bytes([data[offset], data[offset + 1]]))
}

// ── Write helpers ──────────────────────────────────────────────────────────────

fn push_u32_be(buf: &mut Vec<u8>, v: u32) {
    buf.extend_from_slice(&v.to_be_bytes());
}

fn push_i32_be(buf: &mut Vec<u8>, v: i32) {
    buf.extend_from_slice(&v.to_be_bytes());
}

fn push_u16_be(buf: &mut Vec<u8>, v: u16) {
    buf.extend_from_slice(&v.to_be_bytes());
}

fn push_u64_zero(buf: &mut Vec<u8>) {
    buf.extend_from_slice(&[0u8; 8]);
}

// ── Creation-date helpers ──────────────────────────────────────────────────────

/// Parse a 6×u16 (year, month, day, hour, min, sec) date-time block (big-endian)
/// and return an approximation of the Unix timestamp.
///
/// Uses the proleptic Gregorian calendar (days since 1970-01-01).
fn parse_creation_date(data: &[u8], offset: usize) -> CalibrationResult<u64> {
    let year = read_u16_be(data, offset)? as u64;
    let month = read_u16_be(data, offset + 2)? as u64;
    let day = read_u16_be(data, offset + 4)? as u64;
    let hour = read_u16_be(data, offset + 6)? as u64;
    let min = read_u16_be(data, offset + 8)? as u64;
    let sec = read_u16_be(data, offset + 10)? as u64;

    // If all fields are zero, return 0 (unknown / not set).
    if year == 0 && month == 0 && day == 0 {
        return Ok(0);
    }

    // Approximate days since Unix epoch (1970-01-01) using the standard formula.
    // Month and day are 1-based in ICC, guard against zero-month.
    let m = month.max(1);
    let d = day.max(1);

    // Days in each year, leap years (simplified):
    // days_since_epoch ≈ (year-1970)*365 + leap_days + month_days + day
    let y = year;
    let leap_days = if y >= 1970 {
        let span = y - 1970;
        span / 4 - span / 100 + span / 400
    } else {
        0
    };

    // Cumulative days at start of each month (non-leap; we correct for leap below).
    const MONTH_DAYS: [u64; 12] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
    let m_idx = (m.saturating_sub(1) as usize).min(11);
    let month_day_offset = MONTH_DAYS[m_idx];

    // Add 1 if month > Feb and current year is a leap year.
    let is_leap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
    let leap_adj: u64 = if m > 2 && is_leap { 1 } else { 0 };

    let days_since_epoch =
        (y.saturating_sub(1970)) * 365 + leap_days + month_day_offset + leap_adj + d - 1;

    Ok(days_since_epoch * 86400 + hour * 3600 + min * 60 + sec)
}

/// Write a Unix timestamp as 6×u16 (year, month, day, hour, min, sec) into `buf`.
fn push_creation_date(buf: &mut Vec<u8>, ts: u64) {
    if ts == 0 {
        for _ in 0..6 {
            push_u16_be(buf, 0);
        }
        return;
    }

    let secs_in_day = ts % 86400;
    let mut days = ts / 86400;

    let hour = secs_in_day / 3600;
    let min = (secs_in_day % 3600) / 60;
    let sec = secs_in_day % 60;

    // Compute year from days since 1970.
    let mut year = 1970u64;
    loop {
        let days_in_year: u64 = if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 {
            366
        } else {
            365
        };
        if days < days_in_year {
            break;
        }
        days -= days_in_year;
        year += 1;
    }

    let is_leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    let days_per_month: [u64; 12] = [
        31,
        if is_leap { 29 } else { 28 },
        31,
        30,
        31,
        30,
        31,
        31,
        30,
        31,
        30,
        31,
    ];
    let mut month = 1u64;
    for dm in &days_per_month {
        if days < *dm {
            break;
        }
        days -= dm;
        month += 1;
    }
    let day = days + 1; // 1-based

    push_u16_be(buf, year as u16);
    push_u16_be(buf, month as u16);
    push_u16_be(buf, day as u16);
    push_u16_be(buf, hour as u16);
    push_u16_be(buf, min as u16);
    push_u16_be(buf, sec as u16);
}

// ── XYZNumber tag helpers ──────────────────────────────────────────────────────

/// Parse an XYZType tag blob starting at `tag_offset` within `data`.
/// Layout: 4B signature + 4B reserved + 3 × s15Fixed16.
fn parse_xyz_tag(data: &[u8], tag_offset: usize) -> CalibrationResult<[f64; 3]> {
    // Check we have at least 20 bytes (8-byte header + 12 bytes for 3 × s15F16).
    if tag_offset + 20 > data.len() {
        return Err(CalibrationError::IccParseError(format!(
            "XYZType tag at offset {tag_offset}: insufficient data"
        )));
    }
    let sig = read_u32_be(data, tag_offset)?;
    if sig != SIG_XYZ_TYPE {
        return Err(CalibrationError::IccParseError(format!(
            "XYZType tag: unexpected signature {sig:#010x}"
        )));
    }
    let x = s15f16_to_f64(read_i32_be(data, tag_offset + 8)?);
    let y = s15f16_to_f64(read_i32_be(data, tag_offset + 12)?);
    let z = s15f16_to_f64(read_i32_be(data, tag_offset + 16)?);
    Ok([x, y, z])
}

/// Serialize an XYZType tag blob (20 bytes).
fn xyz_tag_bytes(xyz: &[f64; 3]) -> Vec<u8> {
    let mut buf = Vec::with_capacity(20);
    push_u32_be(&mut buf, SIG_XYZ_TYPE); // type signature 'XYZ '
    push_u32_be(&mut buf, 0); // reserved
    push_i32_be(&mut buf, f64_to_s15f16(xyz[0]));
    push_i32_be(&mut buf, f64_to_s15f16(xyz[1]));
    push_i32_be(&mut buf, f64_to_s15f16(xyz[2]));
    buf
}

// ── Description tag helpers ────────────────────────────────────────────────────

/// Parse a `desc` or `mluc` tag blob and extract an ASCII/Latin-1 string.
fn parse_desc_tag(data: &[u8], tag_offset: usize, tag_size: usize) -> CalibrationResult<String> {
    if tag_offset + 4 > data.len() {
        return Err(CalibrationError::IccParseError(
            "desc tag: too short to read signature".to_string(),
        ));
    }
    let sig = read_u32_be(data, tag_offset)?;

    match sig {
        SIG_DESC => parse_desc_v2(data, tag_offset, tag_size),
        SIG_MLUC => parse_mluc_tag(data, tag_offset, tag_size),
        _ => Err(CalibrationError::IccParseError(format!(
            "desc/mluc tag: unexpected signature {sig:#010x}"
        ))),
    }
}

/// Parse an ICC v2 `desc` tag.
/// Layout: 4B sig + 4B reserved + 4B ascii_length + ascii_bytes (null-terminated).
fn parse_desc_v2(data: &[u8], tag_offset: usize, _tag_size: usize) -> CalibrationResult<String> {
    if tag_offset + 12 > data.len() {
        return Err(CalibrationError::IccParseError(
            "desc tag (v2): too short for header".to_string(),
        ));
    }
    let ascii_len = read_u32_be(data, tag_offset + 8)? as usize;
    let start = tag_offset + 12;
    if ascii_len == 0 {
        return Ok(String::new());
    }
    let end = start
        .checked_add(ascii_len)
        .ok_or_else(|| CalibrationError::IccParseError("desc tag: length overflow".to_string()))?;
    if end > data.len() {
        return Err(CalibrationError::IccParseError(format!(
            "desc tag (v2): ascii_len {ascii_len} exceeds data length"
        )));
    }
    let raw = &data[start..end];
    // Trim null terminator if present.
    let raw = raw.split(|&b| b == 0).next().unwrap_or(raw);
    Ok(String::from_utf8_lossy(raw).into_owned())
}

/// Parse an ICC v4 `mluc` (MultiLocalizedUnicode) tag — extract first record as UTF-16BE.
fn parse_mluc_tag(data: &[u8], tag_offset: usize, tag_size: usize) -> CalibrationResult<String> {
    // mluc layout:
    //   4B sig + 4B reserved + 4B record_count + 4B record_size (usually 12)
    //   Then records: 2B lang + 2B country + 4B length_in_bytes + 4B offset_from_tag_start
    if tag_offset + 16 > data.len() {
        return Err(CalibrationError::IccParseError(
            "mluc tag: too short for header".to_string(),
        ));
    }
    let record_count = read_u32_be(data, tag_offset + 8)? as usize;
    if record_count == 0 {
        return Ok(String::new());
    }
    let record_size = read_u32_be(data, tag_offset + 12)? as usize;
    if record_size < 12 {
        return Err(CalibrationError::IccParseError(
            "mluc tag: record_size too small".to_string(),
        ));
    }
    // First record starts at tag_offset + 16.
    let rec_start = tag_offset + 16;
    if rec_start + record_size > data.len() {
        return Err(CalibrationError::IccParseError(
            "mluc tag: first record out of bounds".to_string(),
        ));
    }
    let str_len_bytes = read_u32_be(data, rec_start + 4)? as usize; // length in bytes (UTF-16BE)
    let str_offset_from_tag = read_u32_be(data, rec_start + 8)? as usize;
    let abs_str_start = tag_offset + str_offset_from_tag;
    let abs_str_end = abs_str_start.checked_add(str_len_bytes).ok_or_else(|| {
        CalibrationError::IccParseError("mluc: string length overflow".to_string())
    })?;
    let _ = tag_size; // used for doc context
    if abs_str_end > data.len() {
        return Err(CalibrationError::IccParseError(
            "mluc tag: string data out of bounds".to_string(),
        ));
    }
    // Decode UTF-16BE.
    let utf16_bytes = &data[abs_str_start..abs_str_end];
    let code_units: Vec<u16> = utf16_bytes
        .chunks_exact(2)
        .map(|ch| u16::from_be_bytes([ch[0], ch[1]]))
        .collect();
    Ok(String::from_utf16_lossy(&code_units))
}

/// Serialize a `desc` tag blob (ICC v2 format).
/// Layout: 4B sig + 4B reserved (0) + 4B ascii_length + ascii_bytes + null + padding.
fn desc_tag_bytes(desc: &str) -> Vec<u8> {
    let ascii: Vec<u8> = desc.bytes().collect();
    let ascii_len = ascii.len() + 1; // include null terminator in length field
    let mut buf = Vec::new();
    push_u32_be(&mut buf, SIG_DESC); // 'desc'
    push_u32_be(&mut buf, 0); // reserved
    push_u32_be(&mut buf, ascii_len as u32); // length including null
    buf.extend_from_slice(&ascii);
    buf.push(0u8); // null terminator
                   // Pad to 4-byte alignment.
    while buf.len() % 4 != 0 {
        buf.push(0u8);
    }
    buf
}

// ── Illuminant detection ───────────────────────────────────────────────────────

/// Map an XYZ triplet to the closest `Illuminant`, or `D50` if nothing is within ±0.01.
fn xyz_to_illuminant(xyz: &[f64; 3]) -> Illuminant {
    let candidates = [
        Illuminant::A,
        Illuminant::D50,
        Illuminant::D55,
        Illuminant::D65,
        Illuminant::D75,
        Illuminant::E,
        Illuminant::F2,
        Illuminant::F7,
        Illuminant::F11,
    ];
    for &ill in &candidates {
        let ref_xyz = ill.xyz();
        // Normalize to Y=1 before comparing (profiles store Y=1 XYZ).
        let y = ref_xyz[1]; // always 1.0 by definition
        if (xyz[0] - ref_xyz[0] / y).abs() < 0.01
            && (xyz[1] - ref_xyz[1] / y).abs() < 0.01
            && (xyz[2] - ref_xyz[2] / y).abs() < 0.01
        {
            return ill;
        }
    }
    Illuminant::D50 // fallback
}

// ── IccProfile implementation ──────────────────────────────────────────────────

impl IccProfile {
    /// Create a new ICC profile.
    #[must_use]
    pub fn new(description: String, to_xyz_matrix: Matrix3x3, white_point: Illuminant) -> Self {
        let from_xyz_matrix = Self::compute_inverse_matrix(&to_xyz_matrix);

        Self {
            description,
            version: IccProfileVersion::V4,
            to_xyz_matrix,
            from_xyz_matrix,
            white_point,
            creation_date: 0, // Placeholder
        }
    }

    /// Parse an ICC profile from bytes.
    ///
    /// # Arguments
    ///
    /// * `data` - ICC profile data
    ///
    /// # Errors
    ///
    /// Returns an error if parsing fails.
    pub fn from_bytes(data: &[u8]) -> CalibrationResult<Self> {
        // ── Step 1: validate minimum header size and 'acsp' signature ──────────
        if data.len() < HEADER_SIZE {
            return Err(CalibrationError::IccParseError(format!(
                "data too short: {} bytes (minimum {})",
                data.len(),
                HEADER_SIZE
            )));
        }
        let acsp = read_u32_be(data, 36)?;
        if acsp != SIG_ACSP {
            return Err(CalibrationError::IccParseError(format!(
                "not an ICC profile: 'acsp' signature missing (got {acsp:#010x})"
            )));
        }

        // ── Step 2: parse version ──────────────────────────────────────────────
        let ver_raw = read_u32_be(data, 8)?;
        let version = match ver_raw >> 24 {
            2 => IccProfileVersion::V2,
            4 => IccProfileVersion::V4,
            5 => IccProfileVersion::V5IccMax,
            _ => {
                // Accept any version but default to V4.
                IccProfileVersion::V4
            }
        };

        // ── Step 3: parse creation date (offset 24, 12 bytes = 6×u16) ─────────
        let creation_date = parse_creation_date(data, 24)?;

        // ── Step 4: parse tag table ────────────────────────────────────────────
        // Tag table starts immediately after the 128-byte header.
        let tag_count = read_u32_be(data, HEADER_SIZE)? as usize;

        // Collect (signature, offset, size) entries.
        let mut desc_pos: Option<(usize, usize)> = None;
        let mut wtpt_pos: Option<(usize, usize)> = None;
        let mut rxyz_pos: Option<(usize, usize)> = None;
        let mut gxyz_pos: Option<(usize, usize)> = None;
        let mut bxyz_pos: Option<(usize, usize)> = None;

        let table_start = HEADER_SIZE + 4;
        for i in 0..tag_count {
            let entry_offset = table_start + i * TAG_ENTRY_SIZE;
            if entry_offset + TAG_ENTRY_SIZE > data.len() {
                break;
            }
            let sig = read_u32_be(data, entry_offset)?;
            let tag_off = read_u32_be(data, entry_offset + 4)? as usize;
            let tag_size = read_u32_be(data, entry_offset + 8)? as usize;

            match sig {
                SIG_DESC => desc_pos = Some((tag_off, tag_size)),
                SIG_WTPT => wtpt_pos = Some((tag_off, tag_size)),
                SIG_RXYZ => rxyz_pos = Some((tag_off, tag_size)),
                SIG_GXYZ => gxyz_pos = Some((tag_off, tag_size)),
                SIG_BXYZ => bxyz_pos = Some((tag_off, tag_size)),
                _ => {} // ignore unknown tags
            }
        }

        // ── Step 5: parse required tags ────────────────────────────────────────
        let description = if let Some((off, sz)) = desc_pos {
            parse_desc_tag(data, off, sz)?
        } else {
            String::new()
        };

        let white_point = if let Some((off, _)) = wtpt_pos {
            let xyz = parse_xyz_tag(data, off)?;
            xyz_to_illuminant(&xyz)
        } else {
            Illuminant::D50
        };

        // Parse primary XYZ tags — build to_xyz_matrix.
        let r_xyz = rxyz_pos
            .map(|(off, _)| parse_xyz_tag(data, off))
            .transpose()?
            .unwrap_or([1.0, 0.0, 0.0]);
        let g_xyz = gxyz_pos
            .map(|(off, _)| parse_xyz_tag(data, off))
            .transpose()?
            .unwrap_or([0.0, 1.0, 0.0]);
        let b_xyz = bxyz_pos
            .map(|(off, _)| parse_xyz_tag(data, off))
            .transpose()?
            .unwrap_or([0.0, 0.0, 1.0]);

        // Columns of to_xyz_matrix are rXYZ, gXYZ, bXYZ.
        let to_xyz_matrix: Matrix3x3 = [
            [r_xyz[0], g_xyz[0], b_xyz[0]], // row 0: X from each primary
            [r_xyz[1], g_xyz[1], b_xyz[1]], // row 1: Y from each primary
            [r_xyz[2], g_xyz[2], b_xyz[2]], // row 2: Z from each primary
        ];
        let from_xyz_matrix = Self::compute_inverse_matrix(&to_xyz_matrix);

        Ok(Self {
            description,
            version,
            to_xyz_matrix,
            from_xyz_matrix,
            white_point,
            creation_date,
        })
    }

    /// Serialize the ICC profile to bytes.
    ///
    /// Produces a well-formed ICC binary that can be round-tripped through
    /// [`IccProfile::from_bytes`].
    ///
    /// # Errors
    ///
    /// Returns an error if serialization fails.
    pub fn to_bytes(&self) -> CalibrationResult<Vec<u8>> {
        // ── Step 1: build tag data blobs ───────────────────────────────────────

        // desc tag
        let desc_blob = desc_tag_bytes(&self.description);

        // wtpt tag — white point XYZ (Y-normalized)
        let wp_xyz = self.white_point.xyz();
        let wtpt_blob = xyz_tag_bytes(&wp_xyz);

        // Primary color tags — columns of to_xyz_matrix → individual XYZ triplets.
        let r_xyz = [
            self.to_xyz_matrix[0][0],
            self.to_xyz_matrix[1][0],
            self.to_xyz_matrix[2][0],
        ];
        let g_xyz = [
            self.to_xyz_matrix[0][1],
            self.to_xyz_matrix[1][1],
            self.to_xyz_matrix[2][1],
        ];
        let b_xyz = [
            self.to_xyz_matrix[0][2],
            self.to_xyz_matrix[1][2],
            self.to_xyz_matrix[2][2],
        ];
        let rxyz_blob = xyz_tag_bytes(&r_xyz);
        let gxyz_blob = xyz_tag_bytes(&g_xyz);
        let bxyz_blob = xyz_tag_bytes(&b_xyz);

        // ── Step 2: calculate layout ───────────────────────────────────────────
        // 5 tags in the tag table.
        const TAG_COUNT: u32 = 5;
        let tag_table_size = 4 + (TAG_COUNT as usize) * TAG_ENTRY_SIZE; // 4 + 5*12 = 64
        let data_start = HEADER_SIZE + tag_table_size; // 128 + 64 = 192

        let desc_off = data_start;
        let wtpt_off = desc_off + desc_blob.len();
        let rxyz_off = wtpt_off + wtpt_blob.len();
        let gxyz_off = rxyz_off + rxyz_blob.len();
        let bxyz_off = gxyz_off + gxyz_blob.len();
        let total_size = bxyz_off + bxyz_blob.len();

        // ── Step 3: write header (128 bytes) ──────────────────────────────────
        let mut buf: Vec<u8> = Vec::with_capacity(total_size);

        // 0-3: total profile size
        push_u32_be(&mut buf, total_size as u32);
        // 4-7: CMM type (0 = unspecified)
        push_u32_be(&mut buf, 0);
        // 8-11: version
        let ver_u32 = match self.version {
            IccProfileVersion::V2 => VERSION_V2,
            IccProfileVersion::V4 => VERSION_V4,
            IccProfileVersion::V5IccMax => VERSION_V5,
        };
        push_u32_be(&mut buf, ver_u32);
        // 12-15: device class 'mntr'
        push_u32_be(&mut buf, SIG_MNTR);
        // 16-19: color space 'RGB '
        push_u32_be(&mut buf, SIG_RGB_);
        // 20-23: PCS 'XYZ '
        push_u32_be(&mut buf, SIG_XYZ_);
        // 24-35: creation date/time (6 × u16 BE)
        push_creation_date(&mut buf, self.creation_date);
        // 36-39: 'acsp'
        push_u32_be(&mut buf, SIG_ACSP);
        // 40-43: platform signature (0)
        push_u32_be(&mut buf, 0);
        // 44-47: flags (0)
        push_u32_be(&mut buf, 0);
        // 48-51: device manufacturer (0)
        push_u32_be(&mut buf, 0);
        // 52-55: device model (0)
        push_u32_be(&mut buf, 0);
        // 56-63: device attributes (0, 8 bytes)
        push_u64_zero(&mut buf);
        // 64-67: rendering intent (0 = perceptual)
        push_u32_be(&mut buf, 0);
        // 68-79: PCS illuminant XYZ in s15Fixed16 (D50 canonical)
        push_i32_be(&mut buf, f64_to_s15f16(D50_PCS_X));
        push_i32_be(&mut buf, f64_to_s15f16(D50_PCS_Y));
        push_i32_be(&mut buf, f64_to_s15f16(D50_PCS_Z));
        // 80-83: profile creator (0)
        push_u32_be(&mut buf, 0);
        // 84-99: profile ID / MD5 (16 zero bytes)
        buf.extend_from_slice(&[0u8; 16]);
        // 100-127: reserved (28 zero bytes)
        buf.extend_from_slice(&[0u8; 28]);

        debug_assert_eq!(buf.len(), HEADER_SIZE, "header must be exactly 128 bytes");

        // ── Step 4: write tag table ────────────────────────────────────────────
        push_u32_be(&mut buf, TAG_COUNT);

        // desc
        push_u32_be(&mut buf, SIG_DESC);
        push_u32_be(&mut buf, desc_off as u32);
        push_u32_be(&mut buf, desc_blob.len() as u32);

        // wtpt
        push_u32_be(&mut buf, SIG_WTPT);
        push_u32_be(&mut buf, wtpt_off as u32);
        push_u32_be(&mut buf, wtpt_blob.len() as u32);

        // rXYZ
        push_u32_be(&mut buf, SIG_RXYZ);
        push_u32_be(&mut buf, rxyz_off as u32);
        push_u32_be(&mut buf, rxyz_blob.len() as u32);

        // gXYZ
        push_u32_be(&mut buf, SIG_GXYZ);
        push_u32_be(&mut buf, gxyz_off as u32);
        push_u32_be(&mut buf, gxyz_blob.len() as u32);

        // bXYZ
        push_u32_be(&mut buf, SIG_BXYZ);
        push_u32_be(&mut buf, bxyz_off as u32);
        push_u32_be(&mut buf, bxyz_blob.len() as u32);

        debug_assert_eq!(
            buf.len(),
            data_start,
            "tag table must end at data_start offset"
        );

        // ── Step 5: write tag data blobs ───────────────────────────────────────
        buf.extend_from_slice(&desc_blob);
        buf.extend_from_slice(&wtpt_blob);
        buf.extend_from_slice(&rxyz_blob);
        buf.extend_from_slice(&gxyz_blob);
        buf.extend_from_slice(&bxyz_blob);

        debug_assert_eq!(
            buf.len(),
            total_size,
            "final size must match calculated total"
        );

        Ok(buf)
    }

    /// Convert an RGB color to XYZ using this profile.
    #[must_use]
    pub fn rgb_to_xyz(&self, rgb: &[f64; 3]) -> [f64; 3] {
        self.apply_matrix(&self.to_xyz_matrix, rgb)
    }

    /// Convert an XYZ color to RGB using this profile.
    #[must_use]
    pub fn xyz_to_rgb(&self, xyz: &[f64; 3]) -> [f64; 3] {
        self.apply_matrix(&self.from_xyz_matrix, xyz)
    }

    /// Apply a 3x3 matrix to a color.
    fn apply_matrix(&self, matrix: &Matrix3x3, color: &[f64; 3]) -> [f64; 3] {
        [
            matrix[0][0] * color[0] + matrix[0][1] * color[1] + matrix[0][2] * color[2],
            matrix[1][0] * color[0] + matrix[1][1] * color[1] + matrix[1][2] * color[2],
            matrix[2][0] * color[0] + matrix[2][1] * color[1] + matrix[2][2] * color[2],
        ]
    }

    /// Compute the inverse of a 3x3 matrix.
    fn compute_inverse_matrix(matrix: &Matrix3x3) -> Matrix3x3 {
        // Compute determinant
        let det = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])
            - matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])
            + matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);

        if det.abs() < 1e-10 {
            // Matrix is singular, return identity
            return [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        }

        let inv_det = 1.0 / det;

        // Compute inverse using adjugate method
        [
            [
                (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) * inv_det,
                (matrix[0][2] * matrix[2][1] - matrix[0][1] * matrix[2][2]) * inv_det,
                (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) * inv_det,
            ],
            [
                (matrix[1][2] * matrix[2][0] - matrix[1][0] * matrix[2][2]) * inv_det,
                (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) * inv_det,
                (matrix[0][2] * matrix[1][0] - matrix[0][0] * matrix[1][2]) * inv_det,
            ],
            [
                (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) * inv_det,
                (matrix[0][1] * matrix[2][0] - matrix[0][0] * matrix[2][1]) * inv_det,
                (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) * inv_det,
            ],
        ]
    }

    /// Validate the ICC profile.
    ///
    /// # Errors
    ///
    /// Returns an error if validation fails.
    pub fn validate(&self) -> CalibrationResult<()> {
        // Check that matrices are not all zeros
        let to_xyz_sum: f64 = self.to_xyz_matrix.iter().flatten().sum();
        let from_xyz_sum: f64 = self.from_xyz_matrix.iter().flatten().sum();

        if to_xyz_sum.abs() < 1e-10 {
            return Err(CalibrationError::IccInvalidProfile(
                "to_xyz_matrix is zero".to_string(),
            ));
        }

        if from_xyz_sum.abs() < 1e-10 {
            return Err(CalibrationError::IccInvalidProfile(
                "from_xyz_matrix is zero".to_string(),
            ));
        }

        Ok(())
    }
}

// ── iccMAX float32 read helpers ───────────────────────────────────────────────

fn read_f32_be(data: &[u8], offset: usize) -> CalibrationResult<f32> {
    if offset + 4 > data.len() {
        return Err(CalibrationError::IccParseError(format!(
            "read_f32_be: offset {offset} out of bounds (len={})",
            data.len()
        )));
    }
    Ok(f32::from_be_bytes([
        data[offset],
        data[offset + 1],
        data[offset + 2],
        data[offset + 3],
    ]))
}

// ── iccMAX tag parsers ─────────────────────────────────────────────────────────

/// Parse an `sf32` (sfloat32Array) iccMAX tag blob.
///
/// Layout: 4B type-sig `sf32` + 4B reserved + N×4B IEEE 754 single floats.
fn parse_sf32_tag(data: &[u8], tag_offset: usize, tag_size: usize) -> CalibrationResult<Vec<f32>> {
    if tag_size < 8 {
        return Ok(Vec::new());
    }
    let sig = read_u32_be(data, tag_offset)?;
    if sig != SIG_SF32 {
        return Err(CalibrationError::IccParseError(format!(
            "sf32 tag: unexpected signature {sig:#010x}"
        )));
    }
    let value_count = (tag_size - 8) / 4;
    let mut values = Vec::with_capacity(value_count);
    for i in 0..value_count {
        values.push(read_f32_be(data, tag_offset + 8 + i * 4)?);
    }
    Ok(values)
}

/// Parse a `spec` (spectralDataType) iccMAX tag blob.
///
/// Simplified layout:
///   4B type-sig `spec` + 4B reserved + 4B start_nm (f32) + 4B end_nm (f32)
///   + 2B sample_count + 2B reserved + sample_count×4B IEEE 754 floats.
fn parse_spectral_tag(
    data: &[u8],
    tag_offset: usize,
    tag_size: usize,
) -> CalibrationResult<SpectralData> {
    if tag_size < 18 {
        return Err(CalibrationError::IccParseError(
            "spectral tag: too small".to_string(),
        ));
    }
    let sig = read_u32_be(data, tag_offset)?;
    if sig != SIG_SPEC {
        return Err(CalibrationError::IccParseError(format!(
            "spectral tag: unexpected signature {sig:#010x}"
        )));
    }
    let start_nm = read_f32_be(data, tag_offset + 8)?;
    let end_nm = read_f32_be(data, tag_offset + 12)?;
    let sample_count = read_u16_be(data, tag_offset + 16)?;
    let data_start = tag_offset + 20;
    let mut values = Vec::with_capacity(sample_count as usize);
    for i in 0..sample_count as usize {
        values.push(read_f32_be(data, data_start + i * 4)?);
    }
    Ok(SpectralData {
        start_nm,
        end_nm,
        sample_count,
        values,
    })
}

/// Parse an ICC v5 / iccMAX profile from raw bytes.
///
/// This function reads the v5 header and dispatches known iccMAX tags
/// (`sf32`, `spec`, `mpef`) in addition to the standard v2/v4 tags.
///
/// # Errors
///
/// Returns an error if the bytes do not form a valid ICC profile or if
/// the version is not 5.
pub fn parse_iccmax(data: &[u8]) -> CalibrationResult<IccMaxProfile> {
    // First, run the standard v2/v4 parser to get the base fields.
    let base = IccProfile::from_bytes(data)?;

    if base.version != IccProfileVersion::V5IccMax {
        return Err(CalibrationError::IccParseError(format!(
            "expected iccMAX v5 profile, got {:?}",
            base.version
        )));
    }

    // Walk the tag table again and collect iccMAX-specific tags.
    let tag_count = read_u32_be(data, HEADER_SIZE)? as usize;
    let table_start = HEADER_SIZE + 4;

    let mut spectral_pcs: Option<SpectralData> = None;
    let mut float_lut: Option<Vec<f32>> = None;
    let mut tags: Vec<IccMaxTag> = Vec::new();

    for i in 0..tag_count {
        let entry_offset = table_start + i * TAG_ENTRY_SIZE;
        if entry_offset + TAG_ENTRY_SIZE > data.len() {
            break;
        }
        let _sig = read_u32_be(data, entry_offset)?;
        let tag_off = read_u32_be(data, entry_offset + 4)? as usize;
        let tag_size = read_u32_be(data, entry_offset + 8)? as usize;

        // Read the type signature from the tag blob itself.
        if tag_off + 4 > data.len() {
            continue;
        }
        let type_sig = read_u32_be(data, tag_off)?;

        match type_sig {
            SIG_SF32 => {
                if let Ok(values) = parse_sf32_tag(data, tag_off, tag_size) {
                    float_lut.get_or_insert_with(Vec::new).extend(&values);
                    tags.push(IccMaxTag::FloatLut(values));
                }
            }
            SIG_SPEC => {
                if let Ok(spec) = parse_spectral_tag(data, tag_off, tag_size) {
                    let spec_clone = spec.clone();
                    spectral_pcs.get_or_insert(spec);
                    tags.push(IccMaxTag::SpectralData(spec_clone));
                }
            }
            SIG_MPE => {
                tags.push(IccMaxTag::MultiProcessElements(tag_size));
            }
            _ => {}
        }
    }

    Ok(IccMaxProfile {
        base,
        spectral_pcs,
        float_lut,
        tags,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── Original tests (preserved) ─────────────────────────────────────────────

    #[test]
    fn test_icc_profile_new() {
        let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];

        let profile = IccProfile::new("Test Profile".to_string(), identity, Illuminant::D65);

        assert_eq!(profile.description, "Test Profile");
        assert_eq!(profile.version, IccProfileVersion::V4);
        assert_eq!(profile.white_point, Illuminant::D65);
    }

    #[test]
    fn test_icc_profile_rgb_to_xyz() {
        let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];

        let profile = IccProfile::new("Test Profile".to_string(), identity, Illuminant::D65);

        let rgb = [0.5, 0.6, 0.7];
        let xyz = profile.rgb_to_xyz(&rgb);

        assert!((xyz[0] - 0.5).abs() < 1e-10);
        assert!((xyz[1] - 0.6).abs() < 1e-10);
        assert!((xyz[2] - 0.7).abs() < 1e-10);
    }

    #[test]
    fn test_icc_profile_xyz_to_rgb() {
        let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];

        let profile = IccProfile::new("Test Profile".to_string(), identity, Illuminant::D65);

        let xyz = [0.5, 0.6, 0.7];
        let rgb = profile.xyz_to_rgb(&xyz);

        assert!((rgb[0] - 0.5).abs() < 1e-10);
        assert!((rgb[1] - 0.6).abs() < 1e-10);
        assert!((rgb[2] - 0.7).abs() < 1e-10);
    }

    #[test]
    fn test_icc_profile_roundtrip() {
        let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];

        let profile = IccProfile::new("Test Profile".to_string(), identity, Illuminant::D65);

        let rgb = [0.3, 0.5, 0.7];
        let xyz = profile.rgb_to_xyz(&rgb);
        let rgb2 = profile.xyz_to_rgb(&xyz);

        assert!((rgb2[0] - rgb[0]).abs() < 1e-10);
        assert!((rgb2[1] - rgb[1]).abs() < 1e-10);
        assert!((rgb2[2] - rgb[2]).abs() < 1e-10);
    }

    #[test]
    fn test_icc_profile_validate() {
        let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];

        let profile = IccProfile::new("Test Profile".to_string(), identity, Illuminant::D65);

        assert!(profile.validate().is_ok());
    }

    #[test]
    fn test_icc_profile_validate_invalid() {
        let zero_matrix = [[0.0; 3]; 3];

        let profile = IccProfile::new("Test Profile".to_string(), zero_matrix, Illuminant::D65);

        assert!(profile.validate().is_err());
    }

    #[test]
    fn test_compute_inverse_matrix() {
        let matrix = [[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]];

        let inverse = IccProfile::compute_inverse_matrix(&matrix);

        // Inverse of 2I should be 0.5I
        assert!((inverse[0][0] - 0.5).abs() < 1e-10);
        assert!((inverse[1][1] - 0.5).abs() < 1e-10);
        assert!((inverse[2][2] - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_compute_inverse_matrix_singular() {
        let singular = [[0.0; 3]; 3];

        let inverse = IccProfile::compute_inverse_matrix(&singular);

        // Should return identity for singular matrix
        assert!((inverse[0][0] - 1.0).abs() < 1e-10);
        assert!((inverse[1][1] - 1.0).abs() < 1e-10);
        assert!((inverse[2][2] - 1.0).abs() < 1e-10);
    }

    // ── New tests for from_bytes / to_bytes ────────────────────────────────────

    /// Build a minimal `IccProfile`, serialize it, parse it back, and check that
    /// all fields survive the round-trip within s15Fixed16 quantization tolerance.
    #[test]
    fn test_round_trip() {
        // Use sRGB-like primaries matrix.
        let to_xyz: Matrix3x3 = [
            [0.4124, 0.3576, 0.1805],
            [0.2126, 0.7152, 0.0722],
            [0.0193, 0.1192, 0.9505],
        ];
        let original = IccProfile {
            description: "sRGB Test".to_string(),
            version: IccProfileVersion::V4,
            to_xyz_matrix: to_xyz,
            from_xyz_matrix: IccProfile::compute_inverse_matrix(&to_xyz),
            white_point: Illuminant::D65,
            creation_date: 0, // zero = write/read as zeros
        };

        let bytes = original
            .to_bytes()
            .expect("to_bytes must succeed for valid profile");
        assert!(bytes.len() >= HEADER_SIZE, "serialized length >= 128");

        let parsed =
            IccProfile::from_bytes(&bytes).expect("from_bytes must succeed on our own output");

        assert_eq!(parsed.description, original.description);
        assert_eq!(parsed.version, original.version);
        assert_eq!(parsed.white_point, original.white_point);
        assert_eq!(parsed.creation_date, original.creation_date);

        // Matrices survive s15Fixed16 quantization: tolerance 1e-4 per entry.
        for row in 0..3 {
            for col in 0..3 {
                let orig = original.to_xyz_matrix[row][col];
                let got = parsed.to_xyz_matrix[row][col];
                assert!(
                    (orig - got).abs() < 1e-4,
                    "to_xyz_matrix[{row}][{col}]: {orig} vs {got}"
                );
            }
        }
    }

    /// from_bytes with a 'wrong acsp' signature must return Err.
    #[test]
    fn test_from_bytes_invalid_signature() {
        // Build a 128-byte block with wrong bytes at offset 36-39.
        let mut data = vec![0u8; HEADER_SIZE];
        // Write wrong magic at bytes 36-39.
        data[36] = b'X';
        data[37] = b'X';
        data[38] = b'X';
        data[39] = b'X';
        let result = IccProfile::from_bytes(&data);
        assert!(result.is_err(), "expected Err for invalid 'acsp' signature");
        let err_str = format!("{}", result.expect_err("checked above"));
        assert!(
            err_str.contains("acsp") || err_str.contains("ICC"),
            "error message should mention acsp: {err_str}"
        );
    }

    /// from_bytes on a 10-byte slice must return Err.
    #[test]
    fn test_from_bytes_too_short() {
        let tiny = vec![0u8; 10];
        let result = IccProfile::from_bytes(&tiny);
        assert!(result.is_err(), "expected Err for too-short input");
        let err_str = format!("{}", result.expect_err("checked above"));
        assert!(
            err_str.contains("too short") || err_str.contains("128") || err_str.contains("10"),
            "error should mention size: {err_str}"
        );
    }

    /// Round-trip with a non-trivial creation_date (a known Unix timestamp).
    #[test]
    fn test_round_trip_creation_date() {
        // 2024-01-15 12:00:00 UTC (approximate, from days arithmetic).
        // days_since_epoch = (2024-1970)*365 + leap_corrections + month + day offsets.
        // We use an explicit value that must round-trip exactly.
        let ts: u64 = 1_705_320_000; // 2024-01-15 12:00:00 UTC (approx)
        let to_xyz: Matrix3x3 = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let original = IccProfile {
            description: "DateTest".to_string(),
            version: IccProfileVersion::V2,
            to_xyz_matrix: to_xyz,
            from_xyz_matrix: IccProfile::compute_inverse_matrix(&to_xyz),
            white_point: Illuminant::D50,
            creation_date: ts,
        };
        let bytes = original.to_bytes().expect("to_bytes succeeded");
        let parsed = IccProfile::from_bytes(&bytes).expect("from_bytes succeeded");
        // Allow ±2 seconds tolerance for calendar conversion rounding.
        assert!(
            parsed.creation_date.abs_diff(ts) <= 2,
            "creation_date round-trip: expected {ts}, got {}",
            parsed.creation_date
        );
        assert_eq!(parsed.version, IccProfileVersion::V2);
    }

    /// Verify s15Fixed16 conversion round-trips correctly.
    #[test]
    fn test_s15f16_roundtrip() {
        for &v in &[0.0f64, 1.0, -1.0, 0.9642, 0.8249, 0.4124, 0.0722] {
            let encoded = f64_to_s15f16(v);
            let decoded = s15f16_to_f64(encoded);
            assert!(
                (v - decoded).abs() < 1.6e-5,
                "s15Fixed16 round-trip failed for {v}: got {decoded}"
            );
        }
    }

    // ── iccMAX v5 tests ────────────────────────────────────────────────────────

    /// Build a minimal v5 ICC binary and verify `IccProfile::from_bytes` parses
    /// the version as `V5IccMax`.
    #[test]
    fn test_iccmax_version_parsed() {
        let to_xyz: Matrix3x3 = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let mut profile = IccProfile {
            description: "iccMAX Test".to_string(),
            version: IccProfileVersion::V5IccMax,
            to_xyz_matrix: to_xyz,
            from_xyz_matrix: IccProfile::compute_inverse_matrix(&to_xyz),
            white_point: Illuminant::D50,
            creation_date: 0,
        };
        // Serialize — to_bytes now writes VERSION_V5 at offset 8.
        let bytes = profile.to_bytes().expect("to_bytes succeeded for V5");
        // Verify the raw version bytes at offset 8.
        let ver_raw = u32::from_be_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]);
        assert_eq!(ver_raw, VERSION_V5, "version bytes should be V5");

        // Parse back and check version field.
        let parsed = IccProfile::from_bytes(&bytes).expect("from_bytes succeeded");
        assert_eq!(
            parsed.version,
            IccProfileVersion::V5IccMax,
            "parsed version must be V5IccMax"
        );

        // parse_iccmax must also succeed and return the base profile correctly.
        profile.version = IccProfileVersion::V5IccMax;
        let iccmax = parse_iccmax(&bytes).expect("parse_iccmax succeeded");
        assert_eq!(iccmax.base.version, IccProfileVersion::V5IccMax);
        assert_eq!(iccmax.base.description, "iccMAX Test");
    }

    /// Test that a hand-crafted `sf32` tag blob is correctly parsed and stored
    /// inside an `IccMaxProfile`.
    #[test]
    fn test_iccmax_float_tag_roundtrip() {
        // Build a v5 profile manually by serializing a base profile and then
        // appending a fake `sf32` tag (type=SIG_SF32) to the tag table.
        //
        // Strategy: we cannot easily modify an already-serialized profile's tag
        // table because offsets are embedded, so we instead call parse_sf32_tag
        // directly with a hand-crafted buffer.

        // ── hand-craft an sf32 tag blob ──
        let values_in: Vec<f32> = vec![0.25_f32, 0.50_f32, 0.75_f32, 1.0_f32];
        let mut blob: Vec<u8> = Vec::new();
        // 4B type signature 'sf32'
        blob.extend_from_slice(&SIG_SF32.to_be_bytes());
        // 4B reserved
        blob.extend_from_slice(&0u32.to_be_bytes());
        // 4 × 4B IEEE 754 f32 BE
        for &v in &values_in {
            blob.extend_from_slice(&v.to_be_bytes());
        }
        let tag_size = blob.len();

        let parsed_values =
            parse_sf32_tag(&blob, 0, tag_size).expect("parse_sf32_tag must succeed");
        assert_eq!(parsed_values.len(), values_in.len(), "value count matches");
        for (a, b) in parsed_values.iter().zip(values_in.iter()) {
            assert!(
                (a - b).abs() < f32::EPSILON,
                "float value mismatch: {a} vs {b}"
            );
        }

        // Also verify that wrapping in IccMaxTag works.
        let tag = IccMaxTag::FloatLut(parsed_values.clone());
        if let IccMaxTag::FloatLut(ref v) = tag {
            assert_eq!(v.len(), 4, "FloatLut should hold 4 values");
            assert!((v[1] - 0.5_f32).abs() < f32::EPSILON);
        } else {
            panic!("expected IccMaxTag::FloatLut");
        }
    }
}