rust-j2k 0.5.0

Decodes JPEG 2000 images in pure Rust, for programs that need to read them without linking a C library.
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
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
//! Stage 2 — Tier-2 packet decoding (ISO/IEC 15444-1 Annex B).
//!
//! Tile-part data is a sequence of *packets*, one per (resolution, layer,
//! component, precinct) in the header's progression order. Each packet header
//! says, via [tag trees](tagtree), which code-blocks are included, how many
//! bit-planes are all-zero, how many coding passes each contributes, and the
//! byte length of each contribution. This stage parses that structure and
//! hands Tier-1 the coded byte segments per code-block — it does **not** run
//! the arithmetic decoder.
//!
//! This stage runs once per **tile**: a tile's packets are its own, in its own
//! progression, and nothing about them crosses a tile boundary. The packet
//! stream walks four axes — layer, resolution, component, and *position* (the
//! precinct) — in the order that tile's `COD` names, or, when a `POC` marker is
//! present, through the sequence of progression volumes it prescribes. The
//! tile's data is then `header₀ body₀ header₁ body₁ …` with no padding, and the
//! packets must tile it exactly; a leftover byte means a misread field.
//!
//! A code-block's contributions accumulate across the layers that include it,
//! so a precinct's tag trees and per-block state outlive any one packet. See
//! [`for_each_packet`] for the orders and [`BlockState`] for what persists.
//!
//! `decode_packets_metered` also computes the resolution / subband /
//! code-block geometry from the [`MainHeader`], so it is the single source of
//! truth the assembly stage reuses.

pub mod bio;
mod geometry;
mod progression;
pub(crate) mod resume;
pub mod spans;
pub mod tagtree;

use crate::codestream::markers::Rect;
use crate::codestream::markers::marker;
use crate::codestream::{MainHeader, Tile};
use crate::{Error, Result};
use bio::BitReader;
use spans::UnreadSpans;
use std::borrow::Cow;
use std::cell::Cell;
use std::ops::Range;
use tagtree::{Saved, TagTree};

pub(crate) use geometry::{
    BandGeom, MAX_CODE_BLOCKS, MAX_PRECINCTS, PrecinctGeom, ResolutionGeom, resolution_geoms,
};
pub(crate) use progression::{PacketWalk, for_each_packet};

/// The four subband orientations. Kept Tier-2-local so this stage stays
/// independent of Tier-1; the assembly stage maps it to the Tier-1
/// `Orientation` that selects the zero-coding context table.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BandKind {
    /// Low-pass both axes (the coarsest resolution only).
    Ll,
    /// High-pass horizontally, low-pass vertically.
    Hl,
    /// Low-pass horizontally, high-pass vertically.
    Lh,
    /// High-pass both axes.
    Hh,
}

/// Which block coder a code-block was coded with.
///
/// A tile-component's code-block style fixes it for every block, except
/// under MIXED (style bits `0x40` and `0x80`, ISO/IEC 15444-15 A.4), where
/// each block is an HT block or a Part 1 one. Tier-2 tells them apart by the
/// block's first nonzero length field (see [`mixed_first_length`]), since the
/// two read their later length fields differently.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Coder {
    /// The Part 1 block coder (ISO/IEC 15444-1 Annex D).
    Part1,
    /// The HTJ2K block coder (ISO/IEC 15444-15).
    Ht,
}

impl Coder {
    /// The coder of every code-block of a tile-component with code-block
    /// style `style`, or `None` under MIXED, where each block has its own.
    /// `ccap15` is the codestream's `Ccap15` word: when it says every
    /// code-block is an HT block (bits 15-14 are `00`, A.3.2), a MIXED style
    /// holds only HT blocks, as A.3.2's note allows.
    pub(crate) fn of_style(
        style: u8,
        ccap15: Option<crate::codestream::markers::Ccap15>,
    ) -> Option<Coder> {
        use crate::codestream::markers::code_block_style::{HT, HT_MIXED};
        if style & HT == 0 {
            Some(Coder::Part1)
        } else if style & HT_MIXED == 0 || ccap15.is_some_and(|c| c.ht_only()) {
            Some(Coder::Ht)
        } else {
            None
        }
    }
}

/// One code-block's coded contribution within its subband. `num_passes` is the
/// coding-pass count from the packet headers (0 if no layer the decode keeps
/// includes the block) and `zero_bit_planes` the all-zero most-significant
/// bit-plane count from the zero-bitplane tag tree. `segments` are the raw
/// coded byte slices Tier-1 decodes (none when the block is not included).
///
/// An HT block's placeholder passes are in none of these: they code no bytes,
/// and each three of them add one to `zero_bit_planes` instead (ISO/IEC
/// 15444-15 B.3). Its segments are a cleanup segment, then, if it has
/// refinement passes, a refinement segment.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodeBlock<'a> {
    /// Top-left of the block within its subband, in band-relative samples.
    pub x: usize,
    pub y: usize,
    pub width: usize,
    pub height: usize,
    /// Coding passes accumulated over every layer the decode keeps that
    /// contributed.
    pub num_passes: u32,
    pub zero_bit_planes: u32,
    /// The block's codeword segments, in order. Tier-1 restarts the MQ decoder
    /// at each one and carries its bit-plane state across them.
    pub segments: Vec<CodedSegment<'a>>,
    /// Whether the last segment may be cut short: a layer the decode drops
    /// adds coding passes to it, or, with `strict` off, a later packet of the
    /// block's precinct, which may add some, is missing: the data ends
    /// before it or the `POC` volumes leave it out. Either
    /// way the encoder may have terminated it past the passes and bytes kept
    /// here, so Tier-1 does not hold it to `predictable termination`. Only
    /// the last segment can be cut, since a layer adds passes and bytes to
    /// the last segment or to new ones.
    pub cut: bool,
    /// The placeholder passes of an HT block in the layers the decode keeps,
    /// which `num_passes` leaves out. Always 0 for a Part 1 block.
    pub placeholders: u32,
    /// The block's coder. A block of a MIXED tile-component whose length
    /// fields are all zero is an HT block of placeholder passes only, which
    /// codes nothing (see [`mixed_first_length`]).
    pub coder: Coder,
    /// With `strict` off, the first layer whose packet of the block's
    /// precinct was not read whole; `None` when every packet of it was read,
    /// and always when strict.
    pub missing_from: Option<u32>,
}

/// One codeword segment: the coding passes it carries and the bytes coding
/// them, which a multi-layer codestream may deliver in several chunks.
///
/// Without per-pass termination a block has exactly one segment, and the MQ
/// codeword runs continuously across every layer that contributed to it. Under
/// `restart` (`termall`) each coding pass is terminated separately, so each is
/// its own segment and Tier-1 must re-initialize the MQ decoder for it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodedSegment<'a> {
    pub passes: u32,
    pub chunks: Vec<&'a [u8]>,
}

impl<'a> CodedSegment<'a> {
    /// The segment's bytes, concatenated across the layers that carried them.
    /// A segment of one chunk is borrowed from the codestream, not copied.
    pub fn bytes(&self) -> Cow<'a, [u8]> {
        match self.chunks[..] {
            [] => Cow::Borrowed(&[]),
            [chunk] => Cow::Borrowed(chunk),
            _ => Cow::Owned(self.chunks.concat()),
        }
    }
}

/// One subband: its orientation, tile-component origin, sample geometry, and the
/// code-block grid carrying the coded segments. Blocks are row-major, so block
/// `(i, j)` is `blocks[j * block_cols + i]`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Subband<'a> {
    pub kind: BandKind,
    /// Tile-component coordinate of the band's top-left sample.
    pub origin: (u32, u32),
    pub width: usize,
    pub height: usize,
    pub block_cols: usize,
    pub block_rows: usize,
    pub blocks: Vec<CodeBlock<'a>>,
}

/// One resolution level's subbands, in packet order: `[Ll]` at the coarsest
/// level, `[Hl, Lh, Hh]` at every finer level, or none when the resolution
/// holds no precinct.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Resolution<'a> {
    pub subbands: Vec<Subband<'a>>,
}

/// One component's coded data, grouped by resolution (coarsest first, index 0
/// the `NLLL` band) so Tier-1 can decode each block independently and the
/// assembly stage can place it back.
///
/// A resolution that holds no precinct lists no subbands: each is empty and
/// owns no code-block. The inverse DWT still needs their origins and sizes,
/// which [`band`](Self::band) gives from `rect`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ComponentCoded<'a> {
    pub resolutions: Vec<Resolution<'a>>,
    /// The tile-component's rectangle on its own grid, as the walk saw it.
    /// `None` for coded data built by hand.
    pub rect: Option<Rect>,
}

/// Where a subband sits and how big it is: [`Subband`] without its blocks.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BandExtent {
    pub origin: (u32, u32),
    pub width: usize,
    pub height: usize,
}

impl BandExtent {
    /// The extent of a band on `[x0, x1) x [y0, y1)`, empty where the bounds
    /// meet or cross.
    fn of_bounds(x0: i64, x1: i64, y0: i64, y1: i64) -> Self {
        BandExtent {
            origin: (x0.max(0) as u32, y0.max(0) as u32),
            width: (x1 - x0).max(0) as usize,
            height: (y1 - y0).max(0) as usize,
        }
    }
}

impl ComponentCoded<'_> {
    /// The origin and size of the `kind` band of resolution `r`: what the
    /// resolution lists or, when it lists none because it holds no precinct,
    /// what its rectangle gives (see [`geometry::band_rects`]). `None` for a
    /// band that does not exist.
    pub fn band(&self, r: usize, kind: BandKind) -> Option<BandExtent> {
        let resolution = self.resolutions.get(r)?;
        if let Some(sb) = resolution.subbands.iter().find(|s| s.kind == kind) {
            return Some(BandExtent {
                origin: sb.origin,
                width: sb.width,
                height: sb.height,
            });
        }
        if !resolution.subbands.is_empty() {
            return None;
        }
        let nl = u32::try_from(self.resolutions.len() - 1).ok()?;
        geometry::band_rects(self.rect?, nl, u32::try_from(r).ok()?)
            .find(|b| b.0 == kind)
            .map(|(_, x0, x1, y0, y1)| BandExtent::of_bounds(x0, x1, y0, y1))
    }

    /// The `HL`, `LH` and `HH` bands of resolution `r` when it lists none
    /// (see [`band`](Self::band)) and is not the coarsest.
    pub fn detail_extents(&self, r: usize) -> Option<[BandExtent; 3]> {
        if r == 0 || !self.resolutions.get(r)?.subbands.is_empty() {
            return None;
        }
        let nl = u32::try_from(self.resolutions.len() - 1).ok()?;
        let mut bands = geometry::band_rects(self.rect?, nl, u32::try_from(r).ok()?)
            .map(|(_, x0, x1, y0, y1)| BandExtent::of_bounds(x0, x1, y0, y1));
        Some([bands.next()?, bands.next()?, bands.next()?])
    }
}

