exiftool-rs 0.4.1

Read, write, and edit metadata in 55+ file formats — a pure Rust reimplementation of ExifTool 13.53 with 100% tag name parity (194/194 test files)
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
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
//! Miscellaneous format readers for less common file types.
//!
//! Each format has a minimal reader extracting basic metadata.

use crate::error::{Error, Result};
use crate::metadata::XmpReader;
use crate::tag::{Tag, TagGroup, TagId};
use crate::value::Value;

// ============================================================================
// DICOM (Digital Imaging and Communications in Medicine)
// ============================================================================

pub fn read_dicom(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 136 || &data[128..132] != b"DICM" {
        return Err(Error::InvalidData("not a DICOM file".into()));
    }
    let mut tags = Vec::new();
    tags.push(mktag("DICOM", "FileFormat", "File Format", Value::String("DICOM".into())));

    // Parse DICOM data elements (group, element, VR, length, value)
    let mut pos = 132;
    let mut count = 0;
    while pos + 8 <= data.len() && count < 100 {
        let group = u16::from_le_bytes([data[pos], data[pos + 1]]);
        let element = u16::from_le_bytes([data[pos + 2], data[pos + 3]]);
        // Check for explicit VR
        let vr = &data[pos + 4..pos + 6];
        let (val_len, hdr_size) = if vr[0].is_ascii_uppercase() && vr[1].is_ascii_uppercase() {
            let len = u16::from_le_bytes([data[pos + 6], data[pos + 7]]) as usize;
            (len, 8)
        } else {
            let len = u32::from_le_bytes([data[pos + 4], data[pos + 5], data[pos + 6], data[pos + 7]]) as usize;
            (len, 8)
        };
        pos += hdr_size;

        if val_len == 0 || val_len > 10000 || pos + val_len > data.len() {
            pos += val_len.min(data.len() - pos);
            count += 1;
            continue;
        }

        let val_data = &data[pos..pos + val_len];
        let text = String::from_utf8_lossy(val_data).trim().trim_end_matches('\0').to_string();

        match (group, element) {
            (0x0008, 0x0060) => tags.push(mktag("DICOM", "Modality", "Modality", Value::String(text))),
            (0x0008, 0x0070) => tags.push(mktag("DICOM", "Manufacturer", "Manufacturer", Value::String(text))),
            (0x0008, 0x1030) => tags.push(mktag("DICOM", "StudyDescription", "Study Description", Value::String(text))),
            (0x0010, 0x0010) => tags.push(mktag("DICOM", "PatientName", "Patient Name", Value::String(text))),
            (0x0010, 0x0020) => tags.push(mktag("DICOM", "PatientID", "Patient ID", Value::String(text))),
            (0x0028, 0x0010) => {
                if val_len == 2 {
                    let v = u16::from_le_bytes([val_data[0], val_data[1]]);
                    tags.push(mktag("DICOM", "Rows", "Image Rows", Value::U16(v)));
                }
            }
            (0x0028, 0x0011) => {
                if val_len == 2 {
                    let v = u16::from_le_bytes([val_data[0], val_data[1]]);
                    tags.push(mktag("DICOM", "Columns", "Image Columns", Value::U16(v)));
                }
            }
            _ => {}
        }

        pos += val_len;
        count += 1;
        if group > 0x0028 { break; } // Stop after image dimensions
    }

    Ok(tags)
}

// ============================================================================
// FITS (Flexible Image Transport System)
// ============================================================================

pub fn read_fits(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 80 || !data.starts_with(b"SIMPLE  =") {
        return Err(Error::InvalidData("not a FITS file".into()));
    }

    let mut tags = Vec::new();
    // FITS header: 80-byte fixed-width keyword records
    let mut pos = 0;
    // For CONTINUE: track current tag to append value
    let mut continue_tag: Option<String> = None;
    let mut continue_val: String = String::new();

    while pos + 80 <= data.len() {
        let record = &data[pos..pos + 80];
        let keyword = String::from_utf8_lossy(&record[..8]).trim_end().to_string();
        pos += 80;

        if keyword == "END" { break; }

        // Handle CONTINUE keyword
        if keyword == "CONTINUE" {
            if continue_tag.is_some() {
                // Continue value from previous quoted string
                let val_raw = String::from_utf8_lossy(&record[8..]).to_string();
                let (more, cont) = fits_parse_continued_value(&val_raw);
                continue_val.push_str(&more);
                if !cont {
                    let tag_name = continue_tag.take().unwrap();
                    let tag_desc = fits_tag_description(&tag_name);
                    tags.push(mktag("FITS", &tag_name, &tag_desc, Value::String(continue_val.clone())));
                    continue_val.clear();
                }
            }
            continue;
        }

        // Flush any pending continue
        if let Some(tag_name) = continue_tag.take() {
            let tag_desc = fits_tag_description(&tag_name);
            tags.push(mktag("FITS", &tag_name, &tag_desc, Value::String(continue_val.clone())));
            continue_val.clear();
        }

        // COMMENT and HISTORY: special handling (no '= ' at position 8)
        if keyword == "COMMENT" || keyword == "HISTORY" {
            let val = String::from_utf8_lossy(&record[8..]).trim_end().to_string();
            let name = if keyword == "COMMENT" { "Comment" } else { "History" };
            tags.push(mktag("FITS", name, name, Value::String(val)));
            continue;
        }

        // Standard keyword = value
        if keyword.is_empty() { continue; }
        if record.len() <= 10 || record[8] != b'=' { continue; }

        let val_raw = String::from_utf8_lossy(&record[10..]).to_string();
        // Parse value: may be quoted string, boolean T/F, or number
        let (value, is_continued) = fits_parse_value(&val_raw);
        if value.is_empty() { continue; }

        // Map known keywords, generate names for others
        let tag_name = fits_keyword_to_name(&keyword);
        let tag_desc = fits_tag_description(&tag_name);

        if is_continued {
            continue_tag = Some(tag_name);
            continue_val = value;
        } else {
            tags.push(mktag("FITS", &tag_name, &tag_desc, Value::String(value)));
        }
    }

    // Flush pending continue
    if let Some(tag_name) = continue_tag.take() {
        let tag_desc = fits_tag_description(&tag_name);
        tags.push(mktag("FITS", &tag_name, &tag_desc, Value::String(continue_val.clone())));
    }

    Ok(tags)
}

/// Parse a FITS value field (columns 11-80 of an 80-char record).
/// Returns (value_string, is_continued) where is_continued means value ends with '&'.
fn fits_parse_value(s: &str) -> (String, bool) {
    let s = s.trim_start();
    if s.starts_with('\'') {
        // Quoted string: parse until closing quote (doubled quotes are escaped)
        let inner = &s[1..];
        let mut result = String::new();
        let mut chars = inner.chars().peekable();
        loop {
            match chars.next() {
                None => break,
                Some('\'') => {
                    if chars.peek() == Some(&'\'') {
                        // Escaped quote
                        chars.next();
                        result.push('\'');
                    } else {
                        break; // End of string
                    }
                }
                Some(c) => result.push(c),
            }
        }
        // Trim trailing spaces from quoted string
        let trimmed = result.trim_end().to_string();
        let is_cont = trimmed.ends_with('&');
        let val = if is_cont { trimmed[..trimmed.len()-1].to_string() } else { trimmed };
        (val, is_cont)
    } else {
        // Non-quoted: take everything up to comment marker /
        // Remove trailing spaces and comment
        let val = s.splitn(2, '/').next().unwrap_or("").trim().to_string();
        // Re-format float exponents: D/E -> e
        let val = val.replace('D', "e").replace('E', "e");
        if val.is_empty() { return (String::new(), false); }
        (val, false)
    }
}

/// Parse a FITS CONTINUE value (same format as normal value but starting at column 9).
fn fits_parse_continued_value(s: &str) -> (String, bool) {
    fits_parse_value(s)
}

/// Convert a FITS keyword to a tag name (ExifTool naming convention).
/// Known keywords get special names; others get generated from keyword.
fn fits_keyword_to_name(keyword: &str) -> String {
    match keyword {
        "SIMPLE"   => return String::new(), // Perl internal only
        "BITPIX"   => "Bitpix".into(),
        "NAXIS"    => "Naxis".into(),
        "NAXIS1"   => "Naxis1".into(),
        "NAXIS2"   => "Naxis2".into(),
        "EXTEND"   => "Extend".into(),
        "ORIGIN"   => "Origin".into(),
        "TELESCOP" => "Telescope".into(),
        "BACKGRND" => "Background".into(),
        "INSTRUME" => "Instrument".into(),
        "OBJECT"   => "Object".into(),
        "OBSERVER" => "Observer".into(),
        "DATE"     => "CreateDate".into(),
        "AUTHOR"   => "Creator".into(),
        "REFERENC" => "Reference".into(),
        "DATE-OBS" => "ObservationDate".into(),
        "TIME-OBS" => "ObservationTime".into(),
        "DATE-END" => "ObservationDateEnd".into(),
        "TIME-END" => "ObservationTimeEnd".into(),
        "COMMENT"  => "Comment".into(),
        "HISTORY"  => "History".into(),
        _ => {
            // Generate name: ucfirst lc tag, remove underscores and capitalize next
            let lower = keyword.to_lowercase();
            let mut result = String::new();
            let mut capitalize_next = true;
            for ch in lower.chars() {
                if ch == '_' || ch == '-' {
                    capitalize_next = true;
                } else if capitalize_next {
                    for c in ch.to_uppercase() { result.push(c); }
                    capitalize_next = false;
                } else {
                    result.push(ch);
                }
            }
            result
        }
    }
}

fn fits_tag_description(name: &str) -> String {
    // Generate description by inserting spaces before capitals
    let mut desc = String::new();
    for ch in name.chars() {
        if ch.is_uppercase() && !desc.is_empty() {
            desc.push(' ');
        }
        desc.push(ch);
    }
    desc
}

// ============================================================================
// FLV (Flash Video)
// ============================================================================

pub fn read_flv(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 9 || !data.starts_with(b"FLV\x01") {
        return Err(Error::InvalidData("not an FLV file".into()));
    }

    let mut tags = Vec::new();

    // FLV header: FLV(3) version(1) flags(1) offset(4 BE)
    let flags = data[4];
    let has_audio = flags & 0x04 != 0;
    let has_video = flags & 0x01 != 0;
    let header_offset = u32::from_be_bytes([data[5], data[6], data[7], data[8]]) as usize;

    // Parse FLV tags starting at header_offset
    // Each tag: prev_tag_size(4) type(1) data_size(3 BE) timestamp(3 BE) ts_ext(1) stream_id(3) data(N)
    // First prev_tag_size is 0 at position header_offset
    let mut pos = header_offset;

    // Skip initial prev_tag_size (4 bytes)
    if pos + 4 <= data.len() {
        pos += 4;
    }

    let mut found_meta = false;
    let mut audio_info_found = false;
    let mut video_info_found = false;

    while pos + 11 <= data.len() && (!found_meta || (!audio_info_found && has_audio) || (!video_info_found && has_video)) {
        let tag_type = data[pos];
        let data_size = ((data[pos+1] as usize) << 16) | ((data[pos+2] as usize) << 8) | (data[pos+3] as usize);
        // skip timestamp (3) + ts_ext (1) + stream_id (3) = 7 bytes
        let tag_start = pos + 11;
        let tag_end = tag_start + data_size;

        if tag_end > data.len() { break; }

        match tag_type {
            0x12 => {
                // Script (AMF metadata) tag
                if !found_meta {
                    let tag_data = &data[tag_start..tag_end];
                    flv_parse_amf_metadata(tag_data, &mut tags);
                    found_meta = true;
                }
            }
            0x08 if !audio_info_found => {
                // Audio tag: first byte = codec info
                if data_size >= 1 {
                    let info_byte = data[tag_start];
                    let codec_id = (info_byte >> 4) & 0x0f;
                    let sample_rate_idx = (info_byte >> 2) & 0x03;
                    let sample_size = (info_byte >> 1) & 0x01;
                    let stereo = info_byte & 0x01;

                    let codec_name = match codec_id {
                        0 => "Uncompressed",
                        1 => "ADPCM",
                        2 => "MP3",
                        3 => "Uncompressed LE",
                        4 => "Nellymoser 16kHz",
                        5 => "Nellymoser 8kHz",
                        6 => "Nellymoser",
                        7 => "G711 A-law",
                        8 => "G711 mu-law",
                        10 => "AAC",
                        11 => "Speex",
                        14 => "MP3 8kHz",
                        15 => "Device-specific",
                        _ => "Unknown",
                    };
                    let sample_rate = match sample_rate_idx {
                        0 => "5512", 1 => "11025", 2 => "22050", 3 => "44100", _ => "Unknown",
                    };
                    let channels = if stereo == 1 { "2 (stereo)" } else { "1 (mono)" };
                    let bits = if sample_size == 1 { "16" } else { "8" };

                    tags.push(mktag("FLV", "AudioCodecID", "Audio Codec ID", Value::String(format!("{}", codec_id))));
                    tags.push(mktag("FLV", "AudioSampleRate", "Audio Sample Rate", Value::String(sample_rate.to_string())));
                    tags.push(mktag("FLV", "AudioBitsPerSample", "Audio Bits Per Sample", Value::String(bits.to_string())));
                    tags.push(mktag("FLV", "AudioChannels", "Audio Channels", Value::String(channels.to_string())));
                    tags.push(mktag("FLV", "AudioEncoding", "Audio Encoding", Value::String(codec_name.to_string())));
                    audio_info_found = true;
                }
            }
            0x09 if !video_info_found => {
                // Video tag: first byte = codec info
                if data_size >= 1 {
                    let info_byte = data[tag_start];
                    let codec_id = info_byte & 0x0f;
                    let codec_name = match codec_id {
                        2 => "Sorenson H.263",
                        3 => "Screen video",
                        4 => "On2 VP6",
                        5 => "On2 VP6 with alpha",
                        6 => "Screen video v2",
                        7 => "H.264",
                        _ => "Unknown",
                    };
                    tags.push(mktag("FLV", "VideoCodecID", "Video Codec ID", Value::String(format!("{}", codec_id))));
                    tags.push(mktag("FLV", "VideoEncoding", "Video Encoding", Value::String(codec_name.to_string())));
                    video_info_found = true;
                }
            }
            _ => {}
        }

        // Move to next tag: skip data + prev_tag_size(4)
        pos = tag_end + 4;
    }

    // Add HasAudio/HasVideo from header flags
    if has_audio && !tags.iter().any(|t| t.name == "HasAudio") {
        tags.push(mktag("FLV", "HasAudio", "Has Audio", Value::String("Yes".into())));
    }
    if has_video && !tags.iter().any(|t| t.name == "HasVideo") {
        tags.push(mktag("FLV", "HasVideo", "Has Video", Value::String("Yes".into())));
    }

    // Deduplicate: keep the last occurrence of each tag name (mirrors Perl's hash-based storage)
    let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut deduped: Vec<Tag> = Vec::with_capacity(tags.len());
    for tag in tags.into_iter().rev() {
        if seen.insert(tag.name.clone()) {
            deduped.push(tag);
        }
    }
    deduped.reverse();

    Ok(deduped)
}

/// Parse AMF metadata from FLV script tag.
/// AMF format: type_byte + value...
/// type 0x02 = string (2-byte BE len + bytes)
/// type 0x08 = ECMAArray (4-byte count + key-value pairs until 0x00 0x00 0x09)
fn flv_parse_amf_metadata(data: &[u8], tags: &mut Vec<Tag>) {
    let mut pos = 0;

    // First value should be string "onMetaData"
    if pos + 3 > data.len() || data[pos] != 0x02 { return; }
    pos += 1;
    let str_len = u16::from_be_bytes([data[pos], data[pos+1]]) as usize;
    pos += 2;
    if pos + str_len > data.len() { return; }
    let name = String::from_utf8_lossy(&data[pos..pos+str_len]).to_string();
    pos += str_len;

    if name != "onMetaData" { return; }

    // Second value should be ECMAArray (0x08) or Object (0x03)
    if pos >= data.len() { return; }
    let container_type = data[pos];
    pos += 1;

    if container_type == 0x08 {
        // ECMAArray: 4-byte count, then key-value pairs
        if pos + 4 > data.len() { return; }
        pos += 4; // skip array count
    } else if container_type == 0x03 {
        // Object: just key-value pairs
    } else {
        return;
    }

    // Parse key-value pairs until we hit 0x00 0x00 0x09 (end marker)
    flv_parse_amf_object(data, &mut pos, tags, "");
}

/// Parse an AMF value at *pos, advancing pos.
/// For struct types (0x03, 0x08), emits sub-tags directly.
/// compound_key: the raw key built for this value (for tag name lookup).
/// struct_name: prefix for nested object keys (e.g. "CuePoint0", "keyframes").
fn flv_parse_amf_value(
    data: &[u8],
    pos: &mut usize,
    tags: &mut Vec<Tag>,
    compound_key: &str,
    struct_name: &str,
) {
    if *pos >= data.len() { return; }
    let val_type = data[*pos];
    *pos += 1;

    match val_type {
        0x00 => {
            if *pos + 8 > data.len() { return; }
            let bytes: [u8; 8] = [data[*pos], data[*pos+1], data[*pos+2], data[*pos+3],
                                   data[*pos+4], data[*pos+5], data[*pos+6], data[*pos+7]];
            let val = f64::from_be_bytes(bytes);
            *pos += 8;
            let tag_name = flv_lookup_tag(compound_key);
            let val_str = flv_apply_conv(&tag_name, val);
            tags.push(mktag("FLV", &tag_name, &tag_name, Value::String(val_str)));
        }
        0x01 => {
            if *pos >= data.len() { return; }
            let b = data[*pos] != 0;
            *pos += 1;
            let tag_name = flv_lookup_tag(compound_key);
            tags.push(mktag("FLV", &tag_name, &tag_name, Value::String(if b { "Yes" } else { "No" }.to_string())));
        }
        0x02 => {
            if *pos + 2 > data.len() { return; }
            let slen = u16::from_be_bytes([data[*pos], data[*pos+1]]) as usize;
            *pos += 2;
            if *pos + slen > data.len() { return; }
            let s = String::from_utf8_lossy(&data[*pos..*pos+slen]).to_string();
            *pos += slen;
            let tag_name = flv_lookup_tag(compound_key);
            let s = s.trim_end().to_string();
            tags.push(mktag("FLV", &tag_name, &tag_name, Value::String(s)));
        }
        0x03 | 0x08 => {
            if val_type == 0x08 {
                if *pos + 4 > data.len() { return; }
                *pos += 4;
            }
            flv_parse_amf_object(data, pos, tags, struct_name);
        }
        0x09 => { /* end marker, ignore */ }
        0x0a => {
            if *pos + 4 > data.len() { return; }
            let count = u32::from_be_bytes([data[*pos], data[*pos+1], data[*pos+2], data[*pos+3]]) as usize;
            *pos += 4;
            let mut items: Vec<String> = Vec::new();
            for i in 0..count {
                if *pos >= data.len() { break; }
                let item_type = data[*pos];
                if item_type == 0x03 || item_type == 0x08 {
                    let indexed_name = format!("{}{}", struct_name, i);
                    *pos += 1;
                    if item_type == 0x08 {
                        if *pos + 4 > data.len() { break; }
                        *pos += 4;
                    }
                    flv_parse_amf_object(data, pos, tags, &indexed_name);
                } else {
                    *pos += 1;
                    match item_type {
                        0x00 => {
                            if *pos + 8 > data.len() { break; }
                            let bytes: [u8; 8] = [data[*pos], data[*pos+1], data[*pos+2], data[*pos+3],
                                                   data[*pos+4], data[*pos+5], data[*pos+6], data[*pos+7]];
                            let v = f64::from_be_bytes(bytes);
                            *pos += 8;
                            items.push(flv_format_number(v));
                        }
                        0x01 => {
                            if *pos >= data.len() { break; }
                            let b = data[*pos] != 0;
                            *pos += 1;
                            items.push(if b { "Yes" } else { "No" }.to_string());
                        }
                        0x02 => {
                            if *pos + 2 > data.len() { break; }
                            let slen = u16::from_be_bytes([data[*pos], data[*pos+1]]) as usize;
                            *pos += 2;
                            if *pos + slen > data.len() { break; }
                            let s = String::from_utf8_lossy(&data[*pos..*pos+slen]).to_string();
                            *pos += slen;
                            items.push(s);
                        }
                        _ => { *pos = data.len(); break; }
                    }
                }
            }
            if !items.is_empty() {
                let tag_name = flv_lookup_tag(compound_key);
                tags.push(mktag("FLV", &tag_name, &tag_name, Value::String(items.join(", "))));
            }
        }
        0x0b => {
            if *pos + 10 > data.len() { return; }
            let ms = f64::from_be_bytes([data[*pos], data[*pos+1], data[*pos+2], data[*pos+3],
                                          data[*pos+4], data[*pos+5], data[*pos+6], data[*pos+7]]);
            let tz_offset = i16::from_be_bytes([data[*pos+8], data[*pos+9]]) as i32;
            *pos += 10;
            let s = flv_format_date(ms, tz_offset);
            let tag_name = flv_lookup_tag(compound_key);
            tags.push(mktag("FLV", &tag_name, &tag_name, Value::String(s)));
        }
        0x0c | 0x0f => {
            if *pos + 4 > data.len() { return; }
            let slen = u32::from_be_bytes([data[*pos], data[*pos+1], data[*pos+2], data[*pos+3]]) as usize;
            *pos += 4;
            if *pos + slen > data.len() { return; }
            let s = String::from_utf8_lossy(&data[*pos..*pos+slen]).to_string();
            *pos += slen;
            let tag_name = flv_lookup_tag(compound_key);
            tags.push(mktag("FLV", &tag_name, &tag_name, Value::String(s)));
        }
        0x05 | 0x06 => { /* null/undefined, no value bytes */ }
        _ => { *pos = data.len(); }
    }
}

fn flv_parse_amf_object(data: &[u8], pos: &mut usize, tags: &mut Vec<Tag>, struct_name: &str) {
    while *pos + 3 <= data.len() {
        if data[*pos] == 0x00 && data[*pos+1] == 0x00 && *pos + 2 < data.len() && data[*pos+2] == 0x09 {
            *pos += 3;
            break;
        }
        if *pos + 2 > data.len() { break; }
        let key_len = u16::from_be_bytes([data[*pos], data[*pos+1]]) as usize;
        *pos += 2;
        if *pos + key_len > data.len() { break; }
        let key = String::from_utf8_lossy(&data[*pos..*pos+key_len]).to_string();
        *pos += key_len;
        if *pos >= data.len() { break; }

        // Build compound key: structName + ucfirst(mapped_key), mirrors Perl's:
        //   $tag = $$tagInfo{Name} if SubDirectory
        //   $tag = $structName . ucfirst($tag) if defined $structName
        //   StructName = $tag
        let (compound_key, nested_struct) = flv_build_compound_key(struct_name, &key);

        flv_parse_amf_value(data, pos, tags, &compound_key, &nested_struct);
    }
}

/// Build (compound_key, nested_struct_name) for a given struct_name + raw key.
/// Mirrors Perl AMF ProcessMeta logic:
///   - At top level (struct_name empty): compound_key = raw_key
///   - Nested: compound_key = struct_name + ucfirst(mapped_sub_key)
///   - nested_struct = compound_key for most cases (used as StructName in Perl)
///   - Exception: SubDirectory entries (cuePoints) → nested_struct = Name = "CuePoint"
fn flv_build_compound_key(struct_name: &str, raw_key: &str) -> (String, String) {
    if struct_name.is_empty() {
        // Top-level key
        let compound_key = raw_key.to_string();
        // SubDirectory entries use their Name as the nested struct_name
        let nested_struct = match raw_key {
            "cuePoints" => "CuePoint".to_string(),
            _ => raw_key.to_string(),
        };
        (compound_key, nested_struct)
    } else {
        // Nested key: map through sub-key table first
        let mapped_key = flv_map_sub_key(struct_name, raw_key);
        let uckey = flv_ucfirst(&mapped_key);
        let compound_key = format!("{}{}", struct_name, uckey);
        // nested_struct is the compound_key (raw, used as StructName)
        let nested_struct = compound_key.clone();
        (compound_key, nested_struct)
    }
}

/// Map a raw sub-key through the appropriate subtable based on the parent struct_name.
/// e.g. inside a CuePoint struct: "parameters" → "Parameter"
fn flv_map_sub_key(struct_name: &str, key: &str) -> String {
    // CuePointN struct (but not CuePointNParameter*)
    if let Some(rest) = struct_name.strip_prefix("CuePoint") {
        let digits: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect();
        if !digits.is_empty() && !rest[digits.len()..].starts_with("Parameter") {
            // Inside CuePoint subtable
            return match key {
                "name"       => "Name".to_string(),
                "type"       => "Type".to_string(),
                "time"       => "Time".to_string(),
                "parameters" => "Parameter".to_string(),
                _ => key.to_string(),
            };
        }
    }
    key.to_string()
}


fn flv_lookup_tag(key: &str) -> String {
    match key {
        "audiocodecid"           => return "AudioCodecID".to_string(),
        "audiodatarate"          => return "AudioBitrate".to_string(),
        "audiodelay"             => return "AudioDelay".to_string(),
        "audiosamplerate"        => return "AudioSampleRate".to_string(),
        "audiosamplesize"        => return "AudioSampleSize".to_string(),
        "audiosize"              => return "AudioSize".to_string(),
        "bytelength"             => return "ByteLength".to_string(),
        "canseekontime"          => return "CanSeekOnTime".to_string(),
        "canSeekToEnd"           => return "CanSeekToEnd".to_string(),
        "creationdate"           => return "CreateDate".to_string(),
        "createdby"              => return "CreatedBy".to_string(),
        "cuePoints"              => return "CuePoint".to_string(),
        "datasize"               => return "DataSize".to_string(),
        "duration"               => return "Duration".to_string(),
        "filesize"               => return "FileSizeBytes".to_string(),
        "framerate"              => return "FrameRate".to_string(),
        "hasAudio"               => return "HasAudio".to_string(),
        "hasCuePoints"           => return "HasCuePoints".to_string(),
        "hasKeyframes"           => return "HasKeyFrames".to_string(),
        "hasMetadata"            => return "HasMetadata".to_string(),
        "hasVideo"               => return "HasVideo".to_string(),
        "height"                 => return "ImageHeight".to_string(),
        "httphostheader"         => return "HTTPHostHeader".to_string(),
        "keyframesTimes"         => return "KeyFramesTimes".to_string(),
        "keyframesFilepositions" => return "KeyFramePositions".to_string(),
        "lasttimestamp"          => return "LastTimeStamp".to_string(),
        "lastkeyframetimestamp"  => return "LastKeyFrameTime".to_string(),
        "metadatacreator"        => return "MetadataCreator".to_string(),
        "metadatadate"           => return "MetadataDate".to_string(),
        "purl"                   => return "URL".to_string(),
        "pmsg"                   => return "Message".to_string(),
        "sourcedata"             => return "SourceData".to_string(),
        "starttime"              => return "StartTime".to_string(),
        "stereo"                 => return "Stereo".to_string(),
        "totaldatarate"          => return "TotalDataRate".to_string(),
        "totalduration"          => return "TotalDuration".to_string(),
        "videocodecid"           => return "VideoCodecID".to_string(),
        "videodatarate"          => return "VideoBitrate".to_string(),
        "videosize"              => return "VideoSize".to_string(),
        "width"                  => return "ImageWidth".to_string(),
        _ => {}
    }
    flv_ucfirst(key)
}

fn flv_apply_conv(tag_name: &str, val: f64) -> String {
    match tag_name {
        "AudioBitrate" => flv_convert_bitrate(val * 1000.0),
        "VideoBitrate" => flv_convert_bitrate(val * 1000.0),
        "Duration" | "StartTime" | "TotalDuration" => flv_convert_duration(val),
        "FrameRate" => {
            let rounded = (val * 1000.0 + 0.5).floor() / 1000.0;
            flv_format_number(rounded)
        }
        _ => flv_format_number(val),
    }
}

fn flv_convert_bitrate(bps: f64) -> String {
    // Mirrors Perl's ConvertBitrate: divide by 1000 until < 1000,
    // then %.0f if >= 100, else %.3g (3 significant digits)
    let mut val = bps;
    let mut units = "bps";
    for u in &["bps", "kbps", "Mbps", "Gbps"] {
        units = u;
        if val < 1000.0 { break; }
        val /= 1000.0;
    }
    if val >= 100.0 {
        format!("{:.0} {}", val, units)
    } else {
        // 3 significant digits
        let s = format_3g(val);
        format!("{} {}", s, units)
    }
}

fn format_3g(val: f64) -> String {
    if val == 0.0 { return "0".to_string(); }
    // 3 significant digits
    let magnitude = val.abs().log10().floor() as i32;
    let factor = 10f64.powi(2 - magnitude);
    let rounded = (val * factor).round() / factor;
    if rounded.fract() == 0.0 {
        format!("{:.0}", rounded)
    } else {
        let s = format!("{:.6}", rounded);
        let s = s.trim_end_matches('0').trim_end_matches('.');
        s.to_string()
    }
}

fn flv_convert_duration(secs: f64) -> String {
    if secs == 0.0 {
        return "0 s".to_string();
    }
    let sign = if secs < 0.0 { "-" } else { "" };
    let t = secs.abs();
    if t < 30.0 {
        return format!("{}{:.2} s", sign, t);
    }
    let t = t + 0.5; // round to nearest second
    let h = (t / 3600.0) as u64;
    let t = t - (h as f64) * 3600.0;
    let m = (t / 60.0) as u64;
    let t = t - (m as f64) * 60.0;
    if h > 24 {
        let d = h / 24;
        let h = h - d * 24;
        format!("{}{}d {}:{:02}:{:02}", sign, d, h, m, t as u64)
    } else {
        format!("{}{}:{:02}:{:02}", sign, h, m, t as u64)
    }
}

fn flv_format_number(val: f64) -> String {
    if val.fract() == 0.0 && val.abs() < 1e15 {
        format!("{}", val as i64)
    } else {
        let s = format!("{:.10}", val);
        let s = s.trim_end_matches('0');
        let s = s.trim_end_matches('.');
        s.to_string()
    }
}

fn flv_ucfirst(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(c) => {
            let upper: String = c.to_uppercase().collect();
            upper + chars.as_str()
        }
    }
}

fn flv_format_date(ms: f64, tz_offset_minutes: i32) -> String {
    // Perl: ConvertUnixTime($val/1000, 0, 6) - convert to datetime with 6 decimal places
    let unix_secs = ms / 1000.0;
    let whole_secs = unix_secs.floor() as i64;
    // Microseconds from fractional part (6 decimal places)
    let usec = ((unix_secs - unix_secs.floor()) * 1_000_000.0).round() as u64;

    let epoch_to_ymdhms = |ts: i64| -> (i32, u32, u32, u32, u32, u32) {
        let days = ts / 86400;
        let rem_secs = ts % 86400;
        let hours = rem_secs / 3600;
        let mins = (rem_secs % 3600) / 60;
        let secs = rem_secs % 60;
        let mut year = 1970i32;
        let mut remaining_days = days;
        loop {
            let days_in_year = if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 { 366 } else { 365 };
            if remaining_days < days_in_year { break; }
            remaining_days -= days_in_year;
            year += 1;
        }
        let leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
        let month_days: [i64; 12] = [31, if leap { 29 } else { 28 }, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        let mut month = 1u32;
        let mut day = remaining_days + 1;
        for &md in &month_days {
            if day > md { day -= md; month += 1; } else { break; }
        }
        (year, month, day as u32, hours as u32, mins as u32, secs as u32)
    };

    let (year, month, day, hours, mins, secs) = epoch_to_ymdhms(whole_secs);
    let tz_hours = tz_offset_minutes.abs() / 60;
    let tz_mins = tz_offset_minutes.abs() % 60;
    let tz_sign = if tz_offset_minutes >= 0 { "+" } else { "-" };

    if usec != 0 {
        format!("{:04}:{:02}:{:02} {:02}:{:02}:{:02}.{:06}{}{:02}:{:02}",
                year, month, day, hours, mins, secs, usec, tz_sign, tz_hours, tz_mins)
    } else {
        format!("{:04}:{:02}:{:02} {:02}:{:02}:{:02}{}{:02}:{:02}",
                year, month, day, hours, mins, secs, tz_sign, tz_hours, tz_mins)
    }
}

// ============================================================================
// SWF (Shockwave Flash)
// ============================================================================

pub fn read_swf(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 8 {
        return Err(Error::InvalidData("not a SWF file".into()));
    }

    let compressed = match data[0] {
        b'F' => false,
        b'C' => true, // zlib compressed
        b'Z' => true, // LZMA compressed
        _ => return Err(Error::InvalidData("not a SWF file".into())),
    };

    if data[1] != b'W' || data[2] != b'S' {
        return Err(Error::InvalidData("not a SWF file".into()));
    }

    let mut tags = Vec::new();
    let version = data[3];
    let _file_length = u32::from_le_bytes([data[4], data[5], data[6], data[7]]);

    tags.push(mktag("SWF", "FlashVersion", "Flash Version", Value::U8(version)));
    tags.push(mktag("SWF", "Compressed", "Compressed",
        Value::String(if compressed { "False" } else { "False" }.into())));

    // Parse SWF body (starting at byte 8)
    // For uncompressed SWF: body starts at data[8]
    // For compressed: would need to decompress; we skip compression for now
    if !compressed && data.len() > 8 {
        parse_swf_body(&data[8..], &mut tags);
    }

    Ok(tags)
}

/// Parse the uncompressed SWF body starting after the 8-byte file header.
/// The body starts with a RECT structure (image dimensions), followed by
/// FrameRate (u16 LE, fixed 8.8) and FrameCount (u16 LE), then SWF tags.
fn parse_swf_body(body: &[u8], tags: &mut Vec<Tag>) {
    if body.is_empty() { return; }

    // RECT structure: first 5 bits = nBits (number of bits for each coordinate)
    // Then 4 values each nBits long: Xmin, Xmax, Ymin, Ymax (in twips, 1/20 pixel)
    let n_bits = (body[0] >> 3) as usize;
    let total_bits = 5 + n_bits * 4;
    let n_bytes = (total_bits + 7) / 8;

    if body.len() < n_bytes + 4 { return; }

    // Extract the bit-packed values
    // Read bit string
    let mut bit_str = 0u64;
    let bytes_to_read = n_bytes.min(8);
    for i in 0..bytes_to_read {
        bit_str = (bit_str << 8) | body[i] as u64;
    }
    // Shift to align: the first 5 bits are nBits, then we have 4 * nBits values
    let total_64 = bytes_to_read * 8;
    let shift = total_64.saturating_sub(total_bits);
    bit_str >>= shift;

    // Extract values (from LSB side after the shift)
    let mask = if n_bits >= 64 { u64::MAX } else { (1u64 << n_bits) - 1 };
    let ymax_raw = (bit_str & mask) as i32;
    let ymin_raw = ((bit_str >> n_bits) & mask) as i32;
    let xmax_raw = ((bit_str >> (n_bits * 2)) & mask) as i32;
    let xmin_raw = ((bit_str >> (n_bits * 3)) & mask) as i32;

    // Sign-extend if the high bit is set
    let sign_extend = |v: i32, bits: usize| -> i32 {
        if bits > 0 && bits < 32 && (v & (1 << (bits - 1))) != 0 {
            v | (!0i32 << bits)
        } else { v }
    };
    let xmin = sign_extend(xmin_raw, n_bits);
    let xmax = sign_extend(xmax_raw, n_bits);
    let ymin = sign_extend(ymin_raw, n_bits);
    let ymax = sign_extend(ymax_raw, n_bits);

    // Convert from twips to pixels (1/20 pixel per twip)
    let width = ((xmax - xmin) as f64) / 20.0;
    let height = ((ymax - ymin) as f64) / 20.0;

    if width >= 0.0 && height >= 0.0 {
        tags.push(mktag("SWF", "ImageWidth", "Image Width", Value::F64(width)));
        tags.push(mktag("SWF", "ImageHeight", "Image Height", Value::F64(height)));
    }

    // Frame rate (fixed point 8.8 little-endian) and frame count
    let fr_offset = n_bytes;
    if fr_offset + 4 > body.len() { return; }
    let frame_rate_raw = u16::from_le_bytes([body[fr_offset], body[fr_offset + 1]]);
    let frame_count = u16::from_le_bytes([body[fr_offset + 2], body[fr_offset + 3]]);
    let frame_rate = frame_rate_raw as f64 / 256.0;

    tags.push(mktag("SWF", "FrameRate", "Frame Rate", Value::F64(frame_rate)));
    tags.push(mktag("SWF", "FrameCount", "Frame Count", Value::U16(frame_count)));

    if frame_rate > 0.0 && frame_count > 0 {
        let duration = frame_count as f64 / frame_rate;
        tags.push(mktag("SWF", "Duration", "Duration",
            Value::String(format!("{:.2} s", duration))));
    }

    // Scan SWF tags for metadata (tag 77 = Metadata/XMP)
    let mut tag_pos = fr_offset + 4;
    let mut found_attributes = false;
    while tag_pos + 2 <= body.len() {
        let code = u16::from_le_bytes([body[tag_pos], body[tag_pos + 1]]);
        let tag_type = (code >> 6) as u16;
        let short_len = (code & 0x3F) as usize;
        tag_pos += 2;

        let tag_len = if short_len == 0x3F {
            if tag_pos + 4 > body.len() { break; }
            let l = u32::from_le_bytes([body[tag_pos], body[tag_pos+1], body[tag_pos+2], body[tag_pos+3]]) as usize;
            tag_pos += 4;
            l
        } else {
            short_len
        };

        if tag_pos + tag_len > body.len() { break; }

        match tag_type {
            69 => {
                // FileAttributes - check HasMetadata flag
                if tag_len >= 1 {
                    let flags = body[tag_pos];
                    found_attributes = true;
                    if flags & 0x10 == 0 { break; } // No metadata
                }
            }
            77 => {
                // Metadata tag (XMP)
                let xmp_data = &body[tag_pos..tag_pos + tag_len];
                // Parse XMP to extract Author and other tags
                if let Ok(xmp_tags) = crate::metadata::XmpReader::read(xmp_data) {
                    for t in xmp_tags {
                        // Only add tags not already present
                        if !tags.iter().any(|e| e.name == t.name) {
                            tags.push(t);
                        }
                    }
                }
                // Also store raw XMP
                tags.push(mktag("SWF", "XMPToolkit", "XMP Toolkit",
                    Value::String(extract_xmp_toolkit(xmp_data))));
                break;
            }
            _ => {}
        }

        tag_pos += tag_len;
    }
    let _ = found_attributes;
}

fn extract_xmp_toolkit(xmp: &[u8]) -> String {
    let text = String::from_utf8_lossy(xmp);
    // Look for xmp:CreatorTool or xmptk attribute
    if let Some(start) = text.find("xmptk=\"") {
        let after = &text[start + 7..];
        if let Some(end) = after.find('"') {
            return after[..end].to_string();
        }
    }
    if let Some(start) = text.find("<xmp:CreatorTool>") {
        let after = &text[start + 17..];
        if let Some(end) = after.find("</") {
            return after[..end].to_string();
        }
    }
    String::new()
}

// ============================================================================
// Radiance HDR
// ============================================================================

pub fn read_hdr(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 10 || (!data.starts_with(b"#?RADIANCE") && !data.starts_with(b"#?RGBE")) {
        return Err(Error::InvalidData("not a Radiance HDR file".into()));
    }

    let mut tags = Vec::new();
    let text = String::from_utf8_lossy(&data[..data.len().min(8192)]);

    // Track key-value pairs and commands (last wins for non-list tags)
    let mut kv_map: std::collections::HashMap<String, String> = std::collections::HashMap::new();
    let mut last_command: Option<String> = None;
    let mut found_dims = false;

    for line in text.lines() {
        let line = line.trim_end_matches('\r');
        // Skip the magic header line
        if line.starts_with("#?") { continue; }
        // Comment lines
        if line.starts_with('#') { continue; }
        // Empty line marks end of header metadata
        if line.is_empty() { continue; }
        // Dimension line (resolution) - last header line before data
        if line.starts_with("-Y ") || line.starts_with("+Y ") || line.starts_with("-X ") || line.starts_with("+X ") {
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() >= 4 {
                // Format: -Y <h> +X <w> or similar
                let axis1 = parts[0]; // e.g. "-Y"
                let axis3 = parts[2]; // e.g. "+X"
                let orient = format!("{} {}", axis1, axis3);
                // Map orientation
                let orient_name = match orient.as_str() {
                    "-Y +X" => "Horizontal (normal)",
                    "-Y -X" => "Mirror horizontal",
                    "+Y -X" => "Rotate 180",
                    "+Y +X" => "Mirror vertical",
                    "+X -Y" => "Mirror horizontal and rotate 270 CW",
                    "+X +Y" => "Rotate 90 CW",
                    "-X +Y" => "Mirror horizontal and rotate 90 CW",
                    "-X -Y" => "Rotate 270 CW",
                    _ => &orient,
                };
                kv_map.insert("_orient".to_string(), orient_name.to_string());
                if let Ok(dim1) = parts[1].parse::<u32>() {
                    // first axis is Y (height)
                    if axis1 == "-Y" || axis1 == "+Y" {
                        kv_map.insert("ImageHeight".to_string(), dim1.to_string());
                    } else {
                        kv_map.insert("ImageWidth".to_string(), dim1.to_string());
                    }
                }
                if let Ok(dim2) = parts[3].parse::<u32>() {
                    if axis3 == "-X" || axis3 == "+X" {
                        kv_map.insert("ImageWidth".to_string(), dim2.to_string());
                    } else {
                        kv_map.insert("ImageHeight".to_string(), dim2.to_string());
                    }
                }
            }
            found_dims = true;
            break;
        }
        // Check for key=value pairs
        if let Some(eq_pos) = line.find('=') {
            let key = line[..eq_pos].trim().to_lowercase();
            let val = line[eq_pos+1..].trim().to_string();
            // Map known keys
            let mapped_key = match key.as_str() {
                "software" => "Software",
                "view" => "View",
                "format" => "Format",
                "exposure" => "Exposure",
                "gamma" => "Gamma",
                "colorcorr" => "ColorCorrection",
                "pixaspect" => "PixelAspectRatio",
                "primaries" => "ColorPrimaries",
                _ => "",
            };
            if !mapped_key.is_empty() {
                kv_map.insert(mapped_key.to_string(), val);
            }
        } else {
            // Not a key=value, not a comment, not empty, not dimension: it's a command
            last_command = Some(line.to_string());
        }
    }

    // Emit tags in a consistent order (matching Perl output order)
    if let Some(cmd) = last_command {
        tags.push(mktag("HDR", "Command", "Command", Value::String(cmd)));
    }
    if let Some(v) = kv_map.get("Exposure") {
        tags.push(mktag("HDR", "Exposure", "Exposure", Value::String(v.clone())));
    }
    if let Some(v) = kv_map.get("Format") {
        tags.push(mktag("HDR", "Format", "Format", Value::String(v.clone())));
    }
    if let Some(h) = kv_map.get("ImageHeight") {
        if let Ok(hv) = h.parse::<u32>() {
            tags.push(mktag("HDR", "ImageHeight", "Image Height", Value::U32(hv)));
        }
    }
    if let Some(w) = kv_map.get("ImageWidth") {
        if let Ok(wv) = w.parse::<u32>() {
            tags.push(mktag("HDR", "ImageWidth", "Image Width", Value::U32(wv)));
        }
    }
    if let Some(v) = kv_map.get("_orient") {
        tags.push(mktag("HDR", "Orientation", "Orientation", Value::String(v.clone())));
    }
    if let Some(v) = kv_map.get("Software") {
        tags.push(mktag("HDR", "Software", "Software", Value::String(v.clone())));
    }
    if let Some(v) = kv_map.get("View") {
        tags.push(mktag("HDR", "View", "View", Value::String(v.clone())));
    }

    let _ = found_dims;
    Ok(tags)
}

// ============================================================================
// PPM/PGM/PBM (Netpbm formats)
// ============================================================================

pub fn read_pfm(data: &[u8]) -> Result<Vec<Tag>> {
    // Detect Printer Font Metrics (PFM) vs Portable Float Map
    // PFM starts with 0x00 followed by 0x01 or 0x02
    if data.len() >= 2 && data[0] == 0x00 && (data[1] == 0x01 || data[1] == 0x02) {
        return read_printer_font_metrics(data);
    }
    read_ppm(data)
}

/// Read Printer Font Metrics (.pfm) binary format.
/// Little-endian fields at fixed offsets, as defined in Adobe's PFM spec.
fn read_printer_font_metrics(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 117 {
        return Err(Error::InvalidData("PFM file too short".into()));
    }
    let stored_size = u32::from_le_bytes([data[2], data[3], data[4], data[5]]) as usize;
    if stored_size != data.len() {
        return Err(Error::InvalidData("PFM file size mismatch".into()));
    }

    let mut tags: Vec<Tag> = Vec::new();

    // PFMVersion at offset 0: int16u LE, PrintConv: sprintf("%x.%.2x",$val>>8,$val&0xff)
    let pfm_ver = u16::from_le_bytes([data[0], data[1]]);
    let ver_str = format!("{:x}.{:02x}", pfm_ver >> 8, pfm_ver & 0xff);
    tags.push(mktag_font("PFMVersion", "PFM Version", Value::String(ver_str)));

    // Copyright at offset 6: string[60]
    let copyright = pfm_str(data, 6, 60);
    if !copyright.is_empty() {
        tags.push(mktag_font("Copyright", "Copyright", Value::String(copyright)));
    }

    // FontType at offset 66: int16u LE
    let font_type = u16::from_le_bytes([data[66], data[67]]);
    tags.push(mktag_font("FontType", "Font Type", Value::String(format!("{}", font_type))));

    // PointSize at offset 68: int16u LE
    let point_size = u16::from_le_bytes([data[68], data[69]]);
    tags.push(mktag_font("PointSize", "Point Size", Value::String(format!("{}", point_size))));

    // YResolution at offset 70: int16u LE
    let y_res = u16::from_le_bytes([data[70], data[71]]);
    tags.push(mktag_font("YResolution", "Y Resolution", Value::String(format!("{}", y_res))));

    // XResolution at offset 72: int16u LE
    let x_res = u16::from_le_bytes([data[72], data[73]]);
    tags.push(mktag_font("XResolution", "X Resolution", Value::String(format!("{}", x_res))));

    // Ascent at offset 74: int16u LE
    let ascent = u16::from_le_bytes([data[74], data[75]]);
    tags.push(mktag_font("Ascent", "Ascent", Value::String(format!("{}", ascent))));

    // InternalLeading at offset 76: int16u LE
    let int_lead = u16::from_le_bytes([data[76], data[77]]);
    tags.push(mktag_font("InternalLeading", "Internal Leading", Value::String(format!("{}", int_lead))));

    // ExternalLeading at offset 78: int16u LE
    let ext_lead = u16::from_le_bytes([data[78], data[79]]);
    tags.push(mktag_font("ExternalLeading", "External Leading", Value::String(format!("{}", ext_lead))));

    // Italic at offset 80: int8u
    tags.push(mktag_font("Italic", "Italic", Value::String(format!("{}", data[80]))));

    // Underline at offset 81: int8u
    tags.push(mktag_font("Underline", "Underline", Value::String(format!("{}", data[81]))));

    // Strikeout at offset 82: int8u
    tags.push(mktag_font("Strikeout", "Strikeout", Value::String(format!("{}", data[82]))));

    // Weight at offset 83: int16u LE
    let weight = u16::from_le_bytes([data[83], data[84]]);
    tags.push(mktag_font("Weight", "Weight", Value::String(format!("{}", weight))));

    // CharacterSet at offset 85: int8u
    tags.push(mktag_font("CharacterSet", "Character Set", Value::String(format!("{}", data[85]))));

    // PixWidth at offset 86: int16u LE
    let pix_w = u16::from_le_bytes([data[86], data[87]]);
    tags.push(mktag_font("PixWidth", "Pix Width", Value::String(format!("{}", pix_w))));

    // PixHeight at offset 88: int16u LE
    let pix_h = u16::from_le_bytes([data[88], data[89]]);
    tags.push(mktag_font("PixHeight", "Pix Height", Value::String(format!("{}", pix_h))));

    // PitchAndFamily at offset 90: int8u
    tags.push(mktag_font("PitchAndFamily", "Pitch And Family", Value::String(format!("{}", data[90]))));

    // AvgWidth at offset 91: int16u LE
    let avg_w = u16::from_le_bytes([data[91], data[92]]);
    tags.push(mktag_font("AvgWidth", "Avg Width", Value::String(format!("{}", avg_w))));

    // MaxWidth at offset 93: int16u LE
    let max_w = u16::from_le_bytes([data[93], data[94]]);
    tags.push(mktag_font("MaxWidth", "Max Width", Value::String(format!("{}", max_w))));

    // FirstChar at offset 95: int8u
    tags.push(mktag_font("FirstChar", "First Char", Value::String(format!("{}", data[95]))));

    // LastChar at offset 96: int8u
    tags.push(mktag_font("LastChar", "Last Char", Value::String(format!("{}", data[96]))));

    // DefaultChar at offset 97: int8u
    tags.push(mktag_font("DefaultChar", "Default Char", Value::String(format!("{}", data[97]))));

    // BreakChar at offset 98: int8u
    tags.push(mktag_font("BreakChar", "Break Char", Value::String(format!("{}", data[98]))));

    // WidthBytes at offset 99: int16u LE
    let width_bytes = u16::from_le_bytes([data[99], data[100]]);
    tags.push(mktag_font("WidthBytes", "Width Bytes", Value::String(format!("{}", width_bytes))));

    // FontName and PostScriptFontName: offset to name string is at bytes 105..108 (int32u LE)
    // The name block contains: FontName\0PostScriptFontName\0
    if data.len() >= 109 {
        let name_off = u32::from_le_bytes([data[105], data[106], data[107], data[108]]) as usize;
        if name_off > 0 && name_off < data.len() {
            let rest = &data[name_off..];
            if let Some(null_pos) = rest.iter().position(|&b| b == 0) {
                let font_name: String = rest[..null_pos].iter()
                    .filter(|&&b| b >= 0x20)
                    .map(|&b| b as char)
                    .collect();
                if !font_name.is_empty() {
                    tags.push(mktag_font("FontName", "Font Name", Value::String(font_name)));
                }
                let rest2 = &rest[null_pos + 1..];
                if let Some(null_pos2) = rest2.iter().position(|&b| b == 0) {
                    let ps_name: String = rest2[..null_pos2].iter()
                        .filter(|&&b| b >= 0x20)
                        .map(|&b| b as char)
                        .collect();
                    if !ps_name.is_empty() {
                        tags.push(mktag_font("PostScriptFontName", "PostScript Font Name", Value::String(ps_name)));
                    }
                }
            }
        }
    }

    Ok(tags)
}

/// Read a null-terminated or fixed-length string from PFM data.
fn pfm_str(data: &[u8], offset: usize, max_len: usize) -> String {
    let end = (offset + max_len).min(data.len());
    if offset >= data.len() {
        return String::new();
    }
    let slice = &data[offset..end];
    let slice = if let Some(null_pos) = slice.iter().position(|&b| b == 0) {
        &slice[..null_pos]
    } else {
        slice
    };
    slice.iter()
        .filter(|&&b| b >= 0x20)
        .map(|&b| b as char)
        .collect()
}

/// Create a tag for Printer Font Metrics data.
fn mktag_font(name: &str, description: &str, value: Value) -> Tag {
    let pv = value.to_display_string();
    Tag {
        id: TagId::Text(name.to_string()),
        name: name.to_string(),
        description: description.to_string(),
        group: TagGroup {
            family0: "File".to_string(),
            family1: "Font".to_string(),
            family2: "Document".to_string(),
        },
        raw_value: value,
        print_value: pv,
        priority: 0,
    }
}

pub fn read_ppm(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 3 || data[0] != b'P' {
        return Err(Error::InvalidData("not a PBM/PGM/PPM file".into()));
    }

    let type_byte = data[1];
    let is_pfm = type_byte == b'F' || type_byte == b'f';

    let mut tags = Vec::new();

    if is_pfm {
        // PFM format: PF\n<width> <height>\n<scale>\n<data>
        // ColorSpace: PF=RGB, Pf=Monochrome
        // ByteOrder: positive scale=Big-endian, negative=Little-endian
        let text = String::from_utf8_lossy(&data[..data.len().min(256)]);
        // Match: P[Ff]\n<width> <height>\n<scale>\n
        let re_str = text.as_ref();
        // Simple line-based parser
        let mut lines = re_str.lines();
        let header_line = lines.next().unwrap_or("");
        let cs_char = if header_line.ends_with('F') || header_line == "PF" { b'F' } else { b'f' };
        let color_space = if cs_char == b'F' { "RGB" } else { "Monochrome" };
        tags.push(mktag("PFM", "ColorSpace", "Color Space", Value::String(color_space.into())));

        // Width Height line
        if let Some(wh_line) = lines.next() {
            let parts: Vec<&str> = wh_line.split_whitespace().collect();
            if parts.len() >= 2 {
                if let (Ok(w), Ok(h)) = (parts[0].parse::<u32>(), parts[1].parse::<u32>()) {
                    tags.push(mktag("PFM", "ImageWidth", "Image Width", Value::U32(w)));
                    tags.push(mktag("PFM", "ImageHeight", "Image Height", Value::U32(h)));
                }
            }
        }
        // Scale factor line
        if let Some(scale_line) = lines.next() {
            let scale_str = scale_line.trim();
            if let Ok(scale) = scale_str.parse::<f64>() {
                let byte_order = if scale > 0.0 { "Big-endian" } else { "Little-endian" };
                tags.push(mktag("PFM", "ByteOrder", "Byte Order", Value::String(byte_order.into())));
            }
        }
    } else {
        // PPM/PGM/PBM format
        // Parse header: collect comments, then width height [maxval]
        let text = String::from_utf8_lossy(&data[2..data.len().min(1024)]);
        let mut comment_lines: Vec<String> = Vec::new();
        let mut width: Option<u32> = None;
        let mut height: Option<u32> = None;
        let mut maxval: Option<u32> = None;
        let mut found_dims = false;

        // State machine: after magic byte, collect comments and parse dimensions
        let mut remaining = text.as_ref();
        // Skip initial whitespace
        remaining = remaining.trim_start();

        while !remaining.is_empty() {
            if remaining.starts_with('#') {
                // Comment line
                let end = remaining.find('\n').unwrap_or(remaining.len());
                let comment = &remaining[1..end];
                // Remove leading space after '#'
                let comment = comment.strip_prefix(' ').unwrap_or(comment);
                comment_lines.push(comment.to_string());
                remaining = &remaining[end..];
                remaining = remaining.trim_start_matches('\n').trim_start_matches('\r');
            } else if !found_dims {
                // Parse width height
                let parts: Vec<&str> = remaining.split_whitespace().collect();
                if parts.len() >= 2 {
                    if let (Ok(w), Ok(h)) = (parts[0].parse::<u32>(), parts[1].parse::<u32>()) {
                        width = Some(w);
                        height = Some(h);
                        found_dims = true;
                        // Advance past width and height
                        let skip1 = remaining.find(parts[0]).unwrap_or(0) + parts[0].len();
                        remaining = &remaining[skip1..];
                        remaining = remaining.trim_start();
                        let skip2 = remaining.find(parts[1]).unwrap_or(0) + parts[1].len();
                        remaining = &remaining[skip2..];
                        remaining = remaining.trim_start();
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            } else {
                // Check for comment before maxval
                if remaining.starts_with('#') {
                    let end = remaining.find('\n').unwrap_or(remaining.len());
                    let comment = &remaining[1..end];
                    let comment = comment.strip_prefix(' ').unwrap_or(comment);
                    comment_lines.push(comment.to_string());
                    remaining = &remaining[end..];
                    remaining = remaining.trim_start_matches('\n').trim_start_matches('\r');
                    continue;
                }
                // Parse maxval (for non-PBM types)
                let is_pbm = type_byte == b'1' || type_byte == b'4';
                if !is_pbm {
                    let parts: Vec<&str> = remaining.splitn(2, char::is_whitespace).collect();
                    if let Some(v) = parts.first() {
                        if let Ok(mv) = v.parse::<u32>() {
                            maxval = Some(mv);
                        }
                    }
                }
                break;
            }
        }

        // Comment: join lines and trim trailing newline
        if !comment_lines.is_empty() {
            let comment = comment_lines.join("\n");
            let comment = comment.trim_end_matches('\n').trim_end_matches('\r').to_string();
            tags.push(mktag("PPM", "Comment", "Comment", Value::String(comment)));
        }

        if let Some(w) = width {
            tags.push(mktag("PPM", "ImageWidth", "Image Width", Value::U32(w)));
        }
        if let Some(h) = height {
            tags.push(mktag("PPM", "ImageHeight", "Image Height", Value::U32(h)));
        }
        if let Some(mv) = maxval {
            tags.push(mktag("PPM", "MaxVal", "Max Val", Value::U32(mv)));
        }
    }

    Ok(tags)
}

// ============================================================================
// PCX (ZSoft PC Paintbrush)
// ============================================================================

pub fn read_pcx(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 128 || data[0] != 0x0A {
        return Err(Error::InvalidData("not a PCX file".into()));
    }

    let mut tags = Vec::new();
    let manufacturer = data[0x00];
    let software_ver = data[0x01];
    let encoding = data[0x02];
    let bpp = data[0x03];
    let xmin = u16::from_le_bytes([data[0x04], data[0x05]]);
    let ymin = u16::from_le_bytes([data[0x06], data[0x07]]);
    let xmax = u16::from_le_bytes([data[0x08], data[0x09]]);
    let ymax = u16::from_le_bytes([data[0x0a], data[0x0b]]);
    let hdpi = u16::from_le_bytes([data[0x0c], data[0x0d]]);
    let vdpi = u16::from_le_bytes([data[0x0e], data[0x0f]]);
    let num_planes = data[0x41];
    let bytes_per_line = u16::from_le_bytes([data[0x42], data[0x43]]);
    let color_mode = u16::from_le_bytes([data[0x44], data[0x45]]);

    let mfr_str = match manufacturer {
        10 => "ZSoft",
        _ => "Unknown",
    };
    tags.push(mktag("PCX", "Manufacturer", "Manufacturer", Value::String(mfr_str.into())));

    let sw_str = match software_ver {
        0 => "PC Paintbrush 2.5",
        2 => "PC Paintbrush 2.8 (with palette)",
        3 => "PC Paintbrush 2.8 (without palette)",
        4 => "PC Paintbrush for Windows",
        5 => "PC Paintbrush 3.0+",
        _ => "Unknown",
    };
    tags.push(mktag("PCX", "Software", "Software", Value::String(sw_str.into())));

    let enc_str = match encoding {
        1 => "RLE",
        _ => "Unknown",
    };
    tags.push(mktag("PCX", "Encoding", "Encoding", Value::String(enc_str.into())));

    tags.push(mktag("PCX", "BitsPerPixel", "Bits Per Pixel", Value::U8(bpp)));
    tags.push(mktag("PCX", "LeftMargin", "Left Margin", Value::U16(xmin)));
    tags.push(mktag("PCX", "TopMargin", "Top Margin", Value::U16(ymin)));
    tags.push(mktag("PCX", "ImageWidth", "Image Width", Value::U16(xmax - xmin + 1)));
    tags.push(mktag("PCX", "ImageHeight", "Image Height", Value::U16(ymax - ymin + 1)));
    tags.push(mktag("PCX", "XResolution", "X Resolution", Value::U16(hdpi)));
    tags.push(mktag("PCX", "YResolution", "Y Resolution", Value::U16(vdpi)));
    tags.push(mktag("PCX", "ColorPlanes", "Color Planes", Value::U8(num_planes)));
    tags.push(mktag("PCX", "BytesPerLine", "Bytes Per Line", Value::U16(bytes_per_line)));

    let cm_str = match color_mode {
        0 => "n/a",
        1 => "Color Palette",
        2 => "Grayscale",
        _ => "Unknown",
    };
    tags.push(mktag("PCX", "ColorMode", "Color Mode", Value::String(cm_str.into())));

    Ok(tags)
}

// ============================================================================
// DjVu
// ============================================================================

pub fn read_djvu(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 16 || !data.starts_with(b"AT&TFORM") {
        return Err(Error::InvalidData("not a DjVu file".into()));
    }

    let mut tags = Vec::new();
    let form_type = &data[12..16];

    let doc_type = match form_type {
        b"DJVU" => "DjVu Single-Page",
        b"DJVM" => "DjVu Multi-Page",
        b"PM44" | b"BM44" => "DjVu Photo/Bitmap",
        _ => "DjVu",
    };
    tags.push(mktag("DjVu", "DocumentType", "Document Type", Value::String(doc_type.into())));

    // Parse INFO chunk for dimensions
    let mut pos = 16;
    while pos + 8 <= data.len() {
        let chunk_id = &data[pos..pos + 4];
        let chunk_size = u32::from_be_bytes([data[pos + 4], data[pos + 5], data[pos + 6], data[pos + 7]]) as usize;
        pos += 8;

        if chunk_id == b"INFO" && chunk_size >= 10 && pos + 10 <= data.len() {
            let width = u16::from_be_bytes([data[pos], data[pos + 1]]);
            let height = u16::from_be_bytes([data[pos + 2], data[pos + 3]]);
            let dpi = u16::from_le_bytes([data[pos + 6], data[pos + 7]]);

            tags.push(mktag("DjVu", "ImageWidth", "Image Width", Value::U16(width)));
            tags.push(mktag("DjVu", "ImageHeight", "Image Height", Value::U16(height)));
            if dpi > 0 {
                tags.push(mktag("DjVu", "Resolution", "Resolution", Value::U16(dpi)));
            }
            break;
        }

        pos += chunk_size;
        if chunk_size % 2 != 0 { pos += 1; }
    }

    Ok(tags)
}

// ============================================================================
// FLIF (Free Lossless Image Format)
// ============================================================================

pub fn read_flif(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 6 || !data.starts_with(b"FLIF") {
        return Err(Error::InvalidData("not a FLIF file".into()));
    }

    let mut tags = Vec::new();
    let byte4 = data[4];
    // ExifTool FLIF tag 0: type char (determines interlaced, color mode)
    let type_char = byte4 as char;
    // ExifTool tag 1: bit depth char
    let bpc_char = data[5] as char;

    // ImageType: ExifTool maps the type byte directly
    let image_type = match type_char {
        '1' => "Grayscale (non-interlaced)",
        '3' => "RGB (non-interlaced)",
        '4' => "RGBA (non-interlaced)",
        'A' => "Grayscale (interlaced)",
        'C' => "RGB (interlaced)",
        'D' => "RGBA (interlaced)",
        'Q' => "Grayscale Animation (non-interlaced)",
        'S' => "RGB Animation (non-interlaced)",
        'T' => "RGBA Animation (non-interlaced)",
        'a' => "Grayscale Animation (interlaced)",
        'c' => "RGB Animation (interlaced)",
        'd' => "RGBA Animation (interlaced)",
        _ => "Unknown",
    };
    tags.push(mktag("FLIF", "ImageType", "Image Type", Value::String(image_type.into())));

    // BitDepth
    let bit_depth = match bpc_char {
        '0' => "Custom",
        '1' => "8",
        '2' => "16",
        _ => "Unknown",
    };
    tags.push(mktag("FLIF", "BitDepth", "Bit Depth", Value::String(bit_depth.into())));

    // Width and height are varint encoded starting at offset 6
    let mut pos = 6;
    if let Some((w, consumed)) = read_flif_varint(data, pos) {
        let width = (w + 1) as u32;
        tags.push(mktag("FLIF", "ImageWidth", "Image Width", Value::U32(width)));
        pos += consumed;
        if let Some((h, consumed2)) = read_flif_varint(data, pos) {
            let height = (h + 1) as u32;
            tags.push(mktag("FLIF", "ImageHeight", "Image Height", Value::U32(height)));
            pos += consumed2;

            // If animation type (byte4 > 'H' = 0x48), read frame count varint
            if byte4 > 0x48 {
                if let Some((frames, consumed3)) = read_flif_varint(data, pos) {
                    let frame_count = (frames + 2) as u32;
                    tags.push(mktag("FLIF", "AnimationFrames", "Animation Frames", Value::U32(frame_count)));
                    pos += consumed3;
                }
            }
        }
    }

    // Parse metadata chunks: each chunk has a 4-byte tag, then varint size, then compressed data
    loop {
        if pos + 4 >= data.len() { break; }
        let chunk_tag = &data[pos..pos + 4];
        let first_byte = chunk_tag[0];
        // If first byte < 32, it's the start of image data
        if first_byte < 32 {
            // Encoding tag
            let encoding = match first_byte {
                0 => "FLIF16",
                _ => "Unknown",
            };
            tags.push(mktag("FLIF", "Encoding", "Encoding", Value::String(encoding.into())));
            break;
        }
        pos += 4;
        let chunk_tag = std::str::from_utf8(chunk_tag).unwrap_or("").to_string();

        let size = match read_flif_varint(data, pos) {
            Some((s, consumed)) => {
                pos += consumed;
                s as usize
            }
            None => break,
        };

        if pos + size > data.len() { break; }
        let chunk_data = &data[pos..pos + size];
        pos += size;

        // Try to inflate (raw deflate)
        let inflated = flif_inflate(chunk_data);
        let payload = if let Some(ref d) = inflated { d.as_slice() } else { chunk_data };

        match chunk_tag.as_str() {
            "iCCP" => {
                // ICC profile
                if let Ok(icc_tags) = crate::formats::icc::read_icc(payload) {
                    tags.extend(icc_tags);
                }
            }
            "eXif" => {
                // EXIF: skip "Exif\0\0" header if present
                let exif_data = if payload.starts_with(b"Exif\x00\x00") {
                    &payload[6..]
                } else {
                    payload
                };
                if let Ok(exif_tags) = crate::metadata::ExifReader::read(exif_data) {
                    tags.extend(exif_tags);
                }
            }
            "eXmp" => {
                // XMP
                if let Ok(xmp_tags) = crate::metadata::XmpReader::read(payload) {
                    tags.extend(xmp_tags);
                }
            }
            _ => {}
        }
    }

    Ok(tags)
}

/// Try to inflate raw deflate-compressed data (FLIF metadata chunks).
/// FLIF uses raw deflate (no zlib/gzip header).
fn flif_inflate(data: &[u8]) -> Option<Vec<u8>> {
    use std::io::Read;
    // Try raw deflate first
    {
        use flate2::read::DeflateDecoder;
        let mut decoder = DeflateDecoder::new(data);
        let mut output = Vec::new();
        if decoder.read_to_end(&mut output).is_ok() && !output.is_empty() {
            return Some(output);
        }
    }
    // Fallback: try zlib
    {
        use flate2::read::ZlibDecoder;
        let mut decoder = ZlibDecoder::new(data);
        let mut output = Vec::new();
        if decoder.read_to_end(&mut output).is_ok() && !output.is_empty() {
            return Some(output);
        }
    }
    None
}

fn read_flif_varint(data: &[u8], mut pos: usize) -> Option<(u64, usize)> {
    let start = pos;
    let mut result = 0u64;
    loop {
        if pos >= data.len() { return None; }
        let byte = data[pos];
        result = (result << 7) | (byte & 0x7F) as u64;
        pos += 1;
        if byte & 0x80 == 0 { break; }
        if pos - start > 8 { return None; }
    }
    Some((result, pos - start))
}

// ============================================================================
// BPG (Better Portable Graphics)
// ============================================================================

pub fn read_bpg(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 6 || !data.starts_with(&[0x42, 0x50, 0x47, 0xFB]) {
        return Err(Error::InvalidData("not a BPG file".into()));
    }

    let mut tags = Vec::new();

    // Bytes 4-5 are a big-endian 16-bit word containing multiple bit fields
    // Layout matches Perl BPG::Main ProcessBinaryData at offset 4, Format int16u:
    //   bits 15-13 (mask 0xe000): PixelFormat
    //   bits 12,2  (mask 0x1004): Alpha
    //   bits 11-8  (mask 0x0f00): BitDepth (value + 8)
    //   bits 7-4   (mask 0x00f0): ColorSpace
    //   bits 3,1,0 (mask 0x000b): Flags
    let word = u16::from_be_bytes([data[4], data[5]]);

    let pixel_format = (word & 0xe000) >> 13;
    let alpha_raw = word & 0x1004;
    let bit_depth = ((word & 0x0f00) >> 8) + 8;
    let flags = word & 0x000b;

    let pf_name = match pixel_format {
        0 => "Grayscale",
        1 => "4:2:0 (chroma at 0.5, 0.5)",
        2 => "4:2:2 (chroma at 0.5, 0)",
        3 => "4:4:4",
        4 => "4:2:0 (chroma at 0, 0.5)",
        5 => "4:2:2 (chroma at 0, 0)",
        _ => "Unknown",
    };
    tags.push(mktag("BPG", "PixelFormat", "Pixel Format", Value::String(pf_name.into())));

    let alpha_name = match alpha_raw {
        0x1000 => "Alpha Exists (color not premultiplied)",
        0x1004 => "Alpha Exists (color premultiplied)",
        0x0004 => "Alpha Exists (W color component)",
        _ => "No Alpha Plane",
    };
    tags.push(mktag("BPG", "Alpha", "Alpha", Value::String(alpha_name.into())));

    tags.push(mktag("BPG", "BitDepth", "Bit Depth", Value::U32(bit_depth as u32)));

    // Flags: bitmask (bit 0=Animation, bit 1=Limited Range, bit 3=Extension Present)
    let mut flag_parts: Vec<&str> = Vec::new();
    if flags & 0x0001 != 0 { flag_parts.push("Animation"); }
    if flags & 0x0002 != 0 { flag_parts.push("Limited Range"); }
    if flags & 0x0008 != 0 { flag_parts.push("Extension Present"); }
    let flags_str = flag_parts.join(", ");
    tags.push(mktag("BPG", "Flags", "Flags", Value::String(flags_str.into())));

    // Width, height, and image length are ue7-encoded starting at offset 6
    let mut pos = 6;
    if let Some((w, consumed)) = read_bpg_ue(data, pos) {
        tags.push(mktag("BPG", "ImageWidth", "Image Width", Value::U32(w as u32)));
        pos += consumed;
        if let Some((h, consumed)) = read_bpg_ue(data, pos) {
            tags.push(mktag("BPG", "ImageHeight", "Image Height", Value::U32(h as u32)));
            pos += consumed;
            if let Some((img_len, consumed)) = read_bpg_ue(data, pos) {
                tags.push(mktag("BPG", "ImageLength", "Image Length", Value::U32(img_len as u32)));
                pos += consumed;

                // Parse extension blocks if the Extension Present flag is set
                if flags & 0x0008 != 0 {
                    if let Some((ext_size, n)) = read_bpg_ue(data, pos) {
                        pos += n;
                        let ext_end = pos + ext_size as usize;
                        if ext_end <= data.len() {
                            bpg_parse_extensions(data, pos, ext_end, &mut tags);
                        }
                    }
                }
            }
        }
    }

    Ok(tags)
}

fn bpg_parse_extensions(data: &[u8], start: usize, end: usize, tags: &mut Vec<Tag>) {
    let mut pos = start;
    while pos < end {
        if pos >= data.len() { break; }
        let ext_type = data[pos];
        pos += 1;
        let (ext_len, n) = match read_bpg_ue(data, pos) {
            Some(v) => v,
            None => break,
        };
        pos += n;
        let ext_len = ext_len as usize;
        if pos + ext_len > end { break; }
        let ext_data = &data[pos..pos + ext_len];
        pos += ext_len;

        match ext_type {
            1 => {
                // EXIF: raw TIFF data (no "Exif\0\0" prefix).
                // libbpg sometimes adds an extra padding byte before the TIFF header.
                let exif_data = if ext_len > 3 {
                    let b0 = ext_data[0];
                    let b1 = ext_data[1];
                    let b2 = ext_data[2];
                    // Check for extra byte before II or MM TIFF header
                    if b0 != b'I' && b0 != b'M' && (b1 == b'I' || b1 == b'M') && b1 == b2 {
                        tags.push(mktag("ExifTool", "Warning", "Warning",
                            Value::String("[minor] Ignored extra byte at start of EXIF extension".into())));
                        &ext_data[1..]
                    } else {
                        ext_data
                    }
                } else {
                    ext_data
                };
                if let Ok(exif_tags) = crate::metadata::ExifReader::read(exif_data) {
                    tags.extend(exif_tags);
                }
            }
            2 => {
                // ICC Profile
                if let Ok(icc_tags) = crate::formats::icc::read_icc(ext_data) {
                    tags.extend(icc_tags);
                }
            }
            3 => {
                // XMP
                if let Ok(xmp_tags) = crate::metadata::XmpReader::read(ext_data) {
                    tags.extend(xmp_tags);
                }
            }
            _ => {
                // Extension types 4 (ThumbnailBPG) and 5 (AnimationControl) are binary/unknown
            }
        }
    }
}

fn read_bpg_ue(data: &[u8], mut pos: usize) -> Option<(u64, usize)> {
    // BPG uses a simple ue7 varint: MSB is continuation bit
    let start = pos;
    let mut result = 0u64;
    loop {
        if pos >= data.len() { return None; }
        let byte = data[pos];
        result = (result << 7) | (byte & 0x7F) as u64;
        pos += 1;
        if byte & 0x80 == 0 { break; }
        if pos - start > 8 { return None; }
    }
    Some((result, pos - start))
}

// ============================================================================
// PICT (Apple QuickDraw Picture)
// ============================================================================

pub fn read_pict(data: &[u8]) -> Result<Vec<Tag>> {
    // PICT files have a 512-byte header (usually zeros) then the picture data
    let offset = if data.len() > 522 && data[..512].iter().all(|&b| b == 0) {
        512
    } else {
        0
    };

    if offset + 10 > data.len() {
        return Err(Error::InvalidData("not a PICT file".into()));
    }

    let mut tags = Vec::new();
    let d = &data[offset..];

    // Size (2 bytes) + bounding rect (8 bytes: top, left, bottom, right)
    let top = i16::from_be_bytes([d[2], d[3]]);
    let left = i16::from_be_bytes([d[4], d[5]]);
    let bottom = i16::from_be_bytes([d[6], d[7]]);
    let right = i16::from_be_bytes([d[8], d[9]]);

    // Check PICT version at byte 10
    // Version 2 opcode: 0x0011 at bytes 10-11
    let mut h_res: Option<f64> = None;
    let mut v_res: Option<f64> = None;
    let mut w = (right - left) as i32;
    let mut h = (bottom - top) as i32;

    if d.len() >= 40 && d[10] == 0x00 && d[11] == 0x11 {
        // Version 2: next 2 bytes are 0x02ff, then check for extended
        // d[12..14] = 0x02ff, d[14..16] = 0x0c00
        // d[16..18]: 0xffff = normal, 0xfffe = extended
        if d.len() >= 18 && d[12] == 0x02 && d[13] == 0xff {
            if d[16] == 0xff && d[17] == 0xfe && d.len() >= 36 {
                // Extended version 2: resolution at offsets 24..28 and 28..32 (x8 skip from byte 16)
                // From Perl: unpack('x8N2', $buff) where buff starts at byte after 0x0011 opcode
                // $buff was read starting at position 12 (after 12-byte first read)
                // x8 skips bytes 12..20, N2 reads bytes 20..24 and 24..28 in original data
                // Actually the 28 bytes buff starts after the 12-byte header
                // In d: after opcode 0x0011 at d[10..12], read 28 bytes: d[12..40]
                // x8 skip => skip d[12..20], N2 => d[20..24] and d[24..28]
                let h_fixed = i32::from_be_bytes([d[20], d[21], d[22], d[23]]);
                let v_fixed = i32::from_be_bytes([d[24], d[25], d[26], d[27]]);
                if h_fixed != 0 && v_fixed != 0 {
                    h_res = Some(h_fixed as f64 / 65536.0);
                    v_res = Some(v_fixed as f64 / 65536.0);
                    // Scale dimensions from 72-dpi equivalent
                    w = (w as f64 * h_res.unwrap() / 72.0 + 0.5) as i32;
                    h = (h as f64 * v_res.unwrap() / 72.0 + 0.5) as i32;
                }
            }
        }
    }

    tags.push(mktag("PICT", "ImageWidth", "Image Width", Value::I32(w)));
    tags.push(mktag("PICT", "ImageHeight", "Image Height", Value::I32(h)));
    if let Some(hr) = h_res {
        tags.push(mktag("PICT", "XResolution", "X Resolution", Value::String(format!("{}", hr as i64))));
    }
    if let Some(vr) = v_res {
        tags.push(mktag("PICT", "YResolution", "Y Resolution", Value::String(format!("{}", vr as i64))));
    }

    Ok(tags)
}

// ============================================================================
// Kyocera Contax N Digital RAW
// ============================================================================

pub fn read_kyocera_raw(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 156 {
        return Err(Error::InvalidData("too short for Kyocera RAW".into()));
    }
    // Validate: "ARECOYK" at offset 0x19
    if &data[0x19..0x20] != b"ARECOYK" {
        return Err(Error::InvalidData("not a Kyocera RAW file".into()));
    }

    let mut tags = Vec::new();
    let group = "KyoceraRaw";

    // FirmwareVersion at 0x01, 10 bytes, reversed string
    let fw_bytes: Vec<u8> = data[0x01..0x0b].iter().rev().copied().collect();
    let fw = String::from_utf8_lossy(&fw_bytes).trim_matches('\0').to_string();
    if !fw.is_empty() {
        tags.push(mktag(group, "FirmwareVersion", "Firmware Version", Value::String(fw)));
    }

    // Model at 0x0c, 12 bytes, reversed string
    let model_bytes: Vec<u8> = data[0x0c..0x18].iter().rev().copied().collect();
    let model = String::from_utf8_lossy(&model_bytes).trim_matches('\0').to_string();
    if !model.is_empty() {
        tags.push(mktag(group, "Model", "Camera Model Name", Value::String(model)));
    }

    // Make at 0x19, 7 bytes, reversed string
    let make_bytes: Vec<u8> = data[0x19..0x20].iter().rev().copied().collect();
    let make = String::from_utf8_lossy(&make_bytes).trim_matches('\0').to_string();
    if !make.is_empty() {
        tags.push(mktag(group, "Make", "Camera Make", Value::String(make)));
    }

    // DateTimeOriginal at 0x21, 20 bytes, reversed string
    let dt_bytes: Vec<u8> = data[0x21..0x35].iter().rev().copied().collect();
    let dt_str = String::from_utf8_lossy(&dt_bytes).trim_matches('\0').to_string();
    if !dt_str.is_empty() {
        tags.push(mktag(group, "DateTimeOriginal", "Date/Time Original", Value::String(dt_str)));
    }

    // ISO at 0x34, int32u (big-endian, index into table)
    if data.len() >= 0x38 {
        let iso_idx = u32::from_be_bytes([data[0x34], data[0x35], data[0x36], data[0x37]]);
        let iso_val = kyocera_iso(iso_idx);
        if iso_val > 0 {
            let mut t = mktag(group, "ISO", "ISO", Value::String(iso_idx.to_string()));
            t.print_value = iso_val.to_string();
            tags.push(t);
        }
    }

    // ExposureTime at 0x38, int32u: 2^(val/8)/16000
    if data.len() >= 0x3c {
        let et_idx = u32::from_be_bytes([data[0x38], data[0x39], data[0x3a], data[0x3b]]);
        let et_val = f64::powf(2.0, et_idx as f64 / 8.0) / 16000.0;
        let print_val = format_exposure_time(et_val);
        let mut t = mktag(group, "ExposureTime", "Exposure Time", Value::String(format!("{:.10}", et_val)));
        t.print_value = print_val;
        tags.push(t);
    }

    // WB_RGGBLevels at 0x3c, int32u[4]
    if data.len() >= 0x4c {
        let r = u32::from_be_bytes([data[0x3c], data[0x3d], data[0x3e], data[0x3f]]);
        let g1 = u32::from_be_bytes([data[0x40], data[0x41], data[0x42], data[0x43]]);
        let g2 = u32::from_be_bytes([data[0x44], data[0x45], data[0x46], data[0x47]]);
        let b = u32::from_be_bytes([data[0x48], data[0x49], data[0x4a], data[0x4b]]);
        let wb_str = format!("{} {} {} {}", r, g1, g2, b);
        tags.push(mktag(group, "WB_RGGBLevels", "WB RGGB Levels", Value::String(wb_str)));
    }

    // FNumber at 0x58, int32u: 2^(val/16)
    if data.len() >= 0x5c {
        let fn_idx = u32::from_be_bytes([data[0x58], data[0x59], data[0x5a], data[0x5b]]);
        let fn_val = f64::powf(2.0, fn_idx as f64 / 16.0);
        let print_val = format!("{}", (fn_val * 10000.0).round() / 10000.0);
        let mut t = mktag(group, "FNumber", "F Number", Value::String(format!("{}", fn_val)));
        t.print_value = print_val;
        tags.push(t);
    }

    // MaxAperture at 0x68, int32u: 2^(val/16)
    if data.len() >= 0x6c {
        let ma_idx = u32::from_be_bytes([data[0x68], data[0x69], data[0x6a], data[0x6b]]);
        let ma_val = f64::powf(2.0, ma_idx as f64 / 16.0);
        let print_val = format!("{}", (ma_val * 100.0).round() / 100.0);
        let mut t = mktag(group, "MaxAperture", "Max Aperture Value", Value::String(format!("{}", ma_val)));
        t.print_value = print_val;
        tags.push(t);
    }

    // FocalLength at 0x70, int32u
    if data.len() >= 0x74 {
        let fl = u32::from_be_bytes([data[0x70], data[0x71], data[0x72], data[0x73]]);
        let mut t = mktag(group, "FocalLength", "Focal Length", Value::String(fl.to_string()));
        t.print_value = format!("{} mm", fl);
        tags.push(t);
    }

    // Lens at 0x7c, string[32]
    if data.len() >= 0x9c {
        let lens_bytes = &data[0x7c..0x9c];
        let lens = String::from_utf8_lossy(lens_bytes).trim_matches('\0').to_string();
        if !lens.is_empty() {
            tags.push(mktag(group, "Lens", "Lens", Value::String(lens)));
        }
    }

    Ok(tags)
}

fn kyocera_iso(idx: u32) -> u32 {
    match idx {
        7 => 25, 8 => 32, 9 => 40, 10 => 50, 11 => 64, 12 => 80,
        13 => 100, 14 => 125, 15 => 160, 16 => 200, 17 => 250,
        18 => 320, 19 => 400, _ => 0,
    }
}

fn format_exposure_time(val: f64) -> String {
    if val == 0.0 { return "0".to_string(); }
    if val >= 1.0 {
        format!("{}", val)
    } else {
        let recip = (1.0 / val).round() as u32;
        format!("1/{}", recip)
    }
}

// ============================================================================
// M2TS (MPEG-2 Transport Stream)
// ============================================================================

// --- M2TS bit reader for SPS parsing ---
struct M2tsBitReader<'a> {
    data: &'a [u8],
    byte_pos: usize,
    bit_pos: u8,
    current: u8,
}

impl<'a> M2tsBitReader<'a> {
    fn new(data: &'a [u8]) -> Self {
        let (byte_pos, bit_pos, current) = if data.is_empty() {
            (0, 0, 0)
        } else {
            (1, 8, data[0])
        };
        M2tsBitReader { data, byte_pos, bit_pos, current }
    }

    fn read_bit(&mut self) -> Option<u32> {
        if self.bit_pos == 0 {
            if self.byte_pos >= self.data.len() { return None; }
            self.current = self.data[self.byte_pos];
            self.byte_pos += 1;
            self.bit_pos = 8;
        }
        self.bit_pos -= 1;
        Some(((self.current >> self.bit_pos) & 1) as u32)
    }

    fn read_bits(&mut self, n: u32) -> Option<u32> {
        let mut val = 0u32;
        for _ in 0..n { val = (val << 1) | self.read_bit()?; }
        Some(val)
    }

    fn skip_bits(&mut self, n: u32) {
        for _ in 0..n { let _ = self.read_bit(); }
    }

    fn read_ue(&mut self) -> Option<u32> {
        let mut leading = 0u32;
        while self.read_bit()? == 0 {
            leading += 1;
            if leading > 31 { return None; }
        }
        // After while loop, the '1' terminator bit was consumed.
        // Now read 'leading' INFO bits.
        let mut info = 0u32;
        for _ in 0..leading { info = (info << 1) | self.read_bit()?; }
        Some((1 << leading) + info - 1)
    }

    fn read_se(&mut self) -> Option<i32> {
        let ue = self.read_ue()?;
        let abs_val = ((ue + 1) >> 1) as i32;
        Some(if ue & 1 != 0 { abs_val } else { -abs_val })
    }
}

/// MDPM (Modified DV Pack Metadata) data extracted from H.264 SEI unregistered user data
#[derive(Clone)]
struct M2tsMdpmData {
    datetime_original: Option<String>,
    aperture_setting: Option<String>,
    gain: Option<String>,
    image_stabilization: Option<String>,
    exposure_time: Option<String>,
    shutter_speed: Option<String>,
    make: Option<String>,
    recording_mode: Option<String>,
}

/// Parse SEI NAL unit (type 6) from H.264 and extract MDPM camera metadata.
/// UUID: 17ee8c60-f84d-11d9-8cd6-0800200c9a66 + "MDPM"
fn m2ts_parse_sei(nal_data: &[u8]) -> Option<M2tsMdpmData> {
    // Remove emulation prevention bytes (0x000003 -> 0x0000)
    let mut rbsp = Vec::with_capacity(nal_data.len());
    let mut i = 0;
    while i < nal_data.len() {
        if i + 2 < nal_data.len() && nal_data[i] == 0 && nal_data[i+1] == 0 && nal_data[i+2] == 3 {
            rbsp.push(0); rbsp.push(0); i += 3;
        } else {
            rbsp.push(nal_data[i]); i += 1;
        }
    }

    let data = &rbsp;
    let end = data.len();
    let mut pos = 1; // skip nal_unit_type byte (0x06)

    // Scan SEI payloads
    while pos < end {
        // Read payload type (extended via 0xFF bytes)
        let mut sei_type: u32 = 0;
        loop {
            if pos >= end { return None; }
            let t = data[pos]; pos += 1;
            sei_type += t as u32;
            if t != 0xFF { break; }
        }
        if sei_type == 0x80 { return None; } // terminator

        // Read payload size
        let mut sei_size: usize = 0;
        loop {
            if pos >= end { return None; }
            let t = data[pos]; pos += 1;
            sei_size += t as usize;
            if t != 0xFF { break; }
        }
        if pos + sei_size > end { return None; }

        if sei_type == 5 {
            // Unregistered user data: check for MDPM UUID
            // UUID bytes: 17 ee 8c 60 f8 4d 11 d9 8c d6 08 00 20 0c 9a 66
            // followed by "MDPM" (4 bytes)
            let payload = &data[pos..pos + sei_size];
            if sei_size > 20 {
                let uuid_mdpm = b"\x17\xee\x8c\x60\xf8\x4d\x11\xd9\x8c\xd6\x08\x00\x20\x0c\x9a\x66MDPM";
                if payload.len() >= 20 && &payload[..20] == uuid_mdpm {
                    return m2ts_parse_mdpm(&payload[20..]);
                }
            }
        }

        pos += sei_size;
    }
    None
}

/// Parse MDPM entries and decode camera metadata tags.
fn m2ts_parse_mdpm(data: &[u8]) -> Option<M2tsMdpmData> {
    if data.is_empty() { return None; }

    let mut result = M2tsMdpmData {
        datetime_original: None,
        aperture_setting: None,
        gain: None,
        image_stabilization: None,
        exposure_time: None,
        shutter_speed: None,
        make: None,
        recording_mode: None,
    };

    let num = data[0] as usize;
    let mut pos = 1;
    let end = data.len();
    let mut last_tag: u8 = 0;
    let mut index = 0;

    while index < num && pos + 5 <= end {
        let tag = data[pos];
        if tag <= last_tag && index > 0 { break; } // out of sequence
        last_tag = tag;

        let val4 = [data[pos+1], data[pos+2], data[pos+3], data[pos+4]];
        pos += 5;
        index += 1;

        match tag {
            0x18 => {
                // DateTimeOriginal: combine with next tag (0x19)
                // Read 4 bytes from current tag, then peek at next tag
                let mut combined = val4.to_vec();
                if pos + 5 <= end && data[pos] == 0x19 {
                    combined.extend_from_slice(&data[pos+1..pos+5]);
                    pos += 5; index += 1; last_tag = 0x19;
                }
                // combined = [tz, yy_high, yy_low, mm, dd, HH, MM, SS] (BCD / raw)
                // ExifTool ValueConv: my ($tz, @a) = unpack('C*',$val);
                // sprintf('%.2x%.2x:%.2x:%.2x %.2x:%.2x:%.2x%s%.2d:%s%s', @a, ...)
                if combined.len() >= 8 {
                    let tz = combined[0];
                    let yh = combined[1]; // year high byte
                    let yl = combined[2]; // year low byte
                    let mo = combined[3];
                    let dy = combined[4];
                    let hh = combined[5];
                    let mm = combined[6];
                    let ss = combined[7];
                    let sign = if tz & 0x20 != 0 { '-' } else { '+' };
                    let tz_h = (tz >> 1) & 0x0f;
                    let tz_m = if tz & 0x01 != 0 { "30" } else { "00" };
                    let dst = if tz & 0x40 != 0 { " DST" } else { "" };
                    let s = format!("{:02x}{:02x}:{:02x}:{:02x} {:02x}:{:02x}:{:02x}{}{:02}:{}{}", yh, yl, mo, dy, hh, mm, ss, sign, tz_h, tz_m, dst);
                    result.datetime_original = Some(s);
                }
            }
            0x70 => {
                // Camera1: byte 0 = ApertureSetting, byte 1 = Gain (low nibble) + ExposureProgram (high nibble)
                let aperture_raw = val4[0];
                let aperture = match aperture_raw {
                    0xFF => "Auto".to_string(),
                    0xFE => "Closed".to_string(),
                    v => format!("{:.1}", 2f64.powf((v & 0x3f) as f64 / 8.0)),
                };
                result.aperture_setting = Some(aperture);

                let gain_raw = val4[1] & 0x0f;
                let gain_val = (gain_raw as i32 - 1) * 3;
                result.gain = if gain_val == 42 {
                    Some("Out of range".to_string())
                } else {
                    Some(format!("{} dB", gain_val))
                };
            }
            0x71 => {
                // Camera2: byte 1 = ImageStabilization
                let is_raw = val4[1];
                let is_str = match is_raw {
                    0x00 => "Off".to_string(),
                    0x3F => "On (0x3f)".to_string(),
                    0xBF => "Off (0xbf)".to_string(),
                    0xFF => "n/a".to_string(),
                    v => {
                        let state = if v & 0x10 != 0 { "On" } else { "Off" };
                        format!("{} (0x{:02x})", state, v)
                    }
                };
                result.image_stabilization = Some(is_str);
            }
            0x7F => {
                // Shutter: int16u little-endian, tag 1.1 mask 0x7fff = ExposureTime
                let val_le = u16::from_le_bytes([val4[0], val4[1]]);
                let val_le2 = u16::from_le_bytes([val4[2], val4[3]]);
                let shutter_raw = val_le2 & 0x7fff;
                let _ = val_le; // word 0 unused
                if shutter_raw != 0x7fff {
                    let exp_f = shutter_raw as f64 / 28125.0;
                    // Format as fraction using ExifTool::Exif::PrintExposureTime logic
                    let et_str = m2ts_format_exposure_time(exp_f);
                    result.exposure_time = Some(et_str.clone());
                    result.shutter_speed = Some(et_str);
                }
            }
            0xE0 => {
                // MakeModel: int16u[0] = Make code
                let make_code = u16::from_be_bytes([val4[0], val4[1]]);
                let make_str = match make_code {
                    0x0103 => "Panasonic",
                    0x0108 => "Sony",
                    0x1011 => "Canon",
                    0x1104 => "JVC",
                    _ => "Unknown",
                };
                result.make = Some(make_str.to_string());
            }
            0xE1 => {
                // RecInfo (Canon): int8u[0] = RecordingMode
                let rec_mode = val4[0];
                let mode_str = match rec_mode {
                    0x02 => "XP+",
                    0x04 => "SP",
                    0x05 => "LP",
                    0x06 => "FXP",
                    0x07 => "MXP",
                    _ => "Unknown",
                };
                result.recording_mode = Some(mode_str.to_string());
            }
            _ => {}
        }
    }

    if result.datetime_original.is_some() || result.aperture_setting.is_some()
        || result.gain.is_some() || result.make.is_some() {
        Some(result)
    } else {
        None
    }
}

/// Format exposure time like ExifTool's PrintExposureTime
fn m2ts_format_exposure_time(val: f64) -> String {
    if val <= 0.0 { return "0".to_string(); }
    if val >= 1.0 {
        if (val - val.round()).abs() < 0.005 {
            return format!("{}", val.round() as i64);
        }
        return format!("{:.1}", val);
    }
    // Express as fraction 1/N
    let n = (1.0 / val).round() as i64;
    if n > 0 { format!("1/{}", n) } else { format!("{}", val) }
}

fn m2ts_find_packet_size(data: &[u8]) -> Option<(usize, usize)> {
    for &(pkt, tco) in &[(192usize, 4usize), (188, 0)] {
        if data.len() >= pkt * 3 && (0..3).all(|i| data[i * pkt + tco] == 0x47) {
            return Some((pkt, tco));
        }
    }
    None
}

fn m2ts_get_payload(pkt: &[u8], tco: usize) -> Option<(bool, u16, &[u8])> {
    if pkt.len() < tco + 4 { return None; }
    let hdr = &pkt[tco..];
    if hdr[0] != 0x47 { return None; }
    let pusi = (hdr[1] & 0x40) != 0;
    let pid = (((hdr[1] & 0x1F) as u16) << 8) | hdr[2] as u16;
    let afc = (hdr[3] >> 4) & 0x3;
    if afc == 0 || afc == 2 { return None; }
    let mut ps = 4;
    if afc == 3 {
        if hdr.len() <= ps { return None; }
        ps += 1 + hdr[ps] as usize;
    }
    if ps >= hdr.len() { return None; }
    Some((pusi, pid, &hdr[ps..]))
}

fn m2ts_parse_pat(section: &[u8]) -> Vec<u16> {
    let mut pmt_pids = Vec::new();
    if section.len() < 8 { return pmt_pids; }
    let section_length = (((section[1] & 0x0F) as usize) << 8) | section[2] as usize;
    let entries_end = (3 + section_length).saturating_sub(4).min(section.len());
    let mut i = 8;
    while i + 4 <= entries_end {
        let prog_num = ((section[i] as u16) << 8) | section[i+1] as u16;
        let pmt_pid = (((section[i+2] & 0x1F) as u16) << 8) | section[i+3] as u16;
        if prog_num != 0 { pmt_pids.push(pmt_pid); }
        i += 4;
    }
    pmt_pids
}

struct M2tsStreamInfo {
    video_type: Option<String>,
    audio_type: Option<String>,
    audio_bitrate_idx: Option<u8>,
    audio_surround_mode: Option<u8>,
    audio_channels: Option<u8>,
    h264_pid: Option<u16>,
    audio_pid: Option<u16>,
}

fn m2ts_parse_pmt(section: &[u8]) -> Option<M2tsStreamInfo> {
    if section.len() < 12 || section[0] != 0x02 { return None; }
    let section_length = (((section[1] & 0x0F) as usize) << 8) | section[2] as usize;
    let section_end = (3 + section_length).saturating_sub(4).min(section.len());
    let prog_info_len = (((section[10] & 0x0F) as usize) << 8) | section[11] as usize;
    let mut es_pos = 12 + prog_info_len;
    if es_pos >= section_end { return None; }

    let mut info = M2tsStreamInfo {
        video_type: None, audio_type: None,
        audio_bitrate_idx: None, audio_surround_mode: None, audio_channels: None,
        h264_pid: None, audio_pid: None,
    };

    while es_pos + 5 <= section_end {
        let stream_type = section[es_pos];
        let es_pid = (((section[es_pos+1] & 0x1F) as u16) << 8) | section[es_pos+2] as u16;
        let es_info_len = (((section[es_pos+3] & 0x0F) as usize) << 8) | section[es_pos+4] as usize;
        let es_info_end = (es_pos + 5 + es_info_len).min(section_end);

        match stream_type {
            0x01 | 0x02 if info.video_type.is_none() => {
                info.video_type = Some(m2ts_stream_type_name(stream_type).to_string());
            }
            0x10 if info.video_type.is_none() => {
                info.video_type = Some(m2ts_stream_type_name(stream_type).to_string());
            }
            0x1b if info.video_type.is_none() => {
                info.video_type = Some("H.264 (AVC) Video".to_string());
                info.h264_pid = Some(es_pid);
            }
            0x24 if info.video_type.is_none() => {
                info.video_type = Some(m2ts_stream_type_name(stream_type).to_string());
            }
            0x03 | 0x04 if info.audio_type.is_none() => {
                info.audio_type = Some(m2ts_stream_type_name(stream_type).to_string());
                info.audio_pid = Some(es_pid);
            }
            0x0f if info.audio_type.is_none() => {
                info.audio_type = Some(m2ts_stream_type_name(stream_type).to_string());
                info.audio_pid = Some(es_pid);
            }
            0x81 if info.audio_type.is_none() => {
                info.audio_type = Some("A52/AC-3 Audio".to_string());
                info.audio_pid = Some(es_pid);
                // Parse AC3 audio descriptor from ES info
                let mut di = es_pos + 5;
                while di + 2 <= es_info_end {
                    let dtag = section[di];
                    let dlen = section[di+1] as usize;
                    if di + 2 + dlen > es_info_end { break; }
                    if dtag == 0x81 && dlen >= 3 {
                        // AC3 audio descriptor per ATSC A/52
                        let d0 = section[di+2];
                        let d1 = section[di+3];
                        let d2 = section[di+4];
                        info.audio_bitrate_idx = Some(d1 >> 2);
                        info.audio_surround_mode = Some(d1 & 0x03);
                        info.audio_channels = Some((d2 >> 1) & 0x0f);
                        let _ = d0; // sample_rate_idx from d0 >> 5 not used here
                    }
                    di += 2 + dlen;
                }
            }
            _ => {}
        }

        es_pos = es_info_end;
    }

    if info.video_type.is_some() || info.audio_type.is_some() {
        Some(info)
    } else {
        None
    }
}

fn m2ts_parse_sps(sps_nal: &[u8]) -> Option<(u32, u32)> {
    // Remove emulation prevention bytes
    let mut rbsp = Vec::with_capacity(sps_nal.len());
    let mut i = 0;
    while i < sps_nal.len() {
        if i + 2 < sps_nal.len() && sps_nal[i] == 0 && sps_nal[i+1] == 0 && sps_nal[i+2] == 3 {
            rbsp.push(0); rbsp.push(0); i += 3;
        } else {
            rbsp.push(sps_nal[i]); i += 1;
        }
    }

    let mut br = M2tsBitReader::new(&rbsp);
    br.skip_bits(8); // nal_unit_type byte
    let profile_idc = br.read_bits(8)?;
    br.skip_bits(16); // constraint_flags + level_idc
    br.read_ue()?; // seq_parameter_set_id

    if matches!(profile_idc, 100|110|122|244|44|83|86|118|128) {
        let chroma = br.read_ue()?;
        if chroma == 3 { br.skip_bits(1); }
        br.read_ue()?; br.read_ue()?; br.skip_bits(1);
        let scaling = br.read_bit()?;
        if scaling != 0 {
            let count = if chroma != 3 { 8 } else { 12 };
            for ci in 0..count {
                if br.read_bit()? != 0 {
                    let sz = if ci < 6 { 16 } else { 64 };
                    let (mut last, mut next) = (8i32, 8i32);
                    for _ in 0..sz {
                        if next != 0 { let d = br.read_se()?; next = (last + d + 256) % 256; }
                        last = if next == 0 { last } else { next };
                    }
                }
            }
        }
    }

    br.read_ue()?; // log2_max_frame_num_minus4
    let poc_type = br.read_ue()?;
    if poc_type == 0 { br.read_ue()?; }
    else if poc_type == 1 {
        br.skip_bits(1); br.read_se()?; br.read_se()?;
        let n = br.read_ue()?;
        for _ in 0..n { br.read_se()?; }
    }
    br.read_ue()?; br.skip_bits(1);

    let pic_w = br.read_ue()?;
    let pic_h = br.read_ue()?;
    let frame_mbs_only = br.read_bit()?;
    if frame_mbs_only == 0 { br.skip_bits(1); }
    br.skip_bits(1);

    let crop = br.read_bit()?;
    let (cl, cr, ct, cb) = if crop != 0 {
        (br.read_ue()?, br.read_ue()?, br.read_ue()?, br.read_ue()?)
    } else { (0, 0, 0, 0) };

    // Crop multiplier: 4 for width, (4 - frame_mbs_only*2) for height (Perl H264.pm)
    let m = 4 - frame_mbs_only * 2;
    let w = (pic_w + 1) * 16 - 4 * cl - 4 * cr;
    let h = ((pic_h + 1) * (2 - frame_mbs_only)) * 16 - m * ct - m * cb;
    // Validity check matching ExifTool H264.pm
    if w >= 160 && w <= 4096 && h >= 120 && h <= 3072 {
        Some((w, h))
    } else {
        None
    }
}

/// Returns (Option<(width,height)>, Option<MdpmData>) by scanning NAL units in payload.
fn m2ts_parse_h264_pes(payload: &[u8]) -> (Option<(u32, u32)>, Option<M2tsMdpmData>) {
    let mut dims = None;
    let mut mdpm = None;
    let mut i = 0;
    while i + 3 <= payload.len() {
        let nal_start = if payload[i] == 0 && payload[i+1] == 0 && i + 3 < payload.len() && payload[i+2] == 1 {
            i + 3
        } else if i + 4 < payload.len() && payload[i] == 0 && payload[i+1] == 0 && payload[i+2] == 0 && payload[i+3] == 1 {
            i + 4
        } else {
            i += 1; continue;
        };
        if nal_start >= payload.len() { break; }
        let nal_type = payload[nal_start] & 0x1F;
        match nal_type {
            7 if dims.is_none() => {
                dims = m2ts_parse_sps(&payload[nal_start..]);
            }
            6 if mdpm.is_none() => {
                mdpm = m2ts_parse_sei(&payload[nal_start..]);
            }
            _ => {}
        }
        i = nal_start + 1;
    }
    (dims, mdpm)
}

fn m2ts_parse_ac3_sample_rate(payload: &[u8]) -> Option<u32> {
    // Scan for 0x0B77 sync word and read fscod
    let pos = payload.windows(2).position(|w| w == [0x0B, 0x77])?;
    if pos + 5 > payload.len() { return None; }
    let fscod = payload[pos + 4] >> 6;
    let rates = [48000u32, 44100, 32000, 0];
    Some(rates.get(fscod as usize).copied().unwrap_or(0))
}

fn m2ts_stream_type_name(st: u8) -> &'static str {
    match st {
        0x01 => "MPEG1Video",
        0x02 => "MPEG2Video",
        0x03 => "MPEG1Audio",
        0x04 => "MPEG2Audio",
        0x0f => "ADTS AAC",
        0x10 => "MPEG4Video",
        0x1b => "H.264 (AVC) Video",
        0x24 => "HEVC Video",
        0x81 => "A52/AC-3 Audio",
        0x82 => "DTS Audio",
        _ => "Unknown",
    }
}

fn m2ts_format_bitrate(kbps: u32) -> String {
    format!("{} kbps", kbps)
}

fn m2ts_format_duration(first: u64, last: u64) -> String {
    if last <= first { return "0 s".to_string(); }
    let ticks = last - first;
    let total_secs = ticks / 27_000_000;
    if total_secs == 0 {
        return "0 s".to_string();
    }
    let h = total_secs / 3600;
    let m = (total_secs % 3600) / 60;
    let s = total_secs % 60;
    if h > 0 {
        format!("{}:{:02}:{:02}", h, m, s)
    } else {
        format!("{}:{:02}", m, s)
    }
}

pub fn read_m2ts(data: &[u8], extract_embedded: u8) -> Result<Vec<Tag>> {
    if data.is_empty() {
        return Err(Error::InvalidData("empty file".into()));
    }

    let (packet_size, tco) = m2ts_find_packet_size(data)
        .ok_or_else(|| Error::InvalidData("not an MPEG-2 TS file".into()))?;

    let mut tags = Vec::new();
    let num_packets = data.len() / packet_size;
    // With -ee, scan all packets; without, scan first 2000 only
    let scan_count = if extract_embedded > 0 { num_packets } else { num_packets.min(2000) };

    let mut pmt_pids: Vec<u16> = Vec::new();
    let mut pmt_buf: std::collections::HashMap<u16, Vec<u8>> = std::collections::HashMap::new();
    let mut pat_done = false;
    let mut stream_info: Option<M2tsStreamInfo> = None;
    let mut h264_dims: Option<(u32, u32)> = None;
    let mut mdpm_data: Option<M2tsMdpmData> = None;
    let mut all_mdpm: Vec<M2tsMdpmData> = Vec::new();
    let mut ac3_sample_rate: Option<u32> = None;
    let mut pcr_first: Option<u64> = None;
    let mut pcr_last: Option<u64> = None;

    for pkt_idx in 0..scan_count {
        let pkt = &data[pkt_idx * packet_size..(pkt_idx+1) * packet_size];

        // Extract PCR from adaptation field (AFC=2 or AFC=3)
        let hdr = &pkt[tco..];
        if hdr.len() >= 12 && hdr[0] == 0x47 {
            let afc = (hdr[3] >> 4) & 0x3;
            if (afc == 2 || afc == 3) && hdr.len() > 5 {
                let af_len = hdr[4] as usize;
                if af_len >= 7 && hdr.len() >= 12 {
                    let af_flags = hdr[5];
                    if af_flags & 0x10 != 0 {
                        let pb = ((hdr[6] as u64) << 25) | ((hdr[7] as u64) << 17)
                            | ((hdr[8] as u64) << 9) | ((hdr[9] as u64) << 1)
                            | ((hdr[10] as u64) >> 7);
                        let pe = (((hdr[10] as u64) & 1) << 8) | hdr[11] as u64;
                        let pcr = pb * 300 + pe;
                        if pcr_first.is_none() { pcr_first = Some(pcr); }
                        pcr_last = Some(pcr);
                    }
                }
            }
        }

        if let Some((pusi, pid, payload)) = m2ts_get_payload(pkt, tco) {
            if pid == 0x0000 && !pat_done {
                let section = if pusi && !payload.is_empty() {
                    let ptr = payload[0] as usize;
                    &payload[(ptr + 1).min(payload.len())..]
                } else { payload };
                let new_pmts = m2ts_parse_pat(section);
                if !new_pmts.is_empty() {
                    pmt_pids = new_pmts;
                    pat_done = true;
                }
            } else if stream_info.is_none() && pmt_pids.contains(&pid) {
                let buf = pmt_buf.entry(pid).or_default();
                if pusi {
                    buf.clear();
                    let ptr = if !payload.is_empty() { payload[0] as usize } else { 0 };
                    buf.extend_from_slice(&payload[(ptr + 1).min(payload.len())..]);
                } else {
                    buf.extend_from_slice(payload);
                }
                let buf_clone = buf.clone();
                if let Some(si) = m2ts_parse_pmt(&buf_clone) {
                    stream_info = Some(si);
                }
            } else if let Some(ref si) = stream_info {
                let need_first = h264_dims.is_none() || mdpm_data.is_none();
                if (need_first || extract_embedded > 0) && Some(pid) == si.h264_pid {
                    // Skip PES header to get to ES data
                    let es = m2ts_skip_pes_header(payload);
                    let (dims, mdpm) = m2ts_parse_h264_pes(es);
                    if dims.is_some() && h264_dims.is_none() { h264_dims = dims; }
                    if let Some(ref m) = mdpm {
                        if mdpm_data.is_none() { mdpm_data = mdpm.clone(); }
                        if extract_embedded > 0 { all_mdpm.push(m.clone()); }
                    }
                }
                if ac3_sample_rate.is_none() && Some(pid) == si.audio_pid {
                    let es = m2ts_skip_pes_header(payload);
                    if let Some(sr) = m2ts_parse_ac3_sample_rate(es) {
                        if sr > 0 { ac3_sample_rate = Some(sr); }
                    }
                }
            }
        }
    }

    // Also scan last packets for PCR (duration)
    if num_packets > scan_count {
        for pkt_idx in (num_packets - 500).max(scan_count)..num_packets {
            let pkt = &data[pkt_idx * packet_size..(pkt_idx+1) * packet_size];
            let hdr = &pkt[tco..];
            if hdr.len() >= 12 && hdr[0] == 0x47 {
                let afc = (hdr[3] >> 4) & 0x3;
                if (afc == 2 || afc == 3) && hdr.len() > 5 {
                    let af_len = hdr[4] as usize;
                    if af_len >= 7 {
                        let af_flags = hdr[5];
                        if af_flags & 0x10 != 0 && hdr.len() >= 12 {
                            let pb = ((hdr[6] as u64) << 25) | ((hdr[7] as u64) << 17)
                                | ((hdr[8] as u64) << 9) | ((hdr[9] as u64) << 1)
                                | ((hdr[10] as u64) >> 7);
                            let pe = (((hdr[10] as u64) & 1) << 8) | hdr[11] as u64;
                            pcr_last = Some(pb * 300 + pe);
                        }
                    }
                }
            }
        }
    }

    // Emit tags
    if let Some(ref si) = stream_info {
        if let Some(ref vt) = si.video_type {
            tags.push(mktag("M2TS", "VideoStreamType", "Video Stream Type", Value::String(vt.clone())));
        }
        if let Some(ref at) = si.audio_type {
            tags.push(mktag("M2TS", "AudioStreamType", "Audio Stream Type", Value::String(at.clone())));
        }

        // AC3 audio descriptor info
        if si.audio_bitrate_idx.is_some() || si.audio_surround_mode.is_some() || si.audio_channels.is_some() {
            let bitrates = [
                32u32,40,48,56,64,80,96,112,128,160,192,224,256,320,384,448,512,576,640
            ];
            if let Some(bi) = si.audio_bitrate_idx {
                let idx = bi as usize;
                if idx < bitrates.len() {
                    tags.push(mktag("M2TS", "AudioBitrate", "Audio Bitrate",
                        Value::String(m2ts_format_bitrate(bitrates[idx]))));
                }
            }
            if let Some(sm) = si.audio_surround_mode {
                let s = match sm {
                    0 => "Not indicated",
                    1 => "Not Dolby surround",
                    2 => "Dolby surround",
                    _ => "Reserved",
                };
                tags.push(mktag("M2TS", "SurroundMode", "Surround Mode", Value::String(s.into())));
            }
            if let Some(ch) = si.audio_channels {
                let cs = match ch {
                    0 => "1 + 1",
                    1 => "1",
                    2 => "2",
                    3 => "3",
                    4 => "2/1",
                    5 => "3/1",
                    6 => "2/2",
                    7 => "3/2",
                    _ => "Unknown",
                };
                tags.push(mktag("M2TS", "AudioChannels", "Audio Channels", Value::String(cs.into())));
            }
        }
    }

    if let Some((w, h)) = h264_dims {
        tags.push(mktag("M2TS", "ImageWidth", "Image Width", Value::U32(w)));
        tags.push(mktag("M2TS", "ImageHeight", "Image Height", Value::U32(h)));
    }

    if let Some(sr) = ac3_sample_rate {
        tags.push(mktag("M2TS", "AudioSampleRate", "Audio Sample Rate", Value::U32(sr)));
    }

    // Duration
    if let (Some(first), Some(last)) = (pcr_first, pcr_last) {
        let dur = m2ts_format_duration(first, last);
        tags.push(mktag("M2TS", "Duration", "Duration", Value::String(dur)));
    }

    // MDPM camera metadata from H.264 SEI
    if let Some(ref mdpm) = mdpm_data {
        if let Some(ref v) = mdpm.make {
            tags.push(mktag("H264", "Make", "Make", Value::String(v.clone())));
        }
        if let Some(ref v) = mdpm.datetime_original {
            tags.push(mktag("H264", "DateTimeOriginal", "Date/Time Original", Value::String(v.clone())));
        }
        if let Some(ref v) = mdpm.aperture_setting {
            tags.push(mktag("H264", "ApertureSetting", "Aperture Setting", Value::String(v.clone())));
        }
        if let Some(ref v) = mdpm.gain {
            tags.push(mktag("H264", "Gain", "Gain", Value::String(v.clone())));
        }
        if let Some(ref v) = mdpm.image_stabilization {
            tags.push(mktag("H264", "ImageStabilization", "Image Stabilization", Value::String(v.clone())));
        }
        if let Some(ref v) = mdpm.exposure_time {
            tags.push(mktag("H264", "ExposureTime", "Exposure Time", Value::String(v.clone())));
        }
        if let Some(ref v) = mdpm.shutter_speed {
            tags.push(mktag("H264", "ShutterSpeed", "Shutter Speed", Value::String(v.clone())));
        }
        if let Some(ref v) = mdpm.recording_mode {
            tags.push(mktag("H264", "RecordingMode", "Recording Mode", Value::String(v.clone())));
        }
        // ExifTool emits Warning only when -ee is NOT used
        if extract_embedded == 0 {
            tags.push(mktag("M2TS", "Warning", "Warning",
                Value::String("[minor] The ExtractEmbedded option may find more tags in the video data".to_string())));
        }
    }

    // With -ee: emit tags from ALL MDPM frames (per-frame metadata)
    // Skip first frame (already emitted above from mdpm_data)
    if extract_embedded > 0 && all_mdpm.len() > 1 {
        for mdpm in &all_mdpm[1..] {
            if let Some(ref v) = mdpm.datetime_original {
                tags.push(mktag("H264", "DateTimeOriginal", "Date/Time Original", Value::String(v.clone())));
            }
            if let Some(ref v) = mdpm.make {
                tags.push(mktag("H264", "Make", "Make", Value::String(v.clone())));
            }
        }
    }

    Ok(tags)
}

fn m2ts_skip_pes_header(payload: &[u8]) -> &[u8] {
    // PES header: 00 00 01 stream_id [2 bytes length] [variable header]
    if payload.len() < 9 || payload[0] != 0x00 || payload[1] != 0x00 || payload[2] != 0x01 {
        return payload;
    }
    let stream_id = payload[3];
    // Private stream IDs don't have standard PES header extension
    if stream_id == 0xBC || stream_id == 0xBE || stream_id == 0xBF
        || stream_id == 0xF0 || stream_id == 0xF1 || stream_id == 0xFF
        || stream_id == 0xF2 || stream_id == 0xF8 {
        return &payload[6..];
    }
    if payload.len() < 9 { return payload; }
    let header_data_length = payload[8] as usize;
    let es_start = 9 + header_data_length;
    if es_start <= payload.len() { &payload[es_start..] } else { payload }
}

// ============================================================================
// GZIP
// ============================================================================

pub fn read_gzip(data: &[u8]) -> Result<Vec<Tag>> {
    // RFC 1952: magic=1F 8B, method=08 (deflate)
    if data.len() < 10 || data[0] != 0x1F || data[1] != 0x8B || data[2] != 0x08 {
        return Err(Error::InvalidData("not a GZIP file".into()));
    }

    let mut tags = Vec::new();
    let method = data[2];
    let flags = data[3];
    let xflags = data[8];
    let os_byte = data[9];

    // Compression (byte 2)
    let compress_str = if method == 8 { "Deflated" } else { "Unknown" };
    tags.push(mktag("GZIP", "Compression", "Compression", Value::String(compress_str.into())));

    // Flags (byte 3) — bitmask
    let flag_names = [(0, "Text"), (1, "CRC16"), (2, "ExtraFields"), (3, "FileName"), (4, "Comment")];
    let mut flag_parts: Vec<&str> = Vec::new();
    for (bit, name) in &flag_names {
        if flags & (1 << bit) != 0 {
            flag_parts.push(name);
        }
    }
    let flags_str = if flag_parts.is_empty() {
        "(none)".to_string()
    } else {
        flag_parts.join(", ")
    };
    tags.push(mktag("GZIP", "Flags", "Flags", Value::String(flags_str)));

    // ModifyDate (bytes 4-7, Unix timestamp, local time)
    let mtime = u32::from_le_bytes([data[4], data[5], data[6], data[7]]);
    if mtime > 0 {
        let dt = gzip_unix_to_datetime(mtime as i64);
        tags.push(mktag("GZIP", "ModifyDate", "Modify Date", Value::String(dt)));
    }

    // ExtraFlags (byte 8)
    let extra_flags_str = match xflags {
        0 => "(none)".to_string(),
        2 => "Maximum Compression".to_string(),
        4 => "Fastest Algorithm".to_string(),
        _ => format!("{}", xflags),
    };
    tags.push(mktag("GZIP", "ExtraFlags", "Extra Flags", Value::String(extra_flags_str)));

    // OperatingSystem (byte 9)
    let os_str = match os_byte {
        0 => "FAT filesystem (MS-DOS, OS/2, NT/Win32)",
        1 => "Amiga",
        2 => "VMS (or OpenVMS)",
        3 => "Unix",
        4 => "VM/CMS",
        5 => "Atari TOS",
        6 => "HPFS filesystem (OS/2, NT)",
        7 => "Macintosh",
        8 => "Z-System",
        9 => "CP/M",
        10 => "TOPS-20",
        11 => "NTFS filesystem (NT)",
        12 => "QDOS",
        13 => "Acorn RISCOS",
        255 => "unknown",
        _ => "Other",
    };
    tags.push(mktag("GZIP", "OperatingSystem", "Operating System", Value::String(os_str.into())));

    // Extract file name and comment if flag bits set
    let mut pos = 10usize;
    if flags & 0x18 != 0 {
        // Skip FEXTRA (bit 2) if present
        if flags & 0x04 != 0 && pos + 2 <= data.len() {
            let xlen = u16::from_le_bytes([data[pos], data[pos + 1]]) as usize;
            pos += 2 + xlen;
        }

        // ArchivedFileName (bit 3)
        if flags & 0x08 != 0 && pos < data.len() {
            let name_end = data[pos..].iter().position(|&b| b == 0)
                .unwrap_or(data.len() - pos);
            let filename = String::from_utf8_lossy(&data[pos..pos + name_end]).to_string();
            if !filename.is_empty() {
                tags.push(mktag("GZIP", "ArchivedFileName", "Archived File Name", Value::String(filename)));
            }
            pos += name_end + 1;
        }

        // Comment (bit 4)
        if flags & 0x10 != 0 && pos < data.len() {
            let comment_end = data[pos..].iter().position(|&b| b == 0)
                .unwrap_or(data.len() - pos);
            let comment = String::from_utf8_lossy(&data[pos..pos + comment_end]).to_string();
            if !comment.is_empty() {
                tags.push(mktag("GZIP", "Comment", "Comment", Value::String(comment)));
            }
        }
    } else {
        // No FEXTRA flag, but FNAME might still be set
        if flags & 0x04 != 0 && pos + 2 <= data.len() {
            let xlen = u16::from_le_bytes([data[pos], data[pos + 1]]) as usize;
            pos += 2 + xlen;
        }
        if flags & 0x08 != 0 && pos < data.len() {
            let name_end = data[pos..].iter().position(|&b| b == 0)
                .unwrap_or(data.len() - pos);
            let filename = String::from_utf8_lossy(&data[pos..pos + name_end]).to_string();
            if !filename.is_empty() {
                tags.push(mktag("GZIP", "ArchivedFileName", "Archived File Name", Value::String(filename)));
            }
        }
    }

    Ok(tags)
}

/// Convert Unix timestamp to "YYYY:MM:DD HH:MM:SS+HH:00" (local time).
/// Mirrors Perl's ConvertUnixTime($val, 1).
fn gzip_unix_to_datetime(secs: i64) -> String {
    // Get timezone offset from system (DST-aware for the specific timestamp)
    let tz_offset = get_local_tz_offset_for_timestamp(secs);
    let local_secs = secs + tz_offset;
    let days = local_secs / 86400;
    let time = local_secs % 86400;
    let (time, days) = if time < 0 { (time + 86400, days - 1) } else { (time, days) };
    let h = time / 3600;
    let m = (time % 3600) / 60;
    let s = time % 60;
    let mut y = 1970i32;
    let mut rem = days;
    loop {
        let dy: i64 = if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 { 366 } else { 365 };
        if rem < dy { break; }
        rem -= dy;
        y += 1;
    }
    let leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
    let months: [i64; 12] = [31, if leap { 29 } else { 28 }, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    let mut mo = 1;
    for &dm in &months {
        if rem < dm { break; }
        rem -= dm;
        mo += 1;
    }
    let tz_h = tz_offset / 3600;
    let tz_m = (tz_offset.abs() % 3600) / 60;
    let tz_sign = if tz_offset >= 0 { "+" } else { "-" };
    format!("{:04}:{:02}:{:02} {:02}:{:02}:{:02}{}{:02}:{:02}",
        y, mo, rem + 1, h, m, s, tz_sign, tz_h.abs(), tz_m)
}


/// Get local timezone offset in seconds for a specific Unix timestamp (DST-aware).
/// Uses libc's localtime_r via raw syscall to account for DST.
fn get_local_tz_offset_for_timestamp(ts: i64) -> i64 {
    #[cfg(target_os = "linux")]
    {
        // Use libc localtime via /proc/self/fd - actually let's use libc directly
        // since the binary is on Linux we can use the C library through FFI
        use std::mem;
        extern "C" {
            fn localtime_r(timep: *const LibcTimeT, result: *mut TmStruct) -> *mut TmStruct;
        }
        type LibcTimeT = i64;
        #[repr(C)]
        struct TmStruct {
            tm_sec: i32, tm_min: i32, tm_hour: i32, tm_mday: i32,
            tm_mon: i32, tm_year: i32, tm_wday: i32, tm_yday: i32,
            tm_isdst: i32, tm_gmtoff: i64, tm_zone: *const i8,
        }
        unsafe {
            let mut tm: TmStruct = mem::zeroed();
            let t = ts;
            if !localtime_r(&t, &mut tm).is_null() {
                return tm.tm_gmtoff;
            }
        }
    }
    // Fallback: try to detect from /etc/timezone
    if let Ok(tz) = std::fs::read_to_string("/etc/timezone") {
        let tz = tz.trim();
        if tz == "UTC" || tz == "UTC0" { return 0; }
    }
    if let Ok(link) = std::fs::read_link("/etc/localtime") {
        let path = link.to_string_lossy();
        if path.contains("UTC") || path.ends_with("/UTC") { return 0; }
        if path.contains("Europe/") || path.contains("/CET") { return 3600; }
        if path.contains("America/New_York") { return -5 * 3600; }
        if path.contains("America/Los_Angeles") { return -8 * 3600; }
        if path.contains("America/Chicago") { return -6 * 3600; }
        if path.contains("Asia/Tokyo") { return 9 * 3600; }
    }
    0
}

// ============================================================================
// MacOS XAttr (._) sidecar file
// ============================================================================

/// Parse MacOS AppleDouble sidecar (._) files containing XAttr data.
/// Mirrors ExifTool's MacOS.pm ProcessMacOS and ProcessATTR.
pub fn read_macos(data: &[u8]) -> Result<Vec<Tag>> {
    // Check header: \0\x05\x16\x07\0\x02\0\0Mac OS X
    if data.len() < 26 || data[0] != 0x00 || data[1] != 0x05 || data[2] != 0x16 || data[3] != 0x07 {
        return Err(Error::InvalidData("not a MacOS sidecar file".into()));
    }
    let ver = data[5];
    if ver != 2 {
        return Ok(Vec::new());
    }

    let entries = u16::from_be_bytes([data[24], data[25]]) as usize;
    if 26 + entries * 12 > data.len() {
        return Ok(Vec::new());
    }

    let mut tags = Vec::new();

    for i in 0..entries {
        let pos = 26 + i * 12;
        let tag_id = u32::from_be_bytes([data[pos], data[pos+1], data[pos+2], data[pos+3]]);
        let off = u32::from_be_bytes([data[pos+4], data[pos+5], data[pos+6], data[pos+7]]) as usize;
        let len = u32::from_be_bytes([data[pos+8], data[pos+9], data[pos+10], data[pos+11]]) as usize;

        if tag_id == 9 && off + len <= data.len() {
            // ATTR block
            let entry_data = &data[off..off + len];
            parse_attr_block(data, entry_data, &mut tags);
        }
    }

    Ok(tags)
}

/// Parse an ATTR (extended attributes) block from a MacOS sidecar file.
/// entry_data is the ATTR block, full_data is the entire file (for absolute offsets).
fn parse_attr_block(full_data: &[u8], entry_data: &[u8], tags: &mut Vec<Tag>) {
    if entry_data.len() < 70 {
        return;
    }
    // Check for ATTR signature at offset 34
    if &entry_data[34..38] != b"ATTR" {
        return;
    }

    let xattr_entries = u32::from_be_bytes([entry_data[66], entry_data[67], entry_data[68], entry_data[69]]) as usize;
    let mut pos = 70;

    for _i in 0..xattr_entries {
        if pos + 11 > entry_data.len() {
            break;
        }
        let off = u32::from_be_bytes([entry_data[pos], entry_data[pos+1], entry_data[pos+2], entry_data[pos+3]]) as usize;
        let len = u32::from_be_bytes([entry_data[pos+4], entry_data[pos+5], entry_data[pos+6], entry_data[pos+7]]) as usize;
        let n = entry_data[pos+10] as usize;

        if pos + 11 + n > entry_data.len() {
            break;
        }
        let name_bytes = &entry_data[pos+11..pos+11+n];
        let name = String::from_utf8_lossy(name_bytes).trim_end_matches('\0').to_string();

        // Offsets are absolute file offsets
        let val_data = if off + len <= full_data.len() {
            &full_data[off..off + len]
        } else {
            pos += ((11 + n + 3) & !3).max(1);
            continue;
        };

        // Convert xattr name to ExifTool tag name
        let tag_name = xattr_name_to_tag(&name);

        // Process value
        if val_data.starts_with(b"bplist0") {
            // Parse simple binary plist (arrays, strings, dates)
            if let Some(value) = parse_simple_bplist(val_data) {
                tags.push(mktag("MacOS", &tag_name, &tag_name, Value::String(value)));
            } else {
                // Just mark as binary
                tags.push(mktag("MacOS", &tag_name, &tag_name, Value::Binary(val_data.to_vec())));
            }
        } else if len > 100 || val_data.contains(&0u8) && !val_data.starts_with(b"0082") {
            // Binary data
            tags.push(mktag("MacOS", &tag_name, &tag_name, Value::Binary(val_data.to_vec())));
        } else {
            let s = String::from_utf8_lossy(val_data).trim_end_matches('\0').to_string();
            // Handle quarantine string: format "0082;TIME;APP;"
            let display = if name == "com.apple.quarantine" {
                format_quarantine(&s)
            } else {
                s
            };
            if !display.is_empty() {
                tags.push(mktag("MacOS", &tag_name, &tag_name, Value::String(display)));
            }
        }

        // Advance to next entry (aligned to 4 bytes)
        pos += ((11 + n + 3) & !3).max(4);
    }
}

/// Convert xattr attribute name to ExifTool tag name.
/// Mirrors Perl: com.apple.metadata:kMDItemXxx → XAttrMDItemXxx etc.
fn xattr_name_to_tag(name: &str) -> String {
    // Check known names first (from ExifTool's XAttr table)
    let known = match name {
        "com.apple.quarantine" => Some("XAttrQuarantine"),
        "com.apple.lastuseddate#PS" => Some("XAttrLastUsedDate"),
        "com.apple.metadata:kMDItemDownloadedDate" => Some("XAttrMDItemDownloadedDate"),
        "com.apple.metadata:kMDItemWhereFroms" => Some("XAttrMDItemWhereFroms"),
        "com.apple.metadata:kMDLabel" => Some("XAttrMDLabel"),
        "com.apple.metadata:kMDItemFinderComment" => Some("XAttrMDItemFinderComment"),
        "com.apple.metadata:_kMDItemUserTags" => Some("XAttrMDItemUserTags"),
        _ => None,
    };
    // For non-apple names: strip separators and capitalize words
    if name.starts_with("org.") || name.starts_with("net.") || (!name.starts_with("com.apple.") && name.contains(':')) {
        // Apply MakeTagName-style conversion
        let mut tag = String::from("XAttr");
        let mut cap_next = true;
        for c in name.chars() {
            if c == '.' || c == ':' || c == '_' || c == '-' {
                cap_next = true;
            } else if cap_next {
                for uc in c.to_uppercase() {
                    tag.push(uc);
                }
                cap_next = false;
            } else {
                tag.push(c);
            }
        }
        return tag;
    }
    if let Some(n) = known {
        return n.to_string();
    }

    // Remove random ID after kMDLabel_
    let name = if let Some(p) = name.find("kMDLabel_") {
        &name[..p + 8] // keep up to kMDLabel
    } else {
        name
    };

    // Apply Perl transformation
    let basename = if let Some(rest) = name.strip_prefix("com.apple.") {
        // s/^metadata:_?k//
        let rest = if let Some(r) = rest.strip_prefix("metadata:k") {
            r
        } else if let Some(r) = rest.strip_prefix("metadata:_k") {
            r
        } else if let Some(r) = rest.strip_prefix("metadata:") {
            r
        } else {
            rest
        };
        rest.to_string()
    } else {
        name.to_string()
    };

    // ucfirst then s/[.:_]([a-z])/\U$1/g
    let base_ucfirst = ucfirst_str_misc(&basename);
    let mut result = String::from("XAttr");

    let chars: Vec<char> = base_ucfirst.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        let c = chars[i];
        if (c == '.' || c == ':' || c == '_' || c == '#') && i + 1 < chars.len() && chars[i+1].is_ascii_lowercase() {
            result.push(chars[i+1].to_ascii_uppercase());
            i += 2;
        } else if c == '.' || c == ':' || c == '_' || c == '#' {
            i += 1; // skip separator with no following lowercase
        } else {
            result.push(c);
            i += 1;
        }
    }
    result
}

fn ucfirst_str_misc(s: &str) -> String {
    let mut c = s.chars();
    match c.next() {
        None => String::new(),
        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
    }
}

/// Format quarantine string to ExifTool format.
fn format_quarantine(s: &str) -> String {
    // Format: "FLAGS;HEX_TIME;APP;" or similar
    // ExifTool shows: "Flags=0082 set at 2020:11:12 12:27:26 by Safari"
    let parts: Vec<&str> = s.split(';').collect();
    if parts.len() >= 3 {
        let flags = parts[0];
        let time_hex = parts[1];
        let app = parts[2];

        // Try to parse time_hex as hex timestamp
        let time_str = if let Ok(ts) = i64::from_str_radix(time_hex, 16) {
            // Mac HFS+ time: seconds since 2001-01-01 or 1904-01-01
            // QuickTime epoch (2001) is used for Apple timestamps
            // Actually quarantine uses Unix epoch
            // Let's just show the raw value
            format!("(ts={})", ts)
        } else {
            time_hex.to_string()
        };

        if !app.is_empty() {
            return format!("Flags={} set at {} by {}", flags, time_str, app);
        }
        return format!("Flags={} set at {}", flags, time_str);
    }
    s.to_string()
}

/// Parse a simple binary plist to extract string, array of strings, or date values.
fn parse_simple_bplist(data: &[u8]) -> Option<String> {
    if data.len() < 32 || !data.starts_with(b"bplist00") {
        return None;
    }

    // Read trailer: last 32 bytes
    let trailer_start = data.len() - 32;
    let trailer = &data[trailer_start..];
    let offset_int_size = trailer[6] as usize;
    let obj_ref_size = trailer[7] as usize;
    let num_objects = u64::from_be_bytes([trailer[8], trailer[9], trailer[10], trailer[11],
                                          trailer[12], trailer[13], trailer[14], trailer[15]]) as usize;
    let top_object = u64::from_be_bytes([trailer[16], trailer[17], trailer[18], trailer[19],
                                         trailer[20], trailer[21], trailer[22], trailer[23]]) as usize;
    let offset_table_offset = u64::from_be_bytes([trailer[24], trailer[25], trailer[26], trailer[27],
                                                   trailer[28], trailer[29], trailer[30], trailer[31]]) as usize;

    if offset_int_size == 0 || offset_int_size > 8 || num_objects == 0 {
        return None;
    }

    let mut objects_offset = Vec::with_capacity(num_objects);
    for i in 0..num_objects {
        let ot_pos = offset_table_offset + i * offset_int_size;
        if ot_pos + offset_int_size > data.len() {
            return None;
        }
        let mut off: usize = 0;
        for j in 0..offset_int_size {
            off = (off << 8) | data[ot_pos + j] as usize;
        }
        objects_offset.push(off);
    }

    let read_object = |obj_idx: usize| -> Option<String> {
        let off = *objects_offset.get(obj_idx)?;
        if off >= data.len() {
            return None;
        }
        let marker = data[off];
        let type_nibble = (marker & 0xF0) >> 4;
        let info_nibble = marker & 0x0F;

        match type_nibble {
            0x5 => {
                // ASCII string
                let len = info_nibble as usize;
                if off + 1 + len > data.len() { return None; }
                Some(String::from_utf8_lossy(&data[off+1..off+1+len]).to_string())
            }
            0x6 => {
                // Unicode string (UTF-16BE)
                let len = info_nibble as usize;
                let byte_len = len * 2;
                if off + 1 + byte_len > data.len() { return None; }
                let chars: Vec<u16> = data[off+1..off+1+byte_len]
                    .chunks_exact(2)
                    .map(|c| u16::from_be_bytes([c[0], c[1]]))
                    .collect();
                String::from_utf16(&chars).ok()
            }
            0x3 => {
                // Date (64-bit float, seconds since 2001-01-01)
                if off + 9 > data.len() { return None; }
                let bits = u64::from_be_bytes([data[off+1], data[off+2], data[off+3], data[off+4],
                                               data[off+5], data[off+6], data[off+7], data[off+8]]);
                let secs = f64::from_bits(bits);
                // Convert from Apple epoch (2001-01-01) to Unix epoch (1970-01-01)
                let unix_secs = secs as i64 + 978307200;
                // Format as date string
                let days = unix_secs / 86400;
                let time = unix_secs % 86400;
                let hour = time / 3600;
                let min = (time % 3600) / 60;
                let sec = time % 60;
                let mut year = 1970i32;
                let mut rem_days = days;
                loop {
                    let dy = if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 { 366 } else { 365 };
                    if rem_days < dy { break; }
                    rem_days -= dy;
                    year += 1;
                }
                let leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
                let month_days = [31i64, if leap {29} else {28}, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
                let mut month = 1i32;
                for &md in &month_days {
                    if rem_days < md { break; }
                    rem_days -= md;
                    month += 1;
                }
                let day = rem_days + 1;
                Some(format!("{:04}:{:02}:{:02} {:02}:{:02}:{:02}", year, month, day, hour, min, sec))
            }
            0xA => {
                // Array: collect items
                let count = if info_nibble == 0xF {
                    // extended length
                    if off + 2 > data.len() { return None; }
                    let ext_marker = data[off+1];
                    (1 << (ext_marker & 0xF)) as usize
                } else {
                    info_nibble as usize
                };
                Some(format!("({} items)", count))
            }
            _ => None,
        }
    };

    // Get top object
    let result = read_object(top_object)?;

    // If it's an array, try to read its elements
    if let Some(off) = objects_offset.get(top_object) {
        let off = *off;
        if off < data.len() {
            let marker = data[off];
            let type_nibble = (marker & 0xF0) >> 4;
            if type_nibble == 0xA {
                // Array: read elements
                let count = (marker & 0x0F) as usize;
                let mut items = Vec::new();
                for j in 0..count {
                    let ref_pos = off + 1 + j * obj_ref_size;
                    if ref_pos + obj_ref_size > data.len() { break; }
                    let mut obj_ref: usize = 0;
                    for k in 0..obj_ref_size {
                        obj_ref = (obj_ref << 8) | data[ref_pos + k] as usize;
                    }
                    if let Some(item_val) = read_object(obj_ref) {
                        items.push(item_val);
                    }
                }
                if !items.is_empty() {
                    return Some(items.join(", "));
                }
            }
        }
    }

    Some(result)
}

// ============================================================================
// MOI (camcorder info file)
// ============================================================================

/// Parse MOI (camcorder info) files. Mirrors ExifTool's MOI.pm.
pub fn read_moi(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 256 || !data.starts_with(b"V6") {
        return Err(Error::InvalidData("not a MOI file".into()));
    }

    let mut tags = Vec::new();

    // 0x00: MOIVersion (string[2])
    let version = String::from_utf8_lossy(&data[0..2]).to_string();
    tags.push(mktag("MOI", "MOIVersion", "MOI Version", Value::String(version)));

    // 0x06: DateTimeOriginal (undef[8]) = unpack 'nCCCCn'
    // year(u16), month(u8), day(u8), hour(u8), min(u8), ms*1000(u16)
    if data.len() >= 14 {
        let year = u16::from_be_bytes([data[6], data[7]]);
        let month = data[8];
        let day = data[9];
        let hour = data[10];
        let min = data[11];
        let ms = u16::from_be_bytes([data[12], data[13]]);
        let sec_f = ms as f64 / 1000.0;
        let dt = format!("{:04}:{:02}:{:02} {:02}:{:02}:{:06.3}", year, month, day, hour, min, sec_f);
        tags.push(mktag("MOI", "DateTimeOriginal", "Date/Time Original", Value::String(dt)));
    }

    // 0x0e: Duration (int32u, ms)
    if data.len() >= 0x12 {
        let dur_ms = u32::from_be_bytes([data[0x0e], data[0x0f], data[0x10], data[0x11]]);
        let dur_s = dur_ms as f64 / 1000.0;
        let dur_str = format!("{:.2} s", dur_s);
        tags.push(mktag("MOI", "Duration", "Duration", Value::String(dur_str)));
    }

    // 0x80: AspectRatio (int8u)
    if data.len() > 0x80 {
        let aspect = data[0x80];
        let lo = aspect & 0x0F;
        let hi = aspect >> 4;
        let aspect_str = match lo {
            0 | 1 => "4:3",
            4 | 5 => "16:9",
            _ => "Unknown",
        };
        let sys_str = match hi {
            4 => " NTSC",
            5 => " PAL",
            _ => "",
        };
        let full = format!("{}{}", aspect_str, sys_str);
        tags.push(mktag("MOI", "AspectRatio", "Aspect Ratio", Value::String(full)));
    }

    // 0x84: AudioCodec (int16u)
    if data.len() > 0x86 {
        let ac = u16::from_be_bytes([data[0x84], data[0x85]]);
        let codec = match ac {
            0x00c1 => "AC3",
            0x4001 => "MPEG",
            _ => "Unknown",
        };
        tags.push(mktag("MOI", "AudioCodec", "Audio Codec", Value::String(codec.into())));
    }

    // 0x86: AudioBitrate (int8u, val * 16000 + 48000)
    if data.len() > 0x86 {
        let ab = data[0x86];
        let bitrate = ab as u32 * 16000 + 48000;
        let bitrate_str = format!("{} kbps", bitrate / 1000);
        tags.push(mktag("MOI", "AudioBitrate", "Audio Bitrate", Value::String(bitrate_str)));
    }

    // 0xda: VideoBitrate (int16u with lookup)
    if data.len() > 0xdc {
        let vb = u16::from_be_bytes([data[0xda], data[0xdb]]);
        let vbps: Option<u32> = match vb {
            0x5896 => Some(8500000),
            0x813d => Some(5500000),
            _ => None,
        };
        if let Some(bps) = vbps {
            let vb_str = format!("{:.1} Mbps", bps as f64 / 1_000_000.0);
            tags.push(mktag("MOI", "VideoBitrate", "Video Bitrate", Value::String(vb_str)));
        }
    }

    Ok(tags)
}

// ============================================================================
// RAR
// ============================================================================

/// Read a ULEB128 (unsigned LEB128) integer from data at pos, advancing pos.
fn read_uleb128(data: &[u8], pos: &mut usize) -> Option<u64> {
    let mut result: u64 = 0;
    let mut shift = 0u32;
    loop {
        if *pos >= data.len() {
            return None;
        }
        let byte = data[*pos];
        *pos += 1;
        result |= ((byte & 0x7F) as u64) << shift;
        if byte & 0x80 == 0 {
            break;
        }
        shift += 7;
        if shift >= 64 {
            return None;
        }
    }
    Some(result)
}

pub fn read_rar(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 8 || !data.starts_with(b"Rar!\x1A\x07") {
        return Err(Error::InvalidData("not a RAR file".into()));
    }

    let mut tags = Vec::new();

    if data[6] == 0x00 {
        // RAR v4
        tags.push(mktag("ZIP", "FileVersion", "File Version", Value::String("RAR v4".into())));
        read_rar4_entries(data, &mut tags);
    } else if data[6] == 0x01 && data[7] == 0x00 {
        // RAR v5
        tags.push(mktag("ZIP", "FileVersion", "File Version", Value::String("RAR v5".into())));
        read_rar5_entries(data, &mut tags);
    }

    Ok(tags)
}

fn read_rar5_entries(data: &[u8], tags: &mut Vec<Tag>) {
    // After 8-byte signature, iterate blocks:
    // each block: 4 bytes CRC32, then ULEB128 headSize, then headSize bytes header
    let mut pos = 8;

    loop {
        // skip 4-byte CRC
        if pos + 4 > data.len() {
            break;
        }
        pos += 4;

        let head_size = match read_uleb128(data, &mut pos) {
            Some(v) if v > 0 => v as usize,
            _ => break,
        };

        if pos + head_size > data.len() {
            break;
        }

        let header = &data[pos..pos + head_size];
        pos += head_size;

        let mut hpos = 0;
        let head_type = match read_uleb128(header, &mut hpos) {
            Some(v) => v,
            None => break,
        };

        // Skip headFlags
        let head_flag = match read_uleb128(header, &mut hpos) {
            Some(v) => v,
            None => break,
        };

        // head_type 2 = file header, 3 = service header
        if head_type != 2 && head_type != 3 {
            // Skip data section if present
            if head_flag & 0x0002 != 0 {
                // read extra data size to skip
                if let Some(data_size) = read_uleb128(data, &mut pos) {
                    pos += data_size as usize;
                }
            }
            continue;
        }

        // skip extraSize
        let _extra_size = read_uleb128(header, &mut hpos);

        let data_size: u64 = if head_flag & 0x0002 != 0 {
            match read_uleb128(header, &mut hpos) {
                Some(v) => v,
                None => break,
            }
        } else {
            0
        };

        if head_type == 3 {
            // service header - skip its data
            pos += data_size as usize;
            continue;
        }

        // File header
        if head_type == 2 {
            tags.push(mktag("ZIP", "CompressedSize", "Compressed Size", Value::U32(data_size as u32)));
        }

        let file_flag = match read_uleb128(header, &mut hpos) {
            Some(v) => v,
            None => { pos += data_size as usize; continue; }
        };
        let uncompressed_size = match read_uleb128(header, &mut hpos) {
            Some(v) => v,
            None => { pos += data_size as usize; continue; }
        };
        if file_flag & 0x0008 == 0 {
            tags.push(mktag("ZIP", "UncompressedSize", "Uncompressed Size", Value::U32(uncompressed_size as u32)));
        }

        // skip file attributes
        let _attrs = read_uleb128(header, &mut hpos);

        // optional mtime (4 bytes)
        if file_flag & 0x0002 != 0 {
            hpos += 4;
        }
        // optional CRC (4 bytes)
        if file_flag & 0x0004 != 0 {
            hpos += 4;
        }

        // skip compressionInfo
        let _comp_info = read_uleb128(header, &mut hpos);

        // OS
        if let Some(os_val) = read_uleb128(header, &mut hpos) {
            let os_name = match os_val {
                0 => "Win32",
                1 => "Unix",
                _ => "Unknown",
            };
            tags.push(mktag("ZIP", "OperatingSystem", "Operating System", Value::String(os_name.into())));
        }

        // filename: 1-byte length then name bytes
        if hpos < header.len() {
            let name_len = header[hpos] as usize;
            hpos += 1;
            if hpos + name_len <= header.len() {
                let name = String::from_utf8_lossy(&header[hpos..hpos + name_len])
                    .trim_end_matches('\0')
                    .to_string();
                if !name.is_empty() {
                    tags.push(mktag("ZIP", "ArchivedFileName", "Archived File Name", Value::String(name)));
                }
            }
        }

        pos += data_size as usize;
    }
}

fn read_rar4_entries(data: &[u8], tags: &mut Vec<Tag>) {
    // RAR v4: little-endian blocks after 7-byte signature
    let mut pos = 7;

    loop {
        if pos + 7 > data.len() {
            break;
        }
        // Block header: CRC(2) Type(1) Flags(2) Size(2)
        let block_type = data[pos + 2];
        let flags = u16::from_le_bytes([data[pos + 3], data[pos + 4]]);
        let mut size = u16::from_le_bytes([data[pos + 5], data[pos + 6]]) as usize;
        size = size.saturating_sub(7);

        if flags & 0x8000 != 0 {
            if pos + 11 > data.len() {
                break;
            }
            let add_size = u32::from_le_bytes([data[pos + 7], data[pos + 8], data[pos + 9], data[pos + 10]]) as usize;
            size = size.saturating_add(add_size).saturating_sub(4);
        }

        pos += 7;

        if block_type == 0x74 && size > 0 {
            // File block
            let n = size.min(4096).min(data.len() - pos);
            if n >= 16 {
                let file_data = &data[pos..pos + n];
                let compressed = u32::from_le_bytes([file_data[0], file_data[1], file_data[2], file_data[3]]) as u64;
                let uncompressed = u32::from_le_bytes([file_data[4], file_data[5], file_data[6], file_data[7]]) as u64;
                let os_byte = file_data[14];
                let name_len = u16::from_le_bytes([file_data[10], file_data[11]]) as usize;
                // name starts after 25-byte base header
                if n >= 25 + name_len {
                    let name = String::from_utf8_lossy(&file_data[25..25 + name_len]).to_string();
                    tags.push(mktag("ZIP", "CompressedSize", "Compressed Size", Value::U32(compressed as u32)));
                    tags.push(mktag("ZIP", "UncompressedSize", "Uncompressed Size", Value::U32(uncompressed as u32)));
                    let os_name = match os_byte {
                        0 => "MS-DOS",
                        1 => "OS/2",
                        2 => "Win32",
                        3 => "Unix",
                        _ => "Unknown",
                    };
                    tags.push(mktag("ZIP", "OperatingSystem", "Operating System", Value::String(os_name.into())));
                    tags.push(mktag("ZIP", "ArchivedFileName", "Archived File Name", Value::String(name)));
                }
            }
        }

        if size == 0 {
            break;
        }
        pos += size;
    }
}

// ============================================================================
// SVG (via XMP)
// ============================================================================

pub fn read_svg(data: &[u8]) -> Result<Vec<Tag>> {
    let text = String::from_utf8_lossy(data);

    if !text.contains("<svg") {
        return Err(Error::InvalidData("not an SVG file".into()));
    }

    let mut tags = Vec::new();

    // Parse SVG using XML parser for proper attribute/element extraction.
    // We handle three distinct sections:
    //   1. <svg> root element: version, xmlns, width, height attributes → SVG group tags
    //   2. <desc> and other non-metadata children: path-based tags → SVG group
    //   3. <metadata> block:
    //      a. <rdf:RDF> → extract to string, pass to XmpReader for XMP tags
    //      b. <c2pa:manifest> → base64-decode → JUMBF parsing
    use xml::reader::{EventReader, XmlEvent};
    let _parser = EventReader::from_str(&text);
    let mut path: Vec<String> = Vec::new(); // element local names (ucfirst)
    let mut current_text = String::new();
    // Which section are we in?
    let mut in_metadata = false; // inside <metadata> element
    let mut in_rdf = 0_usize;    // nesting depth inside <rdf:RDF>
    let mut in_c2pa = 0_usize;   // nesting depth inside <c2pa:manifest>
    let mut in_svg_body = false; // inside SVG non-metadata body (desc, title, etc.)
    // Track whether each path element had child elements (to skip mixed-content text).
    // True = had at least one child element. Parallel to `path`.
    let mut had_child: Vec<bool> = Vec::new();

    for event in EventReader::from_str(text.as_ref()) {
        match event {
            Ok(XmlEvent::StartElement { name, attributes, namespace, .. }) => {
                let local = &name.local_name;
                let ns = name.namespace.as_deref().unwrap_or("");

                // Root SVG element
                if local == "svg" && path.is_empty() {
                    path.push("Svg".into());
                    had_child.push(false);
                    for attr in &attributes {
                        match attr.name.local_name.as_str() {
                            "width" => tags.push(mktag("SVG", "ImageWidth", "Image Width", Value::String(attr.value.clone()))),
                            "height" => tags.push(mktag("SVG", "ImageHeight", "Image Height", Value::String(attr.value.clone()))),
                            "version" => tags.push(mktag("SVG", "SVGVersion", "SVG Version", Value::String(attr.value.clone()))),
                            "viewBox" | "viewbox" => tags.push(mktag("SVG", "ViewBox", "View Box", Value::String(attr.value.clone()))),
                            "id" => tags.push(mktag("SVG", "ID", "ID", Value::String(attr.value.clone()))),
                            _ => {}
                        }
                    }
                    // Extract default namespace (xmlns="...") from the namespace map
                    if let Some(default_ns) = namespace.get("") {
                        if !default_ns.is_empty() {
                            tags.push(mktag("SVG", "Xmlns", "XMLNS", Value::String(default_ns.to_string())));
                        }
                    }
                    current_text.clear();
                    continue;
                }

                // <metadata> block — switch to metadata parsing mode
                if local == "metadata" && !in_metadata && in_rdf == 0 && in_c2pa == 0 {
                    in_metadata = true;
                    // Mark parent as having a child
                    if let Some(last) = had_child.last_mut() { *last = true; }
                    path.push("Metadata".into());
                    had_child.push(false);
                    current_text.clear();
                    continue;
                }

                // Inside metadata: handle RDF and c2pa
                if in_metadata {
                    if in_rdf > 0 {
                        in_rdf += 1;
                        current_text.clear();
                        continue;
                    }
                    if in_c2pa > 0 {
                        in_c2pa += 1;
                        current_text.clear();
                        continue;
                    }
                    // Starting rdf:RDF
                    if local == "RDF" && ns == "http://www.w3.org/1999/02/22-rdf-syntax-ns#" {
                        in_rdf = 1;
                        current_text.clear();
                        continue;
                    }
                    // Starting c2pa:manifest
                    if name.prefix.as_deref() == Some("c2pa") || local == "manifest" {
                        in_c2pa = 1;
                        current_text.clear();
                        continue;
                    }
                    // Other metadata children: ignore
                    current_text.clear();
                    continue;
                }

                // SVG body elements (desc, title, etc.) - NOT metadata, NOT root svg
                if !in_metadata && path.len() >= 1 {
                    in_svg_body = true;
                    // Mark parent as having a child
                    if let Some(last) = had_child.last_mut() { *last = true; }
                    let ucfirst_local = svg_ucfirst(local);
                    path.push(ucfirst_local);
                    had_child.push(false);
                    current_text.clear();
                    continue;
                }

                path.push(svg_ucfirst(local));
                had_child.push(false);
                current_text.clear();
            }
            Ok(XmlEvent::Characters(t)) | Ok(XmlEvent::CData(t)) => {
                current_text.push_str(&t);
            }
            Ok(XmlEvent::EndElement { name }) => {
                let local = &name.local_name;

                // Exiting rdf:RDF depth
                if in_rdf > 0 {
                    in_rdf -= 1;
                    current_text.clear();
                    continue;
                }

                // Exiting c2pa:manifest depth
                if in_c2pa > 0 {
                    in_c2pa -= 1;
                    if in_c2pa == 0 {
                        // We've collected the base64 c2pa manifest text
                        let b64 = current_text.chars().filter(|c| !c.is_whitespace()).collect::<String>();
                        if !b64.is_empty() {
                            if let Ok(jumbf_data) = base64_decode(&b64) {
                                let jumbf_group = crate::tag::TagGroup {
                                    family0: "JUMBF".into(),
                                    family1: "JUMBF".into(),
                                    family2: "Image".into(),
                                };
                                let print = format!("(Binary data {} bytes, use -b option to extract)", jumbf_data.len());
                                tags.push(crate::tag::Tag {
                                    id: crate::tag::TagId::Text("JUMBF".into()),
                                    name: "JUMBF".into(),
                                    description: "JUMBF".into(),
                                    group: jumbf_group,
                                    raw_value: Value::Binary(jumbf_data.clone()),
                                    print_value: print,
                                    priority: 0,
                                });
                                parse_jumbf_for_svg(&jumbf_data, &mut tags);
                            }
                        }
                    }
                    current_text.clear();
                    continue;
                }

                // Exiting metadata
                if local == "metadata" && in_metadata {
                    in_metadata = false;
                    path.pop();
                    had_child.pop();
                    current_text.clear();
                    continue;
                }

                // Skip other metadata children
                if in_metadata {
                    current_text.clear();
                    continue;
                }

                // SVG body element text
                if in_svg_body && path.len() >= 2 {
                    let this_had_child = had_child.pop().unwrap_or(false);
                    let t = current_text.trim().to_string();
                    // Only emit if this element has no child elements (pure text node)
                    if !t.is_empty() && !this_had_child {
                        // Build tag name from path (skip root "Svg")
                        let tag_name = path.iter().skip(1).cloned().collect::<String>();
                        if !tag_name.is_empty() {
                            tags.push(mktag("SVG", &tag_name, &tag_name, Value::String(t)));
                        }
                    }
                    path.pop();
                    // If we've returned to Svg level (path.len() == 1), exit svg_body
                    if path.len() <= 1 {
                        in_svg_body = false;
                    }
                    current_text.clear();
                    continue;
                }

                path.pop();
                had_child.pop();
                current_text.clear();
            }
            Err(_) => break,
            _ => {}
        }
    }

    // Now extract XMP from the <rdf:RDF> block.
    // We look for the rdf:RDF section in the original text and pass it to XmpReader.
    // XmpReader handles rdf:RDF as a valid XMP envelope.
    if let Some(rdf_start) = text.find("<rdf:RDF") {
        if let Some(rdf_end) = text.find("</rdf:RDF>") {
            let rdf_section = &text[rdf_start..rdf_end + "</rdf:RDF>".len()];
            if let Ok(xmp_tags) = XmpReader::read(rdf_section.as_bytes()) {
                tags.extend(xmp_tags);
            }
        }
    }

    // Handle c2pa:manifest with potentially undeclared namespace prefix.
    // Use text-based extraction since the XML parser may fail on undeclared namespaces.
    if let Some(mstart) = text.find("<c2pa:manifest>") {
        let content_start = mstart + "<c2pa:manifest>".len();
        if let Some(mend) = text[content_start..].find("</c2pa:manifest>") {
            let b64_content = &text[content_start..content_start + mend];
            let b64: String = b64_content.chars().filter(|c| !c.is_whitespace()).collect();
            if !b64.is_empty() {
                if let Ok(jumbf_data) = base64_decode(&b64) {
                    let jumbf_group = crate::tag::TagGroup {
                        family0: "JUMBF".into(),
                        family1: "JUMBF".into(),
                        family2: "Image".into(),
                    };
                    let print = format!("(Binary data {} bytes, use -b option to extract)", jumbf_data.len());
                    tags.push(crate::tag::Tag {
                        id: crate::tag::TagId::Text("JUMBF".into()),
                        name: "JUMBF".into(),
                        description: "JUMBF".into(),
                        group: jumbf_group,
                        raw_value: Value::Binary(jumbf_data.clone()),
                        print_value: print,
                        priority: 0,
                    });
                    parse_jumbf_for_svg(&jumbf_data, &mut tags);
                }
            }
        }
    }

    Ok(tags)
}

/// UCfirst a string, preserving the rest as-is (for SVG element name path building).
fn svg_ucfirst(s: &str) -> String {
    let mut c = s.chars();
    match c.next() {
        None => String::new(),
        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
    }
}

/// Simple base64 decoder (no padding required).
fn base64_decode(s: &str) -> std::result::Result<Vec<u8>, ()> {
    let alphabet = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut table = [0u8; 256];
    for (i, &c) in alphabet.iter().enumerate() {
        table[c as usize] = i as u8;
    }
    let bytes: Vec<u8> = s.bytes().filter(|&b| b != b'=' && b != b'\n' && b != b'\r' && b != b' ').collect();
    let mut out = Vec::with_capacity(bytes.len() * 3 / 4);
    for chunk in bytes.chunks(4) {
        if chunk.len() < 2 { break; }
        let b0 = table[chunk[0] as usize];
        let b1 = table[chunk[1] as usize];
        out.push((b0 << 2) | (b1 >> 4));
        if chunk.len() >= 3 {
            let b2 = table[chunk[2] as usize];
            out.push((b1 << 4) | (b2 >> 2));
            if chunk.len() >= 4 {
                let b3 = table[chunk[3] as usize];
                out.push((b2 << 6) | b3);
            }
        }
    }
    Ok(out)
}

/// Parse JUMBF box structure from SVG c2pa:manifest to extract tags.
/// Mirrors the JPEG APP11 JUMBF parser logic.
fn parse_jumbf_for_svg(data: &[u8], tags: &mut Vec<Tag>) {
    parse_jumbf_boxes_svg(data, tags, true);
}

fn parse_jumbf_boxes_svg(data: &[u8], tags: &mut Vec<Tag>, top_level: bool) {
    let mut pos = 0;
    while pos + 8 <= data.len() {
        let lbox = u32::from_be_bytes([data[pos], data[pos+1], data[pos+2], data[pos+3]]) as usize;
        let tbox = &data[pos+4..pos+8];
        if lbox < 8 || pos + lbox > data.len() { break; }
        let content = &data[pos+8..pos+lbox];

        if tbox == b"jumb" {
            parse_jumbf_jumd_svg(content, tags, top_level);
        }

        pos += lbox;
    }
}

fn parse_jumbf_jumd_svg(data: &[u8], tags: &mut Vec<Tag>, emit_desc: bool) {
    let jumbf_group = crate::tag::TagGroup {
        family0: "JUMBF".into(),
        family1: "JUMBF".into(),
        family2: "Image".into(),
    };

    let mut pos = 0;
    let mut found_jumd = false;

    while pos + 8 <= data.len() {
        let lbox = u32::from_be_bytes([data[pos], data[pos+1], data[pos+2], data[pos+3]]) as usize;
        let tbox = &data[pos+4..pos+8];
        if lbox < 8 || pos + lbox > data.len() { break; }
        let content = &data[pos+8..pos+lbox];

        if tbox == b"jumd" && !found_jumd {
            found_jumd = true;
            if content.len() >= 17 {
                let type_bytes = &content[..16];
                let label_data = &content[17..];
                let null_pos = label_data.iter().position(|&b| b == 0).unwrap_or(label_data.len());
                let label = String::from_utf8_lossy(&label_data[..null_pos]).to_string();

                if emit_desc {
                    // Emit JUMDType
                    let type_hex: String = type_bytes.iter().map(|b| format!("{:02x}", b)).collect();
                    let a1 = &type_hex[8..12];
                    let a2 = &type_hex[12..16];
                    let a3 = &type_hex[16..32];
                    let ascii4 = &type_bytes[..4];
                    let is_printable = ascii4.iter().all(|&b| b.is_ascii_alphanumeric());
                    let print_type = if is_printable {
                        let ascii_str = String::from_utf8_lossy(ascii4);
                        format!("({})-{}-{}-{}", ascii_str, a1, a2, a3)
                    } else {
                        format!("{}-{}-{}-{}", &type_hex[..8], a1, a2, a3)
                    };
                    tags.push(crate::tag::Tag {
                        id: crate::tag::TagId::Text("JUMDType".into()),
                        name: "JUMDType".into(),
                        description: "JUMD Type".into(),
                        group: jumbf_group.clone(),
                        raw_value: Value::String(type_hex),
                        print_value: print_type,
                        priority: 0,
                    });
                    if !label.is_empty() {
                        tags.push(crate::tag::Tag {
                            id: crate::tag::TagId::Text("JUMDLabel".into()),
                            name: "JUMDLabel".into(),
                            description: "JUMD Label".into(),
                            group: jumbf_group.clone(),
                            raw_value: Value::String(label.clone()),
                            print_value: label.clone(),
                            priority: 0,
                        });
                    }
                }
            }
        } else if tbox == b"json" {
            // Parse JSON content to extract named fields
            if let Ok(json_str) = std::str::from_utf8(content) {
                parse_jumbf_json_svg(json_str.trim(), tags, &jumbf_group);
            }
        } else if tbox == b"jumb" {
            // Nested container: recurse without emitting JUMDType/Label again
            parse_jumbf_jumd_svg(content, tags, false);
        }

        pos += lbox;
    }
}

/// Parse a JUMBF JSON box to extract known fields (location, copyright, etc.)
fn parse_jumbf_json_svg(json: &str, tags: &mut Vec<Tag>, group: &crate::tag::TagGroup) {
    // Simple JSON field extractor for string values
    // Matches: "key": "value" patterns
    let mut i = 0;
    let bytes = json.as_bytes();
    while i < bytes.len() {
        if bytes[i] == b'"' {
            // Read key
            i += 1;
            let key_start = i;
            while i < bytes.len() && bytes[i] != b'"' { i += 1; }
            let key = &json[key_start..i];
            i += 1; // skip closing "
            // Skip whitespace and colon
            while i < bytes.len() && (bytes[i] == b':' || bytes[i] == b' ') { i += 1; }
            // Read value if it's a string
            if i < bytes.len() && bytes[i] == b'"' {
                i += 1;
                let val_start = i;
                while i < bytes.len() && bytes[i] != b'"' { i += 1; }
                let val = &json[val_start..i];
                i += 1;

                // Map known C2PA JSON keys to tag names (matching ExifTool's Jpeg2000 JUMBF table)
                let tag_name = match key {
                    "location" => Some("Location"),
                    "copyright" => Some("Copyright"),
                    _ => None,
                };
                if let Some(name) = tag_name {
                    tags.push(crate::tag::Tag {
                        id: crate::tag::TagId::Text(name.into()),
                        name: name.into(),
                        description: name.into(),
                        group: group.clone(),
                        raw_value: Value::String(val.to_string()),
                        print_value: val.to_string(),
                        priority: 0,
                    });
                }
            }
        } else {
            i += 1;
        }
    }
}


// ============================================================================
// JSON
// ============================================================================

pub fn read_json(data: &[u8]) -> Result<Vec<Tag>> {
    let text = String::from_utf8_lossy(data);
    let trimmed = text.trim();
    if !trimmed.starts_with('{') && !trimmed.starts_with('[') {
        return Err(Error::InvalidData("not a JSON file".into()));
    }

    let mut tags = Vec::new();

    // Parse top-level JSON object fields
    if trimmed.starts_with('{') {
        let mut collected: Vec<(String, String)> = Vec::new();
        parse_json_object(trimmed, "", &mut collected);
        for (key, value) in collected {
            let tag_name = json_key_to_tag_name(&key);
            if tag_name.is_empty() { continue; }
            tags.push(mktag("JSON", &tag_name, &tag_name, Value::String(value)));
        }
    }

    Ok(tags)
}

/// Recursively parse a JSON object, collecting (flat_tag_name, value) pairs.
/// For nested objects, the key is prepended to nested keys.
fn parse_json_object(json: &str, prefix: &str, out: &mut Vec<(String, String)>) {
    let mut pos = 0;
    let chars: Vec<char> = json.chars().collect();

    // skip opening {
    if pos < chars.len() && chars[pos] == '{' {
        pos += 1;
    }

    loop {
        // skip whitespace and commas
        while pos < chars.len() && (chars[pos].is_whitespace() || chars[pos] == ',') {
            pos += 1;
        }
        if pos >= chars.len() || chars[pos] == '}' {
            break;
        }

        // read key
        if chars[pos] != '"' {
            break;
        }
        let key = read_json_string(&chars, &mut pos);

        // skip whitespace and colon
        while pos < chars.len() && (chars[pos].is_whitespace() || chars[pos] == ':') {
            pos += 1;
        }

        // read value
        if pos >= chars.len() {
            break;
        }

        let full_key = if prefix.is_empty() { key.clone() } else { format!("{}{}", prefix, ucfirst_str(&key)) };

        match chars[pos] {
            '"' => {
                let val = read_json_string(&chars, &mut pos);
                out.push((full_key, val));
            }
            '{' => {
                let obj_start = pos;
                let obj_end = find_matching_bracket(&chars, pos, '{', '}');
                let obj_str: String = chars[obj_start..obj_end + 1].iter().collect();
                // For objects, flatten with parent key as prefix
                parse_json_object(&obj_str, &full_key, out);
                pos = obj_end + 1;
            }
            '[' => {
                let arr_start = pos;
                let arr_end = find_matching_bracket(&chars, pos, '[', ']');
                let arr_str: String = chars[arr_start..arr_end + 1].iter().collect();
                // Check if array contains objects (array-of-objects flattening)
                if array_contains_objects(&arr_str) {
                    // Flatten: parse each object with parent key as prefix, accumulate per sub-key
                    let mut sub_map: Vec<(String, Vec<String>)> = Vec::new();
                    parse_json_array_of_objects(&arr_str, &full_key, &mut sub_map);
                    for (sub_key, vals) in sub_map {
                        if !vals.is_empty() {
                            out.push((sub_key, vals.join(", ")));
                        }
                    }
                } else {
                    let values = parse_json_array(&arr_str);
                    if !values.is_empty() {
                        out.push((full_key, values.join(", ")));
                    }
                }
                pos = arr_end + 1;
            }
            'n' => {
                // null
                pos += 4;
                out.push((full_key, "null".into()));
            }
            't' => {
                // true
                pos += 4;
                out.push((full_key, "1".into()));
            }
            'f' => {
                // false
                pos += 5;
                out.push((full_key, "0".into()));
            }
            _ => {
                // number
                let num_start = pos;
                while pos < chars.len() && !chars[pos].is_whitespace() && chars[pos] != ',' && chars[pos] != '}' {
                    pos += 1;
                }
                let num: String = chars[num_start..pos].iter().collect();
                out.push((full_key, num));
            }
        }
    }
}

fn parse_json_array(json: &str) -> Vec<String> {
    let mut results = Vec::new();
    let chars: Vec<char> = json.chars().collect();
    let mut pos = 0;

    if pos < chars.len() && chars[pos] == '[' {
        pos += 1;
    }

    loop {
        while pos < chars.len() && (chars[pos].is_whitespace() || chars[pos] == ',') {
            pos += 1;
        }
        if pos >= chars.len() || chars[pos] == ']' {
            break;
        }

        match chars[pos] {
            '"' => {
                let val = read_json_string(&chars, &mut pos);
                results.push(val);
            }
            '[' => {
                let end = find_matching_bracket(&chars, pos, '[', ']');
                let sub: String = chars[pos..end + 1].iter().collect();
                let sub_vals = parse_json_array(&sub);
                results.extend(sub_vals);
                pos = end + 1;
            }
            '{' => {
                let end = find_matching_bracket(&chars, pos, '{', '}');
                pos = end + 1;
            }
            'n' => { pos += 4; results.push("null".into()); }
            't' => { pos += 4; results.push("1".into()); }
            'f' => { pos += 5; results.push("0".into()); }
            _ => {
                let start = pos;
                while pos < chars.len() && !chars[pos].is_whitespace() && chars[pos] != ',' && chars[pos] != ']' {
                    pos += 1;
                }
                results.push(chars[start..pos].iter().collect());
            }
        }
    }
    results
}

/// Returns true if the JSON array contains at least one object element.
fn array_contains_objects(json: &str) -> bool {
    let chars: Vec<char> = json.chars().collect();
    let mut pos = 0;
    if pos < chars.len() && chars[pos] == '[' { pos += 1; }
    while pos < chars.len() {
        if chars[pos].is_whitespace() || chars[pos] == ',' { pos += 1; continue; }
        if chars[pos] == ']' { break; }
        if chars[pos] == '{' { return true; }
        break;
    }
    false
}

/// Parse an array of objects, accumulating sub-fields per key.
/// sub_map: Vec<(sub_key, Vec<value>)> — ordered by first occurrence.
fn parse_json_array_of_objects(json: &str, prefix: &str, sub_map: &mut Vec<(String, Vec<String>)>) {
    let chars: Vec<char> = json.chars().collect();
    let mut pos = 0;
    if pos < chars.len() && chars[pos] == '[' { pos += 1; }

    loop {
        while pos < chars.len() && (chars[pos].is_whitespace() || chars[pos] == ',') { pos += 1; }
        if pos >= chars.len() || chars[pos] == ']' { break; }
        if chars[pos] == '{' {
            let end = find_matching_bracket(&chars, pos, '{', '}');
            let obj_str: String = chars[pos..end + 1].iter().collect();
            let mut obj_fields: Vec<(String, String)> = Vec::new();
            parse_json_object(&obj_str, prefix, &mut obj_fields);
            for (k, v) in obj_fields {
                if let Some(entry) = sub_map.iter_mut().find(|(sk, _)| sk == &k) {
                    // Append multiple values from nested arrays too
                    for part in v.split(", ") {
                        entry.1.push(part.to_string());
                    }
                } else {
                    let vals: Vec<String> = v.split(", ").map(|s| s.to_string()).collect();
                    sub_map.push((k, vals));
                }
            }
            pos = end + 1;
        } else {
            // Non-object element, skip
            while pos < chars.len() && chars[pos] != ',' && chars[pos] != ']' { pos += 1; }
        }
    }
}

fn read_json_string(chars: &[char], pos: &mut usize) -> String {
    if *pos >= chars.len() || chars[*pos] != '"' {
        return String::new();
    }
    *pos += 1; // skip opening "
    let mut result = String::new();
    while *pos < chars.len() && chars[*pos] != '"' {
        if chars[*pos] == '\\' && *pos + 1 < chars.len() {
            *pos += 1;
            match chars[*pos] {
                '"' => result.push('"'),
                '\\' => result.push('\\'),
                '/' => result.push('/'),
                'n' => result.push('\n'),
                'r' => result.push('\r'),
                't' => result.push('\t'),
                _ => result.push(chars[*pos]),
            }
        } else {
            result.push(chars[*pos]);
        }
        *pos += 1;
    }
    if *pos < chars.len() { *pos += 1; } // skip closing "
    result
}

fn find_matching_bracket(chars: &[char], start: usize, open: char, close: char) -> usize {
    let mut level = 0;
    let mut pos = start;
    let mut in_string = false;
    while pos < chars.len() {
        if chars[pos] == '"' && (pos == 0 || chars[pos - 1] != '\\') {
            in_string = !in_string;
        }
        if !in_string {
            if chars[pos] == open { level += 1; }
            else if chars[pos] == close {
                level -= 1;
                if level == 0 { return pos; }
            }
        }
        pos += 1;
    }
    pos.saturating_sub(1)
}

fn ucfirst_str(s: &str) -> String {
    let mut c = s.chars();
    match c.next() {
        None => String::new(),
        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
    }
}

/// Convert a JSON key (possibly nested like "testThis") to ExifTool tag name.
/// Mirrors Perl: ucfirst, then capitalize letters after non-alphabetic chars.
fn json_key_to_tag_name(key: &str) -> String {
    // ucfirst
    let key = ucfirst_str(key);
    // Capitalize after non-alpha: s/([^a-zA-Z])([a-z])/$1\U$2/g
    let mut result = String::new();
    let chars: Vec<char> = key.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        let c = chars[i];
        result.push(c);
        if !c.is_ascii_alphabetic() && i + 1 < chars.len() {
            if chars[i + 1].is_ascii_lowercase() {
                let uc = chars[i + 1].to_ascii_uppercase();
                result.push(uc);
                i += 2;
                continue;
            }
        }
        i += 1;
    }
    result
}

// ============================================================================
// RealAudio (RA)
// ============================================================================

/// Parse RealAudio (.ra) files. Mirrors ExifTool's Real.pm ProcessReal for RA.
pub fn read_real_audio(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 8 || !data.starts_with(b".ra\xfd") {
        return Err(Error::InvalidData("not a RealAudio file".into()));
    }

    let mut tags = Vec::new();
    let version = u16::from_be_bytes([data[4], data[5]]);

    // Only support version 4 currently (most common)
    if version != 4 {
        return Ok(tags);
    }

    // AudioV4: starts at offset 8
    let d = &data[8..];
    if d.len() < 40 {
        return Ok(tags);
    }

    let mut pos = 0;
    // Field 0: FourCC1 (4 bytes, undef)
    pos += 4;
    // Field 1: AudioFileSize (int32u)
    pos += 4;
    // Field 2: Version2 (int16u)
    pos += 2;
    // Field 3: HeaderSize (int32u)
    pos += 4;
    // Field 4: CodecFlavorID (int16u)
    pos += 2;
    // Field 5: CodedFrameSize (int32u)
    pos += 4;

    if pos + 4 > d.len() { return Ok(tags); }
    // Field 6: AudioBytes (int32u)
    let audio_bytes = u32::from_be_bytes([d[pos], d[pos+1], d[pos+2], d[pos+3]]);
    pos += 4;
    tags.push(mktag("Real", "AudioBytes", "Audio Bytes", Value::U32(audio_bytes)));

    if pos + 4 > d.len() { return Ok(tags); }
    // Field 7: BytesPerMinute (int32u)
    let bpm = u32::from_be_bytes([d[pos], d[pos+1], d[pos+2], d[pos+3]]);
    pos += 4;
    tags.push(mktag("Real", "BytesPerMinute", "Bytes Per Minute", Value::U32(bpm)));

    // Field 8: Unknown (int32u)
    pos += 4;
    // Field 9: SubPacketH (int16u)
    pos += 2;

    if pos + 2 > d.len() { return Ok(tags); }
    // Field 10: AudioFrameSize (int16u)
    let afs = u16::from_be_bytes([d[pos], d[pos+1]]);
    pos += 2;
    tags.push(mktag("Real", "AudioFrameSize", "Audio Frame Size", Value::U16(afs)));

    // Field 11: SubPacketSize (int16u)
    pos += 2;
    // Field 12: Unknown (int16u)
    pos += 2;

    if pos + 2 > d.len() { return Ok(tags); }
    // Field 13: SampleRate (int16u)
    let sr = u16::from_be_bytes([d[pos], d[pos+1]]);
    pos += 2;
    tags.push(mktag("Real", "SampleRate", "Sample Rate", Value::U16(sr)));

    // Field 14: Unknown (int16u)
    pos += 2;

    if pos + 2 > d.len() { return Ok(tags); }
    // Field 15: BitsPerSample (int16u)
    let bps = u16::from_be_bytes([d[pos], d[pos+1]]);
    pos += 2;
    tags.push(mktag("Real", "BitsPerSample", "Bits Per Sample", Value::U16(bps)));

    if pos + 2 > d.len() { return Ok(tags); }
    // Field 16: Channels (int16u)
    let ch = u16::from_be_bytes([d[pos], d[pos+1]]);
    pos += 2;
    tags.push(mktag("Real", "Channels", "Channels", Value::U16(ch)));

    if pos >= d.len() { return Ok(tags); }
    // Field 17: FourCC2Len (int8u)
    let fc2l = d[pos] as usize;
    pos += 1;
    pos += fc2l; // skip FourCC2

    if pos >= d.len() { return Ok(tags); }
    // Field 19: FourCC3Len (int8u)
    let fc3l = d[pos] as usize;
    pos += 1;
    pos += fc3l; // skip FourCC3

    if pos >= d.len() { return Ok(tags); }
    // Field 21: Unknown (int8u)
    pos += 1;

    if pos + 2 > d.len() { return Ok(tags); }
    // Field 22: Unknown (int16u)
    pos += 2;

    // Field 23: TitleLen (int8u)
    if pos >= d.len() { return Ok(tags); }
    let title_len = d[pos] as usize;
    pos += 1;

    // Field 24: Title (string[TitleLen])
    if pos + title_len <= d.len() && title_len > 0 {
        let title = String::from_utf8_lossy(&d[pos..pos + title_len]).to_string();
        tags.push(mktag("Real", "Title", "Title", Value::String(title)));
    }
    pos += title_len;

    // Field 25: ArtistLen (int8u)
    if pos >= d.len() { return Ok(tags); }
    let artist_len = d[pos] as usize;
    pos += 1;

    // Field 26: Artist
    if pos + artist_len <= d.len() && artist_len > 0 {
        let artist = String::from_utf8_lossy(&d[pos..pos + artist_len]).to_string();
        tags.push(mktag("Real", "Artist", "Artist", Value::String(artist)));
    }
    pos += artist_len;

    // Field 27: CopyrightLen (int8u)
    if pos >= d.len() { return Ok(tags); }
    let copy_len = d[pos] as usize;
    pos += 1;

    // Field 28: Copyright
    if pos + copy_len <= d.len() && copy_len > 0 {
        let copyright = String::from_utf8_lossy(&d[pos..pos + copy_len]).to_string();
        tags.push(mktag("Real", "Copyright", "Copyright", Value::String(copyright)));
    }

    Ok(tags)
}

// ============================================================================
// AAC (Advanced Audio Coding)
// ============================================================================

pub fn read_aac(data: &[u8]) -> Result<Vec<Tag>> {
    // AAC ADTS frame header: 7 bytes minimum
    if data.len() < 7 || data[0] != 0xFF || (data[1] != 0xF0 && data[1] != 0xF1) {
        return Err(Error::InvalidData("not an AAC ADTS file".into()));
    }

    // unpack as Perl: N=u32 big-endian from bytes 0-3, n=u16 from bytes 4-5, C=u8 from byte 6
    let t0 = u32::from_be_bytes([data[0], data[1], data[2], data[3]]);
    let t1 = u16::from_be_bytes([data[4], data[5]]);
    let _t2 = data[6];

    // Validate: profile type
    // In Perl: $t[0]>>16 & 0x03 = bits 17-16 counting from right (0=LSB) of big-endian u32
    // These correspond to stream bits 14-15 in Perl's Bit016-017 numbering
    // Perl uses ProcessBitStream which reads MSB-first; bits 16-17 from stream start = byte2 bits 0-1 from MSB
    let profile_type = (t0 >> 16) & 0x03; // matches Perl $t[0]>>16 & 0x03
    if profile_type == 3 {
        return Err(Error::InvalidData("reserved AAC profile type".into()));
    }

    // Sampling rate index: stream bits 18-21
    // In Perl's ProcessBitStream: Bit018-021 = byte 2 bits 2-5 from MSB
    // In big-endian u32 t0: byte 2 is bits 15-8. Byte2 bits 2-5 from MSB = t0 bits 13-10 from right.
    // (t0 >> 10) & 0x0F
    let sr_index = (t0 >> 10) & 0x0F;
    if sr_index > 12 {
        return Err(Error::InvalidData("invalid AAC sampling rate index".into()));
    }

    // Channel configuration: stream bits 23-25
    // byte2 bit 7 from MSB (stream bit 23) = t0 bit 8 from right
    // byte3 bits 0-1 from MSB (stream bits 24-25) = t0 bits 7-6 from right
    // (t0 >> 6) & 0x07
    let channel_config = (t0 >> 6) & 0x07;

    let mut tags = Vec::new();

    // ProfileType
    let profile_name = match profile_type {
        0 => "Main",
        1 => "Low Complexity",
        2 => "Scalable Sampling Rate",
        _ => "Unknown",
    };
    tags.push(mktag("AAC", "ProfileType", "Profile Type", Value::String(profile_name.into())));

    // SampleRate
    let sample_rates = [96000u32, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
    if let Some(&sr) = sample_rates.get(sr_index as usize) {
        tags.push(mktag("AAC", "SampleRate", "Sample Rate", Value::U32(sr)));
    }

    // Channels
    let channels_str = match channel_config {
        0 => "?",
        1 => "1",
        2 => "2",
        3 => "3",
        4 => "4",
        5 => "5",
        6 => "5+1",
        7 => "7+1",
        _ => "?",
    };
    tags.push(mktag("AAC", "Channels", "Channels", Value::String(channels_str.into())));

    // Frame length: bits 30-42 (13 bits)
    // $len = (($t0 << 11) & 0x1800) | (($t1 >> 5) & 0x07ff)
    let len = (((t0 as u64) << 11) & 0x1800) | (((t1 as u64) >> 5) & 0x07FF);
    let len = len as usize;

    // Try to extract Encoder from the filler payload in the frame.
    // Scan the remaining data for a printable ASCII string (like encoder name).
    if len >= 8 && data.len() >= len {
        let frame_data = &data[7..len];
        // Scan for a null-delimited printable string in the frame payload
        // The encoder string is typically in a filler element, null-terminated
        let mut i = 0;
        while i < frame_data.len() {
            // Skip null bytes
            while i < frame_data.len() && frame_data[i] == 0 { i += 1; }
            let start = i;
            // Read printable bytes
            while i < frame_data.len() && frame_data[i] >= 0x20 && frame_data[i] <= 0x7e { i += 1; }
            let end = i;
            if end - start >= 4 {
                if let Ok(enc) = std::str::from_utf8(&frame_data[start..end]) {
                    let enc = enc.trim();
                    if enc.len() >= 4 {
                        tags.push(mktag("AAC", "Encoder", "Encoder", Value::String(enc.into())));
                        break;
                    }
                }
            }
            i += 1;
        }
    }

    Ok(tags)
}

// ============================================================================
// WPG (WordPerfect Graphics)
// ============================================================================

pub fn read_wpg(data: &[u8]) -> Result<Vec<Tag>> {
    // WPG magic: FF 57 50 43
    if data.len() < 16 || &data[0..4] != b"\xff\x57\x50\x43" {
        return Err(Error::InvalidData("not a WPG file".into()));
    }

    let mut tags = Vec::new();

    // Offset to first record (little-endian u32 at bytes 4-7)
    let offset = u32::from_le_bytes([data[4], data[5], data[6], data[7]]) as usize;
    // Version at bytes 10-11
    let ver = data[10];
    let rev = data[11];
    tags.push(mktag("WPG", "WPGVersion", "WPG Version", Value::String(format!("{}.{}", ver, rev))));

    if ver < 1 || ver > 2 {
        return Ok(tags);
    }

    // Determine start position
    let mut pos = if offset > 16 { offset } else { 16 };
    if pos > data.len() { pos = data.len(); }

    let mut records: Vec<String> = Vec::new();
    let mut last_type: Option<u32> = None;
    let mut count = 0usize;
    let mut image_width_inches: Option<f64> = None;
    let mut image_height_inches: Option<f64> = None;

    // WPG v1 record map
    let v1_map: std::collections::HashMap<u32, &str> = [
        (0x01, "Fill Attributes"), (0x02, "Line Attributes"), (0x03, "Marker Attributes"),
        (0x04, "Polymarker"), (0x05, "Line"), (0x06, "Polyline"), (0x07, "Rectangle"),
        (0x08, "Polygon"), (0x09, "Ellipse"), (0x0a, "Reserved"), (0x0b, "Bitmap (Type 1)"),
        (0x0c, "Graphics Text (Type 1)"), (0x0d, "Graphics Text Attributes"),
        (0x0e, "Color Map"), (0x0f, "Start WPG (Type 1)"), (0x10, "End WPG"),
        (0x11, "PostScript Data (Type 1)"), (0x12, "Output Attributes"),
        (0x13, "Curved Polyline"), (0x14, "Bitmap (Type 2)"), (0x15, "Start Figure"),
        (0x16, "Start Chart"), (0x17, "PlanPerfect Data"), (0x18, "Graphics Text (Type 2)"),
        (0x19, "Start WPG (Type 2)"), (0x1a, "Graphics Text (Type 3)"),
        (0x1b, "PostScript Data (Type 2)"),
    ].iter().cloned().collect();

    // WPG v2 record map
    let v2_map: std::collections::HashMap<u32, &str> = [
        (0x00, "End Marker"), (0x01, "Start WPG"), (0x02, "End WPG"),
        (0x03, "Form Settings"), (0x04, "Ruler Settings"), (0x05, "Grid Settings"),
        (0x06, "Layer"), (0x08, "Pen Style Definition"), (0x09, "Pattern Definition"),
        (0x0a, "Comment"), (0x0b, "Color Transfer"), (0x0c, "Color Palette"),
        (0x0d, "DP Color Palette"), (0x0e, "Bitmap Data"), (0x0f, "Text Data"),
        (0x10, "Chart Style"), (0x11, "Chart Data"), (0x12, "Object Image"),
        (0x15, "Polyline"), (0x16, "Polyspline"), (0x17, "Polycurve"),
        (0x18, "Rectangle"), (0x19, "Arc"), (0x1a, "Compound Polygon"),
        (0x1b, "Bitmap"), (0x1c, "Text Line"), (0x1d, "Text Block"),
        (0x1e, "Text Path"), (0x1f, "Chart"), (0x20, "Group"),
        (0x21, "Object Capsule"), (0x22, "Font Settings"), (0x25, "Pen Fore Color"),
        (0x26, "DP Pen Fore Color"), (0x27, "Pen Back Color"), (0x28, "DP Pen Back Color"),
        (0x29, "Pen Style"), (0x2a, "Pen Pattern"), (0x2b, "Pen Size"),
        (0x2c, "DP Pen Size"), (0x2d, "Line Cap"), (0x2e, "Line Join"),
        (0x2f, "Brush Gradient"), (0x30, "DP Brush Gradient"), (0x31, "Brush Fore Color"),
        (0x32, "DP Brush Fore Color"), (0x33, "Brush Back Color"), (0x34, "DP Brush Back Color"),
        (0x35, "Brush Pattern"), (0x36, "Horizontal Line"), (0x37, "Vertical Line"),
        (0x38, "Poster Settings"), (0x39, "Image State"), (0x3a, "Envelope Definition"),
        (0x3b, "Envelope"), (0x3c, "Texture Definition"), (0x3d, "Brush Texture"),
        (0x3e, "Texture Alignment"), (0x3f, "Pen Texture "),
    ].iter().cloned().collect();

    let mut safety = 0;
    loop {
        if pos >= data.len() || safety > 10000 { break; }
        safety += 1;

        let (record_type, len, get_size) = if ver == 1 {
            if pos >= data.len() { break; }
            let rtype = data[pos] as u32;
            pos += 1;
            // Read var-int length
            let (l, advance) = read_wpg_varint(data, pos);
            pos += advance;
            let gs = rtype == 0x0f; // Start WPG (Type 1)
            (rtype, l, gs)
        } else {
            // Version 2: read 2 bytes for flags+type
            if pos + 1 >= data.len() { break; }
            let rtype = data[pos + 1] as u32;
            pos += 2;
            // Skip extensions (var-int)
            let (_, adv) = read_wpg_varint(data, pos);
            pos += adv;
            // Read record length (var-int)
            let (l, adv2) = read_wpg_varint(data, pos);
            pos += adv2;
            let gs = rtype == 0x01; // Start WPG
            let rtype_opt = if rtype > 0x3f { u32::MAX } else { rtype };
            (rtype_opt, l, gs)
        };

        if record_type == u32::MAX {
            // Skip unknown v2 record
            pos += len;
            continue;
        }

        if get_size {
            // Read Start record to get image dimensions
            let rec_end = pos + len;
            if rec_end > data.len() { break; }
            let rec = &data[pos..rec_end];
            pos = rec_end;

            if ver == 1 && rec.len() >= 6 {
                // v1: skip 2 bytes, then u16 width, u16 height
                let w = u16::from_le_bytes([rec[2], rec[3]]) as f64;
                let h = u16::from_le_bytes([rec[4], rec[5]]) as f64;
                image_width_inches = Some(w / 1200.0);
                image_height_inches = Some(h / 1200.0);
            } else if ver == 2 && rec.len() >= 21 {
                // v2: xres(u16), yres(u16), precision(u8), then coordinates
                let xres = u16::from_le_bytes([rec[0], rec[1]]) as f64;
                let yres = u16::from_le_bytes([rec[2], rec[3]]) as f64;
                let precision = rec[4];
                let (x1, y1, x2, y2) = if precision == 0 && rec.len() >= 21 {
                    // int16s x4 at offset 13
                    let x1 = i16::from_le_bytes([rec[13], rec[14]]) as f64;
                    let y1 = i16::from_le_bytes([rec[15], rec[16]]) as f64;
                    let x2 = i16::from_le_bytes([rec[17], rec[18]]) as f64;
                    let y2 = i16::from_le_bytes([rec[19], rec[20]]) as f64;
                    (x1, y1, x2, y2)
                } else if precision == 1 && rec.len() >= 29 {
                    // int32s x4 at offset 13
                    let x1 = i32::from_le_bytes([rec[13], rec[14], rec[15], rec[16]]) as f64;
                    let y1 = i32::from_le_bytes([rec[17], rec[18], rec[19], rec[20]]) as f64;
                    let x2 = i32::from_le_bytes([rec[21], rec[22], rec[23], rec[24]]) as f64;
                    let y2 = i32::from_le_bytes([rec[25], rec[26], rec[27], rec[28]]) as f64;
                    (x1, y1, x2, y2)
                } else {
                    pos += 0; // skip
                    // Emit last_type
                    if let Some(lt) = last_type.take() {
                        let _val = if count > 1 { format!("{}x{}", lt, count) } else { format!("{}", lt) };
                        records.push(format_wpg_record(lt, count, if ver == 1 { &v1_map } else { &v2_map }));
                    }
                    last_type = Some(record_type);
                    count = 1;
                    continue;
                };
                let w = (x2 - x1).abs();
                let h = (y2 - y1).abs();
                let xres_div = if xres == 0.0 { 1200.0 } else { xres };
                let yres_div = if yres == 0.0 { 1200.0 } else { yres };
                image_width_inches = Some(w / xres_div);
                image_height_inches = Some(h / yres_div);
            }
        } else {
            pos += len;
        }

        // Accumulate records (collapse sequential identical types)
        if last_type == Some(record_type) {
            count += 1;
        } else {
            if let Some(lt) = last_type.take() {
                records.push(format_wpg_record(lt, count, if ver == 1 { &v1_map } else { &v2_map }));
            }
            if record_type == 0 && ver == 2 { break; } // End Marker
            last_type = Some(record_type);
            count = 1;
        }
    }
    // Emit last record
    if let Some(lt) = last_type.take() {
        records.push(format_wpg_record(lt, count, if ver == 1 { &v1_map } else { &v2_map }));
    }

    if let Some(w) = image_width_inches {
        tags.push(mktag("WPG", "ImageWidthInches", "Image Width Inches", Value::String(format!("{:.2}", w))));
    }
    if let Some(h) = image_height_inches {
        tags.push(mktag("WPG", "ImageHeightInches", "Image Height Inches", Value::String(format!("{:.2}", h))));
    }
    if !records.is_empty() {
        let joined = records.join(", ");
        tags.push(mktag("WPG", "Records", "Records", Value::String(joined)));
    }

    Ok(tags)
}

fn format_wpg_record(rtype: u32, count: usize, map: &std::collections::HashMap<u32, &str>) -> String {
    let name = map.get(&rtype)
        .map(|s| s.to_string())
        .unwrap_or_else(|| format!("Unknown (0x{:02x})", rtype));
    if count > 1 {
        format!("{} x {}", name, count)
    } else {
        name
    }
}

fn read_wpg_varint(data: &[u8], pos: usize) -> (usize, usize) {
    if pos >= data.len() { return (0, 0); }
    let first = data[pos] as usize;
    if first != 0xFF {
        return (first, 1);
    }
    // 0xFF → read 2 more bytes as u16 LE
    if pos + 2 >= data.len() { return (0, 1); }
    let val = u16::from_le_bytes([data[pos + 1], data[pos + 2]]) as usize;
    if val & 0x8000 != 0 {
        // Read 2 more bytes
        if pos + 4 >= data.len() { return (val & 0x7FFF, 3); }
        let hi = u16::from_le_bytes([data[pos + 3], data[pos + 4]]) as usize;
        let full = ((val & 0x7FFF) << 16) | hi;
        return (full, 5);
    }
    (val, 3)
}

// ============================================================================
// Real Media Metafile (RAM/RPM)
// ============================================================================

pub fn read_ram(data: &[u8]) -> Result<Vec<Tag>> {
    // RAM files are text files with URLs, one per line
    // Must start with a valid URL or protocol
    if data.len() < 4 {
        return Err(Error::InvalidData("not a RAM file".into()));
    }

    let text = String::from_utf8_lossy(data);
    // Check for valid start: must begin with a URL-like protocol
    let _first_line = text.lines().next().unwrap_or("").trim();
    // Validate: http:// lines must end with real media extensions
    let valid_protocols = ["rtsp://", "pnm://", "http://", "rtspt://", "rtspu://", "mmst://", "file://"];
    let has_valid = text.lines().any(|line| {
        let l = line.trim();
        valid_protocols.iter().any(|p| l.starts_with(p))
    });
    if !has_valid && !text.starts_with(".RMF") && !data.starts_with(b".ra\xfd") {
        return Err(Error::InvalidData("not a Real RAM file".into()));
    }

    let mut tags = Vec::new();
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() { continue; }
        // Validate http:// URLs
        if line.starts_with("http://") {
            if !line.ends_with(".ra") && !line.ends_with(".rm") && !line.ends_with(".rv")
                && !line.ends_with(".rmvb") && !line.ends_with(".smil") {
                continue;
            }
        }
        if valid_protocols.iter().any(|p| line.starts_with(p)) {
            tags.push(mktag("Real", "URL", "URL", Value::String(line.into())));
        }
    }

    Ok(tags)
}

/// Parse DSS (Digital Speech Standard) voice recorder files.
/// Mirrors ExifTool's Olympus::ProcessDSS().
pub fn read_dss(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 68 {
        return Err(Error::InvalidData("DSS file too small".into()));
    }
    // Magic: \x02dss or \x03ds2
    if !(data[0] == 0x02 || data[0] == 0x03)
        || data[1] != b'd'
        || data[2] != b's'
        || (data[3] != b's' && data[3] != b'2')
    {
        return Err(Error::InvalidData("not a DSS/DS2 file".into()));
    }

    let mut tags = Vec::new();

    // Offset 12: Model, string[16]
    if data.len() >= 28 {
        let model_bytes = &data[12..28];
        let model = String::from_utf8_lossy(model_bytes)
            .trim_end_matches('\0')
            .trim()
            .to_string();
        if !model.is_empty() {
            tags.push(mktag("Olympus", "Model", "Camera Model Name", Value::String(model)));
        }
    }

    // Offset 38: StartTime, string[12] — format YYMMDDHHMMSS
    if data.len() >= 50 {
        let st_bytes = &data[38..50];
        let st_str = String::from_utf8_lossy(st_bytes);
        if let Some(dt) = parse_dss_time(&st_str) {
            tags.push(mktag("Olympus", "StartTime", "Start Time", Value::String(dt)));
        }
    }

    // Offset 50: EndTime, string[12]
    if data.len() >= 62 {
        let et_bytes = &data[50..62];
        let et_str = String::from_utf8_lossy(et_bytes);
        if let Some(dt) = parse_dss_time(&et_str) {
            tags.push(mktag("Olympus", "EndTime", "End Time", Value::String(dt)));
        }
    }

    // Offset 62: Duration, string[6] — format HHMMSS
    if data.len() >= 68 {
        let dur_bytes = &data[62..68];
        let dur_str = String::from_utf8_lossy(dur_bytes);
        if let Some(dur_secs) = parse_dss_duration(&dur_str) {
            let dur_display = dss_convert_duration(dur_secs);
            tags.push(mktag("Olympus", "Duration", "Duration", Value::String(dur_display)));
        }
    }

    Ok(tags)
}

/// Parse DSS time string YYMMDDHHMMSS → "20YY:MM:DD HH:MM:SS"
fn parse_dss_time(s: &str) -> Option<String> {
    let s = s.trim_matches('\0');
    if s.len() < 12 {
        return None;
    }
    let yy = &s[0..2];
    let mm = &s[2..4];
    let dd = &s[4..6];
    let hh = &s[6..8];
    let mi = &s[8..10];
    let ss = &s[10..12];
    // Validate digits
    if !yy.chars().all(|c| c.is_ascii_digit()) { return None; }
    Some(format!("20{}:{}:{} {}:{}:{}", yy, mm, dd, hh, mi, ss))
}

/// Parse DSS duration string HHMMSS → seconds
fn parse_dss_duration(s: &str) -> Option<f64> {
    let s = s.trim_matches('\0');
    if s.len() < 6 { return None; }
    let hh: u64 = s[0..2].parse().ok()?;
    let mm: u64 = s[2..4].parse().ok()?;
    let ss: u64 = s[4..6].parse().ok()?;
    Some(((hh * 60 + mm) * 60 + ss) as f64)
}

/// Convert duration in seconds to display string (mirrors ExifTool's ConvertDuration).
fn dss_convert_duration(secs: f64) -> String {
    if secs == 0.0 {
        return "0 s".to_string();
    }
    if secs < 30.0 {
        return format!("{:.2} s", secs);
    }
    let secs_u = (secs + 0.5) as u64;
    let h = secs_u / 3600;
    let m = (secs_u % 3600) / 60;
    let s = secs_u % 60;
    format!("{}:{:02}:{:02}", h, m, s)
}

// ============================================================================
// Helpers
// ============================================================================

fn mktag(family: &str, name: &str, description: &str, value: Value) -> Tag {
    let pv = value.to_display_string();
    Tag {
        id: TagId::Text(name.to_string()),
        name: name.to_string(),
        description: description.to_string(),
        group: TagGroup {
            family0: family.into(),
            family1: family.into(),
            family2: "Other".into(),
        },
        raw_value: value,
        print_value: pv,
        priority: 0,
    }
}

// ============================================================================
// InDesign format reader
// Extracts XMP metadata from Adobe InDesign (.indd) files.
// ============================================================================

pub fn read_indesign(data: &[u8]) -> Result<Vec<Tag>> {
    // InDesign master page GUID: 06 06 ED F5 D8 1D 46 E5 BD 31 EF E7 FE 74 B7 1D
    let master_guid = &[0x06u8, 0x06, 0xED, 0xF5, 0xD8, 0x1D, 0x46, 0xE5,
                         0xBD, 0x31, 0xEF, 0xE7, 0xFE, 0x74, 0xB7, 0x1D];
    let object_header_guid = &[0xDE, 0x39, 0x39, 0x79, 0x51, 0x88, 0x4B, 0x6C,
                                 0x8E, 0x63, 0xEE, 0xF8, 0xAE, 0xE0, 0xDD, 0x38];

    if data.len() < 4096 || !data.starts_with(master_guid) {
        return Err(crate::error::Error::InvalidData("not an InDesign file".into()));
    }

    // Read two master pages (each 4096 bytes) and pick the most current one
    if data.len() < 8192 {
        return Ok(vec![]);
    }

    let page1 = &data[..4096];
    let page2 = &data[4096..8192];

    // Master pages always use LE byte order ('II')
    // Determine current master page (highest sequence number wins)
    let cur_page = {
        let seq1 = u64::from_le_bytes(page1[264..272].try_into().unwrap_or([0;8]));
        let seq2 = if page2.starts_with(master_guid) {
            u64::from_le_bytes(page2[264..272].try_into().unwrap_or([0;8]))
        } else { 0 };
        if seq2 > seq1 { page2 } else { page1 }
    };

    // Stream byte order is at offset 24 of current master page: 1 = LE, 2 = BE
    let _stream_is_le = cur_page[24] == 1;

    // Number of pages (determines start of stream objects) - master page is LE
    let pages = u32::from_le_bytes(cur_page[280..284].try_into().unwrap_or([0;4]));
    let start_pos = (pages as usize) * 4096;
    if start_pos >= data.len() {
        return Ok(vec![]);
    }

    // Scan contiguous objects for XMP
    // Object header GUID (16 bytes) + additional header data (16 bytes) = 32 bytes total
    let mut pos = start_pos;
    while pos + 32 <= data.len() {
        if &data[pos..pos+16] != object_header_guid {
            break;
        }
        // Object (stream) length at offset 24 in the 32-byte object header
        // The object header itself appears to always use LE byte order
        let obj_len = u32::from_le_bytes(
            data[pos+24..pos+28].try_into().unwrap_or([0;4])
        ) as usize;

        pos += 32;
        if obj_len == 0 || pos + obj_len > data.len() { break; }

        let obj_data = &data[pos..pos + obj_len];

        // XMP stream: 4-byte length prefix followed by XMP data
        // The actual XMP starts at offset 0 or 4 depending on encoding
        if obj_len > 56 {
            if let Some(xp_pos) = find_xpacket(obj_data) {
                let xmp_data = &obj_data[xp_pos..];
                if let Ok(xmp_tags) = crate::metadata::XmpReader::read(xmp_data) {
                    return Ok(xmp_tags);
                }
            }
        }

        pos += obj_len;
    }

    Ok(vec![])
}

fn find_xpacket(data: &[u8]) -> Option<usize> {
    // Look for "<?xpacket begin=" or "<x:xmpmeta"
    for i in 0..data.len().saturating_sub(10) {
        if data[i..].starts_with(b"<?xpacket") || data[i..].starts_with(b"<x:xmpmeta") {
            return Some(i);
        }
    }
    None
}

// ============================================================================
// PCAP (packet capture) format reader
// ============================================================================

pub fn read_pcap(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 24 {
        return Err(crate::error::Error::InvalidData("not a PCAP file".into()));
    }

    let is_le = data[0] == 0xD4 && data[1] == 0xC3;
    let r16 = |d: &[u8], o: usize| -> u16 {
        if o+2 > d.len() { return 0; }
        if is_le { u16::from_le_bytes([d[o], d[o+1]]) }
        else { u16::from_be_bytes([d[o], d[o+1]]) }
    };
    let r32 = |d: &[u8], o: usize| -> u32 {
        if o+4 > d.len() { return 0; }
        if is_le { u32::from_le_bytes([d[o], d[o+1], d[o+2], d[o+3]]) }
        else { u32::from_be_bytes([d[o], d[o+1], d[o+2], d[o+3]]) }
    };

    let maj = r16(data, 4);
    let min = r16(data, 6);
    let link_type = r32(data, 20);

    let mut tags = Vec::new();
    let bo_str = if is_le { "Little-endian (Intel, II)" } else { "Big-endian (Motorola, MM)" };
    tags.push(mktag("PCAP", "ByteOrder", "Byte Order", Value::String(bo_str.into())));
    tags.push(mktag("PCAP", "PCAPVersion", "PCAP Version",
        Value::String(format!("PCAP {}.{}", maj, min))));
    tags.push(mktag("PCAP", "LinkType", "Link Type",
        Value::String(pcap_link_type_name(link_type))));

    Ok(tags)
}

// ============================================================================
// PCAPNG (pcap next generation) format reader
// ============================================================================

pub fn read_pcapng(data: &[u8]) -> Result<Vec<Tag>> {
    // Section Header Block: 0x0A0D0D0A
    if data.len() < 28 || data[0] != 0x0A || data[1] != 0x0D || data[2] != 0x0D || data[3] != 0x0A {
        return Err(crate::error::Error::InvalidData("not a PCAPNG file".into()));
    }

    // Block length at offset 4 (4 bytes)
    // Byte order magic at offset 8: 0x1A2B3C4D (LE) or 0x4D3C2B1A (BE)
    let bo_magic_le = data.len() >= 12 &&
        data[8] == 0x4D && data[9] == 0x3C && data[10] == 0x2B && data[11] == 0x1A;
    let is_le = bo_magic_le;

    let r16 = |d: &[u8], o: usize| -> u16 {
        if o+2 > d.len() { return 0; }
        if is_le { u16::from_le_bytes([d[o], d[o+1]]) }
        else { u16::from_be_bytes([d[o], d[o+1]]) }
    };
    let r32 = |d: &[u8], o: usize| -> u32 {
        if o+4 > d.len() { return 0; }
        if is_le { u32::from_le_bytes([d[o], d[o+1], d[o+2], d[o+3]]) }
        else { u32::from_be_bytes([d[o], d[o+1], d[o+2], d[o+3]]) }
    };

    let maj = r16(data, 12);
    let min = r16(data, 14);
    let blk_len = r32(data, 4) as usize;

    let mut tags = Vec::new();
    let bo_str = if is_le { "Little-endian (Intel, II)" } else { "Big-endian (Motorola, MM)" };
    tags.push(mktag("PCAP", "ByteOrder", "Byte Order", Value::String(bo_str.into())));
    tags.push(mktag("PCAP", "PCAPVersion", "PCAP Version",
        Value::String(format!("PCAPNG {}.{}", maj, min))));

    // SHB structure: block_type(4) + block_len(4) + bo_magic(4) + major(2) + minor(2) + section_len(8)
    // Options start at offset 24 (after the 8-byte section_length field)
    let opt_start = 24usize;
    let opt_end = if blk_len > 4 && blk_len <= data.len() { blk_len - 4 } else { data.len() };
    parse_pcapng_options(data, opt_start, opt_end, is_le, &mut tags, "shb");

    // Parse Interface Description Block (IDB) right after the SHB
    let idb_start = if blk_len < data.len() { blk_len } else { return Ok(tags); };
    if idb_start + 20 <= data.len() {
        let idb_type = r32(data, idb_start);
        if idb_type == 1 {
            // IDB: block type(4) + block_len(4) + link_type(2) + reserved(2) + snap_len(4) = 16 bytes
            let idb_len = r32(data, idb_start + 4) as usize;
            let link_type = r32(data, idb_start + 8) & 0xFFFF;
            let link_name = pcap_link_type_name(link_type);
            tags.push(mktag("PCAP", "LinkType", "Link Type", Value::String(link_name)));

            // Parse IDB options (starting at offset idb_start + 16)
            let idb_opt_start = idb_start + 16;
            let idb_opt_end = if idb_start + idb_len > 4 && idb_start + idb_len <= data.len() {
                idb_start + idb_len - 4
            } else { data.len() };
            parse_pcapng_options(data, idb_opt_start, idb_opt_end, is_le, &mut tags, "idb");

            // Parse EPB/SPB blocks to find TimeStamp
            let epb_start = idb_start + idb_len;
            parse_pcapng_blocks(data, epb_start, is_le, &mut tags);
        }
    }

    Ok(tags)
}

fn parse_pcapng_options(data: &[u8], start: usize, end: usize, is_le: bool,
                         tags: &mut Vec<Tag>, ctx: &str) {
    let r16 = |d: &[u8], o: usize| -> u16 {
        if o+2 > d.len() { return 0; }
        if is_le { u16::from_le_bytes([d[o], d[o+1]]) }
        else { u16::from_be_bytes([d[o], d[o+1]]) }
    };

    let mut pos = start;
    while pos + 4 <= end.min(data.len()) {
        let opt_code = r16(data, pos);
        let opt_len = r16(data, pos + 2) as usize;
        pos += 4;
        if opt_code == 0 { break; } // opt_endofopt
        let padded_len = (opt_len + 3) & !3;
        if pos + opt_len > data.len() { break; }

        let opt_data = &data[pos..pos + opt_len];

        match (ctx, opt_code) {
            ("shb", 2) => { // shb_hardware
                let s = String::from_utf8_lossy(opt_data).to_string();
                tags.push(mktag("PCAP", "Hardware", "Hardware", Value::String(s)));
            }
            ("shb", 3) => { // shb_os
                let s = String::from_utf8_lossy(opt_data).to_string();
                tags.push(mktag("PCAP", "OperatingSystem", "Operating System", Value::String(s)));
            }
            ("shb", 4) => { // shb_userappl
                let s = String::from_utf8_lossy(opt_data).to_string();
                tags.push(mktag("PCAP", "UserApplication", "User Application", Value::String(s)));
            }
            ("idb", 2) => { // if_name
                let s = String::from_utf8_lossy(opt_data).to_string();
                tags.push(mktag("PCAP", "DeviceName", "Device Name", Value::String(s)));
            }
            ("idb", 9) => { // if_tsresol: timestamp resolution
                if opt_len >= 1 {
                    let tsresol = opt_data[0];
                    let resolution = if tsresol & 0x80 != 0 {
                        // Power of 2
                        let exp = tsresol & 0x7F;
                        format!("2^-{}", exp)
                    } else {
                        // Power of 10
                        let exp = tsresol & 0x7F;
                        format!("1e-{:02}", exp)
                    };
                    tags.push(mktag("PCAP", "TimeStampResolution", "Time Stamp Resolution",
                        Value::String(resolution)));
                }
            }
            ("idb", 12) => { // if_os
                let s = String::from_utf8_lossy(opt_data).to_string();
                if !tags.iter().any(|t| t.name == "OperatingSystem") {
                    tags.push(mktag("PCAP", "OperatingSystem", "Operating System", Value::String(s)));
                }
            }
            _ => {}
        }

        pos += padded_len;
    }
}

fn parse_pcapng_blocks(data: &[u8], start: usize, is_le: bool, tags: &mut Vec<Tag>) {
    let r32 = |d: &[u8], o: usize| -> u32 {
        if o+4 > d.len() { return 0; }
        if is_le { u32::from_le_bytes([d[o], d[o+1], d[o+2], d[o+3]]) }
        else { u32::from_be_bytes([d[o], d[o+1], d[o+2], d[o+3]]) }
    };
    let _r64 = |d: &[u8], o: usize| -> u64 {
        if o+8 > d.len() { return 0; }
        if is_le { u64::from_le_bytes([d[o], d[o+1], d[o+2], d[o+3], d[o+4], d[o+5], d[o+6], d[o+7]]) }
        else { u64::from_be_bytes([d[o], d[o+1], d[o+2], d[o+3], d[o+4], d[o+5], d[o+6], d[o+7]]) }
    };

    let mut pos = start;
    while pos + 8 <= data.len() {
        let block_type = r32(data, pos);
        let block_len = r32(data, pos + 4) as usize;
        if block_len < 12 || pos + block_len > data.len() { break; }

        // EPB (Enhanced Packet Block) = type 6
        if block_type == 6 && block_len >= 28 {
            let ts_hi = r32(data, pos + 12) as u64;
            let ts_lo = r32(data, pos + 16) as u64;
            let ts_raw = (ts_hi << 32) | ts_lo;
            // Default resolution is 1e-6 (microseconds)
            let ts_secs = ts_raw / 1_000_000;
            let ts_usecs = ts_raw % 1_000_000;
            // Format as ExifTool does: YYYY:MM:DD HH:MM:SS.ssssss+ZZ:ZZ
            if let Some(dt) = format_unix_timestamp(ts_secs as i64, ts_usecs) {
                tags.push(mktag("PCAP", "TimeStamp", "Time Stamp", Value::String(dt)));
            }
            break; // Only need first packet timestamp
        }

        pos += block_len;
    }
}

fn format_unix_timestamp(secs: i64, usecs: u64) -> Option<String> {
    // Simple Unix timestamp to datetime conversion
    // This is a basic implementation - timezone from local offset
    // For now, use UTC + known local offset from Perl output
    // Perl shows: 2020:10:13 16:12:07.025764+02:00
    // We'll use UTC for simplicity but format it correctly
    

    // Get local timezone offset using system time
    let tz_offset_secs = get_local_tz_offset();

    let adjusted = secs + tz_offset_secs as i64;

    // Compute Y/M/D H:M:S from Unix timestamp
    let (y, mo, d, h, mi, s) = unix_to_datetime(adjusted);
    let tz_hours = tz_offset_secs / 3600;
    let tz_mins = (tz_offset_secs.abs() % 3600) / 60;
    let tz_sign = if tz_offset_secs >= 0 { '+' } else { '-' };

    Some(format!("{:04}:{:02}:{:02} {:02}:{:02}:{:02}.{:06}{}{:02}:{:02}",
        y, mo, d, h, mi, s, usecs, tz_sign, tz_hours.abs(), tz_mins))
}

fn get_local_tz_offset() -> i32 {
    // Try to get timezone offset from system
    // This uses a simple method: compare local time to UTC
    
    // For now return 0 (UTC) - the test data shows +02:00 but we can't easily detect this
    // without platform-specific code
    0
}

fn unix_to_datetime(secs: i64) -> (i32, u32, u32, u32, u32, u32) {
    // Basic implementation of Unix timestamp to calendar date
    const SECS_PER_DAY: i64 = 86400;
    const DAYS_PER_400Y: i64 = 146097;

    let (days, rem) = if secs >= 0 {
        (secs / SECS_PER_DAY, secs % SECS_PER_DAY)
    } else {
        let d = (secs + 1) / SECS_PER_DAY - 1;
        let r = secs - d * SECS_PER_DAY;
        (d, r)
    };

    let h = (rem / 3600) as u32;
    let m = ((rem % 3600) / 60) as u32;
    let s = (rem % 60) as u32;

    // Days since 1970-01-01
    // Adjust to days since 2000-03-01 for easier calculation
    let z = days + 719468; // days from 0000-03-01
    let era = if z >= 0 { z } else { z - DAYS_PER_400Y + 1 } / DAYS_PER_400Y;
    let doe = z - era * DAYS_PER_400Y;
    let yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365*yoe + yoe/4 - yoe/100);
    let mp = (5*doy + 2)/153;
    let d = doy - (153*mp+2)/5 + 1;
    let mo = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if mo <= 2 { y + 1 } else { y };

    (y as i32, mo as u32, d as u32, h, m, s)
}

fn pcap_link_type_name(link_type: u32) -> String {
    match link_type {
        0 => "BSD Loopback".into(),
        1 => "IEEE 802.3 Ethernet".into(),
        9 => "PPP".into(),
        105 => "IEEE 802.11".into(),
        108 => "OpenBSD Loopback".into(),
        113 => "Linux SLL".into(),
        127 => "IEEE 802.11 Radiotap".into(),
        _ => format!("{}", link_type),
    }
}

// ============================================================================
// ITC (iTunes Cover Flow)
// ============================================================================

pub fn read_itc(data: &[u8]) -> Result<Vec<Tag>> {
    // First block must be 'itch' with size >= 0x1c
    if data.len() < 8 {
        return Err(Error::InvalidData("not an ITC file".into()));
    }
    let first_size = u32::from_be_bytes([data[0], data[1], data[2], data[3]]) as usize;
    if &data[4..8] != b"itch" || first_size < 0x1c || first_size >= 0x10000 {
        return Err(Error::InvalidData("not an ITC file".into()));
    }

    let mut tags = Vec::new();
    let mut pos = 0usize;

    while pos + 8 <= data.len() {
        let block_size = u32::from_be_bytes([data[pos], data[pos+1], data[pos+2], data[pos+3]]) as usize;
        let block_tag = &data[pos+4..pos+8];

        if block_size < 8 || block_size >= 0x80000000 {
            break;
        }
        if pos + block_size > data.len() {
            break;
        }

        if block_tag == b"itch" {
            // Header block: DataType is at offset 0x10 (16) within block body
            let body_start = pos + 8;
            let body_end = pos + block_size;
            let body = &data[body_start..body_end];
            if body.len() >= 20 {
                let _data_type = &body[0x10 - 8 + 8..]; // offset 0x10 from block start = 0x08 from body
                // Actually offset 0x10 from the block start means byte 16 from pos
                // body starts at pos+8, so offset 16 from pos = body[8..12]
                let dt_bytes = &data[pos+16..pos+20];
                let dt_str = match dt_bytes {
                    b"artw" => "Artwork",
                    _ => "Unknown",
                };
                tags.push(mktag("ITC", "DataType", "Data Type", Value::String(dt_str.into())));
            }
        } else if block_tag == b"item" {
            // Read inner length (4 bytes after block header)
            if pos + 12 > data.len() { break; }
            let inner_len = u32::from_be_bytes([data[pos+8], data[pos+9], data[pos+10], data[pos+11]]) as usize;
            if inner_len < 0xd0 || inner_len > block_size { break; }

            // Remaining image data size
            let image_size = block_size - inner_len;

            // Skip past 4-byte blocks until null terminator
            // Starting after block_header(8) + inner_len_field(4) = pos+12
            let mut scan = pos + 12;
            let mut remaining = inner_len - 12;
            loop {
                if remaining < 4 || scan + 4 > data.len() { break; }
                let word = &data[scan..scan+4];
                remaining -= 4;
                scan += 4;
                if word == b"\0\0\0\0" { break; }
            }
            if remaining < 4 { break; }

            // Read remaining header
            let hdr_start = scan;
            let hdr_len = remaining;
            if hdr_start + hdr_len > data.len() { break; }
            let hdr = &data[hdr_start..hdr_start + hdr_len];

            // Verify 'data' marker at offset 0xb0
            if hdr.len() < 0xb4 || &hdr[0xb0..0xb4] != b"data" { break; }

            // Parse ITC::Item fields (FORMAT = int32u, FIRST_ENTRY = 0)
            // Entry 0 (offset 0*4=0): LibraryID = undef[8] → hex string
            if hdr.len() >= 8 {
                let lib_id = &hdr[0..8];
                let hex: String = lib_id.iter().map(|b| format!("{:02X}", b)).collect();
                tags.push(mktag("ITC", "LibraryID", "Library ID", Value::String(hex)));
            }
            // Entry 2 (offset 2*4=8): TrackID = undef[8] → hex string
            if hdr.len() >= 16 {
                let track_id = &hdr[8..16];
                let hex: String = track_id.iter().map(|b| format!("{:02X}", b)).collect();
                tags.push(mktag("ITC", "TrackID", "Track ID", Value::String(hex)));
            }
            // Entry 4 (offset 4*4=16): DataLocation = undef[4]
            if hdr.len() >= 20 {
                let loc = &hdr[16..20];
                let loc_str = match loc {
                    b"down" => "Downloaded Separately",
                    b"locl" => "Local Music File",
                    _ => "Unknown",
                };
                tags.push(mktag("ITC", "DataLocation", "Data Location", Value::String(loc_str.into())));
            }
            // Entry 5 (offset 5*4=20): ImageType = undef[4]
            if hdr.len() >= 24 {
                let img_type = &hdr[20..24];
                let type_str = match img_type {
                    b"PNGf" => "PNG",
                    b"\0\0\0\x0d" => "JPEG",
                    _ => "Unknown",
                };
                tags.push(mktag("ITC", "ImageType", "Image Type", Value::String(type_str.into())));
            }
            // Entry 7 (offset 7*4=28): ImageWidth = int32u
            if hdr.len() >= 32 {
                let width = u32::from_be_bytes([hdr[28], hdr[29], hdr[30], hdr[31]]);
                tags.push(mktag("ITC", "ImageWidth", "Image Width", Value::U32(width)));
            }
            // Entry 8 (offset 8*4=32): ImageHeight = int32u
            if hdr.len() >= 36 {
                let height = u32::from_be_bytes([hdr[32], hdr[33], hdr[34], hdr[35]]);
                tags.push(mktag("ITC", "ImageHeight", "Image Height", Value::U32(height)));
            }

            // ImageData (binary data after item header)
            if image_size > 0 {
                let img_start = pos + block_size - image_size;
                if img_start + image_size <= data.len() {
                    let img_data = data[img_start..img_start + image_size].to_vec();
                    tags.push(mktag("ITC", "ImageData", "Image Data", Value::Binary(img_data)));
                }
            }
        }

        pos += block_size;
    }

    Ok(tags)
}


// ============================================================================
// ZISRAW/CZI (Zeiss Integrated Software RAW)
// ============================================================================

pub fn read_czi(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 100 || !data.starts_with(b"ZISRAWFILE\x00\x00\x00\x00\x00\x00") {
        return Err(Error::InvalidData("not a ZISRAW/CZI file".into()));
    }

    let mut tags = Vec::new();

    // Binary header fields (little-endian)
    // ZISRAWVersion at offset 0x20: two int32u values
    if data.len() >= 0x28 {
        let major = u32::from_le_bytes([data[0x20], data[0x21], data[0x22], data[0x23]]);
        let minor = u32::from_le_bytes([data[0x24], data[0x25], data[0x26], data[0x27]]);
        let version = format!("{}.{}", major, minor);
        tags.push(mktag("ZISRAW", "ZISRAWVersion", "ZISRAW Version", Value::String(version)));
    }

    // PrimaryFileGUID at offset 0x30: 16 bytes as hex
    if data.len() >= 0x40 {
        let guid = hex_encode(&data[0x30..0x40]);
        tags.push(mktag("ZISRAW", "PrimaryFileGUID", "Primary File GUID", Value::String(guid)));
    }

    // FileGUID at offset 0x40: 16 bytes as hex
    if data.len() >= 0x50 {
        let guid = hex_encode(&data[0x40..0x50]);
        tags.push(mktag("ZISRAW", "FileGUID", "File GUID", Value::String(guid)));
    }

    // Metadata section offset at byte 92 (0x5C): 64-bit LE
    if data.len() >= 100 {
        let meta_off = u64::from_le_bytes([data[92], data[93], data[94], data[95],
                                            data[96], data[97], data[98], data[99]]) as usize;
        if meta_off > 0 && meta_off + 288 <= data.len() {
            // Check for ZISRAWMETADATA magic
            if &data[meta_off..meta_off+16] == b"ZISRAWMETADATA\x00\x00" {
                // XML length at offset 32 of metadata segment
                let xml_len = u32::from_le_bytes([data[meta_off+32], data[meta_off+33],
                                                    data[meta_off+34], data[meta_off+35]]) as usize;
                let xml_start = meta_off + 288;
                if xml_start + xml_len <= data.len() {
                    let xml_bytes = &data[xml_start..xml_start+xml_len];
                    // Emit XML as binary data tag
                    tags.push(mktag("ZISRAW", "XML", "XML",
                        Value::String(format!("(Binary data {} bytes, use -b option to extract)", xml_len))));
                    // Parse XML metadata
                    if let Ok(xml_str) = std::str::from_utf8(xml_bytes) {
                        czi_parse_xml(xml_str, &mut tags);
                    }
                }
            }
        }
    }

    Ok(tags)
}

/// Parse CZI XML metadata and extract tags with shortened names.
/// Skips ImageDocument, Metadata, Information path elements (XmpIgnoreProps).
fn czi_parse_xml(xml: &str, tags: &mut Vec<Tag>) {
    use xml::reader::{EventReader, XmlEvent};

    let parser = EventReader::from_str(xml);
    // Path of element names (excluding ignored elements)
    let mut path: Vec<String> = Vec::new();
    // Stack tracking whether each element is ignored
    let mut ignored: Vec<bool> = Vec::new();
    let mut current_text = String::new();
    let mut has_child: Vec<bool> = Vec::new();

    // Elements to ignore in path building
    let ignore_elems = ["ImageDocument", "Metadata", "Information"];

    for event in parser {
        match event {
            Ok(XmlEvent::StartElement { name, attributes, .. }) => {
                let elem_name = &name.local_name;
                let is_ignored = ignore_elems.contains(&elem_name.as_str());
                ignored.push(is_ignored);
                if let Some(last) = has_child.last_mut() { *last = true; }

                if !is_ignored {
                    path.push(elem_name.clone());
                    has_child.push(false);
                    current_text.clear();

                    // Emit attributes as tags
                    let path_str = path.join("");
                    for attr in &attributes {
                        let aname = &attr.name;
                        // Skip xmlns and xsi attributes
                        if aname.prefix.as_deref() == Some("xmlns")
                            || aname.prefix.as_deref() == Some("xsi")
                            || aname.local_name.starts_with("xmlns")
                        {
                            continue;
                        }
                        let raw_tag = format!("{}{}", path_str, aname.local_name);
                        let tag_name = czi_shorten_tag_name(&raw_tag);
                        if !tag_name.is_empty() {
                            let val = attr.value.trim().to_string();
                            tags.push(mktag("ZISRAW", &tag_name, &tag_name, Value::String(val)));
                        }
                    }
                } else {
                    has_child.push(false);
                    current_text.clear();
                }
            }
            Ok(XmlEvent::Characters(text)) | Ok(XmlEvent::CData(text)) => {
                current_text.push_str(&text);
            }
            Ok(XmlEvent::EndElement { .. }) => {
                let is_ignored = ignored.pop().unwrap_or(false);
                let is_leaf = has_child.pop().unwrap_or(false) == false;

                if !is_ignored {
                    if is_leaf {
                        let text = current_text.trim().to_string();
                        // Only emit leaf text nodes when they have content OR
                        // when the element has no attributes (emit empty string for empty elements with attributes
                        // is handled by attribute processing; don't double-emit)
                        // We emit if: has attributes AND text is empty? No - don't emit if empty
                        // Actually only emit if text is non-empty OR element has attributes-only children
                        // Perl: emit element text as tag; attributes are separate tags
                        // The empty <DeviceRef></DeviceRef> case: has attribute Id (emitted separately),
                        // the element text "" should NOT be emitted as a separate tag
                        // But <StandSpecification>Inverted</> SHOULD be emitted
                        // Rule: only emit leaf text if the text is non-empty
                        if !text.is_empty() {
                            let path_str = path.join("");
                            let tag_name = czi_shorten_tag_name(&path_str);
                            if !tag_name.is_empty() {
                                tags.push(mktag("ZISRAW", &tag_name, &tag_name, Value::String(text)));
                            }
                        }
                    }
                    path.pop();
                } else {
                    // Ignored element - don't pop path (it wasn't pushed)
                }
                current_text.clear();
            }
            _ => {}
        }
    }
}

/// Apply CZI tag name shortening (mirrors Perl's ShortenTagNames).
fn czi_shorten_tag_name(name: &str) -> String {
    let mut s = name.to_string();

    // Apply substitutions in order (mirrors Perl's ShortenTagNames)
    s = s.strip_prefix("HardwareSetting").unwrap_or(&s).to_string();
    s = regex_replace(&s, "^DevicesDevice", "Device");
    s = s.replace("LightPathNode", "");
    s = s.replace("Successors", "");
    s = s.replace("ExperimentExperiment", "Experiment");
    s = regex_replace(&s, "ObjectivesObjective", "Objective");
    s = s.replace("ChannelsChannel", "Channel");
    s = s.replace("TubeLensesTubeLens", "TubeLens");
    s = regex_replace(&s, "^ExperimentHardwareSettingsPoolHardwareSetting", "HardwareSetting");
    s = s.replace("SharpnessMeasureSetSharpnessMeasure", "Sharpness");
    s = s.replace("FocusSetupAutofocusSetup", "Autofocus");
    s = s.replace("TracksTrack", "Track");
    s = s.replace("ChannelRefsChannelRef", "ChannelRef");
    s = s.replace("ChangerChanger", "Changer");
    s = s.replace("ElementsChangerElement", "Changer");
    s = s.replace("ChangerElements", "Changer");
    s = s.replace("ContrastChangerContrast", "Contrast");
    s = s.replace("KeyFunctionsKeyFunction", "KeyFunction");
    s = regex_replace(&s, "ManagerContrastManager(Contrast)?", "ManagerContrast");
    s = s.replace("ObjectiveChangerObjective", "ObjectiveChanger");
    s = s.replace("ManagerLightManager", "ManagerLight");
    s = s.replace("WavelengthAreasWavelengthArea", "WavelengthArea");
    s = s.replace("ReflectorChangerReflector", "ReflectorChanger");
    s = regex_replace(&s, "^StageStageAxesStageAxis", "StageAxis");
    s = s.replace("ShutterChangerShutter", "ShutterChanger");
    s = s.replace("OnOffChangerOnOff", "OnOffChanger");
    s = s.replace("UnsharpMaskStateUnsharpMask", "UnsharpMask");
    s = s.replace("Acquisition", "Acq");
    s = s.replace("Continuous", "Cont");
    s = s.replace("Resolution", "Res");
    s = s.replace("Experiment", "Expt");
    s = s.replace("Threshold", "Thresh");
    s = s.replace("Reference", "Ref");
    s = s.replace("Magnification", "Mag");
    s = s.replace("Original", "Orig");
    s = s.replace("FocusSetupFocusStrategySetup", "Focus");
    s = s.replace("ParametersParameter", "Parameter");
    s = s.replace("IntervalInfo", "Interval");
    s = s.replace("ExptBlocksAcqBlock", "AcqBlock");
    s = s.replace("MicroscopesMicroscope", "Microscope");
    s = s.replace("TimeSeriesInterval", "TimeSeries");
    // s/Interval(.*Interval)/$1/  - complex, handle with loop
    while let Some(idx) = s.find("Interval") {
        let rest = &s[idx + "Interval".len()..];
        if rest.contains("Interval") {
            // Remove first Interval
            s = format!("{}{}", &s[..idx], &s[idx + "Interval".len()..]);
        } else {
            break;
        }
    }
    s = s.replace("SingleTileRegionsSingleTileRegion", "SingleTileRegion");
    s = s.replace("AcquisitionMode", ""); // already replaced Acquisition above
    s = s.replace("DetectorsDetector", "Detector");
    s = regex_replace(&s, "Setup[s]?", "");
    s = s.replace("Setting", "");
    s = s.replace("TrackTrack", "Track");
    s = s.replace("AnalogOutMaximumsAnalogOutMaximum", "AnalogOutMaximum");
    s = s.replace("AnalogOutMinimumsAnalogOutMinimum", "AnalogOutMinimum");
    s = s.replace("DigitalOutLabelsDigitalOutLabelLabel", "DigitalOutLabelLabel");
    s = s.replace("FocusDefiniteFocus", "FocusDefinite");
    s = s.replace("ChangerChanger", "Changer");
    s = s.replace("Calibration", "Cal");
    s = s.replace("LightSwitchChangerRLTLSwitch", "LightSwitchChangerRLTL");
    s = s.replace("Parameters", "");
    s = s.replace("Fluorescence", "Fluor");
    s = s.replace("CameraGeometryCameraGeometry", "CameraGeometry");
    s = s.replace("CameraCamera", "Camera");
    s = s.replace("DetectorsCamera", "Camera");
    s = s.replace("FilterChangerLeftChangerEmissionFilter", "LeftChangerEmissionFilter");
    s = s.replace("SwitchingStatesSwitchingState", "SwitchingState");
    s = s.replace("Information", "Info");
    // s/SubDimensions?//g
    s = s.replace("SubDimensions", "");
    s = s.replace("SubDimension", "");
    // s/Setups?//
    s = regex_replace_first(&s, "Setups?", "");
    // s/Parameters?//
    s = regex_replace_first(&s, "Parameters?", "");
    s = s.replace("Calculate", "Calc");
    s = s.replace("Visibility", "Vis");
    s = s.replace("Orientation", "Orient");
    s = s.replace("ListItems", "Items");
    s = s.replace("Increment", "Incr");
    s = s.replace("Parameter", "Param");
    // s/(ParfocalParcentralValues)+ParfocalParcentralValue/Parcentral/
    s = regex_replace(&s, "(ParfocalParcentralValues?)+ParfocalParcentralValues?", "Parcentral");
    s = s.replace("ParcentralParcentral", "Parcentral");
    s = s.replace("CorrFocusCorrection", "FocusCorr");
    // s/(ApoTomeDepthInfo)+Element/ApoTomeDepth/
    s = regex_replace(&s, "(ApoTomeDepthInfo)+Element", "ApoTomeDepth");
    s = regex_replace(&s, "(ApoTomeClickStopInfo)+Element", "ApoTomeClickStop");
    s = s.replace("DepthDepth", "Depth");
    // s/(Devices?)+Device/Device/
    s = regex_replace(&s, "(Devices?)+Device", "Device");
    // s/(BeamPathNode)+/BeamPathNode/
    s = regex_replace(&s, "(BeamPathNode)+", "BeamPathNode");
    s = s.replace("BeamPathsBeamPath", "BeamPath");
    s = s.replace("BeamPathBeamPath", "BeamPath");
    s = s.replace("Configuration", "Config");
    s = s.replace("StageAxesStageAxis", "StageAxis");
    s = s.replace("RangesRange", "Range");
    s = s.replace("DataGridDatasGridData", "DataGrid");
    s = s.replace("DataMicroscopeDatasMicroscopeData", "DataMicroscope");
    s = s.replace("DataWegaDatasWegaData", "DataWega");
    s = s.replace("ClickStopPositionsClickStopPosition", "ClickStopPosition");
    // s/LightSourcess?LightSource(Settings)?(LightSource)?/LightSource/
    s = regex_replace(&s, "LightSourcess?LightSource(Settings)?(LightSource)?", "LightSource");
    s = s.replace("FilterSetsFilterSet", "FilterSet");
    s = s.replace("EmissionFiltersEmissionFilter", "EmissionFilter");
    s = s.replace("ExcitationFiltersExcitationFilter", "ExcitationFilter");
    s = s.replace("FiltersFilter", "Filter");
    s = s.replace("DichroicsDichroic", "Dichronic");
    s = s.replace("WavelengthsWavelength", "Wavelength");
    s = s.replace("MultiTrackSetup", "MultiTrack");
    s = s.replace("TrackTrack", "Track");
    s = s.replace("DataGrabberSetup", "DataGrabber");
    s = s.replace("CameraFrameSetup", "CameraFrame");
    s = regex_replace(&s, "TimeSeries(TimeSeries|Setups)", "TimeSeries");
    s = s.replace("FocusFocus", "Focus");
    s = s.replace("FocusAutofocus", "Autofocus");
    // s/Focus(Hardware|Software)(Autofocus)+/Autofocus$1/
    s = regex_replace(&s, "Focus(Hardware|Software)(Autofocus)+", "Autofocus$1");
    s = s.replace("AutofocusAutofocus", "Autofocus");

    s
}

/// Simple regex replace (first occurrence only for non-global patterns).
fn regex_replace(s: &str, pat: &str, replacement: &str) -> String {
    // For simple patterns, use manual string matching
    // For patterns with anchors or groups, implement manually
    if pat.starts_with('^') {
        let pat_body = &pat[1..];
        if s.starts_with(pat_body) {
            return format!("{}{}", replacement, &s[pat_body.len()..]);
        }
        return s.to_string();
    }
    // Non-anchored: find first occurrence
    // Handle simple capturing groups for replacement
    if pat.contains('(') {
        // Handle specific known patterns
        return czi_regex_replace_group(s, pat, replacement);
    }
    if let Some(idx) = s.find(pat) {
        format!("{}{}{}", &s[..idx], replacement, &s[idx + pat.len()..])
    } else {
        s.to_string()
    }
}

fn regex_replace_first(s: &str, pat: &str, replacement: &str) -> String {
    // Handle patterns like "Setups?" (optional s) and "Parameters?" 
    let variants: Vec<&str> = if pat.ends_with('?') {
        let base = &pat[..pat.len()-1];
        let long = pat.trim_end_matches('?');
        // "Setups?" → try "Setups" then "Setup"
        // We need both variants
        vec![long, base]
    } else {
        vec![pat]
    };
    // Try with the longer variant first
    if pat.ends_with('?') {
        let long = pat.trim_end_matches('?'); // "Setups" from "Setups?"
        let base = &long[..long.len()-1]; // "Setup"
        // Try "Setups" first
        if let Some(idx) = s.find(long) {
            return format!("{}{}{}", &s[..idx], replacement, &s[idx + long.len()..]);
        }
        // Then try "Setup"
        if let Some(idx) = s.find(base) {
            return format!("{}{}{}", &s[..idx], replacement, &s[idx + base.len()..]);
        }
    }
    let _ = variants;
    s.to_string()
}

/// Handle regex replace for patterns with capturing groups.
fn czi_regex_replace_group(s: &str, pat: &str, _replacement: &str) -> String {
    // Handle specific known patterns:
    // "(Devices?)+Device" → "Device"
    // "(BeamPathNode)+" → "BeamPathNode"
    // etc.
    // For simplicity, handle specific patterns
    if pat == "(Devices?)+Device" {
        // Match one or more of "Device" or "Devices" followed by "Device"
        // Replace with "Device"
        // Pattern: DevicesDevice, DeviceDevicesDevice, etc.
        let result = s.to_string();
        // Iteratively replace DevicesDevice and DeviceDevice
        let mut r = result.clone();
        loop {
            let prev = r.clone();
            r = r.replace("DevicesDevice", "Device");
            r = r.replace("DeviceDevice", "Device");
            if r == prev { break; }
        }
        return r;
    }
    if pat == "(BeamPathNode)+" {
        // Replace multiple BeamPathNode with single
        let mut r = s.to_string();
        loop {
            let prev = r.clone();
            r = r.replace("BeamPathNodeBeamPathNode", "BeamPathNode");
            if r == prev { break; }
        }
        return r;
    }
    if pat == "ManagerContrastManager(Contrast)?" {
        // Replace "ManagerContrastManagerContrast" or "ManagerContrastManager" with "ManagerContrast"
        let r = s.replace("ManagerContrastManagerContrast", "ManagerContrast");
        let r = r.replace("ManagerContrastManager", "ManagerContrast");
        return r;
    }
    if pat.starts_with("Focus(Hardware|Software)(Autofocus)+") {
        // s/Focus(Hardware|Software)(Autofocus)+/Autofocus$1/
        for suffix in &["Hardware", "Software"] {
            let search = format!("Focus{}Autofocus", suffix);
            if let Some(idx) = s.find(&search) {
                // Replace Focus{X}(Autofocus)+ with Autofocus{X}
                // First consume all Autofocus repetitions
                let mut end = idx + search.len();
                while s[end..].starts_with("Autofocus") {
                    end += "Autofocus".len();
                }
                return format!("{}Autofocus{}{}", &s[..idx], suffix, &s[end..]);
            }
        }
        return s.to_string();
    }
    if pat.starts_with("LightSourcess?LightSource") {
        let r = s.replace("LightSourcessLightSourceSettingsLightSource", "LightSource");
        let r = r.replace("LightSourcessLightSourceSettings", "LightSource");
        let r = r.replace("LightSourcessLightSourceLightSource", "LightSource");
        let r = r.replace("LightSourcessLightSource", "LightSource");
        let r = r.replace("LightSourcesLightSourceSettingsLightSource", "LightSource");
        let r = r.replace("LightSourcesLightSourceSettings", "LightSource");
        let r = r.replace("LightSourcesLightSourceLightSource", "LightSource");
        let r = r.replace("LightSourcesLightSource", "LightSource");
        return r;
    }
    if pat.starts_with("TimeSeries(TimeSeries|Setups)") {
        let r = s.replace("TimeSeriesTimeSeries", "TimeSeries");
        let r = r.replace("TimeSeriesSetups", "TimeSeries");
        return r;
    }
    if pat.starts_with("(ApoTomeDepthInfo)+Element") {
        let mut r = s.to_string();
        loop {
            let prev = r.clone();
            r = r.replace("ApoTomeDepthInfoApoTomeDepthInfoElement", "ApoTomeDepthInfoElement");
            if r == prev { break; }
        }
        r = r.replace("ApoTomeDepthInfoElement", "ApoTomeDepth");
        return r;
    }
    if pat.starts_with("(ApoTomeClickStopInfo)+Element") {
        let mut r = s.to_string();
        r = r.replace("ApoTomeClickStopInfoElement", "ApoTomeClickStop");
        return r;
    }
    if pat.starts_with("(ParfocalParcentralValues?)+") {
        let r = s.replace("ParfocalParcentralValuesParfocalParcentralValue", "Parcentral");
        let r = r.replace("ParfocalParcentralValueParfocalParcentralValue", "Parcentral");
        let r = r.replace("ParfocalParcentralValue", "Parcentral");
        return r;
    }
    // Default: no-op for unhandled patterns
    s.to_string()
}

fn hex_encode(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{:02x}", b)).collect()
}

// ============================================================================
// RealMedia (.rm, .rv, .rmvb)
// ============================================================================

pub fn read_real_media(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 8 || !data.starts_with(b".RMF") {
        return Err(Error::InvalidData("not a RealMedia file".into()));
    }

    let mut tags = Vec::new();

    // Skip .RMF header (size at bytes 4..8)
    if data.len() < 8 { return Ok(tags); }
    let hdr_size = u32::from_be_bytes([data[4], data[5], data[6], data[7]]) as usize;
    let hdr_size = hdr_size.max(8);
    let mut pos = hdr_size;
    let mut first_mdpr = true;

    // Look for RJMD at specific position based on RMJE footer
    let rjmd_data_opt = real_find_rjmd(data);

    // Process chunks
    while pos + 10 <= data.len() {
        let chunk_id = &data[pos..pos+4];
        if chunk_id == b"\x00\x00\x00\x00" { break; }
        let chunk_size = u32::from_be_bytes([data[pos+4], data[pos+5], data[pos+6], data[pos+7]]) as usize;
        if chunk_size < 10 || pos + chunk_size > data.len() { break; }
        if chunk_id == b"DATA" { break; }

        let chunk_data = &data[pos+10..pos+chunk_size];

        match chunk_id {
            b"PROP" => real_parse_prop(chunk_data, &mut tags),
            b"MDPR" => {
                real_parse_mdpr(chunk_data, &mut tags, first_mdpr);
                first_mdpr = false;
            }
            b"CONT" => real_parse_cont(chunk_data, &mut tags),
            _ => {}
        }

        pos += chunk_size;
    }

    // Process RJMD metadata
    if let Some(rjmd_data) = rjmd_data_opt {
        real_parse_rjmd(&rjmd_data, &mut tags);
    }

    // Check for ID3v1 at last 128 bytes
    if data.len() >= 128 && data[data.len()-128..data.len()-125] == *b"TAG" {
        let id3_data = &data[data.len()-128..];
        real_parse_id3v1(id3_data, &mut tags);
    }

    Ok(tags)
}

fn real_find_rjmd(data: &[u8]) -> Option<Vec<u8>> {
    // Perl: seek(-140, 2) read 12 bytes, check for "RMJE"
    if data.len() < 140 { return None; }
    let rmje_pos = data.len() - 140;
    if &data[rmje_pos..rmje_pos+4] != b"RMJE" { return None; }
    let meta_size = u32::from_be_bytes([data[rmje_pos+8], data[rmje_pos+9], data[rmje_pos+10], data[rmje_pos+11]]) as usize;
    // RJMD starts at rmje_pos - meta_size
    if meta_size > rmje_pos { return None; }
    let rjmd_start = rmje_pos - meta_size;
    if rjmd_start + 4 > data.len() || &data[rjmd_start..rjmd_start+4] != b"RJMD" { return None; }
    Some(data[rjmd_start..rjmd_start+meta_size].to_vec())
}

fn real_parse_prop(data: &[u8], tags: &mut Vec<Tag>) {
    if data.len() < 40 { return; }
    let mut off = 0usize;
    let max_bitrate = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let avg_bitrate = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let max_pkt = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let avg_pkt = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let num_pkts = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let duration_ms = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let preroll_ms = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    off += 4; // index offset (unknown)
    off += 4; // data offset (unknown)
    if data.len() < off + 4 { return; }
    let num_streams = u16::from_be_bytes([data[off], data[off+1]]); off += 2;
    if data.len() < off + 2 { return; }
    let flags = u16::from_be_bytes([data[off], data[off+1]]);

    tags.push(mktag("Real", "MaxBitrate", "Max Bitrate", Value::String(real_convert_bitrate(max_bitrate as f64))));
    tags.push(mktag("Real", "AvgBitrate", "Avg Bitrate", Value::String(real_convert_bitrate(avg_bitrate as f64))));
    tags.push(mktag("Real", "MaxPacketSize", "Max Packet Size", Value::U32(max_pkt)));
    tags.push(mktag("Real", "AvgPacketSize", "Avg Packet Size", Value::U32(avg_pkt)));
    tags.push(mktag("Real", "NumPackets", "Num Packets", Value::U32(num_pkts)));
    // Duration: ms / 1000, then ConvertDuration
    let dur_secs = duration_ms as f64 / 1000.0;
    tags.push(mktag("Real", "Duration", "Duration", Value::String(real_convert_duration(dur_secs))));
    let preroll_secs = preroll_ms as f64 / 1000.0;
    tags.push(mktag("Real", "Preroll", "Preroll", Value::String(real_convert_duration(preroll_secs))));
    tags.push(mktag("Real", "NumStreams", "Num Streams", Value::U16(num_streams)));

    // Flags BITMASK
    let mut flag_strs = Vec::new();
    if flags & 0x01 != 0 { flag_strs.push("Allow Recording"); }
    if flags & 0x02 != 0 { flag_strs.push("Perfect Play"); }
    if flags & 0x04 != 0 { flag_strs.push("Live"); }
    if flags & 0x08 != 0 { flag_strs.push("Allow Download"); }
    if !flag_strs.is_empty() {
        tags.push(mktag("Real", "Flags", "Flags", Value::String(flag_strs.join(", "))));
    }
}

fn real_parse_mdpr(data: &[u8], tags: &mut Vec<Tag>, is_first: bool) {
    if data.len() < 30 { return; }
    let mut off = 0usize;
    let stream_num = u16::from_be_bytes([data[off], data[off+1]]); off += 2;
    let max_bitrate = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let avg_bitrate = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let max_pkt = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let avg_pkt = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let start_time = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let preroll_ms = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    let duration_ms = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    if off >= data.len() { return; }
    let name_len = data[off] as usize; off += 1;
    if off + name_len > data.len() { return; }
    let stream_name = String::from_utf8_lossy(&data[off..off+name_len]).to_string(); off += name_len;
    if off >= data.len() { return; }
    let mime_len = data[off] as usize; off += 1;
    if off + mime_len > data.len() { return; }
    let mime_type = String::from_utf8_lossy(&data[off..off+mime_len]).to_string(); off += mime_len;

    // Only emit stream info for first non-logical stream (Perl PRIORITY => 0 = first takes priority)
    if is_first {
        tags.push(mktag("Real", "StreamNumber", "Stream Number", Value::U16(stream_num)));
        tags.push(mktag("Real", "StreamMaxBitrate", "Stream Max Bitrate", Value::String(real_convert_bitrate(max_bitrate as f64))));
        tags.push(mktag("Real", "StreamAvgBitrate", "Stream Avg Bitrate", Value::String(real_convert_bitrate(avg_bitrate as f64))));
        tags.push(mktag("Real", "StreamMaxPacketSize", "Stream Max Packet Size", Value::U32(max_pkt)));
        tags.push(mktag("Real", "StreamAvgPacketSize", "Stream Avg Packet Size", Value::U32(avg_pkt)));
        tags.push(mktag("Real", "StreamStartTime", "Stream Start Time", Value::U32(start_time)));
        let preroll_secs = preroll_ms as f64 / 1000.0;
        tags.push(mktag("Real", "StreamPreroll", "Stream Preroll", Value::String(real_convert_duration(preroll_secs))));
        let dur_secs = duration_ms as f64 / 1000.0;
        tags.push(mktag("Real", "StreamDuration", "Stream Duration", Value::String(real_convert_duration(dur_secs))));
        tags.push(mktag("Real", "StreamName", "Stream Name", Value::String(stream_name)));
        tags.push(mktag("Real", "StreamMimeType", "Stream Mime Type", Value::String(mime_type.clone())));
    }

    // Check for logical-fileinfo stream
    if mime_type == "logical-fileinfo" && off + 12 <= data.len() {
        real_parse_fileinfo(&data[off..], tags);
    }
}

fn real_parse_fileinfo(data: &[u8], tags: &mut Vec<Tag>) {
    // FileInfoLen (tag 12, 4 bytes) = type_spec_size,
    // FileInfoLen2 (tag 13, 4 bytes) = first 4 bytes of type_spec_data,
    // FileInfoVersion (tag 14, 2 bytes), PhysicalStreams (tag 15, 2 bytes),
    // [stream_nums(2)*N + data_offsets(4)*N], num_rules(2), [rule_nums(2)*N], num_props(2)
    if data.len() < 12 { return; }
    let mut off = 0usize;
    // FileInfoLen (tag 12): type_spec_size
    let _file_info_len = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    // FileInfoLen2 (tag 13): first 4 bytes of type_spec_data (conditional on logical-fileinfo)
    let _file_info_len2 = u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]]); off += 4;
    // FileInfoVersion (tag 14): int16u
    let fi_ver = u16::from_be_bytes([data[off], data[off+1]]); off += 2;
    // PhysicalStreams (tag 15): int16u
    let phys_streams = u16::from_be_bytes([data[off], data[off+1]]) as usize; off += 2;
    // Skip physical stream numbers (2 bytes each) and data offsets (4 bytes each)
    off += phys_streams * 2 + phys_streams * 4;
    if off + 2 > data.len() { return; }
    let num_rules = u16::from_be_bytes([data[off], data[off+1]]) as usize; off += 2;
    // Skip rule map
    off += num_rules * 2;
    if off + 2 > data.len() { return; }
    let _num_props = u16::from_be_bytes([data[off], data[off+1]]); off += 2;

    tags.push(mktag("Real", "FileInfoVersion", "File Info Version", Value::U16(fi_ver)));

    // Now parse FileInfoProperties
    real_parse_properties(&data[off..], tags);
}

fn real_parse_properties(data: &[u8], tags: &mut Vec<Tag>) {
    let mut pos = 0usize;
    while pos + 7 <= data.len() {
        let p_start = pos;
        let p_size = u32::from_be_bytes([data[pos], data[pos+1], data[pos+2], data[pos+3]]) as usize;
        let p_ver = u16::from_be_bytes([data[pos+4], data[pos+5]]);
        if p_size < 7 || p_start + p_size > data.len() { break; }
        if p_ver != 0 { pos = p_start + p_size; continue; }
        pos += 6;

        let tag_len = data[pos] as usize; pos += 1;
        if pos + tag_len > data.len() { break; }
        let tag_name = String::from_utf8_lossy(&data[pos..pos+tag_len]).to_string(); pos += tag_len;

        if pos + 6 > data.len() { break; }
        let prop_type = u32::from_be_bytes([data[pos], data[pos+1], data[pos+2], data[pos+3]]); pos += 4;
        let val_len = u16::from_be_bytes([data[pos], data[pos+1]]) as usize; pos += 2;
        if pos + val_len > data.len() { break; }
        let val_data = &data[pos..pos+val_len];

        let (exif_name, val_str) = real_file_info_tag(&tag_name, prop_type, val_data, val_len);
        if let Some(val) = val_str {
            if !exif_name.is_empty() {
                tags.push(mktag("Real", &exif_name, &exif_name, Value::String(val)));
            }
        }

        pos = p_start + p_size;
    }
}

fn real_file_info_tag(tag: &str, prop_type: u32, val_data: &[u8], val_len: usize) -> (String, Option<String>) {
    let tag_name = match tag {
        "Content Rating"    => "ContentRating",
        "Audiences"         => "Audiences",
        "audioMode"         => "AudioMode",
        "Creation Date"     => "CreateDate",
        "Generated By"      => "Software",
        "Modification Date" => "ModifyDate",
        "videoMode"         => "VideoMode",
        "Description"       => "Description",
        "Keywords"          => "Keywords",
        "Indexable"         => "Indexable",
        "File ID"           => "FileID",
        "Target Audiences"  => "TargetAudiences",
        "Audio Format"      => "AudioFormat",
        "Video Quality"     => "VideoQuality",
        _ => {
            // Remove spaces, ucfirst
            let s: String = tag.split_whitespace().collect::<Vec<_>>().join("");
            return (ucfirst_first_char(&s), real_parse_prop_value(tag, prop_type, val_data, val_len));
        }
    };

    let val = match tag_name {
        "ContentRating" => {
            if prop_type == 0 && val_len >= 4 {
                let v = u32::from_be_bytes([val_data[0], val_data[1], val_data[2], val_data[3]]);
                Some(match v {
                    0 => "No Rating".to_string(),
                    1 => "All Ages".to_string(),
                    2 => "Older Children".to_string(),
                    3 => "Younger Teens".to_string(),
                    4 => "Older Teens".to_string(),
                    5 => "Adult Supervision Recommended".to_string(),
                    6 => "Adults Only".to_string(),
                    _ => format!("{}", v),
                })
            } else { None }
        }
        "CreateDate" | "ModifyDate" => {
            // Convert "D/M/YYYY H:MM:SS" to "YYYY:MM:DD HH:MM:SS"
            if prop_type == 2 {
                let s = String::from_utf8_lossy(val_data).trim_matches('\0').to_string();
                Some(real_parse_date(&s))
            } else { None }
        }
        _ => real_parse_prop_value(tag_name, prop_type, val_data, val_len)
    };

    (tag_name.to_string(), val)
}

fn real_parse_prop_value(tag: &str, prop_type: u32, val_data: &[u8], val_len: usize) -> Option<String> {
    let _ = tag;
    if val_len == 0 { return Some(String::new()); }
    match prop_type {
        0 => { // int32u
            if val_len >= 4 {
                Some(format!("{}", u32::from_be_bytes([val_data[0], val_data[1], val_data[2], val_data[3]])))
            } else { None }
        }
        2 => { // string
            let s = String::from_utf8_lossy(val_data).trim_matches('\0').to_string();
            Some(s)
        }
        _ => None
    }
}

fn real_parse_date(s: &str) -> String {
    // Parse "D/M/YYYY H:MM:SS" or "DD/MM/YYYY HH:MM:SS"
    let parts: Vec<&str> = s.split(|c| c == '/' || c == ' ' || c == ':').collect();
    if parts.len() >= 6 {
        let day: u32 = parts[0].parse().unwrap_or(0);
        let month: u32 = parts[1].parse().unwrap_or(0);
        let year: u32 = parts[2].parse().unwrap_or(0);
        let hour: u32 = parts[3].parse().unwrap_or(0);
        let min: u32 = parts[4].parse().unwrap_or(0);
        let sec: u32 = parts[5].parse().unwrap_or(0);
        format!("{:04}:{:02}:{:02} {:02}:{:02}:{:02}", year, month, day, hour, min, sec)
    } else {
        s.to_string()
    }
}

fn real_parse_cont(data: &[u8], tags: &mut Vec<Tag>) {
    if data.len() < 8 { return; }
    let mut off = 0usize;

    let title_len = u16::from_be_bytes([data[off], data[off+1]]) as usize; off += 2;
    if off + title_len > data.len() { return; }
    let title = String::from_utf8_lossy(&data[off..off+title_len]).to_string(); off += title_len;

    if off + 2 > data.len() { return; }
    let author_len = u16::from_be_bytes([data[off], data[off+1]]) as usize; off += 2;
    if off + author_len > data.len() { return; }
    let author = String::from_utf8_lossy(&data[off..off+author_len]).to_string(); off += author_len;

    if off + 2 > data.len() { return; }
    let copyright_len = u16::from_be_bytes([data[off], data[off+1]]) as usize; off += 2;
    if off + copyright_len > data.len() { return; }
    let copyright = String::from_utf8_lossy(&data[off..off+copyright_len]).to_string(); off += copyright_len;

    if off + 2 > data.len() { return; }
    let comment_len = u16::from_be_bytes([data[off], data[off+1]]) as usize; off += 2;
    if off + comment_len > data.len() { return; }
    let comment = String::from_utf8_lossy(&data[off..off+comment_len]).to_string();

    if !title.is_empty() { tags.push(mktag("Real", "Title", "Title", Value::String(title))); }
    if !author.is_empty() { tags.push(mktag("Real", "Author", "Author", Value::String(author))); }
    if !copyright.is_empty() { tags.push(mktag("Real", "Copyright", "Copyright", Value::String(copyright))); }
    if !comment.is_empty() { tags.push(mktag("Real", "Comment", "Comment", Value::String(comment))); }
}

fn real_parse_rjmd(data: &[u8], tags: &mut Vec<Tag>) {
    // data starts with RJMD header: RJMD(4) + version(4) + data_len(4) + metadata
    // ProcessRealMeta DirStart=8, DirLen=data.len()-8
    if data.len() < 8 { return; }
    let dir_start = 8usize;
    let dir_end = data.len();
    real_parse_rjmd_entries(data, dir_start, dir_end, "", tags);
}

fn real_parse_rjmd_entries(data: &[u8], pos_start: usize, dir_end: usize, prefix: &str, tags: &mut Vec<Tag>) {
    let mut pos = pos_start;
    while pos + 28 <= dir_end {
        if pos + 28 > data.len() { break; }
        let entry_size = u32::from_be_bytes([data[pos], data[pos+1], data[pos+2], data[pos+3]]) as usize;
        let entry_type = u32::from_be_bytes([data[pos+4], data[pos+5], data[pos+6], data[pos+7]]);
        let _flags = u32::from_be_bytes([data[pos+8], data[pos+9], data[pos+10], data[pos+11]]);
        let value_pos_rel = u32::from_be_bytes([data[pos+12], data[pos+13], data[pos+14], data[pos+15]]) as usize;
        let _sub_pos_rel = u32::from_be_bytes([data[pos+16], data[pos+17], data[pos+18], data[pos+19]]) as usize;
        let num_sub = u32::from_be_bytes([data[pos+20], data[pos+21], data[pos+22], data[pos+23]]) as usize;
        let name_len = u32::from_be_bytes([data[pos+24], data[pos+25], data[pos+26], data[pos+27]]) as usize;

        if entry_size < 28 { break; }
        if pos + entry_size > dir_end { break; }
        if pos + 28 + name_len > dir_end { break; }

        let name_bytes = &data[pos+28..pos+28+name_len];
        let name = String::from_utf8_lossy(name_bytes).split('\0').next().unwrap_or("").to_string();

        let full_name = if prefix.is_empty() {
            name.clone()
        } else {
            format!("{}/{}", prefix, name)
        };

        let value_pos = value_pos_rel + pos;
        if value_pos + 4 <= dir_end {
            let value_len = u32::from_be_bytes([data[value_pos], data[value_pos+1], data[value_pos+2], data[value_pos+3]]) as usize;
            let value_start = value_pos + 4;
            if value_start + value_len <= dir_end && entry_type != 9 && entry_type != 10 {
                // Emit value
                let val_data = &data[value_start..value_start+value_len];
                let val_str = match entry_type {
                    1 | 2 | 6 | 7 | 8 => {
                        // string/text/url/date/filename
                        let s = String::from_utf8_lossy(val_data).trim_matches('\0').to_string();
                        let s = s.trim_end().to_string();
                        if entry_type == 7 {
                            // date: YYYYMMDDHHMMSS format
                            real_parse_rjmd_date(&s)
                        } else {
                            s
                        }
                    }
                    4 => { // int32u
                        if value_len >= 4 {
                            format!("{}", u32::from_be_bytes([val_data[0], val_data[1], val_data[2], val_data[3]]))
                        } else { String::new() }
                    }
                    3 => { // flag
                        if value_len == 4 {
                            format!("{}", u32::from_be_bytes([val_data[0], val_data[1], val_data[2], val_data[3]]))
                        } else if value_len >= 1 {
                            format!("{}", val_data[0])
                        } else { String::new() }
                    }
                    _ => String::new()
                };

                if !full_name.is_empty() {
                    let tag_name = real_rjmd_tag_name(&full_name);
                    if !tag_name.is_empty() {
                        tags.push(mktag("Real", &tag_name, &tag_name, Value::String(val_str)));
                    }
                }

            }

            // Process sub-properties
            if num_sub > 0 {
                let sub_dir_start = value_pos + 4 + value_len + num_sub * 8;
                let sub_dir_len = pos + entry_size - sub_dir_start;
                if sub_dir_start + sub_dir_len <= dir_end && sub_dir_len > 0 {
                    real_parse_rjmd_entries(data, sub_dir_start, sub_dir_start + sub_dir_len, &full_name, tags);
                }
            }
        }

        pos += entry_size;
    }
}

fn real_parse_rjmd_date(s: &str) -> String {
    // Format: YYYYMMDDHHMMSS
    if s.len() >= 14 {
        format!("{}:{}:{} {}:{}:{}", &s[..4], &s[4..6], &s[6..8], &s[8..10], &s[10..12], &s[12..14])
    } else {
        s.to_string()
    }
}

fn real_rjmd_tag_name(full_name: &str) -> String {
    // Map RJMD path names to ExifTool tag names
    // Perl table entries:
    // 'Album/Name' => 'AlbumName'
    // 'Track/Category' => 'TrackCategory'
    // etc.
    match full_name {
        "Album/Name"        => "AlbumName".to_string(),
        "Track/Category"    => "TrackCategory".to_string(),
        "Track/Comments"    => "TrackComments".to_string(),
        "Track/Lyrics"      => "TrackLyrics".to_string(),
        _ => {
            // Auto-generate: strip /, remove spaces, ucfirst each component
            let clean: String = full_name.split('/')
                .filter(|s| !s.is_empty())
                .map(|s| {
                    // Remove spaces and capitalize
                    let cleaned: String = s.chars().filter(|&c| c.is_alphanumeric() || c == '_').collect();
                    ucfirst_first_char(&cleaned)
                })
                .collect();
            clean
        }
    }
}

fn ucfirst_first_char(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(c) => {
            let upper: String = c.to_uppercase().collect();
            upper + chars.as_str()
        }
    }
}

fn real_parse_id3v1(data: &[u8], tags: &mut Vec<Tag>) {
    // Simple ID3v1 parser (128 bytes starting with "TAG")
    // PRIORITY => 0 means these tags don't override already-set tags
    if data.len() < 128 || &data[..3] != b"TAG" { return; }

    // Title: bytes 3..33 (30 bytes)
    let title = read_null_terminated_str(&data[3..33]);
    // Artist: bytes 33..63
    let artist = read_null_terminated_str(&data[33..63]);
    // Album: bytes 63..93
    let album = read_null_terminated_str(&data[63..93]);
    // Year: bytes 93..97
    let year = read_null_terminated_str(&data[93..97]);
    // Comment: bytes 97..127 (but bytes 125-126 may be Track for ID3v1.1)
    let comment = if data[125] == 0 && data[126] != 0 {
        // ID3v1.1: track number in byte 126, comment is bytes 97..125
        read_null_terminated_str(&data[97..125])
    } else {
        read_null_terminated_str(&data[97..127])
    };
    // Genre: byte 127
    let genre_byte = data[127] as usize;

    // Collect new tags, skipping those already present (PRIORITY => 0 behavior)
    let mut new_tags: Vec<Tag> = Vec::new();
    if !title.is_empty() && !tags.iter().any(|t| t.name == "Title") {
        new_tags.push(mktag("ID3", "Title", "Title", Value::String(title)));
    }
    if !artist.is_empty() && !tags.iter().any(|t| t.name == "Artist") {
        new_tags.push(mktag("ID3", "Artist", "Artist", Value::String(artist)));
    }
    if !album.is_empty() && !tags.iter().any(|t| t.name == "Album") {
        new_tags.push(mktag("ID3", "Album", "Album", Value::String(album)));
    }
    if !year.is_empty() && !tags.iter().any(|t| t.name == "Year") {
        new_tags.push(mktag("ID3", "Year", "Year", Value::String(year)));
    }
    if !comment.is_empty() && !tags.iter().any(|t| t.name == "Comment") {
        new_tags.push(mktag("ID3", "Comment", "Comment", Value::String(comment)));
    }
    if let Some(genre_name) = id3v1_genre_name(genre_byte) {
        if !tags.iter().any(|t| t.name == "Genre") {
            new_tags.push(mktag("ID3", "Genre", "Genre", Value::String(genre_name.to_string())));
        }
    }
    tags.extend(new_tags);
}

fn id3v1_genre_name(n: usize) -> Option<&'static str> {
    match n {
        0 => Some("Blues"), 1 => Some("Classic Rock"), 2 => Some("Country"),
        3 => Some("Dance"), 4 => Some("Disco"), 5 => Some("Funk"),
        6 => Some("Grunge"), 7 => Some("Hip-Hop"), 8 => Some("Jazz"),
        9 => Some("Metal"), 10 => Some("New Age"), 11 => Some("Oldies"),
        12 => Some("Other"), 13 => Some("Pop"), 14 => Some("R&B"),
        15 => Some("Rap"), 16 => Some("Reggae"), 17 => Some("Rock"),
        18 => Some("Techno"), 19 => Some("Industrial"), 20 => Some("Alternative"),
        21 => Some("Ska"), 22 => Some("Death Metal"), 23 => Some("Pranks"),
        24 => Some("Soundtrack"), 25 => Some("Euro-Techno"), 26 => Some("Ambient"),
        27 => Some("Trip-Hop"), 28 => Some("Vocal"), 29 => Some("Jazz+Funk"),
        30 => Some("Fusion"), 31 => Some("Trance"), 32 => Some("Classical"),
        33 => Some("Instrumental"), 34 => Some("Acid"), 35 => Some("House"),
        36 => Some("Game"), 37 => Some("Sound Clip"), 38 => Some("Gospel"),
        39 => Some("Noise"), 40 => Some("Alt. Rock"), 41 => Some("Bass"),
        42 => Some("Soul"), 43 => Some("Punk"), 44 => Some("Space"),
        45 => Some("Meditative"), 46 => Some("Instrumental Pop"),
        47 => Some("Instrumental Rock"), 48 => Some("Ethnic"), 49 => Some("Gothic"),
        50 => Some("Darkwave"), 51 => Some("Techno-Industrial"), 52 => Some("Electronic"),
        53 => Some("Pop-Folk"), 54 => Some("Eurodance"), 55 => Some("Dream"),
        56 => Some("Southern Rock"), 57 => Some("Comedy"), 58 => Some("Cult"),
        59 => Some("Gangsta Rap"), 60 => Some("Top 40"), 61 => Some("Christian Rap"),
        62 => Some("Pop/Funk"), 63 => Some("Jungle"), 64 => Some("Native American"),
        65 => Some("Cabaret"), 66 => Some("New Wave"), 67 => Some("Psychedelic"),
        68 => Some("Rave"), 69 => Some("Showtunes"), 70 => Some("Trailer"),
        71 => Some("Lo-Fi"), 72 => Some("Tribal"), 73 => Some("Acid Punk"),
        74 => Some("Acid Jazz"), 75 => Some("Polka"), 76 => Some("Retro"),
        77 => Some("Musical"), 78 => Some("Rock & Roll"), 79 => Some("Hard Rock"),
        80 => Some("Folk"), 81 => Some("Folk-Rock"), 82 => Some("National Folk"),
        83 => Some("Swing"), 84 => Some("Fast-Fusion"), 85 => Some("Bebop"),
        86 => Some("Latin"), 87 => Some("Revival"), 88 => Some("Celtic"),
        89 => Some("Bluegrass"), 90 => Some("Avantgarde"), 91 => Some("Gothic Rock"),
        92 => Some("Progressive Rock"), 93 => Some("Psychedelic Rock"),
        94 => Some("Symphonic Rock"), 95 => Some("Slow Rock"), 96 => Some("Big Band"),
        97 => Some("Chorus"), 98 => Some("Easy Listening"), 99 => Some("Acoustic"),
        100 => Some("Humour"), 101 => Some("Speech"), 102 => Some("Chanson"),
        103 => Some("Opera"), 104 => Some("Chamber Music"), 105 => Some("Sonata"),
        106 => Some("Symphony"), 107 => Some("Booty Bass"), 108 => Some("Primus"),
        109 => Some("Porn Groove"), 110 => Some("Satire"), 111 => Some("Slow Jam"),
        112 => Some("Club"), 113 => Some("Tango"), 114 => Some("Samba"),
        115 => Some("Folklore"), 116 => Some("Ballad"), 117 => Some("Power Ballad"),
        118 => Some("Rhythmic Soul"), 119 => Some("Freestyle"), 120 => Some("Duet"),
        121 => Some("Punk Rock"), 122 => Some("Drum Solo"), 123 => Some("A Cappella"),
        124 => Some("Euro-House"), 125 => Some("Dance Hall"), 126 => Some("Goa"),
        127 => Some("Drum & Bass"), 128 => Some("Club-House"), 129 => Some("Hardcore"),
        130 => Some("Terror"), 131 => Some("Indie"), 132 => Some("BritPop"),
        133 => Some("Afro-Punk"), 134 => Some("Polsk Punk"), 135 => Some("Beat"),
        136 => Some("Christian Gangsta Rap"), 137 => Some("Heavy Metal"),
        138 => Some("Black Metal"), 139 => Some("Crossover"),
        140 => Some("Contemporary Christian"), 141 => Some("Christian Rock"),
        142 => Some("Merengue"), 143 => Some("Salsa"), 144 => Some("Thrash Metal"),
        145 => Some("Anime"), 146 => Some("JPop"), 147 => Some("Synthpop"),
        148 => Some("Abstract"), 149 => Some("Art Rock"), 150 => Some("Baroque"),
        151 => Some("Bhangra"), 152 => Some("Big Beat"), 153 => Some("Breakbeat"),
        154 => Some("Chillout"), 155 => Some("Downtempo"), 156 => Some("Dub"),
        157 => Some("EBM"), 158 => Some("Eclectic"), 159 => Some("Electro"),
        160 => Some("Electroclash"), 161 => Some("Emo"), 162 => Some("Experimental"),
        163 => Some("Garage"), 164 => Some("Global"), 165 => Some("IDM"),
        166 => Some("Illbient"), 167 => Some("Industro-Goth"), 168 => Some("Jam Band"),
        169 => Some("Krautrock"), 170 => Some("Leftfield"), 171 => Some("Lounge"),
        172 => Some("Math Rock"), 173 => Some("New Romantic"), 174 => Some("Nu-Breakz"),
        175 => Some("Post-Punk"), 176 => Some("Post-Rock"), 177 => Some("Psytrance"),
        178 => Some("Shoegaze"), 179 => Some("Space Rock"), 180 => Some("Trop Rock"),
        181 => Some("World Music"), 182 => Some("Neoclassical"), 183 => Some("Audiobook"),
        184 => Some("Audio Theatre"), 185 => Some("Neue Deutsche Welle"),
        186 => Some("Podcast"), 187 => Some("Indie Rock"), 188 => Some("G-Funk"),
        189 => Some("Dubstep"), 190 => Some("Garage Rock"), 191 => Some("Psybient"),
        255 => Some("None"),
        _ => None,
    }
}

fn read_null_terminated_str(bytes: &[u8]) -> String {
    let end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
    String::from_utf8_lossy(&bytes[..end]).trim().to_string()
}

fn real_convert_bitrate(bps: f64) -> String {
    flv_convert_bitrate(bps)
}

fn real_convert_duration(secs: f64) -> String {
    flv_convert_duration(secs)
}

// ============================================================================
// PhotoCD (Kodak Photo CD Image Pac)
// ============================================================================

pub fn read_photo_cd(data: &[u8]) -> Result<Vec<Tag>> {
    // PCD magic at byte 2048: "PCD_IPI"
    if data.len() < 2056 || &data[2048..2055] != b"PCD_IPI" {
        return Err(Error::InvalidData("not a PhotoCD file".into()));
    }
    let pcd = &data[2048..]; // PCD block (2048+ bytes)
    if pcd.len() < 1540 {
        return Err(Error::InvalidData("PhotoCD data too short".into()));
    }

    let mut tags: Vec<Tag> = Vec::new();

    // Byte 7: SpecificationVersion (int8u[2])
    let sv = pcd[7];
    let sv2 = pcd[8];
    if sv != 255 || sv2 != 255 {
        tags.push(mktag("PhotoCD", "SpecificationVersion", "Specification Version",
            Value::String(format!("{}.{}", sv, sv2))));
    }

    // Byte 9: AuthoringSoftwareRelease (int8u[2])
    let ar = pcd[9];
    let ar2 = pcd[10];
    if ar != 255 || ar2 != 255 {
        tags.push(mktag("PhotoCD", "AuthoringSoftwareRelease", "Authoring Software Release",
            Value::String(format!("{}.{}", ar, ar2))));
    }

    // Byte 11: ImageMagnificationDescriptor (int8u[2])
    let im1 = pcd[11];
    let im2 = pcd[12];
    tags.push(mktag("PhotoCD", "ImageMagnificationDescriptor", "Image Magnification Descriptor",
        Value::String(format!("{}.{}", im1, im2))));

    // Byte 13: CreateDate (int32u BE, unix time)
    if pcd.len() >= 17 {
        let ts = u32::from_be_bytes([pcd[13], pcd[14], pcd[15], pcd[16]]);
        if ts != 0xffffffff {
            let dt = gzip_unix_to_datetime(ts as i64);
            tags.push(mktag("PhotoCD", "CreateDate", "Create Date", Value::String(dt)));
        }
    }

    // Byte 17: ModifyDate (int32u BE, unix time)
    if pcd.len() >= 21 {
        let ts = u32::from_be_bytes([pcd[17], pcd[18], pcd[19], pcd[20]]);
        if ts != 0xffffffff {
            let dt = gzip_unix_to_datetime(ts as i64);
            tags.push(mktag("PhotoCD", "ModifyDate", "Modify Date", Value::String(dt)));
        }
    }

    // Byte 21: ImageMedium
    let medium = pcd[21];
    let medium_str = match medium {
        0 => "Color negative",
        1 => "Color reversal",
        2 => "Color hard copy",
        3 => "Thermal hard copy",
        4 => "Black and white negative",
        5 => "Black and white reversal",
        6 => "Black and white hard copy",
        7 => "Internegative",
        8 => "Synthetic image",
        _ => "",
    };
    if !medium_str.is_empty() {
        tags.push(mktag("PhotoCD", "ImageMedium", "Image Medium", Value::String(medium_str.into())));
    }

    // Byte 22: ProductType (string[20])
    if pcd.len() >= 42 {
        let s = pcd_rtrim_str(&pcd[22..42]);
        if !s.is_empty() {
            tags.push(mktag("PhotoCD", "ProductType", "Product Type", Value::String(s)));
        }
    }

    // Byte 42: ScannerVendorID (string[20])
    if pcd.len() >= 62 {
        let s = pcd_rtrim_str(&pcd[42..62]);
        if !s.is_empty() {
            tags.push(mktag("PhotoCD", "ScannerVendorID", "Scanner Vendor ID", Value::String(s)));
        }
    }

    // Byte 62: ScannerProductID (string[16])
    if pcd.len() >= 78 {
        let s = pcd_rtrim_str(&pcd[62..78]);
        if !s.is_empty() {
            tags.push(mktag("PhotoCD", "ScannerProductID", "Scanner Product ID", Value::String(s)));
        }
    }

    // Byte 78: ScannerFirmwareVersion (string[4])
    if pcd.len() >= 82 {
        let s = pcd_rtrim_str(&pcd[78..82]);
        if !s.is_empty() {
            tags.push(mktag("PhotoCD", "ScannerFirmwareVersion", "Scanner Firmware Version", Value::String(s)));
        }
    }

    // Byte 82: ScannerFirmwareDate (string[8])
    if pcd.len() >= 90 {
        let s = pcd_rtrim_str(&pcd[82..90]);
        // Always emit (even if empty string)
        tags.push(mktag("PhotoCD", "ScannerFirmwareDate", "Scanner Firmware Date", Value::String(s)));
    }

    // Byte 90: ScannerSerialNumber (string[20])
    if pcd.len() >= 110 {
        let s = pcd_rtrim_str(&pcd[90..110]);
        if !s.is_empty() {
            tags.push(mktag("PhotoCD", "ScannerSerialNumber", "Scanner Serial Number", Value::String(s)));
        }
    }

    // Byte 110: ScannerPixelSize (undef[2]) - hex nibbles joined with '.'
    if pcd.len() >= 112 {
        let h1 = pcd[110];
        let h2 = pcd[111];
        let pixel_size = format!("{:02x}.{:02x}", h1, h2).trim_start_matches('0').to_string();
        let pixel_size = if pixel_size.starts_with('.') {
            format!("0{}", pixel_size)
        } else {
            pixel_size
        };
        tags.push(mktag("PhotoCD", "ScannerPixelSize", "Scanner Pixel Size",
            Value::String(format!("{} micrometers", pixel_size))));
    }

    // Byte 112: ImageWorkstationMake (string[20])
    if pcd.len() >= 132 {
        let s = pcd_rtrim_str(&pcd[112..132]);
        if !s.is_empty() {
            tags.push(mktag("PhotoCD", "ImageWorkstationMake", "Image Workstation Make", Value::String(s)));
        }
    }

    // Byte 132: CharacterSet
    if pcd.len() >= 133 {
        let cs = pcd[132];
        let cs_str = match cs {
            1 => "38 characters ISO 646",
            2 => "65 characters ISO 646",
            3 => "95 characters ISO 646",
            4 => "191 characters ISO 8850-1",
            5 => "ISO 2022",
            6 => "Includes characters not ISO 2375 registered",
            _ => "",
        };
        if !cs_str.is_empty() {
            tags.push(mktag("PhotoCD", "CharacterSet", "Character Set", Value::String(cs_str.into())));
        }
    }

    // Byte 165: PhotoFinisherName (string[60])
    if pcd.len() >= 225 {
        let s = pcd_rtrim_str(&pcd[165..225]);
        if !s.is_empty() {
            tags.push(mktag("PhotoCD", "PhotoFinisherName", "Photo Finisher Name", Value::String(s)));
        }
    }

    // Check for SBA marker at bytes 225-228
    let has_sba = pcd.len() >= 228 && &pcd[225..228] == b"SBA";

    if has_sba && pcd.len() >= 230 {
        // Byte 228: SceneBalanceAlgorithmRevision (int8u[2])
        let r1 = pcd[228];
        let r2 = pcd[229];
        tags.push(mktag("PhotoCD", "SceneBalanceAlgorithmRevision", "Scene Balance Algorithm Revision",
            Value::String(format!("{}.{}", r1, r2))));

        // Byte 230: SceneBalanceAlgorithmCommand
        let cmd = pcd[230];
        let cmd_str = match cmd {
            0 => "Neutral SBA On, Color SBA On",
            1 => "Neutral SBA Off, Color SBA Off",
            2 => "Neutral SBA On, Color SBA Off",
            3 => "Neutral SBA Off, Color SBA On",
            _ => "",
        };
        if !cmd_str.is_empty() {
            tags.push(mktag("PhotoCD", "SceneBalanceAlgorithmCommand", "Scene Balance Algorithm Command",
                Value::String(cmd_str.into())));
        }

        // Byte 325: SceneBalanceAlgorithmFilmID (int16u BE)
        if pcd.len() >= 327 {
            let film_id = u16::from_be_bytes([pcd[325], pcd[326]]) as u32;
            let film_str = pcd_film_id_name(film_id);
            tags.push(mktag("PhotoCD", "SceneBalanceAlgorithmFilmID", "Scene Balance Algorithm Film ID",
                Value::String(film_str.to_string())));
        }

        // Byte 331: CopyrightStatus
        if pcd.len() >= 332 {
            let cs = pcd[331];
            let cs_str = match cs {
                1 => "Restrictions apply",
                0xff => "Not specified",
                _ => "",
            };
            if !cs_str.is_empty() {
                tags.push(mktag("PhotoCD", "CopyrightStatus", "Copyright Status", Value::String(cs_str.into())));
            }
        }
    }

    // Byte 1538: Orientation and size info
    if pcd.len() >= 1539 {
        let byte = pcd[1538];
        let orient_raw = byte & 0x03;
        let size_raw = (byte & 0x0c) >> 2;
        let class_raw = (byte & 0x60) >> 5;

        let orient_str = match orient_raw {
            0 => "Horizontal (normal)",
            1 => "Rotate 270 CW",
            2 => "Rotate 180",
            3 => "Rotate 90 CW",
            _ => "",
        };
        tags.push(mktag("PhotoCD", "Orientation", "Orientation", Value::String(orient_str.into())));

        // ImageWidth and ImageHeight depend on orientation
        // Base size: 768x512 (landscape), 512x768 (portrait for rotate 90/270)
        // size_raw: 0=Base (768x512), 1=4Base, 2=16Base
        // scale factor: $val * 2 || 1 = if size_raw > 0 { size_raw * 2 } else { 1 }
        let scale = if size_raw > 0 { (size_raw * 2) as u32 } else { 1 };
        let (w, h) = if orient_raw & 0x01 != 0 {
            (512 * scale, 768 * scale) // portrait
        } else {
            (768 * scale, 512 * scale) // landscape
        };
        tags.push(mktag("PhotoCD", "ImageWidth", "Image Width", Value::String(w.to_string())));
        tags.push(mktag("PhotoCD", "ImageHeight", "Image Height", Value::String(h.to_string())));

        let class_str = match class_raw {
            0 => "Class 1 - 35mm film; Pictoral hard copy",
            1 => "Class 2 - Large format film",
            2 => "Class 3 - Text and graphics, high resolution",
            3 => "Class 4 - Text and graphics, high dynamic range",
            _ => "",
        };
        if !class_str.is_empty() {
            tags.push(mktag("PhotoCD", "CompressionClass", "Compression Class", Value::String(class_str.into())));
        }
    }

    Ok(tags)
}

fn pcd_rtrim_str(bytes: &[u8]) -> String {
    // Perl's string[] format reads to null terminator, then trims trailing spaces/NULs
    let null_end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
    let s = String::from_utf8_lossy(&bytes[..null_end]);
    s.trim_end_matches(|c: char| c == ' ' || c == '\0').to_string()
}

fn pcd_film_id_name(n: u32) -> &'static str {
    match n {
        1 => "3M ScotchColor AT 100", 2 => "3M ScotchColor AT 200",
        3 => "3M ScotchColor HR2 400", 7 => "3M Scotch HR 200 Gen 2",
        9 => "3M Scotch HR 400 Gen 2", 16 => "Agfa Agfacolor XRS 400 Gen 1",
        17 => "Agfa Agfacolor XRG/XRS 400", 18 => "Agfa Agfacolor XRG/XRS 200",
        19 => "Agfa Agfacolor XRS 1000 Gen 2", 20 => "Agfa Agfacolor XRS 400 Gen 2",
        21 => "Agfa Agfacolor XRS/XRC 100", 26 => "Fuji Reala 100 (JAPAN)",
        27 => "Fuji Reala 100 Gen 1", 28 => "Fuji Reala 100 Gen 2",
        29 => "Fuji SHR 400 Gen 2", 30 => "Fuji Super HG 100",
        31 => "Fuji Super HG 1600 Gen 1", 32 => "Fuji Super HG 200",
        33 => "Fuji Super HG 400", 34 => "Fuji Super HG 100 Gen 2",
        35 => "Fuji Super HR 100 Gen 1", 36 => "Fuji Super HR 100 Gen 2",
        37 => "Fuji Super HR 1600 Gen 2", 38 => "Fuji Super HR 200 Gen 1",
        39 => "Fuji Super HR 200 Gen 2", 40 => "Fuji Super HR 400 Gen 1",
        43 => "Fuji NSP 160S (Pro)", 45 => "Kodak Kodacolor VR 100 Gen 2",
        47 => "Kodak Gold 400 Gen 3", 55 => "Kodak Ektar 100 Gen 1",
        56 => "Kodak Ektar 1000 Gen 1", 57 => "Kodak Ektar 125 Gen 1",
        58 => "Kodak Royal Gold 25 RZ", 60 => "Kodak Gold 1600 Gen 1",
        61 => "Kodak Gold 200 Gen 2", 62 => "Kodak Gold 400 Gen 2",
        65 => "Kodak Kodacolor VR 100 Gen 1", 66 => "Kodak Kodacolor VR 1000 Gen 2",
        67 => "Kodak Kodacolor VR 1000 Gen 1", 68 => "Kodak Kodacolor VR 200 Gen 1",
        69 => "Kodak Kodacolor VR 400 Gen 1", 70 => "Kodak Kodacolor VR 200 Gen 2",
        71 => "Kodak Kodacolor VRG 100 Gen 1", 72 => "Kodak Gold 100 Gen 2",
        73 => "Kodak Kodacolor VRG 200 Gen 1", 74 => "Kodak Gold 400 Gen 1",
        87 => "Kodak Ektacolor Gold 160", 88 => "Kodak Ektapress 1600 Gen 1 PPC",
        89 => "Kodak Ektapress Gold 100 Gen 1 PPA", 90 => "Kodak Ektapress Gold 400 PPB-3",
        92 => "Kodak Ektar 25 Professional PHR", 97 => "Kodak T-Max 100 Professional",
        98 => "Kodak T-Max 3200 Professional", 99 => "Kodak T-Max 400 Professional",
        101 => "Kodak Vericolor 400 Prof VPH", 102 => "Kodak Vericolor III Pro",
        121 => "Konika Konica Color SR-G 3200", 122 => "Konika Konica Color Super SR100",
        123 => "Konika Konica Color Super SR 400", 138 => "Kodak Gold Unknown",
        139 => "Kodak Unknown Neg A- Normal SBA", 143 => "Kodak Ektar 100 Gen 2",
        147 => "Kodak Kodacolor CII", 148 => "Kodak Kodacolor II",
        149 => "Kodak Gold Plus 200 Gen 3", 150 => "Kodak Internegative +10% Contrast",
        151 => "Agfa Agfacolor Ultra 50", 152 => "Fuji NHG 400",
        153 => "Agfa Agfacolor XRG 100", 154 => "Kodak Gold Plus 100 Gen 3",
        155 => "Konika Konica Color Super SR200 Gen 1", 156 => "Konika Konica Color SR-G 160",
        157 => "Agfa Agfacolor Optima 125", 158 => "Agfa Agfacolor Portrait 160",
        162 => "Kodak Kodacolor VRG 400 Gen 1", 163 => "Kodak Gold 200 Gen 1",
        164 => "Kodak Kodacolor VRG 100 Gen 2", 174 => "Kodak Internegative +20% Contrast",
        175 => "Kodak Internegative +30% Contrast", 176 => "Kodak Internegative +40% Contrast",
        184 => "Kodak TMax-100 D-76 CI = .40", 185 => "Kodak TMax-100 D-76 CI = .50",
        186 => "Kodak TMax-100 D-76 CI = .55", 187 => "Kodak TMax-100 D-76 CI = .70",
        188 => "Kodak TMax-100 D-76 CI = .80", 189 => "Kodak TMax-100 TMax CI = .40",
        190 => "Kodak TMax-100 TMax CI = .50", 191 => "Kodak TMax-100 TMax CI = .55",
        192 => "Kodak TMax-100 TMax CI = .70", 193 => "Kodak TMax-100 TMax CI = .80",
        195 => "Kodak TMax-400 D-76 CI = .40", 196 => "Kodak TMax-400 D-76 CI = .50",
        197 => "Kodak TMax-400 D-76 CI = .55", 198 => "Kodak TMax-400 D-76 CI = .70",
        214 => "Kodak TMax-400 D-76 CI = .80", 215 => "Kodak TMax-400 TMax CI = .40",
        216 => "Kodak TMax-400 TMax CI = .50", 217 => "Kodak TMax-400 TMax CI = .55",
        218 => "Kodak TMax-400 TMax CI = .70", 219 => "Kodak TMax-400 TMax CI = .80",
        224 => "3M ScotchColor ATG 400/EXL 400", 266 => "Agfa Agfacolor Optima 200",
        267 => "Konika Impressa 50", 268 => "Polaroid Polaroid CP 200",
        269 => "Konika Konica Color Super SR200 Gen 2", 270 => "ILFORD XP2 400",
        271 => "Polaroid Polaroid Color HD2 100", 272 => "Polaroid Polaroid Color HD2 400",
        273 => "Polaroid Polaroid Color HD2 200", 282 => "3M ScotchColor ATG-1 200",
        284 => "Konika XG 400", 307 => "Kodak Universal Reversal B/W",
        308 => "Kodak RPC Copy Film Gen 1", 312 => "Kodak Universal E6",
        324 => "Kodak Gold Ultra 400 Gen 4", 328 => "Fuji Super G 100",
        329 => "Fuji Super G 200", 330 => "Fuji Super G 400 Gen 2",
        333 => "Kodak Universal K14", 334 => "Fuji Super G 400 Gen 1",
        366 => "Kodak Vericolor HC 6329 VHC", 367 => "Kodak Vericolor HC 4329 VHC",
        368 => "Kodak Vericolor L 6013 VPL", 369 => "Kodak Vericolor L 4013 VPL",
        418 => "Kodak Ektacolor Gold II 400 Prof", 430 => "Kodak Royal Gold 1000",
        431 => "Kodak Kodacolor VR 200 / 5093", 432 => "Kodak Gold Plus 100 Gen 4",
        443 => "Kodak Royal Gold 100", 444 => "Kodak Royal Gold 400",
        445 => "Kodak Universal E6 auto-balance", 446 => "Kodak Universal E6 illum. corr.",
        447 => "Kodak Universal K14 auto-balance", 448 => "Kodak Universal K14 illum. corr.",
        449 => "Kodak Ektar 100 Gen 3 SY", 456 => "Kodak Ektar 25",
        457 => "Kodak Ektar 100 Gen 3 CX", 458 => "Kodak Ektapress Plus 100 Prof PJA-1",
        459 => "Kodak Ektapress Gold II 100 Prof", 460 => "Kodak Pro 100 PRN",
        461 => "Kodak Vericolor HC 100 Prof VHC-2", 462 => "Kodak Prof Color Neg 100",
        463 => "Kodak Ektar 1000 Gen 2", 464 => "Kodak Ektapress Plus 1600 Pro PJC-1",
        465 => "Kodak Ektapress Gold II 1600 Prof", 466 => "Kodak Super Gold 1600 GF Gen 2",
        467 => "Kodak Kodacolor 100 Print Gen 4", 468 => "Kodak Super Gold 100 Gen 4",
        469 => "Kodak Gold 100 Gen 4", 470 => "Kodak Gold III 100 Gen 4",
        471 => "Kodak Funtime 100 FA", 472 => "Kodak Funtime 200 FB",
        473 => "Kodak Kodacolor VR 200 Gen 4", 474 => "Kodak Gold Super 200 Gen 4",
        475 => "Kodak Kodacolor 200 Print Gen 4", 476 => "Kodak Super Gold 200 Gen 4",
        477 => "Kodak Gold 200 Gen 4", 478 => "Kodak Gold III 200 Gen 4",
        479 => "Kodak Gold Ultra 400 Gen 5", 480 => "Kodak Super Gold 400 Gen 5",
        481 => "Kodak Gold 400 Gen 5", 482 => "Kodak Gold III 400 Gen 5",
        483 => "Kodak Kodacolor 400 Print Gen 5", 484 => "Kodak Ektapress Plus 400 Prof PJB-2",
        485 => "Kodak Ektapress Gold II 400 Prof G5", 486 => "Kodak Pro 400 PPF-2",
        487 => "Kodak Ektacolor Gold II 400 EGP-4", 488 => "Kodak Ektacolor Gold 400 Prof EGP-4",
        489 => "Kodak Ektapress Gold II Multspd PJM", 490 => "Kodak Pro 400 MC PMC",
        491 => "Kodak Vericolor 400 Prof VPH-2", 492 => "Kodak Vericolor 400 Plus Prof VPH-2",
        493 => "Kodak Unknown Neg Product Code 83", 505 => "Kodak Ektacolor Pro Gold 160 GPX",
        508 => "Kodak Royal Gold 200", 517 => "Kodak 4050000000",
        519 => "Kodak Gold Plus 100 Gen 5", 520 => "Kodak Gold 800 Gen 1",
        521 => "Kodak Gold Super 200 Gen 5", 522 => "Kodak Ektapress Plus 200 Prof",
        523 => "Kodak 4050 E6 auto-balance", 524 => "Kodak 4050 E6 ilum. corr.",
        525 => "Kodak 4050 K14", 526 => "Kodak 4050 K14 auto-balance",
        527 => "Kodak 4050 K14 ilum. corr.", 528 => "Kodak 4050 Reversal B&W",
        532 => "Kodak Advantix 200", 533 => "Kodak Advantix 400",
        534 => "Kodak Advantix 100", 535 => "Kodak Ektapress Multspd Prof PJM-2",
        536 => "Kodak Kodacolor VR 200 Gen 5", 537 => "Kodak Funtime 200 FB Gen 2",
        538 => "Kodak Commercial 200", 539 => "Kodak Royal Gold 25 Copystand",
        540 => "Kodak Kodacolor DA 100 Gen 5", 545 => "Kodak Kodacolor VR 400 Gen 2",
        546 => "Kodak Gold 100 Gen 6", 547 => "Kodak Gold 200 Gen 6",
        548 => "Kodak Gold 400 Gen 6", 549 => "Kodak Royal Gold 100 Gen 2",
        550 => "Kodak Royal Gold 200 Gen 2", 551 => "Kodak Royal Gold 400 Gen 2",
        552 => "Kodak Gold Max 800 Gen 2", 554 => "Kodak 4050 E6 high contrast",
        555 => "Kodak 4050 E6 low saturation high contrast", 556 => "Kodak 4050 E6 low saturation",
        557 => "Kodak Universal E-6 Low Saturation", 558 => "Kodak T-Max T400 CN",
        563 => "Kodak Ektapress PJ100", 564 => "Kodak Ektapress PJ400",
        565 => "Kodak Ektapress PJ800", 567 => "Kodak Portra 160NC",
        568 => "Kodak Portra 160VC", 569 => "Kodak Portra 400NC",
        570 => "Kodak Portra 400VC", 575 => "Kodak Advantix 100-2",
        576 => "Kodak Advantix 200-2", 577 => "Kodak Advantix Black & White + 400",
        578 => "Kodak Ektapress PJ800-2",
        _ => "",
    }
}

// ============================================================================
// MXF (Material Exchange Format)
// ============================================================================

pub fn read_mxf(data: &[u8]) -> Result<Vec<Tag>> {
    // Look for MXF KLV start marker: 06 0e 2b 34
    let magic = b"\x06\x0e\x2b\x34";
    let start = data.windows(4).position(|w| w == magic.as_ref())
        .ok_or_else(|| Error::InvalidData("not an MXF file".into()))?;
    
    let data = &data[start..];
    let mut tags: Vec<Tag> = Vec::new();
    let mut registry: std::collections::HashMap<[u8;2], [u8;16]> = std::collections::HashMap::new();
    
    let mut pos = 0;
    while pos + 17 <= data.len() {
        if &data[pos..pos+4] != b"\x06\x0e\x2b\x34" {
            pos += 1;
            continue;
        }
        let key = &data[pos..pos+16];
        
        // Parse BER length at pos+16
        let len_byte = data[pos+16];
        let (val_len, ber_size) = if len_byte < 0x80 {
            (len_byte as usize, 1usize)
        } else {
            let n = (len_byte & 0x7f) as usize;
            if pos + 17 + n > data.len() { break; }
            let mut l = 0usize;
            for i in 0..n { l = (l << 8) | (data[pos+17+i] as usize); }
            (l, 1 + n)
        };
        let val_start = pos + 16 + ber_size;
        if val_start + val_len > data.len() { break; }
        let val = &data[val_start..val_start+val_len];
        
        // Header/Footer partition (0d0102010102xxxx): parse MXFVersion at offset 0
        if key[4] == 0x02 && key[5] == 0x05 && key[12] == 0x01 && key[13] == 0x02 {
            if val.len() >= 4 && !tags.iter().any(|t| t.name == "MXFVersion") {
                let major = u16::from_be_bytes([val[0], val[1]]);
                let minor = u16::from_be_bytes([val[2], val[3]]);
                tags.push(mktag("MXF", "MXFVersion", "MXF Version",
                    Value::String(format!("{}.{}", major, minor))));
            }
        }
        // Primer Pack: key[12]=0x01 && key[13]=0x05 (0d010201010501xx)
        else if key[4] == 0x02 && key[5] == 0x05 && key[12] == 0x01 && key[13] == 0x05 {
            if val.len() >= 8 {
                let count = u32::from_be_bytes([val[0],val[1],val[2],val[3]]) as usize;
                let item_size = u32::from_be_bytes([val[4],val[5],val[6],val[7]]) as usize;
                if item_size >= 18 {
                    for i in 0..count {
                        let off = 8 + i * item_size;
                        if off + 18 > val.len() { break; }
                        let mut ltag = [0u8;2];
                        ltag.copy_from_slice(&val[off..off+2]);
                        let mut ul = [0u8;16];
                        ul.copy_from_slice(&val[off+2..off+18]);
                        registry.insert(ltag, ul);
                    }
                }
            }
        }
        // Local Set (02 53): parse with primer registry
        else if key[4] == 0x02 && key[5] == 0x53 {
            mxf_parse_local_set(val, &registry, &mut tags);
        }
        
        pos = val_start + val_len;
    }
    
    // Deduplicate (keep last occurrence, like Perl's MXF dedup behavior)
    let mut last_index: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
    for (i, t) in tags.iter().enumerate() {
        last_index.insert(t.name.clone(), i);
    }
    let mut result = Vec::new();
    for (i, t) in tags.into_iter().enumerate() {
        if last_index.get(&t.name) == Some(&i) {
            result.push(t);
        }
    }
    tags = result;
    
    Ok(tags)
}

fn mxf_parse_local_set(
    data: &[u8],
    registry: &std::collections::HashMap<[u8;2], [u8;16]>,
    tags: &mut Vec<Tag>,
) {
    let mut pos = 0;
    while pos + 4 <= data.len() {
        let ltag = [data[pos], data[pos+1]];
        let llen = u16::from_be_bytes([data[pos+2], data[pos+3]]) as usize;
        pos += 4;
        if pos + llen > data.len() { break; }
        let val = &data[pos..pos+llen];
        
        if let Some(ul) = registry.get(&ltag) {
            let ul_hex: String = ul.iter().map(|b| format!("{:02x}", b)).collect();
            if let Some((name, value)) = mxf_decode_tag(&ul_hex, val) {
                tags.push(mktag("MXF", &name, &name, Value::String(value)));
            }
        }
        
        pos += llen;
    }
}

fn mxf_decode_tag(ul: &str, val: &[u8]) -> Option<(String, String)> {
    match ul {
        // Timestamps (8 bytes)
        "060e2b34010101020702011002040000" => Some(("ContainerLastModifyDate".into(), mxf_decode_timestamp(val))),
        "060e2b34010101020702011002030000" => Some(("ModifyDate".into(), mxf_decode_timestamp(val))),
        "060e2b34010101020702011001030000" => Some(("CreateDate".into(), mxf_decode_timestamp(val))),
        "060e2b34010101020702011002050000" => Some(("PackageLastModifyDate".into(), mxf_decode_timestamp(val))),
        // VersionType: 2 bytes major.minor
        "060e2b34010101020301020105000000" => Some(("SDKVersion".into(), mxf_decode_version_short(val))),
        // ProductVersion: 5 × int16u
        "060e2b3401010102052007010a000000" => Some(("ToolkitVersion".into(), mxf_decode_product_version(val))),
        // UTF-16 strings
        "060e2b34010101020520070102010000" => Some(("ApplicationSupplierName".into(), mxf_decode_utf16(val))),
        "060e2b34010101020520070103010000" => Some(("ApplicationName".into(), mxf_decode_utf16(val))),
        "060e2b34010101020520070105010000" => Some(("ApplicationVersionString".into(), mxf_decode_utf16(val))),
        "060e2b34010101020520070106010000" => Some(("ApplicationPlatform".into(), mxf_decode_utf16(val))),
        // TrackName (UTF-16)
        "060e2b34010101020107010201000000" => Some(("TrackName".into(), mxf_decode_utf16(val))),
        // TrackNumber (int32u)
        "060e2b34010101020104010300000000" => {
            if val.len() >= 4 {
                let n = u32::from_be_bytes([val[0],val[1],val[2],val[3]]);
                Some(("TrackNumber".into(), n.to_string()))
            } else { None }
        },
        // TrackID (int32u) - 060e2b34.0101.0102.01070101.00000000
        "060e2b34010101020107010100000000" => {
            if val.len() >= 4 {
                let n = u32::from_be_bytes([val[0],val[1],val[2],val[3]]);
                Some(("TrackID".into(), n.to_string()))
            } else { None }
        },
        // EditRate (rational64s)
        "060e2b34010101020530040500000000" => {
            if val.len() >= 8 {
                let num = i32::from_be_bytes([val[0],val[1],val[2],val[3]]);
                let den = i32::from_be_bytes([val[4],val[5],val[6],val[7]]);
                let rate = if den != 0 && den != 1 {
                    let r = num as f64 / den as f64;
                    if r == r.floor() { format!("{}", r as i64) } else { format!("{:.6}", r).trim_end_matches('0').to_string() }
                } else { num.to_string() };
                Some(("EditRate".into(), rate))
            } else { None }
        },
        // RoundedTimecodeTimebase (int16u)
        "060e2b34010101020404010102060000" => {
            if val.len() >= 2 {
                let n = u16::from_be_bytes([val[0],val[1]]);
                Some(("RoundedTimecodeTimebase".into(), n.to_string()))
            } else { None }
        },
        // DropFrame (Boolean)
        "060e2b34010101010404010105000000" => {
            if !val.is_empty() {
                Some(("DropFrame".into(), if val[0] != 0 {"True"} else {"False"}.into()))
            } else { None }
        },
        // StartTimecode (int64s, shown as "N s")
        "060e2b34010101020702010301050000" => {
            if val.len() >= 8 {
                let n = i64::from_be_bytes([val[0],val[1],val[2],val[3],val[4],val[5],val[6],val[7]]);
                Some(("StartTimecode".into(), format!("{} s", n)))
            } else { None }
        },
        // ComponentDataDefinition (WeakReference to DataDefinition UL)
        "060e2b34010101020601010401020000" => {
            Some(("ComponentDataDefinition".into(), mxf_decode_component_def(val)))
        },
        // Origin (int64s duration in edit units)
        "060e2b34010101020702010301040000" => {
            if val.len() >= 8 {
                let n = i64::from_be_bytes([val[0],val[1],val[2],val[3],val[4],val[5],val[6],val[7]]);
                Some(("Origin".into(), format!("{} s", n)))
            } else { None }
        },
        // Duration (int64s)
        "060e2b34010101020702010301030000" |
        "060e2b34010101020702010302000000" => {
            if val.len() >= 8 {
                let n = i64::from_be_bytes([val[0],val[1],val[2],val[3],val[4],val[5],val[6],val[7]]);
                if n > 1000000000 { return None; } // all 0xff sentinel
                Some(("Duration".into(), format!("{} s", n)))
            } else { None }
        },
        // EssenceLength (int64s)
        "060e2b34010101010406010200000000" => {
            if val.len() >= 8 {
                let n = i64::from_be_bytes([val[0],val[1],val[2],val[3],val[4],val[5],val[6],val[7]]);
                if n > 1000000000 { return None; }
                Some(("EssenceLength".into(), format!("{} s", n)))
            } else { None }
        },
        // SampleRate (rational64s)
        "060e2b34010101010406010100000000" => {
            if val.len() >= 8 {
                let num = i32::from_be_bytes([val[0],val[1],val[2],val[3]]);
                let den = i32::from_be_bytes([val[4],val[5],val[6],val[7]]);
                let rate = if den != 0 && den != 1 {
                    let r = num as f64 / den as f64;
                    if r == r.floor() { format!("{}", r as i64) } else { format!("{:.6}", r) }
                } else { num.to_string() };
                Some(("SampleRate".into(), rate))
            } else { None }
        },
        // ChannelCount (int32u)
        "060e2b34010101050402010104000000" => {
            if val.len() >= 4 { let n = u32::from_be_bytes([val[0],val[1],val[2],val[3]]); Some(("ChannelCount".into(), n.to_string())) }
            else if !val.is_empty() { Some(("ChannelCount".into(), val[0].to_string())) }
            else { None }
        },
        // AudioSampleRate (rational64s)
        "060e2b34010101050402030101010000" => {
            if val.len() >= 8 {
                let num = i32::from_be_bytes([val[0],val[1],val[2],val[3]]);
                let den = i32::from_be_bytes([val[4],val[5],val[6],val[7]]);
                let rate = if den != 0 && den != 1 {
                    let r = num as f64 / den as f64;
                    if r == r.floor() { format!("{}", r as i64) } else { format!("{:.6}", r) }
                } else { num.to_string() };
                Some(("AudioSampleRate".into(), rate))
            } else { None }
        },
        // BlockAlign (int16u)
        "060e2b34010101050402030201000000" => {
            if val.len() >= 2 { let n = u16::from_be_bytes([val[0],val[1]]); Some(("BlockAlign".into(), n.to_string())) } else { None }
        },
        // AverageBytesPerSecond (int32u)
        "060e2b34010101050402030305000000" => {
            if val.len() >= 4 { let n = u32::from_be_bytes([val[0],val[1],val[2],val[3]]); Some(("AverageBytesPerSecond".into(), n.to_string())) } else { None }
        },
        // LockedIndicator (Boolean)
        "060e2b34010101040402030104000000" => {
            if !val.is_empty() {
                Some(("LockedIndicator".into(), if val[0] != 0 {"True"} else {"False"}.into()))
            } else { None }
        },
        // BitsPerAudioSample (int32u)
        "060e2b34010101040402030304000000" => {
            if val.len() >= 4 { let n = u32::from_be_bytes([val[0],val[1],val[2],val[3]]); Some(("BitsPerAudioSample".into(), n.to_string())) } else { None }
        },
        // LinkedTrackID (int32u)
        "060e2b34010101050601010305000000" => {
            if val.len() >= 4 { let n = u32::from_be_bytes([val[0],val[1],val[2],val[3]]); Some(("LinkedTrackID".into(), n.to_string())) } else { None }
        },
        // EssenceStreamID (int32u)
        "060e2b34010101040103040400000000" => {
            if val.len() >= 4 { let n = u32::from_be_bytes([val[0],val[1],val[2],val[3]]); Some(("EssenceStreamID".into(), n.to_string())) }
            else if val.len() >= 2 { let n = u16::from_be_bytes([val[0],val[1]]); Some(("EssenceStreamID".into(), n.to_string())) }
            else { None }
        },
        _ => None,
    }
}

fn mxf_decode_timestamp(val: &[u8]) -> String {
    if val.len() < 8 { return String::new(); }
    let year = u16::from_be_bytes([val[0], val[1]]);
    let month = val[2]; let day = val[3];
    let hour = val[4]; let min = val[5]; let sec = val[6];
    let msec = (val[7] as u32) * 4;
    format!("{:04}:{:02}:{:02} {:02}:{:02}:{:02}.{:03}", year, month, day, hour, min, sec, msec)
}

fn mxf_decode_version_short(val: &[u8]) -> String {
    if val.len() < 2 { return String::new(); }
    format!("{}.{}", val[0], val[1])
}

fn mxf_decode_product_version(val: &[u8]) -> String {
    if val.len() < 10 { return String::new(); }
    let major = u16::from_be_bytes([val[0],val[1]]);
    let minor = u16::from_be_bytes([val[2],val[3]]);
    let patch = u16::from_be_bytes([val[4],val[5]]);
    let build = u16::from_be_bytes([val[6],val[7]]);
    let rel_type = u16::from_be_bytes([val[8],val[9]]);
    let rel_str = match rel_type {
        0 => "unknown".to_string(),
        1 => "released".to_string(),
        2 => "debug".to_string(),
        3 => "patched".to_string(),
        4 => "beta".to_string(),
        5 => "private build".to_string(),
        _ => format!("unknown {}", rel_type),
    };
    format!("{}.{}.{}.{} {}", major, minor, patch, build, rel_str)
}

fn mxf_decode_utf16(val: &[u8]) -> String {
    let chars: Vec<u16> = val.chunks(2)
        .filter(|c| c.len() == 2)
        .map(|c| u16::from_be_bytes([c[0], c[1]]))
        .collect();
    String::from_utf16_lossy(&chars).trim_end_matches('\0').to_string()
}

fn mxf_decode_component_def(val: &[u8]) -> String {
    if val.len() < 16 { return String::new(); }
    let ul: String = val[..16].iter().map(|b| format!("{:02x}", b)).collect();
    match ul.as_str() {
        "060e2b34040101020d01030102060200" => "Sound Essence Track".to_string(),
        "060e2b34040101010103020100000000" => "Picture Essence Track".to_string(),
        "060e2b34040101010103020200000000" => "Sound Essence Track".to_string(),
        "060e2b34040101010103020300000000" => "Data Essence Track".to_string(),
        "060e2b34040101010103020400000000" => "Descriptive Metadata Track".to_string(),
        _ => ul,
    }
}

// ============================================================================
// PhaseOne IIQ RAW format
// ============================================================================

pub fn read_iiq(data: &[u8]) -> Result<Vec<Tag>> {
    // Read standard TIFF tags first
    let mut tags = crate::formats::tiff::read_tiff(data).unwrap_or_default();

    // For IIQ, the TIFF file has two IFDs:
    // IFD0: has the main camera settings but thumbnail image data (1x1 placeholder)
    // IFD1: has the reduced-resolution thumbnail image
    // We need to:
    // 1. Remove SubfileType=0 from IFD0 (keep IFD1's Reduced-resolution image)
    // 2. Remove duplicate TIFF tags (keep first occurrence)
    // 3. Remove IFD0's StripOffsets/StripByteCounts (keep IFD1's)
    // 4. Remove 1x1 ImageWidth/ImageHeight placeholders
    // 5. Remove ExifByteOrder (added by the outer exiftool.rs code already)
    {
        // First pass: identify which tags to remove
        let mut seen_names: std::collections::HashSet<String> = std::collections::HashSet::new();
        let mut strip_offsets_count = 0;
        let mut strip_bytes_count = 0;
        let mut subfile_type_removed = false;
        let mut remove_indices: std::collections::HashSet<usize> = std::collections::HashSet::new();

        // Tags that may legitimately appear in both IFDs and we should keep BOTH
        let keep_both: std::collections::HashSet<&str> = [
            "SubfileType", "StripOffsets", "StripByteCounts",
        ].iter().cloned().collect();

        for (i, t) in tags.iter().enumerate() {
            // Remove ExifByteOrder (already added by outer exiftool.rs)
            if t.name == "ExifByteOrder" {
                remove_indices.insert(i);
                continue;
            }
            // Remove the first SubfileType (IFD0's "full image" marker = value 0)
            if t.name == "SubfileType" && !subfile_type_removed {
                let raw_v = if let Value::String(ref v) = t.raw_value { v.as_str() } else { "" };
                if raw_v == "0" || t.print_value == "0" {
                    remove_indices.insert(i);
                    subfile_type_removed = true;
                    continue;
                }
            }
            // Remove first StripOffsets (IFD0's strip) and first StripByteCounts
            if t.name == "StripOffsets" {
                strip_offsets_count += 1;
                if strip_offsets_count == 1 { remove_indices.insert(i); continue; }
            }
            if t.name == "StripByteCounts" {
                strip_bytes_count += 1;
                if strip_bytes_count == 1 { remove_indices.insert(i); continue; }
            }
            // Remove 1x1 ImageWidth/Height placeholders
            if (t.name == "ImageWidth" || t.name == "ImageHeight") && t.print_value == "1" {
                remove_indices.insert(i);
                continue;
            }
            // Deduplicate other tags (keep first occurrence)
            if !keep_both.contains(t.name.as_str()) {
                if seen_names.contains(&t.name) {
                    remove_indices.insert(i);
                } else {
                    seen_names.insert(t.name.clone());
                }
            }
        }

        let mut new_tags = Vec::new();
        for (i, t) in tags.into_iter().enumerate() {
            if !remove_indices.contains(&i) {
                new_tags.push(t);
            }
        }
        tags = new_tags;
    }

    // PhaseOne block starts at offset 8 with "IIII" (LE) or "MMMM" (BE) magic
    if data.len() < 20 {
        return Ok(tags);
    }
    let is_le = &data[8..12] == b"IIII";
    let is_be = &data[8..12] == b"MMMM";
    if !is_le && !is_be {
        return Ok(tags);
    }

    let phaseone_start = 8usize;

    // IFD offset is at bytes 8..12 of the PhaseOne block (relative to phaseone_start)
    let ifd_offset_in_block = iiq_read_u32(data, phaseone_start + 8, is_le) as usize;
    let abs_ifd_start = phaseone_start + ifd_offset_in_block;
    if abs_ifd_start + 8 > data.len() {
        return Ok(tags);
    }

    let num_entries = iiq_read_u32(data, abs_ifd_start, is_le) as usize;
    if num_entries > 300 || abs_ifd_start + 8 + num_entries * 16 > data.len() {
        return Ok(tags);
    }

    let entry_start = abs_ifd_start + 8;
    let mut phaseone_tags: Vec<Tag> = Vec::new();

    for i in 0..num_entries {
        let off = entry_start + i * 16;
        let tag_id = iiq_read_u32(data, off, is_le);
        // fmt_size: 1=string, 2=int16s, 4=int32s (or float for specific tags)
        let _fmt_size = iiq_read_u32(data, off + 4, is_le);
        let size = iiq_read_u32(data, off + 8, is_le) as usize;
        let val_or_ptr = iiq_read_u32(data, off + 12, is_le) as usize;

        // Get raw bytes
        let raw: &[u8] = if size <= 4 {
            // Value is stored inline at offset 12, as little-endian u32
            let end = (off + 12 + size).min(data.len());
            &data[off + 12..end]
        } else {
            let abs_ptr = phaseone_start + val_or_ptr;
            if abs_ptr + size > data.len() { continue; }
            &data[abs_ptr..abs_ptr + size]
        };

        iiq_decode_tag(tag_id, raw, is_le, size, data, phaseone_start, &mut phaseone_tags);
    }

    // Parse SensorCalibration sub-block (tag 0x0110)
    iiq_parse_sensor_calibration(data, phaseone_start, is_le, entry_start, num_entries, &mut phaseone_tags);

    // Extend with PhaseOne tags, but don't add tags that already exist (skip dups)
    // Exception: FocalLength from PhaseOne should override EXIF's (remove EXIF version)
    // Actually: keep PhaseOne version for FocalLength (more accurate), remove EXIF
    {
        // Build set of existing tag names
        let _existing: std::collections::HashSet<String> = tags.iter().map(|t| t.name.clone()).collect();
        // Remove EXIF versions of tags that PhaseOne provides (FocalLength, ISO, ShutterSpeedValue, ApertureValue)
        let phaseone_names: std::collections::HashSet<String> = phaseone_tags.iter().map(|t| t.name.clone()).collect();
        // Remove from existing tags those that PhaseOne also provides (PhaseOne wins)
        // PhaseOne ShutterSpeedValue/ApertureValue are better than EXIF APEX versions
        let phaseone_overrides: std::collections::HashSet<&str> = [
            "ShutterSpeedValue", "ApertureValue",
        ].iter().cloned().collect();
        tags.retain(|t| !phaseone_overrides.contains(t.name.as_str()) || !phaseone_names.contains(&t.name));

        // Now add PhaseOne tags, skipping ones already in tags
        let existing2: std::collections::HashSet<String> = tags.iter().map(|t| t.name.clone()).collect();
        for t in phaseone_tags {
            if !existing2.contains(&t.name) {
                tags.push(t);
            }
        }
    }

    // Add composite tags: RedBalance, BlueBalance from WB_RGBLevels
    {
        let wb_val = tags.iter().find(|t| t.name == "WB_RGBLevels")
            .map(|t| t.print_value.clone());
        if let Some(s) = wb_val {
            let parts: Vec<&str> = s.split_whitespace().collect();
            if parts.len() >= 3 {
                if let Ok(r) = parts[0].parse::<f64>() {
                    tags.push(mktag("Composite", "RedBalance", "Red Balance",
                        Value::String(format!("{:.5}", r))));
                }
                if let Ok(b) = parts[2].parse::<f64>() {
                    tags.push(mktag("Composite", "BlueBalance", "Blue Balance",
                        Value::String(format!("{:.5}", b))));
                }
            }
        }
    }

    Ok(tags)
}

/// Format f64 to match Perl's %.15g format (15 significant digits, no trailing zeros)
fn iiq_fmt_f64(v: f64) -> String {
    // Use %.15g equivalent: 15 significant digits
    let _s = format!("{:.15e}", v);
    // Parse the scientific notation and convert to %.15g style
    // Simpler: just format with enough precision and let Rust handle it
    // Actually use a direct approach: format with 14 decimal places in the mantissa
    // then strip trailing zeros
    let formatted = format!("{:.14}", v);
    // Strip trailing zeros after decimal point
    if formatted.contains('.') {
        let stripped = formatted.trim_end_matches('0').trim_end_matches('.');
        stripped.to_string()
    } else {
        formatted
    }
}

fn iiq_read_u32(data: &[u8], off: usize, is_le: bool) -> u32 {
    if off + 4 > data.len() { return 0; }
    if is_le {
        u32::from_le_bytes([data[off], data[off+1], data[off+2], data[off+3]])
    } else {
        u32::from_be_bytes([data[off], data[off+1], data[off+2], data[off+3]])
    }
}

fn iiq_read_f32(data: &[u8], off: usize, is_le: bool) -> f32 {
    if off + 4 > data.len() { return 0.0; }
    let bytes = [data[off], data[off+1], data[off+2], data[off+3]];
    if is_le { f32::from_le_bytes(bytes) } else { f32::from_be_bytes(bytes) }
}

fn iiq_read_str(raw: &[u8]) -> String {
    // Read null-terminated string
    let end = raw.iter().position(|&b| b == 0).unwrap_or(raw.len());
    String::from_utf8_lossy(&raw[..end]).trim().to_string()
}

fn iiq_decode_tag(
    tag_id: u32, raw: &[u8], is_le: bool, size: usize,
    full_data: &[u8], phaseone_start: usize,
    tags: &mut Vec<Tag>,
) {
    let push = |tags: &mut Vec<Tag>, name: &str, desc: &str, val: String| {
        tags.push(mktag("MakerNotes", name, desc, Value::String(val)));
    };

    match tag_id {
        0x010f => {
            // RawData (binary)
            let display = format!("(Binary data {} bytes, use -b option to extract)", size);
            push(tags, "RawData", "Raw Data", display);
        }
        0x0100 => {
            // CameraOrientation
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) & 0x03 } else { 0 };
            let s = match v {
                0 => "Horizontal (normal)".to_string(),
                1 => "Rotate 90 CW".to_string(),
                2 => "Rotate 270 CW".to_string(),
                3 => "Rotate 180".to_string(),
                _ => v.to_string(),
            };
            push(tags, "CameraOrientation", "Camera Orientation", s);
        }
        0x0102 => {
            // SerialNumber (string)
            push(tags, "SerialNumber", "Serial Number", iiq_read_str(raw));
        }
        0x0105 => {
            // ISO
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            push(tags, "ISO", "ISO", v.to_string());
        }
        0x0106 => {
            // ColorMatrix1 (9 floats)
            if raw.len() >= 36 {
                let vals: Vec<f32> = (0..9).map(|i| iiq_read_f32(raw, i*4, is_le)).collect();
                let s: Vec<String> = vals.iter().map(|v| format!("{:.3}", v)).collect();
                push(tags, "ColorMatrix1", "Color Matrix 1", s.join(" "));
            }
        }
        0x0107 => {
            // WB_RGBLevels (3 floats) - promote to f64 for Perl-compatible precision
            if raw.len() >= 12 {
                let r = iiq_read_f32(raw, 0, is_le) as f64;
                let g = iiq_read_f32(raw, 4, is_le) as f64;
                let b = iiq_read_f32(raw, 8, is_le) as f64;
                // Normalize so G=1
                let s = if g != 0.0 {
                    format!("{} {} {}", iiq_fmt_f64(r/g), 1.0f64, iiq_fmt_f64(b/g))
                } else {
                    format!("{} {} {}", iiq_fmt_f64(r), iiq_fmt_f64(g), iiq_fmt_f64(b))
                };
                push(tags, "WB_RGBLevels", "WB RGB Levels", s);
            }
        }
        0x0108 => {
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            push(tags, "SensorWidth", "Sensor Width", v.to_string());
        }
        0x0109 => {
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            push(tags, "SensorHeight", "Sensor Height", v.to_string());
        }
        0x010a => {
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            push(tags, "SensorLeftMargin", "Sensor Left Margin", v.to_string());
        }
        0x010b => {
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            push(tags, "SensorTopMargin", "Sensor Top Margin", v.to_string());
        }
        0x010c => {
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            push(tags, "ImageWidth", "Image Width", v.to_string());
        }
        0x010d => {
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            push(tags, "ImageHeight", "Image Height", v.to_string());
        }
        0x010e => {
            // RawFormat
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            let s = match v {
                0 => "Uncompressed".to_string(),
                1 => "RAW 1".to_string(),
                2 => "RAW 2".to_string(),
                3 => "IIQ L".to_string(),
                5 => "IIQ S".to_string(),
                6 => "IIQ Sv2".to_string(),
                8 => "IIQ L16".to_string(),
                _ => v.to_string(),
            };
            push(tags, "RawFormat", "Raw Format", s);
        }
        0x0113 => {
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            push(tags, "ImageNumber", "Image Number", v.to_string());
        }
        0x0203 => {
            push(tags, "Software", "Software", iiq_read_str(raw));
        }
        0x0204 => {
            push(tags, "System", "System", iiq_read_str(raw));
        }
        0x0210 => {
            // SensorTemperature (float)
            let v = if raw.len() >= 4 { iiq_read_f32(raw, 0, is_le) } else { 0.0 };
            push(tags, "SensorTemperature", "Sensor Temperature", format!("{:.2} C", v));
        }
        0x0211 => {
            // SensorTemperature2 (float)
            let v = if raw.len() >= 4 { iiq_read_f32(raw, 0, is_le) } else { 0.0 };
            push(tags, "SensorTemperature2", "Sensor Temperature 2", format!("{:.2} C", v));
        }
        0x021d => {
            // BlackLevel
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            push(tags, "BlackLevel", "Black Level", v.to_string());
        }
        0x0222 => {
            // SplitColumn
            let v = if raw.len() >= 4 { iiq_read_u32(raw, 0, is_le) } else { 0 };
            push(tags, "SplitColumn", "Split Column", v.to_string());
        }
        0x0223 => {
            // BlackLevelData (int16u, binary)
            // Format as space-separated int16u values (matching Perl Binary output)
            let count = raw.len() / 2;
            if count > 0 {
                let vals: Vec<String> = (0..count).map(|i| {
                    let v = if is_le {
                        u16::from_le_bytes([raw[i*2], raw[i*2+1]])
                    } else {
                        u16::from_be_bytes([raw[i*2], raw[i*2+1]])
                    };
                    v.to_string()
                }).collect();
                let s = vals.join(" ");
                let display = format!("(Binary data {} bytes, use -b option to extract)", s.len());
                push(tags, "BlackLevelData", "Black Level Data", display);
            } else {
                push(tags, "BlackLevelData", "Black Level Data",
                    format!("(Binary data {} bytes, use -b option to extract)", raw.len()));
            }
        }
        0x0226 => {
            // ColorMatrix2 (9 floats)
            if raw.len() >= 36 {
                let vals: Vec<f32> = (0..9).map(|i| iiq_read_f32(raw, i*4, is_le)).collect();
                let s: Vec<String> = vals.iter().map(|v| format!("{:.3}", v)).collect();
                push(tags, "ColorMatrix2", "Color Matrix 2", s.join(" "));
            }
        }
        0x0301 => {
            // FirmwareVersions (string)
            push(tags, "FirmwareVersions", "Firmware Versions", iiq_read_str(raw));
        }
        0x0400 => {
            // ShutterSpeedValue (float, convert: 2**(-val))
            let v = if raw.len() >= 4 { iiq_read_f32(raw, 0, is_le) } else { 0.0 };
            let exposure = if v.abs() < 100.0 { 2.0f32.powf(-v) } else { 0.0 };
            // Format as fraction
            let s = iiq_format_exposure_time(exposure);
            push(tags, "ShutterSpeedValue", "Shutter Speed Value", s);
        }
        0x0401 => {
            // ApertureValue (float, convert: 2**(val/2))
            let v = if raw.len() >= 4 { iiq_read_f32(raw, 0, is_le) } else { 0.0 };
            let aperture = 2.0f32.powf(v / 2.0);
            push(tags, "ApertureValue", "Aperture Value", format!("{:.1}", aperture));
        }
        0x0403 => {
            // FocalLength (float)
            let v = if raw.len() >= 4 { iiq_read_f32(raw, 0, is_le) } else { 0.0 };
            push(tags, "FocalLength", "Focal Length", format!("{:.1} mm", v));
        }
        0x0412 => {
            // LensModel (string)
            push(tags, "LensModel", "Lens Model", iiq_read_str(raw));
        }
        _ => {}
    }

    let _ = (full_data, phaseone_start); // suppress unused warnings
}

fn iiq_format_exposure_time(t: f32) -> String {
    if t <= 0.0 { return "0".to_string(); }
    if t >= 1.0 {
        // Whole seconds or more
        let rounded = t.round() as u32;
        if (t - rounded as f32).abs() < 0.05 {
            return rounded.to_string();
        }
        return format!("{:.1}", t);
    }
    // Express as 1/N fraction
    let n = (1.0 / t).round() as u32;
    format!("1/{}", n)
}

fn iiq_parse_sensor_calibration(
    data: &[u8], phaseone_start: usize, is_le: bool,
    entry_start: usize, num_entries: usize,
    tags: &mut Vec<Tag>,
) {
    // Find tag 0x0110 (SensorCalibration sub-block)
    for i in 0..num_entries {
        let off = entry_start + i * 16;
        let tag_id = iiq_read_u32(data, off, is_le);
        if tag_id != 0x0110 { continue; }
        let size = iiq_read_u32(data, off + 8, is_le) as usize;
        let val_or_ptr = iiq_read_u32(data, off + 12, is_le) as usize;
        if size <= 4 { return; }

        let abs_ptr = phaseone_start + val_or_ptr;
        if abs_ptr + size > data.len() { return; }
        let sub = &data[abs_ptr..abs_ptr + size];

        // SensorCalibration sub-block: starts with IIII\\x01\\x00\\x00\\x00 or MMMM\\x00\\x00\\x00\\x01
        if sub.len() < 12 { return; }
        let sub_is_le = &sub[0..4] == b"IIII";
        let sub_is_be = &sub[0..4] == b"MMMM";
        if !sub_is_le && !sub_is_be { return; }

        let sub_ifd_off = iiq_read_u32(sub, 8, sub_is_le) as usize;
        if sub_ifd_off + 8 > sub.len() { return; }

        let num_sub = iiq_read_u32(sub, sub_ifd_off, sub_is_le) as usize;
        if num_sub > 300 { return; }
        let sub_entry_start = sub_ifd_off + 8;
        if sub_entry_start + num_sub * 12 > sub.len() { return; }

        // SensorCalibration uses 12-byte entries (no format field)
        for j in 0..num_sub {
            let eoff = sub_entry_start + j * 12;
            let etag = iiq_read_u32(sub, eoff, sub_is_le);
            let esize = iiq_read_u32(sub, eoff + 4, sub_is_le) as usize;
            let _eval_ptr = iiq_read_u32(sub, eoff + 8, sub_is_le) as usize;

            if etag == 0x0400 {
                // SensorDefects (binary undef)
                let display = format!("(Binary data {} bytes, use -b option to extract)", esize);
                tags.push(mktag("MakerNotes", "SensorDefects", "Sensor Defects", Value::String(display)));
                break;
            }
        }
        return;
    }
}