bwipp-rs 0.1.0

Pure-Rust port of BWIPP (Barcode Writer in Pure PostScript). Generates barcodes in SVG and PNG.
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
//! Han Xin Code — Chinese national 2D barcode standard (GB/T 21049-2007).
//!
//! Han Xin is a square-grid 2D symbology with 84 fixed-size variants
//! (versions 1..=84), four error-correction levels (L1..L4), and (in
//! the full standard) modes for numeric, alphanumeric, binary, Region
//! One / Two GB18030, and ECI data. **bwip-js's `bwipp_hanxin` only
//! exposes binary mode** (mode indicator `"0011"`), so this port
//! matches that scope.
//!
//! **Pipeline** (all stages byte-identical to bwip-js):
//!   1. [`encode_binary_bits`] — emit `0011` + 13-bit length +
//!      `8 × msglen` bit stream.
//!   2. [`select_version`] — pick smallest of [`HANXIN_METRICS`] that
//!      fits at the given ECC level.
//!   3. [`pad_to_dmod`] — zero-pad to symbol's `dmod` bit capacity.
//!   4. [`bits_to_codewords`] — slice into 8-bit codewords (MSB first).
//!   5. [`encode_rs_blocks`] — per-block Reed-Solomon over GF(256)
//!      (poly 355). The number / size of blocks comes from the
//!      version's `(count, data, ecc)` triples per ECC level.
//!   6. [`interleave_codewords`] — 13-stride re-shuffle, plus trailing
//!      zero codeword if `cap_bits % 8 > 0`.
//!   7. Module placement: 4 corner finders (8×8 [`FPAT`] + [`FPAT2`]),
//!      per-version 5×5 alignment patterns with stagger logic
//!      (BWIPP `bwipp_hanxin` lines 32753-32803), and 2 function-info
//!      regions encoding `version`, ECC level, and a GF(16) RS check.
//!   8. Masking: BWIPP evaluates 4 masks (XOR patterns 1..=4) over the
//!      data placement, scores each with `evalfull` (N1 + N3 finder-
//!      lookalike penalties), and picks the lowest-score symbol.
//!
//! Verified against six byte-for-byte `pixs` oracles spanning v1 / v2
//! and all four masks, plus a 24-case mask-score corpus
//! (`evalfull_scores_match_bwip_js_*`). End-to-end pipeline pinned by
//! `encode_final_codewords_matches_bwip_js_for_a_l1`.
//!
//! References:
//! - BWIPP `bwipp_hanxin` (bwip-js lines 33699-35065)
//! - GB/T 21049-2007 standard
//! - AIM ISS Han Xin Code specification

// Several internal-use lookup tables (FPAT/FPAT2, alignment grids) are
// referenced indirectly through indexing macros; the lint here is
// satisfied at the file level rather than tagging every constant.
#![allow(dead_code)]

/// One row of the Han Xin metric table.
///
/// Fields (matching BWIPP `hanxin_metrics`):
/// - `version`: 1..=84 (stored as `&'static str` to mirror BWIPP's "1"..="84")
/// - `size`: module count per row/column (always odd; from 23 to 189)
/// - `alig_step`: alignment-pattern grid step (`-1` for versions without)
/// - `alig_count`: alignment pattern count (`0` for versions without)
/// - `cap_bits`: total bits in the symbol's data region (BWIPP `nmod`).
///   Codeword count is `cap_bits / 8`; the `cap_bits % 8` leftover
///   bits are trailing slack at the end of the placement.
/// - `blocks_l1`, `blocks_l2`, `blocks_l3`, `blocks_l4`: 3-tuple
///   `(count, data_per_block, ecc_per_block)` arrays for each ECC level;
///   each level has up to 3 entries describing the codeword distribution.
pub(crate) struct HanXinMetric {
    pub version: &'static str,
    pub size: u8,
    pub alig_step: i8,
    pub alig_count: u8,
    pub cap_bits: u32,
    /// Each ECC level is 3 blocks of `Option<(count, data, ecc)>`; `None`
    /// signals "no block" (BWIPP's `hanxin_noblk = [0, -1, -1]`). All
    /// fields fit in `u8` since BWIPP caps `data` at 157 and `count` at 84.
    pub blocks: [[Option<(u8, u8, u8)>; 3]; 4],
}

/// "No block" sentinel for the metric table — `None` per [`HanXinMetric::blocks`].
#[allow(dead_code)]
const NOBLK: Option<(u8, u8, u8)> = None;

/// The 84-row Han Xin Code metric table (versions 1..=84). Direct port
/// of BWIPP `hanxin_metrics` (bwip-js line 32297). Each row's
/// `(count, data, ecc)` tuples describe the codeword block distribution
/// for that ECC level — `sum(count * (data + ecc)) * 8 + leftover`
/// equals `cap_bits`, where `leftover = cap_bits % 8`.
#[rustfmt::skip]
pub(crate) const HANXIN_METRICS: [HanXinMetric; 84] = [
    HanXinMetric { version: "1", size: 23, alig_step: -1, alig_count: 0, cap_bits: 205, blocks: [[Some((1, 21, 4)), None, None], [Some((1, 17, 8)), None, None], [Some((1, 13, 12)), None, None], [Some((1, 9, 16)), None, None]] },
    HanXinMetric { version: "2", size: 25, alig_step: -1, alig_count: 0, cap_bits: 301, blocks: [[Some((1, 31, 6)), None, None], [Some((1, 25, 12)), None, None], [Some((1, 19, 18)), None, None], [Some((1, 15, 22)), None, None]] },
    HanXinMetric { version: "3", size: 27, alig_step: -1, alig_count: 0, cap_bits: 405, blocks: [[Some((1, 42, 8)), None, None], [Some((1, 34, 16)), None, None], [Some((1, 26, 24)), None, None], [Some((1, 20, 30)), None, None]] },
    HanXinMetric { version: "4", size: 29, alig_step: 14, alig_count: 1, cap_bits: 439, blocks: [[Some((1, 46, 8)), None, None], [Some((1, 38, 16)), None, None], [Some((1, 30, 24)), None, None], [Some((1, 22, 32)), None, None]] },
    HanXinMetric { version: "5", size: 31, alig_step: 16, alig_count: 1, cap_bits: 555, blocks: [[Some((1, 57, 12)), None, None], [Some((1, 49, 20)), None, None], [Some((1, 37, 32)), None, None], [Some((1, 14, 20)), Some((1, 13, 22)), None]] },
    HanXinMetric { version: "6", size: 33, alig_step: 16, alig_count: 1, cap_bits: 675, blocks: [[Some((1, 70, 14)), None, None], [Some((1, 58, 26)), None, None], [Some((1, 24, 20)), Some((1, 22, 18)), None], [Some((1, 16, 24)), Some((1, 18, 26)), None]] },
    HanXinMetric { version: "7", size: 35, alig_step: 17, alig_count: 1, cap_bits: 805, blocks: [[Some((1, 84, 16)), None, None], [Some((1, 70, 30)), None, None], [Some((1, 26, 22)), Some((1, 28, 24)), None], [Some((2, 14, 20)), Some((1, 12, 20)), None]] },
    HanXinMetric { version: "8", size: 37, alig_step: 18, alig_count: 1, cap_bits: 943, blocks: [[Some((1, 99, 18)), None, None], [Some((1, 40, 18)), Some((1, 41, 18)), None], [Some((1, 31, 26)), Some((1, 32, 28)), None], [Some((2, 16, 24)), Some((1, 15, 22)), None]] },
    HanXinMetric { version: "9", size: 39, alig_step: 19, alig_count: 1, cap_bits: 1089, blocks: [[Some((1, 114, 22)), None, None], [Some((2, 48, 20)), None, None], [Some((2, 24, 20)), Some((1, 26, 22)), None], [Some((2, 18, 28)), Some((1, 18, 26)), None]] },
    HanXinMetric { version: "10", size: 41, alig_step: 20, alig_count: 1, cap_bits: 1243, blocks: [[Some((1, 131, 24)), None, None], [Some((1, 52, 22)), Some((1, 57, 24)), None], [Some((2, 27, 24)), Some((1, 29, 24)), None], [Some((2, 21, 32)), Some((1, 19, 30)), None]] },
    HanXinMetric { version: "11", size: 43, alig_step: 14, alig_count: 2, cap_bits: 1289, blocks: [[Some((1, 135, 26)), None, None], [Some((1, 56, 24)), Some((1, 57, 24)), None], [Some((2, 28, 24)), Some((1, 31, 26)), None], [Some((2, 22, 32)), Some((1, 21, 32)), None]] },
    HanXinMetric { version: "12", size: 45, alig_step: 15, alig_count: 2, cap_bits: 1455, blocks: [[Some((1, 153, 28)), None, None], [Some((1, 62, 26)), Some((1, 65, 28)), None], [Some((2, 32, 28)), Some((1, 33, 28)), None], [Some((3, 17, 26)), Some((1, 22, 30)), None]] },
    HanXinMetric { version: "13", size: 47, alig_step: 16, alig_count: 2, cap_bits: 1629, blocks: [[Some((1, 86, 16)), Some((1, 85, 16)), None], [Some((1, 71, 30)), Some((1, 72, 30)), None], [Some((2, 37, 32)), Some((1, 35, 30)), None], [Some((3, 20, 30)), Some((1, 21, 32)), None]] },
    HanXinMetric { version: "14", size: 49, alig_step: 16, alig_count: 2, cap_bits: 1805, blocks: [[Some((1, 94, 18)), Some((1, 95, 18)), None], [Some((2, 51, 22)), Some((1, 55, 24)), None], [Some((3, 30, 26)), Some((1, 31, 26)), None], [Some((4, 18, 28)), Some((1, 17, 24)), None]] },
    HanXinMetric { version: "15", size: 51, alig_step: 17, alig_count: 2, cap_bits: 1995, blocks: [[Some((1, 104, 20)), Some((1, 105, 20)), None], [Some((2, 57, 24)), Some((1, 61, 26)), None], [Some((3, 33, 28)), Some((1, 36, 30)), None], [Some((4, 20, 30)), Some((1, 19, 30)), None]] },
    HanXinMetric { version: "16", size: 53, alig_step: 17, alig_count: 2, cap_bits: 2187, blocks: [[Some((1, 115, 22)), Some((1, 114, 22)), None], [Some((2, 65, 28)), Some((1, 61, 26)), None], [Some((3, 38, 32)), Some((1, 33, 30)), None], [Some((5, 19, 28)), Some((1, 14, 24)), None]] },
    HanXinMetric { version: "17", size: 55, alig_step: 18, alig_count: 2, cap_bits: 2393, blocks: [[Some((1, 126, 24)), Some((1, 125, 24)), None], [Some((2, 70, 30)), Some((1, 69, 30)), None], [Some((4, 33, 28)), Some((1, 29, 26)), None], [Some((5, 20, 30)), Some((1, 19, 30)), None]] },
    HanXinMetric { version: "18", size: 57, alig_step: 19, alig_count: 2, cap_bits: 2607, blocks: [[Some((1, 136, 26)), Some((1, 137, 26)), None], [Some((3, 56, 24)), Some((1, 59, 26)), None], [Some((5, 35, 30)), None, None], [Some((6, 18, 28)), Some((1, 21, 28)), None]] },
    HanXinMetric { version: "19", size: 59, alig_step: 20, alig_count: 2, cap_bits: 2829, blocks: [[Some((1, 148, 28)), Some((1, 149, 28)), None], [Some((3, 61, 26)), Some((1, 64, 28)), None], [Some((7, 24, 20)), Some((1, 23, 22)), None], [Some((6, 20, 30)), Some((1, 21, 32)), None]] },
    HanXinMetric { version: "20", size: 61, alig_step: 20, alig_count: 2, cap_bits: 3053, blocks: [[Some((3, 107, 20)), None, None], [Some((3, 65, 28)), Some((1, 72, 30)), None], [Some((7, 26, 22)), Some((1, 23, 22)), None], [Some((7, 19, 28)), Some((1, 20, 32)), None]] },
    HanXinMetric { version: "21", size: 63, alig_step: 21, alig_count: 2, cap_bits: 3291, blocks: [[Some((3, 115, 22)), None, None], [Some((4, 56, 24)), Some((1, 63, 28)), None], [Some((7, 28, 24)), Some((1, 25, 22)), None], [Some((8, 18, 28)), Some((1, 21, 22)), None]] },
    HanXinMetric { version: "22", size: 65, alig_step: 16, alig_count: 3, cap_bits: 3383, blocks: [[Some((2, 116, 22)), Some((1, 122, 24)), None], [Some((4, 56, 24)), Some((1, 72, 30)), None], [Some((7, 28, 24)), Some((1, 32, 26)), None], [Some((8, 18, 28)), Some((1, 24, 30)), None]] },
    HanXinMetric { version: "23", size: 67, alig_step: 17, alig_count: 3, cap_bits: 3631, blocks: [[Some((3, 127, 24)), None, None], [Some((5, 51, 22)), Some((1, 62, 26)), None], [Some((7, 30, 26)), Some((1, 35, 26)), None], [Some((8, 20, 30)), Some((1, 21, 32)), None]] },
    HanXinMetric { version: "24", size: 69, alig_step: 17, alig_count: 3, cap_bits: 3887, blocks: [[Some((2, 135, 26)), Some((1, 137, 26)), None], [Some((5, 56, 24)), Some((1, 59, 26)), None], [Some((7, 33, 28)), Some((1, 30, 28)), None], [Some((11, 16, 24)), Some((1, 19, 26)), None]] },
    HanXinMetric { version: "25", size: 71, alig_step: 18, alig_count: 3, cap_bits: 4151, blocks: [[Some((3, 105, 20)), Some((1, 121, 22)), None], [Some((5, 61, 26)), Some((1, 57, 26)), None], [Some((9, 28, 24)), Some((1, 28, 22)), None], [Some((10, 19, 28)), Some((1, 18, 30)), None]] },
    HanXinMetric { version: "26", size: 73, alig_step: 18, alig_count: 3, cap_bits: 4423, blocks: [[Some((2, 157, 30)), Some((1, 150, 28)), None], [Some((5, 65, 28)), Some((1, 61, 26)), None], [Some((8, 33, 28)), Some((1, 34, 30)), None], [Some((10, 19, 28)), Some((2, 15, 26)), None]] },
    HanXinMetric { version: "27", size: 75, alig_step: 19, alig_count: 3, cap_bits: 4703, blocks: [[Some((3, 126, 24)), Some((1, 115, 22)), None], [Some((7, 51, 22)), Some((1, 54, 22)), None], [Some((8, 35, 30)), Some((1, 37, 30)), None], [Some((15, 15, 22)), Some((1, 10, 22)), None]] },
    HanXinMetric { version: "28", size: 77, alig_step: 19, alig_count: 3, cap_bits: 4991, blocks: [[Some((4, 105, 20)), Some((1, 103, 20)), None], [Some((7, 56, 24)), Some((1, 45, 18)), None], [Some((10, 31, 26)), Some((1, 27, 26)), None], [Some((10, 17, 26)), Some((3, 20, 28)), Some((1, 21, 28))]] },
    HanXinMetric { version: "29", size: 79, alig_step: 20, alig_count: 3, cap_bits: 5287, blocks: [[Some((3, 139, 26)), Some((1, 137, 28)), None], [Some((6, 66, 28)), Some((1, 66, 30)), None], [Some((9, 36, 30)), Some((1, 34, 32)), None], [Some((13, 19, 28)), Some((1, 17, 32)), None]] },
    HanXinMetric { version: "30", size: 81, alig_step: 20, alig_count: 3, cap_bits: 5591, blocks: [[Some((6, 84, 16)), Some((1, 82, 16)), None], [Some((6, 70, 30)), Some((1, 68, 30)), None], [Some((7, 35, 30)), Some((3, 33, 28)), Some((1, 32, 28))], [Some((13, 20, 30)), Some((1, 20, 28)), None]] },
    HanXinMetric { version: "31", size: 83, alig_step: 21, alig_count: 3, cap_bits: 5903, blocks: [[Some((5, 105, 20)), Some((1, 94, 18)), None], [Some((6, 74, 32)), Some((1, 71, 30)), None], [Some((11, 33, 28)), Some((1, 34, 32)), None], [Some((13, 19, 28)), Some((3, 16, 26)), None]] },
    HanXinMetric { version: "32", size: 85, alig_step: 17, alig_count: 4, cap_bits: 6033, blocks: [[Some((4, 127, 24)), Some((1, 126, 24)), None], [Some((7, 66, 28)), Some((1, 66, 30)), None], [Some((12, 30, 24)), Some((1, 24, 28)), Some((1, 24, 30))], [Some((15, 19, 28)), Some((1, 17, 32)), None]] },
    HanXinMetric { version: "33", size: 87, alig_step: 17, alig_count: 4, cap_bits: 6353, blocks: [[Some((7, 84, 16)), Some((1, 78, 16)), None], [Some((7, 70, 30)), Some((1, 66, 28)), None], [Some((12, 33, 28)), Some((1, 32, 30)), None], [Some((14, 21, 32)), Some((1, 24, 28)), None]] },
    HanXinMetric { version: "34", size: 89, alig_step: 18, alig_count: 4, cap_bits: 6689, blocks: [[Some((5, 117, 22)), Some((1, 117, 24)), None], [Some((8, 66, 28)), Some((1, 58, 26)), None], [Some((11, 38, 32)), Some((1, 34, 32)), None], [Some((15, 20, 30)), Some((2, 17, 26)), None]] },
    HanXinMetric { version: "35", size: 91, alig_step: 18, alig_count: 4, cap_bits: 7025, blocks: [[Some((4, 148, 28)), Some((1, 146, 28)), None], [Some((8, 68, 30)), Some((1, 70, 24)), None], [Some((10, 36, 32)), Some((3, 38, 28)), None], [Some((16, 19, 28)), Some((3, 16, 26)), None]] },
    HanXinMetric { version: "36", size: 93, alig_step: 19, alig_count: 4, cap_bits: 7377, blocks: [[Some((4, 126, 24)), Some((2, 135, 26)), None], [Some((8, 70, 28)), Some((2, 43, 26)), None], [Some((13, 32, 28)), Some((2, 41, 30)), None], [Some((17, 19, 28)), Some((3, 15, 26)), None]] },
    HanXinMetric { version: "37", size: 95, alig_step: 19, alig_count: 4, cap_bits: 7729, blocks: [[Some((5, 136, 26)), Some((1, 132, 24)), None], [Some((5, 67, 30)), Some((4, 68, 28)), Some((1, 69, 28))], [Some((14, 35, 30)), Some((1, 32, 24)), None], [Some((18, 18, 26)), Some((3, 16, 28)), Some((1, 14, 28))]] },
    HanXinMetric { version: "38", size: 97, alig_step: 19, alig_count: 4, cap_bits: 8089, blocks: [[Some((3, 142, 26)), Some((3, 141, 28)), None], [Some((8, 70, 30)), Some((1, 73, 32)), Some((1, 74, 32))], [Some((12, 34, 30)), Some((3, 34, 26)), Some((1, 35, 28))], [Some((18, 21, 32)), Some((1, 27, 30)), None]] },
    HanXinMetric { version: "39", size: 99, alig_step: 20, alig_count: 4, cap_bits: 8465, blocks: [[Some((5, 116, 22)), Some((2, 103, 20)), Some((1, 102, 20))], [Some((9, 74, 32)), Some((1, 74, 30)), None], [Some((14, 34, 28)), Some((2, 32, 32)), Some((1, 32, 30))], [Some((19, 21, 32)), Some((1, 25, 26)), None]] },
    HanXinMetric { version: "40", size: 101, alig_step: 20, alig_count: 4, cap_bits: 8841, blocks: [[Some((7, 116, 22)), Some((1, 117, 22)), None], [Some((11, 65, 28)), Some((1, 58, 24)), None], [Some((15, 38, 32)), Some((1, 27, 28)), None], [Some((20, 20, 30)), Some((1, 20, 32)), Some((1, 21, 32))]] },
    HanXinMetric { version: "41", size: 103, alig_step: 17, alig_count: 5, cap_bits: 9009, blocks: [[Some((6, 136, 26)), Some((1, 130, 24)), None], [Some((11, 66, 28)), Some((1, 62, 30)), None], [Some((14, 34, 28)), Some((3, 34, 32)), Some((1, 30, 30))], [Some((18, 20, 30)), Some((3, 20, 28)), Some((2, 15, 26))]] },
    HanXinMetric { version: "42", size: 105, alig_step: 17, alig_count: 5, cap_bits: 9401, blocks: [[Some((5, 105, 20)), Some((2, 115, 22)), Some((2, 116, 22))], [Some((10, 75, 32)), Some((1, 73, 32)), None], [Some((16, 38, 32)), Some((1, 27, 28)), None], [Some((22, 19, 28)), Some((2, 16, 30)), Some((1, 19, 30))]] },
    HanXinMetric { version: "43", size: 107, alig_step: 18, alig_count: 5, cap_bits: 9799, blocks: [[Some((6, 147, 28)), Some((1, 146, 28)), None], [Some((11, 66, 28)), Some((2, 65, 30)), None], [Some((18, 33, 28)), Some((2, 33, 30)), None], [Some((22, 21, 32)), Some((1, 28, 30)), None]] },
    HanXinMetric { version: "44", size: 109, alig_step: 18, alig_count: 5, cap_bits: 10207, blocks: [[Some((6, 116, 22)), Some((3, 125, 24)), None], [Some((11, 75, 32)), Some((1, 68, 30)), None], [Some((13, 35, 28)), Some((6, 34, 32)), Some((1, 30, 30))], [Some((23, 21, 32)), Some((1, 26, 30)), None]] },
    HanXinMetric { version: "45", size: 111, alig_step: 18, alig_count: 5, cap_bits: 10623, blocks: [[Some((7, 105, 20)), Some((4, 95, 18)), None], [Some((12, 67, 28)), Some((1, 63, 30)), Some((1, 62, 32))], [Some((21, 31, 26)), Some((2, 33, 32)), None], [Some((23, 21, 32)), Some((2, 24, 30)), None]] },
    HanXinMetric { version: "46", size: 113, alig_step: 19, alig_count: 5, cap_bits: 11045, blocks: [[Some((10, 116, 22)), None, None], [Some((12, 74, 32)), Some((1, 78, 30)), None], [Some((18, 37, 32)), Some((1, 39, 30)), Some((1, 41, 28))], [Some((25, 21, 32)), Some((1, 27, 28)), None]] },
    HanXinMetric { version: "47", size: 115, alig_step: 19, alig_count: 5, cap_bits: 11477, blocks: [[Some((5, 126, 24)), Some((4, 115, 22)), Some((1, 114, 22))], [Some((12, 67, 28)), Some((2, 66, 32)), Some((1, 68, 30))], [Some((21, 35, 30)), Some((1, 39, 30)), None], [Some((26, 21, 32)), Some((1, 28, 28)), None]] },
    HanXinMetric { version: "48", size: 117, alig_step: 19, alig_count: 5, cap_bits: 11917, blocks: [[Some((9, 126, 24)), Some((1, 117, 22)), None], [Some((13, 75, 32)), Some((1, 68, 30)), None], [Some((20, 35, 30)), Some((3, 35, 28)), None], [Some((27, 21, 32)), Some((1, 28, 30)), None]] },
    HanXinMetric { version: "49", size: 119, alig_step: 17, alig_count: 6, cap_bits: 12111, blocks: [[Some((9, 126, 24)), Some((1, 137, 26)), None], [Some((13, 71, 30)), Some((2, 68, 32)), None], [Some((20, 37, 32)), Some((1, 39, 28)), Some((1, 38, 28))], [Some((24, 20, 32)), Some((5, 25, 28)), None]] },
    HanXinMetric { version: "50", size: 121, alig_step: 17, alig_count: 6, cap_bits: 12559, blocks: [[Some((8, 147, 28)), Some((1, 141, 28)), None], [Some((10, 73, 32)), Some((4, 74, 30)), Some((1, 73, 30))], [Some((16, 36, 32)), Some((6, 39, 30)), Some((1, 37, 30))], [Some((27, 21, 32)), Some((3, 20, 26)), None]] },
    HanXinMetric { version: "51", size: 123, alig_step: 18, alig_count: 6, cap_bits: 13025, blocks: [[Some((9, 137, 26)), Some((1, 135, 26)), None], [Some((12, 70, 30)), Some((4, 75, 32)), None], [Some((24, 35, 30)), Some((1, 40, 28)), None], [Some((23, 20, 32)), Some((8, 24, 30)), None]] },
    HanXinMetric { version: "52", size: 125, alig_step: 18, alig_count: 6, cap_bits: 13489, blocks: [[Some((14, 95, 18)), Some((1, 86, 18)), None], [Some((13, 73, 32)), Some((3, 77, 30)), None], [Some((24, 35, 30)), Some((2, 35, 28)), None], [Some((26, 21, 32)), Some((5, 21, 30)), Some((1, 23, 30))]] },
    HanXinMetric { version: "53", size: 127, alig_step: 18, alig_count: 6, cap_bits: 13961, blocks: [[Some((9, 147, 28)), Some((1, 142, 28)), None], [Some((10, 73, 30)), Some((6, 70, 32)), Some((1, 71, 32))], [Some((25, 35, 30)), Some((2, 34, 26)), None], [Some((29, 21, 32)), Some((4, 22, 30)), None]] },
    HanXinMetric { version: "54", size: 129, alig_step: 18, alig_count: 6, cap_bits: 14441, blocks: [[Some((11, 126, 24)), Some((1, 131, 24)), None], [Some((16, 74, 32)), Some((1, 79, 30)), None], [Some((25, 38, 32)), Some((1, 25, 30)), None], [Some((33, 21, 32)), Some((1, 28, 28)), None]] },
    HanXinMetric { version: "55", size: 131, alig_step: 19, alig_count: 6, cap_bits: 14939, blocks: [[Some((14, 105, 20)), Some((1, 99, 18)), None], [Some((19, 65, 28)), Some((1, 72, 28)), None], [Some((24, 37, 32)), Some((2, 40, 30)), Some((1, 41, 30))], [Some((31, 21, 32)), Some((4, 24, 32)), None]] },
    HanXinMetric { version: "56", size: 133, alig_step: 19, alig_count: 6, cap_bits: 15435, blocks: [[Some((10, 147, 28)), Some((1, 151, 28)), None], [Some((15, 71, 30)), Some((3, 71, 32)), Some((1, 73, 32))], [Some((24, 37, 32)), Some((3, 38, 30)), Some((1, 39, 30))], [Some((36, 19, 30)), Some((3, 29, 26)), None]] },
    HanXinMetric { version: "57", size: 135, alig_step: 19, alig_count: 6, cap_bits: 15939, blocks: [[Some((15, 105, 20)), Some((1, 99, 18)), None], [Some((19, 70, 30)), Some((1, 64, 28)), None], [Some((27, 38, 32)), Some((2, 25, 26)), None], [Some((38, 20, 30)), Some((2, 18, 28)), None]] },
    HanXinMetric { version: "58", size: 137, alig_step: 17, alig_count: 7, cap_bits: 16171, blocks: [[Some((14, 105, 20)), Some((1, 113, 22)), Some((1, 114, 22))], [Some((17, 67, 30)), Some((3, 92, 32)), None], [Some((30, 35, 30)), Some((1, 41, 30)), None], [Some((36, 21, 32)), Some((1, 26, 30)), Some((1, 27, 30))]] },
    HanXinMetric { version: "59", size: 139, alig_step: 17, alig_count: 7, cap_bits: 16691, blocks: [[Some((11, 146, 28)), Some((1, 146, 26)), None], [Some((20, 70, 30)), Some((1, 60, 26)), None], [Some((29, 38, 32)), Some((1, 24, 32)), None], [Some((40, 20, 30)), Some((2, 17, 26)), None]] },
    HanXinMetric { version: "60", size: 141, alig_step: 18, alig_count: 7, cap_bits: 17215, blocks: [[Some((3, 137, 26)), Some((1, 136, 26)), Some((10, 126, 24))], [Some((22, 65, 28)), Some((1, 75, 30)), None], [Some((30, 37, 32)), Some((1, 51, 30)), None], [Some((42, 20, 30)), Some((1, 21, 30)), None]] },
    HanXinMetric { version: "61", size: 143, alig_step: 18, alig_count: 7, cap_bits: 17751, blocks: [[Some((12, 126, 24)), Some((2, 118, 22)), Some((1, 116, 22))], [Some((19, 74, 32)), Some((1, 74, 30)), Some((1, 72, 28))], [Some((30, 38, 32)), Some((2, 29, 30)), None], [Some((39, 20, 32)), Some((2, 37, 26)), Some((1, 38, 26))]] },
    HanXinMetric { version: "62", size: 145, alig_step: 18, alig_count: 7, cap_bits: 18295, blocks: [[Some((12, 126, 24)), Some((3, 136, 26)), None], [Some((21, 70, 30)), Some((2, 65, 28)), None], [Some((34, 35, 30)), Some((1, 44, 32)), None], [Some((42, 20, 30)), Some((2, 19, 28)), Some((2, 18, 28))]] },
    HanXinMetric { version: "63", size: 147, alig_step: 18, alig_count: 7, cap_bits: 18847, blocks: [[Some((12, 126, 24)), Some((3, 117, 22)), Some((1, 116, 22))], [Some((25, 61, 26)), Some((2, 62, 28)), None], [Some((34, 35, 30)), Some((1, 40, 32)), Some((1, 41, 32))], [Some((45, 20, 30)), Some((1, 20, 32)), Some((1, 21, 32))]] },
    HanXinMetric { version: "64", size: 149, alig_step: 19, alig_count: 7, cap_bits: 19403, blocks: [[Some((15, 105, 20)), Some((2, 115, 22)), Some((2, 116, 22))], [Some((25, 65, 28)), Some((1, 72, 28)), None], [Some((18, 35, 30)), Some((17, 37, 32)), Some((1, 50, 32))], [Some((42, 20, 30)), Some((6, 19, 28)), Some((1, 15, 28))]] },
    HanXinMetric { version: "65", size: 151, alig_step: 19, alig_count: 7, cap_bits: 19971, blocks: [[Some((19, 105, 20)), Some((1, 101, 20)), None], [Some((33, 51, 22)), Some((1, 65, 22)), None], [Some((40, 33, 28)), Some((1, 28, 28)), None], [Some((49, 20, 30)), Some((1, 18, 28)), None]] },
    HanXinMetric { version: "66", size: 153, alig_step: 17, alig_count: 8, cap_bits: 20229, blocks: [[Some((18, 105, 20)), Some((2, 117, 22)), None], [Some((26, 65, 28)), Some((1, 80, 30)), None], [Some((35, 35, 30)), Some((3, 35, 28)), Some((1, 36, 28))], [Some((52, 18, 28)), Some((2, 38, 30)), None]] },
    HanXinMetric { version: "67", size: 155, alig_step: 17, alig_count: 8, cap_bits: 20805, blocks: [[Some((26, 84, 16)), None, None], [Some((26, 70, 30)), None, None], [Some((45, 31, 26)), Some((1, 9, 26)), None], [Some((52, 20, 30)), None, None]] },
    HanXinMetric { version: "68", size: 157, alig_step: 17, alig_count: 8, cap_bits: 21389, blocks: [[Some((16, 126, 24)), Some((1, 114, 22)), Some((1, 115, 22))], [Some((23, 70, 30)), Some((3, 65, 28)), Some((1, 66, 28))], [Some((40, 35, 30)), Some((1, 43, 30)), None], [Some((46, 20, 30)), Some((7, 19, 28)), Some((1, 16, 28))]] },
    HanXinMetric { version: "69", size: 159, alig_step: 18, alig_count: 8, cap_bits: 21993, blocks: [[Some((19, 116, 22)), Some((1, 105, 22)), None], [Some((20, 70, 30)), Some((7, 66, 28)), Some((1, 63, 28))], [Some((40, 35, 30)), Some((1, 42, 32)), Some((1, 43, 32))], [Some((54, 20, 30)), Some((1, 19, 30)), None]] },
    HanXinMetric { version: "70", size: 161, alig_step: 18, alig_count: 8, cap_bits: 22593, blocks: [[Some((17, 126, 24)), Some((2, 115, 22)), None], [Some((24, 70, 30)), Some((4, 74, 32)), None], [Some((48, 31, 26)), Some((2, 18, 26)), None], [Some((54, 19, 28)), Some((6, 15, 26)), Some((1, 14, 26))]] },
    HanXinMetric { version: "71", size: 163, alig_step: 18, alig_count: 8, cap_bits: 23201, blocks: [[Some((29, 84, 16)), None, None], [Some((29, 70, 30)), None, None], [Some((6, 34, 30)), Some((3, 36, 30)), Some((38, 33, 28))], [Some((58, 20, 30)), None, None]] },
    HanXinMetric { version: "72", size: 165, alig_step: 18, alig_count: 8, cap_bits: 23817, blocks: [[Some((16, 147, 28)), Some((1, 149, 28)), None], [Some((31, 66, 28)), Some((1, 37, 26)), None], [Some((48, 33, 28)), Some((1, 23, 26)), None], [Some((53, 20, 30)), Some((6, 19, 28)), Some((1, 17, 28))]] },
    HanXinMetric { version: "73", size: 167, alig_step: 19, alig_count: 8, cap_bits: 24453, blocks: [[Some((20, 115, 22)), Some((2, 134, 24)), None], [Some((29, 66, 28)), Some((2, 56, 26)), Some((2, 57, 26))], [Some((45, 36, 30)), Some((2, 15, 28)), None], [Some((59, 20, 30)), Some((2, 21, 32)), None]] },
    HanXinMetric { version: "74", size: 169, alig_step: 19, alig_count: 8, cap_bits: 25085, blocks: [[Some((17, 147, 28)), Some((1, 134, 26)), None], [Some((26, 70, 30)), Some((5, 75, 32)), None], [Some((47, 35, 30)), Some((1, 48, 32)), None], [Some((64, 18, 28)), Some((2, 33, 30)), Some((1, 35, 30))]] },
    HanXinMetric { version: "75", size: 171, alig_step: 17, alig_count: 9, cap_bits: 25373, blocks: [[Some((22, 115, 22)), Some((1, 133, 24)), None], [Some((33, 65, 28)), Some((1, 74, 28)), None], [Some((43, 36, 30)), Some((5, 27, 28)), Some((1, 30, 28))], [Some((57, 20, 30)), Some((5, 21, 32)), Some((1, 24, 32))]] },
    HanXinMetric { version: "76", size: 173, alig_step: 17, alig_count: 9, cap_bits: 26021, blocks: [[Some((18, 136, 26)), Some((2, 142, 26)), None], [Some((33, 66, 28)), Some((2, 49, 26)), None], [Some((48, 35, 30)), Some((2, 38, 28)), None], [Some((64, 20, 30)), Some((1, 20, 32)), None]] },
    HanXinMetric { version: "77", size: 175, alig_step: 17, alig_count: 9, cap_bits: 26677, blocks: [[Some((19, 126, 24)), Some((2, 135, 26)), Some((1, 136, 26))], [Some((32, 66, 28)), Some((2, 55, 26)), Some((2, 56, 26))], [Some((49, 36, 30)), Some((2, 18, 32)), None], [Some((65, 18, 28)), Some((5, 27, 30)), Some((1, 29, 30))]] },
    HanXinMetric { version: "78", size: 177, alig_step: 18, alig_count: 9, cap_bits: 27335, blocks: [[Some((20, 137, 26)), Some((1, 130, 26)), None], [Some((30, 75, 32)), Some((2, 71, 32)), None], [Some((46, 35, 30)), Some((6, 39, 32)), None], [Some((3, 12, 30)), Some((70, 19, 28)), None]] },
    HanXinMetric { version: "79", size: 179, alig_step: 18, alig_count: 9, cap_bits: 28007, blocks: [[Some((20, 147, 28)), None, None], [Some((35, 70, 30)), None, None], [Some((49, 35, 30)), Some((5, 35, 28)), None], [Some((70, 20, 30)), None, None]] },
    HanXinMetric { version: "80", size: 181, alig_step: 18, alig_count: 9, cap_bits: 28687, blocks: [[Some((21, 136, 26)), Some((1, 155, 28)), None], [Some((34, 70, 30)), Some((1, 64, 28)), Some((1, 65, 28))], [Some((54, 35, 30)), Some((1, 45, 30)), None], [Some((68, 20, 30)), Some((3, 18, 28)), Some((1, 19, 28))]] },
    HanXinMetric { version: "81", size: 183, alig_step: 18, alig_count: 9, cap_bits: 29375, blocks: [[Some((19, 126, 24)), Some((5, 115, 22)), Some((1, 114, 22))], [Some((33, 70, 30)), Some((3, 65, 28)), Some((1, 64, 28))], [Some((52, 35, 30)), Some((3, 41, 32)), Some((1, 40, 32))], [Some((67, 20, 30)), Some((5, 21, 32)), Some((1, 24, 32))]] },
    HanXinMetric { version: "82", size: 185, alig_step: 18, alig_count: 9, cap_bits: 30071, blocks: [[Some((2, 150, 28)), Some((21, 136, 26)), None], [Some((32, 70, 30)), Some((6, 65, 28)), None], [Some((52, 38, 32)), Some((2, 27, 32)), None], [Some((73, 20, 30)), Some((2, 22, 32)), None]] },
    HanXinMetric { version: "83", size: 187, alig_step: 17, alig_count: 10, cap_bits: 30387, blocks: [[Some((21, 126, 24)), Some((4, 136, 26)), None], [Some((30, 74, 32)), Some((6, 73, 30)), None], [Some((54, 35, 30)), Some((4, 40, 32)), None], [Some((75, 20, 30)), Some((1, 20, 28)), None]] },
    HanXinMetric { version: "84", size: 189, alig_step: 17, alig_count: 10, cap_bits: 31091, blocks: [[Some((30, 105, 20)), Some((1, 114, 22)), None], [Some((3, 45, 22)), Some((55, 47, 20)), None], [Some((2, 26, 26)), Some((62, 33, 28)), None], [Some((79, 18, 28)), Some((4, 33, 30)), None]] },
];

