zenjpeg 0.8.1

Pure Rust JPEG encoder/decoder with perceptual optimizations
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
//! Tests for lossless JPEG transforms.

use super::coeff_transform::*;
use crate::foundation::consts::JPEG_NATURAL_ORDER;

/// Create a test block where coefficient at position (row, col) in the 8×8 matrix
/// has value `row * 8 + col + 1` (so DC = 1, and values increase left-to-right, top-to-bottom).
///
/// The block is stored in zigzag order.
fn make_test_block() -> [i16; 64] {
    let mut block = [0i16; 64];
    for z in 0..64 {
        let linear = JPEG_NATURAL_ORDER[z] as usize;
        // Value encodes the 8×8 position: row * 8 + col + 1
        block[z] = (linear + 1) as i16;
    }
    block
}

/// Read the coefficient at 8×8 position (row, col) from a zigzag-ordered block.
fn read_at(block: &[i16; 64], row: usize, col: usize) -> i16 {
    let linear = row * 8 + col;
    let z = super::coeff_transform::ZIGZAG_FROM_LINEAR[linear] as usize;
    block[z]
}

#[test]
fn test_zigzag_roundtrip() {
    // Verify our zigzag ↔ linear conversion is consistent
    for z in 0..64usize {
        let linear = JPEG_NATURAL_ORDER[z] as usize;
        let back = super::coeff_transform::ZIGZAG_FROM_LINEAR[linear];
        assert_eq!(back as usize, z, "roundtrip failed for zigzag index {z}");
    }
}

#[test]
fn test_identity_transform() {
    let block = make_test_block();
    let bt = BlockTransform::for_transform(LosslessTransform::None);
    let result = bt.apply(&block);
    assert_eq!(
        block, result,
        "identity transform should not change coefficients"
    );
}

#[test]
fn test_flip_horizontal_negates_odd_cols() {
    let block = make_test_block();
    let bt = BlockTransform::for_transform(LosslessTransform::FlipHorizontal);
    let result = bt.apply(&block);

    for row in 0..8 {
        for col in 0..8 {
            let src_val = read_at(&block, row, col);
            let dst_val = read_at(&result, row, col);
            if col % 2 == 1 {
                assert_eq!(
                    dst_val, -src_val,
                    "H-flip should negate at ({row},{col}): expected {}, got {dst_val}",
                    -src_val
                );
            } else {
                assert_eq!(
                    dst_val, src_val,
                    "H-flip should preserve at ({row},{col}): expected {src_val}, got {dst_val}"
                );
            }
        }
    }
}

#[test]
fn test_flip_vertical_negates_odd_rows() {
    let block = make_test_block();
    let bt = BlockTransform::for_transform(LosslessTransform::FlipVertical);
    let result = bt.apply(&block);

    for row in 0..8 {
        for col in 0..8 {
            let src_val = read_at(&block, row, col);
            let dst_val = read_at(&result, row, col);
            if row % 2 == 1 {
                assert_eq!(dst_val, -src_val, "V-flip should negate at ({row},{col})");
            } else {
                assert_eq!(dst_val, src_val, "V-flip should preserve at ({row},{col})");
            }
        }
    }
}

#[test]
fn test_transpose_swaps_row_col() {
    let block = make_test_block();
    let bt = BlockTransform::for_transform(LosslessTransform::Transpose);
    let result = bt.apply(&block);

    for row in 0..8 {
        for col in 0..8 {
            let src_val = read_at(&block, row, col);
            // After transpose, value at (row, col) in source appears at (col, row) in dest
            let dst_val = read_at(&result, col, row);
            assert_eq!(
                dst_val, src_val,
                "Transpose: src({row},{col})={src_val} should appear at dst({col},{row}), got {dst_val}"
            );
        }
    }
}

#[test]
fn test_rotate90_is_transpose_plus_hflip() {
    let block = make_test_block();

    // Rotate90 should equal Transpose followed by FlipHorizontal
    let bt_rot90 = BlockTransform::for_transform(LosslessTransform::Rotate90);
    let result_rot90 = bt_rot90.apply(&block);

    let bt_transpose = BlockTransform::for_transform(LosslessTransform::Transpose);
    let bt_hflip = BlockTransform::for_transform(LosslessTransform::FlipHorizontal);
    let intermediate = bt_transpose.apply(&block);
    let result_composed = bt_hflip.apply(&intermediate);

    assert_eq!(
        result_rot90, result_composed,
        "Rotate90 should equal Transpose + FlipHorizontal"
    );
}

#[test]
fn test_rotate180_is_hflip_plus_vflip() {
    let block = make_test_block();

    let bt_rot180 = BlockTransform::for_transform(LosslessTransform::Rotate180);
    let result_rot180 = bt_rot180.apply(&block);

    let bt_hflip = BlockTransform::for_transform(LosslessTransform::FlipHorizontal);
    let bt_vflip = BlockTransform::for_transform(LosslessTransform::FlipVertical);
    let intermediate = bt_hflip.apply(&block);
    let result_composed = bt_vflip.apply(&intermediate);

    assert_eq!(
        result_rot180, result_composed,
        "Rotate180 should equal FlipHorizontal + FlipVertical"
    );
}

#[test]
fn test_rotate270_is_transpose_plus_vflip() {
    let block = make_test_block();

    let bt_rot270 = BlockTransform::for_transform(LosslessTransform::Rotate270);
    let result_rot270 = bt_rot270.apply(&block);

    let bt_transpose = BlockTransform::for_transform(LosslessTransform::Transpose);
    let bt_vflip = BlockTransform::for_transform(LosslessTransform::FlipVertical);
    let intermediate = bt_transpose.apply(&block);
    let result_composed = bt_vflip.apply(&intermediate);

    assert_eq!(
        result_rot270, result_composed,
        "Rotate270 should equal Transpose + FlipVertical"
    );
}

#[test]
fn test_transverse_is_transpose_plus_rot180() {
    let block = make_test_block();

    let bt_transverse = BlockTransform::for_transform(LosslessTransform::Transverse);
    let result_transverse = bt_transverse.apply(&block);

    let bt_transpose = BlockTransform::for_transform(LosslessTransform::Transpose);
    let bt_rot180 = BlockTransform::for_transform(LosslessTransform::Rotate180);
    let intermediate = bt_transpose.apply(&block);
    let result_composed = bt_rot180.apply(&intermediate);

    assert_eq!(
        result_transverse, result_composed,
        "Transverse should equal Transpose + Rotate180"
    );
}

#[test]
fn test_dc_coefficient_never_negated() {
    // The DC coefficient (position 0,0) has even row and even col,
    // so it should never be negated by any transform.
    let mut block = [0i16; 64];
    block[0] = 1000; // DC is at zigzag index 0

    for transform in [
        LosslessTransform::None,
        LosslessTransform::FlipHorizontal,
        LosslessTransform::FlipVertical,
        LosslessTransform::Transpose,
        LosslessTransform::Rotate90,
        LosslessTransform::Rotate180,
        LosslessTransform::Rotate270,
        LosslessTransform::Transverse,
    ] {
        let bt = BlockTransform::for_transform(transform);
        let result = bt.apply(&block);
        // DC is always at (0,0) → zigzag 0, and should never be negated
        assert_eq!(
            read_at(&result, 0, 0),
            1000,
            "DC should not be negated by {:?}",
            transform
        );
    }
}

#[test]
fn test_four_rotations_is_identity() {
    // Applying Rotate90 four times should give back the original
    let block = make_test_block();
    let bt = BlockTransform::for_transform(LosslessTransform::Rotate90);

    let r1 = bt.apply(&block);
    let r2 = bt.apply(&r1);
    let r3 = bt.apply(&r2);
    let r4 = bt.apply(&r3);

    assert_eq!(block, r4, "four 90° rotations should be identity");
}

#[test]
fn test_double_flip_h_is_identity() {
    let block = make_test_block();
    let bt = BlockTransform::for_transform(LosslessTransform::FlipHorizontal);
    let r1 = bt.apply(&block);
    let r2 = bt.apply(&r1);
    assert_eq!(block, r2, "double horizontal flip should be identity");
}

#[test]
fn test_double_flip_v_is_identity() {
    let block = make_test_block();
    let bt = BlockTransform::for_transform(LosslessTransform::FlipVertical);
    let r1 = bt.apply(&block);
    let r2 = bt.apply(&r1);
    assert_eq!(block, r2, "double vertical flip should be identity");
}

#[test]
fn test_double_transpose_is_identity() {
    let block = make_test_block();
    let bt = BlockTransform::for_transform(LosslessTransform::Transpose);
    let r1 = bt.apply(&block);
    let r2 = bt.apply(&r1);
    assert_eq!(block, r2, "double transpose should be identity");
}

#[test]
fn test_double_rot180_is_identity() {
    let block = make_test_block();
    let bt = BlockTransform::for_transform(LosslessTransform::Rotate180);
    let r1 = bt.apply(&block);
    let r2 = bt.apply(&r1);
    assert_eq!(block, r2, "double 180° rotation should be identity");
}

#[test]
fn test_rot90_plus_rot270_is_identity() {
    let block = make_test_block();
    let bt90 = BlockTransform::for_transform(LosslessTransform::Rotate90);
    let bt270 = BlockTransform::for_transform(LosslessTransform::Rotate270);
    let r1 = bt90.apply(&block);
    let r2 = bt270.apply(&r1);
    assert_eq!(block, r2, "90° + 270° should be identity");
}

#[test]
fn test_double_transverse_is_identity() {
    let block = make_test_block();
    let bt = BlockTransform::for_transform(LosslessTransform::Transverse);
    let r1 = bt.apply(&block);
    let r2 = bt.apply(&r1);
    assert_eq!(block, r2, "double transverse should be identity");
}

#[test]
fn test_swaps_dimensions() {
    assert!(!LosslessTransform::None.swaps_dimensions());
    assert!(!LosslessTransform::FlipHorizontal.swaps_dimensions());
    assert!(!LosslessTransform::FlipVertical.swaps_dimensions());
    assert!(LosslessTransform::Transpose.swaps_dimensions());
    assert!(LosslessTransform::Rotate90.swaps_dimensions());
    assert!(!LosslessTransform::Rotate180.swaps_dimensions());
    assert!(LosslessTransform::Rotate270.swaps_dimensions());
    assert!(LosslessTransform::Transverse.swaps_dimensions());
}

// ===== Block grid remap tests =====

#[test]
fn test_block_remap_identity() {
    assert_eq!(remap_block(2, 3, 10, 8, LosslessTransform::None), (2, 3));
}

#[test]
fn test_block_remap_hflip() {
    // In a 10-wide grid, block at x=2 should go to x=7
    assert_eq!(
        remap_block(2, 3, 10, 8, LosslessTransform::FlipHorizontal),
        (7, 3)
    );
}

#[test]
fn test_block_remap_vflip() {
    // In an 8-high grid, block at y=3 should go to y=4
    assert_eq!(
        remap_block(2, 3, 10, 8, LosslessTransform::FlipVertical),
        (2, 4)
    );
}

#[test]
fn test_block_remap_transpose() {
    assert_eq!(
        remap_block(2, 3, 10, 8, LosslessTransform::Transpose),
        (3, 2)
    );
}

#[test]
fn test_block_remap_rot90() {
    // Rotate90: (bx, by) → (bh-1-by, bx)
    // (2, 3) in 10×8 → (8-1-3, 2) = (4, 2) in 8×10 grid
    assert_eq!(
        remap_block(2, 3, 10, 8, LosslessTransform::Rotate90),
        (4, 2)
    );
}

#[test]
fn test_block_remap_rot180() {
    // (2, 3) in 10×8 → (7, 4)
    assert_eq!(
        remap_block(2, 3, 10, 8, LosslessTransform::Rotate180),
        (7, 4)
    );
}

#[test]
fn test_block_remap_rot270() {
    // Rotate270: (bx, by) → (by, bw-1-bx)
    // (2, 3) → (3, 10-1-2) = (3, 7) in 8×10 grid
    assert_eq!(
        remap_block(2, 3, 10, 8, LosslessTransform::Rotate270),
        (3, 7)
    );
}

#[test]
fn test_block_remap_transverse() {
    // Transverse: (bx, by) → (bh-1-by, bw-1-bx)
    // (2, 3) → (8-1-3, 10-1-2) = (4, 7) in 8×10 grid
    assert_eq!(
        remap_block(2, 3, 10, 8, LosslessTransform::Transverse),
        (4, 7)
    );
}

// ===== Full coefficient transform tests =====

use crate::decode::{ComponentCoefficients, DecodedCoefficients};

/// Create a simple 16×16 test image with 1 component (4 blocks in a 2×2 grid).
/// Each block has a distinct DC coefficient for tracking.
fn make_test_coefficients() -> DecodedCoefficients {
    let mut coeffs = vec![0i16; 4 * 64]; // 4 blocks, 64 coefficients each

    // Set DC values to identify each block
    // Block (0,0) DC=10, Block (1,0) DC=20, Block (0,1) DC=30, Block (1,1) DC=40
    coeffs[0] = 10; // block 0 = (bx=0, by=0)
    coeffs[64] = 20; // block 1 = (bx=1, by=0)
    coeffs[2 * 64] = 30; // block 2 = (bx=0, by=1)
    coeffs[3 * 64] = 40; // block 3 = (bx=1, by=1)

    DecodedCoefficients {
        width: 16,
        height: 16,
        components: vec![ComponentCoefficients {
            id: 1,
            coeffs,
            blocks_wide: 2,
            blocks_high: 2,
            h_samp: 1,
            v_samp: 1,
            quant_table_idx: 0,
        }],
        quant_tables: vec![Some([1u16; 64])],
    }
}

fn get_dc(result: &TransformedCoefficients, comp: usize, bx: usize, by: usize) -> i16 {
    let c = &result.components[comp];
    let idx = by * c.blocks_wide + bx;
    c.coeffs[idx * 64]
}

#[test]
fn test_transform_identity() {
    let coeffs = make_test_coefficients();
    let config = TransformConfig {
        transform: LosslessTransform::None,
        edge_handling: EdgeHandling::TrimPartialBlocks,
    };
    let result = transform_coefficients(&coeffs, &config).unwrap();

    assert_eq!(result.width, 16);
    assert_eq!(result.height, 16);
    assert_eq!(get_dc(&result, 0, 0, 0), 10);
    assert_eq!(get_dc(&result, 0, 1, 0), 20);
    assert_eq!(get_dc(&result, 0, 0, 1), 30);
    assert_eq!(get_dc(&result, 0, 1, 1), 40);
}

