ag-psd 0.1.0

Read and write Adobe Photoshop (.psd/.psb) files — a from-scratch Rust port of the ag-psd TypeScript library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
/*
File: crates/ag-psd/src/image_resources.rs

Purpose:
image resource блоки PSD (ресурсы уровня документа).

Source compatibility:
- порт upstream-файла `test/ag-psd/src/imageResources.ts` (разбиение 1:1).

Main responsibilities:
- зеркалировать соответствующий upstream-модуль при портировании;
- держать публичный контракт этого участка в одном месте.

PORT STATUS: ported.

ARCHITECTURE — handler registry
================================
Upstream models per-resource handlers as an array `resourceHandlers[]` plus a
`resourceHandlersMap[key]`, each entry `{ key, has, read, write }`. In Rust a
`Vec<{ read: closure, write: closure }>` is awkward: the closures borrow the
typed `ImageResources` model mutably with differing capture sets and lifetimes,
and `read` needs the `left()` section-length callback. Instead the registry is
expressed as three free functions dispatching on the resource id:

  - [`has_image_resource`]  — mirror of each handler's `has` predicate (which
    resources are present, in upstream order — see [`RESOURCE_IDS`]);
  - [`read_image_resource`] — `match id { .. }` over the per-id read body;
  - [`write_image_resource`] — `match id { .. }` over the per-id write body.

This is the idiomatic, zero-cost Rust analog of `resourceHandlersMap[key]`: the
exact id numbers, signatures, padding and field math are preserved; only the
dispatch shape changes (a `match` instead of a closure table).

Upstream's `MOCK_HANDLERS && addHandler(...)` blocks are gated on
`helpers::MOCK_HANDLERS` which is `false`, so they are never registered. They are
left unported (they only stash raw bytes into `_irNNNN` debug fields that do not
exist on the typed model). The real (always-registered) handlers are all ported.

DEPENDENCY GAPS (cannot be closed without editing other modules)
================================================================
- `reader::read_color` is not yet ported (deferred in reader.rs). A minimal local
  [`read_color`] mirroring upstream `readColor` is provided here for id 1010.
- id 1075 (Timeline Information) needs `parseTrackList`/`serializeTrackList` and
  the `TimelineTrackDescriptor`/`FractionDescriptor` shape converters from
  descriptor.ts, none of which are ported in descriptor.rs. Porting them would
  require touching descriptor.rs, so 1075 is left as a TODO stub (see below).
- id 1036 thumbnail JPEG payload: jpeg.rs is a stub, so the compressed bytes are
  kept/emitted raw (`thumbnail_raw`); no encode/decode is performed.
*/

#![allow(clippy::too_many_lines)]

use crate::descriptor::{
    read_version_and_descriptor, write_version_and_descriptor, Descriptor, DescriptorValue,
};
use crate::helpers::{EnumCodec, MOCK_HANDLERS};
use crate::psd::{
    AnimationDispose, AnimationFrameInfo, AnimationInfo, Animations, CountInformation,
    GridAndGuidesInformation, GridInfo, GuideDirection, GuideInfo,
    ImageResources, LayerCompCapturedInfo, LayerCompListItem, LayerCompsResource, LtrbBounds,
    OnionSkins, PixelAspectRatio, PointF, PrintFlags, PrintInformation, PrintScale,
    PrintScaleStyle, ProofSetup, RenderingIntent, ResolutionInfo, ResolutionUnit, DimensionUnit,
    Rgb, Rgba, Slice, SliceAlignment, SliceBackgroundColorType, SliceGroup, SliceOrigin, SliceType,
    SheetDisclosure, SheetTimelineOption, ThumbnailRaw, UrlListItem, VersionInfo, BlendMode,
};
use crate::reader::{
    check_signature, read_ascii_string, read_bytes, read_color, read_float32, read_float64,
    read_int16, read_int32, read_fixed_point32, read_section, read_signature, read_uint16,
    read_uint32, read_uint8, read_unicode_string, skip_bytes, PsdReader, ReadError, ReadResult,
};
use crate::utf8::{decode_string, encode_string};
use crate::writer::{
    write_ascii_string, write_bytes, write_color, write_fixed_point32, write_float32,
    write_float64, write_int16, write_int32, write_section, write_signature, write_uint16,
    write_uint32, write_uint8, write_unicode_string, write_unicode_string_with_padding, PsdWriter,
};

// ===========================================================================
// Tables / small helpers (mirror module-level consts in imageResources.ts)
// ===========================================================================

/// Mirror `RESOLUTION_UNITS = [undefined, 'PPI', 'PPCM']` (1-based).
const RESOLUTION_UNITS: [Option<ResolutionUnit>; 3] =
    [None, Some(ResolutionUnit::Ppi), Some(ResolutionUnit::Ppcm)];

/// Mirror `MEASUREMENT_UNITS = [undefined, 'Inches', 'Centimeters', 'Points', 'Picas', 'Columns']`.
const MEASUREMENT_UNITS: [Option<DimensionUnit>; 6] = [
    None,
    Some(DimensionUnit::Inches),
    Some(DimensionUnit::Centimeters),
    Some(DimensionUnit::Points),
    Some(DimensionUnit::Picas),
    Some(DimensionUnit::Columns),
];

const HEX: &[u8; 16] = b"0123456789abcdef";

/// Mirror `charToNibble`.
fn char_to_nibble(code: u8) -> u8 {
    if code <= 57 {
        code - 48
    } else {
        code - 87
    }
}

/// Mirror `byteAt(value, index)`.
fn byte_at(value: &str, index: usize) -> u8 {
    let bytes = value.as_bytes();
    (char_to_nibble(bytes[index]) << 4) | char_to_nibble(bytes[index + 1])
}

/// Mirror `readUtf8String(reader, length)`.
fn read_utf8_string(reader: &mut PsdReader, length: usize) -> ReadResult<String> {
    let buffer = read_bytes(reader, length)?;
    decode_string(&buffer).map_err(|_| ReadError::StrictViolation("Invalid UTF-8".to_string()))
}

/// Mirror `writeUtf8String(writer, value)`.
fn write_utf8_string(writer: &mut PsdWriter, value: &str) {
    let buffer = encode_string(value);
    write_bytes(writer, Some(&buffer));
}

/// Mirror `readEncodedString(reader)`.
///
/// Reads a uint8 length, then `length` bytes. If any byte has the high bit set,
/// upstream decodes as GBK; we have no GBK decoder available, so for that branch
/// we fall back to a lossy Latin-1-style decode (each byte -> code point) and
/// note the divergence. Pure-ASCII payloads (the common case) match exactly.
fn read_encoded_string(reader: &mut PsdReader) -> ReadResult<String> {
    let length = read_uint8(reader)? as usize;
    let buffer = read_bytes(reader, length)?;

    let not_ascii = buffer.iter().any(|&b| b & 0x80 != 0);

    if not_ascii {
        // DEPENDENCY GAP: no GBK decoder ported; lossy byte->char fallback.
        Ok(buffer.iter().map(|&b| b as char).collect())
    } else {
        decode_string(&buffer).map_err(|_| ReadError::StrictViolation("Invalid UTF-8".to_string()))
    }
}

/// Mirror `writeEncodedString(writer, value)`.
fn write_encoded_string(writer: &mut PsdWriter, value: &str) {
    // Replace any code point > 0x7f with '?'.
    let ascii: String = value
        .chars()
        .map(|c| if (c as u32) > 0x7f { '?' } else { c })
        .collect();
    let buffer = encode_string(&ascii);
    write_uint8(writer, buffer.len() as u8);
    write_bytes(writer, Some(&buffer));
}

// ===========================================================================
// EnumCodec instances (mirror createEnum<...> module-level consts)
// ===========================================================================

fn dict(pairs: &[(&str, &str)]) -> std::collections::HashMap<String, String> {
    pairs
        .iter()
        .map(|(k, v)| (k.to_string(), v.to_string()))
        .collect()
}

/// Mirror `Inte = createEnum<RenderingIntent>('Inte', 'perceptual', {...})`.
fn inte_codec() -> EnumCodec {
    EnumCodec::new(
        "Inte",
        "perceptual",
        dict(&[
            ("perceptual", "Img "),
            ("saturation", "Grp "),
            ("relative colorimetric", "Clrm"),
            ("absolute colorimetric", "AClr"),
        ]),
    )
}

/// Mirror `FrmD = createEnum<'auto'|'none'|'dispose'>('FrmD', '', {...})`.
fn frmd_codec() -> EnumCodec {
    EnumCodec::new(
        "FrmD",
        "",
        dict(&[("auto", "Auto"), ("none", "None"), ("dispose", "Disp")]),
    )
}

// Slice enums (mirror ESlice* from descriptor.ts, defined locally as a gap).
fn eslice_type_codec() -> EnumCodec {
    EnumCodec::new(
        "ESliceType",
        "image",
        dict(&[("image", "Img "), ("noImage", "Nor ")]),
    )
}
fn eslice_horz_codec() -> EnumCodec {
    EnumCodec::new("ESliceHorzAlign", "default", dict(&[("default", "Dflt")]))
}
fn eslice_vert_codec() -> EnumCodec {
    EnumCodec::new("ESliceVertAlign", "default", dict(&[("default", "Dflt")]))
}
fn eslice_origin_codec() -> EnumCodec {
    EnumCodec::new(
        "ESliceOrigin",
        "userGenerated",
        dict(&[
            ("userGenerated", "userGenerated"),
            ("autoGenerated", "autoGenerated"),
            ("layer", "layer"),
        ]),
    )
}
fn eslice_bg_codec() -> EnumCodec {
    EnumCodec::new(
        "ESliceBGColorType",
        "none",
        dict(&[("none", "None"), ("matte", "Matt"), ("color", "Clr ")]),
    )
}

// ===========================================================================
// RenderingIntent <-> string helpers (model enum <-> EnumCodec string)
// ===========================================================================

fn rendering_intent_to_str(intent: RenderingIntent) -> &'static str {
    match intent {
        RenderingIntent::Perceptual => "perceptual",
        RenderingIntent::Saturation => "saturation",
        RenderingIntent::RelativeColorimetric => "relative colorimetric",
        RenderingIntent::AbsoluteColorimetric => "absolute colorimetric",
    }
}