/// The coded byte segments for every code-block of every component, in SIZ
/// component order.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CodedData<'a> {
    pub components: Vec<ComponentCoded<'a>>,
}

/// Parse all packets of one tile into per-code-block coded segments.
///
/// `header` is the tile's resolved header (`Codestream::tile_header`), so a
/// tile-part COD/QCD override is already in force and nothing below needs to know
/// a main header exists. The geometry is the tile's own too: every
/// tile-component bound is the *tile*'s rect on that component's grid, not the
/// image's.
///
/// There is one packet per (layer, resolution, component, precinct) the tile
/// carries, enumerated in `COD`'s progression order — see [`for_each_packet`].
/// Each component carries its own tile-component geometry, so a sub-sampled
/// component's subbands are smaller at the same resolution, and its precinct
/// lattice is coarser on the reference grid.
///
/// The walk spends `meter`, what is left of the decode's work bound (see
/// [`decode_visits`]), and fails with [`Error::Limit`] once it is gone. A
/// POC sequence's packets are held in a bitmap of at most `poc_bitmap`
/// bits, and no more than the visits left, and past it as a count per
/// precinct (see [`for_each_packet`]). The code-blocks of the resolutions that
/// `reduction` drops are charged to `meter` too (see
/// [`Budget::reduction`]). The packets of the layers past `layers` are read
/// but add nothing to their code-blocks (see [`Budget::layers`]).
pub(crate) fn decode_packets_metered<'a>(
    header: &MainHeader,
    tile: &'a Tile<'a>,
    meter: &Meter,
    poc_bitmap: u64,
    reduction: u8,
    layers: Option<u16>,
) -> std::result::Result<CodedData<'a>, Refused> {
    meter.clear_refusal();
    let mut budget = Budget {
        reduction,
        layers,
        ..Budget::decode(meter.left(), poc_bitmap)
    };
    decode_packets_within(header, tile, &mut budget, meter).map_err(|error| refused(meter, error))
}

/// [`decode_packets_metered`] on a meter of `visits` that is read back into
/// it, for tests that walk a tile on its own.
#[cfg(test)]
pub fn decode_packets<'a>(
    header: &MainHeader,
    tile: &'a Tile<'a>,
    visits: &mut u64,
    poc_bitmap: u64,
    reduction: u8,
) -> std::result::Result<CodedData<'a>, Refused> {
    let meter = Meter::new(*visits);
    let coded = decode_packets_metered(header, tile, &meter, poc_bitmap, reduction, None);
    *visits = meter.left();
    coded
}

/// How a walk that failed with `error` was refused: for work when `meter`
/// recorded why, otherwise as the error it is.
fn refused(meter: &Meter, error: Error) -> Refused {
    match (meter.refusal(), error) {
        (Some(work), Error::Limit(_)) => Refused::Work(work),
        (_, error) => Refused::Other(error),
    }
}

/// The fewest visits (see [`Meter`]) a decode may spend walking its
/// tiles' packets. A visit costs about 1 to 55 ns (see
/// [`DECODE_VISITS_PER_BYTE`]), so this is under a second. A packet a POC
/// marker's volumes reach again is skipped from a bitmap without a visit, in
/// about 4 ns; skipped by the per-precinct counts a sequence past the bitmap
/// keeps, it is charged 12 visits (see `progression::for_each_packet`).
pub(crate) const DECODE_VISITS_MIN: u64 = 1 << 24;

/// The visits a decode may spend per byte of its tiles' data, when that comes
/// to more than [`DECODE_VISITS_MIN`]. The cheapest visits, the layer
/// orders' steps over resolutions with no precinct, refuse a file at about
/// 0.02 to 0.06 µs a byte, 1 to 2 ns a visit. The costliest measured are a
/// positional walk's points over 16384 components and 200 tiles, each with
/// a packet to read: 24 to 28 ns a visit over 32 `POC` volumes and 33 to
/// 55 ns with none, the packet reads included. So the bound comes to between
/// about 0.02 and 1.8 µs a byte, against the 0.19 to 0.73 µs a byte the
/// conformance codestreams past 100 KB take to decode (the best of five
/// release-build decodes of each). At most, a file past the floor is refused
/// in about 2.5 times the slowest conformance decode per byte.
///
/// Setting up a tile's geometry is counted only for the code-blocks of the
/// resolutions a reduction drops (see [`Budget::reduction`]). A resolution with no precinct
/// gets no band or code-block geometry and lists no subband, so its setup
/// allocates nothing. The costliest shape measured, 64 tiles of 16384
/// components with one byte of packet data each, decodes at about 6 µs a
/// byte, about 1.2 times what OpenJPEG takes.
pub(crate) const DECODE_VISITS_PER_BYTE: u64 = 32;

/// The visits a decode may spend walking the packets of tiles that hold
/// `bytes` bytes of data in all, packed packet headers included.
///
/// A code-block entry that reads a bit of its packet header is paid for by
/// the header's bytes and is not counted. What is counted is the work that
/// reads none: each packet, each step over code-blocks whose inclusion is
/// already known to lie past the layer (see [`parse_band_header`]), and each
/// point of the progression order's loops. Encoder output spends 0.04 to 88
/// visits per byte, the high end only in files of a few hundred bytes,
/// under the floor; the largest share of the bound seen is 12.8%, from
/// `opj_compress` on a sparse 2048^2 image in 4x4 code-blocks and 64
/// layers. A packet header of one byte
/// can still make the walk step over each row of code-blocks of its
/// precinct, and a positional sweep can check millions of positions that
/// hold no packet, so without a bound a small file could make the walk run
/// for minutes. The one bound covers every tile of the decode, so many small
/// tiles cannot each spend the floor.
pub(crate) fn decode_visits(bytes: u64) -> u64 {
    DECODE_VISITS_MIN.max(bytes.saturating_mul(DECODE_VISITS_PER_BYTE))
}

/// What one walk of a tile's packets may hold: the geometry guards, the
/// POC packet storage and the reduction. The visits it spends are the
/// [`Meter`]'s. Running out of a guard fails the walk with [`Error::Limit`]
/// before the work or the allocation is done.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Budget {
    /// Precincts across the tile's components (see [`resolution_geoms`]).
    pub(crate) precincts: usize,
    /// Code-blocks across the tile's components (see [`resolution_geoms`]).
    pub(crate) blocks: usize,
    /// The most bits a POC sequence's packets are held in a bitmap of (see
    /// [`for_each_packet`]).
    pub(crate) poc_bitmap: u64,
    /// Whether a POC sequence whose packets are more than
    /// [`poc_bitmap`](Self::poc_bitmap) holds them as a count per precinct;
    /// if not, its walk fails.
    pub(crate) poc_counts: bool,
    /// Whether a POC sequence that leaves out packets of the tile is refused
    /// before the walk (see `progression::check_poc_coverage`). The decode
    /// sets it. `read_header`'s walk of a `Psot = 0` tile does not: it asks
    /// only where the packets the volumes reach end, and the decode refuses
    /// the tile anyway.
    pub(crate) poc_coverage: bool,
    /// The decode's resolution reduction. Each code-block of a resolution it
    /// drops costs a visit once the geometry is built, since every tile builds
    /// its geometry at full resolution whatever the reduction. Nothing else is
    /// charged for the geometry: each precinct is paid for by its packets'
    /// visits, and the code-blocks of the kept resolutions are no more than
    /// the reduced tile's samples, which the sample limit bounds, and the
    /// per-tile cap. Without
    /// the charge, many tiles of next to no data, each building close to the
    /// per-tile code-block cap, would make a small file take minutes to decode
    /// at a reduction. It is 0 for `read_header`'s walk.
    pub(crate) reduction: u8,
    /// The decode's layer cap (`DecodeOptions::max_quality_layers`): the
    /// packets of layer `layers` on are still read, since they frame the
    /// packets after them and their headers carry each code-block's state
    /// into the next layer, but their coding passes and bytes are not added
    /// to the code-blocks. Reading them costs the visits it does without a
    /// cap. `None` keeps every layer, as `read_header`'s walk does.
    pub(crate) layers: Option<u16>,
}

impl Budget {
    /// The decode's budget for one tile: the geometry guards, and a POC
    /// bitmap of no more bits than `visits`, the visits left on the decode's
    /// [`Meter`], or `poc_bitmap`, then a count per precinct. A walk that
    /// reads every packet takes a visit for each, so a bitmap of more bits
    /// than the visits left would be wasted. `poc_bitmap` is the decode's
    /// default bound, so a raised bound does not raise the bitmap's memory.
    pub(crate) const fn decode(visits: u64, poc_bitmap: u64) -> Budget {
        Budget {
            precincts: MAX_PRECINCTS,
            blocks: MAX_CODE_BLOCKS,
            poc_bitmap: if visits < poc_bitmap {
                visits
            } else {
                poc_bitmap
            },
            poc_counts: true,
            poc_coverage: true,
            reduction: 0,
            layers: None,
        }
    }
}

/// What a walk with `strict` off found of a tile's packets: which were never
/// read, and what was left over.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Reach {
    /// Per component, per resolution: the first layer with a packet that was
    /// not read, or `None` when every packet of it was read.
    pub(crate) missing_from: Vec<Vec<Option<u32>>>,
    /// Bytes of tile data not read: where they start and how many.
    pub(crate) leftover: Option<(usize, usize)>,
    /// Bytes of packed packet headers not read.
    pub(crate) leftover_packed: Option<usize>,
}

impl Reach {
    /// Note that packet (`layer`, `res`, `comp`) was not read.
    fn miss(&mut self, comp: usize, res: usize, layer: u32) {
        if let Some(from) = self.missing_from.get_mut(comp).and_then(|c| c.get_mut(res)) {
            *from = Some(from.map_or(layer, |f| f.min(layer)));
        }
    }
}

/// [`decode_packets_metered`] with `strict` off: the tile's data may end
/// anywhere, and what was not read comes back as a [`Reach`] beside the coded
/// data (see [`walk_packets`]).
pub fn decode_packets_partial<'a>(
    header: &MainHeader,
    tile: &'a Tile<'a>,
    meter: &Meter,
    poc_bitmap: u64,
    reduction: u8,
    layers: Option<u16>,
) -> std::result::Result<(CodedData<'a>, Reach), Refused> {
    meter.clear_refusal();
    let mut budget = Budget {
        reduction,
        layers,
        ..Budget::decode(meter.left(), poc_bitmap)
    };
    let mut reach = Reach::default();
    walk_packets(header, tile, &mut budget, meter, Some(&mut reach))
        .map(|coded| (coded, reach))
        .map_err(|error| refused(meter, error))
}