#[test]
fn test_transform_hflip_block_positions() {
    let coeffs = make_test_coefficients();
    let config = TransformConfig {
        transform: LosslessTransform::FlipHorizontal,
        edge_handling: EdgeHandling::TrimPartialBlocks,
    };
    let result = transform_coefficients(&coeffs, &config).unwrap();

    assert_eq!(result.width, 16);
    assert_eq!(result.height, 16);
    // Columns are mirrored: block (0,y) ↔ block (1,y)
    assert_eq!(get_dc(&result, 0, 0, 0), 20); // was at (1,0)
    assert_eq!(get_dc(&result, 0, 1, 0), 10); // was at (0,0)
    assert_eq!(get_dc(&result, 0, 0, 1), 40); // was at (1,1)
    assert_eq!(get_dc(&result, 0, 1, 1), 30); // was at (0,1)
}

#[test]
fn test_transform_vflip_block_positions() {
    let coeffs = make_test_coefficients();
    let config = TransformConfig {
        transform: LosslessTransform::FlipVertical,
        edge_handling: EdgeHandling::TrimPartialBlocks,
    };
    let result = transform_coefficients(&coeffs, &config).unwrap();

    // Rows are mirrored: block (x,0) ↔ block (x,1)
    assert_eq!(get_dc(&result, 0, 0, 0), 30); // was at (0,1)
    assert_eq!(get_dc(&result, 0, 1, 0), 40); // was at (1,1)
    assert_eq!(get_dc(&result, 0, 0, 1), 10); // was at (0,0)
    assert_eq!(get_dc(&result, 0, 1, 1), 20); // was at (1,0)
}

#[test]
fn test_transform_transpose_swaps_dims() {
    let coeffs = make_test_coefficients();
    let config = TransformConfig {
        transform: LosslessTransform::Transpose,
        edge_handling: EdgeHandling::TrimPartialBlocks,
    };
    let result = transform_coefficients(&coeffs, &config).unwrap();

    assert_eq!(result.width, 16); // square, so same
    assert_eq!(result.height, 16);

    // (0,0)→(0,0), (1,0)→(0,1), (0,1)→(1,0), (1,1)→(1,1)
    assert_eq!(get_dc(&result, 0, 0, 0), 10);
    assert_eq!(get_dc(&result, 0, 1, 0), 30); // was (0,1) → now at (1,0)
    assert_eq!(get_dc(&result, 0, 0, 1), 20); // was (1,0) → now at (0,1)
    assert_eq!(get_dc(&result, 0, 1, 1), 40);
}

#[test]
fn test_transform_rot90_block_positions() {
    let coeffs = make_test_coefficients();
    let config = TransformConfig {
        transform: LosslessTransform::Rotate90,
        edge_handling: EdgeHandling::TrimPartialBlocks,
    };
    let result = transform_coefficients(&coeffs, &config).unwrap();

    // Rotate90: (bx, by) → (bh-1-by, bx)
    // (0,0)→(1,0), (1,0)→(1,1), (0,1)→(0,0), (1,1)→(0,1)
    assert_eq!(get_dc(&result, 0, 0, 0), 30); // from (0,1)
    assert_eq!(get_dc(&result, 0, 1, 0), 10); // from (0,0)
    assert_eq!(get_dc(&result, 0, 0, 1), 40); // from (1,1)
    assert_eq!(get_dc(&result, 0, 1, 1), 20); // from (1,0)
}

#[test]
fn test_transform_rot180_block_positions() {
    let coeffs = make_test_coefficients();
    let config = TransformConfig {
        transform: LosslessTransform::Rotate180,
        edge_handling: EdgeHandling::TrimPartialBlocks,
    };
    let result = transform_coefficients(&coeffs, &config).unwrap();

    // (0,0)→(1,1), (1,0)→(0,1), (0,1)→(1,0), (1,1)→(0,0)
    assert_eq!(get_dc(&result, 0, 0, 0), 40); // from (1,1)
    assert_eq!(get_dc(&result, 0, 1, 0), 30); // from (0,1)
    assert_eq!(get_dc(&result, 0, 0, 1), 20); // from (1,0)
    assert_eq!(get_dc(&result, 0, 1, 1), 10); // from (0,0)
}

#[test]
fn test_transform_nonsquare_transpose() {
    // 24×16 image (3×2 blocks) → transpose to 16×24 (2×3 blocks)
    let mut coeffs_data = vec![0i16; 6 * 64];
    for i in 0..6 {
        coeffs_data[i * 64] = (i as i16 + 1) * 10; // DC = 10, 20, 30, 40, 50, 60
    }

    let coeffs = DecodedCoefficients {
        width: 24,
        height: 16,
        components: vec![ComponentCoefficients {
            id: 1,
            coeffs: coeffs_data,
            blocks_wide: 3,
            blocks_high: 2,
            h_samp: 1,
            v_samp: 1,
            quant_table_idx: 0,
        }],
        quant_tables: vec![Some([1u16; 64])],
    };

    let config = TransformConfig {
        transform: LosslessTransform::Transpose,
        edge_handling: EdgeHandling::TrimPartialBlocks,
    };
    let result = transform_coefficients(&coeffs, &config).unwrap();

    assert_eq!(result.width, 16);
    assert_eq!(result.height, 24);
    assert_eq!(result.components[0].blocks_wide, 2);
    assert_eq!(result.components[0].blocks_high, 3);

    // Source layout (blocks_wide=3, blocks_high=2):
    //   (0,0)=10  (1,0)=20  (2,0)=30
    //   (0,1)=40  (1,1)=50  (2,1)=60
    // After transpose (blocks_wide=2, blocks_high=3):
    //   (0,0)=10  (1,0)=40
    //   (0,1)=20  (1,1)=50
    //   (0,2)=30  (1,2)=60
    assert_eq!(get_dc(&result, 0, 0, 0), 10);
    assert_eq!(get_dc(&result, 0, 1, 0), 40);
    assert_eq!(get_dc(&result, 0, 0, 1), 20);
    assert_eq!(get_dc(&result, 0, 1, 1), 50);
    assert_eq!(get_dc(&result, 0, 0, 2), 30);
    assert_eq!(get_dc(&result, 0, 1, 2), 60);
}

// ===== End-to-end pipeline tests =====

mod pipeline_tests {
    use crate::decode::DecodeConfig;
    use crate::lossless::{EdgeHandling, LosslessTransform, TransformConfig, transform};
    use enough::Unstoppable;

    /// Create a test JPEG from a known pixel pattern using zenjpeg encoder.
    fn create_test_jpeg(width: u32, height: u32) -> Vec<u8> {
        use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};

        let mut pixels = Vec::with_capacity((width * height * 3) as usize);
        for y in 0..height {
            for x in 0..width {
                // Create a pattern that's visually distinct when rotated
                pixels.push(((x * 255 / width) & 0xFF) as u8); // R: gradient L→R
                pixels.push(((y * 255 / height) & 0xFF) as u8); // G: gradient T→B
                pixels.push(128u8); // B: constant
            }
        }

        let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
        let mut enc = config
            .encode_from_bytes(width, height, PixelLayout::Rgb8Srgb)
            .unwrap();
        enc.push_packed(&pixels, Unstoppable).unwrap();
        enc.finish().unwrap()
    }

    /// Create a test JPEG with 4:2:0 subsampling.
    fn create_test_jpeg_420(width: u32, height: u32) -> Vec<u8> {
        use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};

        let mut pixels = Vec::with_capacity((width * height * 3) as usize);
        for y in 0..height {
            for x in 0..width {
                pixels.push(((x * 255 / width) & 0xFF) as u8);
                pixels.push(((y * 255 / height) & 0xFF) as u8);
                pixels.push(128u8);
            }
        }

        let config = EncoderConfig::ycbcr(90, ChromaSubsampling::Quarter);
        let mut enc = config
            .encode_from_bytes(width, height, PixelLayout::Rgb8Srgb)
            .unwrap();
        enc.push_packed(&pixels, Unstoppable).unwrap();
        enc.finish().unwrap()
    }

    #[test]
    fn test_roundtrip_identity() {
        use archmage::testing::{CompileTimePolicy, for_each_token_permutation};

        // Transform with None should produce a valid JPEG that decodes to
        // the same pixels (modulo Huffman table differences)
        let jpeg = create_test_jpeg(64, 64);

        let _ = for_each_token_permutation(CompileTimePolicy::Warn, |perm| {
            let result = transform(
                &jpeg,
                &TransformConfig {
                    transform: LosslessTransform::None,
                    edge_handling: EdgeHandling::TrimPartialBlocks,
                },
                Unstoppable,
            )
            .unwrap();

            // Result should be a valid JPEG
            assert!(result.len() > 100, "output too small at {perm}");
            assert_eq!(result[0], 0xFF);
            assert_eq!(result[1], 0xD8); // SOI

            // Decode both and compare
            let decoder = DecodeConfig::new();
            let orig = decoder.decode(&jpeg, Unstoppable).unwrap();
            let transformed = decoder.decode(&result, Unstoppable).unwrap();

            assert_eq!(orig.width(), transformed.width());
            assert_eq!(orig.height(), transformed.height());

            // Pixels should be identical (lossless round-trip of coefficients)
            let orig_px = orig.pixels_u8().unwrap();
            let trans_px = transformed.pixels_u8().unwrap();
            assert_eq!(orig_px.len(), trans_px.len());

            let mut max_diff = 0u8;
            for (a, b) in orig_px.iter().zip(trans_px.iter()) {
                let diff = (*a as i16 - *b as i16).unsigned_abs() as u8;
                max_diff = max_diff.max(diff);
            }
            assert_eq!(
                max_diff, 0,
                "identity transform should produce identical pixels at {perm}"
            );
        });
    }

    #[test]
    fn test_rotate90_dimensions() {
        let jpeg = create_test_jpeg(64, 48);

        let result = transform(
            &jpeg,
            &TransformConfig {
                transform: LosslessTransform::Rotate90,
                edge_handling: EdgeHandling::TrimPartialBlocks,
            },
            Unstoppable,
        )
        .unwrap();

        let decoder = DecodeConfig::new();
        let decoded = decoder.decode(&result, Unstoppable).unwrap();

        // 64×48 rotated 90° → 48×64
        assert_eq!(decoded.width(), 48);
        assert_eq!(decoded.height(), 64);
    }

    #[test]
    fn test_rotate180_same_dimensions() {
        let jpeg = create_test_jpeg(64, 48);

        let result = transform(
            &jpeg,
            &TransformConfig {
                transform: LosslessTransform::Rotate180,
                edge_handling: EdgeHandling::TrimPartialBlocks,
            },
            Unstoppable,
        )
        .unwrap();

        let decoder = DecodeConfig::new();
        let decoded = decoder.decode(&result, Unstoppable).unwrap();

        assert_eq!(decoded.width(), 64);
        assert_eq!(decoded.height(), 48);
    }

    #[test]
    fn test_double_rotate90_equals_rotate180() {
        use archmage::testing::{CompileTimePolicy, for_each_token_permutation};

        let jpeg = create_test_jpeg(64, 64);

        let _ = for_each_token_permutation(CompileTimePolicy::Warn, |perm| {
            let rot90_1 = transform(
                &jpeg,
                &TransformConfig {
                    transform: LosslessTransform::Rotate90,
                    edge_handling: EdgeHandling::TrimPartialBlocks,
                },
                Unstoppable,
            )
            .unwrap();

            let rot90_2 = transform(
                &rot90_1,
                &TransformConfig {
                    transform: LosslessTransform::Rotate90,
                    edge_handling: EdgeHandling::TrimPartialBlocks,
                },
                Unstoppable,
            )
            .unwrap();

            let rot180 = transform(
                &jpeg,
                &TransformConfig {
                    transform: LosslessTransform::Rotate180,
                    edge_handling: EdgeHandling::TrimPartialBlocks,
                },
                Unstoppable,
            )
            .unwrap();

            // Both should decode to the same pixels
            let decoder = DecodeConfig::new();
            let px_2x90 = decoder.decode(&rot90_2, Unstoppable).unwrap();
            let px_180 = decoder.decode(&rot180, Unstoppable).unwrap();

            let px_a = px_2x90.pixels_u8().unwrap();
            let px_b = px_180.pixels_u8().unwrap();
            assert_eq!(px_a.len(), px_b.len());

            let mut max_diff = 0u8;
            for (a, b) in px_a.iter().zip(px_b.iter()) {
                let diff = (*a as i16 - *b as i16).unsigned_abs() as u8;
                max_diff = max_diff.max(diff);
            }
            assert_eq!(max_diff, 0, "2×rot90 should equal rot180 at {perm}");
        });
    }

    #[test]
    fn test_four_rotations_roundtrip() {
        let jpeg = create_test_jpeg(64, 64);

        let mut current = jpeg.clone();
        for _ in 0..4 {
            current = transform(
                &current,
                &TransformConfig {
                    transform: LosslessTransform::Rotate90,
                    edge_handling: EdgeHandling::TrimPartialBlocks,
                },
                Unstoppable,
            )
            .unwrap();
        }

        // After 4 rotations, should be back to original pixels
        let decoder = DecodeConfig::new();
        let orig = decoder.decode(&jpeg, Unstoppable).unwrap();
        let roundtrip = decoder.decode(&current, Unstoppable).unwrap();

        assert_eq!(orig.width(), roundtrip.width());
        assert_eq!(orig.height(), roundtrip.height());

        let px_orig = orig.pixels_u8().unwrap();
        let px_rt = roundtrip.pixels_u8().unwrap();

        let mut max_diff = 0u8;
        for (a, b) in px_orig.iter().zip(px_rt.iter()) {
            let diff = (*a as i16 - *b as i16).unsigned_abs() as u8;
            max_diff = max_diff.max(diff);
        }
        assert_eq!(max_diff, 0, "4×rot90 should equal identity");
    }

    #[test]
    fn test_hflip_roundtrip() {
        let jpeg = create_test_jpeg(64, 48);

        let flipped = transform(
            &jpeg,
            &TransformConfig {
                transform: LosslessTransform::FlipHorizontal,
                edge_handling: EdgeHandling::TrimPartialBlocks,
            },
            Unstoppable,
        )
        .unwrap();

        let double_flipped = transform(
            &flipped,
            &TransformConfig {
                transform: LosslessTransform::FlipHorizontal,
                edge_handling: EdgeHandling::TrimPartialBlocks,
            },
            Unstoppable,
        )
        .unwrap();

        let decoder = DecodeConfig::new();
        let orig = decoder.decode(&jpeg, Unstoppable).unwrap();
        let roundtrip = decoder.decode(&double_flipped, Unstoppable).unwrap();

        let px_orig = orig.pixels_u8().unwrap();
        let px_rt = roundtrip.pixels_u8().unwrap();

        let mut max_diff = 0u8;
        for (a, b) in px_orig.iter().zip(px_rt.iter()) {
            let diff = (*a as i16 - *b as i16).unsigned_abs() as u8;
            max_diff = max_diff.max(diff);
        }
        assert_eq!(max_diff, 0, "2×hflip should equal identity");
    }

    #[test]
    fn test_420_rotate90() {
        // Test with 4:2:0 subsampling (MCU size 16×16)
        let jpeg = create_test_jpeg_420(64, 48);

        let result = transform(
            &jpeg,
            &TransformConfig {
                transform: LosslessTransform::Rotate90,
                edge_handling: EdgeHandling::TrimPartialBlocks,
            },
            Unstoppable,
        )
        .unwrap();

        let decoder = DecodeConfig::new();
        let decoded = decoder.decode(&result, Unstoppable).unwrap();

        // Should produce a valid, decodable JPEG with swapped dimensions
        assert_eq!(decoded.width(), 48);
        assert_eq!(decoded.height(), 64);
    }

    #[test]
    fn test_all_transforms_produce_valid_jpeg() {
        let jpeg = create_test_jpeg(64, 64);
        let decoder = DecodeConfig::new();

        for xform in [
            LosslessTransform::None,
            LosslessTransform::FlipHorizontal,
            LosslessTransform::FlipVertical,
            LosslessTransform::Transpose,
            LosslessTransform::Rotate90,
            LosslessTransform::Rotate180,
            LosslessTransform::Rotate270,
            LosslessTransform::Transverse,
        ] {
            let result = transform(
                &jpeg,
                &TransformConfig {
                    transform: xform,
                    edge_handling: EdgeHandling::TrimPartialBlocks,
                },
                Unstoppable,
            )
            .unwrap();

            // Should be a valid JPEG
            let decoded = decoder.decode(&result, Unstoppable);
            assert!(
                decoded.is_ok(),
                "transform {:?} should produce valid JPEG, got error: {:?}",
                xform,
                decoded.err()
            );
        }
    }
}