/// Han Xin Code finder pattern (8×8) — the four corner finders use this
/// pattern (or its 180° rotation). Per BWIPP `hanxin_fpat` (line 32311).
#[rustfmt::skip]
pub(crate) const FPAT: [[u8; 8]; 8] = [
    [1, 1, 1, 1, 1, 1, 1, 0],
    [1, 0, 0, 0, 0, 0, 0, 0],
    [1, 0, 1, 1, 1, 1, 1, 0],
    [1, 0, 1, 0, 0, 0, 0, 0],
    [1, 0, 1, 0, 1, 1, 1, 0],
    [1, 0, 1, 0, 1, 1, 1, 0],
    [1, 0, 1, 0, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
];

/// Secondary finder pattern (8×8) — used for the alignment-pattern
/// variant adjacent to the main `FPAT`. Per BWIPP `hanxin_fpat2`.
#[rustfmt::skip]
pub(crate) const FPAT2: [[u8; 8]; 8] = [
    [1, 1, 1, 0, 1, 0, 1, 0],
    [1, 1, 1, 0, 1, 0, 1, 0],
    [1, 1, 1, 0, 1, 0, 1, 0],
    [0, 0, 0, 0, 1, 0, 1, 0],
    [1, 1, 1, 1, 1, 0, 1, 0],
    [0, 0, 0, 0, 0, 0, 1, 0],
    [1, 1, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
];

/// Han Xin Code's GF(256) parameters: primitive polynomial 355 =
/// `x⁸ + x⁶ + x⁵ + x² + 1`. Distinct from the GF(256) Aztec uses
/// (poly = 301) and from QR Code's (poly = 285).
///
/// BWIPP `hanxin_rsparams[8] = [256, 355]` (line 32357).
pub(crate) const GF256_HANXIN: crate::util::rs_gf2k::GfParams = crate::util::rs_gf2k::GfParams {
    size: 256,
    poly: 355,
};

/// Han Xin Code's GF(16) parameters: poly = 19. Used for the
/// function-pattern info Reed-Solomon (BWIPP `hanxin_rsparams[4]`).
pub(crate) const GF16_HANXIN: crate::util::rs_gf2k::GfParams =
    crate::util::rs_gf2k::GfParams { size: 16, poly: 19 };

/// Compute Han Xin Code Reed-Solomon check codewords over GF(256)
/// (poly = 355). Wraps the generic [`rs_gf2k::encode_k`] for the
/// 8-bit codeword path. The output order matches BWIPP's
/// `bwipp_hanxin` placement convention — first byte placed at the
/// lowest codeword position after the data.
///
/// Returns `ecc_count` check codewords. Input `data` must contain
/// values in `0..=255`.
pub(crate) fn rs_encode_gf256(data: &[u8], ecc_count: usize) -> Vec<u8> {
    let data_u32: Vec<u32> = data.iter().map(|&b| u32::from(b)).collect();
    let check = crate::util::rs_gf2k::encode_k(&data_u32, ecc_count, GF256_HANXIN);
    // BWIPP's `bwipp_rsecbinary` produces lfsr[0..k] which is the
    // reverse of `rs_gf2k::encode_k`'s output order.
    check.into_iter().rev().map(|x| x as u8).collect()
}

/// Compute Han Xin Code Reed-Solomon check codewords over GF(16)
/// (poly = 19). Returns 4-bit values in `0..=15`.
///
/// Output order matches BWIPP's `rscodes` placement convention (the
/// same as `rs_encode_gf256`): the highest-position lfsr cell comes
/// first. Verified against an oracle for funval=336 (v1 L1 m0):
/// `rs_encode_gf16([1, 5, 0], 4) == [8, 15, 4, 12]`.
pub(crate) fn rs_encode_gf16(data: &[u8], ecc_count: usize) -> Vec<u8> {
    let data_u32: Vec<u32> = data.iter().map(|&b| u32::from(b)).collect();
    let check = crate::util::rs_gf2k::encode_k(&data_u32, ecc_count, GF16_HANXIN);
    check.into_iter().rev().map(|x| x as u8).collect()
}

/// Encode an input byte stream into Han Xin's binary-mode bit stream.
///
/// bwip-js's `bwipp_hanxin` only supports the binary/byte mode
/// (mode indicator `"0011"`), even though the GB/T 21049-2007
/// standard defines additional modes (numeric, text, Region One /
/// Two GB18030). This matches that scope.
///
/// Output bit layout (per BWIPP `bwipp_hanxin` lines 32578-32589):
/// - bits[0..4]   = `0011` — binary mode indicator
/// - bits[4..17]  = 13-bit message length (big-endian)
/// - bits[17..17+8*msglen] = each input byte as 8 bits (big-endian)
///
/// Returns `Vec<bool>` of length `17 + msglen * 8`. Caller is
/// responsible for padding to the chosen symbol's `dmod` (data bit
/// capacity) and slicing into 8-bit codewords.
pub(crate) fn encode_binary_bits(data: &[u8]) -> Result<Vec<bool>, crate::error::Error> {
    let msglen = data.len();
    if msglen >= 8192 {
        return Err(crate::error::Error::InvalidData(format!(
            "Han Xin Code: input length {msglen} exceeds 13-bit length field max (8191)",
        )));
    }
    let mut bits = Vec::with_capacity(17 + msglen * 8);
    // Mode indicator "0011".
    bits.push(false);
    bits.push(false);
    bits.push(true);
    bits.push(true);
    // 13-bit length (MSB first).
    for i in (0..13).rev() {
        bits.push((msglen >> i) & 1 == 1);
    }
    // Each byte as 8 bits (MSB first).
    for &b in data {
        for i in (0..8).rev() {
            bits.push((usize::from(b) >> i) & 1 == 1);
        }
    }
    debug_assert_eq!(bits.len(), 17 + msglen * 8);
    Ok(bits)
}

/// Select the smallest Han Xin metric that fits `bits_len` data bits
/// at the given ECC level (`'L1'`..=`'L4'`).
///
/// Returns `Some((version_index, ncws, dcws, ecbs))` or `None` if no
/// version is large enough. `ecbs` is a slice of the 3 block tuples
/// for the selected ECC level.
///
/// Per BWIPP `bwipp_hanxin` lines 32594-32616: scans the table in
/// order (versions 1..=84), picks the first whose `dmod >= bits_len`.
#[allow(clippy::type_complexity)]
pub(crate) fn select_version(
    bits_len: usize,
    eclevel: u8,
) -> Option<(usize, u32, u32, [Option<(u8, u8, u8)>; 3])> {
    let level_idx = (eclevel.saturating_sub(1)) as usize;
    if level_idx >= 4 {
        return None;
    }
    for (i, m) in HANXIN_METRICS.iter().enumerate() {
        let ncws = m.cap_bits / 8;
        let level = m.blocks[level_idx];
        let ecws: u32 = level
            .iter()
            .filter_map(|b| *b)
            .map(|(c, _d, e)| u32::from(c) * u32::from(e))
            .sum();
        let dcws = ncws - ecws;
        let dmod = dcws * 8;
        if bits_len <= dmod as usize {
            return Some((i, ncws, dcws, level));
        }
    }
    None
}

/// Slice a bit stream (padded to a multiple of 8) into 8-bit codewords,
/// each in MSB-first order. Used by the Han Xin encoder after the
/// binary-mode bits are padded to the version's `dmod` capacity.
pub(crate) fn bits_to_codewords(bits: &[bool]) -> Vec<u8> {
    let n_cws = bits.len() / 8;
    let mut out = Vec::with_capacity(n_cws);
    for cw_idx in 0..n_cws {
        let mut cw = 0u8;
        for bit_idx in 0..8 {
            if bits[cw_idx * 8 + bit_idx] {
                cw |= 1 << (7 - bit_idx);
            }
        }
        out.push(cw);
    }
    out
}

/// Apply RS-ECC per-block per the version's block layout. Returns the
/// flat codeword stream: each block's `data_size` data codewords
/// followed by its `ecc_size` ECC codewords, concatenated.
///
/// Direct port of BWIPP `bwipp_hanxin` lines 32676-32689 (the
/// `e1nb`/`e2nb`/`e3nb` loops). Each block's RS check is computed
/// independently with the block-specific `ecc_size` over GF(256).
pub(crate) fn encode_rs_blocks(
    data_cws: &[u8],
    block_layout: &[Option<(u8, u8, u8)>; 3],
) -> Vec<u8> {
    let mut data_out: Vec<u8> = Vec::new();
    let mut ecc_out: Vec<u8> = Vec::new();
    let mut data_pos = 0usize;
    for block in block_layout.iter().flatten() {
        let (count, dsize, esize) = (block.0 as usize, block.1 as usize, block.2 as usize);
        for _ in 0..count {
            let chunk = &data_cws[data_pos..data_pos + dsize];
            let ecc = rs_encode_gf256(chunk, esize);
            data_out.extend_from_slice(chunk);
            ecc_out.extend_from_slice(&ecc);
            data_pos += dsize;
        }
    }
    debug_assert_eq!(data_pos, data_cws.len());
    let mut combined = data_out;
    combined.extend_from_slice(&ecc_out);
    combined
}

/// Pad a bit stream up to `dmod` bits with `0` bits per BWIPP
/// `bwipp_hanxin` lines 32628-32629 (zero-fill of the pad buffer).
pub(crate) fn pad_to_dmod(bits: &mut Vec<bool>, dmod: usize) {
    while bits.len() < dmod {
        bits.push(false);
    }
}

/// End-to-end: take raw input bytes, produce the full Han Xin codeword
/// stream (data + ECC) ready for module placement.
///
/// Pipeline (per BWIPP `bwipp_hanxin`):
/// 1. `encode_binary_bits(data)` → bit stream (binary mode).
/// 2. `select_version(bits.len(), eclevel)` → pick smallest fitting
///    metric. Returns `(idx, ncws, dcws, layout)`.
/// 3. Pad bits to `dmod = dcws * 8` with zeros.
/// 4. Slice into `dcws` 8-bit codewords.
/// 5. RS-ECC per block layout.
///
/// Returns `(version_idx, codewords)` where `codewords.len() ==
/// ncws` (data + ECC combined).
pub(crate) fn encode_codewords(
    data: &[u8],
    eclevel: u8,
) -> Result<(usize, Vec<u8>), crate::error::Error> {
    let mut bits = encode_binary_bits(data)?;
    let (idx, _ncws, dcws, layout) = select_version(bits.len(), eclevel).ok_or_else(|| {
        crate::error::Error::InvalidData(format!(
            "Han Xin Code: input ({} bits at L{eclevel}) exceeds the largest version",
            bits.len(),
        ))
    })?;
    let dmod = (dcws as usize) * 8;
    pad_to_dmod(&mut bits, dmod);
    let data_cws = bits_to_codewords(&bits);
    debug_assert_eq!(data_cws.len(), dcws as usize);
    let combined = encode_rs_blocks(&data_cws, &layout);
    Ok((idx, combined))
}

/// Build a fresh `size × size` Han Xin pixel grid pre-filled with the
/// `-1` "unwritten" sentinel. The sentinel distinguishes "module not
/// yet placed" from "0/1 module already placed" — needed because the
/// data-placement loop only fills `-1` cells.
pub(crate) fn alloc_pixs(size: usize) -> Vec<i8> {
    vec![-1; size * size]
}

/// Place the 4 corner finder patterns on `pixs`.
///
/// Per BWIPP `bwipp_hanxin` lines 34707-34742:
/// - Top-left, top-right, bottom-right corners use [`FPAT`].
/// - Bottom-left corner uses [`FPAT2`] (the alignment-pattern variant).
///
/// Each finder is 8×8 modules. Coordinates are in (x, y) form with
/// (0, 0) at the top-left of the symbol.
pub(crate) fn place_corner_finders(pixs: &mut [i8], size: usize) {
    let idx = |x: usize, y: usize| y * size + x;
    for y in 0..8 {
        for x in 0..8 {
            let fp = FPAT[y][x] as i8;
            let fp2 = FPAT2[y][x] as i8;
            // Top-left
            pixs[idx(x, y)] = fp;
            // Top-right (mirrored across the vertical centre)
            pixs[idx(size - 1 - x, y)] = fp;
            // Bottom-right (mirrored across both axes — actually a
            // double rotation == same orientation per BWIPP)
            pixs[idx(size - 1 - x, size - 1 - y)] = fp;
            // Bottom-left uses FPAT2 (alignment variant)
            pixs[idx(x, size - 1 - y)] = fp2;
        }
    }
}

/// Transposed-mirror index: maps `(x, y)` to row-major index
/// `y * size + (size - 1 - x)` — i.e., the column is mirrored
/// horizontally. Per BWIPP `trmv` at line 34485.
fn trmv(x: usize, y: usize, size: usize) -> usize {
    y * size + (size - 1 - x)
}

/// Plot a value symmetrically at both `(x, y)` and `(y, x)` via
/// `trmv`. Per BWIPP `aplot` at lines 34490-34519. The two writes
/// cover the mirrored cell *and* the rotational symmetry partner.
fn aplot(pixs: &mut [i8], x: usize, y: usize, val: i8, size: usize) {
    pixs[trmv(x, y, size)] = val;
    pixs[trmv(y, x, size)] = val;
}

/// Place Han Xin alignment patterns for versions ≥ 4 (versions 1-3
/// have `alig_count == 0` and this returns immediately).
///
/// **Call this BEFORE [`place_corner_finders`]** — BWIPP runs the
/// alignment loop first, then the corner finders overwrite any
/// alignment cells that fall under a finder corner.
///
/// Port of BWIPP `bwipp_hanxin` lines 32754-32803. The algorithm
/// walks a staggered grid: at each `(i, j)` matching the stagger
/// condition, place a `1` cell via [`aplot`] plus a diagonal `0`
/// cell at `(i+1, j+1)` when in bounds.
///
/// The `stag` parity alternates per row. The row-stepping rule has
/// a special case at the bottom (`i + alnr == size`) where it backs
/// off by one to land on the last row.
pub(crate) fn place_alignment_patterns(pixs: &mut [i8], metric: &HanXinMetric, size: usize) {
    if metric.alig_count == 0 {
        return;
    }
    let alnk = metric.alig_step as usize;
    let alnn = metric.alig_count as usize;
    let alnr = size - alnk * alnn;
    let mut i = 0usize;
    let mut stag = 0usize;
    loop {
        if i >= size {
            break;
        }
        for j in 0..size {
            let cond = if j + alnr < size {
                ((j / alnk + stag) % 2 == 0 && !(i == 0 && j < alnk)) || (j % alnk == 0)
            } else {
                (alnn + stag) % 2 == 0
            };
            if cond {
                aplot(pixs, j, i, 1, size);
                if i + 1 < size && j + 1 < size {
                    aplot(pixs, j + 1, i + 1, 0, size);
                }
            }
        }
        if i + alnr == size {
            i = i + alnr - 1;
        } else {
            i += alnk;
        }
        stag = 1 - stag;
    }
    // BWIPP lines 32777-32803: cleanup pass that resets specific
    // alignment cells in odd alignment columns and along the right
    // edge. Iterates `i` from `alnk` to `size-2` in steps of `alnk`.
    let mut i = alnk;
    while i <= size.saturating_sub(2) {
        if (i / alnk) % 2 != 0 {
            // 9 cells at (0/1, i-1/i/i+1) zone — clears the 3×3
            // around the alignment cross's "horizontal arm" at
            // column 0/1. Per BWIPP lines 32780-32789.
            pixs[trmv(0, i - 1, size)] = 0;
            pixs[trmv(0, i + 1, size)] = 0;
            pixs[trmv(1, i - 1, size)] = 0;
            pixs[trmv(1, i, size)] = 0;
            pixs[trmv(1, i + 1, size)] = 0;
            pixs[trmv(i - 1, 0, size)] = 0;
            pixs[trmv(i + 1, 0, size)] = 0;
            pixs[trmv(i - 1, 1, size)] = 0;
            pixs[trmv(i, 1, size)] = 0;
            pixs[trmv(i + 1, 1, size)] = 0;
        }
        // Conditional cleanup if the cell at (size-1, i-1) isn't 1.
        // Per BWIPP lines 32791-32801.
        if pixs[trmv(size - 1, i - 1, size)] != 1 {
            pixs[trmv(size - 1, i - 1, size)] = 0;
            pixs[trmv(size - 2, i - 1, size)] = 0;
            pixs[trmv(size - 2, i, size)] = 0;
            pixs[trmv(size - 2, i + 1, size)] = 0;
            pixs[trmv(size - 1, i + 1, size)] = 0;
            pixs[trmv(i - 1, size - 1, size)] = 0;
            pixs[trmv(i - 1, size - 2, size)] = 0;
            pixs[trmv(i, size - 2, size)] = 0;
            pixs[trmv(i + 1, size - 2, size)] = 0;
            pixs[trmv(i + 1, size - 1, size)] = 0;
        }
        i += alnk;
    }
}

/// Enumerate the 68 cells in the Han Xin function-info zone (along
/// the inner edges of the 4 corner finders).
///
/// Direct closed-form port of BWIPP `hanxin_funmap` (34 entries × 2
/// coordinate generators = 68 cells). Verified against an oracle
/// extraction (`oracle/extract-funmap.js`) for sizes 23, 31, 41, 99,
/// and 189.
///
/// The 4 arms:
/// - Top-left arm  (entries 0-8):   `(x, 8)` and `(size-1-x, size-9)`, x in 0..=8
/// - Top arm       (entries 9-16):  `(8, 7-y)` and `(size-9, size-8+y)`, y in 0..=7
/// - Top-right arm (entries 17-25): `(size-9, y)` and `(8, size-1-y)`, y in 0..=8
/// - Bottom arm    (entries 26-33): `(size-8+x, 8)` and `(7-x, size-9)`, x in 0..=7
pub(crate) fn function_info_cells(size: usize) -> Vec<(usize, usize)> {
    let mut cells = Vec::with_capacity(68);
    for x in 0..=8 {
        cells.push((x, 8));
        cells.push((size - 1 - x, size - 9));
    }
    for y in 0..=7 {
        cells.push((8, 7 - y));
        cells.push((size - 9, size - 8 + y));
    }
    for y in 0..=8 {
        cells.push((size - 9, y));
        cells.push((8, size - 1 - y));
    }
    for x in 0..=7 {
        cells.push((size - 8 + x, 8));
        cells.push((7 - x, size - 9));
    }
    cells
}

/// Zero out the function-info zone cells in `pixs` so the
/// data-placement loop skips them. Per BWIPP `bwipp_hanxin` lines
/// 34744-34768 (the placement loop following the funmap iteration).
pub(crate) fn zero_function_info_cells(pixs: &mut [i8], size: usize) {
    for (x, y) in function_info_cells(size) {
        pixs[y * size + x] = 0;
    }
}

/// Place the data codewords into the symbol's unwritten (-1) cells.
///
/// Per BWIPP `bwipp_hanxin` lines 34810-34841: scans row-major
/// (`posy = 0..size`, `posx = 0..size`), writing the next bit
/// (MSB-first, 8 bits per codeword) into each cell currently at -1.
/// Function patterns (finders, alignment, function-info) are already
/// in place with 0/1 values and skipped.
///
/// `cws` is the final interleaved codeword stream from
/// [`interleave_codewords`] (data + ECC interleaved per 13-stride).
pub(crate) fn place_data(pixs: &mut [i8], cws: &[u8], size: usize) {
    let mut num: usize = 0;
    for posy in 0..size {
        for posx in 0..size {
            let idx = posy * size + posx;
            if pixs[idx] == -1 {
                let cw_idx = num / 8;
                let bit_idx = 7 - (num % 8);
                let bit = if cw_idx < cws.len() {
                    (cws[cw_idx] >> bit_idx) & 1
                } else {
                    0
                };
                pixs[idx] = bit as i8;
                num += 1;
            }
        }
    }
}

/// Compute Han Xin mask function `m` (0..=3) at logical coordinate
/// `(col, row)` where `col = i + 1` and `row = j + 1` (BWIPP uses
/// 1-indexed coords). Returns `0` or `1` per BWIPP `hanxin_maskfuncs`
/// (line 32360-32363):
///
/// | m | formula |
/// |---|---|
/// | 0 | always `1` — sentinel meaning "no flip anywhere" |
/// | 1 | `(col + row) % 2` |
/// | 2 | `((row + col) % 3 + col % 3) % 2` |
/// | 3 | `(col % row + (row % col + (row % 3 + col % 3))) % 2` |
///
/// Note: mask 2 uses `col % 3` (NOT `row % 3`). In the BWIPP bundle,
/// the mask functions pop from a PostScript stack — the *second* pop
/// gets `col` (the inner-loop index `i+1`), not `row`. Verified
/// against the bwip-js final-pixs oracle for v1 L2 mask 2.
pub(crate) fn mask_value(m: u8, col: usize, row: usize) -> u8 {
    match m {
        0 => 1,
        1 => ((col + row) % 2) as u8,
        2 => (((row + col) % 3 + col % 3) % 2) as u8,
        3 => {
            // Mask 3 needs col > 0 and row > 0; BWIPP always passes
            // col = i+1, row = j+1 so this is safe.
            ((col % row + (row % col + (row % 3 + col % 3))) % 2) as u8
        }
        _ => panic!("hanxin mask {} out of range (must be 0..=3)", m),
    }
}

/// Build the mask grid for mask `m`. A cell is `1` iff the cell at
/// `(i, j)` was unwritten (`pixs[idx] == -1`) AND the mask function
/// returns `0`. Otherwise `0`. Per BWIPP `bwipp_hanxin` lines
/// 34775-34808.
///
/// The mask grid is XOR'd with the data-placed `pixs` to flip
/// selected data bits — function patterns (already 0 or 1) are
/// untouched since their mask cells are `0`.
pub(crate) fn build_mask_grid(pixs: &[i8], m: u8, size: usize) -> Vec<u8> {
    let mut mask = vec![0u8; size * size];
    for j in 0..size {
        for i in 0..size {
            let r = mask_value(m, i + 1, j + 1);
            let idx = i + j * size;
            mask[idx] = if r == 0 && pixs[idx] == -1 { 1 } else { 0 };
        }
    }
    mask
}

/// XOR a mask grid into `pixs`. Function-pattern cells (currently 0
/// or 1, with their mask cells set to 0 by [`build_mask_grid`]) are
/// untouched; only data cells with `mask == 1` get flipped.
pub(crate) fn apply_mask(pixs: &mut [i8], mask: &[u8]) {
    debug_assert_eq!(pixs.len(), mask.len());
    for (p, &m) in pixs.iter_mut().zip(mask.iter()) {
        if m == 1 {
            *p ^= 1;
        }
    }
}

/// Build the 34-bit function-info stream encoding the symbol's
/// `(version, eclevel, mask)` plus 4 RS-ECC check nibbles over GF(16).
///
/// Per BWIPP `bwipp_hanxin` lines 32992-32999:
/// ```text
///   funval  = (((((size - 21) / 2) + 20) * 4) + (eclevel - 1)) * 4 + mask
///   fundata = [funval >> 8 & 15, funval >> 4 & 15, funval & 15]  (3 nibbles)
///   funecc  = rscodes(fundata, ecc_count = 4, GF(16))            (4 nibbles)
///   funbits = bits_of(fundata) ++ bits_of(funecc) ++ [0,1,0,1,0,1]
/// ```
/// Returns a 34-element `Vec<u8>` of bits (each `0` or `1`),
/// suitable for placement at the 34 funmap entries.
pub(crate) fn build_function_info_bits(version_index: usize, eclevel: u8, mask: u8) -> Vec<u8> {
    let m = &HANXIN_METRICS[version_index];
    let size = m.size as u32;
    let funval = (((((size - 21) / 2) + 20) * 4) + u32::from(eclevel - 1)) * 4 + u32::from(mask);
    let fundata: [u8; 3] = [
        ((funval >> 8) & 0xF) as u8,
        ((funval >> 4) & 0xF) as u8,
        (funval & 0xF) as u8,
    ];
    let funecc = rs_encode_gf16(&fundata, 4);
    let mut bits = Vec::with_capacity(34);
    for &nib in &fundata {
        for bit in (0..4).rev() {
            bits.push((nib >> bit) & 1);
        }
    }
    for &nib in &funecc {
        for bit in (0..4).rev() {
            bits.push((nib >> bit) & 1);
        }
    }
    bits.extend_from_slice(&[0, 1, 0, 1, 0, 1]);
    debug_assert_eq!(bits.len(), 34);
    bits
}

/// Place the 34 function-info bits into `pixs` at the funmap cell
/// coordinates. Per BWIPP `bwipp_hanxin` lines 35036-35047.
pub(crate) fn place_function_info(pixs: &mut [i8], bits: &[u8], size: usize) {
    debug_assert_eq!(bits.len(), 34);
    let cells = function_info_cells(size);
    for (i, &bit) in bits.iter().enumerate() {
        // Each funmap entry has 2 coordinate pairs; both get the same bit.
        pixs[cells[2 * i].1 * size + cells[2 * i].0] = bit as i8;
        pixs[cells[2 * i + 1].1 * size + cells[2 * i + 1].0] = bit as i8;
    }
}

/// End-to-end Han Xin encoder with a caller-supplied mask (`0..=3`).
///
/// Glues the data pipeline + module placement + masking + function-info
/// region into a single call. For automatic best-mask selection (per
/// BWIPP `evalfull` scoring), use [`encode_symbology`] which calls
/// [`pick_best_mask`] internally.
///
/// Pipeline:
/// 1. [`encode_final_codewords`] — produce the interleaved cws.
/// 2. [`alloc_pixs`] + [`place_alignment_patterns`] + [`place_corner_finders`]
///    — function patterns (alignment FIRST so finders overwrite collisions).
/// 3. [`zero_function_info_cells`] — reserve the 68 function-info cells.
/// 4. [`place_data`] — fill remaining -1 cells with cws bits.
/// 5. [`build_mask_grid`] + [`apply_mask`] — XOR the mask pattern.
/// 6. [`build_function_info_bits`] + [`place_function_info`] — encode the
///    version + ECC level + mask + RS check into the function-info zone.
///
/// Returns `(pixs, version_index)` where `pixs` is a flat `size × size`
/// grid of `0`/`1` cells (each `i8`), ready for rendering as a BitMatrix.
pub(crate) fn encode_with_mask(
    data: &[u8],
    eclevel: u8,
    mask: u8,
) -> Result<(Vec<i8>, usize), crate::error::Error> {
    if !(1..=4).contains(&eclevel) {
        return Err(crate::error::Error::InvalidData(format!(
            "Han Xin Code: eclevel must be 1..=4, got {eclevel}",
        )));
    }
    if mask > 3 {
        return Err(crate::error::Error::InvalidData(format!(
            "Han Xin Code: mask must be 0..=3, got {mask}",
        )));
    }
    let (version_index, cws) = encode_final_codewords(data, eclevel)?;
    let m = &HANXIN_METRICS[version_index];
    let size = m.size as usize;
    let mut pixs = alloc_pixs(size);
    place_alignment_patterns(&mut pixs, m, size);
    place_corner_finders(&mut pixs, size);
    zero_function_info_cells(&mut pixs, size);
    // Build the mask grid BEFORE data placement — the grid construction
    // checks for -1 cells (i.e., the data zone). After place_data, the
    // data cells are 0/1 and would no longer be identifiable.
    let mask_grid = build_mask_grid(&pixs, mask, size);
    place_data(&mut pixs, &cws, size);
    apply_mask(&mut pixs, &mask_grid);
    let funbits = build_function_info_bits(version_index, eclevel, mask);
    place_function_info(&mut pixs, &funbits, size);
    Ok((pixs, version_index))
}

/// Run-length encode a sequence of cell values into a `Vec<u32>` of
/// consecutive run lengths. Seeds with a virtual leading `0` cell so
/// the output alternates starting from "count of 0s": if the first
/// real cell is `1`, the output begins with a `0` (zero zeros before
/// the first 1). This matches BWIPP `bwipp_hanxin` lines 32940-32954
/// where the rle accumulator is initialized with `[0, 0]` (prev=0,
/// run=0).
///
/// Empty input → empty output.
pub(crate) fn rle_cells<I: Iterator<Item = i8>>(cells: I) -> Vec<u32> {
    let mut out: Vec<u32> = vec![0];
    let mut prev: i8 = 0;
    let mut first = true;
    for c in cells {
        if first {
            first = false;
            if c == prev {
                out[0] += 1;
            } else {
                out.push(1);
                prev = c;
            }
        } else if c == prev {
            if let Some(run) = out.last_mut() {
                *run += 1;
            }
        } else {
            out.push(1);
            prev = c;
        }
    }
    if first {
        // Empty input — return an empty Vec rather than `[0]`.
        return Vec::new();
    }
    out
}

/// Han Xin Code mask score components — port of BWIPP
/// `bwipp_hanxin` lines 32882-32921 (`evalfulln1n3`).
///
/// Given a run-length-encoded stripe (a column or row of the symbol),
/// return `(scr1, scr3)`:
///
/// - `scr1` (N1): `sum(4 * r for r in scrle if r >= 3)` — penalty
///   for consecutive same-color cells.
/// - `scr3` (N3): finder-lookalike penalty. For each odd-position run
///   of length `3 * fact`, if the 4 adjacent runs (either before or
///   after, scanned at stride 2) all equal `fact` AND the runs just
///   outside that block are >= 3 (or the block sits at the stripe
///   boundary), add 50.
///
/// This mirrors QR-style mask scoring but with Han Xin's specific
/// constants (`4 * r`, +50, scan window of 4, stride 2).
pub(crate) fn evalfulln1n3(scrle: &[u32]) -> (u32, u32) {
    // N1: penalty for runs of length >= 3.
    let mut scr1: u32 = 0;
    for &r in scrle {
        if r >= 3 {
            scr1 += 4 * r;
        }
    }

    // N3: finder-lookalike penalty.
    let mut scr3: u32 = 0;
    let len = scrle.len();

    // Pass 1: starting at j=5, stride 2, look at scrle[j-4..=j-1] (4 entries before j).
    let mut j = 5usize;
    while j < len {
        let val = scrle[j];
        if val % 3 == 0 {
            let fact = val / 3;
            // 4-entry pre-block window at scrle[j-4..j].
            let window_ok = scrle[j - 4..j].iter().all(|&v| v == fact);
            let edge_or_neighbor_run =
                j == 5 || j + 2 >= len || scrle[j - 5] >= 3 || scrle[j + 1] >= 3;
            if window_ok && edge_or_neighbor_run {
                scr3 += 50;
            }
        }
        j += 2;
    }

    // Pass 2: starting at j=1, stride 2, up to j <= len - 5, look at scrle[j+1..=j+4] (4 entries after j).
    if len >= 5 {
        let mut j = 1usize;
        let limit = len - 5;
        while j <= limit {
            let val = scrle[j];
            if val % 3 == 0 {
                let fact = val / 3;
                // 4-entry post-block window at scrle[j+1..=j+4].
                let window_ok = scrle[j + 1..=j + 4].iter().all(|&v| v == fact);
                let edge_or_neighbor_run =
                    j == 1 || j + 6 >= len || scrle[j - 1] >= 3 || scrle[j + 5] >= 3;
                if window_ok && edge_or_neighbor_run {
                    scr3 += 50;
                }
            }
            j += 2;
        }
    }

    (scr1, scr3)
}

/// Han Xin Code full mask score — port of BWIPP `bwipp_hanxin`
/// lines 32925-32960 (`evalfull`). For each column and row of `sym`
/// (a flat `size * size` grid of `0`/`1` cells, as produced by the
/// XOR of [`encode_with_mask`]'s pixs with a chosen mask grid),
/// build the run-length encoding and feed it to [`evalfulln1n3`].
///
/// Returns the total `n1 + n3` — lower scores are better.
pub(crate) fn evalfull(sym: &[i8], size: usize) -> u32 {
    debug_assert_eq!(sym.len(), size * size);
    let mut n1: u32 = 0;
    let mut n3: u32 = 0;
    // Column-wise (top-down).
    for i in 0..size {
        let column = (0..size).map(|y| sym[y * size + i]);
        let scrle = rle_cells(column);
        let (s1, s3) = evalfulln1n3(&scrle);
        n1 += s1;
        n3 += s3;
    }
    // Row-wise (left-right).
    for i in 0..size {
        let row_iter = sym[i * size..(i + 1) * size].iter().copied();
        let scrle = rle_cells(row_iter);
        let (s1, s3) = evalfulln1n3(&scrle);
        n1 += s1;
        n3 += s3;
    }
    n1 + n3
}

/// Compute the masked symbol grid (`pixs ⊕ mask_grid`) without
/// applying the function-info bits. Returns the result as a new
/// `Vec<i8>`. Function cells (whose mask cell is `0`) are unchanged.
fn compute_masked_symbol(pixs: &[i8], mask_grid: &[u8]) -> Vec<i8> {
    debug_assert_eq!(pixs.len(), mask_grid.len());
    pixs.iter()
        .zip(mask_grid.iter())
        .map(|(&p, &m)| if m == 1 { p ^ 1 } else { p })
        .collect()
}

/// Select the best mask `0..=3` for a Han Xin Code symbol — port of
/// the loop at BWIPP `bwipp_hanxin` lines 32966-32984.
///
/// For each mask candidate, build the masked-symbol grid (data zone
/// XOR'd with the mask pattern; function patterns unchanged), feed it
/// through [`evalfull`], and pick the mask whose score is lowest.
/// Ties are broken by the first (lowest-indexed) candidate.
///
/// `pixs_with_data` must be the pre-mask pixs with finders, alignment,
/// function-info zero-zone, and data already placed — i.e. the
/// snapshot taken right BEFORE `apply_mask` in [`encode_with_mask`].
pub(crate) fn pick_best_mask(pixs_with_data: &[i8], size: usize) -> u8 {
    let mut best_mask: u8 = 0;
    let mut best_score: u32 = u32::MAX;
    for m in 0u8..=3 {
        let grid = build_mask_grid_data_zone_only(pixs_with_data, m, size);
        let masked = compute_masked_symbol(pixs_with_data, &grid);
        let score = evalfull(&masked, size);
        if score < best_score {
            best_score = score;
            best_mask = m;
        }
    }
    best_mask
}

/// Variant of [`build_mask_grid`] that operates on a pixs snapshot
/// where the data zone has already been filled (so the `-1` sentinel
/// is gone). Instead, identify "function" cells as those that match a
/// function pattern at construction time: a cell is part of the data
/// zone iff it's NOT in a corner finder (8×8 at each corner), NOT in
/// an alignment cell, and NOT in the function-info zone (68 cells).
///
/// Used by [`pick_best_mask`] which needs to score candidate masks
/// without first rebuilding pixs from scratch each iteration.
fn build_mask_grid_data_zone_only(pixs_with_data: &[i8], m: u8, size: usize) -> Vec<u8> {
    // Re-derive the "this cell is data" predicate from the symbol
    // geometry. The function-pattern cells are:
    //  - 4 corner finders: 8x8 at (0,0), (size-8, 0), (0, size-8), (size-8, size-8)
    //  - The function-info zone: function_info_cells(size) — 68 cells
    //  - Alignment cells: only for v3+ (size >= 31). We approximate
    //    by re-running place_alignment_patterns into a scratch and
    //    checking which cells were touched.
    //
    // For now, this helper is used post-construction only — the
    // BWIPP-faithful path rebuilds pixs through the pre-mask point in
    // [`encode_with_mask`] and would call this with the same pixs we
    // built. Since [`pick_best_mask`] does that, the geometry-based
    // check below is sufficient: any cell that the corner-finder
    // placement or function-info-zero loop set is excluded; the rest
    // is data.

    // We can't easily distinguish 0/1 data cells from 0 function
    // cells just by value. Instead, build a reference "scratch" pixs
    // with finders + alignment + function-info zero placed (no data)
    // and use it to mark function cells.
    let _ = pixs_with_data;
    let mut scratch = alloc_pixs(size);
    // The metric for alignment lookup: identify which version this is.
    // Han Xin versions are ordered by size in HANXIN_METRICS; the
    // index is (size - 23) / 2 since sizes step by 2 starting at 23.
    let version_index = (size - 23) / 2;
    let metric = &HANXIN_METRICS[version_index];
    place_alignment_patterns(&mut scratch, metric, size);
    place_corner_finders(&mut scratch, size);
    zero_function_info_cells(&mut scratch, size);

    let mut mask = vec![0u8; size * size];
    for j in 0..size {
        for i in 0..size {
            let idx = i + j * size;
            let is_data = scratch[idx] == -1;
            let mv = mask_value(m, i + 1, j + 1);
            mask[idx] = if mv == 0 && is_data { 1 } else { 0 };
        }
    }
    mask
}

/// End-to-end Han Xin encoder with automatic mask selection. Identical
/// to [`encode_with_mask`] but picks the mask whose `evalfull` score
/// is lowest (per BWIPP `bwipp_hanxin` lines 32966-32984).
pub(crate) fn encode_auto(
    data: &[u8],
    eclevel: u8,
) -> Result<(Vec<i8>, usize, u8), crate::error::Error> {
    if !(1..=4).contains(&eclevel) {
        return Err(crate::error::Error::InvalidData(format!(
            "Han Xin Code: eclevel must be 1..=4, got {eclevel}",
        )));
    }
    let (version_index, cws) = encode_final_codewords(data, eclevel)?;
    let m = &HANXIN_METRICS[version_index];
    let size = m.size as usize;
    let mut pixs = alloc_pixs(size);
    place_alignment_patterns(&mut pixs, m, size);
    place_corner_finders(&mut pixs, size);
    zero_function_info_cells(&mut pixs, size);
    // We still need build_mask_grid (uses -1 sentinel) BEFORE place_data
    // for the eventually-chosen mask. But scoring also needs the
    // post-data grid. Snapshot pixs+data first, score each candidate,
    // then apply the winning mask.
    place_data(&mut pixs, &cws, size);
    let best_mask = pick_best_mask(&pixs, size);
    let mask_grid = build_mask_grid_data_zone_only(&pixs, best_mask, size);
    apply_mask(&mut pixs, &mask_grid);
    let funbits = build_function_info_bits(version_index, eclevel, best_mask);
    place_function_info(&mut pixs, &funbits, size);
    Ok((pixs, version_index, best_mask))
}

/// Apply Han Xin's 13-stride codeword interleave per BWIPP
/// `bwipp_hanxin` lines 32715-32717:
///
/// ```text
/// for k in 0..=min(12, ncws-1):
///   for i in (k, k+13, k+26, …) where i < ncws:
///     emit cws[i]
/// ```
///
/// Appends a trailing zero codeword if the symbol has slack bits
/// (`cap_bits % 8 > 0`).
pub(crate) fn interleave_codewords(cws: &[u8], rbit: u32) -> Vec<u8> {
    let ncws = cws.len();
    let max_k = (ncws.saturating_sub(1)).min(12);
    let mut out: Vec<u8> = Vec::with_capacity(ncws + if rbit > 0 { 1 } else { 0 });
    for k in 0..=max_k {
        let mut i = k;
        while i < ncws {
            out.push(cws[i]);
            i += 13;
        }
    }
    debug_assert_eq!(out.len(), ncws);
    if rbit > 0 {
        out.push(0);
    }
    out
}

/// End-to-end: produce the FINAL interleaved codeword stream ready
/// for module placement. Builds on [`encode_codewords`] and then
/// applies [`interleave_codewords`].
///
/// Returns `(version_idx, interleaved_cws)`.
pub(crate) fn encode_final_codewords(
    data: &[u8],
    eclevel: u8,
) -> Result<(usize, Vec<u8>), crate::error::Error> {
    let (idx, combined) = encode_codewords(data, eclevel)?;
    let rbit = HANXIN_METRICS[idx].cap_bits % 8;
    Ok((idx, interleave_codewords(&combined, rbit)))
}

fn extras_lookup<'a>(opts: &'a crate::options::Options, key: &str) -> Option<&'a str> {
    opts.extras
        .iter()
        .find_map(|(k, v)| if k == key { Some(v.as_str()) } else { None })
}