/// [`decode_packets_metered`] under `budget`, spending `meter`, which is left
/// holding what was not spent, also when the walk fails.
pub(crate) fn decode_packets_within<'a>(
    header: &MainHeader,
    tile: &'a Tile<'a>,
    budget: &mut Budget,
    meter: &Meter,
) -> Result<CodedData<'a>> {
    walk_packets(header, tile, budget, meter, None)
}

/// The packet walk behind [`decode_packets_within`] and
/// [`decode_packets_partial`]: strict when `reach` is `None`.
///
/// Not strict, the walk reads packets until the data ends (see
/// [`parse_packet`]), then goes on through the rest of the progression
/// without reading, to note each packet not read in `reach`. That costs a
/// visit a packet, from the same meter, and the end of the data costs
/// [`END_VISITS`] for each resolution of each component, for the setup no
/// byte paid for. Packets the `POC` volumes leave out are noted the same way
/// rather than refused, and bytes left after the last packet are noted
/// rather than refused. Every other fault, the work bound included, fails as
/// it does when strict.
fn walk_packets<'a>(
    header: &MainHeader,
    tile: &'a Tile<'a>,
    budget: &mut Budget,
    meter: &Meter,
    mut reach: Option<&mut Reach>,
) -> Result<CodedData<'a>> {
    let data: &'a [u8] = &tile.data;

    // Precincts are bounded *as the geometry is built*, not after: a precinct's
    // grid dimensions are cheap to compute but each precinct then costs a
    // `PrecinctGeom` per band and a pair of tag trees, each tree a heap
    // allocation, and the block count does not bound them — a band empty in
    // one axis has no blocks while its resolution still has a full column of
    // precincts. A 1 × 2^26 image under a 2^1 precinct is the shape that
    // exploits it: ~2^25 precincts, built before any after-the-fact check
    // could fire. So the cap is threaded through `resolution_geoms` and
    // enforced before the allocation, across every component of the tile
    // (`resolution_geoms` mutates the running total).
    //
    // The code-block count is bounded the same way and for the same reason:
    // the per-block `BlockState`s and tag-tree leaves below, and the
    // `CodeBlock`s after them, are sized by it, and legal 4×4 blocks push the
    // count toward samples/16, so the cap has to bite before they are
    // allocated rather than summing them afterwards. Both running totals are
    // threaded through `resolution_geoms`; the geometry itself holds nothing
    // per block.
    //
    // The code-blocks of the resolutions the reduction drops are charged to
    // `meter` as each component is built (see [`Budget::reduction`]), so a
    // tile can pass the bound by at most one component's geometry, which the
    // caps bound.
    let geoms = tile_geometry(header, tile.index, budget, |dropped| {
        meter.spend(
            dropped,
            WorkRefusal::DroppedBlocks {
                blocks: dropped as u64,
            },
        )
    })?;
    let mut states = band_states(header, &geoms);

    let walk = PacketWalk {
        siz: &header.siz,
        tile: header
            .siz
            .tile_rect(tile.index)
            .ok_or_else(|| Error::inconsistent(format!("no tile {} in `SIZ`", tile.index)))?,
        geoms: &geoms,
    };
    let delimiters = Delimiters {
        sop: header.cod.use_sop,
        eph: header.cod.use_eph,
    };
    // When the tile carries PPT packed headers, packet headers come from that
    // buffer and only the bodies stay inline; otherwise both are `data` and the
    // two cursors move together.
    let packed = !tile.packed_headers.is_empty();
    let mut streams = PacketStreams {
        header: if packed { &tile.packed_headers } else { data },
        header_pos: 0,
        data,
        body_pos: 0,
        packed,
        tile: tile.index,
    };
    if let Some(reach) = reach.as_deref_mut() {
        reach.missing_from = geoms.iter().map(|c| vec![None; c.len()]).collect();
        progression::poc_gaps(header, &walk, |comp, res, layer| {
            reach.miss(comp, res, u32::from(layer));
            // The packets left out may add passes to the last segment of
            // any code-block of the resolution, as packets the data never
            // reached may.
            mark_gap(&mut states[comp][res], u32::from(layer));
            Ok(())
        })?;
    } else if budget.poc_coverage {
        progression::check_poc_coverage(header, &walk, tile.index)?;
    }
    let strict = reach.is_none();
    let end_visits = geoms
        .iter()
        .map(Vec::len)
        .sum::<usize>()
        .saturating_mul(END_VISITS);
    let mut log = Vec::new();
    let mut scratch = Scratch::default();
    // Not strict: whether the data has ended.
    let mut ended = false;
    let mut packet_index = 0u32;
    for_each_packet(
        header,
        &walk,
        meter,
        budget.poc_bitmap,
        budget.poc_counts,
        |layer, resolution, component, precinct| {
            if let Some(reach) = reach.as_deref_mut() {
                if ended {
                    // Past the end of the data: count the packet, not read.
                    meter.spend(1, WorkRefusal::PacketWalk)?;
                    reach.miss(component, resolution, layer);
                    mark_unread(&mut states[component][resolution], precinct, layer);
                    return Ok(());
                }
            }
            let geom = &geoms[component][resolution];
            let packet = parse_packet(
                &mut streams,
                meter,
                layer,
                packet_index,
                delimiters,
                header.components[component].coding.code_block_style,
                &geom.bands,
                precinct,
                &mut states[component][resolution],
                &mut scratch,
                budget.layers.is_none_or(|cap| layer < u32::from(cap)),
                if strict { None } else { Some(&mut log) },
                None,
            )?;
            if packet != Packet::Read {
                ended = true;
                mark_unread(&mut states[component][resolution], precinct, layer);
                meter.spend(end_visits, WorkRefusal::PacketWalk)?;
                if let Some(reach) = reach.as_deref_mut() {
                    reach.miss(component, resolution, layer);
                }
            }
            packet_index += 1;
            Ok(())
        },
    )?;

    let mut components = Vec::with_capacity(states.len());
    for (comp, (component_states, component_geoms)) in states.iter().zip(&geoms).enumerate() {
        let rect = header
            .siz
            .tile_component_rect(tile.index, comp)
            .ok_or_else(|| Error::inconsistent(format!("no component {comp} in `SIZ`")))?;
        let mut resolutions = Vec::with_capacity(component_states.len());
        for (band_states, level) in component_states.iter().zip(component_geoms) {
            let subbands = if level.bands.is_empty() {
                Vec::new()
            } else {
                build_subbands(&level.bands, band_states, data)
            };
            resolutions.push(Resolution { subbands });
        }
        components.push(ComponentCoded {
            resolutions,
            rect: Some(rect),
        });
    }

    // Not strict, bytes left after the last packet are noted rather than
    // refused. Data that ran out leaves none.
    if let Some(reach) = reach {
        if !ended {
            if streams.body_pos < data.len() {
                reach.leftover = Some((streams.body_pos, data.len() - streams.body_pos));
            }
            if packed && streams.header_pos < streams.header.len() {
                reach.leftover_packed = Some(streams.header.len() - streams.header_pos);
            }
        }
        return Ok(CodedData { components });
    }

    // The packets tile the tile-part data exactly, with no padding, up to the
    // closing EOC. Any remainder means a misread field — a dropped layer shows up
    // here as leftover bytes (this doubles as the parse self-check).
    if streams.body_pos != data.len() {
        return Err(Error::codestream(format!(
            "tile {} has {} byte(s) left after the last packet",
            tile.index,
            data.len() - streams.body_pos
        )));
    }
    // The packed headers must be exhausted too: a leftover header byte is the
    // same kind of misread, caught on the header stream instead of the body.
    if packed && streams.header_pos != streams.header.len() {
        return Err(Error::codestream(format!(
            "tile {} has {} byte(s) of packed packet headers left after the last packet",
            tile.index,
            streams.header.len() - streams.header_pos
        )));
    }

    Ok(CodedData { components })
}

/// The geometry of each component of tile `tile`, built under `budget`'s
/// caps. After each component, `dropped` is called with the code-blocks of
/// the resolutions `budget`'s reduction drops (see [`Budget::reduction`]),
/// and an error from it ends the build.
fn tile_geometry(
    header: &MainHeader,
    tile: u32,
    budget: &mut Budget,
    mut dropped: impl FnMut(usize) -> Result<()>,
) -> Result<Vec<Vec<ResolutionGeom>>> {
    let component_count = header.siz.components.len();
    let mut geoms = Vec::with_capacity(component_count);
    for c in 0..component_count {
        let resolutions =
            resolution_geoms(header, tile, c, &mut budget.precincts, &mut budget.blocks)?;
        let kept = resolutions
            .len()
            .saturating_sub(usize::from(budget.reduction));
        dropped(
            resolutions[kept..]
                .iter()
                .flat_map(|resolution| &resolution.bands)
                .map(BandGeom::block_count)
                .sum(),
        )?;
        geoms.push(resolutions);
    }
    Ok(geoms)
}

/// One state per (component, resolution, band) of `geoms`, carried across
/// every layer. The tag trees decode incrementally and there is a pair per
/// *precinct*, so they must outlive the packet that starts them.
fn band_states(header: &MainHeader, geoms: &[Vec<ResolutionGeom>]) -> Vec<Vec<Vec<BandState>>> {
    geoms
        .iter()
        .zip(&header.components)
        .map(|(component, params)| {
            let coder = Coder::of_style(params.coding.code_block_style, header.ccap15);
            component
                .iter()
                .map(|level| {
                    level
                        .bands
                        .iter()
                        .map(|band| BandState::new(band, coder))
                        .collect()
                })
                .collect()
        })
        .collect()
}

/// Ceiling on the zero-bitplane count: a single read above this resolves any
/// real value, while rejecting a malformed run of zero bits rather than
/// looping on it. The count is bounded by the *raised* plane count
/// `Kmax = Mb + SPrgn` (ISO H.2), not by `Mb` alone — a background-only block
/// under a maxshift legitimately signals more zero bit-planes than the band
/// has magnitude planes. `Mb` is at most 37 (7 guard bits + exponent 31 − 1)
/// and the maxshift byte at most 255, so 292 is the largest value a
/// conformant stream can signal and this threshold resolves every one.
const ZBP_LIMIT: u32 = 293;

/// Upper bound on the Lblock length-indicator before a malformed packet is
/// rejected. The length field is `Lblock + floor(log2(num_passes))` bits and
/// `num_passes ≤ 164` (so `floor(log2) ≤ 7`); capping Lblock at 24 keeps that
/// read at most 31 bits, inside the `u32` [`BitReader::read`] accepts.
const LBLOCK_MAX: u32 = 24;