mod coefficient_roundtrip_tests {
    use crate::decode::DecodeConfig;
    use crate::lossless::{EdgeHandling, LosslessTransform, TransformConfig, transform};
    use enough::Unstoppable;

    /// Create a test JPEG
    fn create_test_jpeg(width: u32, height: u32) -> Vec<u8> {
        use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
        let mut pixels = Vec::with_capacity((width * height * 3) as usize);
        for y in 0..height {
            for x in 0..width {
                pixels.push(((x * 255 / width) & 0xFF) as u8);
                pixels.push(((y * 255 / height) & 0xFF) as u8);
                pixels.push(128u8);
            }
        }
        let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
        let mut enc = config
            .encode_from_bytes(width, height, PixelLayout::Rgb8Srgb)
            .unwrap();
        enc.push_packed(&pixels, Unstoppable).unwrap();
        enc.finish().unwrap()
    }

    #[test]
    fn test_coefficient_identity_roundtrip() {
        // Compare coefficients before and after identity transform
        let jpeg = create_test_jpeg(64, 64);
        let decoder = DecodeConfig::new();

        let orig_coeffs = decoder.decode_coefficients(&jpeg, Unstoppable).unwrap();

        let result = transform(
            &jpeg,
            &TransformConfig {
                transform: LosslessTransform::None,
                edge_handling: EdgeHandling::TrimPartialBlocks,
            },
            Unstoppable,
        )
        .unwrap();

        let new_coeffs = decoder.decode_coefficients(&result, Unstoppable).unwrap();

        // Compare coefficient by coefficient
        for (comp_idx, (c1, c2)) in orig_coeffs
            .components
            .iter()
            .zip(&new_coeffs.components)
            .enumerate()
        {
            assert_eq!(
                c1.blocks_wide, c2.blocks_wide,
                "blocks_wide mismatch comp {comp_idx}"
            );
            assert_eq!(
                c1.blocks_high, c2.blocks_high,
                "blocks_high mismatch comp {comp_idx}"
            );
            assert_eq!(c1.h_samp, c2.h_samp, "h_samp mismatch comp {comp_idx}");
            assert_eq!(c1.v_samp, c2.v_samp, "v_samp mismatch comp {comp_idx}");

            let num_blocks = c1.num_blocks().min(c2.num_blocks());
            let mut diffs = 0;
            let mut max_diff = 0i16;
            for block_idx in 0..num_blocks {
                let b1 = c1.block(block_idx);
                let b2 = c2.block(block_idx);
                for i in 0..64 {
                    let d = (b1[i] as i32 - b2[i] as i32).abs() as i16;
                    if d != 0 {
                        diffs += 1;
                        max_diff = max_diff.max(d);
                    }
                }
            }
            if diffs > 0 {
                eprintln!(
                    "Component {comp_idx}: {diffs} differing coefficients, max_diff={max_diff}"
                );
            }
            assert_eq!(
                diffs, 0,
                "Component {comp_idx}: coefficients should be identical after identity transform"
            );
        }
    }

    #[test]
    fn test_quant_tables_preserved() {
        let jpeg = create_test_jpeg(64, 64);
        let decoder = DecodeConfig::new();

        let orig_coeffs = decoder.decode_coefficients(&jpeg, Unstoppable).unwrap();

        let result = transform(
            &jpeg,
            &TransformConfig {
                transform: LosslessTransform::None,
                edge_handling: EdgeHandling::TrimPartialBlocks,
            },
            Unstoppable,
        )
        .unwrap();

        let new_coeffs = decoder.decode_coefficients(&result, Unstoppable).unwrap();

        // Quant tables should be preserved
        for (idx, (qt1, qt2)) in orig_coeffs
            .quant_tables
            .iter()
            .zip(&new_coeffs.quant_tables)
            .enumerate()
        {
            match (qt1, qt2) {
                (Some(t1), Some(t2)) => {
                    assert_eq!(t1, t2, "quant table {idx} should be preserved");
                }
                (None, None) => {}
                _ => panic!("quant table {idx} presence mismatch"),
            }
        }
    }
}

#[cfg(test)]
mod debug_tests {
    use crate::decode::DecodeConfig;
    use enough::Unstoppable;

    fn create_test_jpeg_444(width: u32, height: u32) -> Vec<u8> {
        use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
        let pixels = vec![128u8; (width * height * 3) as usize];
        let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
        let mut enc = config
            .encode_from_bytes(width, height, PixelLayout::Rgb8Srgb)
            .unwrap();
        enc.push_packed(&pixels, Unstoppable).unwrap();
        enc.finish().unwrap()
    }

    #[test]
    fn debug_quant_table_indices() {
        let jpeg = create_test_jpeg_444(64, 64);
        let decoder = DecodeConfig::new();
        let coeffs = decoder.decode_coefficients(&jpeg, Unstoppable).unwrap();

        eprintln!("Number of quant table slots: {}", coeffs.quant_tables.len());
        for (i, qt) in coeffs.quant_tables.iter().enumerate() {
            eprintln!(
                "  Table {}: {}",
                i,
                if qt.is_some() { "present" } else { "absent" }
            );
        }

        eprintln!("Number of components: {}", coeffs.components.len());
        for (i, comp) in coeffs.components.iter().enumerate() {
            eprintln!(
                "  Component {}: id={}, h_samp={}, v_samp={}, blocks={}x{}",
                i, comp.id, comp.h_samp, comp.v_samp, comp.blocks_wide, comp.blocks_high
            );
        }
    }
}

mod exif_tests {
    use crate::lossless::coeff_transform::LosslessTransform;
    use crate::lossless::exif::{parse_exif_orientation, set_exif_orientation};

    /// Build minimal EXIF APP1 data with an orientation tag.
    /// Uses little-endian ("II") byte order.
    fn build_exif_with_orientation_le(orientation: u16) -> Vec<u8> {
        let mut data = Vec::new();

        // Exif\0\0 prefix
        data.extend_from_slice(b"Exif\0\0");

        // TIFF header: little-endian
        data.extend_from_slice(b"II");
        data.extend_from_slice(&42u16.to_le_bytes()); // magic
        data.extend_from_slice(&8u32.to_le_bytes()); // IFD0 offset (right after header)

        // IFD0: 1 entry
        data.extend_from_slice(&1u16.to_le_bytes());

        // Entry: Orientation (tag 0x0112, type SHORT, count 1, value inline)
        data.extend_from_slice(&0x0112u16.to_le_bytes()); // tag
        data.extend_from_slice(&3u16.to_le_bytes()); // type = SHORT
        data.extend_from_slice(&1u32.to_le_bytes()); // count
        data.extend_from_slice(&(orientation as u32).to_le_bytes()); // value

        // Next IFD offset = 0
        data.extend_from_slice(&0u32.to_le_bytes());

        data
    }

    /// Build minimal EXIF APP1 data with an orientation tag.
    /// Uses big-endian ("MM") byte order.
    fn build_exif_with_orientation_be(orientation: u16) -> Vec<u8> {
        let mut data = Vec::new();

        // Exif\0\0 prefix
        data.extend_from_slice(b"Exif\0\0");

        // TIFF header: big-endian
        data.extend_from_slice(b"MM");
        data.extend_from_slice(&42u16.to_be_bytes());
        data.extend_from_slice(&8u32.to_be_bytes());

        // IFD0: 1 entry
        data.extend_from_slice(&1u16.to_be_bytes());

        // Entry: Orientation
        data.extend_from_slice(&0x0112u16.to_be_bytes());
        data.extend_from_slice(&3u16.to_be_bytes());
        data.extend_from_slice(&1u32.to_be_bytes());
        // For big-endian SHORT, value is in first 2 bytes of the 4-byte field
        data.extend_from_slice(&orientation.to_be_bytes());
        data.extend_from_slice(&[0u8; 2]); // padding
        // Next IFD offset = 0
        data.extend_from_slice(&0u32.to_be_bytes());

        data
    }

    #[test]
    fn test_parse_exif_orientation_le() {
        for orient in 1..=8u16 {
            let data = build_exif_with_orientation_le(orient);
            assert_eq!(
                parse_exif_orientation(&data),
                Some(orient as u8),
                "failed to parse LE orientation {orient}"
            );
        }
    }

    #[test]
    fn test_parse_exif_orientation_be() {
        for orient in 1..=8u16 {
            let data = build_exif_with_orientation_be(orient);
            assert_eq!(
                parse_exif_orientation(&data),
                Some(orient as u8),
                "failed to parse BE orientation {orient}"
            );
        }
    }

    #[test]
    fn test_parse_exif_orientation_no_prefix() {
        // Missing Exif\0\0 prefix
        assert_eq!(parse_exif_orientation(b"not exif data"), None);
    }

    #[test]
    fn test_parse_exif_orientation_too_short() {
        assert_eq!(parse_exif_orientation(b"Exif\0\0II"), None);
    }

    #[test]
    fn test_parse_exif_orientation_invalid_value() {
        // Orientation = 0 (invalid)
        let data = build_exif_with_orientation_le(0);
        assert_eq!(parse_exif_orientation(&data), None);

        // Orientation = 9 (invalid)
        let data = build_exif_with_orientation_le(9);
        assert_eq!(parse_exif_orientation(&data), None);
    }

    #[test]
    fn test_set_exif_orientation_le() {
        let mut data = build_exif_with_orientation_le(6);
        assert_eq!(parse_exif_orientation(&data), Some(6));

        let modified = set_exif_orientation(&mut data, 1);
        assert!(modified, "should find and modify orientation tag");
        assert_eq!(parse_exif_orientation(&data), Some(1));
    }

    #[test]
    fn test_set_exif_orientation_be() {
        let mut data = build_exif_with_orientation_be(8);
        assert_eq!(parse_exif_orientation(&data), Some(8));

        let modified = set_exif_orientation(&mut data, 1);
        assert!(modified);
        assert_eq!(parse_exif_orientation(&data), Some(1));
    }

    #[test]
    fn test_set_exif_orientation_no_tag() {
        // EXIF with no orientation tag — set should return false
        let mut data = Vec::new();
        data.extend_from_slice(b"Exif\0\0");
        data.extend_from_slice(b"II");
        data.extend_from_slice(&42u16.to_le_bytes());
        data.extend_from_slice(&8u32.to_le_bytes());
        // IFD0: 1 entry — but a different tag (Copyright 0x8298)
        data.extend_from_slice(&1u16.to_le_bytes());
        data.extend_from_slice(&0x8298u16.to_le_bytes()); // tag
        data.extend_from_slice(&2u16.to_le_bytes()); // type = ASCII
        data.extend_from_slice(&1u32.to_le_bytes()); // count
        data.extend_from_slice(&0u32.to_le_bytes()); // value
        data.extend_from_slice(&0u32.to_le_bytes()); // next IFD

        let modified = set_exif_orientation(&mut data, 1);
        assert!(!modified, "should not modify when orientation tag absent");
    }

    #[test]
    fn test_from_exif_orientation() {
        // Exhaustive mapping check
        assert_eq!(
            LosslessTransform::from_exif_orientation(1),
            Some(LosslessTransform::None)
        );
        assert_eq!(
            LosslessTransform::from_exif_orientation(2),
            Some(LosslessTransform::FlipHorizontal)
        );
        assert_eq!(
            LosslessTransform::from_exif_orientation(3),
            Some(LosslessTransform::Rotate180)
        );
        assert_eq!(
            LosslessTransform::from_exif_orientation(4),
            Some(LosslessTransform::FlipVertical)
        );
        assert_eq!(
            LosslessTransform::from_exif_orientation(5),
            Some(LosslessTransform::Transpose)
        );
        assert_eq!(
            LosslessTransform::from_exif_orientation(6),
            Some(LosslessTransform::Rotate90)
        );
        assert_eq!(
            LosslessTransform::from_exif_orientation(7),
            Some(LosslessTransform::Transverse)
        );
        assert_eq!(
            LosslessTransform::from_exif_orientation(8),
            Some(LosslessTransform::Rotate270)
        );

        // Invalid values
        assert_eq!(LosslessTransform::from_exif_orientation(0), None);
        assert_eq!(LosslessTransform::from_exif_orientation(9), None);
    }

    #[test]
    fn test_apply_exif_orientation_no_exif() {
        // A JPEG without EXIF should be returned unchanged
        use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
        use crate::lossless::apply_exif_orientation;
        use enough::Unstoppable;

        let pixels = vec![128u8; 64 * 64 * 3];
        let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
        let mut enc = config
            .encode_from_bytes(64, 64, PixelLayout::Rgb8Srgb)
            .unwrap();
        enc.push_packed(&pixels, Unstoppable).unwrap();
        let jpeg = enc.finish().unwrap();

        let result = apply_exif_orientation(&jpeg, Unstoppable).unwrap();
        assert_eq!(result, jpeg, "no-EXIF JPEG should be returned unchanged");
    }

    #[test]
    fn test_apply_exif_orientation_normal() {
        // Orientation=1 should be fast path (returned unchanged)
        use crate::encoder::{ChromaSubsampling, EncoderConfig, Exif, Orientation, PixelLayout};
        use crate::lossless::apply_exif_orientation;
        use enough::Unstoppable;

        let pixels = vec![128u8; 64 * 64 * 3];
        let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
        let mut enc = config
            .request()
            .exif(Exif::build().orientation(Orientation::Normal))
            .encode_from_bytes(64, 64, PixelLayout::Rgb8Srgb)
            .unwrap();
        enc.push_packed(&pixels, Unstoppable).unwrap();
        let jpeg = enc.finish().unwrap();

        let result = apply_exif_orientation(&jpeg, Unstoppable).unwrap();
        assert_eq!(result, jpeg, "orientation=1 should be returned unchanged");
    }

