bioformats 0.1.3

Pure Rust reimplementation of Bio-Formats — read/write scientific image formats
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
//! Camera and RAW format readers — PCO, Bio-Rad GEL, Li-Cor L2D, and more.
//!
//! Includes three binary readers with partial metadata parsing (PcoRawReader,
//! BioRadGelReader, L2dReader) and several extension-only placeholder readers.

use std::collections::HashMap;
use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};

use crate::common::error::{BioFormatsError, Result};
use crate::common::metadata::{DimensionOrder, ImageMetadata, MetadataValue};
use crate::common::pixel_type::PixelType;
use crate::common::reader::FormatReader;
use crate::common::region::crop_full_plane;

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------
fn placeholder_meta_u16() -> ImageMetadata {
    ImageMetadata {
        size_x: 512,
        size_y: 512,
        size_z: 1,
        size_c: 1,
        size_t: 1,
        pixel_type: PixelType::Uint16,
        bits_per_pixel: 16,
        image_count: 1,
        dimension_order: DimensionOrder::XYZCT,
        is_rgb: false,
        is_interleaved: false,
        is_indexed: false,
        is_little_endian: true,
        resolution_count: 1,
        series_metadata: HashMap::new(),
        lookup_table: None,
        modulo_z: None,
        modulo_c: None,
        modulo_t: None,
    }
}

// ---------------------------------------------------------------------------
// Shared RAW / Bayer-CFA pixel helpers
//
// Faithful Rust port of the pixel path used by the Java RAW-camera readers:
//   * `loci.common.DataTools.unpackBytes(long, byte[], int, int, boolean)`
//   * `loci.formats.ImageTools.interpolate(short[], byte[], int[], int, int, boolean)`
//   * the MSB-first bit reader used by `RandomAccessInputStream.readBits`
//
// These are used by `CanonRawReader` (this file) and by the MRW and DNG CFA
// paths in `extended.rs`, which re-export them via `pub(crate)`.
// ---------------------------------------------------------------------------
pub(crate) mod cfa {
    /// Port of `loci.common.DataTools.unpackBytes`.
    ///
    /// Writes the low `nbytes` bytes of `value` into `buf` starting at `ndx`,
    /// little- or big-endian. Matches the Java implementation byte-for-byte:
    /// little-endian stores byte `i` as `(value >> (8*i)) & 0xff`; big-endian
    /// stores byte `i` as `(value >> (8*(nbytes-i-1))) & 0xff`.
    pub fn unpack_bytes(value: i64, buf: &mut [u8], ndx: usize, nbytes: usize, little: bool) {
        if little {
            for i in 0..nbytes {
                buf[ndx + i] = ((value >> (8 * i)) & 0xff) as u8;
            }
        } else {
            for i in 0..nbytes {
                buf[ndx + i] = ((value >> (8 * (nbytes - i - 1))) & 0xff) as u8;
            }
        }
    }