/// What a codeword segment codes. It decides how many passes the segment may
/// hold and what the next segment is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SegmentKind {
    /// Part 1 passes the MQ coder codes: every segment of a block without
    /// `bypass`, and the cleanup segments of one with it.
    Mq,
    /// The raw segment of a `bypass` block: a significance and a refinement
    /// pass coded as raw bits.
    Raw,
    /// An HT cleanup segment (ISO/IEC 15444-15 B.2). A block's first one
    /// holds `placeholders` passes before its cleanup pass, a multiple of
    /// three; every later one holds its cleanup pass alone. `None` while every
    /// pass read is a placeholder pass: the cleanup pass, and so the segment's
    /// end, is still to come (B.3 NOTE 4). See [`ht_first_cleanup`].
    HtCleanup { placeholders: Option<u32> },
    /// An HT refinement segment: a SigProp pass and, if present, a MagRef
    /// pass.
    HtRefinement,
}

/// The kind of the *next* codeword segment, and how many coding passes it may
/// hold, given the block's coder, the code-block style and the last segment
/// opened (`opj_t2_init_seg`).
///
/// `termall` terminates the MQ coder after every pass, so each pass is its own
/// segment. `bypass` (lazy) terminates at every raw/MQ boundary: the first
/// segment holds the ten MQ passes of the four most significant bit-planes, then
/// each lower plane splits into a two-pass raw segment (significance +
/// refinement) and a one-pass MQ segment (cleanup) — the `10, 2, 1, 2, 1, …`
/// pattern OpenJPEG spells with `prev.maxpasses in {1, 10} ? 2 : 1`. Otherwise a
/// segment takes up to 109 passes — `3 * Mb - 2` with `Mb` capped at 37 — which a
/// code-block can never exceed, so the default never splits.
///
/// An HT block's segments alternate by ISO/IEC 15444-15 B.2: after a cleanup
/// segment comes a refinement segment of up to two passes, then a one-pass
/// cleanup segment. Its first segment is opened by [`ht_first_cleanup`]
/// instead, since where it ends depends on the placeholder passes of the
/// contribution that holds the cleanup pass. The other style bits mean nothing
/// to an HT block.
fn next_segment(coder: Coder, style: u8, last: Option<&SegmentState>) -> (SegmentKind, u32) {
    use crate::codestream::markers::code_block_style::{LAZY, TERMALL};
    let last = last.map(|s| s.kind);
    if coder == Coder::Ht {
        match last {
            Some(SegmentKind::HtCleanup { .. }) => (SegmentKind::HtRefinement, 2),
            _ => (
                SegmentKind::HtCleanup {
                    placeholders: Some(0),
                },
                1,
            ),
        }
    } else if style & TERMALL != 0 {
        // Every pass ends its segment, so nothing turns on the kind.
        (SegmentKind::Mq, 1)
    } else if style & LAZY != 0 {
        match last {
            None => (SegmentKind::Mq, 10),
            Some(SegmentKind::Mq) => (SegmentKind::Raw, 2),
            Some(_) => (SegmentKind::Mq, 1),
        }
    } else {
        (SegmentKind::Mq, 109)
    }
}

/// The width of a length field: `Lblock + floor(log2(n))` bits for a codeword
/// segment of `n` passes from one packet (ISO B.10.7.1). An HT block uses the
/// same rule for each codeword segment (ISO/IEC 15444-15 B.3), so the field of
/// its first cleanup segment counts the placeholder passes in it too.
fn length_bits(lblock: u32, passes: u32) -> u32 {
    lblock + passes.ilog2()
}

/// The longest HT cleanup segment, `Lcup < 65535`, and HT refinement segment,
/// `Lref < 2047` (ISO/IEC 15444-15 7.1.1, 7.1.2).
const HT_CLEANUP_MAX: u64 = 65534;
const HT_REFINEMENT_MAX: u64 = 2046;

/// Add a length field's `bytes` to `segment`, which an HT segment may hold
/// only within its limit. An HT segment's bytes are those of its codeword
/// segments over the layers (B.3), so the limit is on the sum.
fn add_length(segment: &mut SegmentState, bytes: u32) -> Result<()> {
    segment.length += u64::from(bytes);
    let (what, max) = match segment.kind {
        SegmentKind::HtCleanup { .. } => ("cleanup", HT_CLEANUP_MAX),
        SegmentKind::HtRefinement => ("refinement", HT_REFINEMENT_MAX),
        SegmentKind::Mq | SegmentKind::Raw => return Ok(()),
    };
    if segment.length > max {
        return Err(Error::codestream(format!(
            "HT {what} segment is {} bytes, over the limit of {max}",
            segment.length
        )));
    }
    Ok(())
}

/// What the first length field of an HT block's contribution is, while the
/// block's cleanup pass is still to come. See [`ht_first_cleanup`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FirstCleanup {
    /// Every pass of the contribution is a placeholder pass.
    Placeholders,
    /// The contribution holds the cleanup pass, `placeholders` passes from
    /// the block's first, and its first codeword segment has `passes` passes,
    /// up to the cleanup pass, and `length` bytes.
    Cleanup {
        placeholders: u32,
        passes: u32,
        length: u32,
    },
}

/// Read the first length field of an HT block's contribution of `new` passes,
/// after `prior` passes that were all placeholders, and say whether the
/// contribution holds the cleanup pass (ISO/IEC 15444-15 B.1 to B.3).
///
/// The packet with a block's first cleanup pass holds no other cleanup pass
/// and at most two refinement passes after it (B.3). If this contribution
/// holds it, it is pass `3 P0` (from 0), `P0 = floor((prior + new - 1) / 3)`,
/// and the contribution's first codeword segment ends there. A contribution
/// of placeholder passes only (NOTE 4) is one codeword segment of all `new`
/// passes, whose length is zero, since placeholder passes carry no bytes.
///
/// The pass counts cannot tell the two apart, but the length can. The first
/// cleanup segment is at least 2 bytes long (B.3), so the field read at the
/// cleanup reading's width is nonzero exactly when the contribution holds the
/// cleanup pass. That width is never more than the placeholder reading's, so
/// a zero is the leading bits of an all-zero field, and the rest of it is read
/// next. When `3 P0` falls before `prior`, the contribution cannot hold the
/// cleanup pass, and its field is read at the placeholder width at once.
fn ht_first_cleanup(
    lblock: u32,
    prior: u32,
    new: u32,
    bio: &mut BitReader,
) -> Result<FirstCleanup> {
    let placeholders = 3 * ((prior + new - 1) / 3);
    let mut read = 0;
    if placeholders >= prior {
        let passes = placeholders + 1 - prior;
        let bits = length_bits(lblock, passes);
        match bio.read(bits) {
            0 => read = bits,
            1 => {
                return Err(Error::codestream(
                    "HT code-block's first cleanup segment is 1 byte long; it must be at least 2",
                ));
            }
            length => {
                return Ok(FirstCleanup::Cleanup {
                    placeholders,
                    passes,
                    length,
                });
            }
        }
    }
    if bio.read(length_bits(lblock, new) - read) != 0 {
        return Err(Error::codestream(
            "HT code-block placeholder passes carry bytes",
        ));
    }
    Ok(FirstCleanup::Placeholders)
}

/// What the first length field of a contribution says about a code-block of
/// a MIXED tile-component whose earlier length fields were all zero. See
/// [`mixed_first_length`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MixedFirst {
    /// The field is zero: the contribution codes no bytes, and the block's
    /// coder is still open.
    Empty,
    /// The block is an HT block, and the contribution holds its cleanup pass.
    Ht(FirstCleanup),
    /// The block is a Part 1 block, and its one codeword segment takes
    /// `length` bytes from the contribution.
    Part1 { length: u32 },
}

/// Read the first length field of a contribution of `new` passes to a
/// code-block of a MIXED tile-component, after `prior` passes whose length
/// fields were all zero, and say which coder the block uses (ISO/IEC
/// 15444-15 A.4).
///
/// The two coders read the field at different widths when the contribution
/// holds an HT cleanup pass and refinement passes after it: an HT block's
/// first field ends at its cleanup pass (see [`ht_first_cleanup`]), a Part 1
/// block's one field spans all `new` passes, since A.4 allows a Part 1 block
/// here neither bypass nor termination on each pass. Both fields start at
/// the same bit, and A.4 requires of an HT block that the first bit of its
/// first nonzero length field be 0 and that `Lblock` be above 3 there. So a
/// nonzero field with `Lblock` at 3 or a leading 1 is a Part 1 block's.
/// Otherwise the field is read as an HT block's, and a value an HT block
/// cannot have (a cleanup length of 1 or over `HT_CLEANUP_MAX`, or bytes on
/// placeholder passes) makes it a Part 1 block's: that is the A.4 note's "processing the code-block
/// assuming it conforms to Annex B", and it fails here, in the length field,
/// or not at all.
///
/// The bits read are the same whichever reading wins: a zero field is
/// `Lblock + floor(log2(new))` bits in both, and a Part 1 field is read to
/// that width. So the choice is made once, here, and the walk never holds
/// two readings of a block.
fn mixed_first_length(
    lblock: u32,
    prior: u32,
    new: u32,
    bio: &mut BitReader,
) -> Result<MixedFirst> {
    let width = length_bits(lblock, new);
    // The HT reading's first field, when the contribution can hold the
    // cleanup pass: its width, and its `placeholders` before the pass.
    let placeholders = 3 * ((prior + new - 1) / 3);
    let cleanup = (placeholders >= prior)
        .then(|| (length_bits(lblock, placeholders + 1 - prior), placeholders));
    // The Part 1 reading of the field, whose first `read` bits, of value
    // `top`, are read already.
    let part1 = |top: u32, read: u32, bio: &mut BitReader| match (top << (width - read))
        | bio.read(width - read)
    {
        0 => MixedFirst::Empty,
        length => MixedFirst::Part1 { length },
    };
    if lblock <= 3 {
        return Ok(part1(0, 0, bio));
    }
    if bio.read_bit() == 1 {
        return Ok(part1(1, 1, bio));
    }
    let Some((bits, placeholders)) = cleanup else {
        return Ok(part1(0, 1, bio));
    };
    match bio.read(bits - 1) {
        top @ (0 | 1) => Ok(part1(top, bits, bio)),
        // A cleanup segment over its limit (7.1.1) is not an HT block's.
        top if u64::from(top) > HT_CLEANUP_MAX => Ok(part1(top, bits, bio)),
        length => Ok(MixedFirst::Ht(FirstCleanup::Cleanup {
            placeholders,
            passes: placeholders + 1 - prior,
            length,
        })),
    }
}

/// Settle a code-block of a MIXED tile-component as a Part 1 block. Its
/// earlier passes, held as placeholder passes of no bytes, become passes of
/// its one MQ codeword segment. A.4 allows a Part 1 block there neither
/// bypass nor termination on each pass, so all its passes, in every layer,
/// are one codeword segment with one length field per contribution
/// (ISO/IEC 15444-1 B.10.7.1). The segment has no pass limit: OpenJPEG's
/// 109-pass split ([`next_segment`]) is for the Part 1 tile-components it
/// decodes. A block of more passes than tier-1 can decode is refused there.
fn become_part1(block: &mut BlockState) {
    block.coder = Some(Coder::Part1);
    match block.segments.first_mut() {
        Some(segment) => {
            segment.kind = SegmentKind::Mq;
            segment.max_passes = u32::MAX;
            block.num_passes += segment.kept;
        }
        None => block
            .segments
            .push(SegmentState::new(SegmentKind::Mq, u32::MAX)),
    }
}