    #[test]
    fn test_apply_exif_orientation_rotate90() {
        // Create a 64x48 JPEG with orientation=6 (Rotate 90 CW)
        // After apply_exif_orientation, dimensions should be 48x64 and orientation=1
        use crate::decode::{DecodeConfig, PreserveConfig};
        use crate::encoder::{ChromaSubsampling, EncoderConfig, Exif, Orientation, PixelLayout};
        use crate::lossless::apply_exif_orientation;
        use enough::Unstoppable;

        let (w, h) = (64u32, 48u32);
        let mut pixels = Vec::with_capacity((w * h * 3) as usize);
        for y in 0..h {
            for x in 0..w {
                pixels.push(((x * 255 / w) & 0xFF) as u8);
                pixels.push(((y * 255 / h) & 0xFF) as u8);
                pixels.push(128u8);
            }
        }

        let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
        let mut enc = config
            .request()
            .exif(Exif::build().orientation(Orientation::Rotate90))
            .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
            .unwrap();
        enc.push_packed(&pixels, Unstoppable).unwrap();
        let jpeg = enc.finish().unwrap();

        // Verify the source has orientation=6
        let decoder = DecodeConfig::new().preserve(PreserveConfig::all());
        let src_result = decoder.decode(&jpeg, Unstoppable).unwrap();
        let src_exif = src_result.extras().unwrap().exif().unwrap();
        assert_eq!(parse_exif_orientation(src_exif), Some(6));

        // Apply orientation
        let corrected = apply_exif_orientation(&jpeg, Unstoppable).unwrap();
        assert_ne!(corrected, jpeg, "rotated JPEG should differ from source");

        // Verify output dimensions swapped (rotate90: 64x48 → 48x64)
        let out_result = decoder.decode(&corrected, Unstoppable).unwrap();
        assert_eq!(out_result.width(), h, "width should be original height");
        assert_eq!(out_result.height(), w, "height should be original width");

        // Verify output EXIF orientation is 1
        let out_exif = out_result.extras().unwrap().exif().unwrap();
        assert_eq!(
            parse_exif_orientation(out_exif),
            Some(1),
            "output orientation should be reset to 1"
        );
    }
}

// ==========================================================================
// Transform composition (then/inverse) tests
// ==========================================================================

#[test]
fn test_then_identity() {
    // a.then(None) == a, None.then(a) == a
    for &t in &LosslessTransform::ALL {
        assert_eq!(
            t.then(LosslessTransform::None),
            t,
            "{t:?}.then(None) should be {t:?}"
        );
        assert_eq!(
            LosslessTransform::None.then(t),
            t,
            "None.then({t:?}) should be {t:?}"
        );
    }
}

#[test]
fn test_inverse_roundtrip() {
    // t.then(t.inverse()) == None for all t
    for &t in &LosslessTransform::ALL {
        let composed = t.then(t.inverse());
        assert_eq!(
            composed,
            LosslessTransform::None,
            "{t:?}.then({:?}) should be None, got {composed:?}",
            t.inverse()
        );
    }
}

#[test]
fn test_inverse_both_directions() {
    // t.inverse().then(t) == None for all t
    for &t in &LosslessTransform::ALL {
        let composed = t.inverse().then(t);
        assert_eq!(
            composed,
            LosslessTransform::None,
            "{:?}.then({t:?}) should be None, got {composed:?}",
            t.inverse()
        );
    }
}

#[test]
fn test_then_cayley_table_by_block_transform() {
    // Verify all 64 compositions by applying both transforms sequentially
    // to a test block and comparing with the composed single transform.
    let src = make_test_block();

    for &a in &LosslessTransform::ALL {
        let bt_a = BlockTransform::for_transform(a);
        for &b in &LosslessTransform::ALL {
            let bt_b = BlockTransform::for_transform(b);

            // Sequential: apply a then b
            let after_a = bt_a.apply(&src);
            let sequential = bt_b.apply(&after_a);

            // Composed: apply a.then(b) in one step
            let composed = a.then(b);
            let bt_composed = BlockTransform::for_transform(composed);
            let single = bt_composed.apply(&src);

            assert_eq!(
                sequential, single,
                "{a:?}.then({b:?}) = {composed:?} — block mismatch"
            );
        }
    }
}

#[test]
fn test_then_known_compositions() {
    // Verify specific known compositions
    use LosslessTransform::*;

    // Rotate90 = Transpose then FlipHorizontal
    assert_eq!(Transpose.then(FlipHorizontal), Rotate90);

    // Rotate270 = Transpose then FlipVertical
    assert_eq!(Transpose.then(FlipVertical), Rotate270);

    // Rotate180 = FlipHorizontal then FlipVertical
    assert_eq!(FlipHorizontal.then(FlipVertical), Rotate180);

    // Rotate90 then Rotate90 = Rotate180
    assert_eq!(Rotate90.then(Rotate90), Rotate180);

    // Rotate90 then Rotate180 = Rotate270
    assert_eq!(Rotate90.then(Rotate180), Rotate270);

    // Rotate90 then Rotate270 = None (full rotation)
    assert_eq!(Rotate90.then(Rotate270), None);
}

#[test]
fn test_inverse_values() {
    use LosslessTransform::*;
    assert_eq!(None.inverse(), None);
    assert_eq!(FlipHorizontal.inverse(), FlipHorizontal);
    assert_eq!(FlipVertical.inverse(), FlipVertical);
    assert_eq!(Transpose.inverse(), Transpose);
    assert_eq!(Rotate90.inverse(), Rotate270);
    assert_eq!(Rotate180.inverse(), Rotate180);
    assert_eq!(Rotate270.inverse(), Rotate90);
    assert_eq!(Transverse.inverse(), Transverse);
}

// ==========================================================================
// Decode-time transform tests
// ==========================================================================

#[test]
fn test_decode_auto_orient() {
    // Create a 64x48 JPEG with orientation=6 (Rotate 90 CW)
    // Decode with auto_orient(true) — output should be 48x64
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, Exif, Orientation, PixelLayout};
    use enough::Unstoppable;

    let (w, h) = (64u32, 48u32);
    let mut pixels = Vec::with_capacity((w * h * 3) as usize);
    for y in 0..h {
        for x in 0..w {
            pixels.push(((x * 255 / w) & 0xFF) as u8);
            pixels.push(((y * 255 / h) & 0xFF) as u8);
            pixels.push(128u8);
        }
    }

    let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
    let mut enc = config
        .request()
        .exif(Exif::build().orientation(Orientation::Rotate90))
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    let result = DecodeConfig::new()
        .auto_orient(true)
        .decode(&jpeg, Unstoppable)
        .unwrap();

    // Orientation=6 means Rotate90 CW, which swaps dimensions
    assert_eq!(
        result.width(),
        h,
        "width should be original height after auto_orient"
    );
    assert_eq!(
        result.height(),
        w,
        "height should be original width after auto_orient"
    );
}

#[test]
fn test_decode_transform_rotate90() {
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
    use enough::Unstoppable;

    let (w, h) = (64u32, 48u32);
    let pixels = vec![128u8; (w * h * 3) as usize];

    let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
    let mut enc = config
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    let result = DecodeConfig::new()
        .transform(LosslessTransform::Rotate90)
        .decode(&jpeg, Unstoppable)
        .unwrap();

    assert_eq!(result.width(), h, "Rotate90 should swap dimensions");
    assert_eq!(result.height(), w, "Rotate90 should swap dimensions");
}

#[test]
fn test_decode_transform_rotate180_preserves_dimensions() {
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
    use enough::Unstoppable;

    let (w, h) = (64u32, 48u32);
    let pixels = vec![128u8; (w * h * 3) as usize];

    let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
    let mut enc = config
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    let result = DecodeConfig::new()
        .transform(LosslessTransform::Rotate180)
        .decode(&jpeg, Unstoppable)
        .unwrap();

    assert_eq!(result.width(), w, "Rotate180 should preserve width");
    assert_eq!(result.height(), h, "Rotate180 should preserve height");
}

#[test]
fn test_decode_composed_exif_plus_transform() {
    // EXIF orientation=6 (Rotate90) + transform(Rotate270) = identity
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, Exif, Orientation, PixelLayout};
    use enough::Unstoppable;

    let (w, h) = (64u32, 48u32);
    let mut pixels = Vec::with_capacity((w * h * 3) as usize);
    for y in 0..h {
        for x in 0..w {
            pixels.push(((x * 255 / w) & 0xFF) as u8);
            pixels.push(((y * 255 / h) & 0xFF) as u8);
            pixels.push(128u8);
        }
    }

    let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
    let mut enc = config
        .request()
        .exif(Exif::build().orientation(Orientation::Rotate90))
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    // EXIF=Rotate90, then user Rotate270 → identity
    let composed = DecodeConfig::new()
        .auto_orient(true)
        .transform(LosslessTransform::Rotate270)
        .decode(&jpeg, Unstoppable)
        .unwrap();

    // Rotate90.then(Rotate270) = None → dimensions unchanged from original
    assert_eq!(composed.width(), w, "composed transform should be identity");
    assert_eq!(
        composed.height(),
        h,
        "composed transform should be identity"
    );

    // Compare with plain decode (no transforms, no auto-orient to get raw pixels)
    let plain = DecodeConfig::new()
        .auto_orient(false)
        .decode(&jpeg, Unstoppable)
        .unwrap();

    // Both should produce identical pixel data
    assert_eq!(
        composed.pixels_u8().unwrap(),
        plain.pixels_u8().unwrap(),
        "composed identity transform should produce same pixels as plain decode"
    );
}

#[test]
fn test_decode_auto_orient_noop() {
    use archmage::testing::{CompileTimePolicy, for_each_token_permutation};
    // Orientation=1 should produce same result as no auto_orient
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, Exif, Orientation, PixelLayout};
    use enough::Unstoppable;

    let (w, h) = (64u32, 64u32);
    let pixels = vec![128u8; (w * h * 3) as usize];

    let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
    let mut enc = config
        .request()
        .exif(Exif::build().orientation(Orientation::Normal))
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    let _ = for_each_token_permutation(CompileTimePolicy::Warn, |perm| {
        let with_orient = DecodeConfig::new()
            .auto_orient(true)
            .decode(&jpeg, Unstoppable)
            .unwrap();

        let without_orient = DecodeConfig::new().decode(&jpeg, Unstoppable).unwrap();

        assert_eq!(with_orient.width(), without_orient.width());
        assert_eq!(with_orient.height(), without_orient.height());
        assert_eq!(
            with_orient.pixels_u8().unwrap(),
            without_orient.pixels_u8().unwrap(),
            "orientation=1 + auto_orient should produce identical pixels at {perm}"
        );
    });
}

#[test]
fn test_decode_no_exif_auto_orient() {
    use archmage::testing::{CompileTimePolicy, for_each_token_permutation};
    // No EXIF at all — auto_orient should be a no-op
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
    use enough::Unstoppable;

    let (w, h) = (64u32, 64u32);
    let pixels = vec![128u8; (w * h * 3) as usize];

    let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
    let mut enc = config
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    let _ = for_each_token_permutation(CompileTimePolicy::Warn, |perm| {
        let with_orient = DecodeConfig::new()
            .auto_orient(true)
            .decode(&jpeg, Unstoppable)
            .unwrap();

        let without_orient = DecodeConfig::new().decode(&jpeg, Unstoppable).unwrap();

        assert_eq!(with_orient.width(), without_orient.width());
        assert_eq!(with_orient.height(), without_orient.height());
        assert_eq!(
            with_orient.pixels_u8().unwrap(),
            without_orient.pixels_u8().unwrap(),
            "no EXIF + auto_orient should produce identical pixels at {perm}"
        );
    });
}

#[test]
fn test_scanline_transform_matches_decode() {
    // Scanline reader with transform should produce same pixels as decode()
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
    use enough::Unstoppable;
    use imgref::ImgRefMut;

    let (w, h) = (64u32, 48u32);
    let mut pixels = Vec::with_capacity((w * h * 3) as usize);
    for y in 0..h {
        for x in 0..w {
            pixels.push(((x * 255 / w) & 0xFF) as u8);
            pixels.push(((y * 255 / h) & 0xFF) as u8);
            pixels.push(128u8);
        }
    }

    let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
    let mut enc = config
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    let transform = LosslessTransform::Rotate90;

    // Full decode with transform
    let decode_result = DecodeConfig::new()
        .transform(transform)
        .decode(&jpeg, Unstoppable)
        .unwrap();

    let out_w = decode_result.width() as usize;
    let out_h = decode_result.height() as usize;

    // Scanline reader with same transform
    let mut reader = DecodeConfig::new()
        .transform(transform)
        .scanline_reader(&jpeg)
        .unwrap();

    assert_eq!(reader.width() as usize, out_w);
    assert_eq!(reader.height() as usize, out_h);

    let mut scanline_pixels = vec![0u8; out_w * out_h * 3];
    let mut rows_read = 0;
    while rows_read < out_h {
        let remaining = out_h - rows_read;
        let output = ImgRefMut::new(
            &mut scanline_pixels[rows_read * out_w * 3..],
            out_w * 3,
            remaining,
        );
        let count = reader.read_rows_rgb8(output).unwrap();
        assert!(count > 0, "read_rows_rgb8 should make progress");
        rows_read += count;
    }

    assert_eq!(
        scanline_pixels,
        decode_result.pixels_u8().unwrap(),
        "scanline reader with transform should produce same pixels as decode()"
    );
}

#[test]
fn test_scanline_auto_orient() {
    // Scanline reader with auto_orient should produce correctly-oriented output
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, Exif, Orientation, PixelLayout};
    use enough::Unstoppable;
    use imgref::ImgRefMut;

    let (w, h) = (64u32, 48u32);
    let mut pixels = Vec::with_capacity((w * h * 3) as usize);
    for y in 0..h {
        for x in 0..w {
            pixels.push(((x * 255 / w) & 0xFF) as u8);
            pixels.push(((y * 255 / h) & 0xFF) as u8);
            pixels.push(128u8);
        }
    }

    let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
    let mut enc = config
        .request()
        .exif(Exif::build().orientation(Orientation::Rotate90))
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    let mut reader = DecodeConfig::new()
        .auto_orient(true)
        .scanline_reader(&jpeg)
        .unwrap();

    // Rotate90 swaps dimensions: 64x48 → 48x64
    assert_eq!(reader.width(), h, "width should be original height");
    assert_eq!(reader.height(), w, "height should be original width");

    let out_w = reader.width() as usize;
    let out_h = reader.height() as usize;
    let mut scanline_pixels = vec![0u8; out_w * out_h * 3];
    let mut rows_read = 0;
    while rows_read < out_h {
        let remaining = out_h - rows_read;
        let output = ImgRefMut::new(
            &mut scanline_pixels[rows_read * out_w * 3..],
            out_w * 3,
            remaining,
        );
        let count = reader.read_rows_rgb8(output).unwrap();
        assert!(count > 0, "read_rows_rgb8 should make progress");
        rows_read += count;
    }

    // Compare with full decode
    let decode_result = DecodeConfig::new()
        .auto_orient(true)
        .decode(&jpeg, Unstoppable)
        .unwrap();

    assert_eq!(
        scanline_pixels,
        decode_result.pixels_u8().unwrap(),
        "scanline auto_orient should match decode auto_orient"
    );
}