    /// MSB-first bit reader matching `RandomAccessInputStream.readBits` /
    /// `skipBits` used by the Java RAW readers (bits are consumed from the most
    /// significant bit of each successive byte).
    pub struct BitReader<'a> {
        data: &'a [u8],
        /// Absolute bit position into `data`.
        bit_pos: usize,
    }

    impl<'a> BitReader<'a> {
        pub fn new(data: &'a [u8]) -> Self {
            BitReader { data, bit_pos: 0 }
        }

        /// Read `n` bits MSB-first as an unsigned value. Reads past the end of
        /// the buffer yield zero bits, mirroring Java's behaviour of returning
        /// -1/EOF bits as 0 once exhausted (callers size buffers to the data).
        pub fn read_bits(&mut self, n: u32) -> u32 {
            let mut value: u32 = 0;
            for _ in 0..n {
                let byte_index = self.bit_pos >> 3;
                let bit_index = 7 - (self.bit_pos & 7);
                let bit = if byte_index < self.data.len() {
                    (self.data[byte_index] >> bit_index) & 1
                } else {
                    0
                };
                value = (value << 1) | bit as u32;
                self.bit_pos += 1;
            }
            value
        }

        /// Skip `n` bits (port of `skipBits`).
        pub fn skip_bits(&mut self, n: usize) {
            self.bit_pos += n;
        }
    }

    /// Port of `loci.formats.ImageTools.interpolate`.
    ///
    /// `s` is a planar short buffer of length `width*height*3` laid out as three
    /// stacked planes [R | G | B]. `buf` receives interleaved RGB samples
    /// (R,G,B per pixel, 2 bytes each, in the given byte order). `bayer_pattern`
    /// is a 4-element color map indexed by `(row%2)*2 + (col%2)` where the value
    /// names the channel present at that CFA position (0=R, 1=G, 2=B).
    ///
    /// This is NOT a fancy demosaic: it is exactly Java's nearest-neighbour
    /// average per missing component (the same algorithm Java DNG/MRW/Canon use).
    pub fn interpolate(
        s: &[i16],
        buf: &mut [u8],
        bayer_pattern: &[i32; 4],
        width: usize,
        height: usize,
        little_endian: bool,
    ) {
        if width == 1 && height == 1 {
            for b in buf.iter_mut() {
                *b = s[0] as u8;
            }
            return;
        }

        let plane = width * height;

        for row in 0..height {
            for col in 0..width {
                let even_col = (col % 2) == 0;

                let index = (row % 2) * 2 + (col % 2);
                let need_green = bayer_pattern[index] != 1;
                let need_red = bayer_pattern[index] != 0;
                let need_blue = bayer_pattern[index] != 2;

                // --- Green channel (buf offset +2) ---
                if need_green {
                    let mut sum: i32 = 0;
                    let mut ncomps = 0i32;
                    if row > 0 {
                        sum += s[plane + (row - 1) * width + col] as i32;
                        ncomps += 1;
                    }
                    if row < height - 1 {
                        sum += s[plane + (row + 1) * width + col] as i32;
                        ncomps += 1;
                    }
                    if col > 0 {
                        sum += s[plane + row * width + col - 1] as i32;
                        ncomps += 1;
                    }
                    if col < width - 1 {
                        sum += s[plane + row * width + col + 1] as i32;
                        ncomps += 1;
                    }
                    let v = (sum / ncomps) as i16;
                    unpack_bytes(
                        v as i64,
                        buf,
                        row * width * 6 + col * 6 + 2,
                        2,
                        little_endian,
                    );
                } else {
                    unpack_bytes(
                        s[plane + row * width + col] as i64,
                        buf,
                        row * width * 6 + col * 6 + 2,
                        2,
                        little_endian,
                    );
                }

                // --- Red channel (buf offset +0) ---
                if need_red {
                    let mut sum: i32 = 0;
                    let mut ncomps = 0i32;
                    if !need_blue {
                        // four corners
                        if row > 0 {
                            if col > 0 {
                                sum += s[(row - 1) * width + col - 1] as i32;
                                ncomps += 1;
                            }
                            if col < width - 1 {
                                sum += s[(row - 1) * width + col + 1] as i32;
                                ncomps += 1;
                            }
                        }
                        if row < height - 1 {
                            if col > 0 {
                                sum += s[(row + 1) * width + col - 1] as i32;
                                ncomps += 1;
                            }
                            if col < width - 1 {
                                sum += s[(row + 1) * width + col + 1] as i32;
                                ncomps += 1;
                            }
                        }
                    } else if (even_col && bayer_pattern[index + 1] == 0)
                        || (!even_col && bayer_pattern[index - 1] == 0)
                    {
                        // horizontal
                        if col > 0 {
                            sum += s[row * width + col - 1] as i32;
                            ncomps += 1;
                        }
                        if col < width - 1 {
                            sum += s[row * width + col + 1] as i32;
                            ncomps += 1;
                        }
                    } else {
                        // vertical
                        if row > 0 {
                            sum += s[(row - 1) * width + col] as i32;
                            ncomps += 1;
                        }
                        if row < height - 1 {
                            sum += s[(row + 1) * width + col] as i32;
                            ncomps += 1;
                        }
                    }
                    let v = (sum / ncomps) as i16;
                    unpack_bytes(v as i64, buf, row * width * 6 + col * 6, 2, little_endian);
                } else {
                    unpack_bytes(
                        s[row * width + col] as i64,
                        buf,
                        row * width * 6 + col * 6,
                        2,
                        little_endian,
                    );
                }

                // --- Blue channel (buf offset +4) ---
                if need_blue {
                    let mut sum: i32 = 0;
                    let mut ncomps = 0i32;
                    if !need_red {
                        // four corners
                        if row > 0 {
                            if col > 0 {
                                sum += s[(2 * height + row - 1) * width + col - 1] as i32;
                                ncomps += 1;
                            }
                            if col < width - 1 {
                                sum += s[(2 * height + row - 1) * width + col + 1] as i32;
                                ncomps += 1;
                            }
                        }
                        if row < height - 1 {
                            if col > 0 {
                                sum += s[(2 * height + row + 1) * width + col - 1] as i32;
                                ncomps += 1;
                            }
                            if col < width - 1 {
                                sum += s[(2 * height + row + 1) * width + col + 1] as i32;
                                ncomps += 1;
                            }
                        }
                    } else if (even_col && bayer_pattern[index + 1] == 2)
                        || (!even_col && bayer_pattern[index - 1] == 2)
                    {
                        // horizontal
                        if col > 0 {
                            sum += s[(2 * height + row) * width + col - 1] as i32;
                            ncomps += 1;
                        }
                        if col < width - 1 {
                            sum += s[(2 * height + row) * width + col + 1] as i32;
                            ncomps += 1;
                        }
                    } else {
                        // vertical
                        if row > 0 {
                            sum += s[(2 * height + row - 1) * width + col] as i32;
                            ncomps += 1;
                        }
                        if row < height - 1 {
                            sum += s[(2 * height + row + 1) * width + col] as i32;
                            ncomps += 1;
                        }
                    }
                    let v = (sum / ncomps) as i16;
                    unpack_bytes(
                        v as i64,
                        buf,
                        row * width * 6 + col * 6 + 4,
                        2,
                        little_endian,
                    );
                } else {
                    unpack_bytes(
                        s[2 * plane + row * width + col] as i64,
                        buf,
                        row * width * 6 + col * 6 + 4,
                        2,
                        little_endian,
                    );
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Macro for TIFF wrapper readers
// ---------------------------------------------------------------------------
macro_rules! tiff_wrapper {
    (
        $(#[$attr:meta])*
        pub struct $name:ident;
        extensions: [$($ext:literal),+];
    ) => {
        $(#[$attr])*
        pub struct $name {
            inner: crate::tiff::TiffReader,
        }

        impl $name {
            pub fn new() -> Self {
                $name { inner: crate::tiff::TiffReader::new() }
            }
        }

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

        impl FormatReader for $name {
            fn is_this_type_by_name(&self, path: &Path) -> bool {
                let ext = path.extension()
                    .and_then(|e| e.to_str())
                    .map(|e| e.to_ascii_lowercase());
                matches!(ext.as_deref(), $(Some($ext))|+)
            }

            fn is_this_type_by_bytes(&self, _header: &[u8]) -> bool { false }

            fn set_id(&mut self, path: &Path) -> Result<()> {
                self.inner.set_id(path)
            }

            fn close(&mut self) -> Result<()> {
                self.inner.close()
            }

            fn series_count(&self) -> usize {
                self.inner.series_count()
            }

            fn set_series(&mut self, s: usize) -> Result<()> {
                self.inner.set_series(s)
            }

            fn series(&self) -> usize {
                self.inner.series()
            }

            fn metadata(&self) -> &ImageMetadata {
                self.inner.metadata()
            }

            fn open_bytes(&mut self, p: u32) -> Result<Vec<u8>> {
                self.inner.open_bytes(p)
            }

            fn open_bytes_region(&mut self, p: u32, x: u32, y: u32, w: u32, h: u32) -> Result<Vec<u8>> {
                self.inner.open_bytes_region(p, x, y, w, h)
            }

            fn open_thumb_bytes(&mut self, p: u32) -> Result<Vec<u8>> {
                self.inner.open_thumb_bytes(p)
            }

            fn resolution_count(&self) -> usize {
                self.inner.resolution_count()
            }

            fn set_resolution(&mut self, level: usize) -> Result<()> {
                self.inner.set_resolution(level)
            }
        }
    };
}

// ---------------------------------------------------------------------------
// 1. PCO B16 raw camera file
// ---------------------------------------------------------------------------
/// PCO camera raw B16 binary format (`.b16`).
///
/// Header is 216 bytes; width at offset 4 (u16 LE), height at offset 6 (u16 LE).
/// Pixel data starts at offset 216 as 16-bit little-endian grayscale values.
pub struct PcoRawReader {
    path: Option<PathBuf>,
    meta: Option<ImageMetadata>,
}

impl PcoRawReader {
    pub fn new() -> Self {
        PcoRawReader {
            path: None,
            meta: None,
        }
    }
}

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

impl FormatReader for PcoRawReader {
    fn is_this_type_by_name(&self, path: &Path) -> bool {
        matches!(
            path.extension()
                .and_then(|e| e.to_str())
                .map(|e| e.to_ascii_lowercase())
                .as_deref(),
            Some("b16")
        )
    }

    fn is_this_type_by_bytes(&self, _header: &[u8]) -> bool {
        false
    }

    fn set_id(&mut self, path: &Path) -> Result<()> {
        self.close()?;
        let mut f = std::fs::File::open(path).map_err(|e| BioFormatsError::Io(e))?;
        let file_size = f.metadata().map_err(BioFormatsError::Io)?.len();
        let mut header = [0u8; 216];
        let n = f.read(&mut header).map_err(|e| BioFormatsError::Io(e))?;
        let (w, h) = if n >= 8 {
            let w = u16::from_le_bytes([header[4], header[5]]) as u32;
            let h = u16::from_le_bytes([header[6], header[7]]) as u32;
            if w == 0 || h == 0 {
                return Err(BioFormatsError::UnsupportedFormat(
                    "PCO B16 header contains zero image dimensions".into(),
                ));
            } else {
                (w, h)
            }
        } else {
            return Err(BioFormatsError::UnsupportedFormat(
                "PCO B16 header is too short to contain dimensions".into(),
            ));
        };
        let expected = (w as u64)
            .checked_mul(h as u64)
            .and_then(|pixels| pixels.checked_mul(2))
            .and_then(|bytes| bytes.checked_add(216))
            .ok_or_else(|| {
                BioFormatsError::UnsupportedFormat("PCO B16 declared dimensions overflow".into())
            })?;
        if file_size < expected {
            return Err(BioFormatsError::UnsupportedFormat(format!(
                "PCO B16 file is too short for declared dimensions {w}x{h}"
            )));
        }
        self.path = Some(path.to_path_buf());
        self.meta = Some(ImageMetadata {
            size_x: w,
            size_y: h,
            pixel_type: PixelType::Uint16,
            bits_per_pixel: 16,
            is_little_endian: true,
            ..placeholder_meta_u16()
        });
        Ok(())
    }

    fn close(&mut self) -> Result<()> {
        self.path = None;
        self.meta = None;
        Ok(())
    }

    fn series_count(&self) -> usize {
        usize::from(self.meta.is_some())
    }

    fn set_series(&mut self, s: usize) -> Result<()> {
        if self.meta.is_none() {
            return Err(BioFormatsError::NotInitialized);
        }
        if s != 0 {
            Err(BioFormatsError::SeriesOutOfRange(s))
        } else {
            Ok(())
        }
    }

    fn series(&self) -> usize {
        0
    }

    fn metadata(&self) -> &ImageMetadata {
        self.meta
            .as_ref()
            .unwrap_or(crate::common::reader::uninitialized_metadata())
    }

    fn open_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        if plane_index >= meta.image_count {
            return Err(BioFormatsError::PlaneOutOfRange(plane_index));
        }
        let n_bytes = meta.size_x as usize * meta.size_y as usize * 2;
        let path = self.path.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let mut f = std::fs::File::open(path).map_err(|e| BioFormatsError::Io(e))?;
        f.seek(SeekFrom::Start(216))
            .map_err(|e| BioFormatsError::Io(e))?;
        let mut buf = vec![0u8; n_bytes];
        f.read_exact(&mut buf).map_err(|e| BioFormatsError::Io(e))?;
        Ok(buf)
    }

    fn open_bytes_region(
        &mut self,
        plane_index: u32,
        _x: u32,
        _y: u32,
        w: u32,
        h: u32,
    ) -> Result<Vec<u8>> {
        let full = self.open_bytes(plane_index)?;
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        crop_full_plane("PCO B16", &full, meta, 1, _x, _y, w, h)
    }

    fn open_thumb_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let tw = meta.size_x.min(256);
        let th = meta.size_y.min(256);
        let tx = (meta.size_x - tw) / 2;
        let ty = (meta.size_y - th) / 2;
        self.open_bytes_region(plane_index, tx, ty, tw, th)
    }
}

// ---------------------------------------------------------------------------
// 2. Bio-Rad GEL phosphor imager (.1sc)
// ---------------------------------------------------------------------------
/// Bio-Rad GEL phosphor imager format (`.1sc`).
///
/// Port of BioRadGelReader.java: magic 0xafaf, chunk-walks from offsets
/// START_OFFSET (160) / BASE_OFFSET (352), reads bpp (2 or 4 bytes, the latter
/// being FLOAT), and a dynamic pixel offset relative to PIXEL_OFFSET (59654).
pub struct BioRadGelReader {
    path: Option<PathBuf>,
    meta: Option<ImageMetadata>,
    /// Whether the on-disk values are little-endian ("Intel Format").
    little_endian: bool,
    /// Java `diff = BASE_OFFSET - baseFP`, used to pick the pixel offset.
    diff: i64,
}

const BRG_MAGIC: u16 = 0xafaf;
const BRG_PIXEL_OFFSET: u64 = 59654;
const BRG_START_OFFSET: u64 = 160;
const BRG_BASE_OFFSET: i64 = 352;

impl BioRadGelReader {
    pub fn new() -> Self {
        BioRadGelReader {
            path: None,
            meta: None,
            little_endian: false,
            diff: 0,
        }
    }

    /// Compute the seek position for the pixel data, mirroring openBytes() in
    /// BioRadGelReader.java. Returns None when no special offset applies and the
    /// caller should fall back to (file_len - plane_size).
    fn pixel_seek(&self, f: &mut std::fs::File, plane_size: u64, file_len: u64) -> Result<u64> {
        if BRG_PIXEL_OFFSET + plane_size < file_len {
            if self.diff < 0 {
                let mut pos = 0x379d1u64;
                if pos + plane_size > file_len {
                    pos = BRG_PIXEL_OFFSET + 62;
                }
                Ok(pos)
            } else if self.diff == 0 {
                Ok(BRG_PIXEL_OFFSET)
            } else if file_len - plane_size > 61000 {
                // Scan backwards for the "scn0x" marker starting near
                // PIXEL_OFFSET - 196, then skip a variable metadata block.
                let mut pos = BRG_PIXEL_OFFSET - 196;
                loop {
                    f.seek(SeekFrom::Start(pos)).map_err(BioFormatsError::Io)?;
                    let mut s = [0u8; 5];
                    f.read_exact(&mut s).map_err(BioFormatsError::Io)?;
                    if &s == b"scn0x" {
                        break;
                    }
                    // back up 4 from the post-read position (== pos + 5 - 4)
                    pos = (pos + 5) - 4;
                }
                let mut p = pos + 5; // after reading "scn0x"
                p += 69;
                f.seek(SeekFrom::Start(p)).map_err(BioFormatsError::Io)?;
                let mut check = [0u8; 1];
                f.read_exact(&mut check).map_err(BioFormatsError::Io)?;
                p += 1;
                p += 19;
                f.seek(SeekFrom::Start(p)).map_err(BioFormatsError::Io)?;
                if check[0] != 0 {
                    let extra = read_i16(f, self.little_endian)? as i64 - 2;
                    p += 2;
                    p += extra.max(0) as u64;
                    f.seek(SeekFrom::Start(p)).map_err(BioFormatsError::Io)?;
                }
                let len = read_i16(f, self.little_endian)? as i64;
                p += 2;
                p += len.max(0) as u64;
                p += 32;
                Ok(p)
            } else {
                Ok(file_len - plane_size)
            }
        } else {
            Ok(file_len - plane_size)
        }
    }
}

fn read_i16(f: &mut std::fs::File, little_endian: bool) -> Result<i16> {
    let mut b = [0u8; 2];
    f.read_exact(&mut b).map_err(BioFormatsError::Io)?;
    Ok(if little_endian {
        i16::from_le_bytes(b)
    } else {
        i16::from_be_bytes(b)
    })
}

fn read_i32(f: &mut std::fs::File, little_endian: bool) -> Result<i32> {
    let mut b = [0u8; 4];
    f.read_exact(&mut b).map_err(BioFormatsError::Io)?;
    Ok(if little_endian {
        i32::from_le_bytes(b)
    } else {
        i32::from_be_bytes(b)
    })
}

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

impl FormatReader for BioRadGelReader {
    fn is_this_type_by_name(&self, path: &Path) -> bool {
        matches!(
            path.extension()
                .and_then(|e| e.to_str())
                .map(|e| e.to_ascii_lowercase())
                .as_deref(),
            Some("1sc")
        )
    }

    fn is_this_type_by_bytes(&self, header: &[u8]) -> bool {
        // Magic: first big-endian short == 0xafaf.
        header.len() >= 2 && u16::from_be_bytes([header[0], header[1]]) == BRG_MAGIC
    }

    fn set_id(&mut self, path: &Path) -> Result<()> {
        self.close()?;
        let mut f = std::fs::File::open(path).map_err(BioFormatsError::Io)?;
        let file_size = f.metadata().map_err(BioFormatsError::Io)?.len();

        // Reject files too small to hold the 48-byte header and the metadata
        // chunk table that begins at START_OFFSET, instead of leaking an Io EOF.
        if file_size < BRG_START_OFFSET + 4 {
            return Err(BioFormatsError::UnsupportedFormat(
                "Bio-Rad GEL file is too short".into(),
            ));
        }

        // Header begins with a 48-byte string; "Intel Format" => little-endian.
        let mut head48 = [0u8; 48];
        f.read_exact(&mut head48).map_err(BioFormatsError::Io)?;
        let check = String::from_utf8_lossy(&head48);
        let mut little_endian = check.contains("Intel Format");

        // Walk metadata chunks from START_OFFSET until code 0x81 is found.
        f.seek(SeekFrom::Start(BRG_START_OFFSET))
            .map_err(BioFormatsError::Io)?;
        let mut code_found = false;
        let mut skip: i64 = 0;
        let mut base_fp: i64 = 0;
        // Guard against runaway loops on malformed input.
        let mut iterations = 0u32;
        while !code_found {
            iterations += 1;
            if iterations > 100_000 {
                return Err(BioFormatsError::UnsupportedFormat(
                    "Bio-Rad GEL: chunk walk did not find code 0x81".into(),
                ));
            }
            let code = read_i16(&mut f, little_endian)?;
            if code == 0x81 {
                code_found = true;
            }
            let length = read_i16(&mut f, little_endian)?;

            f.seek(SeekFrom::Current(2 + 2 * length as i64))
                .map_err(BioFormatsError::Io)?;
            if code_found {
                let fp = f.stream_position().map_err(BioFormatsError::Io)? as i64;
                base_fp = fp + 2;
                if length > 1 {
                    f.seek(SeekFrom::Current(-2)).map_err(BioFormatsError::Io)?;
                }
                skip = read_i32(&mut f, little_endian)? as i64 - 32;
            } else if length == 1 {
                f.seek(SeekFrom::Current(12)).map_err(BioFormatsError::Io)?;
            } else if length == 2 {
                f.seek(SeekFrom::Current(10)).map_err(BioFormatsError::Io)?;
            }
        }

        self.diff = BRG_BASE_OFFSET - base_fp;
        skip += self.diff;

        // Seek to baseFP + skip and read dimensions + bpp.
        let dims_pos = (base_fp + skip).max(0) as u64;
        f.seek(SeekFrom::Start(dims_pos))
            .map_err(BioFormatsError::Io)?;

        let mut size_x = (read_i16(&mut f, little_endian)? as u16) as u32;
        let mut size_y = (read_i16(&mut f, little_endian)? as u16) as u32;
        if (size_x as u64) * (size_y as u64) > file_size {
            // Retry as little-endian, re-reading the two shorts.
            little_endian = true;
            f.seek(SeekFrom::Current(-4)).map_err(BioFormatsError::Io)?;
            size_x = read_i16(&mut f, little_endian)? as u32;
            size_y = read_i16(&mut f, little_endian)? as u32;
        }
        f.seek(SeekFrom::Current(2)).map_err(BioFormatsError::Io)?; // skip 2

        let bpp = read_i16(&mut f, little_endian)?;
        // pixelTypeFromBytes(bpp, signed=false, fp=false): 2 -> Uint16, 4 -> Uint32.
        // Java uses fp=false here; 4-byte support is FLOAT per the GEL spec, but
        // the reader declares an integer type. Follow Java: unsigned integer.
        let (pixel_type, bits) = match bpp {
            2 => (PixelType::Uint16, 16u8),
            4 => (PixelType::Uint32, 32u8),
            _ => {
                return Err(BioFormatsError::UnsupportedFormat(format!(
                    "Bio-Rad GEL: unsupported bytes per pixel {bpp}"
                )))
            }
        };

        if size_x == 0 || size_y == 0 {
            return Err(BioFormatsError::UnsupportedFormat(
                "Bio-Rad GEL: invalid image dimensions".into(),
            ));
        }
        self.little_endian = little_endian;
        let plane_size = (size_x as u64)
            .checked_mul(size_y as u64)
            .and_then(|pixels| pixels.checked_mul(pixel_type.bytes_per_sample() as u64))
            .ok_or_else(|| {
                BioFormatsError::UnsupportedFormat(
                    "Bio-Rad GEL: declared image is too large".into(),
                )
            })?;
        let pixel_offset = self.pixel_seek(&mut f, plane_size, file_size)?;
        if pixel_offset
            .checked_add(plane_size)
            .is_none_or(|end| end > file_size)
        {
            return Err(BioFormatsError::UnsupportedFormat(
                "Bio-Rad GEL: file is too short for declared pixel payload".into(),
            ));
        }

        self.path = Some(path.to_path_buf());
        self.meta = Some(ImageMetadata {
            size_x,
            size_y,
            pixel_type,
            bits_per_pixel: bits,
            is_little_endian: little_endian,
            ..placeholder_meta_u16()
        });
        Ok(())
    }

    fn close(&mut self) -> Result<()> {
        self.path = None;
        self.meta = None;
        Ok(())
    }

    fn series_count(&self) -> usize {
        usize::from(self.meta.is_some())
    }

    fn set_series(&mut self, s: usize) -> Result<()> {
        if self.meta.is_none() {
            return Err(BioFormatsError::NotInitialized);
        }
        if s != 0 {
            Err(BioFormatsError::SeriesOutOfRange(s))
        } else {
            Ok(())
        }
    }

    fn series(&self) -> usize {
        0
    }

    fn metadata(&self) -> &ImageMetadata {
        self.meta
            .as_ref()
            .unwrap_or(crate::common::reader::uninitialized_metadata())
    }

    fn open_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        if plane_index >= meta.image_count {
            return Err(BioFormatsError::PlaneOutOfRange(plane_index));
        }
        let bpp = meta.pixel_type.bytes_per_sample();
        let pixel = bpp * meta.size_c as usize;
        let w = meta.size_x as usize;
        let h = meta.size_y as usize;
        let plane_size = (pixel * w * h) as u64;

        let path = self.path.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let mut f = std::fs::File::open(path).map_err(BioFormatsError::Io)?;
        let file_len = f.metadata().map_err(BioFormatsError::Io)?.len();

        let seek_pos = self.pixel_seek(&mut f, plane_size, file_len)?;
        f.seek(SeekFrom::Start(seek_pos))
            .map_err(BioFormatsError::Io)?;

        // Java reads rows bottom-to-top into the destination buffer, which flips
        // the image vertically relative to disk order.
        let row_bytes = w * pixel;
        let mut buf = vec![0u8; h * row_bytes];
        for row in (0..h).rev() {
            f.read_exact(&mut buf[row * row_bytes..(row + 1) * row_bytes])
                .map_err(BioFormatsError::Io)?;
        }
        Ok(buf)
    }

    fn open_bytes_region(
        &mut self,
        plane_index: u32,
        _x: u32,
        _y: u32,
        w: u32,
        h: u32,
    ) -> Result<Vec<u8>> {
        let full = self.open_bytes(plane_index)?;
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let spp = meta.size_c as usize;
        crop_full_plane("Bio-Rad GEL", &full, meta, spp, _x, _y, w, h)
    }

    fn open_thumb_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let tw = meta.size_x.min(256);
        let th = meta.size_y.min(256);
        let tx = (meta.size_x - tw) / 2;
        let ty = (meta.size_y - th) / 2;
        self.open_bytes_region(plane_index, tx, ty, tw, th)
    }
}

// ---------------------------------------------------------------------------
// 3. Li-Cor L2D companion-file reader
// ---------------------------------------------------------------------------
/// Li-Cor L2D format (`.l2d`).
///
/// Java Bio-Formats stores L2D pixels in companion TIFF files listed by the
/// `.l2d` scan manifest and each scan's `.scn` metadata file.
pub struct L2dReader {
    current_id: Option<PathBuf>,
    tiffs: Vec<Vec<PathBuf>>,
    metadata: Vec<ImageMetadata>,
    current_series: usize,
    reader: crate::tiff::TiffReader,
}

impl L2dReader {
    const LICOR_MAGIC: &'static str = "LI-COR LI2D";

    pub fn new() -> Self {
        L2dReader {
            current_id: None,
            tiffs: Vec::new(),
            metadata: Vec::new(),
            current_series: 0,
            reader: crate::tiff::TiffReader::new(),
        }
    }

    fn parse_key_value_lines(text: &str) -> HashMap<String, String> {
        text.lines()
            .filter_map(|line| {
                let line = line.trim();
                if line.is_empty() || line.starts_with('#') {
                    return None;
                }
                let (key, value) = line.split_once('=')?;
                Some((key.trim().to_string(), value.trim().to_string()))
            })
            .collect()
    }

    fn split_list(value: &str) -> Vec<String> {
        value
            .split(',')
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_string)
            .collect()
    }

    fn set_l2d_id(&mut self, path: &Path) -> Result<()> {
        let text = std::fs::read_to_string(path).map_err(BioFormatsError::Io)?;
        if !text.contains(Self::LICOR_MAGIC) {
            return Err(BioFormatsError::UnsupportedFormat(
                "Li-Cor L2D file is missing LI-COR LI2D marker".into(),
            ));
        }

        let l2d = Self::parse_key_value_lines(&text);
        let scans = l2d
            .get("ScanNames")
            .map(|v| Self::split_list(v))
            .ok_or_else(|| BioFormatsError::Format("Li-Cor L2D missing ScanNames".into()))?;
        if scans.is_empty() {
            return Err(BioFormatsError::Format(
                "Li-Cor L2D ScanNames list is empty".into(),
            ));
        }

        let parent = path.parent().unwrap_or_else(|| Path::new("."));
        let mut tiffs = Vec::new();
        let mut metadata = Vec::new();

        for scan in scans {
            let scan_dir = parent.join(&scan);
            if !scan_dir.is_dir() {
                continue;
            }
            let scan_path = scan_dir.join(format!("{scan}.scn"));
            let scan_text = std::fs::read_to_string(&scan_path).map_err(BioFormatsError::Io)?;
            let scan_meta = Self::parse_key_value_lines(&scan_text);
            let image_names = scan_meta
                .get("ImageNames")
                .map(|v| Self::split_list(v))
                .ok_or_else(|| {
                    BioFormatsError::Format(format!("Li-Cor L2D scan {scan} missing ImageNames"))
                })?;
            if image_names.is_empty() {
                return Err(BioFormatsError::Format(format!(
                    "Li-Cor L2D scan {scan} ImageNames list is empty"
                )));
            }

            let scan_tiffs: Vec<PathBuf> = image_names
                .into_iter()
                .map(|name| scan_dir.join(name))
                .collect();
            for tiff in &scan_tiffs {
                if !tiff.is_file() {
                    return Err(BioFormatsError::Format(format!(
                        "Li-Cor L2D companion TIFF is missing: {}",
                        tiff.display()
                    )));
                }
            }

            self.reader.set_id(&scan_tiffs[0])?;
            let first = self.reader.metadata().clone();
            self.reader.close()?;

            let mut series_meta = first;
            series_meta.image_count = scan_tiffs.len() as u32;
            series_meta.size_z = 1;
            series_meta.size_t = 1;
            series_meta.size_c = scan_tiffs.len() as u32;
            series_meta.dimension_order = DimensionOrder::XYCZT;
            series_meta.series_metadata = scan_meta
                .into_iter()
                .map(|(k, v)| (k, crate::common::metadata::MetadataValue::String(v)))
                .collect();
            tiffs.push(scan_tiffs);
            metadata.push(series_meta);
        }

        if tiffs.is_empty() {
            return Err(BioFormatsError::Format(
                "Li-Cor L2D did not reference any existing scan directories".into(),
            ));
        }

        self.current_id = Some(path.to_path_buf());
        self.tiffs = tiffs;
        self.metadata = metadata;
        self.current_series = 0;
        Ok(())
    }
}

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

impl FormatReader for L2dReader {
    fn is_this_type_by_name(&self, path: &Path) -> bool {
        matches!(
            path.extension()
                .and_then(|e| e.to_str())
                .map(|e| e.to_ascii_lowercase())
                .as_deref(),
            Some("l2d") | Some("scn")
        )
    }

    fn is_this_type_by_bytes(&self, header: &[u8]) -> bool {
        std::str::from_utf8(&header[..header.len().min(512)])
            .map(|s| s.contains(Self::LICOR_MAGIC))
            .unwrap_or(false)
    }

    fn set_id(&mut self, path: &Path) -> Result<()> {
        self.close()?;
        let l2d_path = if path
            .extension()
            .and_then(|e| e.to_str())
            .map(|e| e.eq_ignore_ascii_case("l2d"))
            .unwrap_or(false)
        {
            path.to_path_buf()
        } else {
            return Err(BioFormatsError::UnsupportedFormat(
                "Li-Cor L2D grouped reads must be opened from the .l2d manifest".into(),
            ));
        };
        self.set_l2d_id(&l2d_path)
    }

    fn close(&mut self) -> Result<()> {
        self.current_id = None;
        self.tiffs.clear();
        self.metadata.clear();
        self.current_series = 0;
        self.reader.close()?;
        Ok(())
    }

    fn series_count(&self) -> usize {
        self.metadata.len()
    }

    fn set_series(&mut self, s: usize) -> Result<()> {
        if self.metadata.is_empty() {
            return Err(BioFormatsError::NotInitialized);
        }
        if s >= self.metadata.len() {
            Err(BioFormatsError::SeriesOutOfRange(s))
        } else {
            self.current_series = s;
            Ok(())
        }
    }

    fn series(&self) -> usize {
        self.current_series
    }

    fn metadata(&self) -> &ImageMetadata {
        self.metadata
            .get(self.current_series)
            .unwrap_or(crate::common::reader::uninitialized_metadata())
    }

    fn open_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self
            .metadata
            .get(self.current_series)
            .ok_or(BioFormatsError::NotInitialized)?;
        if plane_index >= meta.image_count {
            return Err(BioFormatsError::PlaneOutOfRange(plane_index));
        }
        let tiff = self
            .tiffs
            .get(self.current_series)
            .and_then(|series| series.get(plane_index as usize))
            .ok_or(BioFormatsError::PlaneOutOfRange(plane_index))?
            .clone();
        self.reader.set_id(&tiff)?;
        let bytes = self.reader.open_bytes(0);
        self.reader.close()?;
        bytes
    }

    fn open_bytes_region(
        &mut self,
        plane_index: u32,
        x: u32,
        y: u32,
        w: u32,
        h: u32,
    ) -> Result<Vec<u8>> {
        let meta = self
            .metadata
            .get(self.current_series)
            .ok_or(BioFormatsError::NotInitialized)?;
        if plane_index >= meta.image_count {
            return Err(BioFormatsError::PlaneOutOfRange(plane_index));
        }
        let tiff = self
            .tiffs
            .get(self.current_series)
            .and_then(|series| series.get(plane_index as usize))
            .ok_or(BioFormatsError::PlaneOutOfRange(plane_index))?
            .clone();
        self.reader.set_id(&tiff)?;
        let bytes = self.reader.open_bytes_region(0, x, y, w, h);
        self.reader.close()?;
        bytes
    }

    fn open_thumb_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self
            .metadata
            .get(self.current_series)
            .ok_or(BioFormatsError::NotInitialized)?;
        let tw = meta.size_x.min(256);
        let th = meta.size_y.min(256);
        let tx = (meta.size_x - tw) / 2;
        let ty = (meta.size_y - th) / 2;
        self.open_bytes_region(plane_index, tx, ty, tw, th)
    }
}