fn rendering_intent_from_str(s: &str) -> RenderingIntent {
    match s {
        "saturation" => RenderingIntent::Saturation,
        "relative colorimetric" => RenderingIntent::RelativeColorimetric,
        "absolute colorimetric" => RenderingIntent::AbsoluteColorimetric,
        _ => RenderingIntent::Perceptual,
    }
}

// ===========================================================================
// Descriptor field accessors (typed-tree convenience)
// ===========================================================================

fn get_bool(desc: &Descriptor, key: &str) -> Option<bool> {
    match desc.get(key) {
        Some(DescriptorValue::Boolean(b)) => Some(*b),
        _ => None,
    }
}
fn get_text(desc: &Descriptor, key: &str) -> Option<String> {
    match desc.get(key) {
        Some(DescriptorValue::Text(s)) => Some(s.clone()),
        _ => None,
    }
}
fn get_enum(desc: &Descriptor, key: &str) -> Option<String> {
    match desc.get(key) {
        Some(DescriptorValue::Enum(s)) => Some(s.clone()),
        _ => None,
    }
}
fn get_int(desc: &Descriptor, key: &str) -> Option<i32> {
    match desc.get(key) {
        Some(DescriptorValue::Integer(i)) => Some(*i),
        _ => None,
    }
}
fn get_double(desc: &Descriptor, key: &str) -> Option<f64> {
    match desc.get(key) {
        Some(DescriptorValue::Double(d)) => Some(*d),
        Some(DescriptorValue::Integer(i)) => Some(*i as f64),
        _ => None,
    }
}
fn get_descriptor<'a>(desc: &'a Descriptor, key: &str) -> Option<&'a Descriptor> {
    match desc.get(key) {
        Some(DescriptorValue::Descriptor(d)) => Some(d),
        _ => None,
    }
}
fn get_list<'a>(desc: &'a Descriptor, key: &str) -> Option<&'a Vec<DescriptorValue>> {
    match desc.get(key) {
        Some(DescriptorValue::List(l)) => Some(l),
        _ => None,
    }
}

// ===========================================================================
// Registry surface (ordered list of always-registered resource ids)
// ===========================================================================

/// Resource ids in upstream registration order (real, non-MOCK handlers).
///
/// Mirror of the order in which `addHandler(...)` runs for non-mock entries.
pub const RESOURCE_IDS: &[u16] = &[
    1061, // captionDigest
    1060, // xmpMetadata
    1082, // printInformation
    1005, // resolutionInfo
    1062, // printScale
    1006, // alphaChannelNames (encoded)
    1045, // alphaChannelNames (unicode)
    1053, // alphaIdentifiers
    1010, // backgroundColor
    1037, // globalAngle
    1049, // globalAltitude
    1011, // printFlags
    1034, // copyrighted
    1035, // url
    1080, // countInformation
    1024, // layerState
    1026, // layersGroup
    1072, // layerGroupsEnabledId
    1069, // layerSelectionIds
    1032, // gridAndGuidesInformation
    1065, // layerComps
    1078, // onionSkins
    1075, // timelineInformation (stub — see header)
    1076, // sheetDisclosure
    1054, // urlsList
    1050, // slices
    1064, // pixelAspectRatio
    1041, // iccUntaggedProfile
    1044, // idsSeedNumber
    1036, // thumbnail
    1057, // versionInfo
    7000, // imageReadyVariables
    7001, // imageReadyDataSets
    1088, // pathSelectionState
    4000, // animations
];

/// Mirror of each handler's `has(target)` predicate. Returns whether the resource
/// should be written, plus a count for `slices` (matches upstream returning a
/// number there). For most resources the count is 1.
pub fn has_image_resource(id: u16, target: &ImageResources) -> usize {
    let present = match id {
        1061 => target.caption_digest.is_some(),
        1060 => target.xmp_metadata.is_some(),
        1082 => target.print_information.is_some(),
        1005 => target.resolution_info.is_some(),
        1062 => target.print_scale.is_some(),
        1006 | 1045 => target.alpha_channel_names.is_some(),
        1053 => target.alpha_identifiers.is_some(),
        1010 => target.background_color.is_some(),
        1037 => target.global_angle.is_some(),
        1049 => target.global_altitude.is_some(),
        1011 => target.print_flags.is_some(),
        1034 => target.copyrighted.is_some(),
        1035 => target.url.is_some(),
        1080 => target.count_information.is_some(),
        1024 => target.layer_state.is_some(),
        1069 => target.layer_selection_ids.is_some(),
        1032 => target.grid_and_guides_information.is_some(),
        1065 => target.layer_comps.is_some(),
        1078 => target.onion_skins.is_some(),
        1075 => target.timeline_information.is_some(),
        1076 => target.sheet_disclosure.is_some(),
        1054 => target.urls_list.is_some(),
        1050 => return target.slices.as_ref().map_or(0, |s| s.len()),
        1064 => target.pixel_aspect_ratio.is_some(),
        1041 => target.icc_untagged_profile.is_some(),
        1044 => target.ids_seed_number.is_some(),
        1036 => target.thumbnail.is_some() || target.thumbnail_raw.is_some(),
        1057 => target.version_info.is_some(),
        7000 => target.image_ready_variables.is_some(),
        7001 => target.image_ready_data_sets.is_some(),
        1088 => target.path_selection_state.is_some(),
        4000 => target.animations.is_some(),
        // layersGroup / layerGroupsEnabledId are InternalImageResources-only
        // (1026 / 1072) and not on the public model.
        _ => false,
    };
    usize::from(present)
}

// ===========================================================================
// Read dispatch
// ===========================================================================