// ==========================================================================
// Synthetic pixel-position verification tests
// ==========================================================================
//
// These tests use tiny asymmetric images with known pixel patterns to verify
// that transforms move pixels to the correct positions, not just that
// dimensions change. Each corner gets a unique color so we can track where
// pixels end up.

/// Encode a tiny image and return JPEG bytes.
/// Uses Q97 4:4:4 to minimize compression artifacts on solid blocks.
fn encode_test_image(
    w: u32,
    h: u32,
    pixels: &[u8],
    orientation: Option<crate::encoder::Orientation>,
) -> Vec<u8> {
    use crate::encoder::{ChromaSubsampling, EncoderConfig, Exif, PixelLayout};
    use enough::Unstoppable;

    let config = EncoderConfig::ycbcr(97, ChromaSubsampling::None);
    let req = config.request();
    let req = if let Some(orient) = orientation {
        req.exif(Exif::build().orientation(orient))
    } else {
        req
    };
    let mut enc = req.encode_from_bytes(w, h, PixelLayout::Rgb8Srgb).unwrap();
    enc.push_packed(pixels, Unstoppable).unwrap();
    enc.finish().unwrap()
}

/// Decode with given config, return (width, height, pixels_rgb8).
fn decode_test(jpeg: &[u8], config: &crate::decode::DecodeConfig) -> (u32, u32, Vec<u8>) {
    use enough::Unstoppable;
    let result = config.decode(jpeg, Unstoppable).unwrap();
    let w = result.width();
    let h = result.height();
    let pixels = result.into_pixels_u8().unwrap();
    (w, h, pixels)
}

/// Decode via scanline reader with given config, return (width, height, pixels_rgb8).
fn scanline_decode_test(jpeg: &[u8], config: &crate::decode::DecodeConfig) -> (u32, u32, Vec<u8>) {
    use imgref::ImgRefMut;
    let mut reader = config.scanline_reader(jpeg).unwrap();
    let w = reader.width() as usize;
    let h = reader.height() as usize;
    let mut pixels = vec![0u8; w * h * 3];
    let mut rows_read = 0;
    while rows_read < h {
        let remaining = h - rows_read;
        let output = ImgRefMut::new(&mut pixels[rows_read * w * 3..], w * 3, remaining);
        let count = reader.read_rows_rgb8(output).unwrap();
        assert!(count > 0);
        rows_read += count;
    }
    (w as u32, h as u32, pixels)
}

/// Get the average RGB of a block of pixels at (bx*8, by*8).
/// Uses 4x4 center of the 8x8 block to avoid edge effects.
fn block_avg(pixels: &[u8], stride_w: usize, bx: usize, by: usize) -> (u8, u8, u8) {
    let mut r_sum = 0u32;
    let mut g_sum = 0u32;
    let mut b_sum = 0u32;
    let count = 16u32; // 4x4 center
    for dy in 2..6 {
        for dx in 2..6 {
            let px = bx * 8 + dx;
            let py = by * 8 + dy;
            let idx = (py * stride_w + px) * 3;
            r_sum += pixels[idx] as u32;
            g_sum += pixels[idx + 1] as u32;
            b_sum += pixels[idx + 2] as u32;
        }
    }
    (
        (r_sum / count) as u8,
        (g_sum / count) as u8,
        (b_sum / count) as u8,
    )
}

/// Check that a block's average color is close to expected (within tolerance).
fn assert_block_near(
    pixels: &[u8],
    stride_w: usize,
    bx: usize,
    by: usize,
    expected: (u8, u8, u8),
    tolerance: u8,
    label: &str,
) {
    let actual = block_avg(pixels, stride_w, bx, by);
    let dr = actual.0.abs_diff(expected.0);
    let dg = actual.1.abs_diff(expected.1);
    let db = actual.2.abs_diff(expected.2);
    assert!(
        dr <= tolerance && dg <= tolerance && db <= tolerance,
        "{label}: block({bx},{by}) expected ~{expected:?}, got {actual:?} (delta {dr},{dg},{db})"
    );
}

/// Create a 16x16 image with 4 colored quadrants (2x2 blocks).
///
/// Block layout (each block is 8x8 pixels):
/// ```text
///   (0,0) RED     (1,0) GREEN
///   (0,1) BLUE    (1,1) YELLOW
/// ```
///
/// Returns (width=16, height=16, pixels).
fn make_quadrant_image() -> (u32, u32, Vec<u8>) {
    let (w, h) = (16u32, 16u32);
    let mut pixels = vec![0u8; (w * h * 3) as usize];
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            let (r, g, b) = match (x >= 8, y >= 8) {
                (false, false) => (220, 30, 30), // top-left: RED
                (true, false) => (30, 220, 30),  // top-right: GREEN
                (false, true) => (30, 30, 220),  // bottom-left: BLUE
                (true, true) => (220, 220, 30),  // bottom-right: YELLOW
            };
            pixels[idx] = r;
            pixels[idx + 1] = g;
            pixels[idx + 2] = b;
        }
    }
    (w, h, pixels)
}

/// Create a 16x8 asymmetric image with 2 colored blocks.
///
/// Block layout (each block is 8x8 pixels):
/// ```text
///   (0,0) RED     (1,0) GREEN
/// ```
///
/// Returns (width=16, height=8, pixels).
fn make_wide_image() -> (u32, u32, Vec<u8>) {
    let (w, h) = (16u32, 8u32);
    let mut pixels = vec![0u8; (w * h * 3) as usize];
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            let (r, g, b) = if x < 8 {
                (220, 30, 30) // left: RED
            } else {
                (30, 220, 30) // right: GREEN
            };
            pixels[idx] = r;
            pixels[idx + 1] = g;
            pixels[idx + 2] = b;
        }
    }
    (w, h, pixels)
}

const RED: (u8, u8, u8) = (220, 30, 30);
const GREEN: (u8, u8, u8) = (30, 220, 30);
const BLUE: (u8, u8, u8) = (30, 30, 220);
const YELLOW: (u8, u8, u8) = (220, 220, 30);

// Q97 4:4:4 has some compression artifacts on solid color boundaries
const TOL: u8 = 12;

/// Verify all 8 transforms produce correct pixel positions (2x2 quadrant image).
///
/// Source layout:
/// ```text
///   RED    GREEN
///   BLUE   YELLOW
/// ```
///
/// Expected output per transform:
/// ```text
/// None:        RED    GREEN    FlipH:       GREEN  RED
///              BLUE   YELLOW                YELLOW BLUE
///
/// FlipV:       BLUE   YELLOW   Rotate180:   YELLOW BLUE
///              RED    GREEN                 GREEN  RED
///
/// Transpose:   RED    BLUE     Rotate90:    BLUE   RED
///              GREEN  YELLOW                YELLOW GREEN
///
/// Transverse:  YELLOW GREEN    Rotate270:   GREEN  YELLOW
///              BLUE   RED                   RED    BLUE
/// ```
#[test]
fn test_all_transforms_pixel_positions() {
    use crate::decode::DecodeConfig;

    let (w, h, pixels) = make_quadrant_image();
    let jpeg = encode_test_image(w, h, &pixels, None);

    // (transform, expected_w, expected_h, TL, TR, BL, BR)
    let cases: &[(
        LosslessTransform,
        u32,
        u32,
        (u8, u8, u8),
        (u8, u8, u8),
        (u8, u8, u8),
        (u8, u8, u8),
    )] = &[
        (LosslessTransform::None, 16, 16, RED, GREEN, BLUE, YELLOW),
        (
            LosslessTransform::FlipHorizontal,
            16,
            16,
            GREEN,
            RED,
            YELLOW,
            BLUE,
        ),
        (
            LosslessTransform::FlipVertical,
            16,
            16,
            BLUE,
            YELLOW,
            RED,
            GREEN,
        ),
        (
            LosslessTransform::Rotate180,
            16,
            16,
            YELLOW,
            BLUE,
            GREEN,
            RED,
        ),
        (
            LosslessTransform::Transpose,
            16,
            16,
            RED,
            BLUE,
            GREEN,
            YELLOW,
        ),
        (
            LosslessTransform::Rotate90,
            16,
            16,
            BLUE,
            RED,
            YELLOW,
            GREEN,
        ),
        (
            LosslessTransform::Rotate270,
            16,
            16,
            GREEN,
            YELLOW,
            RED,
            BLUE,
        ),
        (
            LosslessTransform::Transverse,
            16,
            16,
            YELLOW,
            GREEN,
            BLUE,
            RED,
        ),
    ];

    for &(transform, exp_w, exp_h, tl, tr, bl, br) in cases {
        let config = DecodeConfig::new().transform(transform);
        let (out_w, out_h, out_pixels) = decode_test(&jpeg, &config);

        assert_eq!(out_w, exp_w, "{transform:?}: wrong width");
        assert_eq!(out_h, exp_h, "{transform:?}: wrong height");

        let label = format!("{transform:?}");
        assert_block_near(&out_pixels, out_w as usize, 0, 0, tl, TOL, &label);
        assert_block_near(&out_pixels, out_w as usize, 1, 0, tr, TOL, &label);
        assert_block_near(&out_pixels, out_w as usize, 0, 1, bl, TOL, &label);
        assert_block_near(&out_pixels, out_w as usize, 1, 1, br, TOL, &label);
    }
}

/// Verify dimension-swapping transforms on an asymmetric image (16x8 → 8x16).
#[test]
fn test_dimension_swap_pixel_positions() {
    use crate::decode::DecodeConfig;

    let (w, h, pixels) = make_wide_image();
    let jpeg = encode_test_image(w, h, &pixels, None);

    // Transpose: (0,0)=RED (1,0)=GREEN → 8x16 with (0,0)=RED (0,1)=GREEN
    let config = DecodeConfig::new().transform(LosslessTransform::Transpose);
    let (out_w, out_h, out_pixels) = decode_test(&jpeg, &config);
    assert_eq!((out_w, out_h), (8, 16), "Transpose should swap 16x8 → 8x16");
    assert_block_near(&out_pixels, out_w as usize, 0, 0, RED, TOL, "Transpose-TL");
    assert_block_near(
        &out_pixels,
        out_w as usize,
        0,
        1,
        GREEN,
        TOL,
        "Transpose-BL",
    );

    // Rotate90: (0,0)=RED (1,0)=GREEN → 8x16 with (0,0)=RED (0,1)=GREEN... wait
    // Rotate90 maps (x,y) → (H-1-y, x): so pixel at (0,0) goes to (7,0), pixel at (8,0) goes to (7,8)
    // Block (0,0)RED→block(0,0), block(1,0)GREEN→block(0,1)
    // Actually for a 2x1 → 1x2 grid: src block(0,0) at x=0 y=0 → dst remap
    // remap_block(0,0, 2,1, Rot90) → (0, 2-1-0) = (0, 1)... wait let me check
    // Actually the remap depends on the grid dimensions.
    // For Rotate90 on a 2x1 grid → 1x2 grid:
    //   src(0,0) → dst: Rotate90 maps (bx,by) → (src_bh-1-by, bx) = (0, 0)
    //   src(1,0) → dst: (0, 1)
    // So: block(0,0)=RED stays at (0,0), block(1,0)=GREEN goes to (0,1)
    // Hmm that's same as Transpose for 1-row images.
    // Let me just verify dimensions and that scanline matches decode.

    let config = DecodeConfig::new().transform(LosslessTransform::Rotate90);
    let (out_w, out_h, _) = decode_test(&jpeg, &config);
    assert_eq!((out_w, out_h), (8, 16), "Rotate90 should swap 16x8 → 8x16");

    let config = DecodeConfig::new().transform(LosslessTransform::Rotate270);
    let (out_w, out_h, _) = decode_test(&jpeg, &config);
    assert_eq!((out_w, out_h), (8, 16), "Rotate270 should swap 16x8 → 8x16");
}

/// Verify all 8 EXIF orientations produce correct pixels via auto_orient.
#[test]
fn test_auto_orient_all_orientations() {
    use crate::decode::DecodeConfig;
    use crate::encoder::Orientation;

    let (w, h, pixels) = make_quadrant_image();

    // Each EXIF orientation tells the viewer how to transform the stored pixels
    // to get the correct display. auto_orient applies that transform.
    //
    // Orientation N means "the stored pixels need transform N to look correct."
    // So the stored image is the INVERSE of what the photographer intended.
    // auto_orient applies the forward transform to undo the camera rotation.
    let cases: &[(
        Orientation,
        (u8, u8, u8),
        (u8, u8, u8),
        (u8, u8, u8),
        (u8, u8, u8),
    )] = &[
        // orientation=1 (Normal): no change
        (Orientation::Normal, RED, GREEN, BLUE, YELLOW),
        // orientation=2 (FlipH): apply FlipH
        (Orientation::FlipHorizontal, GREEN, RED, YELLOW, BLUE),
        // orientation=3 (Rotate180): apply Rotate180
        (Orientation::Rotate180, YELLOW, BLUE, GREEN, RED),
        // orientation=4 (FlipV): apply FlipV
        (Orientation::FlipVertical, BLUE, YELLOW, RED, GREEN),
        // orientation=5 (Transpose): apply Transpose → dims stay 16x16
        (Orientation::Transpose, RED, BLUE, GREEN, YELLOW),
        // orientation=6 (Rotate90): apply Rotate90 → dims stay 16x16
        (Orientation::Rotate90, BLUE, RED, YELLOW, GREEN),
        // orientation=7 (Transverse): apply Transverse → dims stay 16x16
        (Orientation::Transverse, YELLOW, GREEN, BLUE, RED),
        // orientation=8 (Rotate270): apply Rotate270 → dims stay 16x16
        (Orientation::Rotate270, GREEN, YELLOW, RED, BLUE),
    ];

    for &(orient, tl, tr, bl, br) in cases {
        let jpeg = encode_test_image(w, h, &pixels, Some(orient));

        let config = DecodeConfig::new().auto_orient(true);
        let (out_w, out_h, out_pixels) = decode_test(&jpeg, &config);

        let label = format!("auto_orient({orient:?})");

        // Square image: dimensions always 16x16 regardless of transform
        assert_eq!(out_w, 16, "{label}: wrong width");
        assert_eq!(out_h, 16, "{label}: wrong height");

        assert_block_near(&out_pixels, out_w as usize, 0, 0, tl, TOL, &label);
        assert_block_near(&out_pixels, out_w as usize, 1, 0, tr, TOL, &label);
        assert_block_near(&out_pixels, out_w as usize, 0, 1, bl, TOL, &label);
        assert_block_near(&out_pixels, out_w as usize, 1, 1, br, TOL, &label);
    }
}