// ---------------------------------------------------------------------------
// 4. Canon RAW (CR2 / CRW / CR3) — TIFF wrapper
// ---------------------------------------------------------------------------
/// Canon RAW format reader (`.cr2`, `.crw`, `.cr3`).
///
/// Two code paths, mirroring Java Bio-Formats:
///
/// * **Legacy CRW** — `CanonRawReader.java` recognises raw Canon 300D `.crw`
///   files solely by a fixed file length of 18 653 760 bytes. Those have no
///   TIFF structure: bytes are byte-swapped in pairs, 12-bit samples are
///   unpacked, and the Bayer mosaic is split into an interleaved RGB plane
///   (`COLOR_MAP = {1,0,2,1}`, sizeX=4080, sizeY=3048, UINT16, 12 bpp). This
///   reader reproduces that unpacking exactly (the `ImageTools.interpolate`
///   demosaic in Java is a simple channel split, not full demosaicing).
/// * **TIFF-based** — modern CR2 files are valid TIFFs; delegate to
///   `TiffReader`.
pub struct CanonRawReader {
    inner: crate::tiff::TiffReader,
    /// Set when the file matched the legacy fixed-length CRW layout.
    legacy: Option<LegacyCrw>,
}

/// State for a legacy fixed-length Canon `.crw` file.
struct LegacyCrw {
    path: PathBuf,
    meta: ImageMetadata,
    /// Decoded interleaved RGB plane (UINT16 LE, 3 samples/pixel), cached.
    plane: Option<Vec<u8>>,
}

