zenavif-serialize 0.1.2

AVIF container serializer (MPEG/HEIF/MIAF/ISO-BMFF) with animation and grid support
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
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
//! # AVIF image serializer (muxer)
//!
//! ## Usage
//!
//! 1. Compress pixels using an AV1 encoder, such as [rav1e](https://lib.rs/rav1e). [libaom](https://lib.rs/libaom-sys) works too.
//!
//! 2. Call `avif_serialize::serialize_to_vec(av1_data, None, width, height, 8)`
//!
//! See [cavif](https://github.com/kornelski/cavif-rs) for a complete implementation.

pub mod animated;
mod boxes;
pub mod constants;
pub mod grid;
mod writer;

use crate::boxes::*;
use arrayvec::ArrayVec;
use std::io;

// Re-export box types needed by the public API
pub use crate::boxes::{Av1CBox, ClapBox, ClliBox, ColrBox, ColrIccBox, IrotBox, ImirBox, MdcvBox, PaspBox};

/// Chroma subsampling configuration for AV1 encoding.
///
/// `(false, false)` = 4:4:4 (no subsampling).
/// `(true, true)` = 4:2:0 (both axes subsampled).
/// `(true, false)` = 4:2:2 (horizontal only).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChromaSubsampling {
    /// Whether the horizontal (X) axis is subsampled.
    pub horizontal: bool,
    /// Whether the vertical (Y) axis is subsampled.
    pub vertical: bool,
}

impl ChromaSubsampling {
    /// 4:4:4 — no chroma subsampling.
    pub const NONE: Self = Self { horizontal: false, vertical: false };
    /// 4:2:0 — both axes subsampled.
    pub const YUV420: Self = Self { horizontal: true, vertical: true };
    /// 4:2:2 — horizontal subsampling only.
    pub const YUV422: Self = Self { horizontal: true, vertical: false };
}

impl From<(bool, bool)> for ChromaSubsampling {
    fn from((h, v): (bool, bool)) -> Self {
        Self { horizontal: h, vertical: v }
    }
}

impl From<ChromaSubsampling> for (bool, bool) {
    fn from(cs: ChromaSubsampling) -> Self {
        (cs.horizontal, cs.vertical)
    }
}

/// Config for the serialization (allows setting advanced image properties).
///
/// See [`Aviffy::new`].
pub struct Aviffy {
    premultiplied_alpha: bool,
    colr: ColrBox,
    clli: Option<ClliBox>,
    mdcv: Option<MdcvBox>,
    irot: Option<IrotBox>,
    imir: Option<ImirBox>,
    clap: Option<ClapBox>,
    pasp: Option<PaspBox>,
    icc_profile: Option<Vec<u8>>,
    min_seq_profile: u8,
    chroma_subsampling: ChromaSubsampling,
    monochrome: bool,
    width: u32,
    height: u32,
    bit_depth: u8,
    exif: Option<Vec<u8>>,
    xmp: Option<Vec<u8>>,
    gain_map: Option<GainMapConfig>,
}

/// Configuration for an ISO 21496-1 gain map embedded in the AVIF container.
///
/// The gain map enables adaptive HDR rendering: an SDR base image combined
/// with a gain map and tone-map metadata allows reconstructing an HDR rendition.
struct GainMapConfig {
    /// AV1-encoded gain map image data.
    av1_data: Vec<u8>,
    /// Width of the gain map image in pixels.
    width: u32,
    /// Height of the gain map image in pixels.
    height: u32,
    /// Bit depth of the gain map image (8, 10, or 12).
    bit_depth: u8,
    /// ISO 21496-1 binary metadata blob (the `tmap` item payload).
    metadata: Vec<u8>,
    /// CICP color information for the alternate (typically HDR) rendition.
    /// Stored as a `colr` property on the `tmap` item.
    alt_colr: Option<ColrBox>,
    /// Chroma subsampling of the gain map AV1 data.
    chroma_subsampling: ChromaSubsampling,
    /// Whether the gain map is monochrome.
    monochrome: bool,
}

/// Makes an AVIF file given encoded AV1 data (create the data with [`rav1e`](https://lib.rs/rav1e))
///
/// `color_av1_data` is already-encoded AV1 image data for the color channels (YUV, RGB, etc.).
/// [You can parse this information out of AV1 payload with `avif-parse`](https://docs.rs/zenavif-parse/latest/zenavif_parse/struct.AV1Metadata.html).
///
/// The color image should have been encoded without chroma subsampling AKA YUV444 (`Cs444` in `rav1e`)
/// AV1 handles full-res color so effortlessly, you should never need chroma subsampling ever again.
///
/// Optional `alpha_av1_data` is a monochrome image (`rav1e` calls it "YUV400"/`Cs400`) representing transparency.
/// Alpha adds a lot of header bloat, so don't specify it unless it's necessary.
///
/// `width`/`height` is image size in pixels. It must of course match the size of encoded image data.
/// `depth_bits` should be 8, 10 or 12, depending on how the image was encoded.
///
/// Color and alpha must have the same dimensions and depth.
///
/// Data is written (streamed) to `into_output`.
pub fn serialize<W: io::Write>(into_output: W, color_av1_data: &[u8], alpha_av1_data: Option<&[u8]>, width: u32, height: u32, depth_bits: u8) -> io::Result<()> {
    Aviffy::new()
        .set_width(width)
        .set_height(height)
        .set_bit_depth(depth_bits)
        .write_slice(into_output, color_av1_data, alpha_av1_data)
}

impl Default for Aviffy {
    fn default() -> Self {
        Self::new()
    }
}