/// Which packet delimiters `COD` signals (`Scod` bits 1 and 2).
#[derive(Debug, Clone, Copy)]
struct Delimiters {
    /// SOP *may* precede each packet.
    sop: bool,
    /// EPH *shall* follow each packet header.
    eph: bool,
}

/// The two byte streams a packet is read from, with a cursor into each.
///
/// A packet is a header followed by its body. Normally both are inline in the
/// tile data and the two cursors move in lockstep. When the tile carries `PPT`
/// packed headers, the header stream is that separate buffer and the body stream
/// is still the tile data, so the cursors advance independently (A.7.5). SOP
/// stays in the body stream; EPH moves with the headers.
struct PacketStreams<'a> {
    /// Where packet *headers* are read: the `PPT` buffer, or `data` when inline.
    header: &'a [u8],
    header_pos: usize,
    /// Where packet *bodies* (and SOP markers) are read: always the tile data.
    data: &'a [u8],
    body_pos: usize,
    /// Whether the header stream is separate from the body stream.
    packed: bool,
    /// The tile's index, for an error message.
    tile: u32,
}

/// Why a packet walk was refused for work, with the numbers a caller needs to
/// name a [`WorkLimit`](crate::WorkLimit) factor. It travels beside the
/// [`Error`] the walk fails with (see [`Refused`]), so nothing reads it back
/// out of the message. A new work refusal (the sample-area one, say) is a new
/// variant here, a line in [`WorkRefusal::why`], and a function in
/// `limit_hint`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum WorkRefusal {
    /// The walk ran out of visits.
    PacketWalk,
    /// A POC sequence's volumes reach `reached` packets, more than the visits
    /// left.
    PocReach { reached: u64 },
    /// A component's geometry holds `blocks` code-blocks in the resolutions
    /// the reduction drops, more than the visits left (see
    /// [`Budget::reduction`]).
    DroppedBlocks { blocks: u64 },
}

impl WorkRefusal {
    /// The plain message of the [`Error::Limit`] for this refusal, without
    /// the tile or a retry factor. [`decode`](crate::decode) replaces it with
    /// the full message from `limit_hint`.
    pub(crate) fn why(&self) -> String {
        match self {
            WorkRefusal::PacketWalk => "reading the packet headers goes past the packet work \
                                        bound (see `DecodeOptions::packet_work_limit`)"
                .into(),
            WorkRefusal::PocReach { reached } => format!(
                "the `POC` volumes reach {reached} packets, a step each, more than \
                 the steps left of the packet work bound (see \
                 `DecodeOptions::packet_work_limit`)"
            ),
            WorkRefusal::DroppedBlocks { blocks } => format!(
                "the resolutions the reduction drops hold {blocks} code-blocks in \
                 one component, a step each, more than the steps left of the \
                 packet work bound (see `DecodeOptions::packet_work_limit`)"
            ),
        }
    }
}

/// How [`decode_packets_metered`] fails: for work, with the typed reason, or
/// for anything else, as the [`Error`] it is.
#[derive(Debug)]
pub(crate) enum Refused {
    /// The tile went past the packet work bound.
    Work(WorkRefusal),
    /// Any other failure.
    Other(Error),
}

impl From<Refused> for Error {
    fn from(refused: Refused) -> Error {
        match refused {
            Refused::Work(work) => Error::limit_unhinted(crate::LimitKind::PacketWork, work.why()),
            Refused::Other(error) => error,
        }
    }
}

/// A decode's packet work count: the visits it started with, what is left,
/// and, once a walk is refused for work, why. The decode owns one and every
/// tile's walk spends it, so there is a single count to read afterwards.
///
/// A visit is a step of a walk that reads no bit: packets read, steps over
/// code-blocks a packet header does not name a bit for, and the points of
/// the progression order's loops, such as each position of a positional
/// sweep for each (component, resolution) it checks. A packet header can
/// be a single byte and still pass over every row of code-blocks of its
/// precinct, and a sweep can check millions of points that hold no
/// packet, so this bounds the work where the other limits cannot.
pub(crate) struct Meter {
    first: u64,
    left: Cell<u64>,
    refusal: Cell<Option<WorkRefusal>>,
}

impl Meter {
    /// A meter with `first` visits to spend.
    pub(crate) fn new(first: u64) -> Self {
        Meter {
            first,
            left: Cell::new(first),
            refusal: Cell::new(None),
        }
    }

    /// The visits it started with.
    pub(crate) fn first(&self) -> u64 {
        self.first
    }

    /// The visits left.
    pub(crate) fn left(&self) -> u64 {
        self.left.get()
    }

    /// The visits spent so far.
    pub(crate) fn spent(&self) -> u64 {
        self.first.saturating_sub(self.left.get())
    }

    /// Why the walk was refused for work, once it is.
    fn refusal(&self) -> Option<WorkRefusal> {
        self.refusal.get()
    }

    /// Forget a refusal an earlier walk recorded, so that it cannot label
    /// this walk's other limit errors as work.
    fn clear_refusal(&self) {
        self.refusal.set(None);
    }

    /// Spend `n` visits, or leave the count as it is and say whether they
    /// were there. For a charge whose refusal is not a [`WorkRefusal`].
    pub(crate) fn try_spend(&self, n: u64) -> bool {
        match self.left.get().checked_sub(n) {
            Some(rest) => {
                self.left.set(rest);
                true
            }
            None => false,
        }
    }

    /// Spend `n` visits, or refuse the walk for `why` when fewer are left.
    pub(crate) fn spend(&self, n: usize, why: WorkRefusal) -> Result<()> {
        if self.try_spend(n as u64) {
            Ok(())
        } else {
            Err(self.refuse(why))
        }
    }

    /// Record `why` as the reason the walk is refused, and give the [`Error`]
    /// to fail it with.
    pub(crate) fn refuse(&self, why: WorkRefusal) -> Error {
        self.refusal.set(Some(why));
        Error::limit_unhinted(crate::LimitKind::PacketWork, why.why())
    }
}

/// What the end of a tile's data costs with `strict` off, in visits, for
/// each resolution of each of the tile's components.
///
/// A strict decode stops at the first tile whose data runs out; one with
/// `strict` off goes on to the next. The geometry built for a tile and the
/// decode after its walk cost about a microsecond for each resolution of
/// each component, which the bytes of its packets pay for when they are
/// there. When they are not, a tile-part of 14 bytes would buy that setup
/// for thousands of components, so this charges it: at 32 visits, the
/// default bound's 2^24 visits hold about half a second of it. The data of
/// a codestream a strict decode reads never ends early, so this costs that
/// decode nothing.
const END_VISITS: usize = 32;

/// The error for a packed packet header that reads past the end of the
/// `PPM` or `PPT` bytes it is packed in.
fn packed_overrun() -> Error {
    Error::codestream("a packet header runs past the end of its `PPM` or `PPT` packed headers")
}

/// The two-byte marker at `pos`, or `None` if it would run past the end.
fn peek_marker(data: &[u8], pos: usize) -> Option<u16> {
    let hi = *data.get(pos)?;
    let lo = *data.get(pos + 1)?;
    Some(u16::from_be_bytes([hi, lo]))
}

/// Consume the SOP marker segment at `pos` and return the offset of the packet
/// header that follows (ISO A.8.1).
///
/// `Lsop` is fixed at 4 and `Nsop` counts packets from zero **within the tile**,
/// continuing across that tile's tile-parts: OpenJPEG's encoder writes
/// `tile->packno`, which lives on the tile and is never reset per tile-part. The
/// corpus agrees — every single-tile entry that carries SOP numbers its packets
/// sequentially from zero, every multi-tile one restarts.
///
/// The counter therefore lives in [`decode_packets_metered`], which runs once
/// per tile over that tile's tile-parts joined end to end: it starts at zero
/// for each tile and does not restart at a tile-part boundary.
///
/// OpenJPEG leaves `Nsop` unchecked — `/* TODO : check the Nsop value */` — so
/// this is stricter than the oracle. Validating the sequence number is the whole
/// point of a resynchronization marker.
fn read_sop(data: &[u8], pos: usize, packet_index: u32) -> Result<usize> {
    let end = pos + 6;
    if end > data.len() {
        return Err(Error::codestream("truncated `SOP` marker segment"));
    }
    let lsop = u16::from_be_bytes([data[pos + 2], data[pos + 3]]);
    if lsop != 4 {
        return Err(Error::codestream(format!(
            "`SOP` declares `Lsop` {lsop}, expected 4"
        )));
    }
    let nsop = u16::from_be_bytes([data[pos + 4], data[pos + 5]]);
    // Nsop is 16 bits and wraps; a tile with more than 65536 packets restarts it.
    let expected = (packet_index % 65536) as u16;
    if nsop != expected {
        return Err(Error::codestream(format!(
            "`SOP` sequence number {nsop} for packet {packet_index}, expected {expected}"
        )));
    }
    Ok(end)
}

/// One code-block's decode state, carried across the quality layers of its
/// precinct (ISO/IEC 15444-1 B.10).
///
/// A block is included in the packets of one layer onward, never in isolation:
/// once the inclusion tag tree resolves it, later layers signal a contribution
/// with a single bit. `lblock` and `zero_bit_planes` are read once and then
/// persist, and `num_passes` and `segments` accumulate. Rebuilding any of this
/// per packet — which a single-layer decoder can get away with — desynchronizes
/// the header bit-reader on the second layer.
struct BlockState {
    /// The block's coder: the tile-component's, or, under MIXED, `None`
    /// until the block's first nonzero length field settles it (see
    /// [`mixed_first_length`]). While it is `None` every length field read
    /// was zero, which both coders read alike: the block's passes sit in one
    /// open HT cleanup segment of no bytes, as placeholder passes, and become
    /// passes of its one MQ segment if the block turns out to be a Part 1
    /// block.
    coder: Option<Coder>,
    included: bool,
    /// Length-indicator width, grown by a unary run of 1s and never reset.
    lblock: u32,
    /// The zero bit-planes the tag tree gives, without an HT block's skipped
    /// ones (see [`skipped_planes`](Self::skipped_planes)).
    zero_bit_planes: u32,
    /// Coding passes summed over every contributing layer the decode keeps
    /// (see [`Budget::layers`]), an HT block's placeholder passes left out.
    num_passes: u32,
    /// Codeword segments in order; the last one may still take passes.
    segments: Vec<SegmentState>,
}