impl CanonRawReader {
    /// Fixed file length used by `CanonRawReader.java` to detect legacy CRW.
    const FILE_LENGTH: u64 = 18_653_760;
    const SIZE_X: usize = 4080;
    const SIZE_Y: usize = 3048;
    /// Bayer color map: index = (row%2)*2 + (col%2) -> 0=R, 1=G, 2=B.
    const COLOR_MAP: [u8; 4] = [1, 0, 2, 1];

    pub fn new() -> Self {
        CanonRawReader {
            inner: crate::tiff::TiffReader::new(),
            legacy: None,
        }
    }

    /// Decode the legacy CRW interleaved RGB plane (port of initFile + the
    /// channel split in openBytes from `CanonRawReader.java`).
    fn decode_legacy_plane(path: &Path) -> Result<Vec<u8>> {
        let mut buf = std::fs::read(path).map_err(BioFormatsError::Io)?;
        if buf.len() < Self::FILE_LENGTH as usize {
            return Err(BioFormatsError::UnsupportedFormat(
                "Canon CRW: file shorter than expected fixed length".into(),
            ));
        }
        buf.truncate(Self::FILE_LENGTH as usize);

        // Reverse bytes in pairs.
        let mut i = 0;
        while i + 1 < buf.len() {
            buf.swap(i, i + 1);
            i += 2;
        }

        let w = Self::SIZE_X;
        let h = Self::SIZE_Y;
        let plane = w * h;
        // pix layout: 3 planar channels [R | G | B], each w*h shorts.
        let mut pix = vec![0i16; plane * 3];

        let mut next_byte = 0usize;
        let mut even = true;
        for row in 0..h {
            let row_offset = row * w;
            for col in 0..w {
                let v: u32 = if even {
                    let a = buf[next_byte] as u32;
                    next_byte += 1;
                    let b = buf[next_byte] as u32;
                    (a << 4) | ((b & 0xf0) >> 4)
                } else {
                    let a = buf[next_byte] as u32;
                    next_byte += 1;
                    let b = buf[next_byte] as u32;
                    next_byte += 1;
                    ((a & 0xf) << 8) | b
                };
                let val = (v & 0xffff) as u16 as i16;
                even = !even;

                let map_index = (row % 2) * 2 + (col % 2);
                match Self::COLOR_MAP[map_index] {
                    0 => pix[row_offset + col] = val,
                    1 => pix[plane + row_offset + col] = val,
                    2 => pix[2 * plane + row_offset + col] = val,
                    _ => {}
                }
            }
        }

        // Java: ImageTools.interpolate(pix, plane, COLOR_MAP, ...) fills in the
        // missing CFA components, then readPlane delivers interleaved RGB.
        // littleEndian = true (m.littleEndian set in initFile).
        let color_map = Self::COLOR_MAP.map(|c| c as i32);
        let mut out = vec![0u8; plane * 3 * 2];
        cfa::interpolate(&pix, &mut out, &color_map, w, h, true);
        Ok(out)
    }
}

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