impl Aviffy {
    /// You will have to set image properties to match the AV1 bitstream.
    ///
    /// [You can get this information out of the AV1 payload with `avif-parse`](https://docs.rs/zenavif-parse/latest/zenavif_parse/struct.AV1Metadata.html).
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self {
            premultiplied_alpha: false,
            min_seq_profile: 1,
            chroma_subsampling: ChromaSubsampling::NONE,
            monochrome: false,
            width: 0,
            height: 0,
            bit_depth: 0,
            colr: ColrBox::default(),
            clli: None,
            mdcv: None,
            irot: None,
            imir: None,
            clap: None,
            pasp: None,
            icc_profile: None,
            exif: None,
            xmp: None,
            gain_map: None,
        }
    }

    /// If set, must match the AV1 color payload, and will result in `colr` box added to AVIF.
    /// Defaults to BT.601, because that's what Safari assumes when `colr` is missing.
    /// Other browsers are smart enough to read this from the AV1 payload instead.
    #[inline]
    pub fn set_matrix_coefficients(&mut self, matrix_coefficients: constants::MatrixCoefficients) -> &mut Self {
        self.colr.matrix_coefficients = matrix_coefficients;
        self
    }

    #[doc(hidden)]
    pub fn matrix_coefficients(&mut self, matrix_coefficients: constants::MatrixCoefficients) -> &mut Self {
        self.set_matrix_coefficients(matrix_coefficients)
    }

    /// If set, must match the AV1 color payload, and will result in `colr` box added to AVIF.
    /// Defaults to sRGB.
    #[inline]
    pub fn set_transfer_characteristics(&mut self, transfer_characteristics: constants::TransferCharacteristics) -> &mut Self {
        self.colr.transfer_characteristics = transfer_characteristics;
        self
    }

    #[doc(hidden)]
    pub fn transfer_characteristics(&mut self, transfer_characteristics: constants::TransferCharacteristics) -> &mut Self {
        self.set_transfer_characteristics(transfer_characteristics)
    }

    /// If set, must match the AV1 color payload, and will result in `colr` box added to AVIF.
    /// Defaults to sRGB/Rec.709.
    #[inline]
    pub fn set_color_primaries(&mut self, color_primaries: constants::ColorPrimaries) -> &mut Self {
        self.colr.color_primaries = color_primaries;
        self
    }

    #[doc(hidden)]
    pub fn color_primaries(&mut self, color_primaries: constants::ColorPrimaries) -> &mut Self {
        self.set_color_primaries(color_primaries)
    }

    /// If set, must match the AV1 color payload, and will result in `colr` box added to AVIF.
    /// Defaults to full.
    #[inline]
    pub fn set_full_color_range(&mut self, full_range: bool) -> &mut Self {
        self.colr.full_range_flag = full_range;
        self
    }

    #[doc(hidden)]
    pub fn full_color_range(&mut self, full_range: bool) -> &mut Self {
        self.set_full_color_range(full_range)
    }

    /// Set Content Light Level Information for HDR (CEA-861.3).
    ///
    /// `max_content_light_level` (MaxCLL) is the maximum light level of any single pixel in cd/m².
    /// `max_pic_average_light_level` (MaxFALL) is the maximum frame-average light level in cd/m².
    ///
    /// Adds a `clli` property box to the AVIF container.
    #[inline]
    pub fn set_content_light_level(&mut self, max_content_light_level: u16, max_pic_average_light_level: u16) -> &mut Self {
        self.clli = Some(ClliBox {
            max_content_light_level,
            max_pic_average_light_level,
        });
        self
    }

    /// Set Mastering Display Colour Volume for HDR (SMPTE ST 2086).
    ///
    /// `primaries` are the display primaries in CIE 1931 xy × 50000.
    /// Order: \[green, blue, red\] per SMPTE ST 2086.
    /// `white_point` uses the same encoding (e.g. D65 = (15635, 16450)).
    ///
    /// `max_luminance` and `min_luminance` are in cd/m² × 10000
    /// (e.g. 1000 cd/m² = 10_000_000, 0.005 cd/m² = 50).
    ///
    /// Adds an `mdcv` property box to the AVIF container.
    #[inline]
    pub fn set_mastering_display(&mut self, primaries: [(u16, u16); 3], white_point: (u16, u16), max_luminance: u32, min_luminance: u32) -> &mut Self {
        self.mdcv = Some(MdcvBox {
            primaries,
            white_point,
            max_luminance,
            min_luminance,
        });
        self
    }

    /// Set image rotation (counter-clockwise).
    ///
    /// `angle` is a raw code: 0=0°, 1=90°, 2=180°, 3=270°.
    /// Adds an `irot` property box.
    #[inline]
    pub fn set_rotation(&mut self, angle: u8) -> &mut Self {
        self.irot = Some(IrotBox { angle: angle & 0x03 });
        self
    }

    /// Set image mirror axis.
    ///
    /// `axis` = 0: vertical axis (left-right flip).
    /// `axis` = 1: horizontal axis (top-bottom flip).
    /// Adds an `imir` property box.
    #[inline]
    pub fn set_mirror(&mut self, axis: u8) -> &mut Self {
        self.imir = Some(ImirBox { axis: axis & 0x01 });
        self
    }

    /// Set clean aperture (crop rectangle).
    ///
    /// All values are rational numbers. Defines a centered crop region.
    /// Adds a `clap` property box.
    #[inline]
    pub fn set_clean_aperture(&mut self, clap: ClapBox) -> &mut Self {
        self.clap = Some(clap);
        self
    }

    /// Set pixel aspect ratio.
    ///
    /// `h_spacing` / `v_spacing` defines the ratio. For square pixels use (1, 1).
    /// Adds a `pasp` property box.
    #[inline]
    pub fn set_pixel_aspect_ratio(&mut self, h_spacing: u32, v_spacing: u32) -> &mut Self {
        self.pasp = Some(PaspBox { h_spacing, v_spacing });
        self
    }

    /// Set XMP metadata to be included in the AVIF file as a separate item.
    ///
    /// The data should be a valid XMP/RDF XML document.
    #[inline]
    pub fn set_xmp(&mut self, xmp: Vec<u8>) -> &mut Self {
        self.xmp = Some(xmp);
        self
    }

    /// Set ICC color profile to embed in the AVIF file.
    ///
    /// This adds a `colr` box with colour_type='prof'. When set, this
    /// replaces the nclx `colr` box (they are mutually exclusive per spec,
    /// though some files include both).
    #[inline]
    pub fn set_icc_profile(&mut self, icc_data: Vec<u8>) -> &mut Self {
        self.icc_profile = Some(icc_data);
        self
    }

    /// Set gain map data for ISO 21496-1 tone mapping.
    ///
    /// Creates three items in the AVIF container:
    /// - A gain map image item (`av01`) containing the AV1-encoded gain map
    /// - A `tmap` derived image item referencing both the primary image and gain map
    /// - The `tmap` item's payload containing the ISO 21496-1 metadata
    ///
    /// The `metadata` blob is the raw ISO 21496-1 binary format (version byte,
    /// minimum_version, writer_version, flags, headroom, per-channel parameters).
    ///
    /// The gain map image is typically a lower-resolution, monochrome or RGB image
    /// encoding the per-pixel gain needed to reconstruct the HDR rendition from
    /// the SDR base.
    #[inline]
    pub fn set_gain_map(&mut self, av1_data: Vec<u8>, width: u32, height: u32, bit_depth: u8, metadata: Vec<u8>) -> &mut Self {
        self.gain_map = Some(GainMapConfig {
            av1_data,
            width,
            height,
            bit_depth,
            metadata,
            alt_colr: None,
            chroma_subsampling: ChromaSubsampling::YUV420,
            monochrome: false,
        });
        self
    }

    /// Set the color information for the alternate (typically HDR) rendition.
    ///
    /// This CICP colr box is attached as a property of the `tmap` item,
    /// telling decoders what colour space the tone-mapped output targets.
    /// Only meaningful if [`set_gain_map`](Self::set_gain_map) has been called.
    #[inline]
    pub fn set_gain_map_alt_colr(&mut self, colr: ColrBox) -> &mut Self {
        if let Some(ref mut gm) = self.gain_map {
            gm.alt_colr = Some(colr);
        }
        self
    }

    /// Set chroma subsampling for the gain map AV1 data.
    ///
    /// Defaults to 4:2:0 if not called. Only meaningful if
    /// [`set_gain_map`](Self::set_gain_map) has been called.
    #[inline]
    pub fn set_gain_map_chroma_subsampling(&mut self, subsampling: impl Into<ChromaSubsampling>) -> &mut Self {
        if let Some(ref mut gm) = self.gain_map {
            gm.chroma_subsampling = subsampling.into();
        }
        self
    }

    /// Set whether the gain map image is monochrome.
    ///
    /// Defaults to false. Only meaningful if
    /// [`set_gain_map`](Self::set_gain_map) has been called.
    #[inline]
    pub fn set_gain_map_monochrome(&mut self, monochrome: bool) -> &mut Self {
        if let Some(ref mut gm) = self.gain_map {
            gm.monochrome = monochrome;
        }
        self
    }

    /// Makes an AVIF file given encoded AV1 data (create the data with [`rav1e`](https://lib.rs/rav1e))
    ///
    /// `color_av1_data` is already-encoded AV1 image data for the color channels (YUV, RGB, etc.).
    /// The color image should have been encoded without chroma subsampling AKA YUV444 (`Cs444` in `rav1e`)
    /// AV1 handles full-res color so effortlessly, you should never need chroma subsampling ever again.
    ///
    /// Optional `alpha_av1_data` is a monochrome image (`rav1e` calls it "YUV400"/`Cs400`) representing transparency.
    /// Alpha adds a lot of header bloat, so don't specify it unless it's necessary.
    ///
    /// `width`/`height` is image size in pixels. It must of course match the size of encoded image data.
    /// `depth_bits` should be 8, 10 or 12, depending on how the image has been encoded in AV1.
    ///
    /// Color and alpha must have the same dimensions and depth.
    ///
    /// Data is written (streamed) to `into_output`.
    #[inline]
    pub fn write<W: io::Write>(&self, into_output: W, color_av1_data: &[u8], alpha_av1_data: Option<&[u8]>, width: u32, height: u32, depth_bits: u8) -> io::Result<()> {
        self.make_boxes(color_av1_data, alpha_av1_data, width, height, depth_bits)?.write(into_output)
    }

    /// See [`Self::write`]
    #[inline]
    pub fn write_slice<W: io::Write>(&self, into_output: W, color_av1_data: &[u8], alpha_av1_data: Option<&[u8]>) -> io::Result<()> {
        self.make_boxes(color_av1_data, alpha_av1_data, self.width, self.height, self.bit_depth)?.write(into_output)
    }

    fn make_boxes<'data>(&'data self, color_av1_data: &'data [u8], alpha_av1_data: Option<&'data [u8]>, width: u32, height: u32, depth_bits: u8) -> io::Result<AvifFile<'data>> {
        if ![8, 10, 12].contains(&depth_bits) {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "depth must be 8/10/12"));
        }

        let mut image_items = ArrayVec::new();
        let mut iloc_items = ArrayVec::new();
        let mut ipma_entries = ArrayVec::new();
        let mut irefs = ArrayVec::new();
        let mut multi_irefs: ArrayVec<IrefMultiEntryBox, 2> = ArrayVec::new();
        let mut ipco = IpcoBox::new();
        let color_image_id: u16 = 1;
        let alpha_image_id: u16 = 2;
        let mut next_item_id: u16 = 3;
        const ESSENTIAL_BIT: u8 = 0x80;
        let color_depth_bits = depth_bits;
        let alpha_depth_bits = depth_bits; // Sadly, the spec requires these to match.

        image_items.push(InfeBox {
            id: color_image_id,
            typ: FourCC(*b"av01"),
            name: "",
            content_type: "",
        });

        let ispe_prop = ipco.push(IpcoProp::Ispe(IspeBox { width, height })).ok_or(io::ErrorKind::InvalidInput)?;

        // This is redundant, but Chrome wants it, and checks that it matches :(
        let av1c_color_prop = ipco.push(IpcoProp::Av1C(Av1CBox {
            seq_profile: self.min_seq_profile.max(if color_depth_bits >= 12 { 2 } else { 0 }),
            seq_level_idx_0: 31,
            seq_tier_0: false,
            high_bitdepth: color_depth_bits >= 10,
            twelve_bit: color_depth_bits >= 12,
            monochrome: self.monochrome,
            chroma_subsampling_x: self.chroma_subsampling.horizontal,
            chroma_subsampling_y: self.chroma_subsampling.vertical,
            chroma_sample_position: 0,
        })).ok_or(io::ErrorKind::InvalidInput)?;

        // Useless bloat
        let pixi_3 = ipco.push(IpcoProp::Pixi(PixiBox {
            channels: 3,
            depth: color_depth_bits,
        })).ok_or(io::ErrorKind::InvalidInput)?;

        let mut ipma = IpmaEntry {
            item_id: color_image_id,
            prop_ids: from_array([ispe_prop, av1c_color_prop | ESSENTIAL_BIT, pixi_3]),
        };

        // ICC profile takes precedence over nclx if both set
        if let Some(ref icc_data) = self.icc_profile {
            let colr_icc_prop = ipco.push(IpcoProp::ColrIcc(ColrIccBox {
                icc_data: icc_data.clone(),
            })).ok_or(io::ErrorKind::InvalidInput)?;
            ipma.prop_ids.push(colr_icc_prop);
        } else if self.colr != ColrBox::default() {
            // Redundant info, already in AV1
            let colr_color_prop = ipco.push(IpcoProp::Colr(self.colr)).ok_or(io::ErrorKind::InvalidInput)?;
            ipma.prop_ids.push(colr_color_prop);
        }

        if let Some(clli) = self.clli {
            let clli_prop = ipco.push(IpcoProp::Clli(clli)).ok_or(io::ErrorKind::InvalidInput)?;
            ipma.prop_ids.push(clli_prop);
        }

        if let Some(mdcv) = self.mdcv {
            let mdcv_prop = ipco.push(IpcoProp::Mdcv(mdcv)).ok_or(io::ErrorKind::InvalidInput)?;
            ipma.prop_ids.push(mdcv_prop);
        }

        if let Some(irot) = self.irot {
            let irot_prop = ipco.push(IpcoProp::Irot(irot)).ok_or(io::ErrorKind::InvalidInput)?;
            ipma.prop_ids.push(irot_prop | ESSENTIAL_BIT);
        }

        if let Some(imir) = self.imir {
            let imir_prop = ipco.push(IpcoProp::Imir(imir)).ok_or(io::ErrorKind::InvalidInput)?;
            ipma.prop_ids.push(imir_prop | ESSENTIAL_BIT);
        }

        if let Some(clap) = self.clap {
            let clap_prop = ipco.push(IpcoProp::Clap(clap)).ok_or(io::ErrorKind::InvalidInput)?;
            ipma.prop_ids.push(clap_prop | ESSENTIAL_BIT);
        }

        if let Some(pasp) = self.pasp {
            let pasp_prop = ipco.push(IpcoProp::Pasp(pasp)).ok_or(io::ErrorKind::InvalidInput)?;
            ipma.prop_ids.push(pasp_prop);
        }

        ipma_entries.push(ipma);

        if let Some(exif_data) = self.exif.as_deref() {
            let exif_id = next_item_id;
            next_item_id += 1;

            image_items.push(InfeBox {
                id: exif_id,
                typ: FourCC(*b"Exif"),
                name: "",
                content_type: "",
            });

            iloc_items.push(IlocItem {
                id: exif_id,
                extents: [IlocExtent { data: exif_data }],
            });

            irefs.push(IrefEntryBox {
                from_id: exif_id,
                to_id: color_image_id,
                typ: FourCC(*b"cdsc"),
            });
        }

        if let Some(xmp_data) = self.xmp.as_deref() {
            let xmp_id = next_item_id;
            next_item_id += 1;

            image_items.push(InfeBox {
                id: xmp_id,
                typ: FourCC(*b"mime"),
                name: "",
                content_type: "application/rdf+xml",
            });

            iloc_items.push(IlocItem {
                id: xmp_id,
                extents: [IlocExtent { data: xmp_data }],
            });

            irefs.push(IrefEntryBox {
                from_id: xmp_id,
                to_id: color_image_id,
                typ: FourCC(*b"cdsc"),
            });
        }

        if let Some(alpha_data) = alpha_av1_data {
            image_items.push(InfeBox {
                id: alpha_image_id,
                typ: FourCC(*b"av01"),
                name: "",
                content_type: "",
            });

            irefs.push(IrefEntryBox {
                from_id: alpha_image_id,
                to_id: color_image_id,
                typ: FourCC(*b"auxl"),
            });

            if self.premultiplied_alpha {
                irefs.push(IrefEntryBox {
                    from_id: color_image_id,
                    to_id: alpha_image_id,
                    typ: FourCC(*b"prem"),
                });
            }

            let av1c_alpha_prop = ipco.push(boxes::IpcoProp::Av1C(Av1CBox {
                seq_profile: if alpha_depth_bits >= 12 { 2 } else { 0 },
                seq_level_idx_0: 31,
                seq_tier_0: false,
                high_bitdepth: alpha_depth_bits >= 10,
                twelve_bit: alpha_depth_bits >= 12,
                monochrome: true,
                chroma_subsampling_x: true,
                chroma_subsampling_y: true,
                chroma_sample_position: 0,
            })).ok_or(io::ErrorKind::InvalidInput)?;

            // So pointless
            let pixi_1 = ipco.push(IpcoProp::Pixi(PixiBox {
                channels: 1,
                depth: alpha_depth_bits,
            })).ok_or(io::ErrorKind::InvalidInput)?;

            // that's a silly way to add 1 bit of information, isn't it?
            let auxc_prop = ipco.push(IpcoProp::AuxC(AuxCBox {
                urn: "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha",
            })).ok_or(io::ErrorKind::InvalidInput)?;

            ipma_entries.push(IpmaEntry {
                item_id: alpha_image_id,
                prop_ids: from_array([ispe_prop, av1c_alpha_prop | ESSENTIAL_BIT, auxc_prop, pixi_1]),
            });

            // Use interleaved color and alpha, with alpha first.
            // Makes it possible to display partial image.
            iloc_items.push(IlocItem {
                id: alpha_image_id,
                extents: [IlocExtent { data: alpha_data }],
            });
        }
        // Gain map: gain map image item (av01) + tmap derived image
        if let Some(ref gm) = self.gain_map {
            let gm_depth = gm.bit_depth;
            let gain_map_id = next_item_id;
            next_item_id += 1;
            let tmap_id = next_item_id;
            next_item_id += 1;
            let _ = next_item_id;

            // Gain map image item (av01)
            image_items.push(InfeBox {
                id: gain_map_id,
                typ: FourCC(*b"av01"),
                name: "",
                content_type: "",
            });

            // Gain map ispe (may differ from primary dimensions)
            let gm_ispe = ipco.push(IpcoProp::Ispe(IspeBox {
                width: gm.width,
                height: gm.height,
            })).ok_or(io::ErrorKind::InvalidInput)?;

            // Gain map av1C
            let gm_av1c = ipco.push(IpcoProp::Av1C(Av1CBox {
                seq_profile: if gm_depth >= 12 { 2 } else { 0 },
                seq_level_idx_0: 31,
                seq_tier_0: false,
                high_bitdepth: gm_depth >= 10,
                twelve_bit: gm_depth >= 12,
                monochrome: gm.monochrome,
                chroma_subsampling_x: gm.chroma_subsampling.horizontal,
                chroma_subsampling_y: gm.chroma_subsampling.vertical,
                chroma_sample_position: 0,
            })).ok_or(io::ErrorKind::InvalidInput)?;

            ipma_entries.push(IpmaEntry {
                item_id: gain_map_id,
                prop_ids: from_array([gm_ispe, gm_av1c | ESSENTIAL_BIT]),
            });

            // Gain map image data in iloc
            iloc_items.push(IlocItem {
                id: gain_map_id,
                extents: [IlocExtent { data: &gm.av1_data }],
            });

            // tmap derived image item
            image_items.push(InfeBox {
                id: tmap_id,
                typ: FourCC(*b"tmap"),
                name: "",
                content_type: "",
            });

            // tmap item properties: optional colr for alternate rendition
            if let Some(alt_colr) = gm.alt_colr {
                let tmap_colr = ipco.push(IpcoProp::Colr(alt_colr)).ok_or(io::ErrorKind::InvalidInput)?;
                ipma_entries.push(IpmaEntry {
                    item_id: tmap_id,
                    prop_ids: from_array([tmap_colr]),
                });
            }

            // tmap payload (ISO 21496-1 metadata) in iloc
            iloc_items.push(IlocItem {
                id: tmap_id,
                extents: [IlocExtent { data: &gm.metadata }],
            });

            // dimg reference: tmap -> [primary, gain_map]
            // Must be a single iref entry with reference_count=2 so the parser
            // assigns reference_index 0 to primary and 1 to gain_map.
            let mut to_ids = ArrayVec::new();
            to_ids.push(color_image_id);
            to_ids.push(gain_map_id);
            multi_irefs.push(IrefMultiEntryBox {
                from_id: tmap_id,
                to_ids,
                typ: FourCC(*b"dimg"),
            });
        }

        iloc_items.push(IlocItem {
            id: color_image_id,
            extents: [IlocExtent { data: color_av1_data }],
        });

        Ok(AvifFile {
            ftyp: FtypBox {
                major_brand: FourCC(*b"avif"),
                minor_version: 0,
                compatible_brands: [FourCC(*b"mif1"), FourCC(*b"miaf")].into(),
            },
            meta: MetaBox {
                hdlr: HdlrBox {},
                iinf: IinfBox { items: image_items },
                pitm: PitmBox(color_image_id),
                iloc: IlocBox {
                    absolute_offset_start: None,
                    items: iloc_items,
                },
                iprp: IprpBox {
                    ipco,
                    // It's not enough to define these properties,
                    // they must be assigned to the image
                    ipma: IpmaBox { entries: ipma_entries },
                },
                iref: IrefBox { entries: irefs, multi_entries: multi_irefs },
            },
            // Here's the actual data. If HEIF wasn't such a kitchen sink, this
            // would have been the only data this file needs.
            mdat: MdatBox,
        })
    }

    /// Panics if the input arguments were invalid. Use [`Self::write`] to handle the errors.
    #[must_use]
    #[track_caller]
    pub fn to_vec(&self, color_av1_data: &[u8], alpha_av1_data: Option<&[u8]>, width: u32, height: u32, depth_bits: u8) -> Vec<u8> {
        let mut file = self.make_boxes(color_av1_data, alpha_av1_data, width, height, depth_bits).unwrap();
        let mut out = Vec::new();
        file.write_to_vec(&mut out).unwrap();
        out
    }

    /// Set chroma subsampling. Use [`ChromaSubsampling::NONE`] for 4:4:4,
    /// [`ChromaSubsampling::YUV420`] for 4:2:0, etc.
    ///
    /// Also accepts `(bool, bool)` tuples for backward compatibility.
    ///
    /// `chroma_sample_position` is always 0. Don't use chroma subsampling with AVIF.
    #[inline]
    pub fn set_chroma_subsampling(&mut self, subsampling: impl Into<ChromaSubsampling>) -> &mut Self {
        self.chroma_subsampling = subsampling.into();
        self
    }

    /// Set whether the image is monochrome (grayscale).
    /// This is used to set the `monochrome` flag in the AV1 sequence header.
    #[inline]
    pub fn set_monochrome(&mut self, monochrome: bool) -> &mut Self {
        self.monochrome = monochrome;
        self
    }

    /// Set exif metadata to be included in the AVIF file as a separate item.
    #[inline]
    pub fn set_exif(&mut self, exif: Vec<u8>) -> &mut Self {
        self.exif = Some(exif);
        self
    }

    /// Sets minimum required
    ///
    /// Higher bit depth may increase this
    #[inline]
    pub fn set_seq_profile(&mut self, seq_profile: u8) -> &mut Self {
        self.min_seq_profile = seq_profile;
        self
    }

    #[inline]
    pub fn set_width(&mut self, width: u32) -> &mut Self {
        self.width = width;
        self
    }

    #[inline]
    pub fn set_height(&mut self, height: u32) -> &mut Self {
        self.height = height;
        self
    }

    /// 8, 10 or 12.
    #[inline]
    pub fn set_bit_depth(&mut self, bit_depth: u8) -> &mut Self {
        self.bit_depth = bit_depth;
        self
    }

    /// Set whether image's colorspace uses premultiplied alpha, i.e. RGB channels were multiplied by their alpha value,
    /// so that transparent areas are all black. Image decoders will be instructed to undo the premultiplication.
    ///
    /// Premultiplied alpha images usually compress better and tolerate heavier compression, but
    /// may not be supported correctly by less capable AVIF decoders.
    ///
    /// This just sets the configuration property. The pixel data must have already been processed before compression.
    /// If a decoder displays semitransparent colors too dark, it doesn't support premultiplied alpha.
    /// If a decoder displays semitransparent colors too bright, you didn't premultiply the colors before encoding.
    ///
    /// If you're not using premultiplied alpha, consider bleeding RGB colors into transparent areas,
    /// otherwise there may be unwanted outlines around edges of transparency.
    #[inline]
    pub fn set_premultiplied_alpha(&mut self, is_premultiplied: bool) -> &mut Self {
        self.premultiplied_alpha = is_premultiplied;
        self
    }

    #[doc(hidden)]
    pub fn premultiplied_alpha(&mut self, is_premultiplied: bool) -> &mut Self {
        self.set_premultiplied_alpha(is_premultiplied)
    }
}