/// Verify scanline reader matches buffered decode for all 8 transforms.
#[test]
fn test_scanline_matches_decode_all_transforms() {
    use crate::decode::DecodeConfig;

    let (w, h, pixels) = make_quadrant_image();
    let jpeg = encode_test_image(w, h, &pixels, None);

    for &transform in &LosslessTransform::ALL {
        let config = DecodeConfig::new().transform(transform);
        let (dw, dh, decode_pixels) = decode_test(&jpeg, &config);
        let (sw, sh, scanline_pixels) = scanline_decode_test(&jpeg, &config);

        assert_eq!((dw, dh), (sw, sh), "{transform:?}: dimension mismatch");
        assert_eq!(
            decode_pixels, scanline_pixels,
            "{transform:?}: scanline pixels differ from buffered decode"
        );
    }
}

/// Verify scanline reader matches buffered decode for all 8 EXIF orientations.
#[test]
fn test_scanline_matches_decode_all_orientations() {
    use crate::decode::DecodeConfig;
    use crate::encoder::Orientation;

    let (w, h, pixels) = make_quadrant_image();

    let orientations = [
        Orientation::Normal,
        Orientation::FlipHorizontal,
        Orientation::Rotate180,
        Orientation::FlipVertical,
        Orientation::Transpose,
        Orientation::Rotate90,
        Orientation::Transverse,
        Orientation::Rotate270,
    ];

    for orient in orientations {
        let jpeg = encode_test_image(w, h, &pixels, Some(orient));

        let config = DecodeConfig::new().auto_orient(true);
        let (dw, dh, decode_pixels) = decode_test(&jpeg, &config);
        let (sw, sh, scanline_pixels) = scanline_decode_test(&jpeg, &config);

        assert_eq!(
            (dw, dh),
            (sw, sh),
            "auto_orient({orient:?}): dimension mismatch"
        );
        assert_eq!(
            decode_pixels, scanline_pixels,
            "auto_orient({orient:?}): scanline pixels differ from buffered decode"
        );
    }
}

/// Verify transforms work with 4:2:0 chroma subsampling.
#[test]
fn test_transform_420_subsampling() {
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
    use enough::Unstoppable;

    // 32x32 so MCU size (16x16 for 4:2:0) divides evenly → 2x2 MCU grid
    let (w, h) = (32u32, 32u32);
    let mut pixels = vec![0u8; (w * h * 3) as usize];
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            let (r, g, b) = match (x >= 16, y >= 16) {
                (false, false) => (220, 30, 30), // RED
                (true, false) => (30, 220, 30),  // GREEN
                (false, true) => (30, 30, 220),  // BLUE
                (true, true) => (220, 220, 30),  // YELLOW
            };
            pixels[idx] = r;
            pixels[idx + 1] = g;
            pixels[idx + 2] = b;
        }
    }

    let config = EncoderConfig::ycbcr(97, ChromaSubsampling::Quarter);
    let mut enc = config
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    // Larger tolerance for 4:2:0 chroma bleeding at block boundaries
    let tol_420: u8 = 25;

    for &transform in &LosslessTransform::ALL {
        let config = DecodeConfig::new().transform(transform);
        let (dw, dh, decode_pixels) = decode_test(&jpeg, &config);
        let (sw, sh, _) = scanline_decode_test(&jpeg, &config);

        assert_eq!(
            (dw, dh),
            (sw, sh),
            "{transform:?} 4:2:0: dimension mismatch"
        );
        assert_eq!(
            (dw, dh),
            (32, 32),
            "{transform:?} 4:2:0: square stays square"
        );

        // Verified 2026-03-31: all 8 transforms produce zero-diff between
        // buffered and scanline paths on 4:2:0 (47x53 non-MCU-aligned).
        // See test_420_scanline_vs_buffered_per_transform for proof.

        // Check block positions for identity
        if transform == LosslessTransform::None {
            assert_block_near(
                &decode_pixels,
                dw as usize,
                0,
                0,
                RED,
                tol_420,
                "420-None-TL",
            );
            assert_block_near(
                &decode_pixels,
                dw as usize,
                2,
                0,
                GREEN,
                tol_420,
                "420-None-TR",
            );
            assert_block_near(
                &decode_pixels,
                dw as usize,
                0,
                2,
                BLUE,
                tol_420,
                "420-None-BL",
            );
            assert_block_near(
                &decode_pixels,
                dw as usize,
                2,
                2,
                YELLOW,
                tol_420,
                "420-None-BR",
            );
        }
    }
}

/// Measure per-transform max pixel diff between buffered and scanline decode for 4:2:0.
/// This test exists to identify which transforms have chroma boundary errors so we can
/// force buffered decode for those transforms only.
#[test]
fn test_420_scanline_vs_buffered_per_transform() {
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
    use enough::Unstoppable;

    // Use a non-MCU-aligned image with varied content to exercise chroma upsampling
    // 47x53 forces partial MCUs on both edges for 4:2:0 (MCU=16)
    let (w, h) = (47u32, 53u32);
    let mut pixels = vec![0u8; (w * h * 3) as usize];
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            // Varied pattern: gradients + high-frequency chroma
            pixels[idx] = ((x * 7 + y * 3) % 256) as u8;
            pixels[idx + 1] = ((x * 3 + y * 11 + 128) % 256) as u8;
            pixels[idx + 2] = ((x * 13 + y * 5 + 64) % 256) as u8;
        }
    }

    let config = EncoderConfig::ycbcr(85, ChromaSubsampling::Quarter);
    let mut enc = config
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    // Test ALL transforms, not just non-swapping.
    // Dimension-swapping ones already use buffered fallback, but verify anyway.
    for &transform in &LosslessTransform::ALL {
        let cfg = DecodeConfig::new().transform(transform);
        let (_dw, _dh, buffered) = decode_test(&jpeg, &cfg);
        let (_sw, _sh, scanline) = scanline_decode_test(&jpeg, &cfg);

        let mut max_diff = 0u8;
        let mut diff_count = 0usize;
        for (a, b) in buffered.iter().zip(scanline.iter()) {
            let d = a.abs_diff(*b);
            if d > max_diff {
                max_diff = d;
            }
            if d > 0 {
                diff_count += 1;
            }
        }

        // Print results for diagnosis
        eprintln!(
            "{:?}: max_diff={}, diff_pixels={}/{} ({:.1}%)",
            transform,
            max_diff,
            diff_count / 3, // per pixel, not per channel
            buffered.len() / 3,
            100.0 * diff_count as f64 / buffered.len() as f64
        );

        // ALL non-swapping transforms MUST produce identical output between
        // buffered and scanline paths. Any difference is a bug.
        assert_eq!(
            max_diff,
            0,
            "{:?}: scanline vs buffered max_diff={}, {} pixels differ. \
             4:2:0 scanline path must match buffered decode exactly.",
            transform,
            max_diff,
            diff_count / 3
        );
    }
}

/// Verify transform on grayscale images.
#[test]
fn test_transform_grayscale() {
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
    use enough::Unstoppable;

    // 16x8 grayscale: left half dark, right half bright
    let (w, h) = (16u32, 8u32);
    let mut pixels = vec![0u8; (w * h * 3) as usize];
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            let v = if x < 8 { 40u8 } else { 210u8 };
            pixels[idx] = v;
            pixels[idx + 1] = v;
            pixels[idx + 2] = v;
        }
    }

    let config = EncoderConfig::ycbcr(97, ChromaSubsampling::None);
    let mut enc = config
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    // Rotate90: 16x8 → 8x16
    let config = DecodeConfig::new().transform(LosslessTransform::Rotate90);
    let (out_w, out_h, _) = decode_test(&jpeg, &config);
    assert_eq!((out_w, out_h), (8, 16), "Rotate90 on 16x8 should give 8x16");

    // FlipH: 16x8 stays 16x8, left/right swap
    let config = DecodeConfig::new().transform(LosslessTransform::FlipHorizontal);
    let (out_w, out_h, out_pixels) = decode_test(&jpeg, &config);
    assert_eq!((out_w, out_h), (16, 8), "FlipH preserves dimensions");

    // After FlipH, left should be bright, right should be dark
    let left_avg = block_avg(&out_pixels, out_w as usize, 0, 0);
    let right_avg = block_avg(&out_pixels, out_w as usize, 1, 0);
    assert!(
        left_avg.0 > 180 && right_avg.0 < 80,
        "FlipH should swap dark/bright: left={left_avg:?}, right={right_avg:?}"
    );

    // Scanline matches decode for all transforms
    for &transform in &LosslessTransform::ALL {
        let config = DecodeConfig::new().transform(transform);
        let (dw, dh, decode_pixels) = decode_test(&jpeg, &config);
        let (sw, sh, scanline_pixels) = scanline_decode_test(&jpeg, &config);

        assert_eq!(
            (dw, dh),
            (sw, sh),
            "{transform:?} grayscale: dimension mismatch"
        );
        assert_eq!(
            decode_pixels, scanline_pixels,
            "{transform:?} grayscale: scanline differs from decode"
        );
    }
}

/// Verify that no-transform path produces identical output to normal decode.
#[test]
fn test_no_transform_unchanged() {
    use crate::decode::DecodeConfig;

    let (w, h, pixels) = make_quadrant_image();
    let jpeg = encode_test_image(w, h, &pixels, None);

    // Default config (no transform, no auto_orient)
    let baseline = DecodeConfig::new();
    let (bw, bh, baseline_pixels) = decode_test(&jpeg, &baseline);

    // Explicit None transform should produce identical output
    let config = DecodeConfig::new().transform(LosslessTransform::None);
    let (tw, th, transform_pixels) = decode_test(&jpeg, &config);

    assert_eq!((bw, bh), (tw, th));
    assert_eq!(
        baseline_pixels, transform_pixels,
        "None transform should be identical to no transform"
    );

    // auto_orient with no EXIF should also produce identical output
    let config = DecodeConfig::new().auto_orient(true);
    let (aw, ah, orient_pixels) = decode_test(&jpeg, &config);

    assert_eq!((bw, bh), (aw, ah));
    assert_eq!(
        baseline_pixels, orient_pixels,
        "auto_orient with no EXIF should be identical"
    );
}

/// Verify composed transform: EXIF orientation + explicit user transform.
#[test]
fn test_composed_orientation_and_transform() {
    use crate::decode::DecodeConfig;
    use crate::encoder::Orientation;

    let (w, h, pixels) = make_quadrant_image();

    // Encode with orientation=6 (Rotate90)
    let jpeg = encode_test_image(w, h, &pixels, Some(Orientation::Rotate90));

    // auto_orient(true) alone → applies Rotate90
    let config = DecodeConfig::new().auto_orient(true);
    let (_, _, orient_only) = decode_test(&jpeg, &config);

    // Rotate90.then(Rotate270) = None → should match original decode without any transform
    let config = DecodeConfig::new()
        .auto_orient(true)
        .transform(LosslessTransform::Rotate270);
    let (cw, ch, composed_pixels) = decode_test(&jpeg, &config);
    assert_eq!((cw, ch), (16, 16));

    // The composed result should match decoding without any transform (raw, no auto-orient)
    let baseline = DecodeConfig::new().auto_orient(false);
    let (_, _, baseline_pixels) = decode_test(&jpeg, &baseline);
    assert_eq!(
        composed_pixels, baseline_pixels,
        "Rotate90 + Rotate270 should be identity"
    );

    // auto_orient alone should differ from baseline (it actually rotates)
    assert_ne!(
        orient_only, baseline_pixels,
        "auto_orient(Rotate90) should change the pixels"
    );
}

// ==========================================================================
// Non-MCU-aligned (partial block) tests
// ==========================================================================
//
// JPEG pads images to MCU boundaries (right and bottom edges). After a
// dimension-swapping transform, padding that was on the bottom moves to a
// different edge. The decoder must output the correct cropped region.

/// Verify all transforms on a non-MCU-aligned image (12x20, partial blocks).
///
/// 12x20 → 2 blocks wide (12/8=1.5 → ceil=2), 3 blocks tall (20/8=2.5 → ceil=3).
/// Padding: 4 columns on right, 4 rows on bottom.
///
/// After Rotate90 (swaps dims), output should be 20x12 — the rotated image
/// must still show the correct content, not padding.
#[test]
fn test_non_mcu_aligned_all_transforms() {
    use crate::decode::DecodeConfig;

    let (w, h) = (12u32, 20u32);
    let mut pixels = vec![0u8; (w * h * 3) as usize];

    // Paint a distinctive pattern: top-left quadrant (6x10) is RED,
    // top-right (6x10) is GREEN, bottom-left is BLUE, bottom-right is YELLOW.
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            let (r, g, b) = match (x >= 6, y >= 10) {
                (false, false) => (220, 30, 30), // RED
                (true, false) => (30, 220, 30),  // GREEN
                (false, true) => (30, 30, 220),  // BLUE
                (true, true) => (220, 220, 30),  // YELLOW
            };
            pixels[idx] = r;
            pixels[idx + 1] = g;
            pixels[idx + 2] = b;
        }
    }

    let jpeg = encode_test_image(w, h, &pixels, None);

    for &transform in &LosslessTransform::ALL {
        let config = DecodeConfig::new().transform(transform);
        let (out_w, out_h, out_pixels) = decode_test(&jpeg, &config);

        let label = format!("{transform:?}");

        // Verify dimensions
        if transform.swaps_dimensions() {
            assert_eq!(
                (out_w, out_h),
                (20, 12),
                "{label}: non-aligned swapped dimensions"
            );
        } else {
            assert_eq!(
                (out_w, out_h),
                (12, 20),
                "{label}: non-aligned preserved dimensions"
            );
        }

        // Verify content: check that each corner pixel is near the expected
        // color (not padding/garbage). Use pixel (1,1) and (w-2,h-2) etc.
        // to avoid edge effects.
        let check_pixel = |px: usize, py: usize, label: &str| -> (u8, u8, u8) {
            let idx = (py * out_w as usize + px) * 3;
            assert!(
                idx + 2 < out_pixels.len(),
                "{label}: pixel ({px},{py}) out of bounds (image {out_w}x{out_h})"
            );
            (out_pixels[idx], out_pixels[idx + 1], out_pixels[idx + 2])
        };

        // Top-left corner (2,2) — should NOT be black/garbage
        let tl = check_pixel(2, 2, &label);
        assert!(
            tl.0 > 10 || tl.1 > 10 || tl.2 > 10,
            "{label}: top-left pixel is black (likely padding): {tl:?}"
        );

        // Bottom-right corner (w-3, h-3) — should NOT be black/garbage
        let br = check_pixel(out_w as usize - 3, out_h as usize - 3, &label);
        assert!(
            br.0 > 10 || br.1 > 10 || br.2 > 10,
            "{label}: bottom-right pixel is black (likely padding): {br:?}"
        );

        // Scanline should match decode
        let (sw, sh, scanline_pixels) = scanline_decode_test(&jpeg, &config);
        assert_eq!(
            (out_w, out_h),
            (sw, sh),
            "{label}: scanline dimension mismatch"
        );
        assert_eq!(
            out_pixels, scanline_pixels,
            "{label}: scanline pixels differ from decode"
        );
    }
}