/// Mirror `resourceHandlersMap[key].read(reader, target, left)`.
///
/// `left` is the number of bytes remaining in the resource block (mirror of the
/// `left()` callback). Unknown ids are ignored (caller skips the block).
pub fn read_image_resource(
    id: u16,
    reader: &mut PsdReader,
    target: &mut ImageResources,
    left: usize,
) -> ReadResult<()> {
    match id {
        1061 => {
            let mut caption_digest = String::new();
            for _ in 0..16 {
                let byte = read_uint8(reader)?;
                caption_digest.push(HEX[(byte >> 4) as usize] as char);
                caption_digest.push(HEX[(byte & 0xf) as usize] as char);
            }
            target.caption_digest = Some(caption_digest);
        }
        1060 => {
            target.xmp_metadata = Some(read_utf8_string(reader, left)?);
        }
        1082 => {
            let desc = read_version_and_descriptor(reader)?;
            let intent = inte_codec()
                .decode(&get_enum(&desc, "Inte").unwrap_or_else(|| "Inte.Img ".to_string()))
                .unwrap_or_else(|_| "perceptual".to_string());

            let mut info = PrintInformation {
                printer_name: Some(get_text(&desc, "printerName").unwrap_or_default()),
                rendering_intent: Some(rendering_intent_from_str(&intent)),
                ..Default::default()
            };

            if let Some(v) = get_bool(&desc, "PstS") {
                info.printer_manages_colors = Some(v);
            }
            if let Some(v) = get_text(&desc, "Nm  ") {
                info.printer_profile = Some(v);
            }
            if let Some(v) = get_bool(&desc, "MpBl") {
                info.black_point_compensation = Some(v);
            }
            if let Some(v) = get_bool(&desc, "printSixteenBit") {
                info.print_sixteen_bit = Some(v);
            }
            if let Some(v) = get_bool(&desc, "hardProof") {
                info.hard_proof = Some(v);
            }
            if let Some(setup) = get_descriptor(&desc, "printProofSetup") {
                if let Some(DescriptorValue::Enum(bltn)) = setup.get("Bltn") {
                    let builtin = bltn.split('.').nth(1).unwrap_or("").to_string();
                    info.proof_setup = Some(ProofSetup::Builtin { builtin });
                } else if let Some(DescriptorValue::Text(bltn)) = setup.get("Bltn") {
                    let builtin = bltn.split('.').nth(1).unwrap_or("").to_string();
                    info.proof_setup = Some(ProofSetup::Builtin { builtin });
                } else {
                    let intent = inte_codec()
                        .decode(
                            &get_enum(setup, "Inte").unwrap_or_else(|| "Inte.Img ".to_string()),
                        )
                        .unwrap_or_else(|_| "perceptual".to_string());
                    info.proof_setup = Some(ProofSetup::Profile {
                        profile: get_text(setup, "profile").unwrap_or_default(),
                        rendering_intent: Some(rendering_intent_from_str(&intent)),
                        black_point_compensation: Some(get_bool(setup, "MpBl").unwrap_or(false)),
                        paper_white: Some(get_bool(setup, "paperWhite").unwrap_or(false)),
                    });
                }
            }

            target.print_information = Some(info);
        }
        1005 => {
            let horizontal_resolution = read_fixed_point32(reader)?;
            let horizontal_resolution_unit = read_uint16(reader)? as usize;
            let width_unit = read_uint16(reader)? as usize;
            let vertical_resolution = read_fixed_point32(reader)?;
            let vertical_resolution_unit = read_uint16(reader)? as usize;
            let height_unit = read_uint16(reader)? as usize;

            target.resolution_info = Some(ResolutionInfo {
                horizontal_resolution,
                horizontal_resolution_unit: RESOLUTION_UNITS
                    .get(horizontal_resolution_unit)
                    .copied()
                    .flatten()
                    .unwrap_or(ResolutionUnit::Ppi),
                width_unit: MEASUREMENT_UNITS
                    .get(width_unit)
                    .copied()
                    .flatten()
                    .unwrap_or(DimensionUnit::Inches),
                vertical_resolution,
                vertical_resolution_unit: RESOLUTION_UNITS
                    .get(vertical_resolution_unit)
                    .copied()
                    .flatten()
                    .unwrap_or(ResolutionUnit::Ppi),
                height_unit: MEASUREMENT_UNITS
                    .get(height_unit)
                    .copied()
                    .flatten()
                    .unwrap_or(DimensionUnit::Inches),
            });
        }
        1062 => {
            let style_index = read_int16(reader)?;
            let style = match style_index {
                0 => Some(PrintScaleStyle::Centered),
                1 => Some(PrintScaleStyle::SizeToFit),
                2 => Some(PrintScaleStyle::UserDefined),
                _ => None,
            };
            target.print_scale = Some(PrintScale {
                style,
                x: Some(read_float32(reader)? as f64),
                y: Some(read_float32(reader)? as f64),
                scale: Some(read_float32(reader)? as f64),
            });
        }
        1006 => {
            // skip if the unicode versions are already read
            if target.alpha_channel_names.is_none() {
                let mut names = Vec::new();
                let end = reader.offset + left;
                while reader.offset < end {
                    names.push(read_encoded_string(reader)?);
                }
                target.alpha_channel_names = Some(names);
            } else {
                skip_bytes(reader, left);
            }
        }
        1045 => {
            let mut names = Vec::new();
            let end = reader.offset + left;
            while reader.offset < end {
                names.push(read_unicode_string(reader)?);
            }
            target.alpha_channel_names = Some(names);
        }
        1053 => {
            let mut ids = Vec::new();
            let end = reader.offset + left;
            while end.saturating_sub(reader.offset) >= 4 {
                ids.push(read_uint32(reader)? as f64);
            }
            target.alpha_identifiers = Some(ids);
        }
        1010 => {
            target.background_color = Some(read_color(reader)?);
        }
        1037 => {
            target.global_angle = Some(read_int32(reader)? as f64);
        }
        1049 => {
            target.global_altitude = Some(read_uint32(reader)? as f64);
        }
        1011 => {
            target.print_flags = Some(PrintFlags {
                labels: Some(read_uint8(reader)? != 0),
                crop_marks: Some(read_uint8(reader)? != 0),
                color_bars: Some(read_uint8(reader)? != 0),
                registration_marks: Some(read_uint8(reader)? != 0),
                negative: Some(read_uint8(reader)? != 0),
                flip: Some(read_uint8(reader)? != 0),
                interpolate: Some(read_uint8(reader)? != 0),
                caption: Some(read_uint8(reader)? != 0),
                print_flags: Some(read_uint8(reader)? != 0),
            });
        }
        1034 => {
            target.copyrighted = Some(read_uint8(reader)? != 0);
        }
        1035 => {
            target.url = Some(read_ascii_string(reader, left)?);
        }
        1080 => {
            let desc = read_version_and_descriptor(reader)?;
            let mut groups = Vec::new();
            if let Some(list) = get_list(&desc, "countGroupList") {
                for item in list {
                    if let DescriptorValue::Descriptor(g) = item {
                        let mut points = Vec::new();
                        if let Some(plist) = get_list(g, "countObjectList") {
                            for p in plist {
                                if let DescriptorValue::Descriptor(pd) = p {
                                    points.push(PointF {
                                        x: get_double(pd, "X   ").unwrap_or(0.0),
                                        y: get_double(pd, "Y   ").unwrap_or(0.0),
                                    });
                                }
                            }
                        }
                        groups.push(CountInformation {
                            color: Rgb {
                                r: get_double(g, "Rd  ").unwrap_or(0.0),
                                g: get_double(g, "Grn ").unwrap_or(0.0),
                                b: get_double(g, "Bl  ").unwrap_or(0.0),
                            },
                            name: get_text(g, "Nm  ").unwrap_or_default(),
                            size: get_double(g, "Rds ").unwrap_or(0.0),
                            font_size: get_double(g, "fontSize").unwrap_or(0.0),
                            visible: get_bool(g, "Vsbl").unwrap_or(false),
                            points,
                        });
                    }
                }
            }
            target.count_information = Some(groups);
        }
        1024 => {
            target.layer_state = Some(read_uint16(reader)? as f64);
        }
        1026 => {
            // InternalImageResources.layersGroup — not on public model; skip.
            skip_bytes(reader, left);
        }
        1072 => {
            // InternalImageResources.layerGroupsEnabledId — not on public model; skip.
            skip_bytes(reader, left);
        }
        1069 => {
            let mut count = read_uint16(reader)?;
            let mut ids = Vec::new();
            while count > 0 {
                count -= 1;
                ids.push(read_uint32(reader)? as f64);
            }
            target.layer_selection_ids = Some(ids);
        }
        1032 => {
            let version = read_uint32(reader)?;
            let horizontal = read_uint32(reader)? as f64;
            let vertical = read_uint32(reader)? as f64;
            let count = read_uint32(reader)?;

            if version != 1 {
                return Err(ReadError::StrictViolation(format!(
                    "Invalid 1032 resource version: {version}"
                )));
            }

            let mut guides = Vec::new();
            for _ in 0..count {
                let location = read_uint32(reader)? as f64 / 32.0;
                let direction = if read_uint8(reader)? != 0 {
                    GuideDirection::Horizontal
                } else {
                    GuideDirection::Vertical
                };
                guides.push(GuideInfo {
                    location,
                    direction,
                });
            }

            target.grid_and_guides_information = Some(GridAndGuidesInformation {
                grid: Some(GridInfo {
                    horizontal,
                    vertical,
                }),
                guides: Some(guides),
            });
        }
        1065 => {
            let desc = read_version_and_descriptor(reader)?;
            let mut list = Vec::new();
            if let Some(items) = get_list(&desc, "list") {
                for item in items {
                    if let DescriptorValue::Descriptor(it) = item {
                        let comment = get_text(it, "comment");
                        list.push(LayerCompListItem {
                            id: get_double(it, "compID").unwrap_or(0.0),
                            name: get_text(it, "Nm  ").unwrap_or_default(),
                            comment,
                            captured_info: captured_info_from_num(
                                get_double(it, "capturedInfo").unwrap_or(0.0),
                            ),
                        });
                    }
                }
            }
            let last_applied = get_double(&desc, "lastAppliedComp");
            target.layer_comps = Some(LayerCompsResource {
                list,
                last_applied,
            });
        }
        1078 => {
            let desc = read_version_and_descriptor(reader)?;
            let blnm = get_int(&desc, "BlnM").unwrap_or(0);
            target.onion_skins = Some(OnionSkins {
                enabled: get_bool(&desc, "enab").unwrap_or(false),
                frames_before: get_double(&desc, "numBefore").unwrap_or(0.0),
                frames_after: get_double(&desc, "numAfter").unwrap_or(0.0),
                frame_spacing: get_double(&desc, "Spcn").unwrap_or(0.0),
                min_opacity: get_double(&desc, "minOpacity").unwrap_or(0.0) / 100.0,
                max_opacity: get_double(&desc, "maxOpacity").unwrap_or(0.0) / 100.0,
                blend_mode: onion_skin_blend_mode(blnm),
            });
        }
        1075 => {
            // TODO: needs parseTrackList/serializeTrackList + TimelineTrackDescriptor
            // shape converters from descriptor.ts (not ported in descriptor.rs).
            // Skipping payload to keep the rest of the block framing intact.
            skip_bytes(reader, left);
        }
        1076 => {
            let desc = read_version_and_descriptor(reader)?;
            let mut disclosure = SheetDisclosure::default();
            if let Some(list) = get_list(&desc, "sheetTimelineOptions") {
                let mut options = Vec::new();
                for item in list {
                    if let DescriptorValue::Descriptor(o) = item {
                        options.push(SheetTimelineOption {
                            sheet_id: get_double(o, "sheetID").unwrap_or(0.0),
                            sheet_disclosed: get_bool(o, "sheetDisclosed").unwrap_or(false),
                            lights_disclosed: get_bool(o, "lightsDisclosed").unwrap_or(false),
                            meshes_disclosed: get_bool(o, "meshesDisclosed").unwrap_or(false),
                            materials_disclosed: get_bool(o, "materialsDisclosed").unwrap_or(false),
                        });
                    }
                }
                disclosure.sheet_timeline_options = Some(options);
            }
            target.sheet_disclosure = Some(disclosure);
        }
        1054 => {
            let count = read_uint32(reader)?;
            let mut list = Vec::new();
            for _ in 0..count {
                let long = read_signature(reader)?;
                if long != "slic" && reader.options.throw_for_missing_features == Some(true) {
                    return Err(ReadError::StrictViolation("Unknown long".to_string()));
                }
                let id = read_uint32(reader)? as f64;
                let url = read_unicode_string(reader)?;
                list.push(UrlListItem {
                    id,
                    url,
                    r#ref: "slice".to_string(),
                });
            }
            target.urls_list = Some(list);
        }
        1050 => {
            read_slices(reader, target)?;
        }
        1064 => {
            if read_uint32(reader)? > 2 {
                return Err(ReadError::StrictViolation(
                    "Invalid pixelAspectRatio version".to_string(),
                ));
            }
            target.pixel_aspect_ratio = Some(PixelAspectRatio {
                aspect: read_float64(reader)?,
            });
        }
        1041 => {
            target.icc_untagged_profile = Some(read_uint8(reader)? != 0);
        }
        1044 => {
            target.ids_seed_number = Some(read_uint32(reader)? as f64);
        }
        1036 => {
            read_thumbnail(reader, target, left)?;
        }
        1057 => {
            let version = read_uint32(reader)?;
            if version != 1 {
                return Err(ReadError::StrictViolation(
                    "Invalid versionInfo version".to_string(),
                ));
            }
            target.version_info = Some(VersionInfo {
                has_real_merged_data: read_uint8(reader)? != 0,
                writer_name: read_unicode_string(reader)?,
                reader_name: read_unicode_string(reader)?,
                file_version: read_uint32(reader)? as f64,
            });
            // skipBytes(reader, left()) — already consumed; remaining skipped by caller.
            let consumed_end = reader.offset;
            let _ = consumed_end;
            skip_bytes(reader, 0);
        }
        7000 => {
            target.image_ready_variables = Some(read_utf8_string(reader, left)?);
        }
        7001 => {
            target.image_ready_data_sets = Some(read_utf8_string(reader, left)?);
        }
        1088 => {
            let desc = read_version_and_descriptor(reader)?;
            let mut paths = Vec::new();
            if let Some(list) = get_list(&desc, "null") {
                for item in list {
                    if let DescriptorValue::Text(s) = item {
                        paths.push(s.clone());
                    }
                }
            }
            target.path_selection_state = Some(paths);
        }
        4000 => {
            read_animations(reader, target, left)?;
        }
        _ => {
            // Unknown / MOCK-only id — caller skips the block.
        }
    }
    Ok(())
}

// ===========================================================================
// Write dispatch
// ===========================================================================