#[inline(always)]
fn from_array<const L1: usize, const L2: usize, T: Copy>(array: [T; L1]) -> ArrayVec<T, L2> {
    assert!(L1 <= L2);
    let mut tmp = ArrayVec::new_const();
    let _ = tmp.try_extend_from_slice(&array);
    tmp
}

/// See [`serialize`] for description. This one makes a `Vec` instead of using `io::Write`.
#[must_use]
#[track_caller]
pub fn serialize_to_vec(color_av1_data: &[u8], alpha_av1_data: Option<&[u8]>, width: u32, height: u32, depth_bits: u8) -> Vec<u8> {
    Aviffy::new().to_vec(color_av1_data, alpha_av1_data, width, height, depth_bits)
}

#[test]
fn test_roundtrip_parse_mp4() {
    let test_img = b"av12356abc";
    let avif = serialize_to_vec(test_img, None, 10, 20, 8);

    let ctx = mp4parse::read_avif(&mut avif.as_slice(), mp4parse::ParseStrictness::Normal).unwrap();

    assert_eq!(&test_img[..], ctx.primary_item_coded_data().unwrap());
}

#[test]
fn test_roundtrip_parse_mp4_alpha() {
    let test_img = b"av12356abc";
    let test_a = b"alpha";
    let avif = serialize_to_vec(test_img, Some(test_a), 10, 20, 8);

    let ctx = mp4parse::read_avif(&mut avif.as_slice(), mp4parse::ParseStrictness::Normal).unwrap();

    assert_eq!(&test_img[..], ctx.primary_item_coded_data().unwrap());
    assert_eq!(&test_a[..], ctx.alpha_item_coded_data().unwrap());
}