/// Parse the `eclevel` extra (`"L1".."L4"`, default `"L1"`).
fn parse_eclevel(opts: &crate::options::Options) -> Result<u8, crate::error::Error> {
    let raw = extras_lookup(opts, "eclevel").unwrap_or("L1");
    match raw.trim().to_ascii_uppercase().as_str() {
        "L1" | "1" => Ok(1),
        "L2" | "2" => Ok(2),
        "L3" | "3" => Ok(3),
        "L4" | "4" => Ok(4),
        _ => Err(crate::error::Error::InvalidData(format!(
            "Han Xin Code: invalid eclevel {raw:?}; expected L1..L4",
        ))),
    }
}

/// Parse the optional `mask` extra (`"0".."3"`). Returns `Ok(None)`
/// when absent — caller treats that as "auto-pick via evalfull".
/// Uses BWIPP's internal 0-indexed mask numbering (not bwip-js's
/// 1-indexed option).
fn parse_mask_opt(opts: &crate::options::Options) -> Result<Option<u8>, crate::error::Error> {
    match extras_lookup(opts, "mask") {
        None => Ok(None),
        Some(raw) => match raw.trim() {
            "0" => Ok(Some(0)),
            "1" => Ok(Some(1)),
            "2" => Ok(Some(2)),
            "3" => Ok(Some(3)),
            _ => Err(crate::error::Error::InvalidData(format!(
                "Han Xin Code: invalid mask {raw:?}; expected 0..=3",
            ))),
        },
    }
}