/// A codeword segment while it is being filled from successive layers.
struct SegmentState {
    kind: SegmentKind,
    /// Coding passes from every layer read, which decide where the next
    /// segment starts. An HT block's first cleanup segment counts its
    /// placeholder passes.
    passes: u32,
    /// The passes of [`passes`](Self::passes) from the layers the decode
    /// keeps: all of them without a layer cap. The layers past the cap only
    /// add passes after these, and add no chunks.
    kept: u32,
    /// How many passes this segment may hold before the next one starts:
    /// 1 under `termall`, the `10, 2, 1, 2, 1, …` pattern under `bypass`,
    /// otherwise 109 — the most a code-block can carry (`3 * Mb - 2` with
    /// `Mb <= 37`), so the default never splits. See [`next_segment`],
    /// which mirrors OpenJPEG's `opj_t2_init_seg`. A Part 1 block of a MIXED
    /// tile-component has one segment of any number of passes
    /// ([`become_part1`]). An HT block's first
    /// cleanup segment holds its placeholder passes and the cleanup pass,
    /// and until the cleanup pass comes it takes any number.
    max_passes: u32,
    /// The bytes the length fields gave it, over every layer read.
    length: u64,
    /// Where each layer's bytes of it lie in the tile data, in layer order.
    /// They are offsets, not slices, so that the state can outlive the
    /// bytes it was read from and go on over a longer copy of them.
    chunks: Vec<Range<usize>>,
}

impl SegmentState {
    fn new(kind: SegmentKind, max_passes: u32) -> Self {
        SegmentState {
            kind,
            passes: 0,
            kept: 0,
            max_passes,
            length: 0,
            chunks: Vec::new(),
        }
    }

    /// The placeholder passes of [`kept`](Self::kept): all of them while the
    /// cleanup pass is still to come.
    fn kept_placeholders(&self) -> u32 {
        match self.kind {
            SegmentKind::HtCleanup {
                placeholders: Some(p),
            } => self.kept.min(p),
            SegmentKind::HtCleanup { placeholders: None } => self.kept,
            _ => 0,
        }
    }

    /// The kept passes Tier-1 decodes: [`kept`](Self::kept) without the
    /// placeholder passes, which code nothing.
    fn coded_kept(&self) -> u32 {
        self.kept - self.kept_placeholders()
    }
}

impl BlockState {
    fn new(coder: Option<Coder>) -> Self {
        BlockState {
            coder,
            included: false,
            lblock: 3,
            zero_bit_planes: 0,
            num_passes: 0,
            segments: Vec::new(),
        }
    }

    /// The magnitude bit-planes an HT block skips past those the tag tree
    /// gives: one per three placeholder passes (`P0`, ISO/IEC 15444-15 B.3),
    /// counted only when the decode keeps the cleanup pass after them. A
    /// block without its cleanup pass decodes to zeros whatever the count.
    fn skipped_planes(&self) -> u32 {
        match self.segments.first() {
            Some(
                s @ SegmentState {
                    kind:
                        SegmentKind::HtCleanup {
                            placeholders: Some(p),
                        },
                    ..
                },
            ) if s.coded_kept() > 0 => p / 3,
            _ => 0,
        }
    }
}

/// One subband's decode state across the layers: a tag-tree pair **per
/// precinct**, plus a state per code-block of the whole band.
///
/// The trees are per precinct because a packet is per precinct — each one
/// decodes incrementally at a rising threshold over its own precinct's block
/// grid, and knows nothing of the blocks next door. The block states stay
/// band-indexed so that [`build_subbands`] can hand the band back whole; a block
/// is touched by exactly one precinct's packets, so the two indexings never
/// collide.
struct BandState {
    precincts: Vec<PrecinctState>,
    blocks: Vec<BlockState>,
}

/// The two tag trees one precinct runs over one subband: which code-blocks the
/// packet includes, and how many of their leading bit-planes are all zero.
struct PrecinctState {
    inclusion: TagTree,
    zero_bits: TagTree,
    /// With `strict` off: the first layer of the precinct whose packet the
    /// `POC` volumes leave out. It stays missing however much data comes.
    gap: Option<u32>,
    /// With `strict` off: the first layer whose packet of the precinct was
    /// not read, or read only in part, because the data ended. More data
    /// may bring it.
    ended: Option<u32>,
}

impl PrecinctState {
    /// The first layer of the precinct whose packet was not read whole,
    /// for either reason. Its code-blocks may then lack passes a later layer
    /// adds to their last segment.
    fn unread(&self) -> Option<u32> {
        match (self.gap, self.ended) {
            (Some(a), Some(b)) => Some(a.min(b)),
            (a, b) => a.or(b),
        }
    }
}

impl BandState {
    /// The bytes the band's state holds on the heap.
    fn heap_bytes(&self) -> usize {
        use std::mem::size_of;
        let precincts = self.precincts.capacity() * size_of::<PrecinctState>()
            + self
                .precincts
                .iter()
                .map(|p| p.inclusion.heap_bytes() + p.zero_bits.heap_bytes())
                .sum::<usize>();
        let blocks = self.blocks.capacity() * size_of::<BlockState>()
            + self
                .blocks
                .iter()
                .map(|b| {
                    b.segments.capacity() * size_of::<SegmentState>()
                        + b.segments
                            .iter()
                            .map(|s| s.chunks.capacity() * size_of::<Range<usize>>())
                            .sum::<usize>()
                })
                .sum::<usize>();
        precincts + blocks
    }

    /// Free each precinct's tag trees, once no packet is left to read
    /// them, and forget where the data ended.
    fn drop_trees(&mut self) {
        for precinct in &mut self.precincts {
            precinct.inclusion = TagTree::new(1, 1);
            precinct.zero_bits = TagTree::new(1, 1);
            precinct.ended = None;
        }
        self.precincts.shrink_to_fit();
    }

    /// `coder` is the tile-component's coder, `None` under MIXED.
    fn new(band: &BandGeom, coder: Option<Coder>) -> Self {
        BandState {
            precincts: band
                .precincts
                .iter()
                .map(|precinct| PrecinctState {
                    inclusion: TagTree::new(precinct.cols as u32, precinct.rows as u32),
                    zero_bits: TagTree::new(precinct.cols as u32, precinct.rows as u32),
                    gap: None,
                    ended: None,
                })
                .collect(),
            blocks: (0..band.block_count())
                .map(|_| BlockState::new(coder))
                .collect(),
        }
    }
}

/// The tag-tree nodes a packet's reads changed, as they were before, per
/// band of its resolution: the inclusion tree's and the zero bit-plane
/// tree's. With the block log (see [`Undo`]) and the stream positions, it
/// undoes a packet whole, so that a walk can stop inside a packet the data
/// cuts and read it again once more data comes.
#[derive(Debug, Default)]
pub(crate) struct Journal {
    trees: Vec<[Vec<Saved>; 2]>,
}

impl Journal {
    /// The bytes the notes hold on the heap, capacity included.
    fn heap_bytes(&self) -> usize {
        use std::mem::size_of;
        self.trees.capacity() * size_of::<[Vec<Saved>; 2]>()
            + self
                .trees
                .iter()
                .flatten()
                .map(|notes| notes.capacity() * size_of::<Saved>())
                .sum::<usize>()
    }

    /// Forget what the last packet noted.
    fn clear(&mut self) {
        for [inclusion, zero_bits] in &mut self.trees {
            inclusion.clear();
            zero_bits.clear();
        }
    }

    /// The two trees' notes for band `band`.
    fn band(&mut self, band: usize) -> &mut [Vec<Saved>; 2] {
        if self.trees.len() <= band {
            self.trees.resize_with(band + 1, Default::default);
        }
        &mut self.trees[band]
    }

    /// Put back the trees of `precinct` in `states` as they were before the
    /// packet that noted them.
    fn restore(&self, states: &mut [BandState], precinct: usize) {
        for (state, [inclusion, zero_bits]) in states.iter_mut().zip(&self.trees) {
            if let Some(trees) = state.precincts.get_mut(precinct) {
                trees.zero_bits.restore(zero_bits);
                trees.inclusion.restore(inclusion);
            }
        }
    }
}

/// What one codeword segment takes from *this* packet: the length of the byte
/// range that follows the header, and where to file it.
struct Contribution {
    band: usize,
    block: usize,
    segment: usize,
    seg_len: usize,
}

/// The buffers a packet read fills and empties again, kept across the
/// packets of a walk so that a packet allocates nothing of its own.
#[derive(Default)]
struct Scratch {
    /// The packet's contributions, in header order.
    contributions: Vec<Contribution>,
    /// The unread spans of one band's inclusion pass.
    unread: UnreadSpans,
}

impl Scratch {
    /// The bytes the buffers hold on the heap, capacity included.
    fn heap_bytes(&self) -> usize {
        self.contributions.capacity() * std::mem::size_of::<Contribution>()
            + self.unread.heap_bytes()
    }
}

/// How a packet read ended, when the walk is not strict.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Packet {
    /// The packet was read whole.
    Read,
    /// The data ended at or inside the packet. What it holds of whole
    /// code-blocks is kept; the rest of it is undone.
    Ended,
}

/// A code-block's state before a packet added to it, so that the packet can
/// be undone (see [`undo`]).
struct Undo {
    band: usize,
    block: usize,
    coder: Option<Coder>,
    included: bool,
    lblock: u32,
    zero_bit_planes: u32,
    num_passes: u32,
    /// How many segments the block had, and the state of the last of them.
    segments: usize,
    last: Option<LastSegment>,
}

/// The state of a block's last segment before a packet, which the packet may
/// add passes and bytes to, or, for an HT block's first cleanup segment, end.
struct LastSegment {
    kind: SegmentKind,
    passes: u32,
    kept: u32,
    max_passes: u32,
    length: u64,
    chunks: usize,
}

impl LastSegment {
    fn of(segment: &SegmentState) -> Self {
        LastSegment {
            kind: segment.kind,
            passes: segment.passes,
            kept: segment.kept,
            max_passes: segment.max_passes,
            length: segment.length,
            chunks: segment.chunks.len(),
        }
    }
}

/// Put back the state of every block in `log`, latest first, so that the
/// packet that logged them adds nothing to those blocks. Their earlier
/// layers stay.
fn undo(log: &[Undo], states: &mut [BandState]) {
    for entry in log.iter().rev() {
        let block = &mut states[entry.band].blocks[entry.block];
        block.coder = entry.coder;
        block.included = entry.included;
        block.lblock = entry.lblock;
        block.zero_bit_planes = entry.zero_bit_planes;
        block.num_passes = entry.num_passes;
        block.segments.truncate(entry.segments);
        if let (Some(last), Some(was)) = (block.segments.last_mut(), &entry.last) {
            last.kind = was.kind;
            last.passes = was.passes;
            last.kept = was.kept;
            last.max_passes = was.max_passes;
            last.length = was.length;
            last.chunks.truncate(was.chunks);
        }
    }
}