/// Mirror `resourceHandlersMap[key].write(writer, target, index)`.
pub fn write_image_resource(
    id: u16,
    writer: &mut PsdWriter,
    target: &ImageResources,
    index: usize,
) -> ReadResult<()> {
    match id {
        1061 => {
            let digest = target.caption_digest.as_deref().unwrap_or("");
            for i in 0..16 {
                write_uint8(writer, byte_at(digest, i * 2));
            }
        }
        1060 => {
            write_utf8_string(writer, target.xmp_metadata.as_deref().unwrap_or(""));
        }
        1082 => {
            let info = target.print_information.as_ref().unwrap();
            let mut desc = Descriptor::new("", "printOutput");

            if info.printer_manages_colors == Some(true) {
                desc.set("PstS", DescriptorValue::Boolean(true));
            } else {
                if let Some(hp) = info.hard_proof {
                    desc.set("hardProof", DescriptorValue::Boolean(hp));
                }
                desc.set("ClrS", DescriptorValue::Enum("ClrS.RGBC".to_string()));
                desc.set(
                    "Nm  ",
                    DescriptorValue::Text(
                        info.printer_profile
                            .clone()
                            .unwrap_or_else(|| "CIE RGB".to_string()),
                    ),
                );
            }

            let intent = info.rendering_intent.unwrap_or(RenderingIntent::Perceptual);
            desc.set(
                "Inte",
                DescriptorValue::Enum(
                    inte_codec()
                        .encode(Some(rendering_intent_to_str(intent)))
                        .unwrap(),
                ),
            );

            if info.printer_manages_colors != Some(true) {
                desc.set(
                    "MpBl",
                    DescriptorValue::Boolean(info.black_point_compensation.unwrap_or(false)),
                );
            }

            desc.set(
                "printSixteenBit",
                DescriptorValue::Boolean(info.print_sixteen_bit.unwrap_or(false)),
            );
            desc.set(
                "printerName",
                DescriptorValue::Text(info.printer_name.clone().unwrap_or_default()),
            );

            match &info.proof_setup {
                Some(ProofSetup::Profile {
                    profile,
                    rendering_intent,
                    black_point_compensation,
                    paper_white,
                }) => {
                    let mut sub = Descriptor::new("", "prfP");
                    sub.set("profile", DescriptorValue::Text(profile.clone()));
                    let pi = rendering_intent.unwrap_or(RenderingIntent::Perceptual);
                    sub.set(
                        "Inte",
                        DescriptorValue::Enum(
                            inte_codec().encode(Some(rendering_intent_to_str(pi))).unwrap(),
                        ),
                    );
                    sub.set(
                        "MpBl",
                        DescriptorValue::Boolean(black_point_compensation.unwrap_or(false)),
                    );
                    sub.set(
                        "paperWhite",
                        DescriptorValue::Boolean(paper_white.unwrap_or(false)),
                    );
                    desc.set("printProofSetup", DescriptorValue::Descriptor(sub));
                }
                other => {
                    let builtin = match other {
                        Some(ProofSetup::Builtin { builtin }) if !builtin.is_empty() => {
                            format!("builtinProof.{builtin}")
                        }
                        _ => "builtinProof.proofCMYK".to_string(),
                    };
                    let mut sub = Descriptor::new("", "prfP");
                    sub.set("Bltn", DescriptorValue::Enum(builtin));
                    desc.set("printProofSetup", DescriptorValue::Descriptor(sub));
                }
            }

            write_version_and_descriptor(writer, &desc);
        }
        1005 => {
            let info = target.resolution_info.as_ref().unwrap();
            write_fixed_point32(writer, info.horizontal_resolution);
            write_uint16(writer, resolution_unit_index(info.horizontal_resolution_unit));
            write_uint16(writer, measurement_unit_index(info.width_unit));
            write_fixed_point32(writer, info.vertical_resolution);
            write_uint16(writer, resolution_unit_index(info.vertical_resolution_unit));
            write_uint16(writer, measurement_unit_index(info.height_unit));
        }
        1062 => {
            let ps = target.print_scale.as_ref().unwrap();
            let style_index = match ps.style {
                Some(PrintScaleStyle::Centered) => 0,
                Some(PrintScaleStyle::SizeToFit) => 1,
                Some(PrintScaleStyle::UserDefined) => 2,
                None => 0,
            };
            write_int16(writer, style_index);
            write_float32(writer, ps.x.unwrap_or(0.0) as f32);
            write_float32(writer, ps.y.unwrap_or(0.0) as f32);
            write_float32(writer, ps.scale.unwrap_or(0.0) as f32);
        }
        1006 => {
            for name in target.alpha_channel_names.as_ref().unwrap() {
                write_encoded_string(writer, name);
            }
        }
        1045 => {
            for name in target.alpha_channel_names.as_ref().unwrap() {
                write_unicode_string_with_padding(writer, name);
            }
        }
        1053 => {
            for id in target.alpha_identifiers.as_ref().unwrap() {
                write_uint32(writer, *id as u32);
            }
        }
        1010 => {
            write_color(writer, target.background_color.as_ref());
        }
        1037 => {
            write_int32(writer, target.global_angle.unwrap() as i32);
        }
        1049 => {
            write_uint32(writer, target.global_altitude.unwrap() as u32);
        }
        1011 => {
            let f = target.print_flags.as_ref().unwrap();
            for b in [
                f.labels,
                f.crop_marks,
                f.color_bars,
                f.registration_marks,
                f.negative,
                f.flip,
                f.interpolate,
                f.caption,
                f.print_flags,
            ] {
                write_uint8(writer, u8::from(b.unwrap_or(false)));
            }
        }
        1034 => {
            write_uint8(writer, u8::from(target.copyrighted.unwrap_or(false)));
        }
        1035 => {
            write_ascii_string(writer, target.url.as_deref().unwrap());
        }
        1080 => {
            let mut desc = Descriptor::new("", "Cnt ");
            desc.set("Vrsn", DescriptorValue::Integer(1));
            let mut group_list = Vec::new();
            for g in target.count_information.as_ref().unwrap() {
                let mut gd = Descriptor::new("", "cntG");
                gd.set("Rd  ", DescriptorValue::Integer(g.color.r as i32));
                gd.set("Grn ", DescriptorValue::Integer(g.color.g as i32));
                gd.set("Bl  ", DescriptorValue::Integer(g.color.b as i32));
                gd.set("Nm  ", DescriptorValue::Text(g.name.clone()));
                gd.set("Rds ", DescriptorValue::Integer(g.size as i32));
                gd.set("fontSize", DescriptorValue::Integer(g.font_size as i32));
                gd.set("Vsbl", DescriptorValue::Boolean(g.visible));
                let mut points = Vec::new();
                for p in &g.points {
                    let mut pd = Descriptor::new("", "cntO");
                    pd.set("X   ", DescriptorValue::Integer(p.x as i32));
                    pd.set("Y   ", DescriptorValue::Integer(p.y as i32));
                    points.push(DescriptorValue::Descriptor(pd));
                }
                gd.set("countObjectList", DescriptorValue::List(points));
                group_list.push(DescriptorValue::Descriptor(gd));
            }
            desc.set("countGroupList", DescriptorValue::List(group_list));
            write_version_and_descriptor(writer, &desc);
        }
        1024 => {
            write_uint16(writer, target.layer_state.unwrap() as u16);
        }
        1069 => {
            let ids = target.layer_selection_ids.as_ref().unwrap();
            write_uint16(writer, ids.len() as u16);
            for id in ids {
                write_uint32(writer, *id as u32);
            }
        }
        1032 => {
            let info = target.grid_and_guides_information.as_ref().unwrap();
            let grid = info.grid.unwrap_or(GridInfo {
                horizontal: 18.0 * 32.0,
                vertical: 18.0 * 32.0,
            });
            let empty = Vec::new();
            let guides = info.guides.as_ref().unwrap_or(&empty);
            write_uint32(writer, 1);
            write_uint32(writer, grid.horizontal as u32);
            write_uint32(writer, grid.vertical as u32);
            write_uint32(writer, guides.len() as u32);
            for g in guides {
                write_uint32(writer, (g.location * 32.0) as u32);
                write_uint8(
                    writer,
                    u8::from(g.direction == GuideDirection::Horizontal),
                );
            }
        }
        1065 => {
            let lc = target.layer_comps.as_ref().unwrap();
            let mut desc = Descriptor::new("", "CompList");
            let mut list = Vec::new();
            for item in &lc.list {
                let mut t = Descriptor::new("", "Comp");
                t.set("Nm  ", DescriptorValue::Text(item.name.clone()));
                if let Some(comment) = &item.comment {
                    t.set("comment", DescriptorValue::Text(comment.clone()));
                }
                t.set("compID", DescriptorValue::Integer(item.id as i32));
                t.set(
                    "capturedInfo",
                    DescriptorValue::Integer(item.captured_info as i32),
                );
                list.push(DescriptorValue::Descriptor(t));
            }
            desc.set("list", DescriptorValue::List(list));
            if let Some(last) = lc.last_applied {
                desc.set("lastAppliedComp", DescriptorValue::Integer(last as i32));
            }
            write_version_and_descriptor(writer, &desc);
        }
        1078 => {
            let os = target.onion_skins.as_ref().unwrap();
            let mut desc = Descriptor::new("", "null");
            desc.set("Vrsn", DescriptorValue::Integer(1));
            desc.set("enab", DescriptorValue::Boolean(os.enabled));
            desc.set("numBefore", DescriptorValue::Integer(os.frames_before as i32));
            desc.set("numAfter", DescriptorValue::Integer(os.frames_after as i32));
            desc.set("Spcn", DescriptorValue::Integer(os.frame_spacing as i32));
            desc.set(
                "minOpacity",
                DescriptorValue::Integer((os.min_opacity * 100.0) as i32),
            );
            desc.set(
                "maxOpacity",
                DescriptorValue::Integer((os.max_opacity * 100.0) as i32),
            );
            desc.set(
                "BlnM",
                DescriptorValue::Integer(onion_skin_blend_index(os.blend_mode)),
            );
            write_version_and_descriptor(writer, &desc);
        }
        1075 => {
            // TODO: needs serializeTrackList + TimelineTrackDescriptor (not ported).
            // Nothing emitted (caller must avoid registering 1075 for now).
        }
        1076 => {
            let d = target.sheet_disclosure.as_ref().unwrap();
            let mut desc = Descriptor::new("", "null");
            desc.set("Vrsn", DescriptorValue::Integer(1));
            if let Some(opts) = &d.sheet_timeline_options {
                let mut list = Vec::new();
                for o in opts {
                    let mut od = Descriptor::new("", "shtT");
                    od.set("Vrsn", DescriptorValue::Integer(2));
                    od.set("sheetID", DescriptorValue::Integer(o.sheet_id as i32));
                    od.set("sheetDisclosed", DescriptorValue::Boolean(o.sheet_disclosed));
                    od.set(
                        "lightsDisclosed",
                        DescriptorValue::Boolean(o.lights_disclosed),
                    );
                    od.set(
                        "meshesDisclosed",
                        DescriptorValue::Boolean(o.meshes_disclosed),
                    );
                    od.set(
                        "materialsDisclosed",
                        DescriptorValue::Boolean(o.materials_disclosed),
                    );
                    list.push(DescriptorValue::Descriptor(od));
                }
                desc.set("sheetTimelineOptions", DescriptorValue::List(list));
            }
            write_version_and_descriptor(writer, &desc);
        }
        1054 => {
            let list = target.urls_list.as_ref().unwrap();
            write_uint32(writer, list.len() as u32);
            for item in list {
                write_signature(writer, "slic");
                write_uint32(writer, item.id as u32);
                write_unicode_string(writer, &item.url);
            }
        }
        1050 => {
            write_slices(writer, target, index);
        }
        1064 => {
            write_uint32(writer, 2); // version
            write_float64(writer, target.pixel_aspect_ratio.as_ref().unwrap().aspect);
        }
        1041 => {
            write_uint8(
                writer,
                u8::from(target.icc_untagged_profile.unwrap_or(false)),
            );
        }
        1044 => {
            write_uint32(writer, target.ids_seed_number.unwrap() as u32);
        }
        1036 => {
            write_thumbnail(writer, target);
        }
        1057 => {
            let vi = target.version_info.as_ref().unwrap();
            write_uint32(writer, 1);
            write_uint8(writer, u8::from(vi.has_real_merged_data));
            write_unicode_string(writer, &vi.writer_name);
            write_unicode_string(writer, &vi.reader_name);
            write_uint32(writer, vi.file_version as u32);
        }
        7000 => {
            write_utf8_string(writer, target.image_ready_variables.as_deref().unwrap());
        }
        7001 => {
            write_utf8_string(writer, target.image_ready_data_sets.as_deref().unwrap());
        }
        1088 => {
            let mut desc = Descriptor::new("", "null");
            let paths = target.path_selection_state.as_ref().unwrap();
            let list = paths
                .iter()
                .map(|s| DescriptorValue::Text(s.clone()))
                .collect();
            desc.set("null", DescriptorValue::List(list));
            write_version_and_descriptor(writer, &desc);
        }
        4000 => {
            write_animations(writer, target);
        }
        _ => {}
    }
    Ok(())
}

