jixel 0.2.24

JPEG XL encoder
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
/*
 * // Copyright (c) Radzivon Bartoshyk 5/2026. All rights reserved.
 * //
 * // Redistribution and use in source and binary forms, with or without modification,
 * // are permitted provided that the following conditions are met:
 * //
 * // 1.  Redistributions of source code must retain the above copyright notice, this
 * // list of conditions and the following disclaimer.
 * //
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
 * // this list of conditions and the following disclaimer in the documentation
 * // and/or other materials provided with the distribution.
 * //
 * // 3.  Neither the name of the copyright holder nor the names of its
 * // contributors may be used to endorse or promote products derived from
 * // this software without specific prior written permission.
 * //
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#[path = "lossless_lz77.rs"]
mod lz77;
#[path = "lossless_ma_lookup.rs"]
mod ma_lookup;
#[path = "lossless_palette.rs"]
mod palette;
#[path = "lossless_predictor.rs"]
mod predictor;
#[path = "lossless_rct.rs"]
mod rct;
#[path = "lossless_squeeze.rs"]
mod squeeze;
#[path = "lossless_tokens.rs"]
mod tokens;
use ma_lookup::MaLookup;
use tokens::{RawTokenStreams, RawTokens};

use crate::adaptive_quant::dirty_log2f;
use crate::bit_writer::BitWriter;
use crate::coder_scratch::{CoderScratch, LZ77_MAX_CONTEXTS};
use crate::encode_image::AlphaPlane;
use crate::entropy::{
    OwnedEntropyCode, Token, optimize_entropy_code, pack_signed, uint_encode, write_entropy_code,
    write_token,
};
use crate::image::Image3Si;
use crate::ma_tree::{
    LearnedTree, MA_REF_CHANNELS, MaLearnParams, MaNode, MaSamples, NUM_MA_PREDS, NUM_MA_PROPS,
    deepen_ma_tree, learn_ma_tree, learn_ma_tree_indexed,
};
use crate::patches::{
    ModularFrameKind, NUM_PATCH_CONTEXTS, PATCH_REF_ID, PATCH_TILE, PatchReference,
    find_lossless_patches,
};
use crate::thread_pool::ThreadPool;
use crate::weighted_predictor::{WpNeighbors, WpParams, WpState, write_wp_header};
use crate::xyb::quantize_xyb_channels;
pub(crate) use lz77::LzToken;
use lz77::{
    LZ77_MIN_SYMBOL, RunLzWriter, build_lz_pixel_code, build_lz_pixel_code_threads,
    estimate_coded_bits, estimate_literal_and_run_bits, estimate_streams_bits,
    lz77_compress_channels_for_speed, lz77_compress_channels_for_speed_with_depth,
    lz77_compress_for_speed, lz77_compress_for_speed_with_depth, lz77_run_count,
    write_local_tree_lz77, write_lz_section, write_tree_lz77,
};
use palette::{
    PALETTE_COARSE_MARGIN, PALETTE_FINAL_MARGIN, build_global_palette,
    try_encode_local_palette_multi_group, try_encode_palette_single_group, write_palette_transform,
};
pub(crate) use predictor::{GradPackInteriorFn, GradientScratch, selected_grad_pack_interior_fn};
use predictor::{
    PREDICTOR_GRADIENT, PREDICTOR_WEIGHTED, PredictorNeighbors, channel_to_context,
    choose_predictor_for_plane, choose_predictors_with_wp, choose_wp_params, fixed_predictor,
    predictor_neighbors, predictor_value, tokenize_all, tokenize_channels_with_wp, tokenize_plane,
    tokenize_plane_rows, tokenize_runs_with_wp,
};
pub(crate) use rct::forward_ycocg;
use rct::{rank_rcts, rct_planes as rct_planes_fn, write_rct_transform};
pub(crate) use squeeze::{encode_frame_lossy_modular_squeeze, lm_calibrated_distance};
use squeeze::{encode_squeeze_multigroup, encode_squeeze_single_group};
use std::sync::{Condvar, Mutex};

const TREE_CTX_SPLIT_VAL: u32 = 0;
const TREE_CTX_PROPERTY: u32 = 1;
const TREE_CTX_PREDICTOR: u32 = 2;
const TREE_CTX_OFFSET: u32 = 3;
const TREE_CTX_MULTIPLIER_LOG: u32 = 4;
const TREE_CTX_MULTIPLIER_BITS: u32 = 5;
const NUM_TREE_CONTEXTS: usize = 6;

/// Modular group layout: the frame header's `group_size_shift`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct GroupLayout {
    shift: u32,
}

impl GroupLayout {
    /// 256-pixel groups: the codestream default and libjxl's lossless choice.
    pub(crate) const DEFAULT: Self = Self { shift: 1 };
    /// 1024-pixel groups. Every group edge costs its first row and column
    /// their north/west context and restarts the WP state, so Slow lossless
    /// prefers the largest groups whenever the learner's estimate agrees.
    pub(crate) const LARGE: Self = Self { shift: 3 };

    pub(crate) const fn dim(self) -> usize {
        128 << self.shift
    }

    pub(crate) const fn lf_dim(self) -> usize {
        self.dim() * 8
    }
}

const GROUP_DIM: usize = GroupLayout::DEFAULT.dim();
const LF_GROUP_DIM: usize = GroupLayout::DEFAULT.lf_dim();
/// Each active deep-LZ lane owns about 4.7 MiB of hash lookup storage. Eight
/// lanes retain useful parallelism without allowing a many-core encoder to
/// multiply that fixed working set across every worker.
const SLOW_DEEP_LZ_MAX_THREADS: usize = 8;

/// The learned-tree entropy proxy must beat its flat alternative decisively
/// before Slow mode trusts it enough to omit the context/flat final encodes.
/// Near decisions retain the exact byte-level tournament.
const MA_DECISIVE_MIN_SAVINGS: f64 = 0.18;

#[inline]
fn learned_tree_is_decisive(estimated_savings: f64) -> bool {
    estimated_savings >= MA_DECISIVE_MIN_SAVINGS
}

#[inline]
fn group_lz_threads(speed: crate::Speed, pool: &ThreadPool) -> usize {
    if speed == crate::Speed::Slow {
        pool.num_threads().min(SLOW_DEEP_LZ_MAX_THREADS)
    } else {
        pool.num_threads()
    }
}

/// Fixed set of reusable deep-LZ lookup tables for one group-compression
/// phase. Unlike worker-owned scratch, these buffers are dropped as soon as
/// the phase finishes and cannot spread across more than the configured lanes.
struct DeepLzScratchPool {
    available: Mutex<Vec<Vec<u32>>>,
    activity: Condvar,
}

impl DeepLzScratchPool {
    fn new(slots: usize) -> Self {
        Self {
            available: Mutex::new((0..slots.max(1)).map(|_| Vec::new()).collect()),
            activity: Condvar::new(),
        }
    }

    fn with_depth<T>(&self, f: impl FnOnce(&mut Vec<u32>) -> T) -> T {
        let depth = {
            let mut available = self.available.lock().unwrap();
            while available.is_empty() {
                available = self.activity.wait(available).unwrap();
            }
            available.pop().unwrap()
        };

        let mut guard = DeepLzScratchGuard {
            owner: self,
            depth: Some(depth),
        };
        f(guard.depth.as_mut().unwrap())
    }
}

struct DeepLzScratchGuard<'a> {
    owner: &'a DeepLzScratchPool,
    depth: Option<Vec<u32>>,
}

impl Drop for DeepLzScratchGuard<'_> {
    fn drop(&mut self) {
        let depth = self.depth.take().unwrap();
        self.owner.available.lock().unwrap().push(depth);
        self.owner.activity.notify_one();
    }
}

fn zero_alpha_like(alpha: Option<&AlphaPlane>, pixels: usize) -> Option<AlphaPlane> {
    match alpha {
        Some(AlphaPlane::U8(_)) => Some(AlphaPlane::U8(vec![0; pixels])),
        Some(AlphaPlane::U16 { bits, .. }) => Some(AlphaPlane::U16 {
            data: vec![0; pixels],
            bits: *bits,
        }),
        Some(AlphaPlane::F32(_)) => Some(AlphaPlane::F32(vec![0; pixels])),
        None => None,
    }
}

/// Encode one single-channel modular group section from precomputed
/// gradient-predictor residual tokens (token contexts are ignored): a local
/// single-leaf Gradient tree, LZ77 run collapse, and the rANS-capable pixel
/// entropy code shared with the lossless path. Used by the lossy extra-channel
/// (alpha) path, where near-constant planes otherwise pay the 1 bit/pixel
/// prefix-code floor. Returns false (writing nothing) when a residual symbol
/// would collide with the LZ77 length alphabet (huge residuals, e.g. raw-float
/// alpha bits).
pub(crate) fn write_single_channel_lz77_section(
    tokens: &[Token],
    scratch: &mut CoderScratch,
    w: &mut BitWriter,
) -> bool {
    if tokens
        .iter()
        .any(|t| crate::entropy::uint_encode(t.value).0 >= LZ77_MIN_SYMBOL)
    {
        return false;
    }
    let mut runs = RunLzWriter::with_capacity(tokens.len());
    for t in tokens {
        runs.push(Token::new(0, t.value));
    }
    let lz = runs.finish();
    let code = build_lz_pixel_code(
        std::iter::once(lz.as_slice()),
        1,
        LZ77_MIN_SYMBOL,
        true,
        &mut scratch.lz_entropy,
        &mut scratch.huffman_pool,
    );
    // GroupHeader: use_global_tree=0, wp all_default=1, 0 transforms.
    w.write(1, 0);
    w.write(1, 1);
    w.write(2, 0);
    write_local_tree_lz77(
        &[PREDICTOR_GRADIENT],
        &code,
        LZ77_MIN_SYMBOL,
        &mut scratch.huffman_pool,
        w,
    );
    write_lz_section(&lz, 1, &code, LZ77_MIN_SYMBOL, w);
    true
}

/// Independent streams share frozen entropy tables but retain their own ANS
/// state. Bound simultaneous staging memory as well as the worker count.
fn write_lz_groups(
    groups: &[Vec<LzToken>],
    code: &crate::entropy::EntropyCode<'_>,
    distance_context: u32,
    min_symbol: u32,
    wp_params: WpParams,
    pool: &ThreadPool,
    sections: &mut [BitWriter],
) {
    write_lz_groups_with_header(
        groups,
        code,
        distance_context,
        min_symbol,
        pool,
        sections,
        |_, section| {
            section.write(1, 1);
            write_wp_header(wp_params, section);
            section.write(2, 0);
        },
    );
}

fn write_lz_groups_with_header(
    groups: &[Vec<LzToken>],
    code: &crate::entropy::EntropyCode<'_>,
    distance_context: u32,
    min_symbol: u32,
    pool: &ThreadPool,
    sections: &mut [BitWriter],
    write_header: impl Fn(usize, &mut BitWriter) + Sync,
) {
    debug_assert_eq!(groups.len(), sections.len());
    let max_staging = if code.use_prefix_code {
        0
    } else {
        groups
            .iter()
            .map(|g| g.len().saturating_mul(8))
            .max()
            .unwrap_or(0)
    };
    #[allow(clippy::manual_clamp)]
    let lanes = (32 * 1024 * 1024 / max_staging.max(1))
        .max(1)
        .min(SLOW_DEEP_LZ_MAX_THREADS)
        .min(pool.num_threads())
        .min(sections.len());
    let write_group = |i: usize, section: &mut BitWriter| {
        write_header(i, section);
        write_lz_section(&groups[i], distance_context, code, min_symbol, section);
        section.zero_pad_to_byte();
    };
    if lanes <= 1 {
        for (i, section) in sections.iter_mut().enumerate() {
            write_group(i, section);
        }
        return;
    }
    // The caller's entropy scratch is borrowed by `code`. This lightweight
    // scratch supplies the caller lane; section writers need no worker tables.
    let mut writer_scratch = CoderScratch::lossless();
    pool.steal_for_each_mut_with_threads(&mut writer_scratch, sections, lanes, |i, section, _| {
        write_group(i, section);
    });
}

fn keep_smaller_writer(best: &mut Option<BitWriter>, candidate: BitWriter) {
    if best
        .as_ref()
        .is_none_or(|current| candidate.bits_written() < current.bits_written())
    {
        *best = Some(candidate);
    }
}

pub(crate) fn encode_frame_lossless(
    linear: &Image3Si,
    alpha: Option<&AlphaPlane>,
    max_bits: u32,
    progressive: bool,
    patches: bool,
    num_color: usize,
    speed: crate::Speed,
    decoding_speed: crate::DecodingSpeed,
    num_threads: usize,
    writer: &mut BitWriter,
) {
    let pool = ThreadPool::new_lossless(num_threads);
    let mut scratch = Box::new(CoderScratch::lossless());
    encode_frame_lossless_with_pool(
        linear,
        alpha,
        max_bits,
        progressive,
        patches,
        num_color,
        speed,
        decoding_speed,
        &pool,
        &mut scratch,
        writer,
    );
}

#[allow(clippy::too_many_arguments)]
fn encode_frame_lossless_with_pool(
    linear: &Image3Si,
    alpha: Option<&AlphaPlane>,
    max_bits: u32,
    progressive: bool,
    patches: bool,
    num_color: usize,
    speed: crate::Speed,
    decoding_speed: crate::DecodingSpeed,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    writer: &mut BitWriter,
) {
    if patches
        && num_color == 3
        && let Some(plan) = find_lossless_patches(linear, pool, scratch)
    {
        // Patches change prediction, LZ77, entropy histograms, and add both a
        // reference-only frame and a dictionary. Their true cost cannot be
        // inferred reliably from covered pixel area, so encode both complete
        // alternatives and select by their final byte-aligned bit count.
        let mut regular_writer = BitWriter::new();
        let decisive_tree = encode_frame_lossless_core(
            linear,
            alpha,
            max_bits,
            progressive,
            num_color,
            speed,
            decoding_speed,
            pool,
            scratch,
            ModularFrameKind::Regular,
            &mut regular_writer,
        );
        // Patched frames are coded by the flat path only, so they cannot beat
        // a decisive learned tree, and a plan covering little of the image
        // cannot pay for its dictionary and reference frame.
        let covered: usize = plan
            .references
            .iter()
            .map(|r| r.positions.len())
            .sum::<usize>()
            * crate::patches::PATCH_TILE
            * crate::patches::PATCH_TILE;
        let coverage = covered as f64 / (linear.xsize() * linear.ysize()) as f64;
        if decisive_tree || coverage < PATCH_MIN_COVERAGE {
            writer.append(&regular_writer);
            return;
        }

        let mut patched_writer = BitWriter::new();
        let atlas_alpha =
            zero_alpha_like(alpha, plan.atlas.xsize().saturating_mul(plan.atlas.ysize()));
        encode_frame_lossless_core(
            &plan.atlas,
            atlas_alpha.as_ref(),
            max_bits,
            false,
            num_color,
            speed,
            decoding_speed,
            pool,
            scratch,
            ModularFrameKind::ReferenceOnly {
                width: plan.atlas.xsize(),
                height: plan.atlas.ysize(),
            },
            &mut patched_writer,
        );
        encode_frame_lossless_core(
            &plan.base,
            alpha,
            max_bits,
            false,
            num_color,
            speed,
            decoding_speed,
            pool,
            scratch,
            ModularFrameKind::Patched(&plan.references),
            &mut patched_writer,
        );

        if patched_writer.bits_written() < regular_writer.bits_written() {
            writer.append(&patched_writer);
        } else {
            writer.append(&regular_writer);
        }
        return;
    }
    encode_frame_lossless_core(
        linear,
        alpha,
        max_bits,
        progressive,
        num_color,
        speed,
        decoding_speed,
        pool,
        scratch,
        ModularFrameKind::Regular,
        writer,
    );
}

#[allow(clippy::too_many_arguments)]
fn encode_frame_lossless_core(
    linear: &Image3Si,
    alpha: Option<&AlphaPlane>,
    max_bits: u32,
    progressive: bool,
    num_color: usize,
    speed: crate::Speed,
    decoding_speed: crate::DecodingSpeed,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    frame_kind: ModularFrameKind<'_>,
    writer: &mut BitWriter,
) -> bool {
    encode_frame_lossless_core_impl(
        linear,
        alpha,
        max_bits,
        progressive,
        num_color,
        speed,
        decoding_speed,
        pool,
        scratch,
        frame_kind,
        true,
        writer,
    )
}

#[allow(clippy::too_many_arguments)]
fn encode_frame_lossless_core_impl(
    linear: &Image3Si,
    alpha: Option<&AlphaPlane>,
    max_bits: u32,
    progressive: bool,
    num_color: usize,
    speed: crate::Speed,
    decoding_speed: crate::DecodingSpeed,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    frame_kind: ModularFrameKind<'_>,
    allow_palette_candidates: bool,
    writer: &mut BitWriter,
) -> bool {
    let use_wp = decoding_speed.use_weighted_predictor();
    // The hybrid-uint token of a modular residual can reach ~4*B+11 for B-bit
    // input. For B<=13 this stays below 64, so keep the tight default; for higher
    // bit depths raise LZ77_MIN_SYMBOL above the max literal token so the decoder
    // never mistakes a large residual for an LZ77 back-reference (the cause of
    // the old ~12-bit lossless ceiling). Stays under the 128-symbol prefix-code
    // alphabet through ~18-bit.
    let min_symbol: u32 = if max_bits <= 13 {
        LZ77_MIN_SYMBOL
    } else {
        4 * max_bits + 24
    };
    let xsize = linear.xsize();
    let ysize = linear.ysize();
    let nb_chans = num_color + if alpha.is_some() { 1 } else { 0 };

    let xsize_groups = xsize.div_ceil(GROUP_DIM);
    let ysize_groups = ysize.div_ceil(GROUP_DIM);
    let num_ac_groups = xsize_groups * ysize_groups;
    let xsize_dc_groups = xsize.div_ceil(LF_GROUP_DIM);
    let ysize_dc_groups = ysize.div_ceil(LF_GROUP_DIM);
    let num_dc_groups = xsize_dc_groups * ysize_dc_groups;
    let single_group = num_ac_groups == 1;
    let grad_pack_fn = selected_grad_pack_interior_fn();

    // Palette (v2): single-group, RGB/RGBA, <=256 distinct colors. Encodes
    // the image as a small palette meta-channel + an index channel, which is a
    // large win for low-color/graphic content. Falls through to the normal
    // RCT+predictor path when it doesn't apply.
    if allow_palette_candidates && frame_kind.is_regular() && single_group && num_color == 3 {
        let mut palette_writer = BitWriter::new();
        if try_encode_palette_single_group(
            linear,
            alpha,
            xsize,
            ysize,
            min_symbol,
            grad_pack_fn,
            speed,
            use_wp,
            scratch,
            &mut palette_writer,
        ) {
            if speed == crate::Speed::Slow {
                let mut normal_writer = BitWriter::new();
                let normal_decisive = encode_frame_lossless_core_impl(
                    linear,
                    alpha,
                    max_bits,
                    progressive,
                    num_color,
                    speed,
                    decoding_speed,
                    pool,
                    scratch,
                    frame_kind,
                    false,
                    &mut normal_writer,
                );
                if normal_writer.bits_written() < palette_writer.bits_written() {
                    writer.append(&normal_writer);
                    return normal_decisive;
                }
                writer.append(&palette_writer);
            } else {
                writer.append(&palette_writer);
            }
            return false;
        }
    }

    // Large graphics often exceed 256 colors globally while each 256x256 group
    // remains low-color. Encode qualifying RGB/RGBA groups through an exact
    // local Palette transform and leave high-color groups on the normal path.
    if allow_palette_candidates && frame_kind.is_regular() && !single_group && num_color == 3 {
        let mut palette_writer = BitWriter::new();
        if try_encode_local_palette_multi_group(
            linear,
            alpha,
            xsize,
            ysize,
            xsize_groups,
            ysize_groups,
            num_dc_groups,
            min_symbol,
            grad_pack_fn,
            speed,
            use_wp,
            pool,
            scratch,
            &mut palette_writer,
        ) {
            if speed == crate::Speed::Slow {
                let mut normal_writer = BitWriter::new();
                let normal_decisive = encode_frame_lossless_core_impl(
                    linear,
                    alpha,
                    max_bits,
                    progressive,
                    num_color,
                    speed,
                    decoding_speed,
                    pool,
                    scratch,
                    frame_kind,
                    false,
                    &mut normal_writer,
                );
                if normal_writer.bits_written() < palette_writer.bits_written() {
                    writer.append(&normal_writer);
                    return normal_decisive;
                }
                writer.append(&palette_writer);
            } else {
                writer.append(&palette_writer);
            }
            return false;
        }
    }

    // Stage-2 progressive lossless (opt-in): Squeeze pyramid (RGB + optional alpha).
    if frame_kind.is_regular() && single_group && num_color == 3 && progressive {
        encode_squeeze_single_group(
            linear,
            alpha,
            xsize,
            ysize,
            min_symbol,
            grad_pack_fn,
            speed,
            use_wp,
            pool,
            scratch,
            writer,
        );
        return false;
    }

    // Progressive multi-group (Stage A: all squeezed channels fit the global
    // stream). Falls through to the non-progressive path if a channel is still
    // larger than a group (Stage B).
    if frame_kind.is_regular()
        && !single_group
        && num_color == 3
        && progressive
        && encode_squeeze_multigroup(
            linear,
            alpha,
            xsize,
            ysize,
            min_symbol,
            xsize_groups,
            ysize_groups,
            xsize_dc_groups,
            ysize_dc_groups,
            num_dc_groups,
            num_ac_groups,
            grad_pack_fn,
            speed,
            use_wp,
            pool,
            scratch,
            writer,
        )
    {
        return false;
    }

    // Fast uses fixed Weighted prediction and skips all adaptive analysis. Slow
    // searches the supported Slow predictor set per channel by estimated cost
    // (one global choice, used for the tree and every group).
    let adaptive_search = speed == crate::Speed::Slow;

    // RCT re-selection (Slow, regular RGB frames): estimated over the same
    // fixed-context gradient cost libjxl uses. When a non-YCoCg transform
    // wins, the color planes are rebuilt and the frame headers signal it.
    let mut rct_type = 6u32;
    let mut rct_planes: Option<Image3Si> = None;
    let rct_ranked: Vec<(u32, f32)> =
        if frame_kind.is_regular() && adaptive_search && num_color == 3 {
            rank_rcts(linear, xsize, ysize, pool, scratch)
        } else {
            Vec::new()
        };
    if let Some(&(t, _)) = rct_ranked.first()
        && t != 6
    {
        rct_type = t;
        rct_planes = Some(rct_planes_fn(linear, xsize, ysize, t));
    }
    // The palette candidate below needs the untransformed YCoCg planes.
    let ycocg = linear;
    let linear = rct_planes.as_ref().unwrap_or(linear);

    let wp_header_count = if single_group {
        1
    } else {
        2 + num_dc_groups + num_ac_groups
    };
    let mut wp_params = if adaptive_search && use_wp {
        choose_wp_params(linear, alpha, num_color, wp_header_count, pool, scratch)
    } else {
        WpParams::DEFAULT
    };
    // The estimates below remain useful prefilters, but Slow mode keeps every
    // viable finalist and compares its complete byte-aligned frame against the
    // v1 and flat alternatives. Tree headers, clustering, hybrid uint choices,
    // and LZ77 can otherwise reverse the order predicted from residual entropy.
    let compare_tree_candidates = frame_kind.is_regular()
        && adaptive_search
        && num_color == 3
        && decoding_speed.use_ma_trees();
    let mut best_tree_writer: Option<BitWriter> = None;

    // Sources: the RGB(A) planes under the best transform and, for frames
    // with <= 1024 colors, a global palette index image. The corner-sampled
    // WP proxy and the gradient RCT estimator both mis-rank on a third of
    // Kodak (up to 0.7% of the file), so a reduced-leaf learner ranks every
    // WP preset per source (and the transform runner-up for RGB). First the
    // two sources are compared by one first-stage learn each under the proxy
    // preset: a source trailing the leader by more than
    // PALETTE_COARSE_MARGIN is dropped entirely (on palette graphics the RGB
    // tree is ~25% behind and was paying the whole ranking for nothing).
    // Each ranking winner seeds its final learn.
    let (layout, learned_tree_decisive) = {
        let palette = if compare_tree_candidates {
            build_global_palette(ycocg, alpha, xsize, ysize, pool, scratch)
        } else {
            None
        };
        let header_bits = |params: WpParams| {
            if params == WpParams::DEFAULT {
                0.0
            } else {
                (51 * wp_header_count) as f64
            }
        };
        let mut rank_rgb = compare_tree_candidates;
        // Final learned estimate of the RGB frame; gates the palette write.
        let rgb_final_est: f64;
        let mut rank_pal = compare_tree_candidates && palette.is_some();
        let mut rgb_seed: Option<RankedLearn> = None;
        let mut pal_seed: Option<RankedLearn> = None;
        let mut pal_wp = wp_params;
        if compare_tree_candidates && matches!(decoding_speed, crate::DecodingSpeed::Slow) {
            let rgb_stage = {
                let source = rgb_ma_source(linear, alpha, xsize, rct_type);
                rank_coarse(
                    &source, xsize, ysize, min_symbol, wp_params, use_wp, pool, scratch,
                )
            };
            let pal_stage = palette.as_ref().and_then(|p| {
                let source = p.source(xsize, ysize);
                rank_coarse(
                    &source, xsize, ysize, min_symbol, wp_params, use_wp, pool, scratch,
                )
            });
            let coarse_of = |stage: &Option<CoarseLearn>| {
                stage.as_ref().map_or(f64::INFINITY, |s| {
                    s.candidate.est_real + header_bits(wp_params)
                })
            };
            let (rgb_coarse, pal_coarse) = (coarse_of(&rgb_stage), coarse_of(&pal_stage));
            rank_rgb = rgb_coarse <= pal_coarse * PALETTE_COARSE_MARGIN;
            rank_pal = pal_stage.is_some() && pal_coarse <= rgb_coarse * PALETTE_COARSE_MARGIN;

            if rank_rgb {
                let (mut best_bits, mut best_coarse, params, seed) = {
                    let source = rgb_ma_source(linear, alpha, xsize, rct_type);
                    rank_presets(
                        &source,
                        xsize,
                        ysize,
                        min_symbol,
                        use_wp,
                        rgb_stage,
                        wp_params,
                        &header_bits,
                        pool,
                        scratch,
                    )
                };
                wp_params = params;
                rgb_seed = seed;
                // Transform runner-up with the chosen preset, same coarse prune.
                if let Some(&(runner_up, cost)) = rct_ranked.get(1)
                    && cost <= rct_ranked[0].1 * RCT_RANK_MARGIN
                {
                    let planes =
                        (runner_up != 6).then(|| rct_planes_fn(ycocg, xsize, ysize, runner_up));
                    let source =
                        rgb_ma_source(planes.as_ref().unwrap_or(ycocg), alpha, xsize, runner_up);
                    if let Some(stage) = rank_coarse(
                        &source, xsize, ysize, min_symbol, wp_params, use_wp, pool, scratch,
                    ) && stage.candidate.est_real + header_bits(wp_params)
                        <= best_coarse * RANK_COARSE_PRUNE
                    {
                        best_coarse =
                            best_coarse.min(stage.candidate.est_real + header_bits(wp_params));
                        let (bits, ranked) = rank_finish(stage, min_symbol, use_wp, pool, scratch);
                        if bits + header_bits(wp_params) < best_bits {
                            best_bits = bits + header_bits(wp_params);
                            rct_type = runner_up;
                            rct_planes = planes;
                            rgb_seed = Some(ranked);
                        }
                    }
                }
                let _ = (best_bits, best_coarse);
            }
            if rank_pal && let Some(p) = &palette {
                let source = p.source(xsize, ysize);
                let (_, _, params, seed) = rank_presets(
                    &source,
                    xsize,
                    ysize,
                    min_symbol,
                    use_wp,
                    pal_stage,
                    wp_params,
                    &header_bits,
                    pool,
                    scratch,
                );
                pal_wp = params;
                pal_seed = seed;
            }
        }
        let linear = rct_planes.as_ref().unwrap_or(ycocg);

        // Group layout. With every decoding tool available, Slow lossless runs
        // the first learning stage (sampling plus a 32-leaf tree) under both
        // 1024- and 256-pixel groups. The 1024 layout wins on 22/24 Kodak images
        // and is always deepened; the 256 layout is deepened only when its first
        // stage already estimates smaller, and the full estimates then decide
        // (23/24 correct there, where the first stage alone is right 18/24).
        // Layouts are processed one at a time so a single sample set is alive.
        // Everything else keeps the codestream default.
        let layouts: &[GroupLayout] =
            if compare_tree_candidates && matches!(decoding_speed, crate::DecodingSpeed::Slow) {
                &[GroupLayout::LARGE, GroupLayout::DEFAULT]
            } else {
                &[GroupLayout::DEFAULT]
            };
        let (layout, rgb_coarse_est, mut learned_tree_decisive) = {
            let rgb_source = rgb_ma_source(linear, alpha, xsize, rct_type);
            let mut learned_estimated_savings: Option<f64> = None;
            let learned = if rank_rgb {
                learn_best_layout(
                    &rgb_source,
                    xsize,
                    ysize,
                    layouts,
                    min_symbol,
                    wp_params,
                    use_wp,
                    f64::INFINITY,
                    false,
                    rgb_seed,
                    pool,
                    scratch,
                )
            } else {
                None
            };
            let rgb_coarse_est = learned.as_ref().map_or(f64::INFINITY, |(_, _, e)| *e);
            rgb_final_est = learned
                .as_ref()
                .map_or(f64::INFINITY, |(_, c, _)| c.est_real);
            let layout = learned.as_ref().map_or(layouts[0], |(l, _, _)| *l);

            // Learned MA context tree (v2): greedy learned tree over the standard
            // property vector with per-leaf predictors.
            if let Some((_, cand, _)) = &learned {
                let mut candidate = BitWriter::new();
                let estimated_savings = write_learned_tree_frame(
                    &rgb_source,
                    alpha.is_some(),
                    xsize,
                    ysize,
                    layout,
                    min_symbol,
                    pool,
                    scratch,
                    wp_params,
                    use_wp,
                    cand,
                    &mut candidate,
                );
                learned_estimated_savings = Some(estimated_savings);
                keep_smaller_writer(&mut best_tree_writer, candidate);
            }
            (
                layout,
                rgb_coarse_est,
                learned_estimated_savings.is_some_and(learned_tree_is_decisive),
            )
        };
        if learned_tree_decisive {
            // Only the original YCoCg planes are needed for the remaining palette
            // candidate. A decisive learned tree never returns to the RCT-based
            // v1/flat paths, so release those full-image planes before palette work.
            rct_planes = None;
        }

        // Global palette (<= 1024 colors) with its own learned tree, WP preset,
        // layout and literal/LZ77 choice over the index image. libjxl's e7
        // lossless gains ~10% from it on palette graphics; the complete frame
        // competes on size, and a decisive palette tree ends the search like a
        // decisive RGB tree does.
        if rank_pal && let Some(palette) = &palette {
            let palette_source = palette.source(xsize, ysize);
            let coarse_limit = rgb_coarse_est * PALETTE_COARSE_MARGIN;
            if let Some((palette_layout, cand, _)) = learn_best_layout(
                &palette_source,
                xsize,
                ysize,
                layouts,
                min_symbol,
                pal_wp,
                use_wp,
                coarse_limit,
                true,
                pal_seed,
                pool,
                scratch,
            ) {
                let palette_can_win = cand.est_real <= rgb_final_est * PALETTE_FINAL_MARGIN;
                // The frame write (tokenize, code, sections) is the palette candidate's
                // most expensive step after the learn; a final estimate clearly behind
                // the RGB frame's cannot win the size comparison (a 3,195-color
                // screenshot: ratio 1.24, frame +19%; a gray-image tie at 1.000 still writes).
                if palette_can_win {
                    let mut candidate = BitWriter::new();
                    let estimated_savings = write_learned_tree_frame(
                        &palette_source,
                        alpha.is_some(),
                        xsize,
                        ysize,
                        palette_layout,
                        min_symbol,
                        pool,
                        scratch,
                        pal_wp,
                        use_wp,
                        &cand,
                        &mut candidate,
                    );
                    if learned_tree_is_decisive(estimated_savings) {
                        learned_tree_decisive = true;
                        rct_planes = None;
                    }
                    keep_smaller_writer(&mut best_tree_writer, candidate);
                }
            }
        }
        (layout, learned_tree_decisive)
    };

    if learned_tree_decisive {
        writer.append(
            best_tree_writer
                .as_ref()
                .expect("decisive learned-tree candidate"),
        );
        return true;
    }

    let gdim = layout.dim();
    let xsize_groups = xsize.div_ceil(gdim);
    let ysize_groups = ysize.div_ceil(gdim);
    let num_ac_groups = xsize_groups * ysize_groups;
    let num_dc_groups = xsize.div_ceil(layout.lf_dim()) * ysize.div_ceil(layout.lf_dim());
    let single_group = num_ac_groups == 1;

    let linear = rct_planes.as_ref().unwrap_or(ycocg);
    // Per-channel predictor search for the v1 context tree and flat paths
    // (deferred: a decisive learned tree never needs it).
    let predictors = if adaptive_search {
        choose_predictors_with_wp(
            linear, alpha, xsize, ysize, num_color, pool, scratch, wp_params, use_wp,
        )
    } else {
        [fixed_predictor(use_wp); 4]
    };
    // Contiguous per-modular-channel predictors: the `num_color` color channels
    // (Y for gray; Y/Co/Cg for color) followed by alpha. For 3-color this is just
    // predictors[..nb_chans]; for gray it is [Y_pred, (alpha_pred)].
    let chan_preds: Vec<u32> = {
        let mut v: Vec<u32> = (0..num_color).map(|c| predictors[c]).collect();
        if alpha.is_some() {
            v.push(predictors[3]);
        }
        v
    };

    // Context tree (v1): single-group. Splits each channel's entropy context on
    // the WP activity property; a big win on smooth+edge content. Falls through
    // to the flat path when it isn't estimated to help.
    if compare_tree_candidates && use_wp {
        let mut candidate = BitWriter::new();
        if single_group {
            if try_encode_context_tree_single_group(
                linear,
                alpha,
                xsize,
                ysize,
                layout,
                &predictors,
                min_symbol,
                rct_type,
                pool,
                scratch,
                wp_params,
                &mut candidate,
            ) {
                keep_smaller_writer(&mut best_tree_writer, candidate);
            }
        } else if try_encode_context_tree_multi_group(
            linear,
            alpha,
            xsize,
            ysize,
            layout,
            &predictors,
            xsize_groups,
            ysize_groups,
            num_dc_groups,
            min_symbol,
            rct_type,
            pool,
            scratch,
            wp_params,
            &mut candidate,
        ) {
            keep_smaller_writer(&mut best_tree_writer, candidate);
        }
    }

    if !chan_preds.contains(&PREDICTOR_WEIGHTED) {
        wp_params = WpParams::DEFAULT;
    }

    let mut flat_writer = BitWriter::new();
    {
        let writer = if compare_tree_candidates {
            &mut flat_writer
        } else {
            &mut *writer
        };

        write_frame_header_modular_kind(alpha.is_some(), frame_kind, layout, writer);

        if single_group {
            // Single section: GroupHeader + local tree + pixel histograms + pixels.
            let mut section = BitWriter::new();
            if let ModularFrameKind::Patched(references) = frame_kind {
                write_patch_dictionary(references, alpha.is_some(), scratch, &mut section);
            }
            // 1 bit: dc_quant all_default = 1
            section.write(1, 1);
            // 1 bit: has_tree = 0  (no global tree; the local tree lives in the GroupHeader).
            section.write(1, 0);
            // GroupHeader: use_global_tree=0, wp_default=1, RCT transform on R/G/B.
            section.write(1, 0);
            write_wp_header(wp_params, &mut section);
            write_modular_transforms(nb_chans, rct_type, &mut section);

            // Tokenize all channels (post-YCoCg, per-channel contexts).
            let channel_tokens = tokenize_channels_with_wp(
                linear,
                alpha,
                xsize,
                ysize,
                0,
                0,
                xsize,
                ysize,
                num_color,
                &chan_preds,
                grad_pack_fn,
                pool,
                scratch,
                wp_params,
            );

            // LZ77 layer: collapse runs of identical tokens into back-references.
            // The distance context is the (nb_chans)-th context, appended after the
            // per-channel ones.
            let distance_ctx = nb_chans as u32;
            let lz_tokens =
                lz77_compress_channels_for_speed(channel_tokens, distance_ctx, speed, scratch);

            // Per-cluster prefix codes (nb_chans + 1 contexts), balanced N-leaf tree.
            let code = build_lz_pixel_code(
                std::iter::once(lz_tokens.as_slice()),
                nb_chans,
                min_symbol,
                speed == crate::Speed::Slow,
                &mut scratch.lz_entropy,
                &mut scratch.huffman_pool,
            );
            write_local_tree_lz77(
                &chan_preds,
                &code,
                min_symbol,
                &mut scratch.huffman_pool,
                &mut section,
            );

            // Emit the LZ77'd token stream.
            write_lz_section(&lz_tokens, distance_ctx, &code, min_symbol, &mut section);
            section.zero_pad_to_byte();

            // TOC.
            writer.write(1, 0); // no permutation
            writer.zero_pad_to_byte();
            write_toc_entry(section.bits_written() / 8, writer);
            writer.zero_pad_to_byte();
            writer.append_byte_aligned(std::slice::from_mut(&mut section));
            writer.zero_pad_to_byte();
        } else {
            // Multi-group: a single global tree + histograms in DC global, then each
            // AC group emits its tokens against those codes.
            let num_sections = 1 + num_dc_groups + 1 + num_ac_groups;
            let mut sections: Vec<BitWriter> =
                (0..num_sections).map(|_| BitWriter::new()).collect();

            // Tokenize each AC group (sub-image-local neighbors, matching what
            // we'll emit below) and run LZ77 over each group's stream separately so
            // back-references stay within a group's modular sub-image.  Pool the
            // resulting LzToken streams to build a single global prefix code so
            // every per-group emission is guaranteed to be representable.
            let distance_ctx = nb_chans as u32;
            let group_lz_tokens: Vec<Vec<LzToken>> = {
                let deep_lz = (speed == crate::Speed::Slow)
                    .then(|| DeepLzScratchPool::new(group_lz_threads(speed, pool)));
                pool.steal_map_with_threads(
                    scratch,
                    num_ac_groups,
                    group_lz_threads(speed, pool),
                    |group_index, scratch| {
                        let gx = group_index % xsize_groups;
                        let gy = group_index / xsize_groups;
                        let x0 = gx * gdim;
                        let y0 = gy * gdim;
                        let gw = gdim.min(xsize - x0);
                        let gh = gdim.min(ysize - y0);
                        if speed == crate::Speed::Slow {
                            let channel_tokens = tokenize_channels_with_wp(
                                linear,
                                alpha,
                                xsize,
                                ysize,
                                x0,
                                y0,
                                gw,
                                gh,
                                num_color,
                                &chan_preds,
                                grad_pack_fn,
                                pool,
                                scratch,
                                wp_params,
                            );
                            deep_lz.as_ref().unwrap().with_depth(|depth| {
                                lz77_compress_channels_for_speed_with_depth(
                                    channel_tokens,
                                    distance_ctx,
                                    speed,
                                    depth,
                                    scratch,
                                )
                            })
                        } else {
                            tokenize_runs_with_wp(
                                linear,
                                alpha,
                                xsize,
                                x0,
                                y0,
                                gw,
                                gh,
                                num_color,
                                &chan_preds,
                                grad_pack_fn,
                                scratch,
                                wp_params,
                            )
                        }
                    },
                )
            };
            // ----- Section 0: DC global -----
            if let ModularFrameKind::Patched(references) = frame_kind {
                write_patch_dictionary(references, alpha.is_some(), scratch, &mut sections[0]);
            }
            let code = build_lz_pixel_code(
                group_lz_tokens.iter().map(Vec::as_slice),
                nb_chans,
                min_symbol,
                speed == crate::Speed::Slow,
                &mut scratch.lz_entropy,
                &mut scratch.huffman_pool,
            );
            sections[0].write(1, 1); // dc_quant all_default = 1
            sections[0].write(1, 1); // has_tree = 1
            write_local_tree_lz77(
                &chan_preds,
                &code,
                min_symbol,
                &mut scratch.huffman_pool,
                &mut sections[0],
            );
            // GroupHeader for the global modular image: use_global_tree=1, wp=1, RCT transform.
            sections[0].write(1, 1);
            write_wp_header(wp_params, &mut sections[0]);
            write_modular_transforms(nb_chans, rct_type, &mut sections[0]);
            sections[0].zero_pad_to_byte();

            // ----- DC groups: empty GroupHeader only -----
            for section in &mut sections[1..1 + num_dc_groups] {
                section.write(1, 1); // use_global_tree
                write_wp_header(wp_params, section);
                section.write(2, 0); // 0 transforms
                section.zero_pad_to_byte();
            }

            // ----- AC global: trivial (all_default flags) -----
            let ac_global_idx = 1 + num_dc_groups;
            sections[ac_global_idx].write(1, 1);
            write_wp_header(wp_params, &mut sections[ac_global_idx]);
            sections[ac_global_idx].zero_pad_to_byte();

            write_lz_groups(
                &group_lz_tokens,
                &code,
                distance_ctx,
                min_symbol,
                wp_params,
                pool,
                &mut sections[2 + num_dc_groups..],
            );

            // TOC.
            writer.write(1, 0);
            writer.zero_pad_to_byte();
            for s in &sections {
                write_toc_entry(s.bits_written() / 8, writer);
            }
            writer.zero_pad_to_byte();
            writer.append_byte_aligned(&mut sections);
            writer.zero_pad_to_byte();
        }
    }

    if compare_tree_candidates {
        keep_smaller_writer(&mut best_tree_writer, flat_writer);
        writer.append(best_tree_writer.as_ref().expect("flat lossless candidate"));
    }
    false
}

fn write_u64(value: u64, w: &mut BitWriter) {
    match value {
        0 => w.write(2, 0),
        1..=16 => {
            w.write(2, 1);
            w.write(4, value - 1);
        }
        17..=272 => {
            w.write(2, 2);
            w.write(8, value - 17);
        }
        _ => unreachable!("lossless frame flags fit the short U64 form"),
    }
}

fn write_frame_dimension(value: usize, w: &mut BitWriter) {
    let value = value as u64;
    if value < 256 {
        w.write(2, 0);
        w.write(8, value);
    } else if value < 2304 {
        w.write(2, 1);
        w.write(11, value - 256);
    } else if value < 18688 {
        w.write(2, 2);
        w.write(14, value - 2304);
    } else {
        w.write(2, 3);
        w.write(30, value - 18688);
    }
}

fn write_frame_header_modular_kind(
    has_alpha: bool,
    kind: ModularFrameKind<'_>,
    layout: GroupLayout,
    w: &mut BitWriter,
) {
    match kind {
        ModularFrameKind::Regular => write_frame_header_modular(has_alpha, layout, w),
        ModularFrameKind::Patched(_) => write_frame_header_modular_flags(has_alpha, 2, layout, w),
        ModularFrameKind::ReferenceOnly { width, height } => {
            w.write(1, 0); // all_default = false
            w.write(2, 0b10); // reference-only frame
            w.write(1, 1); // Modular
            write_u64(0, w); // flags
            w.write(1, 0); // color transform = None
            w.write(2, 0); // upsampling = 1
            if has_alpha {
                w.write(2, 0); // extra-channel upsampling = 1
            }
            w.write(2, layout.shift as u64);
            // Reference-only frames do not serialize Passes.
            w.write(1, 1); // custom size
            write_frame_dimension(width, w);
            write_frame_dimension(height, w);
            // No blending and no is_last for reference-only frames.
            w.write(2, PATCH_REF_ID as u64); // save_as_reference = 3
            w.write(1, 1); // save_before_color_transform = true
            w.write(2, 0); // empty name
            w.write(1, 0); // loop filter not all-default
            w.write(1, 0); // no gaborish
            w.write(2, 0); // no EPF
            w.write(2, 0); // no loop-filter extensions
            w.write(2, 0); // no frame-header extensions
        }
    }
}

/// Frame header for a modular reference-only frame inside the lossy
/// (`xyb_encoded`) codestream. Identical to the lossless variant except that
/// the container sets `xyb_encoded`, so the do_YCbCr bit is absent and the
/// frame's color transform is implicitly XYB. Saved to the modular slot so a
/// hybrid plan can also emit the VarDCT atlas in slot 3.
fn write_frame_header_modular_xyb_reference(
    width: usize,
    height: usize,
    has_alpha: bool,
    w: &mut BitWriter,
) {
    w.write(1, 0); // all_default = false
    w.write(2, 0b10); // reference-only frame
    w.write(1, 1); // encoding = Modular
    write_u64(0, w); // flags
    // No do_YCbCr bit: with xyb_encoded set the color transform is XYB.
    w.write(2, 0); // upsampling = 1
    if has_alpha {
        w.write(2, 0); // ec_upsampling[0] = 1
    }
    w.write(2, GroupLayout::DEFAULT.shift as u64);
    // Reference-only frames do not serialize Passes.
    w.write(1, 1); // custom size
    write_frame_dimension(width, w);
    write_frame_dimension(height, w);
    // No blending and no is_last for reference-only frames.
    w.write(2, crate::patches::MODULAR_PATCH_REF_ID as u64); // save_as_reference = 2
    w.write(1, 1); // save_before_color_transform = true
    w.write(2, 0); // empty name
    // Loop filter off: the atlas is stored exactly as coded; gaborish or EPF
    // would smear the glyph edges patches exist to preserve.
    w.write(1, 0); // loop filter not all-default
    w.write(1, 0); // no gaborish
    w.write(2, 0); // no EPF
    w.write(2, 0); // no loop-filter extensions
    w.write(2, 0); // no frame-header extensions
}

/// Reference-only atlas frame for the lossy codestream, coded with the modular
/// machinery on the fixed XYB integer lattice: near-lossless, ringing-free, and
/// palette-compressed when the quantized atlas holds few distinct colors.
///
/// Returns false when the atlas cannot take this path (it must fit a single
/// 256-pixel modular group); the caller then keeps the VarDCT atlas. Because
/// this encoding is sharper than any lossy VarDCT atlas, selecting the smaller
/// of the two by bits alone cannot regress quality.
pub(crate) fn encode_modular_xyb_atlas(
    atlas: &crate::image::Image3F,
    has_alpha: bool,
    lattice_scale: u32,
    speed: crate::Speed,
    use_wp: bool,
    scratch: &mut CoderScratch,
    writer: &mut BitWriter,
) -> bool {
    use std::collections::HashMap;
    // The 13-bit residual bound behind LZ77_MIN_SYMBOL caps the refinement.
    debug_assert!(lattice_scale.is_power_of_two() && lattice_scale <= 8);
    let (xsize, ysize) = (atlas.xsize(), atlas.ysize());
    if xsize == 0 || ysize == 0 || xsize > GROUP_DIM || ysize > GROUP_DIM {
        return false;
    }
    let ch = quantize_xyb_channels(atlas, lattice_scale);
    let npx = xsize * ysize;
    let grad_pack_fn = selected_grad_pack_interior_fn();
    let slow = speed == crate::Speed::Slow;

    // Distinct quantized XYB triples, palette-capped like the lossless path.
    let mut seen: HashMap<[i32; 3], ()> = HashMap::with_capacity(257);
    let [y, x, b] = &ch;
    let pixel_colors = || y[..npx].iter().zip(&x[..npx]).zip(&b[..npx]);
    for ((&y, &x), &b) in pixel_colors() {
        seen.entry([y, x, b]).or_insert(());
        if seen.len() > 256 {
            break;
        }
    }
    let use_palette = !seen.is_empty() && seen.len() <= 256;

    write_frame_header_modular_xyb_reference(xsize, ysize, has_alpha, writer);

    let mut section = BitWriter::new();
    // LfChannelDequant: the decoder multiplies each channel by these steps.
    // Custom F16 steps (each read as value/128) refine the lattice by
    // `lattice_scale`; powers of two keep every step F16-exact.
    if lattice_scale == 1 {
        section.write(1, 1); // dc_quant all_default = 1
    } else {
        use crate::quant_weights::INV_DC_QUANT;
        use crate::util::f32_to_f16_bits;
        section.write(1, 0);
        for c in 0..3 {
            let step_x128 = 128.0 / (INV_DC_QUANT[c] * lattice_scale as f32);
            section.write(16, f32_to_f16_bits(step_x128) as u64);
        }
    }
    section.write(1, 0); // has_tree = 0
    section.write(1, 0); // GroupHeader: use_global_tree = 0
    section.write(1, 1); // wp_default = 1

    // The atlas alpha is always the all-zero plane `zero_alpha_for_lossy`
    // produces (patch entries blend alpha with None, so its content is never
    // read); the palette absorbs it as a constant fourth component and the
    // plain path codes it as one all-zero channel.
    let num_c = 3 + usize::from(has_alpha);
    let mut tokens: Vec<Token> = Vec::new();
    let (preds, nb_chans): (Vec<u32>, usize) = if use_palette {
        let mut colors: Vec<[i32; 3]> = seen.keys().copied().collect();
        colors.sort_unstable();
        let nb_colors = colors.len();
        let mut idx_of: HashMap<[i32; 3], u32> = HashMap::with_capacity(nb_colors);
        for (i, c) in colors.iter().enumerate() {
            idx_of.insert(*c, i as u32);
        }
        // Component rows of the palette meta-channel; the alpha row (if any)
        // stays zero.
        let mut palette_ch = vec![0i32; num_c * nb_colors];
        for (c, row) in palette_ch[..3 * nb_colors]
            .chunks_exact_mut(nb_colors)
            .enumerate()
        {
            for (value, color) in row.iter_mut().zip(&colors) {
                *value = color[c];
            }
        }
        let index_img: Vec<i32> = pixel_colors()
            .map(|((&y, &x), &b)| idx_of[&[y, x, b]] as i32)
            .collect();

        write_palette_transform(num_c as u32, nb_colors as u32, &mut section);
        let pget = |gx: usize, gy: usize| palette_ch[gy * nb_colors + gx];
        let iget = |gx: usize, gy: usize| index_img[gy * xsize + gx];
        let preds = if slow {
            vec![
                choose_predictor_for_plane(pget, nb_colors, num_c, use_wp),
                choose_predictor_for_plane(iget, xsize, ysize, use_wp),
            ]
        } else {
            vec![fixed_predictor(use_wp); 2]
        };
        tokenize_plane_rows(
            channel_to_context(0, 2),
            |y| &palette_ch[y * nb_colors..][..nb_colors],
            nb_colors,
            num_c,
            preds[0],
            grad_pack_fn,
            &mut scratch.gradient,
            &mut tokens,
        );
        tokenize_plane_rows(
            channel_to_context(1, 2),
            |y| &index_img[y * xsize..][..xsize],
            xsize,
            ysize,
            preds[1],
            grad_pack_fn,
            &mut scratch.gradient,
            &mut tokens,
        );
        (preds, 2)
    } else {
        section.write(2, 0b00); // 0 transforms: Y/X/B-Y are already decorrelated
        let mut preds = Vec::with_capacity(num_c);
        for (c, data) in ch.iter().enumerate() {
            let get = |gx: usize, gy: usize| data[gy * xsize + gx];
            let pred = if slow {
                choose_predictor_for_plane(get, xsize, ysize, use_wp)
            } else {
                fixed_predictor(use_wp)
            };
            tokenize_plane_rows(
                channel_to_context(c, num_c),
                |y| &data[y * xsize..][..xsize],
                xsize,
                ysize,
                pred,
                grad_pack_fn,
                &mut scratch.gradient,
                &mut tokens,
            );
            preds.push(pred);
        }
        if has_alpha {
            // Constant zero plane: every predictor is exact, tokens are free.
            tokenize_plane(
                channel_to_context(3, num_c),
                |_, _| 0,
                xsize,
                ysize,
                fixed_predictor(use_wp),
                grad_pack_fn,
                &mut scratch.gradient,
                &mut tokens,
            );
            preds.push(fixed_predictor(use_wp));
        }
        (preds, num_c)
    };

    // Same guard as the lossless frame path: literals whose hybrid-uint token
    // would reach the LZ77 symbol range must push that range up, or the decoder
    // reads a large residual as a back-reference. The refined lattice makes the
    // channel values up to 13 bits, and residuals can double that.
    let max_abs = ch
        .iter()
        .flat_map(|plane| plane.iter())
        .map(|v| v.unsigned_abs())
        .max()
        .unwrap_or(0);
    let value_bits = 33 - (2 * max_abs).max(1).leading_zeros();
    let min_symbol = if value_bits <= 13 {
        LZ77_MIN_SYMBOL
    } else {
        4 * value_bits + 24
    };

    let distance_ctx = nb_chans as u32;
    let lz_tokens = lz77_compress_for_speed(&tokens, distance_ctx, speed, scratch);
    let code = build_lz_pixel_code(
        std::iter::once(lz_tokens.as_slice()),
        nb_chans,
        min_symbol,
        slow,
        &mut scratch.lz_entropy,
        &mut scratch.huffman_pool,
    );
    write_local_tree_lz77(
        &preds,
        &code,
        min_symbol,
        &mut scratch.huffman_pool,
        &mut section,
    );
    write_lz_section(&lz_tokens, distance_ctx, &code, min_symbol, &mut section);
    section.zero_pad_to_byte();

    writer.write(1, 0); // TOC: no permutation
    writer.zero_pad_to_byte();
    write_toc_entry(section.bits_written() / 8, writer);
    writer.zero_pad_to_byte();
    writer.append_byte_aligned(std::slice::from_mut(&mut section));
    writer.zero_pad_to_byte();
    true
}

fn write_frame_header_modular(has_alpha: bool, layout: GroupLayout, w: &mut BitWriter) {
    write_frame_header_modular_flags(has_alpha, 0, layout, w);
}

fn write_frame_header_modular_flags(
    has_alpha: bool,
    flags: u64,
    layout: GroupLayout,
    w: &mut BitWriter,
) {
    w.write(1, 0); // all_default = false
    w.write(2, 0b00); // regular frame
    w.write(1, 1); // encoding = Modular
    write_u64(flags, w);
    w.write(1, 0); // do_ycbcr = false   (xyb_encoded=0 so this is serialized)
    w.write(2, 0b00); // upsampling = 1
    if has_alpha {
        w.write(2, 0b00);
    }
    w.write(2, layout.shift as u64);
    w.write(2, 0b00); // num_passes = 1
    w.write(1, 0); // have_crop = false
    w.write(2, 0b00); // blending = Replace
    if has_alpha {
        w.write(2, 0b00);
    }
    w.write(1, 1); // is_last
    w.write(2, 0b00); // name length = 0
    w.write(1, 0); // loop_filter NOT all_default
    w.write(1, 0); // no gaborish
    w.write(2, 0); // 0 EPF iters
    w.write(2, 0b00); // no LF extensions
    w.write(2, 0b00); // no FH extensions
}

pub(crate) fn write_patch_dictionary(
    references: &[PatchReference],
    has_alpha: bool,
    scratch: &mut CoderScratch,
    w: &mut BitWriter,
) {
    const NUM_REF: u32 = 0;
    const REFERENCE_FRAME: u32 = 1;
    const PATCH_SIZE: u32 = 2;
    const REF_POSITION: u32 = 3;
    const POSITION: u32 = 4;
    const BLEND_MODE: u32 = 5;
    const OFFSET: u32 = 6;
    const COUNT: u32 = 7;

    let mut tokens = Vec::new();
    tokens.push(Token::new(NUM_REF, references.len() as u32));
    for reference in references {
        tokens.push(Token::new(REFERENCE_FRAME, reference.ref_frame));
        tokens.push(Token::new(REF_POSITION, reference.atlas_x as u32));
        tokens.push(Token::new(REF_POSITION, reference.atlas_y as u32));
        tokens.push(Token::new(PATCH_SIZE, (PATCH_TILE - 1) as u32));
        tokens.push(Token::new(PATCH_SIZE, (PATCH_TILE - 1) as u32));
        tokens.push(Token::new(COUNT, (reference.positions.len() - 1) as u32));
        for (i, &(x, y)) in reference.positions.iter().enumerate() {
            if i == 0 {
                tokens.push(Token::new(POSITION, x as u32));
                tokens.push(Token::new(POSITION, y as u32));
            } else {
                let (px, py) = reference.positions[i - 1];
                tokens.push(Token::new(OFFSET, pack_signed(x as i32 - px as i32)));
                tokens.push(Token::new(OFFSET, pack_signed(y as i32 - py as i32)));
            }
            tokens.push(Token::new(BLEND_MODE, 1)); // color = Replace
            if has_alpha {
                tokens.push(Token::new(BLEND_MODE, 0)); // alpha = None
            }
        }
    }
    let code = optimize_entropy_code(&tokens, NUM_PATCH_CONTEXTS, &mut scratch.huffman_pool);
    let code_ref = code.as_ref();
    w.write(1, 0); // patch dictionary entropy stream has no LZ77
    write_entropy_code(&code_ref, &mut scratch.huffman_pool, w);
    for token in tokens {
        write_token(token, &code_ref, w);
    }
}

fn write_modular_transforms(nb_chans: usize, rct_type: u32, w: &mut BitWriter) {
    if nb_chans >= 3 {
        // transforms count u2S(0, 1, Bits(4)+2, Bits(8)+18): selector 1 = Val(1) → 1 transform.
        w.write(2, 0b01);
        write_rct_transform(rct_type, w);
    } else {
        w.write(2, 0b00); // 0 transforms
    }
}

fn write_toc_entry(byte_len: usize, w: &mut BitWriter) {
    static OFFSETS: [usize; 4] = [0, 1024, 17_408, 4_211_712];
    static BITS: [usize; 4] = [10, 14, 22, 30];
    let mut bucket = 0usize;
    while bucket < 3 && byte_len >= OFFSETS[bucket + 1] {
        bucket += 1;
    }
    w.write(2, bucket as u64);
    w.write(BITS[bucket], (byte_len - OFFSETS[bucket]) as u64);
}

// ---------------------------------------------------------------------------
// Context tree (MA tree): split each channel's entropy context on the WP error
// property kWPProp (p[15]) into 3 activity buckets. The decoder must run the WP
// state for every pixel of a channel whose subtree references p[15], so we run
// WP for all channels here regardless of the selected leaf predictor.
// ---------------------------------------------------------------------------

const PROP_WP: u32 = 15; // kNumStaticProperties(2) + 13

/// Encoder-side tree: Split(property, splitval, gt-branch, le-branch) routes
/// `prop > splitval` to the gt-branch. Leaf carries (predictor, tag) where
/// tag = channel*3 + bucket.
enum CtTree {
    Split(u32, i32, Box<CtTree>, Box<CtTree>),
    Leaf(u32, u32),
}

/// BFS-emit the tree (matches libjxl's FIFO tree decode) and return the context
/// id assigned to each leaf tag. Context id == leaf's order of appearance.
fn emit_ct_tree(root: &CtTree, out: &mut Vec<Token>) -> std::collections::HashMap<u32, u32> {
    use std::collections::{HashMap, VecDeque};
    let mut map: HashMap<u32, u32> = HashMap::new();
    let mut q: VecDeque<&CtTree> = VecDeque::new();
    q.push_back(root);
    let mut ctx = 0u32;
    while let Some(node) = q.pop_front() {
        match node {
            CtTree::Split(prop, val, gt, le) => {
                push_split(out, *prop, *val);
                q.push_back(gt);
                q.push_back(le);
            }
            CtTree::Leaf(pred, tag) => {
                push_leaf(out, *pred);
                map.insert(*tag, ctx);
                ctx += 1;
            }
        }
    }
    map
}

#[inline]
fn bucket_of(prop: i64, t: i64) -> u32 {
    if prop > t {
        2
    } else if prop > -t - 1 {
        1
    } else {
        0
    }
}

/// 3-leaf activity subtree for channel `c` with predictor `pred` and threshold `t`.
fn act_sub(c: u32, pred: u32, t: i32) -> CtTree {
    CtTree::Split(
        PROP_WP,
        t,
        Box::new(CtTree::Leaf(pred, c * 3 + 2)),
        Box::new(CtTree::Split(
            PROP_WP,
            -t - 1,
            Box::new(CtTree::Leaf(pred, c * 3 + 1)),
            Box::new(CtTree::Leaf(pred, c * 3)),
        )),
    )
}

/// Channel-split tree (same shape as build_balanced_tree_tokens) with each
/// channel-leaf replaced by its activity subtree.
fn build_context_tree(nb_chans: usize, preds: &[u32], t: &[i32]) -> CtTree {
    let a = |c: usize| act_sub(c as u32, preds[c], t[c]);
    match nb_chans {
        1 => a(0),
        2 => CtTree::Split(0, 0, Box::new(a(1)), Box::new(a(0))),
        3 => CtTree::Split(
            0,
            1,
            Box::new(a(2)),
            Box::new(CtTree::Split(0, 0, Box::new(a(1)), Box::new(a(0)))),
        ),
        4 => CtTree::Split(
            0,
            1,
            Box::new(CtTree::Split(0, 2, Box::new(a(3)), Box::new(a(2)))),
            Box::new(CtTree::Split(0, 0, Box::new(a(1)), Box::new(a(0)))),
        ),
        _ => unreachable!("context tree supports 1..=4 channels"),
    }
}

fn order0_entropy(vals: &[u32], cell: &mut Vec<u64>) -> f32 {
    if vals.is_empty() {
        return 0.0;
    }
    // Direct-indexed frequency histogram (residual symbols are small-range), in
    // place of a HashMap: no hashing, and a deterministic accumulation order.
    let max = vals.iter().copied().max().unwrap_or(0) as usize;
    if cell.len() < max + 1 {
        cell.resize(max + 1, 0);
    }
    let hist = &mut cell[..max + 1];
    hist.fill(0);
    for &v in vals {
        hist[v as usize] += 1;
    }
    let total = vals.len() as f32;
    let mut bits = 0.0;
    for &c in hist.iter() {
        if c != 0 {
            let p = c as f32 / total;
            bits -= c as f32 * dirty_log2f(p);
        }
    }
    bits
}

/// Run WP over one channel's group rectangle, returning per-pixel
/// (packed residual under `pred_id`, WP property p[15]) in row-major order.
fn collect_channel(
    get: impl Fn(usize, usize) -> i32,
    gw: usize,
    gh: usize,
    pred_id: u32,
    wp_params: WpParams,
) -> (Vec<u32>, Vec<i64>) {
    let mut wp = WpState::with_params(gw, wp_params);
    let mut res: Vec<u32> = Vec::with_capacity(gw * gh);
    let mut prp = Vec::with_capacity(gw * gh);
    for gy in 0..gh {
        for gx in 0..gw {
            let v = get(gx, gy) as i64;
            let neighbors = predictor_neighbors(&get, gx, gy, gw);
            let wp_pred = wp.predict(
                gx,
                gy,
                neighbors.top,
                neighbors.left,
                neighbors.top_right,
                neighbors.top_left,
                neighbors.top_top,
            );
            prp.push(wp.wp_prop);
            let pred = predictor_value(pred_id, neighbors, wp_pred);
            res.push(pack_signed((v - pred) as i32));
            wp.update(v, gx, gy);
        }
    }
    (res, prp)
}

/// Pick the best activity threshold for a channel among candidates, returning
/// (best_t, best_bucketed_bits, flat_bits).
fn entropy_of_hist(hist: &[u64], total: u64) -> f32 {
    if total == 0 {
        return 0.0;
    }
    let t = total as f32;
    let mut bits = 0.0;
    for &c in hist.iter() {
        if c != 0 {
            let p = c as f32 / t;
            bits -= c as f32 * p.log2();
        }
    }
    bits
}

#[derive(Default)]
pub(crate) struct PickThresholdScratch {
    pub(crate) hist_scratch: Vec<u64>,
}

impl PickThresholdScratch {
    fn make_scratches(&mut self, size: usize) -> (&mut [u64], &mut [u64], &mut [u64]) {
        let bucket = size + 1;
        if self.hist_scratch.len() < bucket * 3 {
            self.hist_scratch.resize(bucket * 3, 0);
        }
        let (b0, r0) = self.hist_scratch.split_at_mut(bucket);
        let (b1, b2) = r0.split_at_mut(bucket);
        (b0, b1, &mut b2[..bucket])
    }
}

fn pick_threshold(res: &[u32], prp: &[i64], scratch: &mut CoderScratch) -> (i32, f32, f32) {
    let flat = order0_entropy(res, &mut scratch.order0_entropy);
    let max = res.iter().copied().max().unwrap_or(0) as usize;
    let (h0, h1, h2) = scratch.threshold.make_scratches(max + 1);
    let mut best_t = 0i32;
    let mut best_bits = f32::INFINITY;
    for &t in &[8i64, 16, 24, 32, 48, 64, 96] {
        h0.fill(0);
        h1.fill(0);
        h2.fill(0);
        let (mut n0, mut n1, mut n2) = (0u64, 0u64, 0u64);
        for (&r, &p) in res.iter().zip(prp.iter()) {
            match bucket_of(p, t) {
                0 => {
                    h0[r as usize] += 1;
                    n0 += 1;
                }
                1 => {
                    h1[r as usize] += 1;
                    n1 += 1;
                }
                _ => {
                    h2[r as usize] += 1;
                    n2 += 1;
                }
            }
        }
        let bits = entropy_of_hist(h0, n0) + entropy_of_hist(h1, n1) + entropy_of_hist(h2, n2);
        if bits < best_bits {
            best_bits = bits;
            best_t = t as i32;
        }
    }
    (best_t, best_bits, flat)
}

/// Multi-group form of `pick_threshold` that scans the already-collected
/// group slices directly. This avoids rebuilding full-image residual and
/// property vectors for every channel solely to calculate histograms.
fn pick_threshold_grouped(
    groups: &[Vec<(Vec<u32>, Vec<i64>)>],
    channel: usize,
    scratch: &mut CoderScratch,
) -> (i32, f32, f32) {
    let total: usize = groups.iter().map(|group| group[channel].0.len()).sum();
    if total == 0 {
        return (0, 0.0, 0.0);
    }
    let max = groups
        .iter()
        .flat_map(|group| group[channel].0.iter().copied())
        .max()
        .unwrap_or(0) as usize;

    if scratch.order0_entropy.len() < max + 1 {
        scratch.order0_entropy.resize(max + 1, 0);
    }
    let flat_hist = &mut scratch.order0_entropy[..max + 1];
    flat_hist.fill(0);
    for group in groups {
        for &residual in &group[channel].0 {
            flat_hist[residual as usize] += 1;
        }
    }
    let total_f = total as f32;
    let mut flat = 0.0;
    for &count in flat_hist.iter() {
        if count != 0 {
            let p = count as f32 / total_f;
            flat -= count as f32 * dirty_log2f(p);
        }
    }

    let (h0, h1, h2) = scratch.threshold.make_scratches(max + 1);
    let mut best_t = 0i32;
    let mut best_bits = f32::INFINITY;
    for &t in &[8i64, 16, 24, 32, 48, 64, 96] {
        h0.fill(0);
        h1.fill(0);
        h2.fill(0);
        let (mut n0, mut n1, mut n2) = (0u64, 0u64, 0u64);
        for group in groups {
            let (residuals, properties) = &group[channel];
            for (&residual, &property) in residuals.iter().zip(properties.iter()) {
                match bucket_of(property, t) {
                    0 => {
                        h0[residual as usize] += 1;
                        n0 += 1;
                    }
                    1 => {
                        h1[residual as usize] += 1;
                        n1 += 1;
                    }
                    _ => {
                        h2[residual as usize] += 1;
                        n2 += 1;
                    }
                }
            }
        }
        let bits = entropy_of_hist(h0, n0) + entropy_of_hist(h1, n1) + entropy_of_hist(h2, n2);
        if bits < best_bits {
            best_bits = bits;
            best_t = t as i32;
        }
    }
    (best_t, best_bits, flat)
}

// ---------------------------------------------------------------------------
// Learned MA context tree (v2): greedy learned tree over the standard modular
// property vector (libjxl ids 0..=15) with per-leaf predictors. The learner
// lives in ma_tree.rs; this section computes the property vectors (bit-exact
// with libjxl's PredictImpl, which the decoder re-runs), samples the image,
// and routes every pixel through the learned tree.
// ---------------------------------------------------------------------------

/// Target sample count for tree learning (across all channels/groups). Photos
/// stop gaining well below this (Kodak: stride 1 is not better than 3), but
/// graphics with many near-deterministic contexts keep gaining: the 17 MP
/// Burning Ship fractal is −3.4% at 2M samples over 512K. Only one layout's
/// sample set is alive at a time (110 B per sample with two reference channels).
const MA_TARGET_SAMPLES: usize = 1 << 21;
/// Preserve full sampling for genuinely small images. Above the former 256K
/// target, a stride of three is both cheaper and less prone to fitting local
/// pixel-phase noise than switching abruptly to a full-image sample.
const MA_FULL_SAMPLE_LIMIT: usize = 1 << 22;
/// First-stage learner budget. Most unhelpful or simple trees terminate here;
/// only a saturated tree with a clear rate win is retrained on the full probe.
const MA_COARSE_TARGET_SAMPLES: usize = 1 << 16;
const MA_COARSE_MAX_LEAVES: usize = 32;
const MA_DEEPEN_MIN_SAVINGS: f64 = 0.02;
/// Bits a split must save (image domain) before it's kept. Growth is
/// best-first, so this mostly prunes the tail once the leaf budget is spent.
const MA_SPLIT_COST_BITS: f32 = 100.0;
/// Leaf cap: pixel contexts + the LZ77 distance context must fit the
/// LZ77_MAX_CONTEXTS scratch; ANS clustering reduces them to <= 128
/// histograms. Best-first growth adds exactly one leaf per split, so the cap
/// is hard.
const MA_MAX_LEAVES: usize = LZ77_MAX_CONTEXTS - 1;
/// Minimum samples in a node before it must become a leaf.
const MA_MIN_NODE_SAMPLES: usize = 128;
/// Predictors each side of a candidate split may choose from (the node's
/// cheapest ones).
const MA_SIDE_PREDS: usize = 4;

/// Walk one channel rectangle in scan order, feeding the visitor the property
/// vector (libjxl ids 0..=15), the neighborhood, and the WP prediction of
/// every pixel.
/// A previously coded channel of the same size as the one being walked,
/// cropped like it: the source of the decoder's reference properties.
#[derive(Clone, Copy)]
struct MaRefPlane<'a> {
    pixels: &'a [i32],
    stride: usize,
    x0: usize,
    y0: usize,
}

/// libjxl `PrecomputeReferences`: per reference channel |v|, v, |v − g|,
/// v − g, with left = 0 at the crop's left edge, top = left on its first row
/// and top-left = left on either edge, g the clamped gradient.
#[inline]
fn ref_props(refs: &[MaRefPlane<'_>], x: usize, y: usize, p: &mut [i32; NUM_MA_PROPS]) {
    for (k, r) in refs.iter().take(MA_REF_CHANNELS).enumerate() {
        let row = &r.pixels[(r.y0 + y) * r.stride + r.x0..];
        let v = row[x] as i64;
        let left = if x > 0 { row[x - 1] as i64 } else { 0 };
        let (top, top_left) = if y > 0 {
            let above = &r.pixels[(r.y0 + y - 1) * r.stride + r.x0..];
            (
                above[x] as i64,
                if x > 0 { above[x - 1] as i64 } else { left },
            )
        } else {
            (left, left)
        };
        let g = (left + top - top_left).clamp(left.min(top), left.max(top));
        let base = 16 + 4 * k;
        p[base] = v.abs() as i32;
        p[base + 1] = v as i32;
        p[base + 2] = (v - g).abs() as i32;
        p[base + 3] = (v - g) as i32;
    }
}

fn walk_channel_ma<'a, T: Copy + 'a>(
    get_row: impl Fn(usize) -> &'a [T],
    gw: usize,
    gh: usize,
    chan: u32,
    stream: i32,
    refs: &[MaRefPlane<'a>],
    wp_params: WpParams,
    use_wp: bool,
    mut visit: impl FnMut(usize, usize, i64, &[i32; NUM_MA_PROPS], PredictorNeighbors, i64),
) where
    i64: From<T>,
{
    let disabled = !use_wp;
    let mut wp = WpState::with_params(gw, wp_params);
    let mut p = [0i32; NUM_MA_PROPS];
    p[0] = chan as i32;
    p[1] = stream;
    let mut north_row: &[T] = &[];
    let mut north_north_row: &[T] = &[];
    for y in 0..gh {
        let current_row = get_row(y);
        assert_eq!(current_row.len(), gw);
        // Bind valid rows even at the top border, so all three lengths are
        // established before entering the pixel loop.
        if y == 0 {
            north_row = current_row;
        }
        if y < 2 {
            north_north_row = north_row;
        }
        assert_eq!(north_row.len(), gw);
        assert_eq!(north_north_row.len(), gw);
        let row = wp.row_offsets(y);
        p[2] = y as i32;
        p[9] = 0; // "local gradient" carry, reset per row (InitPropsRow)
        // Keep the visitor in each loop body. Passing this large WP/property
        // update through a row callback can outline a call for every pixel.
        macro_rules! visit_pixel {
            ($x:expr, $value:expr, $neighbors:expr) => {{
                let x = $x;
                let value = $value;
                let n = $neighbors;
                p[3] = x as i32;
                p[4] = n.top.abs() as i32;
                p[5] = n.left.abs() as i32;
                p[6] = n.top as i32;
                p[7] = n.left as i32;
                // p[8] reads the previous pixel's p[9] (0 at row start).
                p[8] = (n.left - p[9] as i64) as i32;
                p[9] = (n.left + n.top - n.top_left) as i32;
                p[10] = (n.left - n.top_left) as i32;
                p[11] = (n.top_left - n.top) as i32;
                p[12] = (n.top - n.top_right) as i32;
                p[13] = (n.top - n.top_top) as i32;
                p[14] = (n.left - n.left_left) as i32;
                let wp_pred = if disabled {
                    0
                } else {
                    wp.predict_and_update_flat(
                        value,
                        x,
                        row,
                        WpNeighbors {
                            north: n.top,
                            west: n.left,
                            north_east: n.top_right,
                            north_west: n.top_left,
                            north_north: n.top_top,
                        },
                    )
                };
                p[15] = if disabled { 0 } else { wp.wp_prop as i32 };
                ref_props(refs, x, y, &mut p);
                visit(x, y, value, &p, n, wp_pred);
            }};
        }
        if y == 0 {
            if let Some((&first, tail)) = current_row.split_first() {
                let first = i64::from(first);
                visit_pixel!(
                    0,
                    first,
                    PredictorNeighbors {
                        left: 0,
                        top: 0,
                        top_left: 0,
                        top_right: 0,
                        left_left: 0,
                        top_top: 0,
                        top_right_right: 0,
                    }
                );
                let mut left = first;
                let mut left_left = first;
                for (x, &value) in tail.iter().enumerate() {
                    let value = i64::from(value);
                    visit_pixel!(
                        x + 1,
                        value,
                        PredictorNeighbors {
                            left,
                            top: left,
                            top_left: left,
                            top_right: left,
                            left_left,
                            top_top: left,
                            top_right_right: left,
                        }
                    );
                    left_left = left;
                    left = value;
                }
            }
        } else {
            let edge = |x: usize| {
                let top = i64::from(north_row[x]);
                let left = if x > 0 {
                    i64::from(current_row[x - 1])
                } else {
                    top
                };
                let top_right = north_row.get(x + 1).map_or(top, |&v| i64::from(v));
                PredictorNeighbors {
                    left,
                    top,
                    top_left: if x > 0 {
                        i64::from(north_row[x - 1])
                    } else {
                        left
                    },
                    top_right,
                    left_left: if x > 1 {
                        i64::from(current_row[x - 2])
                    } else {
                        left
                    },
                    top_top: i64::from(north_north_row[x]),
                    top_right_right: north_row.get(x + 2).map_or(top_right, |&v| i64::from(v)),
                }
            };
            for (x, &value) in current_row.iter().take(2).enumerate() {
                visit_pixel!(x, i64::from(value), edge(x));
            }
            if gw > 4 {
                let mut left = i64::from(current_row[1]);
                let mut left_left = i64::from(current_row[0]);
                let pixels = current_row[2..gw - 2]
                    .iter()
                    .zip(north_row[1..].array_windows::<4>())
                    .zip(&north_north_row[2..]);
                for (x, ((&value, &[nw, n, ne, nee]), &nn)) in pixels.enumerate() {
                    let value = i64::from(value);
                    visit_pixel!(
                        x + 2,
                        value,
                        PredictorNeighbors {
                            left,
                            top: i64::from(n),
                            top_left: i64::from(nw),
                            top_right: i64::from(ne),
                            left_left,
                            top_top: i64::from(nn),
                            top_right_right: i64::from(nee),
                        }
                    );
                    left_left = left;
                    left = value;
                }
            }
            for x in gw.saturating_sub(2).max(2)..gw {
                visit_pixel!(x, i64::from(current_row[x]), edge(x));
            }
        }
        north_north_row = north_row;
        north_row = current_row;
    }
}

/// Exact sample count for the rotating row phase.
fn ma_channel_sample_count(w: usize, h: usize, stride: usize) -> usize {
    // A complete cycle of row phases selects exactly w samples. In the
    // remaining rows, the first w % stride phases select one extra sample.
    (h / stride) * w + (h % stride) * (w / stride) + (h % stride).min(w % stride)
}

/// Visit the selected pixels with residual tokens under every decoder predictor.
fn sample_channel_ma<'a, T: Copy + 'a>(
    get_row: impl Fn(usize) -> &'a [T],
    gw: usize,
    gh: usize,
    chan: u32,
    stream: i32,
    refs: &[MaRefPlane<'a>],
    wp_params: WpParams,
    use_wp: bool,
    stride: usize,
    mut push: impl FnMut([i32; NUM_MA_PROPS], [u8; NUM_MA_PREDS]),
) where
    i64: From<T>,
{
    debug_assert!(stride != 0);
    let disabled = !use_wp;
    let mut wp = WpState::with_params(gw, wp_params);
    let mut north_row: &[T] = &[];
    let mut north_north_row: &[T] = &[];
    for y in 0..gh {
        let current_row = get_row(y);
        assert_eq!(current_row.len(), gw);
        // Bind valid rows even at the top border, so all three lengths are
        // established before entering the pixel loop.
        if y == 0 {
            north_row = current_row;
        }
        if y < 2 {
            north_north_row = north_row;
        }
        assert_eq!(north_row.len(), gw);
        assert_eq!(north_north_row.len(), gw);
        let row = wp.row_offsets(y);
        let mut west = 0i64;
        let mut west_west = 0i64;
        let mut previous_local_gradient = 0i32;
        // Rotate the sampling phase every row so the probe never locks onto
        // one column residue (a row width divisible by the stride would
        // otherwise sample a single x phase for the whole channel).
        let mut until_sample = 1 + y % stride;
        for x in 0..gw {
            let value = i64::from(current_row[x]);
            let north = if y > 0 { i64::from(north_row[x]) } else { west };
            let left = if x > 0 { west } else { north };
            let top_left = if x > 0 && y > 0 {
                i64::from(north_row[x - 1])
            } else {
                left
            };
            let top_right = if x + 1 < gw && y > 0 {
                i64::from(north_row[x + 1])
            } else {
                north
            };
            let top_top = if y > 1 {
                i64::from(north_north_row[x])
            } else {
                north
            };
            let wp_pred = if disabled {
                0
            } else {
                let wp_neighbors = WpNeighbors {
                    north,
                    west: left,
                    north_east: top_right,
                    north_west: top_left,
                    north_north: top_top,
                };
                wp.predict_and_update_flat(value, x, row, wp_neighbors)
            };
            let local_gradient = (left + north - top_left) as i32;

            until_sample -= 1;
            if until_sample == 0 {
                until_sample = stride;
                let neighbors = PredictorNeighbors {
                    left,
                    top: north,
                    top_left,
                    top_right,
                    left_left: if x > 1 { west_west } else { left },
                    top_top,
                    top_right_right: if x + 2 < gw && y > 0 {
                        i64::from(north_row[x + 2])
                    } else {
                        top_right
                    },
                };
                let mut props = [0i32; NUM_MA_PROPS];
                props[0] = chan as i32;
                props[1] = stream;
                props[2] = y as i32;
                props[3] = x as i32;
                props[4] = north.abs() as i32;
                props[5] = left.abs() as i32;
                props[6] = north as i32;
                props[7] = left as i32;
                props[8] = (left - previous_local_gradient as i64) as i32;
                props[9] = local_gradient;
                props[10] = (left - top_left) as i32;
                props[11] = (top_left - north) as i32;
                props[12] = (north - top_right) as i32;
                props[13] = (north - top_top) as i32;
                props[14] = (left - neighbors.left_left) as i32;
                props[15] = if disabled { 0 } else { wp.wp_prop as i32 };
                ref_props(refs, x, y, &mut props);

                let mut tok = [0u8; NUM_MA_PREDS];
                for pred in 0..NUM_MA_PREDS as u32 {
                    let pv = predictor_value(pred, neighbors, wp_pred);
                    let (t, _, _) = uint_encode(pack_signed((value - pv) as i32));
                    tok[pred as usize] = t.min(u8::MAX as u32) as u8;
                }
                push(props, tok);
            }
            previous_local_gradient = local_gradient;
            west_west = west;
            west = value;
        }
        north_north_row = north_row;
        north_row = current_row;
    }
}

/// Tokenize the channel rectangle through the learned tree: every pixel is
/// routed to its leaf's context and coded with its leaf's predictor.
fn tokenize_channel_ma<'a, T: Copy + 'a>(
    get_row: impl Fn(usize) -> &'a [T],
    gw: usize,
    gh: usize,
    chan: u32,
    stream: i32,
    refs: &[MaRefPlane<'a>],
    wp_params: WpParams,
    use_wp: bool,
    lookup: &MaLookup,
    offsets: &[i32],
    out: &mut RawTokens,
) where
    i64: From<T>,
{
    // Keep the traversal's stores simple, then validate and pack one row at
    // a time. The temporary row fits in cache and is reused for the channel.
    let mut row = Vec::with_capacity(gw);
    walk_channel_ma(
        get_row,
        gw,
        gh,
        chan,
        stream,
        refs,
        wp_params,
        use_wp && lookup.needs_wp(),
        |x, _y, v, p, n, wp_pred| {
            let (context, pred) = lookup.lookup(p);
            let pv = predictor_value(pred, n, wp_pred);
            row.push(Token::new(
                context,
                pack_signed((v - pv) as i32 - offsets[context as usize]),
            ));
            if x + 1 == gw {
                out.extend(&row);
                row.clear();
            }
        },
    );
}

/// BFS-emit a learned tree (matches libjxl's FIFO tree decode). Returns
/// (tree tokens, context id per node index — leaves only, context count).
fn emit_learned_tree(tree: &LearnedTree) -> (Vec<Token>, Vec<u32>, u32) {
    use std::collections::VecDeque;
    let mut tokens = Vec::new();
    let mut leaf_ctx = vec![u32::MAX; tree.nodes.len()];
    let mut q: VecDeque<u32> = VecDeque::new();
    q.push_back(0);
    let mut ctx = 0u32;
    while let Some(i) = q.pop_front() {
        match tree.nodes[i as usize] {
            MaNode::Split { prop, val, gt, le } => {
                push_split(&mut tokens, prop as u32, val);
                q.push_back(gt);
                q.push_back(le);
            }
            MaNode::Leaf { pred } => {
                push_leaf(&mut tokens, pred);
                leaf_ctx[i as usize] = ctx;
                ctx += 1;
            }
        }
    }
    (tokens, leaf_ctx, ctx)
}

/// Largest |offset| a leaf may carry. The learner keeps hybrid-uint symbols,
/// which are exact packed residuals only below the split (symbols < 16 =
/// residuals −8..=7); larger symbols carry their sign in raw bits the sample
/// does not keep, so they only count towards the zero fraction.
const MA_LEAF_OFFSET_LIMIT: i32 = 8;
const MA_LEAF_EXACT_SYMBOLS: u32 = 16;
/// Leaves with fewer routed samples keep offset 0.
const MA_LEAF_OFFSET_MIN_SAMPLES: u32 = 16;
/// No offsets at all when more than this share of residuals is exactly zero:
/// such content (screenshots, fractals, flat graphics) has no predictor bias
/// to cancel.
const MA_LEAF_OFFSET_MAX_ZERO_FRACTION: f64 = 0.9;

fn unpack_signed(token: u32) -> i32 {
    if token & 1 == 1 {
        -(((token + 1) >> 1) as i32)
    } else {
        (token >> 1) as i32
    }
}

/// Per-context predictor offsets: the median residual of each leaf's chosen
/// predictor over the samples the finished tree routes to it. Shifting a
/// leaf's residuals does not change its own entropy, but it centres the
/// distribution, which lets ANS clustering merge leaves by shape rather than
/// by bias and keeps small residuals inside the hybrid-uint's exact symbols.
fn leaf_offsets(
    tree: &LearnedTree,
    leaf_ctx: &[u32],
    num_ctx: u32,
    samples: &MaSamples,
) -> Vec<i32> {
    const BINS: usize = 2 * MA_LEAF_OFFSET_LIMIT as usize + 1;
    let mut hist = vec![[0u32; BINS]; num_ctx as usize];
    // Every fourth sample: leaves keep >100 samples on average and the
    // routing pass drops below the noise of the encode time.
    let mut routed = 0u64;
    for (props, tok) in samples.props.iter().zip(&samples.tok).step_by(4) {
        let (node, pred) = tree.lookup(props);
        routed += 1;
        let symbol = tok[pred as usize] as u32;
        if symbol < MA_LEAF_EXACT_SYMBOLS {
            let r = unpack_signed(symbol);
            hist[leaf_ctx[node as usize] as usize][(r + MA_LEAF_OFFSET_LIMIT) as usize] += 1;
        }
    }
    let zeros: u64 = hist
        .iter()
        .map(|h| h[MA_LEAF_OFFSET_LIMIT as usize] as u64)
        .sum();
    if zeros as f64 > MA_LEAF_OFFSET_MAX_ZERO_FRACTION * routed as f64 {
        return vec![0; num_ctx as usize];
    }
    hist.iter()
        .map(|h| {
            let total: u32 = h.iter().sum();
            if total < MA_LEAF_OFFSET_MIN_SAMPLES {
                return 0;
            }
            let half = total.div_ceil(2);
            let mut acc = 0u32;
            for (bin, &c) in h.iter().enumerate() {
                acc += c;
                if acc >= half {
                    return bin as i32 - MA_LEAF_OFFSET_LIMIT;
                }
            }
            0
        })
        .collect()
}

/// Sampling stride for tree learning; odd to avoid column aliasing.
fn ma_sample_stride(total_px: usize, full_sample_limit: usize) -> usize {
    let mut stride = total_px.div_ceil(MA_TARGET_SAMPLES).max(1);
    if stride == 1 && total_px > full_sample_limit {
        stride = 3;
    }
    if stride > 1 && stride.is_multiple_of(2) {
        stride += 1;
    }
    stride
}

/// Header overhead model for a learned tree: tree tokens plus the extra
/// per-context histogram / context-map cost (conservative; clustering merges
/// most of it). Returns the candidate only when it beats the flat estimate.
/// `samples` is `Some` only for candidates that may be written: routing every
/// sample through the tree for the offsets is not worth doing for the
/// ranking stage's throwaway candidates.
fn gate_ma_tree(
    tree: LearnedTree,
    sample_scale: f64,
    samples: Option<&MaSamples>,
) -> Option<LearnedCandidate> {
    let (mut tree_tokens, leaf_ctx, num_ctx) = emit_learned_tree(&tree);
    let overhead_bits = tree_tokens.len() as f64 * 10.0 + num_ctx as f64 * 200.0;
    let est_real = tree.est_bits * sample_scale + overhead_bits;
    let flat_real = tree.flat_bits * sample_scale;
    if est_real.partial_cmp(&flat_real) != Some(std::cmp::Ordering::Less) {
        return None;
    }
    // Offsets do not affect the token-count gate. Route only accepted trees,
    // then fill their existing offset tokens in BFS context order.
    let leaf_offset = match samples {
        Some(samples) => leaf_offsets(&tree, &leaf_ctx, num_ctx, samples),
        None => vec![0; num_ctx as usize],
    };
    for (token, &offset) in tree_tokens
        .iter_mut()
        .filter(|token| token.context == TREE_CTX_OFFSET)
        .zip(&leaf_offset)
    {
        token.value = pack_signed(offset);
    }
    let estimated_savings = 1.0 - tree.est_bits / tree.flat_bits.max(f64::MIN_POSITIVE);
    Some(LearnedCandidate {
        tree,
        tree_tokens,
        leaf_ctx,
        leaf_offset,
        num_ctx,
        est_real,
        flat_real,
        estimated_savings,
    })
}

fn ma_learn_params(
    min_symbol: u32,
    max_leaves: usize,
    max_candidates: usize,
    split_scale: f64,
    use_wp: bool,
) -> MaLearnParams {
    MaLearnParams {
        alphabet: min_symbol as usize,
        max_leaves,
        split_cost_bits: MA_SPLIT_COST_BITS / split_scale as f32,
        min_node: MA_MIN_NODE_SAMPLES,
        allow_wp: use_wp,
        allowed_preds: u16::MAX,
        side_preds: MA_SIDE_PREDS,
        max_candidates,
    }
}

/// First learning stage: the merged samples with a gated coarse tree (or the
/// complete tree for small sample sets). Cheap enough to run once per group
/// layout; only the winning layout is deepened.
struct CoarseLearn {
    samples: MaSamples,
    stride: usize,
    max_candidates: usize,
    /// Leaf predictor offsets apply (RCT sources; palette indices lose).
    leaf_offsets: bool,
    candidate: LearnedCandidate,
    /// The coarse tree saturated its leaf budget with a clear win, so the
    /// full-probe second stage is worth its cost.
    deepen: bool,
}

fn learn_ma_coarse(
    samples: MaSamples,
    stride: usize,
    min_symbol: u32,
    use_wp: bool,
    max_leaves: usize,
    max_candidates: usize,
    leaf_offsets: bool,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
) -> Option<CoarseLearn> {
    if samples.len() < 4 * MA_MIN_NODE_SAMPLES {
        return None;
    }
    if samples.len() <= MA_COARSE_TARGET_SAMPLES {
        let tree = learn_ma_tree(
            &samples,
            ma_learn_params(
                min_symbol,
                max_leaves,
                max_candidates,
                stride as f64,
                use_wp,
            ),
            pool,
            scratch,
        );
        let candidate = gate_ma_tree(tree, stride as f64, leaf_offsets.then_some(&samples))?;
        return Some(CoarseLearn {
            samples,
            stride,
            max_candidates,
            leaf_offsets,
            candidate,
            deepen: false,
        });
    }
    let coarse_indices = samples.evenly_sampled_indices(MA_COARSE_TARGET_SAMPLES);
    let coarse_scale = stride as f64 * samples.len() as f64 / coarse_indices.len() as f64;
    let coarse_tree = learn_ma_tree_indexed(
        &samples,
        coarse_indices,
        ma_learn_params(
            min_symbol,
            MA_COARSE_MAX_LEAVES,
            max_candidates,
            coarse_scale,
            use_wp,
        ),
        pool,
        scratch,
    );
    let candidate = gate_ma_tree(coarse_tree, coarse_scale, None)?;
    let coarse_leaves = candidate.tree.nodes.len().div_ceil(2);
    let coarse_savings = 1.0 - candidate.est_real / candidate.flat_real;
    let deepen = coarse_leaves == MA_COARSE_MAX_LEAVES && coarse_savings >= MA_DEEPEN_MIN_SAVINGS;
    Some(CoarseLearn {
        samples,
        stride,
        max_candidates,
        leaf_offsets,
        candidate,
        deepen,
    })
}

/// Second stage: re-score the coarse tree on the full probe and keep growing
/// it, falling back to the coarse tree when the deep one fails its gate.
fn finish_ma_learn(
    coarse: CoarseLearn,
    min_symbol: u32,
    use_wp: bool,
    max_leaves: usize,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
) -> LearnedCandidate {
    if !coarse.deepen {
        return coarse.candidate;
    }
    let (deep_tree, samples) = deepen_ma_tree(
        coarse.samples,
        ma_learn_params(
            min_symbol,
            max_leaves,
            coarse.max_candidates,
            coarse.stride as f64,
            use_wp,
        ),
        coarse.candidate.tree.clone(),
        pool,
        scratch,
    );
    gate_ma_tree(
        deep_tree,
        coarse.stride as f64,
        coarse.leaf_offsets.then_some(&samples),
    )
    .unwrap_or(coarse.candidate)
}

/// Rank the WP presets of `source` by the reduced-leaf learner, starting
/// from `first` (already learned under `first_params`), the rest in order
/// of how often they win, each deepened only when its first stage is within
/// `RANK_COARSE_PRUNE` of the best seen. Returns (best bits, best coarse
/// bits, preset, the winner's learn as a seed).
#[allow(clippy::too_many_arguments)]
fn rank_presets(
    source: &MaSource<'_>,
    xsize: usize,
    ysize: usize,
    min_symbol: u32,
    use_wp: bool,
    first: Option<CoarseLearn>,
    first_params: WpParams,
    header_bits: &dyn Fn(WpParams) -> f64,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
) -> (f64, f64, WpParams, Option<RankedLearn>) {
    let mut best_bits = f64::INFINITY;
    let mut best_coarse = f64::INFINITY;
    let mut best_params = first_params;
    let mut seed = None;
    let mut order: Vec<WpParams> = vec![first_params];
    if use_wp {
        for &preset in [1usize, 2, 3, 0].iter().map(|&i| &WpParams::PRESETS[i]) {
            if preset != first_params {
                order.push(preset);
            }
        }
    }
    let mut first = first;
    for preset in order {
        let stage = match first.take() {
            Some(stage) => Some(stage),
            None => rank_coarse(
                source, xsize, ysize, min_symbol, preset, use_wp, pool, scratch,
            ),
        };
        let Some(stage) = stage else {
            continue;
        };
        let coarse = stage.candidate.est_real + header_bits(preset);
        if coarse > best_coarse * RANK_COARSE_PRUNE {
            continue;
        }
        best_coarse = best_coarse.min(coarse);
        let (bits, ranked) = rank_finish(stage, min_symbol, use_wp, pool, scratch);
        let bits = bits + header_bits(preset);
        if bits < best_bits {
            best_bits = bits;
            best_params = preset;
            seed = Some(ranked);
        }
    }
    (best_bits, best_coarse, best_params, seed)
}

/// A ranking learn kept alive for the winner: its reduced-leaf tree seeds the
/// final learn, which then only grows from `MA_RANK_LEAVES` to the full
/// budget on the same (already compacted) sample set.
struct RankedLearn {
    candidate: LearnedCandidate,
    samples: MaSamples,
    stride: usize,
    max_candidates: usize,
    leaf_offsets: bool,
    coarse_est: f64,
}

impl RankedLearn {
    /// Continue best-first growth to the full leaf budget.
    fn finish(
        self,
        min_symbol: u32,
        use_wp: bool,
        pool: &ThreadPool,
        scratch: &mut CoderScratch,
    ) -> LearnedCandidate {
        let (tree, samples) = deepen_ma_tree(
            self.samples,
            ma_learn_params(
                min_symbol,
                MA_MAX_LEAVES,
                self.max_candidates,
                self.stride as f64,
                use_wp,
            ),
            self.candidate.tree.clone(),
            pool,
            scratch,
        );
        gate_ma_tree(
            tree,
            self.stride as f64,
            self.leaf_offsets.then_some(&samples),
        )
        .unwrap_or(self.candidate)
    }
}

/// A gated learned tree with its BFS emission and rate estimate.
struct LearnedCandidate {
    tree: LearnedTree,
    tree_tokens: Vec<Token>,
    leaf_ctx: Vec<u32>,
    /// Predictor offset per context (see `leaf_offsets`).
    leaf_offset: Vec<i32>,
    num_ctx: u32,
    /// Estimated image-domain bits of the tree-coded frame, headers included.
    est_real: f64,
    /// Estimated image-domain bits of the flat alternative.
    flat_real: f64,
    /// Estimated fractional saving over the flat path.
    estimated_savings: f64,
}

/// One modular channel as the learner and tokenizer see it.
struct MaChannel<'a> {
    w: usize,
    h: usize,
    /// Meta channels (palette) always live in the global stream, whatever
    /// their size.
    meta: bool,
    pixels: MaPixels<'a>,
}

/// Borrow the original sample representation, including palette indices and
/// alpha, without expanding or copying rows for MA traversal.
enum MaPixels<'a> {
    U8(&'a [u8]),
    U16(&'a [u16]),
    I32(&'a [i32]),
}

// Resolve the sample type once per rectangle so the row kernels remain
// monomorphized. All variants provide the same cropped, borrowed row interface.
macro_rules! with_ma_pixels {
    ($channel:expr, |$pixels:ident| $body:expr) => {
        match &$channel.pixels {
            MaPixels::U8($pixels) => $body,
            MaPixels::U16($pixels) => $body,
            MaPixels::I32($pixels) => $body,
        }
    };
}

/// Split candidates for a photographic (RGB/RCT) source; palette index
/// images overfit above `MA_PALETTE_CANDIDATES`.
pub(super) const MA_PHOTO_CANDIDATES: usize = 32;
/// Split candidates for a palette index image: more than this overfits the
/// near-deterministic index.
pub(super) const MA_PALETTE_CANDIDATES: usize = 16;

/// The single global transform a learned-tree frame declares.
#[derive(Clone, Copy)]
enum MaTransform {
    /// RCT on the first three channels (`6` = YCoCg).
    Rct(u32),
    /// Global palette over `num_c` components: channel 0 is the palette
    /// meta-channel (`nb_colors` x `num_c`), channel 1 the index image.
    Palette { num_c: u32, nb_colors: u32 },
}

/// The modular channels of a frame plus their transform.
struct MaSource<'a> {
    channels: Vec<MaChannel<'a>>,
    transform: MaTransform,
}

/// Quant tables reserved in the decoder's modular stream numbering
/// (`kNumQuantTables`); AC group streams are numbered after them.
const MODULAR_NUM_QUANT_TABLES: usize = 17;

/// The decoder's stream id (MA property 1) of AC group `group_index`:
/// `ModularStreamId::ModularAC(group, pass 0)` = 1 + 3 * DC groups + quant
/// tables + group. The global stream is id 0.
fn ac_stream_id(num_dc_groups: usize, group_index: usize) -> i32 {
    (1 + 3 * num_dc_groups + MODULAR_NUM_QUANT_TABLES + group_index) as i32
}

/// Where a channel is coded under a layout: in the single section / the
/// DC-global stream, or per AC group. Channel ids restart per stream, as the
/// decoder's channel property does.
struct MaPlacement {
    channel: usize,
    chan_id: u32,
    global: bool,
}

impl MaSource<'_> {
    /// Split candidates the learner should evaluate for this source.
    fn max_candidates(&self) -> usize {
        match self.transform {
            MaTransform::Palette { .. } => MA_PALETTE_CANDIDATES,
            MaTransform::Rct(_) => MA_PHOTO_CANDIDATES,
        }
    }

    fn write_transforms(&self, w: &mut BitWriter) {
        match self.transform {
            MaTransform::Rct(rct_type) => {
                write_modular_transforms(self.channels.len(), rct_type, w)
            }
            MaTransform::Palette { num_c, nb_colors } => {
                write_palette_transform(num_c, nb_colors, w)
            }
        }
    }

    /// Channels no larger than a group are coded in the global stream (the
    /// decoder stops the global decode at the first bigger channel); in a
    /// single-group frame everything shares one stream.
    fn placement(&self, layout: GroupLayout, single_group: bool) -> Vec<MaPlacement> {
        let gdim = layout.dim();
        let mut global_id = 0u32;
        let mut group_id = 0u32;
        self.channels
            .iter()
            .enumerate()
            .map(|(channel, ch)| {
                let global = single_group || ch.meta || (ch.w <= gdim && ch.h <= gdim);
                let chan_id = if single_group {
                    channel as u32
                } else if global {
                    let id = global_id;
                    global_id += 1;
                    id
                } else {
                    let id = group_id;
                    group_id += 1;
                    id
                };
                MaPlacement {
                    channel,
                    chan_id,
                    global,
                }
            })
            .collect()
    }

    fn total_values(&self) -> usize {
        self.channels.iter().map(|c| c.w * c.h).sum()
    }

    /// The reference planes of placement `p`'s channel: earlier channels of
    /// the same stream and size, most recent first, cropped at (x0, y0).
    fn ref_planes(
        &self,
        placement: &[MaPlacement],
        p: &MaPlacement,
        x0: usize,
        y0: usize,
    ) -> Vec<MaRefPlane<'_>> {
        let ch = &self.channels[p.channel];
        placement[..p.channel]
            .iter()
            .rev()
            .filter(|q| q.global == p.global)
            .filter_map(|q| {
                let other = &self.channels[q.channel];
                match other.pixels {
                    MaPixels::I32(pixels) if other.w == ch.w && other.h == ch.h => {
                        Some(MaRefPlane {
                            pixels,
                            stride: other.w,
                            x0,
                            y0,
                        })
                    }
                    _ => None,
                }
            })
            .take(MA_REF_CHANNELS)
            .collect()
    }
}

/// The YCoCg(A) planes as a learned-tree source.
fn rgb_ma_source<'a>(
    linear: &'a Image3Si,
    alpha: Option<&'a AlphaPlane>,
    xsize: usize,
    rct_type: u32,
) -> MaSource<'a> {
    let ysize = linear.ysize();
    let mut channels: Vec<MaChannel<'a>> = (0..3)
        .map(|c| {
            let pd = linear.plane_data(c);
            MaChannel {
                w: xsize,
                h: ysize,
                meta: false,
                pixels: MaPixels::I32(pd),
            }
        })
        .collect();
    if let Some(a) = alpha {
        channels.push(MaChannel {
            w: xsize,
            h: ysize,
            meta: false,
            pixels: match a {
                AlphaPlane::U8(pixels) => MaPixels::U8(pixels),
                AlphaPlane::U16 { data, .. } => MaPixels::U16(data),
                AlphaPlane::F32(pixels) => MaPixels::I32(pixels),
            },
        });
    }
    MaSource {
        channels,
        transform: MaTransform::Rct(rct_type),
    }
}

/// Below this covered-area fraction the patched alternative is not encoded.
const PATCH_MIN_COVERAGE: f64 = 0.05;

/// Sample every stream of `source` under `layout` (group-local coordinates
/// and a fresh WP state per group, exactly as the decoder will see them) and
/// run the first learning stage. `None` when no tree is estimated to beat
/// the flat path.
#[allow(clippy::too_many_arguments)]
fn learn_ma_candidate(
    source: &MaSource<'_>,
    xsize: usize,
    ysize: usize,
    layout: GroupLayout,
    min_symbol: u32,
    wp_params: WpParams,
    use_wp: bool,
    max_leaves: usize,
    full_sample_limit: usize,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
) -> Option<CoarseLearn> {
    let gdim = layout.dim();
    let xsize_groups = xsize.div_ceil(gdim);
    let num_groups = xsize_groups * ysize.div_ceil(gdim);
    let placement = source.placement(layout, num_groups == 1);
    let max_candidates = source.max_candidates();
    let stride = ma_sample_stride(source.total_values(), full_sample_limit);

    // Sampling jobs: global streams first, then group-major / channel-minor.
    struct Job<'a> {
        channel: usize,
        chan_id: u32,
        stream: i32,
        refs: Vec<MaRefPlane<'a>>,
        x0: usize,
        y0: usize,
        w: usize,
        h: usize,
    }
    let num_dc_groups = xsize.div_ceil(layout.lf_dim()) * ysize.div_ceil(layout.lf_dim());
    let mut jobs: Vec<Job> = Vec::new();
    for p in placement.iter().filter(|p| p.global) {
        let ch = &source.channels[p.channel];
        jobs.push(Job {
            channel: p.channel,
            chan_id: p.chan_id,
            stream: 0,
            refs: source.ref_planes(&placement, p, 0, 0),
            x0: 0,
            y0: 0,
            w: ch.w,
            h: ch.h,
        });
    }
    if num_groups > 1 {
        for group_index in 0..num_groups {
            let x0 = (group_index % xsize_groups) * gdim;
            let y0 = (group_index / xsize_groups) * gdim;
            for p in placement.iter().filter(|p| !p.global) {
                let ch = &source.channels[p.channel];
                jobs.push(Job {
                    channel: p.channel,
                    chan_id: p.chan_id,
                    stream: ac_stream_id(num_dc_groups, group_index),
                    refs: source.ref_planes(&placement, p, x0, y0),
                    x0,
                    y0,
                    w: gdim.min(ch.w - x0),
                    h: gdim.min(ch.h - y0),
                });
            }
        }
    }

    // Each job owns a disjoint range in the final sample arrays. The rotating
    // row phase gives this exact count, including narrow and partial groups.
    let counts: Vec<usize> = jobs
        .iter()
        .map(|job| ma_channel_sample_count(job.w, job.h, stride))
        .collect();
    let total_samples = counts.iter().sum();
    let mut samples = MaSamples {
        props: vec![[0; NUM_MA_PROPS]; total_samples],
        tok: vec![[0; NUM_MA_PREDS]; total_samples],
    };
    let mut props = samples.props.as_mut_slice();
    let mut tok = samples.tok.as_mut_slice();
    let mut ranges = Vec::with_capacity(jobs.len());
    for count in counts {
        let (p, rest_p) = props.split_at_mut(count);
        let (t, rest_t) = tok.split_at_mut(count);
        ranges.push((p, t));
        (props, tok) = (rest_p, rest_t);
    }
    pool.steal_for_each_mut(scratch, &mut ranges, |i, range, _scratch| {
        let job = &jobs[i];
        let ch = &source.channels[job.channel];
        let mut rows = range.0.iter_mut().zip(range.1.iter_mut());
        with_ma_pixels!(ch, |pixels| sample_channel_ma(
            |y| &pixels[(job.y0 + y) * ch.w + job.x0..][..job.w],
            job.w,
            job.h,
            job.chan_id,
            job.stream,
            &job.refs,
            wp_params,
            use_wp,
            stride,
            |p, t| {
                let (props, tok) = rows.next().expect("sample count");
                *props = p;
                *tok = t;
            },
        ));
        debug_assert!(rows.next().is_none());
    });
    learn_ma_coarse(
        samples,
        stride,
        min_symbol,
        use_wp,
        max_leaves,
        max_candidates,
        matches!(source.transform, MaTransform::Rct(_)),
        pool,
        scratch,
    )
}

/// Leaf budget of the ranking learner that scores WP presets and the RCT
/// runner-up: 96 leaves rank exactly like 128 on Kodak.
const MA_RANK_LEAVES: usize = 96;
/// The estimator's RCT runner-up is scored by the ranking learner when its
/// estimated cost is within this factor of the best.
const RCT_RANK_MARGIN: f32 = 1.02;

/// Sampling budget of the ranking learners. It must match the final learn's:
/// the best preset for a stride-3 learner is not the best for a stride-1
/// learner.
const MA_RANK_SAMPLE_LIMIT: usize = MA_FULL_SAMPLE_LIMIT;
/// A ranking candidate whose 32-leaf coarse estimate trails the best coarse
/// estimate seen so far by more than this is not deepened.
const RANK_COARSE_PRUNE: f64 = 1.005;

/// First ranking stage of `source`: its coarse learn under the 1024-px
/// layout at the ranking sample budget.
#[allow(clippy::too_many_arguments)]
fn rank_coarse(
    source: &MaSource<'_>,
    xsize: usize,
    ysize: usize,
    min_symbol: u32,
    wp_params: WpParams,
    use_wp: bool,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
) -> Option<CoarseLearn> {
    learn_ma_candidate(
        source,
        xsize,
        ysize,
        GroupLayout::LARGE,
        min_symbol,
        wp_params,
        use_wp,
        MA_RANK_LEAVES,
        MA_RANK_SAMPLE_LIMIT,
        pool,
        scratch,
    )
}

/// Second ranking stage: deepen to the ranking leaf budget and return the
/// estimated bits (the flat alternatives when the tree does not qualify)
/// with to learn itself, so the winner can seed the final learn.
fn rank_finish(
    stage: CoarseLearn,
    min_symbol: u32,
    use_wp: bool,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
) -> (f64, RankedLearn) {
    let coarse_est = stage.candidate.est_real;
    let flat = stage.candidate.flat_real;
    let stride = stage.stride;
    let max_candidates = stage.max_candidates;
    let leaf_offsets = stage.leaf_offsets;
    let (candidate, samples) = if stage.deepen {
        let (tree, samples) = deepen_ma_tree(
            stage.samples,
            ma_learn_params(
                min_symbol,
                MA_RANK_LEAVES,
                max_candidates,
                stride as f64,
                use_wp,
            ),
            stage.candidate.tree.clone(),
            pool,
            scratch,
        );
        (
            gate_ma_tree(tree, stride as f64, None).unwrap_or(stage.candidate),
            samples,
        )
    } else {
        (stage.candidate, stage.samples)
    };
    let bits = candidate.est_real.min(flat);
    (
        bits,
        RankedLearn {
            candidate,
            samples,
            stride,
            max_candidates,
            leaf_offsets,
            coarse_est,
        },
    )
}

/// First learning stage under every layout, second stage for the preferred
/// (first) layout and for any later layout whose first stage already
/// estimates smaller; the full estimates pick the winner. Layouts are
/// processed one at a time so a single sample set is alive.
#[allow(clippy::too_many_arguments)]
fn learn_best_layout(
    source: &MaSource<'_>,
    xsize: usize,
    ysize: usize,
    layouts: &[GroupLayout],
    min_symbol: u32,
    wp_params: WpParams,
    use_wp: bool,
    coarse_limit: f64,
    deepen_all: bool,
    seed: Option<RankedLearn>,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
) -> Option<(GroupLayout, LearnedCandidate, f64)> {
    let mut best_coarse_est = f64::INFINITY;
    let mut learned: Option<(GroupLayout, LearnedCandidate)> = None;
    let mut seed = seed;
    for (index, &layout) in layouts.iter().enumerate() {
        // The ranking winner already sampled and partly grew the first layout.
        if index == 0
            && let Some(ranked) = seed.take()
        {
            best_coarse_est = ranked.coarse_est;
            let cand = ranked.finish(min_symbol, use_wp, pool, scratch);
            learned = Some((layout, cand));
            continue;
        }
        let Some(stage) = learn_ma_candidate(
            source,
            xsize,
            ysize,
            layout,
            min_symbol,
            wp_params,
            use_wp,
            MA_MAX_LEAVES,
            MA_FULL_SAMPLE_LIMIT,
            pool,
            scratch,
        ) else {
            continue;
        };
        let coarse_est = stage.candidate.est_real;
        // A source whose first stage already trails `coarse_limit` is not
        // deepened (the palette candidate against the RGB one). Between this
        // source's own layouts the coarse estimate mis-ranks palette index
        // images (it prefers 1024 where the accurate estimate prefers 256, a
        // ~4% loss), so `deepen_all` sources deepen every layout and pick by
        // the final estimate; photos keep the cheap coarse prune.
        let layout_pruned = index > 0 && !deepen_all && coarse_est > best_coarse_est;
        if coarse_est > coarse_limit || layout_pruned {
            best_coarse_est = best_coarse_est.min(coarse_est);
            continue;
        }
        best_coarse_est = best_coarse_est.min(coarse_est);
        let cand = finish_ma_learn(stage, min_symbol, use_wp, MA_MAX_LEAVES, pool, scratch);
        if learned
            .as_ref()
            .is_none_or(|(_, best)| cand.est_real < best.est_real)
        {
            learned = Some((layout, cand));
        }
    }
    learned.map(|(layout, cand)| (layout, cand, best_coarse_est))
}

// Compile both representations once; use the compact path only when every
// stream fits. The wide fallback retains the same stream order and values.
macro_rules! with_raw_streams {
    ($raw:expr, |$streams:ident| $body:block) => {
        match RawTokenStreams::new($raw) {
            RawTokenStreams::Compact($streams) => $body,
            RawTokenStreams::Wide($streams) => $body,
        }
    };
}

/// Write the complete learned-tree frame under `layout`: a local tree in the
/// single section when the frame is one group, otherwise a global tree in the
/// DC-global section (followed by any global-stream channels) with every AC
/// group routed through it (fresh WP state and group-local coordinates per
/// group, matching the decoder). Returns the candidate's estimated fractional
/// saving over the flat path.
#[allow(clippy::too_many_arguments)]
fn write_learned_tree_frame(
    source: &MaSource<'_>,
    has_alpha: bool,
    xsize: usize,
    ysize: usize,
    layout: GroupLayout,
    min_symbol: u32,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    wp_params: WpParams,
    use_wp: bool,
    cand: &LearnedCandidate,
    writer: &mut BitWriter,
) -> f64 {
    let gdim = layout.dim();
    let xsize_groups = xsize.div_ceil(gdim);
    let ysize_groups = ysize.div_ceil(gdim);
    let num_ac_groups = xsize_groups * ysize_groups;
    let num_dc_groups = xsize.div_ceil(layout.lf_dim()) * ysize.div_ceil(layout.lf_dim());
    let single_group = num_ac_groups == 1;
    let placement = source.placement(layout, single_group);
    let tree = &cand.tree;
    let leaf_ctx = &cand.leaf_ctx;
    let num_ctx = cand.num_ctx;
    let distance_ctx = num_ctx;
    // Placement is in source-channel order; its channel ids already account
    // for the independent global and group streams (including palettes).
    // The compiled lookup folds the channel and stream ids, so it is built
    // per (channel, stream): cheap next to tokenizing the stream itself.
    let tokenize = |p: &MaPlacement,
                    stream: i32,
                    x0: usize,
                    y0: usize,
                    w: usize,
                    h: usize,
                    out: &mut RawTokens| {
        let ch = &source.channels[p.channel];
        let lookup = MaLookup::new(tree, leaf_ctx, p.chan_id, stream);
        let refs = source.ref_planes(&placement, p, x0, y0);
        with_ma_pixels!(ch, |pixels| tokenize_channel_ma(
            |y| &pixels[(y0 + y) * ch.w + x0..][..w],
            w,
            h,
            p.chan_id,
            stream,
            &refs,
            wp_params,
            use_wp,
            &lookup,
            &cand.leaf_offset,
            out,
        ));
    };

    write_frame_header_modular(has_alpha, layout, writer);

    if single_group {
        let channel_tokens = pool.steal_map(scratch, placement.len(), |i, _scratch| {
            let p = &placement[i];
            let ch = &source.channels[p.channel];
            let mut tokens = RawTokens::with_capacity(ch.w * ch.h);
            tokenize(p, 0, 0, 0, ch.w, ch.h, &mut tokens);
            tokens
        });
        let mut section = with_raw_streams!(channel_tokens, |channel_tokens| {
            let mut tokens = Vec::with_capacity(source.total_values());
            for channel in channel_tokens {
                tokens.extend(channel);
            }

            let variants = choose_tree_streams(
                vec![tokens],
                distance_ctx,
                num_ctx as usize + 1,
                min_symbol,
                pool,
                scratch,
            );
            let mut best: Option<BitWriter> = None;
            let mut best_estimate = f64::INFINITY;
            for variant in variants {
                match variant {
                    TreeStreams::Literals(streams) => {
                        let stream = streams.into_iter().next().expect("one stream");
                        write_learned_single_variant(
                            &stream,
                            source,
                            cand,
                            min_symbol,
                            wp_params,
                            pool,
                            scratch,
                            &mut best,
                            &mut best_estimate,
                        );
                    }
                    TreeStreams::Lz(streams) => {
                        let stream = streams.into_iter().next().expect("one stream");
                        write_learned_single_variant(
                            &stream,
                            source,
                            cand,
                            min_symbol,
                            wp_params,
                            pool,
                            scratch,
                            &mut best,
                            &mut best_estimate,
                        );
                    }
                }
            }
            best.expect("learned-tree section")
        });

        writer.write(1, 0);
        writer.zero_pad_to_byte();
        write_toc_entry(section.bits_written() / 8, writer);
        writer.zero_pad_to_byte();
        writer.append_byte_aligned(std::slice::from_mut(&mut section));
        writer.zero_pad_to_byte();
        return cand.estimated_savings;
    }

    // Global stream: channels that fit a group, coded in the DC-global section.
    let mut global_tokens = RawTokens::with_capacity(0);
    for p in placement.iter().filter(|p| p.global) {
        let ch = &source.channels[p.channel];
        tokenize(p, 0, 0, 0, ch.w, ch.h, &mut global_tokens);
    }
    let has_global_stream = !global_tokens.is_empty();

    let group_placement: Vec<&MaPlacement> = placement.iter().filter(|p| !p.global).collect();
    let group_tokens: Vec<RawTokens> = pool.steal_map_with_threads(
        scratch,
        num_ac_groups,
        group_lz_threads(crate::Speed::Slow, pool),
        |group_index, _scratch| {
            let x0 = (group_index % xsize_groups) * gdim;
            let y0 = (group_index / xsize_groups) * gdim;
            let num_values = group_placement
                .iter()
                .map(|p| {
                    let ch = &source.channels[p.channel];
                    gdim.min(ch.w - x0) * gdim.min(ch.h - y0)
                })
                .sum();
            let mut toks = RawTokens::with_capacity(num_values);
            for p in &group_placement {
                let ch = &source.channels[p.channel];
                let gw = gdim.min(ch.w - x0);
                let gh = gdim.min(ch.h - y0);
                tokenize(
                    p,
                    ac_stream_id(num_dc_groups, group_index),
                    x0,
                    y0,
                    gw,
                    gh,
                    &mut toks,
                );
            }
            toks
        },
    );
    // Stream 0 is the global stream (possibly empty), then one per AC group.
    let mut all_tokens: Vec<RawTokens> = Vec::with_capacity(1 + num_ac_groups);
    all_tokens.push(global_tokens);
    all_tokens.extend(group_tokens);
    let mut sections = with_raw_streams!(all_tokens, |all_tokens| {
        let variants = choose_tree_streams(
            all_tokens,
            distance_ctx,
            num_ctx as usize + 1,
            min_symbol,
            pool,
            scratch,
        );
        let mut best: Option<Vec<BitWriter>> = None;
        let mut best_bits = usize::MAX;
        let mut best_estimate = f64::INFINITY;
        for variant in variants {
            match variant {
                TreeStreams::Literals(streams) => {
                    write_learned_grouped_variant(
                        &streams,
                        source,
                        cand,
                        min_symbol,
                        wp_params,
                        pool,
                        scratch,
                        num_ac_groups,
                        num_dc_groups,
                        has_global_stream,
                        &mut best,
                        &mut best_bits,
                        &mut best_estimate,
                    );
                }
                TreeStreams::Lz(streams) => {
                    write_learned_grouped_variant(
                        &streams,
                        source,
                        cand,
                        min_symbol,
                        wp_params,
                        pool,
                        scratch,
                        num_ac_groups,
                        num_dc_groups,
                        has_global_stream,
                        &mut best,
                        &mut best_bits,
                        &mut best_estimate,
                    );
                }
            }
        }
        best.expect("learned-tree sections")
    });

    writer.write(1, 0);
    writer.zero_pad_to_byte();
    for s in &sections {
        write_toc_entry(s.bits_written() / 8, writer);
    }
    writer.zero_pad_to_byte();
    writer.append_byte_aligned(&mut sections);
    writer.zero_pad_to_byte();
    cand.estimated_savings
}

#[allow(clippy::too_many_arguments)]
fn write_learned_single_variant<T: lz77::LzTokenSource>(
    stream: &[T],
    source: &MaSource<'_>,
    cand: &LearnedCandidate,
    min_symbol: u32,
    wp_params: WpParams,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    best: &mut Option<BitWriter>,
    best_estimate: &mut f64,
) {
    let num_ctx = cand.num_ctx;
    let distance_ctx = num_ctx;
    let code = build_lz_pixel_code_threads(
        std::iter::once(stream),
        num_ctx as usize,
        min_symbol,
        true,
        true,
        Some(pool),
        &mut scratch.lz_entropy,
        &mut scratch.huffman_pool,
    );
    let estimate = estimate_coded_bits(
        &[stream],
        distance_ctx,
        &code,
        min_symbol,
        pool.num_threads(),
    );
    if estimate >= *best_estimate * VARIANT_WRITE_TOLERANCE {
        return;
    }
    *best_estimate = (*best_estimate).min(estimate);
    let mut body = BitWriter::new();
    body.write(1, 1); // dc_quant all_default = 1
    body.write(1, 0); // has_tree = 0 (local tree in GroupHeader)
    body.write(1, 0); // use_global_tree = 0
    write_wp_header(wp_params, &mut body);
    source.write_transforms(&mut body);
    write_tree_lz77(
        &cand.tree_tokens,
        &code,
        min_symbol,
        &mut scratch.huffman_pool,
        &mut body,
    );
    write_lz_section(stream, distance_ctx, &code, min_symbol, &mut body);
    body.zero_pad_to_byte();
    keep_smaller_writer(best, body);
}

#[allow(clippy::too_many_arguments)]
fn write_learned_grouped_variant<T: lz77::LzTokenSource>(
    streams: &[Vec<T>],
    source: &MaSource<'_>,
    cand: &LearnedCandidate,
    min_symbol: u32,
    wp_params: WpParams,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    num_ac_groups: usize,
    num_dc_groups: usize,
    has_global_stream: bool,
    best: &mut Option<Vec<BitWriter>>,
    best_bits: &mut usize,
    best_estimate: &mut f64,
) {
    let num_ctx = cand.num_ctx;
    let distance_ctx = num_ctx;
    let num_sections = 1 + num_dc_groups + 1 + num_ac_groups;
    let code = build_lz_pixel_code_threads(
        streams.iter().map(Vec::as_slice),
        num_ctx as usize,
        min_symbol,
        true,
        true,
        Some(pool),
        &mut scratch.lz_entropy,
        &mut scratch.huffman_pool,
    );
    let estimate = {
        let slices: Vec<&[T]> = streams.iter().map(Vec::as_slice).collect();
        estimate_coded_bits(&slices, distance_ctx, &code, min_symbol, pool.num_threads())
    };
    if estimate >= *best_estimate * VARIANT_WRITE_TOLERANCE {
        return;
    }
    *best_estimate = (*best_estimate).min(estimate);
    let mut sections: Vec<BitWriter> = (0..num_sections).map(|_| BitWriter::new()).collect();

    sections[0].write(1, 1); // dc_quant all_default = 1
    sections[0].write(1, 1); // has_tree = 1
    write_tree_lz77(
        &cand.tree_tokens,
        &code,
        min_symbol,
        &mut scratch.huffman_pool,
        &mut sections[0],
    );
    sections[0].write(1, 1); // use_global_tree
    write_wp_header(wp_params, &mut sections[0]);
    source.write_transforms(&mut sections[0]);
    if has_global_stream {
        write_lz_section(
            &streams[0],
            distance_ctx,
            &code,
            min_symbol,
            &mut sections[0],
        );
    }
    sections[0].zero_pad_to_byte();

    for section in sections[1..num_dc_groups + 1].iter_mut() {
        section.write(1, 1);
        write_wp_header(wp_params, section);
        section.write(2, 0);
        section.zero_pad_to_byte();
    }

    let ac_global_idx = 1 + num_dc_groups;
    sections[ac_global_idx].write(1, 1);
    write_wp_header(wp_params, &mut sections[ac_global_idx]);
    sections[ac_global_idx].zero_pad_to_byte();

    // Every AC group section is independent given the code (scoped
    // threads: the code borrows the worker scratch the pool would need).
    let code_ref = &code;
    let group_streams = &streams[1..];
    let threads = pool.num_threads().clamp(1, num_ac_groups.max(1));
    let chunk = num_ac_groups.div_ceil(threads).max(1);
    let group_sections: Vec<BitWriter> = std::thread::scope(|scope| {
        let handles: Vec<_> = group_streams
            .chunks(chunk)
            .map(|part| {
                scope.spawn(move || {
                    part.iter()
                        .map(|stream| {
                            let mut section = BitWriter::new();
                            section.write(1, 1);
                            write_wp_header(wp_params, &mut section);
                            section.write(2, 0);
                            write_lz_section(
                                stream,
                                distance_ctx,
                                code_ref,
                                min_symbol,
                                &mut section,
                            );
                            section.zero_pad_to_byte();
                            section
                        })
                        .collect::<Vec<_>>()
                })
            })
            .collect();
        handles
            .into_iter()
            .flat_map(|h| h.join().expect("group section writer"))
            .collect()
    });
    for (group_index, section) in group_sections.into_iter().enumerate() {
        sections[2 + num_dc_groups + group_index] = section;
    }
    let bits: usize = sections.iter().map(BitWriter::bits_written).sum();
    if bits < *best_bits {
        *best_bits = bits;
        *best = Some(sections);
    }
}

enum TreeStreams<T> {
    /// Retain the original tokens without expanding them to LzToken records.
    Literals(Vec<Vec<T>>),
    Lz(Vec<Vec<LzToken>>),
}

/// Runs and matches collapse a token stream, but a learned tree routes flat
/// content into near-deterministic contexts where a literal costs almost
/// nothing under ANS while every run token still pays for its length and
/// distance (the Burning Ship fractal codes 24% smaller as literals) — yet
/// a palette index image can prefer long matches. Streams that barely
/// collapse keep the LZ77 layer outright; otherwise the order-0 estimates
/// order the literal and LZ77 variants (the deep matcher is skipped when
/// runs alone trail literals by `LZ_DEEP_MATCHER_MAX_RATIO`), and the
/// writer only codes a later variant when its code-based estimate beats
/// the one already written.
const LZ_LITERAL_ALTERNATIVE_MAX_RATIO: f64 = 0.98;
/// Order-0 estimates are optimistic for literal streams whose contexts get
/// merged by clustering (up to ~30% on palette indices), so the matcher is
/// only skipped on a wide margin.
const LZ_DEEP_MATCHER_MAX_RATIO: f64 = 1.6;
/// A later variant is written when its code-based estimate is within this
/// factor of the best written one (the estimate itself is ~0.5% accurate).
const VARIANT_WRITE_TOLERANCE: f64 = 1.005;

/// Stream variants to try for one learned-tree frame, most promising first:
/// literal variants reuse the input, and LZ variants own their match streams.
fn choose_tree_streams<T: lz77::LiteralToken>(
    tokens: Vec<Vec<T>>,
    distance_ctx: u32,
    num_contexts: usize,
    min_symbol: u32,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
) -> Vec<TreeStreams<T>> {
    let raw: usize = tokens.iter().map(Vec::len).sum();
    // Keep the per-stream counts from the existing gate pass so the run
    // candidates can allocate exactly once, without retaining growth slack.
    let run_counts = pool.steal_map(scratch, tokens.len(), |k, _scratch| {
        lz77_run_count(&tokens[k])
    });
    let run_len: usize = run_counts.iter().sum();
    let deep = |scratch: &mut CoderScratch| -> Vec<Vec<LzToken>> {
        let deep_lz = DeepLzScratchPool::new(group_lz_threads(crate::Speed::Slow, pool));
        pool.steal_map_with_threads(
            scratch,
            tokens.len(),
            group_lz_threads(crate::Speed::Slow, pool),
            |k, scratch| {
                deep_lz.with_depth(|depth| {
                    lz77_compress_for_speed_with_depth(
                        &tokens[k],
                        distance_ctx,
                        crate::Speed::Slow,
                        depth,
                        Some(run_counts[k]),
                        scratch,
                    )
                })
            },
        )
    };
    if (run_len as f64) >= LZ_LITERAL_ALTERNATIVE_MAX_RATIO * raw as f64 {
        return vec![TreeStreams::Lz(deep(scratch))];
    }
    let raw_slices: Vec<&[T]> = tokens.iter().map(Vec::as_slice).collect();
    let (e_lit, e_run) =
        estimate_literal_and_run_bits(&raw_slices, num_contexts, min_symbol, pool, scratch);
    if e_run > e_lit * LZ_DEEP_MATCHER_MAX_RATIO {
        return vec![TreeStreams::Literals(tokens)];
    }
    let lz = deep(scratch);
    let slices: Vec<&[LzToken]> = lz.iter().map(Vec::as_slice).collect();
    let e_lz = estimate_streams_bits(&slices, num_contexts, min_symbol, pool, scratch);
    if e_lz < e_lit {
        vec![TreeStreams::Lz(lz), TreeStreams::Literals(tokens)]
    } else {
        vec![TreeStreams::Literals(tokens), TreeStreams::Lz(lz)]
    }
}

/// v1 context tree: single-group only. Returns true (and writes the full frame)
/// if the context tree is estimated to help; false otherwise (caller falls
/// through to the flat path, having written nothing).
fn try_encode_context_tree_single_group(
    linear: &Image3Si,
    alpha: Option<&AlphaPlane>,
    xsize: usize,
    ysize: usize,
    layout: GroupLayout,
    predictors: &[u32],
    min_symbol: u32,
    rct_type: u32,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    wp_params: WpParams,
    writer: &mut BitWriter,
) -> bool {
    let nb_chans = 3 + if alpha.is_some() { 1 } else { 0 };

    // Collect residuals + WP property per channel (WP runs for every channel).
    let collected = pool.steal_map(scratch, nb_chans, |chan, _scratch| {
        if chan < 3 {
            let pd = linear.plane_data(chan);
            collect_channel(
                |x, y| pd[y * xsize + x],
                xsize,
                ysize,
                predictors[chan],
                wp_params,
            )
        } else {
            let a = alpha.expect("alpha channel must exist");
            collect_channel(
                |x, y| a.get_i32(y * xsize + x),
                xsize,
                ysize,
                predictors[chan],
                wp_params,
            )
        }
    });
    let (chan_res, chan_prp): (Vec<Vec<u32>>, Vec<Vec<i64>>) = collected.into_iter().unzip();

    // Per-channel threshold + cost comparison.
    let mut ts = [0i32; 4];
    let mut ctx_bits = 0.0;
    let mut flat_bits = 0.0;
    let threshold_costs = pool.steal_map(scratch, nb_chans, |chan, scratch| {
        pick_threshold(&chan_res[chan], &chan_prp[chan], scratch)
    });
    for (chan, (t, cb, fb)) in threshold_costs.into_iter().enumerate() {
        ts[chan] = t;
        ctx_bits += cb;
        flat_bits += fb;
    }
    // Guard: require the context tree to beat the flat path by more than the
    // extra-context header overhead (~64 bytes per added context, conservative).
    let overhead_bits = (2 * nb_chans) as f32 * 64.0 * 8.0;
    if ctx_bits + overhead_bits >= flat_bits {
        return false;
    }

    // Build tree + context map.
    let tree = build_context_tree(nb_chans, predictors, &ts);
    let mut tree_tokens: Vec<Token> = Vec::new();
    let ctx_map = emit_ct_tree(&tree, &mut tree_tokens);
    // Flat lookup over the dense (chan*3+bucket) property space, replacing a
    // per-pixel HashMap probe in the tokenize loop.
    let ctx_lut: Vec<u32> = (0..(nb_chans as u32) * 3).map(|k| ctx_map[&k]).collect();
    let num_pixel_ctx = nb_chans * 3;

    // Tokenize: each pixel routed to context (channel,bucket).
    let channel_tokens = pool.steal_map(scratch, nb_chans, |chan, _scratch| {
        let mut tokens = Vec::with_capacity(xsize * ysize);
        let res = &chan_res[chan];
        let prp = &chan_prp[chan];
        let t = ts[chan] as i64;
        for (&prp, &res) in prp[..res.len()].iter().zip(res.iter()) {
            let bucket = bucket_of(prp, t);
            let ctx = ctx_lut[chan * 3 + bucket as usize];
            tokens.push(Token::new(ctx, res));
        }
        tokens
    });
    let mut tokens: Vec<Token> = Vec::with_capacity(xsize * ysize * nb_chans);
    for channel in channel_tokens {
        tokens.extend(channel);
    }

    // Frame header + single section.
    write_frame_header_modular(alpha.is_some(), layout, writer);
    let mut section = BitWriter::new();
    section.write(1, 1); // dc_quant all_default = 1
    section.write(1, 0); // has_tree = 0
    section.write(1, 0); // use_global_tree = 0
    write_wp_header(wp_params, &mut section);
    write_modular_transforms(nb_chans, rct_type, &mut section);

    let distance_ctx = num_pixel_ctx as u32;
    let lz_tokens = lz77_compress_for_speed(&tokens, distance_ctx, crate::Speed::Slow, scratch);
    let code = build_lz_pixel_code(
        std::iter::once(lz_tokens.as_slice()),
        num_pixel_ctx,
        min_symbol,
        true,
        &mut scratch.lz_entropy,
        &mut scratch.huffman_pool,
    );
    write_tree_lz77(
        &tree_tokens,
        &code,
        min_symbol,
        &mut scratch.huffman_pool,
        &mut section,
    );
    write_lz_section(&lz_tokens, distance_ctx, &code, min_symbol, &mut section);
    section.zero_pad_to_byte();

    writer.write(1, 0);
    writer.zero_pad_to_byte();
    write_toc_entry(section.bits_written() / 8, writer);
    writer.zero_pad_to_byte();
    writer.append_byte_aligned(std::slice::from_mut(&mut section));
    writer.zero_pad_to_byte();
    true
}

/// v1 context tree, multi-group. Global context tree in LfGlobal; each AC group
/// routes its group-local pixels through it (fresh WP per group, matching the
/// decoder). Returns true (and writes the full frame) if it helps; else false.
fn try_encode_context_tree_multi_group(
    linear: &Image3Si,
    alpha: Option<&AlphaPlane>,
    xsize: usize,
    ysize: usize,
    layout: GroupLayout,
    predictors: &[u32],
    xsize_groups: usize,
    ysize_groups: usize,
    num_dc_groups: usize,
    min_symbol: u32,
    rct_type: u32,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    wp_params: WpParams,
    writer: &mut BitWriter,
) -> bool {
    let nb_chans = 3 + if alpha.is_some() { 1 } else { 0 };
    let num_ac_groups = xsize_groups * ysize_groups;

    // 1) Collect (residual, WP property) per group per channel (group-local WP).
    let groups: Vec<Vec<(Vec<u32>, Vec<i64>)>> =
        pool.steal_map(scratch, num_ac_groups, |group_index, _scratch| {
            let gx = group_index % xsize_groups;
            let gy = group_index / xsize_groups;
            let x0 = gx * layout.dim();
            let y0 = gy * layout.dim();
            let gw = layout.dim().min(xsize - x0);
            let gh = layout.dim().min(ysize - y0);
            let mut chans: Vec<(Vec<u32>, Vec<i64>)> = Vec::with_capacity(nb_chans);
            for chan in 0..3usize {
                let pd = linear.plane_data(chan);
                let get = |lx: usize, ly: usize| pd[(y0 + ly) * xsize + (x0 + lx)];
                chans.push(collect_channel(get, gw, gh, predictors[chan], wp_params));
            }
            if let Some(a) = alpha {
                let get = |lx: usize, ly: usize| a.get_i32((y0 + ly) * xsize + (x0 + lx));
                chans.push(collect_channel(get, gw, gh, predictors[3], wp_params));
            }
            chans
        });

    // 2) Global per-channel threshold from aggregated stats.
    let mut ts = [0i32; 4];
    let mut ctx_bits = 0.0;
    let mut flat_bits = 0.0;
    let threshold_costs = pool.steal_map(scratch, nb_chans, |chan, scratch| {
        pick_threshold_grouped(&groups, chan, scratch)
    });
    for (chan, (t, cb, fb)) in threshold_costs.into_iter().enumerate() {
        ts[chan] = t;
        ctx_bits += cb;
        flat_bits += fb;
    }
    let overhead_bits = (2 * nb_chans) as f32 * 64.0 * 8.0;
    if ctx_bits + overhead_bits >= flat_bits {
        return false;
    }

    // 3) Global context tree + map.
    let tree = build_context_tree(nb_chans, predictors, &ts);
    let mut tree_tokens: Vec<Token> = Vec::new();
    let ctx_map = emit_ct_tree(&tree, &mut tree_tokens);
    // Flat lookup over the dense (chan*3+bucket) property space, replacing a
    // per-pixel HashMap probe in the tokenize loop.
    let ctx_lut: Vec<u32> = (0..(nb_chans as u32) * 3).map(|k| ctx_map[&k]).collect();
    let num_pixel_ctx = nb_chans * 3;
    let distance_ctx = num_pixel_ctx as u32;

    // 4) Per-group tokens (reusing collected res/prop) + per-group LZ77.
    let group_lz_tokens: Vec<Vec<LzToken>> = {
        let deep_lz = DeepLzScratchPool::new(group_lz_threads(crate::Speed::Slow, pool));
        pool.steal_map_with_threads(
            scratch,
            num_ac_groups,
            group_lz_threads(crate::Speed::Slow, pool),
            |group_index, scratch| {
                let g = &groups[group_index];
                let token_count = g.iter().map(|(res, _)| res.len()).sum();
                let mut toks: Vec<Token> = Vec::with_capacity(token_count);
                for (((res, prp), &threshold), contexts) in g[..nb_chans]
                    .iter()
                    .zip(&ts[..nb_chans])
                    .zip(ctx_lut.as_chunks::<3>().0)
                {
                    let t = threshold as i64;
                    for (&prp, &res) in prp[..res.len()].iter().zip(res.iter()) {
                        let bucket = bucket_of(prp, t);
                        let ctx = contexts[bucket as usize];
                        toks.push(Token::new(ctx, res));
                    }
                }
                deep_lz.with_depth(|depth| {
                    lz77_compress_for_speed_with_depth(
                        &toks,
                        distance_ctx,
                        crate::Speed::Slow,
                        depth,
                        None,
                        scratch,
                    )
                })
            },
        )
    };
    let code = build_lz_pixel_code(
        group_lz_tokens.iter().map(Vec::as_slice),
        num_pixel_ctx,
        min_symbol,
        true,
        &mut scratch.lz_entropy,
        &mut scratch.huffman_pool,
    );

    // 5) Sections (same layout as the flat multi-group path).
    write_frame_header_modular(alpha.is_some(), layout, writer);
    let num_sections = 1 + num_dc_groups + 1 + num_ac_groups;
    let mut sections: Vec<BitWriter> = (0..num_sections).map(|_| BitWriter::new()).collect();

    sections[0].write(1, 1); // dc_quant all_default = 1
    sections[0].write(1, 1); // has_tree = 1
    write_tree_lz77(
        &tree_tokens,
        &code,
        min_symbol,
        &mut scratch.huffman_pool,
        &mut sections[0],
    );
    sections[0].write(1, 1); // use_global_tree
    write_wp_header(wp_params, &mut sections[0]);
    write_modular_transforms(nb_chans, rct_type, &mut sections[0]);
    sections[0].zero_pad_to_byte();

    for section in sections[1..num_dc_groups + 1].iter_mut() {
        section.write(1, 1);
        write_wp_header(wp_params, section);
        section.write(2, 0);
        section.zero_pad_to_byte();
    }

    let ac_global_idx = 1 + num_dc_groups;
    sections[ac_global_idx].write(1, 1);
    write_wp_header(wp_params, &mut sections[ac_global_idx]);
    sections[ac_global_idx].zero_pad_to_byte();

    write_lz_groups(
        &group_lz_tokens,
        &code,
        distance_ctx,
        min_symbol,
        wp_params,
        pool,
        &mut sections[2 + num_dc_groups..],
    );

    writer.write(1, 0);
    writer.zero_pad_to_byte();
    for s in &sections {
        write_toc_entry(s.bits_written() / 8, writer);
    }
    writer.zero_pad_to_byte();
    writer.append_byte_aligned(&mut sections);
    writer.zero_pad_to_byte();
    true
}

// ---------------------------------------------------------------------------
// Tree writing (balanced N-leaf, Gradient predictor).
//
// libjxl reads modular trees in BFS order: each node is one PROPERTY token
// (=0 for leaf, =prop+1 for split) optionally followed by a SPLIT_VAL token
// or 4 leaf-data tokens (predictor, offset, multiplier-log, multiplier-bits).
// All channels share the Gradient predictor with offset 0 and multiplier 1.
// ---------------------------------------------------------------------------

fn push_split(out: &mut Vec<Token>, property: u32, split_val: i32) {
    out.push(Token::new(TREE_CTX_PROPERTY, property + 1));
    out.push(Token::new(TREE_CTX_SPLIT_VAL, pack_signed(split_val)));
}

fn push_leaf(out: &mut Vec<Token>, predictor: u32) {
    push_leaf_mul(out, predictor, 1);
}

/// Leaf with a residual multiplier: the decoder reconstructs
/// `pred + multiplier * residual` (multiplier = (mul_bits+1) << mul_log).
fn push_leaf_mul(out: &mut Vec<Token>, predictor: u32, multiplier: u32) {
    debug_assert!(multiplier >= 1);
    let mul_log = multiplier.trailing_zeros();
    let mul_bits = (multiplier >> mul_log) - 1;
    out.push(Token::new(TREE_CTX_PROPERTY, 0));
    out.push(Token::new(TREE_CTX_PREDICTOR, predictor));
    out.push(Token::new(TREE_CTX_OFFSET, pack_signed(0)));
    out.push(Token::new(TREE_CTX_MULTIPLIER_LOG, mul_log));
    out.push(Token::new(TREE_CTX_MULTIPLIER_BITS, mul_bits));
}

/// Balanced channel-split tree whose leaves carry per-channel residual
/// multipliers (the lossy-modular quantization steps).
fn build_balanced_tree_tokens_mul(leaves: &[(u32, u32)]) -> Vec<Token> {
    let n_leaves = leaves.len();
    let mut t = Vec::new();
    match n_leaves {
        1 => push_leaf_mul(&mut t, leaves[0].0, leaves[0].1),
        2 => {
            push_split(&mut t, 0, 0);
            push_leaf_mul(&mut t, leaves[1].0, leaves[1].1);
            push_leaf_mul(&mut t, leaves[0].0, leaves[0].1);
        }
        _ => {
            // Right-leaning chain, BFS leaves chan(N-1)..chan0 (same shape as
            // `build_balanced_tree_tokens`).
            for k in (1..=(n_leaves - 2)).rev() {
                push_split(&mut t, 0, k as i32);
                push_leaf_mul(&mut t, leaves[k + 1].0, leaves[k + 1].1);
            }
            push_split(&mut t, 0, 0);
            push_leaf_mul(&mut t, leaves[1].0, leaves[1].1);
            push_leaf_mul(&mut t, leaves[0].0, leaves[0].1);
        }
    }
    t
}

/// Build a balanced binary tree over `n_leaves` leaves splitting on property 0
/// (the channel index after RCT).  BFS leaf order is chan N-1, ..., chan 0.
fn build_balanced_tree_tokens(predictors: &[u32]) -> Vec<Token> {
    let n_leaves = predictors.len();
    let mut t = Vec::new();
    // Leaves are emitted in BFS order chan(N-1), ..., chan0 (matches fjxl), so
    // index them with predictors[chan].
    match n_leaves {
        1 => push_leaf(&mut t, predictors[0]),
        2 => {
            push_split(&mut t, 0, 0);
            push_leaf(&mut t, predictors[1]); // chan 1
            push_leaf(&mut t, predictors[0]); // chan 0
        }
        3 => {
            push_split(&mut t, 0, 1);
            push_leaf(&mut t, predictors[2]); // chan 2
            push_split(&mut t, 0, 0);
            push_leaf(&mut t, predictors[1]); // chan 1
            push_leaf(&mut t, predictors[0]); // chan 0
        }
        4 => {
            push_split(&mut t, 0, 1);
            push_split(&mut t, 0, 2);
            push_split(&mut t, 0, 0);
            push_leaf(&mut t, predictors[3]); // chan 3
            push_leaf(&mut t, predictors[2]); // chan 2
            push_leaf(&mut t, predictors[1]); // chan 1
            push_leaf(&mut t, predictors[0]); // chan 0
        }
        _ => {
            // N>=5: right-leaning chain. BFS emission yields leaves in order
            // chan(N-1)..chan0 -> contexts 0..N-1, matching channel_to_context.
            // Peels off the highest channel at each split: split(0,k) sends
            // channel>k (== k+1) to a leaf, else continue.
            for k in (1..=(n_leaves - 2)).rev() {
                push_split(&mut t, 0, k as i32);
                push_leaf(&mut t, predictors[k + 1]);
            }
            push_split(&mut t, 0, 0);
            push_leaf(&mut t, predictors[1]);
            push_leaf(&mut t, predictors[0]);
        }
    }
    t
}

// ---------------------------------------------------------------------------
// f32 lossless (v1): non-negative float, RGB, no alpha. The 32-bit float bits
// are reinterpreted as int32 channels (matching libjxl float_to_int for
// bits==32: a raw memcpy), then coded as a modular image with NO RCT and NO
// LZ77. LZ77 must be off because float residual tokens can reach 127, leaving
// no room below the 128-symbol alphabet for LZ77 length symbols. Per-channel
// gradient/WP prediction and the channel-split tree are reused unchanged.
// ---------------------------------------------------------------------------

/// Write a channel-split MA tree (no LZ77 in the tree) followed by the pixel
/// entropy code header with LZ77 DISABLED. Mirrors the tree-writing convention
/// but flips the pixel stream's LZ77 flag off.
fn write_tree_and_pixel_code_nolz(
    tree_tokens: &[Token],
    pixel_code: &OwnedEntropyCode,
    scratch: &mut CoderScratch,
    w: &mut BitWriter,
) {
    let tree_code =
        optimize_entropy_code(tree_tokens, NUM_TREE_CONTEXTS, &mut scratch.huffman_pool);
    let tree_code_ref = tree_code.as_ref();
    w.write(1, 0); // tree entropy code: no LZ77
    write_entropy_code(&tree_code_ref, &mut scratch.huffman_pool, w);
    for tok in tree_tokens {
        write_token(*tok, &tree_code_ref, w);
    }
    // Pixel entropy code: LZ77 DISABLED (1 bit = 0), then the code itself.
    w.write(1, 0);
    write_entropy_code(&pixel_code.as_ref(), &mut scratch.huffman_pool, w);
}

pub(crate) fn encode_frame_lossless_float(
    linear: &Image3Si,
    alpha: Option<&AlphaPlane>,
    num_threads: usize,
    writer: &mut BitWriter,
) {
    let pool = ThreadPool::new_lossless(num_threads);
    let mut scratch = Box::new(CoderScratch::lossless());
    encode_frame_lossless_float_with_pool(linear, alpha, &pool, &mut scratch, writer);
}

fn encode_frame_lossless_float_with_pool(
    linear: &Image3Si,
    alpha: Option<&AlphaPlane>,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    writer: &mut BitWriter,
) {
    let xsize = linear.xsize();
    let ysize = linear.ysize();
    let nb_chans = 3usize + if alpha.is_some() { 1 } else { 0 };

    let xsize_groups = xsize.div_ceil(GROUP_DIM);
    let ysize_groups = ysize.div_ceil(GROUP_DIM);
    let num_ac_groups = xsize_groups * ysize_groups;
    let xsize_dc_groups = xsize.div_ceil(LF_GROUP_DIM);
    let ysize_dc_groups = ysize.div_ceil(LF_GROUP_DIM);
    let num_dc_groups = xsize_dc_groups * ysize_dc_groups;
    let single_group = num_ac_groups == 1;
    let grad_pack_fn = selected_grad_pack_interior_fn();

    // Float-bit values reach ~2^30, where the Weighted Predictor's internal
    // weight/divlookup arithmetic is not bit-verified against libjxl. Gradient
    // (clamp(W+N-NW), computed in i64) is exact at any magnitude and the
    // decoder reconstructs it identically, so the float path uses gradient on
    // every channel. (Predictor id 5 = gradient.)
    const GRADIENT_PRED: u32 = 5;
    let predictors = [GRADIENT_PRED; 4];
    let tree_tokens = build_balanced_tree_tokens(&predictors[..nb_chans]);

    write_frame_header_modular(alpha.is_some(), GroupLayout::DEFAULT, writer);

    if single_group {
        let mut section = BitWriter::new();
        section.write(1, 1); // dc_quant all_default = 1
        section.write(1, 0); // has_tree = 0
        section.write(1, 0); // use_global_tree = 0
        section.write(1, 1); // wp_default = 1
        section.write(2, 0b00); // 0 transforms (no RCT for float bits)

        let tokens = tokenize_all(
            linear,
            alpha,
            xsize,
            ysize,
            0,
            0,
            xsize,
            ysize,
            3,
            &predictors,
            grad_pack_fn,
            pool,
            scratch,
        );
        let code = optimize_entropy_code(&tokens, nb_chans, &mut scratch.huffman_pool);
        write_tree_and_pixel_code_nolz(&tree_tokens, &code, scratch, &mut section);
        for t in &tokens {
            write_token(*t, &code.as_ref(), &mut section);
        }
        section.zero_pad_to_byte();

        writer.write(1, 0);
        writer.zero_pad_to_byte();
        write_toc_entry(section.bits_written() / 8, writer);
        writer.zero_pad_to_byte();
        writer.append_byte_aligned(std::slice::from_mut(&mut section));
        writer.zero_pad_to_byte();
    } else {
        let num_sections = 1 + num_dc_groups + 1 + num_ac_groups;
        let mut sections: Vec<BitWriter> = (0..num_sections).map(|_| BitWriter::new()).collect();

        // Tokenize each AC group (group-local) and pool for one global code.
        let group_tokens: Vec<Vec<Token>> =
            pool.steal_map(scratch, num_ac_groups, |group_index, scratch| {
                let gx = group_index % xsize_groups;
                let gy = group_index / xsize_groups;
                let x0 = gx * GROUP_DIM;
                let y0 = gy * GROUP_DIM;
                let gw = GROUP_DIM.min(xsize - x0);
                let gh = GROUP_DIM.min(ysize - y0);
                tokenize_all(
                    linear,
                    alpha,
                    xsize,
                    ysize,
                    x0,
                    y0,
                    gw,
                    gh,
                    3,
                    &predictors,
                    grad_pack_fn,
                    pool,
                    scratch,
                )
            });
        let mut all_tokens: Vec<Token> = Vec::new();
        for tokens in &group_tokens {
            all_tokens.extend_from_slice(tokens);
        }
        let code = optimize_entropy_code(&all_tokens, nb_chans, &mut scratch.huffman_pool);

        // Section 0: DC global (tree + pixel code, both no-LZ77) + GroupHeader.
        sections[0].write(1, 1); // dc_quant
        sections[0].write(1, 1); // has_tree = 1
        write_tree_and_pixel_code_nolz(&tree_tokens, &code, scratch, &mut sections[0]);
        sections[0].write(1, 1); // use_global_tree
        sections[0].write(1, 1); // wp_default
        sections[0].write(2, 0b00); // 0 transforms
        sections[0].zero_pad_to_byte();

        for section in &mut sections[1..1 + num_dc_groups] {
            section.write(1, 1);
            section.write(1, 1);
            section.write(2, 0);
            section.zero_pad_to_byte();
        }

        let ac_global_idx = 1 + num_dc_groups;
        sections[ac_global_idx].write(1, 1);
        sections[ac_global_idx].write(1, 1);
        sections[ac_global_idx].zero_pad_to_byte();

        for (section, tokens) in sections[2 + num_dc_groups..][..num_ac_groups]
            .iter_mut()
            .zip(&group_tokens[..num_ac_groups])
        {
            section.write(1, 1);
            section.write(1, 1);
            section.write(2, 0);
            for t in tokens {
                write_token(*t, &code.as_ref(), section);
            }
            section.zero_pad_to_byte();
        }

        writer.write(1, 0);
        writer.zero_pad_to_byte();
        for s in &sections {
            write_toc_entry(s.bits_written() / 8, writer);
        }
        writer.zero_pad_to_byte();
        writer.append_byte_aligned(&mut sections);
        writer.zero_pad_to_byte();
    }
}

#[cfg(test)]
mod rate_selection_tests {
    use super::palette::local_palette_coverage_is_sufficient;
    use super::*;

    #[test]
    fn shortlist_thresholds_keep_ambiguous_candidates() {
        assert!(!local_palette_coverage_is_sufficient(24, 100));
        assert!(local_palette_coverage_is_sufficient(25, 100));
        assert!(!learned_tree_is_decisive(
            MA_DECISIVE_MIN_SAVINGS - f64::EPSILON
        ));
        assert!(learned_tree_is_decisive(MA_DECISIVE_MIN_SAVINGS));
    }

    fn image_from_rgb(
        width: usize,
        height: usize,
        mut rgb: impl FnMut(usize, usize) -> [i32; 3],
    ) -> Image3Si {
        let mut image = Image3Si::new(width, height);
        for y in 0..height {
            let [p0, p1, p2] = image.all_plane_rows_mut(y);
            for x in 0..width {
                let [r, g, b] = rgb(x, y);
                let (yc, co, cg) = forward_ycocg(r, g, b);
                p0[x] = yc;
                p1[x] = co;
                p2[x] = cg;
            }
        }
        image
    }

    fn encode_core(image: &Image3Si, allow_palettes: bool) -> BitWriter {
        let pool = ThreadPool::new_lossless(1);
        let mut scratch = CoderScratch::lossless();
        let mut writer = BitWriter::new();
        encode_frame_lossless_core_impl(
            image,
            None,
            8,
            false,
            3,
            crate::Speed::Slow,
            crate::DecodingSpeed::Slow,
            &pool,
            &mut scratch,
            ModularFrameKind::Regular,
            allow_palettes,
            &mut writer,
        );
        writer
    }

    #[test]
    fn slow_single_group_palette_is_rate_safe() {
        let image = image_from_rgb(64, 64, |x, y| {
            let c = ((x / 8 + y / 8) & 3) as i32;
            [c * 61, c * 37, c * 19]
        });
        let selected = encode_core(&image, true);
        let normal = encode_core(&image, false);
        assert!(selected.bits_written() <= normal.bits_written());
    }

    #[test]
    fn slow_local_palette_is_rate_safe_for_mixed_content() {
        let image = image_from_rgb(257, 257, |x, y| {
            if x < 256 && y < 128 {
                let c = ((x / 16 + y / 16) & 7) as i32;
                [c * 31, c * 17, c * 7]
            } else {
                let v = x
                    .wrapping_mul(1_664_525)
                    .wrapping_add(y.wrapping_mul(1_013_904_223));
                [
                    ((v >> 8) & 255) as i32,
                    ((v >> 16) & 255) as i32,
                    ((v >> 24) & 255) as i32,
                ]
            }
        });
        let selected = encode_core(&image, true);
        let normal = encode_core(&image, false);
        assert!(selected.bits_written() <= normal.bits_written());
    }
}

#[cfg(test)]
mod ma_sampling_tests {
    use super::*;

    #[test]
    fn row_walk_matches_point_fetched_properties_and_predictions() {
        for width in [0usize, 1, 2, 3, 4, 5, 17, 257] {
            for height in [0usize, 1, 2, 7] {
                for flat in [false, true] {
                    let plane: Vec<i32> = (0..width * height)
                        .map(|i| {
                            if flat {
                                8192
                            } else {
                                [i32::MIN, i32::MAX, -65536, 65535, -8192, 0, 8192]
                                    [(i * 17 + i / width) % 7]
                            }
                        })
                        .collect();
                    let get = |x: usize, y: usize| plane[y * width + x];
                    for &params in &WpParams::PRESETS {
                        for use_wp in [false, true] {
                            let mut wp = WpState::with_params(width, params);
                            let mut previous_gradient = 0i32;
                            let mut count = 0;
                            walk_channel_ma(
                                |y| &plane[y * width..][..width],
                                width,
                                height,
                                2,
                                0,
                                &[],
                                params,
                                use_wp,
                                |x, y, value, props, neighbors, weighted| {
                                    assert_eq!(count, y * width + x);
                                    assert_eq!(value, get(x, y) as i64);
                                    count += 1;
                                    if x == 0 {
                                        previous_gradient = 0;
                                    }
                                    let n = predictor_neighbors(&get, x, y, width);
                                    let expected_wp = if use_wp {
                                        wp.predict(
                                            x,
                                            y,
                                            n.top,
                                            n.left,
                                            n.top_right,
                                            n.top_left,
                                            n.top_top,
                                        )
                                    } else {
                                        0
                                    };
                                    let gradient = (n.left + n.top - n.top_left) as i32;
                                    // No reference planes: the extra properties stay 0.
                                    assert!(props[16..].iter().all(|&v| v == 0));
                                    assert_eq!(
                                        props[..16],
                                        [
                                            2,
                                            0,
                                            y as i32,
                                            x as i32,
                                            n.top.abs() as i32,
                                            n.left.abs() as i32,
                                            n.top as i32,
                                            n.left as i32,
                                            (n.left - previous_gradient as i64) as i32,
                                            gradient,
                                            (n.left - n.top_left) as i32,
                                            (n.top_left - n.top) as i32,
                                            (n.top - n.top_right) as i32,
                                            (n.top - n.top_top) as i32,
                                            (n.left - n.left_left) as i32,
                                            if use_wp { wp.wp_prop as i32 } else { 0 },
                                        ]
                                    );
                                    assert_eq!(weighted, expected_wp);
                                    for pred in 0..NUM_MA_PREDS as u32 {
                                        assert_eq!(
                                            predictor_value(pred, neighbors, weighted),
                                            predictor_value(pred, n, expected_wp),
                                        );
                                    }
                                    previous_gradient = gradient;
                                    if use_wp {
                                        wp.update(value, x, y);
                                    }
                                },
                            );
                            assert_eq!(count, width * height);
                        }
                    }
                }
            }
        }
    }

    fn reference_samples(
        plane: &[i32],
        width: usize,
        height: usize,
        stride: usize,
        params: WpParams,
    ) -> MaSamples {
        let mut samples = MaSamples::new();
        walk_channel_ma(
            |y| &plane[y * width..][..width],
            width,
            height,
            2,
            0,
            &[],
            params,
            true,
            |x, y, value, p, n, wp_pred| {
                // Row-rotated phase: row y samples x == y (mod stride).
                if x % stride != y % stride {
                    return;
                }
                let mut tok = [0u8; NUM_MA_PREDS];
                for pred in 0..NUM_MA_PREDS as u32 {
                    let prediction = predictor_value(pred, n, wp_pred);
                    let (token, _, _) = uint_encode(pack_signed((value - prediction) as i32));
                    tok[pred as usize] = token.min(u8::MAX as u32) as u8;
                }
                samples.push(*p, tok);
            },
        );
        samples
    }

    #[test]
    fn stride_aware_sampling_matches_full_property_walk() {
        for &(width, height) in &[(0, 3), (1, 1), (1, 7), (2, 5), (9, 7), (17, 4)] {
            let plane: Vec<i32> = (0..width * height)
                .map(|i| {
                    let x = i % width.max(1);
                    let y = i / width.max(1);
                    (((x * 977 + y * 619) ^ (x * y * 37)) as i32 & 0xffff) - 0x7fff
                })
                .collect();
            let get_row = |y: usize| &plane[y * width..][..width];
            for &stride in &[1, 2, 3, 5, 11, width * height + 1] {
                for &params in &WpParams::PRESETS {
                    let expected = reference_samples(&plane, width, height, stride, params);
                    let mut actual = MaSamples::new();
                    sample_channel_ma(
                        get_row,
                        width,
                        height,
                        2,
                        0,
                        &[],
                        params,
                        true,
                        stride,
                        |p, t| actual.push(p, t),
                    );
                    assert_eq!(actual.len(), ma_channel_sample_count(width, height, stride));
                    assert_eq!(
                        actual.props, expected.props,
                        "properties: {width}x{height}, stride={stride}, params={params:?}"
                    );
                    assert_eq!(
                        actual.tok, expected.tok,
                        "tokens: {width}x{height}, stride={stride}, params={params:?}"
                    );
                }
            }
        }
    }
}

#[cfg(test)]
mod lz_reach_probe {

    use super::quantize_xyb_channels;
    use crate::image::Image3F;
    use crate::quant_weights::INV_DC_QUANT;

    #[test]
    fn slow_lossless_encode_of_repetitive_image() {
        // Highly repetitive => lz_has_repetition() is true => deep path runs.
        let (w, h) = (512usize, 512usize);
        let mut rgb = vec![0u8; w * h * 3];
        for y in 0..h {
            for x in 0..w {
                let v = (((x / 8) + (y / 8)) % 2) as u8 * 255;
                let i = (y * w + x) * 3;
                rgb[i] = v;
                rgb[i + 1] = v;
                rgb[i + 2] = v;
            }
        }
        let cfg = crate::EncodeConfig::default()
            .with_lossless(true)
            .with_speed(crate::Speed::Slow)
            .with_num_threads(1);
        let out = crate::encode_image(&rgb, w, h, &cfg).expect("encode failed");
        eprintln!("PROBE encoded {} bytes", out.len());
        assert!(!out.is_empty());
    }

    #[test]
    fn packed_quantization_matches_the_channel_formula() {
        let mut atlas = Image3F::new(5, 3);
        for i in 0..15 {
            atlas.plane_row_mut(0, i / 5)[i % 5] = (i as f32 - 7.0) / 41.0;
            atlas.plane_row_mut(1, i / 5)[i % 5] = i as f32 / 29.0;
            atlas.plane_row_mut(2, i / 5)[i % 5] = (15 - i) as f32 / 31.0;
        }

        for lattice_scale in [1, 2, 8] {
            let got = quantize_xyb_channels(&atlas, lattice_scale);
            let m = lattice_scale as f32;
            for i in 0..15 {
                let yq = (atlas.plane_data(1)[i] * INV_DC_QUANT[1] * m).round() as i32;
                assert_eq!(got[0][i], yq);
                assert_eq!(
                    got[1][i],
                    (atlas.plane_data(0)[i] * INV_DC_QUANT[0] * m).round() as i32
                );
                assert_eq!(
                    got[2][i],
                    (atlas.plane_data(2)[i] * INV_DC_QUANT[2] * m).round() as i32 - yq
                );
            }
        }
    }
}
#[cfg(test)]
mod context_tree_tests {
    use super::*;

    #[test]
    fn gated_tree_keeps_bfs_tokens_with_leaf_offsets() {
        let tree = LearnedTree {
            nodes: vec![
                MaNode::Split {
                    prop: 0,
                    val: 0,
                    gt: 1,
                    le: 2,
                },
                MaNode::Leaf { pred: 1 },
                MaNode::Split {
                    prop: 3,
                    val: 3,
                    gt: 3,
                    le: 4,
                },
                MaNode::Leaf { pred: 6 },
                MaNode::Leaf { pred: 0 },
            ],
            est_bits: 100.,
            flat_bits: 10000.,
        };
        let mut samples = MaSamples::new();
        for (channel, x, residual) in [(1, 0, -3), (0, 4, 5), (0, 3, 0)] {
            let mut props = [0; NUM_MA_PROPS];
            props[0] = channel;
            props[3] = x;
            for _ in 0..64 {
                samples.push(props, [pack_signed(residual) as u8; NUM_MA_PREDS]);
            }
        }
        for with_offsets in [false, true] {
            let candidate =
                gate_ma_tree(tree.clone(), 1., with_offsets.then_some(&samples)).unwrap();
            let offsets = if with_offsets { [-3, 5, 0] } else { [0; 3] };
            assert_eq!(candidate.leaf_offset, offsets);
            assert_eq!(candidate.leaf_ctx, [u32::MAX, 0, u32::MAX, 1, 2]);
            assert_eq!(candidate.num_ctx, 3);
            let leaf = |pred, offset| {
                [
                    (TREE_CTX_PROPERTY, 0),
                    (TREE_CTX_PREDICTOR, pred),
                    (TREE_CTX_OFFSET, pack_signed(offset)),
                    (TREE_CTX_MULTIPLIER_LOG, 0),
                    (TREE_CTX_MULTIPLIER_BITS, 0),
                ]
            };
            let mut expected = vec![(TREE_CTX_PROPERTY, 1), (TREE_CTX_SPLIT_VAL, 0)];
            expected.extend(leaf(1, offsets[0]));
            expected.extend([(TREE_CTX_PROPERTY, 4), (TREE_CTX_SPLIT_VAL, 6)]);
            expected.extend(leaf(6, offsets[1]));
            expected.extend(leaf(0, offsets[2]));
            let actual: Vec<_> = candidate
                .tree_tokens
                .iter()
                .map(|token| (token.context, token.value))
                .collect();
            assert_eq!(actual, expected);
            assert_eq!(candidate.est_real, 890.);
        }
        // Admission uses only the unchanged token-count estimate.
        for est_bits in [10000., f64::NAN] {
            assert!(
                gate_ma_tree(
                    LearnedTree {
                        est_bits,
                        ..tree.clone()
                    },
                    1.,
                    Some(&samples)
                )
                .is_none()
            );
        }
    }

    #[test]
    fn grouped_threshold_scoring_matches_concatenated_reference() {
        let groups = vec![
            vec![(vec![0, 3, 7, 3], vec![-20, -4, 12, 80])],
            vec![(vec![7, 1, 1, 9, 2], vec![-100, -9, 0, 17, 110])],
        ];
        let residuals: Vec<u32> = groups
            .iter()
            .flat_map(|group| group[0].0.iter().copied())
            .collect();
        let properties: Vec<i64> = groups
            .iter()
            .flat_map(|group| group[0].1.iter().copied())
            .collect();
        let expected = pick_threshold(&residuals, &properties, &mut CoderScratch::default());
        let actual = pick_threshold_grouped(&groups, 0, &mut CoderScratch::default());
        assert_eq!(actual, expected);
    }
    #[test]
    fn compiled_ma_tokenization_matches_full_tree_with_and_without_wp() {
        let tree = LearnedTree {
            nodes: vec![
                MaNode::Split {
                    prop: 0,
                    val: 0,
                    gt: 1,
                    le: 4,
                },
                MaNode::Split {
                    prop: 15,
                    val: 0,
                    gt: 2,
                    le: 3,
                },
                MaNode::Leaf { pred: 6 },
                MaNode::Leaf { pred: 1 },
                MaNode::Split {
                    prop: 3,
                    val: 3,
                    gt: 5,
                    le: 6,
                },
                MaNode::Leaf { pred: 5 },
                MaNode::Leaf { pred: 1 },
            ],
            est_bits: 0.,
            flat_bits: 0.,
        };
        let (_, contexts, _) = emit_learned_tree(&tree);
        for channel in [0, 1, 7, u32::MAX] {
            let lookup = MaLookup::new(&tree, &contexts, channel, 0);
            assert_eq!(lookup.needs_wp(), (channel as i32) > 0);
            for width in [0, 1, 2, 3, 4, 5, 9, 257] {
                for height in [0, 1, 2, 3, 17] {
                    let values: Vec<i32> = (0..width * height)
                        .map(|i| ((i * 7919 ^ (i / 7 * 1237)) % 65536) as i32 - 32768)
                        .collect();
                    let get_row = |y| &values[y * width..][..width];
                    for use_wp in [false, true] {
                        let mut expected = Vec::new();
                        walk_channel_ma(
                            get_row,
                            width,
                            height,
                            channel,
                            0,
                            &[],
                            WpParams::DEFAULT,
                            use_wp,
                            |_x, _y, value, props, n, wp| {
                                let (node, pred) = tree.lookup(props);
                                expected.push((
                                    contexts[node as usize],
                                    pack_signed((value - predictor_value(pred, n, wp)) as i32),
                                ));
                            },
                        );
                        let mut actual = RawTokens::with_capacity(values.len());
                        tokenize_channel_ma(
                            get_row,
                            width,
                            height,
                            channel,
                            0,
                            &[],
                            WpParams::DEFAULT,
                            use_wp,
                            &lookup,
                            &vec![0; contexts.len()],
                            &mut actual,
                        );
                        let actual: Vec<_> = match RawTokenStreams::new(vec![actual]) {
                            RawTokenStreams::Compact(streams) => streams
                                .into_iter()
                                .flatten()
                                .map(|t| {
                                    let t = t.unpack();
                                    (t.context, t.value)
                                })
                                .collect(),
                            RawTokenStreams::Wide(streams) => streams
                                .into_iter()
                                .flatten()
                                .map(|t| (t.context, t.value))
                                .collect(),
                        };
                        assert_eq!(
                            actual, expected,
                            "channel={channel}, width={width}, height={height}, wp={use_wp}"
                        );
                    }
                }
            }
        }
    }
}