/// Parse one packet — one precinct of one resolution of one component in one
/// layer — starting at byte `start` of the tile-part `data`.
///
/// Folds the packet's contributions into `states` and returns the byte offset
/// where the next packet begins. The subbands are built once every layer has
/// been read; see [`build_subbands`].
///
/// With `keep` false (a layer past the decode's cap, see [`Budget::layers`]),
/// the packet is read the same way and its header still moves each
/// code-block's state on, but its passes and bytes are not added.
///
/// With `log` given (a walk with `strict` off), running out of data is not a
/// fault. A packet that ends past the data adds the code-blocks whose data is
/// all there and undoes the rest, from the first code-block whose data runs
/// out on, back to their earlier layers; a packet whose inline header runs out
/// adds nothing. Either is [`Packet::Ended`]. Every other fault fails as it
/// does when strict, a packed header that runs out included, and a packet the
/// strict walk reads is read the same way.
#[allow(clippy::too_many_arguments)]
fn parse_packet(
    streams: &mut PacketStreams<'_>,
    meter: &Meter,
    layer: u32,
    packet_index: u32,
    delimiters: Delimiters,
    style: u8,
    bands: &[BandGeom],
    precinct: usize,
    states: &mut [BandState],
    scratch: &mut Scratch,
    keep: bool,
    mut log: Option<&mut Vec<Undo>>,
    mut journal: Option<&mut Journal>,
) -> Result<Packet> {
    meter.spend(1, WorkRefusal::PacketWalk)?;
    let partial = log.is_some();
    if partial
        && delimiters.sop
        && peek_marker(streams.data, streams.body_pos) == Some(marker::SOP)
        && streams.body_pos + 6 > streams.data.len()
    {
        return Ok(Packet::Ended);
    }
    // SOP precedes the packet in the *body* stream when COD allows it — but only
    // *may* (A.8.1), so its absence is not an error, and it stays inline even
    // when the headers are packed. OpenJPEG warns and carries on.
    //
    // The peek is unambiguous: a packet header can never begin `FF 91`. Its
    // first byte may well be `0xFF`, but the header's bit stuffing then forces
    // the next byte's most significant bit to zero, so that byte is at most
    // `0x7F` and never `0x91`. See [`bio`].
    if delimiters.sop && peek_marker(streams.data, streams.body_pos) == Some(marker::SOP) {
        streams.body_pos = read_sop(streams.data, streams.body_pos, packet_index)?;
    }

    // Inline, the header starts where the body cursor now sits (past any SOP);
    // packed, it continues in the separate buffer.
    if !streams.packed {
        streams.header_pos = streams.body_pos;
    }
    // A packet header occupies at least one byte, so a cursor at or past the end
    // of the header stream means the codestream promised more packets than it
    // carries.
    if streams.header_pos >= streams.header.len() {
        if partial {
            return Ok(Packet::Ended);
        }
        return Err(Error::codestream(format!(
            "the data of tile {} ends before the last packet",
            streams.tile
        )));
    }

    let mut bio = BitReader::new(&streams.header[streams.header_pos..]);
    if let Some(log) = log.as_deref_mut() {
        log.clear();
    }

    // The first bit flags an empty packet (no contributions) against a present
    // one. An empty packet still costs its layer: the tag trees are untouched,
    // not reset.
    let present = bio.read_bit() == 1;
    let contributions = &mut scratch.contributions;
    contributions.clear();
    let mut fault = None;
    if present {
        for (band_index, (band, state)) in bands.iter().zip(states.iter_mut()).enumerate() {
            let parsed = parse_band_header(
                meter,
                band_index,
                &band.precincts[precinct],
                &mut state.precincts[precinct],
                &mut state.blocks,
                layer,
                style,
                keep,
                &mut bio,
                contributions,
                &mut scratch.unread,
                log.as_deref_mut(),
                journal.as_deref_mut().map(|j| j.band(band_index)),
            );
            if let Err(error) = parsed {
                fault = Some(error);
                break;
            }
        }
    }
    // A packed header that reads past its bytes is damaged, strict or not:
    // the bits it needs are not in the codestream. Its bytes sit in the main
    // or tile-part headers, which are there whole or not at all, so this is
    // never the data ending. It is checked before any fault, which the fill
    // bits may have caused, so the overrun is what is reported.
    if streams.packed && bio.overran() {
        return Err(packed_overrun());
    }
    if let Some(error) = fault {
        // Not strict, a fault in an inline header that runs past the data is
        // the data ending, not damage: the bits past the end are fill.
        return match log.as_deref() {
            Some(log) if bio.overran() => {
                undo(log, states);
                Ok(Packet::Ended)
            }
            _ => Err(error),
        };
    }

    // The header is a whole number of bytes. EPH, when COD signals it, sits
    // between the header and the body — after an empty packet's header too — and
    // moves into the packed buffer with the headers, so it is read from the
    // header stream.
    //
    // Not strict, an inline header whose bits run past the data is the data
    // ending. That is taken before the alignment, which past a last byte of
    // 0xFF reads one more byte that a strict decode does not ask for. Packed
    // headers are not checked here: their bytes sit in the main or tile-part
    // headers, which are there whole or not at all, so one that runs short
    // was refused above, as damage, and one the alignment runs past is
    // refused below.
    let header_overran = bio.overran() && !streams.packed;
    bio.align();
    // A packed header whose last byte is 0xFF needs the stuffed byte after it
    // (B.10.1). Without one the header is damaged, as OpenJPEG's
    // `opj_bio_inalign` also finds.
    if streams.packed && bio.overran() {
        return Err(packed_overrun());
    }
    streams.header_pos += bio.bytes_consumed();
    if let Some(log) = log.as_deref() {
        let eph_cut = delimiters.eph && streams.header_pos + 2 > streams.header.len();
        if header_overran || eph_cut {
            undo(log, states);
            return Ok(Packet::Ended);
        }
    }
    if delimiters.eph {
        if peek_marker(streams.header, streams.header_pos) != Some(marker::EPH) {
            return Err(Error::codestream(
                "`COD` signals `EPH` but the packet header is not followed by one",
            ));
        }
        streams.header_pos += 2;
    }

    // Inline, the body follows the header in the same buffer.
    if !streams.packed {
        streams.body_pos = streams.header_pos;
    }
    for contribution in contributions.iter() {
        let end = match streams
            .body_pos
            .checked_add(contribution.seg_len)
            .filter(|&e| e <= streams.data.len())
        {
            Some(end) => end,
            None => {
                if let Some(log) = log.as_deref() {
                    // Keep the blocks before this one; undo this one and every
                    // block after it in the header.
                    let from = log
                        .iter()
                        .position(|u| (u.band, u.block) == (contribution.band, contribution.block))
                        .unwrap_or(0);
                    undo(&log[from..], states);
                    return Ok(Packet::Ended);
                }
                return Err(Error::codestream(format!(
                    "a packet body segment overruns the data of tile {}",
                    streams.tile
                )));
            }
        };
        if keep {
            states[contribution.band].blocks[contribution.block].segments[contribution.segment]
                .chunks
                .push(streams.body_pos..end);
        }
        streams.body_pos = end;
    }
    // Inline, keep the two cursors together for the next packet.
    if !streams.packed {
        streams.header_pos = streams.body_pos;
    }

    Ok(Packet::Read)
}