// ===========================================================================
// Unit-index helpers (mirror Math.max(1, ARRAY.indexOf(x)))
// ===========================================================================

fn resolution_unit_index(u: ResolutionUnit) -> u16 {
    match u {
        ResolutionUnit::Ppi => 1,
        ResolutionUnit::Ppcm => 2,
    }
}

fn measurement_unit_index(u: DimensionUnit) -> u16 {
    match u {
        DimensionUnit::Inches => 1,
        DimensionUnit::Centimeters => 2,
        DimensionUnit::Points => 3,
        DimensionUnit::Picas => 4,
        DimensionUnit::Columns => 5,
    }
}

// ===========================================================================
// LayerCompCapturedInfo <-> number
// ===========================================================================

fn captured_info_from_num(n: f64) -> LayerCompCapturedInfo {
    match n as i32 {
        1 => LayerCompCapturedInfo::Visibility,
        2 => LayerCompCapturedInfo::Position,
        4 => LayerCompCapturedInfo::Appearance,
        _ => LayerCompCapturedInfo::None,
    }
}

// ===========================================================================
// Onion-skin blend mode table (mirror onionSkinsBlendModes)
// ===========================================================================

fn onion_skin_blend_mode(index: i32) -> BlendMode {
    match index {
        7 => BlendMode::Multiply,
        8 => BlendMode::Screen,
        23 => BlendMode::Difference,
        _ => BlendMode::Normal,
    }
}

fn onion_skin_blend_index(mode: BlendMode) -> i32 {
    match mode {
        BlendMode::Multiply => 7,
        BlendMode::Screen => 8,
        BlendMode::Difference => 23,
        BlendMode::Normal => 0,
        _ => 0,
    }
}

// ===========================================================================
// Slices (id 1050)
// ===========================================================================

fn ltrb_from_bounds_desc(desc: &Descriptor) -> LtrbBounds {
    LtrbBounds {
        top: get_double(desc, "Top ").unwrap_or(0.0),
        left: get_double(desc, "Left").unwrap_or(0.0),
        bottom: get_double(desc, "Btom").unwrap_or(0.0),
        right: get_double(desc, "Rght").unwrap_or(0.0),
    }
}

fn bounds_desc_from_ltrb(b: &LtrbBounds) -> Descriptor {
    let mut d = Descriptor::new("", "Rct1");
    d.set("Top ", DescriptorValue::Integer(b.top as i32));
    d.set("Left", DescriptorValue::Integer(b.left as i32));
    d.set("Btom", DescriptorValue::Integer(b.bottom as i32));
    d.set("Rght", DescriptorValue::Integer(b.right as i32));
    d
}

fn slice_origin_from_index(i: u32) -> SliceOrigin {
    // ['autoGenerated', 'layer', 'userGenerated'], clamped.
    match i {
        0 => SliceOrigin::AutoGenerated,
        1 => SliceOrigin::Layer,
        _ => SliceOrigin::UserGenerated,
    }
}

fn slice_origin_index(o: SliceOrigin) -> u32 {
    match o {
        SliceOrigin::AutoGenerated => 0,
        SliceOrigin::Layer => 1,
        SliceOrigin::UserGenerated => 2,
    }
}

fn slice_type_from_index(i: u32) -> SliceType {
    // ['noImage', 'image'], clamped.
    if i == 0 {
        SliceType::NoImage
    } else {
        SliceType::Image
    }
}

fn slice_type_index(t: SliceType) -> u32 {
    match t {
        SliceType::NoImage => 0,
        SliceType::Image => 1,
    }
}

fn read_slices(reader: &mut PsdReader, target: &mut ImageResources) -> ReadResult<()> {
    let version = read_uint32(reader)?;

    if version == 6 {
        if target.slices.is_none() {
            target.slices = Some(Vec::new());
        }
        let top = read_int32(reader)? as f64;
        let left = read_int32(reader)? as f64;
        let bottom = read_int32(reader)? as f64;
        let right = read_int32(reader)? as f64;
        let group_name = read_unicode_string(reader)?;
        let count = read_uint32(reader)?;

        let mut slices = Vec::new();
        for _ in 0..count {
            let id = read_uint32(reader)? as f64;
            let group_id = read_uint32(reader)? as f64;
            let origin = slice_origin_from_index(read_uint32(reader)?);
            let associated_layer_id = if origin == SliceOrigin::Layer {
                read_uint32(reader)? as f64
            } else {
                0.0
            };
            let name = read_unicode_string(reader)?;
            let slice_type = slice_type_from_index(read_uint32(reader)?);
            let s_left = read_int32(reader)? as f64;
            let s_top = read_int32(reader)? as f64;
            let s_right = read_int32(reader)? as f64;
            let s_bottom = read_int32(reader)? as f64;
            let url = read_unicode_string(reader)?;
            let s_target = read_unicode_string(reader)?;
            let message = read_unicode_string(reader)?;
            let alt_tag = read_unicode_string(reader)?;
            let cell_text_is_html = read_uint8(reader)? != 0;
            let cell_text = read_unicode_string(reader)?;
            let _horz = read_uint32(reader)?; // clamped to 'default'
            let _vert = read_uint32(reader)?;
            let a = read_uint8(reader)? as f64;
            let r = read_uint8(reader)? as f64;
            let g = read_uint8(reader)? as f64;
            let b = read_uint8(reader)? as f64;
            let background_color_type = if (a + r + g + b) == 0.0 {
                SliceBackgroundColorType::None
            } else if a == 0.0 {
                SliceBackgroundColorType::Matte
            } else {
                SliceBackgroundColorType::Color
            };
            slices.push(Slice {
                id,
                group_id,
                origin: Some(origin),
                associated_layer_id,
                name: Some(name),
                slice_type: Some(slice_type),
                bounds: LtrbBounds {
                    top: s_top,
                    left: s_left,
                    bottom: s_bottom,
                    right: s_right,
                },
                url,
                target: s_target,
                message,
                alt_tag,
                cell_text_is_html,
                cell_text,
                horizontal_alignment: Some(SliceAlignment::Default),
                vertical_alignment: Some(SliceAlignment::Default),
                background_color_type: Some(background_color_type),
                background_color: Rgba { r, g, b, a },
                top_outset: None,
                left_outset: None,
                bottom_outset: None,
                right_outset: None,
            });
        }

        let desc = read_version_and_descriptor(reader)?;
        if let Some(slice_list) = get_list(&desc, "slices") {
            for d in slice_list {
                if let DescriptorValue::Descriptor(d) = d {
                    let slice_id = get_double(d, "sliceID").unwrap_or(0.0);
                    if let Some(slice) = slices.iter_mut().find(|s| s.id == slice_id) {
                        slice.top_outset = get_double(d, "topOutset");
                        slice.left_outset = get_double(d, "leftOutset");
                        slice.bottom_outset = get_double(d, "bottomOutset");
                        slice.right_outset = get_double(d, "rightOutset");
                    }
                }
            }
        }

        target.slices.as_mut().unwrap().push(SliceGroup {
            bounds: LtrbBounds {
                top,
                left,
                bottom,
                right,
            },
            group_name,
            slices,
        });
    } else if version == 7 || version == 8 {
        let desc = read_version_and_descriptor(reader)?;
        if target.slices.is_none() {
            target.slices = Some(Vec::new());
        }
        let bounds = get_descriptor(&desc, "bounds")
            .map(ltrb_from_bounds_desc)
            .unwrap_or_default();
        let mut slices = Vec::new();
        if let Some(list) = get_list(&desc, "slices") {
            for item in list {
                if let DescriptorValue::Descriptor(s) = item {
                    let origin = eslice_origin_codec()
                        .decode(&get_enum(s, "origin").unwrap_or_default())
                        .ok();
                    let slice_type = eslice_type_codec()
                        .decode(&get_enum(s, "Type").unwrap_or_default())
                        .ok();
                    let bg = get_descriptor(s, "bgColor");
                    let background_color = bg
                        .map(|c| Rgba {
                            r: get_double(c, "Rd  ").unwrap_or(0.0),
                            g: get_double(c, "Grn ").unwrap_or(0.0),
                            b: get_double(c, "Bl  ").unwrap_or(0.0),
                            a: get_double(c, "alpha").unwrap_or(0.0),
                        })
                        .unwrap_or(Rgba {
                            r: 0.0,
                            g: 0.0,
                            b: 0.0,
                            a: 0.0,
                        });
                    slices.push(Slice {
                        name: get_text(s, "Nm  "),
                        id: get_double(s, "sliceID").unwrap_or(0.0),
                        group_id: get_double(s, "groupID").unwrap_or(0.0),
                        associated_layer_id: 0.0,
                        origin: origin.map(|o| match o.as_str() {
                            "autoGenerated" => SliceOrigin::AutoGenerated,
                            "layer" => SliceOrigin::Layer,
                            _ => SliceOrigin::UserGenerated,
                        }),
                        slice_type: slice_type.map(|t| {
                            if t == "noImage" {
                                SliceType::NoImage
                            } else {
                                SliceType::Image
                            }
                        }),
                        bounds: get_descriptor(s, "bounds")
                            .map(ltrb_from_bounds_desc)
                            .unwrap_or_default(),
                        url: get_text(s, "url").unwrap_or_default(),
                        target: get_text(s, "null").unwrap_or_default(),
                        message: get_text(s, "Msge").unwrap_or_default(),
                        alt_tag: get_text(s, "altTag").unwrap_or_default(),
                        cell_text_is_html: get_bool(s, "cellTextIsHTML").unwrap_or(false),
                        cell_text: get_text(s, "cellText").unwrap_or_default(),
                        horizontal_alignment: Some(SliceAlignment::Default),
                        vertical_alignment: Some(SliceAlignment::Default),
                        background_color_type: eslice_bg_codec()
                            .decode(&get_enum(s, "bgColorType").unwrap_or_default())
                            .ok()
                            .map(|t| match t.as_str() {
                                "matte" => SliceBackgroundColorType::Matte,
                                "color" => SliceBackgroundColorType::Color,
                                _ => SliceBackgroundColorType::None,
                            }),
                        background_color,
                        top_outset: Some(get_double(s, "topOutset").unwrap_or(0.0)),
                        left_outset: Some(get_double(s, "leftOutset").unwrap_or(0.0)),
                        bottom_outset: Some(get_double(s, "bottomOutset").unwrap_or(0.0)),
                        right_outset: Some(get_double(s, "rightOutset").unwrap_or(0.0)),
                    });
                }
            }
        }
        target.slices.as_mut().unwrap().push(SliceGroup {
            group_name: get_text(&desc, "baseName").unwrap_or_default(),
            bounds,
            slices,
        });
    } else {
        return Err(ReadError::StrictViolation(format!(
            "Invalid slices version ({version})"
        )));
    }
    Ok(())
}