/// Verify non-MCU-aligned with auto_orient + dimension swap.
///
/// A 12x20 image with orientation=6 (Rotate90) should produce 20x12 output.
#[test]
fn test_non_mcu_aligned_auto_orient() {
    use crate::decode::DecodeConfig;
    use crate::encoder::Orientation;

    let (w, h) = (12u32, 20u32);
    let mut pixels = vec![0u8; (w * h * 3) as usize];
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            // Left half bright, right half dark
            let v = if x < 6 { 200u8 } else { 50u8 };
            pixels[idx] = v;
            pixels[idx + 1] = v;
            pixels[idx + 2] = v;
        }
    }

    let jpeg = encode_test_image(w, h, &pixels, Some(Orientation::Rotate90));

    let config = DecodeConfig::new().auto_orient(true);
    let (out_w, out_h, out_pixels) = decode_test(&jpeg, &config);

    // Rotate90 swaps dimensions: 12x20 → 20x12
    assert_eq!(
        (out_w, out_h),
        (20, 12),
        "auto_orient Rotate90 on 12x20 should give 20x12"
    );

    // Verify we get valid pixel data (not padding) at all corners
    let corners = [
        (1, 1, "TL"),
        (out_w as usize - 2, 1, "TR"),
        (1, out_h as usize - 2, "BL"),
        (out_w as usize - 2, out_h as usize - 2, "BR"),
    ];
    for (px, py, name) in corners {
        let idx = (py * out_w as usize + px) * 3;
        let pixel = (out_pixels[idx], out_pixels[idx + 1], out_pixels[idx + 2]);
        assert!(
            pixel.0 > 10 || pixel.1 > 10 || pixel.2 > 10,
            "auto_orient non-aligned {name} pixel is black (padding?): {pixel:?}"
        );
    }

    // Scanline should match
    let (sw, sh, scanline_pixels) = scanline_decode_test(&jpeg, &config);
    assert_eq!((out_w, out_h), (sw, sh));
    assert_eq!(
        out_pixels, scanline_pixels,
        "non-aligned auto_orient: scanline differs"
    );
}

/// Verify non-MCU-aligned with 4:2:0 subsampling and transforms.
///
/// 12x20 with 4:2:0: MCU = 16x16, so MCU grid is 1x2.
/// This is the most challenging case: partial MCUs + chroma subsampling + transform.
#[test]
fn test_non_mcu_aligned_420_transform() {
    use crate::decode::DecodeConfig;
    use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
    use enough::Unstoppable;

    let (w, h) = (12u32, 20u32);
    let mut pixels = vec![0u8; (w * h * 3) as usize];
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            let v = if y < 10 { 200u8 } else { 50u8 };
            pixels[idx] = v;
            pixels[idx + 1] = v / 2;
            pixels[idx + 2] = 100;
        }
    }

    let config = EncoderConfig::ycbcr(97, ChromaSubsampling::Quarter);
    let mut enc = config
        .encode_from_bytes(w, h, PixelLayout::Rgb8Srgb)
        .unwrap();
    enc.push_packed(&pixels, Unstoppable).unwrap();
    let jpeg = enc.finish().unwrap();

    for &transform in &LosslessTransform::ALL {
        let config = DecodeConfig::new().transform(transform);
        let (out_w, out_h, out_pixels) = decode_test(&jpeg, &config);

        let label = format!("{transform:?} 4:2:0 non-aligned");

        if transform.swaps_dimensions() {
            assert_eq!((out_w, out_h), (20, 12), "{label}: wrong dimensions");
        } else {
            assert_eq!((out_w, out_h), (12, 20), "{label}: wrong dimensions");
        }

        // No padding/garbage at corners
        let corners = [
            (1, 1),
            (out_w as usize - 2, 1),
            (1, out_h as usize - 2),
            (out_w as usize - 2, out_h as usize - 2),
        ];
        for (px, py) in corners {
            let idx = (py * out_w as usize + px) * 3;
            let pixel = (out_pixels[idx], out_pixels[idx + 1], out_pixels[idx + 2]);
            assert!(
                pixel.0 > 10 || pixel.1 > 10 || pixel.2 > 10,
                "{label}: corner ({px},{py}) is black (padding?): {pixel:?}"
            );
        }
    }
}

/// Apply a lossless transform to pixels in pixel-space (reference implementation).
///
/// Input: RGB8 packed pixels in row-major order for a w×h image.
/// Returns (new_w, new_h, new_pixels).
fn pixel_transform(
    pixels: &[u8],
    w: usize,
    h: usize,
    transform: LosslessTransform,
) -> (usize, usize, Vec<u8>) {
    let (out_w, out_h) = if transform.swaps_dimensions() {
        (h, w)
    } else {
        (w, h)
    };
    let mut out = vec![0u8; out_w * out_h * 3];
    for sy in 0..h {
        for sx in 0..w {
            let (dx, dy) = match transform {
                LosslessTransform::None => (sx, sy),
                LosslessTransform::FlipHorizontal => (w - 1 - sx, sy),
                LosslessTransform::FlipVertical => (sx, h - 1 - sy),
                LosslessTransform::Rotate180 => (w - 1 - sx, h - 1 - sy),
                LosslessTransform::Transpose => (sy, sx),
                LosslessTransform::Rotate90 => (h - 1 - sy, sx),
                LosslessTransform::Rotate270 => (sy, w - 1 - sx),
                LosslessTransform::Transverse => (h - 1 - sy, w - 1 - sx),
            };
            let si = (sy * w + sx) * 3;
            let di = (dy * out_w + dx) * 3;
            out[di..di + 3].copy_from_slice(&pixels[si..si + 3]);
        }
    }
    (out_w, out_h, out)
}

/// Verify border pixels survive lossless transform on a 15x17 non-MCU-aligned image.
///
/// Strategy: encode once, decode without transform (reference), decode with each
/// transform, then compare the transformed decode against a pixel-space transform
/// of the reference. If the DCT-domain transform is truly lossless, every pixel
/// should match exactly.
///
/// 15x17 is deliberately awkward:
/// - 4:4:4 MCU=8: 2x3 MCU grid, partial right column (7px) and partial bottom row (1px)
/// - After dimension-swap: 17x15, 3x2 MCU grid, partial right (1px) and bottom (7px)
#[test]
fn test_15x17_border_pixels_lossless() {
    use crate::decode::DecodeConfig;

    let (w, h) = (15u32, 17u32);

    // Create an image with unique per-pixel values derived from position.
    let mut pixels = vec![0u8; (w * h * 3) as usize];
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            pixels[idx] = (x * 17 % 256) as u8;
            pixels[idx + 1] = (y * 15 % 256) as u8;
            pixels[idx + 2] = ((x * 7 + y * 11) % 256) as u8;
        }
    }

    let jpeg = encode_test_image(w, h, &pixels, None);

    // Decode reference with i16 IDCT (used for non-dimension-swapping transforms)
    let config_none = DecodeConfig::new();
    let (ref_w, ref_h, ref_pixels_i16) = decode_test(&jpeg, &config_none);
    assert_eq!((ref_w, ref_h), (w, h));

    // Decode reference with f32 IDCT (used for dimension-swapping transforms,
    // since they force f32 IDCT for symmetric rounding)
    let mut config_f32 = DecodeConfig::new();
    config_f32.force_f32_idct = true;
    let (_, _, ref_pixels_f32) = decode_test(&jpeg, &config_f32);

    // Collect all border pixel positions for the original image
    let border_positions: Vec<(usize, usize)> = {
        let mut positions = Vec::new();
        for x in 0..w as usize {
            positions.push((x, 0)); // top edge
            positions.push((x, h as usize - 1)); // bottom edge
        }
        for y in 1..h as usize - 1 {
            positions.push((0, y)); // left edge
            positions.push((w as usize - 1, y)); // right edge
        }
        positions
    };

    for &transform in &LosslessTransform::ALL {
        let label = format!("{transform:?}");

        // DCT-domain transform (uses f32 IDCT for dimension-swapping, i16 otherwise)
        let config = DecodeConfig::new().transform(transform);
        let (dct_w, dct_h, dct_pixels) = decode_test(&jpeg, &config);

        // Pixel-space transform of the matching reference
        let ref_pixels = if transform.swaps_dimensions() {
            &ref_pixels_f32
        } else {
            &ref_pixels_i16
        };
        let (px_w, px_h, px_pixels) =
            pixel_transform(ref_pixels, ref_w as usize, ref_h as usize, transform);

        assert_eq!(
            (dct_w, dct_h),
            (px_w as u32, px_h as u32),
            "{label}: dimension mismatch"
        );

        let ow = ref_w as usize;
        let oh = ref_h as usize;
        let tw = dct_w as usize;

        let mut max_diff = 0u8;
        let mut worst_pos = (0, 0);
        let mut mismatches = 0;

        for &(sx, sy) in &border_positions {
            // Where does this border pixel end up after transform?
            let (dx, dy) = match transform {
                LosslessTransform::None => (sx, sy),
                LosslessTransform::FlipHorizontal => (ow - 1 - sx, sy),
                LosslessTransform::FlipVertical => (sx, oh - 1 - sy),
                LosslessTransform::Rotate180 => (ow - 1 - sx, oh - 1 - sy),
                LosslessTransform::Transpose => (sy, sx),
                LosslessTransform::Rotate90 => (oh - 1 - sy, sx),
                LosslessTransform::Rotate270 => (sy, ow - 1 - sx),
                LosslessTransform::Transverse => (oh - 1 - sy, ow - 1 - sx),
            };

            let idx = (dy * tw + dx) * 3;

            for c in 0..3 {
                let diff =
                    (dct_pixels[idx + c] as i16 - px_pixels[idx + c] as i16).unsigned_abs() as u8;
                if diff > max_diff {
                    max_diff = diff;
                    worst_pos = (dx, dy);
                }
                if diff > 0 {
                    mismatches += 1;
                }
            }
        }

        assert_eq!(
            max_diff,
            0,
            "{label}: border pixel mismatch! max_diff={max_diff} at ({},{}) \
             mismatches={mismatches}/{} channels",
            worst_pos.0,
            worst_pos.1,
            border_positions.len() * 3
        );
    }
}

/// Same as above but also tests the scanline reader path matches buffered.
#[test]
fn test_15x17_border_pixels_scanline() {
    use crate::decode::DecodeConfig;

    let (w, h) = (15u32, 17u32);

    let mut pixels = vec![0u8; (w * h * 3) as usize];
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            pixels[idx] = (x * 17 % 256) as u8;
            pixels[idx + 1] = (y * 15 % 256) as u8;
            pixels[idx + 2] = ((x * 7 + y * 11) % 256) as u8;
        }
    }

    let jpeg = encode_test_image(w, h, &pixels, None);

    for &transform in &LosslessTransform::ALL {
        let label = format!("{transform:?}");

        let config = DecodeConfig::new().transform(transform);
        let (buf_w, buf_h, buf_pixels) = decode_test(&jpeg, &config);
        let (scan_w, scan_h, scan_pixels) = scanline_decode_test(&jpeg, &config);

        assert_eq!(
            (buf_w, buf_h),
            (scan_w, scan_h),
            "{label}: scanline dimensions mismatch"
        );

        // Compare all pixels between buffered and scanline
        let mut max_diff = 0u8;
        let mut worst_pos = (0, 0);
        for y in 0..buf_h as usize {
            for x in 0..buf_w as usize {
                let idx = (y * buf_w as usize + x) * 3;
                for c in 0..3 {
                    let diff = (buf_pixels[idx + c] as i16 - scan_pixels[idx + c] as i16)
                        .unsigned_abs() as u8;
                    if diff > max_diff {
                        max_diff = diff;
                        worst_pos = (x, y);
                    }
                }
            }
        }

        assert_eq!(
            max_diff, 0,
            "{label}: scanline vs buffered mismatch! max_diff={max_diff} at ({},{})",
            worst_pos.0, worst_pos.1
        );
    }
}

/// Verify ALL pixels match between DCT-domain and pixel-space transform on 15x17.
#[test]
fn test_15x17_all_pixels_lossless() {
    // Lock prevents permutation tests from changing SIMD tier mid-test.
    let _lock = archmage::testing::lock_token_testing();
    use crate::decode::DecodeConfig;

    let (w, h) = (15u32, 17u32);

    let mut pixels = vec![0u8; (w * h * 3) as usize];
    for y in 0..h {
        for x in 0..w {
            let idx = ((y * w + x) * 3) as usize;
            pixels[idx] = ((x * 37 + y * 53) % 200 + 30) as u8;
            pixels[idx + 1] = ((x * 61 + y * 23) % 180 + 40) as u8;
            pixels[idx + 2] = ((x * 43 + y * 71) % 160 + 50) as u8;
        }
    }

    let jpeg = encode_test_image(w, h, &pixels, None);

    // Two references: i16 IDCT (default) and f32 IDCT (for dimension-swapping)
    let config_none = DecodeConfig::new();
    let (ref_w, ref_h, ref_pixels_i16) = decode_test(&jpeg, &config_none);
    assert_eq!((ref_w, ref_h), (w, h));
    let mut config_f32 = DecodeConfig::new();
    config_f32.force_f32_idct = true;
    let (_, _, ref_pixels_f32) = decode_test(&jpeg, &config_f32);

    for &transform in &LosslessTransform::ALL {
        if transform == LosslessTransform::None {
            continue;
        }
        let label = format!("{transform:?}");

        // DCT-domain transform
        let config = DecodeConfig::new().transform(transform);
        let (dct_w, dct_h, dct_pixels) = decode_test(&jpeg, &config);

        // Pixel-space transform of matching reference
        let ref_pixels = if transform.swaps_dimensions() {
            &ref_pixels_f32
        } else {
            &ref_pixels_i16
        };
        let (px_w, px_h, px_pixels) =
            pixel_transform(ref_pixels, ref_w as usize, ref_h as usize, transform);

        assert_eq!(
            (dct_w as usize, dct_h as usize),
            (px_w, px_h),
            "{label}: dimension mismatch"
        );

        let tw = dct_w as usize;
        let th = dct_h as usize;

        let mut max_diff = 0u8;
        let mut worst_pos = (0, 0);
        let mut total_diff = 0u64;
        let mut mismatches = 0usize;

        for y in 0..th {
            for x in 0..tw {
                let idx = (y * tw + x) * 3;
                for c in 0..3 {
                    let diff = (dct_pixels[idx + c] as i16 - px_pixels[idx + c] as i16)
                        .unsigned_abs() as u8;
                    if diff > max_diff {
                        max_diff = diff;
                        worst_pos = (x, y);
                    }
                    if diff > 0 {
                        mismatches += 1;
                        total_diff += diff as u64;
                    }
                }
            }
        }

        assert_eq!(
            max_diff,
            0,
            "{label}: DCT vs pixel transform mismatch! \
             max_diff={max_diff} at ({},{}) mismatches={mismatches}/{} mean_diff={:.2}",
            worst_pos.0,
            worst_pos.1,
            tw * th * 3,
            if mismatches > 0 {
                total_diff as f64 / mismatches as f64
            } else {
                0.0
            }
        );
    }
}