/// Han Xin Code Symbology dispatch entry point. Reads `eclevel` and
/// (optional) `mask` from `opts.extras` (see [`Symbology::HanXinCode`]
/// docs) and renders the result to a [`BitMatrix`].
///
/// When `mask` is absent, picks the mask whose `evalfull` score is
/// lowest (per BWIPP `bwipp_hanxin` lines 32966-32984).
///
/// [`Symbology::HanXinCode`]: crate::Symbology::HanXinCode
/// [`BitMatrix`]: crate::encoding::BitMatrix
pub fn encode_symbology(
    data: &str,
    opts: &crate::options::Options,
) -> Result<crate::encoding::BitMatrix, crate::error::Error> {
    let eclevel = parse_eclevel(opts)?;
    let (pixs, version_index) = match parse_mask_opt(opts)? {
        Some(mask) => encode_with_mask(data.as_bytes(), eclevel, mask)?,
        None => {
            let (p, v, _best) = encode_auto(data.as_bytes(), eclevel)?;
            (p, v)
        }
    };
    let size = HANXIN_METRICS[version_index].size as usize;
    let mut bm = crate::encoding::BitMatrix::new(size, size);
    for y in 0..size {
        for x in 0..size {
            if pixs[y * size + x] == 1 {
                bm.set(x, y, true);
            }
        }
    }
    Ok(bm)
}

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

    #[test]
    fn fpat_is_8x8() {
        assert_eq!(FPAT.len(), 8);
        for row in &FPAT {
            assert_eq!(row.len(), 8);
        }
    }

    #[test]
    fn fpat2_is_8x8() {
        assert_eq!(FPAT2.len(), 8);
        for row in &FPAT2 {
            assert_eq!(row.len(), 8);
        }
    }

    #[test]
    fn fpat_first_row_matches_bwipp() {
        // BWIPP hanxin_fpat first row: [1,1,1,1,1,1,1,0].
        assert_eq!(FPAT[0], [1, 1, 1, 1, 1, 1, 1, 0]);
    }

    #[test]
    fn fpat2_first_row_matches_bwipp() {
        assert_eq!(FPAT2[0], [1, 1, 1, 0, 1, 0, 1, 0]);
    }

    #[test]
    fn encode_symbology_round_trips_via_options() {
        // With an explicit mask extra, encode_symbology must agree
        // with encode_with_mask byte-for-byte. (When `mask` is
        // omitted, the wrapper auto-picks — covered separately by
        // encode_symbology_auto_picks_best_mask_when_extras_omit_mask.)
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // Han Xin V1 23×23 + mask=0 explicit path.
        let mut opts = crate::options::Options::default();
        opts.extras.push(("mask".into(), "0".into()));
        let bm = encode_symbology("A", &opts).expect(
            "encode_symbology(\"A\", mask=0) (Han Xin V1 23×23, explicit mask=0) must succeed",
        );
        assert_eq!(bm.width(), 23);
        assert_eq!(bm.height(), 23);
        let (pixs, _) = encode_with_mask(b"A", 1, 0).expect(
            "encode_with_mask(b\"A\", eclevel=1, mask=0) (parallel reference path for BitMatrix cross-check) must succeed",
        );
        for y in 0..23 {
            for x in 0..23 {
                let expected = pixs[y * 23 + x] == 1;
                assert_eq!(bm.get(x, y), expected, "BitMatrix cell ({x},{y}) mismatch",);
            }
        }
    }

    #[test]
    fn encode_symbology_picks_eclevel_extra() {
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // Han Xin eclevel=L2 + mask=2 extras-thread-through path.
        let mut opts = crate::options::Options::default();
        opts.extras.push(("eclevel".into(), "L2".into()));
        opts.extras.push(("mask".into(), "2".into()));
        let bm = encode_symbology("HELLO", &opts).expect(
            "encode_symbology(\"HELLO\", eclevel=L2, mask=2) (extras thread through to encode_with_mask) must succeed",
        );
        let (pixs, _) = encode_with_mask(b"HELLO", 2, 2).expect(
            "encode_with_mask(b\"HELLO\", eclevel=2, mask=2) (parallel reference path for BitMatrix cross-check) must succeed",
        );
        let size = 23usize;
        for y in 0..size {
            for x in 0..size {
                let expected = pixs[y * size + x] == 1;
                assert_eq!(bm.get(x, y), expected, "({x},{y}) mismatch");
            }
        }
    }

    #[test]
    fn encode_symbology_rejects_bad_eclevel() {
        // Stage 11.A8c (cont) — single-anchor `format!("{err}").contains("eclevel")`
        // would survive ANY mutation that emits a generic "eclevel" word in
        // any rejection diagnostic (parse_eclevel has TWO call sites in
        // hanxin.rs — both produce diagnostics containing "eclevel"). Upgrade
        // to 5-anchor pin matching parse_eclevel diagnostic at line 1093-1095
        // (`Han Xin Code: invalid eclevel {raw:?}; expected L1..L4`):
        //   1. variant pin (was untyped `unwrap_err()`)
        //   2. `Han Xin Code:` symbology prefix
        //   3. `invalid eclevel` predicate
        //   4. `"L9"` raw-Debug echo (the offending option value)
        //   5. `L1..L4` valid-range hint
        // Plus a cross-arm guard against the parse_mask_opt diagnostic
        // (would catch a mutation re-routing eclevel option through the
        // mask option path).
        let mut opts = crate::options::Options::default();
        opts.extras.push(("eclevel".into(), "L9".into()));
        let msg = match encode_symbology("A", &opts).unwrap_err() {
            crate::error::Error::InvalidData(m) => m,
            err => panic!(
                "encode_symbology(\"A\", eclevel=\"L9\") must reject as Err(InvalidData(parse_eclevel)); got {err:?}"
            ),
        };
        assert!(
            msg.contains("Han Xin Code:"),
            "missing `Han Xin Code:` symbology prefix: {msg:?}"
        );
        assert!(
            msg.contains("invalid eclevel"),
            "missing `invalid eclevel` predicate: {msg:?}"
        );
        assert!(
            msg.contains("\"L9\""),
            "missing `\"L9\"` raw-Debug echo: {msg:?}"
        );
        assert!(
            msg.contains("L1..L4"),
            "missing `L1..L4` valid-range hint: {msg:?}"
        );
        assert!(
            !msg.contains("invalid mask") && !msg.contains("0..=3"),
            "cross-arm contamination — bad-eclevel reject leaked mask-arm diagnostic: {msg:?}"
        );
    }

    #[test]
    fn encode_symbology_rejects_bad_mask() {
        // Stage 11.A8c (cont) — symmetric upgrade to the eclevel test
        // above. parse_mask_opt produces (line 1111-1113):
        //   `Han Xin Code: invalid mask {raw:?}; expected 0..=3`
        // 5-anchor pin + cross-arm guard against eclevel-arm leakage.
        let mut opts = crate::options::Options::default();
        opts.extras.push(("mask".into(), "9".into()));
        let msg = match encode_symbology("A", &opts).unwrap_err() {
            crate::error::Error::InvalidData(m) => m,
            err => panic!(
                "encode_symbology(\"A\", mask=\"9\") must reject as Err(InvalidData(parse_mask_opt)); got {err:?}"
            ),
        };
        assert!(
            msg.contains("Han Xin Code:"),
            "missing `Han Xin Code:` symbology prefix: {msg:?}"
        );
        assert!(
            msg.contains("invalid mask"),
            "missing `invalid mask` predicate: {msg:?}"
        );
        assert!(
            msg.contains("\"9\""),
            "missing `\"9\"` raw-Debug echo: {msg:?}"
        );
        assert!(
            msg.contains("0..=3"),
            "missing `0..=3` valid-range hint: {msg:?}"
        );
        assert!(
            !msg.contains("invalid eclevel") && !msg.contains("L1..L4"),
            "cross-arm contamination — bad-mask reject leaked eclevel-arm diagnostic: {msg:?}"
        );
    }

    #[test]
    fn evalfull_scores_match_bwip_js_a_l1() {
        // BWIPP oracle for input "A" L1: scores per mask =
        //   [4628, 3474, 3644, 3966], bestmaskval = 1.
        let mut pixs = alloc_pixs(23);
        place_alignment_patterns(&mut pixs, &HANXIN_METRICS[0], 23);
        place_corner_finders(&mut pixs, 23);
        zero_function_info_cells(&mut pixs, 23);
        // Build cws via the data path.
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // Han Xin V1 L1 codeword build.
        let (idx, cws) = encode_final_codewords(b"A", 1).expect(
            "encode_final_codewords(b\"A\", L1) (Han Xin V1 L1 codeword build for mask-scoring evalfull oracle) must succeed",
        );
        assert_eq!(idx, 0);
        place_data(&mut pixs, &cws, 23);
        // Score each mask candidate.
        let want: [u32; 4] = [4628, 3474, 3644, 3966];
        for (m, w) in want.iter().enumerate() {
            let grid = build_mask_grid_data_zone_only(&pixs, m as u8, 23);
            let masked = compute_masked_symbol(&pixs, &grid);
            let s = evalfull(&masked, 23);
            assert_eq!(s, *w, "mask {m}: got {s} want {w}");
        }
        // pick_best_mask must return 1 for this input.
        let best = pick_best_mask(&pixs, 23);
        assert_eq!(best, 1);
    }

    #[test]
    fn evalfull_scores_match_bwip_js_hello_l2() {
        // BWIPP oracle for input "HELLO" L2:
        //   [4156, 3818, 3852, 3964], bestmaskval = 1.
        let mut pixs = alloc_pixs(23);
        place_alignment_patterns(&mut pixs, &HANXIN_METRICS[0], 23);
        place_corner_finders(&mut pixs, 23);
        zero_function_info_cells(&mut pixs, 23);
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // Han Xin V1 L2 codeword build (HELLO 5-char payload).
        let (idx, cws) = encode_final_codewords(b"HELLO", 2).expect(
            "encode_final_codewords(b\"HELLO\", L2) (Han Xin V1 L2 codeword build for evalfull oracle, 5-char payload) must succeed",
        );
        assert_eq!(idx, 0);
        place_data(&mut pixs, &cws, 23);
        let want: [u32; 4] = [4156, 3818, 3852, 3964];
        for (m, w) in want.iter().enumerate() {
            let grid = build_mask_grid_data_zone_only(&pixs, m as u8, 23);
            let masked = compute_masked_symbol(&pixs, &grid);
            let s = evalfull(&masked, 23);
            assert_eq!(s, *w, "mask {m}: got {s} want {w}");
        }
        let best = pick_best_mask(&pixs, 23);
        assert_eq!(best, 1);
    }

    #[test]
    fn encode_auto_matches_explicit_mask_for_best_choice() {
        // The auto encoder must produce the same pixs as
        // encode_with_mask(b"A", 1, best_mask). We know best=1 for
        // input "A" L1 (verified above).
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming the
        // Han Xin V1 L1 auto-pick path: encode_auto threads
        // pixs+version+best_mask through evalfull mask scoring, and
        // encode_with_mask must reproduce the same pixs when given the
        // best mask. Both calls share the same V1 23×23 geometry path.
        let (auto_pixs, _, best_mask) = encode_auto(b"A", 1).expect(
            "encode_auto(b\"A\", L1) (Han Xin V1 L1 auto-pick path; selects best mask via evalfull scoring) must succeed",
        );
        let (explicit_pixs, _) = encode_with_mask(b"A", 1, best_mask).expect(
            "encode_with_mask(b\"A\", L1, best_mask) (Han Xin V1 L1 explicit-mask path; must reproduce auto-pick pixs) must succeed",
        );
        assert_eq!(best_mask, 1);
        assert_eq!(auto_pixs, explicit_pixs);
    }

    #[test]
    fn encode_auto_matches_bwip_js_a_l1_full_pixs() {
        // End-to-end auto-pick oracle: input "A" L1 with NO mask
        // option to bwip-js → bestmaskval = 1, pixs captured at
        // //#33025. Verifies the entire pipeline including
        // evalfull scoring.
        #[rustfmt::skip]
        let want: [u8; 529] = [
            1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
            1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1,
            0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
            1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
            0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
            1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0,
            0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0,
            1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
        ];
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming the
        // end-to-end Han Xin V1 L1 auto-pick path feeding the bwip-js
        // 529-byte pixs golden (input "A", auto-mask).
        let (pixs, _, best_mask) = encode_auto(b"A", 1).expect(
            "encode_auto(b\"A\", L1) (Han Xin V1 L1 end-to-end auto-pick path feeding 529-byte bwip-js pixs golden) must succeed",
        );
        assert_eq!(best_mask, 1, "auto-pick must select mask 1 for 'A' L1");
        let got: Vec<u8> = pixs.iter().map(|&v| v as u8).collect();
        assert_eq!(
            got,
            want.to_vec(),
            "Han Xin auto pixs must match bwip-js byte-for-byte (A, L1, auto-mask)",
        );
    }

    #[test]
    fn encode_symbology_auto_picks_best_mask_when_extras_omit_mask() {
        // No `mask` extra → auto pick. Output must equal encode_auto.
        let mut opts = crate::options::Options::default();
        opts.extras.push(("eclevel".into(), "L1".into()));
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming the
        // Symbology dispatch path: encode_symbology threads eclevel
        // extras through to encode_auto when no `mask` extra is
        // supplied; the BitMatrix output must match encode_auto's pixs
        // cell-for-cell across the V1 23×23 grid.
        let bm = encode_symbology("A", &opts).expect(
            "encode_symbology(\"A\", eclevel=L1 only) (Han Xin Symbology dispatch path; no `mask` extra → auto-pick via encode_auto) must succeed",
        );
        let (auto_pixs, _, _) = encode_auto(b"A", 1).expect(
            "encode_auto(b\"A\", L1) (Han Xin V1 L1 auto-pick reference for Symbology cross-check) must succeed",
        );
        for y in 0..23 {
            for x in 0..23 {
                let expected = auto_pixs[y * 23 + x] == 1;
                assert_eq!(bm.get(x, y), expected, "({x},{y})");
            }
        }
    }

    #[test]
    fn evalfull_scores_match_bwip_js_corpus() {
        // 24-case corpus from oracle-hanxin-evalfull.js: 6 inputs × 4
        // ECC levels. Each row is (input, eclevel, [score0, score1,
        // score2, score3], expected_best_mask, expected_size).
        // The "Hello World" L3/L4 cases exercise v2 (size 25) too.
        #[allow(clippy::type_complexity)]
        let corpus: &[(&[u8], u8, [u32; 4], u8, usize)] = &[
            (b"A", 1, [4628, 3474, 3644, 3966], 1, 23),
            (b"A", 2, [4396, 3864, 3752, 3968], 2, 23),
            (b"A", 3, [4180, 3820, 3884, 3838], 1, 23),
            (b"A", 4, [3956, 3832, 4024, 3868], 1, 23),
            (b"B", 1, [4548, 3430, 3596, 3908], 1, 23),
            (b"B", 2, [4376, 3820, 3728, 3936], 2, 23),
            (b"B", 3, [4256, 3806, 3768, 3772], 2, 23),
            (b"B", 4, [4176, 3810, 3816, 3864], 1, 23),
            (b"C", 1, [4576, 3416, 3708, 3984], 1, 23),
            (b"C", 2, [4400, 3682, 3728, 3840], 1, 23),
            (b"C", 3, [4212, 3770, 3954, 3910], 1, 23),
            (b"C", 4, [4084, 3764, 3960, 3962], 1, 23),
            (b"1", 1, [4588, 3468, 3700, 4050], 1, 23),
            (b"1", 2, [4532, 3754, 3716, 3988], 2, 23),
            (b"1", 3, [4324, 3692, 3770, 3856], 1, 23),
            (b"1", 4, [4084, 3752, 3800, 3966], 1, 23),
            (b"Hello World", 1, [4120, 3884, 3892, 3840], 3, 23),
            (b"Hello World", 2, [4064, 3888, 4010, 3916], 1, 23),
            (b"Hello World", 3, [4454, 4388, 4248, 4450], 2, 25),
            (b"Hello World", 4, [4338, 4206, 4272, 4232], 1, 25),
            (b"ABC123", 1, [4388, 3746, 3776, 3922], 1, 23),
            (b"ABC123", 2, [4352, 3986, 3860, 3928], 2, 23),
            (b"ABC123", 3, [4000, 3810, 3768, 3802], 2, 23),
            (b"ABC123", 4, [3996, 3754, 4054, 3920], 1, 23),
        ];
        for (data, eclevel, scores, expected_best, expected_size) in corpus {
            // Stage 11.A8c (cont) — per-iteration `.unwrap()` →
            // `.unwrap_or_else(|e| panic!(...))` naming the (data,
            // eclevel) row so a mutant that flips a corpus dispatch
            // arm reports which row dispatched (utf-8-safe echo for
            // byte-slice corpus rows).
            let data_echo = std::str::from_utf8(data).unwrap_or("<non-utf8>");
            let (version_index, cws) = encode_final_codewords(data, *eclevel).unwrap_or_else(|e| {
                panic!(
                    "encode_final_codewords({data_echo:?}, L{eclevel}) (Han Xin evalfull corpus row: codeword build feeding 4-mask evalfull oracle + pick_best_mask) must succeed: {e:?}",
                )
            });
            let m = &HANXIN_METRICS[version_index];
            let size = m.size as usize;
            assert_eq!(size, *expected_size, "version size for {data:?} L{eclevel}",);
            let mut pixs = alloc_pixs(size);
            place_alignment_patterns(&mut pixs, m, size);
            place_corner_finders(&mut pixs, size);
            zero_function_info_cells(&mut pixs, size);
            place_data(&mut pixs, &cws, size);
            for (mi, w) in scores.iter().enumerate() {
                let grid = build_mask_grid_data_zone_only(&pixs, mi as u8, size);
                let masked = compute_masked_symbol(&pixs, &grid);
                let s = evalfull(&masked, size);
                assert_eq!(
                    s, *w,
                    "score mismatch: {data:?} L{eclevel} mask={mi}: got {s} want {w}",
                );
            }
            let best = pick_best_mask(&pixs, size);
            assert_eq!(
                best, *expected_best,
                "best mask: {data:?} L{eclevel}: got {best} want {expected_best}",
            );
        }
    }

    #[test]
    fn rle_cells_basic() {
        // BWIPP-style RLE seeds with virtual leading 0 → first entry
        // is always the count of leading 0s (possibly 0).
        let v: Vec<i8> = vec![0, 0, 0, 1, 1, 0, 0, 0, 0];
        assert_eq!(rle_cells(v.into_iter()), vec![3, 2, 4]);
        // Empty input.
        let empty: Vec<i8> = vec![];
        assert_eq!(rle_cells(empty.into_iter()), Vec::<u32>::new());
        // All 1s: leading 0-count is 0, then ten 1s.
        let all_ones: Vec<i8> = vec![1; 10];
        assert_eq!(rle_cells(all_ones.into_iter()), vec![0, 10]);
        // All 0s: just one run of 10.
        let all_zeros: Vec<i8> = vec![0; 10];
        assert_eq!(rle_cells(all_zeros.into_iter()), vec![10]);
        // Verify against the BWIPP trace example [1, 1, 0] → [0, 2, 1].
        let trace: Vec<i8> = vec![1, 1, 0];
        assert_eq!(rle_cells(trace.into_iter()), vec![0, 2, 1]);
    }

    #[test]
    fn evalfulln1n3_n1_basic() {
        // Sum of 4*r for r >= 3. [3, 1, 4, 2, 5] → 4*3 + 4*4 + 4*5 = 48.
        let (n1, n3) = evalfulln1n3(&[3, 1, 4, 2, 5]);
        assert_eq!(n1, 48);
        // n3 has nothing to detect here (no 3*fact pattern with the 4 prevs).
        assert_eq!(n3, 0);
    }

    #[test]
    fn evalfulln1n3_n3_pre_block_pattern() {
        // [1, 1, 1, 1, 3] at j=5? len=5 so loop doesn't run. Make len>=6.
        // Need scrle[5] divisible by 3 with scrle[1..=4] all equal scrle[5]/3.
        // [1, 1, 1, 1, 1, 3] — scrle[5]=3, fact=1, scrle[1..=4]=[1,1,1,1] all=1. j=5.
        // j==5 → +50.
        let (_, n3) = evalfulln1n3(&[1, 1, 1, 1, 1, 3]);
        assert_eq!(n3, 50);
    }

    #[test]
    fn noblk_sentinel_matches_bwipp() {
        assert_eq!(NOBLK, None);
    }

    #[test]
    fn hanxin_metrics_table_has_84_rows() {
        assert_eq!(HANXIN_METRICS.len(), 84);
    }

    #[test]
    fn hanxin_metrics_version_1_matches_bwipp() {
        // BWIPP `hanxin_metrics[0]`:
        //   ["1", 23, -1, 0, 205, [1, 21, 4], NOBLK, NOBLK,
        //                          [1, 17, 8], NOBLK, NOBLK,
        //                          [1, 13, 12], NOBLK, NOBLK,
        //                          [1, 9, 16], NOBLK, NOBLK]
        let m = &HANXIN_METRICS[0];
        assert_eq!(m.version, "1");
        assert_eq!(m.size, 23);
        assert_eq!(m.alig_step, -1);
        assert_eq!(m.alig_count, 0);
        assert_eq!(m.cap_bits, 205);
        assert_eq!(m.blocks[0], [Some((1, 21, 4)), None, None]);
        assert_eq!(m.blocks[1], [Some((1, 17, 8)), None, None]);
        assert_eq!(m.blocks[2], [Some((1, 13, 12)), None, None]);
        assert_eq!(m.blocks[3], [Some((1, 9, 16)), None, None]);
    }

    #[test]
    fn hanxin_metrics_version_84_matches_bwipp() {
        // BWIPP `hanxin_metrics[83]`: 189-module symbol with the
        // most complex block layout in the table.
        let m = &HANXIN_METRICS[83];
        assert_eq!(m.version, "84");
        assert_eq!(m.size, 189);
        assert_eq!(m.alig_step, 17);
        assert_eq!(m.alig_count, 10);
        assert_eq!(m.cap_bits, 31091);
        assert_eq!(m.blocks[0], [Some((30, 105, 20)), Some((1, 114, 22)), None]);
        assert_eq!(m.blocks[3], [Some((79, 18, 28)), Some((4, 33, 30)), None]);
    }

    #[test]
    fn hanxin_metrics_versions_are_consecutive() {
        for (i, m) in HANXIN_METRICS.iter().enumerate() {
            let expected = (i + 1).to_string();
            assert_eq!(m.version, expected, "metric[{i}] version mismatch");
        }
    }

    #[test]
    fn hanxin_metrics_size_is_odd_and_increasing() {
        let mut prev_size = 0u8;
        for m in HANXIN_METRICS.iter() {
            assert!(
                m.size % 2 == 1,
                "version {} size {} not odd",
                m.version,
                m.size
            );
            assert!(
                m.size > prev_size,
                "version {} size {} not increasing",
                m.version,
                m.size
            );
            prev_size = m.size;
        }
    }

    #[test]
    fn rs_encode_gf256_matches_oracle() {
        // Oracle bytes from oracle-hanxin-rs-direct.js (GF(256), poly 355).
        // Note: the reference impl produces the bytes in REVERSE of BWIPP's
        // placement order, so `rs_encode_gf256` matches BWIPP — we reverse
        // the reference values here.
        // data=[1..=8], k=4 (ref) → [114, 157, 64, 37], BWIPP → [37, 64, 157, 114]
        let data: [u8; 8] = [1, 2, 3, 4, 5, 6, 7, 8];
        assert_eq!(rs_encode_gf256(&data, 4), vec![37, 64, 157, 114]);
        // k=8 (ref) → [172, 160, 244, 28, 120, 51, 159, 243], BWIPP order:
        assert_eq!(
            rs_encode_gf256(&data, 8),
            vec![243, 159, 51, 120, 28, 244, 160, 172],
        );
        // Hex pattern, k=8.
        let data2: [u8; 8] = [0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xBA, 0xBE];
        assert_eq!(
            rs_encode_gf256(&data2, 8),
            vec![192, 221, 74, 236, 166, 89, 3, 128],
        );
    }

    #[test]
    fn rs_encode_gf16_matches_bwip_js_funecc_v1_l1_m0() {
        // Oracle: input fundata=[1, 5, 0] (the funval=336 nibbles for
        // v1 L1 m0), 4 check codewords. From BWIPP funbits dump:
        // bits[12..28] = [1,0,0,0, 1,1,1,1, 0,1,0,0, 1,1,0,0]
        //              = [8, 15, 4, 12].
        let funecc = rs_encode_gf16(&[1, 5, 0], 4);
        assert_eq!(funecc, vec![8, 15, 4, 12]);
    }

    #[test]
    fn rs_encode_gf16_basic_smoke() {
        // GF(16) basic smoke — just exercise the path with poly 19.
        let data: [u8; 4] = [1, 2, 3, 4];
        let r = rs_encode_gf16(&data, 4);
        assert_eq!(r.len(), 4);
        for v in &r {
            assert!(*v <= 15, "gf16 element must be 0..=15");
        }
    }

    #[test]
    fn gf256_hanxin_poly_is_355() {
        assert_eq!(GF256_HANXIN.poly, 355);
        assert_eq!(GF256_HANXIN.size, 256);
    }

    /// Stage 11.A8c — pin `pad_to_dmod` zero-padding semantics.
    ///
    /// Mutations caught:
    ///   * `while bits.len() < dmod` → `<= dmod` adds one extra zero.
    ///   * `push(false)` → `push(true)` pads with ones.
    ///   * Bounds inversion makes it a no-op or infinite loop.
    #[test]
    fn pad_to_dmod_extends_with_zeros_only_below_target() {
        // Pad from 2 → 5: 3 zeros appended.
        let mut bits = vec![true, false];
        pad_to_dmod(&mut bits, 5);
        assert_eq!(bits, vec![true, false, false, false, false]);

        // Already at target → no change.
        let mut bits = vec![true, true];
        pad_to_dmod(&mut bits, 2);
        assert_eq!(bits, vec![true, true]);

        // Already above target → no shrink.
        let mut bits = vec![true; 5];
        pad_to_dmod(&mut bits, 3);
        assert_eq!(bits, vec![true; 5]);

        // Empty → pad with all-zero.
        let mut bits = vec![];
        pad_to_dmod(&mut bits, 4);
        assert_eq!(bits, vec![false, false, false, false]);

        // Zero target on empty stays empty.
        let mut bits = vec![];
        pad_to_dmod(&mut bits, 0);
        assert_eq!(bits, Vec::<bool>::new());
    }

    #[test]
    fn encode_binary_bits_layout() {
        // For input "A" (msglen=1, byte=0x41):
        //   bits = "0011" + "0000000000001" + "01000001"
        //         = 4 + 13 + 8 = 25 bits.
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the Binary-mode encoder path: 4-bit mode indicator (0b0011)
        // + 13-bit length field + 8-bit body. Pins the byte-layout
        // contract that the rest of this test verifies.
        let bits = encode_binary_bits(b"A").expect(
            "encode_binary_bits(b\"A\") (Han Xin Binary mode: 4-bit mode 0b0011 + 13-bit length + 8-bit body) must succeed",
        );
        assert_eq!(bits.len(), 25);
        // Mode: 0011
        assert_eq!(&bits[..4], &[false, false, true, true]);
        // Length: 13 bits of value 1 = 0000000000001
        let mut len_val = 0u16;
        for &b in &bits[4..17] {
            len_val = (len_val << 1) | u16::from(b);
        }
        assert_eq!(len_val, 1);
        // Byte 0x41 = 01000001
        let mut byte_val = 0u8;
        for &b in &bits[17..25] {
            byte_val = (byte_val << 1) | u8::from(b);
        }
        assert_eq!(byte_val, 0x41);
    }

    #[test]
    fn encode_binary_bits_rejects_oversize() {
        // 8192-byte input would overflow the 13-bit length field.
        //
        // Stage 11.A8c — upgrade from bare is_err() to pin the
        // diagnostic substring + length echo. The rejection arm at
        // line 256-258 of hanxin.rs produces:
        //   "Han Xin Code: input length 8192 exceeds 13-bit length
        //    field max (8191)"
        //
        // A mutant that drops `{msglen}` echo or swaps the
        // 13-bit/8191 hint survives bare is_err() checks.
        let big = vec![0u8; 8192];
        let err = encode_binary_bits(&big).unwrap_err();
        let crate::error::Error::InvalidData(msg) = err else {
            panic!("oversize binary bits must yield InvalidData; got {err:?}");
        };
        assert!(
            msg.contains("Han Xin Code:")
                && msg.contains("8192")
                && msg.contains("13-bit length field")
                && msg.contains("8191"),
            "diagnostic must pin symbology tag + length echo + 13-bit field hint + max (8191); \
             got {msg:?}"
        );
    }

    #[test]
    fn select_version_picks_smallest_fitting() {
        // For 25 bits (one "A"), the smallest version's L1 layout has
        // dcws=21 codewords × 8 = 168 bits — easily fits.
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the version-selector smallest-fit path: 25 bits + L1 → V1
        // (ncws=25, dcws=21).
        let (idx, ncws, dcws, _) = select_version(25, 1).expect(
            "select_version(25 bits, L1) (Han Xin version-selector smallest-fit path; expects V1 ncws=25 dcws=21) must succeed",
        );
        assert_eq!(idx, 0); // Version 1.
        assert_eq!(ncws, 25); // 205/8 = 25.
        assert_eq!(dcws, 21);
    }

    #[test]
    fn select_version_l4_picks_larger_for_same_input() {
        // L4 has more ECC, fewer data slots — so a fixed bit count may
        // need a larger version. For 25 bits, L4 of v1 fits (9 data * 8 = 72 ≥ 25).
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the version-selector L4-dcws path: 25 bits + L4 → V1
        // (fewer data slots; dcws=9 at L4 vs dcws=21 at L1).
        let (idx, _, dcws, _) = select_version(25, 4).expect(
            "select_version(25 bits, L4) (Han Xin version-selector L4 path; expects V1 dcws=9 due to L4's higher ECC overhead) must succeed",
        );
        assert_eq!(idx, 0);
        assert_eq!(dcws, 9);
    }

    /// Stage 11.A8c — extend `select_version` coverage with the
    /// `<=` boundary fit, V1→V2 spill, and the two reject paths
    /// (invalid eclevel + oversized input). The existing
    /// `select_version_picks_smallest_fitting` (25 bits L1) and
    /// `select_version_l4_picks_larger_for_same_input` (25 bits L4)
    /// only exercise a single mid-range bit count → V1, so:
    ///
    ///   * The exact `bits_len <= dmod` boundary isn't pinned; a
    ///     mutation `< dmod` would let 25-bit inputs through both
    ///     branches (still V1) and survive.
    ///   * The version-spill from V1 to V2 when bits exceed V1's
    ///     dmod isn't pinned; mutations to the iteration loop
    ///     (e.g. `enumerate()` swap, `return Some` shift) survive.
    ///   * The `level_idx >= 4` short-circuit isn't pinned; a
    ///     mutation to `> 4` would let eclevel=5 (level_idx=4)
    ///     through and likely panic on `m.blocks[4]` out-of-bounds.
    ///   * The "no version fits" None return isn't pinned;
    ///     mutations like `bits_len <= dmod * 2` would let
    ///     impossibly large inputs claim some version.
    ///
    /// V1 metric anchors (from `hanxin_metrics_version_1_matches_bwipp`):
    ///   cap_bits = 205, ncws = 205/8 = 25,
    ///   L1 blocks = [(1, 21, 4)] → ecws = 4, dcws = 21, dmod = 168.
    ///   L4 blocks = [(1, 9, 16)] → ecws = 16, dcws = 9, dmod = 72.
    /// V2 metric (from HANXIN_METRICS[1]):
    ///   cap_bits = 301, ncws = 301/8 = 37,
    ///   L1 blocks = [(1, 31, 6)] → ecws = 6, dcws = 31, dmod = 248.
    ///   L4 blocks = [(1, 15, 22)] → ecws = 22, dcws = 15, dmod = 120.
    /// V84 L4 dmod = 12432 (derivable from `hanxin_metrics_version_84_matches_bwipp`,
    ///   blocks[3] = [(79, 18, 28), (4, 33, 30)]: ecws = 79*28+4*30 = 2332,
    ///   ncws = 31091/8 = 3886, dcws = 1554, dmod = 12432).
    ///
    /// Mutations caught:
    ///   * `bits_len <= dmod` → `< dmod`: case (168, 1) would skip V1.
    ///   * `bits_len <= dmod` → `<= dmod * 2`: case (15000, 4) would
    ///     wrongly claim a fit instead of None.
    ///   * `level_idx >= 4` → `> 4`: case (25, 5) → level_idx=4, the
    ///     mutant continues into the loop and panics on blocks[4].
    ///   * `dcws = ncws - ecws` → `ncws + ecws`: V1 L1 dmod = (25+4)*8
    ///     = 232; case (169, 1) would now fit V1 → idx=0, wrong.
    #[test]
    fn select_version_invalid_eclevel_and_boundary_fits() {
        // (1) Invalid eclevel: level_idx >= 4 short-circuit.
        assert!(
            select_version(25, 5).is_none(),
            "eclevel=5 (level_idx=4) must return None"
        );
        assert!(
            select_version(25, 100).is_none(),
            "eclevel=100 (level_idx way past 4) must return None"
        );

        // (2) Exact-boundary fit at V1 L1 (168 bits = dcws*8).
        let (idx_168, _, dcws_168, _) =
            select_version(168, 1).expect("168 bits L1 must fit V1 (boundary)");
        assert_eq!(
            idx_168, 0,
            "168 bits L1 fits V1 exactly (bits_len <= dmod with dmod=168)"
        );
        assert_eq!(dcws_168, 21, "V1 L1 dcws = 21");

        // (3) One bit over V1 L1 → spill to V2.
        let (idx_169, _, dcws_169, _) = select_version(169, 1).expect("169 bits L1 must fit V2");
        assert_eq!(idx_169, 1, "169 bits L1 exceeds V1's 168-bit cap → V2");
        assert_eq!(dcws_169, 31, "V2 L1 dcws = 31");

        // (4) Exact-boundary fit at V1 L4 (72 bits).
        let (idx_l4_72, _, dcws_l4_72, _) = select_version(72, 4).expect("72 bits L4 must fit V1");
        assert_eq!(idx_l4_72, 0);
        assert_eq!(dcws_l4_72, 9);

        // (5) One bit over V1 L4 → spill to V2.
        let (idx_l4_73, _, dcws_l4_73, _) = select_version(73, 4).expect("73 bits L4 must fit V2");
        assert_eq!(idx_l4_73, 1, "73 bits L4 exceeds V1's 72-bit cap → V2");
        assert_eq!(dcws_l4_73, 15, "V2 L4 dcws = 15");

        // (6) Excessively large at L4: V84 L4 dmod = 12432; no fit
        // beyond that.
        assert!(
            select_version(15000, 4).is_none(),
            "15000 bits L4 exceeds all 84 versions (V84 L4 dmod = 12432) → None"
        );
    }

    #[test]
    fn bits_to_codewords_msb_first() {
        // 16 bits: 0b01000001_10000010 → [0x41, 0x82]
        let bits: Vec<bool> = [
            false, true, false, false, false, false, false, true, true, false, false, false, false,
            false, true, false,
        ]
        .to_vec();
        assert_eq!(bits_to_codewords(&bits), vec![0x41, 0x82]);
    }

    #[test]
    fn encode_final_codewords_matches_bwip_js_for_a_l1() {
        // bwip-js oracle (oracle-hanxin-cws.js with input "A" eclevel L1):
        // version=1, ncws=25, dcws=21, ecws=4, rbit=5.
        // Final interleaved cws (26 elements = 25 + 1 slack):
        let want: [u8; 26] = [
            48, 0, 0, 0, 160, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 0, 210, 0, 0, 0, 209, 0, 0,
        ];
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the end-to-end codeword pipeline for V1 L1 input "A": bits
        // → pad-to-dmod → bits_to_codewords → RS-ECC → interleave →
        // 26-element vector matching bwip-js oracle.
        let (idx, cws) = encode_final_codewords(b"A", 1).expect(
            "encode_final_codewords(b\"A\", L1) (Han Xin V1 L1 end-to-end codeword pipeline: bits → pad → cws → RS-ECC → interleave; 26-element bwip-js oracle) must succeed",
        );
        assert_eq!(idx, 0); // version 1
        assert_eq!(cws, want.to_vec());
    }

    /// Stage 11.A8c — pin the **pre-interleave** output of
    /// `encode_codewords`. This isolates failure between the
    /// bits → pad → cws → RS-ECC pipeline and the stride-13
    /// interleave that the existing
    /// `encode_final_codewords_matches_bwip_js_for_a_l1` test
    /// applies on top.
    ///
    /// The expected values are derived from the BWIPP oracle for
    /// `encode_final_codewords(b"A", L1)` by inverting the
    /// interleave (verified against `interleave_codewords` semantics
    /// at lines 1047-1063 + the existing
    /// `interleave_codewords_stride_13_and_trailing_zero` test):
    ///
    ///   Post-interleave (BWIPP oracle, 26 elements):
    ///     [48, 0, 0, 0, 160, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    ///      148, 0, 210, 0, 0, 0, 209, 0, 0]
    ///
    ///   For ncws=25, max_k=12. Each col k emits cws[k] then cws[k+13]
    ///   (or just cws[k] for k=12). Col-to-position map:
    ///     out[0..2]   = [cws[0], cws[13]]
    ///     out[2..4]   = [cws[1], cws[14]]
    ///     out[4..6]   = [cws[2], cws[15]]
    ///     ...
    ///     out[22..24] = [cws[11], cws[24]]
    ///     out[24]     = cws[12]
    ///     out[25]     = trailing 0 (rbit = 205 % 8 = 5 > 0)
    ///
    ///   Inverting yields pre-interleave cws (25 elements):
    ///     data (cws[0..21]):
    ///       [48, 0, 160, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    ///        0, 0, 0, 0, 0, 0]
    ///     ECC (cws[21..25]):
    ///       [148, 210, 0, 209]
    ///
    /// The data section is also derivable from first principles by
    /// stepping through encode_binary_bits("A"):
    ///   bits[0..4]   = "0011" (mode)
    ///   bits[4..17]  = "0000000000001" (length=1, MSB-first)
    ///   bits[17..25] = "01000001" (byte 'A' = 0x41, MSB-first)
    ///   bits[25..168] = zero padding
    /// Slicing as MSB-first 8-bit chunks:
    ///   cws[0] = "00110000" = 0x30 = 48
    ///   cws[1] = "00000000" = 0
    ///   cws[2] = "10100000" = 0xA0 = 160
    ///   cws[3] = "10000000" = 0x80 = 128
    ///   cws[4..21] = 0 (padding)
    /// → matches the data section above, confirming the inversion.
    ///
    /// Mutations caught (beyond the existing end-to-end test):
    ///   * `dmod = (dcws as usize) * 8` → `* 4` or `* 16`: padding
    ///     would over- or undershoot; bits_to_codewords would
    ///     produce wrong-length output.
    ///   * `pad_to_dmod` swap: any padding bug shifts the data
    ///     section.
    ///   * `encode_rs_blocks` ordering swap: data and ECC sections
    ///     would interleave wrong.
    ///   * `bits_to_codewords` MSB→LSB byte order: cws[0] becomes
    ///     0x0C instead of 0x30.
    ///   * Mismatch between `dcws as usize` and the loop bound:
    ///     `dcws.len() != dcws as usize` would trip the
    ///     debug_assert.
    #[test]
    fn encode_codewords_isolates_pre_interleave_pipeline_for_a_l1() {
        let (idx, combined) =
            encode_codewords(b"A", 1).expect("encode_codewords(\"A\", L1) must succeed");

        // Version: V1 (index 0).
        assert_eq!(idx, 0, "V1 metric chosen for 25-bit input at L1");

        // Total length: ncws = 205/8 = 25.
        assert_eq!(combined.len(), 25, "V1 ncws = 25");

        // Data section (first 21 cws): bits → padded → MSB-first
        // 8-bit chunks. Derivation in test docstring above.
        let want_data: [u8; 21] = [
            48, 0, 160, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        ];
        assert_eq!(
            combined[..21],
            want_data,
            "data section pins bits→pad→cws sub-pipeline"
        );

        // ECC section (cws[21..25]): rs_encode_gf256(data, 4) per
        // BWIPP oracle (inverted from the interleave golden).
        let want_ecc: [u8; 4] = [148, 210, 0, 209];
        assert_eq!(
            combined[21..],
            want_ecc,
            "ECC section pins rs_encode_gf256 + encode_rs_blocks ordering"
        );
    }

    #[test]
    fn alloc_pixs_fills_with_minus_one() {
        let p = alloc_pixs(23);
        assert_eq!(p.len(), 23 * 23);
        assert!(p.iter().all(|&v| v == -1));
    }

    #[test]
    fn place_corner_finders_v1_top_left_matches_fpat() {
        // Version 1 = size 23.
        let mut pixs = alloc_pixs(23);
        place_corner_finders(&mut pixs, 23);
        // Top-left 8×8 should be FPAT.
        for y in 0..8 {
            for x in 0..8 {
                assert_eq!(pixs[y * 23 + x], FPAT[y][x] as i8, "TL ({x}, {y}) mismatch",);
            }
        }
    }

    #[test]
    fn place_corner_finders_v1_bottom_left_uses_fpat2() {
        let mut pixs = alloc_pixs(23);
        place_corner_finders(&mut pixs, 23);
        // Bottom-left 8×8 uses FPAT2 (not FPAT).
        for (y, row) in FPAT2.iter().enumerate() {
            for (x, &cell) in row.iter().enumerate() {
                let row_y = 23 - 1 - y;
                assert_eq!(
                    pixs[row_y * 23 + x],
                    cell as i8,
                    "BL ({x}, y={row_y}) should be FPAT2[{y}][{x}]",
                );
            }
        }
    }

    #[test]
    fn place_corner_finders_v1_top_right_and_bottom_right_use_fpat() {
        let mut pixs = alloc_pixs(23);
        place_corner_finders(&mut pixs, 23);
        for (y, row) in FPAT.iter().enumerate() {
            for (x, &cell) in row.iter().enumerate() {
                let tr_x = 23 - 1 - x;
                assert_eq!(pixs[y * 23 + tr_x], cell as i8, "TR ({tr_x}, {y}) mismatch",);
                let br_x = 23 - 1 - x;
                let br_y = 23 - 1 - y;
                assert_eq!(
                    pixs[br_y * 23 + br_x],
                    cell as i8,
                    "BR ({br_x}, {br_y}) mismatch",
                );
            }
        }
    }

    #[test]
    fn place_corner_finders_matches_bwip_js_v1_bottom_left() {
        // Per oracle-hanxin-pixs.js for input "A" eclevel L1 (size=23):
        // BL 8x8 (y from size-8=15 to size-1=22) matches FPAT2 in
        // "flipped" row order — y=22 has FPAT2[0], y=15 has FPAT2[7].
        let mut pixs = alloc_pixs(23);
        place_corner_finders(&mut pixs, 23);
        // y=22 should equal FPAT2[0] = [1,1,1,0,1,0,1,0]
        let row22: Vec<i8> = (0..8).map(|x| pixs[22 * 23 + x]).collect();
        assert_eq!(row22, vec![1, 1, 1, 0, 1, 0, 1, 0]);
        // y=16 should equal FPAT2[6] = [1,1,1,1,1,1,1,0]
        let row16: Vec<i8> = (0..8).map(|x| pixs[16 * 23 + x]).collect();
        assert_eq!(row16, vec![1, 1, 1, 1, 1, 1, 1, 0]);
        // y=15 should equal FPAT2[7] = [0,0,0,0,0,0,0,0]
        let row15: Vec<i8> = (0..8).map(|x| pixs[15 * 23 + x]).collect();
        assert_eq!(row15, vec![0; 8]);
    }

    #[test]
    fn place_corner_finders_v1_centre_remains_unwritten() {
        let mut pixs = alloc_pixs(23);
        place_corner_finders(&mut pixs, 23);
        // Centre cell at (11, 11) is far from any finder corner.
        assert_eq!(pixs[11 * 23 + 11], -1);
    }

    /// Stage 11.A8c — pin `interleave_codewords`:
    ///   * Stride-13 column walk: for k in 0..=min(ncws-1, 12),
    ///     emit cws[k], cws[k+13], cws[k+26], …
    ///   * `rbit > 0` appends a single trailing zero codeword.
    ///   * `rbit == 0` appends nothing.
    ///
    /// Mutations caught:
    ///   * Stride `i += 13` → `i += 12` or `i += 14` reshuffles all
    ///     multi-stride outputs.
    ///   * `max_k = min(ncws-1, 12)` → off-by-one shifts which columns
    ///     emit — caught by the ncws=14 and ncws=26 cases.
    ///   * `rbit > 0` → `rbit >= 0` would push 0 always, failing the
    ///     `rbit=0` assertion.
    #[test]
    fn interleave_codewords_stride_13_and_trailing_zero() {
        // ncws=13: max_k=12 → each column emits one value → identity.
        let cws13: Vec<u8> = (0..13).collect();
        assert_eq!(
            interleave_codewords(&cws13, 0),
            cws13,
            "ncws=13: identity ordering (no second stride step)"
        );

        // ncws=14: column k=0 emits [cws[0], cws[13]]; columns 1..=12
        // emit single value. → [0,13,1,2,3,4,5,6,7,8,9,10,11,12].
        let cws14: Vec<u8> = (0..14).collect();
        assert_eq!(
            interleave_codewords(&cws14, 0),
            vec![0, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
        );

        // ncws=26: each column k=0..=12 emits [k, k+13] pair →
        // [0,13, 1,14, 2,15, …, 12,25].
        let cws26: Vec<u8> = (0..26).collect();
        let expected: Vec<u8> = (0..13).flat_map(|k| [k, k + 13]).collect();
        assert_eq!(interleave_codewords(&cws26, 0), expected);

        // Small ncws=4: max_k=3 → identity.
        assert_eq!(interleave_codewords(&[1, 2, 3, 4], 0), vec![1, 2, 3, 4]);

        // rbit > 0 → exactly ONE trailing 0 (no matter the rbit value).
        assert_eq!(interleave_codewords(&[1, 2, 3], 1), vec![1, 2, 3, 0]);
        assert_eq!(interleave_codewords(&[1, 2, 3], 7), vec![1, 2, 3, 0]);
        // rbit == 0 → no trailing 0.
        assert_eq!(interleave_codewords(&[1, 2, 3], 0), vec![1, 2, 3]);
    }

    #[test]
    fn trmv_and_aplot_mirror_and_symmetric_writes() {
        // Stage 11.A8c — pin `trmv(x, y, size) = y*size + (size-1-x)`
        // and `aplot` writing both `(x,y)` and `(y,x)` mirror cells.
        //
        // Mutations caught:
        //   * `y * size + (size - 1 - x)` swap to `x * size + …`
        //     would flip row/col → trmv(0,0,5) becomes 4 vs 4 (same),
        //     but trmv(4,0,5)=0 → mutation gives 4*5+4=24.
        //   * `size - 1 - x` → `size - 1 + x` would map (4,0,5) to 8
        //     (out of [0,4]+row=0 row range) — caught by anchor.
        //   * `aplot` dropping the `(y,x)` mirror write would leave
        //     index 0 unwritten when we plot at (0,4) → fails second
        //     read.
        //
        // size=5 grid → indices 0..25.
        assert_eq!(trmv(0, 0, 5), 4, "(0,0) → row 0, mirrored col 4");
        assert_eq!(trmv(4, 0, 5), 0, "(4,0) → row 0, mirrored col 0");
        assert_eq!(trmv(0, 4, 5), 24, "(0,4) → row 4, mirrored col 4");
        assert_eq!(trmv(4, 4, 5), 20, "(4,4) → row 4, mirrored col 0");
        assert_eq!(trmv(2, 2, 5), 12, "(2,2) → centre");
        assert_eq!(trmv(1, 3, 5), 18, "(1,3) → row 3, mirrored col 3");

        // `aplot(x=0, y=4, val=7, size=5)` must write index
        // trmv(0,4)=24 AND trmv(4,0)=0 — the symmetric partner.
        let mut pixs = vec![0i8; 25];
        aplot(&mut pixs, 0, 4, 7, 5);
        assert_eq!(pixs[24], 7, "aplot writes trmv(0,4)=24");
        assert_eq!(pixs[0], 7, "aplot writes trmv(4,0)=0 (symmetric)");
        // Other cells stay 0.
        assert_eq!(pixs[12], 0);
        assert_eq!(pixs[20], 0);
    }

    #[test]
    fn compute_masked_symbol_xors_only_where_mask_is_one() {
        // Stage 11.A8c — pin `compute_masked_symbol`:
        //   * `if m == 1 { p ^ 1 } else { p }` per cell.
        //   * pins the m==1 branch direction (mutation `m == 0`
        //     would flip cells where the data zone shouldn't be
        //     masked).
        //   * pins XOR (mutation `p | 1` or `p & 1` gives different
        //     output for p=0,1 vs the truth table).
        //   * pins the else-branch identity (mutation `p ^ 1` in
        //     else would flip function cells).
        //   * pixs may be 0, 1, or -1 (function-cell sentinel) — the
        //     -1 case at mask=0 must stay -1; at mask=1 it goes to
        //     -1 ^ 1 = -2 (two's complement). We only assert the
        //     m==0 case keeps -1.
        let pixs: Vec<i8> = vec![0, 1, 0, 1, -1, -1];
        let mask: Vec<u8> = vec![0, 0, 1, 1, 0, 0];
        let got = compute_masked_symbol(&pixs, &mask);
        assert_eq!(
            got,
            vec![0, 1, 1, 0, -1, -1],
            "m=0 → identity; m=1 → XOR with 1"
        );

        // m=2 (or any non-1 value) must also use the identity branch
        // — pins `m == 1` against `m >= 1` or `m != 0` mutations.
        let pixs: Vec<i8> = vec![0, 1];
        let mask: Vec<u8> = vec![2, 2];
        assert_eq!(
            compute_masked_symbol(&pixs, &mask),
            vec![0, 1],
            "m=2 (not equal to 1) → identity"
        );

        // Empty stays empty.
        assert_eq!(compute_masked_symbol(&[], &[]), Vec::<i8>::new());
    }

    #[test]
    fn mask_value_known_outputs() {
        // Mask 0: always 1 (no-flip).
        assert_eq!(mask_value(0, 1, 1), 1);
        assert_eq!(mask_value(0, 5, 7), 1);
        // Mask 1: (col + row) % 2
        assert_eq!(mask_value(1, 1, 1), 0); // (1+1)%2 = 0
        assert_eq!(mask_value(1, 1, 2), 1); // (1+2)%2 = 1
        assert_eq!(mask_value(1, 7, 7), 0); // 14%2 = 0
                                            // Mask 2: ((row + col) % 3 + col % 3) % 2
                                            // (1, 1): ((1+1)%3 + 1%3) % 2 = (2 + 1) % 2 = 1
        assert_eq!(mask_value(2, 1, 1), 1);
        // (3, 3): ((6)%3 + 3%3) % 2 = (0 + 0) % 2 = 0
        assert_eq!(mask_value(2, 3, 3), 0);
        // Asymmetric — discriminates `col % 3` from `row % 3`.
        // (col=2, row=4): ((6)%3 + 2%3) % 2 = (0 + 2) % 2 = 0
        // If buggy `row % 3` form: (0 + 4%3) % 2 = (0 + 1) % 2 = 1.
        assert_eq!(mask_value(2, 2, 4), 0);
        // (col=4, row=2): ((6)%3 + 4%3) % 2 = (0 + 1) % 2 = 1
        // If buggy `row % 3` form: (0 + 2%3) % 2 = (0 + 2) % 2 = 0.
        assert_eq!(mask_value(2, 4, 2), 1);
        // Mask 3: (col % row + (row % col + (row%3 + col%3))) % 2
        // (1, 1): (1%1 + (1%1 + (1%3 + 1%3))) % 2 = (0 + (0 + 2)) % 2 = 0
        assert_eq!(mask_value(3, 1, 1), 0);
    }

    #[test]
    fn build_mask_grid_marks_only_data_cells() {
        // 5x5 grid with finder cells at corners, data elsewhere.
        let size = 5;
        let mut pixs = vec![-1i8; 25];
        pixs[0] = 1; // function cell
        pixs[24] = 0; // function cell
        let mask = build_mask_grid(&pixs, 1, size);
        // Cells at idx 0 and 24 should be 0 in the mask (function cells
        // never get masked).
        assert_eq!(mask[0], 0);
        assert_eq!(mask[24], 0);
        // For mask 1 at (col=2, row=1): (2+1)%2 = 1, mask cell = 0.
        // For mask 1 at (col=1, row=1): (1+1)%2 = 0 → mask cell = 1 if pixs[0]==-1.
        // pixs[0] = 1 (function), so still mask[0] = 0.
        // For (col=2, row=2) → (2+2)%2 = 0, pixs[1*5+1=6] = -1, so mask[6] = 1.
        assert_eq!(mask[6], 1);
    }

    #[test]
    fn apply_mask_flips_marked_cells() {
        let mut pixs = vec![0i8, 1, 0, 1];
        let mask = vec![0u8, 1, 1, 0];
        apply_mask(&mut pixs, &mask);
        assert_eq!(pixs, vec![0, 0, 1, 1]);
    }

    #[test]
    fn place_data_fills_all_unwritten_cells() {
        // Start with a v1-sized grid where only the corners have
        // function patterns. place_data should fill every -1 cell.
        let size = 23;
        let mut pixs = alloc_pixs(size);
        place_corner_finders(&mut pixs, size);
        let cws = vec![0xAA; 100]; // 100 codewords of pattern 10101010
        place_data(&mut pixs, &cws, size);
        // No -1 cells should remain.
        assert!(!pixs.contains(&-1), "place_data must fill every -1 cell",);
        // Every cell is 0 or 1.
        assert!(pixs.iter().all(|&v| v == 0 || v == 1));
    }

    #[test]
    fn place_data_alternates_with_aa_pattern() {
        // For cws = [0xAA, 0xAA, ...], the first bit (MSB) is 1, second
        // is 0, third is 1, etc. The cell-scan order is row-major
        // (posy=0, posx=0..size; then posy=1, ...). The first cell
        // written should be 1, the second 0, etc.
        let size = 23;
        let mut pixs = alloc_pixs(size);
        place_corner_finders(&mut pixs, size);
        let cws = vec![0xAA; 100];
        place_data(&mut pixs, &cws, size);
        // Find the first -1 cell index in the original (after finders),
        // and verify it's 1. The first non-finder cell scanning row-major
        // should be at column 8 of row 0 (cols 0-7 are TL finder).
        // FPAT[0][7] = 0, so col 7 is occupied. Col 8 is the first -1.
        assert_eq!(pixs[8], 1, "first data bit should be MSB of 0xAA = 1");
    }

    #[test]
    fn build_function_info_bits_length_is_34() {
        let bits = build_function_info_bits(0, 1, 0);
        assert_eq!(bits.len(), 34);
        for &b in &bits {
            assert!(b == 0 || b == 1);
        }
        // Last 6 bits are always [0, 1, 0, 1, 0, 1].
        assert_eq!(&bits[28..], &[0, 1, 0, 1, 0, 1]);
    }

    #[test]
    fn build_function_info_bits_v1_l1_m0_funval_336() {
        // For (version=1, ecc=1, mask=0): funval = (((21)*4)+0)*4 + 0 = 336.
        // fundata = [336>>8 & 15, 336>>4 & 15, 336 & 15] = [1, 5, 0].
        // First 12 bits should be 0001 0101 0000.
        let bits = build_function_info_bits(0, 1, 0);
        assert_eq!(
            &bits[..12],
            &[0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0],
            "fundata bits for v1 L1 m0",
        );
    }

    #[test]
    fn place_function_info_writes_at_funmap_cells() {
        // For v1 (size=23), place 34 bits and verify cell (0, 8) and
        // (22, 14) got the first bit.
        let size = 23;
        let mut pixs = alloc_pixs(size);
        let bits = build_function_info_bits(0, 1, 0);
        place_function_info(&mut pixs, &bits, size);
        // funmap[0] pairs: (0, 8) and (22, 14) — both should get bits[0].
        assert_eq!(pixs[8 * size], bits[0] as i8);
        assert_eq!(pixs[14 * size + 22], bits[0] as i8);
    }

    #[test]
    fn function_info_cells_count_is_68() {
        for size in [23, 25, 31, 41, 99, 189] {
            let cells = function_info_cells(size);
            assert_eq!(cells.len(), 68, "size {size} should have 68 cells");
        }
    }

    #[test]
    fn function_info_cells_v1_matches_oracle() {
        // From oracle/extract-funmap.js size=23 dump, the first 6
        // pairs (entries 0..3 worth = 6 cells) are:
        //   (0, 8), (22, 14), (1, 8), (21, 14), (2, 8), (20, 14)
        let cells = function_info_cells(23);
        assert_eq!(cells[0], (0, 8));
        assert_eq!(cells[1], (22, 14));
        assert_eq!(cells[2], (1, 8));
        assert_eq!(cells[3], (21, 14));
        // And the last 4 entries (size 23, entries 32-33):
        //   (21, 8), (1, 14), (22, 8), (0, 14)
        assert_eq!(cells[64], (21, 8));
        assert_eq!(cells[65], (1, 14));
        assert_eq!(cells[66], (22, 8));
        assert_eq!(cells[67], (0, 14));
    }

    #[test]
    fn zero_function_info_cells_writes_zeros() {
        let size = 23;
        let mut pixs = alloc_pixs(size);
        zero_function_info_cells(&mut pixs, size);
        // Spot-check a few cells from the oracle.
        assert_eq!(pixs[8 * size], 0); // (0, 8)
        assert_eq!(pixs[14 * size + 22], 0); // (22, 14)
        assert_eq!(pixs[7 * size + 8], 0); // (8, 7) — top arm
        assert_eq!(pixs[14], 0); // (14, 0) — top-right arm
                                 // Centre should still be -1 (not in function-info zone).
        assert_eq!(pixs[11 * size + 11], -1);
    }

    #[test]
    fn place_alignment_patterns_v1_is_noop() {
        // Version 1 has alig_count=0, so the function returns
        // immediately without writing any cells.
        let mut pixs = alloc_pixs(23);
        place_alignment_patterns(&mut pixs, &HANXIN_METRICS[0], 23);
        assert!(pixs.iter().all(|&v| v == -1));
    }

    #[test]
    fn finders_plus_alignment_v5_corner_rows_match_bwip_js() {
        // bwip-js oracle: version 5 (size=31), L1, input "A"*45.
        // BWIPP placement order: alignment FIRST, then finders (which
        // overwrite the corners) — so any alignment cell that lands
        // under a finder gets clobbered. The test mirrors that order.
        let m = &HANXIN_METRICS[4];
        assert_eq!(m.size, 31);
        let size = m.size as usize;
        let mut pixs = alloc_pixs(size);
        place_alignment_patterns(&mut pixs, m, size);
        place_corner_finders(&mut pixs, size);
        let row0: Vec<i8> = (0..size).map(|x| pixs[x]).collect();
        let want_row0: [i8; 31] = [
            1, 1, 1, 1, 1, 1, 1, 0, -1, -1, -1, -1, -1, 0, 1, 0, -1, -1, -1, -1, -1, -1, -1, 0, 1,
            1, 1, 1, 1, 1, 1,
        ];
        assert_eq!(row0, want_row0.to_vec(), "row 0 mismatch");
        let row7: Vec<i8> = (0..size).map(|x| pixs[7 * size + x]).collect();
        let want_row7: [i8; 31] = [
            0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
            0, 0, 0, 0, 0, 0, 0,
        ];
        assert_eq!(row7, want_row7.to_vec(), "row 7 mismatch");
    }

    #[test]
    fn place_alignment_patterns_v4_writes_some_cells() {
        // Version 4 has alig_step=14, alig_count=1, size=29.
        // alnr = 29 - 14*1 = 15.
        let m = &HANXIN_METRICS[3];
        assert_eq!(m.version, "4");
        assert_eq!(m.size, 29);
        assert_eq!(m.alig_step, 14);
        assert_eq!(m.alig_count, 1);
        let size = m.size as usize;
        let mut pixs = alloc_pixs(size);
        place_alignment_patterns(&mut pixs, m, size);
        let written = pixs.iter().filter(|&&v| v != -1).count();
        assert!(written > 0, "v4 should write some alignment cells");
    }

    // Position-weighted fingerprint of the full alignment grid: any cell
    // that moves or flips changes (c1,s1) for the 1-cells or (c0,s0) for
    // the 0-cells, so this pins the entire placement-arithmetic surface.
    fn alig_fingerprint(idx: usize) -> (usize, u64, usize, u64) {
        let m = &HANXIN_METRICS[idx];
        let size = m.size as usize;
        let mut pixs = alloc_pixs(size);
        place_alignment_patterns(&mut pixs, m, size);
        let (mut c1, mut s1, mut c0, mut s0) = (0usize, 0u64, 0usize, 0u64);
        for (k, &v) in pixs.iter().enumerate() {
            if v == 1 {
                c1 += 1;
                s1 = s1.wrapping_add((k as u64).wrapping_mul(2_654_435_761));
            } else if v == 0 {
                c0 += 1;
                s0 = s0.wrapping_add((k as u64).wrapping_mul(2_654_435_761));
            }
        }
        (c1, s1, c0, s0)
    }

    #[test]
    fn place_alignment_patterns_full_grid_pinned() {
        // Pins the complete alignment-pattern placement (bwip-js-verified
        // output) for four versions spanning size 31..=105, exercising the
        // `j + alnr < size` window, the odd-column cleanup, and the
        // right-edge cleanup. Any arithmetic/index mutation in
        // place_alignment_patterns shifts or flips a cell and changes the
        // position-weighted fingerprint. Expected values captured from the
        // oracle-matched encoder (see finders_plus_alignment_v5_corner_rows
        // _match_bwip_js for the row-level cross-check on v5).
        // Stage 11.A8c-L: HANXIN_METRICS[15] (v16) added to kill the 8th
        // residual placement mutant (`506:11 <= with <`) that's killable
        // only on this metric (see equivalence-proofs section of
        // MUTATION_RESULTS.md). Fingerprint captured 2026-05-28.
        let cases = [
            (
                4usize,
                (64usize, 99461707964670u64, 38usize, 53513424941760u64),
            ),
            (10, (143, 377158159537446, 131, 321972440066256)),
            (15, (173, 690291328519572, 171, 660752767371164)), // v16 — kills L506:11 <= with <
            (20, (209, 1202549650548874, 183, 972309201511256)),
            (41, (692, 10324693335985600, 637, 9187596762431464)),
        ];
        for (idx, want) in cases {
            assert_eq!(
                alig_fingerprint(idx),
                want,
                "alignment fingerprint changed for HANXIN_METRICS[{idx}] (v{})",
                HANXIN_METRICS[idx].version
            );
        }
    }

    #[test]
    fn trmv_mirrors_horizontally() {
        // trmv(x, y, size) = y * size + (size - 1 - x).
        // For size=23: trmv(0, 0, 23) = 22, trmv(22, 0, 23) = 0.
        assert_eq!(trmv(0, 0, 23), 22);
        assert_eq!(trmv(22, 0, 23), 0);
        assert_eq!(trmv(5, 3, 23), 3 * 23 + 17);
    }

    #[test]
    fn hanxin_metrics_capacity_matches_block_sum() {
        // Per BWIPP `bwipp_hanxin` lines 32602-32606: `ncws = nmod / 8`
        // (codeword count) and `sum(count * (data + ecc)) == ncws`.
        // The `cap_bits % 8` leftover is trailing slack bits.
        for m in HANXIN_METRICS.iter() {
            let ncws_expected = m.cap_bits / 8;
            for (level_idx, level) in m.blocks.iter().enumerate() {
                let sum: u32 = level
                    .iter()
                    .filter_map(|b| *b)
                    .map(|(c, d, e)| u32::from(c) * (u32::from(d) + u32::from(e)))
                    .sum();
                assert_eq!(
                    sum, ncws_expected,
                    "version {} level {level_idx}: block sum {sum} != ncws {ncws_expected} (cap_bits={})",
                    m.version, m.cap_bits,
                );
            }
        }
    }

    /// Stage 11.A8c (cont) — per-iteration single-substring
    /// `msg.contains("eclevel")` upgraded to 4-anchor pin:
    ///   1. `Han Xin Code:` symbology prefix
    ///   2. `eclevel must be 1..=4` predicate with range
    ///   3. per-iteration value echo `got {bad}`
    ///   4. cross-arm contamination guard: must NOT contain `mask must`
    ///      (the sibling rejection arm's wording at line 756 of
    ///      hanxin.rs).
    #[test]
    fn encode_with_mask_rejects_invalid_eclevel() {
        for bad in [0u8, 5, 99] {
            let err = encode_with_mask(b"A", bad, 0).unwrap_err();
            let msg = format!("{err}");
            assert!(
                msg.contains("Han Xin Code:"),
                "missing Han Xin Code prefix for eclevel={bad}: {msg:?}"
            );
            assert!(
                msg.contains("eclevel must be 1..=4"),
                "missing eclevel range predicate for eclevel={bad}: {msg:?}"
            );
            assert!(
                msg.contains(&format!("got {bad}")),
                "missing value echo `got {bad}`: {msg:?}"
            );
            assert!(
                !msg.contains("mask must"),
                "cross-arm contamination: eclevel reject mentions `mask must`: {msg:?}"
            );
        }
    }

    /// Stage 11.A8c (cont) — per-iteration single-substring
    /// `msg.contains("mask")` upgraded to 4-anchor pin:
    ///   1. `Han Xin Code:` symbology prefix
    ///   2. `mask must be 0..=3` predicate with range
    ///   3. per-iteration value echo `got {bad}`
    ///   4. cross-arm contamination guard: must NOT contain `eclevel
    ///      must` (sibling at line 751 of hanxin.rs).
    #[test]
    fn encode_with_mask_rejects_invalid_mask() {
        for bad in [4u8, 5, 200] {
            let err = encode_with_mask(b"A", 1, bad).unwrap_err();
            let msg = format!("{err}");
            assert!(
                msg.contains("Han Xin Code:"),
                "missing Han Xin Code prefix for mask={bad}: {msg:?}"
            );
            assert!(
                msg.contains("mask must be 0..=3"),
                "missing mask range predicate for mask={bad}: {msg:?}"
            );
            assert!(
                msg.contains(&format!("got {bad}")),
                "missing value echo `got {bad}`: {msg:?}"
            );
            assert!(
                !msg.contains("eclevel must"),
                "cross-arm contamination: mask reject mentions `eclevel must`: {msg:?}"
            );
        }
    }

    #[test]
    fn encode_with_mask_produces_full_v1_grid() {
        // Smoke test: input "A" at L1 mask 0 should pick version 1
        // (23x23) and fill every cell with 0 or 1 — no -1 cells left.
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the Han Xin V1 L1 mask-0 smoke path (every cell must be 0 or
        // 1, no -1 left over from unwritten data zone).
        let (pixs, version_index) = encode_with_mask(b"A", 1, 0).expect(
            "encode_with_mask(b\"A\", L1, mask=0) (Han Xin V1 L1 mask-0 smoke: full-grid fill, no -1 cells, no -1 residue) must succeed",
        );
        assert_eq!(version_index, 0, "input 'A' L1 should fit in v1");
        let m = &HANXIN_METRICS[version_index];
        let size = m.size as usize;
        assert_eq!(size, 23);
        assert_eq!(pixs.len(), size * size);
        assert!(
            !pixs.contains(&-1),
            "encode_with_mask must leave no -1 cells",
        );
        assert!(
            pixs.iter().all(|&v| v == 0 || v == 1),
            "every cell must be 0 or 1",
        );
    }

    #[test]
    fn encode_with_mask_top_left_finder_intact() {
        // The TL finder is FPAT (an 8x8 pattern). Its cells must not be
        // touched by mask XOR (apply_mask only flips the mask_grid cells,
        // which are the data zone). The finder cells should equal FPAT.
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the V1 L1 mask-0 TL-finder-intact path (FPAT 8×8 must not be
        // touched by apply_mask).
        let (pixs, _) = encode_with_mask(b"A", 1, 0).expect(
            "encode_with_mask(b\"A\", L1, mask=0) (Han Xin V1 L1 mask-0 TL-finder-intact: FPAT 8×8 must survive XOR) must succeed",
        );
        let size = 23usize;
        for (y, row) in FPAT.iter().enumerate() {
            for (x, &cell) in row.iter().enumerate() {
                assert_eq!(
                    pixs[y * size + x],
                    cell as i8,
                    "TL finder cell ({x},{y}) must equal FPAT",
                );
            }
        }
    }

    #[test]
    fn encode_with_mask_varies_with_mask_choice() {
        // Different masks must produce different grids (otherwise the
        // mask is being applied wrong). All four masks should land in
        // the same version (since the data is identical) but differ in
        // at least one data-zone cell.
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // each mask index (0..=3) so a mutant that mis-routes a mask
        // dispatch reports which mask the encoder rejected. All four
        // calls share input "A" L1 (V1 23×23 geometry).
        let (m0, v0) = encode_with_mask(b"A", 1, 0).expect(
            "encode_with_mask(b\"A\", L1, mask=0) (Han Xin V1 L1 mask-0 path: identity mask, r=1 always) must succeed",
        );
        let (m1, v1) = encode_with_mask(b"A", 1, 1).expect(
            "encode_with_mask(b\"A\", L1, mask=1) (Han Xin V1 L1 mask-1 path: (col+row) mod 2) must succeed",
        );
        let (m2, v2) = encode_with_mask(b"A", 1, 2).expect(
            "encode_with_mask(b\"A\", L1, mask=2) (Han Xin V1 L1 mask-2 path: ((row+col) mod 3 + col mod 3) mod 2) must succeed",
        );
        let (m3, v3) = encode_with_mask(b"A", 1, 3).expect(
            "encode_with_mask(b\"A\", L1, mask=3) (Han Xin V1 L1 mask-3 path: (col mod row + (row mod col + (row mod 3 + col mod 3))) mod 2) must succeed",
        );
        assert_eq!(v0, v1);
        assert_eq!(v0, v2);
        assert_eq!(v0, v3);
        assert_ne!(m0, m1, "mask 0 and mask 1 must produce different grids");
        assert_ne!(m0, m2, "mask 0 and mask 2 must produce different grids");
        assert_ne!(m0, m3, "mask 0 and mask 3 must produce different grids");
    }

    #[test]
    fn encode_with_mask_matches_bwip_js_v1_l1_m0() {
        // Byte-for-byte oracle: input "A" eclevel L1 mask 0 (bwip-js
        // option mask=1, since bwip-js uses 1-indexed masks).
        // Captured from node-sidecar/oracle-hanxin-final-pixs.js — the
        // patched bundle dumps $_.pixs at //#33025 (right before
        // bwipp_renmatrix). 23×23 = 529 cells.
        #[rustfmt::skip]
        let want: [u8; 529] = [
            1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,
            1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1,
            0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
            0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0,
            1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,
        ];
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the V1 L1 mask-0 byte-for-byte 529-cell oracle dispatch.
        let (pixs, version_index) = encode_with_mask(b"A", 1, 0).expect(
            "encode_with_mask(b\"A\", L1, mask=0) (Han Xin V1 L1 mask-0 byte-for-byte 529-cell bwip-js oracle) must succeed",
        );
        assert_eq!(version_index, 0, "input 'A' L1 fits in v1");
        assert_eq!(pixs.len(), 529);
        let got: Vec<u8> = pixs.iter().map(|&v| v as u8).collect();
        assert_eq!(
            got,
            want.to_vec(),
            "Han Xin pixs must match bwip-js byte-for-byte for input 'A' L1 mask 0",
        );
    }

    #[test]
    fn encode_with_mask_matches_bwip_js_hello_l2_m0() {
        // Second oracle: input "HELLO" eclevel L2 mask 0 (bwip-js
        // option mask=1, since bwip-js is 1-indexed). Distinct from
        // the L1 m0 case above — exercises the L2 RS-ECC block layout
        // (v1 L2 is 1 block of 17 data + 8 ECC, vs L1's 17 data + 4
        // ECC). Mask 0 means no XOR — isolates the data pipeline.
        #[rustfmt::skip]
        let want: [u8; 529] = [
            1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
            1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
            0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1,
            1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0,
            0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0,
            1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
            1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,
        ];
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the V1 L2 mask-0 byte-for-byte 529-cell oracle dispatch
        // (L2 RS-ECC block: 17 data + 8 ECC, distinct from L1's 4 ECC).
        let (pixs, version_index) = encode_with_mask(b"HELLO", 2, 0).expect(
            "encode_with_mask(b\"HELLO\", L2, mask=0) (Han Xin V1 L2 mask-0 byte-for-byte 529-cell bwip-js oracle; 17 data + 8 ECC) must succeed",
        );
        assert_eq!(version_index, 0, "input 'HELLO' L2 fits in v1");
        assert_eq!(pixs.len(), 529);
        let got: Vec<u8> = pixs.iter().map(|&v| v as u8).collect();
        assert_eq!(
            got,
            want.to_vec(),
            "Han Xin pixs must match bwip-js byte-for-byte for input 'HELLO' L2 mask 0",
        );
    }

    #[test]
    fn encode_with_mask_matches_bwip_js_hello_l2_m2() {
        // Third oracle: input "HELLO" eclevel L2 mask 2 (bwip-js mask=3).
        // Exercises mask 2 — the asymmetric `col % 3` term distinguishes
        // it from the buggy `row % 3` form.
        #[rustfmt::skip]
        let want: [u8; 529] = [
            1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1,
            1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1,
            0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1,
            0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
            0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1,
            0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0,
            1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0,
            1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
            1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
        ];
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the V1 L2 mask-2 byte-for-byte oracle: the asymmetric `col %
        // 3` term distinguishes the correct mask-2 from a buggy `row %
        // 3` swap.
        let (pixs, _) = encode_with_mask(b"HELLO", 2, 2).expect(
            "encode_with_mask(b\"HELLO\", L2, mask=2) (Han Xin V1 L2 mask-2 byte-for-byte oracle; guards against row/col swap in ((row+col)%3+col%3)%2) must succeed",
        );
        let got: Vec<u8> = pixs.iter().map(|&v| v as u8).collect();
        assert_eq!(
            got,
            want.to_vec(),
            "Han Xin pixs must match bwip-js byte-for-byte for input 'HELLO' L2 mask 2",
        );
    }

    #[test]
    fn encode_with_mask_matches_bwip_js_hello_l2_m1() {
        // Mask 1: (col + row) % 2.
        #[rustfmt::skip]
        let want: [u8; 529] = [
            1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,
            1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1,
            0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0,
            1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
            1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0,
            1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0,
            1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0,
            1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
            1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
        ];
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the V1 L2 mask-1 byte-for-byte oracle: mask 1 is the
        // simplest (col+row)%2 path.
        let (pixs, _) = encode_with_mask(b"HELLO", 2, 1).expect(
            "encode_with_mask(b\"HELLO\", L2, mask=1) (Han Xin V1 L2 mask-1 byte-for-byte oracle; (col+row)%2 path) must succeed",
        );
        let got: Vec<u8> = pixs.iter().map(|&v| v as u8).collect();
        assert_eq!(got, want.to_vec(), "Han Xin v1 L2 mask 1 byte-for-byte");
    }

    #[test]
    fn encode_with_mask_matches_bwip_js_hello_l2_m3() {
        // Mask 3: (col % row + (row % col + (row%3 + col%3))) % 2.
        // The most arithmetic-heavy mask — guards against operand swaps.
        #[rustfmt::skip]
        let want: [u8; 529] = [
            1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
            1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1,
            0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1,
            1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0,
            0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0,
            0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0,
            0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0,
            1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
            1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,
        ];
        // Stage 11.A8c (cont) — `.unwrap()` → `.expect(...)` naming
        // the V1 L2 mask-3 byte-for-byte oracle: mask 3 is the most
        // arithmetic-heavy (col%row + (row%col + (row%3 + col%3)))%2 —
        // guards against operand swaps in the modulus chain.
        let (pixs, _) = encode_with_mask(b"HELLO", 2, 3).expect(
            "encode_with_mask(b\"HELLO\", L2, mask=3) (Han Xin V1 L2 mask-3 byte-for-byte oracle; arithmetic-heavy modulus-chain path) must succeed",
        );
        let got: Vec<u8> = pixs.iter().map(|&v| v as u8).collect();
        assert_eq!(got, want.to_vec(), "Han Xin v1 L2 mask 3 byte-for-byte");
    }

    /// Stage 11.A8c — pin `mask_value` for every (mask, col, row)
    /// boundary combination. Kills the `% with /` / `% with +` /
    /// `+ with -` arithmetic mutations on lines 632-637.
    #[test]
    fn mask_value_per_mask_boundaries() {
        // Mask 0: constant 1 regardless of position.
        for r in 1..5 {
            for c in 1..5 {
                assert_eq!(mask_value(0, c, r), 1, "m=0 c={c} r={r}");
            }
        }
        // Mask 1: (col + row) % 2. At (1,1) → 0; (2,1) → 1; (1,2) → 1;
        // (2,2) → 0. Pins the parity.
        assert_eq!(mask_value(1, 1, 1), 0);
        assert_eq!(mask_value(1, 2, 1), 1);
        assert_eq!(mask_value(1, 1, 2), 1);
        assert_eq!(mask_value(1, 2, 2), 0);
        assert_eq!(mask_value(1, 3, 4), 1); // 3+4=7, odd
                                            // Mask 2: ((row + col) % 3 + col % 3) % 2. Pin specific values
                                            // computed by hand.
                                            // (1,1): (1+1)%3 + 1%3 = 2+1=3, 3%2=1.
        assert_eq!(mask_value(2, 1, 1), 1);
        // (3,3): (3+3)%3 + 3%3 = 0+0=0, 0%2=0.
        assert_eq!(mask_value(2, 3, 3), 0);
        // (2,1): (2+1)%3 + 2%3 = 0+2=2, 2%2=0.
        assert_eq!(mask_value(2, 2, 1), 0);
        // (1,2): (1+2)%3 + 1%3 = 0+1=1, 1%2=1.
        assert_eq!(mask_value(2, 1, 2), 1);
        // Mask 3: (col % row + (row % col + (row % 3 + col % 3))) % 2.
        // (1,1): 1%1 + (1%1 + (1%3 + 1%3)) = 0 + (0 + 2) = 2, 2%2=0.
        assert_eq!(mask_value(3, 1, 1), 0);
        // (2,2): 2%2 + (2%2 + (2%3 + 2%3)) = 0 + (0 + 4) = 4, 4%2=0.
        assert_eq!(mask_value(3, 2, 2), 0);
        // (1,2): 1%2 + (2%1 + (2%3 + 1%3)) = 1 + (0 + 3) = 4, 4%2=0.
        assert_eq!(mask_value(3, 1, 2), 0);
        // (3,2): 3%2 + (2%3 + (2%3 + 3%3)) = 1 + (2 + 2) = 5, 5%2=1.
        assert_eq!(mask_value(3, 3, 2), 1);
    }

    /// Stage 11.A8c — pin `rs_encode_gf16` / `rs_encode_gf256` for
    /// simple non-empty inputs. Kills the function-replacement
    /// mutations on lines 218, 233 by exercising both flavours with
    /// inputs that produce specific deterministic outputs.
    #[test]
    fn rs_encode_gf16_and_gf256_simple_outputs() {
        // GF(16) flavour: 4 ecc nibbles from a 3-byte input.
        let ecc = rs_encode_gf16(&[1, 2, 3], 4);
        assert_eq!(ecc.len(), 4);
        // All nibbles must be in [0, 15].
        for n in &ecc {
            assert!(*n <= 15, "nibble {n} out of range");
        }

        // GF(256) flavour: 8 ecc bytes from a 3-byte input.
        let ecc = rs_encode_gf256(&[0xAB, 0xCD, 0xEF], 8);
        assert_eq!(ecc.len(), 8);

        // Sanity: same input + same ecc count → deterministic output
        // (catches the function-replacement mutant which returns
        // Default::default() = empty Vec).
        let ecc1 = rs_encode_gf256(&[1, 2, 3], 4);
        let ecc2 = rs_encode_gf256(&[1, 2, 3], 4);
        assert_eq!(ecc1, ecc2, "rs_encode must be deterministic");
        assert_eq!(ecc1.len(), 4);

        // Different inputs → different outputs (kills the "return
        // fixed value" class of mutants).
        let ecc_a = rs_encode_gf256(&[1, 2, 3], 4);
        let ecc_b = rs_encode_gf256(&[4, 5, 6], 4);
        assert_ne!(ecc_a, ecc_b, "different inputs must give different ecc");
    }

    /// Stage 11.A8c — pin `parse_eclevel(opts)` arms + default. Han
    /// Xin EC level extra accepts `L1..L4` / `1..4` (case-insensitive)
    /// and defaults to `L1` when the extra is absent. All other inputs
    /// must Err.
    ///
    /// Used by every Han Xin encode entry — but only exercised
    /// indirectly through full-symbol golden tests, so per-arm
    /// mutations (arm swap, default change, case-insensitivity
    /// removal, trim removal) hide easily.
    #[test]
    fn parse_eclevel_default_and_per_arm_anchors() {
        use crate::options::Options;

        // Default arm: no `eclevel` extra → L1 = 1.
        assert_eq!(parse_eclevel(&Options::default()).unwrap(), 1);

        // 8 happy values: 4 "Ln" + 4 "n".
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "L1")).unwrap(),
            1
        );
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "L2")).unwrap(),
            2
        );
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "L3")).unwrap(),
            3
        );
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "L4")).unwrap(),
            4
        );
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "1")).unwrap(),
            1
        );
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "2")).unwrap(),
            2
        );
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "3")).unwrap(),
            3
        );
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "4")).unwrap(),
            4
        );

        // Case-insensitive (kills `to_ascii_uppercase()` removal).
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "l2")).unwrap(),
            2
        );
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "l4")).unwrap(),
            4
        );

        // Whitespace trim (kills `.trim()` removal).
        assert_eq!(
            parse_eclevel(&Options::default().with("eclevel", "  L3  ")).unwrap(),
            3
        );

        // Stage 11.A8c — upgrade these 6 weak is_err() checks to
        // per-input diagnostic-substring pins. parse_eclevel has ONE
        // rejection arm (line 1093-1095) producing:
        //   "Han Xin Code: invalid eclevel {raw:?}; expected L1..L4"
        //
        // A mutant that drops `{raw:?}` (fixed value in message),
        // swaps "L1..L4" hint, or routes the rejection through a
        // different InvalidData arm would survive bare is_err() checks.
        // Distinct echoes (each input's debug form) kill `{raw:?}`
        // drop mutants.
        for input in ["", "L5", "0", "5", "LL1", "lvl-3"] {
            let err = parse_eclevel(&Options::default().with("eclevel", input)).unwrap_err();
            let crate::error::Error::InvalidData(msg) = err else {
                panic!("eclevel={input:?} must yield InvalidData; got {err:?}");
            };
            let want_echo = format!("{input:?}");
            assert!(
                msg.contains("Han Xin Code:")
                    && msg.contains("invalid eclevel")
                    && msg.contains(&want_echo)
                    && msg.contains("L1..L4"),
                "eclevel={input:?} must pin symbology tag + 'invalid eclevel' + {want_echo:?} \
                 echo + L1..L4 hint; got {msg:?}"
            );
        }
    }

    /// Stage 11.A8c — pin `parse_mask_opt(opts)`. Han Xin mask extra:
    /// absent → `None` (auto-pick), `"0".."3"` → `Some(0..=3)` (force
    /// that mask), else Err. Trim is applied; no case folding needed.
    ///
    /// Mutations killed:
    ///   * absent → `Ok(Some(0))` mutant (would override the
    ///     auto-select path);
    ///   * arm-extend `0..=4`: mask=4 must Err (out-of-range);
    ///   * arm-flip per-mask-value (each happy combo pinned distinctly);
    ///   * `.trim()` removal (the " 2 " anchor would fail to parse
    ///     without trim).
    #[test]
    fn parse_mask_opt_arms_and_range_guard() {
        use crate::options::Options;

        // Absent → None (kills `Some(0)` default mutant).
        assert_eq!(parse_mask_opt(&Options::default()).unwrap(), None);

        // Happy: 0..=3.
        assert_eq!(
            parse_mask_opt(&Options::default().with("mask", "0")).unwrap(),
            Some(0)
        );
        assert_eq!(
            parse_mask_opt(&Options::default().with("mask", "1")).unwrap(),
            Some(1)
        );
        assert_eq!(
            parse_mask_opt(&Options::default().with("mask", "2")).unwrap(),
            Some(2)
        );
        assert_eq!(
            parse_mask_opt(&Options::default().with("mask", "3")).unwrap(),
            Some(3)
        );

        // Trim applied (kills `.trim()` removal).
        assert_eq!(
            parse_mask_opt(&Options::default().with("mask", "  2  ")).unwrap(),
            Some(2)
        );

        // Stage 11.A8c — upgrade these 5 weak is_err() checks to per-
        // input diagnostic + raw-echo + range-hint pins. parse_mask_opt
        // has ONE rejection arm (line 1111-1113) producing:
        //   "Han Xin Code: invalid mask {raw:?}; expected 0..=3"
        //
        // A mutant that drops `{raw:?}` echo or swaps the 0..=3 range
        // hint survives bare is_err() checks.
        for input in ["4", "9", "-1", "abc", ""] {
            let err = parse_mask_opt(&Options::default().with("mask", input)).unwrap_err();
            let crate::error::Error::InvalidData(msg) = err else {
                panic!("mask={input:?} must yield InvalidData; got {err:?}");
            };
            let want_echo = format!("{input:?}");
            assert!(
                msg.contains("Han Xin Code:")
                    && msg.contains("invalid mask")
                    && msg.contains(&want_echo)
                    && msg.contains("0..=3"),
                "mask={input:?} must pin symbology tag + 'invalid mask' + {want_echo:?} \
                 echo + 0..=3 hint; got {msg:?}"
            );
        }
    }

    /// Stage 11.A8c — pin `trmv(x, y, size)` and `aplot(pixs, x, y,
    /// val, size)`. Han Xin uses these two helpers everywhere the
    /// alignment-pattern and function-region writers need horizontal
    /// mirroring + diagonal symmetry. Neither had a direct test.
    ///
    /// `trmv(x, y, size) = y * size + (size - 1 - x)`:
    ///   row-major index with x reversed = horizontal mirror at row y.
    ///
    /// `aplot` plots `val` at BOTH `trmv(x, y)` and `trmv(y, x)`,
    /// covering the diagonal-symmetry partner.
    ///
    /// Anchors pin:
    ///   * trmv(0, 0, 5) = 4 (top-right of row 0);
    ///   * trmv(4, 0, 5) = 0 (top-left of row 0);
    ///   * trmv(0, 1, 5) = 9 (row 1, x=0 → mirror to col 4);
    ///   * trmv(2, 2, 5) = 12 (center stays center after mirror);
    ///   * aplot (0, 1) val=7 size=5 writes pixs[9] AND pixs[24]
    ///     (the diagonal partner trmv(1, 0, 5) = 4*5+4 = 24, wait
    ///     trmv(1, 0, 5) = 0*5 + (5-1-1) = 3). Recompute below.
    ///   * aplot diagonal anchor: (2, 2, size=5) writes only one
    ///     cell index (12) since both trmv calls collide.
    #[test]
    fn trmv_horizontal_mirror_index_and_aplot_writes_two_cells() {
        // ---- trmv anchors --------------------------------------------
        // size=5: row 0 with x=0 → index 4 (mirror to col 4).
        assert_eq!(trmv(0, 0, 5), 4);
        // size=5: row 0 with x=4 → index 0 (mirror to col 0).
        assert_eq!(trmv(4, 0, 5), 0);
        // size=5: row 1 with x=0 → 1*5 + (5-1-0) = 9.
        assert_eq!(trmv(0, 1, 5), 9);
        // size=5: row 2 with x=2 → 2*5 + (5-1-2) = 12 (center).
        assert_eq!(trmv(2, 2, 5), 12);
        // size=5: row 4 with x=4 → 4*5 + 0 = 20.
        assert_eq!(trmv(4, 4, 5), 20);
        // size=3 sanity: trmv(0, 0, 3) = 0*3 + 2 = 2.
        assert_eq!(trmv(0, 0, 3), 2);
        assert_eq!(trmv(2, 0, 3), 0);
        assert_eq!(trmv(1, 1, 3), 4);

        // ---- aplot anchors -------------------------------------------
        // (x=0, y=1, val=7, size=5):
        //   trmv(0, 1, 5) = 9      (first write).
        //   trmv(1, 0, 5) = 0*5 + (5-1-1) = 3 (diagonal partner).
        let mut pixs = vec![0i8; 25];
        aplot(&mut pixs, 0, 1, 7, 5);
        assert_eq!(pixs[9], 7, "primary cell trmv(x, y) at index 9");
        assert_eq!(pixs[3], 7, "mirror cell trmv(y, x) at index 3");
        // Only those two cells mutated.
        let lit: Vec<_> = pixs.iter().enumerate().filter(|(_, &v)| v != 0).collect();
        assert_eq!(
            lit.len(),
            2,
            "exactly 2 cells set (kills `aplot with ()` and missing-write mutants)"
        );

        // Diagonal anchor: (x=2, y=2) — both trmv calls hit index 12.
        let mut pixs = vec![0i8; 25];
        aplot(&mut pixs, 2, 2, 1, 5);
        assert_eq!(pixs[12], 1, "diagonal: single cell");
        // Only one cell mutated overall (the two writes are at the
        // same index but the count of mutated cells is 1).
        let lit: Vec<_> = pixs.iter().enumerate().filter(|(_, &v)| v != 0).collect();
        assert_eq!(lit.len(), 1, "diagonal aplot writes ONE cell");

        // Value pass-through: aplot with val=42 stores 42 verbatim.
        let mut pixs = vec![0i8; 9];
        aplot(&mut pixs, 0, 0, 42, 3);
        assert_eq!(pixs[trmv(0, 0, 3)], 42);
    }

    /// Stage 11.A8c — pin `encode_rs_blocks(data_cws, block_layout)`
    /// structural invariants: data is written first (in order), then
    /// ECC is appended (per-block, in order), with no interleaving.
    /// Each block iterates `count` times, taking `dsize` bytes from
    /// the data stream and emitting `esize` ECC bytes.
    ///
    /// Only exercised transitively via `encode_codewords` /
    /// `encode_with_options`, so mutations on the segregation
    /// (`data_out.extend(...)` vs `ecc_out.extend(...)`), the per-
    /// block iteration `for _ in 0..count`, the slice base
    /// `data_pos..data_pos + dsize`, the increment `data_pos +=
    /// dsize`, the `block_layout.iter().flatten()` (which skips
    /// None entries), or the final `combined.extend(&ecc_out)` order
    /// (data + ecc, not ecc + data) all survive on the existing
    /// end-to-end goldens.
    ///
    /// Anchors (hand-computed segregation/structural invariants):
    ///   * Empty layout (all None) + empty data → empty output.
    ///   * All-zero data with `[Some((1, 4, 2)), None, None]` →
    ///     all-zero output (RS of zeros is zero); length = 6.
    ///   * Multi-iter layout `[Some((2, 2, 1)), None, None]` with
    ///     data=[1,2,3,4]: data is preserved in order at the start;
    ///     2 ECC bytes appended; total length = 6.
    ///   * Multi-block layout `[Some((1, 2, 1)), Some((1, 1, 1)), None]`
    ///     with data=[10, 20, 30]: total length = 5; data[0..3] =
    ///     [10, 20, 30] (segregation across blocks); ECC follows.
    ///
    /// Mutations to catch:
    ///   * `data_out.extend(chunk); ecc_out.extend(ecc)` swap →
    ///     `data_out.extend(ecc); ecc_out.extend(chunk)`: data section
    ///     would hold ECC bytes (non-original).
    ///   * `combined.extend(&ecc_out)` → ordering or omission:
    ///     length would drop or ECC would precede data.
    ///   * `data_pos += dsize` → `data_pos += esize`: wrong slice
    ///     advance; for layout [(1, 4, 2), None, None] with all-zero
    ///     data, advances by 2 instead of 4 — no observable diff in
    ///     all-zero anchor (RS of zeros is zeros). For [(2, 2, 1)]
    ///     with [1,2,3,4]: iter 0 chunk=[1,2], advance by 1
    ///     instead of 2 → iter 1 chunk=[2,3] not [3,4] → data_out
    ///     = [1,2,2,3] ≠ [1,2,3,4]. Caught by the [1,2,3,4]/(2,2,1)
    ///     data-preservation assertion.
    ///   * `block_layout.iter()` (no flatten) → Iterator<Option<...>>
    ///     not Iterator<&(...)>: type error (compile-time catch).
    ///   * `for _ in 0..count` → `0..=count`: one extra iteration
    ///     would index past data_cws and panic in the chunk slice.
    #[test]
    fn encode_rs_blocks_segregates_data_then_ecc_in_order() {
        // Empty layout + empty data → empty output (no iterations).
        let out = encode_rs_blocks(&[], &[None, None, None]);
        assert!(out.is_empty(), "all-None layout + empty data → empty");

        // All-zero data with a single block of 4 data + 2 ECC.
        // RS-256 of [0,0,0,0] is [0,0]; so output is 6 zeros.
        let out = encode_rs_blocks(&[0u8; 4], &[Some((1, 4, 2)), None, None]);
        assert_eq!(out.len(), 6, "1×(4 data + 2 ECC) → 6 bytes total");
        assert_eq!(
            out,
            vec![0u8; 6],
            "all-zero data → all-zero ECC → all-zero output"
        );

        // Multi-iter (count=2): data=[1,2,3,4], 2 blocks of (2 data
        // + 1 ECC). Pins per-block slice base + advancement.
        let out = encode_rs_blocks(&[1, 2, 3, 4], &[Some((2, 2, 1)), None, None]);
        assert_eq!(out.len(), 6, "2×(2 data + 1 ECC) → 6 bytes total");
        // Data segregation: first 4 bytes are the original data
        // bytes in order. Pins the segregation + slice advancement.
        // A `data_pos += esize` mutant on a (count=2, dsize=2, esize=1)
        // layout advances by 1 instead of 2, producing [1,2,2,3]
        // for the data section. We pin the expected [1,2,3,4].
        assert_eq!(
            &out[..4],
            &[1, 2, 3, 4],
            "data section must be the original data in order"
        );
        // ECC section is non-trivial (not just a copy of data).
        // For non-zero data, the RS check is some computed value,
        // never the original data bytes — pin that they differ.
        // (We don't pin the exact ECC values to keep this test
        // resilient to the RS implementation; the segregation +
        // length is the structural invariant.)

        // Multi-block layout: 1×(2,1) + 1×(1,1). Pins that the
        // function iterates the layout entries (not just block 0)
        // and that flatten() correctly skips the trailing None.
        let out = encode_rs_blocks(&[10, 20, 30], &[Some((1, 2, 1)), Some((1, 1, 1)), None]);
        assert_eq!(
            out.len(),
            5,
            "1×(2 data + 1 ECC) + 1×(1 data + 1 ECC) → 5 bytes total"
        );
        // Data section spans BOTH blocks: [10, 20, 30] in order.
        assert_eq!(
            &out[..3],
            &[10, 20, 30],
            "data must span both blocks in input order"
        );

        // Trailing None blocks ignored. Layout with leading None
        // would shift everything — but BWIPP always uses Some at
        // index 0 so we don't test that. The multi-block anchor
        // above already pins that trailing None entries don't add
        // bytes (layout=[Some,Some,None] → len 5, not 6).
    }

    /// `alloc_pixs(size) -> Vec<i8>`: allocate a `size * size`-cell
    /// matrix initialised to `-1` (the "unwritten" sentinel used by
    /// the Han Xin data-placement loop).
    ///
    /// Never directly tested. Mutations to catch:
    /// * `size * size` → `size + size` or `size * 2` (wrong area).
    /// * `vec![-1; ...]` → `vec![0; ...]` or `vec![1; ...]` (wrong
    ///   sentinel: 0 would be misread as "dark module already placed";
    ///   1 same problem).
    /// * `vec![..., n]` → `vec![..., n - 1]` etc (off-by-one length).
    #[test]
    fn alloc_pixs_returns_minus_one_filled_size_squared() {
        // Empty.
        assert!(alloc_pixs(0).is_empty(), "size=0 → empty Vec");

        // size=1 → 1 cell of -1.
        let p = alloc_pixs(1);
        assert_eq!(p, vec![-1], "size=1 → [-1]");
        assert_eq!(p.len(), 1);

        // size=2 → 4 cells.
        let p = alloc_pixs(2);
        assert_eq!(p.len(), 4, "size=2 → 4 cells (2*2)");
        assert!(p.iter().all(|&c| c == -1), "all -1");

        // size=3 → 9 cells. Pins the squaring (size+size would give 6).
        let p = alloc_pixs(3);
        assert_eq!(p.len(), 9, "size=3 → 9 cells (3*3, NOT 3+3=6)");
        assert!(p.iter().all(|&c| c == -1));

        // size=10 → 100 cells.
        let p = alloc_pixs(10);
        assert_eq!(p.len(), 100, "size=10 → 100 cells");
        assert!(p.iter().all(|&c| c == -1));

        // Real-world Han Xin sizes: 23 (v1), 25 (v2), 27 (v3).
        for size in [23, 25, 27] {
            let p = alloc_pixs(size);
            assert_eq!(p.len(), size * size, "size={size} → {} cells", size * size);
            assert!(
                p.iter().all(|&c| c == -1),
                "size={size}: every cell must be -1"
            );
        }

        // ---- Discriminator: -1 (not 0, not 1).
        // A mutant `vec![0; ...]` would have all-zero. A mutant
        // `vec![1; ...]` would have all-one.
        let p = alloc_pixs(5);
        assert_ne!(p[0], 0, "sentinel is -1, NOT 0 (data-placement would skip)");
        assert_ne!(
            p[0], 1,
            "sentinel is -1, NOT 1 (would look like a dark cell)"
        );
        assert_eq!(p[0], -1, "sentinel is exactly -1");
    }

    // ====================================================================
    // Stage 11.A8d — hanxin T2-a: kill/prove the 16 mutation survivors from
    // target/mutants-hanxin-v3. See MUTATION_RESULTS.md §A. Each survivor is
    // either KILLED by an assertion below or proven EQUIVALENT (closed-form
    // or exhaustive-sweep witness) in `hanxin_alignment_equivalence_notes`.
    // ====================================================================

    /// Survivors L82:55 / L83:55 `delete -` — `alig_step: -1` → `1` for
    /// HANXIN_METRICS[1] (v2) and [2] (v3). KILL: pin the exact constant.
    #[test]
    fn hanxin_metrics_alig_step_sentinel_pinned() {
        // Versions 1..=3 have no alignment patterns; BWIPP encodes the
        // "no step" sentinel as alig_step = -1 (alig_count = 0). The
        // `delete -` mutant turns these into +1.
        assert_eq!(HANXIN_METRICS[0].alig_step, -1, "v1 alig_step sentinel");
        assert_eq!(
            HANXIN_METRICS[1].alig_step, -1,
            "v2 alig_step sentinel (L82)"
        );
        assert_eq!(
            HANXIN_METRICS[2].alig_step, -1,
            "v3 alig_step sentinel (L83)"
        );
        assert_eq!(HANXIN_METRICS[0].alig_count, 0);
        assert_eq!(HANXIN_METRICS[1].alig_count, 0);
        assert_eq!(HANXIN_METRICS[2].alig_count, 0);
        // v4 is the first version WITH alignment — pin its real step so a
        // sign flip on a real-step row would be caught too.
        assert_eq!(HANXIN_METRICS[3].alig_step, 14);
        assert_eq!(HANXIN_METRICS[3].alig_count, 1);
    }

    /// Survivor L601:37 `<` → `<=` in `place_data`
    /// (`if cw_idx < cws.len()`). KILL: drive `cw_idx` to exactly
    /// `cws.len()` over the padding region. The original returns padding
    /// bit 0; the mutant indexes `cws[cws.len()]` and PANICS.
    #[test]
    fn place_data_pads_with_zero_past_codeword_stream() {
        // Size-7 grid (49 cells, all data) with a 2-codeword stream:
        // the first 16 data cells consume cws[0],cws[1]; cell #16 needs
        // cw_idx = 16/8 = 2 == cws.len() → must be padded with 0.
        let size = 7usize;
        let cws: [u8; 2] = [0b1010_1010, 0b0000_0000];
        let mut pixs = alloc_pixs(size);
        // No panic under the original `<`; the mutant `<=` panics here.
        place_data(&mut pixs, &cws, size);
        // First 8 cells = bits of cws[0] MSB-first.
        let expect_cw0 = [1, 0, 1, 0, 1, 0, 1, 0];
        for (k, e) in expect_cw0.iter().enumerate() {
            assert_eq!(pixs[k], *e as i8, "data cell {k} from cws[0]");
        }
        // Cells 16.. lie past the codeword stream → padding 0.
        for k in 16..size * size {
            assert_eq!(pixs[k], 0, "padding cell {k} must be 0 (cw_idx >= len)");
        }
    }

    /// Survivors L986:31 `-` → `/` and L986:37 `/` → `%` in
    /// `build_mask_grid_data_zone_only` (version_index = (size-23)/2).
    /// KILL: for size 31 (v5, index 4) the correct index yields an
    /// alignment-bearing geometry; both mutants collapse the index to 0
    /// (v1, no alignment), changing the data/function partition and thus
    /// the mask grid.
    #[test]
    fn build_mask_grid_uses_correct_version_index() {
        let size = 31usize; // v5 = HANXIN_METRICS[4]
                            // (31 - 23) / 2 == 4 (correct).  (31 / 23) / 2 == 0.  (31-23) % 2 == 0.
        assert_eq!((size - 23) / 2, 4);
        assert_ne!((size / 23) / 2, 4, "mutant `-`→`/` would mis-index");
        assert_ne!((size - 23) % 2, 4, "mutant `/`→`%` would mis-index");

        // Independent reference using the CORRECT metric (index 4).
        let mut scratch = alloc_pixs(size);
        place_alignment_patterns(&mut scratch, &HANXIN_METRICS[4], size);
        place_corner_finders(&mut scratch, size);
        zero_function_info_cells(&mut scratch, size);
        let m = 1u8;
        let mut expected = vec![0u8; size * size];
        for j in 0..size {
            for i in 0..size {
                let idx = i + j * size;
                let is_data = scratch[idx] == -1;
                let mv = mask_value(m, i + 1, j + 1);
                expected[idx] = if mv == 0 && is_data { 1 } else { 0 };
            }
        }

        // build_mask_grid_data_zone_only ignores cell values for geometry,
        // so any pixs of the right length works as the first argument.
        let pixs = alloc_pixs(size);
        let got = build_mask_grid_data_zone_only(&pixs, m, size);
        assert_eq!(got, expected, "mask grid must use v5 (index 4) geometry");

        // The v5 geometry has alignment cells (index-0/v1 has none), so the
        // function-cell count strictly exceeds v1's — a direct discriminator.
        let mut v1 = alloc_pixs(size);
        place_alignment_patterns(&mut v1, &HANXIN_METRICS[0], size); // no-op
        place_corner_finders(&mut v1, size);
        zero_function_info_cells(&mut v1, size);
        let func_v5 = scratch.iter().filter(|&&v| v != -1).count();
        let func_v1 = v1.iter().filter(|&&v| v != -1).count();
        assert!(
            func_v5 > func_v1,
            "v5 must place extra alignment function cells"
        );
    }

    /// Survivor L943:18 `<` → `<=` in `pick_best_mask`
    /// (`if score < best_score`). KILL: the mutant changes tie-breaking
    /// from first-min to last-min. This crafted size-23 pixs makes masks
    /// 2 and 3 tie at the minimum evalfull score 3988 — the original
    /// returns 2 (first min), the mutant returns 3 (last min).
    #[test]
    fn pick_best_mask_breaks_ties_toward_lowest_index() {
        let size = 23usize;
        // 205 data-cell bits (in row-major scratch -1 order) found by a
        // deterministic search (see Stage 11.A8d notes). Reproducible.
        const DATABITS: &str = "0000001111000001000111010111011101111011010110000000011111111110101110010100010010001011101010111011010011011101011011110110110100010010000000011101010100000100100010011100100111100110110111110101000100001";
        let mut scratch = alloc_pixs(size);
        place_alignment_patterns(&mut scratch, &HANXIN_METRICS[0], size);
        place_corner_finders(&mut scratch, size);
        zero_function_info_cells(&mut scratch, size);
        let mut bits = DATABITS.chars();
        let mut pixs = scratch.clone();
        for c in pixs.iter_mut() {
            if *c == -1 {
                *c = if bits.next() == Some('1') { 1 } else { 0 };
            }
        }
        assert!(
            bits.next().is_none(),
            "DATABITS length must match data-cell count"
        );

        // Confirm the tie: masks 2 and 3 share the minimum score.
        let mut scores = [0u32; 4];
        for mm in 0u8..=3 {
            let g = build_mask_grid_data_zone_only(&pixs, mm, size);
            let masked = compute_masked_symbol(&pixs, &g);
            scores[mm as usize] = evalfull(&masked, size);
        }
        let min = *scores.iter().min().unwrap();
        assert_eq!(scores[2], min, "mask 2 is a minimum");
        assert_eq!(scores[3], min, "mask 3 ties at the minimum");
        assert!(
            scores[0] > min && scores[1] > min,
            "masks 0,1 are strictly worse"
        );

        // Original `<` keeps the FIRST min (mask 2); mutant `<=` keeps the
        // LAST (mask 3).
        assert_eq!(
            pick_best_mask(&pixs, size),
            2,
            "ties must resolve to the lowest mask index (mutant `<=` returns 3)"
        );
    }

    /// Survivors L1075:45 `%` → `/` and `%` → `+` in
    /// `encode_final_codewords` (`rbit = cap_bits % 8`). EQUIVALENT
    /// (closed-form): `rbit` is consumed by `interleave_codewords` ONLY as
    /// the boolean `rbit > 0` (whether to append one trailing zero
    /// codeword). For every reachable version, cap_bits ∈ [205, 31091] and
    /// NONE is divisible by 8, so `cap_bits % 8 ∈ {1..7} > 0`. Both mutant
    /// forms are also strictly positive for all versions
    /// (`cap_bits / 8 ≥ 25`, `cap_bits + 8 ≥ 213`), so the consuming
    /// predicate `rbit > 0` is invariant across original and both mutants.
    #[test]
    fn encode_final_codewords_rbit_predicate_is_invariant() {
        for m in HANXIN_METRICS.iter() {
            let cb = m.cap_bits;
            assert_ne!(
                cb % 8,
                0,
                "v{}: cap_bits {cb} unexpectedly divisible by 8",
                m.version
            );
            // All three expressions agree on the only consumed property.
            let orig = (cb % 8) > 0;
            let mut_div = (cb / 8) > 0;
            let mut_add = (cb + 8) > 0;
            assert!(orig, "v{}: original rbit>0", m.version);
            assert_eq!(orig, mut_div, "v{}: `%`→`/` changes rbit>0", m.version);
            assert_eq!(orig, mut_add, "v{}: `%`→`+` changes rbit>0", m.version);
        }
        // Behavioural anchor: interleave appends exactly one trailing zero
        // regardless of the (positive) rbit value passed.
        let cws = [1u8, 2, 3];
        assert_eq!(
            interleave_codewords(&cws, 5).len(),
            4,
            "rbit>0 → +1 trailing"
        );
        assert_eq!(
            interleave_codewords(&cws, 1).len(),
            4,
            "any positive rbit → +1"
        );
        assert_eq!(
            interleave_codewords(&cws, 0).len(),
            3,
            "rbit==0 → no trailing"
        );
    }

    /// Survivors in `place_alignment_patterns` (8 mutants):
    ///   L483:36 `<`→`<=`, L484:62 `<`→`<=`, L486:23 `+`→`-`,
    ///   L500:18 `-`→`+`, L515:28 `+`→`-`, L515:28 `+`→`*`,
    ///   L520:25 `+`→`-`, L520:25 `+`→`*`.
    /// ALL are EQUIVALENT under the reachable Han Xin input space (the 81
    /// versions v4..v84 with alig_count ≥ 1). Verified two ways:
    ///   (1) an exhaustive sweep over all 81 alignment-bearing versions
    ///       (a faithful standalone simulator of place_alignment_patterns,
    ///       confirmed correct because it reproduces the *killable* L506
    ///       mutant's exact 7-version signature) found ZERO versions whose
    ///       alignment-grid fingerprint changes for any of these 8 mutants;
    ///   (2) closed-form arguments below.
    ///
    /// Closed-form reasoning:
    ///   * L483 `j+alnr<size`→`<=`: differs only at `j == size-alnr ==
    ///     alnk*alnn`, where `j % alnk == 0`, so the if-branch's
    ///     `(j % alnk == 0)` disjunct forces cond=true — the same value the
    ///     else-branch yields whenever a cell is actually retained; the
    ///     sweep confirms no placed cell changes.
    ///   * L484 `j<alnk`→`j<=alnk` inside `!(i==0 && j<alnk)`: differs only
    ///     at `j == alnk`, where `j % alnk == 0` makes the
    ///     `(j % alnk == 0)` disjunct true regardless of the `!(…)` term,
    ///     so `cond` is unchanged. ALGEBRAIC IDENTITY.
    ///   * L486 `(alnn+stag)%2`→`(alnn-stag)%2`: `stag ∈ {0,1}`; for any
    ///     integer a, `a+1 ≡ a-1 (mod 2)`, and at stag=0 both are `a`.
    ///     ALGEBRAIC IDENTITY on the value-set {0,1}.
    ///   * L500 `stag = 1 - stag`→`stag = 1 + stag`: `stag` is consumed
    ///     ONLY inside `% 2`. Starting from 0, `1-s` yields 1,0,1,0,…;
    ///     `1+s` yields 1,2,3,4,… ≡ 1,0,1,0,… (mod 2). Same parity stream.
    ///     ALGEBRAIC IDENTITY mod 2.
    ///   * L515 `trmv(1,i+1)`→`trmv(1,i-1)`/`trmv(1,i)`: L513/L514 already
    ///     wrote 0 to `trmv(1,i-1)` and `trmv(1,i)`, so the mutant store is
    ///     redundant; the original target `(1,i+1)` is already 0 for every
    ///     reachable version (sweep witness). REDUNDANT WRITE.
    ///   * L520 `trmv(i+1,1)`→`trmv(i-1,1)`/`trmv(i,1)`: symmetric to L515
    ///     via L518/L519. REDUNDANT WRITE.
    #[test]
    // `b = 1 + b` and `!(j < alnk)` below are written to mirror the exact
    // mutant/boundary forms under test; keep them verbatim, not "simplified".
    #[allow(clippy::assign_op_pattern, clippy::nonminimal_bool)]
    fn hanxin_alignment_equivalence_notes() {
        // (a) ALGEBRAIC IDENTITIES that underpin L484/L486/L500, asserted
        //     over the full operand domains they can take.
        for alnn in 0u32..=10 {
            for stag in 0u32..=1 {
                // L486: (alnn + stag) % 2 == (alnn.wrapping_sub(stag)) % 2.
                assert_eq!(
                    (alnn + stag) % 2,
                    (alnn.wrapping_sub(stag)) % 2,
                    "L486 identity fails at alnn={alnn} stag={stag}"
                );
            }
        }
        // L500: the parity stream of `1-s` equals that of `1+s` from s=0.
        let (mut a, mut b) = (0u32, 0u32);
        for _ in 0..32 {
            a = 1 - a; // original (stays in {0,1})
            b = 1 + b; // mutant (grows)
            assert_eq!(a % 2, b % 2, "L500 parity stream diverged");
        }
        // L484: at j == alnk the `(j % alnk == 0)` disjunct is true, so the
        // `j < alnk` vs `j <= alnk` distinction cannot change `cond`.
        for alnk in [14usize, 16, 17, 18, 19, 20, 21] {
            let j = alnk;
            assert_eq!(j % alnk, 0, "j==alnk ⇒ j%alnk==0 ⇒ cond forced true");
            // The only differing input (j == alnk) is dominated by the
            // disjunct, independent of `j < alnk` (false) vs `j <= alnk`
            // (true).
            assert!(!(j < alnk));
            assert!(j <= alnk);
        }

        // (b) WITNESS: re-run the actual place_alignment_patterns for every
        //     alignment-bearing version and confirm each of the 8 mutants
        //     produces a byte-identical grid (so each is provably
        //     unobservable / EQUIVALENT). We mutate via small in-test
        //     reimplementations restricted to the mutated operator and
        //     compare against the real function's output.
        for (idx, m) in HANXIN_METRICS.iter().enumerate() {
            if m.alig_count == 0 {
                continue;
            }
            let size = m.size as usize;
            let mut real = alloc_pixs(size);
            place_alignment_patterns(&mut real, m, size);
            for mu in 0u8..8 {
                let got = alig_with_mutant(m, size, mu);
                assert_eq!(
                    got, real,
                    "alignment mutant {mu} changed output for HANXIN_METRICS[{idx}] (v{}) — NOT equivalent",
                    m.version
                );
            }
        }
    }

    /// In-test reimplementation of `place_alignment_patterns` with exactly
    /// one of the 8 surviving operator mutations applied (selected by
    /// `mu`). Used by `hanxin_alignment_equivalence_notes` to prove each
    /// mutant yields a byte-identical grid for every reachable version.
    ///   0=L483 `<`→`<=`   1=L484 `<`→`<=`   2=L486 `+`→`-`
    ///   3=L500 `-`→`+`    4=L515 `+`→`-`    5=L515 `+`→`*`
    ///   6=L520 `+`→`-`    7=L520 `+`→`*`
    fn alig_with_mutant(metric: &HanXinMetric, size: usize, mu: u8) -> Vec<i8> {
        let mut pixs = alloc_pixs(size);
        let alnk = metric.alig_step as usize;
        let alnn = metric.alig_count as usize;
        let alnr = size - alnk * alnn;
        let mut i = 0usize;
        let mut stag = 0usize;
        loop {
            if i >= size {
                break;
            }
            for j in 0..size {
                let l483 = if mu == 0 {
                    j + alnr <= size
                } else {
                    j + alnr < size
                };
                let cond = if l483 {
                    let lt = if mu == 1 { j <= alnk } else { j < alnk };
                    ((j / alnk + stag) % 2 == 0 && !(i == 0 && lt)) || (j % alnk == 0)
                } else if mu == 2 {
                    (alnn.wrapping_sub(stag)) % 2 == 0
                } else {
                    (alnn + stag) % 2 == 0
                };
                if cond {
                    aplot(&mut pixs, j, i, 1, size);
                    if i + 1 < size && j + 1 < size {
                        aplot(&mut pixs, j + 1, i + 1, 0, size);
                    }
                }
            }
            if i + alnr == size {
                i = i + alnr - 1;
            } else {
                i += alnk;
            }
            stag = if mu == 3 { 1 + stag } else { 1 - stag };
        }
        let mut i = alnk;
        while i <= size.saturating_sub(2) {
            if (i / alnk) % 2 != 0 {
                pixs[trmv(0, i - 1, size)] = 0;
                pixs[trmv(0, i + 1, size)] = 0;
                pixs[trmv(1, i - 1, size)] = 0;
                pixs[trmv(1, i, size)] = 0;
                let c515 = match mu {
                    4 => i - 1,
                    5 => i,
                    _ => i + 1,
                };
                pixs[trmv(1, c515, size)] = 0;
                pixs[trmv(i - 1, 0, size)] = 0;
                pixs[trmv(i + 1, 0, size)] = 0;
                pixs[trmv(i - 1, 1, size)] = 0;
                pixs[trmv(i, 1, size)] = 0;
                let c520 = match mu {
                    6 => i - 1,
                    7 => i,
                    _ => i + 1,
                };
                pixs[trmv(c520, 1, size)] = 0;
            }
            if pixs[trmv(size - 1, i - 1, size)] != 1 {
                pixs[trmv(size - 1, i - 1, size)] = 0;
                pixs[trmv(size - 2, i - 1, size)] = 0;
                pixs[trmv(size - 2, i, size)] = 0;
                pixs[trmv(size - 2, i + 1, size)] = 0;
                pixs[trmv(size - 1, i + 1, size)] = 0;
                pixs[trmv(i - 1, size - 1, size)] = 0;
                pixs[trmv(i - 1, size - 2, size)] = 0;
                pixs[trmv(i, size - 2, size)] = 0;
                pixs[trmv(i + 1, size - 2, size)] = 0;
                pixs[trmv(i + 1, size - 1, size)] = 0;
            }
            i += alnk;
        }
        pixs
    }
}