impl FormatReader for CanonRawReader {
    fn is_this_type_by_name(&self, path: &Path) -> bool {
        let ext = path
            .extension()
            .and_then(|e| e.to_str())
            .map(|e| e.to_ascii_lowercase());
        matches!(ext.as_deref(), Some("cr2") | Some("crw") | Some("cr3"))
    }

    fn is_this_type_by_bytes(&self, _header: &[u8]) -> bool {
        false
    }

    fn set_id(&mut self, path: &Path) -> Result<()> {
        self.close()?;
        // Legacy detection: exact fixed file length (CanonRawReader.java).
        let len = std::fs::metadata(path).map(|m| m.len()).unwrap_or(0);
        if len == Self::FILE_LENGTH {
            let mut meta = placeholder_meta_u16();
            meta.size_x = Self::SIZE_X as u32;
            meta.size_y = Self::SIZE_Y as u32;
            meta.size_c = 3;
            meta.pixel_type = PixelType::Uint16;
            meta.bits_per_pixel = 12;
            meta.image_count = 1;
            meta.is_rgb = true;
            meta.is_interleaved = true;
            meta.dimension_order = DimensionOrder::XYCZT;
            self.legacy = Some(LegacyCrw {
                path: path.to_path_buf(),
                meta,
                plane: None,
            });
            return Ok(());
        }
        self.legacy = None;
        self.inner.set_id(path)
    }

    fn close(&mut self) -> Result<()> {
        self.legacy = None;
        self.inner.close()
    }

    fn series_count(&self) -> usize {
        if self.legacy.is_some() {
            1
        } else {
            self.inner.series_count()
        }
    }

    fn set_series(&mut self, s: usize) -> Result<()> {
        if self.legacy.is_some() {
            if s != 0 {
                return Err(BioFormatsError::SeriesOutOfRange(s));
            }
            Ok(())
        } else if self.inner.series_count() == 0 {
            Err(BioFormatsError::NotInitialized)
        } else {
            self.inner.set_series(s)
        }
    }

    fn series(&self) -> usize {
        if self.legacy.is_some() {
            0
        } else {
            self.inner.series()
        }
    }

    fn metadata(&self) -> &ImageMetadata {
        if let Some(l) = &self.legacy {
            &l.meta
        } else {
            self.inner.metadata()
        }
    }

    fn open_bytes(&mut self, p: u32) -> Result<Vec<u8>> {
        if let Some(l) = &mut self.legacy {
            if p != 0 {
                return Err(BioFormatsError::PlaneOutOfRange(p));
            }
            if l.plane.is_none() {
                l.plane = Some(Self::decode_legacy_plane(&l.path)?);
            }
            return Ok(l.plane.clone().unwrap());
        }
        self.inner.open_bytes(p)
    }

    fn open_bytes_region(&mut self, p: u32, x: u32, y: u32, w: u32, h: u32) -> Result<Vec<u8>> {
        if self.legacy.is_some() {
            let full = self.open_bytes(p)?;
            let meta = self.metadata().clone();
            return crop_full_plane("Canon CRW", &full, &meta, 3, x, y, w, h);
        }
        self.inner.open_bytes_region(p, x, y, w, h)
    }

    fn open_thumb_bytes(&mut self, p: u32) -> Result<Vec<u8>> {
        if self.legacy.is_some() {
            let meta = self.metadata().clone();
            let tw = meta.size_x.min(256);
            let th = meta.size_y.min(256);
            let tx = (meta.size_x - tw) / 2;
            let ty = (meta.size_y - th) / 2;
            return self.open_bytes_region(p, tx, ty, tw, th);
        }
        self.inner.open_thumb_bytes(p)
    }