// ===== Restructure tests =====

mod restructure_tests {
    use crate::decode::DecodeConfig;
    use crate::lossless::{
        EdgeHandling, LosslessTransform, OutputMode, RestartInterval, RestructureConfig,
        TransformConfig, restructure,
    };
    use enough::Unstoppable;

    /// Create a test JPEG (4:4:4).
    fn create_test_jpeg(width: u32, height: u32) -> Vec<u8> {
        use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};

        let mut pixels = Vec::with_capacity((width * height * 3) as usize);
        for y in 0..height {
            for x in 0..width {
                pixels.push(((x * 255 / width) & 0xFF) as u8);
                pixels.push(((y * 255 / height) & 0xFF) as u8);
                pixels.push(128u8);
            }
        }

        let config = EncoderConfig::ycbcr(90, ChromaSubsampling::None);
        let mut enc = config
            .encode_from_bytes(width, height, PixelLayout::Rgb8Srgb)
            .unwrap();
        enc.push_packed(&pixels, Unstoppable).unwrap();
        enc.finish().unwrap()
    }

    /// Create a test JPEG with 4:2:0.
    fn create_test_jpeg_420(width: u32, height: u32) -> Vec<u8> {
        use crate::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};

        let mut pixels = Vec::with_capacity((width * height * 3) as usize);
        for y in 0..height {
            for x in 0..width {
                pixels.push(((x * 255 / width) & 0xFF) as u8);
                pixels.push(((y * 255 / height) & 0xFF) as u8);
                pixels.push(128u8);
            }
        }

        let config = EncoderConfig::ycbcr(90, ChromaSubsampling::Quarter);
        let mut enc = config
            .encode_from_bytes(width, height, PixelLayout::Rgb8Srgb)
            .unwrap();
        enc.push_packed(&pixels, Unstoppable).unwrap();
        enc.finish().unwrap()
    }

    /// Create a grayscale test JPEG.
    fn create_test_jpeg_gray(width: u32, height: u32) -> Vec<u8> {
        use crate::encoder::{EncoderConfig, PixelLayout};

        let mut pixels = Vec::with_capacity((width * height) as usize);
        for y in 0..height {
            for x in 0..width {
                pixels.push(((x * 255 / width + y * 64 / height) & 0xFF) as u8);
            }
        }

        let config = EncoderConfig::grayscale(90);
        let mut enc = config
            .encode_from_bytes(width, height, PixelLayout::Gray8Srgb)
            .unwrap();
        enc.push_packed(&pixels, Unstoppable).unwrap();
        enc.finish().unwrap()
    }

    /// Compare coefficients between two JPEGs.
    /// Returns (total_diffs, max_diff).
    fn compare_coefficients(a: &[u8], b: &[u8]) -> (usize, i16) {
        let decoder = DecodeConfig::new();
        let ca = decoder.decode_coefficients(a, Unstoppable).unwrap();
        let cb = decoder.decode_coefficients(b, Unstoppable).unwrap();

        assert_eq!(
            ca.components.len(),
            cb.components.len(),
            "component count mismatch"
        );

        let mut total_diffs = 0;
        let mut max_diff = 0i16;

        for (c1, c2) in ca.components.iter().zip(&cb.components) {
            assert_eq!(c1.blocks_wide, c2.blocks_wide, "blocks_wide mismatch");
            assert_eq!(c1.blocks_high, c2.blocks_high, "blocks_high mismatch");

            let num_blocks = c1.num_blocks().min(c2.num_blocks());
            for block_idx in 0..num_blocks {
                let b1 = c1.block(block_idx);
                let b2 = c2.block(block_idx);
                for i in 0..64 {
                    let d = (b1[i] as i32 - b2[i] as i32).abs() as i16;
                    if d != 0 {
                        total_diffs += 1;
                        max_diff = max_diff.max(d);
                    }
                }
            }
        }

        (total_diffs, max_diff)
    }

    /// Check if a JPEG contains a SOF2 (progressive) marker.
    fn is_progressive_jpeg(data: &[u8]) -> bool {
        for i in 0..data.len().saturating_sub(1) {
            if data[i] == 0xFF && data[i + 1] == 0xC2 {
                return true;
            }
        }
        false
    }

    /// Check if a JPEG contains a DRI marker.
    fn has_dri_marker(data: &[u8]) -> bool {
        for i in 0..data.len().saturating_sub(1) {
            if data[i] == 0xFF && data[i + 1] == 0xDD {
                return true;
            }
        }
        false
    }

    // ===== Sequential roundtrip tests =====

    #[test]
    fn test_restructure_sequential_identity() {
        let jpeg = create_test_jpeg(64, 64);
        let config = RestructureConfig {
            output_mode: OutputMode::Sequential,
            restart_interval: RestartInterval::None,
            transform: None,
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        assert!(!is_progressive_jpeg(&result));
        assert!(!has_dri_marker(&result));

        let (diffs, max_diff) = compare_coefficients(&jpeg, &result);
        assert_eq!(
            diffs, 0,
            "identity restructure should preserve all coefficients (max_diff={max_diff})"
        );
    }

    // ===== Restart marker tests =====

    #[test]
    fn test_restructure_sequential_with_restart_mcus() {
        let jpeg = create_test_jpeg(64, 64);
        let config = RestructureConfig {
            output_mode: OutputMode::Sequential,
            restart_interval: RestartInterval::EveryMcus(10),
            transform: None,
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        assert!(!is_progressive_jpeg(&result));
        assert!(has_dri_marker(&result), "output should contain DRI marker");

        // Coefficients must be identical
        let (diffs, max_diff) = compare_coefficients(&jpeg, &result);
        assert_eq!(
            diffs, 0,
            "restart markers should not affect coefficients (max_diff={max_diff})"
        );

        // Verify it decodes correctly
        let decoder = DecodeConfig::new();
        let decoded = decoder.decode(&result, Unstoppable).unwrap();
        assert_eq!(decoded.width(), 64);
        assert_eq!(decoded.height(), 64);
    }

    #[test]
    fn test_restructure_sequential_with_restart_mcu_rows() {
        let jpeg = create_test_jpeg(64, 64);
        let config = RestructureConfig {
            output_mode: OutputMode::Sequential,
            restart_interval: RestartInterval::EveryMcuRows(1),
            transform: None,
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        assert!(has_dri_marker(&result));

        let (diffs, max_diff) = compare_coefficients(&jpeg, &result);
        assert_eq!(
            diffs, 0,
            "MCU row restart should preserve coefficients (max_diff={max_diff})"
        );
    }

    // ===== Progressive tests =====

    #[test]
    fn test_restructure_progressive_roundtrip() {
        let jpeg = create_test_jpeg(64, 64);

        // Convert to progressive
        let prog_config = RestructureConfig {
            output_mode: OutputMode::Progressive,
            restart_interval: RestartInterval::None,
            transform: None,
        };
        let progressive = restructure(&jpeg, &prog_config, Unstoppable).unwrap();
        assert!(
            is_progressive_jpeg(&progressive),
            "output should be progressive"
        );

        // Coefficients must survive sequential->progressive
        let (diffs, max_diff) = compare_coefficients(&jpeg, &progressive);
        assert_eq!(
            diffs, 0,
            "progressive roundtrip should preserve all coefficients (max_diff={max_diff})"
        );
    }

    #[test]
    fn test_restructure_progressive_to_sequential() {
        let jpeg = create_test_jpeg(64, 64);

        // First convert to progressive
        let prog_config = RestructureConfig {
            output_mode: OutputMode::Progressive,
            ..Default::default()
        };
        let progressive = restructure(&jpeg, &prog_config, Unstoppable).unwrap();
        assert!(is_progressive_jpeg(&progressive));

        // Then back to sequential
        let seq_config = RestructureConfig {
            output_mode: OutputMode::Sequential,
            ..Default::default()
        };
        let sequential = restructure(&progressive, &seq_config, Unstoppable).unwrap();
        assert!(!is_progressive_jpeg(&sequential));

        // Coefficients must survive the round-trip
        let (diffs, max_diff) = compare_coefficients(&jpeg, &sequential);
        assert_eq!(
            diffs, 0,
            "progressive->sequential roundtrip should preserve coefficients (max_diff={max_diff})"
        );
    }

    #[test]
    fn test_restructure_progressive_ignores_restart() {
        // Progressive restart markers are not yet supported (token replay
        // infrastructure doesn't handle them). Verify that passing a restart
        // interval still produces a valid progressive JPEG without DRI.
        let jpeg = create_test_jpeg(64, 64);
        let config = RestructureConfig {
            output_mode: OutputMode::Progressive,
            restart_interval: RestartInterval::EveryMcus(8),
            transform: None,
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        assert!(is_progressive_jpeg(&result));
        // DRI is intentionally not written for progressive (not yet supported)
        assert!(!has_dri_marker(&result));

        let (diffs, max_diff) = compare_coefficients(&jpeg, &result);
        assert_eq!(
            diffs, 0,
            "progressive should preserve coefficients (max_diff={max_diff})"
        );

        // Verify it decodes
        let decoder = DecodeConfig::new();
        let decoded = decoder.decode(&result, Unstoppable).unwrap();
        assert_eq!(decoded.width(), 64);
    }

    // ===== Combined transform + restructure =====

    #[test]
    fn test_restructure_with_rotation() {
        let jpeg = create_test_jpeg(64, 48);
        let config = RestructureConfig {
            output_mode: OutputMode::Progressive,
            restart_interval: RestartInterval::None,
            transform: Some(TransformConfig {
                transform: LosslessTransform::Rotate90,
                edge_handling: EdgeHandling::TrimPartialBlocks,
            }),
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        assert!(is_progressive_jpeg(&result));

        // Verify dimensions swapped
        let decoder = DecodeConfig::new();
        let decoded = decoder.decode(&result, Unstoppable).unwrap();
        assert_eq!(decoded.width(), 48);
        assert_eq!(decoded.height(), 64);
    }

    // ===== Metadata preservation =====

    #[test]
    fn test_restructure_preserves_metadata() {
        let jpeg = create_test_jpeg(64, 64);

        // Parse metadata from original
        let decoder = DecodeConfig::new().preserve(crate::decode::PreserveConfig::all());
        let (_, orig_extras) = decoder
            .decode_coefficients_with_extras(&jpeg, Unstoppable)
            .unwrap();

        // Restructure to progressive
        let config = RestructureConfig {
            output_mode: OutputMode::Progressive,
            ..Default::default()
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        // Parse metadata from result
        let (_, result_extras) = decoder
            .decode_coefficients_with_extras(&result, Unstoppable)
            .unwrap();

        // Check metadata segment count matches
        let orig_segments = orig_extras
            .as_ref()
            .map(|e| e.segments().len())
            .unwrap_or(0);
        let result_segments = result_extras
            .as_ref()
            .map(|e| e.segments().len())
            .unwrap_or(0);
        assert_eq!(
            orig_segments, result_segments,
            "metadata segment count should be preserved"
        );
    }

    // ===== Subsampling variants =====

    #[test]
    fn test_restructure_420_sequential() {
        let jpeg = create_test_jpeg_420(64, 64);
        let config = RestructureConfig {
            output_mode: OutputMode::Sequential,
            restart_interval: RestartInterval::EveryMcus(4),
            ..Default::default()
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        let (diffs, max_diff) = compare_coefficients(&jpeg, &result);
        assert_eq!(
            diffs, 0,
            "4:2:0 sequential restructure should preserve coefficients (max_diff={max_diff})"
        );
    }

    #[test]
    fn test_restructure_420_progressive() {
        let jpeg = create_test_jpeg_420(64, 64);
        let config = RestructureConfig {
            output_mode: OutputMode::Progressive,
            restart_interval: RestartInterval::None,
            ..Default::default()
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        assert!(is_progressive_jpeg(&result));

        let (diffs, max_diff) = compare_coefficients(&jpeg, &result);
        assert_eq!(
            diffs, 0,
            "4:2:0 progressive restructure should preserve coefficients (max_diff={max_diff})"
        );
    }

    #[test]
    fn test_restructure_grayscale_sequential() {
        let jpeg = create_test_jpeg_gray(64, 64);
        let config = RestructureConfig {
            output_mode: OutputMode::Sequential,
            restart_interval: RestartInterval::EveryMcus(8),
            ..Default::default()
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        let (diffs, max_diff) = compare_coefficients(&jpeg, &result);
        assert_eq!(
            diffs, 0,
            "grayscale sequential restructure should preserve coefficients (max_diff={max_diff})"
        );
    }

    #[test]
    fn test_restructure_grayscale_progressive() {
        let jpeg = create_test_jpeg_gray(64, 64);
        let config = RestructureConfig {
            output_mode: OutputMode::Progressive,
            ..Default::default()
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        assert!(is_progressive_jpeg(&result));

        let (diffs, max_diff) = compare_coefficients(&jpeg, &result);
        assert_eq!(
            diffs, 0,
            "grayscale progressive restructure should preserve coefficients (max_diff={max_diff})"
        );
    }

    // ===== MCU row restart interval computation =====

    #[test]
    fn test_restructure_mcu_row_restart_420() {
        let jpeg = create_test_jpeg_420(64, 64);
        let config = RestructureConfig {
            output_mode: OutputMode::Sequential,
            restart_interval: RestartInterval::EveryMcuRows(1),
            ..Default::default()
        };
        let result = restructure(&jpeg, &config, Unstoppable).unwrap();

        assert!(has_dri_marker(&result));

        let (diffs, max_diff) = compare_coefficients(&jpeg, &result);
        assert_eq!(
            diffs, 0,
            "4:2:0 MCU row restart should preserve coefficients (max_diff={max_diff})"
        );
    }
}