fn write_slices(writer: &mut PsdWriter, target: &ImageResources, index: usize) {
    let group = &target.slices.as_ref().unwrap()[index];
    let bounds = &group.bounds;

    write_uint32(writer, 6); // version
    write_int32(writer, bounds.top as i32);
    write_int32(writer, bounds.left as i32);
    write_int32(writer, bounds.bottom as i32);
    write_int32(writer, bounds.right as i32);
    write_unicode_string(writer, &group.group_name);
    write_uint32(writer, group.slices.len() as u32);

    for slice in &group.slices {
        let (mut a, mut r, mut g, mut b) = (
            slice.background_color.a,
            slice.background_color.r,
            slice.background_color.g,
            slice.background_color.b,
        );
        match slice.background_color_type {
            Some(SliceBackgroundColorType::None) => {
                a = 0.0;
                r = 0.0;
                g = 0.0;
                b = 0.0;
            }
            Some(SliceBackgroundColorType::Matte) => {
                a = 0.0;
                r = 255.0;
                g = 255.0;
                b = 255.0;
            }
            _ => {}
        }

        write_uint32(writer, slice.id as u32);
        write_uint32(writer, slice.group_id as u32);
        let origin = slice.origin.unwrap_or(SliceOrigin::UserGenerated);
        write_uint32(writer, slice_origin_index(origin));
        if origin == SliceOrigin::Layer {
            write_uint32(writer, slice.associated_layer_id as u32);
        }
        write_unicode_string(writer, slice.name.as_deref().unwrap_or(""));
        write_uint32(
            writer,
            slice_type_index(slice.slice_type.unwrap_or(SliceType::Image)),
        );
        write_int32(writer, slice.bounds.left as i32);
        write_int32(writer, slice.bounds.top as i32);
        write_int32(writer, slice.bounds.right as i32);
        write_int32(writer, slice.bounds.bottom as i32);
        write_unicode_string(writer, &slice.url);
        write_unicode_string(writer, &slice.target);
        write_unicode_string(writer, &slice.message);
        write_unicode_string(writer, &slice.alt_tag);
        write_uint8(writer, u8::from(slice.cell_text_is_html));
        write_unicode_string(writer, &slice.cell_text);
        write_uint32(writer, 0); // horizontalAlignment -> 'default'
        write_uint32(writer, 0); // verticalAlignment -> 'default'
        write_uint8(writer, a as u8);
        write_uint8(writer, r as u8);
        write_uint8(writer, g as u8);
        write_uint8(writer, b as u8);
    }

    let mut desc = Descriptor::new("", "null");
    desc.set(
        "bounds",
        DescriptorValue::Descriptor(bounds_desc_from_ltrb(bounds)),
    );
    let mut slice_list = Vec::new();
    for s in &group.slices {
        let mut sd = Descriptor::new("", "slcD");
        sd.set("sliceID", DescriptorValue::Integer(s.id as i32));
        sd.set("groupID", DescriptorValue::Integer(s.group_id as i32));
        sd.set(
            "origin",
            DescriptorValue::Enum(
                eslice_origin_codec()
                    .encode(Some(match s.origin.unwrap_or(SliceOrigin::UserGenerated) {
                        SliceOrigin::AutoGenerated => "autoGenerated",
                        SliceOrigin::Layer => "layer",
                        SliceOrigin::UserGenerated => "userGenerated",
                    }))
                    .unwrap(),
            ),
        );
        sd.set(
            "Type",
            DescriptorValue::Enum(
                eslice_type_codec()
                    .encode(Some(match s.slice_type.unwrap_or(SliceType::Image) {
                        SliceType::NoImage => "noImage",
                        SliceType::Image => "image",
                    }))
                    .unwrap(),
            ),
        );
        sd.set(
            "bounds",
            DescriptorValue::Descriptor(bounds_desc_from_ltrb(&s.bounds)),
        );
        if let Some(name) = &s.name {
            sd.set("Nm  ", DescriptorValue::Text(name.clone()));
        }
        sd.set("url", DescriptorValue::Text(s.url.clone()));
        sd.set("null", DescriptorValue::Text(s.target.clone()));
        sd.set("Msge", DescriptorValue::Text(s.message.clone()));
        sd.set("altTag", DescriptorValue::Text(s.alt_tag.clone()));
        sd.set("cellTextIsHTML", DescriptorValue::Boolean(s.cell_text_is_html));
        sd.set("cellText", DescriptorValue::Text(s.cell_text.clone()));
        sd.set(
            "horzAlign",
            DescriptorValue::Enum(eslice_horz_codec().encode(Some("default")).unwrap()),
        );
        sd.set(
            "vertAlign",
            DescriptorValue::Enum(eslice_vert_codec().encode(Some("default")).unwrap()),
        );
        sd.set(
            "bgColorType",
            DescriptorValue::Enum(
                eslice_bg_codec()
                    .encode(Some(
                        match s.background_color_type.unwrap_or(SliceBackgroundColorType::None) {
                            SliceBackgroundColorType::None => "none",
                            SliceBackgroundColorType::Matte => "matte",
                            SliceBackgroundColorType::Color => "color",
                        },
                    ))
                    .unwrap(),
            ),
        );
        if s.background_color_type == Some(SliceBackgroundColorType::Color) {
            let mut bg = Descriptor::new("", "RGBC");
            bg.set("Rd  ", DescriptorValue::Integer(s.background_color.r as i32));
            bg.set("Grn ", DescriptorValue::Integer(s.background_color.g as i32));
            bg.set("Bl  ", DescriptorValue::Integer(s.background_color.b as i32));
            bg.set("alpha", DescriptorValue::Integer(s.background_color.a as i32));
            sd.set("bgColor", DescriptorValue::Descriptor(bg));
        }
        sd.set("topOutset", DescriptorValue::Integer(s.top_outset.unwrap_or(0.0) as i32));
        sd.set("leftOutset", DescriptorValue::Integer(s.left_outset.unwrap_or(0.0) as i32));
        sd.set(
            "bottomOutset",
            DescriptorValue::Integer(s.bottom_outset.unwrap_or(0.0) as i32),
        );
        sd.set(
            "rightOutset",
            DescriptorValue::Integer(s.right_outset.unwrap_or(0.0) as i32),
        );
        slice_list.push(DescriptorValue::Descriptor(sd));
    }
    desc.set("slices", DescriptorValue::List(slice_list));
    write_version_and_descriptor(writer, &desc);
}

// ===========================================================================
// Thumbnail (ids 1033 / 1036)
//
// JPEG encode/decode is NOT ported (jpeg.rs is a stub), so the compressed JPEG
// payload is kept/emitted as raw bytes via `thumbnail_raw`.
// TODO: jpeg.rs encode/decode when ported.
// ===========================================================================