    fn resolution_count(&self) -> usize {
        if self.legacy.is_some() {
            1
        } else {
            self.inner.resolution_count()
        }
    }

    fn set_resolution(&mut self, level: usize) -> Result<()> {
        if self.legacy.is_some() {
            if level != 0 {
                return Err(BioFormatsError::Format(format!(
                    "resolution {} out of range",
                    level
                )));
            }
            Ok(())
        } else {
            self.inner.set_resolution(level)
        }
    }
}

// ---------------------------------------------------------------------------
// 5. Hasselblad Imacon — TIFF with private tags
// ---------------------------------------------------------------------------
/// Hasselblad Imacon format reader (`.fff`).
///
/// Ported from `ImaconReader.java` (extends `BaseTiffReader`). Imacon `.fff`
/// files are TIFFs identified by private tag 50457 (`XML_TAG`); each main IFD
/// is a separate series. The CREATOR tag (34377) carries experimenter/name/date
/// lines. Pixel reading is delegated to `TiffReader`; this reader adds the
/// tag-based detection and metadata parsing.
pub struct ImaconReader {
    inner: crate::tiff::TiffReader,
    meta: Option<ImageMetadata>,
}

impl ImaconReader {
    const XML_TAG: u16 = 50457;
    const CREATOR_TAG: u16 = 34377;

    pub fn new() -> Self {
        ImaconReader {
            inner: crate::tiff::TiffReader::new(),
            meta: None,
        }
    }
}

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

impl FormatReader for ImaconReader {
    fn is_this_type_by_name(&self, path: &Path) -> bool {
        matches!(
            path.extension()
                .and_then(|e| e.to_str())
                .map(|e| e.to_ascii_lowercase())
                .as_deref(),
            Some("fff")
        )
    }

    fn is_this_type_by_bytes(&self, _header: &[u8]) -> bool {
        // Java requires the XML_TAG in the first IFD; bytes alone insufficient.
        false
    }

    fn set_id(&mut self, path: &Path) -> Result<()> {
        self.close()?;
        self.inner.set_id(path)?;

        let first = self
            .inner
            .ifd(0)
            .ok_or_else(|| BioFormatsError::UnsupportedFormat("Imacon: no IFD".into()))?;
        if first.get(Self::XML_TAG).is_none() {
            let _ = self.inner.close();
            return Err(BioFormatsError::UnsupportedFormat(
                "Imacon: TIFF is missing the XML tag (50457)".into(),
            ));
        }

        let mut meta = self.inner.metadata().clone();
        meta.series_metadata
            .insert("format".into(), MetadataValue::String("Imacon".into()));

        // CREATOR_TAG: newline-delimited; Java reads experimenter (line 4),
        // image name (line 6), creation date (lines 8 + 10).
        if let Some(creator) = first.get_str(Self::CREATOR_TAG) {
            let lines: Vec<&str> = creator.split('\n').collect();
            if lines.len() > 4 {
                meta.series_metadata.insert(
                    "Experimenter".into(),
                    MetadataValue::String(lines[4].trim().to_string()),
                );
            }
            if lines.len() > 6 {
                meta.series_metadata.insert(
                    "ImageName".into(),
                    MetadataValue::String(lines[6].trim().to_string()),
                );
            }
            if lines.len() > 8 {
                let mut date = lines[8].trim().to_string();
                if lines.len() > 10 {
                    date.push(' ');
                    date.push_str(lines[10].trim());
                }
                meta.series_metadata
                    .insert("CreationDate".into(), MetadataValue::String(date));
            }
        }

        self.meta = Some(meta);
        Ok(())
    }

    fn close(&mut self) -> Result<()> {
        self.meta = None;
        self.inner.close()
    }

    fn series_count(&self) -> usize {
        if self.meta.is_some() {
            self.inner.series_count()
        } else {
            0
        }
    }

    fn set_series(&mut self, s: usize) -> Result<()> {
        if self.meta.is_none() {
            return Err(BioFormatsError::NotInitialized);
        }
        self.inner.set_series(s)
    }

    fn series(&self) -> usize {
        self.inner.series()
    }

    fn metadata(&self) -> &ImageMetadata {
        self.meta
            .as_ref()
            .unwrap_or(crate::common::reader::uninitialized_metadata())
    }

    fn open_bytes(&mut self, p: u32) -> Result<Vec<u8>> {
        self.inner.open_bytes(p)
    }

    fn open_bytes_region(&mut self, p: u32, x: u32, y: u32, w: u32, h: u32) -> Result<Vec<u8>> {
        self.inner.open_bytes_region(p, x, y, w, h)
    }

    fn open_thumb_bytes(&mut self, p: u32) -> Result<Vec<u8>> {
        self.inner.open_thumb_bytes(p)
    }

    fn resolution_count(&self) -> usize {
        self.inner.resolution_count()
    }

    fn set_resolution(&mut self, level: usize) -> Result<()> {
        self.inner.set_resolution(level)
    }
}

// ---------------------------------------------------------------------------
// 6. Santa Barbara Instrument Group — FITS wrapper
// ---------------------------------------------------------------------------
/// Santa Barbara Instrument Group reader (`.fts`).
///
/// SBIG .fts files use the FITS format; this reader delegates to `FitsReader`.
pub struct SbigReader {
    inner: crate::formats::fits::FitsReader,
}

impl SbigReader {
    pub fn new() -> Self {
        SbigReader {
            inner: crate::formats::fits::FitsReader::new(),
        }
    }
}

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

impl FormatReader for SbigReader {
    fn is_this_type_by_name(&self, path: &Path) -> bool {
        let ext = path
            .extension()
            .and_then(|e| e.to_str())
            .map(|e| e.to_ascii_lowercase());
        matches!(ext.as_deref(), Some("fts"))
    }

    fn is_this_type_by_bytes(&self, _header: &[u8]) -> bool {
        false
    }

    fn set_id(&mut self, path: &Path) -> Result<()> {
        self.inner.set_id(path)
    }

    fn close(&mut self) -> Result<()> {
        self.inner.close()
    }

    fn series_count(&self) -> usize {
        self.inner.series_count()
    }

    fn set_series(&mut self, s: usize) -> Result<()> {
        self.inner.set_series(s)
    }

    fn series(&self) -> usize {
        self.inner.series()
    }

    fn metadata(&self) -> &ImageMetadata {
        self.inner.metadata()
    }

    fn open_bytes(&mut self, p: u32) -> Result<Vec<u8>> {
        self.inner.open_bytes(p)
    }

    fn open_bytes_region(&mut self, p: u32, x: u32, y: u32, w: u32, h: u32) -> Result<Vec<u8>> {
        self.inner.open_bytes_region(p, x, y, w, h)
    }

    fn open_thumb_bytes(&mut self, p: u32) -> Result<Vec<u8>> {
        self.inner.open_thumb_bytes(p)
    }
}

// ---------------------------------------------------------------------------
// 7. Image-Pro Workspace — OLE2 compound document with embedded TIFFs
// ---------------------------------------------------------------------------
/// Image-Pro Workspace format reader (`.ipw`).
///
/// Ported from `IPWReader.java`. An IPW file is an OLE2/Compound Document
/// (magic `0xd0cf11e0`), NOT a plain TIFF. Each image plane is stored as an
/// embedded `ImageTIFF` stream; an `ImageInfo` stream carries a text
/// description with `channels`/`slices`/`frames` counts. This reader uses the
/// `cfb` crate to enumerate streams, parses dimensions from the first
/// embedded TIFF, and reads each plane by extracting its `ImageTIFF` stream
/// to a temporary file and delegating to `TiffReader` (the in-tree TIFF
/// reader is path-based).
pub struct IpwReader {
    path: Option<PathBuf>,
    meta: Option<ImageMetadata>,
    /// Embedded TIFF stream paths, ordered by plane index.
    image_streams: Vec<String>,
}

impl IpwReader {
    pub fn new() -> Self {
        IpwReader {
            path: None,
            meta: None,
            image_streams: Vec::new(),
        }
    }

    /// Extract an embedded stream to a temp file and run a `TiffReader` op.
    fn read_embedded_tiff(
        &self,
        stream_path: &str,
        op: impl FnOnce(&mut crate::tiff::TiffReader) -> Result<Vec<u8>>,
    ) -> Result<Vec<u8>> {
        let (mut reader, tmp) = self.open_embedded_tiff(stream_path)?;
        let result = op(&mut reader);
        reader.close().ok();
        std::fs::remove_file(&tmp).ok();
        result
    }

    /// Extract an embedded stream to a temp file, returning an initialised
    /// `TiffReader` plus the temp path to clean up.
    fn open_embedded_tiff(&self, stream_path: &str) -> Result<(crate::tiff::TiffReader, PathBuf)> {
        let path = self.path.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let mut comp =
            cfb::open(path).map_err(|e| BioFormatsError::Format(format!("IPW CFB open: {e}")))?;
        let mut stream = comp
            .open_stream(stream_path)
            .map_err(|e| BioFormatsError::Format(format!("IPW stream {stream_path}: {e}")))?;
        let mut data = Vec::new();
        stream.read_to_end(&mut data).map_err(BioFormatsError::Io)?;
        drop(stream);
        drop(comp);

        let tmp = std::env::temp_dir().join(format!(
            "bioformats_ipw_{}_{}.tif",
            std::process::id(),
            stream_path.replace(['/', '\\', ' '], "_")
        ));
        std::fs::write(&tmp, &data).map_err(BioFormatsError::Io)?;
        let mut reader = crate::tiff::TiffReader::new();
        match reader.set_id(&tmp) {
            Ok(()) => Ok((reader, tmp)),
            Err(e) => {
                std::fs::remove_file(&tmp).ok();
                Err(e)
            }
        }
    }
}

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