#[test]
fn test_roundtrip_parse_exif() {
    let test_img = b"av12356abc";
    let test_a = b"alpha";
    let avif = Aviffy::new()
        .set_exif(b"lol".to_vec())
        .to_vec(test_img, Some(test_a), 10, 20, 8);

    let ctx = mp4parse::read_avif(&mut avif.as_slice(), mp4parse::ParseStrictness::Normal).unwrap();

    assert_eq!(&test_img[..], ctx.primary_item_coded_data().unwrap());
    assert_eq!(&test_a[..], ctx.alpha_item_coded_data().unwrap());
}

#[test]
fn test_roundtrip_parse_avif() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let test_alpha = [77, 88, 99];
    let avif = serialize_to_vec(&test_img, Some(&test_alpha), 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();

    assert_eq!(&test_img[..], parser.primary_data().unwrap().as_ref());
    assert_eq!(&test_alpha[..], parser.alpha_data().unwrap().unwrap().as_ref());
}

#[test]
fn test_roundtrip_parse_avif_colr() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let test_alpha = [77, 88, 99];
    let avif = Aviffy::new()
        .matrix_coefficients(constants::MatrixCoefficients::Bt709)
        .to_vec(&test_img, Some(&test_alpha), 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();

    assert_eq!(&test_img[..], parser.primary_data().unwrap().as_ref());
    assert_eq!(&test_alpha[..], parser.alpha_data().unwrap().unwrap().as_ref());
}