fn read_thumbnail(
    reader: &mut PsdReader,
    target: &mut ImageResources,
    left: usize,
) -> ReadResult<()> {
    let start = reader.offset;
    let format = read_uint32(reader)?; // 1 = kJpegRGB, 0 = kRawRGB
    let width = read_uint32(reader)? as f64;
    let height = read_uint32(reader)? as f64;
    let _width_bytes = read_uint32(reader)?;
    let _total_size = read_uint32(reader)?;
    let _size_after_compression = read_uint32(reader)?;
    let bits_per_pixel = read_uint16(reader)?; // 24
    let planes = read_uint16(reader)?; // 1

    let consumed = reader.offset - start;
    let remaining = left.saturating_sub(consumed);

    if format != 1 || bits_per_pixel != 24 || planes != 1 {
        skip_bytes(reader, remaining);
        return Ok(());
    }

    let data = read_bytes(reader, remaining)?;
    // TODO: jpeg.rs decode when ported — keep raw compressed bytes for now.
    target.thumbnail_raw = Some(ThumbnailRaw {
        width,
        height,
        data,
    });
    Ok(())
}

fn write_thumbnail(writer: &mut PsdWriter, target: &ImageResources) {
    let mut width = 0.0;
    let mut height = 0.0;
    let mut data: Vec<u8> = Vec::new();

    if let Some(raw) = &target.thumbnail_raw {
        width = raw.width;
        height = raw.height;
        data = raw.data.clone();
    }
    // TODO: jpeg.rs encode when ported — would encode `target.thumbnail` canvas.

    let bits_per_pixel = 24.0_f64;
    let width_bytes = ((width * bits_per_pixel + 31.0) / 32.0).floor() * 4.0;
    let planes = 1.0_f64;
    let total_size = width_bytes * height * planes;
    let size_after_compression = data.len() as f64;

    write_uint32(writer, 1); // 1 = kJpegRGB
    write_uint32(writer, width as u32);
    write_uint32(writer, height as u32);
    write_uint32(writer, width_bytes as u32);
    write_uint32(writer, total_size as u32);
    write_uint32(writer, size_after_compression as u32);
    write_uint16(writer, bits_per_pixel as u16);
    write_uint16(writer, planes as u16);
    write_bytes(writer, Some(&data));
}

// ===========================================================================
// Animations (id 4000)
// ===========================================================================

fn read_animations(
    reader: &mut PsdReader,
    target: &mut ImageResources,
    left: usize,
) -> ReadResult<()> {
    let key = read_signature(reader)?;

    if key == "mani" {
        check_signature(reader, "IRFR", None)?;
        read_section(
            reader,
            1,
            |reader, sect_left| {
                while sect_left(reader) > 0 {
                    check_signature(reader, "8BIM", None)?;
                    let sub_key = read_signature(reader)?;
                    read_section(
                        reader,
                        1,
                        |reader, inner_left| {
                            if sub_key == "AnDs" {
                                let desc = read_version_and_descriptor(reader)?;
                                target.animations = Some(parse_animations(&desc));
                            } else {
                                // 'Roll' or unhandled — skip bytes.
                                let n = inner_left(reader);
                                skip_bytes(reader, n);
                            }
                            Ok(())
                        },
                        true,
                        false,
                    )?;
                }
                Ok(())
            },
            true,
            false,
        )?;
    } else {
        // 'mopt' or unhandled — skip the remaining bytes of the block.
        let consumed = 4; // signature
        skip_bytes(reader, left.saturating_sub(consumed));
    }
    Ok(())
}

fn parse_animations(desc: &Descriptor) -> Animations {
    let mut frames = Vec::new();
    if let Some(list) = get_list(desc, "FrIn") {
        for item in list {
            if let DescriptorValue::Descriptor(x) = item {
                let dispose = get_enum(x, "FrDs")
                    .and_then(|s| frmd_codec().decode(&s).ok())
                    .map(|d| match d.as_str() {
                        "none" => AnimationDispose::None,
                        "dispose" => AnimationDispose::Dispose,
                        _ => AnimationDispose::Auto,
                    })
                    .unwrap_or(AnimationDispose::Auto);
                frames.push(AnimationFrameInfo {
                    id: get_double(x, "FrID").unwrap_or(0.0),
                    delay: get_double(x, "FrDl").unwrap_or(0.0) / 100.0,
                    dispose: Some(dispose),
                });
            }
        }
    }
    let mut animations = Vec::new();
    if let Some(list) = get_list(desc, "FSts") {
        for item in list {
            if let DescriptorValue::Descriptor(x) = item {
                let mut fr = Vec::new();
                if let Some(fslist) = get_list(x, "FsFr") {
                    for f in fslist {
                        if let DescriptorValue::Integer(i) = f {
                            fr.push(*i as f64);
                        }
                    }
                }
                animations.push(AnimationInfo {
                    id: get_double(x, "FsID").unwrap_or(0.0),
                    frames: fr,
                    repeats: Some(get_double(x, "LCnt").unwrap_or(0.0)),
                    active_frame: Some(get_double(x, "AFrm").unwrap_or(0.0)),
                });
            }
        }
    }
    Animations { frames, animations }
}

fn write_animations(writer: &mut PsdWriter, target: &ImageResources) {
    let Some(animations) = &target.animations else {
        return;
    };
    write_signature(writer, "mani");
    write_signature(writer, "IRFR");
    write_section(
        writer,
        1,
        |writer| {
            write_signature(writer, "8BIM");
            write_signature(writer, "AnDs");
            write_section(
                writer,
                1,
                |writer| {
                    let mut desc = Descriptor::new("", "null");
                    let mut fr_in = Vec::new();
                    for f in &animations.frames {
                        let mut frame = Descriptor::new("", "AnFr");
                        frame.set("FrID", DescriptorValue::Integer(f.id as i32));
                        if f.delay != 0.0 {
                            frame.set(
                                "FrDl",
                                DescriptorValue::Integer((f.delay * 100.0) as i32),
                            );
                        }
                        frame.set(
                            "FrDs",
                            DescriptorValue::Enum(
                                frmd_codec()
                                    .encode(Some(match f.dispose.unwrap_or(AnimationDispose::Auto) {
                                        AnimationDispose::Auto => "auto",
                                        AnimationDispose::None => "none",
                                        AnimationDispose::Dispose => "dispose",
                                    }))
                                    .unwrap(),
                            ),
                        );
                        fr_in.push(DescriptorValue::Descriptor(frame));
                    }
                    desc.set("FrIn", DescriptorValue::List(fr_in));

                    let mut f_sts = Vec::new();
                    for a in &animations.animations {
                        let mut anim = Descriptor::new("", "AnSt");
                        anim.set("FsID", DescriptorValue::Integer(a.id as i32));
                        anim.set(
                            "AFrm",
                            DescriptorValue::Integer(a.active_frame.unwrap_or(0.0) as i32),
                        );
                        let frames = a
                            .frames
                            .iter()
                            .map(|f| DescriptorValue::Integer(*f as i32))
                            .collect();
                        anim.set("FsFr", DescriptorValue::List(frames));
                        anim.set(
                            "LCnt",
                            DescriptorValue::Integer(a.repeats.unwrap_or(0.0) as i32),
                        );
                        f_sts.push(DescriptorValue::Descriptor(anim));
                    }
                    desc.set("FSts", DescriptorValue::List(f_sts));

                    write_version_and_descriptor(writer, &desc);
                },
                false,
                false,
            );
        },
        false,
        false,
    );
}