/// Parse the IPW `ImageInfo` description into (sizeC, sizeZ, sizeT).
fn parse_ipw_image_info(text: &str) -> Result<(Option<u32>, Option<u32>, Option<u32>)> {
    let (mut c, mut z, mut t) = (None, None, None);
    for line in text.split('\n') {
        if let Some((label, data)) = line.split_once('=') {
            let label = label.trim();
            match label.trim() {
                "channels" | "slices" | "frames" => {
                    let value = data.trim().parse::<u32>().map_err(|_| {
                        BioFormatsError::Format(format!("IPW: invalid {label} value"))
                    })?;
                    if value == 0 {
                        return Err(BioFormatsError::Format(format!(
                            "IPW: {label} must be positive"
                        )));
                    }
                    match label {
                        "channels" => c = Some(value),
                        "slices" => z = Some(value),
                        "frames" => t = Some(value),
                        _ => {}
                    }
                }
                _ => {}
            }
        }
    }
    Ok((c, z, t))
}

impl FormatReader for IpwReader {
    fn is_this_type_by_name(&self, path: &Path) -> bool {
        matches!(
            path.extension()
                .and_then(|e| e.to_str())
                .map(|e| e.to_ascii_lowercase())
                .as_deref(),
            Some("ipw")
        )
    }

    fn is_this_type_by_bytes(&self, _header: &[u8]) -> bool {
        false
    }

    fn set_id(&mut self, path: &Path) -> Result<()> {
        self.close()?;
        let mut comp =
            cfb::open(path).map_err(|e| BioFormatsError::Format(format!("IPW CFB open: {e}")))?;

        // Enumerate streams. ImageTIFF streams hold pixels; the numeric
        // storage just above the stream is the plane index (Java parses it
        // from the path, defaulting to 0 directly under Root Entry).
        let entries: Vec<(String, bool)> = comp
            .walk()
            .map(|e| (e.path().to_string_lossy().to_string(), e.is_stream()))
            .collect();

        let mut image_streams: Vec<(u32, String)> = Vec::new();
        let mut info_stream: Option<String> = None;
        for (raw_path, is_stream) in &entries {
            if !is_stream {
                continue;
            }
            let norm = raw_path.replace('\\', "/");
            let base = norm.rsplit('/').next().unwrap_or("");
            if base == "ImageTIFF" {
                let parts: Vec<&str> = norm.trim_matches('/').split('/').collect();
                let idx = if parts.len() >= 2 {
                    parts[parts.len() - 2]
                        .chars()
                        .filter(|c| c.is_ascii_digit())
                        .collect::<String>()
                        .parse::<u32>()
                        .unwrap_or(0)
                } else {
                    0
                };
                image_streams.push((idx, raw_path.clone()));
            } else if base == "ImageInfo" {
                info_stream = Some(raw_path.clone());
            }
        }

        if image_streams.is_empty() {
            return Err(BioFormatsError::UnsupportedFormat(
                "IPW: no embedded ImageTIFF streams found".into(),
            ));
        }
        image_streams.sort_by_key(|(idx, _)| *idx);
        let image_count = image_streams.len() as u32;
        let ordered: Vec<String> = image_streams.into_iter().map(|(_, p)| p).collect();

        // Parse ImageInfo for axis sizes.
        let mut series_metadata = HashMap::new();
        series_metadata.insert(
            "format".into(),
            MetadataValue::String("Image-Pro Workspace".into()),
        );
        let (mut size_c, mut size_z, mut size_t) = (None, None, None);
        if let Some(info_path) = &info_stream {
            if let Ok(mut s) = comp.open_stream(info_path) {
                let mut buf = Vec::new();
                if s.read_to_end(&mut buf).is_ok() {
                    let text = String::from_utf8_lossy(&buf);
                    series_metadata.insert(
                        "Image Description".into(),
                        MetadataValue::String(text.trim().to_string()),
                    );
                    let (c, z, t) = parse_ipw_image_info(&text)?;
                    size_c = c;
                    size_z = z;
                    size_t = t;
                }
            }
        }
        drop(comp);

        self.path = Some(path.to_path_buf());
        self.image_streams = ordered;

        // Read first embedded TIFF for X/Y/pixel type.
        let first_stream = self.image_streams[0].clone();
        let (mut tiff, tmp) = self.open_embedded_tiff(&first_stream)?;
        let first_meta = tiff.metadata().clone();
        tiff.close().ok();
        std::fs::remove_file(&tmp).ok();

        let mut size_z = size_z.unwrap_or(1);
        let size_c = size_c.unwrap_or(1);
        let size_t = size_t.unwrap_or(1);
        // Java: if axis product == 1 but multiple planes exist, treat as Z.
        if size_z * size_c * size_t == 1 && image_count != 1 {
            size_z = image_count;
        }

        let meta = ImageMetadata {
            size_x: first_meta.size_x,
            size_y: first_meta.size_y,
            size_z,
            size_c,
            size_t,
            pixel_type: first_meta.pixel_type,
            bits_per_pixel: first_meta.bits_per_pixel,
            image_count,
            dimension_order: if first_meta.is_rgb {
                DimensionOrder::XYCZT
            } else {
                DimensionOrder::XYZCT
            },
            is_rgb: first_meta.is_rgb,
            is_interleaved: first_meta.is_interleaved,
            is_indexed: first_meta.is_indexed,
            is_little_endian: first_meta.is_little_endian,
            resolution_count: 1,
            series_metadata,
            lookup_table: first_meta.lookup_table.clone(),
            modulo_z: None,
            modulo_c: None,
            modulo_t: None,
        };
        self.meta = Some(meta);
        Ok(())
    }

    fn close(&mut self) -> Result<()> {
        self.path = None;
        self.meta = None;
        self.image_streams.clear();
        Ok(())
    }

    fn series_count(&self) -> usize {
        usize::from(self.meta.is_some())
    }

    fn set_series(&mut self, s: usize) -> Result<()> {
        if self.meta.is_none() {
            return Err(BioFormatsError::NotInitialized);
        }
        if s != 0 {
            Err(BioFormatsError::SeriesOutOfRange(s))
        } else {
            Ok(())
        }
    }

    fn series(&self) -> usize {
        0
    }

    fn metadata(&self) -> &ImageMetadata {
        self.meta
            .as_ref()
            .unwrap_or(crate::common::reader::uninitialized_metadata())
    }

    fn open_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        if plane_index >= meta.image_count {
            return Err(BioFormatsError::PlaneOutOfRange(plane_index));
        }
        let stream = self.image_streams[plane_index as usize].clone();
        self.read_embedded_tiff(&stream, |r| r.open_bytes(0))
    }

    fn open_bytes_region(
        &mut self,
        plane_index: u32,
        x: u32,
        y: u32,
        w: u32,
        h: u32,
    ) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        if plane_index >= meta.image_count {
            return Err(BioFormatsError::PlaneOutOfRange(plane_index));
        }
        let stream = self.image_streams[plane_index as usize].clone();
        self.read_embedded_tiff(&stream, move |r| r.open_bytes_region(0, x, y, w, h))
    }

    fn open_thumb_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let tw = meta.size_x.min(256);
        let th = meta.size_y.min(256);
        let tx = (meta.size_x - tw) / 2;
        let ty = (meta.size_y - th) / 2;
        self.open_bytes_region(plane_index, tx, ty, tw, th)
    }
}