/// Read one precinct's code-block entries, on one subband, from this layer's
/// packet header (ISO B.10): per block its inclusion, and for a contributing
/// block the zero-bitplane count (first inclusion only), coding-pass count, and
/// the length of its byte contribution.
///
/// The blocks are visited in raster order **within the precinct**, and the tag
/// trees are addressed in the precinct's own coordinates — the packet header
/// knows nothing of the band's wider grid, so a band-relative index here would
/// address the wrong leaf on every precinct but the first.
///
/// A block not yet included can sit under an inclusion-tree node already
/// known to lie past this layer. Its entry then reads no bit, nor does any
/// other entry under that node, so the walk steps over that node's blocks at
/// once (see [`TagTree::unread_span`]): along the row, or over all of its rows
/// when it spans the whole precinct width, and again on each row below that
/// it reaches (see [`UnreadSpans`]). Each such step spends a visit. Any
/// other entry reads at least one bit, so its work is paid for by the header's
/// bytes and is not counted.
///
/// With `keep` false the entries are read the same way, and the passes still
/// fill the segments so that the next layer's lengths are read at the right
/// width, but they are not counted as kept (see [`SegmentState::kept`]).
#[allow(clippy::too_many_arguments)]
fn parse_band_header(
    meter: &Meter,
    band_index: usize,
    precinct: &PrecinctGeom,
    trees: &mut PrecinctState,
    blocks: &mut [BlockState],
    layer: u32,
    style: u8,
    keep: bool,
    bio: &mut BitReader,
    contributions: &mut Vec<Contribution>,
    unread: &mut UnreadSpans,
    mut log: Option<&mut Vec<Undo>>,
    mut saved: Option<&mut [Vec<Saved>; 2]>,
) -> Result<()> {
    let cols = precinct.cols as u32;
    let threshold = layer + 1;

    unread.clear();
    let mut entry = 0;
    while entry < precinct.cols * precinct.rows {
        let bx = entry as u32 % cols;
        let by = entry as u32 / cols;
        let block_index = precinct.block(bx as usize, by as usize);
        entry += 1;

        let was_included = blocks[block_index].included;
        let span = if was_included {
            None
        } else {
            unread.find(&trees.inclusion, bx, by, threshold)
        };
        if let Some(span) = span {
            meter.spend(1, WorkRefusal::PacketWalk)?;
            entry = span.next_entry(by, cols);
            continue;
        }
        let contributes = if was_included {
            // Already included: one bit says whether this layer adds passes.
            bio.read_bit() == 1
        } else {
            // Not yet included: the inclusion tree resolves to the first layer
            // that carries this block. Reading at `layer + 1` asks "is that
            // layer at most this one?" and leaves the tree part-decoded when it
            // is not, so the next layer resumes where this one stopped.
            let saved = saved.as_deref_mut().map(|[inclusion, _]| inclusion);
            match trees.inclusion.read_saving(bx, by, threshold, bio, saved) {
                Some(_) => {
                    blocks[block_index].included = true;
                    true
                }
                None => false,
            }
        };
        if !contributes {
            continue;
        }
        if let Some(log) = log.as_deref_mut() {
            let block = &blocks[block_index];
            log.push(Undo {
                band: band_index,
                block: block_index,
                coder: block.coder,
                included: was_included,
                lblock: block.lblock,
                zero_bit_planes: block.zero_bit_planes,
                num_passes: block.num_passes,
                segments: block.segments.len(),
                last: block.segments.last().map(LastSegment::of),
            });
        }

        // The zero-bitplane tree is resolved in full the first time a block is
        // included, so a single read at the ceiling settles it. Later layers
        // never read it again.
        if !was_included {
            let saved = saved.as_deref_mut().map(|[_, zero_bits]| zero_bits);
            blocks[block_index].zero_bit_planes = trees
                .zero_bits
                .read_saving(bx, by, ZBP_LIMIT, bio, saved)
                .ok_or_else(|| {
                    Error::codestream(format!(
                        "code-block zero-bitplane count is over {}, the most a valid stream \
                         can signal",
                        ZBP_LIMIT - 1
                    ))
                })?;
        }

        // `read_num_passes` always returns >= 1, so the `ilog2` below never hits
        // the zero case.
        let num_passes = read_num_passes(bio);

        // Lblock grows by a unary run of 1s and carries into later layers; the
        // length field is then `Lblock + floor(log2(num_passes))` bits wide
        // (ISO B.10.7.5), over *this* contribution's passes, not the running
        // total. OpenJPEG spells the same thing `numlenbits +
        // floorlog2(seg->numnewpasses)`.
        //
        // One length field, because one codeword segment. A contribution splits
        // into several segments only under a code-block style that terminates
        // (`termall`, `bypass`); there OpenJPEG's `do { ... } while (n > 0)`
        // reads a length per segment, which the loop below mirrors.
        let block = &mut blocks[block_index];
        while bio.read_bit() == 1 {
            block.lblock += 1;
            if block.lblock > LBLOCK_MAX {
                return Err(Error::codestream(format!(
                    "code-block `Lblock` grows past the limit of {LBLOCK_MAX}"
                )));
            }
        }
        // The passes are handed to codeword segments in turn, each with its own
        // length field. Without termination one segment swallows them all;
        // under `restart` each pass terminates, so each gets a segment and a
        // length of its own. OpenJPEG spells this `do { ... } while (n > 0)`.
        //
        // An HT block's segments follow ISO/IEC 15444-15 B.2 instead (see
        // [`next_segment`]), once its first cleanup segment has ended. Until
        // then a contribution may be placeholder passes only, or end that
        // segment at its cleanup pass, which [`ht_first_cleanup`] tells apart.
        let mut remaining = num_passes;
        let mut placeholders = 0;
        let open = SegmentKind::HtCleanup { placeholders: None };
        let first_open = block.segments.first().is_none_or(|s| s.kind == open);
        let prior = block.segments.first().map_or(0, |s| s.passes);
        // A Part 1 block's length field, when reading it settled the coder
        // of a MIXED block.
        let mut part1_length = None;
        let first = match block.coder {
            Some(Coder::Ht) if first_open => {
                Some(ht_first_cleanup(block.lblock, prior, num_passes, bio)?)
            }
            Some(_) => None,
            None => match mixed_first_length(block.lblock, prior, num_passes, bio)? {
                MixedFirst::Empty => Some(FirstCleanup::Placeholders),
                MixedFirst::Ht(first) => {
                    block.coder = Some(Coder::Ht);
                    Some(first)
                }
                MixedFirst::Part1 { length } => {
                    become_part1(block);
                    part1_length = Some(length);
                    None
                }
            },
        };
        if let Some(first) = first {
            if block.segments.is_empty() {
                block.segments.push(SegmentState::new(open, u32::MAX));
            }
            let segment = &mut block.segments[0];
            let passes = match first {
                FirstCleanup::Placeholders => {
                    placeholders = num_passes;
                    num_passes
                }
                FirstCleanup::Cleanup {
                    placeholders: before,
                    passes,
                    length,
                } => {
                    placeholders = before - prior;
                    segment.kind = SegmentKind::HtCleanup {
                        placeholders: Some(before),
                    };
                    segment.max_passes = before + 1;
                    add_length(segment, length)?;
                    contributions.push(Contribution {
                        band: band_index,
                        block: block_index,
                        segment: 0,
                        seg_len: length as usize,
                    });
                    passes
                }
            };
            segment.passes += passes;
            if keep {
                segment.kept += passes;
            }
            remaining -= passes;
        }
        if keep {
            block.num_passes += num_passes - placeholders;
        }
        while remaining > 0 {
            if block
                .segments
                .last()
                .is_none_or(|s| s.passes >= s.max_passes)
            {
                let coder = block.coder.unwrap_or(Coder::Ht);
                let (kind, max_passes) = next_segment(coder, style, block.segments.last());
                block.segments.push(SegmentState::new(kind, max_passes));
            }
            let segment_index = block.segments.len() - 1;
            let segment = &mut block.segments[segment_index];
            let new_passes = remaining.min(segment.max_passes - segment.passes);

            // The length field's width uses *this* segment's new passes.
            let seg_len = match part1_length.take() {
                Some(length) => length,
                None => bio.read(length_bits(block.lblock, new_passes)),
            };
            add_length(segment, seg_len)?;
            let seg_len = seg_len as usize;

            // A layer may add coding passes and no bytes (B.10.7 sets no
            // lower bound on the length). The MQ codeword is continuous
            // within a segment, so the encoder's rate split can put passes in
            // one layer and the bytes carrying them in the next. A segment
            // that never gets a byte is decoded from the `0xFF` fill (D.4.1),
            // as OpenJPEG decodes it. The passes still count and still fill
            // the segment, so a later layer's passes land in the right one.
            // A zero length files no chunk, and reading it took at least the
            // header bits above, so it costs no more than one with bytes.
            let segment = &mut block.segments[segment_index];
            segment.passes += new_passes;
            if keep {
                segment.kept += new_passes;
            }
            if seg_len > 0 {
                contributions.push(Contribution {
                    band: band_index,
                    block: block_index,
                    segment: segment_index,
                    seg_len,
                });
            }

            remaining -= new_passes;
        }
    }
    Ok(())
}

/// Note that the packet of `precinct` in `layer` was not read whole because
/// the data ended (see [`PrecinctState::ended`]). A packet spans every band
/// of the resolution.
fn mark_unread(bands: &mut [BandState], precinct: usize, layer: u32) {
    for band in bands {
        if let Some(state) = band.precincts.get_mut(precinct) {
            state.ended = Some(state.ended.map_or(layer, |from| from.min(layer)));
        }
    }
}

/// Note that the `POC` volumes leave out the packets of every precinct of
/// a resolution from `layer` on (see [`PrecinctState::gap`]).
fn mark_gap(bands: &mut [BandState], layer: u32) {
    for band in bands {
        for state in &mut band.precincts {
            state.gap = Some(state.gap.map_or(layer, |from| from.min(layer)));
        }
    }
}

/// Turn the accumulated per-layer state of one resolution into its subbands.
///
/// A block may end up with coding passes and no bytes, in one codeword segment
/// or in all of them, since B.10.7 lets a contribution's length be zero. Tier-1
/// decodes such a segment from the `0xFF` bytes D.4.1 appends, as OpenJPEG
/// does.
///
/// `data` is the tile data the segments' chunks are offsets into. The state
/// is left as it was, so a walk can go on from it.
fn build_subbands<'a>(
    bands: &[BandGeom],
    states: &[BandState],
    data: &'a [u8],
) -> Vec<Subband<'a>> {
    bands
        .iter()
        .zip(states)
        .map(|(band, state)| {
            // The rectangles come from a nested loop, which gives no exact
            // length, so the vector is sized up front rather than grown.
            debug_assert_eq!(
                state.blocks.len(),
                band.block_count(),
                "one block state per code-block of the band"
            );
            // The code-blocks of the precincts the data ended in or before.
            // A precinct owns a rectangle of the band's blocks, and each
            // block lies in one precinct, so this visits each block at most
            // once. A walk that read every packet allocates nothing here.
            let mut unread = Vec::new();
            for (geom, precinct) in band.precincts.iter().zip(&state.precincts) {
                if let Some(from) = precinct.unread() {
                    unread.resize(state.blocks.len(), None);
                    for by in 0..geom.rows {
                        for bx in 0..geom.cols {
                            if let Some(flag) = unread.get_mut(geom.block(bx, by)) {
                                *flag = Some(from);
                            }
                        }
                    }
                }
            }
            let mut blocks = Vec::with_capacity(state.blocks.len());
            blocks.extend(band.block_rects().zip(&state.blocks).enumerate().map(
                |(index, ((x, y, width, height), block))| {
                    let missing_from = unread.get(index).copied().flatten();
                    let unread = missing_from.is_some();
                    // Under a layer cap, the segments the kept layers never
                    // reached are left out, and the last one kept may hold
                    // only some of its passes. Without a cap every pass is
                    // kept. The filter is what keeps a cut segment last:
                    // Tier-1 exempts only the last segment from `predictable
                    // termination`, so an empty segment left after the cut
                    // one would have it checked and refused.
                    //
                    // An HT block's placeholder passes code nothing, so they
                    // count as neither passes nor a segment (see
                    // [`SegmentState::coded_kept`]); they skip bit-planes
                    // instead.
                    //
                    // With `strict` off, a block whose precinct the data
                    // ended in may have lost later passes of its last
                    // segment, unless that segment holds all the passes it
                    // can: then the next pass opens a new segment, and this
                    // one is whole.
                    let mut cut = false;
                    let coder = block.coder.unwrap_or(Coder::Ht);
                    let zero_bit_planes = block.zero_bit_planes + block.skipped_planes();
                    let placeholders = block
                        .segments
                        .first()
                        .map_or(0, SegmentState::kept_placeholders);
                    let segments = block
                        .segments
                        .iter()
                        .take_while(|s| s.coded_kept() > 0)
                        .map(|s| {
                            cut = s.kept < s.passes || (unread && s.passes < s.max_passes);
                            CodedSegment {
                                passes: s.coded_kept(),
                                chunks: s.chunks.iter().map(|r| &data[r.clone()]).collect(),
                            }
                        })
                        .collect();
                    CodeBlock {
                        x,
                        y,
                        width,
                        height,
                        num_passes: block.num_passes,
                        zero_bit_planes,
                        segments,
                        cut,
                        placeholders,
                        coder,
                        missing_from,
                    }
                },
            ));
            Subband {
                kind: band.kind,
                origin: band.origin,
                width: band.width,
                height: band.height,
                block_cols: band.block_cols,
                block_rows: band.block_rows,
                blocks,
            }
        })
        .collect()
}

/// Decode the number of coding passes (ISO Table B.4 / OpenJPEG
/// `opj_t2_getnumpasses`): a prefix code spanning 1 to 164 passes.
fn read_num_passes(bio: &mut BitReader) -> u32 {
    if bio.read_bit() == 0 {
        return 1;
    }
    if bio.read_bit() == 0 {
        return 2;
    }
    let n = bio.read(2);
    if n != 3 {
        return 3 + n;
    }
    let n = bio.read(5);
    if n != 31 {
        return 6 + n;
    }
    37 + bio.read(7)
}

#[cfg(test)]
mod partial_tests;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod work_bound_tests;