// Reference MOCK_HANDLERS so the import is considered used even though the
// gated handlers are not ported (helpers::MOCK_HANDLERS == false).
#[allow(dead_code)]
const _MOCK: bool = MOCK_HANDLERS;

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::reader::PsdReader;
    use crate::writer::{create_writer, get_writer_buffer};

    /// Mirror the resource-block framing: signature '8BIM', id (u16), pascal name,
    /// size (u32), then padded-to-even payload. Used to test framing round-trip.
    fn write_block(id: u16, name: &str, target: &ImageResources, index: usize) -> Vec<u8> {
        let mut w = create_writer(256);
        write_signature(&mut w, "8BIM");
        write_uint16(&mut w, id);
        // pascal name padded to 2 (mirror writePascalString(name, 2)).
        crate::writer::write_pascal_string(&mut w, name, 2);
        write_section(
            &mut w,
            2,
            |w| {
                write_image_resource(id, w, target, index).unwrap();
            },
            false,
            false,
        );
        get_writer_buffer(&w)
    }

    /// Read back a framed block, returning (id, remaining payload consumed via handler).
    fn read_block(bytes: &[u8]) -> ImageResources {
        let mut r = PsdReader::new(bytes, None, None);
        check_signature(&mut r, "8BIM", None).unwrap();
        let id = read_uint16(&mut r).unwrap();
        let _name = crate::reader::read_pascal_string(&mut r, 2).unwrap();
        let mut target = ImageResources::default();
        // read the section length, then dispatch with `left`.
        let len = read_uint32(&mut r).unwrap() as usize;
        let payload_start = r.offset;
        read_image_resource(id, &mut r, &mut target, len).unwrap();
        // advance to padded end (pad-to-even)
        let mut end = payload_start + len;
        if len % 2 != 0 {
            end += 1;
        }
        r.offset = end;
        target
    }

    #[test]
    fn resolution_info_round_trip() {
        let mut target = ImageResources::default();
        target.resolution_info = Some(ResolutionInfo {
            horizontal_resolution: 300.0,
            horizontal_resolution_unit: ResolutionUnit::Ppi,
            width_unit: DimensionUnit::Inches,
            vertical_resolution: 300.0,
            vertical_resolution_unit: ResolutionUnit::Ppcm,
            height_unit: DimensionUnit::Centimeters,
        });

        let mut w = create_writer(64);
        write_image_resource(1005, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1005, &mut r, &mut out, bytes.len()).unwrap();

        let a = target.resolution_info.unwrap();
        let b = out.resolution_info.unwrap();
        assert_eq!(a.horizontal_resolution, b.horizontal_resolution);
        assert_eq!(a.horizontal_resolution_unit, b.horizontal_resolution_unit);
        assert_eq!(a.width_unit, b.width_unit);
        assert_eq!(a.vertical_resolution, b.vertical_resolution);
        assert_eq!(a.vertical_resolution_unit, b.vertical_resolution_unit);
        assert_eq!(a.height_unit, b.height_unit);
    }

    #[test]
    fn xmp_metadata_string_round_trip() {
        let mut target = ImageResources::default();
        target.xmp_metadata = Some("<x:xmpmeta>data</x:xmpmeta>".to_string());

        let mut w = create_writer(64);
        write_image_resource(1060, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1060, &mut r, &mut out, bytes.len()).unwrap();
        assert_eq!(out.xmp_metadata, target.xmp_metadata);
    }

    #[test]
    fn caption_digest_round_trip() {
        let mut target = ImageResources::default();
        target.caption_digest = Some("0123456789abcdef0123456789abcdef".to_string());

        let mut w = create_writer(64);
        write_image_resource(1061, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);
        assert_eq!(bytes.len(), 16);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1061, &mut r, &mut out, bytes.len()).unwrap();
        assert_eq!(out.caption_digest, target.caption_digest);
    }

    #[test]
    fn framing_odd_length_pad() {
        // url resource: ASCII string of odd length forces pad-to-even on the block.
        let mut target = ImageResources::default();
        target.url = Some("abc".to_string()); // 3 bytes -> needs 1 pad byte

        let bytes = write_block(1035, "", &target, 0);
        // Find the section length and assert payload is padded to even.
        // Layout: 4 (8BIM) + 2 (id) + pascal name (1 len byte + 0 chars + 1 pad = 2)
        //         + 4 (size) + payload(3) + pad(1)
        let header = 4 + 2 + 2 + 4;
        let payload_len = 3usize;
        assert_eq!(bytes.len(), header + payload_len + 1); // +1 pad to even

        let out = read_block(&bytes);
        assert_eq!(out.url, target.url);
    }

    #[test]
    fn print_scale_round_trip() {
        let mut target = ImageResources::default();
        target.print_scale = Some(PrintScale {
            style: Some(PrintScaleStyle::UserDefined),
            x: Some(1.5),
            y: Some(2.5),
            scale: Some(0.75),
        });

        let mut w = create_writer(64);
        write_image_resource(1062, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1062, &mut r, &mut out, bytes.len()).unwrap();
        let p = out.print_scale.unwrap();
        assert_eq!(p.style, Some(PrintScaleStyle::UserDefined));
        assert_eq!(p.x, Some(1.5));
        assert_eq!(p.y, Some(2.5));
        assert!((p.scale.unwrap() - 0.75).abs() < 1e-6);
    }

    #[test]
    fn version_info_round_trip() {
        let mut target = ImageResources::default();
        target.version_info = Some(VersionInfo {
            has_real_merged_data: true,
            writer_name: "ag-psd".to_string(),
            reader_name: "ag-psd".to_string(),
            file_version: 1.0,
        });

        let mut w = create_writer(64);
        write_image_resource(1057, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1057, &mut r, &mut out, bytes.len()).unwrap();
        let v = out.version_info.unwrap();
        assert!(v.has_real_merged_data);
        assert_eq!(v.writer_name, "ag-psd");
        assert_eq!(v.reader_name, "ag-psd");
        assert_eq!(v.file_version, 1.0);
    }

    #[test]
    fn grid_and_guides_round_trip() {
        let mut target = ImageResources::default();
        target.grid_and_guides_information = Some(GridAndGuidesInformation {
            grid: Some(GridInfo {
                horizontal: 576.0,
                vertical: 576.0,
            }),
            guides: Some(vec![
                GuideInfo {
                    location: 10.0,
                    direction: GuideDirection::Horizontal,
                },
                GuideInfo {
                    location: 20.0,
                    direction: GuideDirection::Vertical,
                },
            ]),
        });

        let mut w = create_writer(64);
        write_image_resource(1032, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1032, &mut r, &mut out, bytes.len()).unwrap();
        let info = out.grid_and_guides_information.unwrap();
        let grid = info.grid.unwrap();
        assert_eq!(grid.horizontal, 576.0);
        assert_eq!(grid.vertical, 576.0);
        let guides = info.guides.unwrap();
        assert_eq!(guides.len(), 2);
        assert_eq!(guides[0].location, 10.0);
        assert_eq!(guides[0].direction, GuideDirection::Horizontal);
        assert_eq!(guides[1].direction, GuideDirection::Vertical);
    }

    #[test]
    fn alpha_identifiers_round_trip() {
        let mut target = ImageResources::default();
        target.alpha_identifiers = Some(vec![1.0, 2.0, 3.0]);

        let mut w = create_writer(64);
        write_image_resource(1053, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1053, &mut r, &mut out, bytes.len()).unwrap();
        assert_eq!(out.alpha_identifiers, Some(vec![1.0, 2.0, 3.0]));
    }

    #[test]
    fn url_list_round_trip() {
        let mut target = ImageResources::default();
        target.urls_list = Some(vec![UrlListItem {
            id: 7.0,
            r#ref: "slice".to_string(),
            url: "http://example.com".to_string(),
        }]);

        let mut w = create_writer(128);
        write_image_resource(1054, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1054, &mut r, &mut out, bytes.len()).unwrap();
        let list = out.urls_list.unwrap();
        assert_eq!(list.len(), 1);
        assert_eq!(list[0].id, 7.0);
        assert_eq!(list[0].url, "http://example.com");
        assert_eq!(list[0].r#ref, "slice");
    }

    #[test]
    fn pixel_aspect_ratio_round_trip() {
        let mut target = ImageResources::default();
        target.pixel_aspect_ratio = Some(PixelAspectRatio { aspect: 1.25 });

        let mut w = create_writer(64);
        write_image_resource(1064, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1064, &mut r, &mut out, bytes.len()).unwrap();
        assert_eq!(out.pixel_aspect_ratio.unwrap().aspect, 1.25);
    }

    #[test]
    fn animations_round_trip() {
        let mut target = ImageResources::default();
        target.animations = Some(Animations {
            frames: vec![
                AnimationFrameInfo {
                    id: 1.0,
                    delay: 0.1,
                    dispose: Some(AnimationDispose::Auto),
                },
                AnimationFrameInfo {
                    id: 2.0,
                    delay: 0.0,
                    dispose: Some(AnimationDispose::None),
                },
            ],
            animations: vec![AnimationInfo {
                id: 10.0,
                frames: vec![1.0, 2.0],
                repeats: Some(3.0),
                active_frame: Some(1.0),
            }],
        });

        let mut w = create_writer(512);
        write_image_resource(4000, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(4000, &mut r, &mut out, bytes.len()).unwrap();
        let a = out.animations.unwrap();
        assert_eq!(a.frames.len(), 2);
        assert_eq!(a.frames[0].id, 1.0);
        assert!((a.frames[0].delay - 0.1).abs() < 1e-6);
        assert_eq!(a.frames[1].dispose, Some(AnimationDispose::None));
        assert_eq!(a.animations.len(), 1);
        assert_eq!(a.animations[0].id, 10.0);
        assert_eq!(a.animations[0].frames, vec![1.0, 2.0]);
        assert_eq!(a.animations[0].repeats, Some(3.0));
    }

    #[test]
    fn slices_round_trip() {
        let mut target = ImageResources::default();
        target.slices = Some(vec![SliceGroup {
            bounds: LtrbBounds {
                left: 0.0,
                top: 0.0,
                right: 100.0,
                bottom: 80.0,
            },
            group_name: "group".to_string(),
            slices: vec![Slice {
                id: 1.0,
                group_id: 0.0,
                origin: Some(SliceOrigin::UserGenerated),
                associated_layer_id: 0.0,
                name: Some("slice 1".to_string()),
                slice_type: Some(SliceType::Image),
                bounds: LtrbBounds {
                    left: 1.0,
                    top: 2.0,
                    right: 3.0,
                    bottom: 4.0,
                },
                url: "u".to_string(),
                target: "t".to_string(),
                message: "m".to_string(),
                alt_tag: "a".to_string(),
                cell_text_is_html: true,
                cell_text: "c".to_string(),
                horizontal_alignment: Some(SliceAlignment::Default),
                vertical_alignment: Some(SliceAlignment::Default),
                background_color_type: Some(SliceBackgroundColorType::None),
                background_color: Rgba {
                    r: 0.0,
                    g: 0.0,
                    b: 0.0,
                    a: 0.0,
                },
                top_outset: None,
                left_outset: None,
                bottom_outset: None,
                right_outset: None,
            }],
        }]);

        let mut w = create_writer(1024);
        write_image_resource(1050, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1050, &mut r, &mut out, bytes.len()).unwrap();
        let groups = out.slices.unwrap();
        assert_eq!(groups.len(), 1);
        let g = &groups[0];
        assert_eq!(g.group_name, "group");
        assert_eq!(g.bounds.right, 100.0);
        assert_eq!(g.slices.len(), 1);
        let s = &g.slices[0];
        assert_eq!(s.id, 1.0);
        assert_eq!(s.name.as_deref(), Some("slice 1"));
        assert_eq!(s.slice_type, Some(SliceType::Image));
        assert_eq!(s.url, "u");
        assert_eq!(s.cell_text_is_html, true);
        assert_eq!(s.bounds.left, 1.0);
        assert_eq!(s.bounds.bottom, 4.0);
    }

    #[test]
    fn print_information_round_trip() {
        let mut target = ImageResources::default();
        target.print_information = Some(PrintInformation {
            printer_manages_colors: None,
            printer_name: Some("My Printer".to_string()),
            printer_profile: Some("sRGB".to_string()),
            print_sixteen_bit: Some(false),
            rendering_intent: Some(RenderingIntent::RelativeColorimetric),
            hard_proof: Some(true),
            black_point_compensation: Some(true),
            proof_setup: Some(ProofSetup::Builtin {
                builtin: "proofCMYK".to_string(),
            }),
        });

        let mut w = create_writer(512);
        write_image_resource(1082, &mut w, &target, 0).unwrap();
        let bytes = get_writer_buffer(&w);

        let mut r = PsdReader::new(&bytes, None, None);
        let mut out = ImageResources::default();
        read_image_resource(1082, &mut r, &mut out, bytes.len()).unwrap();
        let info = out.print_information.unwrap();
        assert_eq!(info.printer_name.as_deref(), Some("My Printer"));
        assert_eq!(info.printer_profile.as_deref(), Some("sRGB"));
        assert_eq!(info.rendering_intent, Some(RenderingIntent::RelativeColorimetric));
        assert_eq!(info.black_point_compensation, Some(true));
        match info.proof_setup {
            Some(ProofSetup::Builtin { builtin }) => assert_eq!(builtin, "proofCMYK"),
            _ => panic!("expected builtin proof setup"),
        }
    }
}