// ---------------------------------------------------------------------------
// 8. Photoshop-annotated TIFF — TIFF wrapper
// ---------------------------------------------------------------------------
tiff_wrapper! {
    /// Photoshop-annotated TIFF format (`.tif`).
    pub struct PhotoshopTiffReader;
    extensions: ["tif"];
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::metadata::MetadataValue;
    use crate::common::writer::FormatWriter;
    use crate::tiff::TiffWriter;
    use std::fs;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn temp_dir(name: &str) -> PathBuf {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let dir = std::env::temp_dir().join(format!(
            "bioformats_camera2_{name}_{}_{}",
            std::process::id(),
            unique
        ));
        fs::create_dir_all(&dir).unwrap();
        dir
    }

    fn write_u8_tiff(path: &Path, pixels: &[u8], width: u32, height: u32) {
        let mut meta = ImageMetadata::default();
        meta.size_x = width;
        meta.size_y = height;
        meta.pixel_type = PixelType::Uint8;
        meta.bits_per_pixel = 8;
        meta.image_count = 1;

        let mut writer = TiffWriter::new();
        writer.set_metadata(&meta).unwrap();
        writer.set_id(path).unwrap();
        writer.save_bytes(0, pixels).unwrap();
        writer.close().unwrap();
    }

    fn write_l2d_dataset(root: &Path) -> PathBuf {
        let scan_dir = root.join("ScanA");
        fs::create_dir_all(&scan_dir).unwrap();
        write_u8_tiff(&scan_dir.join("ch1.tif"), &[1, 2, 3, 4, 5, 6], 3, 2);
        write_u8_tiff(&scan_dir.join("ch2.tif"), &[7, 8, 9, 10, 11, 12], 3, 2);
        fs::write(
            scan_dir.join("ScanA.scn"),
            "ImageNames=ch1.tif, ch2.tif\nComments=synthetic\nScanChannels=700,800\n",
        )
        .unwrap();
        let l2d = root.join("sample.l2d");
        fs::write(&l2d, "FileType=LI-COR LI2D\nScanNames=ScanA\n").unwrap();
        l2d
    }

    #[test]
    fn l2d_delegates_planes_to_companion_tiffs() {
        let root = temp_dir("l2d_planes");
        let l2d = write_l2d_dataset(&root);
        let mut reader = L2dReader::new();
        reader.set_id(&l2d).unwrap();

        let meta = reader.metadata();
        assert_eq!(
            (meta.size_x, meta.size_y, meta.size_c, meta.image_count),
            (3, 2, 2, 2)
        );
        assert_eq!(meta.dimension_order, DimensionOrder::XYCZT);
        match meta.series_metadata.get("Comments") {
            Some(MetadataValue::String(value)) => assert_eq!(value, "synthetic"),
            other => panic!("unexpected Comments metadata: {other:?}"),
        }
        assert_eq!(reader.open_bytes(0).unwrap(), vec![1, 2, 3, 4, 5, 6]);
        assert_eq!(reader.open_bytes(1).unwrap(), vec![7, 8, 9, 10, 11, 12]);

        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn l2d_delegates_regions_to_companion_tiffs() {
        let root = temp_dir("l2d_region");
        let l2d = write_l2d_dataset(&root);
        let mut reader = L2dReader::new();
        reader.set_id(&l2d).unwrap();

        assert_eq!(
            reader.open_bytes_region(1, 1, 0, 2, 2).unwrap(),
            vec![8, 9, 11, 12]
        );

        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn l2d_rejects_manifest_without_magic() {
        let root = temp_dir("l2d_magic");
        let l2d = root.join("bad.l2d");
        fs::write(&l2d, "ScanNames=ScanA\n").unwrap();
        let err = L2dReader::new().set_id(&l2d).unwrap_err();
        assert!(
            err.to_string().contains("LI-COR LI2D"),
            "unexpected error: {err}"
        );

        fs::remove_dir_all(root).unwrap();
    }

    // -----------------------------------------------------------------------
    // CFA / RAW helper tests (cfa::unpack_bytes, cfa::BitReader, cfa::interpolate)
    // -----------------------------------------------------------------------

    #[test]
    fn unpack_bytes_little_and_big_endian() {
        // Port-of-Java check: DataTools.unpackBytes(0x1234, buf, 0, 2, le).
        let mut le = [0u8; 2];
        cfa::unpack_bytes(0x1234, &mut le, 0, 2, true);
        assert_eq!(le, [0x34, 0x12]);

        let mut be = [0u8; 2];
        cfa::unpack_bytes(0x1234, &mut be, 0, 2, false);
        assert_eq!(be, [0x12, 0x34]);

        // Offset and a 4-byte write, big-endian.
        let mut buf = [0u8; 6];
        cfa::unpack_bytes(0x0A0B0C0D, &mut buf, 2, 4, false);
        assert_eq!(buf, [0, 0, 0x0A, 0x0B, 0x0C, 0x0D]);

        // Negative short truncated to its low 2 bytes (Java casts to short).
        let mut neg = [0u8; 2];
        cfa::unpack_bytes((-1i16) as i64, &mut neg, 0, 2, true);
        assert_eq!(neg, [0xff, 0xff]);
    }

    #[test]
    fn bit_reader_msb_first_and_skip() {
        // 0b1010_0110, 0b1100_1111
        let data = [0xA6u8, 0xCF];
        let mut r = cfa::BitReader::new(&data);
        assert_eq!(r.read_bits(4), 0b1010);
        assert_eq!(r.read_bits(4), 0b0110);
        assert_eq!(r.read_bits(8), 0b1100_1111);
        // Past end -> zero bits.
        assert_eq!(r.read_bits(4), 0);

        // 12-bit reads crossing byte boundaries, matching readBits(12).
        let data = [0xFF, 0xF0, 0x00];
        let mut r = cfa::BitReader::new(&data);
        assert_eq!(r.read_bits(12), 0xFFF);
        assert_eq!(r.read_bits(12), 0x000);

        // skip_bits advances the cursor.
        let data = [0b1111_0000, 0b1010_1010];
        let mut r = cfa::BitReader::new(&data);
        r.skip_bits(4);
        assert_eq!(r.read_bits(4), 0);
        assert_eq!(r.read_bits(8), 0b1010_1010);
    }

    #[test]
    fn interpolate_single_pixel_special_case() {
        // width == 1 && height == 1: every output byte = (byte) s[0].
        let s = [0x42i16, 0, 0];
        let mut buf = [0u8; 6];
        cfa::interpolate(&s, &mut buf, &[1, 0, 2, 1], 1, 1, true);
        assert_eq!(buf, [0x42; 6]);
    }

    #[test]
    fn interpolate_fills_missing_components() {
        // 2x2 image, color map {1,0,2,1} (G R / B G), the Canon/DNG default.
        // Planar source [R|G|B] of 4 shorts each; only the present channel is
        // set at each CFA site, exactly as the Java readers populate `pix`.
        let w = 2usize;
        let h = 2usize;
        let plane = w * h;
        let color_map = [1i32, 0, 2, 1];

        // CFA layout by index (row%2)*2 + (col%2):
        //   (0,0)->1=G  (0,1)->0=R
        //   (1,0)->2=B  (1,1)->1=G
        let mut s = vec![0i16; plane * 3];
        // Greens at (0,0) and (1,1).
        s[plane] = 10; // G(0,0): channel 1, row 0, col 0
        s[plane + 1 * w + 1] = 40; // G(1,1)
                                   // Red at (0,1).
        s[1] = 20; // R(0,1): channel 0, row 0, col 1
                   // Blue at (1,0).
        s[2 * plane + 1 * w + 0] = 30; // B(1,0)

        let mut buf = vec![0u8; plane * 3 * 2];
        cfa::interpolate(&s, &mut buf, &color_map, w, h, true);

        // Helper to read an interleaved RGB sample (little-endian u16 -> i16).
        let px = |buf: &[u8], row: usize, col: usize, c: usize| -> i16 {
            let base = row * w * 6 + col * 6 + c * 2;
            i16::from_le_bytes([buf[base], buf[base + 1]])
        };

        // Present components are passed through unchanged.
        assert_eq!(px(&buf, 0, 0, 1), 10); // G present at (0,0)
        assert_eq!(px(&buf, 0, 1, 0), 20); // R present at (0,1)
        assert_eq!(px(&buf, 1, 0, 2), 30); // B present at (1,0)
        assert_eq!(px(&buf, 1, 1, 1), 40); // G present at (1,1)

        // Missing green at (0,1): neighbours in green plane are (0,0)=10 and
        // (1,1)=40 -> (10+40)/2 = 25.
        assert_eq!(px(&buf, 0, 1, 1), 25);
        // Missing red at (0,0): index 0, need_red && need_blue so !need_blue is
        // false. even_col && bayer_pattern[index+1]==0 (pattern[1]==0) -> the
        // horizontal branch: col==0 so only the right neighbour R(0,1)=20 is
        // summed (ncomps==1) -> 20.
        assert_eq!(px(&buf, 0, 0, 0), 20);
    }

    #[test]
    fn interpolate_matches_planar_passthrough_when_fully_sampled() {
        // If every CFA site already carries all three channels (degenerate but
        // exercises the "else" passthrough branches), interpolate must copy the
        // present component for each channel verbatim.
        let w = 2usize;
        let h = 2usize;
        let plane = w * h;
        let color_map = [1i32, 0, 2, 1];
        let mut s = vec![0i16; plane * 3];
        for p in 0..plane {
            s[p] = (p as i16) + 1; // R plane
            s[plane + p] = (p as i16) + 11; // G plane
            s[2 * plane + p] = (p as i16) + 21; // B plane
        }
        let mut buf = vec![0u8; plane * 3 * 2];
        cfa::interpolate(&s, &mut buf, &color_map, w, h, true);
        let px = |buf: &[u8], idx: usize, c: usize| -> i16 {
            let base = idx * 6 + c * 2;
            i16::from_le_bytes([buf[base], buf[base + 1]])
        };
        for (row, col, idx) in [(0usize, 0usize, 0usize), (0, 1, 1), (1, 0, 2), (1, 1, 3)] {
            let cfa_index = (row % 2) * 2 + (col % 2);
            // The channel present at this site is copied straight through.
            let present = color_map[cfa_index] as usize;
            let expected_plane_val = s[present * plane + idx];
            assert_eq!(px(&buf, idx, present), expected_plane_val);
        }
    }
}