#[test]
fn premultiplied_flag() {
    let test_img = [1,2,3,4];
    let test_alpha = [55,66,77,88,99];
    let avif = Aviffy::new().premultiplied_alpha(true).to_vec(&test_img, Some(&test_alpha), 5, 5, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();

    assert!(parser.premultiplied_alpha());
    assert_eq!(&test_img[..], parser.primary_data().unwrap().as_ref());
    assert_eq!(&test_alpha[..], parser.alpha_data().unwrap().unwrap().as_ref());
}

#[test]
fn size_required() {
    assert!(Aviffy::new().set_bit_depth(10).write_slice(&mut vec![], &[], None).is_err());
}

#[test]
fn depth_required() {
    assert!(Aviffy::new().set_width(1).set_height(1).write_slice(&mut vec![], &[], None).is_err());
}

#[test]
fn clli_roundtrip() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let avif = Aviffy::new()
        .set_content_light_level(1000, 400)
        .to_vec(&test_img, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    let cll = parser.content_light_level().expect("clli box should be present");
    assert_eq!(cll.max_content_light_level, 1000);
    assert_eq!(cll.max_pic_average_light_level, 400);
}

#[test]
fn mdcv_roundtrip() {
    let test_img = [1, 2, 3, 4, 5, 6];
    // BT.2020 primaries (standard encoding: CIE xy × 50000)
    let primaries = [
        (8500, 39850),   // green
        (6550, 2300),    // blue
        (35400, 14600),  // red
    ];
    let white_point = (15635, 16450); // D65
    let max_luminance = 10_000_000; // 1000 cd/m²
    let min_luminance = 1;          // 0.0001 cd/m²

    let avif = Aviffy::new()
        .set_mastering_display(primaries, white_point, max_luminance, min_luminance)
        .to_vec(&test_img, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    let mdcv = parser.mastering_display().expect("mdcv box should be present");
    assert_eq!(mdcv.primaries, primaries);
    assert_eq!(mdcv.white_point, white_point);
    assert_eq!(mdcv.max_luminance, max_luminance);
    assert_eq!(mdcv.min_luminance, min_luminance);
}

#[test]
fn hdr10_full_metadata() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let test_alpha = [77, 88, 99];
    let primaries = [
        (8500, 39850),
        (6550, 2300),
        (35400, 14600),
    ];
    let white_point = (15635, 16450);

    let avif = Aviffy::new()
        .set_transfer_characteristics(constants::TransferCharacteristics::Smpte2084)
        .set_color_primaries(constants::ColorPrimaries::Bt2020)
        .set_matrix_coefficients(constants::MatrixCoefficients::Bt2020Ncl)
        .set_content_light_level(4000, 1000)
        .set_mastering_display(primaries, white_point, 40_000_000, 50)
        .to_vec(&test_img, Some(&test_alpha), 10, 20, 10);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();

    // Verify CLLI
    let cll = parser.content_light_level().expect("clli box should be present");
    assert_eq!(cll.max_content_light_level, 4000);
    assert_eq!(cll.max_pic_average_light_level, 1000);

    // Verify MDCV
    let mdcv = parser.mastering_display().expect("mdcv box should be present");
    assert_eq!(mdcv.primaries, primaries);
    assert_eq!(mdcv.white_point, white_point);
    assert_eq!(mdcv.max_luminance, 40_000_000);
    assert_eq!(mdcv.min_luminance, 50);

    // Verify data integrity
    assert_eq!(parser.primary_data().unwrap().as_ref(), &test_img[..]);
    assert_eq!(parser.alpha_data().unwrap().unwrap().as_ref(), &test_alpha[..]);
}

#[test]
fn no_hdr_metadata_by_default() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let avif = serialize_to_vec(&test_img, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    assert!(parser.content_light_level().is_none());
    assert!(parser.mastering_display().is_none());
}

#[test]
fn rotation_roundtrip() {
    let test_img = [1, 2, 3, 4, 5, 6];
    for angle in 0..4u8 {
        let avif = Aviffy::new()
            .set_rotation(angle)
            .to_vec(&test_img, None, 10, 20, 8);

        let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
        let rot = parser.rotation().expect("irot box should be present");
        let expected_angle = match angle {
            0 => 0,
            1 => 90,
            2 => 180,
            3 => 270,
            _ => unreachable!(),
        };
        assert_eq!(rot.angle, expected_angle, "angle code {angle}");

        // Verify data still parses
        assert_eq!(parser.primary_data().unwrap().as_ref(), &test_img[..]);
    }
}

#[test]
fn mirror_roundtrip() {
    let test_img = [1, 2, 3, 4, 5, 6];
    for axis in 0..2u8 {
        let avif = Aviffy::new()
            .set_mirror(axis)
            .to_vec(&test_img, None, 10, 20, 8);

        let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
        let mir = parser.mirror().expect("imir box should be present");
        assert_eq!(mir.axis, axis);
    }
}

#[test]
fn clap_roundtrip() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let avif = Aviffy::new()
        .set_clean_aperture(ClapBox {
            width_n: 800, width_d: 1,
            height_n: 600, height_d: 1,
            horiz_off_n: 0, horiz_off_d: 1,
            vert_off_n: 0, vert_off_d: 1,
        })
        .to_vec(&test_img, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    let clap = parser.clean_aperture().expect("clap box should be present");
    assert_eq!(clap.width_n, 800);
    assert_eq!(clap.width_d, 1);
    assert_eq!(clap.height_n, 600);
    assert_eq!(clap.height_d, 1);
    assert_eq!(clap.horiz_off_n, 0);
    assert_eq!(clap.horiz_off_d, 1);
    assert_eq!(clap.vert_off_n, 0);
    assert_eq!(clap.vert_off_d, 1);
}

#[test]
fn clap_with_negative_offset() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let avif = Aviffy::new()
        .set_clean_aperture(ClapBox {
            width_n: 640, width_d: 1,
            height_n: 480, height_d: 1,
            horiz_off_n: -10, horiz_off_d: 1,
            vert_off_n: -20, vert_off_d: 1,
        })
        .to_vec(&test_img, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    let clap = parser.clean_aperture().expect("clap box should be present");
    assert_eq!(clap.horiz_off_n, -10);
    assert_eq!(clap.vert_off_n, -20);
}

#[test]
fn pasp_roundtrip() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let avif = Aviffy::new()
        .set_pixel_aspect_ratio(2, 1)
        .to_vec(&test_img, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    let pasp = parser.pixel_aspect_ratio().expect("pasp box should be present");
    assert_eq!(pasp.h_spacing, 2);
    assert_eq!(pasp.v_spacing, 1);
}

#[test]
fn icc_profile_roundtrip() {
    let test_img = [1, 2, 3, 4, 5, 6];
    // Fake ICC profile data (real profiles are much larger, but any bytes work for roundtrip)
    let fake_icc = vec![0x00, 0x00, 0x00, 0x18, b'a', b'c', b's', b'p',
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    let avif = Aviffy::new()
        .set_icc_profile(fake_icc.clone())
        .to_vec(&test_img, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    let color_info = parser.color_info().expect("colr box should be present");
    match color_info {
        zenavif_parse::ColorInformation::IccProfile(data) => {
            assert_eq!(data.as_slice(), &fake_icc[..]);
        }
        _ => panic!("expected ICC profile color info, got {:?}", color_info),
    }
}

#[test]
fn icc_overrides_nclx() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let fake_icc = vec![1, 2, 3, 4];
    // Setting both ICC and nclx: ICC should win
    let avif = Aviffy::new()
        .set_color_primaries(constants::ColorPrimaries::Bt2020)
        .set_icc_profile(fake_icc.clone())
        .to_vec(&test_img, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    match parser.color_info() {
        Some(zenavif_parse::ColorInformation::IccProfile(data)) => {
            assert_eq!(data.as_slice(), &fake_icc[..]);
        }
        other => panic!("expected ICC profile, got {:?}", other),
    }
}

#[test]
fn xmp_roundtrip() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let xmp_data = b"<x:xmpmeta xmlns:x='adobe:ns:meta/'><test/></x:xmpmeta>".to_vec();

    let avif = Aviffy::new()
        .set_xmp(xmp_data.clone())
        .to_vec(&test_img, None, 10, 20, 8);

    // Verify the primary data is intact
    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    assert_eq!(parser.primary_data().unwrap().as_ref(), &test_img[..]);

    // Verify XMP data is somewhere in the file
    let xmp_str = b"<x:xmpmeta";
    assert!(avif.windows(xmp_str.len()).any(|w| w == xmp_str),
        "XMP data should be present in AVIF file");
}

#[test]
fn rotation_and_mirror_combined() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let avif = Aviffy::new()
        .set_rotation(1)  // 90° CCW
        .set_mirror(0)    // vertical axis
        .to_vec(&test_img, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    let rot = parser.rotation().expect("irot should be present");
    let mir = parser.mirror().expect("imir should be present");
    assert_eq!(rot.angle, 90);
    assert_eq!(mir.axis, 0);
}

#[test]
fn all_properties_combined() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let test_alpha = [77, 88, 99];
    let avif = Aviffy::new()
        .set_rotation(2)
        .set_mirror(1)
        .set_clean_aperture(ClapBox {
            width_n: 8, width_d: 1,
            height_n: 18, height_d: 1,
            horiz_off_n: 0, horiz_off_d: 1,
            vert_off_n: 0, vert_off_d: 1,
        })
        .set_pixel_aspect_ratio(1, 1)
        .set_content_light_level(1000, 400)
        .set_mastering_display(
            [(8500, 39850), (6550, 2300), (35400, 14600)],
            (15635, 16450), 10_000_000, 50,
        )
        .to_vec(&test_img, Some(&test_alpha), 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    assert_eq!(parser.rotation().unwrap().angle, 180);
    assert_eq!(parser.mirror().unwrap().axis, 1);
    assert!(parser.clean_aperture().is_some());
    assert!(parser.pixel_aspect_ratio().is_some());
    assert!(parser.content_light_level().is_some());
    assert!(parser.mastering_display().is_some());

    assert_eq!(parser.primary_data().unwrap().as_ref(), &test_img[..]);
    assert_eq!(parser.alpha_data().unwrap().unwrap().as_ref(), &test_alpha[..]);
}

// Tests using avif-parse (upstream parser) to verify broad compatibility.
// avif-parse v2.0 only exposes primary_item, alpha_item, premultiplied_alpha,
// clli, and mdcv. For other features we verify it at least parses without panic.

/// Helper: parse with avif-parse, return AvifData. Panics on parse failure.
#[cfg(test)]
fn parse_with_avif_parse(avif: &[u8]) -> avif_parse::AvifData {
    avif_parse::read_avif(&mut &*avif)
        .unwrap_or_else(|e| panic!("avif-parse failed to parse: {e:?}"))
}

#[test]
fn avif_parse_basic_roundtrip() {
    let color = b"av1colordata";
    let avif = serialize_to_vec(color, None, 10, 20, 8);
    let parsed = parse_with_avif_parse(&avif);
    assert_eq!(parsed.primary_item.as_slice(), &color[..]);
    assert!(parsed.alpha_item.is_none());
}

#[test]
fn avif_parse_alpha_roundtrip() {
    let color = b"av1colordata";
    let alpha = b"alphadata";
    let avif = serialize_to_vec(color, Some(alpha), 10, 20, 8);
    let parsed = parse_with_avif_parse(&avif);
    assert_eq!(parsed.primary_item.as_slice(), &color[..]);
    assert_eq!(parsed.alpha_item.unwrap().as_slice(), &alpha[..]);
}

#[test]
fn avif_parse_premultiplied_alpha() {
    let color = [1, 2, 3, 4];
    let alpha = [55, 66, 77, 88];
    let avif = Aviffy::new().premultiplied_alpha(true)
        .to_vec(&color, Some(&alpha), 5, 5, 8);
    let parsed = parse_with_avif_parse(&avif);
    assert!(parsed.premultiplied_alpha);
    assert_eq!(parsed.primary_item.as_slice(), &color[..]);
    assert_eq!(parsed.alpha_item.unwrap().as_slice(), &alpha[..]);
}

#[test]
fn avif_parse_clli_roundtrip() {
    let img = [1, 2, 3, 4, 5, 6];
    let avif = Aviffy::new()
        .set_content_light_level(1000, 400)
        .to_vec(&img, None, 10, 20, 8);
    let parsed = parse_with_avif_parse(&avif);
    let cll = parsed.content_light_level.expect("clli should be present");
    assert_eq!(cll.max_content_light_level, 1000);
    assert_eq!(cll.max_pic_average_light_level, 400);
}

#[test]
fn avif_parse_mdcv_roundtrip() {
    let img = [1, 2, 3, 4, 5, 6];
    let primaries = [(8500, 39850), (6550, 2300), (35400, 14600)];
    let avif = Aviffy::new()
        .set_mastering_display(primaries, (15635, 16450), 10_000_000, 50)
        .to_vec(&img, None, 10, 20, 8);
    let parsed = parse_with_avif_parse(&avif);
    let mdcv = parsed.mastering_display.expect("mdcv should be present");
    assert_eq!(mdcv.primaries, primaries);
    assert_eq!(mdcv.white_point, (15635, 16450));
    assert_eq!(mdcv.max_luminance, 10_000_000);
    assert_eq!(mdcv.min_luminance, 50);
}

#[test]
fn avif_parse_hdr10_full() {
    let img = [1, 2, 3, 4, 5, 6];
    let alpha = [77, 88, 99];
    let avif = Aviffy::new()
        .set_transfer_characteristics(constants::TransferCharacteristics::Smpte2084)
        .set_color_primaries(constants::ColorPrimaries::Bt2020)
        .set_matrix_coefficients(constants::MatrixCoefficients::Bt2020Ncl)
        .set_content_light_level(4000, 1000)
        .set_mastering_display(
            [(8500, 39850), (6550, 2300), (35400, 14600)],
            (15635, 16450), 40_000_000, 50,
        )
        .to_vec(&img, Some(&alpha), 10, 20, 10);
    let parsed = parse_with_avif_parse(&avif);
    assert_eq!(parsed.primary_item.as_slice(), &img[..]);
    assert_eq!(parsed.alpha_item.unwrap().as_slice(), &alpha[..]);
    assert!(parsed.content_light_level.is_some());
    assert!(parsed.mastering_display.is_some());
}

// The following tests verify avif-parse doesn't crash on features it
// doesn't expose fields for (transforms, metadata, animation, grid).

#[test]
fn avif_parse_survives_rotation() {
    let img = [1, 2, 3, 4, 5, 6];
    for angle in 0..4u8 {
        let avif = Aviffy::new().set_rotation(angle).to_vec(&img, None, 10, 20, 8);
        let parsed = parse_with_avif_parse(&avif);
        assert_eq!(parsed.primary_item.as_slice(), &img[..]);
    }
}

#[test]
fn avif_parse_survives_mirror() {
    let img = [1, 2, 3, 4, 5, 6];
    for axis in 0..2u8 {
        let avif = Aviffy::new().set_mirror(axis).to_vec(&img, None, 10, 20, 8);
        let parsed = parse_with_avif_parse(&avif);
        assert_eq!(parsed.primary_item.as_slice(), &img[..]);
    }
}

#[test]
fn avif_parse_survives_clap() {
    let img = [1, 2, 3, 4, 5, 6];
    let avif = Aviffy::new()
        .set_clean_aperture(ClapBox {
            width_n: 800, width_d: 1,
            height_n: 600, height_d: 1,
            horiz_off_n: 0, horiz_off_d: 1,
            vert_off_n: 0, vert_off_d: 1,
        })
        .to_vec(&img, None, 10, 20, 8);
    let parsed = parse_with_avif_parse(&avif);
    assert_eq!(parsed.primary_item.as_slice(), &img[..]);
}

#[test]
fn avif_parse_survives_pasp() {
    let img = [1, 2, 3, 4, 5, 6];
    let avif = Aviffy::new().set_pixel_aspect_ratio(2, 1).to_vec(&img, None, 10, 20, 8);
    let parsed = parse_with_avif_parse(&avif);
    assert_eq!(parsed.primary_item.as_slice(), &img[..]);
}

#[test]
fn avif_parse_survives_icc_profile() {
    let img = [1, 2, 3, 4, 5, 6];
    let icc = vec![0, 0, 0, 24, b'a', b'c', b's', b'p', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    let avif = Aviffy::new().set_icc_profile(icc).to_vec(&img, None, 10, 20, 8);
    let parsed = parse_with_avif_parse(&avif);
    assert_eq!(parsed.primary_item.as_slice(), &img[..]);
}

#[test]
fn avif_parse_survives_exif() {
    let img = b"av1colordata";
    let avif = Aviffy::new()
        .set_exif(b"exifdata".to_vec())
        .to_vec(img, None, 10, 20, 8);
    let parsed = parse_with_avif_parse(&avif);
    assert_eq!(parsed.primary_item.as_slice(), &img[..]);
}

#[test]
fn avif_parse_survives_xmp() {
    let img = [1, 2, 3, 4, 5, 6];
    let avif = Aviffy::new()
        .set_xmp(b"<x:xmpmeta/>".to_vec())
        .to_vec(&img, None, 10, 20, 8);
    let parsed = parse_with_avif_parse(&avif);
    assert_eq!(parsed.primary_item.as_slice(), &img[..]);
}

#[test]
fn avif_parse_survives_all_properties() {
    let img = [1, 2, 3, 4, 5, 6];
    let alpha = [77, 88, 99];
    let avif = Aviffy::new()
        .set_rotation(2)
        .set_mirror(1)
        .set_clean_aperture(ClapBox {
            width_n: 8, width_d: 1,
            height_n: 18, height_d: 1,
            horiz_off_n: 0, horiz_off_d: 1,
            vert_off_n: 0, vert_off_d: 1,
        })
        .set_pixel_aspect_ratio(1, 1)
        .set_content_light_level(1000, 400)
        .set_mastering_display(
            [(8500, 39850), (6550, 2300), (35400, 14600)],
            (15635, 16450), 10_000_000, 50,
        )
        .set_exif(b"exif".to_vec())
        .set_xmp(b"<xmp/>".to_vec())
        .to_vec(&img, Some(&alpha), 10, 20, 8);
    let parsed = parse_with_avif_parse(&avif);
    assert_eq!(parsed.primary_item.as_slice(), &img[..]);
    assert_eq!(parsed.alpha_item.unwrap().as_slice(), &alpha[..]);
    assert!(parsed.content_light_level.is_some());
    assert!(parsed.mastering_display.is_some());
}

#[test]
fn avif_parse_survives_animated() {
    use crate::animated::{AnimatedImage, AnimFrame};
    let mut anim = AnimatedImage::new();
    anim.set_timescale(1000);
    anim.set_color_config(boxes::Av1CBox {
        seq_profile: 0, seq_level_idx_0: 4, seq_tier_0: false,
        high_bitdepth: false, twelve_bit: false, monochrome: false,
        chroma_subsampling_x: true, chroma_subsampling_y: true,
        chroma_sample_position: 0,
    });
    let frames = [
        AnimFrame::new(b"frame0data", 33).with_sync(true),
        AnimFrame::new(b"frame1data", 33),
    ];
    let avif = anim.serialize(320, 240, &frames, b"seqhdr", None);
    // avif-parse may or may not support avis brand, but must not crash
    let _ = avif_parse::read_avif(&mut avif.as_slice());
}

#[test]
fn avif_parse_survives_grid() {
    use crate::grid::GridImage;
    let tiles: Vec<Vec<u8>> = (0..4).map(|i| vec![i as u8; 100]).collect();
    let tile_refs: Vec<&[u8]> = tiles.iter().map(|t| t.as_slice()).collect();

    let mut grid = GridImage::new();
    grid.set_color_config(boxes::Av1CBox {
        seq_profile: 0, seq_level_idx_0: 4, seq_tier_0: false,
        high_bitdepth: false, twelve_bit: false, monochrome: false,
        chroma_subsampling_x: true, chroma_subsampling_y: true,
        chroma_sample_position: 0,
    });
    let avif = grid.serialize(2, 2, 200, 200, 100, 100, &tile_refs, None).unwrap();
    // avif-parse may or may not support grid, but must not crash
    let _ = avif_parse::read_avif(&mut avif.as_slice());
}

// ─── Gain map / tmap tests ───────────────────────────────────────────

/// Build a minimal ISO 21496-1 metadata blob (single-channel).
///
/// Format: version(u8) + minimum_version(u16) + writer_version(u16) + flags(u8)
///   + base_hdr_headroom(u32×2) + alternate_hdr_headroom(u32×2)
///   + per-channel: gain_map_min(i32+u32) + gain_map_max(i32+u32)
///     + gamma(u32×2) + base_offset(i32+u32) + alternate_offset(i32+u32)
#[cfg(test)]
fn make_test_tmap_metadata(
    is_multichannel: bool,
    use_base_colour_space: bool,
    base_headroom_n: u32,
    base_headroom_d: u32,
    alt_headroom_n: u32,
    alt_headroom_d: u32,
) -> Vec<u8> {
    let mut buf = Vec::new();
    buf.push(0); // version
    buf.extend_from_slice(&0u16.to_be_bytes()); // minimum_version
    buf.extend_from_slice(&0u16.to_be_bytes()); // writer_version
    let flags = (u8::from(is_multichannel) << 7) | (u8::from(use_base_colour_space) << 6);
    buf.push(flags);

    buf.extend_from_slice(&base_headroom_n.to_be_bytes());
    buf.extend_from_slice(&base_headroom_d.to_be_bytes());
    buf.extend_from_slice(&alt_headroom_n.to_be_bytes());
    buf.extend_from_slice(&alt_headroom_d.to_be_bytes());

    let channel_count = if is_multichannel { 3 } else { 1 };
    for _ in 0..channel_count {
        // gain_map_min = 0/1
        buf.extend_from_slice(&0i32.to_be_bytes());
        buf.extend_from_slice(&1u32.to_be_bytes());
        // gain_map_max = 1/1
        buf.extend_from_slice(&1i32.to_be_bytes());
        buf.extend_from_slice(&1u32.to_be_bytes());
        // gamma = 1/1
        buf.extend_from_slice(&1u32.to_be_bytes());
        buf.extend_from_slice(&1u32.to_be_bytes());
        // base_offset = 0/1
        buf.extend_from_slice(&0i32.to_be_bytes());
        buf.extend_from_slice(&1u32.to_be_bytes());
        // alternate_offset = 0/1
        buf.extend_from_slice(&0i32.to_be_bytes());
        buf.extend_from_slice(&1u32.to_be_bytes());
    }
    buf
}

#[test]
fn gain_map_roundtrip() {
    let primary_data = b"primary_av1_data";
    let gain_map_data = b"gain_map_av1_data";
    let metadata = make_test_tmap_metadata(false, true, 0, 1, 1, 1);

    let avif = Aviffy::new()
        .set_gain_map(gain_map_data.to_vec(), 4, 4, 8, metadata.clone())
        .to_vec(primary_data, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();

    // Primary data intact
    assert_eq!(parser.primary_data().unwrap().as_ref(), &primary_data[..]);

    // Gain map metadata should be detected
    let gm_meta = parser.gain_map_metadata().expect("gain map metadata should be present");
    assert!(!gm_meta.is_multichannel);
    assert!(gm_meta.use_base_colour_space);
    assert_eq!(gm_meta.base_hdr_headroom_n, 0);
    assert_eq!(gm_meta.base_hdr_headroom_d, 1);
    assert_eq!(gm_meta.alternate_hdr_headroom_n, 1);
    assert_eq!(gm_meta.alternate_hdr_headroom_d, 1);

    // Gain map image data should be extractable
    let gm_data = parser.gain_map_data().expect("gain map data should be present").unwrap();
    assert_eq!(gm_data.as_ref(), &gain_map_data[..]);
}

#[test]
fn gain_map_with_alpha() {
    let primary_data = b"primary_av1";
    let alpha_data = b"alpha_av1";
    let gain_map_data = b"gm_av1_data";
    let metadata = make_test_tmap_metadata(false, true, 0, 1, 1, 1);

    let avif = Aviffy::new()
        .set_gain_map(gain_map_data.to_vec(), 4, 4, 8, metadata)
        .to_vec(primary_data, Some(alpha_data), 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();

    // Primary + alpha data intact
    assert_eq!(parser.primary_data().unwrap().as_ref(), &primary_data[..]);
    assert_eq!(parser.alpha_data().unwrap().unwrap().as_ref(), &alpha_data[..]);

    // Gain map detected
    let gm_data = parser.gain_map_data().expect("gain map data present").unwrap();
    assert_eq!(gm_data.as_ref(), &gain_map_data[..]);
    assert!(parser.gain_map_metadata().is_some());
}

#[test]
fn gain_map_multichannel_metadata() {
    let primary_data = [1, 2, 3, 4, 5, 6];
    let gain_map_data = [10, 20, 30, 40];
    let metadata = make_test_tmap_metadata(true, false, 0, 1, 3, 1);

    let avif = Aviffy::new()
        .set_gain_map(gain_map_data.to_vec(), 2, 2, 8, metadata.clone())
        .to_vec(&primary_data, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    let gm_meta = parser.gain_map_metadata().expect("gain map metadata present");
    assert!(gm_meta.is_multichannel);
    assert!(!gm_meta.use_base_colour_space);
    assert_eq!(gm_meta.alternate_hdr_headroom_n, 3);
}

#[test]
fn gain_map_metadata_field_exact() {
    // Known metadata blob -> embed -> extract -> verify field-by-field
    let primary_data = [1, 2, 3, 4];
    let gain_map_data = [99, 88, 77];
    let metadata = make_test_tmap_metadata(false, true, 0, 1, 6, 1);

    let avif = Aviffy::new()
        .set_gain_map(gain_map_data.to_vec(), 1, 1, 8, metadata.clone())
        .to_vec(&primary_data, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    let gm_meta = parser.gain_map_metadata().expect("gain map metadata present");

    assert_eq!(gm_meta.alternate_hdr_headroom_n, 6);
    assert_eq!(gm_meta.alternate_hdr_headroom_d, 1);
    assert_eq!(gm_meta.channels[0].gain_map_min_n, 0);
    assert_eq!(gm_meta.channels[0].gain_map_min_d, 1);
    assert_eq!(gm_meta.channels[0].gain_map_max_n, 1);
    assert_eq!(gm_meta.channels[0].gain_map_max_d, 1);
    assert_eq!(gm_meta.channels[0].gamma_n, 1);
    assert_eq!(gm_meta.channels[0].gamma_d, 1);
    assert_eq!(gm_meta.channels[0].base_offset_n, 0);
    assert_eq!(gm_meta.channels[0].base_offset_d, 1);
    assert_eq!(gm_meta.channels[0].alternate_offset_n, 0);
    assert_eq!(gm_meta.channels[0].alternate_offset_d, 1);
}

#[test]
fn gain_map_alt_colr_roundtrip() {
    let primary_data = [1, 2, 3, 4, 5, 6];
    let gain_map_data = [10, 20, 30];
    let metadata = make_test_tmap_metadata(false, true, 0, 1, 1, 1);

    let alt_colr = ColrBox {
        color_primaries: constants::ColorPrimaries::Bt2020,
        transfer_characteristics: constants::TransferCharacteristics::Smpte2084,
        matrix_coefficients: constants::MatrixCoefficients::Bt2020Ncl,
        full_range_flag: false,
    };

    let avif = Aviffy::new()
        .set_gain_map(gain_map_data.to_vec(), 2, 2, 8, metadata)
        .set_gain_map_alt_colr(alt_colr)
        .to_vec(&primary_data, None, 10, 20, 8);

    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();

    // Gain map data intact
    let gm_data = parser.gain_map_data().expect("gain map data present").unwrap();
    assert_eq!(gm_data.as_ref(), &gain_map_data[..]);

    // Verify alternate color info
    let alt = parser.gain_map_color_info().expect("alt color info should be present");
    match alt {
        zenavif_parse::ColorInformation::Nclx {
            color_primaries,
            transfer_characteristics,
            matrix_coefficients,
            full_range,
        } => {
            assert_eq!(*color_primaries, 9); // BT.2020
            assert_eq!(*transfer_characteristics, 16); // PQ
            assert_eq!(*matrix_coefficients, 9); // BT.2020 NCL
            assert!(!full_range);
        }
        other => panic!("expected NCLX color info, got: {:?}", other),
    }
}

#[test]
fn no_gain_map_by_default() {
    let test_img = [1, 2, 3, 4, 5, 6];
    let avif = serialize_to_vec(&test_img, None, 10, 20, 8);
    let parser = zenavif_parse::AvifParser::from_bytes(&avif).unwrap();
    assert!(parser.gain_map_metadata().is_none(), "no gain map metadata by default");
    assert!(parser.gain_map_data().is_none(), "no gain map data by default");
}

#[test]
fn avif_parse_survives_gain_map() {
    let primary_data = b"primary_color";
    let gain_map_data = b"gain_map_pixels";
    let metadata = make_test_tmap_metadata(false, true, 0, 1, 1, 1);

    let avif = Aviffy::new()
        .set_gain_map(gain_map_data.to_vec(), 4, 4, 8, metadata)
        .to_vec(primary_data, None, 10, 20, 8);

    // avif-parse (older parser) must not crash
    let _ = avif_parse::read_avif(&mut avif.as_slice());
}