pixon 0.8.0

SIMD-dispatched color conversion for the FFmpeg AVPixelFormat space: one-call decode of validated source frames to RGB / RGBA / Luma / HSV, fused resampling while converting, colorimetry-honest matrices and chroma siting, no_std-friendly.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
//! [`MixedSinker`] — the common "I want some subset of {RGB, Luma, HSV}
//! written into my own buffers" consumer.
//!
//! Generic over the source format via an `F: SourceFormat` type
//! parameter. One `PixelSink` impl per supported format. Currently
//! ships impls for:
//!
//! - **8‑bit planar**: [`Yuv411p`](crate::source::Yuv411p),
//!   [`Yuv420p`](crate::source::Yuv420p),
//!   [`Yuv422p`](crate::source::Yuv422p),
//!   [`Yuv440p`](crate::source::Yuv440p),
//!   [`Yuv444p`](crate::source::Yuv444p).
//! - **8‑bit semi‑planar**: [`Nv12`](crate::source::Nv12),
//!   [`Nv21`](crate::source::Nv21), [`Nv16`](crate::source::Nv16),
//!   [`Nv24`](crate::source::Nv24), [`Nv42`](crate::source::Nv42).
//! - **9/10/12/14/16‑bit planar 4:2:0**:
//!   [`Yuv420p9`](crate::source::Yuv420p9),
//!   [`Yuv420p10`](crate::source::Yuv420p10),
//!   [`Yuv420p12`](crate::source::Yuv420p12),
//!   [`Yuv420p14`](crate::source::Yuv420p14),
//!   [`Yuv420p16`](crate::source::Yuv420p16).
//! - **9/10/12/14/16‑bit planar 4:2:2**:
//!   [`Yuv422p9`](crate::source::Yuv422p9),
//!   [`Yuv422p10`](crate::source::Yuv422p10),
//!   [`Yuv422p12`](crate::source::Yuv422p12),
//!   [`Yuv422p14`](crate::source::Yuv422p14),
//!   [`Yuv422p16`](crate::source::Yuv422p16).
//! - **10/12‑bit planar 4:4:0**:
//!   [`Yuv440p10`](crate::source::Yuv440p10),
//!   [`Yuv440p12`](crate::source::Yuv440p12).
//! - **9/10/12/14/16‑bit planar 4:4:4**:
//!   [`Yuv444p9`](crate::source::Yuv444p9),
//!   [`Yuv444p10`](crate::source::Yuv444p10),
//!   [`Yuv444p12`](crate::source::Yuv444p12),
//!   [`Yuv444p14`](crate::source::Yuv444p14),
//!   [`Yuv444p16`](crate::source::Yuv444p16).
//! - **10/12/16‑bit semi‑planar high‑bit‑packed 4:2:0**:
//!   [`P010`](crate::source::P010), [`P012`](crate::source::P012),
//!   [`P016`](crate::source::P016).
//! - **10/12/16‑bit semi‑planar high‑bit‑packed 4:2:2**:
//!   [`P210`](crate::source::P210), [`P212`](crate::source::P212),
//!   [`P216`](crate::source::P216).
//! - **10/12/16‑bit semi‑planar high‑bit‑packed 4:4:4**:
//!   [`P410`](crate::source::P410), [`P412`](crate::source::P412),
//!   [`P416`](crate::source::P416).
//! - **YUVA (alpha-bearing planar)**: the entire FFmpeg-shipped
//!   YUVA family — `Yuva420p` / `Yuva420p9/10/16`, `Yuva422p` /
//!   `Yuva422p9/10/12/16`, `Yuva444p` / `Yuva444p9/10/12/14/16`.
//!   Source-side alpha pass-through to `with_rgba` /
//!   `with_rgba_u16`, with native SIMD on every backend.
//! - **8‑bit packed RGB sources** (Tier 6):
//!   [`Rgb24`](crate::source::Rgb24) (`R, G, B` bytes),
//!   [`Bgr24`](crate::source::Bgr24) (`B, G, R` bytes),
//!   [`Rgba`](crate::source::Rgba) (`R, G, B, A` bytes),
//!   [`Bgra`](crate::source::Bgra) (`B, G, R, A` bytes),
//!   [`Argb`](crate::source::Argb) (`A, R, G, B` bytes — leading alpha),
//!   [`Abgr`](crate::source::Abgr) (`A, B, G, R` bytes — leading alpha),
//!   [`Xrgb`](crate::source::Xrgb) / [`Rgbx`](crate::source::Rgbx) /
//!   [`Xbgr`](crate::source::Xbgr) / [`Bgrx`](crate::source::Bgrx)
//!   (4-byte packed RGB with one ignored padding byte at the leading
//!   or trailing position).
//!   The source row is already 8‑bit RGB at the byte level —
//!   `with_rgb` is an identity copy / channel swap /
//!   drop-alpha-or-padding, `with_rgba` is a memcpy / channel
//!   reorder (alpha passed through for the alpha-bearing 4-byte
//!   sources, forced to `0xFF` for the 3-byte sources and the
//!   padding-byte family), `with_luma` derives Y' from R/G/B,
//!   `with_hsv` reuses the existing kernel.
//! - **8‑bit planar GBR sources** (Tier 10):
//!   [`Gbrp`](crate::source::Gbrp) (three planes: G, B, R) and
//!   [`Gbrap`](crate::source::Gbrap) (four planes: G, B, R, A — real
//!   per-pixel α). Both reuse the standard `with_rgb` / `with_rgba` /
//!   `with_luma` / `with_luma_u16` / `with_hsv` channels via dedicated
//!   `gbr_to_rgb_row` / `gbra_to_rgba_row` / `gbr_to_rgba_opaque_row`
//!   SIMD kernels (no chroma matrix — the source is already component
//!   RGB). `Gbrap`'s `with_rgb + with_rgba` combo uses Strategy A+
//!   (expand RGB → RGBA, then α-overwrite from the source plane).
//! - **10‑bit packed RGB sources** (Tier 6 — Ship 9e):
//!   [`X2Rgb10`](crate::source::X2Rgb10) and
//!   [`X2Bgr10`](crate::source::X2Bgr10). Each pixel is a 32-bit LE word
//!   with `(MSB) 2X | 10c2 | 10c1 | 10c0 (LSB)` (R/G/B for X2RGB10,
//!   B/G/R for X2BGR10). Unlike the 8‑bit byte-shuffle family above,
//!   the source is **not** byte-aligned RGB — every output path
//!   starts with bit-level extraction of the three 10‑bit channels:
//!   `with_rgb` extracts and down-shifts each channel from 10→8 bits,
//!   `with_rgba` does the same and forces alpha to `0xFF` (the 2‑bit
//!   field is padding, not real alpha), `with_rgb_u16` preserves
//!   native 10‑bit precision (low-bit aligned in `u16`, value range
//!   `[0, 1023]`), and `with_luma` / `with_hsv` reuse the staged u8
//!   RGB scratch path.
//!
//! High‑bit‑depth source impls expose both `with_rgb` (u8 output) and
//! `with_rgb_u16` (native‑depth u16 output). Calling `with_rgb_u16` on
//! an 8‑bit source format is a compile error.
//!
//! All configuration and processing methods are fallible — no panics
//! under normal contract violations — so the sink is usable on
//! `panic = "abort"` targets.

use core::marker::PhantomData;

// `Vec<u8>` is only used by the `rgb_scratch` lazy scratch buffer
// (gated on the same 15-feature any as the per-format `process`
// impls). The import is left unconditional because gating it would
// also leave `extern crate alloc as std` unused under
// `--features "alloc"` alone, which is harder to express.
#[allow(unused_imports)]
use std::vec::Vec;

use derive_more::{Display, IsVariant, TryUnwrap, Unwrap};
use thiserror::Error;

use crate::{
  SourceFormat,
  resample::{NoopResampler, ResampleError, ResamplePlan, Resampler},
};
// The RFC #238 averaging-domain selector + caller-configurable transfer
// curve drive the `yuv-planar`-only linear-light path; the fields and
// builders are gated to that family.
#[cfg(feature = "yuv-planar")]
use crate::resample::{AveragingDomain, LinearMode, TransferFunction};
// PixelSink is referenced only via intra-doc links (`[`PixelSink::*`]`)
// in this file; the rustc lint can't see those uses, so silence it.
#[allow(unused_imports)]
use crate::PixelSink;

pub use mediaframe::{
  frame::{WidthAlignment, WidthAlignmentRequirement},
  source::{HsvFrame, HsvFrameMut, HsvPlane},
};

/// Whether `loc` places 4:2:0 chroma at the **horizontal center** between
/// its two luma columns — the MPEG-1 / JPEG phase that needs phase-0.5
/// reconstruction ([`chroma_upsample_2to1_center_h`](crate::row::scalar::chroma_upsample_2to1_center_h))
/// rather than pixon's default nearest-neighbor decode (#302).
///
/// The full [`ChromaLocation`](crate::ChromaLocation) → 4:2:0 sample-phase
/// map (FFmpeg `AVChromaLocation` semantics — horizontal phase in
/// luma-column units, `0` = co-sited with the left column, `0.5` = centered;
/// vertical phase `0` = top luma row, `0.5` = between rows, `1` = bottom):
///
/// | Variant            | Horizontal | Vertical |
/// |--------------------|------------|----------|
/// | `Unspecified`      | default    | default  |
/// | `Unknown`          | default    | default  |
/// | `Left`             | `0`        | `0.5`    |
/// | `Center`           | `0.5`      | `0.5`    |
/// | `TopLeft`          | `0`        | `0`      |
/// | `Top`              | `0.5`      | `0`      |
/// | `BottomLeft`       | `0`        | `1`      |
/// | `Bottom`           | `0.5`      | `1`      |
///
/// This predicate consumes only the **horizontal** axis: it returns `true`
/// for the centered group (`Center` / `Top` / `Bottom`) and `false` for the
/// co-sited group (`Left` / `TopLeft` / `BottomLeft`) and the
/// unspecified/unknown codes. The co-sited and unspecified sitings all keep
/// the byte-identical default path — pixon's existing nearest-neighbor
/// decode already places chroma at the left-column (phase-0) position, so
/// they need no new interpolation.
///
/// This predicate consumes only the **horizontal** axis; the **vertical** phase
/// is a separate concern (see [`chroma_420_bottom_sited_v`]) because mediaframe's
/// 4:2:0 walker streams the sink one chroma row per luma row
/// (`chroma_row = row / 2`), so a vertical blend needs an adjacent chroma row the
/// single-row `process` call does not directly hold.
#[cfg(feature = "yuv-planar")]
#[inline]
pub(super) const fn chroma_420_center_sited_h(loc: &crate::ChromaLocation) -> bool {
  matches!(
    loc,
    crate::ChromaLocation::Center | crate::ChromaLocation::Top | crate::ChromaLocation::Bottom
  )
}

/// Whether `loc` places 4:2:0 chroma at the **bottom** vertical phase (`v = 1`)
/// — the FFmpeg `AVCHROMA_LOC_BOTTOM` (`Bottom`, `h = 0.5`) and
/// `AVCHROMA_LOC_BOTTOMLEFT` (`BottomLeft`, `h = 0`) sitings, whose chroma is
/// co-sited with the **bottom** luma row of each vertical pair; the full
/// [`ChromaLocation`](crate::ChromaLocation) → sample-phase map is in
/// [`chroma_420_center_sited_h`]'s table. At `v = 1` the chroma sample sits on
/// the lower luma row, so:
///
/// - the **even** luma row `2i` lies halfway between chroma rows `i-1` and `i`,
///   and its reconstructed chroma is the vertical box average of those two rows,
///   fused with the siting's horizontal phase —
///   [`chroma_upsample_420_bottom_even_h`](crate::row::scalar::chroma_upsample_420_bottom_even_h)
///   for `Bottom`'s `h = 0.5` centered fold,
///   [`chroma_upsample_420_bottomleft_even_h`](crate::row::scalar::chroma_upsample_420_bottomleft_even_h)
///   for `BottomLeft`'s `h = 0` co-sited (nearest-neighbor) fold;
/// - the **odd** luma row `2i+1` is co-sited with chroma row `i`, so it needs no
///   vertical blend and keeps the siting's plain horizontal upsample.
///
/// The `v = 1` phase is the one vertical siting a top-to-bottom stream can
/// reconstruct with a **backward** one-row chroma lookback: the even row's
/// `c[i-1]` is the *previous* chroma row (already streamed), so the blend is
/// causal. The horizontal axis is independent — `Bottom` rides the centered
/// full-width staging the `h = 0.5` identity path already uses, `BottomLeft`
/// rides the co-sited (nearest-neighbor) full-width staging — so both fold the
/// same vertical box average and this predicate matches both (the 4:2:0 twin of
/// the wider [`chroma_440_bottom_sited_v`] match). The `v = 0` (`Top` /
/// `TopLeft`) and `v = 0.5` (`Center` / `Left`) phases instead need the *next*
/// chroma row for the odd output row — which the row-at-a-time `process` cannot
/// see without a forward delay line — so they keep their vertical-co-sited decode
/// (`Center` / `Top` still apply their horizontal phase via
/// [`chroma_420_center_sited_h`]). `Left` (the `yuv420p` default, `v = 0.5`) and
/// every unspecified siting keep the byte-identical vertical-replicate decode.
#[cfg(feature = "yuv-planar")]
#[inline]
pub(super) const fn chroma_420_bottom_sited_v(loc: &crate::ChromaLocation) -> bool {
  matches!(
    loc,
    crate::ChromaLocation::Bottom | crate::ChromaLocation::BottomLeft
  )
}

/// Whether `loc` places 4:2:0 chroma at the **top** vertical phase (`v = 0`) —
/// the FFmpeg `AVCHROMA_LOC_TOP` (`Top`, `h = 0.5`) and `AVCHROMA_LOC_TOPLEFT`
/// (`TopLeft`, `h = 0`) sitings, whose chroma is co-sited with the **top** luma
/// row of each vertical pair; the full
/// [`ChromaLocation`](crate::ChromaLocation) → sample-phase map is in
/// [`chroma_420_center_sited_h`]'s table. At `v = 0` the chroma sample sits on
/// the upper luma row, so:
///
/// - the **even** luma row `2i` is co-sited with chroma row `i`, so it needs no
///   vertical blend and keeps the siting's plain horizontal upsample;
/// - the **odd** luma row `2i+1` lies halfway between chroma rows `i` and `i+1`,
///   and its reconstructed chroma is the vertical box average of those two rows,
///   fused with the siting's horizontal phase.
///
/// `Top` is the vertical MIRROR of [`chroma_420_bottom_sited_v`]'s `Bottom`
/// (`v = 1`): where Bottom's even row reaches BACKWARD to the already-streamed
/// `c[i-1]`, Top's odd row reaches FORWARD to `c[i+1]` — the *next* chroma row,
/// which a top-to-bottom row-at-a-time stream has not been fed when the odd row
/// arrives. The 4:2:0 sink therefore reconstructs `Top` with a **forward
/// one-row output delay** (the mirror of Bottom's backward `chroma_prev`
/// lookback): the odd output row is held until the next chroma row arrives, then
/// box-blended and emitted — the same `(c[i] + c[i+1] + 1) >> 1` fold Bottom's
/// even row applies to `(c[i-1], c[i])`, reusing its fused-`h` kernels verbatim.
/// The horizontal axis is independent — `Top` rides the centered full-width
/// staging, `TopLeft` the co-sited staging — so both fold the same vertical box
/// average and this predicate matches both. The native / HSV-only binning tiers
/// instead fold the `v = 0` FORWARD triangle into `area_chroma_420`'s vertical
/// weights ([`AxisSpans::area_chroma_phased_v_top`](crate::resample)), needing
/// no delay because the [`AreaStream`](crate::resample) window absorbs the
/// forward reach. `Center` (`h = 0.5, v = 0.5`) / `Left` (the `yuv420p` default,
/// `v = 0.5`) and every unspecified siting keep the byte-identical
/// vertical-replicate decode.
#[cfg(feature = "yuv-planar")]
#[inline]
pub(super) const fn chroma_420_top_sited_v(loc: &crate::ChromaLocation) -> bool {
  matches!(
    loc,
    crate::ChromaLocation::Top | crate::ChromaLocation::TopLeft
  )
}

/// Whether `loc` places 4:4:0 chroma at the **bottom** vertical phase (`v = 1`)
/// — the one vertical siting a top-to-bottom stream can reconstruct with a
/// backward one-row chroma lookback (the even luma row's predecessor chroma row
/// is already streamed).
///
/// 4:4:0 subsamples chroma 2:1 **vertically only** (full-width chroma), so the
/// siting reduces to its vertical axis alone and there is NO horizontal phase to
/// stage. This is the 4:4:0 companion of the 4:2:0 [`chroma_420_bottom_sited_v`]
/// but with a WIDER match: both `Bottom` (`h = 0.5`, `v = 1`) AND `BottomLeft`
/// (`h = 0`, `v = 1`) are `v = 1`, and because 4:4:0 never reconstructs
/// horizontally their horizontal difference is irrelevant — both fold the
/// identical vertical box average. (4:2:0 excludes `BottomLeft` because there the
/// `h = 0` phase would need a co-sited full-width horizontal staging its
/// `h = 0.5` path lacks; 4:4:0 has no such constraint.) The `v = 0` (`Top` /
/// `TopLeft`) phase needs the *next* chroma row and is handled by its FORWARD
/// one-row-delay companion [`chroma_440_top_sited_v`]; the `v = 0.5` (`Center` /
/// `Left`) phase keeps the byte-identical vertical-replicate decode.
#[cfg(feature = "yuv-planar")]
#[inline]
pub(super) const fn chroma_440_bottom_sited_v(loc: &crate::ChromaLocation) -> bool {
  matches!(
    loc,
    crate::ChromaLocation::Bottom | crate::ChromaLocation::BottomLeft
  )
}

/// Whether `loc` places 4:4:0 chroma at the **top** vertical phase (`v = 0`) —
/// the FFmpeg `AVCHROMA_LOC_TOP` (`Top`) and `AVCHROMA_LOC_TOPLEFT` (`TopLeft`)
/// sitings, whose chroma is co-sited with the **top** luma row of each vertical
/// pair. The 4:4:0 companion of the 4:2:0 [`chroma_420_top_sited_v`] and the
/// vertical MIRROR of [`chroma_440_bottom_sited_v`]'s `Bottom` (`v = 1`): where
/// Bottom's even row reaches BACKWARD to the already-streamed `c[i-1]`, Top's
/// odd row reaches FORWARD to `c[i+1]` — the *next* chroma row, which a
/// top-to-bottom row-at-a-time stream has not been fed when the odd row arrives.
///
/// 4:4:0 subsamples chroma 2:1 **vertically only** (full-width chroma), so the
/// siting reduces to its vertical axis alone and there is NO horizontal phase to
/// stage. Both `Top` (`h = 0.5`, `v = 0`) AND `TopLeft` (`h = 0`, `v = 0`) are
/// `v = 0`, and because 4:4:0 never reconstructs horizontally their horizontal
/// difference is irrelevant — both fold the identical vertical box average, so
/// this predicate matches both (the wider match mirrors [`chroma_440_bottom_sited_v`]).
///
/// The 4:4:0 sink reconstructs `Top` with a **forward one-row output delay** (the
/// mirror of Bottom's backward `chroma_prev` lookback): an EVEN luma row `2i` is
/// co-sited with chroma row `i` (a plain co-sited decode), while an ODD luma row
/// `2i+1` needs the vertical box average of chroma rows `i` and `i+1` — so its
/// colour output is held until the following even row supplies `c[i+1]`, then
/// box-blended through the SAME `(c[i] + c[i+1]) / 2` fold a `Bottom` EVEN row
/// applies to `(c[i-1], c[i])` (reused verbatim at the even index). The native /
/// HSV-only binning tiers instead fold the `v = 0` FORWARD triangle into
/// `area_chroma_440`'s vertical weights
/// ([`AxisSpans::area_chroma_phased_v_top`](crate::resample)), needing no delay
/// because the [`AreaStream`](crate::resample) window absorbs the forward reach.
/// `Center` / `Left` (`v = 0.5`) and every unspecified siting keep the
/// byte-identical vertical-replicate decode.
#[cfg(feature = "yuv-planar")]
#[inline]
pub(super) const fn chroma_440_top_sited_v(loc: &crate::ChromaLocation) -> bool {
  matches!(
    loc,
    crate::ChromaLocation::Top | crate::ChromaLocation::TopLeft
  )
}

/// Whether `loc` places 4:2:2 chroma at the **horizontal center** between its
/// two luma columns — the MPEG-1 / JPEG horizontal phase that needs phase-0.5
/// reconstruction
/// ([`chroma_upsample_2to1_center_h`](crate::row::scalar::chroma_upsample_2to1_center_h),
/// the SAME 2:1 horizontal upsample 4:2:0 uses) rather than pixon's default
/// nearest-neighbor decode (#302).
///
/// 4:2:2 subsamples chroma 2:1 **horizontally only** — one chroma row per luma
/// row, no vertical subsampling — so the siting reduces to its horizontal axis
/// alone. This consumes exactly that axis: it returns `true` for the centered
/// group (`Center` / `Top` / `Bottom`, all `h = 0.5`) and `false` for the
/// co-sited group (`Left` / `TopLeft` / `BottomLeft`, all `h = 0`) and the
/// unspecified / unknown codes, which keep the byte-identical nearest-neighbor
/// decode. Unlike 4:2:0 there is no vertical companion predicate: for 4:2:2 the
/// vertical phase drives no chroma reconstruction (the per-axis sample-phase map
/// is in [`chroma_420_center_sited_h`]).
#[cfg(feature = "yuv-planar")]
#[inline]
pub(super) const fn chroma_422_center_sited_h(loc: &crate::ChromaLocation) -> bool {
  matches!(
    loc,
    crate::ChromaLocation::Center | crate::ChromaLocation::Top | crate::ChromaLocation::Bottom
  )
}

/// Whether `loc` places 4:1:1 chroma at the **horizontal center** of the four
/// luma columns it covers — the MPEG-1 / JPEG horizontal phase that needs the
/// `1→4` phase reconstruction
/// ([`chroma_upsample_4to1_center_h`](crate::row::scalar::chroma_upsample_4to1_center_h))
/// rather than pixon's default nearest-neighbor decode (#302).
///
/// 4:1:1 subsamples chroma 4:1 **horizontally only** — one chroma row per luma
/// row, no vertical subsampling — so the siting reduces to its horizontal axis
/// alone (the 4:1:1 analog of [`chroma_422_center_sited_h`]). This consumes
/// exactly that axis: it returns `true` for the centered group (`Center` / `Top`
/// / `Bottom`, all `h = 0.5` luma = `+1.5` luma from the left-co-sited column)
/// and `false` for the co-sited group (`Left` / `TopLeft` / `BottomLeft`) and
/// the unspecified / unknown codes, which keep the byte-identical nearest decode.
#[cfg(feature = "yuv-planar")]
#[inline]
pub(super) const fn chroma_411_center_sited_h(loc: &crate::ChromaLocation) -> bool {
  matches!(
    loc,
    crate::ChromaLocation::Center | crate::ChromaLocation::Top | crate::ChromaLocation::Bottom
  )
}

/// Whether `loc` places 4:1:0 chroma at the **horizontal center** of the four
/// luma columns it covers — the same `1→4` horizontal phase as 4:1:1
/// ([`chroma_411_center_sited_h`]); 4:1:0 additionally subsamples chroma 4:1
/// **vertically**, but that vertical axis drives no reconstruction here (this
/// consumes the horizontal axis alone, matching the horizontal-only siting this
/// PR wires). Returns `true` for the centered group (`Center` / `Top` /
/// `Bottom`) and `false` for the co-sited / unspecified sitings, which keep the
/// byte-identical nearest decode.
#[cfg(feature = "yuv-planar")]
#[inline]
pub(super) const fn chroma_410_center_sited_h(loc: &crate::ChromaLocation) -> bool {
  matches!(
    loc,
    crate::ChromaLocation::Center | crate::ChromaLocation::Top | crate::ChromaLocation::Bottom
  )
}

// L2 shared chroma-reconstruction stage (RFC #238): the element-generic
// centered-siting reconstruction the `Yuv422p` identity decode funnels through.
// Re-exported here alongside the siting predicates above so the per-format sink
// modules reach it on the same `super` path as `chroma_422_center_sited_h`.
#[cfg(feature = "yuv-planar")]
pub(super) use chroma_reconstruct::{Chroma411U8, ChromaU8, ChromaU16, reconstruct_chroma};

/// Frame dimensions handed to `begin_frame` don't match the sinker's
/// configured size.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DimensionMismatch {
  /// Width declared at sinker construction.
  configured_w: usize,
  /// Height declared at sinker construction.
  configured_h: usize,
  /// Width of the frame handed to the walker.
  frame_w: u32,
  /// Height of the frame handed to the walker.
  frame_h: u32,
}

impl DimensionMismatch {
  /// Constructs a new `DimensionMismatch` payload.
  #[inline]
  pub const fn new(configured_w: usize, configured_h: usize, frame_w: u32, frame_h: u32) -> Self {
    Self {
      configured_w,
      configured_h,
      frame_w,
      frame_h,
    }
  }

  /// Width declared at sinker construction.
  #[inline]
  pub const fn configured_w(&self) -> usize {
    self.configured_w
  }

  /// Height declared at sinker construction.
  #[inline]
  pub const fn configured_h(&self) -> usize {
    self.configured_h
  }

  /// Width of the frame handed to the walker.
  #[inline]
  pub const fn frame_w(&self) -> u32 {
    self.frame_w
  }

  /// Height of the frame handed to the walker.
  #[inline]
  pub const fn frame_h(&self) -> u32 {
    self.frame_h
  }
}

/// Generic "insufficient buffer" payload, shared across every
/// `MixedSinkerError::Insufficient*Buffer` variant. `expected` / `actual`
/// are expressed in the unit reported by each variant's Display
/// impl (`bytes` for the byte buffers, `elements` for the typed
/// `u16` / `f32` / `f16` buffers).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InsufficientBuffer {
  /// Minimum elements (bytes or typed elements) required.
  expected: usize,
  /// Elements supplied.
  actual: usize,
}

impl InsufficientBuffer {
  /// Constructs a new `InsufficientBuffer` payload.
  #[inline]
  pub const fn new(expected: usize, actual: usize) -> Self {
    Self { expected, actual }
  }

  /// Minimum elements (bytes or typed elements) required.
  #[inline]
  pub const fn expected(&self) -> usize {
    self.expected
  }

  /// Elements supplied.
  #[inline]
  pub const fn actual(&self) -> usize {
    self.actual
  }
}

/// HSV plane identification and size mismatch payload for
/// [`MixedSinkerError::InsufficientHsvPlane`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InsufficientHsvPlane {
  /// Which HSV plane was short (H, S, or V).
  which: HsvPlane,
  /// Minimum bytes required (`width x height`).
  expected: usize,
  /// Bytes supplied.
  actual: usize,
}

impl InsufficientHsvPlane {
  /// Constructs a new `InsufficientHsvPlane` payload.
  #[inline]
  pub const fn new(which: HsvPlane, expected: usize, actual: usize) -> Self {
    Self {
      which,
      expected,
      actual,
    }
  }

  /// Which HSV plane was short (H, S, or V).
  #[inline]
  pub const fn which(&self) -> HsvPlane {
    self.which
  }

  /// Minimum bytes required (`width x height`).
  #[inline]
  pub const fn expected(&self) -> usize {
    self.expected
  }

  /// Bytes supplied.
  #[inline]
  pub const fn actual(&self) -> usize {
    self.actual
  }
}

/// Frame-size overflow payload for [`MixedSinkerError::GeometryOverflow`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GeometryOverflow {
  /// Configured width.
  width: usize,
  /// Configured height.
  height: usize,
  /// Channel count the overflowing product was computed with.
  channels: usize,
}

impl GeometryOverflow {
  /// Constructs a new `GeometryOverflow` payload.
  #[inline]
  pub const fn new(width: usize, height: usize, channels: usize) -> Self {
    Self {
      width,
      height,
      channels,
    }
  }

  /// Configured width.
  #[inline]
  pub const fn width(&self) -> usize {
    self.width
  }

  /// Configured height.
  #[inline]
  pub const fn height(&self) -> usize {
    self.height
  }

  /// Channel count the overflowing product was computed with.
  #[inline]
  pub const fn channels(&self) -> usize {
    self.channels
  }
}

/// Row shape mismatch payload for [`MixedSinkerError::RowShapeMismatch`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RowShapeMismatch {
  /// Which slice mismatched. See [`RowSlice`] for variants.
  which: RowSlice,
  /// Row index reported by the offending row.
  row: usize,
  /// Expected slice length in elements of the slice's element type.
  expected: usize,
  /// Actual slice length in the same unit as `expected`.
  actual: usize,
}

impl RowShapeMismatch {
  /// Constructs a new `RowShapeMismatch` payload.
  #[inline]
  pub const fn new(which: RowSlice, row: usize, expected: usize, actual: usize) -> Self {
    Self {
      which,
      row,
      expected,
      actual,
    }
  }

  /// Which slice mismatched. See [`RowSlice`] for variants.
  #[inline]
  pub const fn which(&self) -> RowSlice {
    self.which
  }

  /// Row index reported by the offending row.
  #[inline]
  pub const fn row(&self) -> usize {
    self.row
  }

  /// Expected slice length in elements of the slice's element type.
  #[inline]
  pub const fn expected(&self) -> usize {
    self.expected
  }

  /// Actual slice length in the same unit as `expected`.
  #[inline]
  pub const fn actual(&self) -> usize {
    self.actual
  }
}

/// Row-index-out-of-range payload for [`MixedSinkerError::RowIndexOutOfRange`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RowIndexOutOfRange {
  /// Row index reported by the offending row.
  row: usize,
  /// Sink's configured height.
  configured_height: usize,
}

impl RowIndexOutOfRange {
  /// Constructs a new `RowIndexOutOfRange` payload.
  #[inline]
  pub const fn new(row: usize, configured_height: usize) -> Self {
    Self {
      row,
      configured_height,
    }
  }

  /// Row index reported by the offending row.
  #[inline]
  pub const fn row(&self) -> usize {
    self.row
  }

  /// Sink's configured height.
  #[inline]
  pub const fn configured_height(&self) -> usize {
    self.configured_height
  }
}

/// Snapshot of a resampled frame's complete output configuration:
/// presence plus attachment identity (data pointer and length) for
/// luma, luma_u16, rgb, rgba, rgb_u16, rgba_u16, the `rgb_f32` /
/// `rgba_f32` float-RGB(A) outputs, the `xyz_f32` linear-XYZ output,
/// the `rgb_f16` / `rgba_f16` half-float outputs, the `luma_f32`
/// float-luma output, and the three HSV planes. Equality is the
/// per-frame immutability check — in safe code a mid-frame `set_*`
/// necessarily supplies a different borrow, so an identity change is
/// exactly a reattachment. The `*_u16` / `rgb_f32` / `rgba_f32` /
/// `xyz_f32` / `*_f16` / `luma_f32` slots are `(0, 0)` for every format
/// that attaches no such output, so adding them leaves those formats'
/// snapshots unchanged.
#[cfg(any(
  feature = "yuv-planar",
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "xyz",
  feature = "bayer",
  feature = "mono",
  feature = "yuv-semi-planar",
  feature = "yuv-packed",
  feature = "yuv-444-packed",
  feature = "y2xx",
  feature = "v210",
  feature = "rgb-legacy"
))]
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct FrozenOutputs {
  idents: [(usize, usize); 15],
}

#[cfg(any(
  feature = "yuv-planar",
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "xyz",
  feature = "bayer",
  feature = "mono",
  feature = "yuv-semi-planar",
  feature = "yuv-packed",
  feature = "yuv-444-packed",
  feature = "y2xx",
  feature = "v210",
  feature = "rgb-legacy"
))]
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
impl FrozenOutputs {
  /// Identity of one attached buffer: `(data pointer, length)`, or
  /// `(0, 0)` for an absent output (a slice pointer is never null).
  fn ident<T>(buf: Option<&[T]>) -> (usize, usize) {
    buf.map_or((0, 0), |b| (b.as_ptr() as usize, b.len()))
  }

  /// Builds the snapshot from the currently attached outputs.
  #[allow(clippy::too_many_arguments)]
  pub(super) fn snapshot(
    luma: Option<&[u8]>,
    luma_u16: Option<&[u16]>,
    rgb: Option<&[u8]>,
    rgba: Option<&[u8]>,
    rgb_u16: Option<&[u16]>,
    rgba_u16: Option<&[u16]>,
    rgb_f32: Option<&[f32]>,
    rgba_f32: Option<&[f32]>,
    xyz_f32: Option<&[f32]>,
    rgb_f16: Option<&[half::f16]>,
    rgba_f16: Option<&[half::f16]>,
    hsv: Option<(&[u8], &[u8], &[u8])>,
    luma_f32: Option<&[f32]>,
  ) -> Self {
    let (h, s, v) = match hsv {
      Some((h, s, v)) => (
        Self::ident(Some(h)),
        Self::ident(Some(s)),
        Self::ident(Some(v)),
      ),
      None => ((0, 0), (0, 0), (0, 0)),
    };
    Self {
      idents: [
        Self::ident(luma),
        Self::ident(luma_u16),
        Self::ident(rgb),
        Self::ident(rgba),
        Self::ident(rgb_u16),
        Self::ident(rgba_u16),
        Self::ident(rgb_f32),
        Self::ident(rgba_f32),
        Self::ident(xyz_f32),
        Self::ident(rgb_f16),
        Self::ident(rgba_f16),
        h,
        s,
        v,
        Self::ident(luma_f32),
      ],
    }
  }
}

/// Compare-only resample preflight (RFC #238 L1): the no-output short-circuit,
/// out-of-sequence rejection, and mid-frame output-set-change rejection every
/// routed row-stage / filter resample arm runs before it allocates a stream —
/// but with **NO commit** (the output freeze is NOT stored). Each arm builds its
/// streams (and any centered-chroma scratch) into locals, then commits the
/// output-set freeze via [`frozen_outputs_check`] and inserts the streams only
/// AFTER every pre-feed allocation has succeeded — so a first-row allocation
/// failure leaves `resample_outputs` and the streams untouched, state-atomic and
/// retryable even with a changed output attachment (the same compare-before /
/// commit-after discipline the linear-light tail uses). It also gates the RFC
/// #238 centered filter / row-stage chroma reserve, so a recoverable reserve
/// failure likewise leaves `resample_outputs` `None`.
///
/// `expected` is supplied by the caller because the per-row sequence counter
/// lives on whichever concrete stream (`AreaStream` / `FilterStream` / the HSV
/// join's Y stream) is fed every row, keeping this helper free of any
/// stream-element type. `ControlFlow::Break` (no output attached) is turned into
/// `Ok(())` by the caller. Rejects mirror the committing path exactly, minus the
/// freeze:
/// [`ResampleOutputsChanged`](MixedSinkerError::ResampleOutputsChanged)
/// on a mid-frame output-set change,
/// [`OutOfSequenceRow`](crate::resample::OutOfSequenceRow) on a row-order
/// violation. Shared with the packed / planar routed paths (this is the
/// "shared transactional resample preflight" the RFC #238 tail-collapse migrates
/// every family onto), so it lives here beside [`frozen_outputs_check`] rather
/// than in the `yuv-planar`-gated `planar_resample` module — the gray / mono
/// 1-channel families run it without pulling in the planar machinery.
#[cfg(any(
  feature = "yuv-planar",
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "xyz",
  feature = "bayer",
  feature = "mono",
  feature = "yuv-semi-planar",
  feature = "yuv-packed",
  feature = "yuv-444-packed",
  feature = "y2xx",
  feature = "v210",
  feature = "rgb-legacy"
))]
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
#[allow(clippy::too_many_arguments)]
pub(super) fn resample_preflight_check_only(
  resample_outputs: &Option<FrozenOutputs>,
  luma: &Option<&mut [u8]>,
  luma_u16: &Option<&mut [u16]>,
  rgb: &Option<&mut [u8]>,
  rgba: &Option<&mut [u8]>,
  rgb_u16: &Option<&mut [u16]>,
  rgba_u16: &Option<&mut [u16]>,
  rgb_f32: &Option<&mut [f32]>,
  rgba_f32: &Option<&mut [f32]>,
  xyz_f32: &Option<&mut [f32]>,
  rgb_f16: &Option<&mut [half::f16]>,
  rgba_f16: &Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  luma_f32: &Option<&mut [f32]>,
  expected: Option<usize>,
  idx: usize,
) -> Result<core::ops::ControlFlow<()>, MixedSinkerError> {
  let Some(expected) = expected else {
    return Ok(core::ops::ControlFlow::Break(()));
  };
  // First row only: reject an out-of-sequence row BEFORE the output-set compare,
  // exactly mirroring the committing path's ordering (minus the commit). On a
  // LATER row the compare runs first, so a genuine mid-frame output attachment
  // change surfaces as ResampleOutputsChanged, never OutOfSequenceRow.
  if resample_outputs.is_none() && expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }
  // Mid-frame output-set change → reject WITHOUT committing (a later row only;
  // the first row's `None` stores no snapshot).
  if let Some(frozen) = resample_outputs {
    let snapshot = FrozenOutputs::snapshot(
      luma.as_deref(),
      luma_u16.as_deref(),
      rgb.as_deref(),
      rgba.as_deref(),
      rgb_u16.as_deref(),
      rgba_u16.as_deref(),
      rgb_f32.as_deref(),
      rgba_f32.as_deref(),
      xyz_f32.as_deref(),
      rgb_f16.as_deref(),
      rgba_f16.as_deref(),
      hsv.as_mut().map(|f| {
        let (h, s, v) = f.hsv();
        (&h[..], &s[..], &v[..])
      }),
      luma_f32.as_deref(),
    );
    if *frozen != snapshot {
      return Err(MixedSinkerError::ResampleOutputsChanged(
        ResampleOutputsChanged::new(idx),
      ));
    }
  }
  // Post-compare: reject an out-of-sequence later row (mirrors
  // the committing path's post-freeze sequence check).
  if expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }
  Ok(core::ops::ControlFlow::Continue(()))
}

/// Enforces the per-frame frozen output configuration for resampling
/// sinkers — presence AND buffer identity of every output the emit
/// closures consult. Shared by every routed format's resampled paths.
#[cfg(any(
  feature = "yuv-planar",
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "xyz",
  feature = "bayer",
  feature = "mono",
  feature = "yuv-semi-planar",
  feature = "yuv-packed",
  feature = "yuv-444-packed",
  feature = "y2xx",
  feature = "v210",
  feature = "rgb-legacy"
))]
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
#[allow(clippy::too_many_arguments)]
pub(super) fn frozen_outputs_check(
  resample_outputs: &mut Option<FrozenOutputs>,
  luma: &Option<&mut [u8]>,
  luma_u16: &Option<&mut [u16]>,
  rgb: &Option<&mut [u8]>,
  rgba: &Option<&mut [u8]>,
  rgb_u16: &Option<&mut [u16]>,
  rgba_u16: &Option<&mut [u16]>,
  rgb_f32: &Option<&mut [f32]>,
  rgba_f32: &Option<&mut [f32]>,
  xyz_f32: &Option<&mut [f32]>,
  rgb_f16: &Option<&mut [half::f16]>,
  rgba_f16: &Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  luma_f32: &Option<&mut [f32]>,
  idx: usize,
) -> Result<(), MixedSinkerError> {
  let snapshot = FrozenOutputs::snapshot(
    luma.as_deref(),
    luma_u16.as_deref(),
    rgb.as_deref(),
    rgba.as_deref(),
    rgb_u16.as_deref(),
    rgba_u16.as_deref(),
    rgb_f32.as_deref(),
    rgba_f32.as_deref(),
    xyz_f32.as_deref(),
    rgb_f16.as_deref(),
    rgba_f16.as_deref(),
    hsv.as_mut().map(|f| {
      let (h, s, v) = f.hsv();
      (&h[..], &s[..], &v[..])
    }),
    luma_f32.as_deref(),
  );
  match resample_outputs {
    None => *resample_outputs = Some(snapshot),
    Some(frozen) if *frozen != snapshot => {
      return Err(MixedSinkerError::ResampleOutputsChanged(
        ResampleOutputsChanged::new(idx),
      ));
    }
    Some(_) => {}
  }
  Ok(())
}

/// Mid-frame output-set change payload for
/// [`MixedSinkerError::ResampleOutputsChanged`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResampleOutputsChanged {
  /// Source row whose `process` call observed the changed output set.
  row: usize,
}

impl ResampleOutputsChanged {
  /// Constructs a new `ResampleOutputsChanged` payload.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(row: usize) -> Self {
    Self { row }
  }

  /// Source row whose `process` call observed the changed output set.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn row(&self) -> usize {
    self.row
  }
}

/// Mid-frame native-vs-row-stage route change payload for
/// [`MixedSinkerError::NativeRouteChanged`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NativeRouteChanged {
  /// Source row whose `process` call observed the changed route.
  row: usize,
}

impl NativeRouteChanged {
  /// Constructs a new `NativeRouteChanged` payload.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(row: usize) -> Self {
    Self { row }
  }

  /// Source row whose `process` call observed the changed route.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn row(&self) -> usize {
    self.row
  }
}

/// Payload for [`MixedSinkerError::UnsupportedMatrixResample`]: a non-affine
/// colour matrix was paired with a resampling (non-identity) plan, which the
/// resample tier does not (yet) decode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UnsupportedMatrixResample {
  /// Source row whose `process` call hit the unsupported combination.
  row: usize,
  /// Human-readable name of the offending non-affine matrix
  /// (`"ChromaDerivedCl"` / `"Ictcp"` / `"IptC2"` / `"Smpte2085"`).
  matrix: &'static str,
}

impl UnsupportedMatrixResample {
  /// Constructs a new `UnsupportedMatrixResample` payload.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(row: usize, matrix: &'static str) -> Self {
    Self { row, matrix }
  }

  /// Source row whose `process` call hit the unsupported combination.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn row(&self) -> usize {
    self.row
  }

  /// Name of the offending non-affine matrix.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn matrix(&self) -> &'static str {
    self.matrix
  }
}

/// Mid-frame [`AveragingDomain::Linear`] transfer-function change payload
/// for [`MixedSinkerError::TransferFunctionChanged`].
///
/// [`AveragingDomain::Linear`]: crate::resample::AveragingDomain::Linear
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TransferFunctionChanged {
  /// Source row whose `process` call observed the changed transfer function.
  row: usize,
}

impl TransferFunctionChanged {
  /// Constructs a new `TransferFunctionChanged` payload.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(row: usize) -> Self {
    Self { row }
  }

  /// Source row whose `process` call observed the changed transfer function.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn row(&self) -> usize {
    self.row
  }
}

/// Mid-frame [`AveragingDomain::Linear`] [`LinearMode`] change payload for
/// [`MixedSinkerError::LinearModeChanged`].
///
/// [`AveragingDomain::Linear`]: crate::resample::AveragingDomain::Linear
/// [`LinearMode`]: crate::resample::LinearMode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LinearModeChanged {
  /// Source row whose `process` call observed the changed linear mode.
  row: usize,
}

impl LinearModeChanged {
  /// Constructs a new `LinearModeChanged` payload.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(row: usize) -> Self {
    Self { row }
  }

  /// Source row whose `process` call observed the changed linear mode.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn row(&self) -> usize {
    self.row
  }
}

/// Mid-frame [`AveragingDomain`] change payload for
/// [`MixedSinkerError::AveragingDomainChanged`].
///
/// [`AveragingDomain`]: crate::resample::AveragingDomain
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AveragingDomainChanged {
  /// Source row whose `process` call observed the changed averaging domain.
  row: usize,
}

impl AveragingDomainChanged {
  /// Constructs a new `AveragingDomainChanged` payload.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(row: usize) -> Self {
    Self { row }
  }

  /// Source row whose `process` call observed the changed averaging domain.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn row(&self) -> usize {
    self.row
  }
}

/// Mid-frame 4:2:2 chroma-siting (sampling-phase) change payload for
/// [`MixedSinkerError::ChromaSitingChanged`].
///
/// RFC #238 routes chroma siting through the resample as a sampling
/// phase frozen on the first output-bearing row; a later row that
/// observes a different siting would bin a mixture of phases, so it is
/// rejected with this recoverable error (the caller restarts the frame
/// via `begin_frame` to change siting).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChromaSitingChanged {
  /// Source row whose `process` call observed the changed chroma siting.
  row: usize,
}

impl ChromaSitingChanged {
  /// Constructs a new `ChromaSitingChanged` payload.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(row: usize) -> Self {
    Self { row }
  }

  /// Source row whose `process` call observed the changed chroma siting.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn row(&self) -> usize {
    self.row
  }
}

/// Errors returned by [`MixedSinker`] configuration and per-frame
/// preflight.
///
/// All variants are recoverable: the sinker never mutates caller
/// buffers on an error return, so the caller can inspect the variant,
/// rebuild or resize buffers, and retry.
///
/// **Note (API change):** the former `*BufferTooShort` / `HsvPlaneTooShort`
/// variants were renamed to `Insufficient*Buffer` / `InsufficientHsvPlane`
/// and their payload structs renamed from `BufferTooShort` /
/// `HsvPlaneTooShort` to `InsufficientBuffer` / `InsufficientHsvPlane`.
// No `Eq`: the wrapped `ResampleError` carries an `f64` (the filter
// kernel support in `InvalidFilterSupport`), so it is `PartialEq` only.
// Nothing requires `Eq` on this type.
#[derive(Debug, Clone, Copy, PartialEq, IsVariant, TryUnwrap, Unwrap, Error)]
#[non_exhaustive]
pub enum MixedSinkerError {
  /// The frame handed to the walker does not match the dimensions
  /// declared at [`MixedSinker::new`]. Returned from
  /// [`PixelSink::begin_frame`] before any row is processed.
  #[error(
    "MixedSinker frame dimensions mismatch: configured {}x{} but got {}x{}",
    .0.configured_w(), .0.configured_h(), .0.frame_w(), .0.frame_h()
  )]
  DimensionMismatch(DimensionMismatch),

  /// RGB buffer attached via [`MixedSinker::with_rgb`] /
  /// [`MixedSinker::set_rgb`] is shorter than `width x height x 3`.
  #[error("MixedSinker insufficient rgb buffer: expected >= {} bytes, got {}", .0.expected(), .0.actual())]
  InsufficientRgbBuffer(InsufficientBuffer),

  /// `u16` RGB buffer attached via [`MixedSinker::with_rgb_u16`] /
  /// [`MixedSinker::set_rgb_u16`] is shorter than `width x height x 3`
  /// `u16` elements. Only the high‑bit‑depth source impls
  /// (currently [`Yuv420p10`](crate::source::Yuv420p10)) write into this
  /// buffer.
  #[error("MixedSinker insufficient rgb_u16 buffer: expected >= {} elements, got {}", .0.expected(), .0.actual())]
  InsufficientRgbU16Buffer(InsufficientBuffer),

  /// Native-depth `u16` luma buffer attached via per-format
  /// `with_luma_u16` is shorter than `width x height` `u16`
  /// elements. Tier 4 sources (V210 / Y210 / Y212 / Y216) are the
  /// first consumers of this API.
  #[error("MixedSinker insufficient luma_u16 buffer: expected >= {} elements, got {}", .0.expected(), .0.actual())]
  InsufficientLumaU16Buffer(InsufficientBuffer),

  /// RGBA buffer attached via [`MixedSinker::with_rgba`] /
  /// [`MixedSinker::set_rgba`] is shorter than `width x height x 4`.
  /// The fourth byte per pixel is alpha — opaque (`0xFF`) by default
  /// when the source has no alpha plane.
  #[error("MixedSinker insufficient rgba buffer: expected >= {} bytes, got {}", .0.expected(), .0.actual())]
  InsufficientRgbaBuffer(InsufficientBuffer),

  /// `u16` RGBA buffer attached via `with_rgba_u16` / `set_rgba_u16`
  /// (per-format impl, not yet shipped on any sink) is shorter than
  /// `width x height x 4` `u16` elements. Only high‑bit‑depth source
  /// impls write into this buffer; the fourth `u16` per pixel is
  /// alpha — opaque (`(1 << BITS) - 1`) by default when the source
  /// has no alpha plane.
  #[error("MixedSinker insufficient rgba_u16 buffer: expected >= {} elements, got {}", .0.expected(), .0.actual())]
  InsufficientRgbaU16Buffer(InsufficientBuffer),

  /// `f32` RGB buffer attached via per-format `with_rgb_f32` /
  /// `set_rgb_f32` is shorter than `width x height x 3` `f32` elements.
  /// Only float-source impls (currently
  /// [`Rgbf32`](crate::source::Rgbf32)) write into this buffer.
  #[error("MixedSinker insufficient rgb_f32 buffer: expected >= {} elements, got {}", .0.expected(), .0.actual())]
  InsufficientRgbF32Buffer(InsufficientBuffer),

  /// `half::f16` RGB buffer attached via per-format `with_rgb_f16` /
  /// `set_rgb_f16` is shorter than `width x height x 3` `f16` elements.
  /// Only half-float-source impls (currently
  /// [`Rgbf16`](crate::source::Rgbf16)) write into this buffer.
  #[error("MixedSinker insufficient rgb_f16 buffer: expected >= {} elements, got {}", .0.expected(), .0.actual())]
  InsufficientRgbF16Buffer(InsufficientBuffer),

  /// `f32` RGBA buffer attached via per-format `with_rgba_f32` /
  /// `set_rgba_f32` is shorter than `width x height x 4` `f32` elements.
  /// Only float-planar-GBR source impls write into this buffer.
  #[error("MixedSinker insufficient rgba_f32 buffer: expected >= {} elements, got {}", .0.expected(), .0.actual())]
  InsufficientRgbaF32Buffer(InsufficientBuffer),

  /// `half::f16` RGBA buffer attached via per-format `with_rgba_f16` /
  /// `set_rgba_f16` is shorter than `width x height x 4` `f16` elements.
  /// Only float-planar-GBR source impls write into this buffer.
  #[error("MixedSinker insufficient rgba_f16 buffer: expected >= {} elements, got {}", .0.expected(), .0.actual())]
  InsufficientRgbaF16Buffer(InsufficientBuffer),

  /// `f32` XYZ buffer attached via `with_xyz_f32` / `set_xyz_f32` is
  /// shorter than `width x height x 3` `f32` elements. Only the
  /// [`Xyz12`](crate::source::Xyz12) source impl writes into this buffer.
  #[error("MixedSinker insufficient xyz_f32 buffer: expected >= {} elements, got {}", .0.expected(), .0.actual())]
  InsufficientXyzF32Buffer(InsufficientBuffer),

  /// `f32` luma buffer attached via `with_luma_f32` / `set_luma_f32` is
  /// shorter than `width x height` `f32` elements.
  #[error("MixedSinker insufficient luma_f32 buffer: expected >= {} elements, got {}", .0.expected(), .0.actual())]
  InsufficientLumaF32Buffer(InsufficientBuffer),

  /// Luma buffer is shorter than `width x height`.
  #[error("MixedSinker insufficient luma buffer: expected >= {} bytes, got {}", .0.expected(), .0.actual())]
  InsufficientLumaBuffer(InsufficientBuffer),

  /// One of the three HSV planes is shorter than `width x height`.
  #[error("MixedSinker insufficient hsv {:?} plane: expected >= {} bytes, got {}", .0.which(), .0.expected(), .0.actual())]
  InsufficientHsvPlane(InsufficientHsvPlane),

  /// Declared frame geometry does not fit in `usize`. Only reachable
  /// on 32‑bit targets (wasm32, i686) with extreme dimensions.
  #[error("MixedSinker frame size overflows usize: {} x {} x channels={}", .0.width(), .0.height(), .0.channels())]
  GeometryOverflow(GeometryOverflow),

  /// A row handed directly to [`PixelSink::process`] has a slice
  /// length that doesn't match the sink's configured width. Returned
  /// by `process` as a defense-in-depth check — [`PixelSink::begin_frame`]
  /// already validates frame-level dimensions, but this catches
  /// direct `process` callers that bypass the walker (hand-crafted
  /// rows, replayed rows, etc.) before a wrong-shaped slice reaches
  /// an unsafe SIMD kernel.
  ///
  /// Lengths are expressed in **slice elements** — `u8` bytes for
  /// the 8‑bit source rows (Y, U/V half, UV/VU half) and `u16`
  /// elements for the 10‑bit source rows (Y10, U/V half 10). The
  /// message deliberately says "elements" rather than "bytes" so the
  /// same variant can serve both the `u8` and `u16` row families.
  #[error(
    "MixedSinker row shape mismatch at row {}: {} slice has {} elements, expected {}",
    .0.row(), .0.which(), .0.actual(), .0.expected()
  )]
  RowShapeMismatch(RowShapeMismatch),

  /// A row handed to [`PixelSink::process`] has `row.row() >=
  /// configured_height`. The walker bounds `idx < height` via its
  /// `for row in 0..h` loop combined with the `begin_frame`
  /// dimension check, but a direct caller could pass any value.
  /// Returning an error instead of slice-indexing past the end keeps
  /// the no-panic contract intact.
  #[error(
    "MixedSinker row index {} is out of range for configured height {}",
    .0.row(), .0.configured_height()
  )]
  RowIndexOutOfRange(RowIndexOutOfRange),

  /// The sinker's configured `width` violates the format's chroma-group
  /// stride requirement. For 4:2:0 / 4:2:2 formats the width must be
  /// even (`WidthAlignmentRequirement::Even`); for planar 4:1:0
  /// ([`Yuv410p`](crate::source::Yuv410p)) and packed 4:1:1
  /// ([`Uyyvyy411`](crate::source::Uyyvyy411)) the width must be a
  /// multiple of 4 (`WidthAlignmentRequirement::MultipleOfFour`).
  /// Planar 4:1:1 ([`Yuv411p`](crate::source::Yuv411p)) accepts
  /// non-4-aligned widths via `width.div_ceil(4)` and does not produce
  /// this error. Supersedes the former `OddWidth` (even-only) and
  /// `WidthNotMultipleOf4` variants.
  ///
  /// `MixedSinker::new` is infallible and accepts any width, so this error
  /// surfaces the misconfiguration at the first use site
  /// ([`PixelSink::begin_frame`] or [`PixelSink::process`]) before any row
  /// primitive is invoked, preserving the no-panic contract.
  #[error("MixedSinker configured width {} {}", .0.width(), .0.required())]
  WidthAlignment(WidthAlignment),

  /// Building the resampling plan failed in
  /// [`MixedSinker::with_resampler`]: the strategy rejected the
  /// requested output geometry. Surfaces before the sinker exists, so
  /// no buffer state is touched.
  #[error(transparent)]
  Resample(#[from] ResampleError),

  /// On a resampling sinker the attached-output configuration is
  /// frozen per frame: streams carry frame progress, so an output
  /// attached, detached, or **replaced with a different buffer**
  /// after the first processed row would silently miss (or split)
  /// the rows already finalized. The offending `process` call fails
  /// before any stream mutates caller output; re-attach and call
  /// [`PixelSink::begin_frame`] to restart the frame with the new
  /// configuration. The direct (identity) path is unaffected.
  #[error(
    "MixedSinker resampled output set changed mid-frame at source row {}; \
     restart the frame via begin_frame",
    .0.row()
  )]
  ResampleOutputsChanged(ResampleOutputsChanged),

  /// On a resampling sinker whose source format carries the native
  /// decimation tier (the high-bit semi-planar `P010` / `P012` /
  /// `P016` family), the native-vs-row-stage route is frozen per frame
  /// alongside the output set: the route is chosen on the first
  /// resampled row, and the two tiers carry independent, in-order
  /// stream state. A caller manually driving `process()` that flips
  /// [`MixedSinker::set_native`] mid-frame would feed later rows through
  /// the *other* tier's fresh streams, splitting one frame across two
  /// incompatible state machines. The offending `process` call fails
  /// before either tier consumes the row; re-select the tier and call
  /// [`PixelSink::begin_frame`] to restart the frame. Enforced by every
  /// native 4:2:0 family: the 8-bit / high-bit planar (Yuv420p /
  /// Yuv420p10/12/14/16) and 8-bit / high-bit semi-planar (Nv12 / Nv21 /
  /// P010/P012/P016) tiers.
  #[error(
    "MixedSinker resample route (native vs row-stage) changed mid-frame at \
     source row {}; restart the frame via begin_frame",
    .0.row()
  )]
  NativeRouteChanged(NativeRouteChanged),

  /// The [`AveragingDomain::Linear`] transfer function changed mid-frame.
  ///
  /// The linear-light tail buffers each source row linearised under the
  /// transfer function resolved on the first output-bearing row; a later
  /// row resolving a different transfer (a caller flipping
  /// [`MixedSinker::with_transfer_function`] or the source `KernelMatrix`
  /// mid-frame) would bin rows linearised under inconsistent curves. The
  /// offending `process` call fails before the row is consumed; restore the
  /// transfer and call [`PixelSink::begin_frame`] to restart the frame.
  ///
  /// [`AveragingDomain::Linear`]: crate::resample::AveragingDomain::Linear
  #[error(
    "MixedSinker Linear transfer function changed mid-frame at source row \
     {}; restart the frame via begin_frame",
    .0.row()
  )]
  TransferFunctionChanged(TransferFunctionChanged),

  /// The [`AveragingDomain::Linear`] [`LinearMode`] changed mid-frame.
  ///
  /// The linear-light tail picks the `YUV→RGB` decode — display-referred
  /// (Q15-clamped) or scene-referred (unclamped `f32`) — from the
  /// [`LinearMode`] resolved on the first output-bearing row and buffers every
  /// source row decoded under it; a later row resolving a different mode (a
  /// caller flipping [`MixedSinker::with_linear_mode`] / [`MixedSinker::set_linear_mode`]
  /// mid-frame) would bin rows decoded under inconsistent referents, mixing
  /// display- and scene-decoded rows in one frame. The offending `process` call
  /// fails before the row is consumed; restore the mode and call
  /// [`PixelSink::begin_frame`] to restart the frame.
  ///
  /// [`AveragingDomain::Linear`]: crate::resample::AveragingDomain::Linear
  /// [`LinearMode`]: crate::resample::LinearMode
  #[error(
    "MixedSinker Linear mode changed mid-frame at source row \
     {}; restart the frame via begin_frame",
    .0.row()
  )]
  LinearModeChanged(LinearModeChanged),

  /// The [`AveragingDomain`] chosen on the first output-bearing resampled row
  /// is frozen for the frame, alongside the output set, the native-vs-row-stage
  /// route, and (for [`AveragingDomain::Linear`]) the transfer function and the
  /// [`LinearMode`]: the domain selects the pipeline stage the area average is
  /// spliced at, so a
  /// caller flipping [`MixedSinker::set_averaging_domain`] mid-frame (between
  /// `process` rows, without [`PixelSink::begin_frame`]) would split one frame
  /// across two incompatible averaging stages. The offending `process` call
  /// fails before any stream consumes the row or writes caller output; restore
  /// the domain and call [`PixelSink::begin_frame`] to restart the frame. The
  /// freeze is enforced by the planar 8-bit YUV family (Yuv420p / Yuv422p /
  /// Yuv444p / Yuv440p), the formats whose dispatch reads the configured domain.
  ///
  /// [`AveragingDomain`]: crate::resample::AveragingDomain
  /// [`AveragingDomain::Linear`]: crate::resample::AveragingDomain::Linear
  #[error(
    "MixedSinker averaging domain changed mid-frame at source row \
     {}; restart the frame via begin_frame",
    .0.row()
  )]
  AveragingDomainChanged(AveragingDomainChanged),

  /// The 4:2:2 chroma siting (sampling phase) changed mid-frame. RFC #238
  /// freezes the effective siting on the first output-bearing row of a
  /// frame so every row bins the same phase; restart the frame via
  /// begin_frame to change siting.
  #[error(
    "MixedSinker chroma siting changed mid-frame at source row \
     {}; restart the frame via begin_frame",
    .0.row()
  )]
  ChromaSitingChanged(ChromaSitingChanged),

  /// A non-affine colour matrix was paired with a resampling (non-identity)
  /// plan, which the resample tier cannot decode.
  ///
  /// The constant-luminance [`ColorMatrix::ChromaDerivedCl`] (BT.2020
  /// `YcCbcCrc`), [`ColorMatrix::Ictcp`] (BT.2100), [`ColorMatrix::IptC2`]
  /// (IPT-C2 / Dolby Vision Profile 5), and [`ColorMatrix::Smpte2085`]
  /// (SMPTE ST 2085 "Y'D'zD'x" X'Y'Z') decodes are non-affine: they
  /// reconstruct RGB through a per-channel non-linear transfer, not a single
  /// Q15 matrix, and are wired only on the **identity** (no-resample) path. The
  /// resample tier averages in the encoded-code / native-code domain and
  /// decodes through the *affine* `yuv444p*_to_rgb*` kernels, which would
  /// silently produce wrong, route-dependent colour for these matrices (the
  /// averaged result is not the constant-luminance / ICtCp / IPT-C2 / SMPTE
  /// 2085 decode).
  /// Rather than emit corrupt output, the offending `process` call fails before
  /// any stream consumes the row. Decode these matrices on an identity sink (no
  /// resample plan), or downscale after the colour convert. Full non-affine
  /// resample support is tracked as follow-up.
  ///
  /// Only raised when the matrix actually **resolves** to its non-affine
  /// decode (BT.2020 primaries for `ChromaDerivedCl`, a PQ/HLG transfer for
  /// `Ictcp`, a PQ transfer for `IptC2` / `Smpte2085`); an unresolved tag
  /// already falls back to the affine path on the identity route, so it
  /// resamples affinely without error, unchanged.
  ///
  /// [`ColorMatrix::ChromaDerivedCl`]: crate::ColorMatrix::ChromaDerivedCl
  /// [`ColorMatrix::Ictcp`]: crate::ColorMatrix::Ictcp
  /// [`ColorMatrix::IptC2`]: crate::ColorMatrix::IptC2
  /// [`ColorMatrix::Smpte2085`]: crate::ColorMatrix::Smpte2085
  #[error(
    "MixedSinker cannot resample the non-affine {} matrix at source row {}; \
     decode on an identity sink or downscale after the colour convert",
    .0.matrix(),
    .0.row()
  )]
  UnsupportedMatrixResample(UnsupportedMatrixResample),

  /// A [`KernelMatrix::ChromaDerivedNcl`](crate::KernelMatrix::ChromaDerivedNcl)
  /// decode was requested over [`Primaries::SmpteSt428`](crate::Primaries::SmpteSt428)
  /// while the sink is in [`St428Interpretation::CieXyz`] mode (#310).
  ///
  /// ST 428-1 is CIE XYZ, so a YCbCr `ChromaDerivedNcl` matrix derived from
  /// its tabulated primaries is meaningless; the default
  /// [`St428Interpretation::FfmpegTabulated`] never raises this. See
  /// [`MixedSinker::with_st428_interpretation`].
  #[cfg(feature = "yuv-planar")]
  #[error(transparent)]
  St428CieXyzUnsupported(St428CieXyzUnsupported),

  /// The [`ColorSpec`](crate::ColorSpec) handed to [`Convert`](crate::Convert)
  /// names a colour matrix pixon implements no decode for.
  ///
  /// Raised at the door, before a single row is read — see
  /// [`ColorSpec::kernel_matrix`](crate::ColorSpec::kernel_matrix) for the
  /// exact table of what is accepted and what is refused. Through 0.1 these
  /// matrices decoded as BT.709 and returned a wrong picture with no
  /// diagnostic.
  #[error(transparent)]
  UnsupportedColorMatrix(#[from] crate::UnsupportedKernelMatrixError),

  /// The [`DcpTargetGamut`](crate::DcpTargetGamut) handed to
  /// [`Convert::target_gamut`](crate::Convert::target_gamut) names no
  /// tabulated XYZ → RGB matrix.
  ///
  /// Raised at the door, before a single row is read — an unknown gamut has
  /// no luma basis, so it must never be silently converted as if it were
  /// DCI-P3.
  #[cfg(feature = "xyz")]
  #[error(transparent)]
  UnsupportedTargetGamut(#[from] crate::UnsupportedKernelGamutError),
}

/// Identifies which slice of a multi‑plane source row mismatched in
/// [`MixedSinkerError::RowShapeMismatch`].
///
/// `#[non_exhaustive]` because each new source format the crate grows
/// support for — YUV422p / YUV444p (full‑width chroma), P010 / P016
/// (10/16‑bit planes), etc. — will add its own variant. Pattern
/// matches from downstream code should include a `_ => …` arm.
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, Hash, IsVariant)]
#[non_exhaustive]
pub enum RowSlice {
  /// Y (luma) plane — every 4:2:0 / 4:2:2 / 4:4:4 source.
  #[display("Y")]
  Y,
  /// Half‑width U (Cb) plane in a planar 4:2:0 source ([`Yuv420p`]).
  #[display("U Half")]
  UHalf,
  /// Half‑width V (Cr) plane in a planar 4:2:0 source ([`Yuv420p`]).
  #[display("V Half")]
  VHalf,
  /// Quarter‑width U (Cb) plane in a planar 4:1:1 / 4:1:0 source
  /// ([`Yuv411p`](crate::source::Yuv411p) — DV-NTSC legacy;
  /// [`Yuv410p`](crate::source::Yuv410p) — Cinepak / extreme-old codecs).
  /// `width.div_ceil(4)` bytes per row — each chroma sample covers
  /// four Y columns horizontally. Yuv410p enforces `width % 4 == 0`
  /// at the frame layer (so `width.div_ceil(4) == width / 4`); Yuv411p
  /// accepts arbitrary widths via FFmpeg ceiling chroma. In 4:1:0 the
  /// same chroma row also covers four consecutive Y rows vertically;
  /// in 4:1:1 chroma is full-height.
  #[display("U Quarter")]
  UQuarter,
  /// Quarter‑width V (Cr) plane in a planar 4:1:1 / 4:1:0 source
  /// ([`Yuv411p`](crate::source::Yuv411p) /
  /// [`Yuv410p`](crate::source::Yuv410p)). `width.div_ceil(4)` bytes per
  /// row (see [`Self::UQuarter`] for the Yuv410p-vs-Yuv411p
  /// width-rounding distinction).
  #[display("V Quarter")]
  VQuarter,
  /// Half‑width interleaved UV plane in a semi‑planar 4:2:0 source
  /// ([`Nv12`]). Each row is `U0, V0, U1, V1, …` for `width / 2` pairs.
  #[display("UV Half")]
  UvHalf,
  /// Half‑width interleaved VU plane in a semi‑planar 4:2:0 source
  /// ([`Nv21`]). Each row is `V0, U0, V1, U1, …` for `width / 2`
  /// pairs — byte order swapped relative to [`Self::UvHalf`].
  #[display("VU Half")]
  VuHalf,
  /// Full-width U (Cb) plane in a planar 4:4:4 source
  /// ([`Yuv444p`](crate::source::Yuv444p)). `width` bytes per row.
  #[display("U Full")]
  UFull,
  /// Full-width V (Cr) plane in a planar 4:4:4 source
  /// ([`Yuv444p`](crate::source::Yuv444p)). `width` bytes per row.
  #[display("V Full")]
  VFull,
  /// Full-width alpha plane in an 8‑bit YUVA source
  /// ([`Yuva420p`](crate::source::Yuva420p)). `width` bytes per row
  /// (1:1 with Y).
  #[display("A Full")]
  AFull,
  /// Full-width U row of a **10-bit** 4:4:4 planar source. `u16`
  /// samples, `width` elements.
  #[display("U Full 10")]
  UFull10,
  /// Full-width V row of a **10-bit** 4:4:4 planar source.
  #[display("V Full 10")]
  VFull10,
  /// Full-width alpha row of a **10-bit** 4:4:4 planar source with an
  /// alpha plane ([`Yuva444p10`](crate::source::Yuva444p10)). `u16`
  /// samples, `width` elements, low-bit-packed.
  #[display("A Full 10")]
  AFull10,
  /// Full-width U row of a **12-bit** 4:4:4 planar source.
  #[display("U Full 12")]
  UFull12,
  /// Full-width V row of a **12-bit** 4:4:4 planar source.
  #[display("V Full 12")]
  VFull12,
  /// Full-width alpha row of a **12-bit** YUVA planar source
  /// ([`Yuva422p12`](crate::source::Yuva422p12) /
  /// [`Yuva444p12`](crate::source::Yuva444p12)). `u16` samples, `width`
  /// elements, low-bit-packed.
  #[display("A Full 12")]
  AFull12,
  /// Full-width U row of a **14-bit** 4:4:4 planar source.
  #[display("U Full 14")]
  UFull14,
  /// Full-width V row of a **14-bit** 4:4:4 planar source.
  #[display("V Full 14")]
  VFull14,
  /// Full-width alpha row of a **14-bit** YUVA planar source
  /// ([`Yuva444p14`](crate::source::Yuva444p14)). `u16` samples, `width`
  /// elements, low-bit-packed.
  #[display("A Full 14")]
  AFull14,
  /// Full‑width interleaved UV plane in a semi‑planar **4:4:4** source
  /// ([`Nv24`](crate::source::Nv24)). Each row is `U0, V0, U1, V1, …` for
  /// `width` pairs (`2 * width` bytes). One UV pair per Y pixel — no
  /// chroma subsampling.
  #[display("UV Full")]
  UvFull,
  /// Full‑width interleaved VU plane in a semi‑planar **4:4:4** source
  /// ([`Nv42`](crate::source::Nv42)). Each row is `V0, U0, V1, U1, …` for
  /// `width` pairs — byte order swapped relative to [`Self::UvFull`].
  #[display("VU Full")]
  VuFull,
  /// Full‑width Y row of a **9‑bit** planar source
  /// ([`Yuv420p9`](crate::source::Yuv420p9) /
  /// [`Yuv422p9`](crate::source::Yuv422p9) /
  /// [`Yuv444p9`](crate::source::Yuv444p9)). `u16` samples, `width`
  /// elements (low 9 bits active).
  #[display("Y9")]
  Y9,
  /// Half‑width U row of a **9‑bit** planar source. `u16` samples,
  /// `width / 2` elements.
  #[display("U Half 9")]
  UHalf9,
  /// Half‑width V row of a **9‑bit** planar source. `u16` samples,
  /// `width / 2` elements.
  #[display("V Half 9")]
  VHalf9,
  /// Full‑width U row of a **9‑bit** 4:4:4 planar source.
  #[display("U Full 9")]
  UFull9,
  /// Full‑width V row of a **9‑bit** 4:4:4 planar source.
  #[display("V Full 9")]
  VFull9,
  /// Full-width alpha row of a **9-bit** YUVA planar source
  /// ([`Yuva420p9`](crate::source::Yuva420p9)). `u16` samples, `width`
  /// elements, low-bit-packed.
  #[display("A Full 9")]
  AFull9,
  /// Full‑width Y row of a **10‑bit** planar source ([`Yuv420p10`]).
  /// `u16` samples, `width` elements.
  #[display("Y10")]
  Y10,
  /// Half‑width U row of a **10‑bit** planar source. `u16` samples,
  /// `width / 2` elements.
  #[display("U Half 10")]
  UHalf10,
  /// Half‑width V row of a **10‑bit** planar source. `u16` samples,
  /// `width / 2` elements.
  #[display("V Half 10")]
  VHalf10,
  /// Half‑width interleaved UV row of a **10‑bit semi‑planar** source
  /// ([`P010`]). `u16` samples, `width` elements laid out as
  /// `U0, V0, U1, V1, …` (high‑bit‑packed: each element's 10 active
  /// bits sit in the high 10 of its `u16`).
  #[display("UV Half 10")]
  UvHalf10,
  /// Full‑width Y row of a **12‑bit** source — used for both the
  /// planar ([`Yuv420p12`], low‑bit‑packed) and semi‑planar
  /// ([`P012`], high‑bit‑packed) families. `u16` samples, `width`
  /// elements. The packing direction depends on the source format;
  /// the row‑shape check only verifies length, so a single variant
  /// covers both.
  #[display("Y12")]
  Y12,
  /// Half‑width U row of a **12‑bit** planar source. `u16` samples,
  /// `width / 2` elements.
  #[display("U Half 12")]
  UHalf12,
  /// Half‑width V row of a **12‑bit** planar source. `u16` samples,
  /// `width / 2` elements.
  #[display("V Half 12")]
  VHalf12,
  /// Half‑width interleaved UV row of a **12‑bit semi‑planar** source
  /// ([`P012`]). `u16` samples, `width` elements (high‑bit‑packed: 12
  /// active bits in the high 12 of each `u16`).
  #[display("UV Half 12")]
  UvHalf12,
  /// Full‑width Y row of a **14‑bit** planar source ([`Yuv420p14`]).
  /// `u16` samples, `width` elements, low‑bit‑packed.
  #[display("Y14")]
  Y14,
  /// Half‑width U row of a **14‑bit** planar source. `u16` samples,
  /// `width / 2` elements.
  #[display("U Half 14")]
  UHalf14,
  /// Half‑width V row of a **14‑bit** planar source. `u16` samples,
  /// `width / 2` elements.
  #[display("V Half 14")]
  VHalf14,
  /// Full‑width Y row of a **16‑bit** source — used for both the
  /// planar ([`Yuv420p16`](crate::source::Yuv420p16)) and semi‑planar
  /// ([`P016`](crate::source::P016)) families. At 16 bits there is no
  /// high‑vs‑low packing distinction.
  #[display("Y16")]
  Y16,
  /// Half‑width U row of a **16‑bit** planar source. `u16` samples,
  /// `width / 2` elements.
  #[display("U Half 16")]
  UHalf16,
  /// Half‑width V row of a **16‑bit** planar source. `u16` samples,
  /// `width / 2` elements.
  #[display("V Half 16")]
  VHalf16,
  /// Half‑width interleaved UV row of a **16‑bit semi‑planar** source
  /// ([`P016`](crate::source::P016)). `u16` samples, `width` elements.
  #[display("UV Half 16")]
  UvHalf16,
  /// Full-width alpha row of a **16-bit** YUVA planar source
  /// ([`Yuva420p16`](crate::source::Yuva420p16) /
  /// [`Yuva444p16`](crate::source::Yuva444p16)). `u16` samples,
  /// `width` elements (full u16 range).
  #[display("A Full 16")]
  AFull16,
  /// Full-width U row of a **16-bit** 4:4:4 planar source
  /// ([`Yuv444p16`](crate::source::Yuv444p16) /
  /// [`Yuva444p16`](crate::source::Yuva444p16)). `u16` samples,
  /// `width` elements (full u16 range).
  #[display("U Full 16")]
  UFull16,
  /// Full-width V row of a **16-bit** 4:4:4 planar source. `u16`
  /// samples, `width` elements (full u16 range).
  #[display("V Full 16")]
  VFull16,
  /// Full‑width interleaved UV row of a **10‑bit semi‑planar 4:4:4**
  /// source ([`P410`](crate::source::P410)). `u16` samples, `2 * width`
  /// elements, high‑bit‑packed.
  #[display("UV Full 10")]
  UvFull10,
  /// Full‑width interleaved UV row of a **12‑bit semi‑planar 4:4:4**
  /// source ([`P412`](crate::source::P412)). `u16` samples, `2 * width`
  /// elements, high‑bit‑packed.
  #[display("UV Full 12")]
  UvFull12,
  /// Full‑width interleaved UV row of a **16‑bit semi‑planar 4:4:4**
  /// source ([`P416`](crate::source::P416)). `u16` samples, `2 * width`
  /// elements (no high/low packing distinction at 16 bits).
  #[display("UV Full 16")]
  UvFull16,
  /// `above` row of an **8-bit Bayer** source
  /// ([`Bayer`](crate::raw::Bayer)). `u8` samples, `width` elements;
  /// supplied by the walker via the **mirror-by-2** boundary
  /// contract — see [`crate::raw::BayerRow::above`] — so at the
  /// top edge this is `mid_row(1)`, not `mid` itself. Replicate
  /// fallback (`above == mid`) only when `height < 2` (no mirror
  /// partner exists).
  #[display("Bayer Above")]
  BayerAbove,
  /// `mid` row of an **8-bit Bayer** source. `u8` samples, `width`
  /// elements — the row currently being produced.
  #[display("Bayer Mid")]
  BayerMid,
  /// `below` row of an **8-bit Bayer** source. `u8` samples, `width`
  /// elements; mirror-by-2 supplies `mid_row(h - 2)` at the bottom
  /// edge — see [`crate::raw::BayerRow::below`]. Replicate fallback
  /// (`below == mid`) only when `height < 2`.
  #[display("Bayer Below")]
  BayerBelow,
  /// `above` row of a **high-bit-depth Bayer** source
  /// ([`Bayer16<BITS>`](crate::raw::Bayer16)). `u16` samples,
  /// `width` elements; mirror-by-2 supplies `mid_row(1)` at the
  /// top edge. Replicate fallback (`above == mid`) only when
  /// `height < 2`.
  #[display("Bayer16 Above")]
  Bayer16Above,
  /// `mid` row of a **high-bit-depth Bayer** source. `u16` samples,
  /// `width` elements.
  #[display("Bayer16 Mid")]
  Bayer16Mid,
  /// `below` row of a **high-bit-depth Bayer** source. `u16`
  /// samples, `width` elements; mirror-by-2 supplies
  /// `mid_row(h - 2)` at the bottom edge. Replicate fallback
  /// (`below == mid`) only when `height < 2`.
  #[display("Bayer16 Below")]
  Bayer16Below,
  /// Pixel-index row of a [`Pal8`](crate::raw::Pal8) source.
  /// `u8` samples, `width` elements — each value is an index into
  /// the 256-entry BGRA palette carried alongside in `Pal8Row`.
  #[display("Pal8 index row")]
  Pal8IndexRow,
  /// Packed **RGB565** LE row of an [`Rgb565`](crate::source::Rgb565) source.
  /// `2 * width` `u8` bytes — one `u16` LE word per pixel.
  #[display("RGB565 packed")]
  Rgb565Packed,
  /// Packed **BGR565** LE row of a [`Bgr565`](crate::source::Bgr565) source.
  /// Same `2 * width` byte shape as [`Rgb565Packed`](Self::Rgb565Packed)
  /// with R↔B channel positions swapped.
  #[display("BGR565 packed")]
  Bgr565Packed,
  /// Packed **RGB555** LE row of an [`Rgb555`](crate::source::Rgb555) source.
  /// `2 * width` `u8` bytes — one `u16` LE word per pixel (bit 15 unused).
  #[display("RGB555 packed")]
  Rgb555Packed,
  /// Packed **BGR555** LE row of a [`Bgr555`](crate::source::Bgr555) source.
  /// Same shape as [`Rgb555Packed`](Self::Rgb555Packed) with R↔B swapped.
  #[display("BGR555 packed")]
  Bgr555Packed,
  /// Packed **RGB444** LE row of an [`Rgb444`](crate::source::Rgb444) source.
  /// `2 * width` `u8` bytes — one `u16` LE word per pixel (bits [15:12]
  /// unused).
  #[display("RGB444 packed")]
  Rgb444Packed,
  /// Packed **BGR444** LE row of a [`Bgr444`](crate::source::Bgr444) source.
  /// Same shape as [`Rgb444Packed`](Self::Rgb444Packed) with R↔B swapped.
  #[display("BGR444 packed")]
  Bgr444Packed,
  /// Packed **RGB8** row of an [`Rgb8`](crate::source::Rgb8) source.
  /// `width` `u8` bytes — one 3:3:2 pixel per byte.
  #[display("RGB8 packed")]
  Rgb8Packed,
  /// Packed **BGR8** row of a [`Bgr8`](crate::source::Bgr8) source.
  /// Same `width` byte shape as [`Rgb8Packed`](Self::Rgb8Packed) with the
  /// 3:3:2 channel positions in B, G, R order.
  #[display("BGR8 packed")]
  Bgr8Packed,
  /// Packed **RGB4_BYTE** row of an [`Rgb4Byte`](crate::source::Rgb4Byte)
  /// source. `width` `u8` bytes — one 1:2:1 pixel in each low nibble
  /// (the high nibble is unused padding).
  #[display("RGB4_BYTE packed")]
  Rgb4BytePacked,
  /// Packed **BGR4_BYTE** row of a [`Bgr4Byte`](crate::source::Bgr4Byte)
  /// source. Same shape as [`Rgb4BytePacked`](Self::Rgb4BytePacked) with
  /// R↔B swapped.
  #[display("BGR4_BYTE packed")]
  Bgr4BytePacked,
  /// Packed **RGB4** row of an [`Rgb4`](crate::source::Rgb4) source.
  /// `width.div_ceil(2)` `u8` bytes — two 1:2:1 pixels per byte (the even
  /// pixel in the high nibble).
  #[display("RGB4 packed")]
  Rgb4Packed,
  /// Packed **BGR4** row of a [`Bgr4`](crate::source::Bgr4) source.
  /// Same shape as [`Rgb4Packed`](Self::Rgb4Packed) with R↔B swapped.
  #[display("BGR4 packed")]
  Bgr4Packed,
  /// Packed `R, G, B` row of an [`Rgb24`](crate::source::Rgb24) source.
  /// `3 * width` `u8` bytes.
  #[display("RGB packed")]
  RgbPacked,
  /// Packed `B, G, R` row of a [`Bgr24`](crate::source::Bgr24) source.
  /// `3 * width` `u8` bytes (channel-order swapped relative to
  /// [`RgbPacked`](Self::RgbPacked)).
  #[display("BGR packed")]
  BgrPacked,
  /// Packed `R, G, B, A` row of an [`Rgba`](crate::source::Rgba) source.
  /// `4 * width` `u8` bytes — alpha is real (not padding).
  #[display("RGBA packed")]
  RgbaPacked,
  /// Packed `B, G, R, A` row of a [`Bgra`](crate::source::Bgra) source.
  /// `4 * width` `u8` bytes — alpha lane preserved, channel order
  /// swapped on the first three bytes relative to
  /// [`RgbaPacked`](Self::RgbaPacked).
  #[display("BGRA packed")]
  BgraPacked,
  /// Packed `A, R, G, B` row of an [`Argb`](crate::source::Argb) source.
  /// `4 * width` `u8` bytes — alpha at the **leading** position vs
  /// [`RgbaPacked`](Self::RgbaPacked).
  #[display("ARGB packed")]
  ArgbPacked,
  /// Packed `A, B, G, R` row of an [`Abgr`](crate::source::Abgr) source.
  /// `4 * width` `u8` bytes — leading alpha + reversed RGB order vs
  /// [`ArgbPacked`](Self::ArgbPacked).
  #[display("ABGR packed")]
  AbgrPacked,
  /// Packed `X, R, G, B` row of an [`Xrgb`](crate::source::Xrgb) source
  /// (FFmpeg `0rgb`). `4 * width` `u8` bytes — leading **padding**
  /// byte (not alpha).
  #[display("XRGB packed")]
  XrgbPacked,
  /// Packed `R, G, B, X` row of an [`Rgbx`](crate::source::Rgbx) source
  /// (FFmpeg `rgb0`). `4 * width` `u8` bytes — trailing padding byte.
  #[display("RGBX packed")]
  RgbxPacked,
  /// Packed `X, B, G, R` row of an [`Xbgr`](crate::source::Xbgr) source
  /// (FFmpeg `0bgr`). `4 * width` `u8` bytes — leading padding byte
  /// + reversed RGB order vs [`XrgbPacked`](Self::XrgbPacked).
  #[display("XBGR packed")]
  XbgrPacked,
  /// Packed `B, G, R, X` row of a [`Bgrx`](crate::source::Bgrx) source
  /// (FFmpeg `bgr0`). `4 * width` `u8` bytes — trailing padding byte
  /// + reversed RGB order vs [`RgbxPacked`](Self::RgbxPacked).
  #[display("BGRX packed")]
  BgrxPacked,
  /// Packed `X2RGB10` LE row of an
  /// [`X2Rgb10`](crate::source::X2Rgb10) source. `4 * width` `u8` bytes
  /// (one little-endian `u32` per pixel with `(MSB) 2X | 10R | 10G |
  /// 10B (LSB)` packing).
  #[display("X2RGB10 packed")]
  X2Rgb10Packed,
  /// Packed `X2BGR10` LE row of an
  /// [`X2Bgr10`](crate::source::X2Bgr10) source. `4 * width` `u8` bytes
  /// — channel positions reversed relative to
  /// [`X2Rgb10Packed`](Self::X2Rgb10Packed).
  #[display("X2BGR10 packed")]
  X2Bgr10Packed,
  /// Packed `Y0, U0, Y1, V0, …` row of a
  /// [`Yuyv422`](crate::source::Yuyv422) source (FFmpeg `yuyv422` /
  /// YUY2). `2 * width` `u8` bytes — Y in even byte positions, U/V
  /// in odd positions with U preceding V.
  #[display("YUYV422 packed")]
  Yuyv422Packed,
  /// Packed `U0, Y0, V0, Y1, …` row of a
  /// [`Uyvy422`](crate::source::Uyvy422) source (FFmpeg `uyvy422` /
  /// UYVY). `2 * width` `u8` bytes — Y in odd byte positions, U/V
  /// in even positions with U preceding V.
  #[display("UYVY422 packed")]
  Uyvy422Packed,
  /// Packed `Y0, V0, Y1, U0, …` row of a
  /// [`Yvyu422`](crate::source::Yvyu422) source (FFmpeg `yvyu422` /
  /// YVYU). `2 * width` `u8` bytes — Y in even byte positions, V/U
  /// in odd positions with V preceding U (chroma order swapped vs
  /// [`Yuyv422Packed`](Self::Yuyv422Packed)).
  #[display("YVYU422 packed")]
  Yvyu422Packed,
  /// Packed `U0, Y0, Y1, V0, Y2, Y3, …` row of a
  /// [`Uyyvyy411`](crate::source::Uyyvyy411) source (FFmpeg
  /// `uyyvyy411`). `width * 3 / 2` `u8` bytes — one (U, V) chroma
  /// pair shared across 4 luma samples (4:1:1 horizontal
  /// subsampling, 12 bpp, DV legacy).
  #[display("UYYVYY411 packed")]
  Uyyvyy411Packed,
  /// Packed `v210` row of a [`V210`](crate::source::V210) source —
  /// Tier 4 10-bit pro-broadcast SDI capture format. Each 16-byte
  /// word holds 12 x 10-bit samples = 6 pixels (4:2:2: 6 Y +
  /// 3 Cb + 3 Cr). Row length: `(width / 6) * 16` `u8` bytes.
  #[display("V210 packed")]
  V210Packed,
  /// Packed `y210` row of a [`Y210`](crate::source::Y210) source —
  /// Tier 4 10-bit MSB-aligned in u16 with YUYV422 byte order.
  /// Row length: `2 * width` `u16` elements (= `4 * width` bytes).
  #[display("Y210 packed")]
  Y210Packed,
  /// Packed `y212` row — same shape as Y210 with BITS=12.
  #[display("Y212 packed")]
  Y212Packed,
  /// Packed `y216` row — same shape as Y210 with BITS=16.
  #[display("Y216 packed")]
  Y216Packed,
  /// Packed `v410` row of a `V410` source — Tier 5 10-bit 4:4:4
  /// packed format. One `u32` word per pixel; row length: `width`
  /// `u32` elements (= `4 * width` bytes).
  #[display("V410 packed")]
  V410Packed,
  /// Packed `v30x` row of a `V30X` source — Tier 5 10-bit 4:4:4
  /// packed format, sibling of V410 with 2-bit padding at the
  /// **low** end. One `u32` word per pixel; row length: `width`
  /// `u32` elements (= `4 * width` bytes).
  #[display("V30X packed")]
  V30XPacked,
  /// Packed `xv36` row of an `Xv36` source — Tier 5 16-bit 4:4:4
  /// packed format. Four `u16` elements per pixel (one per channel);
  /// row length: `4 * width` `u16` elements (= `8 * width` bytes).
  #[display("XV36 packed")]
  Xv36Packed,
  /// Packed `xv48` row of an `Xv48` source — Tier 5 16-bit 4:4:4
  /// packed format (full-depth sibling of XV36). Four `u16` elements per
  /// pixel (one per channel); row length: `4 * width` `u16` elements
  /// (= `8 * width` bytes).
  #[display("XV48 packed")]
  Xv48Packed,
  /// Packed `vuya` row of a `Vuya` source — Tier 5 8-bit 4:4:4
  /// packed format. Four bytes per pixel in V/U/Y/A order; row
  /// length: `4 * width` bytes.
  #[display("VUYA packed")]
  VuyaPacked,
  /// Packed `vuyx` row of a `Vuyx` source — Tier 5 8-bit 4:4:4
  /// packed format. Four bytes per pixel in V/U/Y/X order (X is
  /// padding); row length: `4 * width` bytes.
  #[display("VUYX packed")]
  VuyxPacked,
  /// Packed `ayuv64` row of an `Ayuv64` source — Tier 5 16-bit
  /// 4:4:4 packed format. Four `u16` elements per pixel in A/Y/U/V
  /// order; row length: `4 * width` `u16` elements (= `8 * width`
  /// bytes).
  #[display("AYUV64 packed")]
  Ayuv64Packed,
  /// Packed `ayuv` row of an `Ayuv` source — 8-bit 4:4:4 packed format.
  /// Four bytes per pixel in A/Y/U/V order (A is real source alpha);
  /// row length: `4 * width` bytes.
  #[display("AYUV packed")]
  AyuvPacked,
  /// Packed `uyva` row of a `Uyva` source — 8-bit 4:4:4 packed format.
  /// Four bytes per pixel in U/Y/V/A order (A is real source alpha);
  /// row length: `4 * width` bytes.
  #[display("UYVA packed")]
  UyvaPacked,
  /// Packed `vyu444` row of a `Vyu444` source — 8-bit 4:4:4 packed format
  /// with **no alpha** (24bpp). Three bytes per pixel in V/Y/U order;
  /// row length: `3 * width` bytes.
  #[display("VYU444 packed")]
  Vyu444Packed,
  /// Packed `R, G, B` row of an [`Rgbf32`](crate::source::Rgbf32) source —
  /// Tier 9 32-bit float per channel. Row length: `3 * width` `f32`
  /// elements (= `12 * width` bytes).
  #[display("RGBF32 packed")]
  RgbF32Packed,
  /// Packed `R, G, B` row of an [`Rgbf16`](crate::source::Rgbf16) source —
  /// Tier 9 16-bit half-precision float per channel. Row length:
  /// `3 * width` `half::f16` elements (= `6 * width` bytes).
  #[display("RGBF16 packed")]
  RgbF16Packed,
  /// Packed `R, G, B, A` row of an [`Rgbaf32`](crate::source::Rgbaf32)
  /// source — Tier 9 32-bit float per channel, real alpha. Row length:
  /// `4 * width` `f32` elements (= `16 * width` bytes).
  #[display("RGBAF32 packed")]
  RgbaF32Packed,
  /// Packed `R, G, B, A` row of an [`Rgbaf16`](crate::source::Rgbaf16)
  /// source — Tier 9 16-bit half-precision float per channel, real alpha.
  /// Row length: `4 * width` `half::f16` elements (= `8 * width` bytes).
  #[display("RGBAF16 packed")]
  RgbaF16Packed,
  /// Packed `X, Y, Z` row of an [`Xyz12`](crate::source::Xyz12) source —
  /// Tier 12 12-bit CIE XYZ packed in u16 triples — active 12 bits
  /// in `[15:4]`, low 4 bits zero (per FFmpeg
  /// `AV_PIX_FMT_XYZ12LE/BE`). Row length: `3 * width` `u16` elements
  /// (= `6 * width` bytes).
  #[display("XYZ12 packed")]
  Xyz12Packed,
  /// Green plane row of an 8-bit planar GBR source
  /// ([`Gbrp`](crate::source::Gbrp) /
  /// [`Gbrap`](crate::source::Gbrap)). `u8` samples, `width` elements.
  #[display("G plane")]
  GPlane,
  /// Blue plane row of an 8-bit planar GBR source. `u8` samples,
  /// `width` elements.
  #[display("B plane")]
  BPlane,
  /// Red plane row of an 8-bit planar GBR source. `u8` samples,
  /// `width` elements.
  #[display("R plane")]
  RPlane,
  /// Plane row of a float-32 planar GBR source (`Gbrpf32` /
  /// `Gbrapf32`). `f32` samples, `width` elements per plane.
  #[display("GBR f32 plane")]
  GbrF32Plane,
  /// Plane row of a float-16 planar GBR source (`Gbrpf16` /
  /// `Gbrapf16`). `half::f16` samples, `width` elements per plane.
  #[display("GBR f16 plane")]
  GbrF16Plane,
  /// Packed `R, G, B` row of an [`Rgb48`](crate::source::Rgb48) source —
  /// `width * 3` u16 elements (each channel 16 bits, R, G, B order).
  #[display("RGB48 packed")]
  Rgb48Packed,
  /// Packed `B, G, R` row of a [`Bgr48`](crate::source::Bgr48) source —
  /// `width * 3` u16 elements (channel order reversed vs
  /// [`Rgb48Packed`](Self::Rgb48Packed)).
  #[display("BGR48 packed")]
  Bgr48Packed,
  /// Packed `R, G, B, A` row of an [`Rgba64`](crate::source::Rgba64) source —
  /// `width * 4` u16 elements (each channel 16 bits; alpha is real).
  #[display("RGBA64 packed")]
  Rgba64Packed,
  /// Packed `B, G, R, A` row of a [`Bgra64`](crate::source::Bgra64) source —
  /// `width * 4` u16 elements (channel order reversed on RGB vs
  /// [`Rgba64Packed`](Self::Rgba64Packed); alpha at slot 3 is real).
  #[display("BGRA64 packed")]
  Bgra64Packed,
  /// Packed `R, G, B` row of an [`Rgb96`](crate::source::Rgb96) source —
  /// `width * 3` u32 elements (each channel 32 bits, R, G, B order).
  #[display("RGB96 packed")]
  Rgb96Packed,
  /// Packed `R, G, B, A` row of an [`Rgba128`](crate::source::Rgba128) source —
  /// `width * 4` u32 elements (each channel 32 bits; alpha at slot 3 is real).
  #[display("RGBA128 packed")]
  Rgba128Packed,
}

/// How a packed-alpha source's color channels relate to its alpha when
/// the frame is **area-resampled** — the only place the distinction is
/// observable, because area-binning averages color and alpha together.
///
/// In [`Self::Straight`] (a.k.a. *unassociated* / *non-premultiplied*)
/// alpha, the RGB triple stores the surface's own color and α is an
/// independent coverage term; averaging the channels independently is
/// correct. In [`Self::Premultiplied`] (a.k.a. *associated*) alpha, RGB
/// has already been multiplied by α, so a correct area-average must bin
/// the premultiplied channels and un-premultiply afterwards — averaging
/// straight RGB of a premultiplied source would let fully-transparent
/// pixels (whose stored RGB is arbitrary) bleed into the result.
///
/// Every packed-RGBA source format pixon ships today is straight (see
/// [`DefaultAlphaMode`]); the mode only matters on the resample path and
/// is a no-op for the direct (identity-plan) conversions, which copy
/// alpha through untouched.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, IsVariant)]
pub enum AlphaMode {
  /// Unassociated alpha: RGB is the surface color, α an independent
  /// coverage term. Channels area-average independently.
  #[default]
  Straight,
  /// Associated alpha: RGB is already premultiplied by α. The resample
  /// path bins the premultiplied channels and un-premultiplies per
  /// finalized output row.
  Premultiplied,
}

impl AlphaMode {
  /// Returns the lowercase string name of the mode (`"straight"` /
  /// `"premultiplied"`), matching the variant's conventional spelling.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn as_str(self) -> &'static str {
    match self {
      Self::Straight => "straight",
      Self::Premultiplied => "premultiplied",
    }
  }
}

/// Per-format default [`AlphaMode`], consulted by [`MixedSinker::new`]
/// to seed the sink's alpha mode before any
/// [`MixedSinker::with_alpha_mode`] override.
///
/// The blanket impl below makes every [`SourceFormat`] default to
/// [`AlphaMode::Straight`] — true of every packed-RGBA source pixon
/// ships today (`Rgba` / `Bgra` / `Argb` / `Abgr` / `Rgba64` /
/// `Bgra64`). A future source format whose wire alpha is associated
/// would carry its premultiplied default here (replacing the blanket
/// with per-format impls), so callers get correct area-resampling
/// without having to pass [`MixedSinker::with_alpha_mode`] by hand.
pub trait DefaultAlphaMode: SourceFormat {
  /// The alpha interpretation a freshly built [`MixedSinker`] over this
  /// format starts in.
  const DEFAULT_ALPHA_MODE: AlphaMode = AlphaMode::Straight;
}

impl<F: SourceFormat> DefaultAlphaMode for F {}

/// How SMPTE ST 428-1 (Digital Cinema) [`Primaries::SmpteSt428`] are
/// interpreted when they feed the
/// [`KernelMatrix::ChromaDerivedNcl`] luma-weight derivation.
///
/// ST 428-1 encodes colour directly in **CIE XYZ** — its channels *are*
/// X, Y, Z, so colorimetrically its primaries are the XYZ axes (a
/// near-identity), not an RGB gamut. FFmpeg's `av_csp_primaries_desc`
/// nonetheless tabulates ST 428-1 as the D-Cinema RGB primaries
/// `R(0.735, 0.265) G(0.274, 0.718) B(0.167, 0.009)`, white point E, and
/// mediaframe's [`Primaries::chromaticities`] mirrors FFmpeg (its
/// authority). This toggle selects which reading the sink applies:
///
/// - [`Self::FfmpegTabulated`] (the **default**) derives `ChromaDerivedNcl`
///   luma weights from FFmpeg's tabulated chromaticities, exactly as before —
///   every existing caller stays byte-identical.
/// - [`Self::CieXyz`] takes ST 428-1 at its true CIE-XYZ meaning. A YCbCr
///   `ChromaDerivedNcl` matrix over XYZ data is colorimetrically meaningless,
///   so the sink **rejects** that combination with [`St428CieXyzUnsupported`]
///   rather than deriving weights from the tabulated RGB primaries;
///   XYZ-encoded ST 428-1 belongs on the `xyz12` path instead.
///
/// The toggle only affects `ChromaDerivedNcl` decodes whose source primaries
/// are [`Primaries::SmpteSt428`]; every other matrix, and every other primary
/// set, resolves identically in both modes.
///
/// # Future extension
///
/// A full XYZ↔RGB *matrix* derivation — tabulated primaries → the real
/// D-Cinema RGB matrix under [`Self::FfmpegTabulated`], versus the XYZ
/// identity under [`Self::CieXyz`] — is a later enhancement with no consumer
/// today (pixon has no primaries-driven RGB↔XYZ matrix path; XYZ sources go
/// through the separate `xyz12` colorimetry). This toggle reserves the
/// caller-facing switch so that derivation has a home when a consumer arrives.
///
/// [`Primaries::SmpteSt428`]: crate::Primaries::SmpteSt428
/// [`Primaries::chromaticities`]: crate::Primaries::chromaticities
/// [`KernelMatrix::ChromaDerivedNcl`]: crate::KernelMatrix::ChromaDerivedNcl
#[cfg(feature = "yuv-planar")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, IsVariant)]
pub enum St428Interpretation {
  /// Derive from FFmpeg's tabulated D-Cinema RGB primaries — the default,
  /// preserving every existing conversion byte-for-byte.
  #[default]
  FfmpegTabulated,
  /// Treat ST 428-1 as its true CIE-XYZ encoding: reject a `ChromaDerivedNcl`
  /// YCbCr derivation over [`Primaries::SmpteSt428`](crate::Primaries::SmpteSt428)
  /// instead of deriving meaningless weights from the tabulated RGB values.
  CieXyz,
}

#[cfg(feature = "yuv-planar")]
impl St428Interpretation {
  /// Lowercase identifier for the interpretation
  /// (`"ffmpeg-tabulated"` / `"cie-xyz"`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn as_str(self) -> &'static str {
    match self {
      Self::FfmpegTabulated => "ffmpeg-tabulated",
      Self::CieXyz => "cie-xyz",
    }
  }
}

/// A [`KernelMatrix::ChromaDerivedNcl`](crate::KernelMatrix::ChromaDerivedNcl)
/// decode was requested over [`Primaries::SmpteSt428`](crate::Primaries::SmpteSt428)
/// while the sink is in [`St428Interpretation::CieXyz`] mode.
///
/// Under the CIE-XYZ interpretation ST 428-1 data *is* CIE XYZ, so deriving
/// YCbCr luma weights from its (RGB-shaped, FFmpeg-tabulated) primaries is
/// colorimetrically meaningless. Decode XYZ-encoded ST 428-1 through the
/// `xyz12` path, switch to [`St428Interpretation::FfmpegTabulated`] to accept
/// FFmpeg's tabulated interpretation, or select a non-`ChromaDerivedNcl`
/// matrix.
#[cfg(feature = "yuv-planar")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error(
  "ChromaDerivedNcl over SmpteSt428 primaries is unsupported under the CIE-XYZ \
   interpretation: ST 428-1 is CIE XYZ, not RGB — decode via the xyz12 path, or \
   select St428Interpretation::FfmpegTabulated"
)]
pub struct St428CieXyzUnsupported(());

#[cfg(feature = "yuv-planar")]
impl St428CieXyzUnsupported {
  /// Constructs the marker error.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub(crate) const fn new() -> Self {
    Self(())
  }
}

/// Whether `primaries` denote data that is *already* CIE XYZ under the
/// [CIE-XYZ interpretation](St428Interpretation::CieXyz).
///
/// Self-contained pixon knowledge, independent of the mediaframe version:
/// Enforces the [`St428Interpretation`] contract at the single
/// primaries-driven luma-weight derivation
/// ([`ChromaDerivedNcl`](crate::KernelMatrix::ChromaDerivedNcl), resolved by
/// `row::scalar::Coefficients::for_matrix_with_primaries`).
///
/// Returns [`St428CieXyzUnsupported`] iff `interp` is
/// [`St428Interpretation::CieXyz`], `matrix` is `ChromaDerivedNcl`, and
/// `primaries` are CIE-XYZ ([`Primaries::is_cie_xyz`](crate::Primaries::is_cie_xyz))
/// — the one combination
/// whose tabulated YCbCr derivation is colorimetrically meaningless. Every
/// other combination (the default
/// [`FfmpegTabulated`](St428Interpretation::FfmpegTabulated) mode, any
/// non-`ChromaDerivedNcl` matrix, any non-ST 428-1 primaries) is `Ok`, so the
/// tabulated derivation runs unchanged.
#[cfg(feature = "yuv-planar")]
#[cfg_attr(not(tarpaulin), inline(always))]
const fn st428_chroma_derived_guard(
  matrix: crate::KernelMatrix,
  primaries: &crate::Primaries,
  interp: St428Interpretation,
) -> Result<(), St428CieXyzUnsupported> {
  if matches!(interp, St428Interpretation::CieXyz)
    && matches!(matrix, crate::KernelMatrix::ChromaDerivedNcl)
    && primaries.is_cie_xyz()
  {
    return Err(St428CieXyzUnsupported::new());
  }
  Ok(())
}

/// A sink that writes any subset of `{RGB, Luma, HSV}` into
/// caller-provided buffers.
///
/// Each output is optional — provide `Some(buffer)` to have that
/// channel written, leave it `None` to skip. Providing no outputs is
/// legal (the kernel still walks the source and calls `process`
/// for each row, but nothing is written).
///
/// When HSV is requested **without** RGB, `MixedSinker` keeps a single
/// row of intermediate RGB in an internal scratch buffer (allocated
/// lazily on first use). If RGB output is also requested, the user's
/// RGB buffer serves as the intermediate for HSV and no scratch is
/// allocated.
///
/// # Type parameters
///
/// `F` identifies the source format — `Yuv420p`, `Nv12`, `Nv21`,
/// `Yuv420p10`, `Yuv420p12`, `Yuv420p14`, `P010`, `P012`, etc. Each
/// format provides its own `impl PixelSink for MixedSinker<'_, F>`.
/// See the module‑level docs for the full list of shipped impls.
///
/// `R` is the resampling strategy deciding the sinker's **output**
/// geometry, injected via [`Self::with_resampler`]. The default
/// [`NoopResampler`] is the identity:
/// output geometry == source geometry, i.e. exactly the historical
/// behavior of [`Self::new`]. Output buffers always validate against
/// the output geometry; [`PixelSink::begin_frame`] always validates
/// the walker against the source geometry.
///
/// Formats route non-identity plans as they wire into the streaming
/// engine (currently [`Yuv420p`](crate::source::Yuv420p)). Every other
/// per-format [`PixelSink`] impl stays pinned to the default strategy:
/// a sinker built with a non-identity strategy can attach
/// (output-validated) buffers but does not implement [`PixelSink`], so
/// routing it through a walker is a compile error rather than a
/// geometry-mismatch panic.
pub struct MixedSinker<'a, F: SourceFormat, R = NoopResampler> {
  rgb: Option<&'a mut [u8]>,
  rgb_u16: Option<&'a mut [u16]>,
  rgb_f32: Option<&'a mut [f32]>,
  rgb_f16: Option<&'a mut [half::f16]>,
  rgba: Option<&'a mut [u8]>,
  rgba_u16: Option<&'a mut [u16]>,
  rgba_f32: Option<&'a mut [f32]>,
  rgba_f16: Option<&'a mut [half::f16]>,
  luma: Option<&'a mut [u8]>,
  luma_u16: Option<&'a mut [u16]>,
  luma_f32: Option<&'a mut [f32]>,
  // `HsvFrameMut` is cfg-gated to the same 15-feature any as the
  // per-format `process` impls that read it.
  #[cfg(any(
    feature = "bayer",
    feature = "gbr",
    feature = "gray",
    feature = "mono",
    feature = "rgb",
    feature = "rgb-float",
    feature = "rgb-legacy",
    feature = "v210",
    feature = "xyz",
    feature = "y2xx",
    feature = "yuv-444-packed",
    feature = "yuv-packed",
    feature = "yuv-planar",
    feature = "yuv-semi-planar",
    feature = "yuva",
  ))]
  hsv: Option<HsvFrameMut<'a>>,
  /// Lossless linear-XYZ pass-through buffer used by the
  /// [`Xyz12`](crate::source::Xyz12) source's `with_xyz_f32` accessor.
  /// `None` for every other source format.
  #[cfg(feature = "xyz")]
  xyz_f32: Option<&'a mut [f32]>,
  width: usize,
  height: usize,
  /// Output geometry from the resampler's plan; equals
  /// `(width, height)` under the identity plan. Every output-buffer
  /// length validation sizes against these, never against the source
  /// geometry.
  out_width: usize,
  out_height: usize,
  /// The non-identity plan fixed by [`MixedSinker::with_resampler`];
  /// `None` for [`MixedSinker::new`] and identity plans (the sinker
  /// then takes the direct conversion path).
  plan: Option<ResamplePlan>,
  /// Row-stage area streams (color group / luma group) for formats
  /// that route non-identity plans. Lazily created in `process`,
  /// reset in `begin_frame`. Gated like the engine itself, widening
  /// as families wire in.
  #[cfg(any(
    feature = "yuv-planar",
    feature = "rgb",
    feature = "gbr",
    feature = "gray",
    feature = "xyz",
    feature = "bayer",
    feature = "mono",
    feature = "yuv-semi-planar",
    feature = "yuv-packed",
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "v210",
    feature = "rgb-legacy"
  ))]
  #[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
  rgb_stream: Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
  /// Row-stage area stream for high-bit packed-RGB sources (`u16`
  /// elements binned at native depth). Lazily created in `process`,
  /// reset in `begin_frame`. Gated to `rgb` (high-bit packed RGB),
  /// `gbr` (high-bit planar GBR scatters into the same u16 tail),
  /// `yuv-444-packed` / `y2xx` / `v210` (the high-bit packed YUV color
  /// groups bin their converted native-u16 RGB row here),
  /// `yuv-planar` (the high-bit planar YUV 4:4:4 / 4:2:2 color group
  /// bins its converted native-u16 RGB row here), and `yuv-semi-planar`
  /// (the high-bit semi-planar P-format color group bins its converted
  /// native-u16 RGB row here); widens as high-bit families wire in.
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "v210",
    feature = "yuv-planar",
    feature = "yuv-semi-planar"
  ))]
  rgb_stream_u16: Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  /// Row-stage **4-channel** `u8` area stream for the alpha-aware u8 color
  /// of packed straight/premult RGBA sources (`Rgba` / `Bgra` / `Argb` /
  /// `Abgr`), the planar GBR+alpha family (`Gbrap`, decoded to the same
  /// canonical RGBA row), gray+alpha (`Ya8`), and the packed YUVA family
  /// (`Vuya`, and `Ayuv64`'s u8 color group — the converted u8 RGBA row).
  /// Bins the staged canonical `R, G, B, A` row so resampled alpha is a
  /// real area mean (not forced opaque) and — under
  /// [`AlphaMode::Premultiplied`] — color is binned premultiplied.
  /// Lazily created in `process`, reset in `begin_frame`. Gated to `rgb` /
  /// `gbr` / `gray` / `mono` / `yuv-444-packed`; the 3-channel
  /// [`Self::rgb_stream`] still serves the rgb-only straight path with no
  /// regression. (`mono` joins for `Pal8`, whose palette carries real
  /// per-entry alpha — the expand-to-RGBA-then-bin route.)
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "gray",
    feature = "mono",
    feature = "yuva",
    feature = "yuv-444-packed"
  ))]
  rgba_stream: Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
  /// Row-stage **4-channel** `u16` area stream for the native-depth u16
  /// color of the high-bit packed RGBA sources (`Rgba64` / `Bgra64`), the
  /// high-bit planar GBR+alpha family (`Gbrap10` … `Gbrap16`), gray+alpha
  /// (`Ya16`), and the packed YUVA `Ayuv64` (its independent u16 color
  /// group — the converted native u16 RGBA row). Bins the staged canonical
  /// `R, G, B, A` row at native depth; the native-depth `rgba_u16` output
  /// copies it (the RGB-source narrowed outputs derive via
  /// `>> (SRC_BITS - 8)`, but `Ayuv64` instead bins its u8 color
  /// independently in [`Self::rgba_stream`]). Lazily created in `process`,
  /// reset in `begin_frame`. Gated to `rgb` / `gbr` / `gray` /
  /// `yuv-444-packed`.
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "gray",
    feature = "yuva",
    feature = "yuv-444-packed"
  ))]
  rgba_stream_u16: Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  /// Row-stage 3-channel `u32` **area** stream for the native-`u32` packed-RGB
  /// resample tier (`Rgb96`, and the rgb-only / alpha-dropped paths of
  /// `Rgba128` / `Gbrap32`): bins the staged host-native `R, G, B` `u32` row at
  /// full `u32` precision so every output narrows only after binning — exact
  /// 0-ULP versus the narrow-first u16 stream (closes issue #289). Lazily
  /// created in `process`, reset in `begin_frame`. Gated to `rgb` / `gbr`.
  #[cfg(any(feature = "rgb", feature = "gbr"))]
  rgb_stream_u32: Option<std::boxed::Box<crate::resample::AreaStream<u32>>>,
  /// Row-stage 3-channel `u32` **filter** stream — the
  /// [`SpanKind::Filter`](crate::resample::SpanKind) twin of
  /// [`Self::rgb_stream_u32`]. Filters the staged host-native `R, G, B` `u32`
  /// row at the `f64` domain (carries a `u32` sample exactly), narrowing only
  /// per output (0-ULP). Gated to `rgb` / `gbr`.
  #[cfg(any(feature = "rgb", feature = "gbr"))]
  rgb_filter_stream_u32: Option<std::boxed::Box<crate::resample::FilterStream<u32>>>,
  /// Row-stage 4-channel `u32` **area** stream for the native-`u32` alpha-aware
  /// packed-RGBA resample tier (`Rgba128` / `Gbrap32`): bins the staged
  /// canonical host-native `R, G, B, A` `u32` row at full `u32` precision (and,
  /// under [`AlphaMode::Premultiplied`], premultiplied at `u32`) so resampled
  /// alpha and colour are exact 0-ULP after the post-bin narrow (issue #289).
  /// Gated to `rgb` / `gbr`.
  #[cfg(any(feature = "rgb", feature = "gbr"))]
  rgba_stream_u32: Option<std::boxed::Box<crate::resample::AreaStream<u32>>>,
  /// Row-stage 4-channel `u32` **filter** stream — the
  /// [`SpanKind::Filter`](crate::resample::SpanKind) twin of
  /// [`Self::rgba_stream_u32`]; filters R, G, B, A independently at `u32`
  /// precision (straight alpha only — premultiplied has no filter analogue).
  /// Gated to `rgb` / `gbr`.
  #[cfg(any(feature = "rgb", feature = "gbr"))]
  rgba_filter_stream_u32: Option<std::boxed::Box<crate::resample::FilterStream<u32>>>,
  /// Alpha mode frozen at a resampled frame's first row. A mid-frame
  /// [`Self::set_alpha_mode`] change is then rejected before any stream
  /// is fed, since a stream mixing straight and premultiplied rows would
  /// match neither all-straight nor all-premultiplied output. `None`
  /// between frames; re-armed on each frame's first resampled row (so a
  /// stale value never leaks across frames). Gated to `rgb` / `gbr` /
  /// `gray` / `mono` / `yuv-444-packed`.
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "gray",
    feature = "mono",
    feature = "yuva",
    feature = "yuv-444-packed"
  ))]
  frozen_alpha_mode: Option<AlphaMode>,
  /// Row-stage area stream for packed-float-RGB sources (`f32`
  /// elements binned in float). Lazily created in `process`, reset in
  /// `begin_frame`. The `rgb-float` family needs the engine fenced in
  /// (`AreaStream` is gated to `yuv-planar` / `rgb`, which `rgb-float`
  /// does not imply); the `gbr` family already pulls the engine via the
  /// #146 cascade, so its float-GBR scatter reaches this same tail with
  /// no separate fence.
  #[cfg(any(
    all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
    feature = "gbr"
  ))]
  rgb_stream_f32: Option<std::boxed::Box<crate::resample::AreaStream<f32>>>,
  /// Row-stage **filter** stream for packed-float-RGB sources
  /// ([`Rgbf32`](crate::source::Rgbf32)) — the
  /// [`SpanKind::Filter`](crate::resample::SpanKind) twin of
  /// [`Self::rgb_stream_f32`]. Lazily created in `process`, reset in
  /// `begin_frame`. Fed when the plan kind is `Filter`; bins at f32
  /// precision and emits unclamped (full-range float, PIL `F`-mode). Gated
  /// like [`Self::rgb_stream_f32`]: the packed-float-RGB consumers
  /// (`rgb-float`, with the engine fenced in: `FilterStream` is gated to
  /// `yuv-planar` / `rgb`, which `rgb-float` does not imply) AND `gbr` — the
  /// float planar GBR sources `Gbrpf32` / `Gbrpf16` scatter their G/B/R
  /// planes into the same packed `f32` RGB row and filter through this
  /// stream (the `gbr` build already carries the engine via the #146
  /// cascade).
  #[cfg(any(
    all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
    feature = "gbr"
  ))]
  rgb_filter_stream_f32: Option<std::boxed::Box<crate::resample::FilterStream<f32>>>,
  /// Row-stage **4-channel** `f32` area stream for the float planar
  /// GBR+alpha family ([`Gbrapf32`](crate::source::Gbrapf32) /
  /// [`Gbrapf16`](crate::source::Gbrapf16), the latter widened f16 ->
  /// host-native f32). Bins the staged canonical `R, G, B, A` f32 row so
  /// resampled alpha is a real area mean (not forced opaque) and — under
  /// [`AlphaMode::Premultiplied`] — color is binned premultiplied. Lazily
  /// created in `process`, reset in `begin_frame`. GBR-only: there is no
  /// packed-float RGBA source, so this is gated to `gbr` (which already
  /// carries the engine via the #146 cascade); the 3-channel
  /// [`Self::rgb_stream_f32`] still serves the rgb-only straight float path.
  #[cfg(any(
    feature = "gbr",
    all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
  ))]
  rgba_stream_f32: Option<std::boxed::Box<crate::resample::AreaStream<f32>>>,
  /// Row-stage **4-channel** `f32` **filter** stream for the float planar
  /// GBR+alpha family ([`Gbrapf32`](crate::source::Gbrapf32) /
  /// [`Gbrapf16`](crate::source::Gbrapf16), the latter widened f16 ->
  /// host-native f32) — the [`SpanKind::Filter`](crate::resample::SpanKind)
  /// twin of [`Self::rgba_stream_f32`] and the 4-channel analogue of
  /// [`Self::rgb_filter_stream_f32`]. PIL filters R, G, B, A independently
  /// with no premultiplication, so the staged canonical `R, G, B, A` f32 row
  /// bins through one 4-channel [`FilterStream<f32>`](crate::resample::FilterStream)
  /// and resampled alpha is a real filtered channel (never forced opaque).
  /// Lazily created in `process`, reset in `begin_frame`. There is no
  /// premultiplied filter analogue (the filter engine cannot un-premultiply),
  /// so a premultiplied frame stays on the area [`Self::rgba_stream_f32`];
  /// the filter stream is reached only for straight alpha. GBR-only (there is
  /// no packed-float RGBA source), gated to `gbr` to match
  /// [`Self::rgba_stream_f32`]; the engine is already pulled in for `gbr` via
  /// the #146 cascade. The float `rgb_f32` / `rgba_f32` outputs are
  /// full-range by design (HDR > 1 and negatives preserved), so there is no
  /// native-depth clamp on the filter output — the integer / f16 outputs
  /// clamp `[0, 1]` in their own narrows, so a signed-coefficient overshoot
  /// cannot wrap them.
  #[cfg(any(
    feature = "gbr",
    all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
  ))]
  rgba_filter_stream_f32: Option<std::boxed::Box<crate::resample::FilterStream<f32>>>,
  /// Row-stage area stream for the packed-CIE-XYZ-12-bit source
  /// ([`Xyz12`](crate::source::Xyz12)). The wire row converts to
  /// **linear XYZ** `f32` (post-OETF, pre-matrix) and bins in float so
  /// the area mean is taken in linear light — the matrix and gamma are
  /// applied per finalized output row, after the bin. Gated to `xyz`;
  /// the engine is already pulled in for `xyz` by the shared
  /// [`AreaStream`] gate (the `#145`/`#146` cascade widened it to
  /// `xyz`), so no separate engine feature is required.
  #[cfg(feature = "xyz")]
  xyz_stream_f32: Option<std::boxed::Box<crate::resample::AreaStream<f32>>>,
  /// Row-stage **filter** stream for the packed-CIE-XYZ-12-bit source
  /// ([`Xyz12`](crate::source::Xyz12)) — the
  /// [`SpanKind::Filter`](crate::resample::SpanKind) twin of
  /// [`Self::xyz_stream_f32`]. Bins the staged **linear XYZ** `f32` row
  /// (post-OETF, pre-matrix) with signed filter coefficients, then the
  /// per-finalized-row matrix + gamma + narrow run exactly as on the area
  /// path. Lazily created in `process`, reset in `begin_frame`. Gated to
  /// `xyz` to match [`Self::xyz_stream_f32`]: the shared engine gate
  /// already compiles [`FilterStream`] in for `xyz`. There is no
  /// native-depth clamp on the filter output — the integer / f16 outputs
  /// clamp `[0, 1]` in their own narrows and the `rgb_f32` / `xyz_f32`
  /// outputs are full-range by design (HDR > 1 and out-of-gamut negatives
  /// preserved), so a signed-coefficient overshoot cannot wrap them.
  #[cfg(feature = "xyz")]
  xyz_filter_stream_f32: Option<std::boxed::Box<crate::resample::FilterStream<f32>>>,
  /// Row-stage area stream for single-plane luma binning. Used by the
  /// planar YUV family (Y-plane luma), the [`Gray8`](crate::source::Gray8)
  /// source (Gray *is* a luma plane), and `mono` (bin the expanded
  /// 0/255 luma plane). Lazily created in `process`, reset in
  /// `begin_frame`. Gated like the engine; widens as families wire in.
  #[cfg(any(
    feature = "yuv-planar",
    feature = "rgb",
    feature = "gbr",
    feature = "gray",
    feature = "xyz",
    feature = "bayer",
    feature = "mono",
    feature = "yuv-semi-planar",
    feature = "yuv-packed",
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "v210",
    feature = "rgb-legacy"
  ))]
  #[cfg_attr(
    not(any(feature = "yuv-planar", feature = "gray", feature = "mono")),
    allow(dead_code)
  )]
  luma_stream: Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
  /// Row-stage area stream for single-plane **u16** luma binning. Used
  /// by the [`Gray16`](crate::source::Gray16) source, whose luma plane
  /// is a native `u16` and so bins at u16 precision (the `u8`
  /// [`Self::luma_stream`] would lose the low byte), by the high-bit
  /// packed YUV families (`yuv-444-packed` / `y2xx` / `v210`), and by the
  /// high-bit planar YUV families (`yuv-planar`: Yuv444p / Yuv422p
  /// 10/12/14/16), which bin their native Y here so resampled luma stays
  /// the area-downscaled Y at native depth. Lazily created in `process`,
  /// reset in `begin_frame`. Gated to `gray` / `yuv-444-packed` / `y2xx`
  /// / `v210` / `yuv-planar` / `yuv-semi-planar` (the high-bit semi-planar
  /// P-format family bins its de-packed native Y here, then narrows since
  /// P-formats expose no `luma_u16`); widens as u16 luma families wire in.
  #[cfg(any(
    feature = "gray",
    feature = "yuva",
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "v210",
    feature = "yuv-planar",
    feature = "yuv-semi-planar"
  ))]
  luma_stream_u16: Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  /// Row-stage area stream for single-plane **u32** luma binning. Used by
  /// the [`Gray32`](crate::source::Gray32) source, whose luma plane is a
  /// native `u32`: binning at native `u32` precision (then narrowing each
  /// output) is **0-ULP** (closes issue #289), whereas the `u16`
  /// [`Self::luma_stream_u16`] would drop the low 16 bits before averaging
  /// (≤1-LSB off the exact mean). Lazily created in `process`, reset in
  /// `begin_frame`. Gated to `gray`; widens if more `u32` luma families
  /// wire in.
  #[cfg(feature = "gray")]
  luma_stream_u32: Option<std::boxed::Box<crate::resample::AreaStream<u32>>>,
  /// Row-stage area stream for single-plane **f32** luma binning. Used
  /// by the [`Grayf32`](crate::source::Grayf32) source, whose luma plane
  /// is a native `f32` and so bins at f32 precision (the `u8` / `u16`
  /// luma streams would quantize every sample before averaging). Lazily
  /// created in `process`, reset in `begin_frame`. Gated to `gray`;
  /// widens as f32 luma families wire in.
  #[cfg(feature = "gray")]
  luma_stream_f32: Option<std::boxed::Box<crate::resample::AreaStream<f32>>>,
  /// Row-stage **filter** stream for the packed-RGB `u8` color group
  /// ([`Rgb24`](crate::source::Rgb24)) — the signed-coefficient
  /// (PIL-parity) twin of [`Self::rgb_stream`], fed when the plan's
  /// [`SpanKind`](crate::resample::SpanKind) is `Filter`. Lazily created
  /// in `process`, reset in `begin_frame`. The first format routed through
  /// the filter engine in this stage; the gate widens with the area
  /// engine as more packed-RGB sources wire in (`yuv-444-packed` joins for
  /// the high-bit packed YUV 4:4:4 colour group — `V410` / `V30X` convert
  /// their YUV to a u8 RGB row and filter it here, the filter twin of the
  /// area [`Self::rgb_stream`]; `y2xx` joins for the high-bit packed YUV
  /// 4:2:2 colour group — `Y210` / `Y212` / `Y216`, whose 4:2:2 chroma the
  /// convert closures upsample before this RGB-space filter; `yuv-planar`
  /// joins for the 8-bit planar YUV family — `Yuv410p` / `Yuv420p` /
  /// `Yuv422p` / `Yuv444p` / `Yuv440p` convert their separate Y/U/V planes
  /// to a source-width u8 RGB row and filter it here, matching the area
  /// [`Self::rgb_stream`] gate; `yuv-semi-planar` joins for the 8-bit
  /// semi-planar YUV family — `Nv12` / `Nv16` / `Nv21` / `Nv24` / `Nv42`
  /// convert their de-interleaved chroma into the same source-width u8 RGB
  /// row and filter it here, the filter twin of their area
  /// [`Self::rgb_stream`] use; `yuv-packed` joins for the 8-bit packed YUV
  /// 4:2:2 colour group — `Yuyv422` / `Uyvy422` / `Yvyu422` — and the 8-bit
  /// packed YUV 4:1:1 `Uyyvyy411`, whose `*_to_rgb_row` kernels
  /// de-interleave + horizontally upsample the chroma to a u8 RGB row
  /// before this RGB-space filter).
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "yuv-planar",
    feature = "yuv-semi-planar",
    feature = "yuv-packed"
  ))]
  rgb_filter_stream: Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
  /// Row-stage **filter** stream for the 8-bit packed-RGBA `u8` color
  /// group ([`Rgba`](crate::source::Rgba) and the leading-/trailing-alpha
  /// reorderings) — the 4-channel, signed-coefficient twin of
  /// [`Self::rgb_filter_stream`], fed when a real-alpha packed-RGBA source
  /// takes a `Filter` plan. PIL resizes RGBA by filtering R, G, B, A
  /// independently with no premultiplication, so the four interleaved
  /// channels bin through one 4-channel filter and a resampled RGBA frame
  /// is byte-exact versus PIL's RGBA resize. Lazily created in `process`,
  /// reset in `begin_frame`. Padding-alpha sources keep the 3-channel
  /// [`Self::rgb_filter_stream`] (the X byte is never filtered). Gated to
  /// `any(rgb, gbr, yuv-444-packed, mono)`: besides the 8-bit planar
  /// GBR+alpha source [`Gbrap`], the 8-bit packed YUV 4:4:4 sources `Vuya`
  /// (real source alpha) and `Vuyx` (padding byte forced opaque) convert
  /// their YUVA to a canonical u8 `R, G, B, A` row and filter through this
  /// same 4-channel stream, and the 8-bit palette-indexed [`Pal8`] expands
  /// each index to its palette color (real per-entry straight alpha) and
  /// filters that canonical RGBA row through this stream too — its non-color
  /// outputs (luma / u16) keep the direct Q8 / `(v << 8) | v` derivations,
  /// not the matrix-based ones the chromatic packed-RGBA path uses. `yuva`
  /// joins for the 8-bit planar YUVA family (`Yuva420p` / `Yuva422p` /
  /// `Yuva444p`), which converts its Y/U/V/A planes to a canonical u8
  /// `R, G, B, A` row and filters through this same 4-channel stream (the
  /// filter twin of its area [`Self::rgba_stream`] use); luma stays native Y.
  /// `gray` joins for the 8-bit packed gray+alpha source `Ya8`, a degenerate
  /// YUVA (`R = G = B = Y`, neutral chroma, real straight alpha): it decodes
  /// each `[Y, A]` row into the canonical u8 RGBA via `ya8_to_rgba_row` and
  /// filters through this same 4-channel stream, with native-Y luma on the
  /// `u8` [`Self::luma_filter_stream`] (parity with `Gray8`).
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "yuv-444-packed",
    feature = "mono",
    feature = "yuva",
    feature = "gray"
  ))]
  rgba_filter_stream: Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
  /// Row-stage **filter** stream for the high-bit packed-RGB `u16` color
  /// group ([`Rgb48`](crate::source::Rgb48), and the high-bit planar GBR
  /// sources `Gbrp9`…`Gbrp16` which scatter into the same packed `u16` RGB
  /// row, and the high-bit packed YUV 4:4:4 colour group `V410` / `V30X`,
  /// and the high-bit packed YUV 4:2:2 colour group `Y210` / `Y212` /
  /// `Y216`, which convert their YUV to a native-depth u16 RGB row) — the
  /// filter twin of [`Self::rgb_stream_u16`]. Lazily created in `process`,
  /// reset in `begin_frame`. Fed when the plan kind is `Filter`. The
  /// [`packed_rgb_u16_resample_emit`] tail clamps a signed-kernel overshoot
  /// to the source's native max `(1 << SRC_BITS) - 1` for a sub-16-bit
  /// source (`V410` / `V30X` / `Y210` are 10-bit, `Y212` 12-bit), matching
  /// the in-range area path. `yuv-planar` joins for the high-bit planar YUV
  /// family (`Yuv4{2,4}{0,2,4}p{10,12,14,16}`), which converts its separate
  /// Y/U/V planes to a native-depth u16 RGB row and filters it here — the
  /// filter twin of its area [`Self::rgb_stream_u16`] use, with the same
  /// `SRC_BITS` native-max clamp keyed by its source depth. `yuv-semi-planar`
  /// joins for the high-bit semi-planar P-format family, the filter twin of
  /// its area [`Self::rgb_stream_u16`] use.
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "yuv-planar",
    feature = "yuv-semi-planar"
  ))]
  rgb_filter_stream_u16: Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  /// Row-stage **filter** stream for the high-bit packed-RGBA `u16` color
  /// group ([`Rgba64`](crate::source::Rgba64) /
  /// [`Bgra64`](crate::source::Bgra64)) — the 4-channel, signed-coefficient
  /// twin of [`Self::rgb_filter_stream_u16`] and the `u16` analogue of
  /// [`Self::rgba_filter_stream`], fed when a real-alpha `Rgba64` / `Bgra64`
  /// source takes a `Filter` plan. PIL resizes RGBA by filtering R, G, B, A
  /// independently with no premultiplication, so the four interleaved native
  /// u16 channels bin through one 4-channel filter and a resampled RGBA frame
  /// is byte-exact (per channel) versus the merged engine. Lazily created in
  /// `process`, reset in `begin_frame`. Gated to
  /// `any(rgb, gbr, yuv-444-packed)`: besides `Rgba64` / `Bgra64`, the
  /// high-bit planar GBR+alpha family (`Gbrap10`…`Gbrap16`) de-interleaves
  /// its native-depth G/B/R/A planes into the same canonical packed RGBA
  /// u16 row and filters through this 4-channel stream (the native-max clamp
  /// keyed by the source `BITS`); the packed YUVA 4:4:4 filter route shares
  /// this `u16` colour stream for any sub-16-bit source that exposes native
  /// u16 colour outputs (the 8-bit `Vuya` / `Vuyx` do not, so they leave it
  /// unused). `yuva` joins for the 8-bit planar YUVA family (`Yuva420p` /
  /// `Yuva422p` / `Yuva444p`), which routes through the same 4:4:4 filter
  /// tail; those sources expose no u16 colour outputs either, so they leave
  /// this stream unused too. `gray` joins for the high-bit packed gray+alpha
  /// source `Ya16` (a degenerate YUVA, `R = G = B = Y`, real straight alpha):
  /// it decodes each `[Y, A]` u16 row into the canonical host-native u16 RGBA
  /// via `ya16_to_rgba_u16_row` and filters its native-depth colour through
  /// this 4-channel stream (full 16-bit, so the `FilterStream`'s `0..=65535`
  /// clamp is the native clamp), with native-Y luma on the `u16`
  /// [`Self::luma_filter_stream_u16`] (parity with `Gray16`).
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "yuv-444-packed",
    feature = "yuva",
    feature = "gray"
  ))]
  rgba_filter_stream_u16: Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  /// Row-stage **filter** stream for single-plane `u8` native-Y luma
  /// resampling — the [`SpanKind::Filter`](crate::resample::SpanKind) twin
  /// of [`Self::luma_stream`] for the 8-bit planar YUV family (`Yuv410p` /
  /// `Yuv420p` / `Yuv422p` / `Yuv444p` / `Yuv440p`). On a `Filter` plan the
  /// Y plane is filter-resampled here as a 1-channel `u8` stream, so luma
  /// stays **native Y** (never colour-derived) — byte-exact to the direct
  /// Y-plane copy's filter resample, the filter analogue of the area path's
  /// native-Y bin ([`planar_dual_resample`](planar_resample::planar_dual_resample)).
  /// These sources are 8-bit, so no native-depth clamp is needed (the
  /// stream finalizes to the full `u8` range, which *is* the native range);
  /// `luma_u16` zero-extends each resampled Y byte. Lazily created in
  /// `process`, reset in `begin_frame`. Gated to `yuv-planar` /
  /// `yuv-semi-planar` (the 8-bit semi-planar family `Nv12` / `Nv16` /
  /// `Nv21` / `Nv24` / `Nv42` bins its native Y here too) / `yuv-packed`
  /// (packed 4:2:2/4:1:1) / `mono` (the 1-bit bilevel `Monoblack` /
  /// `Monowhite` filter-resample the expanded 0/255 luma through this
  /// single-channel stream — full-range `u8`, so no native-depth clamp) /
  /// `gray` (the 8-bit single-channel [`Gray8`](crate::source::Gray8) *is*
  /// a luma plane — its Y is filter-resampled here directly, full-range
  /// `u8`, no native-depth clamp); widens as more 8-bit native-Y filter
  /// families wire in.
  #[cfg(any(
    feature = "yuv-planar",
    feature = "yuv-semi-planar",
    feature = "yuv-packed",
    feature = "mono",
    feature = "gray"
  ))]
  luma_filter_stream: Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
  /// Row-stage **filter** stream for single-plane `f32` luma binning
  /// ([`Grayf32`](crate::source::Grayf32)) — the filter twin of
  /// [`Self::luma_stream_f32`]. Lazily created in `process`, reset in
  /// `begin_frame`. Fed when the plan kind is `Filter`; bins at f32
  /// precision and emits unclamped (PIL `F`-mode).
  #[cfg(feature = "gray")]
  luma_filter_stream_f32: Option<std::boxed::Box<crate::resample::FilterStream<f32>>>,
  /// Row-stage **filter** stream for single-plane **`u16`** native-Y luma
  /// resampling — the [`SpanKind::Filter`](crate::resample::SpanKind) twin
  /// of [`Self::luma_stream_u16`] for the high-bit packed YUV 4:4:4 colour
  /// group (`V410` / `V30X`) and the high-bit packed YUV 4:2:2 colour group
  /// (`Y210` / `Y212` / `Y216`). On a `Filter` plan the native Y is
  /// de-interleaved into a source-width `u16` scratch and resampled here at
  /// native depth, so luma stays **native Y** (never colour-derived) —
  /// byte-exact to the direct `*_to_luma*` kernels' filter resample, the
  /// filter analogue of the area path's native-Y bin. Lazily created in
  /// `process`, reset in `begin_frame`. Gated to `any(yuv-444-packed,
  /// y2xx, yuv-planar, yuva)` — the high-bit planar YUV family
  /// (`Yuv4{2,4}{0,2,4}p{10,12,14,16}`) de-interleaves its native Y plane
  /// into a source-width `u16` scratch and resamples it here, the filter
  /// twin of its area native-Y bin (with the same sub-16-bit native-max
  /// clamp). `gray` joins for the high-bit single-channel
  /// [`Gray16`](crate::source::Gray16) (full 16-bit, native max == u16 max,
  /// so the `FilterStream`'s `0..=65535` clamp *is* the native clamp — no
  /// extra clamp needed). The 8-bit planar YUVA family (`Yuva420p` /
  /// `Yuva422p` / `Yuva444p`, `yuva` — which also pulls in `yuv-planar`)
  /// de-interleaves its zero-extended native Y here too (the 8-bit
  /// native-max clamp is a value no-op for in-range Y); the high-bit
  /// semi-planar P-format family (`yuv-semi-planar`) filter-resamples its
  /// de-packed native Y here (then narrows, exposing no `luma_u16`);
  /// widens as more native-Y filter families wire in.
  #[cfg(any(
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "yuv-planar",
    feature = "gray",
    feature = "yuva",
    feature = "yuv-semi-planar"
  ))]
  luma_filter_stream_u16: Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  /// Row-stage **filter** stream for single-plane **u32** luma — the
  /// signed-coefficient twin of [`Self::luma_stream_u32`] for
  /// [`Gray32`](crate::source::Gray32). Filters the native `u32` luma plane
  /// at full precision (the `f64` filter domain carries a `u32` sample
  /// exactly), then narrows each output, so the filter resample is 0-ULP
  /// versus narrowing first (closes issue #289). Lazily created in
  /// `process`, reset in `begin_frame`. Gated to `gray`.
  #[cfg(feature = "gray")]
  luma_filter_stream_u32: Option<std::boxed::Box<crate::resample::FilterStream<u32>>>,
  /// Output configuration frozen at a resampled frame's first
  /// processed row; `None` between frames. Captures presence AND
  /// attachment identity (pointer/length) of every output the emit
  /// closures consult, so both membership changes and same-channel
  /// buffer replacement trip
  /// [`MixedSinkerError::ResampleOutputsChanged`].
  #[cfg(any(
    feature = "yuv-planar",
    feature = "rgb",
    feature = "gbr",
    feature = "gray",
    feature = "xyz",
    feature = "bayer",
    feature = "mono",
    feature = "yuv-semi-planar",
    feature = "yuv-packed",
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "v210",
    feature = "rgb-legacy"
  ))]
  #[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
  resample_outputs: Option<FrozenOutputs>,
  /// Whether resampled processing may take the native decimation tier
  /// (bin native planes, convert once at output resolution). Defaults
  /// to `true`; benchmarks and differential tests flip it to force the
  /// row-stage tier — the [`Self::with_simd`] pattern. The native tier
  /// exists for the 8-bit planar 4:2:0
  /// ([`Yuv420p`](crate::source::Yuv420p)), the 8-bit semi-planar 4:2:0
  /// ([`Nv12`](crate::source::Nv12) / [`Nv21`](crate::source::Nv21)),
  /// the high-bit planar 4:2:0 family
  /// ([`Yuv420p10`](crate::source::Yuv420p10) /12/14/16), and the high-bit
  /// semi-planar 4:2:0 family ([`P010`](crate::source::P010) /
  /// [`P012`](crate::source::P012) / [`P016`](crate::source::P016)); every
  /// other routed family always takes the row-stage tier and ignores this
  /// flag. The native tier itself is compiled only under `yuv-planar` (the
  /// high-bit P-format tier reuses the planar join), so under a
  /// yuv-semi-planar-solo build the flag is accepted by the builder but
  /// never read — the P-format sinks always take the row-stage tail there.
  #[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
  #[cfg_attr(not(feature = "yuv-planar"), allow(dead_code))]
  native: bool,
  /// Native-tier join state for the 4:2:0 planar family; lazily
  /// created in `process`, reset in `begin_frame`.
  ///
  /// Boxed so the `MixedSinker` — constructed as a stack local across the
  /// crate's tests and callers — holds only a pointer for this >1 KiB join;
  /// the heap allocation still happens lazily on the first native row, so the
  /// recoverable-allocation contract is unchanged.
  #[cfg(feature = "yuv-planar")]
  native_420: Option<std::boxed::Box<planar_8bit::NativeYuv420>>,
  /// Native-tier join state for the **straight-alpha** 4:2:0 planar source
  /// `Yuva420p` (RFC #238 Phase 5 / the #235 alpha resolution) — the
  /// alpha-bearing sibling of [`Self::native_420`]: it embeds the no-alpha
  /// 4:2:0 join for Y / U / V and adds a luma-grid α [`AreaStream`] whose
  /// binned α is substituted into the RGBA output. Taken ONLY under
  /// [`AlphaMode::Straight`] (premultiplied stays on the packed-YUVA area
  /// tail); lazily created in `process`, reset in `begin_frame`.
  ///
  /// Boxed: the join embeds the full no-alpha 4:2:0 join plus an α stream
  /// (>1 KiB), so storing it inline would enlarge every `MixedSinker` — held
  /// on the stack across the crate — by that much; the box keeps the field a
  /// single pointer and the heap allocation only happens on the first native
  /// straight-alpha row.
  #[cfg(feature = "yuva")]
  native_yuva_420: Option<std::boxed::Box<planar_8bit::NativeYuva420>>,
  /// BICUBLIN per-plane filter join for the 4:2:0 planar family — the
  /// signed-coefficient twin of [`Self::native_420`]. Holds the three
  /// [`FilterStream`](crate::resample::FilterStream)s (Y with the cubic
  /// luma kernel, U / V with the linear chroma kernel) and the per-plane
  /// landing buffers; lazily created in `process` on a BICUBLIN plan,
  /// reset in `begin_frame`. Only a [`Bicublin`](crate::resample::Bicublin)
  /// plan populates it. **Boxed** — the three filter streams make this the
  /// largest single join, so keeping it behind a pointer holds the already
  /// large `MixedSinker` stack footprint down (the join is heap-allocated
  /// lazily anyway).
  #[cfg(feature = "yuv-planar")]
  bicublin_420: Option<std::boxed::Box<planar_8bit::BicublinYuv420>>,
  /// Native-tier join state for the non-4:2:0 8-bit planar families
  /// (`Yuv422p` / `Yuv444p` / `Yuv440p`) — the sibling of
  /// [`Self::native_420`] for chroma layouts that are not half-resolution
  /// in both axes. A given sink instantiates exactly one planar family, so
  /// one field is shared across the three impls; lazily created in
  /// `process`, reset in `begin_frame`.
  #[cfg(feature = "yuv-planar")]
  native_planar: Option<std::boxed::Box<planar_8bit::NativePlanarYuv>>,
  /// RGB-free YUV-domain HSV-only **area** join for the planar ROW-STAGE
  /// resample (#263 follow-up): when ONLY `with_hsv()` is attached (no RGB /
  /// RGBA), the row-stage path bins Y / U / V on their own grids straight to
  /// output resolution and converts each output row through
  /// `yuv_444_to_hsv_row` — RGB-free, and bit-identical to the native fast
  /// tier's HSV-only output. Used by the planar 8-bit families (`Yuv420p` /
  /// `Yuv410p` / `Yuv411p` / `Yuv422p` / `Yuv444p` / `Yuv440p`). The 8-bit
  /// semi-planar family (`Nv12` etc.), whose chroma arrives interleaved and
  /// whose 4:2:0 / 4:4:0 chroma-plan builders are `yuv-planar`-gated, keeps
  /// the RGB-staged HSV-only row-stage for now (a documented follow-up), so
  /// this field is `yuv-planar`-gated. Lazily created in `process` on an
  /// HSV-only row-stage row, reset in `begin_frame`. Boxed to keep the
  /// (large) `MixedSinker` stack footprint down; the heap allocation happens
  /// lazily on the first such row.
  #[cfg(feature = "yuv-planar")]
  hsv_planar: Option<std::boxed::Box<planar_resample::HsvDirectPlanarYuv>>,
  /// Native-tier join state for the HIGH-BIT planar 4:2:0 family
  /// (`Yuv420p10/12/14/16`) — the `u16` twin of [`Self::native_420`].
  /// Lazily created in `process`, reset in `begin_frame`.
  #[cfg(feature = "yuv-planar")]
  native_420_u16: Option<std::boxed::Box<subsampled_4_2_0_high_bit::NativeYuv420U16>>,
  /// Native-tier join state for the HIGH-BIT non-4:2:0 planar families
  /// (`Yuv422p10/12/14/16` / `Yuv444p10/12/14/16` / `Yuv440p10/12`) — the
  /// `u16` twin of [`Self::native_planar`] and the non-4:2:0 sibling of
  /// [`Self::native_420_u16`]. A given sink instantiates exactly one planar
  /// family, so one field is shared across the three impls; lazily created in
  /// `process`, reset in `begin_frame`.
  #[cfg(feature = "yuv-planar")]
  native_planar_u16: Option<std::boxed::Box<planar_high_bit_native::NativePlanarYuvU16>>,
  /// De-pack staging for the native fast tier of the **MSB-aligned high-bit
  /// planar** 4:4:4 family ([`Yuv444p10Msb`](crate::source::Yuv444p10Msb) /
  /// [`Yuv444p12Msb`](crate::source::Yuv444p12Msb)). Each plane is
  /// high-bit-packed (`logical << (16 - BITS)`); the native wrapper
  /// wire-decodes (`from_le` / `from_be`) and DE-PACKS (`>> (16 - BITS)`) each
  /// of Y / U / V into these host-native LOGICAL `u16` scratches before the
  /// reused HIGH-BIT non-4:2:0 planar join
  /// ([`planar_high_bit_native::yuv_planar16_process_native`]) bins them at
  /// `Yuv444p` geometry (the MSB twin of the low-bit family, which feeds the
  /// join directly — no de-pack). `yuv444_msb_y_depack` grows to `width` on
  /// every native row; `yuv444_msb_u_depack` / `yuv444_msb_v_depack` grow to
  /// `width` each only on a colour native row (4:4:4 chroma is full-width);
  /// empty otherwise.
  #[cfg(feature = "yuv-planar")]
  yuv444_msb_y_depack: Vec<u16>,
  /// U-plane de-pack scratch for the native MSB-aligned planar 4:4:4 tier;
  /// twin of [`Self::yuv444_msb_y_depack`].
  #[cfg(feature = "yuv-planar")]
  yuv444_msb_u_depack: Vec<u16>,
  /// V-plane de-pack scratch for the native MSB-aligned planar 4:4:4 tier;
  /// twin of [`Self::yuv444_msb_u_depack`].
  #[cfg(feature = "yuv-planar")]
  yuv444_msb_v_depack: Vec<u16>,
  /// Half-width U / V de-interleave staging for the native 4:2:0
  /// decimation tier of the **semi-planar** family
  /// ([`Nv12`](crate::source::Nv12) / [`Nv21`](crate::source::Nv21)):
  /// the interleaved chroma row splits into these two `width / 2`
  /// scratch planes so [`planar_8bit::yuv420p_process_native`] bins
  /// Y + U + V through the same per-plane join the planar twin uses.
  /// Lazily grown to `width / 2` `u8` each on the first chroma-bearing
  /// native row; empty otherwise. Gated to the intersection — the
  /// native tier reuses the planar join, so it only exists when
  /// `yuv-planar` is also compiled.
  #[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
  semi_planar_u_half: Vec<u8>,
  /// V-plane twin of [`Self::semi_planar_u_half`].
  #[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
  semi_planar_v_half: Vec<u8>,
  /// De-pack staging for the native 4:2:0 decimation tier of the
  /// **high-bit semi-planar** P-format family
  /// ([`P010`](crate::source::P010) / [`P012`](crate::source::P012) /
  /// [`P016`](crate::source::P016)). The P-format Y plane is
  /// high-bit-packed (`logical << (16 - BITS)`) and the chroma plane is
  /// interleaved + high-packed; the native wrapper de-interleaves + DE-PACKS
  /// each wire plane into these host-native LOGICAL `u16` scratches before
  /// the reused planar high-bit join ([`subsampled_4_2_0_high_bit::yuv420p16_process_native`])
  /// bins Y + U + V. `p0xx_y_half` grows to `width` on every native row;
  /// `p0xx_u_half` / `p0xx_v_half` grow to `width / 2` each only on a
  /// chroma-bearing native row; empty otherwise. Gated to the intersection
  /// — the native tier reuses the planar join, so it only exists when
  /// `yuv-planar` is also compiled.
  #[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
  p0xx_y_half: Vec<u16>,
  /// U-plane de-pack scratch for the native high-bit semi-planar tier;
  /// twin of [`Self::p0xx_y_half`] at chroma width.
  #[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
  p0xx_u_half: Vec<u16>,
  /// V-plane de-pack scratch for the native high-bit semi-planar tier;
  /// twin of [`Self::p0xx_u_half`].
  #[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
  p0xx_v_half: Vec<u16>,
  /// De-pack staging for the native fast tier of the **packed** 8-bit
  /// 4:2:2 YUV family ([`Yuyv422`](crate::source::Yuyv422) /
  /// [`Uyvy422`](crate::source::Uyvy422) / [`Yvyu422`](crate::source::Yvyu422)).
  /// Each format carries one fully-interleaved plane (`Y U Y V …` etc., two
  /// bytes per pixel); the native wrapper de-packs each packed row into
  /// these separate Y (`width`) / U (`width / 2`) / V (`width / 2`)
  /// scratch planes at the per-format byte offsets, then the reused planar
  /// 8-bit join ([`planar_8bit::yuv_planar_process_native`]) bins Y + U + V
  /// at `Yuv422p` geometry. `packed_yuv_y_full` grows to `width` on every
  /// native row; `packed_yuv_u_half` / `packed_yuv_v_half` grow to
  /// `width / 2` each only on a colour native row; empty otherwise. Gated
  /// to the intersection — the native tier reuses a yuv-planar fn, so it
  /// only exists when `yuv-planar` is also compiled (a `yuv-packed`-solo
  /// build takes the row-stage tail).
  #[cfg(all(feature = "yuv-packed", feature = "yuv-planar"))]
  packed_yuv_y_full: Vec<u8>,
  /// U-plane de-pack scratch for the native packed 4:2:2 tier; twin of
  /// [`Self::packed_yuv_y_full`] at chroma width (`width / 2`).
  #[cfg(all(feature = "yuv-packed", feature = "yuv-planar"))]
  packed_yuv_u_half: Vec<u8>,
  /// V-plane de-pack scratch for the native packed 4:2:2 tier; twin of
  /// [`Self::packed_yuv_u_half`].
  #[cfg(all(feature = "yuv-packed", feature = "yuv-planar"))]
  packed_yuv_v_half: Vec<u8>,
  /// De-pack staging for the native fast tier of the **high-bit packed**
  /// 4:2:2 YUV family ([`Y210`](crate::source::Y210) /
  /// [`Y212`](crate::source::Y212) / [`Y216`](crate::source::Y216)). Each
  /// format carries one fully-interleaved plane of MSB-aligned u16 words in
  /// YUYV order (`Y₀ U Y₁ V …`, four words per 2-pixel group); the native
  /// wrapper wire-decodes (`from_le` / `from_be`), de-packs (`>> (16 - BITS)`),
  /// and de-interleaves each packed row into these separate host-native
  /// LOGICAL Y (`width`) / U (`width / 2`) / V (`width / 2`) scratch planes,
  /// then the reused HIGH-BIT non-4:2:0 planar join
  /// ([`planar_high_bit_native::yuv_planar16_process_native`]) bins Y + U + V
  /// at `Yuv422p` geometry. `y2xx_y_full` grows to `width` on every native
  /// row; `y2xx_u_half` / `y2xx_v_half` grow to `width / 2` each only on a
  /// colour native row; empty otherwise. The `u16` analog of
  /// [`Self::packed_yuv_y_full`] (the 8-bit packed tier) combined with the
  /// MSB-bridge the high-bit semi-planar tier ([`Self::p0xx_y_half`]) carries.
  /// Gated to the intersection — the native tier reuses a yuv-planar fn, so it
  /// only exists when `yuv-planar` is also compiled (a `y2xx`-solo build takes
  /// the row-stage tail).
  #[cfg(all(feature = "y2xx", feature = "yuv-planar"))]
  y2xx_y_full: Vec<u16>,
  /// U-plane de-pack scratch for the native high-bit packed 4:2:2 tier; twin
  /// of [`Self::y2xx_y_full`] at chroma width (`width / 2`).
  #[cfg(all(feature = "y2xx", feature = "yuv-planar"))]
  y2xx_u_half: Vec<u16>,
  /// V-plane de-pack scratch for the native high-bit packed 4:2:2 tier; twin
  /// of [`Self::y2xx_u_half`].
  #[cfg(all(feature = "y2xx", feature = "yuv-planar"))]
  y2xx_v_half: Vec<u16>,
  /// De-pack staging for the native fast tier of the **exotic 10-bit packed
  /// 4:2:2** YUV format ([`V210`](crate::source::V210)). V210 packs 12 × 10-bit
  /// samples per 16-byte word (four 32-bit LE/BE words) = 6 pixels (6 Y + 3 Cb +
  /// 3 Cr); the native wrapper wire-decodes (`from_le` / `from_be`) and
  /// bit-extracts each word into these separate host-native LOGICAL Y (`width`)
  /// / U (`width / 2`) / V (`width / 2`) scratch planes, then the reused HIGH-BIT
  /// non-4:2:0 planar join
  /// ([`planar_high_bit_native::yuv_planar16_process_native`]) bins Y + U + V at
  /// `Yuv422p10` geometry. `v210_y_full` grows to `width` on every native row;
  /// `v210_u_half` / `v210_v_half` grow to `width / 2` each only on a colour
  /// native row; empty otherwise. The fixed-`BITS = 10` sibling of
  /// [`Self::y2xx_y_full`] (the high-bit packed 4:2:2 tier), with the V210 word
  /// packing in place of the YUYV MSB-aligned u16 words. Gated to the
  /// intersection — the native tier reuses a yuv-planar fn, so it only exists
  /// when `yuv-planar` is also compiled (a `v210`-solo build takes the row-stage
  /// tail).
  #[cfg(all(feature = "v210", feature = "yuv-planar"))]
  v210_y_full: Vec<u16>,
  /// U-plane de-pack scratch for the native V210 tier; twin of
  /// [`Self::v210_y_full`] at chroma width (`width / 2`).
  #[cfg(all(feature = "v210", feature = "yuv-planar"))]
  v210_u_half: Vec<u16>,
  /// V-plane de-pack scratch for the native V210 tier; twin of
  /// [`Self::v210_u_half`].
  #[cfg(all(feature = "v210", feature = "yuv-planar"))]
  v210_v_half: Vec<u16>,
  /// De-pack staging for the native fast tier of the **8-bit packed 4:4:4**
  /// non-alpha YUV format ([`Vuyx`](crate::source::Vuyx) — bytes `V U Y X`
  /// per pixel, `X` padding). The native wrapper de-packs each packed row
  /// into these separate Y / U / V scratch planes — all FULL width (`width`),
  /// since 4:4:4 has no chroma subsampling — then the reused planar 8-bit join
  /// ([`planar_8bit::yuv_planar_process_native`]) bins Y + U + V at
  /// [`Yuv444p`](crate::source::Yuv444p) geometry (`chroma_w = width`,
  /// `chroma_vsub = 1`). `packed_444_y_full` grows to `width` on every native
  /// row; `packed_444_u_full` / `packed_444_v_full` grow to `width` each only
  /// on a colour native row; empty otherwise. The 4:4:4 (full-width chroma)
  /// analog of [`Self::packed_yuv_y_full`] (the 8-bit packed 4:2:2 tier).
  /// Gated to the intersection — the native tier reuses a yuv-planar fn, so it
  /// only exists when `yuv-planar` is also compiled (a `yuv-444-packed`-solo
  /// build takes the row-stage tail).
  #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
  packed_444_y_full: Vec<u8>,
  /// U-plane de-pack scratch for the native 8-bit packed 4:4:4 tier; twin of
  /// [`Self::packed_444_y_full`] at FULL chroma width (`width`).
  #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
  packed_444_u_full: Vec<u8>,
  /// V-plane de-pack scratch for the native 8-bit packed 4:4:4 tier; twin of
  /// [`Self::packed_444_u_full`].
  #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
  packed_444_v_full: Vec<u8>,
  /// De-pack staging for the native fast tier of the **high-bit packed 4:4:4**
  /// non-alpha YUV formats ([`V410`](crate::source::V410) — one 10-bit
  /// bit-field-packed u32 word per pixel; [`Xv36`](crate::source::Xv36) — four
  /// MSB-aligned 12-bit u16 slots `U Y V A` per pixel, `A` padding). Each
  /// wrapper bit-extracts / de-packs its OWN wire layout into these separate
  /// host-native LOGICAL u16 Y / U / V scratch planes — all FULL width
  /// (`width`), since 4:4:4 has no chroma subsampling — then the reused HIGH-BIT
  /// non-4:2:0 planar join
  /// ([`planar_high_bit_native::yuv_planar16_process_native`]) bins Y + U + V at
  /// [`Yuv444p10`](crate::source::Yuv444p10) /
  /// [`Yuv444p12`](crate::source::Yuv444p12) geometry (`chroma_w = width`,
  /// `chroma_vsub = 1`). `packed_444_y_full_u16` grows to `width` on every
  /// native row; `packed_444_u_full_u16` / `packed_444_v_full_u16` grow to
  /// `width` each only on a colour native row; empty otherwise. A given sink
  /// instantiates exactly one format, so V410 and Xv36 (both high-bit 4:4:4)
  /// share these fields. The 4:4:4 (full-width chroma) analog of
  /// [`Self::y2xx_y_full`] (the high-bit packed 4:2:2 tier). Gated to the
  /// intersection — the native tier reuses a yuv-planar fn, so it only exists
  /// when `yuv-planar` is also compiled.
  #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
  packed_444_y_full_u16: Vec<u16>,
  /// U-plane de-pack scratch for the native high-bit packed 4:4:4 tier; twin of
  /// [`Self::packed_444_y_full_u16`] at FULL chroma width (`width`).
  #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
  packed_444_u_full_u16: Vec<u16>,
  /// V-plane de-pack scratch for the native high-bit packed 4:4:4 tier; twin of
  /// [`Self::packed_444_u_full_u16`].
  #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
  packed_444_v_full_u16: Vec<u16>,
  /// The native / row-stage route chosen on the first resampled row of a
  /// frame; a mid-frame change is rejected. The two tiers carry
  /// independent, in-order stream state, so flipping
  /// [`Self::set_native`] mid-frame would split one frame across two
  /// incompatible state machines. Shared by every native 4:2:0 family
  /// the guard covers — the 8-bit planar (Yuv420p) and high-bit planar
  /// (Yuv420p10/12/14/16) tiers (both `yuv-planar`), the 8-bit
  /// semi-planar (Nv12/Nv21) tier, and the high-bit semi-planar P-format
  /// (P010/P012/P016) tier (both additionally `yuv-semi-planar`, a subset
  /// of `yuv-planar`). Reset to `None` per frame: via
  /// `reset_high_bit_yuv_streams` for the high-bit families and inline in
  /// the 8-bit families' `begin_frame`. Gated to `yuv-planar`, the union
  /// where the native tier exists (a yuv-semi-planar-solo build can't
  /// enable the native tier, so no guard is needed there).
  #[cfg(feature = "yuv-planar")]
  frozen_native_route: Option<bool>,
  /// The [`AveragingDomain`] captured on the first output-bearing resampled row
  /// of a frame; a mid-frame change is rejected. The domain selects the pipeline
  /// stage the area average is spliced at (encoded codes vs linear light vs
  /// premultiplied), so flipping [`Self::set_averaging_domain`] mid-frame would
  /// split one frame across two incompatible averaging stages. Completes the
  /// per-frame freeze family (output set, native route, transfer, domain). Reset
  /// to `None` per frame in the planar 8-bit families' `begin_frame` (the only
  /// formats whose dispatch reads the configured domain). Gated to `yuv-planar`,
  /// where that dispatch — and so the only reader of this field — lives; the
  /// high-bit / semi-planar / packed dispatches splice `AveragingDomain::Encoded`
  /// as a literal and never consult it.
  ///
  /// [`AveragingDomain`]: crate::resample::AveragingDomain
  #[cfg(feature = "yuv-planar")]
  frozen_domain: Option<crate::resample::AveragingDomain>,
  /// RFC #238: the effective 4:2:2 chroma sampling phase (`true` =
  /// centered, `false` = co-sited) frozen on the first output-bearing
  /// row of the current frame. A later row observing a different siting
  /// is rejected with [`MixedSinkerError::ChromaSitingChanged`] so the
  /// frame never bins a mixture of phases; cleared each `begin_frame`.
  #[cfg(feature = "yuv-planar")]
  frozen_chroma_centered: Option<bool>,
  /// RFC #238 S4-B companion to [`Self::frozen_chroma_centered`]: the effective
  /// 4:2:0 **vertical** chroma phase (`true` = `Bottom` `v = 1`, `false` =
  /// co-sited vertical) frozen on the first output-bearing row of the current
  /// frame. `Center` and `Bottom` are BOTH horizontally centered, so the
  /// centered flag alone cannot tell them apart; freezing the vertical phase
  /// too rejects a mid-frame `Center` ⇆ `Bottom` flip with
  /// [`MixedSinkerError::ChromaSitingChanged`]. Only the `Yuv420p` dispatch
  /// (the sole vertical-siting reader) sets it; every other format leaves it
  /// `None` and passes `v_phase = 0`. Cleared each `begin_frame`.
  #[cfg(feature = "yuv-planar")]
  frozen_chroma_bottom_v: Option<bool>,
  /// RFC #238 Top companion to [`Self::frozen_chroma_bottom_v`]: the effective
  /// 4:2:0 **Top** vertical phase (`true` = `Top` / `TopLeft` `v = 0` forward
  /// fold, `false` otherwise) frozen on the first output-bearing row of the
  /// current frame. `Center` and `Top` are BOTH horizontally centered and BOTH
  /// carry `frozen_chroma_bottom_v = Some(false)`, so neither prior flag can
  /// tell them apart; freezing the Top phase too rejects a mid-frame
  /// `Center`/`Bottom` ⇆ `Top` flip with
  /// [`MixedSinkerError::ChromaSitingChanged`]. Only the `Yuv420p` dispatch sets
  /// it; every other format leaves it `None`. Cleared each `begin_frame`.
  #[cfg(feature = "yuv-planar")]
  frozen_chroma_top_v: Option<bool>,
  /// Forward one-row output-delay state for the 4:2:0 **Top** (`v = 0`) identity
  /// decode (RFC #238) — the mirror of the backward [`Self::chroma_prev`]
  /// lookback `Bottom` uses. A `Top` chroma sample sits on the TOP luma row of
  /// each pair, so an ODD output luma row `2i+1` needs the *next* chroma row
  /// `c[i+1]`, which the row-at-a-time stream has not been fed when `2i+1`
  /// arrives. Its colour output is therefore HELD here until the following even
  /// row `2i+2` (carrying `c[i+1]`) arrives, then box-blended and emitted.
  /// `Some((row, matrix, full_range))` records the deferred odd row's index and
  /// its decode parameters; `None` when nothing is pending. Reset to `None` each
  /// `begin_frame` so a held row never leaks across frames. Gated to
  /// `yuv-planar`, like [`Self::chroma_prev`].
  #[cfg(feature = "yuv-planar")]
  chroma_top_pending: Option<(usize, crate::KernelMatrix, bool)>,
  /// ST 428-1 colorimetry frozen alongside [`Self::chroma_top_pending`] for the
  /// 8-bit `Yuv420p` **Top** forward-delay identity decode (#310): the source
  /// primaries and [`St428Interpretation`] active when the deferred odd row was
  /// SUBMITTED. The identity flush decodes the held row through these frozen
  /// values — not the sink's current [`Self::primaries`] /
  /// [`Self::st428_interpretation`] — so a mid-frame `set_color_spec` /
  /// `set_st428_interpretation` cannot change how an already-accepted row
  /// decodes, and re-applies [`st428_chroma_derived_guard`] to them so the
  /// delayed path is guarded exactly like the in-order one. Set in lockstep with
  /// `chroma_top_pending` at the identity defer, cleared at its flush, and reset
  /// to `None` each `begin_frame`; `None` for every non-`Top` row. Gated to
  /// `yuv-planar`, like `chroma_top_pending`.
  #[cfg(feature = "yuv-planar")]
  chroma_top_st428: Option<(crate::Primaries, St428Interpretation)>,
  /// Buffered Y row for the deferred [`Self::chroma_top_pending`] odd output row
  /// — a copy of that row's `width` `u8` luma, retained because the walker's row
  /// borrow is invalidated after `process` returns. Lazily grown to `width` `u8`
  /// on the first deferred `Top` row; empty otherwise. Gated to `yuv-planar`.
  #[cfg(feature = "yuv-planar")]
  chroma_top_y: Vec<u8>,
  /// Buffered **alpha** row for the deferred [`Self::chroma_top_pending`] odd
  /// output row on the 8-bit YUVA 4:2:0 **Top** (`v = 0`) forward one-row delay
  /// (RFC #238) — the alpha companion of [`Self::chroma_top_y`]. The 8-bit
  /// `Yuva420p` carries a full-resolution `u8` alpha plane; the Top forward delay
  /// defers the odd row's whole decode, so its alpha is held here alongside its
  /// luma until the following even row flushes it. Lazily grown to `width` `u8`
  /// on the first deferred `Top` row; empty otherwise. Gated to `yuva` — its
  /// only reader is the `Yuva420p` sink.
  #[cfg(feature = "yuva")]
  chroma_top_a: Vec<u8>,
  /// Lazily grown to `3 * width` bytes when HSV is requested without a
  /// user RGB buffer. Empty otherwise.
  ///
  /// Consumed by per-format `process` impls that derive HSV from RGB
  /// via the lazy scratch path. Under `--features "alloc"` alone (no
  /// per-format family), no `process` impl reads this field, so the
  /// cfg enumerates every source family.
  #[cfg(any(
    feature = "bayer",
    feature = "gbr",
    feature = "gray",
    feature = "mono",
    feature = "rgb",
    feature = "rgb-float",
    feature = "rgb-legacy",
    feature = "v210",
    feature = "xyz",
    feature = "y2xx",
    feature = "yuv-444-packed",
    feature = "yuv-packed",
    feature = "yuv-planar",
    feature = "yuv-semi-planar",
    feature = "yuva",
  ))]
  rgb_scratch: Vec<u8>,
  /// Full-width `u8` chroma staging for the siting-aware 4:2:0 upsample
  /// (#302). Holds the horizontally upsampled U then V planes back-to-back
  /// (`2 * width` bytes: `[0..width]` = U, `[width..2*width]` = V) so the
  /// centered-siting identity path can reuse the 4:4:4 decode kernels. Shared
  /// by the planar `Yuv420p` and the semi-planar `Nv12` / `Nv21` decodes (the
  /// latter de-interleave the interleaved UV into the half-width
  /// [`Self::semi_planar_u_half`] / `_v_half` scratch first, then upsample into
  /// this buffer). Lazily grown to `2 * width` `u8` on the first centered-siting
  /// chroma row; empty otherwise (the default left/unspecified siting never
  /// touches it). Gated to `yuv-planar` (the 4:4:4 kernels + scalar upsample it
  /// reuses live there; the semi-planar path additionally requires
  /// `yuv-semi-planar`).
  #[cfg(feature = "yuv-planar")]
  chroma_full: Vec<u8>,
  /// One-row chroma **lookback** for the bottom-sited (`AVCHROMA_LOC_BOTTOM`)
  /// vertical-phase 4:2:0 upsample (#302). Holds a copy of a chroma row's
  /// half-width U then V back-to-back (`width` `u8`: `[0..width/2]` = `U`,
  /// `[width/2..width]` = `V`), so an *even* output luma row `2i` can vertically
  /// box-blend chroma rows `i-1` (here) and `i` (the current row) before the
  /// horizontal centered upsample. WHICH chroma row this buffer currently holds
  /// is tracked by [`Self::chroma_prev_row`]; the blend only trusts it when it
  /// holds exactly the wanted predecessor `i-1`, otherwise it falls back to the
  /// top-edge clamp (current row as its own predecessor) — so a direct
  /// `process` caller that replays / skips / reorders rows or attaches colour
  /// late can never blend STALE chroma from an older pair or a previous frame.
  /// Lazily grown to `width` `u8` on the first bottom-sited chroma row; empty
  /// otherwise (only `ChromaLocation::Bottom` touches it — every other siting,
  /// the default included, keeps the vertical-replicate decode). Gated to
  /// `yuv-planar`, like [`Self::chroma_full`] which it feeds.
  #[cfg(feature = "yuv-planar")]
  chroma_prev: Vec<u8>,
  /// Which chroma row [`Self::chroma_prev`] currently holds, or `None` when it
  /// holds nothing trustworthy (`MixedSinker::new`, after every
  /// [`begin_frame`](crate::PixelSink::begin_frame), and before the first
  /// bottom-sited row) — the validity tag that makes the bottom-sited
  /// vertical-phase lookback row-order-safe (#302). The walker feeds rows in
  /// order so it tracks `idx / 2`, but a direct `process` caller can break that
  /// sequence; the bottom even-row blend at pair `k` checks this equals
  /// `Some(k - 1)` and clamps (current row as its own predecessor) when it does
  /// not, never blending stale data. Reset to `None` in `begin_frame` so no
  /// stale row leaks across frames. Gated to `yuv-planar`, like
  /// [`Self::chroma_prev`].
  #[cfg(feature = "yuv-planar")]
  chroma_prev_row: Option<usize>,
  /// Full-width `u16` chroma staging for the **high-bit** siting-aware upsample
  /// (#302) — the `u16` twin of [`Self::chroma_full`]. Shared by the planar
  /// `Yuv420p9` … `Yuv420p16` / `Yuv422p9` … `Yuv422p16` decodes (which lay it
  /// out as the horizontally upsampled U then V planes back-to-back:
  /// `[0..width]` = U, `[width..2*width]` = V) AND the semi-planar P-format
  /// `P010` / `P012` / `P016` (4:2:0) / `P210` / `P212` / `P216` (4:2:2) decodes
  /// (which lay it out as a full-width INTERLEAVED `U V U V…` plane). Either way
  /// it is `2 * width` `u16` in the source's wire byte order, so the
  /// centered-siting identity path can reuse the high-bit 4:4:4 decode kernels.
  /// Lazily grown to `2 * width` `u16` on the first centered-siting chroma row;
  /// empty otherwise (the default left/unspecified siting never touches it).
  /// Gated to `yuv-planar` (the high-bit 4:4:4 kernels + `u16` scalar upsample
  /// it reuses live there).
  #[cfg(feature = "yuv-planar")]
  chroma_full_u16: Vec<u16>,
  /// One-row chroma **lookback** for the bottom-sited (`AVCHROMA_LOC_BOTTOM`)
  /// vertical-phase HIGH-BIT 4:2:0 upsample (RFC #238 S6d/S6e) — the `u16` twin
  /// of [`Self::chroma_prev`]. Holds a copy of one chroma row (`width` `u16`, in
  /// the source's wire byte order) so an *even* output luma row `2i` can
  /// vertically box-blend chroma rows `i-1` (here) and `i` (the current row)
  /// before the horizontal centered upsample. The planar `Yuv420p9` …
  /// `Yuv420p16` sinks lay it out as the half-width U then V back-to-back
  /// (`[0..width/2]` = `U`, `[width/2..width]` = `V`, RFC #238 S6d); the
  /// semi-planar `P010` / `P012` / `P016` sinks lay it out as the packed
  /// INTERLEAVED `U V U V…` half-row (RFC #238 S6e) — the SAME `width` `u16`,
  /// whichever layout its owning monomorphized sink writes. WHICH chroma row it
  /// holds is tracked by the SHARED [`Self::chroma_prev_row`] tag
  /// (element-agnostic, so the `u8` and `u16` bottom paths — never both live on
  /// one monomorphized sink — reuse it); the blend trusts it only for the wanted
  /// predecessor `i-1`, else falls back to the top-edge clamp. Lazily grown to
  /// `width` `u16` on the first bottom-sited chroma row; empty otherwise (only
  /// `ChromaLocation::Bottom` on a `Yuv420p9` … `Yuv420p16` / `P010` / `P012` /
  /// `P016` sink touches it). Gated to `yuv-planar`, like
  /// [`Self::chroma_full_u16`] which it feeds.
  #[cfg(feature = "yuv-planar")]
  chroma_prev_u16: Vec<u16>,
  /// Buffered wire-format Y row for the deferred [`Self::chroma_top_pending`] odd
  /// output row on the HIGH-BIT 4:2:0 **Top** (`v = 0`) forward one-row delay
  /// (RFC #238) — the `u16` twin of [`Self::chroma_top_y`]. Holds a copy of the
  /// odd row's `width` `u16` luma in the source's wire byte order (the walker's
  /// row borrow is invalidated after `process` returns), retained until the
  /// following even row flushes it. Shared by the planar `Yuv420p9` …
  /// `Yuv420p16` identity decode and the row-stage / filter reconstruction
  /// tiers. Lazily grown to `width` `u16` on the first deferred `Top` row; empty
  /// otherwise. Gated to `yuv-planar`, like [`Self::chroma_prev_u16`].
  #[cfg(feature = "yuv-planar")]
  chroma_top_y_u16: Vec<u16>,
  /// Buffered wire-format **alpha** row for the deferred
  /// [`Self::chroma_top_pending`] odd output row on the HIGH-BIT 4:2:0 YUVA
  /// **Top** (`v = 0`) forward one-row delay (RFC #238) — the alpha companion of
  /// [`Self::chroma_top_y_u16`]. YUVA carries a FULL-RESOLUTION alpha plane
  /// (never chroma-subsampled) alongside Y; the Top forward delay defers the odd
  /// row's whole decode, so its alpha — like its luma — must be held until the
  /// following even row flushes it (the walker's row borrow expires after
  /// `process` returns). Holds a copy of the odd row's `width` `u16` alpha in the
  /// source's wire byte order. Only a `Yuva420p9` … `Yuva420p16` sink under
  /// `Top` / `TopLeft` touches it; lazily grown to `width` `u16` on the first
  /// deferred `Top` row, empty otherwise. Gated to `yuva` — its only readers
  /// are the high-bit `Yuva420p` sinks.
  #[cfg(feature = "yuva")]
  chroma_top_a_u16: Vec<u16>,
  /// Source-width `u8` luma staging for the **packed YUV 4:2:2** resample
  /// path (the interleaved Y bytes are de-interleaved here via the format's
  /// own `*_to_luma_row` kernel — the exact Y→luma derivation the direct
  /// path uses) and the `Ya8` gray+alpha resample (the native Y bytes of
  /// the packed `[Y, A]` row are de-interleaved here via `ya8_to_luma_row`)
  /// before feeding the single-channel [`Self::luma_stream`]. The colour
  /// stream simultaneously stages its RGB / RGBA row in
  /// [`Self::rgb_scratch`] / [`Self::rgba_scratch`], so the Y row needs its
  /// own buffer rather than sharing that scratch. Lazily grown to `width`
  /// `u8`; empty otherwise. Gated to `yuv-packed` / `gray`.
  #[cfg(any(feature = "yuv-packed", feature = "gray"))]
  luma_scratch: Vec<u8>,
  /// Source-width **native-channel** `u8` staging for the legacy 16-bit
  /// packed-RGB ([`Rgb565`](crate::source::Rgb565) and family) resample
  /// path: each packed source row is unpacked to its 3 **native** R/G/B
  /// channels (5/6/5, 5/5/5 or 4/4/4 values, each `<= 63`, NOT expanded
  /// to 8-bit) here before feeding the shared u8 [`Self::rgb_stream`], so
  /// the area mean is taken at native depth. Lazily grown to `3 * width`
  /// `u8`; empty otherwise. Gated to `rgb-legacy`.
  #[cfg(feature = "rgb-legacy")]
  legacy_rgb_native_scratch: Vec<u8>,
  /// Out-width **re-packed source-format** `u8` staging for the legacy
  /// 16-bit packed-RGB resample tail. Per finalized output row the binned
  /// native R/G/B channels are re-packed back into the source's packed
  /// `u16` word (LE bytes, `2 * out_width`) here, then the **exact**
  /// direct `*_to_*` kernels run over it — so every output is
  /// byte-identical to a direct conversion of the area-downscaled
  /// source-format frame. The integer twin of the `gbr` family's
  /// [`Self::rgb_plane_scratch_f32`] de-interleave staging. Lazily grown
  /// to `2 * out_width` `u8`; empty for a no-output sink. Gated to
  /// `rgb-legacy`.
  #[cfg(feature = "rgb-legacy")]
  legacy_rgb_packed_scratch: Vec<u8>,
  /// Source-width `u16` RGB staging for high-bit packed-RGB resampling:
  /// the wire row converts here before feeding [`Self::rgb_stream_u16`].
  /// Lazily grown to `3 * width` `u16`; empty otherwise. Gated to `rgb`
  /// (high-bit packed RGB), `gbr` (high-bit planar GBR scatters its
  /// G/B/R planes here before the same u16 tail), the high-bit packed
  /// YUV color groups (`yuv-444-packed` / `y2xx` / `v210`), the
  /// high-bit planar YUV color groups (`yuv-planar`: Yuv444p / Yuv422p
  /// 10/12/14/16), and the high-bit semi-planar P-format color groups
  /// (`yuv-semi-planar`) which stage their converted native-u16 RGB row
  /// here.
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "gray",
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "v210",
    feature = "yuv-planar",
    feature = "yuv-semi-planar"
  ))]
  rgb_scratch_u16: Vec<u16>,
  /// Source-width canonical `R, G, B, A` `u8` staging for the alpha-aware
  /// u8-color resample tails: each source row is converted to canonical
  /// RGBA (`Rgba` identity, `Bgra` swap, `Argb` / `Abgr` rotate α to slot
  /// 3; `Gbrap` de-interleaves its G/B/R/A planes; `Ya8` replicates Y;
  /// `Vuya` / `Ayuv64` run the u8 `YUV→RGB` kernel with source α) here —
  /// and, under [`AlphaMode::Premultiplied`], premultiplied in place —
  /// before feeding the 4-channel [`Self::rgba_stream`]. Lazily grown to
  /// `4 * width` `u8`; empty otherwise. Gated to `rgb` / `gbr` / `gray` /
  /// `mono` / `yuv-444-packed`. (`mono` joins for `Pal8`, which stages its
  /// per-pixel palette lookup `[R, G, B, A]` here before binning.)
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "gray",
    feature = "mono",
    feature = "yuva",
    feature = "yuv-444-packed"
  ))]
  rgba_scratch: Vec<u8>,
  /// Source-width canonical `R, G, B, A` host-native `u16` staging for the
  /// alpha-aware native-u16-color resample tails (`Rgba64` / `Bgra64`, the
  /// high-bit planar GBR+alpha family `Gbrap10` … `Gbrap16`, `Ya16`, and
  /// the packed YUVA `Ayuv64`'s independent u16 color group): the wire row
  /// converts to host-native u16 RGBA here (and is premultiplied in place
  /// under [`AlphaMode::Premultiplied`]) before feeding the 4-channel
  /// [`Self::rgba_stream_u16`]. Lazily grown to `4 * width` `u16`; empty
  /// otherwise. Gated to `rgb` / `gbr` / `gray` / `yuv-444-packed`.
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "gray",
    feature = "yuva",
    feature = "yuv-444-packed"
  ))]
  rgba_scratch_u16: Vec<u16>,
  /// Out-width host-native straight `R, G, B, A` `u16` staging for the
  /// native-u16-color resample tails: per finalized output row the binned
  /// native RGBA is resolved to its straight form here (a copy in
  /// [`AlphaMode::Straight`], an un-premultiply in
  /// [`AlphaMode::Premultiplied`]), then every output derives from this
  /// single straight row (the high-bit packed RGBA tail's narrowed u8
  /// outputs and `Ayuv64`'s u16 color group's rgb_u16 / rgba_u16). Lazily
  /// grown to `4 * out_width` `u16`; empty for an output-less sink. Gated
  /// to `rgb` / `gbr` / `gray` / `yuv-444-packed`.
  #[cfg(any(
    feature = "rgb",
    feature = "gbr",
    feature = "gray",
    feature = "yuva",
    feature = "yuv-444-packed"
  ))]
  rgba_color_scratch_u16: Vec<u16>,
  /// Source-width host-native `R, G, B` `u32` staging (`3 * width`) for the
  /// native-`u32` packed-RGB resample tier: the wire row converts here (the
  /// `BE` swap only — NO depth narrow) before feeding [`Self::rgb_stream_u32`]
  /// / [`Self::rgb_filter_stream_u32`], so binning runs at full `u32` precision
  /// (0-ULP, issue #289). Lazily grown; empty otherwise. Gated to `rgb` / `gbr`.
  #[cfg(any(feature = "rgb", feature = "gbr"))]
  rgb_scratch_u32: Vec<u32>,
  /// Source-width canonical host-native `R, G, B, A` `u32` staging (`4 * width`)
  /// for the native-`u32` alpha-aware packed-RGBA resample tier (and
  /// premultiplied in place at `u32` under
  /// [`AlphaMode::Premultiplied`]) before feeding [`Self::rgba_stream_u32`] /
  /// [`Self::rgba_filter_stream_u32`]. Lazily grown; empty otherwise. Gated to
  /// `rgb` / `gbr`.
  #[cfg(any(feature = "rgb", feature = "gbr"))]
  rgba_scratch_u32: Vec<u32>,
  /// Out-width straight `R, G, B, A` `u32` staging (`4 * out_width`) for the
  /// native-`u32` packed-RGBA area tier: per finalized output row the binned
  /// premultiplied `u32` RGBA is un-premultiplied here (the straight path reads
  /// the binned row directly), then narrowed `>> 16` and derived. Lazily grown;
  /// empty for a straight-alpha or output-less sink. Gated to `rgb` / `gbr`.
  #[cfg(any(feature = "rgb", feature = "gbr"))]
  rgba_color_scratch_u32: Vec<u32>,
  /// Source-width host-native `u16` luma staging for the
  /// [`Gray16`](crate::source::Gray16) resample path: the wire `Gray16`
  /// row converts here (source wire `BE` → host-native u16, the same
  /// kernel the direct `luma_u16` path uses) before feeding
  /// [`Self::luma_stream_u16`]. The high-bit packed YUV families
  /// (`yuv-444-packed` / `y2xx` / `v210`) reuse it to stage their
  /// de-interleaved native Y row before the same u16 luma stream, as do
  /// the high-bit planar YUV families (`yuv-planar`: Yuv444p / Yuv422p
  /// 10/12/14/16) staging their host-native Y plane, as do the high-bit
  /// semi-planar P-format families (`yuv-semi-planar`) staging their
  /// de-packed native Y. Lazily grown to `width` `u16`; empty otherwise.
  /// Gated to `gray` / `yuv-444-packed` / `y2xx` / `v210` / `yuv-planar`
  /// / `yuv-semi-planar`.
  #[cfg(any(
    feature = "gray",
    feature = "yuva",
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "v210",
    feature = "yuv-planar",
    feature = "yuv-semi-planar"
  ))]
  luma_scratch_u16: Vec<u16>,
  /// Source-width host-native `u32` luma staging for the
  /// [`Gray32`](crate::source::Gray32) resample path: the wire `Gray32` row
  /// converts here (source wire `BE` → host-native `u32` value) before
  /// feeding [`Self::luma_stream_u32`] / [`Self::luma_filter_stream_u32`],
  /// so binning runs at native `u32` precision and each output narrows only
  /// afterwards (0-ULP, closes issue #289). Lazily grown to `width` `u32`;
  /// empty otherwise. Gated to `gray`.
  #[cfg(feature = "gray")]
  luma_scratch_u32: Vec<u32>,
  /// Source-width host-native `f32` luma staging for the
  /// [`Grayf32`](crate::source::Grayf32) resample path: the wire
  /// `Grayf32` row converts here (source wire `BE` → host-native f32 via
  /// the same kernel the direct `luma_f32` path uses) before feeding
  /// [`Self::luma_stream_f32`]. Lazily grown to `width` `f32`; empty
  /// otherwise. Gated to `gray`.
  #[cfg(feature = "gray")]
  luma_scratch_f32: Vec<f32>,
  /// Row-stage **3-channel** `f32` **area** stream for the
  /// [`Yaf32`](crate::source::Yaf32) /
  /// [`Yaf16`](crate::source::Yaf16) (widened) resample path. Channel
  /// layout per pixel is `[Yc, A, Y]`: `Yc` is the colour luma fed straight
  /// (`= Y`) or premultiplied (`= Y * A`), `A` the real source alpha, and
  /// `Y` the independent native luma (mode-independent `mean(Y)`, so
  /// `luma*` outputs stay byte-identical to [`Grayf32`](crate::source::Grayf32)
  /// for the same Y). The engine bins each channel independently, so channel
  /// 2 equals a 1-channel native-Y bin. Lazily created in `process`, reset in
  /// `begin_frame`. Gated to `gray`.
  #[cfg(feature = "gray")]
  yaf_stream_f32: Option<std::boxed::Box<crate::resample::AreaStream<f32>>>,
  /// Row-stage **3-channel** `f32` **filter** stream — the filter twin of
  /// [`Self::yaf_stream_f32`] (straight alpha only; a premultiplied filter
  /// plan routes to the area tail, which surfaces `UnsupportedFilter`). f32
  /// is full-range, so there is no native-depth clamp on the filter output.
  /// Lazily created in `process`, reset in `begin_frame`. Gated to `gray`.
  #[cfg(feature = "gray")]
  yaf_filter_stream_f32: Option<std::boxed::Box<crate::resample::FilterStream<f32>>>,
  /// Source-width 3-channel `f32` staging for the Yaf resample: each wire
  /// `[Y, A]` pixel decodes here as `[Y, A, Y]` (host-native) before the
  /// optional premultiply (`row[3x] *= row[3x+1]`) and the single feed of
  /// [`Self::yaf_stream_f32`] / [`Self::yaf_filter_stream_f32`]. Lazily grown
  /// to `3 * width` `f32`; empty otherwise. Gated to `gray`.
  #[cfg(feature = "gray")]
  yaf_stage_scratch_f32: Vec<f32>,
  /// Out-width 3-channel `f32` de-interleave staging for the Yaf resample
  /// emit: each finalized binned `[cy, ca, ny]` row de-interleaves into
  /// contiguous `colorY | alpha | nativeY` planes (`colorY` resolved straight
  /// in place) so the direct `grayf32_*` kernels can derive every output.
  /// Lazily grown to `3 * out_width` `f32`; empty otherwise. Gated to `gray`.
  #[cfg(feature = "gray")]
  yaf_plane_scratch_f32: Vec<f32>,
  /// Source-width `f32` RGB staging for packed-float-RGB resampling:
  /// the wire row converts here (host-native, lossless) before feeding
  /// [`Self::rgb_stream_f32`]. Lazily grown to `3 * width` `f32`; empty
  /// otherwise. Gated like [`Self::rgb_stream_f32`]: the `rgb-float`
  /// family fences in the engine, `gbr` already carries it.
  #[cfg(any(
    all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
    feature = "gbr"
  ))]
  rgb_scratch_f32: Vec<f32>,
  /// Out-width G/B/R `f32` plane staging for the float planar-GBR
  /// ([`Gbrpf32`](crate::source::Gbrpf32)) resample tail. That tail
  /// de-interleaves each binned packed `R, G, B` row back into G/B/R
  /// planes (`[0..w]` = G, `[w..2w]` = B, `[2w..3w]` = R) so it can run
  /// the exact direct `gbrpf32_*` kernels — the `rgb-float`
  /// ([`Rgbf32`](crate::source::Rgbf32)) tail's packed `rgbf32_*` kernels
  /// are not compiled in a `gbr` build. Lazily grown to `3 * out_width`
  /// `f32`; empty for an `rgb_f32`-only sink (which copies the binned row
  /// directly). Gated to `gbr`.
  #[cfg(feature = "gbr")]
  rgb_plane_scratch_f32: Vec<f32>,
  /// Out-width G/B/R `half::f16` plane staging for the half-float
  /// planar-GBR ([`Gbrpf16`](crate::source::Gbrpf16)) resample tail.
  /// There is no `AreaStream<f16>`, so that tail bins in `f32` (the
  /// shared [`Self::rgb_stream_f32`]) and, per finalized output row,
  /// de-interleaves the binned packed `R, G, B` `f32` row into the
  /// `f32` planes ([`Self::rgb_plane_scratch_f32`]), **rounds each
  /// element to `half::f16`** into these planes (`[0..w]` = G,
  /// `[w..2w]` = B, `[2w..3w]` = R), then runs the exact direct
  /// `gbrpf16_*` kernels — so every output is byte-identical to a
  /// direct `Gbrpf16` conversion of the `f32` block-mean rounded to
  /// f16. Lazily grown to `3 * out_width` `half::f16`; empty for a
  /// sink with no f16-plane-derived output. Gated to `gbr`.
  #[cfg(feature = "gbr")]
  rgb_plane_scratch_f16: Vec<half::f16>,
  /// Source-width canonical `R, G, B, A` `f32` staging for the float
  /// planar GBR+alpha resample tail ([`Gbrapf32`](crate::source::Gbrapf32) /
  /// [`Gbrapf16`](crate::source::Gbrapf16)): the G/B/R/A planes interleave
  /// here (host-native f32, for `Gbrapf16` after the f16 -> f32 widen) —
  /// and, under [`AlphaMode::Premultiplied`], are premultiplied in place —
  /// before feeding the 4-channel [`Self::rgba_stream_f32`]. Lazily grown
  /// to `4 * width` `f32`; empty otherwise. Gated to `gbr` (the planar
  /// GBR+alpha float sources) plus the packed-float RGBA sources
  /// ([`Rgbaf32`](crate::source::Rgbaf32) /
  /// [`Rgbaf16`](crate::source::Rgbaf16), the latter widened f16 -> f32),
  /// which stage their wire row here before binning the 4-channel stream.
  #[cfg(any(
    feature = "gbr",
    all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
  ))]
  rgba_scratch_f32: Vec<f32>,
  /// Out-width host-native straight `R, G, B, A` `f32` staging for the
  /// float planar GBR+alpha resample tail: per finalized output row the
  /// binned packed RGBA is resolved to its straight form here (a copy in
  /// [`AlphaMode::Straight`], an un-premultiply in
  /// [`AlphaMode::Premultiplied`]) before it is de-interleaved into the
  /// G/B/R/A planes every output reads. Lazily grown to `4 * out_width`
  /// `f32`; empty for an output-less sink. Gated to `gbr`.
  #[cfg(feature = "gbr")]
  rgba_color_scratch_f32: Vec<f32>,
  /// Out-width G/B/R/A `f32` plane staging for the float planar GBR+alpha
  /// ([`Gbrapf32`](crate::source::Gbrapf32)) resample tail. That tail
  /// de-interleaves each resolved straight packed `R, G, B, A` row into
  /// G/B/R/A planes (`[0..ow]` = G, `[ow..2ow]` = B, `[2ow..3ow]` = R,
  /// `[3ow..4ow]` = A) so it can run the exact direct `gbrapf32_*` (RGBA) /
  /// `gbrpf32_*` (RGB / luma / hsv) kernels. Lazily grown to `4 * out_width`
  /// `f32`; empty for an `rgba_f32`-only sink (which copies the binned row
  /// directly). Gated to `gbr`.
  #[cfg(feature = "gbr")]
  rgba_plane_scratch_f32: Vec<f32>,
  /// Out-width G/B/R/A `half::f16` plane staging for the half-float planar
  /// GBR+alpha ([`Gbrapf16`](crate::source::Gbrapf16)) resample tail. There
  /// is no `AreaStream<f16>`, so that tail bins in `f32` (the shared
  /// [`Self::rgba_stream_f32`]) and, per finalized output row, resolves the
  /// straight binned RGBA, de-interleaves it into the `f32` planes, **rounds
  /// each element to `half::f16`** into these planes (`[0..ow]` = G,
  /// `[ow..2ow]` = B, `[2ow..3ow]` = R, `[3ow..4ow]` = A), then runs the
  /// exact direct `gbrapf16_*` / `gbrpf16_*` kernels — so every output is
  /// byte-identical to a direct `Gbrapf16` conversion of the `f32`
  /// block-mean rounded to f16. Lazily grown to `4 * out_width`
  /// `half::f16`; empty for a sink with no f16-plane-derived output. Gated
  /// to `gbr`.
  #[cfg(feature = "gbr")]
  rgba_plane_scratch_f16: Vec<half::f16>,
  /// Out-width **packed** `R, G, B` `half::f16` staging for the
  /// half-float packed-RGB ([`Rgbf16`](crate::source::Rgbf16)) resample
  /// tail. There is no `AreaStream<f16>`, so that tail bins in `f32` (the
  /// shared [`Self::rgb_stream_f32`]) and, per finalized output row,
  /// **rounds each binned packed `f32` element to `half::f16`** into this
  /// packed row, then runs the exact direct `rgbf16_*` kernels — so every
  /// output is byte-identical to a direct `Rgbf16` conversion of the `f32`
  /// block-mean rounded to f16. Unlike the planar
  /// [`Gbrpf16`](crate::source::Gbrpf16) tail this row stays **packed** (no
  /// de-interleave into planes), because the `rgbf16_*` kernels consume
  /// packed input. Lazily grown to `3 * out_width` `half::f16`; empty for a
  /// sink with no f16-derived output. Gated like [`Self::rgb_stream_f32`]:
  /// the `rgb-float` family fences in the engine.
  #[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
  rgb_packed_scratch_f16: Vec<half::f16>,
  /// Out-width **packed** `R, G, B, A` `half::f16` staging for the
  /// half-float packed-RGBA ([`Rgbaf16`](crate::source::Rgbaf16)) resample
  /// tail — the 4-channel twin of [`Self::rgb_packed_scratch_f16`]. There
  /// is no `AreaStream<f16>`, so that tail bins in `f32` (the shared
  /// 4-channel [`Self::rgba_stream_f32`]) and, per finalized output row,
  /// **rounds each binned packed `f32` element to `half::f16`** into this
  /// packed `R, G, B, A` row, then runs the exact direct `rgbaf16_*`
  /// kernels — so every output is byte-identical to a direct `Rgbaf16`
  /// conversion of the `f32` block-mean rounded to f16. Lazily grown to
  /// `4 * out_width` `half::f16`; empty for a sink with no f16-derived
  /// output. Gated like [`Self::rgb_packed_scratch_f16`].
  #[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
  rgba_packed_scratch_f16: Vec<half::f16>,
  /// Source-width **linear-XYZ** `f32` staging for the
  /// [`Xyz12`](crate::source::Xyz12) resample path: the wire row
  /// converts here (inverse-OETF only, no matrix) before feeding
  /// [`Self::xyz_stream_f32`]. Lazily grown to `3 * width` `f32`; empty
  /// otherwise. Gated to `xyz`.
  #[cfg(feature = "xyz")]
  xyz_scratch_f32: Vec<f32>,
  /// Whether row primitives dispatch to their SIMD backend. Defaults
  /// to `true`; benchmarks flip this with [`Self::with_simd`] /
  /// [`Self::set_simd`] to A/B test scalar vs SIMD on the same frame.
  simd: bool,
  /// How the source's packed alpha relates to its color when the frame
  /// is area-resampled. Seeded from `F::DEFAULT_ALPHA_MODE`
  /// ([`DefaultAlphaMode`]) at construction; overridden per call by
  /// [`Self::with_alpha_mode`] / [`Self::set_alpha_mode`], the
  /// [`Self::with_simd`] flag pattern. Only the packed-RGBA resample
  /// tail consults it; every direct (identity-plan) path and every
  /// non-RGBA source ignores it.
  alpha_mode: AlphaMode,
  /// The RFC #238 averaging domain an area downscale resamples in.
  /// Defaults to [`AveragingDomain::Encoded`] (the gamma-encoded /
  /// libswscale-class average — bit-identical to the pre-RFC behaviour).
  /// [`AveragingDomain::Linear`], set via [`Self::with_averaging_domain`],
  /// opts into the physically-correct linear-light average for the planar
  /// 8-bit YUV family (the only family wired in Phase 2); every other
  /// format ignores it and stays on the encoded path.
  #[cfg(feature = "yuv-planar")]
  averaging_domain: AveragingDomain,
  /// Optional caller override for the [`AveragingDomain::Linear`] transfer
  /// curve. `None` (the default) derives the curve from the sink's
  /// [`KernelMatrix`](crate::KernelMatrix) via [`TransferFunction::for_matrix`]; `Some(tf)` pins
  /// `tf` regardless of the matrix. Consulted only on the linear-light
  /// path; the encoded path never reads it. See
  /// [`Self::with_transfer_function`].
  #[cfg(feature = "yuv-planar")]
  transfer_function: Option<TransferFunction>,
  /// Chroma sample location driving siting-aware 4:2:0 chroma upsampling
  /// (#302). Defaults to [`ChromaLocation::Unspecified`](crate::ChromaLocation::Unspecified)
  /// — pixon's nearest-neighbor decode, byte-identical to the pre-#302
  /// behaviour. Set via [`Self::with_chroma_location`] /
  /// [`Self::set_chroma_location`]; the centered horizontal sitings
  /// (`Center` / `Top` / `Bottom`, per `chroma_420_center_sited_h`) route
  /// the identity-plan 4:2:0 decode through the phase-0.5 chroma upsample +
  /// 4:4:4 kernels. Consulted by the planar 8-bit `Yuv420p`, the semi-planar
  /// `Nv12` / `Nv21`, and the high-bit planar `Yuv420p9` … `Yuv420p16` identity
  /// paths; the remaining 4:2:0 families and the resampling tiers ignore it
  /// until the #302 rollout wires them in.
  #[cfg(feature = "yuv-planar")]
  chroma_location: crate::ChromaLocation,
  /// Source colour [`Primaries`](crate::Primaries) — **sink-consumed**, like
  /// [`chroma_location`](Self::chroma_location): mediaframe's YUV row carries
  /// only range + matrix, not the primaries, so the
  /// [`KernelMatrix::ChromaDerivedNcl`](crate::KernelMatrix::ChromaDerivedNcl)
  /// decode (whose `Kr` / `Kb` are *derived* from the primaries) reads them
  /// here. Defaults to [`Primaries::Unspecified`](crate::Primaries::Unspecified)
  /// (no chromaticities → the prior BT.709 fallback, byte-identical to the
  /// pre-#303 behaviour). Set via [`Self::with_color_spec`] /
  /// [`Self::set_color_spec`]. Currently consulted by the planar 8-bit
  /// `Yuv420p` identity RGB / RGBA path; other families and the resampling
  /// tiers keep the matrix-tag coefficients until wired in.
  #[cfg(feature = "yuv-planar")]
  primaries: crate::Primaries,
  /// How [`Primaries::SmpteSt428`](crate::Primaries::SmpteSt428) is interpreted
  /// when it feeds the [`KernelMatrix::ChromaDerivedNcl`](crate::KernelMatrix::ChromaDerivedNcl)
  /// luma-weight derivation (#310). Defaults to
  /// [`St428Interpretation::FfmpegTabulated`] — the tabulated derivation,
  /// byte-identical to the pre-#310 behaviour. [`St428Interpretation::CieXyz`]
  /// instead rejects the (meaningless) YCbCr-over-XYZ combination. Set via
  /// [`Self::with_st428_interpretation`] / [`Self::set_st428_interpretation`].
  /// Consulted by the planar 8-bit `Yuv420p` `ChromaDerivedNcl` path (the
  /// representative #310 wiring, mirroring [`primaries`](Self::primaries)); the
  /// remaining families keep the tabulated derivation until wired in.
  #[cfg(feature = "yuv-planar")]
  st428_interpretation: St428Interpretation,
  /// Source [`Transfer`](crate::Transfer) characteristics — **sink-consumed**,
  /// like [`primaries`](Self::primaries): mediaframe's YUV row carries only
  /// range + matrix, not the transfer, so the
  /// [`ColorMatrix::Ictcp`](crate::ColorMatrix::Ictcp) non-affine decode
  /// (H.273 `MatrixCoefficients = 14`, BT.2100 ICtCp), which selects its
  /// inverse matrix + per-channel EOTF from the PQ/HLG transfer, reads it
  /// here. Defaults to [`Transfer::Unspecified`](crate::Transfer::Unspecified)
  /// (no PQ/HLG transfer → the affine matrix-tag fallback, byte-identical to
  /// the pre-#303 behaviour). Set via [`Self::with_color_spec`] /
  /// [`Self::set_color_spec`]. Currently consulted by the planar 12-bit
  /// `Yuv444p12` identity RGB / RGBA path (the representative #303 wiring);
  /// other families and the resampling tiers keep the affine path until
  /// wired in.
  #[cfg(feature = "yuv-planar")]
  transfer: crate::Transfer,
  /// Source **descriptor** [`ColorMatrix`](crate::ColorMatrix) — the open
  /// matrix tag, sink-consumed alongside [`primaries`](Self::primaries) and
  /// [`transfer`](Self::transfer).
  ///
  /// A walker row carries a [`KernelMatrix`](crate::KernelMatrix), the closed
  /// selector of the matrices with tabulated *affine* coefficients. The four
  /// **non-affine** decodes (#303) — [`Ictcp`](crate::ColorMatrix::Ictcp),
  /// [`ChromaDerivedCl`](crate::ColorMatrix::ChromaDerivedCl),
  /// [`IptC2`](crate::ColorMatrix::IptC2),
  /// [`Smpte2085`](crate::ColorMatrix::Smpte2085) — have no such coefficients
  /// and therefore no `KernelMatrix` spelling, so their tag cannot ride the
  /// row; it rides here instead, with the transfer and primaries that select
  /// their variant. Defaults to
  /// [`ColorMatrix::Unspecified`](crate::ColorMatrix::Unspecified) — no
  /// non-affine tag → the affine path, byte-identical to the pre-#303
  /// behaviour. Set via [`Self::with_color_spec`] / [`Self::set_color_spec`].
  ///
  /// **One description, one reader.** This field and
  /// [`kernel_matrix`](Self::kernel_matrix) are not two knobs to keep in step:
  /// colour intent has a single carrier, [`ColorSpec`](crate::ColorSpec), and
  /// both are its projections, written together by the one call that can write
  /// either. Since mediaframe 0.4 the row's selector is read from this same
  /// sink rather than passed beside it, so the two provably came from one
  /// description — there is no longer a signature anywhere that could carry a
  /// second answer. Losing the non-affine tag by naming a matrix somewhere
  /// else is not a mistake this API can express.
  ///
  /// [`Convert::spec`](crate::Convert::spec) sets both from one call.
  #[cfg(feature = "yuv-planar")]
  matrix: crate::ColorMatrix,
  /// Per-frame accumulator for the RFC #238 [`AveragingDomain::Linear`]
  /// linear-light tail (planar 8-bit YUV family). Lazily created on the
  /// first output-bearing row of a linear-domain frame; reset to `None` per
  /// `begin_frame`. Present only when the linear path is wired
  /// (`yuv-planar` + `rgb`).
  #[cfg(all(feature = "yuv-planar", feature = "rgb"))]
  linear_light_frame: Option<linear_light::LinearLightFrame>,
  /// How the [`AveragingDomain::Linear`] tail decodes `YUV→RGB` before the
  /// EOTF (RFC #238 #244). [`LinearMode::DisplayReferred`] (the default)
  /// decodes through the clamped Q15 kernel; [`LinearMode::SceneReferred`]
  /// decodes the same affine matrix in unclamped `f32`, preserving
  /// out-of-gamut excursions, and clamps only at the re-encoded output.
  /// Consulted only on the linear-light path; the encoded and direct paths
  /// ignore it. Set via [`Self::with_linear_mode`].
  #[cfg(feature = "yuv-planar")]
  linear_mode: LinearMode,
  /// Source-width unclamped normalized `f32` RGB staging for the
  /// scene-referred ([`LinearMode::SceneReferred`]) linear-light decode: the
  /// unclamped affine `YUV→RGB` decode writes here (`[0, 1]`-scale, may leave
  /// `[0, 1]`) before the tail lifts it through the EOTF. Lazily grown to
  /// `3 * width` `f32`; empty otherwise (the display-referred path uses the
  /// `u8` [`Self::rgb_scratch`] instead). Present only when the linear path
  /// is wired (`yuv-planar` + `rgb`).
  #[cfg(all(feature = "yuv-planar", feature = "rgb"))]
  linear_scene_scratch: Vec<f32>,
  /// Q8 fixed-point luma coefficients `(cr, cg, cb)` such that
  /// `luma = ((cr * R + cg * G + cb * B + 128) >> 8) as u8`. Only
  /// consulted by source impls that *derive* luma from RGB
  /// (currently the `Bayer` / `Bayer16<BITS>` family and the `Pal8`
  /// mono palette path — YUV impls memcpy from the native Y plane
  /// and ignore this field). Default: BT.709 `(54, 183, 19)`.
  #[cfg(any(feature = "bayer", feature = "mono"))]
  luma_coefficients_q8: (u32, u32, u32),
  /// The closed affine selector every row of a walk is stamped with —
  /// returned from this sinker's
  /// [`PixelSink::kernel_matrix`](crate::PixelSink::kernel_matrix).
  ///
  /// mediaframe 0.4 took the matrix off the `{fmt}_to` signature and asks
  /// the sink for it once per walk, because the sink is the thing that
  /// already holds a colour description and two doors onto one fact is a
  /// bug generator. So this is where the row's selector now lives.
  ///
  /// It can only be written by [`Self::set_color_spec`] /
  /// [`Self::with_color_spec`], out of
  /// [`ColorSpec::kernel_matrix`](crate::ColorSpec::kernel_matrix) — the
  /// single exchange point. Default
  /// [`KernelMatrix::Bt709`](crate::KernelMatrix::Bt709): the
  /// unnamed-colorimetry posture, identical to what a spec carrying
  /// [`ColorMatrix::Unspecified`](crate::ColorMatrix::Unspecified) resolves
  /// to.
  ///
  /// The open [`matrix`](Self::matrix) descriptor beside it is *not* a
  /// second knob — both are projections of one
  /// [`ColorSpec`](crate::ColorSpec), set together by the same call. The
  /// open one carries the four non-affine tags that have no `KernelMatrix`
  /// spelling; this one carries the affine selector the kernels index.
  kernel_matrix: crate::KernelMatrix,
  /// The output RGB gamut an XYZ12 walk decodes into — returned from this
  /// sinker's [`Xyz12Sink::target_gamut`](crate::source::Xyz12Sink::target_gamut).
  ///
  /// Sink-held for the same reason as [`Self::kernel_matrix`], but on the
  /// opposite axis: a gamut describes the *output*, and this sinker **is**
  /// the output. Written only by [`Self::set_target_gamut`] /
  /// [`Self::with_target_gamut`], out of the open
  /// [`DcpTargetGamut`](crate::DcpTargetGamut) descriptor. Default
  /// [`KernelGamut::DciP3`](crate::KernelGamut::DciP3) — the theatrical
  /// decode a DCP distribution master is mastered for.
  #[cfg(feature = "xyz")]
  target_gamut: crate::KernelGamut,
  _fmt: PhantomData<F>,
  _resampler: PhantomData<R>,
}

/// Luma coefficient set for sources that derive luma from RGB.
///
/// Only consulted by `MixedSinker` impls whose source is *not* YUV
/// (currently the Bayer / Bayer16 family — YUV impls memcpy from
/// the native Y plane). For Bayer the choice should match the
/// gamut your [`crate::raw::ColorCorrectionMatrix`] targets:
///
/// - CCM target = Rec.709 / sRGB → use [`Self::Bt709`] (the default)
/// - CCM target = Rec.2020 (UHDTV / HDR10) → use [`Self::Bt2020`]
/// - CCM target = DCI-P3 (cinema) → use [`Self::DciP3`]
/// - CCM target = ACEScg / ACES AP1 → use [`Self::AcesAp1`]
/// - CCM target = SDTV (rare for RAW) → use [`Self::Bt601`]
/// - CCM target = something else, or you've measured your own
///   weights → use [`Self::Custom`] (constructed via
///   [`Self::try_custom`] or [`Self::custom`])
///
/// Picking the wrong set still produces a **valid** luma plane,
/// but its numeric values won't match what a downstream
/// luma-driven analysis (scene-cut detection, brightness
/// thresholding, perceptual diff) expects for non-grayscale
/// content. Uniform-gray content is unaffected — every coefficient
/// set agrees on gray.
///
/// Each variant resolves to a Q8 `(cr, cg, cb)` triple summing to
/// `256` so `(cr * R + cg * G + cb * B + 128) >> 8` produces
/// `u8` luma without bias. The triples come from each standard's
/// published coefficients rounded to nearest u32.
#[derive(Debug, Clone, Copy, PartialEq, IsVariant)]
#[non_exhaustive]
pub enum LumaCoefficients {
  /// **BT.709 / sRGB** (`R=0.2126, G=0.7152, B=0.0722`) → Q8
  /// `(54, 183, 19)`. The default; most common output gamut and
  /// the implicit weights every YUV→RGB→luma video pipeline uses.
  Bt709,
  /// **BT.2020 / Rec.2020** (`R=0.2627, G=0.6780, B=0.0593`) → Q8
  /// `(67, 174, 15)`. UHDTV / HDR10 / Rec.2100 (HLG, PQ).
  Bt2020,
  /// **BT.601 / SMPTE 170M** (`R=0.2990, G=0.5870, B=0.1140`) →
  /// Q8 `(77, 150, 29)`. Legacy SDTV / NTSC / PAL. Rare for RAW
  /// pipelines but included for completeness.
  Bt601,
  /// **DCI-P3** (`R=0.228975, G=0.691739, B=0.079287`) → Q8
  /// `(59, 177, 20)`. Theatrical / cinema P3 displays. Note the
  /// **D65 white point** is the same as Rec.709, so for
  /// luma-only purposes this is close to `Bt709` (within ~1 LSB
  /// for most content).
  DciP3,
  /// **ACES AP1 / ACEScg** (`R=0.2722287, G=0.6740818,
  /// B=0.0536895`) → Q8 `(70, 172, 14)`. Cinema grading working
  /// space. Numerically very close to BT.2020. (Naïve nearest
  /// rounding gives `(70, 173, 14)` which sums to 257; the `cg`
  /// term is rounded down by 1 LSB so the triple sums to 256
  /// without biasing the `>> 8` divisor.)
  AcesAp1,
  /// Caller-supplied coefficients. Use [`Self::try_custom`] or
  /// [`Self::custom`] to construct — the inner
  /// [`CustomLumaCoefficients`] keeps fields private so every
  /// `Custom` value is guaranteed finite, non-negative, and
  /// magnitude-bounded.
  Custom(CustomLumaCoefficients),
}

/// Validated red / green / blue luma weights, accessible only through
/// [`LumaCoefficients::Custom`] (or [`Self::try_new`] /
/// [`Self::new`]).
///
/// Each weight is a finite, non-negative `f32` ≤
/// [`Self::MAX_COEFFICIENT`]. The bound is much tighter than
/// [`crate::raw::WhiteBalance::MAX_GAIN`] (`1e6`) because the luma
/// kernel multiplies these into a `u32` accumulator — see
/// [`Self::MAX_COEFFICIENT`] for the overflow analysis.
///
/// The struct intentionally has no public fields. Use
/// [`Self::r`] / [`Self::g`] / [`Self::b`] to read components.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CustomLumaCoefficients {
  r: f32,
  g: f32,
  b: f32,
}

impl CustomLumaCoefficients {
  /// Maximum permitted per-channel weight. `10.0` is far above any
  /// realistic published luma coefficient (the standard sets all
  /// individual weights are ≤ `1.0`) and far below the value at
  /// which the per-pixel `u32` accumulator could overflow:
  /// `(coef * 256 + 0.5) as u32 ≤ 10 * 256 + 1 = 2_561`, so the
  /// largest per-row term is `2_561 * 255 = 653_055`, and the
  /// three-channel sum + bias `3 * 653_055 + 128 = 1_959_293` —
  /// six orders of magnitude below `u32::MAX`.
  ///
  /// `1e6` (the
  /// [`crate::raw::WhiteBalance::MAX_GAIN`] bound) **would not be
  /// safe here** — `1e6 * 256 = 256_000_000`, and `256_000_000 *
  /// 255 ≈ 6.5e10` overflows `u32`.
  pub const MAX_COEFFICIENT: f32 = 10.0;

  /// Constructs a [`CustomLumaCoefficients`] from explicit R / G / B
  /// weights, validating that each is **finite, non-negative, and
  /// ≤ [`Self::MAX_COEFFICIENT`]**.
  ///
  /// Returns [`LumaCoefficientsError`] for the first failing
  /// channel. A weight of `0` is permitted (the channel doesn't
  /// contribute to luma — degenerate but well-defined).
  ///
  /// The weights are *not* required to sum to `1.0`; sums far from
  /// `1.0` produce a brightness-scaled luma plane (the doc on
  /// [`LumaCoefficients`] flags this), which is sometimes
  /// intentional (matte / key extraction). Only NaN / ±∞ /
  /// negative / out-of-range weights are rejected because those
  /// would silently corrupt the luma plane via the `f32 → u32`
  /// saturating cast or overflow the accumulator.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn try_new(r: f32, g: f32, b: f32) -> Result<Self, LumaCoefficientsError> {
    if !r.is_finite() {
      return Err(LumaCoefficientsError::NonFinite {
        channel: LumaChannel::R,
        value: r,
      });
    }
    if !g.is_finite() {
      return Err(LumaCoefficientsError::NonFinite {
        channel: LumaChannel::G,
        value: g,
      });
    }
    if !b.is_finite() {
      return Err(LumaCoefficientsError::NonFinite {
        channel: LumaChannel::B,
        value: b,
      });
    }
    if r < 0.0 {
      return Err(LumaCoefficientsError::Negative {
        channel: LumaChannel::R,
        value: r,
      });
    }
    if g < 0.0 {
      return Err(LumaCoefficientsError::Negative {
        channel: LumaChannel::G,
        value: g,
      });
    }
    if b < 0.0 {
      return Err(LumaCoefficientsError::Negative {
        channel: LumaChannel::B,
        value: b,
      });
    }
    if r > Self::MAX_COEFFICIENT {
      return Err(LumaCoefficientsError::OutOfBounds {
        channel: LumaChannel::R,
        value: r,
        max: Self::MAX_COEFFICIENT,
      });
    }
    if g > Self::MAX_COEFFICIENT {
      return Err(LumaCoefficientsError::OutOfBounds {
        channel: LumaChannel::G,
        value: g,
        max: Self::MAX_COEFFICIENT,
      });
    }
    if b > Self::MAX_COEFFICIENT {
      return Err(LumaCoefficientsError::OutOfBounds {
        channel: LumaChannel::B,
        value: b,
        max: Self::MAX_COEFFICIENT,
      });
    }
    Ok(Self { r, g, b })
  }

  /// Constructs a [`CustomLumaCoefficients`], panicking on invalid
  /// input. Prefer [`Self::try_new`] for caller-supplied values.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(r: f32, g: f32, b: f32) -> Self {
    match Self::try_new(r, g, b) {
      Ok(c) => c,
      Err(_) => panic!("invalid CustomLumaCoefficients (non-finite, negative, or out of range)"),
    }
  }

  /// Red weight.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn r(&self) -> f32 {
    self.r
  }

  /// Green weight.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn g(&self) -> f32 {
    self.g
  }

  /// Blue weight.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn b(&self) -> f32 {
    self.b
  }
}

/// Identifies which luma weight failed validation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, IsVariant)]
#[non_exhaustive]
pub enum LumaChannel {
  /// Red weight.
  R,
  /// Green weight.
  G,
  /// Blue weight.
  B,
}

/// Errors returned by [`CustomLumaCoefficients::try_new`] (and the
/// convenience [`LumaCoefficients::try_custom`]).
#[derive(Debug, Clone, Copy, PartialEq, IsVariant, Error)]
#[non_exhaustive]
pub enum LumaCoefficientsError {
  /// A weight is non-finite (NaN, +∞, or -∞).
  #[error("CustomLumaCoefficients.{channel:?} is non-finite (got {value})")]
  NonFinite {
    /// Which channel failed validation.
    channel: LumaChannel,
    /// The offending weight value.
    value: f32,
  },
  /// A weight is negative. Zero is allowed (zeroes the channel).
  #[error("CustomLumaCoefficients.{channel:?} is negative (got {value})")]
  Negative {
    /// Which channel failed validation.
    channel: LumaChannel,
    /// The offending weight value.
    value: f32,
  },
  /// A weight exceeds [`CustomLumaCoefficients::MAX_COEFFICIENT`]
  /// (`10.0`). The bound is far above any realistic luma weight
  /// but closes the door on values that would saturate the
  /// `f32 → u32` cast in [`LumaCoefficients::to_q8`] or overflow
  /// the per-row `u32` accumulator.
  #[error("CustomLumaCoefficients.{channel:?} = {value} exceeds the magnitude bound ({max})")]
  OutOfBounds {
    /// Which channel failed validation.
    channel: LumaChannel,
    /// The offending weight value.
    value: f32,
    /// The bound that was exceeded
    /// ([`CustomLumaCoefficients::MAX_COEFFICIENT`]).
    max: f32,
  },
}

impl LumaCoefficients {
  /// Resolves the coefficient set to its Q8 fixed-point triple
  /// `(cr, cg, cb)` such that
  /// `luma = ((cr * R + cg * G + cb * B + 128) >> 8) as u8`. The
  /// preset triples come from each standard's published weights
  /// rounded to nearest u32 and (for the published presets) sum
  /// to exactly `256`.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn to_q8(self) -> (u32, u32, u32) {
    match self {
      Self::Bt709 => (54, 183, 19),
      Self::Bt2020 => (67, 174, 15),
      Self::Bt601 => (77, 150, 29),
      Self::DciP3 => (59, 177, 20),
      // Naïve nearest rounding gives `(70, 173, 14)` which sums
      // to 257; the `>> 8` divisor implicitly assumes 256, so we
      // shave 1 LSB off `cg` (the largest, smallest-relative-
      // -error coefficient). Resulting (R, G, B) error vs. the
      // published weights is `(+0.0012, -0.0022, +0.0010)`.
      Self::AcesAp1 => (70, 172, 14),
      // Custom values are guaranteed finite + non-negative +
      // ≤ `MAX_COEFFICIENT` (= 10.0) by `CustomLumaCoefficients::
      // try_new`, so the `as u32` cast cannot saturate to
      // `u32::MAX` and the downstream accumulator cannot overflow.
      Self::Custom(c) => (
        (c.r * 256.0 + 0.5) as u32,
        (c.g * 256.0 + 0.5) as u32,
        (c.b * 256.0 + 0.5) as u32,
      ),
    }
  }

  /// Constructs [`Self::Custom`] from explicit R / G / B weights,
  /// validating each via [`CustomLumaCoefficients::try_new`].
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn try_custom(r: f32, g: f32, b: f32) -> Result<Self, LumaCoefficientsError> {
    match CustomLumaCoefficients::try_new(r, g, b) {
      Ok(c) => Ok(Self::Custom(c)),
      Err(e) => Err(e),
    }
  }

  /// Constructs [`Self::Custom`] from explicit R / G / B weights,
  /// panicking on invalid input. Prefer [`Self::try_custom`] for
  /// caller-supplied values.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn custom(r: f32, g: f32, b: f32) -> Self {
    Self::Custom(CustomLumaCoefficients::new(r, g, b))
  }
}

impl Default for LumaCoefficients {
  /// Default is [`Self::Bt709`] — matches the implicit weights
  /// every YUV-source → RGB → luma video pipeline uses.
  fn default() -> Self {
    Self::Bt709
  }
}

impl<F: SourceFormat> MixedSinker<'_, F, NoopResampler> {
  /// Creates an empty [`MixedSinker`] for the given dimensions, with
  /// the identity resampler (output geometry == source geometry).
  /// Attach output buffers with `with_rgb` / `with_luma` / `with_hsv`;
  /// each attachment validates that the buffer is at least
  /// `width * height * bytes_per_pixel` so short-buffer bugs surface
  /// *before* any rows are written — not after half the frame has
  /// been mutated. For a sinker whose outputs land at a smaller
  /// geometry, construct via [`MixedSinker::with_resampler`] instead.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn new(width: usize, height: usize) -> Self {
    Self::with_geometry(width, height, width, height)
  }
}

impl<F: SourceFormat, R> MixedSinker<'_, F, R> {
  /// Field initializer shared by [`MixedSinker::new`] and
  /// [`MixedSinker::with_resampler`]: source geometry plus the output
  /// geometry that buffer validation sizes against.
  fn with_geometry(width: usize, height: usize, out_width: usize, out_height: usize) -> Self {
    Self {
      rgb: None,
      rgb_u16: None,
      rgb_f32: None,
      rgb_f16: None,
      rgba: None,
      rgba_u16: None,
      rgba_f32: None,
      rgba_f16: None,
      luma: None,
      luma_u16: None,
      luma_f32: None,
      #[cfg(any(
        feature = "bayer",
        feature = "gbr",
        feature = "gray",
        feature = "mono",
        feature = "rgb",
        feature = "rgb-float",
        feature = "rgb-legacy",
        feature = "v210",
        feature = "xyz",
        feature = "y2xx",
        feature = "yuv-444-packed",
        feature = "yuv-packed",
        feature = "yuv-planar",
        feature = "yuv-semi-planar",
        feature = "yuva",
      ))]
      hsv: None,
      #[cfg(feature = "xyz")]
      xyz_f32: None,
      width,
      height,
      out_width,
      out_height,
      plan: None,
      #[cfg(any(
        feature = "yuv-planar",
        feature = "rgb",
        feature = "gbr",
        feature = "gray",
        feature = "xyz",
        feature = "bayer",
        feature = "mono",
        feature = "yuv-semi-planar",
        feature = "yuv-packed",
        feature = "yuv-444-packed",
        feature = "y2xx",
        feature = "v210",
        feature = "rgb-legacy"
      ))]
      rgb_stream: None,
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "yuv-444-packed",
        feature = "y2xx",
        feature = "v210",
        feature = "yuv-planar",
        feature = "yuv-semi-planar"
      ))]
      rgb_stream_u16: None,
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "gray",
        feature = "mono",
        feature = "yuva",
        feature = "yuv-444-packed"
      ))]
      rgba_stream: None,
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "gray",
        feature = "yuva",
        feature = "yuv-444-packed"
      ))]
      rgba_stream_u16: None,
      #[cfg(any(feature = "rgb", feature = "gbr"))]
      rgb_stream_u32: None,
      #[cfg(any(feature = "rgb", feature = "gbr"))]
      rgb_filter_stream_u32: None,
      #[cfg(any(feature = "rgb", feature = "gbr"))]
      rgba_stream_u32: None,
      #[cfg(any(feature = "rgb", feature = "gbr"))]
      rgba_filter_stream_u32: None,
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "gray",
        feature = "mono",
        feature = "yuva",
        feature = "yuv-444-packed"
      ))]
      frozen_alpha_mode: None,
      #[cfg(any(
        all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
        feature = "gbr"
      ))]
      rgb_stream_f32: None,
      #[cfg(any(
        all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
        feature = "gbr"
      ))]
      rgb_filter_stream_f32: None,
      #[cfg(any(
        feature = "gbr",
        all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
      ))]
      rgba_stream_f32: None,
      #[cfg(any(
        feature = "gbr",
        all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
      ))]
      rgba_filter_stream_f32: None,
      #[cfg(feature = "xyz")]
      xyz_stream_f32: None,
      #[cfg(feature = "xyz")]
      xyz_filter_stream_f32: None,
      #[cfg(any(
        feature = "yuv-planar",
        feature = "rgb",
        feature = "gbr",
        feature = "gray",
        feature = "xyz",
        feature = "bayer",
        feature = "mono",
        feature = "yuv-semi-planar",
        feature = "yuv-packed",
        feature = "yuv-444-packed",
        feature = "y2xx",
        feature = "v210",
        feature = "rgb-legacy"
      ))]
      luma_stream: None,
      #[cfg(any(
        feature = "gray",
        feature = "yuva",
        feature = "yuv-444-packed",
        feature = "y2xx",
        feature = "v210",
        feature = "yuv-planar",
        feature = "yuv-semi-planar"
      ))]
      luma_stream_u16: None,
      #[cfg(feature = "gray")]
      luma_stream_u32: None,
      #[cfg(feature = "gray")]
      luma_stream_f32: None,
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "yuv-444-packed",
        feature = "y2xx",
        feature = "yuv-planar",
        feature = "yuv-semi-planar",
        feature = "yuv-packed"
      ))]
      rgb_filter_stream: None,
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "yuv-444-packed",
        feature = "mono",
        feature = "yuva",
        feature = "gray"
      ))]
      rgba_filter_stream: None,
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "yuv-444-packed",
        feature = "y2xx",
        feature = "yuv-planar",
        feature = "yuv-semi-planar"
      ))]
      rgb_filter_stream_u16: None,
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "yuv-444-packed",
        feature = "yuva",
        feature = "gray"
      ))]
      rgba_filter_stream_u16: None,
      #[cfg(any(
        feature = "yuv-planar",
        feature = "yuv-semi-planar",
        feature = "yuv-packed",
        feature = "mono",
        feature = "gray"
      ))]
      luma_filter_stream: None,
      #[cfg(feature = "gray")]
      luma_filter_stream_f32: None,
      #[cfg(any(
        feature = "yuv-444-packed",
        feature = "y2xx",
        feature = "yuv-planar",
        feature = "gray",
        feature = "yuva",
        feature = "yuv-semi-planar"
      ))]
      luma_filter_stream_u16: None,
      #[cfg(feature = "gray")]
      luma_filter_stream_u32: None,
      #[cfg(any(
        feature = "yuv-planar",
        feature = "rgb",
        feature = "gbr",
        feature = "gray",
        feature = "xyz",
        feature = "bayer",
        feature = "mono",
        feature = "yuv-semi-planar",
        feature = "yuv-packed",
        feature = "yuv-444-packed",
        feature = "y2xx",
        feature = "v210",
        feature = "rgb-legacy"
      ))]
      resample_outputs: None,
      #[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
      native: true,
      #[cfg(feature = "yuv-planar")]
      native_420: None,
      #[cfg(feature = "yuva")]
      native_yuva_420: None,
      #[cfg(feature = "yuv-planar")]
      bicublin_420: None,
      #[cfg(feature = "yuv-planar")]
      native_planar: None,
      #[cfg(feature = "yuv-planar")]
      hsv_planar: None,
      #[cfg(feature = "yuv-planar")]
      native_420_u16: None,
      #[cfg(feature = "yuv-planar")]
      native_planar_u16: None,
      #[cfg(feature = "yuv-planar")]
      yuv444_msb_y_depack: Vec::new(),
      #[cfg(feature = "yuv-planar")]
      yuv444_msb_u_depack: Vec::new(),
      #[cfg(feature = "yuv-planar")]
      yuv444_msb_v_depack: Vec::new(),
      #[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
      semi_planar_u_half: Vec::new(),
      #[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
      semi_planar_v_half: Vec::new(),
      #[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
      p0xx_y_half: Vec::new(),
      #[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
      p0xx_u_half: Vec::new(),
      #[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
      p0xx_v_half: Vec::new(),
      #[cfg(all(feature = "yuv-packed", feature = "yuv-planar"))]
      packed_yuv_y_full: Vec::new(),
      #[cfg(all(feature = "yuv-packed", feature = "yuv-planar"))]
      packed_yuv_u_half: Vec::new(),
      #[cfg(all(feature = "yuv-packed", feature = "yuv-planar"))]
      packed_yuv_v_half: Vec::new(),
      #[cfg(all(feature = "y2xx", feature = "yuv-planar"))]
      y2xx_y_full: Vec::new(),
      #[cfg(all(feature = "y2xx", feature = "yuv-planar"))]
      y2xx_u_half: Vec::new(),
      #[cfg(all(feature = "y2xx", feature = "yuv-planar"))]
      y2xx_v_half: Vec::new(),
      #[cfg(all(feature = "v210", feature = "yuv-planar"))]
      v210_y_full: Vec::new(),
      #[cfg(all(feature = "v210", feature = "yuv-planar"))]
      v210_u_half: Vec::new(),
      #[cfg(all(feature = "v210", feature = "yuv-planar"))]
      v210_v_half: Vec::new(),
      #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
      packed_444_y_full: Vec::new(),
      #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
      packed_444_u_full: Vec::new(),
      #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
      packed_444_v_full: Vec::new(),
      #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
      packed_444_y_full_u16: Vec::new(),
      #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
      packed_444_u_full_u16: Vec::new(),
      #[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
      packed_444_v_full_u16: Vec::new(),
      #[cfg(feature = "yuv-planar")]
      frozen_native_route: None,
      #[cfg(feature = "yuv-planar")]
      frozen_domain: None,
      #[cfg(feature = "yuv-planar")]
      frozen_chroma_centered: None,
      #[cfg(feature = "yuv-planar")]
      frozen_chroma_bottom_v: None,
      #[cfg(feature = "yuv-planar")]
      frozen_chroma_top_v: None,
      #[cfg(feature = "yuv-planar")]
      chroma_top_pending: None,
      #[cfg(feature = "yuv-planar")]
      chroma_top_st428: None,
      #[cfg(feature = "yuv-planar")]
      chroma_top_y: Vec::new(),
      #[cfg(feature = "yuva")]
      chroma_top_a: Vec::new(),
      #[cfg(any(
        feature = "bayer",
        feature = "gbr",
        feature = "gray",
        feature = "mono",
        feature = "rgb",
        feature = "rgb-float",
        feature = "rgb-legacy",
        feature = "v210",
        feature = "xyz",
        feature = "y2xx",
        feature = "yuv-444-packed",
        feature = "yuv-packed",
        feature = "yuv-planar",
        feature = "yuv-semi-planar",
        feature = "yuva",
      ))]
      rgb_scratch: Vec::new(),
      #[cfg(feature = "yuv-planar")]
      chroma_full: Vec::new(),
      #[cfg(feature = "yuv-planar")]
      chroma_prev: Vec::new(),
      #[cfg(feature = "yuv-planar")]
      chroma_prev_row: None,
      #[cfg(feature = "yuv-planar")]
      chroma_full_u16: Vec::new(),
      #[cfg(feature = "yuv-planar")]
      chroma_prev_u16: Vec::new(),
      #[cfg(feature = "yuv-planar")]
      chroma_top_y_u16: Vec::new(),
      #[cfg(feature = "yuva")]
      chroma_top_a_u16: Vec::new(),
      #[cfg(any(feature = "yuv-packed", feature = "gray"))]
      luma_scratch: Vec::new(),
      #[cfg(feature = "rgb-legacy")]
      legacy_rgb_native_scratch: Vec::new(),
      #[cfg(feature = "rgb-legacy")]
      legacy_rgb_packed_scratch: Vec::new(),
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "gray",
        feature = "yuv-444-packed",
        feature = "y2xx",
        feature = "v210",
        feature = "yuv-planar",
        feature = "yuv-semi-planar"
      ))]
      rgb_scratch_u16: Vec::new(),
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "gray",
        feature = "mono",
        feature = "yuva",
        feature = "yuv-444-packed"
      ))]
      rgba_scratch: Vec::new(),
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "gray",
        feature = "yuva",
        feature = "yuv-444-packed"
      ))]
      rgba_scratch_u16: Vec::new(),
      #[cfg(any(
        feature = "rgb",
        feature = "gbr",
        feature = "gray",
        feature = "yuva",
        feature = "yuv-444-packed"
      ))]
      rgba_color_scratch_u16: Vec::new(),
      #[cfg(any(feature = "rgb", feature = "gbr"))]
      rgb_scratch_u32: Vec::new(),
      #[cfg(any(feature = "rgb", feature = "gbr"))]
      rgba_scratch_u32: Vec::new(),
      #[cfg(any(feature = "rgb", feature = "gbr"))]
      rgba_color_scratch_u32: Vec::new(),
      #[cfg(any(
        feature = "gray",
        feature = "yuva",
        feature = "yuv-444-packed",
        feature = "y2xx",
        feature = "v210",
        feature = "yuv-planar",
        feature = "yuv-semi-planar"
      ))]
      luma_scratch_u16: Vec::new(),
      #[cfg(feature = "gray")]
      luma_scratch_u32: Vec::new(),
      #[cfg(feature = "gray")]
      luma_scratch_f32: Vec::new(),
      #[cfg(feature = "gray")]
      yaf_stream_f32: None,
      #[cfg(feature = "gray")]
      yaf_filter_stream_f32: None,
      #[cfg(feature = "gray")]
      yaf_stage_scratch_f32: Vec::new(),
      #[cfg(feature = "gray")]
      yaf_plane_scratch_f32: Vec::new(),
      #[cfg(any(
        all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
        feature = "gbr"
      ))]
      rgb_scratch_f32: Vec::new(),
      #[cfg(feature = "gbr")]
      rgb_plane_scratch_f32: Vec::new(),
      #[cfg(feature = "gbr")]
      rgb_plane_scratch_f16: Vec::new(),
      #[cfg(any(
        feature = "gbr",
        all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
      ))]
      rgba_scratch_f32: Vec::new(),
      #[cfg(feature = "gbr")]
      rgba_color_scratch_f32: Vec::new(),
      #[cfg(feature = "gbr")]
      rgba_plane_scratch_f32: Vec::new(),
      #[cfg(feature = "gbr")]
      rgba_plane_scratch_f16: Vec::new(),
      #[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
      rgb_packed_scratch_f16: Vec::new(),
      #[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
      rgba_packed_scratch_f16: Vec::new(),
      #[cfg(feature = "xyz")]
      xyz_scratch_f32: Vec::new(),
      simd: true,
      alpha_mode: F::DEFAULT_ALPHA_MODE,
      #[cfg(feature = "yuv-planar")]
      averaging_domain: AveragingDomain::Encoded,
      #[cfg(feature = "yuv-planar")]
      transfer_function: None,
      #[cfg(feature = "yuv-planar")]
      chroma_location: crate::ChromaLocation::Unspecified,
      #[cfg(feature = "yuv-planar")]
      primaries: crate::Primaries::Unspecified,
      #[cfg(feature = "yuv-planar")]
      st428_interpretation: St428Interpretation::FfmpegTabulated,
      #[cfg(feature = "yuv-planar")]
      transfer: crate::Transfer::Unspecified,
      #[cfg(feature = "yuv-planar")]
      matrix: crate::ColorMatrix::Unspecified,
      #[cfg(feature = "yuv-planar")]
      linear_mode: LinearMode::DisplayReferred,
      #[cfg(all(feature = "yuv-planar", feature = "rgb"))]
      linear_light_frame: None,
      #[cfg(all(feature = "yuv-planar", feature = "rgb"))]
      linear_scene_scratch: Vec::new(),
      // BT.709 by default — matches the implicit weights every
      // YUV→RGB→luma pipeline uses, and is the most common Bayer
      // CCM target. Per-format impls (`MixedSinker<Bayer>` etc.)
      // expose `with_luma_coefficients` for callers whose CCM
      // targets a different gamut.
      #[cfg(any(feature = "bayer", feature = "mono"))]
      luma_coefficients_q8: (54, 183, 19),
      // The unnamed-colorimetry posture: what a spec carrying
      // `ColorMatrix::Unspecified` resolves to, and what the walkers'
      // matrix parameter defaulted to before mediaframe 0.4 moved the
      // question to the sink.
      kernel_matrix: crate::KernelMatrix::Bt709,
      #[cfg(feature = "xyz")]
      target_gamut: crate::KernelGamut::DciP3,
      _fmt: PhantomData,
      _resampler: PhantomData,
    }
  }

  /// Returns `true` iff the sinker will write 8‑bit RGB.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_rgb(&self) -> bool {
    self.rgb.is_some()
  }

  /// Returns `true` iff the sinker will write `u16` RGB at the
  /// source's native bit depth. Only high‑bit‑depth source impls
  /// (currently [`Yuv420p10`](crate::source::Yuv420p10)) honor this
  /// buffer — attaching it on an 8‑bit source format is legal but
  /// no writes occur.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_rgb_u16(&self) -> bool {
    self.rgb_u16.is_some()
  }

  /// Returns `true` iff the sinker will write `f32` RGB. Only
  /// float-source impls (currently [`Rgbf32`](crate::source::Rgbf32))
  /// honor this buffer.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_rgb_f32(&self) -> bool {
    self.rgb_f32.is_some()
  }

  /// Returns `true` iff the sinker will write `half::f16` RGB. Only
  /// half-float-source impls (currently [`Rgbf16`](crate::source::Rgbf16))
  /// honor this buffer.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_rgb_f16(&self) -> bool {
    self.rgb_f16.is_some()
  }

  /// Returns `true` iff the sinker will write `f32` RGBA. Only
  /// float-planar-GBR source impls (`Gbrpf32` / `Gbrapf32` / `Gbrpf16` /
  /// `Gbrapf16`) honor this buffer.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_rgba_f32(&self) -> bool {
    self.rgba_f32.is_some()
  }

  /// Returns `true` iff the sinker will write `half::f16` RGBA. Only
  /// float-planar-GBR source impls (`Gbrpf32` / `Gbrapf32` / `Gbrpf16` /
  /// `Gbrapf16`) honor this buffer.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_rgba_f16(&self) -> bool {
    self.rgba_f16.is_some()
  }

  /// Returns `true` iff the sinker will write 8‑bit RGBA. The
  /// fourth byte per pixel is alpha — opaque (`0xFF`) by default
  /// when the source has no alpha plane.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_rgba(&self) -> bool {
    self.rgba.is_some()
  }

  /// Returns `true` iff the sinker will write `u16` RGBA at the
  /// source's native bit depth. The fourth `u16` per pixel is alpha
  /// — opaque (`(1 << BITS) - 1`) by default when the source has no
  /// alpha plane. Only high‑bit‑depth source impls honor this
  /// buffer.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_rgba_u16(&self) -> bool {
    self.rgba_u16.is_some()
  }

  /// Returns `true` iff the sinker will write luma.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_luma(&self) -> bool {
    self.luma.is_some()
  }

  /// Returns `true` iff the sinker will write native-depth `u16`
  /// luma. Only honored by per-format impls that wire the
  /// `with_luma_u16` accessor (Tier 4 source families).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_luma_u16(&self) -> bool {
    self.luma_u16.is_some()
  }

  /// Returns `true` iff the sinker will write `f32` luma.
  /// Only honored by `Grayf32` source impls.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_luma_f32(&self) -> bool {
    self.luma_f32.is_some()
  }

  /// Returns `true` iff the sinker will write HSV.
  ///
  /// Gated on the same 15-feature any as the `hsv` field — under
  /// `--features "alloc"` alone, no per-format `process` impl
  /// compiles, the field doesn't exist, and this getter is also gone.
  #[cfg(any(
    feature = "bayer",
    feature = "gbr",
    feature = "gray",
    feature = "mono",
    feature = "rgb",
    feature = "rgb-float",
    feature = "rgb-legacy",
    feature = "v210",
    feature = "xyz",
    feature = "y2xx",
    feature = "yuv-444-packed",
    feature = "yuv-packed",
    feature = "yuv-planar",
    feature = "yuv-semi-planar",
    feature = "yuva",
  ))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn produces_hsv(&self) -> bool {
    self.hsv.is_some()
  }

  /// Frame width in pixels. Declared at construction.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn width(&self) -> usize {
    self.width
  }

  /// Frame height in pixels. Declared at construction.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn height(&self) -> usize {
    self.height
  }

  /// Output width in pixels — what output buffers validate against.
  /// Equals [`Self::width`] unless constructed via
  /// [`MixedSinker::with_resampler`] with a non-identity plan.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn out_width(&self) -> usize {
    self.out_width
  }

  /// Output height in pixels — what output buffers validate against.
  /// Equals [`Self::height`] unless constructed via
  /// [`MixedSinker::with_resampler`] with a non-identity plan.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn out_height(&self) -> usize {
    self.out_height
  }

  /// The resampling plan fixed at construction — `Some` only for
  /// sinkers built via [`MixedSinker::with_resampler`] with a
  /// non-identity strategy.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn resample_plan(&self) -> Option<&ResamplePlan> {
    self.plan.as_ref()
  }

  /// Capacity of the source-row staging scratch — a white-box probe
  /// for the resample ordering tests (a rejected row must not have
  /// grown the scratch). Gated on `std` like the tests that consume it,
  /// so it is not dead code in the alloc-only test build.
  // The `gbr` family routes its resample ordering through the dedicated
  // 4-channel RGBA tail and the high-bit u16 tail (probed via the
  // dedicated `*_scratch` capacities), and cannot construct an
  // out-of-sequence row to grow this u8 scratch (`GbrapRow` / `GbrpRow`
  // are `pub(crate)` in mediaframe), so no `gbr` test consumes this probe
  // — keep it out of the gate to avoid a `gbr`-solo dead-code warning.
  #[cfg(all(
    test,
    feature = "std",
    any(
      feature = "rgb",
      feature = "xyz",
      feature = "bayer",
      feature = "gray",
      feature = "yuv-packed",
      feature = "yuv-444-packed",
      feature = "yuva",
      feature = "rgb-legacy",
      all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
    )
  ))]
  pub(crate) fn rgb_scratch_capacity(&self) -> usize {
    self.rgb_scratch.capacity()
  }

  /// Capacity of the legacy 16-bit packed-RGB source-row native-channel
  /// staging scratch — a white-box probe for the resample ordering tests
  /// (a rejected row must not have grown the scratch). Gated on
  /// `rgb-legacy` + `std` like the tests that consume it.
  #[cfg(all(test, feature = "rgb-legacy", feature = "std"))]
  pub(crate) fn legacy_rgb_native_scratch_capacity(&self) -> usize {
    self.legacy_rgb_native_scratch.capacity()
  }

  /// Capacity of the legacy 16-bit packed-RGB re-packed-source-row
  /// staging scratch — a white-box probe for the resample tests (a
  /// native-`u16`-only sink, which copies the binned row at native depth,
  /// must still size it because the re-pack feeds the `rgb_u16` kernel;
  /// a no-output sink must not). Gated on `rgb-legacy` + `std`.
  #[cfg(all(test, feature = "rgb-legacy", feature = "std"))]
  pub(crate) fn legacy_rgb_packed_scratch_capacity(&self) -> usize {
    self.legacy_rgb_packed_scratch.capacity()
  }

  /// Whether the high-bit packed-RGB `u16` area stream has been
  /// created — a white-box probe for the resample ordering tests (an
  /// out-of-sequence first row must be rejected before the stream is
  /// allocated). Gated on `std` + the families that bin into the u16
  /// tail (`rgb` high-bit packed RGB, `yuv-444-packed` high-bit packed
  /// 4:4:4 YUV color group, `y2xx` / `v210` high-bit packed 4:2:2 YUV).
  #[cfg(all(
    test,
    feature = "std",
    any(
      feature = "rgb",
      feature = "yuv-444-packed",
      feature = "y2xx",
      feature = "v210",
      feature = "yuv-planar",
      feature = "yuv-semi-planar"
    )
  ))]
  pub(crate) fn rgb_stream_u16_allocated(&self) -> bool {
    self.rgb_stream_u16.is_some()
  }

  /// Whether the **4-channel** high-bit packed-RGBA `u16` area stream has been
  /// created — the alpha-aware twin of [`Self::rgb_stream_u16_allocated`], a
  /// white-box probe for the RFC #238 tail-collapse packed-RGBA `u16` / `Gbrap`
  /// resample atomicity tests (a first-row allocation failure must leave the
  /// field `None`, never a partial commit). Gated on `std` + the `rgb` family
  /// whose packed-RGBA `u16` resample atomicity test probes it.
  #[cfg(all(test, feature = "std", feature = "rgb"))]
  pub(crate) fn rgba_stream_u16_allocated(&self) -> bool {
    self.rgba_stream_u16.is_some()
  }

  /// Whether the **3-channel** native-`u32` packed-RGB area stream has been
  /// created — the `u32` twin of [`Self::rgb_stream_u16_allocated`], a white-box
  /// probe for the RFC #238 tail-collapse native-`u32` packed-RGB resample
  /// atomicity tests (a first-row allocation failure must leave the field `None`,
  /// never a partial commit). Gated on `std` + the `rgb` family whose packed-RGB
  /// `u32` resample atomicity test probes it.
  #[cfg(all(test, feature = "std", feature = "rgb"))]
  pub(crate) fn rgb_stream_u32_allocated(&self) -> bool {
    self.rgb_stream_u32.is_some()
  }

  /// Whether the **4-channel** native-`u32` packed-RGBA area stream has been
  /// created — the alpha-aware `u32` twin of [`Self::rgb_stream_u32_allocated`], a
  /// white-box probe for the RFC #238 tail-collapse native-`u32` packed-RGBA /
  /// `Gbrap32` resample atomicity tests (a first-row allocation failure must leave
  /// the field `None`, never a partial commit). Gated on `std` + the `rgb` family
  /// whose packed-RGBA `u32` resample atomicity test probes it.
  #[cfg(all(test, feature = "std", feature = "rgb"))]
  pub(crate) fn rgba_stream_u32_allocated(&self) -> bool {
    self.rgba_stream_u32.is_some()
  }

  /// Whether the packed-float-RGB `f32` area stream has been created —
  /// a white-box probe for the resample ordering tests (an
  /// out-of-sequence first row must be rejected before the stream is
  /// allocated). Gated on `std` and the families that drive the float
  /// tail: the `rgb-float` family (fenced to the engine) or `gbr` (which
  /// already carries the engine).
  #[cfg(all(
    test,
    feature = "std",
    any(
      all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
      feature = "gbr"
    )
  ))]
  pub(crate) fn rgb_stream_f32_allocated(&self) -> bool {
    self.rgb_stream_f32.is_some()
  }

  /// Whether the **4-channel** packed-RGBA / planar-GBR+alpha `f32` area stream
  /// has been created — a white-box probe for the resample tests (a no-output sink
  /// must not allocate it; a first-row allocation failure must leave it `None`, the
  /// RFC #238 tail-collapse atomicity proof). Gated on `std` + the packed-float-RGBA
  /// (`rgb-float`, engine fenced in) and planar-GBR+alpha (`gbr`) families that bin
  /// through the shared 4-channel `rgba_stream_f32`.
  #[cfg(all(
    test,
    feature = "std",
    any(
      all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
      feature = "gbr"
    )
  ))]
  pub(crate) fn rgba_stream_f32_allocated(&self) -> bool {
    self.rgba_stream_f32.is_some()
  }

  /// Whether the **3-channel** packed-`f32` **filter** stream has been
  /// created — a white-box probe for the float planar GBR filter resample
  /// tests (a no-output sink must not allocate it). Gated on `gbr` + `std`
  /// like the tests that consume it (the `rgb-float` consumers have their own
  /// coverage; the `gbr` arm widens the gate here so the probe is available
  /// in a `gbr`-only build).
  #[cfg(all(test, feature = "gbr", feature = "std"))]
  pub(crate) fn rgb_filter_stream_f32_allocated(&self) -> bool {
    self.rgb_filter_stream_f32.is_some()
  }

  /// Whether the **4-channel** float planar GBR+alpha `f32` **filter** stream
  /// has been created — a white-box probe for the filter resample tests (a
  /// no-output sink must not allocate it). Gated on `gbr` + `std` like the
  /// tests that consume it.
  #[cfg(all(test, feature = "gbr", feature = "std"))]
  pub(crate) fn rgba_filter_stream_f32_allocated(&self) -> bool {
    self.rgba_filter_stream_f32.is_some()
  }

  /// Capacity of the float planar-GBR G/B/R plane scratch — a white-box
  /// probe for the resample tests (an `rgb_f32`-only sink must not grow
  /// it). Gated on `gbr` + `std` like the test that consumes it.
  #[cfg(all(test, feature = "gbr", feature = "std"))]
  pub(crate) fn rgb_plane_scratch_capacity(&self) -> usize {
    self.rgb_plane_scratch_f32.capacity()
  }

  /// Capacity of the half-float planar-GBR ([`Gbrpf16`](crate::source::Gbrpf16))
  /// G/B/R f16 plane scratch — a white-box probe for the resample tests (a
  /// no-output sink must not grow it). Gated on `gbr` + `std` like the test
  /// that consumes it.
  #[cfg(all(test, feature = "gbr", feature = "std"))]
  pub(crate) fn rgb_plane_scratch_f16_capacity(&self) -> usize {
    self.rgb_plane_scratch_f16.capacity()
  }

  /// Capacity of the half-float packed-RGB ([`Rgbf16`](crate::source::Rgbf16))
  /// packed f16 scratch row — a white-box probe for the resample tests (a
  /// no-output sink must not grow it). Gated on the `rgb-float` engine fence
  /// + `std` like the test that consumes it.
  #[cfg(all(
    test,
    feature = "std",
    feature = "rgb-float",
    any(feature = "yuv-planar", feature = "rgb")
  ))]
  pub(crate) fn rgb_packed_scratch_f16_capacity(&self) -> usize {
    self.rgb_packed_scratch_f16.capacity()
  }

  /// Whether the [`Xyz12`](crate::source::Xyz12) linear-XYZ `f32` area
  /// stream has been created — a white-box probe for the resample
  /// ordering tests (an out-of-sequence first row must be rejected
  /// before the stream is allocated). Gated on `xyz` and `std` like the
  /// tests that consume it.
  #[cfg(all(test, feature = "xyz", feature = "std"))]
  pub(crate) fn xyz_stream_f32_allocated(&self) -> bool {
    self.xyz_stream_f32.is_some()
  }

  /// Capacity of the [`Xyz12`](crate::source::Xyz12) source-row
  /// linear-XYZ staging scratch — a white-box probe for the resample
  /// ordering tests (a rejected row must not have grown the scratch).
  /// Gated on `xyz` and `std` like the tests that consume it.
  #[cfg(all(test, feature = "xyz", feature = "std"))]
  pub(crate) fn xyz_scratch_f32_capacity(&self) -> usize {
    self.xyz_scratch_f32.capacity()
  }

  /// Whether the per-frame output-set snapshot has been committed (frozen)
  /// — a white-box probe for the RFC #238 tail-collapse atomicity tests: a
  /// first-row scratch OOM must leave `resample_outputs` UNCOMMITTED so the
  /// freeze half of the commit-together shape is proven directly, without
  /// relying on a walker retry (which would `begin_frame`-reset the field
  /// before it could be observed). Gated to cover the `xyz` + `mono` (Pal8) +
  /// `rgb-legacy` + `yuv-planar` (the 4:2:0 high-bit native rebuild-atomicity
  /// suite) test-module gates that consume it.
  #[cfg(all(
    test,
    feature = "std",
    any(
      feature = "xyz",
      feature = "mono",
      feature = "rgb-legacy",
      feature = "yuv-planar"
    )
  ))]
  pub(crate) fn resample_outputs_frozen(&self) -> bool {
    self.resample_outputs.is_some()
  }

  /// Whether the single-channel luma `u8` area stream has been created
  /// — a white-box probe for the [`Gray8`](crate::source::Gray8),
  /// `mono`, and packed-YUV-4:2:2 resample ordering tests (an
  /// out-of-sequence first row must be rejected before the stream is
  /// allocated). Gated on `gray`/`mono`/`yuv-packed` and `std` like the
  /// tests that consume it.
  #[cfg(all(
    test,
    feature = "std",
    any(feature = "gray", feature = "mono", feature = "yuv-packed")
  ))]
  pub(crate) fn luma_stream_allocated(&self) -> bool {
    self.luma_stream.is_some()
  }

  /// Whether the 3-channel packed-RGB `u8` area stream has been created
  /// — a white-box probe for the packed-YUV-4:2:2, high-bit packed 4:4:4
  /// / 4:2:2 YUV, legacy packed-RGB, and RFC #238 tail-collapse
  /// (packed-RGB / GBR / Bayer) resample ordering tests (an out-of-sequence
  /// first row must be rejected — and a first-row allocation failure must
  /// leave the field `None` — before the stream is committed). Gated on
  /// `std` + the families that bin their converted u8 RGB through the shared
  /// `rgb_stream`.
  #[cfg(all(
    test,
    feature = "std",
    any(
      feature = "yuv-packed",
      feature = "yuv-444-packed",
      feature = "y2xx",
      feature = "v210",
      feature = "rgb-legacy",
      feature = "yuv-planar",
      feature = "yuv-semi-planar",
      feature = "rgb",
      feature = "bayer"
    )
  ))]
  pub(crate) fn rgb_stream_allocated(&self) -> bool {
    self.rgb_stream.is_some()
  }

  /// Whether the 4-channel packed-RGBA `u8` area stream has been created —
  /// the alpha-aware twin of [`Self::rgb_stream_allocated`], a white-box
  /// probe for the RFC #238 tail-collapse packed-RGBA / `Gbrap` / `Pal8`
  /// resample atomicity tests (a first-row allocation failure must leave the
  /// field `None`, never a partial commit). Gated on `std` + the `rgb` / `mono`
  /// families whose packed-RGBA / `Pal8` resample atomicity tests probe it.
  #[cfg(all(test, feature = "std", any(feature = "rgb", feature = "mono")))]
  pub(crate) fn rgba_stream_allocated(&self) -> bool {
    self.rgba_stream.is_some()
  }

  /// Whether the 3-channel packed-RGB `u8` **filter** stream has been
  /// created — a white-box probe for the 8-bit packed-YUV filter resample
  /// ordering tests (a no-output sink and an out-of-sequence first row must
  /// not allocate it). Gated on `std` + `yuv-packed` like the tests that
  /// consume it.
  #[cfg(all(test, feature = "std", feature = "yuv-packed"))]
  pub(crate) fn rgb_filter_stream_allocated(&self) -> bool {
    self.rgb_filter_stream.is_some()
  }

  /// Whether the 4-channel packed-RGBA `u8` **filter** stream has been
  /// created — a white-box probe for the `Pal8` filter resample ordering
  /// tests (a no-output sink must not allocate it). Gated on `std` + `mono`
  /// like the tests that consume it.
  #[cfg(all(test, feature = "std", feature = "mono"))]
  pub(crate) fn rgba_filter_stream_allocated(&self) -> bool {
    self.rgba_filter_stream.is_some()
  }

  /// Whether the single-channel native-Y `u8` **filter** stream has been
  /// created — the filter twin of [`Self::luma_stream_allocated`], a
  /// white-box probe for the 8-bit packed-YUV and `mono` filter resample
  /// ordering tests. Gated on `std` + `mono`/`yuv-packed` like the tests
  /// that consume it.
  #[cfg(all(test, feature = "std", any(feature = "mono", feature = "yuv-packed")))]
  pub(crate) fn luma_filter_stream_allocated(&self) -> bool {
    self.luma_filter_stream.is_some()
  }

  /// Capacity of the packed-YUV-4:2:2 source-row Y de-interleave staging
  /// scratch — a white-box probe for the resample ordering tests (a
  /// rejected row must not have grown the scratch). Gated on
  /// `yuv-packed` and `std` like the tests that consume it.
  #[cfg(all(test, feature = "std", feature = "yuv-packed"))]
  pub(crate) fn luma_scratch_capacity(&self) -> usize {
    self.luma_scratch.capacity()
  }

  /// Capacity of the source-row `u16` RGB staging scratch — a white-box
  /// probe for the high-bit packed 4:4:4 YUV resample ordering tests (a
  /// rejected row must not have grown the scratch). Gated on
  /// `yuv-444-packed` + `std` like the tests that consume it.
  #[cfg(all(test, feature = "std", feature = "yuv-444-packed"))]
  pub(crate) fn rgb_scratch_u16_capacity(&self) -> usize {
    self.rgb_scratch_u16.capacity()
  }

  /// Capacity of the source-row `u16` luma (native Y) staging scratch —
  /// a white-box probe for the high-bit packed 4:4:4 YUV resample
  /// ordering tests (a rejected row must not have grown the scratch).
  /// Gated on `yuv-444-packed` + `std` like the tests that consume it.
  #[cfg(all(test, feature = "std", feature = "yuv-444-packed"))]
  pub(crate) fn luma_scratch_u16_capacity(&self) -> usize {
    self.luma_scratch_u16.capacity()
  }

  /// Whether the single-channel **u16** luma area stream has been
  /// created — a white-box probe for the
  /// [`Gray16`](crate::source::Gray16) and high-bit packed 4:4:4 YUV
  /// (`yuv-444-packed`) / high-bit packed 4:2:2 YUV (`y2xx` / `v210`) /
  /// high-bit planar YUV (`yuv-planar`) resample ordering tests (an
  /// out-of-sequence first row must be rejected before the stream is
  /// allocated). Gated on `gray` / `yuv-444-packed` / `y2xx` / `v210` /
  /// `yuv-planar` / `yuv-semi-planar` and `std` like the tests that
  /// consume it.
  #[cfg(all(
    test,
    feature = "std",
    any(
      feature = "gray",
      feature = "yuv-444-packed",
      feature = "y2xx",
      feature = "v210",
      feature = "yuv-planar",
      feature = "yuv-semi-planar"
    )
  ))]
  pub(crate) fn luma_stream_u16_allocated(&self) -> bool {
    self.luma_stream_u16.is_some()
  }

  /// Whether the single-channel **f32** luma area stream has been
  /// created — a white-box probe for the
  /// [`Grayf32`](crate::source::Grayf32) resample ordering tests (an
  /// out-of-sequence first row must be rejected before the stream is
  /// allocated). Gated on `gray` and `std` like the tests that consume
  /// it.
  #[cfg(all(test, feature = "std", feature = "gray"))]
  pub(crate) fn luma_stream_f32_allocated(&self) -> bool {
    self.luma_stream_f32.is_some()
  }

  /// Capacity of the [`Grayf32`](crate::source::Grayf32) source-row
  /// host-native `f32` luma staging scratch — a white-box probe for the
  /// resample tests (a rejected row must not have grown the scratch).
  /// Gated on `gray` and `std` like the tests that consume it.
  #[cfg(all(test, feature = "std", feature = "gray"))]
  pub(crate) fn luma_scratch_f32_capacity(&self) -> usize {
    self.luma_scratch_f32.capacity()
  }

  /// Returns `true` iff row primitives dispatch to their SIMD backend.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn simd(&self) -> bool {
    self.simd
  }

  /// Returns `true` iff resampled processing may take the native
  /// decimation tier. See [`Self::with_native`].
  #[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
  #[cfg_attr(not(feature = "yuv-planar"), allow(dead_code))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn native(&self) -> bool {
    self.native
  }

  /// Toggles the native decimation tier in place. See
  /// [`Self::with_native`] for the consuming builder variant.
  #[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn set_native(&mut self, native: bool) -> &mut Self {
    self.native = native;
    self
  }

  /// Sets whether resampled processing may take the native decimation
  /// tier (bin native planes, convert once at output resolution).
  /// Defaults to `true`, mirroring [`Self::with_simd`].
  ///
  /// The tiers differ in color SEMANTICS, not just speed: native
  /// averages in the source (YUV) domain and converts once — the
  /// fused semantics video pipelines (libswscale-class) produce —
  /// while the row-stage tier converts every source pixel first and
  /// averages in RGB, matching `cv2.INTER_AREA` applied to decoded
  /// RGB. Luma is bit-identical either way (both tiers bin the same Y
  /// plane). In-gamut color differs only by per-pixel rounding;
  /// OUT-OF-GAMUT content (super-blacks/whites, illegal chroma
  /// excursions) diverges as far as the content sits outside the
  /// gamut — unbounded in principle, with measured examples of
  /// 34/255 on a mild extreme checkerboard and 117/255 on a crafted
  /// Bt2020 limited-range case (both pinned by regression). Pass
  /// `false` for strict RGB-domain `INTER_AREA` semantics at
  /// source-resolution conversion cost.
  ///
  /// Under a `yuv-semi-planar`-solo build (no `yuv-planar`) the native
  /// tier is not compiled, so the high-bit P-format sinks always use the
  /// row-stage tail; this flag is then accepted but inert.
  #[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn with_native(mut self, native: bool) -> Self {
    self.set_native(native);
    self
  }

  /// Toggles the SIMD dispatch in place. See [`Self::with_simd`] for the
  /// consuming builder variant.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn set_simd(&mut self, simd: bool) -> &mut Self {
    self.simd = simd;
    self
  }

  /// Sets whether row primitives dispatch to their SIMD backend.
  /// Defaults to `true` — pass `false` to force the scalar reference
  /// path (intended for benchmarks, fuzzing, and differential
  /// testing). See [`Self::set_simd`] for the in‑place variant.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn with_simd(mut self, simd: bool) -> Self {
    self.set_simd(simd);
    self
  }

  /// Returns how the source's packed alpha is interpreted when the
  /// frame is area-resampled. See [`Self::with_alpha_mode`].
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn alpha_mode(&self) -> AlphaMode {
    self.alpha_mode
  }

  /// Sets the alpha interpretation in place. See
  /// [`Self::with_alpha_mode`] for the consuming builder variant.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn set_alpha_mode(&mut self, mode: AlphaMode) -> &mut Self {
    self.alpha_mode = mode;
    self
  }

  /// Sets how the source's packed alpha relates to its color channels
  /// when the frame is **area-resampled**, overriding the per-format
  /// default ([`DefaultAlphaMode`], [`AlphaMode::Straight`] for every
  /// packed-RGBA source pixon ships). Mirrors the [`Self::with_simd`]
  /// builder pattern.
  ///
  /// [`AlphaMode::Premultiplied`] makes the packed-RGBA resample tail
  /// bin premultiplied color and un-premultiply per finalized output
  /// row, so fully-transparent pixels never bleed their stored color
  /// into a downscaled result. The mode is a no-op for the direct
  /// (identity-plan) conversions — which copy alpha through untouched —
  /// and for every non-RGBA source. See [`Self::set_alpha_mode`] for
  /// the in-place variant.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn with_alpha_mode(mut self, mode: AlphaMode) -> Self {
    self.set_alpha_mode(mode);
    self
  }

  /// Returns the RFC #238 [averaging domain](AveragingDomain) an area
  /// downscale resamples in. See [`Self::with_averaging_domain`].
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn averaging_domain(&self) -> AveragingDomain {
    self.averaging_domain
  }

  /// Sets the averaging domain in place. See
  /// [`Self::with_averaging_domain`] for the consuming builder variant.
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn set_averaging_domain(&mut self, domain: AveragingDomain) -> &mut Self {
    self.averaging_domain = domain;
    self
  }

  /// Selects the colour domain an area downscale averages in, overriding
  /// the default [`AveragingDomain::Encoded`]. Mirrors the
  /// [`Self::with_simd`] builder pattern.
  ///
  /// [`AveragingDomain::Linear`] opts the **planar 8-bit YUV** family
  /// (`Yuv420p` / `Yuv422p` / `Yuv444p` / `Yuv440p`) into a
  /// physically-correct linear-light downscale: each source pixel is
  /// decoded to RGB, lifted to linear light by the
  /// [`TransferFunction`] EOTF, box-averaged, and re-encoded by the OETF.
  /// Because the YUV→RGB convert is affine (no transfer), this lands at a
  /// materially different RGB than the default encoded average — the
  /// quality-vs-default trade the domain offers. The transfer curve is
  /// resolved from the sink's [`KernelMatrix`](crate::KernelMatrix) unless overridden with
  /// [`Self::with_transfer_function`].
  ///
  /// The domain is a no-op for every other format and for the direct
  /// (identity-plan) conversions; [`AveragingDomain::Encoded`] keeps the
  /// bit-identical pre-RFC behaviour. See [`Self::set_averaging_domain`]
  /// for the in-place variant.
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn with_averaging_domain(mut self, domain: AveragingDomain) -> Self {
    self.set_averaging_domain(domain);
    self
  }

  /// Returns the caller's [`TransferFunction`] override, or `None` when the
  /// linear-light transfer is derived from the [`KernelMatrix`](crate::KernelMatrix). See
  /// [`Self::with_transfer_function`].
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn transfer_function(&self) -> Option<TransferFunction> {
    self.transfer_function
  }

  /// Sets the transfer-function override in place. See
  /// [`Self::with_transfer_function`] for the consuming builder variant.
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn set_transfer_function(&mut self, tf: TransferFunction) -> &mut Self {
    self.transfer_function = Some(tf);
    self
  }

  /// Pins the [`TransferFunction`] the [`AveragingDomain::Linear`] path
  /// linearises and re-encodes through, overriding the per-`KernelMatrix`
  /// default ([`TransferFunction::for_matrix`]: the [`ColorMatrix::Rgb`](crate::ColorMatrix::Rgb)
  /// identity → [`TransferFunction::Srgb`], every YCbCr video matrix →
  /// [`TransferFunction::Bt1886`]).
  ///
  /// Use this when the source's transfer characteristics are known out of
  /// band (pixon's YUV row stage carries the matrix but not the
  /// transfer). The override is consulted only on the linear-light path;
  /// the encoded and direct paths ignore it. See
  /// [`Self::set_transfer_function`] for the in-place variant.
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn with_transfer_function(mut self, tf: TransferFunction) -> Self {
    self.set_transfer_function(tf);
    self
  }

  /// The [`ChromaLocation`](crate::ChromaLocation) driving siting-aware
  /// 4:2:0 chroma upsampling (#302). Defaults to
  /// [`ChromaLocation::Unspecified`](crate::ChromaLocation::Unspecified).
  /// See [`Self::with_chroma_location`].
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn chroma_location(&self) -> crate::ChromaLocation {
    self.chroma_location.clone()
  }

  /// The source colour [`Primaries`](crate::Primaries) carried via
  /// [`Self::with_color_spec`] — the coefficient source for the
  /// [`KernelMatrix::ChromaDerivedNcl`](crate::KernelMatrix::ChromaDerivedNcl)
  /// decode (#303). Defaults to
  /// [`Primaries::Unspecified`](crate::Primaries::Unspecified).
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn primaries(&self) -> crate::Primaries {
    self.primaries.clone()
  }

  /// How [`Primaries::SmpteSt428`](crate::Primaries::SmpteSt428) is interpreted
  /// for the [`KernelMatrix::ChromaDerivedNcl`](crate::KernelMatrix::ChromaDerivedNcl)
  /// decode (#310), set via [`Self::with_st428_interpretation`]. Defaults to
  /// [`St428Interpretation::FfmpegTabulated`].
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn st428_interpretation(&self) -> St428Interpretation {
    self.st428_interpretation
  }

  /// The source [`Transfer`](crate::Transfer) carried via
  /// [`Self::with_color_spec`] — the PQ/HLG selector for the
  /// [`ColorMatrix::Ictcp`](crate::ColorMatrix::Ictcp) non-affine decode
  /// (#303). Defaults to
  /// [`Transfer::Unspecified`](crate::Transfer::Unspecified).
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn transfer(&self) -> crate::Transfer {
    self.transfer.clone()
  }

  /// Sets the chroma sample location in place. See
  /// [`Self::with_chroma_location`] for the consuming builder variant.
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_chroma_location(&mut self, loc: crate::ChromaLocation) -> &mut Self {
    self.chroma_location = loc;
    self
  }

  /// Pins the source's [`ChromaLocation`](crate::ChromaLocation) so the
  /// 4:2:0 decode reconstructs chroma at the correct horizontal phase
  /// (#302). Source it from a [`ColorSpec`](crate::ColorSpec):
  /// `sink.with_chroma_location(spec.chroma_location())`.
  ///
  /// The default [`ChromaLocation::Unspecified`](crate::ChromaLocation::Unspecified)
  /// — and every horizontally co-sited value (`Left` / `TopLeft` /
  /// `BottomLeft`) — keeps pixon's nearest-neighbor decode, byte-identical
  /// to the pre-#302 output. The horizontally **centered** sitings (`Center`
  /// / `Top` / `Bottom`, the MPEG-1 / JPEG phase) route the identity-plan
  /// 4:2:0 decode through a phase-0.5 chroma upsample + the 4:4:4 kernels,
  /// correcting the half-pixel chroma shift. Wired for the planar `Yuv420p`
  /// and the semi-planar `Nv12` / `Nv21` identity decodes. The vertical phase
  /// is not yet consumed (see `chroma_420_center_sited_h`); the remaining 4:2:0
  /// families and the resampling tiers ignore this knob until the #302 rollout
  /// extends them. See [`Self::set_chroma_location`] for the in-place variant.
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_chroma_location(mut self, loc: crate::ChromaLocation) -> Self {
    self.set_chroma_location(loc.clone());
    self
  }

  /// Applies a resolved [`ColorSpec`](crate::ColorSpec) in place — the
  /// [`kernel_matrix`](Self::kernel_matrix) every row of the walk is stamped
  /// with, plus the [`ChromaLocation`](crate::ChromaLocation) (siting-aware
  /// 4:2:0 upsampling, #302), the [`primaries`](Self::primaries),
  /// [`transfer`](Self::transfer) and open matrix descriptor the non-affine
  /// decodes read (#303). See [`Self::with_color_spec`].
  ///
  /// # Errors
  ///
  /// [`UnsupportedKernelMatrixError`](crate::UnsupportedKernelMatrixError)
  /// when the spec names a colour matrix pixon has no decode for — see
  /// [`ColorSpec::kernel_matrix`](crate::ColorSpec::kernel_matrix). The
  /// refusal lands here, before a single row is read, rather than the walk
  /// silently producing BT.709 pixels.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_color_spec(
    &mut self,
    spec: &crate::ColorSpec,
  ) -> Result<&mut Self, crate::UnsupportedKernelMatrixError> {
    self.kernel_matrix = spec.kernel_matrix()?;
    #[cfg(feature = "yuv-planar")]
    {
      self.chroma_location = spec.chroma_location();
      self.primaries = spec.primaries();
      self.transfer = spec.transfer();
      self.matrix = spec.matrix();
    }
    Ok(self)
  }

  /// Configures the sink from a resolved [`ColorSpec`](crate::ColorSpec),
  /// completing the **end-to-end ColorSpec decode path** (#301 / #302 / #303).
  ///
  /// A `ColorSpec` is the single carrier of colour intent, and since
  /// mediaframe 0.4 this sink is its single *reader*: the walkers no longer
  /// take a matrix beside the sink, they ask the sink. So one call configures
  /// the whole decode — the closed
  /// [`kernel_matrix`](crate::ColorSpec::kernel_matrix) the rows are stamped
  /// with, the [`chroma_location`](crate::ColorSpec::chroma_location),
  /// the [`primaries`](crate::ColorSpec::primaries),
  /// the [`transfer`](crate::ColorSpec::transfer), and the open
  /// [`matrix`](crate::ColorSpec::matrix) descriptor that carries the four
  /// non-affine tags no `KernelMatrix` can spell. The spec's
  /// [`full_range`](crate::ColorSpec::full_range) is the one half still
  /// walker-consumed — it is a quantisation fact about the *frame*, not a
  /// colour intent the sink owns — so route it via
  /// [`YuvOptions::from_color_spec`](crate::YuvOptions::from_color_spec).
  /// The `primaries` feed the
  /// [`KernelMatrix::ChromaDerivedNcl`](crate::KernelMatrix::ChromaDerivedNcl)
  /// decode (#303), whose `Kr` / `Kb` are derived from them:
  ///
  /// ```
  /// # #[cfg(all(feature = "yuv-planar", feature = "rgb"))] {
  /// use pixon::{
  ///   ChromaLocation, ColorInfo, ColorMatrix, ColorSpec, DynamicRange, KernelMatrix,
  ///   PixelFormat, Primaries, Transfer, YuvOptions, sinker::MixedSinker, source::Yuv420p,
  /// };
  ///
  /// let info = ColorInfo::new(
  ///     Primaries::Bt709, Transfer::Bt709, ColorMatrix::Bt601,
  ///     DynamicRange::Limited, ChromaLocation::Center,
  /// );
  /// let spec = ColorSpec::from_info(PixelFormat::Yuv420p, info);
  /// let opts = YuvOptions::from_color_spec(&spec);
  /// let mut rgb = [0u8; 4 * 2 * 3];
  /// let sink = MixedSinker::<Yuv420p>::new(4, 2)
  ///     .with_rgb(&mut rgb)
  ///     .unwrap()
  ///     .with_color_spec(&spec) // carries siting *and* the row's matrix
  ///     .unwrap();
  /// assert_eq!(sink.chroma_location(), ChromaLocation::Center);
  /// assert_eq!(sink.kernel_matrix(), KernelMatrix::Bt601);
  /// # let _ = opts;
  /// # }
  /// ```
  ///
  /// # Errors
  ///
  /// [`UnsupportedKernelMatrixError`](crate::UnsupportedKernelMatrixError) —
  /// see [`Self::set_color_spec`].
  ///
  /// **`ChromaDerivedNcl` scope.** The `primaries`-derived `ChromaDerivedNcl`
  /// decode is currently wired for `Yuv420p` only (#316). Every other format —
  /// the planar siblings (`Yuv422p` / `Yuv444p` / `Yuv440p` / …) and the
  /// semi-planar `Nv12` / `Nv21` (#302) — resolves `ChromaDerivedNcl` via the
  /// BT.709 matrix-tag fallback (`Coefficients::for_matrix`), ignoring the
  /// `primaries`; the `chroma_location` is still honored. Wiring the
  /// primaries-derived path into those formats is a follow-up. (Setting a
  /// non-`ChromaDerivedNcl` matrix is unaffected — `primaries` are unused
  /// there.)
  ///
  /// See [`Self::set_color_spec`] for the in-place variant and
  /// [`Self::with_chroma_location`] to set the siting directly.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_color_spec(
    mut self,
    spec: &crate::ColorSpec,
  ) -> Result<Self, crate::UnsupportedKernelMatrixError> {
    self.set_color_spec(spec)?;
    Ok(self)
  }

  /// The closed affine selector this sinker stamps on every row of a walk —
  /// what its [`PixelSink::kernel_matrix`](crate::PixelSink::kernel_matrix)
  /// returns. Set by [`Self::set_color_spec`] / [`Self::with_color_spec`];
  /// defaults to [`KernelMatrix::Bt709`](crate::KernelMatrix::Bt709).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn kernel_matrix(&self) -> crate::KernelMatrix {
    self.kernel_matrix
  }

  /// Pins the row selector alone, leaving the rest of the colour description
  /// untouched — the in-crate twin of mediaframe's `*Row::for_tests` door.
  ///
  /// The kernel-parity suites drive one matrix through one kernel and assert
  /// bytes; they are not describing a stream, so routing them through
  /// [`ColorSpec`](crate::ColorSpec) would also reset the siting / primaries /
  /// transfer a siting test had just set. Crate-private and `#[cfg(test)]`, so
  /// it is not a second door onto the public surface: outside these tests a
  /// [`KernelMatrix`](crate::KernelMatrix) still reaches a walk only through
  /// [`ColorSpec::kernel_matrix`](crate::ColorSpec::kernel_matrix).
  ///
  /// The feature union below mirrors the actual caller set (every kernel-parity
  /// suite calls this): `convert::tests`, gated on `yuv-planar` alone, and both
  /// `sinker::mixed::tests` and `convert::source_coverage`, which need a memory
  /// tier (`std` or `alloc`) *and* at least one pixel-format feature — `std`
  /// alone is NOT enough, since every per-format file under `sinker::mixed::tests`
  /// carries its own format `cfg` beneath the outer `std` gate. A plain
  /// `#[cfg(test)]` (or a bare `feature = "std"` branch here) leaves this unused
  /// (and `-D warnings`-fatal) on a format-free build — `alloc`-only *or*
  /// `std`-only — where none of those caller modules produce any content.
  #[cfg(all(
    test,
    any(
      feature = "yuv-planar",
      all(
        any(feature = "std", feature = "alloc"),
        any(
          feature = "xyz",
          feature = "mono",
          feature = "yuv-semi-planar",
          feature = "yuv-packed",
          feature = "yuv-444-packed",
          feature = "y2xx",
          feature = "v210",
          feature = "yuva",
          feature = "rgb",
          feature = "rgb-float",
          feature = "rgb-legacy",
          feature = "gray",
          feature = "gbr",
        ),
      ),
    ),
  ))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub(crate) const fn set_kernel_matrix(&mut self, matrix: crate::KernelMatrix) -> &mut Self {
    self.kernel_matrix = matrix;
    self
  }

  /// Pins the XYZ12 output gamut alone. The [`Self::set_kernel_matrix`]
  /// rationale, on the gamut axis.
  #[cfg(all(test, feature = "xyz"))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub(crate) const fn set_kernel_gamut(&mut self, gamut: crate::KernelGamut) -> &mut Self {
    self.target_gamut = gamut;
    self
  }

  /// The output RGB gamut an XYZ12 walk decodes into — what this sinker's
  /// [`Xyz12Sink::target_gamut`](crate::source::Xyz12Sink::target_gamut)
  /// returns. See [`Self::with_target_gamut`].
  #[cfg(feature = "xyz")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn target_gamut(&self) -> crate::KernelGamut {
    self.target_gamut
  }

  /// Sets the output RGB gamut of an XYZ12 walk in place, exchanging the open
  /// [`DcpTargetGamut`](crate::DcpTargetGamut) descriptor for the closed
  /// selector the 3×3 matmul indexes. See [`Self::with_target_gamut`].
  ///
  /// # Errors
  ///
  /// [`UnsupportedKernelGamutError`](crate::UnsupportedKernelGamutError) when
  /// the descriptor names a gamut this build has no luma basis for — refused
  /// here rather than silently decoded as if it were DCI-P3.
  #[cfg(feature = "xyz")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_target_gamut(
    &mut self,
    gamut: &crate::DcpTargetGamut,
  ) -> Result<&mut Self, crate::UnsupportedKernelGamutError> {
    self.target_gamut = crate::KernelGamut::try_from(gamut)?;
    Ok(self)
  }

  /// Sets the output RGB gamut of an XYZ12 walk, consuming builder.
  ///
  /// The gamut is an *output* axis and this sinker is the output, so — since
  /// mediaframe 0.4 — [`xyz12_to`](crate::source::xyz12_to) asks the sink for
  /// it instead of taking it beside the sink. This is the only door: a
  /// [`KernelGamut`](crate::KernelGamut) cannot be conjured next to the
  /// descriptor that was supposed to choose it.
  ///
  /// # Errors
  ///
  /// [`UnsupportedKernelGamutError`](crate::UnsupportedKernelGamutError) —
  /// see [`Self::set_target_gamut`].
  #[cfg(feature = "xyz")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_target_gamut(
    mut self,
    gamut: &crate::DcpTargetGamut,
  ) -> Result<Self, crate::UnsupportedKernelGamutError> {
    self.set_target_gamut(gamut)?;
    Ok(self)
  }

  /// Returns how the [`AveragingDomain::Linear`] tail decodes `YUV→RGB`
  /// before the EOTF. See [`Self::with_linear_mode`].
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn linear_mode(&self) -> LinearMode {
    self.linear_mode
  }

  /// Sets the [`LinearMode`] in place. See [`Self::with_linear_mode`] for the
  /// consuming builder variant.
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn set_linear_mode(&mut self, mode: LinearMode) -> &mut Self {
    self.linear_mode = mode;
    self
  }

  /// Selects whether the [`AveragingDomain::Linear`] average is
  /// **display-referred** (the default) or **scene-referred** (RFC #238
  /// #244), overriding the default [`LinearMode::DisplayReferred`].
  ///
  /// [`LinearMode::DisplayReferred`] decodes `YUV→RGB` through the production
  /// Q15 kernel, which clamps and quantizes to 8-bit `[0, 255]` before the
  /// EOTF, then averages the in-gamut RGB in linear light — a gamma-correct
  /// resize that discards out-of-gamut excursions.
  /// [`LinearMode::SceneReferred`] decodes the **same affine matrix** in
  /// unclamped `f32`, preserving super-black / super-white and saturated-chroma
  /// excursions, lifts that to linear light, averages, and clamps **only** at
  /// the re-encoded output — the physically faithful average for out-of-gamut
  /// content. The two coincide (modulo `f32` rounding) on content that stays in
  /// gamut through the decode.
  ///
  /// The mode is consulted only on the [`AveragingDomain::Linear`] path of the
  /// planar 8-bit YUV family (`Yuv420p` / `Yuv422p` / `Yuv444p` / `Yuv440p`);
  /// the encoded and direct paths ignore it, so it is a no-op unless
  /// [`Self::with_averaging_domain`]`(`[`AveragingDomain::Linear`]`)` is also
  /// set. See [`Self::set_linear_mode`] for the in-place variant.
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn with_linear_mode(mut self, mode: LinearMode) -> Self {
    self.set_linear_mode(mode);
    self
  }

  /// Sets the [`St428Interpretation`] in place. See
  /// [`Self::with_st428_interpretation`] for the consuming builder variant.
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn set_st428_interpretation(&mut self, interp: St428Interpretation) -> &mut Self {
    self.st428_interpretation = interp;
    self
  }

  /// Selects how [`Primaries::SmpteSt428`](crate::Primaries::SmpteSt428) is
  /// interpreted when it feeds the
  /// [`KernelMatrix::ChromaDerivedNcl`](crate::KernelMatrix::ChromaDerivedNcl)
  /// luma-weight derivation (#310), overriding the default
  /// [`St428Interpretation::FfmpegTabulated`].
  ///
  /// [`St428Interpretation::FfmpegTabulated`] (the default) derives the YCbCr
  /// weights from FFmpeg's tabulated D-Cinema RGB primaries — byte-identical to
  /// the pre-#310 behaviour, so a sink built without this builder is unchanged.
  /// [`St428Interpretation::CieXyz`] treats ST 428-1 as its true CIE-XYZ
  /// encoding and **rejects** a `ChromaDerivedNcl` decode over `SmpteSt428`
  /// primaries with [`MixedSinkerError::St428CieXyzUnsupported`] (a YCbCr matrix
  /// over XYZ data is meaningless; route XYZ-encoded ST 428-1 through the
  /// `xyz12` path instead).
  ///
  /// The interpretation is consulted only for the `ChromaDerivedNcl` +
  /// `SmpteSt428` combination on the planar 8-bit `Yuv420p` decode (the
  /// representative #310 wiring); every other matrix, every non-ST 428-1
  /// primary set, and the not-yet-wired families are unaffected in both modes.
  /// See [`Self::set_st428_interpretation`] for the in-place variant.
  #[cfg(feature = "yuv-planar")]
  #[cfg_attr(not(tarpaulin), inline(always))]
  #[must_use]
  pub const fn with_st428_interpretation(mut self, interp: St428Interpretation) -> Self {
    self.set_st428_interpretation(interp);
    self
  }

  /// Full-frame slot count (`out_width x out_height x channels`) with
  /// overflow checking — **output** geometry, since this sizes the
  /// caller's output buffers (`out == source` under the identity
  /// plan). The result is the minimum required `buf.len()` for any
  /// `&[T]` buffer holding `channels` slots per pixel — bytes for
  /// `&[u8]`, `u16` elements for `&[u16]`, `f32` elements for `&[f32]`,
  /// `f16` elements for `&[half::f16]`. The function does NOT scale by
  /// element size; callers compare against `buf.len()` (which Rust
  /// reports in elements of the slice's element type).
  ///
  /// Returns `Err(GeometryOverflow)` if the product cannot fit in
  /// `usize` — only reachable on 32‑bit targets with extreme dimensions.
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn frame_elems(&self, channels: usize) -> Result<usize, MixedSinkerError> {
    self
      .out_width
      .checked_mul(self.out_height)
      .and_then(|n| n.checked_mul(channels))
      .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
        self.out_width,
        self.out_height,
        channels,
      )))
  }

  /// Full-frame element count (`out_width x out_height`) for a
  /// single-channel `&[T]` buffer, with overflow checking. Equivalent
  /// to [`frame_elems(1)`](Self::frame_elems) numerically, but the
  /// dedicated name documents "one slot per pixel" at the call site
  /// (e.g. luma planes) without the channels=1 magic number.
  ///
  /// Returns `Err(GeometryOverflow { channels: 1 })` on overflow.
  ///
  /// Consumed by every non-Bayer sinker family; Bayer is RGB-only and
  /// has no single-channel pixel-count sizing.
  #[cfg(any(
    feature = "gbr",
    feature = "gray",
    feature = "mono",
    feature = "rgb",
    feature = "rgb-float",
    feature = "rgb-legacy",
    feature = "v210",
    feature = "xyz",
    feature = "y2xx",
    feature = "yuv-444-packed",
    feature = "yuv-packed",
    feature = "yuv-planar",
    feature = "yuv-semi-planar",
    feature = "yuva",
  ))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn frame_pixels(&self) -> Result<usize, MixedSinkerError> {
    self
      .out_width
      .checked_mul(self.out_height)
      .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
        self.out_width,
        self.out_height,
        1,
      )))
  }
}

impl<'a, F: SourceFormat, R> MixedSinker<'a, F, R> {
  /// Creates an empty [`MixedSinker`] whose **output geometry** is
  /// decided by `resampler`: [`Resampler::plan`] runs once, here, and
  /// every buffer attached afterwards validates against the resulting
  /// output geometry. [`PixelSink::begin_frame`] keeps validating the
  /// walker against the `width x height` **source** geometry, so the
  /// existing frame-mismatch protection is unchanged.
  ///
  /// With [`NoopResampler`] this is equivalent to [`MixedSinker::new`]
  /// (identity plan, infallible in practice).
  ///
  /// Formats route non-identity plans as they wire into the streaming
  /// engine (currently [`Yuv420p`](crate::source::Yuv420p)); the
  /// remaining per-format [`PixelSink`] impls stay pinned to the
  /// default strategy, so a sinker built here for those formats
  /// validates buffers against its output geometry but cannot yet
  /// process frames (see the type-level docs).
  ///
  /// # Errors
  ///
  /// [`MixedSinkerError::Resample`] when the strategy rejects the
  /// requested output geometry — see
  /// [`ResampleError`].
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_resampler(width: usize, height: usize, resampler: R) -> Result<Self, MixedSinkerError>
  where
    R: Resampler,
  {
    let plan = resampler.plan(width, height)?;
    let (out_width, out_height) = match plan.as_ref() {
      Some(plan) => plan.out_dims(),
      None => (width, height),
    };
    let mut sink = Self::with_geometry(width, height, out_width, out_height);
    sink.plan = plan;
    Ok(sink)
  }

  /// Attaches a packed 24-bit RGB output buffer.
  /// Returns `Err(InsufficientRgbBuffer)` if
  /// `buf.len() < out_width x out_height x 3` (output geometry; equals
  /// `width x height x 3` under the default identity resampler), or
  /// `Err(GeometryOverflow)` on 32‑bit targets when the product
  /// overflows.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_rgb(mut self, buf: &'a mut [u8]) -> Result<Self, MixedSinkerError> {
    self.set_rgb(buf)?;
    Ok(self)
  }

  /// In-place variant of [`with_rgb`](Self::with_rgb).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_rgb(&mut self, buf: &'a mut [u8]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(3)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientRgbBuffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.rgb = Some(buf);
    Ok(self)
  }

  // NOTE: `with_rgb_f32` / `set_rgb_f32` and `with_luma_f32` /
  // `set_luma_f32` are **not** declared here. Same rationale as
  // `with_rgb_u16` below: only the float-output formats actually
  // write these buffers, so the setters live on format-specific
  // impl blocks (`Grayf32` writes both; `Rgbf32` and `Rgbf16` only
  // write `rgb_f32`). Attaching an f32 buffer to a sink whose
  // `process` doesn't write it would leave the caller buffer
  // silently stale — the format-specific scoping turns that into a
  // compile error.

  // NOTE: `with_rgb_u16` / `set_rgb_u16` are **not** declared here.
  // They live on a format‑specific impl block further down (currently
  // [`MixedSinker<Yuv420p10>`]) so the buffer can only be attached to
  // sink types whose `PixelSink` impl actually writes it. Attaching a
  // `u16` RGB buffer to a [`Yuv420p`] / [`Nv12`] / [`Nv21`] sink is a
  // compile error, not a silent stale‑state bug. Future high‑bit‑depth
  // markers (12‑bit, 14‑bit, P010) will add their own impl blocks.

  // NOTE: `with_rgba` / `set_rgba` are **not** declared here either —
  // same rationale as `with_rgb_u16` above. The Ship 8 RGBA path is
  // currently wired only on [`MixedSinker<Yuv420p>`]; attaching an
  // RGBA buffer to a sink whose `PixelSink::process` doesn't write
  // it would silently leave the caller buffer untouched while
  // `produces_rgba()` returned `true`. Each format that writes RGBA
  // gets its own format‑specific impl block exposing the accessors.
  // Future formats (NV12 / NV21 / Yuv422p / Yuv444p / P010 / etc.)
  // add their own impl blocks as RGBA support lands.

  // NOTE: `with_rgba_u16` / `set_rgba_u16` are **not** declared here
  // for the same reason — they live on the format‑specific impl
  // blocks for high‑bit‑depth sources that actually write
  // native‑depth RGBA.

  /// Attaches a single-plane luma output buffer.
  /// Returns `Err(InsufficientLumaBuffer)` if
  /// `buf.len() < out_width x out_height` (output geometry), or
  /// `Err(GeometryOverflow)` on 32‑bit overflow.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_luma(mut self, buf: &'a mut [u8]) -> Result<Self, MixedSinkerError> {
    self.set_luma(buf)?;
    Ok(self)
  }

  /// In-place variant of [`with_luma`](Self::with_luma).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_luma(&mut self, buf: &'a mut [u8]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(1)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientLumaBuffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.luma = Some(buf);
    Ok(self)
  }

  /// Attaches three HSV output planes. Returns
  /// `Err(MixedSinkerError::InsufficientHsvPlane(e))` (inspect via
  /// `e.which()` / `e.expected()` / `e.actual()`) naming the first
  /// short plane, or `Err(MixedSinkerError::GeometryOverflow(_))` on
  /// 32-bit overflow.
  ///
  /// HSV is only meaningful when at least one source family is
  /// compiled, so this method is gated on the same 15-feature any as
  /// the per-format `process` impls that consume the `hsv` field.
  #[cfg(any(
    feature = "bayer",
    feature = "gbr",
    feature = "gray",
    feature = "mono",
    feature = "rgb",
    feature = "rgb-float",
    feature = "rgb-legacy",
    feature = "v210",
    feature = "xyz",
    feature = "y2xx",
    feature = "yuv-444-packed",
    feature = "yuv-packed",
    feature = "yuv-planar",
    feature = "yuv-semi-planar",
    feature = "yuva",
  ))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_hsv(
    mut self,
    h: &'a mut [u8],
    s: &'a mut [u8],
    v: &'a mut [u8],
  ) -> Result<Self, MixedSinkerError> {
    self.set_hsv(h, s, v)?;
    Ok(self)
  }

  /// In-place variant of [`with_hsv`](Self::with_hsv).
  #[cfg(any(
    feature = "bayer",
    feature = "gbr",
    feature = "gray",
    feature = "mono",
    feature = "rgb",
    feature = "rgb-float",
    feature = "rgb-legacy",
    feature = "v210",
    feature = "xyz",
    feature = "y2xx",
    feature = "yuv-444-packed",
    feature = "yuv-packed",
    feature = "yuv-planar",
    feature = "yuv-semi-planar",
    feature = "yuva",
  ))]
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_hsv(
    &mut self,
    h: &'a mut [u8],
    s: &'a mut [u8],
    v: &'a mut [u8],
  ) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(1)?;
    if h.len() < expected {
      return Err(MixedSinkerError::InsufficientHsvPlane(
        InsufficientHsvPlane::new(HsvPlane::H, expected, h.len()),
      ));
    }
    if s.len() < expected {
      return Err(MixedSinkerError::InsufficientHsvPlane(
        InsufficientHsvPlane::new(HsvPlane::S, expected, s.len()),
      ));
    }
    if v.len() < expected {
      return Err(MixedSinkerError::InsufficientHsvPlane(
        InsufficientHsvPlane::new(HsvPlane::V, expected, v.len()),
      ));
    }
    self.hsv = Some(HsvFrameMut::new(h, s, v));
    Ok(self)
  }
}

/// Returns `Ok(())` iff the walker's frame dimensions exactly match
/// the sinker's configured dimensions. Called from
/// [`PixelSink::begin_frame`] in every `MixedSinker<F>` impl.
///
/// The sinker's RGB / luma / HSV buffers were sized for
/// `configured_w x configured_h`. A shorter frame would silently
/// leave the bottom rows of those buffers stale from the previous
/// frame; a taller frame would overrun them. Either is a real
/// failure mode, but neither is a panic-worthy bug — the caller can
/// recover by rebuilding the sinker. Returning `Err` before any row
/// is processed guarantees no partial output.
///
/// Consumed by every per-format `MixedSinker<F>::process` impl.
/// Under `--features "alloc"` alone (no per-format family), no
/// `process` impl compiles and this helper would be flagged unused.
#[cfg(any(
  feature = "bayer",
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "rgb",
  feature = "rgb-float",
  feature = "rgb-legacy",
  feature = "v210",
  feature = "xyz",
  feature = "y2xx",
  feature = "yuv-444-packed",
  feature = "yuv-packed",
  feature = "yuv-planar",
  feature = "yuv-semi-planar",
  feature = "yuva",
))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn check_dimensions_match(
  configured_w: usize,
  configured_h: usize,
  frame_w: u32,
  frame_h: u32,
) -> Result<(), MixedSinkerError> {
  let fw = frame_w as usize;
  let fh = frame_h as usize;
  if fw != configured_w || fh != configured_h {
    return Err(MixedSinkerError::DimensionMismatch(DimensionMismatch::new(
      configured_w,
      configured_h,
      frame_w,
      frame_h,
    )));
  }
  Ok(())
}

/// Slice the RGBA row out of an attached RGBA plane buffer. Returns
/// `Err(GeometryOverflow)` if `one_plane_end x 4` wraps `usize` (only
/// reachable on 32-bit targets at extreme dimensions).
///
/// Centralises the duplicated overflow/bounds-check pattern that every
/// `MixedSinker<F>::process` impl runs in both the standalone-RGBA
/// branch and the Strategy-A expand branch.
///
/// Consumed by every non-Bayer sinker family (Bayer is RGB-only, no
/// RGBA path).
#[cfg(any(
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "rgb",
  feature = "rgb-float",
  feature = "rgb-legacy",
  feature = "v210",
  feature = "xyz",
  feature = "y2xx",
  feature = "yuv-444-packed",
  feature = "yuv-packed",
  feature = "yuv-planar",
  feature = "yuv-semi-planar",
  feature = "yuva",
))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn rgba_plane_row_slice(
  buf: &mut [u8],
  one_plane_start: usize,
  one_plane_end: usize,
  width: usize,
  height: usize,
) -> Result<&mut [u8], MixedSinkerError> {
  let end = one_plane_end
    .checked_mul(4)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width, height, 4,
    )))?;
  let start = one_plane_start * 4; // ≤ end, fits.
  Ok(&mut buf[start..end])
}

/// `u16` analogue of [`rgba_plane_row_slice`] — slices the RGBA row out
/// of an attached `u16` RGBA plane buffer. This helper indexes in `u16`
/// elements, not bytes: like the `u8` variant, RGBA rows use `x 4`
/// elements per pixel, so the overflow check is the same, but the byte
/// offsets differ because each element is 2 bytes. Used by the
/// high-bit-depth 4:2:0 sinkers that fan `u16` RGB out to `u16` RGBA.
///
/// Bayer is RGB-only and packed YUV 4:2:2 / 4:1:1 (`yuv-packed`) emits
/// u8 only; semi-planar 8-bit NV is also u8-only and never reaches a
/// u16 RGBA fan-out path, so this helper is unused under those families.
/// `yuv-semi-planar` enables it for the high-bit P-format sinks
/// (P010/…), which DO fan `u16` RGB out to `u16` RGBA.
#[cfg(any(
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "rgb",
  feature = "rgb-float",
  feature = "rgb-legacy",
  feature = "v210",
  feature = "xyz",
  feature = "y2xx",
  feature = "yuv-444-packed",
  feature = "yuv-planar",
  feature = "yuv-semi-planar",
  feature = "yuva",
))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn rgba_u16_plane_row_slice(
  buf: &mut [u16],
  one_plane_start: usize,
  one_plane_end: usize,
  width: usize,
  height: usize,
) -> Result<&mut [u16], MixedSinkerError> {
  let end = one_plane_end
    .checked_mul(4)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width, height, 4,
    )))?;
  let start = one_plane_start * 4; // ≤ end, fits.
  Ok(&mut buf[start..end])
}

// Test-only allocation failpoint for the RGB scratch grow in
// `rgb_row_buf_or_scratch`. When armed, the next call returns the crate's
// recoverable `AllocationFailed` error WITHOUT growing — letting the
// failure-path regression tests verify that no caller output buffer is
// partially written before the scratch preflight. `Cell<bool>` is plenty
// (single-threaded, take-on-read). Gated on `std` + `yuva` to match the
// only consumers (`thread_local!` needs `std`; the unit-test tree is
// `cfg(all(test, std))` and the failure-path tests are `yuva`-gated), so
// it is not dead code in a `std`-but-no-`yuva` test build.
#[cfg(all(
  test,
  feature = "std",
  any(feature = "yuva", all(feature = "gray", feature = "rgb"))
))]
std::thread_local! {
  static FORCE_RGB_SCRATCH_ALLOC_FAILURE: core::cell::Cell<bool> =
    const { core::cell::Cell::new(false) };
}

/// Arms the [`rgb_row_buf_or_scratch`] allocation failpoint for the **next**
/// call on the current thread, simulating a recoverable allocator refusal of
/// the RGB scratch grow. The flag is consumed (take-on-read) by that call, so
/// it fires exactly once and cannot leak into a later test. Test-only.
#[cfg(all(
  test,
  feature = "std",
  any(feature = "yuva", all(feature = "gray", feature = "rgb"))
))]
pub(crate) fn arm_rgb_scratch_alloc_failure() {
  FORCE_RGB_SCRATCH_ALLOC_FAILURE.with(|f| f.set(true));
}

/// Clears the [`arm_rgb_scratch_alloc_failure`] failpoint without firing it.
/// Needed by RGB-free paths (e.g. the #263 direct YUVA→HSV route) that
/// deliberately never call [`rgb_row_buf_or_scratch`]: arming proves the path
/// does not consume the flag, and this disarm prevents an un-consumed flag
/// from leaking into a later same-thread test. Test-only. Only the `yuva` #263
/// direct-HSV route uses it, so it stays `yuva`-gated (unlike the arm/hook, which
/// the `gray` RGB-scratch atomicity test also exercises).
#[cfg(all(test, feature = "std", feature = "yuva"))]
pub(crate) fn disarm_rgb_scratch_alloc_failure() {
  FORCE_RGB_SCRATCH_ALLOC_FAILURE.with(|f| f.set(false));
}

// Test-only failpoint for the centered-siting 4:2:0 chroma scratch grow
// (#302), mirroring `FORCE_RGB_SCRATCH_ALLOC_FAILURE`. Gated on `std` +
// `yuv-planar` to match its consumers — the `chroma_siting_420` (planar) and
// `chroma_siting_nv` (semi-planar `Nv12` / `Nv21`) atomicity tests
// (`yuv-planar`-gated; `thread_local!` needs `std`) — so it is not dead code in
// a `std`-but-no-`yuva` test build. `reserve_420_chroma_full` (in
// `planar_8bit`) reads it via `super::` for both the planar and semi-planar
// centered paths.
#[cfg(all(test, feature = "std", feature = "yuv-planar"))]
std::thread_local! {
  static FORCE_CHROMA_FULL_ALLOC_FAILURE: core::cell::Cell<bool> =
    const { core::cell::Cell::new(false) };
}

/// Arms the `reserve_420_chroma_full` (in `planar_8bit`) allocation
/// failpoint for the **next** call on the current thread,
/// simulating a recoverable allocator refusal of the centered chroma-scratch
/// grow. Consumed (take-on-read) by that call so it fires exactly once and
/// never leaks across tests. Test-only.
#[cfg(all(test, feature = "std", feature = "yuv-planar"))]
pub(crate) fn arm_chroma_full_alloc_failure() {
  FORCE_CHROMA_FULL_ALLOC_FAILURE.with(|f| f.set(true));
}

// Test-only failpoint for the bottom-sited vertical-phase 4:2:0 chroma lookback
// grow (#302), mirroring `FORCE_CHROMA_FULL_ALLOC_FAILURE`. Gated on `std` +
// `yuv-planar` to match its consumer — the `chroma_siting_420` bottom-sited
// atomicity test. `reserve_420_chroma_prev` (in `planar_8bit`) reads it via
// `super::`.
#[cfg(all(test, feature = "std", feature = "yuv-planar"))]
std::thread_local! {
  static FORCE_CHROMA_PREV_ALLOC_FAILURE: core::cell::Cell<bool> =
    const { core::cell::Cell::new(false) };
}

/// Arms the `reserve_420_chroma_prev` (in `planar_8bit`) allocation failpoint
/// for the **next** call on the current thread, simulating a recoverable
/// allocator refusal of the bottom-sited vertical-phase chroma-lookback grow.
/// Consumed (take-on-read) by that call so it fires exactly once and never leaks
/// across tests. Test-only.
#[cfg(all(test, feature = "std", feature = "yuv-planar"))]
pub(crate) fn arm_chroma_prev_alloc_failure() {
  FORCE_CHROMA_PREV_ALLOC_FAILURE.with(|f| f.set(true));
}

/// Pick an RGB row buffer for the kernel to write into: caller's RGB
/// plane slice when attached, or the growing scratch buffer otherwise
/// (HSV-only callers don't allocate an RGB plane). Returns
/// `Err(GeometryOverflow)` if `width x 3` or `one_plane_end x 3` wraps
/// `usize` — see [`rgba_plane_row_slice`] for the rationale.
///
/// `rgb_scratch` is grown via `Vec::resize` only when too small; the
/// caller keeps the existing capacity across rows so steady-state
/// processing allocates zero times.
///
/// Consumed by per-format `process` impls that need a stable RGB row
/// buffer (either user-attached or scratch-backed). Under
/// `--features "alloc"` alone (no per-format family), no impl
/// compiles and this helper would be flagged unused.
#[cfg(any(
  feature = "bayer",
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "rgb",
  feature = "rgb-float",
  feature = "rgb-legacy",
  feature = "v210",
  feature = "xyz",
  feature = "y2xx",
  feature = "yuv-444-packed",
  feature = "yuv-packed",
  feature = "yuv-planar",
  feature = "yuv-semi-planar",
  feature = "yuva",
))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn rgb_row_buf_or_scratch<'a>(
  rgb: Option<&'a mut [u8]>,
  rgb_scratch: &'a mut Vec<u8>,
  one_plane_start: usize,
  one_plane_end: usize,
  width: usize,
  height: usize,
) -> Result<&'a mut [u8], MixedSinkerError> {
  // Test-only allocation failpoint: simulate a recoverable allocator
  // refusal for the scratch grow WITHOUT actually exhausting memory, so
  // the failure-path regression tests can prove no caller output is
  // partially written before this preflight (see `arm_rgb_scratch_alloc_failure`).
  // `take()` clears the flag so an armed failure fires exactly once and
  // never leaks across tests. Strictly test-only — the non-test build is
  // byte-identical (this hook compiles away entirely).
  #[cfg(all(
    test,
    feature = "std",
    any(feature = "yuva", all(feature = "gray", feature = "rgb"))
  ))]
  if FORCE_RGB_SCRATCH_ALLOC_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(width, height, width, height),
    )));
  }
  match rgb {
    Some(buf) => {
      let end = one_plane_end
        .checked_mul(3)
        .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
          width, height, 3,
        )))?;
      let start = one_plane_start * 3;
      Ok(&mut buf[start..end])
    }
    None => {
      let row_bytes = width
        .checked_mul(3)
        .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
          width, height, 3,
        )))?;
      if rgb_scratch.len() < row_bytes {
        rgb_scratch
          .try_reserve_exact(row_bytes - rgb_scratch.len())
          .map_err(|_| {
            MixedSinkerError::Resample(ResampleError::AllocationFailed(
              crate::resample::PlanGeometry::new(width, height, width, height),
            ))
          })?;
        rgb_scratch.resize(row_bytes, 0);
      }
      Ok(&mut rgb_scratch[..row_bytes])
    }
  }
}

/// Grows `scratch` to a single source-width `u16` RGB row
/// (`width * 3` elements) for the **direct** (non-resample) path and
/// returns the slice. Follows the recoverable-allocation contract —
/// `try_reserve_exact` before the resize, mapping allocator refusal to a
/// recoverable [`MixedSinkerError`] instead of aborting in `process` — for
/// the 10-bit packed-RGB `rgba_u16` fan-out, which has no native α kernel
/// and so stages the native RGB row before
/// [`expand_rgb_u16_to_rgba_u16_row`].
#[cfg(feature = "rgb")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn direct_rgb_u16_scratch(
  scratch: &mut Vec<u16>,
  width: usize,
  height: usize,
) -> Result<&mut [u16], MixedSinkerError> {
  let row = width
    .checked_mul(3)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width, height, 3,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(width, height, width, height),
        ))
      })?;
    scratch.resize(row, 0);
  }
  Ok(&mut scratch[..row])
}

/// Grows `rgb_scratch` to a **source-width** RGB row (`width * 3`
/// bytes) and returns the slice, following the planner's recoverable-
/// allocation contract (the exact reserve makes the resize incapable
/// of reallocating; refusal surfaces as `AllocationFailed` in the
/// preflight phase, not an abort in infallible growth).
///
/// The shared staging point for packed-RGB-canonical resampled
/// sources whose row must be channel-swapped or converted to RGB
/// before feeding the area stream. [`MixedSinker<Rgb24>`] skips it —
/// its source is already RGB and feeds the stream with zero copy.
#[cfg(any(
  feature = "yuv-planar",
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "xyz",
  feature = "bayer",
  feature = "mono",
  feature = "yuv-semi-planar",
  feature = "yuv-packed",
  feature = "yuv-444-packed",
  feature = "y2xx",
  feature = "v210",
  feature = "rgb-legacy"
))]
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn source_rgb_scratch<'s>(
  rgb_scratch: &'s mut Vec<u8>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [u8], MixedSinkerError> {
  // Single-shot grow failpoint (RFC #238 tail-collapse tests): refuse this grow as
  // a host OOM would, proving a scratch OOM AFTER a successful stream build leaves
  // the stream field `None` AND `resample_outputs` uncommitted (retryable).
  #[cfg(all(
    test,
    feature = "std",
    any(
      feature = "rgb",
      feature = "gbr",
      feature = "bayer",
      feature = "mono",
      feature = "xyz",
      feature = "rgb-legacy",
      all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
    )
  ))]
  if FORCE_SOURCE_RGB_SCRATCH_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
    )));
  }
  let row_bytes = width
    .checked_mul(3)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.src_h(),
      3,
    )))?;
  if rgb_scratch.len() < row_bytes {
    rgb_scratch
      .try_reserve_exact(row_bytes - rgb_scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    rgb_scratch.resize(row_bytes, 0);
  }
  Ok(&mut rgb_scratch[..row_bytes])
}

// Single-shot source-RGB-scratch grow failpoint for the RFC #238 tail-collapse
// transactional-preflight tests. Gated on the families that stage their converted
// or drop-alpha u8 RGB through this helper (packed-RGB / GBR / Bayer / Pal8 /
// legacy-RGB emit scratch and the `Xyz12` emit's narrow scratch);
// `source_rgb_scratch` consults it under the same gate. Separate from the `try_box`
// box-alloc failpoint so a test can fail the SCRATCH grow specifically (after a
// successful stream construction).
#[cfg(all(
  test,
  feature = "std",
  any(
    feature = "rgb",
    feature = "gbr",
    feature = "bayer",
    feature = "mono",
    feature = "xyz",
    feature = "rgb-legacy",
    all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
  )
))]
std::thread_local! {
  static FORCE_SOURCE_RGB_SCRATCH_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}

/// Arms a single-shot failpoint that makes the next [`source_rgb_scratch`] grow
/// refuse with `AllocationFailed`, exactly as a host OOM would. Proves the
/// commit-together atomic shape (a scratch OOM after a successful stream build
/// leaves the stream field `None` AND `resample_outputs` uncommitted, so the row
/// is retryable). Test-only.
#[cfg(all(
  test,
  feature = "std",
  any(
    feature = "rgb",
    feature = "bayer",
    feature = "mono",
    feature = "xyz",
    feature = "rgb-legacy",
    all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
  )
))]
pub(crate) fn arm_source_rgb_scratch_failure() {
  FORCE_SOURCE_RGB_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// Grows `scratch` to a **source-width** `u8` luma row (`width`
/// bytes) and returns the slice, following the planner's recoverable-
/// allocation contract (the exact reserve makes the resize incapable
/// of reallocating; refusal surfaces as `AllocationFailed` in the
/// preflight phase, not an abort in infallible growth).
///
/// The staging point for `mono` resampling (each 1-bit source row is
/// expanded to source-width 0/255 luma here), for **packed YUV 4:2:2**
/// resampling (the interleaved Y bytes are de-interleaved here), and for
/// the `Ya8` gray+alpha resample (the native Y bytes of the packed
/// `[Y, A]` row are de-interleaved here) before feeding the single-channel
/// area stream. Compiled wherever the packed-RGBA u8 tail is (it threads
/// this for the `Ya8` native-Y stream), so the `rgb` / `gbr` callers — for
/// which the native-Y path is inert — still link.
#[cfg(any(
  feature = "mono",
  feature = "yuv-packed",
  feature = "gray",
  feature = "rgb",
  feature = "gbr",
  feature = "yuv-444-packed",
  feature = "yuva"
))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn source_luma_scratch<'s>(
  scratch: &'s mut Vec<u8>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [u8], MixedSinkerError> {
  // Single-shot grow failpoint (RFC #238 tail-collapse tests): refuse this grow as
  // a host OOM would, proving a scratch OOM AFTER a successful stream build leaves
  // the `mono` luma stream field `None` AND `resample_outputs` uncommitted
  // (retryable). Separate from the `try_box` box-alloc failpoint so a `mono` test
  // can fail the SCRATCH grow specifically.
  #[cfg(all(test, feature = "std", feature = "mono"))]
  if FORCE_SOURCE_LUMA_SCRATCH_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
    )));
  }
  if scratch.len() < width {
    scratch
      .try_reserve_exact(width - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(width, 0);
  }
  Ok(&mut scratch[..width])
}

// Single-shot source-luma-`u8`-scratch grow failpoint for the RFC #238 tail-collapse
// transactional-preflight tests. Gated on `mono` to match the `mono` resample
// atomicity test module; `source_luma_scratch` consults it under the same gate.
#[cfg(all(test, feature = "std", feature = "mono"))]
std::thread_local! {
  static FORCE_SOURCE_LUMA_SCRATCH_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}

/// Arms a single-shot failpoint that makes the next [`source_luma_scratch`] grow
/// refuse with `AllocationFailed`, exactly as a host OOM would. Proves the
/// commit-together atomic shape (a `mono` scratch OOM after a successful stream
/// build leaves the luma stream field `None` AND `resample_outputs` uncommitted, so
/// the row is retryable). Test-only.
#[cfg(all(test, feature = "std", feature = "mono"))]
pub(crate) fn arm_source_luma_scratch_failure() {
  FORCE_SOURCE_LUMA_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// Grows `scratch` to a **source-width** host-native `u16` luma row
/// (`width` elements) and returns the slice, following the planner's
/// recoverable-allocation contract (the exact reserve makes the resize
/// incapable of reallocating; refusal surfaces as `AllocationFailed` in
/// the preflight phase, not an abort in infallible growth).
///
/// The staging point for the [`Gray16`](crate::source::Gray16) resample
/// path (the wire row converts to host-native u16 luma here) and for the
/// high-bit packed YUV families (`yuv-444-packed` / `y2xx` / `v210`),
/// whose native Y is de-interleaved here (each format's own
/// `*_to_luma_u16_row` kernel) before feeding the single-channel u16 luma
/// stream. The u16 twin of [`source_luma_scratch`]. Compiled wherever the
/// high-bit packed-RGBA u16 tail is (it threads this for the `Ya16`
/// native-Y stream), so the `rgb` / `gbr` callers — for which the native-Y
/// path is inert — still link.
#[cfg(any(
  feature = "gray",
  feature = "yuva",
  feature = "yuv-444-packed",
  feature = "y2xx",
  feature = "v210",
  feature = "rgb",
  feature = "gbr",
  feature = "yuv-planar",
  feature = "yuv-semi-planar"
))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn source_luma_u16_scratch<'s>(
  scratch: &'s mut Vec<u16>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [u16], MixedSinkerError> {
  // Single-shot grow failpoint (RFC #238 tail-collapse tests): refuse this grow as
  // a host OOM would, proving a scratch OOM AFTER a successful stream build leaves
  // the u16 gray luma stream field `None` AND `resample_outputs` uncommitted
  // (retryable). Gated on `gray` to match the `Gray16` resample atomicity test.
  #[cfg(all(test, feature = "std", feature = "gray"))]
  if FORCE_SOURCE_LUMA_U16_SCRATCH_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
    )));
  }
  if scratch.len() < width {
    scratch
      .try_reserve_exact(width - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(width, 0);
  }
  Ok(&mut scratch[..width])
}

// Single-shot source-luma-`u16`-scratch grow failpoint for the RFC #238 tail-collapse
// transactional-preflight tests. Gated on `gray` to match the `Gray16` resample
// atomicity test module; `source_luma_u16_scratch` consults it under the same gate.
#[cfg(all(test, feature = "std", feature = "gray"))]
std::thread_local! {
  static FORCE_SOURCE_LUMA_U16_SCRATCH_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}

/// Arms a single-shot failpoint that makes the next [`source_luma_u16_scratch`]
/// grow refuse with `AllocationFailed`, exactly as a host OOM would. Proves the
/// commit-together atomic shape (a `Gray16` scratch OOM after a successful stream
/// build leaves the luma stream field `None` AND `resample_outputs` uncommitted, so
/// the row is retryable). Test-only.
#[cfg(all(test, feature = "std", feature = "gray"))]
pub(crate) fn arm_source_luma_u16_scratch_failure() {
  FORCE_SOURCE_LUMA_U16_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// Source-width host-native `u32` luma staging for the
/// [`Gray32`](crate::source::Gray32) resample path — the wire row converts
/// here (source wire `BE` → host-native `u32` value) before feeding the
/// native-`u32` luma streams, so binning runs at full `u32` precision (the
/// `u32` twin of [`source_luma_u16_scratch`]). Grows the scratch under the
/// planner's recoverable-allocation contract (output-proportional, so a
/// refusal surfaces as `AllocationFailed` in the preflight, never an abort
/// in infallible growth).
#[cfg(feature = "gray")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn source_luma_u32_scratch<'s>(
  scratch: &'s mut Vec<u32>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [u32], MixedSinkerError> {
  if scratch.len() < width {
    scratch
      .try_reserve_exact(width - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(width, 0);
  }
  Ok(&mut scratch[..width])
}

/// The single-channel gray luma stream handed to a [`gray_luma_resample`] emit —
/// the span kind picks the concrete engine (`Area` bins, `Filter` runs the
/// signed-coefficient twin), so the one per-format emit closure serves both. A
/// `None` stream (the pregrow pass) never reaches the emit as a value; `Some`
/// carries whichever kind is fed this frame.
#[cfg(feature = "gray")]
pub(super) enum GrayLumaStreamMut<
  'a,
  E: crate::resample::AreaSample + crate::resample::FilterSample,
> {
  /// Area (box-averaging) luma stream.
  Area(&'a mut crate::resample::AreaStream<E>),
  /// Signed-coefficient filter luma stream.
  Filter(&'a mut crate::resample::FilterStream<E>),
}

/// RFC #238 tail-collapse — the transactional single-channel **gray luma** resample
/// skeleton shared by every pure-gray element family (`Gray8` u8, `GrayN` / `Gray16`
/// u16, `Gray32` u32, `Grayf32` / `Grayf16` f32; the element-generic 1-channel twin
/// of the packed [`packed_rgb_f32_resample`]). Folds the split preflight + luma
/// stream build + per-format emit into one call behind the shared compare-before /
/// commit-after preflight ([`resample_preflight_check_only`]): the missing 1-channel
/// stream is built into a LOCAL, the emit's own source / clamp / RGB scratch is
/// pre-grown via an `emit(None)` grow-only pass, and only THEN do the stream field
/// insert and the output-set freeze COMMIT TOGETHER — so a first-row allocation
/// failure leaves the stream field `None` AND `resample_outputs` uncommitted
/// (retryable even with a changed output attachment, the #180 discipline). Byte-
/// identical to the pre-collapse split path: only the freeze moves past the fallible
/// allocations.
///
/// The span kind selects the engine internally (area bin / signed-coefficient
/// filter) and hands the `emit` the fed stream as a [`GrayLumaStreamMut`], so the one
/// per-format emit closure serves both kinds. `emit(None, ..)` pregrows every
/// emit-owned scratch and returns; `emit(Some(stream), ..)` stages the source luma
/// and derives every attached output. The gray emits carry their own (format-varying)
/// output subset, so the driver threads the full gray superset — `rgb` / `rgba` /
/// `rgb_u16` / `rgba_u16` / `luma` / `luma_u16` / `rgb_f32` / `luma_f32` / `hsv` —
/// and a format passes `&mut None` for the outputs it never emits.
#[cfg(feature = "gray")]
#[allow(clippy::too_many_arguments)]
pub(super) fn gray_luma_resample<E: crate::resample::AreaSample + crate::resample::FilterSample>(
  luma_stream: &mut Option<std::boxed::Box<crate::resample::AreaStream<E>>>,
  luma_filter_stream: &mut Option<std::boxed::Box<crate::resample::FilterStream<E>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  luma_f32: &mut Option<&mut [f32]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  plan: &ResamplePlan,
  idx: usize,
  mut emit: impl FnMut(
    Option<GrayLumaStreamMut<'_, E>>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [f32]>,
    &mut Option<&mut [f32]>,
    &mut Option<HsvFrameMut<'_>>,
  ) -> Result<(), MixedSinkerError>,
) -> Result<(), MixedSinkerError> {
  // Single-kernel filter tail — reject a BICUBLIN plan before any state change (its
  // chroma windows are read only by the `Yuv420p` per-plane route). Ordered first,
  // exactly like the pre-collapse gray path, so even a no-output bicublin call errors.
  plan.ensure_single_kernel_filter()?;
  let need_any = rgb.is_some()
    || rgba.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || luma_f32.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the fed stream is the sole
  // per-row stream), or `None` when no output is attached so the preflight
  // short-circuits to a no-op.
  let expected = if need_any {
    Some(match plan.kind() {
      crate::resample::SpanKind::Area => luma_stream.as_ref().map_or(0, |s| s.next_y()),
      crate::resample::SpanKind::Filter => luma_filter_stream.as_ref().map_or(0, |s| s.next_y()),
    })
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER the
  // fallible stream alloc + scratch pregrow below succeed, so a first-row allocation
  // failure leaves `resample_outputs` uncommitted (retryable with a changed output
  // attachment). A no-output sink short-circuits to a no-op.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    &None,
    &None,
    &None,
    &None,
    hsv,
    luma_f32,
    expected,
    idx,
  )? {
    return Ok(());
  }
  match plan.kind() {
    crate::resample::SpanKind::Area => {
      // Build the missing area stream into a LOCAL (NO field mutation), so the field
      // insert and the freeze commit TOGETHER only after every pre-feed allocation.
      let new_stream = if luma_stream.is_none() {
        let stream =
          crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 1)?;
        Some(crate::resample::try_box(stream).map_err(|_| {
          MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
            crate::resample::PlanGeometry::new(
              plan.src_w(),
              plan.src_h(),
              plan.out_w(),
              plan.out_h(),
            ),
          ))
        })?)
      } else {
        None
      };
      // Pregrow every emit-owned scratch BEFORE the freeze (a `None` stream grows it
      // and returns), so NOTHING fallible runs after the commit.
      emit(
        None, rgb, rgba, rgb_u16, rgba_u16, luma, luma_u16, rgb_f32, luma_f32, hsv,
      )?;
      // Every pre-feed allocation succeeded — COMMIT atomically: insert the stream and
      // freeze the output set together.
      if let Some(s) = new_stream {
        *luma_stream = Some(s);
      }
      frozen_outputs_check(
        resample_outputs,
        luma,
        luma_u16,
        rgb,
        rgba,
        rgb_u16,
        rgba_u16,
        rgb_f32,
        &None,
        &None,
        &None,
        &None,
        hsv,
        luma_f32,
        idx,
      )?;
      let stream = luma_stream.as_deref_mut().expect("created above");
      emit(
        Some(GrayLumaStreamMut::Area(stream)),
        rgb,
        rgba,
        rgb_u16,
        rgba_u16,
        luma,
        luma_u16,
        rgb_f32,
        luma_f32,
        hsv,
      )
    }
    crate::resample::SpanKind::Filter => {
      let new_stream = if luma_filter_stream.is_none() {
        let (fh, fv) = (
          plan
            .filter_h()
            .expect("filter plan carries horizontal windows"),
          plan
            .filter_v()
            .expect("filter plan carries vertical windows"),
        );
        let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 1)?;
        Some(crate::resample::try_box(stream).map_err(|_| {
          MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
            crate::resample::PlanGeometry::new(
              plan.src_w(),
              plan.src_h(),
              plan.out_w(),
              plan.out_h(),
            ),
          ))
        })?)
      } else {
        None
      };
      emit(
        None, rgb, rgba, rgb_u16, rgba_u16, luma, luma_u16, rgb_f32, luma_f32, hsv,
      )?;
      if let Some(s) = new_stream {
        *luma_filter_stream = Some(s);
      }
      frozen_outputs_check(
        resample_outputs,
        luma,
        luma_u16,
        rgb,
        rgba,
        rgb_u16,
        rgba_u16,
        rgb_f32,
        &None,
        &None,
        &None,
        &None,
        hsv,
        luma_f32,
        idx,
      )?;
      let stream = luma_filter_stream.as_deref_mut().expect("created above");
      emit(
        Some(GrayLumaStreamMut::Filter(stream)),
        rgb,
        rgba,
        rgb_u16,
        rgba_u16,
        luma,
        luma_u16,
        rgb_f32,
        luma_f32,
        hsv,
      )
    }
  }
}

/// Feeds the prepared source-width canonical RGB row into the (already
/// sequence-checked) stream and derives every attached output (rgb,
/// rgba, luma, luma_u16, hsv) from each finalized output row.
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "yuv-444-packed",
  feature = "y2xx",
  feature = "v210",
  feature = "yuv-planar",
  feature = "yuv-semi-planar"
))]
#[cfg_attr(
  not(any(
    feature = "rgb",
    feature = "gbr",
    feature = "yuv-444-packed",
    feature = "y2xx",
    feature = "v210",
    feature = "yuv-planar",
    feature = "yuv-semi-planar"
  )),
  allow(dead_code)
)]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_resample_emit(
  stream: &mut impl crate::resample::RowResampler<u8>,
  plan: &ResamplePlan,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  src_rgb: &[u8],
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  let ow = plan.out_w();
  stream.feed_row(idx, src_rgb, use_simd, |oy, out_row| {
    if let Some(buf) = rgb.as_deref_mut() {
      buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(out_row);
    }
    if let Some(buf) = luma.as_deref_mut() {
      crate::row::rgb_to_luma_row(
        out_row,
        &mut buf[oy * ow..(oy + 1) * ow],
        ow,
        matrix,
        full_range,
        use_simd,
      );
    }
    if let Some(buf) = luma_u16.as_deref_mut() {
      crate::row::rgb_to_luma_u16_row(
        out_row,
        &mut buf[oy * ow..(oy + 1) * ow],
        ow,
        matrix,
        full_range,
        use_simd,
      );
    }
    if let Some(hsv) = hsv.as_mut() {
      let (h, s, v) = hsv.hsv();
      crate::row::rgb_to_hsv_row(
        out_row,
        &mut h[oy * ow..(oy + 1) * ow],
        &mut s[oy * ow..(oy + 1) * ow],
        &mut v[oy * ow..(oy + 1) * ow],
        ow,
        use_simd,
      );
    }
    if let Some(buf) = rgba.as_deref_mut() {
      crate::row::expand_rgb_to_rgba_row(out_row, &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow], ow);
    }
  })?;

  Ok(())
}

/// RFC #238 tail-collapse — the transactional packed-RGB **area** resample.
/// Folds the split preflight + area-stream build + [`packed_rgb_resample_emit`]
/// sequence into one call behind the shared compare-before / commit-after
/// preflight
/// ([`resample_preflight_check_only`](resample_preflight_check_only)),
/// mirroring [`planar_dual_resample`](planar_resample::planar_dual_resample): the
/// missing area stream is built into a LOCAL and the source-width scratch is grown
/// with NO field mutation, so the stream field insert and the output-set freeze
/// COMMIT TOGETHER only after every fallible pre-feed allocation succeeds. A
/// first-row allocation failure therefore leaves `rgb_stream` `None` AND
/// `resample_outputs` uncommitted (never a partial commit) — state-atomic and
/// retryable even with a changed output attachment (the #180 discipline). This is
/// a pure reordering of the pre-collapse split path: it emits the identical bytes,
/// only deferring the field mutations past the fallible allocations.
///
/// `direct` is `Some(row)` for the identity source
/// ([`Rgb24`](crate::source::Rgb24) — the packed source row already IS the
/// canonical RGB row, fed zero-copy); the channel-swapped / padding-byte / planar
/// `Gbrp` sources pass `None` and a `convert` closure that stages a source-width
/// canonical RGB row into `rgb_scratch` (the fallible growth is hoisted ahead of
/// the freeze). The per-output emit is [`packed_rgb_resample_emit`].
///
/// The element-type PATTERN-SETTER for the tail-collapse: the u16 / u32 / f32
/// packed-RGB families mirror this shape against their own element-typed stream
/// builder + emit.
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_resample(
  rgb_stream: &mut Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut Vec<u8>,
  direct: Option<&[u8]>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert: impl FnOnce(&mut [u8]),
) -> Result<(), MixedSinkerError> {
  let need_any =
    rgb.is_some() || rgba.is_some() || luma.is_some() || luma_u16.is_some() || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the area stream is the sole
  // per-row stream), or `None` when no output is attached so the preflight
  // short-circuits to a no-op.
  let expected = if need_any {
    Some(rgb_stream.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible stream alloc + scratch growth below succeed, so a first-row
  // allocation failure leaves `resample_outputs` uncommitted (retryable with a
  // changed output attachment). A no-output sink short-circuits to a no-op.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Area-only sink: reject a filter plan before any allocation (the area
  // stream would feed empty spans under a filter plan). Runs AFTER the
  // compare-only preflight, so a no-output sink stays a no-op even under a
  // filter plan.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  // Build the missing area stream into a LOCAL and grow the staging scratch —
  // every fallible pre-feed step — with NO field mutation, so the stream field
  // insert and the freeze commit TOGETHER only after both succeed (the
  // `planar_dual_resample` atomic shape: a scratch OOM leaves `rgb_stream` `None`
  // AND `resample_outputs` uncommitted, never a partial commit).
  let new_stream = if rgb_stream.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 3)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  if direct.is_none() {
    source_rgb_scratch(rgb_scratch, w, plan)?;
  }
  // Every pre-feed allocation succeeded — COMMIT atomically: insert the stream and
  // freeze the output set together (the compare above already caught any later
  // mid-frame output change; the freeze stores the snapshot on the first row).
  if let Some(s) = new_stream {
    *rgb_stream = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Stage the source-width canonical RGB row (identity zero-copy, or the caller's
  // conversion into the grown scratch) and run the shared per-output emit.
  let stream = rgb_stream.as_mut().expect("created above");
  let src: &[u8] = match direct {
    Some(row) => row,
    None => {
      let scratch = source_rgb_scratch(rgb_scratch, w, plan)?;
      convert(scratch);
      scratch
    }
  };
  packed_rgb_resample_emit(
    stream, plan, rgb, rgba, luma, luma_u16, hsv, src, matrix, full_range, idx, use_simd,
  )
}

/// RFC #238 tail-collapse — the transactional packed-RGB **filter** resample, the
/// [`SpanKind::Filter`](crate::resample::SpanKind) twin of [`packed_rgb_resample`]
/// over a 3-channel u8 [`FilterStream`](crate::resample::FilterStream). Identical
/// compare-before / commit-after discipline (the filter stream is built into a
/// local and its field insert + the freeze commit together only after the scratch
/// grows) and identical [`packed_rgb_resample_emit`] tail (the emit is generic
/// over the resampler kind), so the resampled colour equals the equivalent area
/// resample up to the signed kernel weights while remaining byte-identical to the
/// pre-collapse split filter path.
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_resample_filter(
  rgb_filter_stream: &mut Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut Vec<u8>,
  direct: Option<&[u8]>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert: impl FnOnce(&mut [u8]),
) -> Result<(), MixedSinkerError> {
  let need_any =
    rgb.is_some() || rgba.is_some() || luma.is_some() || luma_u16.is_some() || hsv.is_some();
  let expected = if need_any {
    Some(rgb_filter_stream.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit) — see `packed_rgb_resample`.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Single-kernel filter: reject a BICUBLIN plan before any allocation (its chroma
  // windows are read only by the `Yuv420p` per-plane route, so filtering every
  // channel with the luma kernel would be wrong output).
  plan.ensure_single_kernel_filter()?;
  // Build the missing filter stream into a LOCAL and grow the staging scratch —
  // every fallible pre-feed step — with NO field mutation, so the stream field
  // insert and the freeze commit TOGETHER (the `planar_dual_resample` atomic shape:
  // a scratch OOM leaves `rgb_filter_stream` `None` AND `resample_outputs`
  // uncommitted, never a partial commit).
  let new_stream = if rgb_filter_stream.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 3)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  if direct.is_none() {
    source_rgb_scratch(rgb_scratch, w, plan)?;
  }
  if let Some(s) = new_stream {
    *rgb_filter_stream = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  let stream = rgb_filter_stream.as_mut().expect("created above");
  let src: &[u8] = match direct {
    Some(row) => row,
    None => {
      let scratch = source_rgb_scratch(rgb_scratch, w, plan)?;
      convert(scratch);
      scratch
    }
  };
  packed_rgb_resample_emit(
    stream, plan, rgb, rgba, luma, luma_u16, hsv, src, matrix, full_range, idx, use_simd,
  )
}

/// Separable-filter fused resize for the **real-alpha** 8-bit packed-RGBA
/// sources ([`Rgba`](crate::source::Rgba) and the channel reorderings):
/// the `Filter`-plan twin of the area [`packed_rgba_resample`]. PIL resizes
/// RGBA by filtering R, G, B, A **independently with no premultiplication**,
/// so the source row is staged as one canonical source-width `R, G, B, A`
/// u8 row (`convert_rgba`) and fed to a single 4-channel
/// [`FilterStream`](crate::resample::FilterStream); each finalized output row
/// is the resampled RGBA. Because the u8 filter is byte-exact per channel,
/// the resampled RGBA frame is byte-exact versus PIL's RGBA resize.
///
/// Attached outputs derive from each finalized RGBA row: `rgba` copies it,
/// and `rgb` / `luma` / `hsv` come from the alpha-dropped RGB. These sources
/// are genuinely chromatic (no native luma plane), so luma is color-derived
/// from the resampled RGB.
///
/// Sequence-check precedes every allocation (the 4-channel stream creation
/// runs after the no-output and out-of-sequence rejections), keeping the
/// call atomic: a rejected first row stores no frozen-output snapshot and
/// `AllocationFailed` never masks `OutOfSequenceRow`. There is no
/// premultiplied route — a packed-RGBA source under premultiplied alpha
/// stays on the area path (which un-premultiplies); the filter path is
/// reached only for straight alpha.
///
/// Gated to `any(rgb, gbr)`: the 8-bit planar GBR+alpha `Gbrap` source
/// (straight alpha) scatters its G/B/R/A planes into the canonical packed
/// RGBA row via the same `convert_rgba` closure and shares this filter tail.
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_filter_resample(
  rgba_filter_stream: &mut Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<u8>,
  rgb_drop_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba: impl FnOnce(&mut [u8]),
) -> Result<(), MixedSinkerError> {
  // Single-kernel filter tail — reject a BICUBLIN plan (its chroma windows are
  // read only by the `Yuv420p` per-plane route) before any state change.
  plan.ensure_single_kernel_filter()?;
  let ow = plan.out_w();
  let need_any =
    rgb.is_some() || rgba.is_some() || luma.is_some() || luma_u16.is_some() || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the 4-channel filter stream
  // is the sole per-row stream), or `None` when no output is attached so the
  // preflight short-circuits to a no-op.
  let expected = if need_any {
    Some(rgba_filter_stream.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible 4-channel stream alloc + scratch growth below succeed (RFC #238
  // tail-collapse), so a first-row allocation failure leaves `resample_outputs`
  // uncommitted — retryable with a changed output attachment.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // The rgb / hsv / luma / luma_u16 outputs need an alpha-dropped RGB row,
  // sized to the out-width RGB row only when one of those is attached, so an
  // rgba-only sink neither grows it nor risks its allocation failure.
  let need_rgb_drop = rgb.is_some() || hsv.is_some() || luma.is_some() || luma_u16.is_some();
  // Build the missing filter stream into a LOCAL and grow the scratches — every
  // fallible pre-feed step — with NO field mutation, so the stream field insert
  // and the freeze commit TOGETHER only after they all succeed (the
  // `planar_dual_resample` atomic shape: a scratch OOM leaves `rgba_filter_stream`
  // `None` AND `resample_outputs` uncommitted, never a partial commit).
  let new_stream = if rgba_filter_stream.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  if need_rgb_drop {
    source_rgb_scratch(rgb_drop_scratch, ow, plan)?;
  }
  source_rgba_scratch(rgba_scratch, w, plan)?;
  // Every pre-feed allocation (stream, drop-alpha RGB scratch, RGBA scratch)
  // succeeded — COMMIT atomically: insert the stream and freeze the output set
  // together (the compare-only preflight above already caught any later mid-frame
  // output change).
  if let Some(s) = new_stream {
    *rgba_filter_stream = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Stage the drop-alpha RGB + canonical RGBA scratches (these grows re-run as
  // no-ops after the pre-commit grows above) and run the per-output emit.
  let rgb_drop: &mut [u8] = if need_rgb_drop {
    source_rgb_scratch(rgb_drop_scratch, ow, plan)?
  } else {
    &mut []
  };
  let src_rgba = source_rgba_scratch(rgba_scratch, w, plan)?;
  convert_rgba(src_rgba);
  let stream = rgba_filter_stream.as_mut().expect("created above");
  stream.feed_row(idx, src_rgba, use_simd, |oy, finalized| {
    // Straight-alpha RGBA output — the finalized 4-channel filter row.
    if let Some(buf) = rgba.as_deref_mut() {
      buf[oy * 4 * ow..(oy + 1) * 4 * ow].copy_from_slice(finalized);
    }
    if need_rgb_drop {
      let nrow = &mut rgb_drop[..3 * ow];
      drop_alpha_rgba_to_rgb_row(finalized, nrow, ow);
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      if let Some(buf) = luma.as_deref_mut() {
        crate::row::rgb_to_luma_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      // luma_u16 derives from the same alpha-dropped binned RGB (the u8-RGB →
      // u16-luma kernel) — byte-identical to the area 4-channel path. Only
      // `Gbrap` exposes a u16 luma through this u8 4-channel filter; the
      // packed-RGBA u8 sources leave it `None`.
      if let Some(buf) = luma_u16.as_deref_mut() {
        crate::row::rgb_to_luma_u16_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  Ok(())
}

/// Separable-filter fused resize for the **real-alpha** high-bit packed-RGBA
/// `u16` sources ([`Rgba64`](crate::source::Rgba64) /
/// [`Bgra64`](crate::source::Bgra64)): the `Filter`-plan twin of the area
/// [`packed_rgba_u16_resample`], and the 4-channel analogue of the 3-channel
/// [`packed_rgb_u16_resample_emit`]. PIL resizes RGBA by filtering R, G, B, A
/// **independently with no premultiplication**, so the source row is staged as
/// one canonical source-width `R, G, B, A` host-native u16 row
/// (`convert_rgba_u16`, the format's `*_to_rgba_u16` kernel) and fed to a
/// single 4-channel [`FilterStream<u16>`](crate::resample::FilterStream); each
/// finalized output row is the resampled native-depth RGBA. Because the u16
/// filter is byte-exact per channel, every output channel — alpha included —
/// equals the single-plane filter of that channel.
///
/// Attached outputs derive from each finalized RGBA row: `rgba_u16` copies it
/// and `rgb_u16` drops alpha, both at native depth; `rgba` / `rgb` / `luma` /
/// `hsv` come from a single `>> (SRC_BITS - 8)` narrowing of the alpha-dropped
/// RGB — the source-of-truth ordering the 3-channel u16 path uses. These
/// sources are chromatic (no native luma plane), so luma is color-derived from
/// the resampled RGB.
///
/// `NATIVE_LUMA16` selects the `luma_u16` flavor, mirroring the 3-channel u16
/// emit and the area 4-channel path: when `true` (`GbrapN`) luma_u16 is the
/// full native-depth Y' from the clamped alpha-dropped native RGB
/// (`rgb_to_luma_u16_native_row` keyed on `SRC_BITS`), byte-identical to a
/// direct `GbrapN` conversion and to the area path; when `false` (`Rgba64` /
/// `Bgra64`) it is the 8-bit-precision Y' from the narrowed RGB, zero-extended,
/// matching those formats' direct and area paths. Merely attaching an alpha
/// output therefore no longer downgrades `GbrapN` luma_u16 to 0..255.
///
/// `SRC_BITS` is the source's active bit depth — `16` for the full-16-bit
/// `Rgba64` / `Bgra64`. A signed kernel (CatmullRom / Lanczos3) overshoots a
/// legal edge, so a finalized sample can exceed the source's native max
/// `(1 << SRC_BITS) - 1` even though the `FilterStream` clamps to the full
/// `u16` range; for `SRC_BITS < 16` every binned sample (alpha too) is clamped
/// to the native max before any u16 copy, RGBA expansion, or u8 narrowing —
/// the same native-depth clamp the merged high-bit GBR routing applies in the
/// shared u16 emit. For `SRC_BITS == 16` the native max is the u16 max, so the
/// clamp is a no-op (the `FilterStream`'s `0..=65535` clamp *is* the native
/// clamp for `Rgba64` / `Bgra64`).
///
/// Sequence-check precedes every allocation (the 4-channel stream creation
/// runs after the no-output and out-of-sequence rejections), keeping the call
/// atomic: a rejected first row stores no frozen-output snapshot and
/// `AllocationFailed` never masks `OutOfSequenceRow`. There is no
/// premultiplied route — a packed-RGBA source under premultiplied alpha stays
/// on the area path (which un-premultiplies); the filter path is reached only
/// for straight alpha.
///
/// Gated to `any(rgb, gbr)` (the filter twin of
/// [`Self::rgba_filter_stream`]): besides the full-16-bit `Rgba64` / `Bgra64`,
/// the high-bit planar GBR+alpha family (`Gbrap10`…`Gbrap16`, straight alpha)
/// de-interleaves its native-depth G/B/R/A planes into the same canonical
/// packed RGBA u16 row via `convert_rgba_u16` and shares this 4-channel tail,
/// with the `SRC_BITS` native-max clamp keyed by its source depth.
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_u16_filter_resample<const SRC_BITS: u32, const NATIVE_LUMA16: bool>(
  rgba_filter_stream_u16: &mut Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch_u16: &mut Vec<u16>,
  color_scratch_u16: &mut Vec<u16>,
  narrow_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba_u16: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError> {
  const {
    assert!(
      SRC_BITS > 8 && SRC_BITS <= 16,
      "SRC_BITS must be in (8, 16] for the high-bit packed RGBA filter tail"
    )
  };
  // Single-kernel filter tail — reject a BICUBLIN plan (its chroma windows are
  // read only by the `Yuv420p` per-plane route) before any state change.
  plan.ensure_single_kernel_filter()?;
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the 4-channel filter stream
  // is the sole per-row stream), or `None` when no output is attached so the
  // preflight short-circuits to a no-op.
  let expected = if need_any {
    Some(rgba_filter_stream_u16.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible 4-channel stream alloc + scratch growth below succeed (RFC #238
  // tail-collapse), so a first-row allocation failure leaves `resample_outputs`
  // uncommitted — retryable with a changed output attachment.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // The u8 / narrowed-luma_u16 outputs derive from a `>> (SRC_BITS - 8)`
  // narrowing of the alpha-dropped binned RGB; a native-u16-only sink (only
  // rgb_u16 / rgba_u16) never touches it, so the out-width u8 narrow scratch
  // is sized — and its allocation failure risked — only when a narrowed
  // output is attached. Under `NATIVE_LUMA16` (`GbrapN`) luma_u16 is computed
  // at full native precision from the alpha-dropped native RGB instead, so it
  // takes the native-luma path below rather than this narrowing.
  let narrowed_luma_u16 = !NATIVE_LUMA16 && luma_u16.is_some();
  let need_narrow =
    rgb.is_some() || rgba.is_some() || luma.is_some() || narrowed_luma_u16 || hsv.is_some();
  // Native-precision luma_u16 (`GbrapN`): drop alpha from the clamped native
  // binned RGB and run the same `rgb_to_luma_u16_native_row` the area / direct
  // paths use, so the resampled luma_u16 is byte-identical to a direct GbrapN
  // conversion of the binned frame. The narrowed `Rgba64` / `Bgra64` callers
  // leave `NATIVE_LUMA16` false and take the narrowed luma_u16 above.
  let native_luma = NATIVE_LUMA16 && luma_u16.is_some();
  // The clamped drop-alpha native u16 RGB row is the single source for both
  // the u8 narrowing and the native luma, so it is sized whenever either is
  // requested.
  let need_native_rgb = need_narrow || native_luma;
  // Build the missing filter stream into a LOCAL and grow the scratches — every
  // fallible pre-feed step — with NO field mutation, so the stream field insert and
  // the freeze commit TOGETHER only after they all succeed (the
  // `planar_dual_resample` atomic shape: a scratch OOM leaves
  // `rgba_filter_stream_u16` `None` AND `resample_outputs` uncommitted, never a
  // partial commit).
  let new_stream = if rgba_filter_stream_u16.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  // Grow every pre-feed scratch (the conditional native-RGB / narrow rows and the
  // always-sized source-width RGBA staging) BEFORE the commit, so a scratch OOM
  // likewise leaves the stream field `None` and `resample_outputs` uncommitted.
  if need_native_rgb {
    source_rgb_u16_scratch(color_scratch_u16, ow, plan)?;
  }
  if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?;
  }
  source_rgba_u16_scratch(rgba_scratch_u16, w, plan)?;
  // Every pre-feed allocation succeeded — COMMIT atomically: insert the stream and
  // freeze the output set together (the compare-only preflight above already caught
  // any later mid-frame output change).
  if let Some(s) = new_stream {
    *rgba_filter_stream_u16 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Re-fetch the scratches (these grows re-run as no-ops after the pre-commit grows
  // above). Out-width clamped drop-alpha native RGB row, resolved per finalized
  // output row; sized when any narrowed output OR native luma_u16 is attached
  // (every narrowed output and the native luma read this one canonical native RGB
  // row).
  let color_rgb: &mut [u16] = if need_native_rgb {
    source_rgb_u16_scratch(color_scratch_u16, ow, plan)?
  } else {
    &mut []
  };
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  let src_rgba = source_rgba_u16_scratch(rgba_scratch_u16, w, plan)?;
  convert_rgba_u16(src_rgba);
  // A finalized 4-channel `binned` sample can overshoot the native max for a
  // sub-16-bit source; clamp before any output is derived (no-op at 16-bit —
  // the `FilterStream`'s `0..=65535` clamp is already the native clamp).
  let native_max: u16 = ((1u32 << SRC_BITS) - 1) as u16;
  let stream = rgba_filter_stream_u16.as_mut().expect("created above");
  stream.feed_row(idx, src_rgba, use_simd, |oy, binned| {
    // Native-depth u16 RGBA — copy the binned 4-channel row (clamped to the
    // native max for a sub-16-bit source).
    if let Some(buf) = rgba_u16.as_deref_mut() {
      let out = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      if SRC_BITS < 16 {
        for (d, &s) in out.iter_mut().zip(binned.iter()) {
          *d = s.min(native_max);
        }
      } else {
        out.copy_from_slice(binned);
      }
    }
    // Native-depth u16 RGB — drop alpha from the binned row (clamped for a
    // sub-16-bit source).
    if let Some(buf) = rgb_u16.as_deref_mut() {
      let out = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
      for (rgb_px, rgba_px) in out
        .as_chunks_mut::<3>()
        .0
        .iter_mut()
        .zip(binned.as_chunks::<4>().0.iter())
      {
        if SRC_BITS < 16 {
          rgb_px[0] = rgba_px[0].min(native_max);
          rgb_px[1] = rgba_px[1].min(native_max);
          rgb_px[2] = rgba_px[2].min(native_max);
        } else {
          rgb_px.copy_from_slice(&rgba_px[..3]);
        }
      }
    }
    // Clamped drop-alpha native u16 RGB row — the single canonical source for
    // both the native luma_u16 and the u8 narrowing. Resolved once whenever
    // either is requested; a clipped-high edge is clamped to the native max
    // here so a sub-16-bit signed-kernel overshoot never wraps downstream.
    if need_native_rgb {
      let rgb_row = &mut color_rgb[..3 * ow];
      for (out_px, in_px) in rgb_row
        .as_chunks_mut::<3>()
        .0
        .iter_mut()
        .zip(binned.as_chunks::<4>().0.iter())
      {
        if SRC_BITS < 16 {
          out_px[0] = in_px[0].min(native_max);
          out_px[1] = in_px[1].min(native_max);
          out_px[2] = in_px[2].min(native_max);
        } else {
          out_px.copy_from_slice(&in_px[..3]);
        }
      }
    }
    // Native-precision luma_u16 (`GbrapN`): run the same
    // `rgb_to_luma_u16_native_row` the area / direct paths use on the clamped
    // native RGB, so the resampled luma_u16 is byte-identical to a direct
    // GbrapN conversion of the binned frame (full native depth, not the
    // narrowed 0..255 flavor). It clamps each channel to the native max
    // internally, a no-op since `color_rgb` is already clamped above.
    if native_luma && let Some(buf) = luma_u16.as_deref_mut() {
      crate::row::rgb_to_luma_u16_native_row(
        &color_rgb[..3 * ow],
        &mut buf[oy * ow..(oy + 1) * ow],
        ow,
        matrix,
        full_range,
        SRC_BITS,
      );
    }
    if need_narrow {
      // Narrow the clamped drop-alpha native RGB `>> (SRC_BITS - 8)`; a
      // clipped-high edge narrows to `255`, never a wrap.
      let rgb_row = &color_rgb[..3 * ow];
      let nrow = &mut narrow[..3 * ow];
      for (d, &s) in nrow.iter_mut().zip(rgb_row.iter()) {
        *d = (s >> (SRC_BITS - 8)) as u8;
      }
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      if let Some(buf) = rgba.as_deref_mut() {
        // Narrow all four filtered channels (α `>> (SRC_BITS - 8)` too) —
        // alpha is a real filtered channel here, never forced opaque (the
        // 4-channel filter's whole point). Mirrors the area 4-channel path.
        // Clamp to the native max before the shift for a sub-16-bit source so
        // a signed-kernel overshoot clips to `255` (no-op at 16-bit).
        let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
        for (d, px) in dst
          .as_chunks_mut::<4>()
          .0
          .iter_mut()
          .zip(binned.as_chunks::<4>().0.iter())
        {
          if SRC_BITS < 16 {
            d[0] = (px[0].min(native_max) >> (SRC_BITS - 8)) as u8;
            d[1] = (px[1].min(native_max) >> (SRC_BITS - 8)) as u8;
            d[2] = (px[2].min(native_max) >> (SRC_BITS - 8)) as u8;
            d[3] = (px[3].min(native_max) >> (SRC_BITS - 8)) as u8;
          } else {
            d[0] = (px[0] >> (SRC_BITS - 8)) as u8;
            d[1] = (px[1] >> (SRC_BITS - 8)) as u8;
            d[2] = (px[2] >> (SRC_BITS - 8)) as u8;
            d[3] = (px[3] >> (SRC_BITS - 8)) as u8;
          }
        }
      }
      if let Some(buf) = luma.as_deref_mut() {
        crate::row::rgb_to_luma_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      // Narrowed luma_u16 (`Rgba64` / `Bgra64`): 8-bit-precision Y' from the
      // narrowed RGB, zero-extended — byte-identical to those formats' direct
      // path. Skipped under `NATIVE_LUMA16` (`GbrapN`), where luma_u16 came
      // from the native path above.
      if narrowed_luma_u16 && let Some(buf) = luma_u16.as_deref_mut() {
        crate::row::rgb_to_luma_u16_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  Ok(())
}

/// Source-width `u16` RGB staging for high-bit packed-RGB resampling:
/// the wire row converts here before feeding [`AreaStream<u16>`]. Grows
/// `scratch` to `3 * width` `u16` under the planner's
/// recoverable-allocation contract. Mirrors [`source_rgb_scratch`] for
/// the 16-bit element path.
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "yuv-444-packed",
  feature = "y2xx",
  feature = "v210",
  feature = "yuv-planar",
  feature = "yuv-semi-planar"
))]
pub(super) fn source_rgb_u16_scratch<'s>(
  scratch: &'s mut Vec<u16>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [u16], MixedSinkerError> {
  // Single-shot grow failpoint (RFC #238 tail-collapse tests): refuse this grow as
  // a host OOM would, proving a `u16` source-staging OOM AFTER a successful stream
  // build leaves the stream field `None` AND `resample_outputs` uncommitted
  // (retryable). The `u16` twin of the `source_rgb_scratch` failpoint, so a 3-ch
  // `u16` atomicity test can fail this specific pre-commit grow.
  #[cfg(all(test, feature = "std", any(feature = "rgb", feature = "gbr")))]
  if FORCE_SOURCE_RGB_U16_SCRATCH_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
    )));
  }
  let row = width
    .checked_mul(3)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.src_h(),
      3,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0);
  }
  Ok(&mut scratch[..row])
}

// Single-shot source-RGB-`u16`-scratch grow failpoint for the RFC #238
// tail-collapse transactional-preflight tests. Gated on `any(rgb, gbr)` (the
// high-bit packed-RGB / GBR families that stage a source-width `u16` RGB row
// pre-commit); `source_rgb_u16_scratch` consults it under the same gate. Separate
// from the `try_box` box-alloc failpoint so a 3-channel `u16` test can fail the
// SCRATCH grow specifically (after a successful stream construction).
#[cfg(all(test, feature = "std", any(feature = "rgb", feature = "gbr")))]
std::thread_local! {
  static FORCE_SOURCE_RGB_U16_SCRATCH_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}

/// Arms a single-shot failpoint that makes the next [`source_rgb_u16_scratch`]
/// grow refuse with `AllocationFailed`, exactly as a host OOM would. Proves the
/// commit-together atomic shape of [`packed_rgb_u16_resample`] (a source-staging
/// OOM after a successful stream build leaves the stream field `None` AND
/// `resample_outputs` uncommitted, so the row is retryable). Test-only.
#[cfg(all(test, feature = "std", feature = "rgb"))]
pub(crate) fn arm_source_rgb_u16_scratch_failure() {
  FORCE_SOURCE_RGB_U16_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// Feeds the prepared source-width `u16` RGB row into the (already
/// sequence-checked) stream and derives every attached output from each
/// finalized output row. Binning runs at the source's native depth
/// (`SRC_BITS` active bits per `u16` element); the `rgb_u16` /
/// `rgba_u16` outputs copy it directly, while the u8 and `luma_u16`
/// outputs derive from a single `>> (SRC_BITS - 8)` narrowing — the same
/// source-of-truth ordering the direct path uses (luma / luma_u16 / hsv
/// all read the narrowed u8 RGB). `SRC_BITS` is `16` for the packed
/// `Rgb48` / `Bgr48` sources (whose elements are full-range u16) and the
/// source bit depth for the high-bit planar GBR sources (`Gbrp9` … 14
/// carry fewer than 16 active bits, so their narrowing shift and opaque
/// `rgba_u16` alpha both track `SRC_BITS`, not a hard-coded 16).
/// `narrow_scratch` is sized to the out-width u8 RGB row only when one of
/// those narrowed outputs is attached, so a native-u16-only sink neither
/// grows it nor risks its allocation failure.
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "yuv-444-packed",
  feature = "y2xx",
  feature = "v210",
  feature = "yuv-planar",
  feature = "yuv-semi-planar"
))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_u16_resample_emit<const SRC_BITS: u32, const NATIVE_LUMA16: bool>(
  stream: &mut impl crate::resample::RowResampler<u16>,
  plan: &ResamplePlan,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  src_u16: &[u16],
  narrow_scratch: &mut Vec<u8>,
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  const {
    assert!(
      SRC_BITS >= 8 && SRC_BITS <= 16,
      "SRC_BITS must be in [8, 16]"
    )
  };
  let ow = plan.out_w();
  // The u8 / luma_u16 outputs derive from a `>> (SRC_BITS - 8)`
  // narrowing of the binned row; a native-u16-only sink (only rgb_u16 /
  // rgba_u16) never touches it, so the out-width u8 scratch is sized —
  // and its allocation failure risked — only when one of those outputs
  // is attached. The predicate gates both the sizing here and the use in
  // the closure, so they cannot drift.
  let need_narrow = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || hsv.is_some()
    || (!NATIVE_LUMA16 && luma_u16.is_some());
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  // A signed filter kernel (CatmullRom / Lanczos3) overshoots a legal
  // edge, so a finalized `binned` sample can exceed the source's native
  // max `(1 << SRC_BITS) - 1` even though the `FilterStream` clamps it to
  // the full `u16` range. For a sub-16-bit source that overshoot is
  // out-of-contract: the native-depth u16 outputs would publish a value
  // above the documented range, and the u8 narrowing (`>> (SRC_BITS - 8)`)
  // would wrap a clipped-high edge to a small value instead of `255`. So
  // for `SRC_BITS < 16` every binned sample is clamped to the native max
  // before any u16 copy, RGBA expansion, native luma, or u8 narrowing.
  // For `SRC_BITS == 16` the native max is the u16 max, so this is a
  // no-op (`Rgb48` / `Bgr48` are unaffected); the area path never
  // overshoots, so it is a value no-op there too.
  let native_max: u16 = ((1u32 << SRC_BITS) - 1) as u16;
  stream.feed_row(idx, src_u16, use_simd, |oy, binned| {
    // Native-depth u16 outputs copy the binned row (clamped to the native
    // max for a sub-16-bit source).
    if let Some(buf) = rgb_u16.as_deref_mut() {
      let out = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
      if SRC_BITS < 16 {
        for (d, &s) in out.iter_mut().zip(binned.iter()) {
          *d = s.min(native_max);
        }
      } else {
        out.copy_from_slice(binned);
      }
    }
    if let Some(buf) = rgba_u16.as_deref_mut() {
      let out = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      if SRC_BITS < 16 {
        // Clamping twin of `expand_rgb_u16_to_rgba_u16_row` (opaque alpha
        // is the native max, which equals its `(1 << BITS) - 1`).
        for (rgba_px, rgb_px) in out
          .as_chunks_mut::<4>()
          .0
          .iter_mut()
          .zip(binned.as_chunks::<3>().0.iter())
        {
          rgba_px[0] = rgb_px[0].min(native_max);
          rgba_px[1] = rgb_px[1].min(native_max);
          rgba_px[2] = rgb_px[2].min(native_max);
          rgba_px[3] = native_max;
        }
      } else {
        crate::row::expand_rgb_u16_to_rgba_u16_row::<SRC_BITS>(binned, out, ow);
      }
    }
    // Native-precision `luma_u16`: derive directly from the native-depth
    // binned RGB, byte-identical to the direct
    // `gbr_to_luma_u16_high_bit_row` path. Only the high-bit-GBR callers
    // set `NATIVE_LUMA16`; the `Rgb48` / `Bgr48` callers leave it false
    // and take the narrowed `luma_u16` in the `need_narrow` block below.
    // `rgb_to_luma_u16_native_row` clamps each input channel to the
    // native max internally, so a filter overshoot is clipped before the
    // luma sum (a no-op for the in-range area / direct callers).
    if NATIVE_LUMA16 && let Some(buf) = luma_u16.as_deref_mut() {
      crate::row::rgb_to_luma_u16_native_row(
        binned,
        &mut buf[oy * ow..(oy + 1) * ow],
        ow,
        matrix,
        full_range,
        SRC_BITS,
      );
    }
    if need_narrow {
      let nrow = &mut narrow[..3 * ow];
      // Clamp to the native max before the narrowing shift so a sub-16-bit
      // filter overshoot clips to `255` instead of wrapping (no-op for
      // 16-bit and for the in-range area path).
      if SRC_BITS < 16 {
        for (d, &s) in nrow.iter_mut().zip(binned.iter()) {
          *d = (s.min(native_max) >> (SRC_BITS - 8)) as u8;
        }
      } else {
        for (d, &s) in nrow.iter_mut().zip(binned.iter()) {
          *d = (s >> (SRC_BITS - 8)) as u8;
        }
      }
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      if let Some(buf) = rgba.as_deref_mut() {
        crate::row::expand_rgb_to_rgba_row(nrow, &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow], ow);
      }
      if let Some(buf) = luma.as_deref_mut() {
        crate::row::rgb_to_luma_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if !NATIVE_LUMA16 && let Some(buf) = luma_u16.as_deref_mut() {
        crate::row::rgb_to_luma_u16_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  Ok(())
}

/// RFC #238 tail-collapse — the transactional high-bit packed-RGB **area**
/// resample, the u16 element-family twin of [`packed_rgb_resample`]. Folds the
/// split high-bit preflight + area-stream build + [`packed_rgb_u16_resample_emit`]
/// sequence into one call behind the shared compare-before / commit-after
/// preflight
/// ([`resample_preflight_check_only`](resample_preflight_check_only)),
/// mirroring [`packed_rgb_resample`]: the missing area stream is built into a
/// LOCAL and every fallible pre-feed scratch (the source-width `u16` staging row
/// and — when a narrowed output is attached — the out-width `u8` narrow row the
/// emit reads) is grown with NO field mutation, so the stream field insert and the
/// output-set freeze COMMIT TOGETHER only after they all succeed. A first-row
/// allocation failure therefore leaves `rgb_stream_u16` `None` AND
/// `resample_outputs` uncommitted (never a partial commit) — state-atomic and
/// retryable even with a changed output attachment (the #180 discipline). A pure
/// reordering of the pre-collapse split path: it emits the identical bytes, only
/// deferring the field mutations past the fallible allocations.
///
/// `convert` stages the source-width canonical host-native `u16` RGB row into the
/// grown `rgb_scratch_u16` (each format's `*_to_rgb_u16` kernel); that fallible
/// growth is hoisted ahead of the freeze too. `SRC_BITS` / `NATIVE_LUMA16` thread
/// straight to [`packed_rgb_u16_resample_emit`] (the source's active depth drives
/// the u8 `>> (SRC_BITS - 8)` narrowing; `NATIVE_LUMA16` selects the native-depth
/// versus narrowed `luma_u16` flavour), so every output byte matches the
/// pre-collapse emit.
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_u16_resample<const SRC_BITS: u32, const NATIVE_LUMA16: bool>(
  rgb_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch_u16: &mut Vec<u16>,
  narrow_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError> {
  const {
    assert!(
      SRC_BITS >= 8 && SRC_BITS <= 16,
      "SRC_BITS must be in [8, 16]"
    )
  };
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the area stream is the sole
  // per-row stream), or `None` when no output is attached so the preflight
  // short-circuits to a no-op.
  let expected = if need_any {
    Some(rgb_stream_u16.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible stream alloc + scratch growth below succeed, so a first-row
  // allocation failure leaves `resample_outputs` uncommitted (retryable with a
  // changed output attachment). A no-output sink short-circuits to a no-op.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Area-only sink: reject a filter plan before any allocation (mirrors the
  // pre-collapse `packed_rgb_u16_resample_stream` guard). Runs AFTER the
  // compare-only preflight, so a no-output sink stays a no-op even under a filter
  // plan.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  // The emit narrows the binned row `>> (SRC_BITS - 8)` for its u8 / narrowed-
  // `luma_u16` outputs through the out-width `narrow_scratch`; pre-grow it here
  // with the emit's exact predicate so that fallible growth precedes the freeze
  // too (the emit's own grow then re-runs as a no-op). A native-`u16`-only sink
  // neither grows it nor risks its allocation failure.
  let need_narrow = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || hsv.is_some()
    || (!NATIVE_LUMA16 && luma_u16.is_some());
  // Build the missing area stream into a LOCAL and grow the staging scratches —
  // every fallible pre-feed step — with NO field mutation, so the stream field
  // insert and the freeze commit TOGETHER only after they all succeed (the
  // `planar_dual_resample` atomic shape: a scratch OOM leaves `rgb_stream_u16`
  // `None` AND `resample_outputs` uncommitted, never a partial commit).
  let new_stream = if rgb_stream_u16.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 3)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  source_rgb_u16_scratch(rgb_scratch_u16, w, plan)?;
  if need_narrow {
    source_rgb_scratch(narrow_scratch, plan.out_w(), plan)?;
  }
  // Every pre-feed allocation succeeded — COMMIT atomically: insert the stream and
  // freeze the output set together (the compare above already caught any later
  // mid-frame output change; the freeze stores the snapshot on the first row).
  if let Some(s) = new_stream {
    *rgb_stream_u16 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Stage the source-width canonical native-`u16` RGB row (the caller's conversion
  // into the grown scratch) and run the shared per-output emit (its narrow grow
  // re-runs as a no-op after the pre-commit grow above).
  let stream = rgb_stream_u16.as_mut().expect("created above");
  let src_u16 = source_rgb_u16_scratch(rgb_scratch_u16, w, plan)?;
  convert(src_u16);
  packed_rgb_u16_resample_emit::<SRC_BITS, NATIVE_LUMA16>(
    stream,
    plan,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    hsv,
    src_u16,
    narrow_scratch,
    matrix,
    full_range,
    idx,
    use_simd,
  )
}

/// RFC #238 tail-collapse — the transactional high-bit packed-RGB **filter**
/// resample, the [`SpanKind::Filter`](crate::resample::SpanKind) twin of
/// [`packed_rgb_u16_resample`] over a 3-channel `u16`
/// [`FilterStream`](crate::resample::FilterStream). Identical compare-before /
/// commit-after discipline (the filter stream is built into a local and its field
/// insert + the freeze + the pre-grown narrow scratch commit together only after
/// every allocation) and the identical [`packed_rgb_u16_resample_emit`] tail (the
/// emit is generic over the resampler kind), so the resampled colour equals the
/// equivalent area resample up to the signed kernel weights while remaining
/// byte-identical to the pre-collapse split filter path.
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_u16_resample_filter<const SRC_BITS: u32, const NATIVE_LUMA16: bool>(
  rgb_filter_stream_u16: &mut Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch_u16: &mut Vec<u16>,
  narrow_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError> {
  const {
    assert!(
      SRC_BITS >= 8 && SRC_BITS <= 16,
      "SRC_BITS must be in [8, 16]"
    )
  };
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  let expected = if need_any {
    Some(rgb_filter_stream_u16.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit) — see `packed_rgb_u16_resample`.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Single-kernel filter: reject a BICUBLIN plan before any allocation (its chroma
  // windows are read only by the `Yuv420p` per-plane route, so filtering every
  // channel with the luma kernel would be wrong output).
  plan.ensure_single_kernel_filter()?;
  // Pre-grow the emit's out-width narrow scratch with its exact predicate so that
  // fallible growth precedes the freeze too (see `packed_rgb_u16_resample`).
  let need_narrow = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || hsv.is_some()
    || (!NATIVE_LUMA16 && luma_u16.is_some());
  // Build the missing filter stream into a LOCAL and grow the staging scratches —
  // every fallible pre-feed step — with NO field mutation, so the stream field
  // insert and the freeze commit TOGETHER (the `planar_dual_resample` atomic
  // shape: a scratch OOM leaves `rgb_filter_stream_u16` `None` AND
  // `resample_outputs` uncommitted, never a partial commit).
  let new_stream = if rgb_filter_stream_u16.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 3)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  source_rgb_u16_scratch(rgb_scratch_u16, w, plan)?;
  if need_narrow {
    source_rgb_scratch(narrow_scratch, plan.out_w(), plan)?;
  }
  if let Some(s) = new_stream {
    *rgb_filter_stream_u16 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  let stream = rgb_filter_stream_u16.as_mut().expect("created above");
  let src_u16 = source_rgb_u16_scratch(rgb_scratch_u16, w, plan)?;
  convert(src_u16);
  packed_rgb_u16_resample_emit::<SRC_BITS, NATIVE_LUMA16>(
    stream,
    plan,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    hsv,
    src_u16,
    narrow_scratch,
    matrix,
    full_range,
    idx,
    use_simd,
  )
}

// ===== Native-`u32` packed-RGB(A) resample tier (0-ULP, issue #289) ==========
//
// The narrow-first u16 tiers above stage each wire `u32` as its `>> 16` narrow
// **before** binning, so a downscaled / filtered output is the mean of the
// narrowed samples — within 1 LSB of an exact `u32`-domain mean. These tiers
// instead stage the source as **host-native `u32`** (no narrow), bin at full
// `u32` precision (`AreaStream<u32>`'s `u128` accumulators / `FilterStream<u32>`'s
// `f64` domain), and narrow only in the per-output derive — exact 0-ULP for
// both `full_range` true and false. Because narrowing a binned `u32` `>> 16` to
// `u16` and deriving from there is byte-identical to the direct convert of the
// binned `u32` (the direct path itself narrows `>> 16` first), the derive below
// is the `SRC_BITS = 16` specialization of the u16 emit — every native-max
// clamp branch is a no-op at 16 bits and is dropped.

/// Source-width host-native `u32` RGB staging (`3 * width`) for the
/// native-`u32` packed-RGB resample tier. The `u32` twin of
/// [`source_rgb_u16_scratch`].
#[cfg(any(feature = "rgb", feature = "gbr"))]
pub(super) fn source_rgb_u32_scratch<'s>(
  scratch: &'s mut Vec<u32>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [u32], MixedSinkerError> {
  // Single-shot grow failpoint (RFC #238 tail-collapse tests): refuse this grow as
  // a host OOM would, proving a `u32` source-staging OOM AFTER a successful stream
  // build leaves the stream field `None` AND `resample_outputs` uncommitted
  // (retryable). The `u32` twin of the `source_rgb_u16_scratch` failpoint, so a
  // 3-ch `u32` atomicity test can fail this specific pre-commit grow.
  #[cfg(all(test, feature = "std", any(feature = "rgb", feature = "gbr")))]
  if FORCE_SOURCE_RGB_U32_SCRATCH_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
    )));
  }
  let row = width
    .checked_mul(3)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.src_h(),
      3,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0);
  }
  Ok(&mut scratch[..row])
}

// Single-shot source-RGB-`u32`-scratch grow failpoint for the RFC #238
// tail-collapse transactional-preflight tests. Gated on `any(rgb, gbr)` (the
// native-`u32` packed-RGB / GBR families that stage a source-width `u32` RGB row
// pre-commit); `source_rgb_u32_scratch` consults it under the same gate. Separate
// from the `try_box` box-alloc failpoint so a 3-channel `u32` test can fail the
// SCRATCH grow specifically (after a successful stream construction).
#[cfg(all(test, feature = "std", any(feature = "rgb", feature = "gbr")))]
std::thread_local! {
  static FORCE_SOURCE_RGB_U32_SCRATCH_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}

/// Arms a single-shot failpoint that makes the next [`source_rgb_u32_scratch`]
/// grow refuse with `AllocationFailed`, exactly as a host OOM would. Proves the
/// commit-together atomic shape of [`packed_rgb_u32_resample`] (a source-staging
/// OOM after a successful stream build leaves the stream field `None` AND
/// `resample_outputs` uncommitted, so the row is retryable). Test-only.
#[cfg(all(test, feature = "std", feature = "rgb"))]
pub(crate) fn arm_source_rgb_u32_scratch_failure() {
  FORCE_SOURCE_RGB_U32_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// Source-width host-native canonical `R, G, B, A` `u32` staging (`4 * width`)
/// for the native-`u32` packed-RGBA resample tier. The `u32` twin of
/// [`source_rgba_u16_scratch`].
#[cfg(any(feature = "rgb", feature = "gbr"))]
pub(super) fn source_rgba_u32_scratch<'s>(
  scratch: &'s mut Vec<u32>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [u32], MixedSinkerError> {
  // Single-shot grow failpoint (RFC #238 tail-collapse tests): refuse this grow as
  // a host OOM would, proving a 4-channel `u32` scratch OOM AFTER a successful
  // stream build leaves the stream field `None` AND `resample_outputs` uncommitted
  // (retryable). The 4-channel twin of the `source_rgb_u32_scratch` failpoint, so a
  // packed-RGBA `u32` atomicity test can fail this specific pre-commit grow.
  #[cfg(all(test, feature = "std", any(feature = "rgb", feature = "gbr")))]
  if FORCE_SOURCE_RGBA_U32_SCRATCH_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
    )));
  }
  let row = width
    .checked_mul(4)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.src_h(),
      4,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0);
  }
  Ok(&mut scratch[..row])
}

// Single-shot source-RGBA-`u32`-scratch grow failpoint for the RFC #238
// tail-collapse transactional-preflight tests. Gated on `any(rgb, gbr)` (the
// native-`u32` packed-RGBA / `Gbrap32` families that stage a source-width `u32`
// RGBA row pre-commit); `source_rgba_u32_scratch` consults it under the same gate.
// Separate from the `try_box` box-alloc failpoint so a 4-channel `u32` test can
// fail the SCRATCH grow specifically (after a successful stream construction,
// before the stream-field insert).
#[cfg(all(test, feature = "std", any(feature = "rgb", feature = "gbr")))]
std::thread_local! {
  static FORCE_SOURCE_RGBA_U32_SCRATCH_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}

/// Arms a single-shot failpoint that makes the next [`source_rgba_u32_scratch`]
/// grow refuse with `AllocationFailed`, exactly as a host OOM would. Proves the
/// commit-together atomic shape of [`packed_rgba_u32_resample`] (a source-staging
/// OOM after a successful stream build leaves the stream field `None` AND
/// `resample_outputs` uncommitted, so the row is retryable). Test-only.
#[cfg(all(test, feature = "std", feature = "rgb"))]
pub(crate) fn arm_source_rgba_u32_scratch_failure() {
  FORCE_SOURCE_RGBA_U32_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// RFC #238 tail-collapse — the transactional native-`u32` packed-RGB **area**
/// resample, the `u32` element-family twin of [`packed_rgb_u16_resample`]. Folds
/// the split preflight + area-stream build + [`packed_rgb_u32_resample_emit`]
/// sequence into one call behind the shared compare-before / commit-after
/// preflight
/// ([`resample_preflight_check_only`](resample_preflight_check_only)):
/// the missing area stream is built into a LOCAL and every fallible pre-feed
/// scratch (the source-width `u32` staging row, the always-sized out-width binned
/// `u16` row the emit narrows into, and — when a narrowed output is attached — the
/// out-width `u8` narrow row) is grown with NO field mutation, so the stream field
/// insert and the output-set freeze COMMIT TOGETHER only after they all succeed. A
/// first-row allocation failure therefore leaves `rgb_stream_u32` `None` AND
/// `resample_outputs` uncommitted (never a partial commit) — state-atomic and
/// retryable even with a changed output attachment (the #180 discipline). A pure
/// reordering of the pre-collapse split path: identical bytes, only deferring the
/// field mutations past the fallible allocations.
///
/// `convert` stages the source-width canonical host-native `u32` RGB row into the
/// grown `rgb_scratch_u32` (each format's `*_to_rgb_u32` kernel). `NATIVE_LUMA16`
/// threads straight to [`packed_rgb_u32_resample_emit`] (native-depth vs narrowed
/// `luma_u16`), so every output byte matches the pre-collapse emit — full `u32`
/// binning, narrowed only per output (0-ULP, issue #289).
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_u32_resample<const NATIVE_LUMA16: bool>(
  rgb_stream_u32: &mut Option<std::boxed::Box<crate::resample::AreaStream<u32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch_u32: &mut Vec<u32>,
  binned_scratch_u16: &mut Vec<u16>,
  narrow_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert: impl FnOnce(&mut [u32]),
) -> Result<(), MixedSinkerError> {
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the area stream is the sole
  // per-row stream), or `None` when no output is attached so the preflight
  // short-circuits to a no-op.
  let expected = if need_any {
    Some(rgb_stream_u32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible stream alloc + scratch growth below succeed, so a first-row
  // allocation failure leaves `resample_outputs` uncommitted (retryable with a
  // changed output attachment). A no-output sink short-circuits to a no-op.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Area-only sink: reject a filter plan before any allocation (mirrors the
  // pre-collapse `packed_rgb_u32_resample_stream` guard). Runs AFTER the
  // compare-only preflight, so a no-output sink stays a no-op even under a filter
  // plan.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  // The emit narrows the binned `u32` row for its u8 / narrowed-`luma_u16` outputs
  // through the out-width `narrow_scratch`; pre-grow it here with the emit's exact
  // predicate so that fallible growth precedes the freeze too (the emit's own grow
  // then re-runs as a no-op). A native-`u16`-only sink neither grows it nor risks
  // its allocation failure.
  let need_narrow = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || hsv.is_some()
    || (!NATIVE_LUMA16 && luma_u16.is_some());
  // Build the missing area stream into a LOCAL and grow the staging scratches —
  // every fallible pre-feed step — with NO field mutation, so the stream field
  // insert and the freeze commit TOGETHER only after they all succeed (the
  // `planar_dual_resample` atomic shape: a scratch OOM leaves `rgb_stream_u32`
  // `None` AND `resample_outputs` uncommitted, never a partial commit).
  let new_stream = if rgb_stream_u32.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 3)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  source_rgb_u32_scratch(rgb_scratch_u32, w, plan)?;
  source_rgb_u16_scratch(binned_scratch_u16, plan.out_w(), plan)?;
  if need_narrow {
    source_rgb_scratch(narrow_scratch, plan.out_w(), plan)?;
  }
  // Every pre-feed allocation succeeded — COMMIT atomically: insert the stream and
  // freeze the output set together (the compare above already caught any later
  // mid-frame output change; the freeze stores the snapshot on the first row).
  if let Some(s) = new_stream {
    *rgb_stream_u32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Stage the source-width canonical host-native `u32` RGB row (the caller's
  // conversion into the grown scratch) and run the shared per-output emit (its
  // binned / narrow grows re-run as no-ops after the pre-commit grows above).
  let stream = rgb_stream_u32.as_mut().expect("created above");
  let src_u32 = source_rgb_u32_scratch(rgb_scratch_u32, w, plan)?;
  convert(src_u32);
  packed_rgb_u32_resample_emit::<NATIVE_LUMA16>(
    stream,
    plan,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    hsv,
    src_u32,
    binned_scratch_u16,
    narrow_scratch,
    matrix,
    full_range,
    idx,
    use_simd,
  )
}

/// RFC #238 tail-collapse — the transactional native-`u32` packed-RGB **filter**
/// resample, the [`SpanKind::Filter`](crate::resample::SpanKind) twin of
/// [`packed_rgb_u32_resample`] over a 3-channel `u32`
/// [`FilterStream`](crate::resample::FilterStream). Identical compare-before /
/// commit-after discipline (the filter stream is built into a local and its field
/// insert + the freeze + the pre-grown binned / narrow scratches commit together
/// only after every allocation) and the identical [`packed_rgb_u32_resample_emit`]
/// tail (the emit is generic over the resampler kind), so the resampled colour
/// equals the equivalent area resample up to the signed kernel weights while
/// remaining byte-identical to the pre-collapse split filter path.
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_u32_resample_filter<const NATIVE_LUMA16: bool>(
  rgb_filter_stream_u32: &mut Option<std::boxed::Box<crate::resample::FilterStream<u32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch_u32: &mut Vec<u32>,
  binned_scratch_u16: &mut Vec<u16>,
  narrow_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert: impl FnOnce(&mut [u32]),
) -> Result<(), MixedSinkerError> {
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  let expected = if need_any {
    Some(rgb_filter_stream_u32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit) — see `packed_rgb_u32_resample`.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Single-kernel filter: reject a BICUBLIN plan before any allocation (its chroma
  // windows are read only by the `Yuv420p` per-plane route, so filtering every
  // channel with the luma kernel would be wrong output).
  plan.ensure_single_kernel_filter()?;
  // Pre-grow the emit's out-width narrow scratch with its exact predicate so that
  // fallible growth precedes the freeze too (see `packed_rgb_u32_resample`).
  let need_narrow = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || hsv.is_some()
    || (!NATIVE_LUMA16 && luma_u16.is_some());
  // Build the missing filter stream into a LOCAL and grow the staging scratches —
  // every fallible pre-feed step — with NO field mutation, so the stream field
  // insert and the freeze commit TOGETHER (the `planar_dual_resample` atomic
  // shape: a scratch OOM leaves `rgb_filter_stream_u32` `None` AND
  // `resample_outputs` uncommitted, never a partial commit).
  let new_stream = if rgb_filter_stream_u32.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 3)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  source_rgb_u32_scratch(rgb_scratch_u32, w, plan)?;
  source_rgb_u16_scratch(binned_scratch_u16, plan.out_w(), plan)?;
  if need_narrow {
    source_rgb_scratch(narrow_scratch, plan.out_w(), plan)?;
  }
  if let Some(s) = new_stream {
    *rgb_filter_stream_u32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  let stream = rgb_filter_stream_u32.as_mut().expect("created above");
  let src_u32 = source_rgb_u32_scratch(rgb_scratch_u32, w, plan)?;
  convert(src_u32);
  packed_rgb_u32_resample_emit::<NATIVE_LUMA16>(
    stream,
    plan,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    hsv,
    src_u32,
    binned_scratch_u16,
    narrow_scratch,
    matrix,
    full_range,
    idx,
    use_simd,
  )
}

/// Feeds the prepared source-width host-native `u32` RGB row into the (already
/// sequence-checked) `u32` stream and derives every attached output from each
/// finalized output row. Binning runs at full `u32` precision; each output
/// narrows only afterwards — `rgb_u16` / `rgba_u16` take `binned >> 16`, the u8
/// outputs `binned >> 24` (i.e. the `>> 16` u16 then `>> 8`), and luma / hsv
/// derive from the narrowed u8 RGB — so the resampled output is byte-identical
/// to a direct convert of the `u32`-binned frame (0-ULP, both ranges).
/// `NATIVE_LUMA16` selects the `luma_u16` source-of-truth (native-depth Q15
/// from the binned u16 RGB for `Gbrap32`, else the narrowed 8-bit Y'), mirroring
/// the u16 emit's `NATIVE_LUMA16`. The single source-of-truth narrowed-binned
/// u16 RGB row reuses `binned_scratch_u16` (the u32 path's u16 streams are never
/// created for these formats, so it is free); the out-width u8 narrow reuses
/// `narrow_scratch` and is sized only when a narrowed output is attached.
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_u32_resample_emit<const NATIVE_LUMA16: bool>(
  stream: &mut impl crate::resample::RowResampler<u32>,
  plan: &ResamplePlan,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  src_u32: &[u32],
  binned_scratch_u16: &mut Vec<u16>,
  narrow_scratch: &mut Vec<u8>,
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  let ow = plan.out_w();
  let narrowed_luma_u16 = !NATIVE_LUMA16 && luma_u16.is_some();
  let need_narrow =
    rgb.is_some() || rgba.is_some() || luma.is_some() || narrowed_luma_u16 || hsv.is_some();
  // The narrowed-binned u16 RGB row is the single source of truth for every
  // narrowed output; it is always sized (any output reads it). The out-width u8
  // RGB row is sized only when a narrowed output is attached.
  let binned16 = source_rgb_u16_scratch(binned_scratch_u16, ow, plan)?;
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  stream.feed_row(idx, src_u32, use_simd, |oy, binned| {
    // Narrow the binned `u32` RGB `>> 16` to native u16 — the 0-ULP narrow,
    // after binning. This row is the source of truth for every output.
    let b16 = &mut binned16[..3 * ow];
    for (d, &s) in b16.iter_mut().zip(binned.iter()) {
      *d = (s >> 16) as u16;
    }
    let b16: &[u16] = b16;
    if let Some(buf) = rgb_u16.as_deref_mut() {
      buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(b16);
    }
    if let Some(buf) = rgba_u16.as_deref_mut() {
      crate::row::expand_rgb_u16_to_rgba_u16_row::<16>(
        b16,
        &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
        ow,
      );
    }
    if NATIVE_LUMA16 && let Some(buf) = luma_u16.as_deref_mut() {
      crate::row::rgb_to_luma_u16_native_row(
        b16,
        &mut buf[oy * ow..(oy + 1) * ow],
        ow,
        matrix,
        full_range,
        16,
      );
    }
    if need_narrow {
      // The u8 RGB is the binned `u32 >> 24` = `(u32 >> 16) >> 8`.
      let nrow = &mut narrow[..3 * ow];
      for (d, &s) in nrow.iter_mut().zip(b16.iter()) {
        *d = (s >> 8) as u8;
      }
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      if let Some(buf) = rgba.as_deref_mut() {
        crate::row::expand_rgb_to_rgba_row(nrow, &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow], ow);
      }
      if let Some(buf) = luma.as_deref_mut() {
        crate::row::rgb_to_luma_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if narrowed_luma_u16 && let Some(buf) = luma_u16.as_deref_mut() {
        crate::row::rgb_to_luma_u16_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  Ok(())
}

/// Premultiplies one canonical `R, G, B, A` `u32` row in place at full `u32`
/// precision: each color channel becomes `round(c * α / u32::MAX)` (`u64`
/// intermediates); α is unchanged. The `u32` twin of
/// [`premultiply_rgba_row_in_place`] — the generic `u16` helper's `v * a`
/// overflows `u32`, so the `u32` tier needs the widened multiply.
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn premultiply_rgba_u32_row_in_place(row: &mut [u32], width: usize) {
  const MAX: u64 = u32::MAX as u64;
  const HALF: u64 = MAX / 2;
  for px in row[..width * 4].as_chunks_mut::<4>().0 {
    let a = u64::from(px[3]);
    for c in &mut px[..3] {
      let v = u64::from(*c);
      // `pm <= MAX` (since `v, a <= MAX`), so the cast back never truncates.
      *c = ((v * a + HALF) / MAX) as u32;
    }
  }
}

/// Un-premultiplies one binned canonical `R, G, B, A` `u32` row into a straight
/// `u32` RGBA destination (α copied through) at full `u32` precision. The `u32`
/// twin of [`unpremultiply_binned_rgba_into`].
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn unpremultiply_binned_rgba_u32_into(binned: &[u32], dst: &mut [u32], width: usize) {
  const MAX: u64 = u32::MAX as u64;
  for (out_px, in_px) in dst[..width * 4]
    .as_chunks_mut::<4>()
    .0
    .iter_mut()
    .zip(binned[..width * 4].as_chunks::<4>().0.iter())
  {
    let a = u64::from(in_px[3]);
    for c in 0..3 {
      let pm = u64::from(in_px[c]);
      // `checked_div` is `None` exactly when `α == 0` → a fully-transparent
      // binned pixel exposes no color (cannot bleed); else round-half-up + clamp.
      out_px[c] = (pm * MAX + a / 2).checked_div(a).map_or(0, |q| q.min(MAX)) as u32;
    }
    out_px[3] = in_px[3];
  }
}

/// Per-finalized-output-row derive shared by the native-`u32` packed-RGBA area
/// and filter tails: given the straight binned `u32` RGBA row `straight` (an
/// un-premultiply of the binned premult row on the area path, or the binned row
/// itself on the straight / filter paths), narrows `>> 16` to native u16 and
/// emits every attached output — the `SRC_BITS = 16` specialization of the u16
/// 4-channel emit. `color16` / `rgb16` / `narrow` are out-width scratch resolved
/// by the caller (the u32 path's u16 streams are never created for these
/// formats, so the reused u16 scratch is free).
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
fn derive_packed_rgba_u32_outputs<const NATIVE_LUMA16: bool>(
  oy: usize,
  ow: usize,
  straight: &[u32],
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  color16: &mut [u16],
  rgb16: &mut [u16],
  narrow: &mut [u8],
  need_narrow: bool,
  native_luma: bool,
  narrowed_luma_u16: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  use_simd: bool,
) {
  // Narrow the straight binned `u32` RGBA `>> 16` to native u16 — the single
  // source of truth for every output.
  let c16 = &mut color16[..4 * ow];
  for (d, &s) in c16.iter_mut().zip(straight.iter()) {
    *d = (s >> 16) as u16;
  }
  let c16: &[u16] = c16;
  if let Some(buf) = rgba_u16.as_deref_mut() {
    buf[oy * 4 * ow..(oy + 1) * 4 * ow].copy_from_slice(c16);
  }
  if let Some(buf) = rgb_u16.as_deref_mut() {
    drop_alpha_rgba_to_rgb_row(c16, &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow], ow);
  }
  if native_luma && let Some(buf) = luma_u16.as_deref_mut() {
    let rgb_row = &mut rgb16[..3 * ow];
    drop_alpha_rgba_to_rgb_row(c16, rgb_row, ow);
    crate::row::rgb_to_luma_u16_native_row(
      rgb_row,
      &mut buf[oy * ow..(oy + 1) * ow],
      ow,
      matrix,
      full_range,
      16,
    );
  }
  if need_narrow {
    // u8 RGB = straight `u32 >> 24` = `(u32 >> 16) >> 8`, drop α.
    let nrow = &mut narrow[..3 * ow];
    for (d, px) in nrow
      .as_chunks_mut::<3>()
      .0
      .iter_mut()
      .zip(c16.as_chunks::<4>().0.iter())
    {
      d[0] = (px[0] >> 8) as u8;
      d[1] = (px[1] >> 8) as u8;
      d[2] = (px[2] >> 8) as u8;
    }
    if let Some(buf) = rgb.as_deref_mut() {
      buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
    }
    if let Some(buf) = rgba.as_deref_mut() {
      // Narrow all four straight channels (α `>> 24` too) — a real binned α.
      let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      for (d, px) in dst
        .as_chunks_mut::<4>()
        .0
        .iter_mut()
        .zip(c16.as_chunks::<4>().0.iter())
      {
        d[0] = (px[0] >> 8) as u8;
        d[1] = (px[1] >> 8) as u8;
        d[2] = (px[2] >> 8) as u8;
        d[3] = (px[3] >> 8) as u8;
      }
    }
    if let Some(buf) = luma.as_deref_mut() {
      crate::row::rgb_to_luma_row(
        nrow,
        &mut buf[oy * ow..(oy + 1) * ow],
        ow,
        matrix,
        full_range,
        use_simd,
      );
    }
    if narrowed_luma_u16 && let Some(buf) = luma_u16.as_deref_mut() {
      crate::row::rgb_to_luma_u16_row(
        nrow,
        &mut buf[oy * ow..(oy + 1) * ow],
        ow,
        matrix,
        full_range,
        use_simd,
      );
    }
    if let Some(hsv) = hsv.as_mut() {
      let (h, s, v) = hsv.hsv();
      crate::row::rgb_to_hsv_row(
        nrow,
        &mut h[oy * ow..(oy + 1) * ow],
        &mut s[oy * ow..(oy + 1) * ow],
        &mut v[oy * ow..(oy + 1) * ow],
        ow,
        use_simd,
      );
    }
  }
}

/// Native-`u32` fused **area** downscale for the alpha-aware packed-RGBA
/// sources (`Rgba128` / `Gbrap32`) — the 0-ULP twin of
/// [`packed_rgba_u16_resample`] (area-only, no native-Y-luma group: these
/// sources' `luma_u16` is colour-derived). `convert_rgba_u32` stages the source
/// as a canonical host-native `R, G, B, A` `u32` row; under
/// [`AlphaMode::Premultiplied`] it is premultiplied at full `u32` precision
/// before binning and un-premultiplied per output row, so transparent pixels
/// never bleed. RFC #238 tail-collapse: the compare-only preflight commits the
/// output-set freeze only AFTER the fallible stream + scratch allocs, and the
/// stream is built into a LOCAL inserted alongside that freeze, so a first-row
/// allocation failure leaves `rgba_stream_u32` `None` AND `resample_outputs`
/// uncommitted (state-atomic + retryable, the #180 discipline) — byte-identical to
/// the pre-collapse split path.
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_u32_resample<const NATIVE_LUMA16: bool>(
  rgba_stream_u32: &mut Option<std::boxed::Box<crate::resample::AreaStream<u32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch_u32: &mut Vec<u32>,
  color_scratch_u32: &mut Vec<u32>,
  color_scratch_u16: &mut Vec<u16>,
  rgb_scratch_u16: &mut Vec<u16>,
  narrow_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  alpha_mode: AlphaMode,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba_u32: impl FnOnce(&mut [u32]),
) -> Result<(), MixedSinkerError> {
  // Area-only sink (native-`u32` packed RGBA is not routed to the filter path):
  // reject a filter plan before any work.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the colour stream is the sole
  // per-row stream), or `None` when no output is attached so the preflight
  // short-circuits to a no-op.
  let expected = if need_any {
    Some(rgba_stream_u32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible stream alloc + scratch growth below succeed (RFC #238
  // tail-collapse), so a first-row allocation failure leaves `resample_outputs`
  // uncommitted — retryable with a changed output attachment.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  let narrowed_luma_u16 = !NATIVE_LUMA16 && luma_u16.is_some();
  let need_narrow =
    rgb.is_some() || rgba.is_some() || luma.is_some() || narrowed_luma_u16 || hsv.is_some();
  let native_luma = NATIVE_LUMA16 && luma_u16.is_some();
  let premult = alpha_mode.is_premultiplied();
  // Build the missing stream into a LOCAL and grow every pre-feed scratch — every
  // fallible step — with NO field mutation, so the stream field insert and the
  // freeze commit TOGETHER only after they all succeed (the `planar_dual_resample`
  // atomic shape: a scratch OOM leaves `rgba_stream_u32` `None` AND
  // `resample_outputs` uncommitted, never a partial commit).
  let new_stream = if rgba_stream_u32.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  // Grow every pre-feed scratch (the conditional premult / native-luma / narrow
  // rows and the always-sized out-width colour + source-width RGBA staging) BEFORE
  // the commit, so a scratch OOM likewise leaves the stream field `None` and
  // `resample_outputs` uncommitted.
  if premult {
    source_rgba_u32_scratch(color_scratch_u32, ow, plan)?;
  }
  source_rgba_u16_scratch(color_scratch_u16, ow, plan)?;
  if native_luma {
    source_rgb_u16_scratch(rgb_scratch_u16, ow, plan)?;
  }
  if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?;
  }
  source_rgba_u32_scratch(rgba_scratch_u32, w, plan)?;
  // Every pre-feed allocation succeeded — COMMIT atomically: insert the stream and
  // freeze the output set together (the compare-only preflight above already caught
  // any later mid-frame output change).
  if let Some(s) = new_stream {
    *rgba_stream_u32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Re-fetch the scratches (these grows re-run as no-ops after the pre-commit grows
  // above). Out-width straight `u32` RGBA, resolved per output row — needed only to
  // hold the un-premultiply on the premult path (straight mode reads `binned`).
  let color_u32: &mut [u32] = if premult {
    source_rgba_u32_scratch(color_scratch_u32, ow, plan)?
  } else {
    &mut []
  };
  // Out-width straight native-u16 RGBA (always) and drop-alpha RGB (native luma).
  let color16 = source_rgba_u16_scratch(color_scratch_u16, ow, plan)?;
  let rgb16: &mut [u16] = if native_luma {
    source_rgb_u16_scratch(rgb_scratch_u16, ow, plan)?
  } else {
    &mut []
  };
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  let src_rgba = source_rgba_u32_scratch(rgba_scratch_u32, w, plan)?;
  convert_rgba_u32(src_rgba);
  if premult {
    premultiply_rgba_u32_row_in_place(src_rgba, w);
  }
  let stream = rgba_stream_u32.as_mut().expect("created above");
  stream.feed_row(idx, src_rgba, use_simd, |oy, binned| {
    let straight: &[u32] = if premult {
      let c = &mut color_u32[..4 * ow];
      unpremultiply_binned_rgba_u32_into(binned, c, ow);
      c
    } else {
      binned
    };
    derive_packed_rgba_u32_outputs::<NATIVE_LUMA16>(
      oy,
      ow,
      straight,
      rgb,
      rgba,
      rgb_u16,
      rgba_u16,
      luma,
      luma_u16,
      hsv,
      color16,
      rgb16,
      narrow,
      need_narrow,
      native_luma,
      narrowed_luma_u16,
      matrix,
      full_range,
      use_simd,
    );
  })?;
  Ok(())
}

/// Native-`u32` fused **filter** downscale for the alpha-aware packed-RGBA
/// sources (`Rgba128` / `Gbrap32`) — the 0-ULP twin of
/// [`packed_rgba_u16_filter_resample`]. PIL filters R, G, B, A independently
/// with no premultiplication, so the canonical host-native `R, G, B, A` `u32`
/// row bins through one 4-channel [`FilterStream<u32>`](crate::resample::FilterStream)
/// and every output narrows only afterwards (0-ULP). Premultiplied alpha has no
/// filter analogue — those frames stay on the area path; this is reached only
/// for straight alpha. RFC #238 tail-collapse: same compare-before / commit-after
/// discipline as [`packed_rgba_u32_resample`] — the stream builds into a LOCAL and
/// its field insert + the output-set freeze commit together only after every
/// fallible alloc, so a first-row failure leaves `rgba_filter_stream_u32` `None`
/// AND `resample_outputs` uncommitted (byte-identical to the pre-collapse path).
#[cfg(any(feature = "rgb", feature = "gbr"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_u32_filter_resample<const NATIVE_LUMA16: bool>(
  rgba_filter_stream_u32: &mut Option<std::boxed::Box<crate::resample::FilterStream<u32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch_u32: &mut Vec<u32>,
  color_scratch_u16: &mut Vec<u16>,
  rgb_scratch_u16: &mut Vec<u16>,
  narrow_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba_u32: impl FnOnce(&mut [u32]),
) -> Result<(), MixedSinkerError> {
  // Single-kernel filter tail — reject a BICUBLIN plan (its chroma windows are read
  // only by the `Yuv420p` per-plane route) before any state change.
  plan.ensure_single_kernel_filter()?;
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the 4-channel filter stream is
  // the sole per-row stream), or `None` when no output is attached so the preflight
  // short-circuits to a no-op.
  let expected = if need_any {
    Some(rgba_filter_stream_u32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible stream alloc + scratch growth below succeed (RFC #238
  // tail-collapse), so a first-row allocation failure leaves `resample_outputs`
  // uncommitted — retryable with a changed output attachment.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  let narrowed_luma_u16 = !NATIVE_LUMA16 && luma_u16.is_some();
  let need_narrow =
    rgb.is_some() || rgba.is_some() || luma.is_some() || narrowed_luma_u16 || hsv.is_some();
  let native_luma = NATIVE_LUMA16 && luma_u16.is_some();
  // Build the missing filter stream into a LOCAL and grow every pre-feed scratch —
  // every fallible step — with NO field mutation, so the stream field insert and the
  // freeze commit TOGETHER only after they all succeed (the `planar_dual_resample`
  // atomic shape: a scratch OOM leaves `rgba_filter_stream_u32` `None` AND
  // `resample_outputs` uncommitted, never a partial commit).
  let new_stream = if rgba_filter_stream_u32.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  // Grow every pre-feed scratch (the always-sized out-width colour + source-width
  // RGBA staging, plus the conditional native-luma / narrow rows) BEFORE the commit,
  // so a scratch OOM likewise leaves the stream field `None` and `resample_outputs`
  // uncommitted.
  source_rgba_u16_scratch(color_scratch_u16, ow, plan)?;
  if native_luma {
    source_rgb_u16_scratch(rgb_scratch_u16, ow, plan)?;
  }
  if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?;
  }
  source_rgba_u32_scratch(rgba_scratch_u32, w, plan)?;
  // Every pre-feed allocation succeeded — COMMIT atomically: insert the stream and
  // freeze the output set together (the compare-only preflight above already caught
  // any later mid-frame output change).
  if let Some(s) = new_stream {
    *rgba_filter_stream_u32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Re-fetch the scratches (these grows re-run as no-ops after the pre-commit grows
  // above).
  let color16 = source_rgba_u16_scratch(color_scratch_u16, ow, plan)?;
  let rgb16: &mut [u16] = if native_luma {
    source_rgb_u16_scratch(rgb_scratch_u16, ow, plan)?
  } else {
    &mut []
  };
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  let src_rgba = source_rgba_u32_scratch(rgba_scratch_u32, w, plan)?;
  convert_rgba_u32(src_rgba);
  let stream = rgba_filter_stream_u32.as_mut().expect("created above");
  stream.feed_row(idx, src_rgba, use_simd, |oy, binned| {
    // Filter is straight-alpha only; the binned row is the straight color.
    derive_packed_rgba_u32_outputs::<NATIVE_LUMA16>(
      oy,
      ow,
      binned,
      rgb,
      rgba,
      rgb_u16,
      rgba_u16,
      luma,
      luma_u16,
      hsv,
      color16,
      rgb16,
      narrow,
      need_narrow,
      native_luma,
      narrowed_luma_u16,
      matrix,
      full_range,
      use_simd,
    );
  })?;
  Ok(())
}

/// Source-width canonical `R, G, B, A` `u8` staging for the packed
/// straight/premult RGBA resample tail. Grows `scratch` to `4 * width`
/// `u8` under the planner's recoverable-allocation contract. Mirrors
/// [`source_rgb_scratch`] for the 4-channel RGBA row.
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "yuva",
  feature = "yuv-444-packed"
))]
pub(super) fn source_rgba_scratch<'s>(
  scratch: &'s mut Vec<u8>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [u8], MixedSinkerError> {
  let row = width
    .checked_mul(4)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.src_h(),
      4,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0);
  }
  Ok(&mut scratch[..row])
}

/// `u16` analogue of [`source_rgba_scratch`] — source-width canonical
/// `R, G, B, A` host-native `u16` staging for the high-bit packed RGBA
/// resample tail. Grows `scratch` to `4 * width` `u16`.
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "yuva",
  feature = "yuv-444-packed"
))]
pub(super) fn source_rgba_u16_scratch<'s>(
  scratch: &'s mut Vec<u16>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [u16], MixedSinkerError> {
  // Single-shot grow failpoint (RFC #238 tail-collapse tests): refuse this grow as
  // a host OOM would, proving a 4-channel `u16` scratch OOM AFTER a successful
  // stream build leaves the stream field `None` AND `resample_outputs` uncommitted
  // (retryable). The 4-channel twin of the `source_rgb_u16_scratch` failpoint, so a
  // packed-RGBA `u16` atomicity test can fail this specific pre-commit grow.
  #[cfg(all(
    test,
    feature = "std",
    any(feature = "rgb", feature = "gbr", feature = "gray")
  ))]
  if FORCE_SOURCE_RGBA_U16_SCRATCH_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
    )));
  }
  let row = width
    .checked_mul(4)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.src_h(),
      4,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0);
  }
  Ok(&mut scratch[..row])
}

// Single-shot source-RGBA-`u16`-scratch grow failpoint for the RFC #238
// tail-collapse transactional-preflight tests. Gated on `any(rgb, gbr, gray)` (the
// families that bin through the shared 4-channel `rgba_stream_u16` and stage a
// source-width `u16` RGBA row pre-commit); `source_rgba_u16_scratch` consults it
// under the same gate. Separate from the `try_box` box-alloc failpoint so a
// 4-channel `u16` test can fail the SCRATCH grow specifically (after a successful
// stream construction, before the stream-field insert).
#[cfg(all(
  test,
  feature = "std",
  any(feature = "rgb", feature = "gbr", feature = "gray")
))]
std::thread_local! {
  static FORCE_SOURCE_RGBA_U16_SCRATCH_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}

/// Arms a single-shot failpoint that makes the next [`source_rgba_u16_scratch`]
/// grow refuse with `AllocationFailed`, exactly as a host OOM would. Proves the
/// commit-together atomic shape of [`packed_rgba_u16_resample`] (a source-staging
/// OOM after a successful stream build leaves the stream fields `None` AND
/// `resample_outputs` uncommitted, so the row is retryable). Test-only.
#[cfg(all(test, feature = "std", feature = "rgb"))]
pub(crate) fn arm_source_rgba_u16_scratch_failure() {
  FORCE_SOURCE_RGBA_U16_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// Premultiplies one canonical `R, G, B, A` row in place: each color
/// channel becomes `round(c * α / MAX)`; α is left unchanged. The exact
/// integer op the [`AlphaMode::Premultiplied`] oracle mirrors, so the
/// binned-then-un-premultiplied output is byte-exact. `MAX` is `255` for
/// the u8 path and the source's native max for the u16 path.
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "yuva",
  feature = "yuv-444-packed"
))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn premultiply_rgba_row_in_place<T>(row: &mut [T], width: usize, max: u32)
where
  T: Copy + TryFrom<u32> + Into<u32>,
{
  let half = max / 2;
  for px in row[..width * 4].as_chunks_mut::<4>().0 {
    let a: u32 = px[3].into();
    for c in &mut px[..3] {
      let v: u32 = (*c).into();
      let pm = (v * a + half) / max;
      // `pm <= max` (since `v, a <= max`), so the cast back never fails.
      *c = T::try_from(pm).unwrap_or_else(|_| unreachable!("premultiplied value <= max"));
    }
  }
}

/// Un-premultiplied straight color channel for one premultiplied binned
/// value: `round(pm * MAX / α)` clamped to `MAX`, or `0` when `α == 0`
/// (a fully-transparent binned pixel exposes no color, so it cannot
/// bleed). The exact integer inverse of [`premultiply_rgba_row_in_place`].
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "yuva",
  feature = "yuv-444-packed"
))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn unpremultiply_channel(pm: u32, a: u32, max: u32) -> u32 {
  // `checked_div` yields `None` exactly when `α == 0`, which maps to a
  // zero straight channel (a fully-transparent binned pixel exposes no
  // color); otherwise round-half-up and clamp to `max`.
  (pm * max + a / 2).checked_div(a).map_or(0, |q| q.min(max))
}

/// Un-premultiplies one binned canonical `R, G, B, A` row into the
/// caller's straight-RGBA destination (α copied through). Applied per
/// finalized output row when binning premultiplied.
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "yuva",
  feature = "yuv-444-packed"
))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn unpremultiply_binned_rgba_into<T>(binned: &[T], dst: &mut [T], width: usize, max: u32)
where
  T: Copy + TryFrom<u32> + Into<u32>,
{
  for (out_px, in_px) in dst[..width * 4]
    .as_chunks_mut::<4>()
    .0
    .iter_mut()
    .zip(binned[..width * 4].as_chunks::<4>().0.iter())
  {
    let a: u32 = in_px[3].into();
    for c in 0..3 {
      let straight = unpremultiply_channel(in_px[c].into(), a, max);
      out_px[c] =
        T::try_from(straight).unwrap_or_else(|_| unreachable!("un-premultiplied value <= max"));
    }
    out_px[3] = in_px[3];
  }
}

/// Un-premultiplies one binned canonical `R, G, B, A` row into a
/// straight **RGB** destination (α dropped) — the packed RGB the
/// luma / hsv kernels consume in premultiplied mode.
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "yuva",
  feature = "yuv-444-packed"
))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn unpremultiply_binned_rgb_into<T>(binned: &[T], dst: &mut [T], width: usize, max: u32)
where
  T: Copy + TryFrom<u32> + Into<u32>,
{
  for (out_px, in_px) in dst[..width * 3]
    .as_chunks_mut::<3>()
    .0
    .iter_mut()
    .zip(binned[..width * 4].as_chunks::<4>().0.iter())
  {
    let a: u32 = in_px[3].into();
    for c in 0..3 {
      let straight = unpremultiply_channel(in_px[c].into(), a, max);
      out_px[c] =
        T::try_from(straight).unwrap_or_else(|_| unreachable!("un-premultiplied value <= max"));
    }
  }
}

/// Drops α from one canonical `R, G, B, A` row into a packed `R, G, B`
/// destination — the straight-mode RGB the luma / hsv kernels consume.
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "yuva",
  feature = "yuv-444-packed"
))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn drop_alpha_rgba_to_rgb_row<T: Copy>(rgba: &[T], dst: &mut [T], width: usize) {
  for (out_px, in_px) in dst[..width * 3]
    .as_chunks_mut::<3>()
    .0
    .iter_mut()
    .zip(rgba[..width * 4].as_chunks::<4>().0.iter())
  {
    out_px.copy_from_slice(&in_px[..3]);
  }
}

/// Rejects a mid-frame [`AlphaMode`] change for a resampled packed-RGBA
/// frame. The mode is snapshotted in `begin_frame` (`frozen` is the mode
/// at frame start); the resample route and binning use it, so each sink
/// calls this **before** route selection in `process` and a later
/// differing live mode trips `ResampleOutputsChanged` — no row is fed
/// under a changed mode, and the snapshot is immune to out-of-sequence
/// rows since it is taken at the frame boundary, not the first row.
/// (`frozen` is `None` only before the first `begin_frame`, a contract
/// violation, which is likewise rejected.)
#[cfg(any(
  feature = "rgb",
  feature = "gbr",
  feature = "gray",
  feature = "mono",
  feature = "yuva",
  feature = "yuv-444-packed"
))]
pub(super) fn check_frozen_alpha_mode(
  frozen: Option<AlphaMode>,
  current: AlphaMode,
  idx: usize,
) -> Result<(), MixedSinkerError> {
  if frozen != Some(current) {
    return Err(MixedSinkerError::ResampleOutputsChanged(
      ResampleOutputsChanged::new(idx),
    ));
  }
  Ok(())
}

/// Row-stage fused downscale for the packed straight/premultiplied RGBA
/// 8-bit family (`Rgba` / `Bgra` / `Argb` / `Abgr`) — the alpha-aware
/// 4-channel analogue of the 3-channel [`packed_rgb_resample_emit`]
/// path. `convert_rgba` stages the source row as a canonical
/// source-width `R, G, B, A` u8 row (identity / swap / α-rotate per
/// format); this tail bins all four channels so resampled alpha is a
/// real area mean (the forced-opaque-`0xFF` bug the 3-channel path hit),
/// then per finalized output row emits rgba (the binned row),
/// rgb (drop α), luma / luma_u16 / hsv (from the binned RGB).
///
/// Under [`AlphaMode::Premultiplied`] the staged row is premultiplied
/// in place before binning and un-premultiplied per output row, so the
/// color outputs are alpha-weighted and transparent pixels never bleed.
///
/// `NATIVE_Y_LUMA` selects the `luma` / `luma_u16` source-of-truth:
/// - `false` (`Rgba` / `Bgra` / `Argb` / `Abgr` / `Gbrap8`): both are
///   derived from the binned straight RGB via `rgb_to_luma*_row`,
///   honoring the matrix and range (the genuinely chromatic sources'
///   direct-path behavior). The native-Y luma stream / scratch are unused
///   and `deinterleave_y` is never called.
/// - `true` (`Ya8`): luma is a genuine **independent native-Y area bin**,
///   never derived from the alpha- or range-affected color. `deinterleave_y`
///   stages the native Y plane (the Y bytes of the packed `[Y, A]` row) into
///   a source-width u8 scratch; a 1-channel `AreaStream<u8>` (`y_luma_stream`)
///   bins it as a straight area mean, finalized in lockstep with the color
///   stream. `luma` is the binned Y byte and `luma_u16` its zero-extension —
///   byte-exact to the direct `ya8_to_luma_row` / `ya8_to_luma_u16_row`
///   kernels for every matrix, **both ranges**, AND every alpha mode (under
///   `AlphaMode::Premultiplied` the color collapses to `mean(Y*A)/mean(A)`,
///   so a color-derived luma would be wrong; the native-Y bin is `mean(Y)`
///   regardless of alpha). The color feed below emits no luma in this mode.
///
/// Atomic preflight with conditional ordering: a no-output call returns
/// before any freeze; an out-of-sequence FIRST row is rejected before
/// the freeze (so a rejected row stores no snapshot to poison a retry);
/// on a later row the freeze runs first (so a mid-frame output change
/// trips `ResampleOutputsChanged` rather than being masked by a
/// freshly-attached stream's row-0 mismatch). Both streams and every
/// scratch (color staging, drop-alpha RGB, and — under `NATIVE_Y_LUMA` —
/// the native-Y stream and its staging) are created only after the
/// sequence check, all before the single feed, so a failure mutates no
/// caller output. The color and native-Y streams advance in lockstep
/// (same `idx`, same plan), so the single sequence check on the color
/// stream governs both.
#[cfg(any(feature = "rgb", feature = "gbr", feature = "gray"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_resample<const NATIVE_Y_LUMA: bool>(
  rgba_stream: &mut Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
  y_luma_stream: &mut Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<u8>,
  rgb_drop_scratch: &mut Vec<u8>,
  y_luma_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  alpha_mode: AlphaMode,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba: impl FnOnce(&mut [u8]),
  deinterleave_y: impl FnOnce(&mut [u8]),
) -> Result<(), MixedSinkerError> {
  // Area-only sink: reject a filter plan before any work (these packed
  // RGBA / YA families are not routed to the filter path).
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the colour stream is the
  // per-row stream; the native-Y stream advances in lockstep with it), or `None`
  // when no output is attached so the preflight short-circuits to a no-op.
  let expected = if need_any {
    Some(rgba_stream.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible stream allocs + scratch growth below succeed (RFC #238
  // tail-collapse), so a first-row allocation failure leaves `resample_outputs`
  // uncommitted — retryable with a changed output attachment.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  let premult = alpha_mode.is_premultiplied();
  // Under `NATIVE_Y_LUMA` (`Ya8`), luma / luma_u16 come from the
  // independent native-Y area stream, so the color path's drop-alpha RGB
  // row is needed only for the genuinely color-derived outputs.
  let need_y_luma = NATIVE_Y_LUMA && (luma.is_some() || luma_u16.is_some());
  // The rgb / rgb_u16 / hsv outputs (and, in the non-native-Y mode, luma /
  // luma_u16) need a packed RGB row. It is the per-mode binned color with
  // α dropped — sized to the out-width RGB row only when one of those is
  // attached, so an rgba-only sink neither grows it nor risks its
  // allocation failure. (`rgb_u16` zero-extends this same straight RGB;
  // `rgba_u16` zero-extends the straight RGBA resolved per pixel below —
  // the `Ya8` source exposes u16 RGB outputs the packed-RGBA / Gbrap8
  // sources do not, so they are threaded through the same u8 binning.)
  let need_rgb_drop = rgb.is_some()
    || rgb_u16.is_some()
    || hsv.is_some()
    || (!NATIVE_Y_LUMA && (luma.is_some() || luma_u16.is_some()));
  // Build the missing streams into LOCALS and grow the scratches — every fallible
  // pre-feed step — with NO field mutation, so both stream field inserts and the
  // freeze commit TOGETHER only after they all succeed (the `planar_dual_resample`
  // atomic shape: a scratch OOM leaves `rgba_stream` / `y_luma_stream` `None` AND
  // `resample_outputs` uncommitted, never a partial commit).
  let new_rgba = if rgba_stream.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  // Native-Y luma stream (`Ya8`): a 1-channel area bin of the native Y plane,
  // created in lockstep with the colour stream so both advance together (the
  // colour stream's sequence check governs both).
  let new_y_luma = if need_y_luma && y_luma_stream.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 1)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  if need_rgb_drop {
    source_rgb_scratch(rgb_drop_scratch, ow, plan)?;
  }
  if need_y_luma {
    source_luma_scratch(y_luma_scratch, w, plan)?;
  }
  source_rgba_scratch(rgba_scratch, w, plan)?;
  // Every pre-feed allocation (colour + native-Y streams, drop-alpha RGB scratch,
  // native-Y scratch, RGBA scratch) succeeded — COMMIT atomically: insert the
  // streams and freeze the output set together (the compare-only preflight above
  // already caught any later mid-frame output change).
  if let Some(s) = new_rgba {
    *rgba_stream = Some(s);
  }
  if let Some(s) = new_y_luma {
    *y_luma_stream = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Stage the scratches (these grows re-run as no-ops after the pre-commit grows
  // above); the native Y plane de-interleaves into its scratch before the feed.
  let rgb_drop: &mut [u8] = if need_rgb_drop {
    source_rgb_scratch(rgb_drop_scratch, ow, plan)?
  } else {
    &mut []
  };
  let y_src: &mut [u8] = if need_y_luma {
    let scratch = source_luma_scratch(y_luma_scratch, w, plan)?;
    deinterleave_y(scratch);
    scratch
  } else {
    &mut []
  };
  let src_rgba = source_rgba_scratch(rgba_scratch, w, plan)?;
  convert_rgba(src_rgba);
  if premult {
    premultiply_rgba_row_in_place::<u8>(src_rgba, w, 255);
  }
  let stream = rgba_stream.as_mut().expect("created above");
  stream.feed_row(idx, src_rgba, use_simd, |oy, binned| {
    // RGBA output is the per-mode straight color: straight mode copies
    // the binned row; premult mode un-premultiplies it into the dst.
    if let Some(buf) = rgba.as_deref_mut() {
      let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      if premult {
        unpremultiply_binned_rgba_into::<u8>(binned, dst, ow, 255);
      } else {
        dst.copy_from_slice(binned);
      }
    }
    // rgba_u16 zero-extends the straight RGBA: straight mode zero-extends
    // the binned row; premult mode un-premultiplies each channel first.
    if let Some(buf) = rgba_u16.as_deref_mut() {
      let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      if premult {
        for (out_px, in_px) in dst
          .as_chunks_mut::<4>()
          .0
          .iter_mut()
          .zip(binned.as_chunks::<4>().0.iter())
        {
          let a = in_px[3] as u32;
          for c in 0..3 {
            out_px[c] = unpremultiply_channel(in_px[c] as u32, a, 255) as u16;
          }
          out_px[3] = in_px[3] as u16;
        }
      } else {
        for (d, &s) in dst.iter_mut().zip(binned.iter()) {
          *d = s as u16;
        }
      }
    }
    if need_rgb_drop {
      let nrow = &mut rgb_drop[..3 * ow];
      if premult {
        unpremultiply_binned_rgb_into::<u8>(binned, nrow, ow, 255);
      } else {
        drop_alpha_rgba_to_rgb_row(binned, nrow, ow);
      }
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      // rgb_u16 zero-extends the straight RGB.
      if let Some(buf) = rgb_u16.as_deref_mut() {
        let dst = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
        for (d, &s) in dst.iter_mut().zip(nrow.iter()) {
          *d = s as u16;
        }
      }
      // luma / luma_u16 are color-derived only when NOT taking the
      // native-Y stream; under `NATIVE_Y_LUMA` they are emitted from the
      // independent native-Y bin below.
      if !NATIVE_Y_LUMA {
        if let Some(buf) = luma.as_deref_mut() {
          let dst = &mut buf[oy * ow..(oy + 1) * ow];
          crate::row::rgb_to_luma_row(nrow, dst, ow, matrix, full_range, use_simd);
        }
        if let Some(buf) = luma_u16.as_deref_mut() {
          let dst = &mut buf[oy * ow..(oy + 1) * ow];
          crate::row::rgb_to_luma_u16_row(nrow, dst, ow, matrix, full_range, use_simd);
        }
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  // Native-Y luma feed (`Ya8`): a straight single-channel area bin of the
  // native Y plane — alpha- and range-independent by construction. The
  // binned Y byte is the direct `ya8_to_luma_row` output; its zero
  // extension is the direct `ya8_to_luma_u16_row` output. Fed at the same
  // `idx` as the color stream, so the two stay in lockstep.
  if need_y_luma {
    let stream = y_luma_stream.as_mut().expect("created in the preflight");
    stream.feed_row(idx, y_src, use_simd, |oy, binned_y| {
      if let Some(buf) = luma.as_deref_mut() {
        buf[oy * ow..(oy + 1) * ow].copy_from_slice(binned_y);
      }
      if let Some(buf) = luma_u16.as_deref_mut() {
        let dst = &mut buf[oy * ow..(oy + 1) * ow];
        for (d, &y) in dst.iter_mut().zip(binned_y.iter()) {
          *d = y as u16;
        }
      }
    })?;
  }
  Ok(())
}

/// Row-stage fused downscale for `Pal8` (8-bit palette-indexed) — the
/// alpha-aware 4-channel analogue of the 3-channel Bayer feed. Averaging
/// palette *indices* is meaningless, so the only sensible area-resample is
/// to expand each pixel to its palette color and bin THAT: `convert_rgba`
/// stages the source row as a canonical source-width `R, G, B, A` u8 row
/// via the per-pixel palette lookup (`pal8_to_rgba_row`, FFmpeg `[B, G, R,
/// A]` → `[R, G, B, A]`), and this tail bins all four channels — so a
/// resampled frame is byte-identical to a direct full-res `Pal8` →
/// RGBA conversion followed by an area-bin of that color (the parity goal).
///
/// Like the genuinely-chromatic packed-RGBA sources, `Pal8` has **no
/// native luma plane**: its direct `luma` / `luma_u16` are derived from
/// the looked-up RGB. But unlike them it carries **no `KernelMatrix` /
/// range** on the row — its luma uses the sink's configured Q8 coefficient
/// set (`LumaCoefficients`, default BT.709), exactly as the Bayer / Pal8
/// identity path does. So this tail emits luma via the **Q8**
/// [`rgb_row_to_luma_row`] / [`rgb_row_to_luma_u16_row`] over the binned
/// straight RGB — NOT the matrix-based `rgb_to_luma_row` the
/// [`packed_rgba_resample`] tail uses — and `luma_u16` is the Q8 path's
/// `(y << 8) | y` full-range widening, the direct kernel's convention.
///
/// `Pal8`'s `rgb_u16` / `rgba_u16` outputs likewise widen each binned
/// 8-bit channel via `(v << 8) | v` (`pal8_to_*_u16_row`'s `expand_u8_to_u16`)
/// — the full-range expansion, **not** the zero-extension the `Ya8` /
/// `Rgba64` u16 paths use. (`Ya8`'s native-Y u16 keeps the low byte;
/// `Pal8`'s palette color is an 8-bit value mapped to the full u16 range.)
///
/// Under [`AlphaMode::Premultiplied`] the staged row is premultiplied in
/// place before binning and un-premultiplied per output row, so the color
/// outputs are alpha-weighted and a fully-transparent binned pixel
/// (`α == 0`) exposes zero color (never bleeds).
///
/// Same atomic conditional-ordering preflight as [`packed_rgba_resample`]:
/// a no-output call returns before any freeze; an out-of-sequence FIRST
/// row is rejected before the freeze (so a rejected row stores no snapshot
/// to poison a retry); a later-row output change trips
/// `ResampleOutputsChanged`; the color stream and both scratches are
/// created after the sequence check and before the single feed, so a
/// failure mutates no caller output.
#[cfg(feature = "mono")]
#[allow(clippy::too_many_arguments)]
pub(super) fn pal8_rgba_resample(
  rgba_stream: &mut Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<u8>,
  rgb_drop_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  alpha_mode: AlphaMode,
  luma_coeffs_q8: (u32, u32, u32),
  convert_rgba: impl FnOnce(&mut [u8]),
) -> Result<(), MixedSinkerError> {
  // Area-only sink (Pal8 is not routed to the filter path): reject a
  // filter plan before any work.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the RGBA colour stream is the
  // sole per-row stream), or `None` when no output is attached so the preflight
  // short-circuits to a no-op.
  let expected = if need_any {
    Some(rgba_stream.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER the
  // fallible stream alloc + scratch growth below succeed (RFC #238 tail-collapse), so
  // a first-row allocation failure leaves `resample_outputs` uncommitted — retryable
  // with a changed output attachment.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  let premult = alpha_mode.is_premultiplied();
  // The rgb / rgb_u16 / luma / luma_u16 / hsv outputs need a packed RGB
  // row (the per-mode binned color with α dropped); sized to the out-width
  // RGB row only when one of those is attached, so an rgba-only sink
  // neither grows it nor risks its allocation failure.
  let need_rgb_drop =
    rgb.is_some() || rgb_u16.is_some() || luma.is_some() || luma_u16.is_some() || hsv.is_some();
  // Build the missing stream into a LOCAL and grow the scratches with NO field
  // mutation, so the stream field insert and the freeze commit TOGETHER only after
  // they all succeed (a scratch OOM leaves `rgba_stream` `None` AND `resample_outputs`
  // uncommitted, never a partial commit).
  let new_stream = if rgba_stream.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  if need_rgb_drop {
    source_rgb_scratch(rgb_drop_scratch, ow, plan)?;
  }
  source_rgba_scratch(rgba_scratch, w, plan)?;
  // Every pre-feed allocation succeeded — COMMIT atomically: insert the stream and
  // freeze the output set together (the compare-only preflight above already caught
  // any later mid-frame output change).
  if let Some(s) = new_stream {
    *rgba_stream = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Stage the scratches (these grows re-run as no-ops after the pre-commit grows
  // above) and bin.
  let rgb_drop: &mut [u8] = if need_rgb_drop {
    source_rgb_scratch(rgb_drop_scratch, ow, plan)?
  } else {
    &mut []
  };
  let src_rgba = source_rgba_scratch(rgba_scratch, w, plan)?;
  convert_rgba(src_rgba);
  if premult {
    premultiply_rgba_row_in_place::<u8>(src_rgba, w, 255);
  }
  let stream = rgba_stream.as_mut().expect("created above");
  stream.feed_row(idx, src_rgba, use_simd, |oy, binned| {
    // RGBA output is the per-mode straight color: straight mode copies the
    // binned row; premult mode un-premultiplies it into the dst.
    if let Some(buf) = rgba.as_deref_mut() {
      let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      if premult {
        unpremultiply_binned_rgba_into::<u8>(binned, dst, ow, 255);
      } else {
        dst.copy_from_slice(binned);
      }
    }
    // rgba_u16 widens the straight RGBA via `(v << 8) | v` (the direct
    // `pal8_to_rgba_u16_row` convention), per channel including alpha.
    if let Some(buf) = rgba_u16.as_deref_mut() {
      let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      if premult {
        for (out_px, in_px) in dst
          .as_chunks_mut::<4>()
          .0
          .iter_mut()
          .zip(binned.as_chunks::<4>().0.iter())
        {
          let a = in_px[3] as u32;
          for c in 0..3 {
            let s = unpremultiply_channel(in_px[c] as u32, a, 255) as u16;
            out_px[c] = (s << 8) | s;
          }
          let a16 = in_px[3] as u16;
          out_px[3] = (a16 << 8) | a16;
        }
      } else {
        for (d, &s) in dst.iter_mut().zip(binned.iter()) {
          let s = s as u16;
          *d = (s << 8) | s;
        }
      }
    }
    if need_rgb_drop {
      let nrow = &mut rgb_drop[..3 * ow];
      if premult {
        unpremultiply_binned_rgb_into::<u8>(binned, nrow, ow, 255);
      } else {
        drop_alpha_rgba_to_rgb_row(binned, nrow, ow);
      }
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      // rgb_u16 widens the straight RGB via `(v << 8) | v`.
      if let Some(buf) = rgb_u16.as_deref_mut() {
        let dst = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
        for (d, &s) in dst.iter_mut().zip(nrow.iter()) {
          let s = s as u16;
          *d = (s << 8) | s;
        }
      }
      // luma / luma_u16: Q8 coefficients over the binned straight RGB (the
      // direct Pal8 path's derivation — no matrix, no range). `luma_u16`
      // is the Q8 path's `(y << 8) | y` widening.
      if let Some(buf) = luma.as_deref_mut() {
        rgb_row_to_luma_row(nrow, &mut buf[oy * ow..(oy + 1) * ow], luma_coeffs_q8);
      }
      if let Some(buf) = luma_u16.as_deref_mut() {
        rgb_row_to_luma_u16_row(nrow, &mut buf[oy * ow..(oy + 1) * ow], luma_coeffs_q8);
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  Ok(())
}

/// Separable-filter fused resize for `Pal8` (8-bit palette-indexed) — the
/// `Filter`-plan twin of the area [`pal8_rgba_resample`]. Filtering palette
/// *indices* is meaningless, exactly as averaging them is, so the source row
/// is first expanded to its palette color (`convert_rgba`, the same
/// `pal8_to_rgba_row` lookup the area path uses, real per-entry straight
/// alpha) and THAT canonical source-width `R, G, B, A` u8 row is fed to a
/// single 4-channel [`FilterStream`](crate::resample::FilterStream). Filtering
/// the looked-up colors (not the indices) is the convert-then-filter mirror of
/// the area convert-then-bin, so a resampled frame is byte-identical to a
/// direct full-res `Pal8` → RGBA conversion followed by the 4-channel
/// `FilterStream` of that color (the parity goal). Because the u8 filter is
/// byte-exact per channel, every output channel — alpha included — equals the
/// single-plane filter of that channel.
///
/// `Pal8` carries **real straight alpha** in the palette (never premultiplied),
/// so all four channels are filtered independently with no premultiplication —
/// the PIL RGBA convention, identical to the real-alpha packed-RGBA
/// [`packed_rgba_filter_resample`]. A premultiplied-mode frame has no filter
/// analogue (the filter engine cannot un-premultiply) and never reaches this
/// tail: the `Pal8` sink routes it to the area [`pal8_rgba_resample`], whose
/// `Filter`-plan reject surfaces the typed `UnsupportedFilter`.
///
/// The non-color outputs keep the **direct `Pal8` derivations**, matching the
/// area path exactly (NOT the matrix-based derivations the chromatic
/// [`packed_rgba_filter_resample`] uses): `rgb` drops alpha; `luma` /
/// `luma_u16` are the sink's **Q8** [`LumaCoefficients`] over the filtered
/// straight RGB ([`rgb_row_to_luma_row`] / [`rgb_row_to_luma_u16_row`], no
/// matrix / range), with `luma_u16` the Q8 path's `(y << 8) | y` full-range
/// widening; `rgb_u16` / `rgba_u16` widen each filtered 8-bit channel via
/// `(v << 8) | v` (the direct `pal8_to_*_u16_row` convention), per channel
/// including alpha. `Pal8` is an 8-bit source so there is no native-depth
/// clamp — the `FilterStream`'s `0..=255` clamp is exactly the source's
/// native range, even for a signed-kernel (CatmullRom / Lanczos3) overshoot.
///
/// Same atomic conditional-ordering preflight as [`pal8_rgba_resample`]: a
/// no-output call returns before any freeze; an out-of-sequence first row is
/// rejected before the freeze (so a rejected row stores no snapshot to poison
/// a retry); a later-row output change trips `ResampleOutputsChanged`; the
/// 4-channel stream and both scratches are created after the sequence check and
/// before the single feed, so a failure mutates no caller output and
/// `AllocationFailed` never masks `OutOfSequenceRow`.
#[cfg(feature = "mono")]
#[allow(clippy::too_many_arguments)]
pub(super) fn pal8_rgba_filter_resample(
  rgba_filter_stream: &mut Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<u8>,
  rgb_drop_scratch: &mut Vec<u8>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  luma_coeffs_q8: (u32, u32, u32),
  convert_rgba: impl FnOnce(&mut [u8]),
) -> Result<(), MixedSinkerError> {
  // Single-kernel filter tail — reject a BICUBLIN plan (its chroma windows are
  // read only by the `Yuv420p` per-plane route) before any state change.
  plan.ensure_single_kernel_filter()?;
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the RGBA filter stream is the
  // sole per-row stream), or `None` when no output is attached so the preflight
  // short-circuits to a no-op.
  let expected = if need_any {
    Some(rgba_filter_stream.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit) — see `pal8_rgba_resample`: the output-set
  // freeze commits only AFTER the fallible stream alloc + scratch growth below
  // succeed, so a first-row allocation failure leaves `resample_outputs` uncommitted.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // The rgb / rgb_u16 / luma / luma_u16 / hsv outputs need a packed RGB row
  // (the filtered color with α dropped); sized to the out-width RGB row only
  // when one of those is attached, so an rgba-only sink neither grows it nor
  // risks its allocation failure.
  let need_rgb_drop =
    rgb.is_some() || rgb_u16.is_some() || luma.is_some() || luma_u16.is_some() || hsv.is_some();
  // Build the missing filter stream into a LOCAL and grow the scratches with NO field
  // mutation, so the stream field insert and the freeze commit TOGETHER only after
  // they all succeed (a scratch OOM leaves `rgba_filter_stream` `None` AND
  // `resample_outputs` uncommitted, never a partial commit).
  let new_stream = if rgba_filter_stream.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  if need_rgb_drop {
    source_rgb_scratch(rgb_drop_scratch, ow, plan)?;
  }
  source_rgba_scratch(rgba_scratch, w, plan)?;
  // Every pre-feed allocation succeeded — COMMIT atomically: insert the stream and
  // freeze the output set together (the compare-only preflight above already caught
  // any later mid-frame output change).
  if let Some(s) = new_stream {
    *rgba_filter_stream = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Stage the scratches (these grows re-run as no-ops after the pre-commit grows
  // above) and filter.
  let rgb_drop: &mut [u8] = if need_rgb_drop {
    source_rgb_scratch(rgb_drop_scratch, ow, plan)?
  } else {
    &mut []
  };
  let src_rgba = source_rgba_scratch(rgba_scratch, w, plan)?;
  convert_rgba(src_rgba);
  let stream = rgba_filter_stream.as_mut().expect("created above");
  stream.feed_row(idx, src_rgba, use_simd, |oy, finalized| {
    // Straight-alpha RGBA output — the finalized 4-channel filter row.
    if let Some(buf) = rgba.as_deref_mut() {
      buf[oy * 4 * ow..(oy + 1) * 4 * ow].copy_from_slice(finalized);
    }
    // rgba_u16 widens the straight RGBA via `(v << 8) | v` (the direct
    // `pal8_to_rgba_u16_row` convention), per channel including alpha.
    if let Some(buf) = rgba_u16.as_deref_mut() {
      let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      for (d, &s) in dst.iter_mut().zip(finalized.iter()) {
        let s = s as u16;
        *d = (s << 8) | s;
      }
    }
    if need_rgb_drop {
      let nrow = &mut rgb_drop[..3 * ow];
      drop_alpha_rgba_to_rgb_row(finalized, nrow, ow);
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      // rgb_u16 widens the straight RGB via `(v << 8) | v`.
      if let Some(buf) = rgb_u16.as_deref_mut() {
        let dst = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
        for (d, &s) in dst.iter_mut().zip(nrow.iter()) {
          let s = s as u16;
          *d = (s << 8) | s;
        }
      }
      // luma / luma_u16: Q8 coefficients over the filtered straight RGB (the
      // direct Pal8 path's derivation — no matrix, no range). `luma_u16` is
      // the Q8 path's `(y << 8) | y` widening.
      if let Some(buf) = luma.as_deref_mut() {
        rgb_row_to_luma_row(nrow, &mut buf[oy * ow..(oy + 1) * ow], luma_coeffs_q8);
      }
      if let Some(buf) = luma_u16.as_deref_mut() {
        rgb_row_to_luma_u16_row(nrow, &mut buf[oy * ow..(oy + 1) * ow], luma_coeffs_q8);
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  Ok(())
}

/// Row-stage fused downscale for the high-bit packed straight/premult
/// RGBA family (`Rgba64` / `Bgra64`) and the high-bit planar GBR+alpha
/// family (`Gbrap10` … `Gbrap16`, decoded to the same canonical RGBA
/// row) — the alpha-aware 4-channel analogue of the 3-channel
/// [`packed_rgb_u16_resample_emit`] path. `convert_rgba_u16` stages the
/// wire row as a canonical host-native source-width `R, G, B, A` u16 row
/// (the format's `*_to_rgba_u16` kernel, source wire `BE`); this tail
/// bins all four channels at the source's native depth so resampled
/// alpha is a real area mean (not the forced-opaque-`(1 << SRC_BITS) - 1`
/// the 3-channel u16 path emitted), then per finalized output row
/// resolves the binned native RGBA to its straight form (a copy in
/// straight mode, an un-premultiply in premult mode) and emits: rgba_u16
/// / rgb_u16 at native depth, and rgba / rgb / luma / luma_u16 / hsv from
/// a single `>> (SRC_BITS - 8)` narrowing — the source-of-truth ordering
/// the 3-channel u16 path uses.
///
/// `SRC_BITS` is the source's active bit depth — `16` for the full-16-bit
/// `Rgba64` / `Bgra64`, and `10` / `12` / `14` / `16` for the high-bit
/// `Gbrap*` sources. It governs both the narrowing shift (`>> (SRC_BITS -
/// 8)`) and the native maximum `(1 << SRC_BITS) - 1` used for the
/// premultiply rounding and un-premultiply clamp (so a `Gbrap10` premult
/// bin un-premultiplies against `1023`, not `65535`). Mirrors the
/// `SRC_BITS` parameterization of [`packed_rgb_u16_resample_emit`].
///
/// `NATIVE_LUMA16` and `NATIVE_Y_LUMA` select the `luma` / `luma_u16`
/// source-of-truth (at most one is `true`):
/// - both `false` (`Rgba64` / `Bgra64`): luma_u16 at 8-bit precision from
///   the narrowed straight RGB (their direct path's behavior);
/// - `NATIVE_LUMA16` (`GbrapN`): full native precision from the binned
///   straight RGB via `rgb_to_luma_u16_native_row`, so a resampled
///   `GbrapN` luma_u16 is byte-identical to a direct `GbrapN` conversion
///   of the binned frame (grows `luma_rgb_scratch_u16` to the out-width
///   packed u16 RGB row when luma_u16 is attached);
/// - `NATIVE_Y_LUMA` (`Ya16`): luma is a genuine **independent native-Y
///   area bin**, never derived from the alpha- or range-affected color.
///   `deinterleave_y` stages the native Y plane (the Y elements of the
///   packed `[Y, A]` u16 row, host-native) into a source-width u16 scratch;
///   a 1-channel `AreaStream<u16>` (`y_luma_stream_u16`) bins it at native
///   depth, finalized in lockstep with the color stream. `luma_u16` is the
///   binned Y (host-native pass-through) and `luma` is `binned_y >> 8` —
///   byte-exact to the direct `ya16_to_luma_u16_row` / `ya16_to_luma_row`
///   kernels for every matrix (whereas `rgb_to_luma_u16_native_row` would
///   deviate for matrices whose Q15 weights do not sum to exactly `32768`,
///   e.g. SMPTE-240M), every range, AND every alpha mode (under
///   `AlphaMode::Premultiplied` the color collapses to `mean(Y*A)/mean(A)`,
///   so a color-derived luma would be wrong; the native-Y bin is `mean(Y)`
///   regardless of alpha). The narrowed color path emits no luma / luma_u16
///   in this mode.
///
/// Same atomic conditional-ordering preflight as [`packed_rgba_resample`]:
/// a no-output call returns before any freeze; an out-of-sequence first
/// row is rejected before the freeze; a later-row output change trips
/// `ResampleOutputsChanged`; both streams and every scratch are created
/// after the sequence check and before the single feed. The color and
/// native-Y streams advance in lockstep, so the single sequence check on
/// the color stream governs both.
#[cfg(any(feature = "rgb", feature = "gbr", feature = "gray"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_u16_resample<
  const SRC_BITS: u32,
  const NATIVE_LUMA16: bool,
  const NATIVE_Y_LUMA: bool,
>(
  rgba_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  y_luma_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch_u16: &mut Vec<u16>,
  color_scratch_u16: &mut Vec<u16>,
  narrow_scratch: &mut Vec<u8>,
  luma_rgb_scratch_u16: &mut Vec<u16>,
  y_luma_scratch_u16: &mut Vec<u16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  alpha_mode: AlphaMode,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba_u16: impl FnOnce(&mut [u16]),
  deinterleave_y: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError> {
  // Area-only sink (high-bit packed RGBA is not routed to the filter
  // path): reject a filter plan before any work.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  const {
    assert!(
      SRC_BITS > 8 && SRC_BITS <= 16,
      "SRC_BITS must be in (8, 16] for the high-bit packed RGBA tail"
    );
    assert!(
      !(NATIVE_LUMA16 && NATIVE_Y_LUMA),
      "luma_u16 has a single source-of-truth: NATIVE_LUMA16 and NATIVE_Y_LUMA are mutually exclusive"
    );
  };
  // `1 << 16` does not overflow u32; the native max governs premultiply
  // rounding and the un-premultiply clamp.
  let max: u32 = (1u32 << SRC_BITS) - 1;
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma.is_some()
    || luma_u16.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the colour stream is the
  // per-row stream; the native-Y stream advances in lockstep with it), or `None`
  // when no output is attached so the preflight short-circuits to a no-op.
  let expected = if need_any {
    Some(rgba_stream_u16.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible stream allocs + scratch growth below succeed (RFC #238
  // tail-collapse), so a first-row allocation failure leaves `resample_outputs`
  // uncommitted — retryable with a changed output attachment.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Under `NATIVE_Y_LUMA` (`Ya16`), luma / luma_u16 come from the
  // independent native-Y area stream, so the narrowed color path produces
  // them only in the non-native-Y modes.
  let need_y_luma = NATIVE_Y_LUMA && (luma.is_some() || luma_u16.is_some());
  // The u8 / narrowed outputs come from a `>> (SRC_BITS - 8)` narrowing of
  // the straight RGB. `luma_u16` narrows too in the plain branch
  // (`Rgba64` / `Bgra64`: their direct path takes luma at 8-bit precision),
  // but under `NATIVE_LUMA16` (`GbrapN`) luma_u16 is computed at full
  // native precision from the binned straight RGB instead — matching the
  // 3-channel high-bit path's native-luma parity — and under
  // `NATIVE_Y_LUMA` (`Ya16`) luma AND luma_u16 come from the native-Y
  // stream (not the color). None pull in the narrow scratch for luma. A
  // native-u16-only sink never touches the narrow scratch, so the
  // out-width u8 RGB scratch is sized — and its allocation failure risked —
  // only when one of the narrowed outputs is attached.
  let narrowed_luma_u16 = !NATIVE_LUMA16 && !NATIVE_Y_LUMA && luma_u16.is_some();
  let narrowed_luma = !NATIVE_Y_LUMA && luma.is_some();
  let need_narrow =
    rgb.is_some() || rgba.is_some() || narrowed_luma || narrowed_luma_u16 || hsv.is_some();
  // Native-precision luma_u16 (GbrapN) drops alpha from the straight color
  // into this out-width packed u16 RGB scratch, then runs the same
  // `rgb_to_luma_u16_native_row` the direct path uses — so the resampled
  // luma_u16 is byte-identical to a direct GbrapN conversion of the binned
  // frame. Sized only when that output is actually requested.
  let native_luma = NATIVE_LUMA16 && luma_u16.is_some();
  // Build the missing streams into LOCALS and grow the scratches — every fallible
  // pre-feed step — with NO field mutation, so both stream field inserts and the
  // freeze commit TOGETHER only after they all succeed (the `planar_dual_resample`
  // atomic shape: a scratch OOM leaves `rgba_stream_u16` / `y_luma_stream_u16`
  // `None` AND `resample_outputs` uncommitted, never a partial commit).
  let new_rgba = if rgba_stream_u16.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  // Native-Y luma stream (`Ya16`): a 1-channel native-depth area bin of the native
  // Y plane, created in lockstep with the colour stream so both advance together
  // (the colour stream's sequence check governs both).
  let new_y_luma = if need_y_luma && y_luma_stream_u16.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 1)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  // Grow every pre-feed scratch (the always-sized out-width colour row and
  // source-width RGBA staging, plus the conditional narrow / native-luma /
  // native-Y scratches) BEFORE the commit, so a scratch OOM likewise leaves the
  // stream fields `None` and `resample_outputs` uncommitted.
  source_rgba_u16_scratch(color_scratch_u16, ow, plan)?;
  if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?;
  }
  if native_luma {
    source_rgb_u16_scratch(luma_rgb_scratch_u16, ow, plan)?;
  }
  if need_y_luma {
    source_luma_u16_scratch(y_luma_scratch_u16, w, plan)?;
  }
  source_rgba_u16_scratch(rgba_scratch_u16, w, plan)?;
  // Every pre-feed allocation succeeded — COMMIT atomically: insert the streams and
  // freeze the output set together (the compare-only preflight above already caught
  // any later mid-frame output change).
  if let Some(s) = new_rgba {
    *rgba_stream_u16 = Some(s);
  }
  if let Some(s) = new_y_luma {
    *y_luma_stream_u16 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  // Re-fetch the scratches (these grows re-run as no-ops after the pre-commit grows
  // above). Out-width straight RGBA colour row (resolved per output row): always
  // sized when any output is attached, so every native and narrowed output reads
  // one canonical straight row.
  let color = source_rgba_u16_scratch(color_scratch_u16, ow, plan)?;
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  let luma_rgb: &mut [u16] = if native_luma {
    source_rgb_u16_scratch(luma_rgb_scratch_u16, ow, plan)?
  } else {
    &mut []
  };
  // Stage the native Y plane (host-native u16) into a source-width scratch before
  // the feed (all fallible growth precedes the first feed).
  let y_src: &mut [u16] = if need_y_luma {
    let scratch = source_luma_u16_scratch(y_luma_scratch_u16, w, plan)?;
    deinterleave_y(scratch);
    scratch
  } else {
    &mut []
  };
  let premult = alpha_mode.is_premultiplied();
  let src_rgba = source_rgba_u16_scratch(rgba_scratch_u16, w, plan)?;
  convert_rgba_u16(src_rgba);
  if premult {
    premultiply_rgba_row_in_place::<u16>(src_rgba, w, max);
  }
  let stream = rgba_stream_u16.as_mut().expect("created above");
  stream.feed_row(idx, src_rgba, use_simd, |oy, binned| {
    // Resolve the per-mode straight native RGBA once.
    let color = &mut color[..4 * ow];
    if premult {
      unpremultiply_binned_rgba_into::<u16>(binned, color, ow, max);
    } else {
      color.copy_from_slice(binned);
    }
    // Native-depth u16 outputs copy from the straight color row.
    if let Some(buf) = rgba_u16.as_deref_mut() {
      buf[oy * 4 * ow..(oy + 1) * 4 * ow].copy_from_slice(color);
    }
    if let Some(buf) = rgb_u16.as_deref_mut() {
      let dst = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
      drop_alpha_rgba_to_rgb_row(color, dst, ow);
    }
    // Native-precision luma_u16 (GbrapN): drop alpha from the straight
    // native color into the packed u16 RGB scratch, then run the exact
    // `rgb_to_luma_u16_native_row` the direct path uses — full parity at
    // native depth (the `Rgba64` / `Bgra64` path narrows instead, below).
    if native_luma && let Some(buf) = luma_u16.as_deref_mut() {
      let rgb_row = &mut luma_rgb[..3 * ow];
      drop_alpha_rgba_to_rgb_row(color, rgb_row, ow);
      crate::row::rgb_to_luma_u16_native_row(
        rgb_row,
        &mut buf[oy * ow..(oy + 1) * ow],
        ow,
        matrix,
        full_range,
        SRC_BITS,
      );
    }
    if need_narrow {
      let nrow = &mut narrow[..3 * ow];
      for (d, px) in nrow
        .as_chunks_mut::<3>()
        .0
        .iter_mut()
        .zip(color.as_chunks::<4>().0.iter())
      {
        d[0] = (px[0] >> (SRC_BITS - 8)) as u8;
        d[1] = (px[1] >> (SRC_BITS - 8)) as u8;
        d[2] = (px[2] >> (SRC_BITS - 8)) as u8;
      }
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      if let Some(buf) = rgba.as_deref_mut() {
        // Narrow all four straight channels (α `>> (SRC_BITS - 8)` too).
        let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
        for (d, px) in dst
          .as_chunks_mut::<4>()
          .0
          .iter_mut()
          .zip(color.as_chunks::<4>().0.iter())
        {
          d[0] = (px[0] >> (SRC_BITS - 8)) as u8;
          d[1] = (px[1] >> (SRC_BITS - 8)) as u8;
          d[2] = (px[2] >> (SRC_BITS - 8)) as u8;
          d[3] = (px[3] >> (SRC_BITS - 8)) as u8;
        }
      }
      // luma: 8-bit Y' from the narrowed straight RGB — the genuinely
      // chromatic sources' direct-path behavior. Skipped under
      // `NATIVE_Y_LUMA` (`Ya16`), where luma comes from the native-Y bin.
      if narrowed_luma && let Some(buf) = luma.as_deref_mut() {
        let dst = &mut buf[oy * ow..(oy + 1) * ow];
        crate::row::rgb_to_luma_row(nrow, dst, ow, matrix, full_range, use_simd);
      }
      // luma_u16: 8-bit-precision Y' derived from the narrowed straight
      // RGB and zero-extended — byte-identical to the direct full-range
      // u16 path's `luma_u16` (which narrows to u8 before luma). Skipped
      // under `NATIVE_LUMA16` (native-precision luma_u16 computed above)
      // and `NATIVE_Y_LUMA` (native binned Y from the Y stream below).
      if narrowed_luma_u16 && let Some(buf) = luma_u16.as_deref_mut() {
        crate::row::rgb_to_luma_u16_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  // Native-Y luma feed (`Ya16`): a native-depth single-channel area bin of
  // the host-native Y plane — alpha- and range-independent by construction.
  // The binned Y is the direct `ya16_to_luma_u16_row` output (host-native
  // pass-through); `binned_y >> 8` is the direct `ya16_to_luma_row` output.
  // Fed at the same `idx` as the color stream, so the two stay in lockstep.
  if need_y_luma {
    let stream = y_luma_stream_u16
      .as_mut()
      .expect("created in the preflight");
    stream.feed_row(idx, y_src, use_simd, |oy, binned_y| {
      if let Some(buf) = luma_u16.as_deref_mut() {
        buf[oy * ow..(oy + 1) * ow].copy_from_slice(binned_y);
      }
      if let Some(buf) = luma.as_deref_mut() {
        let dst = &mut buf[oy * ow..(oy + 1) * ow];
        for (d, &y) in dst.iter_mut().zip(binned_y.iter()) {
          *d = (y >> 8) as u8;
        }
      }
    })?;
  }
  Ok(())
}

/// Resets the three row-stage area streams (u8 color / native-u16 color
/// / native-u16 luma) and drops the frozen output set for a new frame —
/// the high-bit **planar** YUV 4:4:4 / 4:2:2 sinks' (`yuv-planar`) and the
/// high-bit **semi-planar** P-format sinks' (`yuv-semi-planar`)
/// `begin_frame` body (the streams are lazily created in `process`, so a
/// direct-`process` caller that skips `begin_frame` still gets a correctly
/// initialized first frame). Mirrors the packed high-bit 4:4:4 / 4:2:2
/// sinks' inline resets; factored out because the planar family has eight
/// `begin_frame` impls (Yuv444p / Yuv422p × 10/12/14/16) plus the nine
/// P-format impls share it.
#[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
pub(super) fn reset_high_bit_yuv_streams<F: SourceFormat, R>(sink: &mut MixedSinker<'_, F, R>) {
  if let Some(stream) = sink.rgb_stream.as_mut() {
    stream.reset();
  }
  if let Some(stream) = sink.rgb_stream_u16.as_mut() {
    stream.reset();
  }
  if let Some(stream) = sink.luma_stream_u16.as_mut() {
    stream.reset();
  }
  // The filter trio (lazily created when a `Filter` plan drives the sink)
  // shares the same per-frame restart as the area trio above; whichever
  // route the plan kind picks, its streams restart for the new frame.
  if let Some(stream) = sink.rgb_filter_stream.as_mut() {
    stream.reset();
  }
  if let Some(stream) = sink.rgb_filter_stream_u16.as_mut() {
    stream.reset();
  }
  if let Some(stream) = sink.luma_filter_stream_u16.as_mut() {
    stream.reset();
  }
  // The high-bit planar 4:2:0 native join (when present) shares the
  // frame-restart contract: restart its plane streams for the new frame.
  // The native join and its route guard exist only under `yuv-planar`
  // (the native tier's home — a yuv-semi-planar-solo build always takes
  // the row-stage tail, so there is no route to clear there).
  #[cfg(feature = "yuv-planar")]
  if let Some(join) = sink.native_420_u16.as_mut() {
    join.reset();
  }
  // The high-bit non-4:2:0 planar native join (when present) shares the
  // frame-restart contract too — restart its plane streams for the new frame.
  #[cfg(feature = "yuv-planar")]
  if let Some(join) = sink.native_planar_u16.as_mut() {
    join.reset();
  }
  // Clear the per-frame frozen native/row-stage route and averaging domain so
  // the next frame may pick either tier / any domain (the dispatch re-freezes
  // each on its first resampled row); a mid-frame flip within a frame stays
  // rejected. The RFC #238 frozen 4:2:2 chroma siting is cleared here too, so
  // the next frame may pick either phase while a mid-frame flip stays rejected.
  #[cfg(feature = "yuv-planar")]
  {
    sink.frozen_native_route = None;
    sink.frozen_domain = None;
    sink.frozen_chroma_centered = None;
    // RFC #238 S6d: clear the frozen VERTICAL (`Bottom`) chroma phase too, so
    // the next frame may pick Center or Bottom while a mid-frame Center ⇆ Bottom
    // flip stays rejected; and invalidate the high-bit bottom-sited vertical
    // lookback (`chroma_prev_u16`, tracked by the shared `chroma_prev_row` tag)
    // so frame N+1's first even row can never box-blend frame N's last chroma
    // row. Both stay `None` / untouched for every non-`Bottom` high-bit format.
    sink.frozen_chroma_bottom_v = None;
    sink.chroma_prev_row = None;
    // RFC #238 Top: clear the frozen VERTICAL (`Top`, `v = 0`) chroma phase so
    // the next frame may pick Top while a mid-frame flip to / from Top stays
    // rejected, and drop any held Top forward-delay odd row so frame N's last
    // deferred colour row never emits into frame N+1 (its `chroma_top_y_u16`
    // bytes are left as-is, re-overwritten before any trusted read). Both stay
    // `None` for every non-`Top` high-bit format.
    sink.frozen_chroma_top_v = None;
    sink.chroma_top_pending = None;
  }
  sink.resample_outputs = None;
}

/// Endian-normalizes one high-bit planar Y row into `scratch`, masking each
/// sample to the source's native depth `(1 << BITS) - 1` (a no-op at
/// `BITS = 16`, and a no-op for every in-range sample).
///
/// The depth-masking de-interleave for the high-bit planar **native-Y luma**
/// resample streams — the row-stage / filter `luma` + `luma_u16` feeds and the
/// native fast tier's binned-Y luma — shared across the plain high-bit planar
/// `Yuv4xxpN` arms and the `Yuva*pN*` arms. `Yuv*pN*Frame::try_new` is
/// geometry-only, so a malformed-but-accepted frame can carry out-of-range Y
/// (e.g. `0x1000` at 12-bit); binning it unmasked publishes a `luma` /
/// `luma_u16` *clamped* to the native max instead of *masked*, inconsistent
/// with the `(1 << BITS) - 1`-masked Y the colour RGB/RGBA kernels decode from
/// the same row (#334). Masking at de-interleave restores that consistency; on
/// the native tier the Y is shared with the colour decode, which re-masks via
/// `extract_hb`, so the pre-mask never changes an in-range colour binning.
/// (The native fast tiers also mask U / V through this: they area-bin chroma in
/// the YUV domain BEFORE the kernel re-masks, where `avg(dirty) & mask` can
/// differ from `avg(dirty & mask)`; the row-stage path converts each sample
/// before its RGB bin, so its chroma needs no pre-mask.)
#[cfg(feature = "yuv-planar")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn deinterleave_y_high_bit_masked<const BITS: u32, const BE: bool>(
  y: &[u16],
  scratch: &mut [u16],
  width: usize,
) {
  let mask = ((1u32 << BITS) - 1) as u16;
  for (dst, &s) in scratch[..width].iter_mut().zip(y.iter()) {
    *dst = (if BE { u16::from_be(s) } else { u16::from_le(s) }) & mask;
  }
}

/// Row-stage fused downscale for the high-bit packed 4:4:4 YUV family
/// (`V30X` / `V410` / `Xv36`). Unlike the 8-bit packed-YUV-4:2:2 path,
/// which carries a single u8 colour binning, this routes **three
/// independent area streams** because the direct path's per-output
/// conversions round and scale independently and luma is taken from the
/// native Y — so no single binning can reproduce every output:
///
/// 1. **u8 colour (rgb / rgba / hsv):** `convert_rgb_u8` stages the
///    direct u8 YUV→RGB row into a source-width u8 scratch; that row
///    bins through the shared u8 packed-RGB tail
///    ([`packed_rgb_resample_emit`]), which emits rgb / rgba / hsv.
///    Luma is *not* derived here (it is taken from Y), so `&mut None`
///    is passed for the tail's luma / luma_u16 outputs.
/// 2. **u16 colour (rgb_u16 / rgba_u16):** `convert_rgb_u16` stages the
///    native-depth u16 YUV→RGB row into a source-width u16 scratch; that
///    row bins through the shared u16 packed-RGB tail
///    ([`packed_rgb_u16_resample_emit`]) at `SRC_BITS`, emitting only
///    rgb_u16 / rgba_u16 (every narrowed output is `&mut None`, so the
///    tail's narrow scratch is never sized).
/// 3. **luma (luma / luma_u16):** `deinterleave_y` stages the native Y
///    into a source-width u16 scratch; a 1-channel `AreaStream<u16>`
///    bins it at native depth. `luma_u16` is the host-native binned Y;
///    `luma` is `binned_y >> (SRC_BITS - 8)`.
///
/// Colour outputs are byte-identical to the area-bin of the direct
/// full-resolution conversion (convert-then-bin — the fused form of
/// converting at full resolution then area-downscaling the RGB); luma is
/// the area-mean of the native Y. A uniform-gray downscale leaves every
/// colour output unchanged — the regression a single narrowed binning
/// would silently break.
///
/// Atomic preflight: a single [`frozen_outputs_check`] over the full
/// output set, then a single sequence check **before any allocation**
/// (so an out-of-sequence row is rejected without allocating and
/// `AllocationFailed` never masks `OutOfSequenceRow`), then every stream
/// and every source-width scratch are created — all before the first
/// feed — so a failure mutates no caller output. A no-output call has no
/// stream to sequence and stays a no-op regardless of the row index.
///
/// NOTE (#263 follow-up): HSV-only (no RGB / RGBA) still stages the
/// source-width u8 RGB row here and derives HSV per OUTPUT row off the
/// binned RGB stream — the direct + native fast tiers go RGB-free, but
/// the row-stage resample's `AreaStream` is keyed on the 3-channel RGB
/// row, so an RGB-free resample+HSV-only is deferred. The HSV output is
/// still correct; only the source-width RGB scratch remains.
#[cfg(any(
  feature = "yuv-444-packed",
  feature = "yuv-planar",
  feature = "yuv-semi-planar"
))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_yuv444_triple_resample<const SRC_BITS: u32>(
  rgb_stream: &mut Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
  rgb_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  luma_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut Vec<u8>,
  rgb_scratch_u16: &mut Vec<u16>,
  luma_scratch_u16: &mut Vec<u16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgb_u8: impl FnOnce(&mut [u8]),
  convert_rgb_u16: impl FnOnce(&mut [u16]),
  deinterleave_y: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError> {
  // Area entry: a `Filter` plan is dispatched by the caller to
  // [`packed_yuv444_triple_filter_resample`] (V410 / V30X) or rejected
  // there (the not-yet-routed 4:4:4 callers), so reject a filter plan
  // that reaches this area body before any work.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  const {
    assert!(
      SRC_BITS >= 8 && SRC_BITS <= 16,
      "SRC_BITS must be in [8, 16]"
    )
  };
  let need_luma = luma.is_some() || luma_u16.is_some();
  let need_u8_color = rgb.is_some() || rgba.is_some() || hsv.is_some();
  let need_u16_color = rgb_u16.is_some() || rgba_u16.is_some();

  // Single sequence check before any allocation. The canonical sequence
  // counter is whichever attached stream is fed every row; all attached
  // streams advance in lockstep, so checking one rejects an
  // out-of-sequence row for all without allocating any stream or scratch.
  let expected = if need_luma {
    luma_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else if need_u8_color {
    rgb_stream.as_ref().map_or(0, |s| s.next_y())
  } else if need_u16_color {
    rgb_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else {
    return Ok(());
  };
  // On the first row of a frame nothing is frozen yet, so reject an
  // out-of-sequence row here — before the freeze — so a rejected first row
  // never stores a snapshot that would poison a retry. On a later row the
  // freeze runs first (below) so a mid-frame output-set change is reported
  // as ResampleOutputsChanged rather than masked by a freshly-attached
  // stream's row-0 sequence mismatch.
  if resample_outputs.is_none() && expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }

  // Single freeze over the full output set (luma_f32 is never produced
  // by this family, so it is frozen as absent). A mid-frame output change
  // trips ResampleOutputsChanged.
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  if expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }

  // Create the streams (post-sequence-check). Each plane runs the full
  // output grid against its own source grid (width `w`, height
  // `plan.src_h()`); the colour streams carry 3 interleaved channels, the
  // luma stream 1.
  if need_u8_color && rgb_stream.is_none() {
    *rgb_stream = Some({
      let stream =
        crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 3)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_u16_color && rgb_stream_u16.is_none() {
    *rgb_stream_u16 = Some({
      let stream =
        crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 3)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_luma && luma_stream_u16.is_none() {
    *luma_stream_u16 = Some({
      let stream =
        crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 1)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }

  // Stage + feed + emit. Shared with the filter path
  // ([`packed_yuv444_triple_filter_resample`]) so the area and filter
  // arms run the identical convert-then-resample tail (the only
  // difference is the stream kind built above).
  packed_yuv444_triple_feed_emit::<_, _, _, SRC_BITS>(
    rgb_stream.as_mut(),
    rgb_stream_u16.as_mut(),
    luma_stream_u16.as_mut(),
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    rgb_scratch,
    rgb_scratch_u16,
    luma_scratch_u16,
    w,
    plan,
    idx,
    use_simd,
    matrix,
    full_range,
    convert_rgb_u8,
    convert_rgb_u16,
    deinterleave_y,
  )
}

/// Shared stage-then-feed tail for the packed 4:4:4 YUV (no-alpha)
/// family, used by both [`packed_yuv444_triple_resample`] (area) and
/// [`packed_yuv444_triple_filter_resample`] (filter). The two paths
/// differ only in the resampler kind built by the caller — the
/// convert-then-resample staging and per-output emit are identical, so
/// they live here behind the [`RowResampler`](crate::resample::RowResampler)
/// trait (which both [`AreaStream`](crate::resample::AreaStream) and
/// [`FilterStream`](crate::resample::FilterStream) implement). Keeping
/// the emit byte-identical between the arms is what makes the filter
/// output match the area output up to the kernel weights.
///
/// Each stream is `Some` exactly when its output group is attached (the
/// caller created it post-sequence-check). Staging runs every fallible
/// source-width growth before the first feed so a failure mutates no
/// caller output; the three scratches are distinct fields and never
/// alias.
#[cfg(any(
  feature = "yuv-444-packed",
  feature = "yuv-planar",
  feature = "y2xx",
  feature = "yuv-semi-planar"
))]
#[allow(clippy::too_many_arguments)]
fn packed_yuv444_triple_feed_emit<U8S, U16S, Y16S, const SRC_BITS: u32>(
  rgb_stream: Option<&mut U8S>,
  rgb_stream_u16: Option<&mut U16S>,
  luma_stream_u16: Option<&mut Y16S>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut Vec<u8>,
  rgb_scratch_u16: &mut Vec<u16>,
  luma_scratch_u16: &mut Vec<u16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgb_u8: impl FnOnce(&mut [u8]),
  convert_rgb_u16: impl FnOnce(&mut [u16]),
  deinterleave_y: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError>
where
  U8S: crate::resample::RowResampler<u8>,
  U16S: crate::resample::RowResampler<u16>,
  Y16S: crate::resample::RowResampler<u16>,
{
  let ow = plan.out_w();
  // Stage every source-width row (all fallible growths run before the
  // first feed, keeping the call atomic). The three scratches are
  // distinct fields and never alias.
  let u8_color_row = if rgb_stream.is_some() {
    let scratch = source_rgb_scratch(rgb_scratch, w, plan)?;
    convert_rgb_u8(scratch);
    Some(scratch)
  } else {
    None
  };
  let u16_color_row = if rgb_stream_u16.is_some() {
    let scratch = source_rgb_u16_scratch(rgb_scratch_u16, w, plan)?;
    convert_rgb_u16(scratch);
    Some(scratch)
  } else {
    None
  };
  let y_row = if luma_stream_u16.is_some() {
    let scratch = source_luma_u16_scratch(luma_scratch_u16, w, plan)?;
    deinterleave_y(scratch);
    Some(scratch)
  } else {
    None
  };

  // Feed each stream and emit. The u8 tail emits rgb / rgba / hsv only
  // (luma comes from Y, so its luma / luma_u16 are `&mut None`); the u16
  // tail emits rgb_u16 / rgba_u16 only (every narrowed output is
  // `&mut None`, so its narrow scratch is never sized — `rgb_scratch`,
  // already consumed by the u8 feed above, is passed as the unused
  // placeholder).
  if let Some(scratch) = u8_color_row {
    let stream = rgb_stream.expect("staged only when present");
    packed_rgb_resample_emit(
      stream, plan, rgb, rgba, &mut None, &mut None, hsv, scratch, matrix, full_range, idx,
      use_simd,
    )?;
  }
  if let Some(scratch) = u16_color_row {
    let stream = rgb_stream_u16.expect("staged only when present");
    packed_rgb_u16_resample_emit::<SRC_BITS, false>(
      stream,
      plan,
      &mut None,
      &mut None,
      &mut None,
      rgb_u16,
      rgba_u16,
      &mut None,
      &mut None,
      scratch,
      rgb_scratch,
      matrix,
      full_range,
      idx,
      use_simd,
    )?;
  }
  if let Some(scratch) = y_row {
    let stream = luma_stream_u16.expect("staged only when present");
    // A signed filter kernel (CatmullRom / Lanczos3) overshoots a legal
    // edge, so a finalized binned native-Y sample can exceed the source's
    // native max `(1 << SRC_BITS) - 1` even though the `FilterStream`
    // clamps it to the full `u16` range. The de-interleaved native-Y luma
    // bypasses the colour path's `packed_rgb_u16_resample_emit` clamp, so
    // we clip here — the luma twin of that helper's `native_max` clamp.
    // Without it the native-depth `luma_u16` would publish a value above
    // the documented `[0, (1 << SRC_BITS) - 1]` range, and the u8 luma
    // narrowing (`>> (SRC_BITS - 8)`) would wrap a clipped-high Y edge to a
    // small value instead of `255`. For `SRC_BITS == 16` the native max is
    // the u16 max, so this is a value no-op; the area path never
    // overshoots, so it is a no-op there too.
    let native_max: u16 = ((1u32 << SRC_BITS) - 1) as u16;
    stream.feed_row(idx, scratch, use_simd, |oy, binned_y| {
      // luma_u16: host-native pass-through of the binned native Y, clamped
      // to the native max for a sub-16-bit source.
      if let Some(buf) = luma_u16.as_deref_mut() {
        let out = &mut buf[oy * ow..(oy + 1) * ow];
        if SRC_BITS < 16 {
          for (dst, &src) in out.iter_mut().zip(binned_y) {
            *dst = src.min(native_max);
          }
        } else {
          out.copy_from_slice(binned_y);
        }
      }
      // luma: narrow the binned native Y to u8 (`>> (SRC_BITS - 8)`),
      // clamping to the native max first so a sub-16-bit overshoot clips to
      // `255` instead of wrapping.
      if let Some(buf) = luma.as_deref_mut() {
        if SRC_BITS < 16 {
          for (dst, &src) in buf[oy * ow..(oy + 1) * ow].iter_mut().zip(binned_y) {
            *dst = (src.min(native_max) >> (SRC_BITS - 8)) as u8;
          }
        } else {
          for (dst, &src) in buf[oy * ow..(oy + 1) * ow].iter_mut().zip(binned_y) {
            *dst = (src >> (SRC_BITS - 8)) as u8;
          }
        }
      }
    })?;
  }

  Ok(())
}

/// Separable-filter fused resize for the **packed 4:4:4 YUV (no-alpha)**
/// family — the [`SpanKind::Filter`](crate::resample::SpanKind) twin of
/// [`packed_yuv444_triple_resample`]. It mirrors the area path exactly:
/// the YUV is first converted to RGB by the **same** `convert_rgb_u8` /
/// `convert_rgb_u16` closures, then the RGB is resampled — only the
/// resampler kind differs (signed-coefficient [`FilterStream`] instead
/// of [`AreaStream`]). The staged RGB feeds the same emit
/// ([`packed_yuv444_triple_feed_emit`]), so the resampled colour output
/// equals the equivalent packed-RGB filter resample of the converted
/// pixels, and (because the area path converts-then-bins the same RGB)
/// matches the area output up to the kernel weights.
///
/// Luma is the native Y filter-resampled (the filter twin of the area
/// path's native-Y bin): `deinterleave_y` stages the native Y into a
/// source-width `u16` scratch and a 1-channel
/// [`FilterStream<u16>`](crate::resample::FilterStream)
/// (`luma_filter_stream_u16`) resamples it at native depth — luma is
/// taken from Y, never colour-derived, so it stays byte-exact to the
/// direct `*_to_luma*` kernels' filter resample. `luma_u16` is the
/// host-native resampled Y; `luma` is `resampled_y >> (SRC_BITS - 8)`.
///
/// Both the colour `u16` outputs and the native-Y luma carry the source's
/// native depth (`SRC_BITS`, `10` for `V410` / `V30X`, low-bit-packed
/// `[0, (1 << SRC_BITS) - 1]`): a signed kernel (CatmullRom / Lanczos3)
/// can overshoot a legal edge. For colour, [`packed_rgb_u16_resample_emit`]
/// clamps every resampled sample to the native max before any `u16` copy /
/// RGBA expansion / u8 narrowing (the same `SRC_BITS < 16` clamp the
/// high-bit GBR / planar filter routes use). The de-interleaved native-Y
/// luma bypasses that colour helper, so [`packed_yuv444_triple_feed_emit`]
/// applies the matching clamp to the binned Y before the `luma_u16` copy
/// and the u8 narrowing — keeping `luma_u16 <= (1 << SRC_BITS) - 1` and a
/// clipped-high Y edge mapping to `255` rather than wrapping. Both clamps
/// match the in-range area path's output exactly.
///
/// Atomic preflight (mirrors [`packed_yuv444_triple_resample`]): a single
/// [`frozen_outputs_check`] over the full output set, then a single
/// sequence check on whichever stream is fed every row **before any
/// allocation** (an out-of-sequence first row is rejected before the
/// freeze, storing no snapshot to poison a retry; on a later row the
/// freeze runs first so a mid-frame output change trips
/// `ResampleOutputsChanged`), then every stream and source-width scratch
/// is created before the first feed — so a failure mutates no caller
/// output. A no-output call has no stream to sequence and stays a no-op.
///
/// NOTE (#263 follow-up): HSV-only (no RGB / RGBA) still stages the
/// source-width u8 RGB row here and derives HSV per OUTPUT row off the
/// filtered RGB stream — the direct + native fast tiers go RGB-free, but
/// the row-stage filter resample's stream is keyed on the 3-channel RGB
/// row, so an RGB-free resample+HSV-only is deferred. The HSV output is
/// still correct; only the source-width RGB scratch remains.
#[cfg(any(
  feature = "yuv-444-packed",
  feature = "yuv-planar",
  feature = "yuv-semi-planar"
))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_yuv444_triple_filter_resample<const SRC_BITS: u32>(
  rgb_filter_stream: &mut Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
  rgb_filter_stream_u16: &mut Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  luma_filter_stream_u16: &mut Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut Vec<u8>,
  rgb_scratch_u16: &mut Vec<u16>,
  luma_scratch_u16: &mut Vec<u16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgb_u8: impl FnOnce(&mut [u8]),
  convert_rgb_u16: impl FnOnce(&mut [u16]),
  deinterleave_y: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError> {
  const {
    assert!(
      SRC_BITS >= 8 && SRC_BITS <= 16,
      "SRC_BITS must be in [8, 16]"
    )
  };
  // Single-kernel filter tail — reject a BICUBLIN plan (its chroma windows are
  // read only by the `Yuv420p` per-plane route) before any state change.
  plan.ensure_single_kernel_filter()?;
  let need_luma = luma.is_some() || luma_u16.is_some();
  let need_u8_color = rgb.is_some() || rgba.is_some() || hsv.is_some();
  let need_u16_color = rgb_u16.is_some() || rgba_u16.is_some();

  let (fh, fv) = (
    plan
      .filter_h()
      .expect("filter plan carries horizontal windows"),
    plan
      .filter_v()
      .expect("filter plan carries vertical windows"),
  );

  // Single sequence check before any allocation, on whichever attached
  // stream is fed every row (all attached streams advance in lockstep).
  let expected = if need_luma {
    luma_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else if need_u8_color {
    rgb_filter_stream.as_ref().map_or(0, |s| s.next_y())
  } else if need_u16_color {
    rgb_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else {
    return Ok(());
  };
  // First row: reject an out-of-sequence row before the freeze so a
  // rejected first row stores no snapshot that would poison a retry.
  if resample_outputs.is_none() && expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }

  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  if expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }

  // Create the filter streams (post-sequence-check). Each plane runs the
  // full output grid against its own source grid; the colour streams
  // carry 3 interleaved channels, the luma stream 1.
  if need_u8_color && rgb_filter_stream.is_none() {
    *rgb_filter_stream = Some({
      let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 3)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_u16_color && rgb_filter_stream_u16.is_none() {
    *rgb_filter_stream_u16 = Some({
      let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 3)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_luma && luma_filter_stream_u16.is_none() {
    *luma_filter_stream_u16 = Some({
      let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 1)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }

  packed_yuv444_triple_feed_emit::<_, _, _, SRC_BITS>(
    rgb_filter_stream.as_mut(),
    rgb_filter_stream_u16.as_mut(),
    luma_filter_stream_u16.as_mut(),
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    rgb_scratch,
    rgb_scratch_u16,
    luma_scratch_u16,
    w,
    plan,
    idx,
    use_simd,
    matrix,
    full_range,
    convert_rgb_u8,
    convert_rgb_u16,
    deinterleave_y,
  )
}

/// Shared stage-then-feed tail for the packed 4:4:4 YUV-**with-alpha**
/// family, used by both [`packed_yuva444_resample`] (area) and
/// [`packed_yuva444_filter_resample`] (filter). The two paths differ only
/// in the resampler kind built by the caller — the convert-then-resample
/// staging and per-output emit are identical, so they live here behind the
/// [`RowResampler`](crate::resample::RowResampler) trait (which both
/// [`AreaStream`](crate::resample::AreaStream) and
/// [`FilterStream`](crate::resample::FilterStream) implement). Keeping the
/// emit byte-identical between the arms is what makes the filter output
/// match the area output up to the kernel weights.
///
/// Each stream is `Some` exactly when its output group is attached (the
/// caller created it post-sequence-check). Staging runs every fallible
/// out-width resolve growth and source-width conversion before the first
/// feed so a failure mutates no caller output; the five scratches are
/// distinct fields and never alias.
///
/// `premult` selects the alpha resolve (the area path passes
/// `alpha_mode.is_premultiplied()`; the filter path is straight-only — a
/// premultiplied packed YUVA stays on the area path, which un-premultiplies
/// — so it passes `false`). When `premult` is set the source rows are
/// premultiplied in place before binning and each finalized row is
/// un-premultiplied on the way out.
///
/// `SRC_BITS` is the source's native depth (`8` for `Vuya` / `Vuyx`, `16`
/// for `Ayuv64`). A signed filter kernel (CatmullRom / Lanczos3) overshoots
/// a legal edge, so a finalized binned sample can exceed the source's
/// native max `(1 << SRC_BITS) - 1` even though the `FilterStream` clamps it
/// to the full element range. For `SRC_BITS < 16` every binned native-u16
/// colour channel (alpha too) and the binned native Y are clamped to the
/// native max before any u16 copy / RGB drop / u8 narrowing — the same
/// native-depth clamp the high-bit packed-RGBA and no-alpha 4:4:4 filter
/// tails apply, so `luma_u16` / `rgb_u16` / `rgba_u16` stay within
/// `[0, (1 << SRC_BITS) - 1]` and a clipped-high edge narrows to `255`
/// instead of wrapping. The area path never overshoots, so the clamp is a
/// value no-op there; at `SRC_BITS == 16` the native max is the element max,
/// so it is a no-op for `Ayuv64` too.
///
/// `ZEXT_U16_COLOR` (the packed 8-bit `Ya8` gray+alpha filter caller only):
/// when set, the u16 colour outputs are NOT an independent native-u16 binning
/// — the caller passes no u16 colour stream (`rgba_stream_u16 == None`, so
/// binning 2 below never runs and no native-u16 colour scratch is sized) and
/// the u8 colour binning (binning 1) emits `rgb_u16` / `rgba_u16` as the
/// straight binned u8 colour zero-extended (`s as u16`). This is correct only
/// for an 8-bit source, whose native-depth colour IS the u8 colour widened
/// (`rgba_u16 == rgba as u16`), matching the area path's contract. Every
/// other caller passes `false` and keeps the independent native-u16 binning
/// byte-for-byte. Straight alpha only on this path (the `Ya8` filter caller
/// never premultiplies).
#[cfg(any(feature = "yuv-444-packed", feature = "yuva", feature = "gray"))]
#[allow(clippy::too_many_arguments)]
fn packed_yuva444_feed_emit<C8S, C16S, Y16S, const SRC_BITS: u32, const ZEXT_U16_COLOR: bool>(
  rgba_stream: Option<&mut C8S>,
  rgba_stream_u16: Option<&mut C16S>,
  luma_stream_u16: Option<&mut Y16S>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<u8>,
  rgb_drop_scratch: &mut Vec<u8>,
  rgba_scratch_u16: &mut Vec<u16>,
  color_scratch_u16: &mut Vec<u16>,
  luma_scratch_u16: &mut Vec<u16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  premult: bool,
  convert_rgba_u8: impl FnOnce(&mut [u8]),
  convert_rgba_u16: impl FnOnce(&mut [u16]),
  deinterleave_y: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError>
where
  C8S: crate::resample::RowResampler<u8>,
  C16S: crate::resample::RowResampler<u16>,
  Y16S: crate::resample::RowResampler<u16>,
{
  // `1 << 16` does not overflow u32; governs the u16 premultiply rounding,
  // the un-premultiply clamp, and the sub-16-bit native-max clip. The u8
  // colour stream always uses 255.
  let max_u16: u32 = (1u32 << SRC_BITS) - 1;
  let native_max: u16 = max_u16 as u16;
  let ow = plan.out_w();
  let need_colour_u8 = rgba_stream.is_some();
  let need_colour_u16 = rgba_stream_u16.is_some();
  // `ZEXT_U16_COLOR` (the packed 8-bit `Ya8` gray+alpha caller): the u16
  // colour outputs are the zero-extension of the binned u8 colour (the
  // source is 8-bit, so its native-depth colour is the u8 colour widened —
  // `rgba_u16 == rgba as u16`, exactly the area path's contract), NOT an
  // independent native-u16 binning. So the caller passes no u16 colour
  // stream (`rgba_stream_u16 == None`, `need_colour_u16 == false`: binning 2
  // below never runs, no native-u16 scratch is sized) and the u8 binning
  // emits the widened u16 outputs inline. Every other caller passes
  // `false` and keeps the independent native-u16 binning byte-for-byte.
  let zext_u16 = ZEXT_U16_COLOR && (rgb_u16.is_some() || rgba_u16.is_some());
  debug_assert!(
    !ZEXT_U16_COLOR || !need_colour_u16,
    "ZEXT_U16_COLOR derives the u16 colour from the u8 binning, so no u16 colour stream is fed"
  );
  // The u8 colour stream's rgb / hsv outputs (and, under `ZEXT_U16_COLOR`,
  // rgb_u16) need a packed RGB row (the per-mode binned colour with α
  // dropped), sized to the out-width RGB row only when one of those is
  // attached so an rgba-only sink never grows it.
  let need_rgb_drop = rgb.is_some() || hsv.is_some() || (zext_u16 && rgb_u16.is_some());

  // Stage every source-width row and grow every out-width resolve scratch
  // before the first feed (all fallible growths precede it, keeping the
  // call atomic). The five scratches are distinct fields and never alias.
  let rgb_drop: &mut [u8] = if need_rgb_drop {
    source_rgb_scratch(rgb_drop_scratch, ow, plan)?
  } else {
    &mut []
  };
  let colour_u16: &mut [u16] = if need_colour_u16 {
    source_rgba_u16_scratch(color_scratch_u16, ow, plan)?
  } else {
    &mut []
  };
  let src_rgba_u8 = if need_colour_u8 {
    let scratch = source_rgba_scratch(rgba_scratch, w, plan)?;
    convert_rgba_u8(scratch);
    if premult {
      premultiply_rgba_row_in_place::<u8>(scratch, w, 255);
    }
    Some(scratch)
  } else {
    None
  };
  let src_rgba_u16 = if need_colour_u16 {
    let scratch = source_rgba_u16_scratch(rgba_scratch_u16, w, plan)?;
    convert_rgba_u16(scratch);
    if premult {
      premultiply_rgba_row_in_place::<u16>(scratch, w, max_u16);
    }
    Some(scratch)
  } else {
    None
  };
  let y_row = if luma_stream_u16.is_some() {
    let scratch = source_luma_u16_scratch(luma_scratch_u16, w, plan)?;
    deinterleave_y(scratch);
    Some(scratch)
  } else {
    None
  };

  // Binning 1 — u8 colour. Resolve the per-mode straight RGBA per output
  // row, then emit rgba (straight RGBA), rgb / hsv (straight RGB). luma is
  // native Y (binning 3), so it is never derived here. The u8 filter /
  // area stream already clamps to `[0, 255]`, so no native-max clip is
  // needed on this path.
  if let Some(scratch) = src_rgba_u8 {
    let stream = rgba_stream.expect("staged only when present");
    stream.feed_row(idx, scratch, use_simd, |oy, binned| {
      if let Some(buf) = rgba.as_deref_mut() {
        let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
        if premult {
          unpremultiply_binned_rgba_into::<u8>(binned, dst, ow, 255);
        } else {
          dst.copy_from_slice(binned);
        }
      }
      // `ZEXT_U16_COLOR`: rgba_u16 is the straight binned u8 RGBA
      // zero-extended (`s as u16`), matching the area path's degenerate-YUVA
      // u16 contract. Straight alpha only (this caller never premultiplies).
      if zext_u16 && let Some(buf) = rgba_u16.as_deref_mut() {
        let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
        for (d, &s) in dst.iter_mut().zip(binned.iter()) {
          *d = s as u16;
        }
      }
      if need_rgb_drop {
        let nrow = &mut rgb_drop[..3 * ow];
        if premult {
          unpremultiply_binned_rgb_into::<u8>(binned, nrow, ow, 255);
        } else {
          drop_alpha_rgba_to_rgb_row(binned, nrow, ow);
        }
        if let Some(buf) = rgb.as_deref_mut() {
          buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
        }
        // `ZEXT_U16_COLOR`: rgb_u16 zero-extends the straight binned u8 RGB
        // (α dropped) — the same `nrow` the u8 rgb / hsv use.
        if zext_u16 && let Some(buf) = rgb_u16.as_deref_mut() {
          let dst = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
          for (d, &s) in dst.iter_mut().zip(nrow.iter()) {
            *d = s as u16;
          }
        }
        if let Some(hsv) = hsv.as_mut() {
          let (h, s, v) = hsv.hsv();
          crate::row::rgb_to_hsv_row(
            nrow,
            &mut h[oy * ow..(oy + 1) * ow],
            &mut s[oy * ow..(oy + 1) * ow],
            &mut v[oy * ow..(oy + 1) * ow],
            ow,
            use_simd,
          );
        }
      }
    })?;
  }

  // Binning 2 — u16 colour at native depth, INDEPENDENT of binning 1.
  // Resolve the per-mode straight native RGBA per output row, then emit
  // rgba_u16 (straight) and rgb_u16 (drop α). A signed-kernel overshoot of
  // a sub-16-bit edge is clipped to the native max before any output is
  // derived (no-op at 16-bit — the `FilterStream`'s `0..=65535` clamp is
  // already the native clamp, and the area path never overshoots).
  if let Some(scratch) = src_rgba_u16 {
    let stream = rgba_stream_u16.expect("staged only when present");
    stream.feed_row(idx, scratch, use_simd, |oy, binned| {
      let colour = &mut colour_u16[..4 * ow];
      if premult {
        unpremultiply_binned_rgba_into::<u16>(binned, colour, ow, max_u16);
      } else if SRC_BITS < 16 {
        for (d, &s) in colour.iter_mut().zip(binned.iter()) {
          *d = s.min(native_max);
        }
      } else {
        colour.copy_from_slice(binned);
      }
      if let Some(buf) = rgba_u16.as_deref_mut() {
        buf[oy * 4 * ow..(oy + 1) * 4 * ow].copy_from_slice(colour);
      }
      if let Some(buf) = rgb_u16.as_deref_mut() {
        let dst = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
        drop_alpha_rgba_to_rgb_row(colour, dst, ow);
      }
    })?;
  }

  // Binning 3 — native Y through the 1-channel u16 luma stream. The binned
  // row is host-native; luma_u16 is its pass-through, luma the
  // `>> (SRC_BITS - 8)` narrowing (`>> 0` for an 8-bit source). Alpha- and
  // range-independent by construction. A sub-16-bit signed-kernel overshoot
  // is clipped to the native max before the luma_u16 copy AND the u8
  // narrow, so luma_u16 stays `<= (1 << SRC_BITS) - 1` and a clipped-high Y
  // edge narrows to `255` instead of wrapping (no-op at 16-bit / area).
  if let Some(y_row) = y_row {
    let stream = luma_stream_u16.expect("staged only when present");
    stream.feed_row(idx, y_row, use_simd, |oy, binned_y| {
      if let Some(buf) = luma_u16.as_deref_mut() {
        let out = &mut buf[oy * ow..(oy + 1) * ow];
        if SRC_BITS < 16 {
          for (dst, &y) in out.iter_mut().zip(binned_y) {
            *dst = y.min(native_max);
          }
        } else {
          out.copy_from_slice(binned_y);
        }
      }
      if let Some(buf) = luma.as_deref_mut() {
        if SRC_BITS < 16 {
          for (dst, &y) in buf[oy * ow..(oy + 1) * ow].iter_mut().zip(binned_y) {
            *dst = (y.min(native_max) >> (SRC_BITS - 8)) as u8;
          }
        } else {
          for (dst, &y) in buf[oy * ow..(oy + 1) * ow].iter_mut().zip(binned_y) {
            *dst = (y >> (SRC_BITS - 8)) as u8;
          }
        }
      }
    })?;
  }

  Ok(())
}

/// Row-stage fused downscale for the **packed 4:4:4 YUV-with-alpha**
/// family (`Vuya` 8-bit and `Ayuv64` 16-bit) — the alpha-aware analogue
/// of [`packed_yuv444_triple_resample`]. Packed YUVA is the most
/// demanding alpha family: it must reproduce a direct convert-then-bin
/// for **four** outputs that each round independently, so this routes up
/// to **four** independent area binnings rather than reusing the
/// packed-RGBA tails (whose u8 outputs are a `>> (SRC_BITS - 8)`
/// narrowing of the u16 bin — correct for an RGB source, but **wrong**
/// for YUV, whose u8 and u16 `YUV→RGB` kernels round and scale
/// independently). The four binnings:
///
/// 1. **u8 colour (rgb / rgba / hsv):** `convert_rgba_u8` stages the
///    direct u8 `YUV→RGB` row **with real source alpha** as a canonical
///    source-width `R, G, B, A` u8 row (`*_to_rgba_row`); the 4-channel
///    [`AreaStream<u8>`](crate::resample::AreaStream) (`rgba_stream`)
///    bins all four channels so resampled alpha is a real area mean.
///    Per finalized output row the binned RGBA resolves to its straight
///    form (a copy in [`AlphaMode::Straight`], an un-premultiply in
///    [`AlphaMode::Premultiplied`]) and emits rgba; rgb / hsv drop alpha.
/// 2. **u16 colour (rgb_u16 / rgba_u16):** `convert_rgba_u16` stages the
///    **independent** native-depth u16 `YUV→RGB` row with source alpha
///    (`*_to_rgba_u16_row`); the 4-channel [`AreaStream<u16>`]
///    (`rgba_stream_u16`) bins at native depth and emits rgba_u16 /
///    rgb_u16 from its own straight resolve — never a narrowing of (1).
/// 3. **luma (luma / luma_u16):** `deinterleave_y` stages the native Y
///    plane into a source-width u16 scratch; a 1-channel
///    [`AreaStream<u16>`] (`luma_stream_u16`) bins it at native depth.
///    Luma is **native Y**, NOT derived from either colour stream —
///    byte-exact to the direct `*_to_luma*` kernels for every matrix,
///    both ranges, AND every alpha mode. Under
///    [`AlphaMode::Premultiplied`] each colour stream collapses to
///    `mean(Y·A)/mean(A)`, but native Y stays `mean(Y)`; deriving luma
///    from colour would be wrong (the bug the `Ya` family fixed —
///    [`packed_rgba_resample`]'s `NATIVE_Y_LUMA`). luma_u16 is the
///    host-native binned Y; luma is `binned_Y >> (SRC_BITS - 8)` (an
///    8-bit `Vuya` is `>> 0`, a zero-extension; `Ayuv64` is `>> 8`).
///
/// `SRC_BITS` is the source's native Y / colour depth (`8` for `Vuya`,
/// `16` for `Ayuv64`): it governs the luma narrowing shift and the u16
/// premultiply / un-premultiply maximum `(1 << SRC_BITS) - 1` (so an
/// 8-bit source never builds the u16 colour stream — `Vuya` exposes no
/// u16 outputs, leaving `need_colour_u16` always false). The u8 colour
/// stream's premultiply maximum is always `255`.
///
/// This is an internal `pub(super)` tail, kept separate from
/// [`packed_yuv444_triple_resample`] so the no-alpha 4:4:4 callers
/// (`V30X` / `V410` / `Xv36`, and `Vuyx` whose padding byte forces α
/// opaque) stay byte-identical, and from the packed-RGBA tails so the
/// independent u8/u16 YUV colour rounding is preserved. The alpha
/// arithmetic reuses the shared [`premultiply_rgba_row_in_place`] /
/// [`unpremultiply_binned_rgba_into`] / [`unpremultiply_binned_rgb_into`]
/// helpers, so straight / premultiplied semantics are byte-identical to
/// the packed-RGBA family.
///
/// Atomic preflight (mirrors [`packed_yuv444_triple_resample`]): a
/// no-output call returns before any freeze; a single
/// [`frozen_outputs_check`] runs, then a single sequence check on
/// whichever stream is fed every row (all active streams advance in
/// lockstep) **before any allocation** — an out-of-sequence first row is
/// rejected before the freeze (storing no snapshot to poison a retry),
/// and on a later row the freeze runs first (a mid-frame output change
/// trips `ResampleOutputsChanged` rather than being masked by a
/// freshly-attached stream's row-0 mismatch). Every stream and every
/// (distinct, non-aliasing) scratch is created after the sequence check
/// and before the first feed, so a failure mutates no caller output. The
/// alpha mode is snapshotted at `begin_frame` and checked by the caller
/// (via [`check_frozen_alpha_mode`]) before this tail runs.
#[cfg(any(feature = "yuv-444-packed", feature = "yuva"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_yuva444_resample<const SRC_BITS: u32>(
  rgba_stream: &mut Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
  rgba_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  luma_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<u8>,
  rgb_drop_scratch: &mut Vec<u8>,
  rgba_scratch_u16: &mut Vec<u16>,
  color_scratch_u16: &mut Vec<u16>,
  luma_scratch_u16: &mut Vec<u16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  alpha_mode: AlphaMode,
  convert_rgba_u8: impl FnOnce(&mut [u8]),
  convert_rgba_u16: impl FnOnce(&mut [u16]),
  deinterleave_y: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError> {
  // Area-only sink (packed YUVA 4:4:4 is not routed to the filter path):
  // reject a filter plan before any work.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  const {
    assert!(
      SRC_BITS >= 8 && SRC_BITS <= 16,
      "SRC_BITS must be in [8, 16] for packed YUVA 4:4:4"
    )
  };
  let premult = alpha_mode.is_premultiplied();
  let need_colour_u8 = rgb.is_some() || rgba.is_some() || hsv.is_some();
  let need_colour_u16 = rgb_u16.is_some() || rgba_u16.is_some();
  let need_luma = luma.is_some() || luma_u16.is_some();

  // Single sequence check before any allocation, on whichever stream is
  // fed every row (all active streams advance in lockstep against the
  // frozen output set). A no-output call has no stream to sequence and
  // stays a no-op regardless of the row index.
  let expected = if need_colour_u8 {
    rgba_stream.as_ref().map_or(0, |s| s.next_y())
  } else if need_colour_u16 {
    rgba_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else if need_luma {
    luma_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else {
    return Ok(());
  };
  // First row: reject an out-of-sequence row before the freeze so a
  // rejected first row stores no snapshot that would poison a retry. On a
  // later row the freeze runs first (below).
  if resample_outputs.is_none() && expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  if expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }

  // Create the streams (post-sequence-check), each running the full
  // output grid against its own source grid. The colour streams carry 4
  // interleaved channels, the luma stream 1.
  if need_colour_u8 && rgba_stream.is_none() {
    *rgba_stream = Some({
      let stream =
        crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 4)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_colour_u16 && rgba_stream_u16.is_none() {
    *rgba_stream_u16 = Some({
      let stream =
        crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 4)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_luma && luma_stream_u16.is_none() {
    *luma_stream_u16 = Some({
      let stream =
        crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 1)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }

  // Stage + feed + emit. Shared with the filter path
  // ([`packed_yuva444_filter_resample`]) so the area and filter arms run
  // the identical convert-then-resolve-then-emit tail (the only difference
  // is the stream kind built above). The native-max clamp inside is a value
  // no-op for the area path (area binning never overshoots). The area path
  // keeps the independent native-u16 binning (`ZEXT_U16_COLOR = false`): a
  // genuine 16-bit packed YUVA (`Ayuv64`) needs it, and `Ya8`'s area path
  // already zero-extends via [`packed_rgba_resample`], not this tail.
  packed_yuva444_feed_emit::<_, _, _, SRC_BITS, false>(
    rgba_stream.as_mut(),
    rgba_stream_u16.as_mut(),
    luma_stream_u16.as_mut(),
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    rgba_scratch,
    rgb_drop_scratch,
    rgba_scratch_u16,
    color_scratch_u16,
    luma_scratch_u16,
    w,
    plan,
    idx,
    use_simd,
    premult,
    convert_rgba_u8,
    convert_rgba_u16,
    deinterleave_y,
  )
}

/// Separable-filter fused resize for the **packed 4:4:4 YUV-with-alpha**
/// family — the [`SpanKind::Filter`](crate::resample::SpanKind) twin of
/// [`packed_yuva444_resample`], and the 4-channel analogue of
/// [`packed_yuv444_triple_filter_resample`]. It mirrors the area path
/// exactly: the YUVA is first converted to a canonical `R, G, B, A` row
/// (real source alpha) by the **same** `convert_rgba_u8` / `convert_rgba_u16`
/// closures the area path uses, then resampled — only the resampler kind
/// differs (signed-coefficient [`FilterStream`] instead of [`AreaStream`]).
/// The staged RGBA feeds the same emit ([`packed_yuva444_feed_emit`]), so
/// each resampled colour output equals the equivalent packed-RGBA filter
/// resample of the converted pixels (`rgba` == the 8-bit `Rgba` filter; on
/// the independent-u16 path `rgba_u16` == the `Rgba64` filter clamped to the
/// native max, on the `ZEXT_U16_COLOR` path `rgba_u16` == `rgba as u16` — see
/// the const below), and matches the area output up to the kernel weights.
///
/// **Straight alpha only.** PIL filters R, G, B, A independently with no
/// premultiplication; a premultiplied packed YUVA source stays on the area
/// path (which un-premultiplies), so this is reached only for straight
/// alpha and passes `premult = false` to the shared emit. `Vuyx` reaches
/// here too: its padding byte forces α opaque, so `convert_rgba_u8` writes a
/// constant `0xFF` alpha plane — a constant channel filters to itself
/// (partition of unity), so the 4-channel filter reproduces the no-alpha
/// 3-channel result with α pinned opaque, no separate path needed.
///
/// Luma is the native Y filter-resampled (the filter twin of the area
/// path's native-Y bin) — luma is taken from Y, never colour-derived, so it
/// stays byte-exact to the direct `*_to_luma*` kernels' filter resample. The
/// const `NATIVE_LUMA_U8` selects which native-Y stream carries it:
///
/// - **`false` (the packed `Vuya` / `Vuyx` callers):** `deinterleave_y`
///   stages the native Y into a source-width `u16` scratch and a 1-channel
///   [`FilterStream<u16>`](crate::resample::FilterStream)
///   (`luma_filter_stream_u16`) resamples it at native depth (the shared
///   [`packed_yuva444_feed_emit`] owns the luma binning). The packed Y bytes
///   live interleaved in the source row, so there is no contiguous native-Y
///   plane to feed a `u8` stream directly.
/// - **`true` (the 8-bit planar `Yuva420p` / `Yuva422p` / `Yuva444p`
///   callers):** the contiguous native-Y plane `y_row_u8` feeds a 1-channel
///   [`FilterStream<u8>`](crate::resample::FilterStream)
///   (`luma_filter_stream`) directly — the **same** `u8` luma stream the
///   merged 8-bit planar YUV formats use
///   ([`planar_dual_filter_resample`](planar_resample::planar_dual_filter_resample)),
///   then `luma_u16` zero-extends each resampled Y byte. This keeps the
///   8-bit planar YUVA `luma` / `luma_u16` **byte-identical** to the no-alpha
///   `Yuv420p` / `Yuv422p` / `Yuv444p` filter of the same Y plane: a signed
///   kernel rounds and clamps `[0, 255]` on the 8bpc coefficient grid rather
///   than overshooting at full `u16` precision, so merely attaching an alpha
///   plane cannot change the luma. The 8-bit luma is handled inline here (the
///   shared emit is called with the luma outputs detached) so the shared
///   `u16` binning stays untouched for the packed callers.
///
/// `SRC_BITS` is the source's native Y / colour depth (`8` for `Vuya` /
/// `Vuyx` and the 8-bit planar YUVA). A signed kernel (CatmullRom /
/// Lanczos3) overshoots a legal edge, so for the `u16`-luma path the shared
/// emit clamps both the native-u16 colour and the native Y to the native max
/// `(1 << SRC_BITS) - 1` for a sub-16-bit source before any u16 copy / RGBA
/// expansion / u8 narrowing — keeping `rgb_u16` / `rgba_u16` /
/// `luma_u16 <= (1 << SRC_BITS) - 1` and a clipped-high edge mapping to `255`
/// rather than wrapping. (The 8-bit planar YUVA `u8`-luma path needs no such
/// clamp — the `FilterStream<u8>` already finalizes to the native `[0, 255]`
/// range. `Vuya` / `Vuyx` expose no u16 colour outputs, so the colour clamp
/// is exercised only by the native-Y luma there; both clamps match the
/// in-range area path exactly.)
///
/// The const `ZEXT_U16_COLOR` selects how the u16 colour outputs
/// (`rgb_u16` / `rgba_u16`) are derived:
///
/// - **`false` (every genuine-native-u16 caller — `Vuya` / `Vuyx` expose no
///   u16 colour, and the high-bit `Ayuv64` / `Ya16` are truly 16-bit):** the
///   shared emit bins an **independent** native-u16 colour stream
///   (`convert_rgba_u16` → `rgba_filter_stream_u16`), so `rgba_u16` is the
///   native-u16 filter, clamped to the native max — never a widening of the
///   u8 colour. `Ya16` keeps this: it is genuinely 16-bit, so its native u16
///   colour IS the native u16 filter (matching its area path).
/// - **`true` (the packed 8-bit `Ya8` gray+alpha caller only):** `Ya8` is an
///   8-bit source, so its native-depth colour is the binned **u8** colour
///   zero-extended — `rgba_u16 == rgba as u16`, `rgb_u16 == rgb as u16`,
///   byte-for-byte the contract `Ya8`'s area path
///   ([`packed_rgba_resample`]) emits (it likewise zero-extends the binned
///   u8, never an independent native-u16 bin). So **no** independent u16
///   colour stream is created or fed (`need_colour_u16` is forced false; the
///   `convert_rgba_u16` closure is dead and no native-u16 colour scratch is
///   sized) — the u8 colour binning emits the widened u16 outputs inline.
///   This makes the `Ya8` **filter** u16 colour byte-identical to its **area**
///   u16 colour, and to `rgba as u16`, closing a filter-vs-area /
///   u8-vs-u16 contract split (the independent u16 filter could differ from
///   `rgba as u16` by up to 1 LSB near a signed-kernel overshoot, since the
///   u8 and u16 streams clamp their H-pass intermediates differently). The
///   const-asserts `SRC_BITS == 8`; only valid for an 8-bit source.
///
/// Atomic preflight (mirrors [`packed_yuva444_resample`]): a no-output call
/// returns before any freeze; a single [`frozen_outputs_check`] over the
/// full output set, then a single sequence check on whichever stream is fed
/// every row **before any allocation** (an out-of-sequence first row is
/// rejected before the freeze, storing no snapshot to poison a retry; on a
/// later row the freeze runs first so a mid-frame output change trips
/// `ResampleOutputsChanged`), then every stream and source-width scratch is
/// created before the first feed — so a failure mutates no caller output.
///
/// The `NATIVE_LUMA_U8` u8-luma path takes its native Y either from the
/// caller's contiguous plane (`y_row_u8` non-empty — the planar YUVA callers,
/// whose Y plane is already contiguous, zero copy) or, when `y_row_u8` is
/// empty (the **packed** `Ya8` gray+alpha caller, whose Y is interleaved in
/// `[Y, A]`), by de-interleaving into the caller's `y_u8_scratch` via
/// `deinterleave_y_u8` — staged after the sequence check, so the staging
/// allocation cannot mask an out-of-sequence first row.
#[cfg(any(feature = "yuv-444-packed", feature = "yuva", feature = "gray"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_yuva444_filter_resample<
  const SRC_BITS: u32,
  const NATIVE_LUMA_U8: bool,
  const ZEXT_U16_COLOR: bool,
>(
  rgba_filter_stream: &mut Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
  rgba_filter_stream_u16: &mut Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  luma_filter_stream: &mut Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
  luma_filter_stream_u16: &mut Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<u8>,
  rgb_drop_scratch: &mut Vec<u8>,
  rgba_scratch_u16: &mut Vec<u16>,
  color_scratch_u16: &mut Vec<u16>,
  luma_scratch_u16: &mut Vec<u16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  // The contiguous native-Y plane fed to the `u8` luma stream when
  // `NATIVE_LUMA_U8` is set and the source has one (the 8-bit planar YUVA
  // callers — zero copy). Empty for the packed `Ya8` caller (whose Y is
  // interleaved): the u8-luma path then de-interleaves into `y_u8_scratch`
  // via `deinterleave_y_u8` instead. Ignored entirely on the `u16`-luma path
  // (`NATIVE_LUMA_U8 = false`, the packed `Vuya` / `Vuyx` / `Ya16` callers),
  // which routes luma through `deinterleave_y` + the `u16` stream.
  y_row_u8: &[u8],
  // Source-width `u8` staging for the packed `NATIVE_LUMA_U8` caller's native
  // Y (`Ya8`): used only when `y_row_u8` is empty, sized and filled after the
  // sequence check. `None` for every caller with a contiguous `y_row_u8` or
  // on the `u16`-luma path.
  y_u8_scratch: Option<&mut Vec<u8>>,
  convert_rgba_u8: impl FnOnce(&mut [u8]),
  convert_rgba_u16: impl FnOnce(&mut [u16]),
  deinterleave_y: impl FnOnce(&mut [u16]),
  // De-interleaves the packed source's native Y into `y_u8_scratch` for the
  // `NATIVE_LUMA_U8` u8-luma path when `y_row_u8` is empty (`Ya8`). A no-op
  // for every caller that supplies a contiguous `y_row_u8` or rides the
  // `u16`-luma stream.
  deinterleave_y_u8: impl FnOnce(&mut [u8]),
) -> Result<(), MixedSinkerError> {
  const {
    assert!(
      SRC_BITS >= 8 && SRC_BITS <= 16,
      "SRC_BITS must be in [8, 16] for packed YUVA 4:4:4"
    )
  };
  const {
    assert!(
      !NATIVE_LUMA_U8 || SRC_BITS == 8,
      "NATIVE_LUMA_U8 (the u8 native-Y luma stream) is only valid for an 8-bit source"
    )
  };
  const {
    assert!(
      !ZEXT_U16_COLOR || SRC_BITS == 8,
      "ZEXT_U16_COLOR (u16 colour = zero-extended u8 colour) is only valid for an 8-bit source"
    )
  };
  // `ZEXT_U16_COLOR` (the packed 8-bit `Ya8` gray+alpha caller): the source
  // is 8-bit, so its native-depth colour is the binned u8 colour
  // zero-extended (`rgba_u16 == rgba as u16`) — matching the area path's
  // contract exactly — NOT an independent native-u16 binning. So the u16
  // colour outputs ride the u8 colour stream (the u8 binning emits them
  // widened inline in the shared emit) and the independent native-u16 stream
  // is never created or fed. Every other caller has `ZEXT_U16_COLOR == false`
  // and keeps the independent native-u16 binning byte-for-byte.
  // Single-kernel filter tail — reject a BICUBLIN plan (its chroma windows are
  // read only by the `Yuv420p` per-plane route) before any state change. A
  // `Yuva420p` sink can carry a `Bicublin` resampler, so this fence is live.
  plan.ensure_single_kernel_filter()?;
  let zext_u16_color = ZEXT_U16_COLOR && (rgb_u16.is_some() || rgba_u16.is_some());
  let need_colour_u8 = rgb.is_some() || rgba.is_some() || hsv.is_some() || zext_u16_color;
  let need_colour_u16 = (rgb_u16.is_some() || rgba_u16.is_some()) && !ZEXT_U16_COLOR;
  let need_luma = luma.is_some() || luma_u16.is_some();

  let (fh, fv) = (
    plan
      .filter_h()
      .expect("filter plan carries horizontal windows"),
    plan
      .filter_v()
      .expect("filter plan carries vertical windows"),
  );

  // Single sequence check before any allocation, on whichever attached
  // stream is fed every row (all active streams advance in lockstep). For the
  // 8-bit planar YUVA path luma rides the `u8` stream; for the packed path it
  // rides the `u16` stream.
  let expected = if need_colour_u8 {
    rgba_filter_stream.as_ref().map_or(0, |s| s.next_y())
  } else if need_colour_u16 {
    rgba_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else if need_luma {
    if NATIVE_LUMA_U8 {
      luma_filter_stream.as_ref().map_or(0, |s| s.next_y())
    } else {
      luma_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
    }
  } else {
    return Ok(());
  };
  // First row: reject an out-of-sequence row before the freeze so a
  // rejected first row stores no snapshot that would poison a retry. On a
  // later row the freeze runs first (below).
  if resample_outputs.is_none() && expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  if expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }

  // Create the filter streams (post-sequence-check), each running the full
  // output grid against its own source grid. The colour streams carry 4
  // interleaved channels, the luma stream 1. For the 8-bit planar YUVA path
  // (`NATIVE_LUMA_U8`) luma is a `u8` stream over the contiguous native-Y
  // plane — identical to the merged no-alpha planar YUV formats; for the
  // packed path it is a `u16` stream over the de-interleaved native Y.
  if need_colour_u8 && rgba_filter_stream.is_none() {
    *rgba_filter_stream = Some({
      let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 4)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_colour_u16 && rgba_filter_stream_u16.is_none() {
    *rgba_filter_stream_u16 = Some({
      let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 4)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_luma {
    if NATIVE_LUMA_U8 {
      if luma_filter_stream.is_none() {
        *luma_filter_stream = Some({
          let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 1)?;
          crate::resample::try_box(stream).map_err(|_| {
            MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
              crate::resample::PlanGeometry::new(
                plan.src_w(),
                plan.src_h(),
                plan.out_w(),
                plan.out_h(),
              ),
            ))
          })?
        });
      }
    } else if luma_filter_stream_u16.is_none() {
      *luma_filter_stream_u16 = Some({
        let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 1)?;
        crate::resample::try_box(stream).map_err(|_| {
          MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
            crate::resample::PlanGeometry::new(
              plan.src_w(),
              plan.src_h(),
              plan.out_w(),
              plan.out_h(),
            ),
          ))
        })?
      });
    }
  }

  // Resolve the `NATIVE_LUMA_U8` native-Y source in the preflight (before any
  // feed, so its allocation stays in the atomic preflight): the caller's
  // contiguous plane when supplied (planar YUVA, zero copy), else the packed
  // `Ya8` caller's interleaved Y de-interleaved into `y_u8_scratch`. Held and
  // fed after the colour emit below.
  let native_y_u8: Option<&[u8]> = if NATIVE_LUMA_U8 && need_luma {
    if y_row_u8.is_empty() {
      let scratch = source_luma_scratch(
        y_u8_scratch.expect("packed NATIVE_LUMA_U8 caller supplies a u8 Y scratch"),
        w,
        plan,
      )?;
      deinterleave_y_u8(scratch);
      Some(&scratch[..w])
    } else {
      Some(&y_row_u8[..w])
    }
  } else {
    None
  };

  // Stage + feed + emit the colour outputs. Straight alpha only
  // (`premult = false`); shared with the area path so the
  // convert-then-resolve-then-emit tail is identical (the only difference is
  // the stream kind built above). The native-max clamp inside clips a
  // sub-16-bit signed-kernel overshoot. For the 8-bit planar YUVA path the
  // luma is detached here (passed as `&mut None`) and handled inline below on
  // the `u8` stream; for the packed path the shared emit owns the `u16` luma
  // binning.
  if NATIVE_LUMA_U8 {
    packed_yuva444_feed_emit::<_, _, crate::resample::FilterStream<u16>, SRC_BITS, ZEXT_U16_COLOR>(
      rgba_filter_stream.as_mut(),
      rgba_filter_stream_u16.as_mut(),
      None,
      rgb,
      rgba,
      rgb_u16,
      rgba_u16,
      &mut None,
      &mut None,
      hsv,
      rgba_scratch,
      rgb_drop_scratch,
      rgba_scratch_u16,
      color_scratch_u16,
      luma_scratch_u16,
      w,
      plan,
      idx,
      use_simd,
      false,
      convert_rgba_u8,
      convert_rgba_u16,
      deinterleave_y,
    )?;

    // Native-Y luma on the `u8` stream — the Y row resolved in the preflight
    // (the caller's contiguous plane, or the de-interleaved packed Y) is fed
    // here, so the only fallible step was the stream creation + scratch sizing
    // above, keeping the call atomic. The emit mirrors the merged no-alpha
    // planar YUV path exactly: `luma` copies each finalized `u8` Y row,
    // `luma_u16` zero-extends it. An 8-bit `u8` stream finalizes to the full
    // `u8` range — the native range — so no clamp is needed (unlike the `u16`
    // luma path's native-max clip).
    if let Some(y_src) = native_y_u8 {
      let stream = luma_filter_stream
        .as_mut()
        .expect("created in the preflight when need_luma");
      let ow = plan.out_w();
      stream.feed_row(idx, y_src, use_simd, |oy, out_row| {
        if let Some(buf) = luma.as_deref_mut() {
          buf[oy * ow..(oy + 1) * ow].copy_from_slice(out_row);
        }
        if let Some(buf) = luma_u16.as_deref_mut() {
          for (dst, &src) in buf[oy * ow..(oy + 1) * ow].iter_mut().zip(out_row) {
            *dst = src as u16;
          }
        }
      })?;
    }
    Ok(())
  } else {
    packed_yuva444_feed_emit::<_, _, _, SRC_BITS, ZEXT_U16_COLOR>(
      rgba_filter_stream.as_mut(),
      rgba_filter_stream_u16.as_mut(),
      luma_filter_stream_u16.as_mut(),
      rgb,
      rgba,
      rgb_u16,
      rgba_u16,
      luma,
      luma_u16,
      hsv,
      rgba_scratch,
      rgb_drop_scratch,
      rgba_scratch_u16,
      color_scratch_u16,
      luma_scratch_u16,
      w,
      plan,
      idx,
      use_simd,
      false,
      convert_rgba_u8,
      convert_rgba_u16,
      deinterleave_y,
    )
  }
}

/// Resets the packed-YUVA area streams (`rgba_stream`, `rgba_stream_u16`,
/// `luma_stream_u16`) AND the filter streams (`rgba_filter_stream`,
/// `rgba_filter_stream_u16`, `luma_filter_stream_u16`) and clears the frozen
/// output / alpha-mode snapshots at the start of a new frame for an
/// alpha-aware high-bit planar YUVA sink. Both stream kinds are reset because
/// the plan kind picks which is fed (`Area` bins, `Filter` filters), so a
/// frame switching kind reuses the same fields; the unused kind's streams stay
/// `None` and the resets no-op. The alpha-mode snapshot is re-armed to the
/// sink's current mode so a per-frame `set_alpha_mode` change is accepted (and
/// a mid-frame change is rejected by [`check_frozen_alpha_mode`]).
#[cfg(feature = "yuva")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn reset_high_bit_yuva_streams<F: SourceFormat, R>(sink: &mut MixedSinker<'_, F, R>) {
  if let Some(stream) = sink.rgba_stream.as_mut() {
    stream.reset();
  }
  if let Some(stream) = sink.rgba_stream_u16.as_mut() {
    stream.reset();
  }
  if let Some(stream) = sink.luma_stream_u16.as_mut() {
    stream.reset();
  }
  if let Some(stream) = sink.rgba_filter_stream.as_mut() {
    stream.reset();
  }
  if let Some(stream) = sink.rgba_filter_stream_u16.as_mut() {
    stream.reset();
  }
  if let Some(stream) = sink.luma_filter_stream_u16.as_mut() {
    stream.reset();
  }
  sink.resample_outputs = None;
  // RFC #238 S6c: clear the per-frame frozen 4:2:0 horizontal chroma siting so
  // the next frame may pick either phase while a mid-frame flip stays rejected
  // (mirrors the planar `reset_high_bit_yuv_streams`). Untouched by the 4:2:2 /
  // 4:4:4 YUVA families that share this reset (they never set it).
  sink.frozen_chroma_centered = None;
  // RFC #238 S6f: clear the frozen VERTICAL (`Bottom`) chroma phase too, so the
  // next frame may pick Center or Bottom while a mid-frame Center ⇆ Bottom flip
  // stays rejected; and invalidate the high-bit YUVA bottom-sited vertical
  // lookback via its validity tag (`chroma_prev_row`) so frame N+1's first even
  // row can never box-blend frame N's last chroma row. The `chroma_prev_u16`
  // bytes are re-overwritten before any trusted read, so only the tag is
  // load-bearing (mirrors the planar `reset_high_bit_yuv_streams`). Both stay
  // `None` for the non-`Bottom` / 4:2:2 / 4:4:4 YUVA families that share this
  // reset (they never set them).
  sink.frozen_chroma_bottom_v = None;
  sink.chroma_prev_row = None;
  // RFC #238 Top: clear the frozen VERTICAL (`Top`, `v = 0`) chroma phase so the
  // next frame may pick Top while a mid-frame flip to / from Top stays rejected,
  // and drop any held Top forward-delay odd row so frame N's last deferred colour
  // row never emits into frame N+1 (its held `chroma_top_y_u16` / `chroma_top_a_u16`
  // bytes are left as-is, re-overwritten before any trusted read). Both stay
  // `None` for the non-`Top` / 4:2:2 / 4:4:4 YUVA families that share this reset.
  sink.frozen_chroma_top_v = None;
  sink.chroma_top_pending = None;
  sink.frozen_alpha_mode = Some(sink.alpha_mode);
}

// Test-only allocation failpoint for the wrapper-owned Y / U / V de-pack
// scratch grow in `y2xx_process_native`. Armed, the FIRST (Y) scratch grow of
// an output-bearing row returns the crate's recoverable `AllocationFailed`
// WITHOUT growing — so the atomicity regressions can prove the join's pre-feed
// preflight (out-of-sequence / frozen-output) runs BEFORE this fallible grow.
// Mirrors the high-bit semi-planar `FORCE_P2XX_ALLOC_FAILURE`. Strictly
// test-only — the non-test build compiles this away entirely.
#[cfg(all(test, feature = "std", feature = "y2xx", feature = "yuv-planar"))]
std::thread_local! {
  static FORCE_Y2XX_ALLOC_FAILURE: core::cell::Cell<bool> =
    const { core::cell::Cell::new(false) };
}

/// Arms the wrapper de-pack scratch allocation failpoint for the **next**
/// output-bearing high-bit packed 4:2:2 native row on the current thread. The
/// flag is consumed (take-on-read) by the first fallible scratch grow that row
/// reaches, so it fires exactly once and cannot leak into a later test.
/// Test-only.
#[cfg(all(test, feature = "std", feature = "y2xx", feature = "yuv-planar"))]
pub(crate) fn arm_y2xx_alloc_failure() {
  FORCE_Y2XX_ALLOC_FAILURE.with(|f| f.set(true));
}

/// Grows a wrapper-owned de-pack scratch to `len` `u16` under the planner's
/// recoverable-allocation contract, optionally firing the test-only failpoint
/// (`fail = true` only on the FIRST grow of an output-bearing row). Runs after
/// the join's preflight clears, so a rejected row never reaches it. The
/// high-bit packed 4:2:2 twin of the semi-planar `grow_depack_scratch`.
#[cfg(all(feature = "y2xx", feature = "yuv-planar"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn grow_y2xx_depack_scratch(
  scratch: &mut Vec<u16>,
  len: usize,
  fail: bool,
  w: usize,
  h: usize,
  plan: &crate::resample::ResamplePlan,
) -> Result<(), MixedSinkerError> {
  // `fail` is consumed by the caller; on the non-test build it is `false` and
  // the whole branch compiles away.
  let _ = fail;
  if scratch.len() < len {
    #[cfg(all(test, feature = "std", feature = "y2xx", feature = "yuv-planar"))]
    if fail && FORCE_Y2XX_ALLOC_FAILURE.with(|f| f.take()) {
      return Err(MixedSinkerError::Resample(
        crate::resample::ResampleError::AllocationFailed(crate::resample::PlanGeometry::new(
          w,
          h,
          plan.out_w(),
          plan.out_h(),
        )),
      ));
    }
    scratch
      .try_reserve_exact(len - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(w, h, plan.out_w(), plan.out_h()),
        ))
      })?;
    scratch.resize(len, 0);
  }
  Ok(())
}

/// Native fast-tier decimator for the **high-bit packed 4:2:2** YUV family
/// ([`Y210`](crate::source::Y210) / [`Y212`](crate::source::Y212) /
/// [`Y216`](crate::source::Y216)): bins the native Y / U / V planes straight to
/// the output grid and converts once per output row at output resolution. The
/// PACKED, MSB-aligned analog of the high-bit semi-planar 4:2:2
/// [`p2xx_process_native`](subsampled_4_2_2_high_bit) — same reuse of the
/// high-bit non-4:2:0 PLANAR join verbatim
/// ([`planar_high_bit_native::yuv_planar16_process_native`]) — composed with the
/// packed de-pack of the 8-bit packed 4:2:2
/// [`packed_yuv_8bit`]'s `packed_yuv422_process_native`.
///
/// THE COMPOSITION, per row: wire-decode (`from_le` / `from_be`) AND de-pack the
/// MSB alignment (`>> (16 - BITS)`) AND de-interleave the YUYV-ordered u16 words
/// into wrapper-owned host-native LOGICAL u16 scratch, then delegate with
/// `BE = HOST_NATIVE_BE` so the delegate's own decode is a no-op load on every
/// host. Y2xx packs four u16 words per 2-pixel group: `Y₀` at word 0, `U` at
/// word 1, `Y₁` at word 2, `V` at word 3 (YUYV order). The de-pack hits Y AND U
/// AND V; at `BITS = 16` (`Y216`) the shift is `>> 0` (a harmless no-op — the
/// 10/12 tests guard the live shift).
///
/// 4:2:2 geometry: the chroma plane is `w/2 × h` (horizontal-only subsample,
/// vertical cadence `chroma_vsub = 1`), so a chroma row feeds EVERY colour Y row.
/// The delegate builds its chroma grid against the same output geometry via the
/// `area_chroma_422(w/2, h, out_w, out_h, chroma_h_phase, 0.0)` closure.
///
/// `chroma_h_phase` is the horizontal chroma sampling phase folded into the
/// chroma area weights (`ResamplePlan::area_chroma_422`): `0.25` for the centered
/// 4:2:2 group (`chroma_422_center_sited_h`), `0.0` for co-sited / unspecified. At
/// phase `0.0` the folded plan is byte-identical to the plain `area` plan, so the
/// co-sited output is untouched.
///
/// Native-depth clamp + luma-only lazy chroma both carry through the reused join
/// (the #222 fixes): the join clamps luma `src.min((1 << BITS) - 1) >> (BITS - 8)`
/// for the u8 `luma`, emits the SAME clamped binned Y as the native-depth
/// `luma_u16` (host-native u16, not narrowed), and the colour emit clamps to
/// native max — and the wrapper converts MSB→LSB BEFORE that clamp, so an
/// MSB-packed sample (capped at `MASK` by the de-pack) never wraps; under
/// `need_color == false` only Y is de-packed and the join is handed empty U / V
/// slices, so a luma-only / `luma_u16`-only sink plans no chroma state. Because
/// the join now emits `luma_u16`, attaching it routes through this native tier
/// (no row-stage fallback), keeping the rgb colour semantics native.
///
/// Atomicity (the nv12 / high-bit lesson): the join's COMPLETE pre-feed preflight
/// runs FIRST — `Ok(false)` no-op short-circuit, first-row out-of-sequence,
/// frozen-output — BEFORE any fallible scratch grow, so a rejected row returns
/// its deterministic typed error (`OutOfSequenceRow` / `ResampleOutputsChanged`),
/// never `AllocationFailed`, and touches no caller output. The de-pack into
/// scratch is infallible and happens only after the preflight clears; the
/// delegate re-runs the identical preflight (idempotent) and owns the binning +
/// conversion.
#[cfg(all(feature = "y2xx", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn y2xx_process_native<const BITS: u32, const BE: bool>(
  plan: &crate::resample::ResamplePlan,
  native_planar_u16: &mut Option<std::boxed::Box<NativePlanarYuvU16>>,
  y_scratch: &mut Vec<u16>,
  u_scratch: &mut Vec<u16>,
  v_scratch: &mut Vec<u16>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut Vec<u8>,
  rgb_scratch_u16: &mut Vec<u16>,
  packed: &[u16],
  chroma_h_phase: f64,
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  w: usize,
  h: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  const {
    assert!(
      BITS > 8 && BITS <= 16,
      "BITS must be in (8, 16] for high-bit packed 4:2:2 Y2xx"
    )
  };
  let need_luma = luma.is_some() || luma_u16.is_some();
  let need_color =
    rgb.is_some() || rgba.is_some() || hsv.is_some() || rgb_u16.is_some() || rgba_u16.is_some();
  // 4:2:2 chroma is half-width, full-height: `chroma_w = w / 2`, a chroma row
  // per Y row (`chroma_vsub = 1`).
  let cw = w / 2;

  // Run the planar join's COMPLETE compare-only pre-feed rejection preflight
  // FIRST — no-output short-circuit, first-row out-of-sequence, AND output-set
  // compare (mid-frame output change) — BEFORE any fallible scratch grow below,
  // so every rejection returns its deterministic typed error and leaves the
  // wrapper scratch untouched (the crate's preflight-atomicity contract).
  // Compare-only (no output-set freeze): the de-pack grows below AND the
  // delegate's own colour / source scratch grows all stay pre-commit, and the
  // delegate performs the SINGLE freeze once every pre-feed allocation has
  // succeeded — so a LATER grow (here or in the delegate) failing leaves
  // `resample_outputs` untouched and the row retryable.
  if !native_planar_hb_preflight_check_only(
    native_planar_u16,
    resample_outputs,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    idx,
    need_luma,
    need_color,
  )? {
    return Ok(());
  }

  // Grow the wrapper de-pack scratch under the planner's recoverable contract —
  // Y always, U / V only on a colour row (4:2:2: every Y row is a chroma row
  // when colour is wanted). All grows precede the infallible de-pack and the
  // delegate call. The failpoint fires on the FIRST (Y) grow only.
  grow_y2xx_depack_scratch(y_scratch, w, true, w, h, plan)?;
  if need_color {
    grow_y2xx_depack_scratch(u_scratch, cw, false, w, h, plan)?;
    grow_y2xx_depack_scratch(v_scratch, cw, false, w, h, plan)?;
  }

  // De-pack the interleaved YUYV-ordered MSB-aligned u16 words into host-native
  // LOGICAL scratch. Each 2-pixel group is four words `Y₀, U, Y₁, V`: decode the
  // wire endianness, then shift the active high `BITS` down to the low `BITS`
  // (`>> (16 - BITS)`; `>> 0` at BITS = 16). Y is always de-packed (the join
  // bins Y for both luma and colour); U / V only on a colour row. Everything
  // past here is infallible.
  let depack = |s: u16| -> u16 {
    let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
    logical >> (16 - BITS)
  };
  for (i, group) in packed.as_chunks::<4>().0.iter().enumerate() {
    y_scratch[i * 2] = depack(group[0]);
    y_scratch[i * 2 + 1] = depack(group[2]);
  }
  if need_color {
    for (i, group) in packed.as_chunks::<4>().0.iter().enumerate() {
      u_scratch[i] = depack(group[1]);
      v_scratch[i] = depack(group[3]);
    }
  }

  // Delegate to the planar high-bit non-4:2:0 join with `BE = HOST_NATIVE_BE`
  // so its internal decode is a no-op on the already-native scratch, at the
  // 4:2:2 chroma geometry (`chroma_vsub = 1`, `chroma_w = w / 2`). Empty U / V
  // on luma-only rows (the join reads chroma only under colour). Unlike the
  // high-bit semi-planar wrapper, Y2xx threads its real `luma_u16` buffer: the
  // join now emits the native-depth `luma_u16` (the clamped binned Y), so
  // attaching `luma_u16` no longer falls the whole pipeline back to row-stage —
  // the route stays native and rgb keeps native colour semantics.
  let (u_plane, v_plane): (&[u16], &[u16]) = if need_color {
    (&u_scratch[..cw], &v_scratch[..cw])
  } else {
    (&[], &[])
  };
  planar_high_bit_native::yuv_planar16_process_native::<BITS, HIGH_BIT_HOST_NATIVE_BE>(
    plan,
    native_planar_u16,
    resample_outputs,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    rgb_scratch,
    rgb_scratch_u16,
    &y_scratch[..w],
    u_plane,
    v_plane,
    matrix,
    full_range,
    idx,
    w,
    h,
    1,
    cw,
    || {
      crate::resample::ResamplePlan::area_chroma_422(
        cw,
        h,
        plan.out_w(),
        plan.out_h(),
        chroma_h_phase,
        0.0,
      )
    },
    use_simd,
  )
}

/// Fallible pre-feed reserve for the packed Y2xx centered-siting de-interleave
/// scratch: grows the full-width Y buffer (`width`) and the half-width U / V
/// buffers (`width / 2` each) so the later infallible de-interleave + 4:4:4
/// decode reuse already-sized buffers. The `u16` sibling of the 8-bit packed
/// [`packed_yuv_8bit`]'s `reserve_packed_center_chroma`. Split from the
/// de-interleave so it runs before any output row is written — an allocator
/// refusal must leave the output frame untouched, never partially mutated — and
/// reuses the native tier's de-pack scratch (`width` Y + `width / 2` U / V), so a
/// centered colour row and an area native row never both run in one `process`
/// call. A grow refusal is the typed, recoverable
/// [`ResampleError::AllocationFailed`]; `height` feeds the payload.
///
/// [`ResampleError::AllocationFailed`]: crate::resample::ResampleError::AllocationFailed
#[cfg(all(feature = "y2xx", feature = "yuv-planar"))]
fn reserve_y2xx_center_planes(
  y_full: &mut Vec<u16>,
  u_half: &mut Vec<u16>,
  v_half: &mut Vec<u16>,
  width: usize,
  height: usize,
) -> Result<(), MixedSinkerError> {
  let grow = |scratch: &mut Vec<u16>, len: usize| -> Result<(), MixedSinkerError> {
    if scratch.len() < len {
      scratch
        .try_reserve_exact(len - scratch.len())
        .map_err(|_| {
          MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
            crate::resample::PlanGeometry::new(width, height, width, height),
          ))
        })?;
      scratch.resize(len, 0);
    }
    Ok(())
  };
  grow(y_full, width)?;
  let cw = width / 2;
  grow(u_half, cw)?;
  grow(v_half, cw)?;
  Ok(())
}

/// Reconstructs full-width chroma for a packed Y2xx centered-siting colour row.
/// De-interleaves the packed `[Y0 U Y1 V]` words into a full-width Y plane and
/// half-width U / V planes, shifting each MSB-aligned sample down to the low
/// `BITS` (`>> (16 - BITS)`) while keeping the source's wire byte order — so the
/// planes match the LSB-aligned low-packed layout the [`reconstruct_chroma`]
/// upsample and the high-bit 4:4:4 decode expect, both of which re-decode the
/// endianness (`be`) themselves. Then phase-0.5 upsamples U / V into `chroma_full`,
/// returning the `(u_full, v_full)` slices the 4:4:4 decode reads (the de-packed
/// `y_full` is left in the caller's scratch, read as `&y_full[..w]`). Every
/// fallible grow (chroma scratch + de-pack planes) precedes the infallible
/// de-interleave, so an allocator refusal leaves the output frame untouched (the
/// crate's preflight-ordering atomicity contract). Reuses the native tier's
/// de-pack scratch, so a centered colour row and an area native row never both run
/// in one `process` call.
#[cfg(all(feature = "y2xx", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn y2xx_center_reconstruct<'c, const BITS: u32>(
  packed: &[u16],
  y_full: &mut Vec<u16>,
  u_half: &mut Vec<u16>,
  v_half: &mut Vec<u16>,
  chroma_full: &'c mut Vec<u16>,
  w: usize,
  h: usize,
  be: bool,
  use_simd: bool,
) -> Result<(&'c [u16], &'c [u16]), MixedSinkerError> {
  let cw = w / 2;
  subsampled_4_2_0_high_bit::reserve_420_chroma_full_u16(chroma_full, w, h)?;
  reserve_y2xx_center_planes(y_full, u_half, v_half, w, h)?;
  // Shift the active high `BITS` down to the low `BITS`, then re-encode in the
  // source wire byte order so the reconstruct + 4:4:4 decode (which apply `be`)
  // see LSB-aligned low-packed samples. At `BITS = 16` this is a no-op round-trip.
  let repack = |wire: u16| -> u16 {
    let logical = (if be {
      u16::from_be(wire)
    } else {
      u16::from_le(wire)
    }) >> (16 - BITS);
    if be { logical.to_be() } else { logical.to_le() }
  };
  for (i, group) in packed.as_chunks::<4>().0.iter().take(cw).enumerate() {
    y_full[2 * i] = repack(group[0]);
    y_full[2 * i + 1] = repack(group[2]);
    u_half[i] = repack(group[1]);
    v_half[i] = repack(group[3]);
  }
  Ok(reconstruct_chroma(
    ChromaU16::<BITS> { big_endian: be },
    chroma_full,
    &u_half[..cw],
    &v_half[..cw],
    w,
    use_simd,
  ))
}

// Test-only allocation failpoint for the wrapper-owned Y / U / V de-pack scratch
// grow in `v210_process_native`. Armed, the FIRST (Y) scratch grow of an
// output-bearing row returns the crate's recoverable `AllocationFailed` WITHOUT
// growing — so the atomicity regressions can prove the join's pre-feed preflight
// (out-of-sequence / frozen-output) runs BEFORE this fallible grow. Mirrors
// `FORCE_Y2XX_ALLOC_FAILURE`. Strictly test-only — the non-test build compiles
// this away entirely.
#[cfg(all(test, feature = "std", feature = "v210", feature = "yuv-planar"))]
std::thread_local! {
  static FORCE_V210_ALLOC_FAILURE: core::cell::Cell<bool> =
    const { core::cell::Cell::new(false) };
}

/// Arms the wrapper de-pack scratch allocation failpoint for the **next**
/// output-bearing V210 native row on the current thread. The flag is consumed
/// (take-on-read) by the first fallible scratch grow that row reaches, so it
/// fires exactly once and cannot leak into a later test. Test-only.
#[cfg(all(test, feature = "std", feature = "v210", feature = "yuv-planar"))]
pub(crate) fn arm_v210_alloc_failure() {
  FORCE_V210_ALLOC_FAILURE.with(|f| f.set(true));
}

/// Grows a wrapper-owned de-pack scratch to `len` `u16` under the planner's
/// recoverable-allocation contract, optionally firing the test-only failpoint
/// (`fail = true` only on the FIRST grow of an output-bearing row). Runs after
/// the join's preflight clears, so a rejected row never reaches it. The V210 twin
/// of [`grow_y2xx_depack_scratch`].
#[cfg(all(feature = "v210", feature = "yuv-planar"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn grow_v210_depack_scratch(
  scratch: &mut Vec<u16>,
  len: usize,
  fail: bool,
  w: usize,
  h: usize,
  plan: &crate::resample::ResamplePlan,
) -> Result<(), MixedSinkerError> {
  // `fail` is consumed by the caller; on the non-test build it is `false` and
  // the whole branch compiles away.
  let _ = fail;
  if scratch.len() < len {
    #[cfg(all(test, feature = "std", feature = "v210", feature = "yuv-planar"))]
    if fail && FORCE_V210_ALLOC_FAILURE.with(|f| f.take()) {
      return Err(MixedSinkerError::Resample(
        crate::resample::ResampleError::AllocationFailed(crate::resample::PlanGeometry::new(
          w,
          h,
          plan.out_w(),
          plan.out_h(),
        )),
      ));
    }
    scratch
      .try_reserve_exact(len - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(w, h, plan.out_w(), plan.out_h()),
        ))
      })?;
    scratch.resize(len, 0);
  }
  Ok(())
}

/// Native fast-tier decimator for the **exotic 10-bit packed 4:2:2** YUV format
/// ([`V210`](crate::source::V210)): bins the native Y / U / V planes straight to
/// the output grid and converts once per output row at output resolution. The
/// fixed-`BITS = 10` sibling of the high-bit packed 4:2:2
/// [`y2xx_process_native`] — same reuse of the high-bit non-4:2:0 PLANAR join
/// verbatim ([`planar_high_bit_native::yuv_planar16_process_native`]) at the
/// 4:2:2 chroma geometry (`chroma_w = w / 2`, `chroma_vsub = 1`) — composed with
/// the V210 word de-pack in place of the Y2xx YUYV de-pack.
///
/// THE COMPOSITION, per row: wire-decode (`from_le` / `from_be`) AND bit-extract
/// the V210 word packing into wrapper-owned host-native LOGICAL u16 scratch, then
/// delegate with `BE = HOST_NATIVE_BE` so the delegate's own decode is a no-op
/// load on every host. V210 packs 12 × 10-bit samples per 16-byte word = four
/// 32-bit LE/BE words = 6 pixels: word 0 `[Cb0, Y0, Cr0]`, word 1 `[Y1, Cb1, Y2]`,
/// word 2 `[Cr1, Y3, Cb2]`, word 3 `[Y4, Cr2, Y5]` — each sample in the low 10
/// bits of its lane (`bits[9:0]`, `[19:10]`, `[29:20]`). The de-pack MIRRORS the
/// row-stage `unpack_v210_word` bit positions EXACTLY, so the binned planes are
/// the same logical samples the row-stage tier converts. Y is always de-packed
/// (the join bins Y for both luma and colour); U / V only on a colour row. The
/// 10-bit fields are `<= 1023 = (1 << 10) - 1` by construction, so the join's
/// native-depth clamp is a value no-op on encodable input.
///
/// Even-width + partial-last-word: the configured width must be even (4:2:2
/// chroma pair; the caller rejects odd widths as
/// [`WidthAlignment`](MixedSinkerError::WidthAlignment) before this runs). A width
/// not divisible by 6 ends on a partial word carrying only 2 or 4 valid pixels
/// (1 or 2 valid chroma pairs); the de-pack reads every word but writes only the
/// valid prefix (`y_scratch[..w]`, `u/v_scratch[..w/2]`), matching the row-stage
/// `tail_pixels` handling — the unused high samples in the partial word are never
/// binned.
///
/// Native-depth clamp + `luma_u16` emit + luma-only lazy chroma all carry through
/// the reused join: the join clamps `src.min(1023) >> 2` for the u8 `luma`, emits
/// the SAME clamped binned Y as the native-depth `luma_u16` (host-native u16, not
/// narrowed), and the colour emit clamps to 1023; under `need_color == false`
/// only Y is de-packed and the join is handed empty U / V slices, so a luma-only /
/// `luma_u16`-only sink plans no chroma state. Because the join now emits
/// `luma_u16`, attaching it routes through this native tier (no row-stage
/// fallback), keeping the rgb colour semantics native.
///
/// `chroma_h_phase` is the horizontal chroma sampling phase folded into the
/// chroma area weights (`ResamplePlan::area_chroma_422`): `0.25` for the centered
/// 4:2:2 group (`chroma_422_center_sited_h`), `0.0` for co-sited / unspecified. At
/// phase `0.0` the folded plan is byte-identical to the plain `area` plan, so the
/// co-sited output is untouched.
///
/// Atomicity mirrors [`y2xx_process_native`]: the join's COMPLETE pre-feed
/// preflight runs FIRST — `Ok(false)` no-op short-circuit, first-row
/// out-of-sequence, frozen-output — BEFORE any fallible scratch grow, so a
/// rejected row returns its deterministic typed error
/// (`OutOfSequenceRow` / `ResampleOutputsChanged`), never `AllocationFailed`, and
/// touches no caller output. The de-pack into scratch is infallible and happens
/// only after the preflight clears; the delegate re-runs the identical preflight
/// (idempotent) and owns the binning + conversion.
#[cfg(all(feature = "v210", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn v210_process_native<const BE: bool>(
  plan: &crate::resample::ResamplePlan,
  native_planar_u16: &mut Option<std::boxed::Box<NativePlanarYuvU16>>,
  y_scratch: &mut Vec<u16>,
  u_scratch: &mut Vec<u16>,
  v_scratch: &mut Vec<u16>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut Vec<u8>,
  rgb_scratch_u16: &mut Vec<u16>,
  packed: &[u8],
  chroma_h_phase: f64,
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  w: usize,
  h: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  // V210 is fixed at 10-bit; the reused join is const-generic over BITS.
  const BITS: u32 = 10;
  let need_luma = luma.is_some() || luma_u16.is_some();
  let need_color =
    rgb.is_some() || rgba.is_some() || hsv.is_some() || rgb_u16.is_some() || rgba_u16.is_some();
  // 4:2:2 chroma is half-width, full-height: `chroma_w = w / 2`, a chroma row per
  // Y row (`chroma_vsub = 1`).
  let cw = w / 2;

  // Run the planar join's COMPLETE compare-only pre-feed rejection preflight
  // FIRST — no-output short-circuit, first-row out-of-sequence, AND output-set
  // compare (mid-frame output change) — BEFORE any fallible scratch grow below,
  // so every rejection returns its deterministic typed error and leaves the
  // wrapper scratch untouched (the crate's preflight-atomicity contract).
  // Compare-only (no output-set freeze): the de-pack grows below AND the
  // delegate's own colour / source scratch grows all stay pre-commit, and the
  // delegate performs the SINGLE freeze once every pre-feed allocation has
  // succeeded — so a LATER grow (here or in the delegate) failing leaves
  // `resample_outputs` untouched and the row retryable.
  if !native_planar_hb_preflight_check_only(
    native_planar_u16,
    resample_outputs,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    idx,
    need_luma,
    need_color,
  )? {
    return Ok(());
  }

  // Grow the wrapper de-pack scratch under the planner's recoverable contract —
  // Y always, U / V only on a colour row (4:2:2: every Y row is a chroma row when
  // colour is wanted). All grows precede the infallible de-pack and the delegate
  // call. The failpoint fires on the FIRST (Y) grow only.
  grow_v210_depack_scratch(y_scratch, w, true, w, h, plan)?;
  if need_color {
    grow_v210_depack_scratch(u_scratch, cw, false, w, h, plan)?;
    grow_v210_depack_scratch(v_scratch, cw, false, w, h, plan)?;
  }

  // De-pack the V210 word packing into host-native LOGICAL u16 scratch. Each
  // 16-byte word is four 32-bit LE/BE lanes carrying three 10-bit samples each:
  //   word 0 `[Cb0, Y0, Cr0]`  word 1 `[Y1, Cb1, Y2]`
  //   word 2 `[Cr1, Y3, Cb2]`  word 3 `[Y4, Cr2, Y5]`
  // (sample in `bits[9:0]` / `[19:10]` / `[29:20]` of its lane) — MIRRORING the
  // row-stage `unpack_v210_word` exactly. Decode the wire endianness per lane,
  // then mask `& 0x3FF`. Each full word emits 6 Y + 3 U + 3 V; a final partial
  // word (width not a multiple of 6) writes only its valid prefix — `take_words`
  // caps the per-word output index at `w` (Y) / `cw` (U / V), so the unused high
  // samples are never binned. Y is always de-packed; U / V only on a colour row.
  // Everything past here is infallible.
  let load = |word: &[u8], lane: usize| -> u32 {
    let bytes = [
      word[lane * 4],
      word[lane * 4 + 1],
      word[lane * 4 + 2],
      word[lane * 4 + 3],
    ];
    if BE {
      u32::from_be_bytes(bytes)
    } else {
      u32::from_le_bytes(bytes)
    }
  };
  for (wi, word) in packed.as_chunks::<16>().0.iter().enumerate() {
    let px = wi * 6;
    let w0 = load(word, 0);
    let w1 = load(word, 1);
    let w2 = load(word, 2);
    let w3 = load(word, 3);
    // The six Y samples across the four lanes (`unpack_v210_word` order).
    let ys = [
      ((w0 >> 10) & 0x3FF) as u16, // Y0
      (w1 & 0x3FF) as u16,         // Y1
      ((w1 >> 20) & 0x3FF) as u16, // Y2
      ((w2 >> 10) & 0x3FF) as u16, // Y3
      (w3 & 0x3FF) as u16,         // Y4
      ((w3 >> 20) & 0x3FF) as u16, // Y5
    ];
    for (k, &y) in ys.iter().enumerate() {
      if px + k < w {
        y_scratch[px + k] = y;
      }
    }
    if need_color {
      let cu = px / 2;
      // Cb0 / Cb1 / Cb2 and Cr0 / Cr1 / Cr2 — one chroma pair per 2-pixel pair.
      let us = [
        (w0 & 0x3FF) as u16,         // Cb0
        ((w1 >> 10) & 0x3FF) as u16, // Cb1
        ((w2 >> 20) & 0x3FF) as u16, // Cb2
      ];
      let vs = [
        ((w0 >> 20) & 0x3FF) as u16, // Cr0
        (w2 & 0x3FF) as u16,         // Cr1
        ((w3 >> 10) & 0x3FF) as u16, // Cr2
      ];
      for k in 0..3 {
        if cu + k < cw {
          u_scratch[cu + k] = us[k];
          v_scratch[cu + k] = vs[k];
        }
      }
    }
  }

  // Delegate to the planar high-bit non-4:2:0 join with `BE = HOST_NATIVE_BE` so
  // its internal decode is a no-op on the already-native scratch, at the 4:2:2
  // chroma geometry (`chroma_vsub = 1`, `chroma_w = w / 2`). Empty U / V on
  // luma-only rows (the join reads chroma only under colour). V210 threads its
  // real `luma_u16` buffer: the join emits the native-depth `luma_u16` (the
  // clamped binned Y), so attaching `luma_u16` keeps the native route and rgb
  // keeps native colour semantics.
  let (u_plane, v_plane): (&[u16], &[u16]) = if need_color {
    (&u_scratch[..cw], &v_scratch[..cw])
  } else {
    (&[], &[])
  };
  planar_high_bit_native::yuv_planar16_process_native::<BITS, HIGH_BIT_HOST_NATIVE_BE>(
    plan,
    native_planar_u16,
    resample_outputs,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    rgb_scratch,
    rgb_scratch_u16,
    &y_scratch[..w],
    u_plane,
    v_plane,
    matrix,
    full_range,
    idx,
    w,
    h,
    1,
    cw,
    || {
      crate::resample::ResamplePlan::area_chroma_422(
        cw,
        h,
        plan.out_w(),
        plan.out_h(),
        chroma_h_phase,
        0.0,
      )
    },
    use_simd,
  )
}

/// Fallible pre-feed reserve for the packed V210 centered-siting de-pack scratch:
/// grows the full-width Y buffer (`width`) and the half-width U / V buffers
/// (`width / 2` each) so the later infallible de-pack + 4:4:4 decode reuse
/// already-sized buffers. The V210 twin of [`reserve_y2xx_center_planes`]. Split
/// from the de-pack so it runs before any output row is written — an allocator
/// refusal must leave the output frame untouched, never partially mutated — and
/// reuses the native tier's de-pack scratch (`width` Y + `width / 2` U / V), so a
/// centered colour row and an area native row never both run in one `process`
/// call. A grow refusal is the typed, recoverable
/// [`ResampleError::AllocationFailed`]; `height` feeds the payload.
///
/// [`ResampleError::AllocationFailed`]: crate::resample::ResampleError::AllocationFailed
#[cfg(all(feature = "v210", feature = "yuv-planar"))]
fn reserve_v210_center_planes(
  y_full: &mut Vec<u16>,
  u_half: &mut Vec<u16>,
  v_half: &mut Vec<u16>,
  width: usize,
  height: usize,
) -> Result<(), MixedSinkerError> {
  let grow = |scratch: &mut Vec<u16>, len: usize| -> Result<(), MixedSinkerError> {
    if scratch.len() < len {
      scratch
        .try_reserve_exact(len - scratch.len())
        .map_err(|_| {
          MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
            crate::resample::PlanGeometry::new(width, height, width, height),
          ))
        })?;
      scratch.resize(len, 0);
    }
    Ok(())
  };
  grow(y_full, width)?;
  let cw = width / 2;
  grow(u_half, cw)?;
  grow(v_half, cw)?;
  Ok(())
}

/// Reconstructs full-width chroma for a packed V210 centered-siting colour row.
/// De-packs the V210 word packing (four 32-bit lanes per 16-byte word, each
/// carrying three low-10-bit samples in the `unpack_v210_word` order) into a
/// full-width Y plane and half-width U / V planes, re-encoding each already
/// logical 10-bit sample into the source's wire byte order (`be`) — so the planes
/// match the LSB-aligned low-packed layout the [`reconstruct_chroma`] upsample and
/// the 10-bit 4:4:4 decode expect, both of which re-decode the endianness (`be`)
/// themselves. Then phase-0.5 upsamples U / V into `chroma_full`, returning the
/// `(u_full, v_full)` slices the 4:4:4 decode reads (the de-packed `y_full` is
/// left in the caller's scratch, read as `&y_full[..w]`). Every fallible grow
/// (chroma scratch + de-pack planes) precedes the infallible de-pack, so an
/// allocator refusal leaves the output frame untouched (the crate's
/// preflight-ordering atomicity contract). Reuses the native tier's de-pack
/// scratch, so a centered colour row and an area native row never both run in one
/// `process` call. The V210 twin of [`y2xx_center_reconstruct`] — the same
/// reconstruct-and-decode, differing only in the word de-pack (the native tier's
/// bit-extract in place of the YUYV MSB-align).
#[cfg(all(feature = "v210", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn v210_center_reconstruct<'c>(
  packed: &[u8],
  y_full: &mut Vec<u16>,
  u_half: &mut Vec<u16>,
  v_half: &mut Vec<u16>,
  chroma_full: &'c mut Vec<u16>,
  w: usize,
  h: usize,
  be: bool,
  use_simd: bool,
) -> Result<(&'c [u16], &'c [u16]), MixedSinkerError> {
  const BITS: u32 = 10;
  let cw = w / 2;
  subsampled_4_2_0_high_bit::reserve_420_chroma_full_u16(chroma_full, w, h)?;
  reserve_v210_center_planes(y_full, u_half, v_half, w, h)?;
  // Each bit-extracted 10-bit sample is already logical; re-encode it in the
  // source wire byte order so the reconstruct + 4:4:4 decode (which apply `be`)
  // see the LSB-aligned low-packed samples.
  let encode = |logical: u16| -> u16 { if be { logical.to_be() } else { logical.to_le() } };
  let load = |word: &[u8], lane: usize| -> u32 {
    let bytes = [
      word[lane * 4],
      word[lane * 4 + 1],
      word[lane * 4 + 2],
      word[lane * 4 + 3],
    ];
    if be {
      u32::from_be_bytes(bytes)
    } else {
      u32::from_le_bytes(bytes)
    }
  };
  for (wi, word) in packed.as_chunks::<16>().0.iter().enumerate() {
    let px = wi * 6;
    let w0 = load(word, 0);
    let w1 = load(word, 1);
    let w2 = load(word, 2);
    let w3 = load(word, 3);
    // The six Y samples across the four lanes (`unpack_v210_word` order).
    let ys = [
      ((w0 >> 10) & 0x3FF) as u16, // Y0
      (w1 & 0x3FF) as u16,         // Y1
      ((w1 >> 20) & 0x3FF) as u16, // Y2
      ((w2 >> 10) & 0x3FF) as u16, // Y3
      (w3 & 0x3FF) as u16,         // Y4
      ((w3 >> 20) & 0x3FF) as u16, // Y5
    ];
    for (k, &y) in ys.iter().enumerate() {
      if px + k < w {
        y_full[px + k] = encode(y);
      }
    }
    let cu = px / 2;
    // Cb0 / Cb1 / Cb2 and Cr0 / Cr1 / Cr2 — one chroma pair per 2-pixel pair.
    let us = [
      (w0 & 0x3FF) as u16,         // Cb0
      ((w1 >> 10) & 0x3FF) as u16, // Cb1
      ((w2 >> 20) & 0x3FF) as u16, // Cb2
    ];
    let vs = [
      ((w0 >> 20) & 0x3FF) as u16, // Cr0
      (w2 & 0x3FF) as u16,         // Cr1
      ((w3 >> 10) & 0x3FF) as u16, // Cr2
    ];
    for k in 0..3 {
      if cu + k < cw {
        u_half[cu + k] = encode(us[k]);
        v_half[cu + k] = encode(vs[k]);
      }
    }
  }
  Ok(reconstruct_chroma(
    ChromaU16::<BITS> { big_endian: be },
    chroma_full,
    &u_half[..cw],
    &v_half[..cw],
    w,
    use_simd,
  ))
}

/// The de-pack writes host-native LOGICAL u16 into the wrapper scratch BEFORE
/// the planar delegate, so the delegate's own `from_le` / `from_be` decode must
/// be a no-op load on every host: pass `BE = HOST_NATIVE_BE` (= `from_ne`).
/// Forwarding the source wire `BE` here would byte-swap the already-native
/// scratch on a big-endian target. Mirrors the high-bit semi-planar `p2xx`.
/// Shared by the high-bit packed 4:2:2 (`y2xx`) and 4:4:4 (`yuv-444-packed`
/// V410 / Xv36) native wrappers — both de-pack to host-native LOGICAL u16
/// before delegating. The exotic 10-bit packed 4:2:2 `v210` wrapper shares it
/// (it likewise bit-extracts to host-native LOGICAL u16 before delegating).
#[cfg(all(
  any(feature = "y2xx", feature = "yuv-444-packed", feature = "v210"),
  feature = "yuv-planar"
))]
const HIGH_BIT_HOST_NATIVE_BE: bool = cfg!(target_endian = "big");

// Test-only allocation failpoint for the wrapper-owned Y / U / V de-pack
// scratch grow in the packed 4:4:4 native wrappers (8-bit `packed_vuyx_process_native`
// and high-bit `packed_yuv444_hb_process_native`). Armed, the FIRST (Y) scratch
// grow of an output-bearing row returns the crate's recoverable `AllocationFailed`
// WITHOUT growing — so the atomicity regressions can prove the join's pre-feed
// preflight (out-of-sequence / frozen-output) runs BEFORE this fallible grow.
// Mirrors `FORCE_Y2XX_ALLOC_FAILURE`. A given sink runs exactly one element-type
// grow (u8 for Vuyx, u16 for V410 / Xv36), so the single flag is shared by both
// `grow_packed_444_*_scratch` helpers. Strictly test-only — the non-test build
// compiles this away entirely.
#[cfg(all(
  test,
  feature = "std",
  feature = "yuv-444-packed",
  feature = "yuv-planar"
))]
std::thread_local! {
  static FORCE_PACKED_444_ALLOC_FAILURE: core::cell::Cell<bool> =
    const { core::cell::Cell::new(false) };
}

/// Arms the wrapper de-pack scratch allocation failpoint for the **next**
/// output-bearing packed 4:4:4 native row on the current thread. The flag is
/// consumed (take-on-read) by the first fallible scratch grow that row reaches,
/// so it fires exactly once and cannot leak into a later test. Test-only.
#[cfg(all(
  test,
  feature = "std",
  feature = "yuv-444-packed",
  feature = "yuv-planar"
))]
pub(crate) fn arm_packed_444_alloc_failure() {
  FORCE_PACKED_444_ALLOC_FAILURE.with(|f| f.set(true));
}

/// Grows a wrapper-owned **u8** de-pack scratch (the 8-bit Vuyx tier) to `len`
/// under the planner's recoverable-allocation contract, optionally firing the
/// test-only failpoint (`fail = true` only on the FIRST grow of an output-bearing
/// row). Runs after the join's preflight clears, so a rejected row never reaches
/// it. The 8-bit packed 4:4:4 twin of `grow_y2xx_depack_scratch`.
#[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn grow_packed_444_u8_scratch(
  scratch: &mut Vec<u8>,
  len: usize,
  fail: bool,
  w: usize,
  h: usize,
  plan: &crate::resample::ResamplePlan,
) -> Result<(), MixedSinkerError> {
  let _ = fail;
  if scratch.len() < len {
    #[cfg(all(
      test,
      feature = "std",
      feature = "yuv-444-packed",
      feature = "yuv-planar"
    ))]
    if fail && FORCE_PACKED_444_ALLOC_FAILURE.with(|f| f.take()) {
      return Err(MixedSinkerError::Resample(
        crate::resample::ResampleError::AllocationFailed(crate::resample::PlanGeometry::new(
          w,
          h,
          plan.out_w(),
          plan.out_h(),
        )),
      ));
    }
    scratch
      .try_reserve_exact(len - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(w, h, plan.out_w(), plan.out_h()),
        ))
      })?;
    scratch.resize(len, 0);
  }
  Ok(())
}

/// Grows a wrapper-owned **u16** de-pack scratch (the high-bit V410 / Xv36 tier)
/// to `len` under the planner's recoverable-allocation contract, with the same
/// failpoint + ordering contract as [`grow_packed_444_u8_scratch`]. The high-bit
/// packed 4:4:4 twin of `grow_y2xx_depack_scratch`.
#[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn grow_packed_444_u16_scratch(
  scratch: &mut Vec<u16>,
  len: usize,
  fail: bool,
  w: usize,
  h: usize,
  plan: &crate::resample::ResamplePlan,
) -> Result<(), MixedSinkerError> {
  let _ = fail;
  if scratch.len() < len {
    #[cfg(all(
      test,
      feature = "std",
      feature = "yuv-444-packed",
      feature = "yuv-planar"
    ))]
    if fail && FORCE_PACKED_444_ALLOC_FAILURE.with(|f| f.take()) {
      return Err(MixedSinkerError::Resample(
        crate::resample::ResampleError::AllocationFailed(crate::resample::PlanGeometry::new(
          w,
          h,
          plan.out_w(),
          plan.out_h(),
        )),
      ));
    }
    scratch
      .try_reserve_exact(len - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(w, h, plan.out_w(), plan.out_h()),
        ))
      })?;
    scratch.resize(len, 0);
  }
  Ok(())
}

/// Native fast-tier decimator for the **8-bit packed 4:4:4** non-alpha YUV
/// format ([`Vuyx`](crate::source::Vuyx) — bytes `V U Y X` per pixel, `X`
/// padding): de-PACKS the fully-interleaved source row into the sink's separate
/// Y / U / V scratch planes, then reuses the planar twin's non-4:2:0 join
/// verbatim ([`planar_8bit::yuv_planar_process_native`]) at
/// [`Yuv444p`](crate::source::Yuv444p) geometry. The 4:4:4 (full-width chroma,
/// `chroma_vsub = 1`) analog of the packed 4:2:2
/// [`packed_yuv_8bit`]'s `packed_yuv422_process_native`, so every output is
/// byte-identical to a [`Yuv444p`](crate::source::Yuv444p) native conversion of
/// those de-packed planes, and within ±1 LSB of the packed row-stage tier. Luma
/// is bit-identical (both bin the same native Y).
///
/// Vuyx packs four bytes per pixel `V U Y X`: V at byte 0, U at byte 1, Y at
/// byte 2, X (padding) at byte 3 — ignored. There is NO chroma subsampling
/// (4:4:4), so the de-pack writes FULL-width Y / U / V (`chroma_w = w`) and the
/// chroma plan is a plain `area(w, h, ..)`. Y is always de-packed (the join bins
/// Y for both luma and colour); U / V only on a colour row — on luma-only /
/// no-colour rows the join never reads chroma, the scratch is left as-is and the
/// join gets empty U / V slices (the lazy-chroma contract).
///
/// 8-bit source, so no native-depth clamp is needed (the source's native range
/// is the full `u8` range and the join's averaging keeps every sample in range).
///
/// Atomicity mirrors the packed 4:2:2 wrapper: the join's COMPLETE pre-feed
/// rejection preflight runs FIRST (via
/// [`planar_8bit::native_planar_preflight_check_only`]), before the fallible
/// Y / U / V scratch grow, so a rejected row returns its deterministic typed
/// error (`OutOfSequenceRow` / `ResampleOutputsChanged`), never
/// `AllocationFailed`, and grows no sink state. It is compare-only (no output-set
/// freeze), so the scratch grow stays a pre-feed step ahead of the delegate's
/// commit; the de-pack writes only the private scratch, so no caller output is
/// touched until the delegate clears.
#[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn packed_vuyx_process_native(
  plan: &crate::resample::ResamplePlan,
  native_planar: &mut Option<std::boxed::Box<planar_8bit::NativePlanarYuv>>,
  y_scratch: &mut Vec<u8>,
  u_scratch: &mut Vec<u8>,
  v_scratch: &mut Vec<u8>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut Vec<u8>,
  packed: &[u8],
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  w: usize,
  h: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  let need_luma = luma.is_some() || luma_u16.is_some();
  let need_color = rgb.is_some() || rgba.is_some() || hsv.is_some();

  // Run the join's COMPLETE pre-feed rejection preflight FIRST — before the
  // fallible Y / U / V de-pack scratch grow — so EVERY rejection case returns
  // its deterministic typed error, never AllocationFailed under allocation
  // pressure, and leaves the scratch untouched. Compare-only (no output-set
  // freeze), so the scratch grow stays a pre-feed step ahead of the delegate's
  // commit. `Ok(false)` is the no-output no-op. The delegate re-runs this
  // identical compare harmlessly and owns the commit.
  if !planar_8bit::native_planar_preflight_check_only(
    native_planar,
    resample_outputs,
    rgb,
    rgba,
    luma,
    luma_u16,
    hsv,
    idx,
    need_luma,
    need_color,
  )? {
    return Ok(());
  }

  // De-pack the interleaved `V U Y X` row into the private Y / U / V scratch.
  // 4:4:4: chroma is full-width (`w`). Y is always de-packed; U / V only on a
  // colour row. The failpoint fires on the FIRST (Y) grow only.
  grow_packed_444_u8_scratch(y_scratch, w, true, w, h, plan)?;
  for (i, group) in packed.as_chunks::<4>().0.iter().enumerate() {
    y_scratch[i] = group[2];
  }
  if need_color {
    grow_packed_444_u8_scratch(u_scratch, w, false, w, h, plan)?;
    grow_packed_444_u8_scratch(v_scratch, w, false, w, h, plan)?;
    for (i, group) in packed.as_chunks::<4>().0.iter().enumerate() {
      u_scratch[i] = group[1];
      v_scratch[i] = group[0];
    }
  }

  let (u_plane, v_plane): (&[u8], &[u8]) = if need_color {
    (&u_scratch[..w], &v_scratch[..w])
  } else {
    (&[], &[])
  };
  planar_8bit::yuv_planar_process_native(
    plan,
    native_planar,
    resample_outputs,
    rgb,
    rgba,
    luma,
    luma_u16,
    hsv,
    rgb_scratch,
    &y_scratch[..w],
    u_plane,
    v_plane,
    matrix,
    full_range,
    idx,
    w,
    h,
    1,
    || crate::resample::ResamplePlan::area(w, h, plan.out_w(), plan.out_h()),
    use_simd,
  )
}

/// Native fast-tier decimator for the **8-bit packed 4:4:4** non-alpha YUV
/// format [`Vyu444`](crate::source::Vyu444) — bytes `V Y U` per pixel, **3
/// bytes per pixel (24bpp), no alpha**. Identical to
/// [`packed_vuyx_process_native`] except for the source layout: VYU444 packs
/// three bytes per pixel `V Y U` (V at byte 0, Y at byte 1, U at byte 2 —
/// there is no fourth byte), so the de-pack steps the interleaved row in
/// 3-byte groups (`chunks_exact(3)`) rather than 4. Every other step — the
/// pre-feed preflight ordering, the lazy-chroma contract, the reuse of the
/// [`Yuv444p`](crate::source::Yuv444p) planar join at full-width chroma — is
/// shared verbatim, so each output is byte-identical to a `Yuv444p` native
/// conversion of the de-packed planes and within ±1 LSB of the packed
/// row-stage tier. Luma is bit-identical (both bin the same native Y).
#[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn packed_vyu444_process_native(
  plan: &crate::resample::ResamplePlan,
  native_planar: &mut Option<std::boxed::Box<planar_8bit::NativePlanarYuv>>,
  y_scratch: &mut Vec<u8>,
  u_scratch: &mut Vec<u8>,
  v_scratch: &mut Vec<u8>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut Vec<u8>,
  packed: &[u8],
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  w: usize,
  h: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  let need_luma = luma.is_some() || luma_u16.is_some();
  let need_color = rgb.is_some() || rgba.is_some() || hsv.is_some();

  // Run the join's COMPLETE pre-feed rejection preflight FIRST — before the
  // fallible Y / U / V de-pack scratch grow — so EVERY rejection case returns
  // its deterministic typed error, never AllocationFailed, and leaves the
  // scratch untouched. Compare-only (no output-set freeze), so the scratch grow
  // stays a pre-feed step ahead of the delegate's commit. The delegate re-runs
  // this identical compare harmlessly and owns the commit.
  if !planar_8bit::native_planar_preflight_check_only(
    native_planar,
    resample_outputs,
    rgb,
    rgba,
    luma,
    luma_u16,
    hsv,
    idx,
    need_luma,
    need_color,
  )? {
    return Ok(());
  }

  // De-pack the interleaved `V Y U` row (3 bytes/pixel) into the private
  // Y / U / V scratch. 4:4:4: chroma is full-width (`w`). Y is always
  // de-packed; U / V only on a colour row. The failpoint fires on the FIRST
  // (Y) grow only.
  grow_packed_444_u8_scratch(y_scratch, w, true, w, h, plan)?;
  for (i, group) in packed.as_chunks::<3>().0.iter().enumerate() {
    y_scratch[i] = group[1]; // Y at byte 1
  }
  if need_color {
    grow_packed_444_u8_scratch(u_scratch, w, false, w, h, plan)?;
    grow_packed_444_u8_scratch(v_scratch, w, false, w, h, plan)?;
    for (i, group) in packed.as_chunks::<3>().0.iter().enumerate() {
      u_scratch[i] = group[2]; // U at byte 2
      v_scratch[i] = group[0]; // V at byte 0
    }
  }

  let (u_plane, v_plane): (&[u8], &[u8]) = if need_color {
    (&u_scratch[..w], &v_scratch[..w])
  } else {
    (&[], &[])
  };
  planar_8bit::yuv_planar_process_native(
    plan,
    native_planar,
    resample_outputs,
    rgb,
    rgba,
    luma,
    luma_u16,
    hsv,
    rgb_scratch,
    &y_scratch[..w],
    u_plane,
    v_plane,
    matrix,
    full_range,
    idx,
    w,
    h,
    1,
    || crate::resample::ResamplePlan::area(w, h, plan.out_w(), plan.out_h()),
    use_simd,
  )
}

/// Native fast-tier decimator for the **high-bit packed 4:4:4** non-alpha YUV
/// formats ([`V410`](crate::source::V410) 10-bit / [`Xv36`](crate::source::Xv36)
/// 12-bit): de-PACKS each format's OWN wire layout into the sink's separate
/// host-native LOGICAL u16 Y / U / V scratch planes, then reuses the HIGH-BIT
/// non-4:2:0 PLANAR join verbatim
/// ([`planar_high_bit_native::yuv_planar16_process_native`]) at
/// [`Yuv444p10`](crate::source::Yuv444p10) /
/// [`Yuv444p12`](crate::source::Yuv444p12) geometry. The 4:4:4 (full-width
/// chroma, `chroma_vsub = 1`) analog of the high-bit packed 4:2:2
/// [`y2xx_process_native`].
///
/// The de-pack is format-specific (V410 bit-extracts a 10-bit U / Y / V from one
/// 32-bit LE/BE word; Xv36 reads four MSB-aligned 12-bit u16 slots `U Y V A` and
/// drops the 4 low padding bits), so it is supplied by the caller as two
/// closures: `fill_y` de-packs the Y plane (always run, the join bins Y for both
/// luma and colour), `fill_uv` de-packs the U / V planes (run only on a colour
/// row). Each writes host-native LOGICAL u16 (a bit-field / MSB extraction is
/// inherently `<= (1 << BITS) - 1`, so it is already in the native range — the
/// join's clamp is a value no-op on encodable input). 4:4:4 has no chroma
/// subsampling, so the de-pack width is `w` and the chroma plan is a plain
/// `area(w, h, ..)`.
///
/// The reused join delegates with `BE = HOST_NATIVE_BE` (= `from_ne`) so its
/// internal decode is a no-op load on the already-native scratch — forwarding the
/// source wire `BE` would byte-swap the native value on a big-endian host. The
/// join now emits BOTH u8 `luma` and the native-depth `luma_u16` (the clamped
/// binned Y), so attaching `luma_u16` keeps the native route (no row-stage
/// fallback) — V410 / Xv36 thread their real `luma_u16` buffer. Under
/// `need_color == false` only Y is de-packed and the join is handed empty U / V
/// slices, so a luma-only / `luma_u16`-only sink plans no chroma state.
///
/// Atomicity mirrors [`y2xx_process_native`]: the join's COMPLETE compare-only
/// pre-feed preflight runs FIRST (via
/// [`planar_high_bit_native::native_planar_hb_preflight_check_only`]), before any
/// fallible scratch grow, so a rejected row returns its deterministic typed
/// error, never `AllocationFailed`, and touches no caller output. It commits no
/// output-set freeze — the de-pack grows here AND the delegate's own colour /
/// source scratch grows all stay pre-commit, and the delegate performs the SINGLE
/// freeze once every pre-feed allocation has succeeded, so a LATER grow failing
/// leaves `resample_outputs` untouched and the row retryable. The de-pack into
/// scratch is infallible and runs only after the preflight clears.
#[cfg(all(feature = "yuv-444-packed", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn packed_yuv444_hb_process_native<const BITS: u32>(
  plan: &crate::resample::ResamplePlan,
  native_planar_u16: &mut Option<std::boxed::Box<NativePlanarYuvU16>>,
  y_scratch: &mut Vec<u16>,
  u_scratch: &mut Vec<u16>,
  v_scratch: &mut Vec<u16>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut Vec<u8>,
  rgb_scratch_u16: &mut Vec<u16>,
  fill_y: impl FnOnce(&mut [u16]),
  fill_uv: impl FnOnce(&mut [u16], &mut [u16]),
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  w: usize,
  h: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  const {
    assert!(
      BITS > 8 && BITS <= 16,
      "BITS must be in (8, 16] for high-bit packed 4:4:4 YUV"
    )
  };
  let need_luma = luma.is_some() || luma_u16.is_some();
  let need_color =
    rgb.is_some() || rgba.is_some() || hsv.is_some() || rgb_u16.is_some() || rgba_u16.is_some();

  // Run the planar join's COMPLETE compare-only pre-feed rejection preflight
  // FIRST — before any fallible scratch grow below, so every rejection returns
  // its deterministic typed error and leaves the wrapper scratch untouched.
  // Compare-only (no output-set freeze): the de-pack grows below AND the
  // delegate's own colour / source scratch grows all stay pre-commit, and the
  // delegate performs the SINGLE freeze once every pre-feed allocation has
  // succeeded — so a LATER grow (here or in the delegate) failing leaves
  // `resample_outputs` untouched and the row retryable.
  if !native_planar_hb_preflight_check_only(
    native_planar_u16,
    resample_outputs,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    idx,
    need_luma,
    need_color,
  )? {
    return Ok(());
  }

  // Grow the wrapper de-pack scratch under the planner's recoverable contract —
  // Y always, U / V only on a colour row (4:4:4: full-width chroma). All grows
  // precede the infallible de-pack and the delegate call. The failpoint fires on
  // the FIRST (Y) grow only.
  grow_packed_444_u16_scratch(y_scratch, w, true, w, h, plan)?;
  if need_color {
    grow_packed_444_u16_scratch(u_scratch, w, false, w, h, plan)?;
    grow_packed_444_u16_scratch(v_scratch, w, false, w, h, plan)?;
  }

  // De-pack the wire layout into host-native LOGICAL u16 scratch via the
  // format-specific closures. Y is always de-packed; U / V only on a colour row
  // (`u_scratch` / `v_scratch` are distinct Vecs, so the two `&mut` borrows do
  // not alias). Everything past here is infallible.
  fill_y(&mut y_scratch[..w]);
  if need_color {
    fill_uv(&mut u_scratch[..w], &mut v_scratch[..w]);
  }

  // Delegate to the planar high-bit non-4:2:0 join with `BE = HOST_NATIVE_BE` so
  // its internal decode is a no-op on the already-native scratch, at the 4:4:4
  // chroma geometry (`chroma_vsub = 1`, `chroma_w = w`). Empty U / V on luma-only
  // rows. V410 / Xv36 thread their real `luma_u16` buffer (the join emits the
  // native-depth `luma_u16`), so attaching it stays on the native route.
  let (u_plane, v_plane): (&[u16], &[u16]) = if need_color {
    (&u_scratch[..w], &v_scratch[..w])
  } else {
    (&[], &[])
  };
  planar_high_bit_native::yuv_planar16_process_native::<BITS, HIGH_BIT_HOST_NATIVE_BE>(
    plan,
    native_planar_u16,
    resample_outputs,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    rgb_scratch,
    rgb_scratch_u16,
    &y_scratch[..w],
    u_plane,
    v_plane,
    matrix,
    full_range,
    idx,
    w,
    h,
    1,
    w,
    || crate::resample::ResamplePlan::area(w, h, plan.out_w(), plan.out_h()),
    use_simd,
  )
}

/// Row-stage fused downscale for the **high-bit packed 4:2:2 YUV**
/// family (`Y210` / `Y212` / `Y216`, plus the exotic 10-bit `V210` word
/// packing) — the 4:2:2 analogue of the high-bit 4:4:4 route, with
/// **three** independent native-precision binnings.
///
/// High-bit packed YUV needs three binnings because the u8 and u16
/// YUV→RGB kernels (`range_params_n::<BITS, 8>` vs `::<BITS, BITS>`)
/// round and scale *independently*, and luma is native Y. Narrowing the
/// u16 bin to u8 would change a uniform-gray downscale's colour — a real
/// parity bug — so each output group bins its own native-precision
/// conversion:
/// 1. **u8 colour (rgb / rgba / hsv):** `convert_rgb_u8` fills a
///    source-width u8 RGB row (the format's `*_to_rgb_row` kernel —
///    chroma de-interleave + 4:2:2 upsample in-register), binned through
///    the u8 packed-RGB tail and fanned out to rgb / rgba / hsv.
/// 2. **u16 colour (rgb_u16 / rgba_u16):** `convert_rgb_u16` fills a
///    source-width native u16 RGB row (`*_to_rgb_u16_row`, source wire
///    `BE`), binned at native depth through the u16 tail with
///    `NATIVE_LUMA16 = false` and **only** rgb_u16 / rgba_u16 attached
///    (every narrowed u8 output passed as `&mut None`, so the tail's
///    narrow scratch is never sized).
/// 3. **luma (luma / luma_u16):** `deinterleave_y_u16` fills a
///    source-width host-native u16 Y row (`*_to_luma_u16_row`), binned
///    through the 1-channel u16 luma stream; luma_u16 is the host-native
///    binned Y, luma is `binned_Y >> (SRC_BITS - 8)`.
///
/// Colour outputs are byte-identical to the area-bin of the direct
/// full-resolution conversion (convert-then-bin — the fused form of
/// converting at full resolution then area-downscaling the RGB); luma is
/// the area-mean of the native Y. Atomic preflight: a single
/// [`frozen_outputs_check`], then a sequence check before any allocation
/// (so an out-of-sequence row is rejected without allocating and
/// `AllocationFailed` never masks `OutOfSequenceRow`), then the three
/// distinct, non-aliasing scratches grow and the three source rows stage
/// — all before the first feed, so a failure mutates no caller output. A
/// no-output call is a true no-op regardless of the row index.
///
/// NOTE (#263 follow-up): when ONLY `with_hsv()` is attached (no RGB /
/// RGBA), this row-stage resample path still stages the source-width u8
/// RGB row (group 1) and derives HSV per OUTPUT row off the binned RGB
/// stream. The high-bit planar direct + native fast tiers go RGB-free
/// for HSV-only, but the row-stage resample does not yet — the
/// `AreaStream` is keyed on the 3-channel RGB row, so an RGB-free
/// resample+HSV-only (resample the HSV planes, or resample Y/U/V then
/// convert per output row) is larger than this PR's scope and is
/// deferred. The HSV output is still correct; only the source-width RGB
/// scratch allocation remains.
#[cfg(any(
  feature = "y2xx",
  feature = "v210",
  feature = "yuv-planar",
  feature = "yuv-semi-planar"
))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_yuv422_triple_resample<const SRC_BITS: u32>(
  luma_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  rgb_stream: &mut Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
  rgb_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  luma_scratch_u16: &mut Vec<u16>,
  rgb_scratch: &mut Vec<u8>,
  rgb_scratch_u16: &mut Vec<u16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  deinterleave_y_u16: impl FnOnce(&mut [u16]),
  convert_rgb_u8: impl FnOnce(&mut [u8]),
  convert_rgb_u16: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError> {
  // Area-only sink (high-bit packed YUV 4:2:2 is not routed to the filter
  // path): reject a filter plan before any work.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  const {
    assert!(
      SRC_BITS > 8 && SRC_BITS <= 16,
      "SRC_BITS must be in (8, 16] for high-bit packed 4:2:2 YUV"
    )
  };
  let ow = plan.out_w();
  let need_luma = luma.is_some() || luma_u16.is_some();
  let need_color_u8 = rgb.is_some() || rgba.is_some() || hsv.is_some();
  let need_color_u16 = rgb_u16.is_some() || rgba_u16.is_some();

  // Sequence-check before allocating: every active stream started at row
  // 0 and the frozen output set keeps the active group fixed for the
  // frame, so they advance in lockstep — any active stream gives the
  // expected row. A no-output call has no stream to sequence and stays a
  // no-op regardless of the row index.
  let expected = if need_luma {
    luma_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else if need_color_u8 {
    rgb_stream.as_ref().map_or(0, |s| s.next_y())
  } else if need_color_u16 {
    rgb_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else {
    return Ok(());
  };
  // On the first row of a frame nothing is frozen yet, so reject an
  // out-of-sequence row here — before the freeze — so a rejected first row
  // never stores a snapshot that would poison a retry. On a later row the
  // freeze runs first (below) so a mid-frame output-set change is reported
  // as ResampleOutputsChanged rather than masked by a freshly-attached
  // stream's row-0 sequence mismatch.
  if resample_outputs.is_none() && expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  if expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }
  if need_luma && luma_stream_u16.is_none() {
    *luma_stream_u16 = Some({
      let stream =
        crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 1)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_color_u8 && rgb_stream.is_none() {
    *rgb_stream = Some({
      let stream =
        crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 3)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_color_u16 && rgb_stream_u16.is_none() {
    *rgb_stream_u16 = Some({
      let stream =
        crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 3)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  // Stage the three source-width rows into their own distinct,
  // non-aliasing scratches (all fallible growths precede the feeds,
  // keeping the call atomic).
  let luma_row = if need_luma {
    let scratch = source_luma_u16_scratch(luma_scratch_u16, w, plan)?;
    deinterleave_y_u16(scratch);
    Some(scratch)
  } else {
    None
  };
  let color_u8_row = if need_color_u8 {
    let scratch = source_rgb_scratch(rgb_scratch, w, plan)?;
    convert_rgb_u8(scratch);
    Some(scratch)
  } else {
    None
  };
  let color_u16_row = if need_color_u16 {
    let scratch = source_rgb_u16_scratch(rgb_scratch_u16, w, plan)?;
    convert_rgb_u16(scratch);
    Some(scratch)
  } else {
    None
  };

  // Binning 3 — native Y through the 1-channel u16 luma stream. A finalized
  // binned native-Y sample can exceed the source's native max on a clipped-high
  // edge, so clamp to `(1 << SRC_BITS) - 1` before the `luma_u16` pass-through
  // and the `>> (SRC_BITS - 8)` u8 narrowing (the luma twin of the colour
  // tail's native_max clamp; without it the narrow would wrap a clipped-high Y
  // to a low value). Mirrors `packed_yuv444_triple_feed_emit`.
  if let Some(y_row) = luma_row {
    let native_max: u16 = ((1u32 << SRC_BITS) - 1) as u16;
    let stream = luma_stream_u16.as_mut().expect("created in the preflight");
    stream.feed_row(idx, y_row, use_simd, |oy, binned_y| {
      if let Some(buf) = luma_u16.as_deref_mut() {
        for (dst, &y) in buf[oy * ow..(oy + 1) * ow].iter_mut().zip(binned_y) {
          *dst = y.min(native_max);
        }
      }
      if let Some(buf) = luma.as_deref_mut() {
        for (dst, &y) in buf[oy * ow..(oy + 1) * ow].iter_mut().zip(binned_y) {
          *dst = (y.min(native_max) >> (SRC_BITS - 8)) as u8;
        }
      }
    })?;
  }

  // Binning 1 — u8 colour through the shared u8 packed-RGB tail (luma /
  // luma_u16 handled by binning 3, so they are `&mut None` here).
  if let Some(scratch) = color_u8_row {
    let stream = rgb_stream.as_mut().expect("created in the preflight");
    packed_rgb_resample_emit(
      stream, plan, rgb, rgba, &mut None, &mut None, hsv, scratch, matrix, full_range, idx,
      use_simd,
    )?;
  }

  // Binning 2 — u16 colour through the shared u16 packed-RGB tail at
  // native depth. Only rgb_u16 / rgba_u16 are emitted; every narrowed u8
  // output is `&mut None`, so the tail's narrow scratch is never sized.
  if let Some(scratch) = color_u16_row {
    let stream = rgb_stream_u16.as_mut().expect("created in the preflight");
    packed_rgb_u16_resample_emit::<SRC_BITS, false>(
      stream,
      plan,
      &mut None,
      &mut None,
      &mut None,
      rgb_u16,
      rgba_u16,
      &mut None,
      &mut None,
      scratch,
      rgb_scratch,
      matrix,
      full_range,
      idx,
      use_simd,
    )?;
  }

  Ok(())
}

/// Separable-filter fused resize for the **high-bit packed 4:2:2 YUV
/// (no-alpha)** family (`Y210` 10-bit / `Y212` 12-bit / `Y216` 16-bit) —
/// the [`SpanKind::Filter`](crate::resample::SpanKind) twin of
/// [`packed_yuv422_triple_resample`]. It mirrors the area path exactly: the
/// YUV is first converted to RGB by the **same** `convert_rgb_u8` /
/// `convert_rgb_u16` closures (which upsample the 4:2:2 chroma), then the
/// RGB is resampled — only the resampler kind differs (signed-coefficient
/// [`FilterStream`] instead of [`AreaStream`]). The staged RGB feeds the
/// **same** emit as the 4:4:4 path
/// ([`packed_yuv444_triple_feed_emit`], generic over
/// [`RowResampler`](crate::resample::RowResampler)) — the convert-then-RGB
/// resample tail is identical once the YUV is in RGB space, so the
/// resampled colour output equals the equivalent packed-RGB filter resample
/// of the converted pixels, and (because the area path converts-then-bins
/// the same RGB) matches the area output up to the kernel weights.
///
/// Luma is the native Y filter-resampled (the filter twin of the area
/// path's native-Y bin): `deinterleave_y` stages the native Y into a
/// source-width `u16` scratch and a 1-channel
/// [`FilterStream<u16>`](crate::resample::FilterStream)
/// (`luma_filter_stream_u16`) resamples it at native depth — luma is taken
/// from Y, never colour-derived, so it stays byte-exact to the direct
/// `*_to_luma*` kernels' filter resample. `luma_u16` is the host-native
/// resampled Y; `luma` is `resampled_y >> (SRC_BITS - 8)`.
///
/// Both the colour `u16` outputs and the native-Y luma carry the source's
/// native depth (`SRC_BITS`, `10` for `Y210`, `12` for `Y212`,
/// low-bit-packed `[0, (1 << SRC_BITS) - 1]`): a signed kernel (CatmullRom /
/// Lanczos3) can overshoot a legal edge. For colour,
/// [`packed_rgb_u16_resample_emit`] clamps every resampled sample to the
/// native max before any `u16` copy / RGBA expansion / u8 narrowing. The
/// de-interleaved native-Y luma bypasses that colour helper, so
/// [`packed_yuv444_triple_feed_emit`] applies the matching clamp to the
/// binned Y before the `luma_u16` copy and the u8 narrowing — keeping
/// `luma_u16 <= (1 << SRC_BITS) - 1` and a clipped-high Y edge mapping to
/// `255` rather than wrapping. For `Y216` (`SRC_BITS == 16`) the native max
/// is the u16 max, so both clamps are value no-ops; the area path never
/// overshoots, so they are no-ops there too. Both clamps match the in-range
/// area path's output exactly.
///
/// Atomic preflight (mirrors [`packed_yuv422_triple_resample`]): a single
/// sequence check on whichever stream is fed every row **before any
/// allocation** (an out-of-sequence first row is rejected before the freeze,
/// storing no snapshot to poison a retry; on a later row the freeze runs
/// first so a mid-frame output change trips `ResampleOutputsChanged`), then
/// every stream and source-width scratch is created before the first feed —
/// so a failure mutates no caller output. A no-output call has no stream to
/// sequence and stays a no-op.
///
/// NOTE (#263 follow-up): HSV-only (no RGB / RGBA) still stages the
/// source-width u8 RGB row here and derives HSV per OUTPUT row off the
/// filtered RGB stream — the direct + native fast tiers go RGB-free, but
/// the row-stage filter resample's stream is keyed on the 3-channel RGB
/// row, so an RGB-free resample+HSV-only is deferred. The HSV output is
/// still correct; only the source-width RGB scratch remains.
#[cfg(any(feature = "y2xx", feature = "yuv-planar", feature = "yuv-semi-planar"))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_yuv422_triple_filter_resample<const SRC_BITS: u32>(
  luma_filter_stream_u16: &mut Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  rgb_filter_stream: &mut Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
  rgb_filter_stream_u16: &mut Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  luma_scratch_u16: &mut Vec<u16>,
  rgb_scratch: &mut Vec<u8>,
  rgb_scratch_u16: &mut Vec<u16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  deinterleave_y_u16: impl FnOnce(&mut [u16]),
  convert_rgb_u8: impl FnOnce(&mut [u8]),
  convert_rgb_u16: impl FnOnce(&mut [u16]),
) -> Result<(), MixedSinkerError> {
  const {
    assert!(
      SRC_BITS > 8 && SRC_BITS <= 16,
      "SRC_BITS must be in (8, 16] for high-bit packed 4:2:2 YUV"
    )
  };
  // Single-kernel filter tail — reject a BICUBLIN plan (its chroma windows are
  // read only by the `Yuv420p` per-plane route) before any state change.
  plan.ensure_single_kernel_filter()?;
  let need_luma = luma.is_some() || luma_u16.is_some();
  let need_color_u8 = rgb.is_some() || rgba.is_some() || hsv.is_some();
  let need_color_u16 = rgb_u16.is_some() || rgba_u16.is_some();

  let (fh, fv) = (
    plan
      .filter_h()
      .expect("filter plan carries horizontal windows"),
    plan
      .filter_v()
      .expect("filter plan carries vertical windows"),
  );

  // Single sequence check before any allocation, on whichever attached
  // stream is fed every row (all attached streams advance in lockstep).
  let expected = if need_luma {
    luma_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else if need_color_u8 {
    rgb_filter_stream.as_ref().map_or(0, |s| s.next_y())
  } else if need_color_u16 {
    rgb_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
  } else {
    return Ok(());
  };
  // First row: reject an out-of-sequence row before the freeze so a
  // rejected first row stores no snapshot that would poison a retry.
  if resample_outputs.is_none() && expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }

  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  if expected != idx {
    return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
      crate::resample::OutOfSequenceRow::new(expected, idx),
    )));
  }

  // Create the filter streams (post-sequence-check). Each plane runs the
  // full output grid against its own source grid; the colour streams carry
  // 3 interleaved channels, the luma stream 1.
  if need_luma && luma_filter_stream_u16.is_none() {
    *luma_filter_stream_u16 = Some({
      let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 1)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_color_u8 && rgb_filter_stream.is_none() {
    *rgb_filter_stream = Some({
      let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 3)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }
  if need_color_u16 && rgb_filter_stream_u16.is_none() {
    *rgb_filter_stream_u16 = Some({
      let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 3)?;
      crate::resample::try_box(stream).map_err(|_| {
        MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?
    });
  }

  // Shared convert-then-resample tail (the 4:4:4 feed_emit, generic over
  // `RowResampler`): identical once the YUV is in RGB space, and it applies
  // the native-max clamp to both the u16 colour (via
  // `packed_rgb_u16_resample_emit`) and the native-Y luma for a sub-16-bit
  // source. The 4:2:2 arguments are reordered into the 4:4:4 feed_emit's
  // (rgb, rgb_u16, luma) stream / scratch / closure order.
  packed_yuv444_triple_feed_emit::<_, _, _, SRC_BITS>(
    rgb_filter_stream.as_mut(),
    rgb_filter_stream_u16.as_mut(),
    luma_filter_stream_u16.as_mut(),
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    rgb_scratch,
    rgb_scratch_u16,
    luma_scratch_u16,
    w,
    plan,
    idx,
    use_simd,
    matrix,
    full_range,
    convert_rgb_u8,
    convert_rgb_u16,
    deinterleave_y_u16,
  )
}

/// Source-width `f32` RGB staging for packed-float-RGB resampling: the
/// wire row converts here (host-native f32, lossless) before feeding
/// [`AreaStream<f32>`]. Grows `scratch` to `3 * width` `f32` under the
/// planner's recoverable-allocation contract. Mirrors
/// [`source_rgb_u16_scratch`] for the float element path.
#[cfg(any(
  all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
  feature = "gbr"
))]
pub(super) fn source_rgb_f32_scratch<'s>(
  scratch: &'s mut Vec<f32>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [f32], MixedSinkerError> {
  // Single-shot grow failpoint (RFC #238 tail-collapse tests): refuse this grow as
  // a host OOM would, proving a `f32` source-staging OOM AFTER a successful stream
  // build leaves the stream field `None` AND `resample_outputs` uncommitted
  // (retryable). The float twin of the `source_rgb_u32_scratch` failpoint, so a
  // 3-ch float atomicity test can fail this specific pre-commit grow.
  #[cfg(all(
    test,
    feature = "std",
    any(
      all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
      feature = "gbr"
    )
  ))]
  if FORCE_SOURCE_RGB_F32_SCRATCH_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
    )));
  }
  let row = width
    .checked_mul(3)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.src_h(),
      3,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0.0);
  }
  Ok(&mut scratch[..row])
}

// Single-shot source-RGB-`f32`-scratch grow failpoint for the RFC #238
// tail-collapse transactional-preflight tests. Gated on the packed-float-RGB /
// planar-GBR families that stage a source-width `f32` RGB row pre-commit;
// `source_rgb_f32_scratch` consults it under the same gate. Separate from the
// `try_box` box-alloc failpoint so a 3-channel float test can fail the SCRATCH
// grow specifically (after a successful stream construction).
#[cfg(all(
  test,
  feature = "std",
  any(
    all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
    feature = "gbr"
  )
))]
std::thread_local! {
  static FORCE_SOURCE_RGB_F32_SCRATCH_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}

/// Arms a single-shot failpoint that makes the next [`source_rgb_f32_scratch`]
/// grow refuse with `AllocationFailed`, exactly as a host OOM would. Proves the
/// commit-together atomic shape of [`packed_rgb_f32_resample`] (a source-staging
/// OOM after a successful stream build leaves the stream field `None` AND
/// `resample_outputs` uncommitted, so the row is retryable). Test-only.
#[cfg(all(
  test,
  feature = "std",
  all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
))]
pub(crate) fn arm_source_rgb_f32_scratch_failure() {
  FORCE_SOURCE_RGB_F32_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// Out-width G/B/R `f32` plane staging for the [`Gbrpf32`](crate::source::Gbrpf32)
/// arm of the float packed-RGB tail: the dedicated `gbr` emit de-interleaves
/// each binned packed row back into three planes so it can run the exact
/// direct `gbrpf32_*` kernels for every output. Grows `scratch` to
/// `3 * width` `f32` — three contiguous planes — under the planner's
/// recoverable-allocation contract. Only the `gbr` emit consumes it; the
/// `rgb-float` ([`Rgbf32`](crate::source::Rgbf32)) caller never allocates it.
#[cfg(feature = "gbr")]
pub(super) fn rgb_plane_f32_scratch<'s>(
  scratch: &'s mut Vec<f32>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [f32], MixedSinkerError> {
  let row = width
    .checked_mul(3)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.out_h(),
      3,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0.0);
  }
  Ok(&mut scratch[..row])
}

/// Out-width G/B/R `half::f16` plane staging for the
/// [`Gbrpf16`](crate::source::Gbrpf16) arm of the float packed-RGB
/// tail. There is no `AreaStream<f16>`, so binning runs in `f32`; this
/// emit de-interleaves each binned packed row into `f32` planes, rounds
/// them to `half::f16` here, and runs the exact direct `gbrpf16_*`
/// kernels for every output. Grows `scratch` to `3 * width`
/// `half::f16` — three contiguous planes — under the planner's
/// recoverable-allocation contract. Only the f16 emit consumes it.
#[cfg(feature = "gbr")]
pub(super) fn rgb_plane_f16_scratch<'s>(
  scratch: &'s mut Vec<half::f16>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [half::f16], MixedSinkerError> {
  let row = width
    .checked_mul(3)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.out_h(),
      3,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, half::f16::ZERO);
  }
  Ok(&mut scratch[..row])
}

/// RFC #238 tail-collapse — the transactional packed-float-RGB **area** resample,
/// the float element-family twin of [`packed_rgb_u32_resample`]. Folds the split
/// preflight + area-stream build + per-format emit into one call behind the shared
/// compare-before / commit-after preflight
/// ([`resample_preflight_check_only`](resample_preflight_check_only)):
/// the missing area stream is built into a LOCAL and the source-width `f32` RGB
/// staging scratch is grown with NO field mutation, so the stream field insert and
/// the output-set freeze COMMIT TOGETHER only after they both succeed. A first-row
/// allocation failure therefore leaves `rgb_stream_f32` `None` AND `resample_outputs`
/// uncommitted (never a partial commit) — state-atomic and retryable even with a
/// changed output attachment (the #180 discipline). A pure reordering of the
/// pre-collapse split path: identical bytes, only deferring the field mutations past
/// the fallible allocations.
///
/// The four packed-float-RGB / planar-GBR 3-channel arms (`Rgbf32` / `Rgbf16` /
/// `Gbrpf32` / `Gbrpf16`) share this driver, the shared `f32` area stream, and this
/// preflight, differing only in the final `emit` (each format passes its own of the
/// four 3-channel emits — the f16 arms reuse the f32 stream, rounding to f16 in
/// their emit) and the `convert` that stages the source row — so the driver takes
/// both as parameters. `convert` stages the source-width host-native `f32` RGB row
/// into the grown scratch; `emit` feeds it and derives every output. The emit's own
/// out-width scratch grows after the freeze exactly as the pre-collapse split path
/// did (they are the emit's, not this driver's, so they are not pre-grown here).
#[cfg(any(
  all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
  feature = "gbr"
))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_f32_resample(
  rgb_stream_f32: &mut Option<std::boxed::Box<crate::resample::AreaStream<f32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch_f32: &mut Vec<f32>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  convert: impl FnOnce(&mut [f32]),
  mut emit: impl FnMut(
    Option<&mut crate::resample::AreaStream<f32>>,
    &[f32],
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [f32]>,
    &mut Option<&mut [f32]>,
    &mut Option<&mut [half::f16]>,
    &mut Option<&mut [half::f16]>,
    &mut Option<HsvFrameMut<'_>>,
  ) -> Result<(), MixedSinkerError>,
) -> Result<(), MixedSinkerError> {
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  // Sequence-counter row for the shared L1 preflight (the area stream is the sole
  // per-row stream), or `None` when no output is attached so the preflight
  // short-circuits to a no-op.
  let expected = if need_any {
    Some(rgb_stream_f32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible stream alloc + scratch growth below succeed, so a first-row
  // allocation failure leaves `resample_outputs` uncommitted (retryable with a
  // changed output attachment). A no-output sink short-circuits to a no-op.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Area-only sink: reject a filter plan before any allocation. Runs AFTER the
  // compare-only preflight, so a no-output sink stays a no-op even under a filter
  // plan.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  // Build the missing area stream into a LOCAL and grow the source-width `f32` RGB
  // staging scratch with NO field mutation, so the stream field insert and the
  // freeze commit TOGETHER only after they both succeed (the `planar_dual_resample`
  // atomic shape: a scratch OOM leaves `rgb_stream_f32` `None` AND `resample_outputs`
  // uncommitted, never a partial commit).
  let new_stream = if rgb_stream_f32.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 3)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  source_rgb_f32_scratch(rgb_scratch_f32, w, plan)?;
  // Pre-grow the emit-owned out-width scratch BEFORE the freeze (a `None` stream
  // grows it and returns), so — like the merged u16 / u32 helpers — every fallible
  // pre-feed alloc precedes the commit and NOTHING fallible runs after the freeze.
  emit(
    None,
    &[],
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
  )?;
  // Every pre-feed allocation succeeded — COMMIT atomically: insert the stream and
  // freeze the output set together (the compare above already caught any later
  // mid-frame output change; the freeze stores the snapshot on the first row).
  if let Some(s) = new_stream {
    *rgb_stream_f32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    idx,
  )?;
  // Stage the source-width host-native `f32` RGB row (the caller's conversion into
  // the grown scratch, the grow re-run a no-op) and run the per-format emit.
  let stream = rgb_stream_f32.as_mut().expect("created above");
  let src_f32 = source_rgb_f32_scratch(rgb_scratch_f32, w, plan)?;
  convert(src_f32);
  emit(
    Some(stream),
    src_f32,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
  )
}

/// RFC #238 tail-collapse — the transactional packed-float-RGB **filter**
/// resample, the [`SpanKind::Filter`](crate::resample::SpanKind) twin of
/// [`packed_rgb_f32_resample`] over a 3-channel `f32`
/// [`FilterStream`](crate::resample::FilterStream). Identical compare-before /
/// commit-after discipline (the filter stream is built into a local and its field
/// insert + the freeze + the pre-grown source scratch commit together only after
/// every allocation) and the identical per-format `emit` tail (the emits are
/// generic over the resampler kind), so the resampled colour equals the equivalent
/// area resample up to the signed kernel weights while remaining byte-identical to
/// the pre-collapse split filter path. Gated like [`packed_rgb_f32_resample`]: the
/// packed-float-RGB consumers (`rgb-float`, engine fenced in) AND `gbr` — the float
/// planar GBR sources scatter their G/B/R planes into the same packed `f32` RGB row
/// and filter through this stream.
#[cfg(any(
  all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")),
  feature = "gbr"
))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_f32_resample_filter(
  rgb_filter_stream_f32: &mut Option<std::boxed::Box<crate::resample::FilterStream<f32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch_f32: &mut Vec<f32>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  convert: impl FnOnce(&mut [f32]),
  mut emit: impl FnMut(
    Option<&mut crate::resample::FilterStream<f32>>,
    &[f32],
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [f32]>,
    &mut Option<&mut [f32]>,
    &mut Option<&mut [half::f16]>,
    &mut Option<&mut [half::f16]>,
    &mut Option<HsvFrameMut<'_>>,
  ) -> Result<(), MixedSinkerError>,
) -> Result<(), MixedSinkerError> {
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  let expected = if need_any {
    Some(rgb_filter_stream_f32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit) — see `packed_rgb_f32_resample`.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Single-kernel filter: reject a BICUBLIN plan before any allocation (its chroma
  // windows are read only by the `Yuv420p` per-plane route, so filtering every
  // channel with the luma kernel would be wrong output).
  plan.ensure_single_kernel_filter()?;
  // Build the missing filter stream into a LOCAL and grow the source scratch with
  // NO field mutation, so the stream field insert and the freeze commit TOGETHER
  // (the `planar_dual_resample` atomic shape: a scratch OOM leaves
  // `rgb_filter_stream_f32` `None` AND `resample_outputs` uncommitted).
  let new_stream = if rgb_filter_stream_f32.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 3)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  source_rgb_f32_scratch(rgb_scratch_f32, w, plan)?;
  // Pre-grow the emit-owned out-width scratch BEFORE the freeze (a `None` stream
  // grows it and returns), so every fallible pre-feed alloc precedes the commit and
  // NOTHING fallible runs after the freeze.
  emit(
    None,
    &[],
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
  )?;
  if let Some(s) = new_stream {
    *rgb_filter_stream_f32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    idx,
  )?;
  let stream = rgb_filter_stream_f32.as_mut().expect("created above");
  let src_f32 = source_rgb_f32_scratch(rgb_scratch_f32, w, plan)?;
  convert(src_f32);
  emit(
    Some(stream),
    src_f32,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
  )
}

/// Feeds the prepared source-width `f32` RGB row into the (already
/// sequence-checked) stream and derives every attached output from each
/// finalized output row. Binning runs in float; the `rgb_f32` output
/// copies it losslessly, and every integer output mirrors the direct
/// [`Rgbf32`](crate::source::Rgbf32) path's clamp+scale kernels run over
/// the binned row. The binned row is already host-native f32 (the wire
/// converted via `rgbf32_to_rgb_f32_row::<BE>` before feeding), so the
/// emit kernels use `::<false>` — no further byte swap. `narrow_scratch`
/// is sized to the out-width u8 RGB row only when one of the outputs
/// that stage through it (`rgb` / `luma` / `luma_u16` / `hsv`) is
/// attached, so an f32-only or native-u16-only sink neither grows it nor
/// risks its allocation failure.
#[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_f32_resample_emit(
  stream: Option<&mut impl crate::resample::RowResampler<f32>>,
  plan: &ResamplePlan,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  src_f32: &[f32],
  narrow_scratch: &mut Vec<u8>,
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  let ow = plan.out_w();
  // The u8 RGB / luma / luma_u16 / hsv outputs stage through a u8 RGB
  // narrowing of the binned float row (exactly the direct path's
  // `rgbf32_to_rgb_row` scratch); an f32-only or native-u16-only sink
  // never touches it, so the out-width u8 scratch is sized — and its
  // allocation failure risked — only when one of those outputs is
  // attached. The predicate gates both the sizing here and the use in
  // the closure, so they cannot drift. `rgba` (u8) derives directly
  // from the float source via `rgbf32_to_rgba_row`, mirroring the
  // direct path, so it does not need the narrow row.
  let need_narrow = rgb.is_some() || luma.is_some() || luma_u16.is_some() || hsv.is_some();
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  // The binned row is host-native f32 (the scatter decoded the source to
  // host order before binning), so the `rgbf32_*` kernels — which take a
  // wire-endian const and `load_f32` accordingly — must be told the data
  // is already host-order, else a big-endian target byte-swaps it and
  // corrupts every derived output.
  const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
  // Pregrow phase (RFC #238 tail-collapse): a `None` stream grows the emit-owned
  // out-width scratch above and returns, so the combined helper can pre-grow it
  // BEFORE the freeze; the real feed passes `Some`. Nothing fallible runs after the
  // freeze.
  let Some(stream) = stream else {
    return Ok(());
  };
  stream.feed_row(idx, src_f32, use_simd, |oy, binned| {
    // Lossless float pass-through — copy the binned row verbatim
    // (mirrors the direct path's `rgbf32_to_rgb_f32_row`; the binned
    // row is already host-native, so this is a plain copy).
    if let Some(buf) = rgb_f32.as_deref_mut() {
      buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(binned);
    }
    // u16 outputs — direct float→u16 clamp+scale (no narrowing stage),
    // exactly as the direct Rgbf32 path derives them from the source.
    if let Some(buf) = rgb_u16.as_deref_mut() {
      crate::row::rgbf32_to_rgb_u16_row::<HOST_NATIVE_BE>(
        binned,
        &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
        ow,
        use_simd,
      );
    }
    if let Some(buf) = rgba_u16.as_deref_mut() {
      crate::row::rgbf32_to_rgba_u16_row::<HOST_NATIVE_BE>(
        binned,
        &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
        ow,
        use_simd,
      );
    }
    // u8 RGBA — direct float→u8 clamp+scale, alpha 0xFF (the direct
    // path emits RGBA straight from the float source, not via an
    // expand of the u8 RGB row).
    if let Some(buf) = rgba.as_deref_mut() {
      crate::row::rgbf32_to_rgba_row::<HOST_NATIVE_BE>(
        binned,
        &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
        ow,
        use_simd,
      );
    }
    if need_narrow {
      let nrow = &mut narrow[..3 * ow];
      // Stage the u8 RGB row once via the direct path's float→u8
      // clamp+scale; rgb / luma / luma_u16 / hsv all read it, matching
      // the direct Rgbf32 source-of-truth ordering exactly.
      crate::row::rgbf32_to_rgb_row::<HOST_NATIVE_BE>(binned, nrow, ow, use_simd);
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      if let Some(buf) = luma.as_deref_mut() {
        crate::row::rgb_to_luma_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(buf) = luma_u16.as_deref_mut() {
        crate::row::rgb_to_luma_u16_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  Ok(())
}

/// Out-width **packed** `R, G, B` `half::f16` staging for the
/// [`Rgbf16`](crate::source::Rgbf16) arm of the float packed-RGB tail.
/// There is no `AreaStream<f16>`, so binning runs in `f32`; this emit
/// rounds each binned packed `f32` element to `half::f16` into this row
/// and runs the exact direct `rgbf16_*` kernels for every output. Unlike
/// the planar [`Gbrpf16`](crate::source::Gbrpf16) scratch this row stays
/// **packed** (no de-interleave). Grows `scratch` to `3 * width`
/// `half::f16` under the planner's recoverable-allocation contract. Only
/// the packed f16 emit consumes it.
#[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
pub(super) fn rgb_packed_f16_scratch<'s>(
  scratch: &'s mut Vec<half::f16>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [half::f16], MixedSinkerError> {
  let row = width
    .checked_mul(3)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.out_h(),
      3,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, half::f16::ZERO);
  }
  Ok(&mut scratch[..row])
}

/// Feeds the prepared source-width packed `R, G, B` `f32` row (the
/// [`Rgbf16`](crate::source::Rgbf16) wire widened f16 -> host-native f32)
/// into the f32 stream and derives every attached output from each
/// finalized output row. `stream` is generic over
/// [`RowResampler<f32>`](crate::resample::RowResampler), so the **same**
/// emit serves both the float area path ([`AreaStream<f32>`]) and the
/// signed-coefficient filter path ([`FilterStream<f32>`]) — the span kind
/// is chosen at the call site; the round-then-`rgbf16_*` tail is identical.
///
/// There is no `AreaStream<f16>`/`FilterStream<f16>`, so binning runs in
/// `f32` for precision.
/// Per finalized output row this tail **rounds the binned packed `f32` row
/// to `half::f16`** ([`rgb_packed_f16_scratch`]) and runs the **exact
/// direct `rgbf16_*` kernels** over that packed f16 row. The result is
/// therefore byte-identical to a direct full-resolution `Rgbf16`
/// conversion of the frame whose per-pixel f16 `R, G, B` is the `f32` area
/// mean rounded to f16 (the parity oracle) — because the emit performs the
/// identical round-then-`rgbf16_*`. The f16-native kernels
/// (`rgbf16_to_rgb_f16_row` / `..._u16_row` / `..._rgba_u16_row` /
/// `..._row` / `..._rgba_row`) consume the rounded packed f16 row directly;
/// the lossless `rgb_f32` output — which the direct path derives by
/// *widening* the f16 source to f32 — is reproduced by widening the
/// **rounded** packed f16 row back to f32 (`rgbf16_to_rgb_f32_row`), so it
/// too matches the f16-rounded oracle, not the raw f32 bin. The u8 RGB /
/// luma / luma_u16 / hsv outputs stage through a u8 RGB narrowing of the
/// rounded packed f16 row (`rgbf16_to_rgb_row`, exactly the direct path's
/// scratch); `rgba` (u8) derives directly from the rounded packed f16 row
/// via `rgbf16_to_rgba_row`, mirroring the direct path.
///
/// The rounded packed f16 row holds **host-native** `half::f16` (rounded
/// from host-native binned f32), so every `rgbf16_*` kernel — which takes a
/// wire-endian const and byte-swaps when it differs from the host — is
/// invoked with `HOST_NATIVE_BE` to make its load a no-op on every host.
/// `packed_scratch_f16` (the rounded packed row) is sized — and its
/// allocation failure risked — only when an output is attached;
/// `narrow_scratch` (the u8 RGB row) only when an output that stages
/// through it (`rgb` / `luma` / `luma_u16` / `hsv`) is attached. So an
/// `rgb_f32`-only sink sizes only the packed f16 row, and a no-output sink
/// sizes neither.
#[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgb_f16_resample_emit(
  stream: Option<&mut impl crate::resample::RowResampler<f32>>,
  plan: &ResamplePlan,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  src_f32: &[f32],
  packed_scratch_f16: &mut Vec<half::f16>,
  narrow_scratch: &mut Vec<u8>,
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  // The rounded packed f16 row holds host-native data — the binned row was
  // decoded to host order during scatter, then rounded with `from_f32`,
  // which yields host-native `half::f16`. The `rgbf16_*` kernels take a
  // wire-endian const and byte-swap when it differs from the host, so pass
  // the host's own endianness to make every load a no-op; otherwise a
  // big-endian target would corrupt every output.
  const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");

  let ow = plan.out_w();
  // Every output derives from the rounded packed f16 row; even `rgb_f32`
  // does, because the direct `Rgbf16` path widens the f16 source to f32 (so
  // the oracle's `rgb_f32` is the f32 bin rounded to f16, then widened —
  // not the raw f32 bin). The predicate gates both the sizing here and the
  // round in the closure, so they cannot drift; a sink with no output sizes
  // nothing.
  let need_round = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgb_f16.is_some()
    || hsv.is_some();
  // The u8 RGB / luma / luma_u16 / hsv outputs stage through a u8 RGB
  // narrowing of the rounded packed f16 row (exactly the direct path's
  // `rgbf16_to_rgb_row` scratch); an f32-/f16-/native-u16-only sink never
  // touches it, so the out-width u8 scratch is sized — and its allocation
  // failure risked — only when one of those outputs is attached. `rgba`
  // (u8) derives directly from the rounded f16 row, so it does not need the
  // narrow row.
  let need_narrow = rgb.is_some() || luma.is_some() || luma_u16.is_some() || hsv.is_some();
  // Allocate both scratch rows up front (recoverable) before the stream's
  // closure writes any caller buffer, so an allocation refusal never leaves
  // a partially written output.
  let packed_f16: &mut [half::f16] = if need_round {
    rgb_packed_f16_scratch(packed_scratch_f16, ow, plan)?
  } else {
    &mut []
  };
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  // Pregrow phase (RFC #238 tail-collapse): a `None` stream grows the emit-owned
  // scratch above and returns, so the combined helper can pre-grow it BEFORE the
  // freeze; the real feed passes `Some`. Nothing fallible runs after the freeze.
  let Some(stream) = stream else {
    return Ok(());
  };
  stream.feed_row(idx, src_f32, use_simd, |oy, binned| {
    if need_round {
      // Round the binned packed `R, G, B` `f32` row to the packed f16 row
      // — the exact layout the direct `rgbf16_*` kernels consume, holding
      // the f32 block mean rounded to f16. (The f32-derived `rgb_f32`
      // output widens this rounded row back, never the raw bin.)
      let prow = &mut packed_f16[..3 * ow];
      for (dst, &src) in prow.iter_mut().zip(binned.iter()) {
        *dst = half::f16::from_f32(src);
      }
      let prow = &prow[..3 * ow];

      // ---- f16-native kernels: consume the rounded packed f16 row
      // directly, exactly as the direct `Rgbf16` path does ---------------
      if let Some(buf) = rgb_f16.as_deref_mut() {
        crate::row::rgbf16_to_rgb_f16_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      // Lossless f32 widen of the **rounded** f16 row — the direct path
      // widens its f16 source to f32, so the oracle's `rgb_f32` is the
      // bin rounded to f16 then widened (NOT the raw f32 bin).
      if let Some(buf) = rgb_f32.as_deref_mut() {
        crate::row::rgbf16_to_rgb_f32_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb_u16.as_deref_mut() {
        crate::row::rgbf16_to_rgb_u16_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba_u16.as_deref_mut() {
        crate::row::rgbf16_to_rgba_u16_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      // u8 RGBA — direct f16->u8 clamp+scale, alpha 0xFF (the direct path
      // emits RGBA straight from the f16 source, not via an expand of the
      // u8 RGB row).
      if let Some(buf) = rgba.as_deref_mut() {
        crate::row::rgbf16_to_rgba_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if need_narrow {
        let nrow = &mut narrow[..3 * ow];
        // Stage the u8 RGB row once via the direct path's f16->u8
        // clamp+scale; rgb / luma / luma_u16 / hsv all read it, matching
        // the direct Rgbf16 source-of-truth ordering exactly.
        crate::row::rgbf16_to_rgb_row::<HOST_NATIVE_BE>(prow, nrow, ow, use_simd);
        if let Some(buf) = rgb.as_deref_mut() {
          buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
        }
        if let Some(buf) = luma.as_deref_mut() {
          crate::row::rgb_to_luma_row(
            nrow,
            &mut buf[oy * ow..(oy + 1) * ow],
            ow,
            matrix,
            full_range,
            use_simd,
          );
        }
        if let Some(buf) = luma_u16.as_deref_mut() {
          crate::row::rgb_to_luma_u16_row(
            nrow,
            &mut buf[oy * ow..(oy + 1) * ow],
            ow,
            matrix,
            full_range,
            use_simd,
          );
        }
        if let Some(hsv) = hsv.as_mut() {
          let (h, s, v) = hsv.hsv();
          crate::row::rgb_to_hsv_row(
            nrow,
            &mut h[oy * ow..(oy + 1) * ow],
            &mut s[oy * ow..(oy + 1) * ow],
            &mut v[oy * ow..(oy + 1) * ow],
            ow,
            use_simd,
          );
        }
      }
    }
  })?;
  Ok(())
}

// ---- Packed float RGBA (Rgbaf32 / Rgbaf16) resample tails --------------
//
// The packed-float RGBA sources stage their wire row as a canonical
// source-width packed `R, G, B, A` f32 row and bin all four channels
// (straight alpha — a real per-channel area / filter mean, never forced
// opaque) on the 4-channel `AreaStream<f32>` / `FilterStream<f32>`. Per
// finalized output row every output is derived from the binned packed RGBA
// row by the **exact direct `rgbaf32_*` / `rgbaf16_*` kernels**, so the
// resampled frame is byte-identical to a direct `Rgbaf32` / `Rgbaf16`
// conversion of the binned frame (the parity oracle). These mirror the
// 3-channel `packed_rgb_f32_resample_emit` / `packed_rgb_f16_resample_emit`
// exactly, extended with the alpha channel and the `rgba_f32` / `rgba_f16`
// lossless outputs. Luma / HSV stage through a u8 RGB narrowing of the
// binned row (dropping alpha), matching the `Rgbf32` / `Rgbf16` sources of
// truth.

/// RFC #238 tail-collapse — the transactional packed-RGBA-**float** **area**
/// resample, the 4-channel packed twin of [`packed_rgb_f32_resample`] (`_packed`
/// because [`packed_rgba_f32_resample`] is the planar-GBR-alpha 4-channel path).
/// Folds the split preflight + area-stream build + per-format emit into one call
/// behind the shared compare-before / commit-after preflight: the missing area
/// stream is built into a LOCAL and the source-width `f32` RGBA staging scratch is
/// grown with NO field mutation, so the stream field insert and the output-set
/// freeze COMMIT TOGETHER only after they both succeed. A first-row allocation
/// failure therefore leaves `rgba_stream_f32` `None` AND `resample_outputs`
/// uncommitted (the #180 discipline) — byte-identical to the pre-collapse split
/// path. The [`Rgbaf32`](crate::source::Rgbaf32) and [`Rgbaf16`](crate::source::Rgbaf16)
/// arms share this driver, the shared `f32` area stream, and this preflight,
/// differing only in the final `emit` (the f16 arm reuses the f32 stream, rounding
/// to f16 in its emit) and the `convert` that stages the source row.
#[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_f32_packed_resample(
  rgba_stream_f32: &mut Option<std::boxed::Box<crate::resample::AreaStream<f32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch_f32: &mut Vec<f32>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  convert: impl FnOnce(&mut [f32]),
  mut emit: impl FnMut(
    Option<&mut crate::resample::AreaStream<f32>>,
    &[f32],
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [f32]>,
    &mut Option<&mut [f32]>,
    &mut Option<&mut [half::f16]>,
    &mut Option<&mut [half::f16]>,
    &mut Option<HsvFrameMut<'_>>,
  ) -> Result<(), MixedSinkerError>,
) -> Result<(), MixedSinkerError> {
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  let expected = if need_any {
    Some(rgba_stream_f32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit) — see `packed_rgb_f32_resample`.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Area-only: reject a filter plan before any allocation (the straight-alpha
  // filter path takes `packed_rgba_f32_packed_filter`).
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  // Build the missing area stream into a LOCAL and grow the source-width `f32` RGBA
  // staging scratch with NO field mutation, so the stream field insert and the
  // freeze commit TOGETHER (a scratch OOM leaves `rgba_stream_f32` `None` AND
  // `resample_outputs` uncommitted, never a partial commit). The emit's own
  // out-width scratch grows after the freeze as the pre-collapse split path did.
  let new_stream = if rgba_stream_f32.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  source_rgba_f32_scratch(rgba_scratch_f32, w, plan)?;
  // Pre-grow the emit-owned out-width scratch BEFORE the freeze (a `None` stream
  // grows it and returns), so every fallible pre-feed alloc precedes the commit and
  // NOTHING fallible runs after the freeze.
  emit(
    None,
    &[],
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
  )?;
  if let Some(s) = new_stream {
    *rgba_stream_f32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    idx,
  )?;
  let stream = rgba_stream_f32.as_mut().expect("created above");
  let src_rgba = source_rgba_f32_scratch(rgba_scratch_f32, w, plan)?;
  convert(src_rgba);
  emit(
    Some(stream),
    src_rgba,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
  )
}

/// RFC #238 tail-collapse — the transactional packed-RGBA-**float** **filter**
/// resample, the [`SpanKind::Filter`](crate::resample::SpanKind) twin of
/// [`packed_rgba_f32_packed_resample`] over a 4-channel `f32`
/// [`FilterStream`](crate::resample::FilterStream). PIL filters R, G, B, A
/// independently with no premultiplication, so the four interleaved channels bin
/// through one 4-channel stream and resampled alpha is a real filtered channel;
/// the same compare-before / commit-after discipline as the area path keeps a
/// first-row failure from leaving `rgba_filter_stream_f32` `Some` or the freeze
/// committed.
#[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_f32_packed_filter(
  rgba_filter_stream_f32: &mut Option<std::boxed::Box<crate::resample::FilterStream<f32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch_f32: &mut Vec<f32>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  convert: impl FnOnce(&mut [f32]),
  mut emit: impl FnMut(
    Option<&mut crate::resample::FilterStream<f32>>,
    &[f32],
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u8]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [u16]>,
    &mut Option<&mut [f32]>,
    &mut Option<&mut [f32]>,
    &mut Option<&mut [half::f16]>,
    &mut Option<&mut [half::f16]>,
    &mut Option<HsvFrameMut<'_>>,
  ) -> Result<(), MixedSinkerError>,
) -> Result<(), MixedSinkerError> {
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  let expected = if need_any {
    Some(rgba_filter_stream_f32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit) — see `packed_rgb_f32_resample`.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  plan.ensure_single_kernel_filter()?;
  // Build the missing filter stream into a LOCAL and grow the source scratch with
  // NO field mutation, so the stream field insert and the freeze commit TOGETHER.
  let new_stream = if rgba_filter_stream_f32.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  source_rgba_f32_scratch(rgba_scratch_f32, w, plan)?;
  // Pre-grow the emit-owned out-width scratch BEFORE the freeze (a `None` stream
  // grows it and returns), so every fallible pre-feed alloc precedes the commit and
  // NOTHING fallible runs after the freeze.
  emit(
    None,
    &[],
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
  )?;
  if let Some(s) = new_stream {
    *rgba_filter_stream_f32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    idx,
  )?;
  let stream = rgba_filter_stream_f32.as_mut().expect("created above");
  let src_rgba = source_rgba_f32_scratch(rgba_scratch_f32, w, plan)?;
  convert(src_rgba);
  emit(
    Some(stream),
    src_rgba,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
  )
}

/// Feeds the prepared source-width packed `R, G, B, A` `f32` row into the
/// (already sequence-checked) 4-channel stream and derives every attached
/// output from each finalized output row. The 4-channel twin of
/// [`packed_rgb_f32_resample_emit`]: `rgba_f32` copies the binned row
/// losslessly, `rgb_f32` drops alpha, and every integer / u8 output mirrors
/// the direct [`Rgbaf32`](crate::source::Rgbaf32) clamp+scale kernels over
/// the binned row. The binned row is host-native f32 (the wire converted via
/// `rgbaf32_to_rgba_f32_row::<BE>` before feeding), so the emit kernels use
/// `::<HOST_NATIVE_BE>`. `narrow_scratch` (the out-width u8 RGB row) is sized
/// only when an output that stages through it (`rgb` / `luma` / `luma_u16` /
/// `hsv`) is attached.
#[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_f32_resample_emit(
  stream: Option<&mut impl crate::resample::RowResampler<f32>>,
  plan: &ResamplePlan,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  src_rgba: &[f32],
  narrow_scratch: &mut Vec<u8>,
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  let ow = plan.out_w();
  let need_narrow = rgb.is_some() || luma.is_some() || luma_u16.is_some() || hsv.is_some();
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
  // Pregrow phase (RFC #238 tail-collapse): a `None` stream grows the emit-owned
  // scratch above and returns, so the combined helper can pre-grow it BEFORE the
  // freeze; the real feed passes `Some`. Nothing fallible runs after the freeze.
  let Some(stream) = stream else {
    return Ok(());
  };
  stream.feed_row(idx, src_rgba, use_simd, |oy, binned| {
    // Lossless packed `f32` RGBA pass-through — copy the binned 4-channel
    // row verbatim (already host-native).
    if let Some(buf) = rgba_f32.as_deref_mut() {
      buf[oy * 4 * ow..(oy + 1) * 4 * ow].copy_from_slice(binned);
    }
    // Lossless RGB f32 (drop alpha).
    if let Some(buf) = rgb_f32.as_deref_mut() {
      crate::row::rgbaf32_to_rgb_f32_row::<HOST_NATIVE_BE>(
        binned,
        &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
        ow,
        use_simd,
      );
    }
    // u16 outputs — direct float→u16 clamp+scale over the binned row.
    if let Some(buf) = rgb_u16.as_deref_mut() {
      crate::row::rgbaf32_to_rgb_u16_row::<HOST_NATIVE_BE>(
        binned,
        &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
        ow,
        use_simd,
      );
    }
    if let Some(buf) = rgba_u16.as_deref_mut() {
      crate::row::rgbaf32_to_rgba_u16_row::<HOST_NATIVE_BE>(
        binned,
        &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
        ow,
        use_simd,
      );
    }
    // u8 RGBA — real binned alpha, clamp+scale.
    if let Some(buf) = rgba.as_deref_mut() {
      crate::row::rgbaf32_to_rgba_row::<HOST_NATIVE_BE>(
        binned,
        &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
        ow,
        use_simd,
      );
    }
    if need_narrow {
      let nrow = &mut narrow[..3 * ow];
      // Stage the u8 RGB row once (drop alpha) via the direct path's
      // float→u8 clamp+scale; rgb / luma / luma_u16 / hsv all read it.
      crate::row::rgbaf32_to_rgb_row::<HOST_NATIVE_BE>(binned, nrow, ow, use_simd);
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      if let Some(buf) = luma.as_deref_mut() {
        crate::row::rgb_to_luma_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(buf) = luma_u16.as_deref_mut() {
        crate::row::rgb_to_luma_u16_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  Ok(())
}

/// Out-width **packed** `R, G, B, A` `half::f16` staging for the
/// [`Rgbaf16`](crate::source::Rgbaf16) resample tail — the 4-channel twin
/// of [`rgb_packed_f16_scratch`]. Grows `scratch` to `4 * width`
/// `half::f16` under the planner's recoverable-allocation contract.
#[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
pub(super) fn rgba_packed_f16_scratch<'s>(
  scratch: &'s mut Vec<half::f16>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [half::f16], MixedSinkerError> {
  let row = width
    .checked_mul(4)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.out_h(),
      4,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, half::f16::ZERO);
  }
  Ok(&mut scratch[..row])
}

/// Feeds the prepared source-width packed `R, G, B, A` `f32` row (the
/// [`Rgbaf16`](crate::source::Rgbaf16) wire widened f16 -> host-native f32)
/// into the 4-channel `f32` stream and derives every attached output by
/// **rounding** the binned packed `f32` row to `half::f16`
/// ([`rgba_packed_f16_scratch`]) and running the exact direct `rgbaf16_*`
/// kernels — so every output is byte-identical to a direct `Rgbaf16`
/// conversion of the `f32` block-mean rounded to f16 (the parity oracle).
/// The 4-channel twin of [`packed_rgb_f16_resample_emit`], extended with the
/// `rgba_f16` / `rgba_f32` outputs. The f32-derived `rgb_f32` / `rgba_f32`
/// outputs widen the **rounded** f16 row back (matching the direct path's
/// f16->f32 widen). The rounded packed f16 row is host-native, so every
/// `rgbaf16_*` kernel runs `::<HOST_NATIVE_BE>`.
#[cfg(all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb")))]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_f16_resample_emit(
  stream: Option<&mut impl crate::resample::RowResampler<f32>>,
  plan: &ResamplePlan,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  src_rgba: &[f32],
  packed_scratch_f16: &mut Vec<half::f16>,
  narrow_scratch: &mut Vec<u8>,
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
  let ow = plan.out_w();
  let need_round = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  let need_narrow = rgb.is_some() || luma.is_some() || luma_u16.is_some() || hsv.is_some();
  let packed_f16: &mut [half::f16] = if need_round {
    rgba_packed_f16_scratch(packed_scratch_f16, ow, plan)?
  } else {
    &mut []
  };
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  // Pregrow phase (RFC #238 tail-collapse): a `None` stream grows the emit-owned
  // scratch above and returns, so the combined helper can pre-grow it BEFORE the
  // freeze; the real feed passes `Some`. Nothing fallible runs after the freeze.
  let Some(stream) = stream else {
    return Ok(());
  };
  stream.feed_row(idx, src_rgba, use_simd, |oy, binned| {
    if need_round {
      // Round the binned packed `R, G, B, A` `f32` row to the packed f16
      // row — the exact layout the direct `rgbaf16_*` kernels consume.
      let prow = &mut packed_f16[..4 * ow];
      for (dst, &src) in prow.iter_mut().zip(binned.iter()) {
        *dst = half::f16::from_f32(src);
      }
      let prow = &prow[..4 * ow];

      // f16-native lossless outputs (consume the rounded packed f16 row).
      if let Some(buf) = rgba_f16.as_deref_mut() {
        crate::row::rgbaf16_to_rgba_f16_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb_f16.as_deref_mut() {
        crate::row::rgbaf16_to_rgb_f16_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      // f32 widen of the **rounded** f16 row (matches the direct path's
      // f16->f32 widen of its source, NOT the raw f32 bin).
      if let Some(buf) = rgba_f32.as_deref_mut() {
        crate::row::rgbaf16_to_rgba_f32_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb_f32.as_deref_mut() {
        crate::row::rgbaf16_to_rgb_f32_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb_u16.as_deref_mut() {
        crate::row::rgbaf16_to_rgb_u16_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba_u16.as_deref_mut() {
        crate::row::rgbaf16_to_rgba_u16_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba.as_deref_mut() {
        crate::row::rgbaf16_to_rgba_row::<HOST_NATIVE_BE>(
          prow,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if need_narrow {
        let nrow = &mut narrow[..3 * ow];
        crate::row::rgbaf16_to_rgb_row::<HOST_NATIVE_BE>(prow, nrow, ow, use_simd);
        if let Some(buf) = rgb.as_deref_mut() {
          buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
        }
        if let Some(buf) = luma.as_deref_mut() {
          crate::row::rgb_to_luma_row(
            nrow,
            &mut buf[oy * ow..(oy + 1) * ow],
            ow,
            matrix,
            full_range,
            use_simd,
          );
        }
        if let Some(buf) = luma_u16.as_deref_mut() {
          crate::row::rgb_to_luma_u16_row(
            nrow,
            &mut buf[oy * ow..(oy + 1) * ow],
            ow,
            matrix,
            full_range,
            use_simd,
          );
        }
        if let Some(hsv) = hsv.as_mut() {
          let (h, s, v) = hsv.hsv();
          crate::row::rgb_to_hsv_row(
            nrow,
            &mut h[oy * ow..(oy + 1) * ow],
            &mut s[oy * ow..(oy + 1) * ow],
            &mut v[oy * ow..(oy + 1) * ow],
            ow,
            use_simd,
          );
        }
      }
    }
  })?;
  Ok(())
}

/// Feeds the prepared source-width packed `R, G, B` `f32` row (the
/// [`Gbrpf32`](crate::source::Gbrpf32) planes scattered into RGB order)
/// into the prepared float stream — area or signed-coefficient filter,
/// both [`RowResampler<f32>`](crate::resample::RowResampler) — and derives
/// every attached output from each finalized output row. The `rgb-float`
/// ([`Rgbf32`]) tail's
/// per-row `rgbf32_*` clamp/scale kernels are not compiled in a `gbr`
/// build, so this tail de-interleaves each binned packed row back into
/// G/B/R planes once and runs the **exact direct `gbrpf32_*` kernels** —
/// every output, `luma_u16` included, is therefore byte-identical to a
/// direct full-resolution `Gbrpf32` conversion of the binned frame (the
/// parity oracle). The binned row is host-native f32, so the kernels run
/// `::<false>`. `plane_scratch` holds the out-width G/B/R planes
/// (`[0..ow]` = G, `[ow..2ow]` = B, `[2ow..3ow]` = R); it is sized (and
/// its allocation failure risked) only when an output that reads the
/// per-row `rgbf32_*` clamp/scale kernels are not compiled in a `gbr`
/// build, so this tail de-interleaves each binned packed row back into
/// G/B/R planes once and runs the **exact direct `gbrpf32_*` kernels** —
/// every output, `luma_u16` included, is therefore byte-identical to a
/// direct full-resolution `Gbrpf32` conversion of the binned frame (the
/// parity oracle). The binned row is host-native f32, so the kernels run
/// `::<false>`. `plane_scratch` holds the out-width G/B/R planes
/// (`[0..ow]` = G, `[ow..2ow]` = B, `[2ow..3ow]` = R); it is sized (and
/// its allocation failure risked) only when an output that reads the
/// planes is attached, so an `rgb_f32`-only sink neither grows it nor
/// risks its allocation. `rgb_f32` copies the binned row directly,
/// bypassing the planes. The lossless `rgba_f32` and the half-float
/// `rgb_f16` / `rgba_f16` outputs derive from the same de-interleaved
/// G/B/R planes via the direct `gbrpf32_to_rgba_f32_row` /
/// `gbrpf32_to_rgb_f16_row` / `gbrpf32_to_rgba_f16_row` kernels, so they
/// too are byte-identical to a direct `Gbrpf32` conversion of the binned
/// frame.
#[cfg(feature = "gbr")]
#[allow(clippy::too_many_arguments)]
pub(super) fn planar_gbr_f32_resample_emit(
  stream: Option<&mut impl crate::resample::RowResampler<f32>>,
  plan: &ResamplePlan,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  src_f32: &[f32],
  plane_scratch: &mut Vec<f32>,
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  let ow = plan.out_w();
  // Every output but `rgb_f32` derives from the de-interleaved G/B/R
  // planes; an `rgb_f32`-only sink copies the binned row and never sizes
  // the plane scratch. The predicate gates both the sizing here and the
  // de-interleave in the closure, so they cannot drift. `rgba_f32` and
  // the f16 outputs run their direct `gbrpf32_*` kernels over the same
  // planes (byte-identical to the direct path), so they join the gate.
  let need_planes = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  let planes: &mut [f32] = if need_planes {
    rgb_plane_f32_scratch(plane_scratch, ow, plan)?
  } else {
    &mut []
  };
  // Pregrow phase (RFC #238 tail-collapse): a `None` stream grows the emit-owned
  // plane scratch above and returns, so the combined helper can pre-grow it BEFORE
  // the freeze; the real feed passes `Some`. Nothing fallible runs after the freeze.
  let Some(stream) = stream else {
    return Ok(());
  };
  stream.feed_row(idx, src_f32, use_simd, |oy, binned| {
    // Lossless float pass-through — copy the binned packed row verbatim
    // (the direct path's `gbrpf32_to_rgb_f32_row` over host-native data
    // is a plain interleave; the binned row is already that interleave).
    if let Some(buf) = rgb_f32.as_deref_mut() {
      buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(binned);
    }
    if need_planes {
      // De-interleave the binned packed `R, G, B` row into G/B/R planes
      // — the exact plane layout the direct `gbrpf32_*` kernels consume.
      let (g, rest) = planes.split_at_mut(ow);
      let (b, r) = rest.split_at_mut(ow);
      for x in 0..ow {
        r[x] = binned[x * 3];
        g[x] = binned[x * 3 + 1];
        b[x] = binned[x * 3 + 2];
      }
      let g = &g[..ow];
      let b = &b[..ow];
      let r = &r[..ow];
      // The de-interleaved planes hold host-native f32 (the binned row was
      // decoded to host order during scatter). The `gbrpf32_*` kernels take
      // a wire-endian const and byte-swap when it differs from the host, so
      // pass the host's own endianness to make the load a no-op — otherwise
      // a big-endian target would corrupt every plane-derived output.
      const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
      if let Some(buf) = rgb_u16.as_deref_mut() {
        crate::row::gbrpf32_to_rgb_u16_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba_u16.as_deref_mut() {
        crate::row::gbrpf32_to_rgba_u16_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      // Lossless packed `f32` RGBA — alpha forced to 1.0 (the direct
      // `gbrpf32_to_rgba_f32_row`, which the binned planes feed verbatim).
      if let Some(buf) = rgba_f32.as_deref_mut() {
        crate::row::gbrpf32_to_rgba_f32_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      // f16 RGB / RGBA — fused f32->f16 narrow + interleave, exactly the
      // direct `gbrpf32_to_rgb_f16_row` / `gbrpf32_to_rgba_f16_row`.
      if let Some(buf) = rgb_f16.as_deref_mut() {
        crate::row::gbrpf32_to_rgb_f16_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba_f16.as_deref_mut() {
        crate::row::gbrpf32_to_rgba_f16_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb.as_deref_mut() {
        crate::row::gbrpf32_to_rgb_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba.as_deref_mut() {
        crate::row::gbrpf32_to_rgba_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = luma.as_deref_mut() {
        crate::row::gbrpf32_to_luma_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(buf) = luma_u16.as_deref_mut() {
        crate::row::gbrpf32_to_luma_u16_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::gbrpf32_to_hsv_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  Ok(())
}

/// Feeds the prepared source-width packed `R, G, B` `f32` row (the
/// [`Gbrpf16`](crate::source::Gbrpf16) planes widened f16 -> host-native
/// f32 and scattered into RGB order) into the prepared float stream — area
/// or signed-coefficient filter, both
/// [`RowResampler<f32>`](crate::resample::RowResampler) — and derives every
/// attached output from each finalized output row.
///
/// There is no `AreaStream<f16>` / `FilterStream<f16>`, so binning runs in
/// `f32` for precision. Per finalized output row this tail de-interleaves the
/// binned packed row into `f32` G/B/R planes
/// ([`rgb_plane_f32_scratch`]), **rounds each element to `half::f16`**
/// (`half::f16::from_f32`) into the f16 planes ([`rgb_plane_f16_scratch`]:
/// `[0..ow]` = G, `[ow..2ow]` = B, `[2ow..3ow]` = R), then runs the
/// **exact direct `gbrpf16_*` kernels** over those f16 planes. The
/// result is therefore byte-identical to a direct full-resolution
/// `Gbrpf16` conversion of the frame whose per-pixel f16 G/B/R is the
/// `f32` area mean rounded to f16 (the parity oracle) — because the emit
/// performs the identical round-then-`gbrpf16_*`. The f16-native kernels
/// (`gbrpf16_to_rgb_f16_row` / `..._u16_row` / `..._row`) consume the
/// f16 planes directly; the outputs the direct path derives by widening
/// f16 -> f32 (`rgb_f32` / `rgba_f32` / `luma` / `luma_u16` / `hsv`) are
/// reproduced by widening the **rounded** f16 planes back to f32 (the
/// same lossless widen the direct path applies) and running the direct
/// `gbrpf32_*` kernels — so they too match the f16-rounded oracle, not
/// the raw f32 bin.
///
/// The rounded f16 planes hold **host-native** `half::f16` (rounded from
/// host-native binned f32), so every `gbrpf16_*` kernel — which takes a
/// wire-endian const and byte-swaps when it differs from the host — is
/// invoked with `HOST_NATIVE_BE` to make its load a no-op on every host;
/// the widen of those planes back to f32 likewise uses `HOST_NATIVE_BE`.
/// Only the f16 planes are staged: the f32-derived outputs must consume
/// the **rounded** values (widened back from f16), not the raw f32 bin,
/// so no f32 plane scratch is needed — the round writes directly from the
/// binned packed row. `plane_scratch_f16` is sized (and its allocation
/// failure risked) only when an output is attached, so a no-output sink
/// never grows it.
#[cfg(feature = "gbr")]
#[allow(clippy::too_many_arguments)]
pub(super) fn planar_gbr_f16_resample_emit(
  stream: Option<&mut impl crate::resample::RowResampler<f32>>,
  plan: &ResamplePlan,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  src_f32: &[f32],
  plane_scratch_f16: &mut Vec<half::f16>,
  matrix: crate::KernelMatrix,
  full_range: bool,
  idx: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  use crate::row::scalar::planar_gbr_f16::widen_f16_be_to_host_f32;

  // The rounded f16 planes (and the f32 planes they round from) hold
  // host-native data — the binned row was decoded to host order during
  // scatter, then rounded with `from_f32`, which yields host-native
  // `half::f16`. The `gbrpf16_*` kernels take a wire-endian const and
  // byte-swap when it differs from the host, so pass the host's own
  // endianness to make every plane load a no-op; otherwise a big-endian
  // target would corrupt every output. The widen-back to f32 for the
  // f32-derived outputs uses the same const for the same reason.
  const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
  // Chunk size for the f16 -> f32 widen-back of the rounded planes, used
  // only by the outputs whose direct path widens f16 -> f32 (rgb_f32 /
  // rgba_f32 / luma / luma_u16 / hsv). Matches the dispatch layer's
  // widening chunk so the f32 staging is stack-resident.
  const WIDEN_CHUNK: usize = 64;

  let ow = plan.out_w();
  // Every output derives from the rounded f16 planes; even `rgb_f32`
  // does, because the direct `Gbrpf16` path widens the f16 source to f32
  // (so the oracle's `rgb_f32` is the f32 bin rounded to f16, then
  // widened — not the raw f32 bin). The predicate gates both the sizing
  // here and the de-interleave/round in the closure, so they cannot
  // drift; a sink with no output sizes nothing.
  let need_planes = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  // Allocate the f16 plane scratch up front (recoverable) before the
  // stream's closure writes any caller buffer, so an allocation refusal
  // never leaves a partially written output.
  let planes_f16: &mut [half::f16] = if need_planes {
    rgb_plane_f16_scratch(plane_scratch_f16, ow, plan)?
  } else {
    &mut []
  };
  // Pregrow phase (RFC #238 tail-collapse): a `None` stream grows the emit-owned f16
  // plane scratch above and returns, so the combined helper can pre-grow it BEFORE
  // the freeze; the real feed passes `Some`. Nothing fallible runs after the freeze.
  let Some(stream) = stream else {
    return Ok(());
  };
  stream.feed_row(idx, src_f32, use_simd, |oy, binned| {
    if need_planes {
      // De-interleave the binned packed `R, G, B` row directly into the
      // G/B/R f16 planes, **rounding** each element to `half::f16` — the
      // exact plane layout the direct `gbrpf16_*` kernels consume,
      // holding the f32 block mean rounded to f16. (No f32 plane stage:
      // the f32-derived outputs must consume the rounded values, so they
      // widen these f16 planes back, never the raw bin.)
      let (g16, rest_f16) = planes_f16.split_at_mut(ow);
      let (b16, r16) = rest_f16.split_at_mut(ow);
      for x in 0..ow {
        r16[x] = half::f16::from_f32(binned[x * 3]);
        g16[x] = half::f16::from_f32(binned[x * 3 + 1]);
        b16[x] = half::f16::from_f32(binned[x * 3 + 2]);
      }
      let g16 = &g16[..ow];
      let b16 = &b16[..ow];
      let r16 = &r16[..ow];

      // ---- f16-native kernels: consume the rounded f16 planes directly,
      // exactly as the direct `Gbrpf16` path does ------------------------
      if let Some(buf) = rgb_f16.as_deref_mut() {
        crate::row::gbrpf16_to_rgb_f16_row::<HOST_NATIVE_BE>(
          g16,
          b16,
          r16,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba_f16.as_deref_mut() {
        crate::row::gbrpf16_to_rgba_f16_row::<HOST_NATIVE_BE>(
          g16,
          b16,
          r16,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb_u16.as_deref_mut() {
        crate::row::gbrpf16_to_rgb_u16_row::<HOST_NATIVE_BE>(
          g16,
          b16,
          r16,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba_u16.as_deref_mut() {
        crate::row::gbrpf16_to_rgba_u16_row::<HOST_NATIVE_BE>(
          g16,
          b16,
          r16,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb.as_deref_mut() {
        crate::row::gbrpf16_to_rgb_row::<HOST_NATIVE_BE>(
          g16,
          b16,
          r16,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba.as_deref_mut() {
        crate::row::gbrpf16_to_rgba_row::<HOST_NATIVE_BE>(
          g16,
          b16,
          r16,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }

      // ---- f32-derived outputs: the direct `Gbrpf16` path widens the
      // f16 source planes to f32 and runs the `gbrpf32_*` kernels, so
      // reproduce that exactly by widening the **rounded** f16 planes
      // back to f32 (chunked, stack-resident) and running the same
      // kernels — byte-identical to the f16-rounded oracle ---------------
      let need_wide_back = rgb_f32.is_some()
        || rgba_f32.is_some()
        || luma.is_some()
        || luma_u16.is_some()
        || hsv.is_some();
      if need_wide_back {
        let mut gw = [0.0f32; WIDEN_CHUNK];
        let mut bw = [0.0f32; WIDEN_CHUNK];
        let mut rw = [0.0f32; WIDEN_CHUNK];
        let mut off = 0;
        while off < ow {
          let n = (ow - off).min(WIDEN_CHUNK);
          // The rounded f16 planes are host-native; widen with the
          // host's own endianness so the bit-normalize is a no-op, then
          // run the `gbrpf32_*` kernels with `HOST_NATIVE_BE` (the same
          // post-widen routing the direct path uses).
          widen_f16_be_to_host_f32::<HOST_NATIVE_BE>(g16, off, &mut gw, n);
          widen_f16_be_to_host_f32::<HOST_NATIVE_BE>(b16, off, &mut bw, n);
          widen_f16_be_to_host_f32::<HOST_NATIVE_BE>(r16, off, &mut rw, n);
          let gwn = &gw[..n];
          let bwn = &bw[..n];
          let rwn = &rw[..n];
          let cps = oy * ow + off;
          let cpe = cps + n;
          if let Some(buf) = rgb_f32.as_deref_mut() {
            crate::row::gbrpf32_to_rgb_f32_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              &mut buf[cps * 3..cpe * 3],
              n,
              use_simd,
            );
          }
          if let Some(buf) = rgba_f32.as_deref_mut() {
            crate::row::gbrpf32_to_rgba_f32_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              &mut buf[cps * 4..cpe * 4],
              n,
              use_simd,
            );
          }
          if let Some(buf) = luma.as_deref_mut() {
            crate::row::gbrpf32_to_luma_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              &mut buf[cps..cpe],
              n,
              matrix,
              full_range,
              use_simd,
            );
          }
          if let Some(buf) = luma_u16.as_deref_mut() {
            crate::row::gbrpf32_to_luma_u16_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              &mut buf[cps..cpe],
              n,
              matrix,
              full_range,
              use_simd,
            );
          }
          if let Some(hsv) = hsv.as_mut() {
            let (h, s, v) = hsv.hsv();
            crate::row::gbrpf32_to_hsv_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              &mut h[cps..cpe],
              &mut s[cps..cpe],
              &mut v[cps..cpe],
              n,
              use_simd,
            );
          }
          off += n;
        }
      }
    }
  })?;
  Ok(())
}

// ---- Float planar GBR+alpha (Gbrapf32 / Gbrapf16) resample tails -------
//
// The float planar GBR+alpha sources scatter their G/B/R/A planes into a
// canonical source-width packed `R, G, B, A` f32 row and bin all four
// channels in float on a dedicated 4-channel `AreaStream<f32>` — the float
// analogue of the integer `packed_rgba_resample` / `packed_rgba_u16_resample`
// alpha tails. Per finalized output row the binned packed row is resolved to
// its straight form (a copy in `Straight`, an un-premultiply in
// `Premultiplied`), de-interleaved into G/B/R/A planes, and the exact direct
// `gbrapf32_*` (RGBA, real source α) / `gbrpf32_*` (RGB / luma / hsv, α
// dropped) kernels run — so every output is byte-identical to a direct
// `Gbrapf32` conversion of the binned frame (the parity oracle). The
// `rgb-float` (`Rgbf32`) tail's packed `rgbf32_*` kernels are not compiled in
// a `gbr` build, hence the dedicated planar emit. These are GBR-only (there
// is no packed-float RGBA source), so they are gated to `gbr`.

/// Source-width canonical `R, G, B, A` `f32` staging for the float planar
/// GBR+alpha resample tail. Grows `scratch` to `4 * width` `f32` under the
/// planner's recoverable-allocation contract. Mirrors
/// [`source_rgb_f32_scratch`] for the 4-channel RGBA row.
#[cfg(any(
  feature = "gbr",
  all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
))]
pub(super) fn source_rgba_f32_scratch<'s>(
  scratch: &'s mut Vec<f32>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [f32], MixedSinkerError> {
  // Single-shot grow failpoint (RFC #238 tail-collapse tests): refuse this grow as
  // a host OOM would, proving a 4-channel `f32` scratch OOM AFTER a successful
  // stream build leaves the stream field `None` AND `resample_outputs` uncommitted
  // (retryable). The 4-channel twin of the `source_rgb_f32_scratch` failpoint, so a
  // packed-RGBA-float atomicity test can fail this specific pre-commit grow.
  #[cfg(all(
    test,
    feature = "std",
    any(
      feature = "gbr",
      all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
    )
  ))]
  if FORCE_SOURCE_RGBA_F32_SCRATCH_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
    )));
  }
  let row = width
    .checked_mul(4)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.src_h(),
      4,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0.0);
  }
  Ok(&mut scratch[..row])
}

// Single-shot source-RGBA-`f32`-scratch grow failpoint for the RFC #238
// tail-collapse transactional-preflight tests. Gated on the packed-RGBA-float /
// planar-GBR-alpha families that stage a source-width `f32` RGBA row pre-commit;
// `source_rgba_f32_scratch` consults it under the same gate. Separate from the
// `try_box` box-alloc failpoint so a 4-channel float test can fail the SCRATCH
// grow specifically (after a successful stream construction, before the
// stream-field insert).
#[cfg(all(
  test,
  feature = "std",
  any(
    feature = "gbr",
    all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
  )
))]
std::thread_local! {
  static FORCE_SOURCE_RGBA_F32_SCRATCH_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}

/// Arms a single-shot failpoint that makes the next [`source_rgba_f32_scratch`]
/// grow refuse with `AllocationFailed`, exactly as a host OOM would. Proves the
/// commit-together atomic shape of the packed-RGBA-float area / filter tails (a
/// source-staging OOM after a successful stream build leaves the stream field
/// `None` AND `resample_outputs` uncommitted, so the row is retryable). Test-only.
#[cfg(all(
  test,
  feature = "std",
  all(feature = "rgb-float", any(feature = "yuv-planar", feature = "rgb"))
))]
pub(crate) fn arm_source_rgba_f32_scratch_failure() {
  FORCE_SOURCE_RGBA_F32_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// Out-width canonical `R, G, B, A` `f32` staging for the straight-color
/// resolve of the float planar GBR+alpha tail (a copy in `Straight`, an
/// un-premultiply in `Premultiplied`). Grows `scratch` to `4 * out_width`
/// `f32`. Mirrors [`source_rgba_f32_scratch`] sized to the output width.
#[cfg(feature = "gbr")]
pub(super) fn out_rgba_f32_scratch<'s>(
  scratch: &'s mut Vec<f32>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [f32], MixedSinkerError> {
  let row = width
    .checked_mul(4)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.out_h(),
      4,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0.0);
  }
  Ok(&mut scratch[..row])
}

/// Out-width G/B/R/A `f32` plane staging for the [`Gbrapf32`](crate::source::Gbrapf32)
/// arm of the float planar GBR+alpha tail: the resolved straight packed
/// row de-interleaves into four contiguous planes (`[0..ow]` = G,
/// `[ow..2ow]` = B, `[2ow..3ow]` = R, `[3ow..4ow]` = A) so the exact direct
/// `gbrapf32_*` / `gbrpf32_*` kernels can run. Grows `scratch` to
/// `4 * width` `f32` under the planner's recoverable-allocation contract.
#[cfg(feature = "gbr")]
pub(super) fn rgba_plane_f32_scratch<'s>(
  scratch: &'s mut Vec<f32>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [f32], MixedSinkerError> {
  let row = width
    .checked_mul(4)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.out_h(),
      4,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0.0);
  }
  Ok(&mut scratch[..row])
}

/// Out-width G/B/R/A `half::f16` plane staging for the
/// [`Gbrapf16`](crate::source::Gbrapf16) arm of the float planar GBR+alpha
/// tail. There is no `AreaStream<f16>`, so binning runs in `f32`; this emit
/// de-interleaves the resolved straight packed `f32` row into `f32`,
/// **rounds** each element to `half::f16` into these planes (same layout as
/// [`rgba_plane_f32_scratch`]), and runs the exact direct `gbrapf16_*` /
/// `gbrpf16_*` kernels. Grows `scratch` to `4 * width` `half::f16`.
#[cfg(feature = "gbr")]
pub(super) fn rgba_plane_f16_scratch<'s>(
  scratch: &'s mut Vec<half::f16>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [half::f16], MixedSinkerError> {
  let row = width
    .checked_mul(4)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.out_h(),
      4,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, half::f16::ZERO);
  }
  Ok(&mut scratch[..row])
}

/// Premultiplies one canonical `R, G, B, A` `f32` row in place: each color
/// channel becomes `c * α` (α the raw plane value, normalized 0..1 by the
/// source); α is left unchanged. The float analogue of
/// [`premultiply_rgba_row_in_place`] — the exact op the
/// [`AlphaMode::Premultiplied`] float oracle mirrors (`R' = R * A`), so the
/// binned-then-un-premultiplied output matches the convert-then-bin oracle.
#[cfg(feature = "gbr")]
#[cfg_attr(not(tarpaulin), inline(always))]
fn premultiply_rgba_f32_row_in_place(row: &mut [f32], width: usize) {
  for px in row[..width * 4].as_chunks_mut::<4>().0 {
    let a = px[3];
    for c in &mut px[..3] {
      *c *= a;
    }
  }
}

/// Un-premultiplied straight color channel for one premultiplied binned
/// `f32` value: `pm / α`, or `0.0` when `α == 0` (a fully-transparent binned
/// pixel exposes no color, so it cannot bleed). The float inverse of
/// [`unpremultiply_channel`].
#[cfg(feature = "gbr")]
#[cfg_attr(not(tarpaulin), inline(always))]
fn unpremultiply_channel_f32(pm: f32, a: f32) -> f32 {
  if a == 0.0 { 0.0 } else { pm / a }
}

/// Resolves one binned canonical `R, G, B, A` `f32` row to its straight form
/// in `dst` (α copied through): a verbatim copy under [`AlphaMode::Straight`],
/// an un-premultiply (`R = pm / α`, `α == 0 -> 0`) under
/// [`AlphaMode::Premultiplied`]. The float twin of
/// [`unpremultiply_binned_rgba_into`], used as the single straight-color row
/// every output then reads.
#[cfg(feature = "gbr")]
#[cfg_attr(not(tarpaulin), inline(always))]
fn resolve_straight_rgba_f32_into(binned: &[f32], dst: &mut [f32], width: usize, premult: bool) {
  if !premult {
    dst[..width * 4].copy_from_slice(&binned[..width * 4]);
    return;
  }
  for (out_px, in_px) in dst[..width * 4]
    .as_chunks_mut::<4>()
    .0
    .iter_mut()
    .zip(binned[..width * 4].as_chunks::<4>().0.iter())
  {
    let a = in_px[3];
    for c in 0..3 {
      out_px[c] = unpremultiply_channel_f32(in_px[c], a);
    }
    out_px[3] = a;
  }
}

/// Row-stage fused downscale for the float planar GBR+alpha family
/// ([`Gbrapf32`](crate::source::Gbrapf32)) — the alpha-aware 4-channel f32
/// analogue of the 3-channel [`planar_gbr_f32_resample_emit`]. `convert_rgba`
/// stages the G/B/R/A planes as a canonical source-width packed `R, G, B, A`
/// f32 row (lossless interleave, host-native); this tail bins all four
/// channels so resampled alpha is a real area mean, then per finalized output
/// row resolves the straight color and de-interleaves it into G/B/R/A planes,
/// running the exact direct `gbrapf32_*` (RGBA) / `gbrpf32_*` (RGB / luma /
/// hsv, α dropped) kernels — every output byte-identical to a direct
/// `Gbrapf32` conversion of the binned frame.
///
/// Under [`AlphaMode::Premultiplied`] the staged row is premultiplied in
/// place (`R' = R * A`) before binning and un-premultiplied per output row
/// (`R = mean(R*A) / mean(A)`, `α == 0 -> RGB = 0`), so color outputs are
/// alpha-weighted and transparent pixels never bleed.
///
/// Atomic conditional-ordering preflight identical to
/// [`packed_rgba_resample`]: a no-output call returns before any freeze; an
/// out-of-sequence first row is rejected before the freeze; a later-row
/// output change trips `ResampleOutputsChanged`; the stream and every scratch
/// are created after the sequence check and before the single feed. The
/// alpha-mode freeze itself is checked by the caller's
/// [`check_frozen_alpha_mode`] before route selection (mirroring the integer
/// alpha tails). The binned row is host-native f32 (the scatter decoded the
/// source to host order), so the emit kernels run `::<HOST_NATIVE_BE>` — no
/// further byte swap.
#[cfg(feature = "gbr")]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_f32_resample(
  rgba_stream_f32: &mut Option<std::boxed::Box<crate::resample::AreaStream<f32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<f32>,
  color_scratch: &mut Vec<f32>,
  plane_scratch: &mut Vec<f32>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  alpha_mode: AlphaMode,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba: impl FnOnce(&mut [f32]),
) -> Result<(), MixedSinkerError> {
  // Area-only sink (the planar-GBR-alpha float RGBA is not routed to the filter
  // path): reject a filter plan before any work. A premultiplied filter plan is
  // routed here by the caller (the filter engine cannot un-premultiply), so it
  // surfaces the typed `UnsupportedFilter` here; straight filter plans take the
  // dedicated `packed_rgba_f32_filter_resample`.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  let expected = if need_any {
    Some(rgba_stream_f32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible stream alloc + scratch growth below succeed (RFC #238
  // tail-collapse), so a first-row allocation failure leaves `resample_outputs`
  // uncommitted — retryable with a changed output attachment.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  let premult = alpha_mode.is_premultiplied();
  // Every output but `rgba_f32` reads the de-interleaved G/B/R/A planes;
  // an `rgba_f32`-only sink copies the resolved straight row directly and
  // sizes no plane scratch. The RGB-only outputs (rgb / rgb_u16 / rgb_f16 /
  // luma / luma_u16 / hsv) drop α via the `gbrpf32_*` kernels over the G/B/R
  // planes; the RGBA outputs (rgba / rgba_u16 / rgba_f16) run `gbrapf32_*`.
  let need_planes = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  // Build the missing stream into a LOCAL and grow every pre-feed scratch (the
  // always-sized out-width straight-colour row, the conditional out-width G/B/R/A
  // plane row, and the source-width RGBA staging) with NO field mutation, so the
  // stream field insert and the freeze commit TOGETHER only after they all succeed
  // (the `planar_dual_resample` atomic shape: a scratch OOM leaves `rgba_stream_f32`
  // `None` AND `resample_outputs` uncommitted, never a partial commit). The emit's
  // grows re-run as no-ops after these pre-commit grows.
  let new_stream = if rgba_stream_f32.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  out_rgba_f32_scratch(color_scratch, ow, plan)?;
  if need_planes {
    rgba_plane_f32_scratch(plane_scratch, ow, plan)?;
  }
  source_rgba_f32_scratch(rgba_scratch, w, plan)?;
  if let Some(s) = new_stream {
    *rgba_stream_f32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    idx,
  )?;
  let stream = rgba_stream_f32.as_mut().expect("created above");
  packed_rgba_f32_emit(
    stream,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
    rgba_scratch,
    color_scratch,
    plane_scratch,
    w,
    plan,
    idx,
    use_simd,
    premult,
    need_planes,
    matrix,
    full_range,
    convert_rgba,
  )
}

/// Shared per-output emit for the 4-channel float planar GBR+alpha tail —
/// the body both [`packed_rgba_f32_resample`] (area) and
/// [`packed_rgba_f32_filter_resample`] (filter) run after creating their
/// kind-appropriate [`RowResampler<f32>`](crate::resample::RowResampler).
/// Stages the canonical source-width `R, G, B, A` `f32` row (`convert_rgba`),
/// premultiplies it in place when `premult` (area-only; the filter path is
/// straight-only and passes `premult = false`), feeds it once, and per
/// finalized output row resolves the straight color and de-interleaves it
/// into G/B/R/A planes to run the exact direct `gbrapf32_*` (RGBA) /
/// `gbrpf32_*` (RGB / luma / hsv, α dropped) kernels — so every output is
/// byte-identical between the area and filter paths' shared derivation.
///
/// The binned row is host-native f32 (the scatter decoded the source to host
/// order), so the emit kernels run `::<HOST_NATIVE_BE>`. `need_planes`
/// (computed identically by both callers) gates the plane scratch sizing and
/// the de-interleave, so an `rgba_f32`-only sink copies the resolved straight
/// row directly and sizes no plane scratch.
#[cfg(feature = "gbr")]
#[allow(clippy::too_many_arguments)]
fn packed_rgba_f32_emit(
  stream: &mut impl crate::resample::RowResampler<f32>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<f32>,
  color_scratch: &mut Vec<f32>,
  plane_scratch: &mut Vec<f32>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  premult: bool,
  need_planes: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba: impl FnOnce(&mut [f32]),
) -> Result<(), MixedSinkerError> {
  const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
  let ow = plan.out_w();
  // Resolved straight RGBA color row (per output row); always sized when any
  // output is attached so every output reads one canonical straight row.
  let color = out_rgba_f32_scratch(color_scratch, ow, plan)?;
  let planes: &mut [f32] = if need_planes {
    rgba_plane_f32_scratch(plane_scratch, ow, plan)?
  } else {
    &mut []
  };
  let src_rgba = source_rgba_f32_scratch(rgba_scratch, w, plan)?;
  convert_rgba(src_rgba);
  if premult {
    premultiply_rgba_f32_row_in_place(src_rgba, w);
  }
  stream.feed_row(idx, src_rgba, use_simd, |oy, binned| {
    // Resolve the per-mode straight RGBA once (copy for straight,
    // un-premultiply for premult), then derive every output from it.
    let color = &mut color[..4 * ow];
    resolve_straight_rgba_f32_into(binned, color, ow, premult);
    // Lossless packed `f32` RGBA — copy the resolved straight row verbatim
    // (the direct `gbrapf32_to_rgba_f32_row` over host-native planes is a
    // plain interleave; the resolved row is already that interleave).
    if let Some(buf) = rgba_f32.as_deref_mut() {
      buf[oy * 4 * ow..(oy + 1) * 4 * ow].copy_from_slice(color);
    }
    if need_planes {
      // De-interleave the resolved straight `R, G, B, A` row into G/B/R/A
      // planes — the exact plane layout the direct kernels consume.
      let (g, rest) = planes.split_at_mut(ow);
      let (b, rest) = rest.split_at_mut(ow);
      let (r, a) = rest.split_at_mut(ow);
      for x in 0..ow {
        r[x] = color[x * 4];
        g[x] = color[x * 4 + 1];
        b[x] = color[x * 4 + 2];
        a[x] = color[x * 4 + 3];
      }
      let g = &g[..ow];
      let b = &b[..ow];
      let r = &r[..ow];
      let a = &a[..ow];
      // RGBA outputs carry the resolved straight α via the `gbrapf32_*`
      // kernels (real source α, clamp+scale for the integer/u8 forms,
      // lossless for f16).
      if let Some(buf) = rgba_u16.as_deref_mut() {
        crate::row::gbrapf32_to_rgba_u16_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          a,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba_f16.as_deref_mut() {
        crate::row::gbrapf32_to_rgba_f16_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          a,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba.as_deref_mut() {
        crate::row::gbrapf32_to_rgba_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          a,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      // RGB / luma / hsv outputs drop α via the `gbrpf32_*` kernels over the
      // G/B/R planes — identical to the 3-channel emit's source-of-truth.
      if let Some(buf) = rgb_u16.as_deref_mut() {
        crate::row::gbrpf32_to_rgb_u16_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb_f16.as_deref_mut() {
        crate::row::gbrpf32_to_rgb_f16_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb_f32.as_deref_mut() {
        crate::row::gbrpf32_to_rgb_f32_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb.as_deref_mut() {
        crate::row::gbrpf32_to_rgb_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = luma.as_deref_mut() {
        crate::row::gbrpf32_to_luma_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(buf) = luma_u16.as_deref_mut() {
        crate::row::gbrpf32_to_luma_u16_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          matrix,
          full_range,
          use_simd,
        );
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::gbrpf32_to_hsv_row::<HOST_NATIVE_BE>(
          g,
          b,
          r,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    } else if rgb_f32.as_deref().is_some() {
      // rgb_f32 with no other plane-derived output: drop α straight from the
      // resolved color (the direct `gbrpf32_to_rgb_f32_row` is a plain
      // interleave of host-native planes; here a strided copy of R/G/B).
      let buf = rgb_f32.as_deref_mut().unwrap();
      let dst = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
      for x in 0..ow {
        dst[x * 3] = color[x * 4];
        dst[x * 3 + 1] = color[x * 4 + 1];
        dst[x * 3 + 2] = color[x * 4 + 2];
      }
    }
  })?;
  Ok(())
}

/// Separable-filter fused resize for the float planar GBR+alpha family
/// ([`Gbrapf32`](crate::source::Gbrapf32)) — the
/// [`SpanKind::Filter`](crate::resample::SpanKind) twin of the area
/// [`packed_rgba_f32_resample`], and the 4-channel float analogue of the
/// 3-channel filter path. PIL filters R, G, B, A **independently with no
/// premultiplication**, so the staged canonical `R, G, B, A` `f32` row bins
/// through one 4-channel [`FilterStream<f32>`](crate::resample::FilterStream)
/// and resampled alpha is a real filtered channel (never forced opaque).
/// Each finalized output row then resolves the straight color and runs the
/// exact direct `gbrapf32_*` / `gbrpf32_*` kernels through the shared
/// [`packed_rgba_f32_emit`], so every output is byte-identical to the area
/// path's derivation of the same binned color.
///
/// There is **no native-depth clamp**: `f32` is full-range, so the lossless
/// `rgb_f32` / `rgba_f32` outputs preserve a signed-coefficient overshoot
/// (HDR > 1 and negatives) by design, exactly as the area path and `Rgbf32`
/// do, while the integer / f16 outputs clamp `[0, 1]` in their own narrows —
/// so an overshoot cannot wrap them. (Contrast the high-bit u16 filter tail,
/// which clamps to the native max before any narrow.)
///
/// There is **no premultiplied route** — the filter engine cannot
/// un-premultiply, so a premultiplied frame stays on the area
/// [`packed_rgba_f32_resample`] (which un-premultiplies); this tail is reached
/// only for straight alpha and passes `premult = false`.
///
/// Sequence-check precedes every allocation (the 4-channel stream creation
/// runs after the no-output and out-of-sequence rejections), keeping the call
/// atomic: a rejected first row stores no frozen-output snapshot and
/// `AllocationFailed` never masks `OutOfSequenceRow` — identical to
/// [`packed_rgba_u16_filter_resample`]. The alpha-mode freeze itself is
/// checked by the caller's [`check_frozen_alpha_mode`] before route selection.
#[cfg(feature = "gbr")]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_f32_filter_resample(
  rgba_filter_stream_f32: &mut Option<std::boxed::Box<crate::resample::FilterStream<f32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<f32>,
  color_scratch: &mut Vec<f32>,
  plane_scratch: &mut Vec<f32>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba: impl FnOnce(&mut [f32]),
) -> Result<(), MixedSinkerError> {
  // Single-kernel filter tail — reject a BICUBLIN plan (its chroma windows are
  // read only by the `Yuv420p` per-plane route) before any state change.
  plan.ensure_single_kernel_filter()?;
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  let expected = if need_any {
    Some(rgba_filter_stream_f32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit): the output-set freeze commits only AFTER
  // the fallible stream alloc + scratch growth below succeed (RFC #238
  // tail-collapse), so a first-row allocation failure leaves `resample_outputs`
  // uncommitted — retryable with a changed output attachment.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Every output but `rgba_f32` reads the de-interleaved G/B/R/A planes
  // (computed identically to the area path); an `rgba_f32`-only sink copies
  // the resolved straight row directly and sizes no plane scratch.
  let need_planes = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  // Build the missing filter stream into a LOCAL and grow every pre-feed scratch
  // (the always-sized out-width straight-colour row, the conditional out-width
  // plane row, and the source-width RGBA staging) with NO field mutation, so the
  // stream field insert and the freeze commit TOGETHER (a scratch OOM leaves
  // `rgba_filter_stream_f32` `None` AND `resample_outputs` uncommitted).
  let new_stream = if rgba_filter_stream_f32.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  out_rgba_f32_scratch(color_scratch, ow, plan)?;
  if need_planes {
    rgba_plane_f32_scratch(plane_scratch, ow, plan)?;
  }
  source_rgba_f32_scratch(rgba_scratch, w, plan)?;
  if let Some(s) = new_stream {
    *rgba_filter_stream_f32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    idx,
  )?;
  let stream = rgba_filter_stream_f32.as_mut().expect("created above");
  // Straight alpha only (no premult route): bin → resolve (a verbatim copy) →
  // derive every output, sharing the area path's per-output emit so the two
  // paths' derivations cannot drift.
  packed_rgba_f32_emit(
    stream,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
    rgba_scratch,
    color_scratch,
    plane_scratch,
    w,
    plan,
    idx,
    use_simd,
    false,
    need_planes,
    matrix,
    full_range,
    convert_rgba,
  )
}

/// Row-stage fused downscale for the half-float planar GBR+alpha family
/// ([`Gbrapf16`](crate::source::Gbrapf16)) — the alpha-aware 4-channel
/// analogue of the 3-channel [`planar_gbr_f16_resample_emit`]. `convert_rgba`
/// stages the G/B/R/A planes (widened f16 -> host-native f32) as a canonical
/// source-width packed `R, G, B, A` f32 row; this tail bins all four channels
/// in `f32` (there is no `AreaStream<f16>`), then per finalized output row
/// resolves the straight color, de-interleaves it into G/B/R/A `half::f16`
/// planes **rounding** each element, and runs the exact direct `gbrapf16_*` /
/// `gbrpf16_*` kernels. The f32-derived outputs (rgb_f32 / rgba_f32 / luma /
/// luma_u16 / hsv) widen the **rounded** f16 planes back to f32, exactly as
/// the direct `Gbrapf16` path widens its f16 source — so every output is
/// byte-identical to a direct `Gbrapf16` conversion of the f32 block-mean
/// rounded to f16 (the parity oracle).
///
/// Premultiply / un-premultiply, the freeze ordering, the endian handling,
/// and the GBR-only gating match [`packed_rgba_f32_resample`]; the only
/// difference is the per-output round-to-f16 and widen-back.
#[cfg(feature = "gbr")]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_f16_resample(
  rgba_stream_f32: &mut Option<std::boxed::Box<crate::resample::AreaStream<f32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<f32>,
  color_scratch: &mut Vec<f32>,
  plane_scratch_f16: &mut Vec<half::f16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  alpha_mode: AlphaMode,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba: impl FnOnce(&mut [f32]),
) -> Result<(), MixedSinkerError> {
  // Area-only sink: reject a filter plan before any work. A premultiplied filter
  // plan is routed here by the caller (the filter engine cannot un-premultiply), so
  // it surfaces the typed `UnsupportedFilter` here; straight filter plans take the
  // dedicated `packed_rgba_f16_filter_resample`.
  if plan.kind().is_filter() {
    return Err(plan.unsupported_filter().into());
  }
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  let expected = if need_any {
    Some(rgba_stream_f32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit) — see `packed_rgba_f32_resample`.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  let premult = alpha_mode.is_premultiplied();
  // Every output derives from the rounded f16 planes (even rgb_f32 / rgba_f32,
  // because the direct `Gbrapf16` path widens its f16 source to f32).
  let need_planes = need_any;
  // Build the missing stream into a LOCAL and grow every pre-feed scratch (the
  // always-sized out-width straight-colour row, the conditional out-width f16 plane
  // row, and the source-width RGBA staging) with NO field mutation, so the stream
  // field insert and the freeze commit TOGETHER (a scratch OOM leaves
  // `rgba_stream_f32` `None` AND `resample_outputs` uncommitted).
  let new_stream = if rgba_stream_f32.is_none() {
    let stream =
      crate::resample::AreaStream::new(plan.h(), plan.v(), plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  out_rgba_f32_scratch(color_scratch, ow, plan)?;
  if need_planes {
    rgba_plane_f16_scratch(plane_scratch_f16, ow, plan)?;
  }
  source_rgba_f32_scratch(rgba_scratch, w, plan)?;
  if let Some(s) = new_stream {
    *rgba_stream_f32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    idx,
  )?;
  let stream = rgba_stream_f32.as_mut().expect("created above");
  packed_rgba_f16_emit(
    stream,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
    rgba_scratch,
    color_scratch,
    plane_scratch_f16,
    w,
    plan,
    idx,
    use_simd,
    premult,
    need_planes,
    matrix,
    full_range,
    convert_rgba,
  )
}

/// Shared per-output emit for the half-float planar GBR+alpha tail — the body
/// both [`packed_rgba_f16_resample`] (area) and
/// [`packed_rgba_f16_filter_resample`] (filter) run after creating their
/// kind-appropriate [`RowResampler<f32>`](crate::resample::RowResampler).
/// There is no `AreaStream<f16>` / `FilterStream<f16>`, so binning runs in
/// `f32`; per finalized output row this resolves the straight color,
/// de-interleaves it into G/B/R/A `half::f16` planes **rounding** each element,
/// and runs the exact direct `gbrapf16_*` / `gbrpf16_*` kernels (the f32-derived
/// outputs widen the **rounded** f16 planes back to f32, exactly as the direct
/// `Gbrapf16` path widens its f16 source) — so every output is byte-identical
/// between the area and filter paths and to a direct `Gbrapf16` conversion of
/// the f32 block-mean rounded to f16. The area path passes `premult` from the
/// frame's alpha mode; the filter path is straight-only and passes `false`.
#[cfg(feature = "gbr")]
#[allow(clippy::too_many_arguments)]
fn packed_rgba_f16_emit(
  stream: &mut impl crate::resample::RowResampler<f32>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<f32>,
  color_scratch: &mut Vec<f32>,
  plane_scratch_f16: &mut Vec<half::f16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  premult: bool,
  need_planes: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba: impl FnOnce(&mut [f32]),
) -> Result<(), MixedSinkerError> {
  use crate::row::scalar::planar_gbr_f16::widen_f16_be_to_host_f32;

  // The rounded f16 planes (and the f32 they widen back to) hold host-native
  // data — the binned row was decoded to host order during scatter, then
  // rounded with `from_f32`, which yields host-native `half::f16`. The
  // `gbrpf16_*` / `gbrapf16_*` kernels (and the widen-back `gbrpf32_*` /
  // `gbrapf32_*`) take a wire-endian const and byte-swap when it differs
  // from the host, so pass the host's own endianness to make every load a
  // no-op; otherwise a big-endian target would corrupt every output.
  const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
  // Chunk size for the f16 -> f32 widen-back of the rounded planes, matching
  // the dispatch layer's widening chunk so the f32 staging is stack-resident.
  const WIDEN_CHUNK: usize = 64;

  let ow = plan.out_w();
  let color = out_rgba_f32_scratch(color_scratch, ow, plan)?;
  let planes_f16: &mut [half::f16] = if need_planes {
    rgba_plane_f16_scratch(plane_scratch_f16, ow, plan)?
  } else {
    &mut []
  };
  let src_rgba = source_rgba_f32_scratch(rgba_scratch, w, plan)?;
  convert_rgba(src_rgba);
  if premult {
    premultiply_rgba_f32_row_in_place(src_rgba, w);
  }
  stream.feed_row(idx, src_rgba, use_simd, |oy, binned| {
    if need_planes {
      // Resolve the per-mode straight RGBA, then de-interleave it into the
      // G/B/R/A f16 planes, **rounding** each element to `half::f16` — the
      // exact plane layout the direct `gbrapf16_*` / `gbrpf16_*` kernels
      // consume, holding the f32 block mean rounded to f16.
      let color = &mut color[..4 * ow];
      resolve_straight_rgba_f32_into(binned, color, ow, premult);
      let (g16, rest) = planes_f16.split_at_mut(ow);
      let (b16, rest) = rest.split_at_mut(ow);
      let (r16, a16) = rest.split_at_mut(ow);
      for x in 0..ow {
        r16[x] = half::f16::from_f32(color[x * 4]);
        g16[x] = half::f16::from_f32(color[x * 4 + 1]);
        b16[x] = half::f16::from_f32(color[x * 4 + 2]);
        a16[x] = half::f16::from_f32(color[x * 4 + 3]);
      }
      let g16 = &g16[..ow];
      let b16 = &b16[..ow];
      let r16 = &r16[..ow];
      let a16 = &a16[..ow];

      // ---- f16-native kernels: the outputs the direct `Gbrapf16` path
      // derives straight from the f16 source (no widen) — `rgb_f16` /
      // `rgba_f16` (lossless / fused-narrow) and the u8 `rgb` / `rgba` RGB
      // (the α byte of `rgba` is overwritten from the widened α below,
      // mirroring the direct path's `gbrpf16_to_rgba_row` + α scatter) ---
      if let Some(buf) = rgba_f16.as_deref_mut() {
        crate::row::gbrapf16_to_rgba_f16_row::<HOST_NATIVE_BE>(
          g16,
          b16,
          r16,
          a16,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb_f16.as_deref_mut() {
        crate::row::gbrpf16_to_rgb_f16_row::<HOST_NATIVE_BE>(
          g16,
          b16,
          r16,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgba.as_deref_mut() {
        // RGB from the f16 source (α = 0xFF stub); the real α byte is
        // written from the widened α plane below, exactly as the direct
        // `Gbrapf16` path does (`gbrpf16_to_rgba_row` + α scatter).
        crate::row::gbrpf16_to_rgba_row::<HOST_NATIVE_BE>(
          g16,
          b16,
          r16,
          &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
          ow,
          use_simd,
        );
      }
      if let Some(buf) = rgb.as_deref_mut() {
        crate::row::gbrpf16_to_rgb_row::<HOST_NATIVE_BE>(
          g16,
          b16,
          r16,
          &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow],
          ow,
          use_simd,
        );
      }

      // ---- widen-back f32 outputs: the direct `Gbrapf16` path widens the
      // f16 source to f32 and runs the `gbrapf32_*` / `gbrpf32_*` kernels,
      // so reproduce that by widening the **rounded** f16 planes back to f32
      // (chunked, stack-resident) and running the same kernels —
      // byte-identical to the f16-rounded oracle. The `rgb_u16` / `rgba_u16`
      // and the u8 `rgba` α byte come from this same widened source (no
      // `gbrapf16_to_rgba_u16` / `..._row` kernel exists). -----------------
      let need_wide_back = rgb_u16.is_some()
        || rgba_u16.is_some()
        || rgb_f32.is_some()
        || rgba_f32.is_some()
        || rgba.is_some()
        || luma.is_some()
        || luma_u16.is_some()
        || hsv.is_some();
      if need_wide_back {
        let mut gw = [0.0f32; WIDEN_CHUNK];
        let mut bw = [0.0f32; WIDEN_CHUNK];
        let mut rw = [0.0f32; WIDEN_CHUNK];
        let mut aw = [0.0f32; WIDEN_CHUNK];
        let mut off = 0;
        while off < ow {
          let n = (ow - off).min(WIDEN_CHUNK);
          widen_f16_be_to_host_f32::<HOST_NATIVE_BE>(g16, off, &mut gw, n);
          widen_f16_be_to_host_f32::<HOST_NATIVE_BE>(b16, off, &mut bw, n);
          widen_f16_be_to_host_f32::<HOST_NATIVE_BE>(r16, off, &mut rw, n);
          widen_f16_be_to_host_f32::<HOST_NATIVE_BE>(a16, off, &mut aw, n);
          let gwn = &gw[..n];
          let bwn = &bw[..n];
          let rwn = &rw[..n];
          let awn = &aw[..n];
          let cps = oy * ow + off;
          let cpe = cps + n;
          if let Some(buf) = rgba_u16.as_deref_mut() {
            crate::row::gbrapf32_to_rgba_u16_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              awn,
              &mut buf[cps * 4..cpe * 4],
              n,
              use_simd,
            );
          }
          if let Some(buf) = rgb_u16.as_deref_mut() {
            crate::row::gbrpf32_to_rgb_u16_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              &mut buf[cps * 3..cpe * 3],
              n,
              use_simd,
            );
          }
          // Overwrite the u8 `rgba` α byte from the widened α plane — the
          // same clamp/scale `copy_alpha_plane_f32_to_u8` the direct path's
          // `widen_and_scatter_f16_alpha_to_u8` applies (host-native source).
          if let Some(buf) = rgba.as_deref_mut() {
            crate::row::scalar::alpha_extract::copy_alpha_plane_f32_to_u8::<HOST_NATIVE_BE>(
              awn,
              &mut buf[cps * 4..cpe * 4],
              n,
            );
          }
          if let Some(buf) = rgba_f32.as_deref_mut() {
            crate::row::gbrapf32_to_rgba_f32_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              awn,
              &mut buf[cps * 4..cpe * 4],
              n,
              use_simd,
            );
          }
          if let Some(buf) = rgb_f32.as_deref_mut() {
            crate::row::gbrpf32_to_rgb_f32_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              &mut buf[cps * 3..cpe * 3],
              n,
              use_simd,
            );
          }
          if let Some(buf) = luma.as_deref_mut() {
            crate::row::gbrpf32_to_luma_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              &mut buf[cps..cpe],
              n,
              matrix,
              full_range,
              use_simd,
            );
          }
          if let Some(buf) = luma_u16.as_deref_mut() {
            crate::row::gbrpf32_to_luma_u16_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              &mut buf[cps..cpe],
              n,
              matrix,
              full_range,
              use_simd,
            );
          }
          if let Some(hsv) = hsv.as_mut() {
            let (h, s, v) = hsv.hsv();
            crate::row::gbrpf32_to_hsv_row::<HOST_NATIVE_BE>(
              gwn,
              bwn,
              rwn,
              &mut h[cps..cpe],
              &mut s[cps..cpe],
              &mut v[cps..cpe],
              n,
              use_simd,
            );
          }
          off += n;
        }
      }
    }
  })?;
  Ok(())
}

/// Separable-filter fused resize for the half-float planar GBR+alpha family
/// ([`Gbrapf16`](crate::source::Gbrapf16)) — the
/// [`SpanKind::Filter`](crate::resample::SpanKind) twin of the area
/// [`packed_rgba_f16_resample`]. `convert_rgba` widens the G/B/R/A f16 planes
/// to host-native f32 and stages a canonical source-width packed `R, G, B, A`
/// f32 row; PIL filters the four channels independently (no premultiplication),
/// so they bin through one 4-channel
/// [`FilterStream<f32>`](crate::resample::FilterStream) (there is no
/// `FilterStream<f16>`) and resampled alpha is a real filtered channel. Each
/// finalized output row resolves the straight color, rounds the de-interleaved
/// G/B/R/A planes to `half::f16`, and runs the exact direct `gbrapf16_*` /
/// `gbrpf16_*` kernels through the shared [`packed_rgba_f16_emit`] — so every
/// output is byte-identical to the area path's derivation of the same binned
/// color rounded to f16.
///
/// No native-depth clamp (the f16 narrows clamp `[0, 1]`; the `rgb_f32` /
/// `rgba_f32` outputs are the lossless widen of the rounded f16, full-range by
/// design) and no premultiplied route (a premultiplied frame stays on the area
/// [`packed_rgba_f16_resample`]; this tail is reached only for straight alpha,
/// passing `premult = false`). The atomic conditional-ordering preflight is
/// identical to [`packed_rgba_f32_filter_resample`].
#[cfg(feature = "gbr")]
#[allow(clippy::too_many_arguments)]
pub(super) fn packed_rgba_f16_filter_resample(
  rgba_filter_stream_f32: &mut Option<std::boxed::Box<crate::resample::FilterStream<f32>>>,
  resample_outputs: &mut Option<FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  rgba_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgba_scratch: &mut Vec<f32>,
  color_scratch: &mut Vec<f32>,
  plane_scratch_f16: &mut Vec<half::f16>,
  w: usize,
  plan: &ResamplePlan,
  idx: usize,
  use_simd: bool,
  matrix: crate::KernelMatrix,
  full_range: bool,
  convert_rgba: impl FnOnce(&mut [f32]),
) -> Result<(), MixedSinkerError> {
  // Single-kernel filter tail — reject a BICUBLIN plan (its chroma windows are
  // read only by the `Yuv420p` per-plane route) before any state change.
  plan.ensure_single_kernel_filter()?;
  let ow = plan.out_w();
  let need_any = rgb.is_some()
    || rgba.is_some()
    || luma.is_some()
    || rgb_u16.is_some()
    || rgba_u16.is_some()
    || luma_u16.is_some()
    || rgb_f32.is_some()
    || rgba_f32.is_some()
    || rgb_f16.is_some()
    || rgba_f16.is_some()
    || hsv.is_some();
  let expected = if need_any {
    Some(rgba_filter_stream_f32.as_ref().map_or(0, |s| s.next_y()))
  } else {
    None
  };
  // Compare-only preflight (NO commit) — see `packed_rgba_f32_resample`.
  if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    expected,
    idx,
  )? {
    return Ok(());
  }
  // Every output derives from the rounded f16 planes (even rgb_f32 / rgba_f32,
  // because the direct `Gbrapf16` path widens its f16 source to f32).
  let need_planes = need_any;
  // Build the missing filter stream into a LOCAL and grow every pre-feed scratch
  // (the always-sized out-width straight-colour row, the conditional out-width f16
  // plane row, and the source-width RGBA staging) with NO field mutation, so the
  // stream field insert and the freeze commit TOGETHER (a scratch OOM leaves
  // `rgba_filter_stream_f32` `None` AND `resample_outputs` uncommitted).
  let new_stream = if rgba_filter_stream_f32.is_none() {
    let (fh, fv) = (
      plan
        .filter_h()
        .expect("filter plan carries horizontal windows"),
      plan
        .filter_v()
        .expect("filter plan carries vertical windows"),
    );
    let stream = crate::resample::FilterStream::new(fh, fv, plan.src_w(), plan.src_h(), 4)?;
    Some(crate::resample::try_box(stream).map_err(|_| {
      MixedSinkerError::Resample(crate::resample::ResampleError::AllocationFailed(
        crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
      ))
    })?)
  } else {
    None
  };
  out_rgba_f32_scratch(color_scratch, ow, plan)?;
  if need_planes {
    rgba_plane_f16_scratch(plane_scratch_f16, ow, plan)?;
  }
  source_rgba_f32_scratch(rgba_scratch, w, plan)?;
  if let Some(s) = new_stream {
    *rgba_filter_stream_f32 = Some(s);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    rgb_f32,
    rgba_f32,
    &None,
    rgb_f16,
    rgba_f16,
    hsv,
    &None,
    idx,
  )?;
  let stream = rgba_filter_stream_f32.as_mut().expect("created above");
  // Straight alpha only (no premult route): the shared f16 emit bins in f32,
  // resolves a verbatim straight copy, rounds to f16, and derives every output.
  packed_rgba_f16_emit(
    stream,
    rgb,
    rgba,
    luma,
    rgb_u16,
    rgba_u16,
    luma_u16,
    rgb_f32,
    rgba_f32,
    rgb_f16,
    rgba_f16,
    hsv,
    rgba_scratch,
    color_scratch,
    plane_scratch_f16,
    w,
    plan,
    idx,
    use_simd,
    false,
    need_planes,
    matrix,
    full_range,
    convert_rgba,
  )
}

// ---- Xyz12 (linear-light area mean) resample tail ----------------------
//
// The `Xyz12` source decodes a 2.6-gamma-encoded CIE-XYZ wire sample
// through SMPTE ST 428-1 §8 inverse-OETF -> linear XYZ -> 3x3 gamut
// matrix -> sRGB OETF -> narrow. Area-resampling must average in LINEAR
// light, so the wire row is converted to **linear XYZ** (`xyz12_to_
// xyz_f32_row`, post-OETF / pre-matrix), binned in float, and the
// non-linear tail (matrix + gamma + clamp/scale) is applied per
// finalized output row. Because the bin is a linear combination and the
// matrix is linear, `M . mean(xyz) == mean(M . xyz)` exactly — the
// matrix commutes with the bin — so every derived output equals the
// direct DCP pipeline applied to a frame whose per-pixel linear XYZ is
// the area mean of the source linear XYZ (the linear-light oracle). The
// OETF / narrow are per-pixel and run AFTER the matrix, exactly as the
// direct path does, so they need not commute with the bin.

/// Source-width **linear-XYZ** `f32` staging for the `Xyz12` resample
/// path: the wire row converts here (inverse-OETF only, no matrix)
/// before feeding [`AreaStream<f32>`]. Grows `scratch` to `3 * width`
/// `f32` under the planner's recoverable-allocation contract. Mirrors
/// [`source_rgb_f32_scratch`] for the linear-XYZ element path.
#[cfg(feature = "xyz")]
pub(super) fn source_xyz_f32_scratch<'s>(
  scratch: &'s mut Vec<f32>,
  width: usize,
  plan: &ResamplePlan,
) -> Result<&'s mut [f32], MixedSinkerError> {
  // Single-shot grow failpoint (RFC #238 tail-collapse tests): refuse this grow as a
  // host OOM would, proving a scratch OOM AFTER a successful stream build leaves the
  // `Xyz12` stream field `None` AND `resample_outputs` uncommitted (retryable).
  // Separate from the `try_box` box-alloc failpoint so a test can fail the SCRATCH
  // grow specifically.
  #[cfg(all(test, feature = "xyz", feature = "std"))]
  if FORCE_SOURCE_XYZ_F32_SCRATCH_FAILURE.with(|f| f.take()) {
    return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
      crate::resample::PlanGeometry::new(plan.src_w(), plan.src_h(), plan.out_w(), plan.out_h()),
    )));
  }
  let row = width
    .checked_mul(3)
    .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
      width,
      plan.src_h(),
      3,
    )))?;
  if scratch.len() < row {
    scratch
      .try_reserve_exact(row - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(
          crate::resample::PlanGeometry::new(
            plan.src_w(),
            plan.src_h(),
            plan.out_w(),
            plan.out_h(),
          ),
        ))
      })?;
    scratch.resize(row, 0.0);
  }
  Ok(&mut scratch[..row])
}

// Single-shot source-linear-XYZ-`f32`-scratch grow failpoint for the RFC #238
// tail-collapse transactional-preflight tests. Gated like the `Xyz12` resample test
// module; `source_xyz_f32_scratch` consults it under the same gate. Separate from
// the `try_box` box-alloc failpoint so a test can fail the SCRATCH grow specifically
// (after a successful stream construction).
#[cfg(all(test, feature = "xyz", feature = "std"))]
std::thread_local! {
  static FORCE_SOURCE_XYZ_F32_SCRATCH_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}

/// Arms a single-shot failpoint that makes the next [`source_xyz_f32_scratch`] grow
/// refuse with `AllocationFailed`, exactly as a host OOM would. Proves the
/// commit-together atomic shape (a scratch OOM after a successful `Xyz12` stream
/// build leaves the stream field `None` AND `resample_outputs` uncommitted, so the
/// row is retryable). Test-only.
#[cfg(all(test, feature = "xyz", feature = "std"))]
pub(crate) fn arm_source_xyz_f32_scratch_failure() {
  FORCE_SOURCE_XYZ_F32_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// Feeds the prepared source-width **linear-XYZ** `f32` row into the
/// (already sequence-checked) stream and derives every attached output
/// from each finalized binned linear-XYZ output row. The stream is the
/// kind-appropriate engine — an [`AreaStream`](crate::resample::AreaStream)
/// or a [`FilterStream`](crate::resample::FilterStream) — built by the
/// caller (the `Xyz12` sink's resample route) from the plan's
/// [`SpanKind`](crate::resample::SpanKind); both are
/// [`RowResampler`](crate::resample::RowResampler)s that bin the same staged
/// linear-XYZ `f32` row and run the identical per-output derivation.
///
/// `xyz_f32` copies the binned linear XYZ verbatim. Every other output
/// applies the direct DCP path's math to the binned XYZ: the gamut
/// matrix yields linear RGB (`rgb_f32`); the sRGB OETF + clamp/scale +
/// narrow yield the integer / f16 outputs (`rgb` / `rgba` / `rgb_u16` /
/// `rgba_u16` / `rgb_f16` / `rgba_f16`); and `luma` / `luma_u16` / `hsv`
/// derive from the staged u8 RGB row — exactly the direct path's
/// source-of-truth ordering. The u8 RGB staging row is sized only when
/// one of the outputs that reads it (`rgb` / `rgba` / `luma` /
/// `luma_u16` / `hsv`) is attached, so an f32-/f16-only sink neither
/// grows it nor risks its allocation failure.
///
/// `target_gamut` selects the XYZ -> RGB matrix; `luma_q15` carries the
/// gamut-matched Q15 luma weights (both ride [`Xyz12Row`] on the direct
/// path). These bind the entire frame via [`FrozenOutputs`] +
/// `xyz12_to`'s per-frame constants, so they cannot drift mid-frame.
#[cfg(feature = "xyz")]
#[allow(clippy::too_many_arguments)]
pub(super) fn xyz12_resample_emit(
  stream: &mut impl crate::resample::RowResampler<f32>,
  plan: &ResamplePlan,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  rgb_f32: &mut Option<&mut [f32]>,
  xyz_f32: &mut Option<&mut [f32]>,
  rgb_f16: &mut Option<&mut [half::f16]>,
  rgba_f16: &mut Option<&mut [half::f16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  src_xyz: &[f32],
  narrow_scratch: &mut Vec<u8>,
  target_gamut: crate::KernelGamut,
  luma_q15: (i32, i32, i32),
  idx: usize,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  use crate::row::scalar::{
    xyz12::{matmul3_xyz_rgb, narrow_unit_to_u8, narrow_unit_to_u16, oetf_srgb},
    xyz12_constants::xyz_to_rgb_matrix,
  };
  let ow = plan.out_w();
  let m = xyz_to_rgb_matrix(target_gamut);
  let one_f16 = half::f16::from_f32(1.0);
  // The u8 RGB / luma / luma_u16 / hsv outputs stage through a u8 RGB
  // narrowing of the binned linear XYZ (matrix + OETF + clamp/x255);
  // an f32-/f16-/native-u16-only sink never touches it, so the
  // out-width u8 scratch is sized — and its allocation failure risked —
  // only when one of those outputs is attached. The predicate gates
  // both the sizing here and the use in the closure, so they cannot
  // drift. `rgba` (u8) derives directly from the binned XYZ (matrix +
  // OETF + narrow + alpha, exactly the direct `xyz12_to_rgba_row`), so
  // it does not need the narrow row.
  let need_narrow = rgb.is_some() || luma.is_some() || luma_u16.is_some() || hsv.is_some();
  let narrow: &mut [u8] = if need_narrow {
    source_rgb_scratch(narrow_scratch, ow, plan)?
  } else {
    &mut []
  };
  stream.feed_row(idx, src_xyz, use_simd, |oy, binned| {
    // Lossless linear-XYZ pass-through — copy the binned row verbatim.
    if let Some(buf) = xyz_f32.as_deref_mut() {
      buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(binned);
    }
    // Linear RGB (matrix only, no OETF / clamp) — out-of-gamut negatives
    // and HDR > 1 preserved bit-exact, mirroring `xyz12_to_rgb_f32_row`
    // but over the already-inverse-OETF'd binned XYZ.
    if let Some(buf) = rgb_f32.as_deref_mut() {
      let dst = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
      for x in 0..ow {
        let i = x * 3;
        let rgb_lin = matmul3_xyz_rgb(&m, [binned[i], binned[i + 1], binned[i + 2]]);
        dst[i] = rgb_lin[0];
        dst[i + 1] = rgb_lin[1];
        dst[i + 2] = rgb_lin[2];
      }
    }
    // f16 RGB / RGBA — matrix + OETF + clamp [0, 1] + IEEE-754 RNE
    // narrow, exactly as `xyz12_to_rgb_f16_row` / `xyz12_to_rgba_f16_row`.
    if let Some(buf) = rgb_f16.as_deref_mut() {
      let dst = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
      for x in 0..ow {
        let i = x * 3;
        let rgb_lin = matmul3_xyz_rgb(&m, [binned[i], binned[i + 1], binned[i + 2]]);
        dst[i] = half::f16::from_f32(oetf_srgb(rgb_lin[0]).clamp(0.0, 1.0));
        dst[i + 1] = half::f16::from_f32(oetf_srgb(rgb_lin[1]).clamp(0.0, 1.0));
        dst[i + 2] = half::f16::from_f32(oetf_srgb(rgb_lin[2]).clamp(0.0, 1.0));
      }
    }
    if let Some(buf) = rgba_f16.as_deref_mut() {
      let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      for x in 0..ow {
        let i = x * 3;
        let o = x * 4;
        let rgb_lin = matmul3_xyz_rgb(&m, [binned[i], binned[i + 1], binned[i + 2]]);
        dst[o] = half::f16::from_f32(oetf_srgb(rgb_lin[0]).clamp(0.0, 1.0));
        dst[o + 1] = half::f16::from_f32(oetf_srgb(rgb_lin[1]).clamp(0.0, 1.0));
        dst[o + 2] = half::f16::from_f32(oetf_srgb(rgb_lin[2]).clamp(0.0, 1.0));
        dst[o + 3] = one_f16;
      }
    }
    // u16 RGB / RGBA — matrix + OETF + clamp + x65535 + round-half-up,
    // exactly as `xyz12_to_rgb_u16_row` / `xyz12_to_rgba_u16_row`.
    if let Some(buf) = rgb_u16.as_deref_mut() {
      let dst = &mut buf[oy * 3 * ow..(oy + 1) * 3 * ow];
      for x in 0..ow {
        let i = x * 3;
        let rgb_lin = matmul3_xyz_rgb(&m, [binned[i], binned[i + 1], binned[i + 2]]);
        dst[i] = narrow_unit_to_u16(oetf_srgb(rgb_lin[0]));
        dst[i + 1] = narrow_unit_to_u16(oetf_srgb(rgb_lin[1]));
        dst[i + 2] = narrow_unit_to_u16(oetf_srgb(rgb_lin[2]));
      }
    }
    if let Some(buf) = rgba_u16.as_deref_mut() {
      let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      for x in 0..ow {
        let i = x * 3;
        let o = x * 4;
        let rgb_lin = matmul3_xyz_rgb(&m, [binned[i], binned[i + 1], binned[i + 2]]);
        dst[o] = narrow_unit_to_u16(oetf_srgb(rgb_lin[0]));
        dst[o + 1] = narrow_unit_to_u16(oetf_srgb(rgb_lin[1]));
        dst[o + 2] = narrow_unit_to_u16(oetf_srgb(rgb_lin[2]));
        dst[o + 3] = 0xFFFF;
      }
    }
    // u8 RGBA — matrix + OETF + clamp + x255 + round-half-up, alpha
    // 0xFF (exactly `xyz12_to_rgba_row`), derived directly from the
    // binned XYZ rather than expanded from the staged u8 RGB row.
    if let Some(buf) = rgba.as_deref_mut() {
      let dst = &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow];
      for x in 0..ow {
        let i = x * 3;
        let o = x * 4;
        let rgb_lin = matmul3_xyz_rgb(&m, [binned[i], binned[i + 1], binned[i + 2]]);
        dst[o] = narrow_unit_to_u8(oetf_srgb(rgb_lin[0]));
        dst[o + 1] = narrow_unit_to_u8(oetf_srgb(rgb_lin[1]));
        dst[o + 2] = narrow_unit_to_u8(oetf_srgb(rgb_lin[2]));
        dst[o + 3] = 0xFF;
      }
    }
    if need_narrow {
      let nrow = &mut narrow[..3 * ow];
      // Stage the u8 RGB row once via the direct path's matrix + OETF +
      // clamp + x255; rgb / rgba / luma / luma_u16 / hsv all read it.
      for x in 0..ow {
        let i = x * 3;
        let rgb_lin = matmul3_xyz_rgb(&m, [binned[i], binned[i + 1], binned[i + 2]]);
        nrow[i] = narrow_unit_to_u8(oetf_srgb(rgb_lin[0]));
        nrow[i + 1] = narrow_unit_to_u8(oetf_srgb(rgb_lin[1]));
        nrow[i + 2] = narrow_unit_to_u8(oetf_srgb(rgb_lin[2]));
      }
      if let Some(buf) = rgb.as_deref_mut() {
        buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(nrow);
      }
      if let Some(buf) = luma.as_deref_mut() {
        crate::row::xyz12_rgb_to_luma_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          luma_q15,
          use_simd,
        );
      }
      if let Some(buf) = luma_u16.as_deref_mut() {
        crate::row::xyz12_rgb_to_luma_u16_row(
          nrow,
          &mut buf[oy * ow..(oy + 1) * ow],
          ow,
          luma_q15,
          use_simd,
        );
      }
      if let Some(hsv) = hsv.as_mut() {
        let (h, s, v) = hsv.hsv();
        crate::row::rgb_to_hsv_row(
          nrow,
          &mut h[oy * ow..(oy + 1) * ow],
          &mut s[oy * ow..(oy + 1) * ow],
          &mut v[oy * ow..(oy + 1) * ow],
          ow,
          use_simd,
        );
      }
    }
  })?;
  Ok(())
}

/// Configurable-coefficients luma derivation from packed
/// `R, G, B` u8 row.
///
/// Q8 fixed-point: `Y ≈ (cr·R + cg·G + cb·B + 128) >> 8`, where
/// `(cr, cg, cb)` is the caller's [`LumaCoefficients`] resolved
/// via [`LumaCoefficients::to_q8`]. The presets all sum to `256`
/// so the divisor is implicit in the `>> 8`. `rgb` carries
/// `3 * luma.len()` packed bytes; the loop writes one luma
/// sample per pixel.
///
/// Used by Bayer / Bayer16 / Pal8 [`MixedSinker`] paths whose source
/// has no native luma plane to memcpy from. YUV source impls take
/// their luma directly off the Y plane and don't go through this
/// helper, so they don't need a configurable coefficient set —
/// the source's `KernelMatrix` already fixed it at encode time.
#[cfg(any(feature = "bayer", feature = "mono"))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn rgb_row_to_luma_row(rgb: &[u8], luma: &mut [u8], coeffs_q8: (u32, u32, u32)) {
  // Caller's contract: `rgb` packs `3 * luma.len()` bytes. The
  // current callers (`MixedSinker<Bayer>` and
  // `MixedSinker<Bayer16<BITS>>`) both slice their `luma` and
  // `rgb_row` from the same `width`, so the relationship holds
  // structurally — but the `debug_assert` makes that obvious to
  // any future caller and turns silent OOB indexing into a clear
  // failure under tests.
  //
  // `checked_mul` instead of `3 * luma.len()` because, while the
  // existing `frame_elems` validation in caller paths makes the
  // product fit, a future caller passing a raw slice with no such
  // upstream check could trigger a `usize` overflow inside the
  // assert message itself (panic before the assertion runs).
  // Failing the assert on overflow yields a clean diagnostic.
  debug_assert!(
    luma
      .len()
      .checked_mul(3)
      .is_some_and(|need| rgb.len() >= need),
    "rgb_row_to_luma_row: rgb.len()={} but need {} (= 3 x luma.len()={})",
    rgb.len(),
    luma.len().saturating_mul(3),
    luma.len(),
  );
  let (cr, cg, cb) = coeffs_q8;
  for (i, d) in luma.iter_mut().enumerate() {
    let r = rgb[3 * i] as u32;
    let g = rgb[3 * i + 1] as u32;
    let b = rgb[3 * i + 2] as u32;
    *d = ((cr * r + cg * g + cb * b + 128) >> 8).min(255) as u8;
  }
}

/// Same as [`rgb_row_to_luma_row`] but widens the luma byte to `u16` via
/// `(y << 8) | y` (`0 → 0x0000`, `255 → 0xFFFF`).
///
/// Used by format sinker paths that expose a `with_luma_u16` output channel
/// (e.g. `MixedSinker<Pal8>`).
#[cfg(feature = "mono")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn rgb_row_to_luma_u16_row(
  rgb: &[u8],
  luma_u16: &mut [u16],
  coeffs_q8: (u32, u32, u32),
) {
  debug_assert!(
    luma_u16
      .len()
      .checked_mul(3)
      .is_some_and(|need| rgb.len() >= need),
    "rgb_row_to_luma_u16_row: rgb.len()={} but need {} (= 3 x luma_u16.len()={})",
    rgb.len(),
    luma_u16.len().saturating_mul(3),
    luma_u16.len(),
  );
  let (cr, cg, cb) = coeffs_q8;
  for (i, dst) in luma_u16.iter_mut().enumerate() {
    let r = rgb[3 * i] as u32;
    let g = rgb[3 * i + 1] as u32;
    let b = rgb[3 * i + 2] as u32;
    let y = ((cr * r + cg * g + cb * b + 128) >> 8).min(255) as u16;
    *dst = (y << 8) | y;
  }
}

// ---- Format-specific impl blocks (split out of mod.rs) ------------------
//
// Each child module hosts the `MixedSinker<'_, F>` impl blocks for a
// related family of source formats. mod.rs keeps only the shared
// prelude (errors, types, struct, generic impls, helpers) and the
// `LumaCoefficients` API. Per-format `with_rgba` / `set_rgba` builders
// and `PixelSink` impls live in the child modules below.

#[cfg(feature = "yuv-444-packed")]
mod ayuv;
#[cfg(feature = "yuv-444-packed")]
mod ayuv64;
#[cfg(feature = "bayer")]
mod bayer;
#[cfg(feature = "yuv-planar")]
mod chroma_reconstruct;
#[cfg(feature = "gray")]
mod gray;
#[cfg(feature = "rgb-legacy")]
mod legacy_rgb;
#[cfg(feature = "mono")]
mod mono1bit;
#[cfg(feature = "rgb")]
mod packed_rgb_10bit;
#[cfg(feature = "rgb")]
mod packed_rgb_16bit;
#[cfg(feature = "rgb")]
mod packed_rgb_32bit;
#[cfg(feature = "rgb")]
mod packed_rgb_8bit;
#[cfg(feature = "rgb-float")]
mod packed_rgb_f16;
#[cfg(feature = "rgb-float")]
mod packed_rgb_float;
#[cfg(feature = "yuv-packed")]
mod packed_yuv_4_1_1;
#[cfg(feature = "yuv-packed")]
mod packed_yuv_8bit;
#[cfg(feature = "mono")]
mod pal8;
#[cfg(feature = "yuv-planar")]
mod planar_8bit;
// The chroma failpoint is armed only by the non-4:2:0 native suites that own a
// colour oracle, and each reuses the planar join so each also pulls in
// `yuv-planar`: the packed-8-bit / packed-4:4:4 / 4:1:1 natives, plus the
// semi-planar 8-bit suite's `yuv-planar`-gated `native_tier` (its colour oracle
// adds `rgb`). So its re-export is dead in a `yuv-planar`-without-any-consumer
// build (and a `yuv-planar`-less build has no `planar_8bit` module to import).
#[cfg(all(test, feature = "std", feature = "yuv-planar", feature = "rgb"))]
pub(crate) use planar_8bit::arm_native_rgb_scratch_failure;
#[cfg(all(
  test,
  feature = "std",
  feature = "yuv-planar",
  any(
    feature = "yuv-packed",
    feature = "yuv-444-packed",
    all(feature = "yuv-semi-planar", feature = "rgb")
  )
))]
pub(crate) use planar_8bit::arm_planar_native_chroma_failure;
// RFC #238 Phase 2 — the linear-light averaging path for the planar 8-bit
// YUV family. Produces RGB/RGBA, so it needs both `yuv-planar` (the source
// formats) and `rgb` (the output kernels).
#[cfg(all(feature = "yuv-planar", feature = "rgb"))]
mod linear_light;
#[cfg(feature = "yuv-planar")]
mod planar_high_bit_native;
// Bring the high-bit non-4:2:0 planar native join + entry point (and its
// test-only alloc failpoint) into the `mixed` scope so the three per-format
// high-bit sinks — `subsampled_4_2_2_high_bit::{yuv422p, yuv440p}` and
// `subsampled_4_4_4_high_bit::yuv444p`, which span two parent modules —
// reach them by the same `super::super::{..}` path they use for the shared
// row-stage tails (`packed_yuv4*_triple_resample`).
#[cfg(feature = "yuv-planar")]
use planar_high_bit_native::yuv_planar16_process_native;
// The high-bit semi-planar non-4:2:0 P-format native wrappers
// (`subsampled_4_2_2_high_bit::p2xx` / `subsampled_4_4_4_high_bit::p4xx`) reuse
// the planar join + its pre-feed preflight after de-interleaving + de-packing
// the packed UV plane — the `u16` twin of the 8-bit semi-planar non-4:2:0
// reuse. The high-bit PACKED 4:2:2 native wrapper (`y2xx_process_native`, the
// `Y210` / `Y212` / `Y216` tier) reuses the same join + preflight after
// de-packing + de-interleaving the YUYV-ordered MSB-aligned u16 words. Bring
// the join type + preflight into the `mixed` scope so those sinks reach them by
// `super::{..}` / `super::super::{..}`. The MSB-aligned planar 4:4:4 native
// wrapper (`subsampled_4_4_4_high_bit::yuv444p_msb`) is a `yuv-planar`-only
// consumer (it de-packs the high-bit samples then reuses the same join +
// preflight as the low-bit twin), so the re-export tracks `yuv-planar` alone —
// matching the [`yuv_planar16_process_native`] re-export above; the
// semi-planar / `y2xx` / packed-4:4:4 wrappers are additional consumers.
#[cfg(feature = "yuv-planar")]
use planar_high_bit_native::{NativePlanarYuvU16, native_planar_hb_preflight_check_only};
// The source-scratch + u16-colour-scratch alloc failpoints are armed only by the
// high-bit planar native suite, which is gated on `rgb` (its colour oracle), so
// these re-exports are dead in a `yuv-planar`-without-`rgb` build.
#[cfg(all(test, feature = "std", feature = "yuv-planar", feature = "rgb"))]
pub(crate) use planar_high_bit_native::{
  arm_planar_hb_native_alloc_failure, arm_planar_hb_native_alloc_failure_after,
  arm_planar_hb_native_rgb_u16_scratch_failure,
};
// The chroma failpoint is armed only by the high-bit non-4:2:0 native suites
// that own a colour oracle (V210 / Y2xx / P2xx / P4xx / packed-4:4:4, each
// pulling in `yuv-planar` for the join, plus the `rgb`-gated planar high-bit
// native suite), so its re-export is dead in a `yuv-planar`-without-any-consumer
// build.
#[cfg(all(
  test,
  feature = "std",
  feature = "yuv-planar",
  any(
    feature = "rgb",
    feature = "v210",
    feature = "y2xx",
    feature = "yuv-444-packed",
    feature = "yuv-semi-planar"
  )
))]
pub(crate) use planar_high_bit_native::arm_planar_hb_native_chroma_failure;
#[cfg(feature = "gbr")]
mod planar_gbr_32bit;
#[cfg(feature = "gbr")]
mod planar_gbr_8bit;
#[cfg(feature = "gbr")]
mod planar_gbr_f16;
#[cfg(feature = "gbr")]
mod planar_gbr_float;
#[cfg(feature = "gbr")]
mod planar_gbr_high_bit;
#[cfg(feature = "gbr")]
mod planar_gbr_msb;
/// Format-agnostic planar-YUV resample helpers (the 4:2:0 native /
/// row-stage join and the shared row-stage path), reused by the 8-bit
/// planar family, the semi-planar family, and the packed YUV family
/// (which de-interleaves Y into a scratch first via
/// [`planar_resample::packed_yuv_dual_resample`]). The shared element-type-free
/// compare-only preflight ([`resample_preflight_check_only`]) that every routed
/// family runs lives beside [`frozen_outputs_check`] in this module, so this
/// planar submodule is gated only to its own consumers.
#[cfg(any(
  feature = "yuv-planar",
  feature = "yuv-semi-planar",
  feature = "yuv-packed"
))]
mod planar_resample;
#[cfg(feature = "yuv-semi-planar")]
mod semi_planar_8bit;
// Each `subsampled_4_*_high_bit` parent hosts BOTH the high-bit planar
// `Yuv4*p` sinks (`yuv-planar`) and the high-bit semi-planar P-format
// sinks (`yuv-semi-planar`); the per-submodule gates inside each `mod`
// keep the two families separable, so the parent compiles whenever
// either family is on. Mirrors how `row::dispatch::yuv420` /
// `row::dispatch::pn` host the matching row layer.
#[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
mod subsampled_4_2_0_high_bit;
#[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
mod subsampled_4_2_2_high_bit;
#[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
mod subsampled_4_4_4_high_bit;
#[cfg(feature = "yuv-444-packed")]
mod uyva;
#[cfg(feature = "v210")]
mod v210;
#[cfg(feature = "yuv-444-packed")]
mod v30x;
#[cfg(feature = "yuv-444-packed")]
mod v410;
#[cfg(feature = "yuv-444-packed")]
mod vuya;
#[cfg(feature = "yuv-444-packed")]
mod vuyx;
#[cfg(feature = "yuv-444-packed")]
mod vyu444;
#[cfg(feature = "yuv-444-packed")]
mod xv36;
#[cfg(feature = "yuv-444-packed")]
mod xv48;
#[cfg(feature = "xyz")]
mod xyz12;
#[cfg(feature = "y2xx")]
mod y210;
#[cfg(feature = "y2xx")]
mod y212;
#[cfg(feature = "y2xx")]
mod y216;
#[cfg(feature = "yuva")]
mod yuva_4_2_0;
#[cfg(feature = "yuva")]
mod yuva_4_2_2;
#[cfg(feature = "yuva")]
mod yuva_4_4_4;

#[cfg(all(test, feature = "std"))]
mod tests;

#[cfg(all(test, feature = "std"))]
mod api_smoke_tests {
  use super::*;

  #[cfg(feature = "v210")]
  #[test]
  fn mixed_sinker_default_does_not_produce_luma_u16() {
    // Use the currently available V210 source format marker for this smoke test.
    let sink: MixedSinker<'_, crate::source::V210> = MixedSinker::new(6, 1);
    assert!(!sink.produces_luma_u16());
  }

  #[test]
  fn luma_u16_buffer_too_short_error_displays() {
    let err = MixedSinkerError::InsufficientLumaU16Buffer(InsufficientBuffer::new(100, 50));
    let msg = format!("{err}");
    assert!(msg.contains("100"));
    assert!(msg.contains("50"));
  }

  /// `MixedSinker` is routinely constructed as a stack local (tests, the
  /// crate's own callers), so its inline footprint must stay small enough to
  /// fit a constrained thread stack — the Windows (~1 MiB) and SDE-AVX-512
  /// test threads overflowed once the per-format scratch, the native-tier
  /// joins, and the per-plane area / filter streams accumulated inline. The
  /// large sub-structures (every lazily-created stream, every native join) are
  /// held behind a `Box`, so only a pointer sits inline; this ceiling fails the
  /// test if a future field reintroduces the bloat. The threshold sits well
  /// above the current size (a few KiB) yet far below any plausible thread
  /// stack budget.
  #[cfg(feature = "yuv-planar")]
  #[test]
  fn mixed_sinker_inline_size_stays_small() {
    use crate::{resample::AreaResampler, source::Yuv420p};
    // The largest practical monomorphization — a resampling Yuv420p sink with
    // every output and every native / filter join reachable.
    let area = core::mem::size_of::<MixedSinker<'_, Yuv420p, AreaResampler>>();
    let noop = core::mem::size_of::<MixedSinker<'_, Yuv420p>>();
    assert!(
      area < 16384,
      "MixedSinker<Yuv420p, AreaResampler> inline size {area} exceeds the 16 KiB \
       stack-footprint ceiling; box the largest new field(s) (mirror the existing \
       Option<Box<…>> stream / native-join fields)",
    );
    assert!(
      noop < 16384,
      "MixedSinker<Yuv420p, NoopResampler> inline size {noop} exceeds the 16 KiB ceiling",
    );
  }
}