1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
use crate::common::error::{DecodeWarning, JpegError, Result};
use crate::common::huffman_table::HuffmanTable;
use crate::common::icc;
use crate::common::quant_table::QuantTable;
use crate::common::types::*;
use crate::decode::bitstream::BitReader;
use crate::decode::entropy::{self, McuDecoder};
use crate::decode::huffman;
use crate::decode::idct_scaled;
use crate::decode::lossless;
use crate::decode::marker::{JpegMetadata, MarkerReader, ScanInfo};
use crate::decode::progressive;
use crate::simd::{self, SimdRoutines};
/// Generic nearest-neighbor upsampling for arbitrary h/v factor combinations.
///
/// Handles non-standard sampling factors like 3x2, 3x1, 1x3, 4x2 that lack
/// dedicated optimized paths. Each input sample is replicated h_factor times
/// horizontally and v_factor times vertically.
pub(crate) fn upsample_generic_nearest(
input: &[u8],
in_width: usize,
in_height: usize,
output: &mut [u8],
out_stride: usize,
h_factor: usize,
v_factor: usize,
) {
for y in 0..in_height {
let in_row: &[u8] = &input[y * in_width..y * in_width + in_width];
// Build one upsampled row (horizontal replication)
let out_y_base: usize = y * v_factor;
let first_out_row: usize = out_y_base * out_stride;
for (x, &val) in in_row.iter().enumerate() {
let out_x: usize = x * h_factor;
for dx in 0..h_factor {
output[first_out_row + out_x + dx] = val;
}
}
// Replicate the row vertically
for dy in 1..v_factor {
let src_start: usize = first_out_row;
let dst_start: usize = (out_y_base + dy) * out_stride;
let copy_len: usize = in_width * h_factor;
output.copy_within(src_start..src_start + copy_len, dst_start);
}
}
}
/// Per-component layout info for progressive decoding.
struct CompInfo {
/// Buffer width in blocks (rounded up to MCU alignment: mcus_x * h_samp).
blocks_x: usize,
/// Buffer height in blocks (rounded up to MCU alignment: mcus_y * v_samp).
blocks_y: usize,
h_samp: usize,
v_samp: usize,
comp_w: usize,
block_size: usize,
/// Actual number of encoded block columns for non-interleaved scans.
/// = ceil(image_width * h_samp / (max_h * block_size))
width_in_blocks: usize,
/// Actual number of encoded block rows for non-interleaved scans.
/// = ceil(image_height * v_samp / (max_v * block_size))
height_in_blocks: usize,
}
/// Decoded image data.
#[derive(Debug)]
pub struct Image {
pub width: usize,
pub height: usize,
pub pixel_format: PixelFormat,
pub precision: u8,
pub data: Vec<u8>,
/// Reassembled ICC profile from APP2 markers, if present and valid.
pub icc_profile: Option<Vec<u8>>,
/// Raw EXIF TIFF data from APP1 marker, if present.
pub exif_data: Option<Vec<u8>>,
/// COM marker text, if present.
pub comment: Option<String>,
/// Pixel density from JFIF header.
pub density: DensityInfo,
/// Saved APP/COM markers.
pub saved_markers: Vec<SavedMarker>,
/// Warnings accumulated during lenient decoding.
pub warnings: Vec<DecodeWarning>,
}
impl Image {
/// Returns the ICC color profile embedded in this JPEG, if any.
pub fn icc_profile(&self) -> Option<&[u8]> {
self.icc_profile.as_deref()
}
/// Returns the raw EXIF TIFF data, if present.
pub fn exif_data(&self) -> Option<&[u8]> {
self.exif_data.as_deref()
}
/// Parses and returns the EXIF orientation tag (1-8), if present.
pub fn exif_orientation(&self) -> Option<u8> {
self.exif_data
.as_ref()
.and_then(|d| crate::common::exif::parse_orientation(d))
}
/// Returns all saved markers (APP and COM) collected during decoding.
///
/// Only populated when the decoder was configured with `save_markers()`.
pub fn markers(&self) -> &[SavedMarker] {
&self.saved_markers
}
}
/// JPEG decoder. Orchestrates the full decoding pipeline.
pub struct Decoder<'a> {
metadata: JpegMetadata,
raw_data: &'a [u8],
routines: SimdRoutines,
output_format: Option<PixelFormat>,
scale: ScalingFactor,
lenient: bool,
/// Horizontal crop offset (iMCU-aligned).
crop_x: Option<usize>,
/// Horizontal crop width.
crop_width: Option<usize>,
/// Vertical crop offset in pixels (auto-aligned to MCU boundary).
crop_y: Option<usize>,
/// Vertical crop height in pixels.
crop_height: Option<usize>,
stop_on_warning: bool,
max_pixels: Option<usize>,
max_memory: Option<usize>,
scan_limit: Option<u32>,
/// Fast upsampling toggle.
pub(crate) fast_upsample: bool,
/// Fast DCT toggle.
pub(crate) fast_dct: bool,
/// DCT method for decode.
pub(crate) dct_method: DctMethod,
/// Block smoothing toggle.
pub(crate) block_smoothing: bool,
/// Output colorspace override.
pub(crate) output_colorspace: Option<ColorSpace>,
/// Apply ordered dithering when outputting RGB565.
pub(crate) dither_565: bool,
/// Enable merged upsampling (combined upsample + color convert for H2V1/H2V2).
pub(crate) merged_upsample: bool,
/// Custom marker processor callbacks, keyed by marker code.
#[allow(clippy::type_complexity)]
marker_processors: std::collections::HashMap<u8, Box<dyn Fn(&[u8]) -> Option<Vec<u8>>>>,
}
impl<'a> Decoder<'a> {
pub fn new(data: &'a [u8]) -> Result<Self> {
let mut reader = MarkerReader::new(data);
let mut metadata = reader.read_markers()?;
// MJPEG frames may omit Huffman tables; provide standard defaults
// (JPEG spec section K.3), matching C libjpeg-turbo's std_huff_tables().
Self::fill_default_huffman_tables(&mut metadata);
let routines = simd::detect();
Ok(Self {
metadata,
raw_data: data,
routines,
output_format: None,
scale: ScalingFactor::default(),
lenient: false,
crop_x: None,
crop_width: None,
crop_y: None,
crop_height: None,
stop_on_warning: false,
max_pixels: None,
max_memory: None,
scan_limit: None,
fast_upsample: false,
fast_dct: false,
dct_method: DctMethod::IsLow,
block_smoothing: false,
output_colorspace: None,
dither_565: false,
merged_upsample: false,
marker_processors: std::collections::HashMap::new(),
})
}
/// Fill in standard JPEG Huffman tables when no DHT markers were present.
///
/// MJPEG frames typically omit DHT markers entirely, relying on the decoder
/// to provide the standard tables from JPEG spec section K.3.
/// Only fills when ALL table slots are `None` (no DHT was parsed at all).
fn fill_default_huffman_tables(metadata: &mut JpegMetadata) {
use crate::common::huffman_table::HuffmanTable;
// Only fill defaults if no DHT markers were present at all.
// If any table was defined (even if some slots are empty), respect the
// original DHT data and do not override.
let any_dc = metadata.dc_huffman_tables.iter().any(|t| t.is_some());
let any_ac = metadata.ac_huffman_tables.iter().any(|t| t.is_some());
if any_dc || any_ac {
return;
}
// Standard DC luminance (table 0)
#[rustfmt::skip]
const BITS_DC_LUM: [u8; 17] = [
0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
];
#[rustfmt::skip]
const VALS_DC_LUM: [u8; 12] = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
];
// Standard DC chrominance (table 1)
#[rustfmt::skip]
const BITS_DC_CHR: [u8; 17] = [
0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
];
#[rustfmt::skip]
const VALS_DC_CHR: [u8; 12] = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
];
// Standard AC luminance (table 0)
#[rustfmt::skip]
const BITS_AC_LUM: [u8; 17] = [
0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
];
#[rustfmt::skip]
const VALS_AC_LUM: [u8; 162] = [
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa,
];
// Standard AC chrominance (table 1)
#[rustfmt::skip]
const BITS_AC_CHR: [u8; 17] = [
0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
];
#[rustfmt::skip]
const VALS_AC_CHR: [u8; 162] = [
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa,
];
// Fill missing DC tables
if metadata.dc_huffman_tables[0].is_none() {
if let Ok(tbl) = HuffmanTable::build(&BITS_DC_LUM, &VALS_DC_LUM) {
metadata.dc_huffman_tables[0] = Some(tbl);
}
}
if metadata.dc_huffman_tables[1].is_none() {
if let Ok(tbl) = HuffmanTable::build(&BITS_DC_CHR, &VALS_DC_CHR) {
metadata.dc_huffman_tables[1] = Some(tbl);
}
}
// Fill missing AC tables
if metadata.ac_huffman_tables[0].is_none() {
if let Ok(tbl) = HuffmanTable::build(&BITS_AC_LUM, &VALS_AC_LUM) {
metadata.ac_huffman_tables[0] = Some(tbl);
}
}
if metadata.ac_huffman_tables[1].is_none() {
if let Ok(tbl) = HuffmanTable::build(&BITS_AC_CHR, &VALS_AC_CHR) {
metadata.ac_huffman_tables[1] = Some(tbl);
}
}
// Also fill in ScanInfo Huffman tables for the first scan if needed
for scan in &mut metadata.scans {
for i in 0..4 {
if scan.dc_huffman_tables[i].is_none() && metadata.dc_huffman_tables[i].is_some() {
scan.dc_huffman_tables[i] = metadata.dc_huffman_tables[i].clone();
}
if scan.ac_huffman_tables[i].is_none() && metadata.ac_huffman_tables[i].is_some() {
scan.ac_huffman_tables[i] = metadata.ac_huffman_tables[i].clone();
}
}
}
}
pub fn header(&self) -> &FrameHeader {
&self.metadata.frame
}
/// Set the desired output pixel format.
pub fn set_output_format(&mut self, format: PixelFormat) {
self.output_format = Some(format);
}
/// Set the decompression scaling factor (e.g., 1/2, 1/4, 1/8).
pub fn set_scale(&mut self, scale: ScalingFactor) {
self.scale = scale;
}
/// Enable lenient mode: continue decoding on errors, filling corrupt areas with gray.
pub fn set_lenient(&mut self, lenient: bool) {
self.lenient = lenient;
}
/// Set horizontal crop region. Offsets are auto-aligned to iMCU boundaries.
pub fn set_crop(&mut self, x: usize, width: usize) {
self.crop_x = Some(x);
self.crop_width = Some(width);
}
/// Set full crop region (horizontal + vertical).
/// MCU rows outside the vertical range will skip IDCT during decoding.
pub fn set_crop_region(&mut self, x: usize, y: usize, width: usize, height: usize) {
self.crop_x = Some(x);
self.crop_width = Some(width);
self.crop_y = Some(y);
self.crop_height = Some(height);
}
/// Treat warnings as fatal errors.
pub fn set_stop_on_warning(&mut self, stop: bool) {
self.stop_on_warning = stop;
}
/// Set maximum allowed image size in pixels. Reject images exceeding this.
pub fn set_max_pixels(&mut self, limit: usize) {
self.max_pixels = Some(limit);
}
/// Set maximum memory usage in bytes.
pub fn set_max_memory(&mut self, limit: usize) {
self.max_memory = Some(limit);
}
/// Set maximum number of progressive scans before error.
pub fn set_scan_limit(&mut self, limit: u32) {
self.scan_limit = Some(limit);
}
/// Enable or disable fast (nearest-neighbor) upsampling.
pub fn set_fast_upsample(&mut self, fast: bool) {
self.fast_upsample = fast;
}
/// Enable or disable fast DCT for decoding.
pub fn set_fast_dct(&mut self, fast: bool) {
self.fast_dct = fast;
}
/// Set the DCT/IDCT method for decoding.
pub fn set_dct_method(&mut self, method: DctMethod) {
self.dct_method = method;
}
/// Enable or disable inter-block smoothing.
pub fn set_block_smoothing(&mut self, smooth: bool) {
self.block_smoothing = smooth;
}
/// Override the output color space.
pub fn set_output_colorspace(&mut self, cs: ColorSpace) {
self.output_colorspace = Some(cs);
}
/// Enable or disable ordered dithering for RGB565 output.
///
/// When enabled, applies a 4x4 ordered dither pattern before truncating
/// 8-bit RGB to 5-6-5, reducing visible banding in smooth gradients.
/// Matches libjpeg-turbo's dithered RGB565 output mode.
pub fn set_dither_565(&mut self, dither: bool) {
self.dither_565 = dither;
}
/// Enable merged upsampling optimization (combines upsample + color convert).
///
/// When enabled and subsampling is 4:2:0 or 4:2:2, uses a merged path that
/// performs chroma upsampling and YCbCr->RGB conversion in a single pass.
/// This avoids writing upsampled chroma to intermediate buffers, improving
/// cache behavior. Slightly less accurate than separate fancy upsample
/// because merged uses box-filter (nearest-neighbor) chroma replication.
pub fn set_merged_upsample(&mut self, enabled: bool) {
self.merged_upsample = enabled;
}
/// Configure which markers to save during decoding.
///
/// By default, the decoder only parses known markers (JFIF, ICC, EXIF, Adobe, COM)
/// and discards unknown APP markers. Call this to preserve arbitrary APP/COM markers
/// in the decoded `Image.saved_markers` field.
///
/// This re-parses the JPEG header with the new configuration.
pub fn save_markers(&mut self, config: MarkerSaveConfig) {
let mut reader: MarkerReader<'_> = MarkerReader::new(self.raw_data);
reader.set_marker_save_config(config);
if let Ok(metadata) = reader.read_markers() {
self.metadata = metadata;
}
}
/// Register a custom marker processor callback for a specific marker type.
pub fn set_marker_processor<F>(&mut self, marker_type: u8, processor: F)
where
F: Fn(&[u8]) -> Option<Vec<u8>> + 'static,
{
let has_marker: bool = self
.metadata
.saved_markers
.iter()
.any(|m| m.code == marker_type);
if !has_marker {
let mut reader: MarkerReader<'_> = MarkerReader::new(self.raw_data);
reader.set_marker_save_config(MarkerSaveConfig::Specific(vec![marker_type]));
if let Ok(metadata) = reader.read_markers() {
self.metadata = metadata;
}
}
self.marker_processors
.insert(marker_type, Box::new(processor));
}
pub fn decode(data: &'a [u8]) -> Result<Image> {
let decoder = Self::new(data)?;
decoder.decode_image()
}
pub fn decode_to(data: &'a [u8], format: PixelFormat) -> Result<Image> {
let mut decoder = Self::new(data)?;
decoder.set_output_format(format);
decoder.decode_image()
}
#[inline(always)]
#[allow(dead_code)]
fn idct_islow(&self, coeffs: &[i16; 64], quant: &[u16; 64], output: &mut [u8; 64]) {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::idct::neon_idct_islow(coeffs, quant, output);
}
#[allow(unreachable_code)]
(self.routines.idct_islow)(coeffs, quant, output)
}
/// IDCT writing directly to a strided destination buffer (no intermediate copy).
///
/// # Safety
/// `output` must point to at least `7 * stride + 8` writable bytes.
#[inline(always)]
unsafe fn idct_islow_strided(
&self,
coeffs: &[i16; 64],
quant: &[u16; 64],
output: *mut u8,
stride: usize,
) {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::idct::neon_idct_islow_strided(
coeffs, quant, output, stride,
);
}
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
return crate::simd::x86_64::avx2_idct::avx2_idct_islow_strided(
coeffs, quant, output, stride,
);
}
// SSE2 fallback: IDCT into temp buffer, then copy row-by-row.
let mut tmp = [0u8; 64];
(self.routines.idct_islow)(coeffs, quant, &mut tmp);
for row in 0..8 {
std::ptr::copy_nonoverlapping(
tmp.as_ptr().add(row * 8),
output.add(row * stride),
8,
);
}
return;
}
// Scalar fallback: IDCT into temp buffer, then copy row-by-row.
#[allow(unreachable_code)]
{
let mut tmp = [0u8; 64];
(self.routines.idct_islow)(coeffs, quant, &mut tmp);
for row in 0..8 {
std::ptr::copy_nonoverlapping(
tmp.as_ptr().add(row * 8),
output.add(row * stride),
8,
);
}
}
}
/// Scale-aware IDCT dispatch: picks 8x8, 4x4, 2x2, or 1x1 based on block_size.
///
/// # Safety
/// `output` must point to sufficient writable bytes for the chosen block_size × stride.
#[inline(always)]
unsafe fn idct_scaled_strided(
&self,
coeffs: &[i16; 64],
quant: &[u16; 64],
output: *mut u8,
stride: usize,
block_size: usize,
) {
match block_size {
8 => self.idct_islow_strided(coeffs, quant, output, stride),
4 => idct_scaled::idct_4x4_strided(coeffs, quant, output, stride),
2 => idct_scaled::idct_2x2_strided(coeffs, quant, output, stride),
1 => idct_scaled::idct_1x1_strided(coeffs, quant, output, stride),
_ => unreachable!("invalid block_size: {}", block_size),
}
}
/// Compute per-component IDCT block size for scaled decode.
///
/// Matches C libjpeg-turbo's `jpeg_calc_output_dimensions` (jdmaster.c):
/// chroma components get a larger IDCT to absorb subsampling factors,
/// eliminating spatial upsampling. For example, 4:2:0 at 1/2 scale uses
/// 4x4 IDCT for Y but 8x8 IDCT for Cb/Cr, so all planes end up the same
/// pixel dimensions — no upsample needed.
fn compute_comp_block_size(
min_block_size: usize,
max_h: usize,
max_v: usize,
h_samp: usize,
v_samp: usize,
) -> usize {
let mut ssize: usize = min_block_size;
while ssize < 8
&& (max_h * min_block_size).is_multiple_of(h_samp * ssize * 2)
&& (max_v * min_block_size).is_multiple_of(v_samp * ssize * 2)
{
ssize *= 2;
}
ssize
}
/// Compute per-component block sizes for all components in a frame.
fn compute_all_comp_block_sizes(
min_block_size: usize,
max_h: usize,
max_v: usize,
frame: &FrameHeader,
) -> Vec<usize> {
frame
.components
.iter()
.map(|comp| {
Self::compute_comp_block_size(
min_block_size,
max_h,
max_v,
comp.horizontal_sampling as usize,
comp.vertical_sampling as usize,
)
})
.collect()
}
#[inline(always)]
fn ycbcr_to_rgb_row(&self, y: &[u8], cb: &[u8], cr: &[u8], out: &mut [u8], width: usize) {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::color::neon_ycbcr_to_rgb_row(y, cb, cr, out, width);
}
#[allow(unreachable_code)]
(self.routines.ycbcr_to_rgb_row)(y, cb, cr, out, width)
}
#[inline(always)]
fn ycbcr_to_rgba_row(&self, y: &[u8], cb: &[u8], cr: &[u8], out: &mut [u8], width: usize) {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::color::neon_ycbcr_to_rgba_row(y, cb, cr, out, width);
}
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
return crate::simd::x86_64::avx2_color::avx2_ycbcr_to_rgba_row(
y, cb, cr, out, width,
);
}
}
#[allow(unreachable_code)]
crate::decode::color::ycbcr_to_rgba_row(y, cb, cr, out, width)
}
#[inline(always)]
fn ycbcr_to_bgr_row(&self, y: &[u8], cb: &[u8], cr: &[u8], out: &mut [u8], width: usize) {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::color::neon_ycbcr_to_bgr_row(y, cb, cr, out, width);
}
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
return crate::simd::x86_64::avx2_color::avx2_ycbcr_to_bgr_row(
y, cb, cr, out, width,
);
}
}
#[allow(unreachable_code)]
crate::decode::color::ycbcr_to_bgr_row(y, cb, cr, out, width)
}
#[inline(always)]
fn ycbcr_to_bgra_row(&self, y: &[u8], cb: &[u8], cr: &[u8], out: &mut [u8], width: usize) {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::color::neon_ycbcr_to_bgra_row(y, cb, cr, out, width);
}
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
return crate::simd::x86_64::avx2_color::avx2_ycbcr_to_bgra_row(
y, cb, cr, out, width,
);
}
}
#[allow(unreachable_code)]
crate::decode::color::ycbcr_to_bgra_row(y, cb, cr, out, width)
}
/// Dispatch color conversion for one row based on the target pixel format.
/// `row_index` is the output row number, used for ordered dithering in RGB565 mode.
#[inline(always)]
#[allow(clippy::too_many_arguments)]
fn color_convert_row(
&self,
format: PixelFormat,
y: &[u8],
cb: &[u8],
cr: &[u8],
out: &mut [u8],
width: usize,
row_index: usize,
) {
match format {
PixelFormat::Rgb => self.ycbcr_to_rgb_row(y, cb, cr, out, width),
PixelFormat::Rgba => self.ycbcr_to_rgba_row(y, cb, cr, out, width),
PixelFormat::Bgr => self.ycbcr_to_bgr_row(y, cb, cr, out, width),
PixelFormat::Bgra => self.ycbcr_to_bgra_row(y, cb, cr, out, width),
#[allow(unreachable_code)]
PixelFormat::Rgbx => {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::color::neon_ycbcr_to_rgbx_row(
y, cb, cr, out, width,
);
}
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
return crate::simd::x86_64::avx2_color::avx2_ycbcr_to_rgbx_row(
y, cb, cr, out, width,
);
}
}
crate::decode::color::ycbcr_to_generic_4bpp_row(y, cb, cr, out, width, 0, 1, 2, 3)
}
#[allow(unreachable_code)]
PixelFormat::Bgrx => {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::color::neon_ycbcr_to_bgrx_row(
y, cb, cr, out, width,
);
}
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
return crate::simd::x86_64::avx2_color::avx2_ycbcr_to_bgrx_row(
y, cb, cr, out, width,
);
}
}
crate::decode::color::ycbcr_to_generic_4bpp_row(y, cb, cr, out, width, 2, 1, 0, 3)
}
#[allow(unreachable_code)]
PixelFormat::Xrgb => {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::color::neon_ycbcr_to_xrgb_row(
y, cb, cr, out, width,
);
}
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
return crate::simd::x86_64::avx2_color::avx2_ycbcr_to_xrgb_row(
y, cb, cr, out, width,
);
}
}
crate::decode::color::ycbcr_to_generic_4bpp_row(y, cb, cr, out, width, 1, 2, 3, 0)
}
#[allow(unreachable_code)]
PixelFormat::Xbgr => {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::color::neon_ycbcr_to_xbgr_row(
y, cb, cr, out, width,
);
}
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
return crate::simd::x86_64::avx2_color::avx2_ycbcr_to_xbgr_row(
y, cb, cr, out, width,
);
}
}
crate::decode::color::ycbcr_to_generic_4bpp_row(y, cb, cr, out, width, 3, 2, 1, 0)
}
#[allow(unreachable_code)]
PixelFormat::Argb => {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::color::neon_ycbcr_to_argb_row(
y, cb, cr, out, width,
);
}
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
return crate::simd::x86_64::avx2_color::avx2_ycbcr_to_argb_row(
y, cb, cr, out, width,
);
}
}
crate::decode::color::ycbcr_to_generic_4bpp_row(y, cb, cr, out, width, 1, 2, 3, 0)
}
#[allow(unreachable_code)]
PixelFormat::Abgr => {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::color::neon_ycbcr_to_abgr_row(
y, cb, cr, out, width,
);
}
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
return crate::simd::x86_64::avx2_color::avx2_ycbcr_to_abgr_row(
y, cb, cr, out, width,
);
}
}
crate::decode::color::ycbcr_to_generic_4bpp_row(y, cb, cr, out, width, 3, 2, 1, 0)
}
PixelFormat::Rgb565 => {
if self.dither_565 {
crate::decode::color::ycbcr_to_rgb565_dithered_row(
y, cb, cr, out, width, row_index,
)
} else {
crate::decode::color::ycbcr_to_rgb565_row(y, cb, cr, out, width)
}
}
PixelFormat::Grayscale | PixelFormat::Cmyk => {
unreachable!("grayscale/cmyk handled separately")
}
}
}
/// Merged H2V1 upsample + color convert dispatch.
#[inline(always)]
fn merged_h2v1(y_row: &[u8], cb_row: &[u8], cr_row: &[u8], rgb_out: &mut [u8], width: usize) {
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
crate::simd::x86_64::avx2_merged::avx2_merged_h2v1_ycbcr_to_rgb(
y_row, cb_row, cr_row, rgb_out, width,
);
return;
}
}
crate::decode::merged_upsample::merged_h2v1_ycbcr_to_rgb(
y_row, cb_row, cr_row, rgb_out, width,
);
}
/// Merged H2V2 upsample + color convert dispatch.
#[inline(always)]
fn merged_h2v2(
y_row0: &[u8],
y_row1: &[u8],
cb_row: &[u8],
cr_row: &[u8],
rgb_out0: &mut [u8],
rgb_out1: &mut [u8],
width: usize,
) {
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
{
if is_x86_feature_detected!("avx2") {
crate::simd::x86_64::avx2_merged::avx2_merged_h2v2_ycbcr_to_rgb(
y_row0, y_row1, cb_row, cr_row, rgb_out0, rgb_out1, width,
);
return;
}
}
crate::decode::merged_upsample::merged_h2v2_ycbcr_to_rgb(
y_row0, y_row1, cb_row, cr_row, rgb_out0, rgb_out1, width,
);
}
#[inline(always)]
fn fancy_upsample_h2v1(&self, input: &[u8], in_width: usize, output: &mut [u8]) {
// For in_width <= 2, C's merged path uses box filter (no interpolation).
// NEON/SIMD paths may not handle this edge case correctly, so use scalar.
if in_width <= 2 {
crate::decode::upsample::fancy_h2v1(input, in_width, output, 0);
return;
}
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
return crate::simd::aarch64::upsample::neon_fancy_upsample_h2v1(
input, in_width, output,
);
}
#[allow(unreachable_code)]
(self.routines.fancy_upsample_h2v1)(input, in_width, output)
}
/// Fancy h2v2 upsample. On aarch64 this uses a dedicated helper that
/// fuses the two vertical blends into one pass before the h2v1 stage.
fn fancy_h2v2(
&self,
input: &[u8],
in_width: usize,
in_height: usize,
output: &mut [u8],
out_width: usize,
) {
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
{
crate::simd::aarch64::upsample::neon_fancy_upsample_h2v2(
input, in_width, in_height, output, out_width,
)
}
// Fused H2V2: vertical + horizontal in one pass using >> 4 arithmetic.
// Matches C libjpeg-turbo h2v2_fancy_upsample exactly, avoiding
// double-rounding from the previous two-pass approach.
#[allow(unreachable_code)]
{
crate::decode::upsample::fancy_h2v2(
input,
in_width,
in_height,
output,
out_width,
in_height * 2,
);
}
}
/// Fancy h1v2 upsample: vertical-only 2x (for S440).
/// Each input row produces two output rows using triangle filter vertically.
/// Horizontal samples are copied 1:1.
fn fancy_h1v2(
&self,
input: &[u8],
in_width: usize,
in_height: usize,
output: &mut [u8],
out_width: usize,
) {
for y in 0..in_height {
let cur_row = &input[y * in_width..(y + 1) * in_width];
let above = if y > 0 {
&input[(y - 1) * in_width..y * in_width]
} else {
cur_row
};
let below = if y + 1 < in_height {
&input[(y + 1) * in_width..(y + 2) * in_width]
} else {
cur_row
};
let out_y_top = y * 2;
let out_y_bot = y * 2 + 1;
// split_at_mut to get non-overlapping mutable slices
let (top_half, bot_half) = output.split_at_mut(out_y_bot * out_width);
let out_top = &mut top_half[out_y_top * out_width..out_y_top * out_width + in_width];
let out_bot = &mut bot_half[..in_width];
// Vertical triangle filter with ordered dither to avoid systematic
// rounding bias (matches C jdsample.c h1v2_fancy_upsample):
// top row: bias=1, bottom row: bias=2
for i in 0..in_width {
out_top[i] = ((3 * cur_row[i] as u16 + above[i] as u16 + 1) >> 2) as u8;
out_bot[i] = ((3 * cur_row[i] as u16 + below[i] as u16 + 2) >> 2) as u8;
}
}
}
/// Decode baseline (single-scan) into component planes.
/// Returns component planes and any warnings (in lenient mode).
/// `mcu_row_range`: optional (start, end) MCU row range for IDCT skip optimization.
/// When set, only MCU rows in [start, end) get IDCT; planes are sized for this range only.
fn decode_baseline_planes(
&self,
frame: &FrameHeader,
quant_tables: &[&QuantTable],
num_components: usize,
mcus_x: usize,
mcus_y: usize,
comp_block_sizes: &[usize],
) -> Result<(Vec<Vec<u8>>, Vec<DecodeWarning>)> {
// Non-interleaved baseline: multiple SOS markers, each with a single component.
// Dispatch to dedicated multi-scan path.
if self.metadata.scans.len() > 1 {
return self.decode_non_interleaved_baseline_planes(
frame,
quant_tables,
num_components,
mcus_x,
mcus_y,
comp_block_sizes,
);
}
let scan = &self.metadata.scan;
let block_size: usize = comp_block_sizes[0]; // min (luma) block size for MCU row range
// Determine MCU row range for IDCT
let (mcu_y_start, mcu_y_end) = self.mcu_row_range(mcus_y, block_size, frame);
// Allocate component planes (full MCU-aligned size, uninitialized).
// SAFETY: The MCU decode loop + IDCT writes every pixel before reading.
#[allow(clippy::uninit_vec)]
let mut component_planes: Vec<Vec<u8>> = frame
.components
.iter()
.enumerate()
.map(|(ci, comp)| {
let comp_w = mcus_x * comp.horizontal_sampling as usize * comp_block_sizes[ci];
let comp_h = mcus_y * comp.vertical_sampling as usize * comp_block_sizes[ci];
let size: usize = comp_w * comp_h;
let mut v: Vec<u8> = Vec::with_capacity(size);
unsafe { v.set_len(size) };
v
})
.collect();
let mcu_plan = entropy::resolve_mcu_plan(
frame,
scan,
&self.metadata.dc_huffman_tables,
&self.metadata.ac_huffman_tables,
)?;
struct CompLayout {
comp_w: usize,
h_blocks: usize,
v_blocks: usize,
block_size: usize,
}
let comp_layouts: Vec<CompLayout> = frame
.components
.iter()
.enumerate()
.map(|(ci, comp)| CompLayout {
comp_w: mcus_x * comp.horizontal_sampling as usize * comp_block_sizes[ci],
h_blocks: comp.horizontal_sampling as usize,
v_blocks: comp.vertical_sampling as usize,
block_size: comp_block_sizes[ci],
})
.collect();
let entropy_data = &self.raw_data[self.metadata.entropy_data_offset..];
let mut bit_reader = BitReader::new(entropy_data);
let mut mcu_decoder = McuDecoder::new(num_components);
let mut mcu_count: u32 = 0;
let mut coeffs = [0i16; 64];
let mut warnings: Vec<DecodeWarning> = Vec::new();
let total_mcus = mcus_x * mcus_y;
// Fast path: non-lenient, no cropping — tight loop with minimal branching.
// The lenient/crop path is below with full error recovery support.
if !self.lenient && mcu_y_start == 0 && mcu_y_end == mcus_y {
let restart_interval: u32 = self.metadata.restart_interval as u32;
for mcu_y in 0..mcus_y {
for mcu_x in 0..mcus_x {
if restart_interval > 0
&& mcu_count > 0
&& mcu_count.is_multiple_of(restart_interval)
{
bit_reader.reset();
mcu_decoder.reset();
}
for (comp_idx, layout) in comp_layouts.iter().enumerate() {
let qt_values: &[u16; 64] = &quant_tables[comp_idx].values;
let plan = &mcu_plan[comp_idx];
for v in 0..layout.v_blocks {
for h in 0..layout.h_blocks {
mcu_decoder.decode_block(
&mut bit_reader,
plan.comp_idx,
plan.dc_table,
plan.ac_table,
&mut coeffs,
)?;
let bs: usize = layout.block_size;
let block_x: usize = (mcu_x * layout.h_blocks + h) * bs;
let block_y: usize = (mcu_y * layout.v_blocks + v) * bs;
let dst_offset: usize = block_y * layout.comp_w + block_x;
unsafe {
let dst: *mut u8 =
component_planes[comp_idx].as_mut_ptr().add(dst_offset);
self.idct_scaled_strided(
&coeffs,
qt_values,
dst,
layout.comp_w,
bs,
);
}
}
}
}
mcu_count += 1;
if bit_reader.is_eof() && (mcu_count as usize) < total_mcus {
return Err(JpegError::UnexpectedEof);
}
}
}
} else {
// General path: lenient mode with error recovery + crop support
'mcu_loop: for mcu_y in 0..mcus_y {
for mcu_x in 0..mcus_x {
if self.metadata.restart_interval > 0
&& mcu_count > 0
&& mcu_count.is_multiple_of(self.metadata.restart_interval as u32)
{
bit_reader.reset();
mcu_decoder.reset();
}
let mut mcu_error = false;
for (comp_idx, layout) in comp_layouts.iter().enumerate() {
let qt_values = &quant_tables[comp_idx].values;
let plan = &mcu_plan[comp_idx];
for v in 0..layout.v_blocks {
for h in 0..layout.h_blocks {
let decode_result = mcu_decoder.decode_block(
&mut bit_reader,
plan.comp_idx,
plan.dc_table,
plan.ac_table,
&mut coeffs,
);
match decode_result {
Ok(()) => {}
Err(e) if self.lenient => {
coeffs = [0i16; 64];
if !mcu_error {
warnings.push(DecodeWarning::HuffmanError {
mcu_x,
mcu_y,
message: e.to_string(),
});
mcu_error = true;
}
if matches!(e, JpegError::UnexpectedEof) {
warnings.push(DecodeWarning::TruncatedData {
decoded_mcus: mcu_count as usize,
total_mcus,
});
for plane in &mut component_planes {
plane.fill(128);
}
break 'mcu_loop;
}
mcu_decoder.reset();
}
Err(e) => return Err(e),
}
if mcu_y >= mcu_y_start && mcu_y < mcu_y_end {
let bs: usize = layout.block_size;
let block_x: usize = (mcu_x * layout.h_blocks + h) * bs;
let block_y: usize = (mcu_y * layout.v_blocks + v) * bs;
let dst_offset: usize = block_y * layout.comp_w + block_x;
unsafe {
let dst: *mut u8 =
component_planes[comp_idx].as_mut_ptr().add(dst_offset);
self.idct_scaled_strided(
&coeffs,
qt_values,
dst,
layout.comp_w,
bs,
);
}
}
}
}
}
mcu_count += 1;
if bit_reader.is_eof() && (mcu_count as usize) < total_mcus {
if self.lenient {
warnings.push(DecodeWarning::TruncatedData {
decoded_mcus: mcu_count as usize,
total_mcus,
});
break 'mcu_loop;
} else {
return Err(JpegError::UnexpectedEof);
}
}
}
}
}
Ok((component_planes, warnings))
}
/// Decode non-interleaved baseline JPEG (multiple SOS markers, one component per scan).
///
/// Each SOS contains a single component with full DC+AC coefficients (ss=0, se=63).
/// The MCU for a non-interleaved scan is a single 8x8 block, and blocks are
/// iterated in raster order: blocks_x * blocks_y total blocks per scan.
#[allow(clippy::too_many_arguments)]
fn decode_non_interleaved_baseline_planes(
&self,
frame: &FrameHeader,
quant_tables: &[&QuantTable],
_num_components: usize,
mcus_x: usize,
mcus_y: usize,
comp_block_sizes: &[usize],
) -> Result<(Vec<Vec<u8>>, Vec<DecodeWarning>)> {
// Allocate component planes (full MCU-aligned size, zero-initialized).
// Zero-init is needed because non-interleaved scans may encode fewer
// blocks than the plane holds (padding blocks at right/bottom edges).
let mut component_planes: Vec<Vec<u8>> = frame
.components
.iter()
.enumerate()
.map(|(ci, comp)| {
let comp_w: usize =
mcus_x * comp.horizontal_sampling as usize * comp_block_sizes[ci];
let comp_h: usize = mcus_y * comp.vertical_sampling as usize * comp_block_sizes[ci];
let size: usize = comp_w * comp_h;
vec![0u8; size]
})
.collect();
// Process each scan independently
for scan_info in &self.metadata.scans {
let scan = &scan_info.header;
// Each non-interleaved scan should have exactly 1 component
if scan.components.len() != 1 {
return Err(JpegError::CorruptData(format!(
"non-interleaved baseline scan has {} components, expected 1",
scan.components.len()
)));
}
let scan_comp = &scan.components[0];
// Find the frame component index for this scan's component
let comp_idx: usize = frame
.components
.iter()
.position(|fc| fc.id == scan_comp.component_id)
.ok_or_else(|| {
JpegError::CorruptData(format!(
"scan references unknown component id {}",
scan_comp.component_id
))
})?;
let comp = &frame.components[comp_idx];
let h_samp: usize = comp.horizontal_sampling as usize;
let v_samp: usize = comp.vertical_sampling as usize;
let max_h: usize = frame
.components
.iter()
.map(|c| c.horizontal_sampling as usize)
.max()
.unwrap_or(1);
let max_v: usize = frame
.components
.iter()
.map(|c| c.vertical_sampling as usize)
.max()
.unwrap_or(1);
// For non-interleaved scans, the number of encoded blocks is based on
// the component's actual sample dimensions (JPEG spec ITU T.81 A.2.3):
// comp_samples = ceil(image_dim * h_samp / max_h)
// encoded_blocks = ceil(comp_samples / 8)
let comp_width_samples: usize = (frame.width as usize * h_samp).div_ceil(max_h);
let comp_height_samples: usize = (frame.height as usize * v_samp).div_ceil(max_v);
let encoded_blocks_x: usize = comp_width_samples.div_ceil(8);
let encoded_blocks_y: usize = comp_height_samples.div_ceil(8);
// The plane is allocated based on the interleaved MCU grid,
// which may have more blocks than the encoded data.
let plane_blocks_x: usize = mcus_x * h_samp;
let bs: usize = comp_block_sizes[comp_idx];
let comp_w: usize = plane_blocks_x * bs;
// Resolve Huffman tables for this scan
let dc_table: &HuffmanTable =
Self::resolve_table(&scan_info.dc_huffman_tables, scan_comp.dc_table_index, "DC")?;
let ac_table: &HuffmanTable =
Self::resolve_table(&scan_info.ac_huffman_tables, scan_comp.ac_table_index, "AC")?;
let qt_values: &[u16; 64] = &quant_tables[comp_idx].values;
let entropy_data: &[u8] = &self.raw_data[scan_info.data_offset..];
let mut bit_reader: BitReader = BitReader::new(entropy_data);
// Fresh DC prediction per scan (each non-interleaved scan starts at 0)
let mut mcu_decoder: McuDecoder = McuDecoder::new(frame.components.len());
let mut coeffs: [i16; 64] = [0i16; 64];
let restart_interval: u32 = scan_info.restart_interval as u32;
let mut mcu_count: u32 = 0;
// In a non-interleaved scan, each MCU is a single block.
// Iterate over encoded blocks (may be fewer than plane blocks
// when image dimensions don't align with the MCU grid).
for by in 0..encoded_blocks_y {
for bx in 0..encoded_blocks_x {
// Restart interval handling
if restart_interval > 0
&& mcu_count > 0
&& mcu_count.is_multiple_of(restart_interval)
{
bit_reader.reset();
mcu_decoder.reset();
}
// Decode one 8x8 block
mcu_decoder.decode_block(
&mut bit_reader,
comp_idx,
dc_table,
ac_table,
&mut coeffs,
)?;
// IDCT and store into the component plane
let block_x: usize = bx * bs;
let block_y: usize = by * bs;
let dst_offset: usize = block_y * comp_w + block_x;
unsafe {
let dst: *mut u8 = component_planes[comp_idx].as_mut_ptr().add(dst_offset);
self.idct_scaled_strided(&coeffs, qt_values, dst, comp_w, bs);
}
mcu_count += 1;
}
}
}
Ok((component_planes, Vec::new()))
}
/// Decode arithmetic-coded planes (SOF9 sequential).
fn decode_arithmetic_planes(
&self,
frame: &FrameHeader,
quant_tables: &[&QuantTable],
_num_components: usize,
mcus_x: usize,
mcus_y: usize,
comp_block_sizes: &[usize],
) -> Result<(Vec<Vec<u8>>, Vec<DecodeWarning>)> {
use crate::decode::arithmetic::ArithDecoder;
let scan = &self.metadata.scan;
// Allocate component planes
#[allow(clippy::uninit_vec)]
let mut component_planes: Vec<Vec<u8>> = frame
.components
.iter()
.enumerate()
.map(|(ci, comp)| {
let comp_w = mcus_x * comp.horizontal_sampling as usize * comp_block_sizes[ci];
let comp_h = mcus_y * comp.vertical_sampling as usize * comp_block_sizes[ci];
let size = comp_w * comp_h;
let mut v = Vec::with_capacity(size);
unsafe { v.set_len(size) };
v
})
.collect();
struct CompLayout {
comp_w: usize,
h_blocks: usize,
v_blocks: usize,
block_size: usize,
}
let comp_layouts: Vec<CompLayout> = frame
.components
.iter()
.enumerate()
.map(|(ci, comp)| CompLayout {
comp_w: mcus_x * comp.horizontal_sampling as usize * comp_block_sizes[ci],
h_blocks: comp.horizontal_sampling as usize,
v_blocks: comp.vertical_sampling as usize,
block_size: comp_block_sizes[ci],
})
.collect();
// Build component map from scan selectors
let scan_comps: Vec<(usize, usize, usize)> = scan
.components
.iter()
.map(|sc| {
let comp_idx = frame
.components
.iter()
.position(|fc| fc.id == sc.component_id)
.unwrap_or(0);
(
comp_idx,
sc.dc_table_index as usize,
sc.ac_table_index as usize,
)
})
.collect();
let entropy_data = &self.raw_data[self.metadata.entropy_data_offset..];
let mut arith = ArithDecoder::new(entropy_data, 0);
// Set conditioning parameters
for i in 0..4 {
let (l, u) = self.metadata.arith_dc_params[i];
arith.set_dc_conditioning(i, l, u);
arith.set_ac_conditioning(i, self.metadata.arith_ac_params[i]);
}
let mut coeffs: [i16; 64];
for mcu_y in 0..mcus_y {
for mcu_x in 0..mcus_x {
for &(comp_idx, dc_tbl, ac_tbl) in &scan_comps {
let layout = &comp_layouts[comp_idx];
let qt_values = &quant_tables[comp_idx].values;
for v in 0..layout.v_blocks {
for h in 0..layout.h_blocks {
coeffs = [0i16; 64];
// Arithmetic decode DC + AC
arith.decode_dc_sequential(&mut coeffs, comp_idx, dc_tbl)?;
arith.decode_ac_sequential(&mut coeffs, ac_tbl)?;
// IDCT
let bs: usize = layout.block_size;
let bx = mcu_x * layout.h_blocks + h;
let by = mcu_y * layout.v_blocks + v;
let x_offset = bx * bs;
let y_offset = by * bs;
let plane = &mut component_planes[comp_idx];
let stride = layout.comp_w;
unsafe {
let out_ptr = plane.as_mut_ptr().add(y_offset * stride + x_offset);
self.idct_scaled_strided(&coeffs, qt_values, out_ptr, stride, bs);
}
}
}
}
}
}
Ok((component_planes, Vec::new()))
}
/// Decode arithmetic progressive (SOF10) into component planes.
/// Accumulates DCT coefficients across all scans using ArithDecoder, then runs IDCT.
#[allow(clippy::too_many_arguments)]
fn decode_arithmetic_progressive_planes(
&self,
frame: &FrameHeader,
quant_tables: &[&QuantTable],
_num_components: usize,
mcus_x: usize,
mcus_y: usize,
max_h: usize,
max_v: usize,
comp_block_sizes: &[usize],
) -> Result<(Vec<Vec<u8>>, Vec<DecodeWarning>)> {
use crate::decode::arithmetic::ArithDecoder;
let img_w = frame.width as usize;
let img_h = frame.height as usize;
let dct_size: usize = 8;
// Per-component coefficient buffers
let comp_infos: Vec<CompInfo> = frame
.components
.iter()
.enumerate()
.map(|(ci, comp)| {
let h_samp = comp.horizontal_sampling as usize;
let v_samp = comp.vertical_sampling as usize;
let bs = comp_block_sizes[ci];
CompInfo {
blocks_x: mcus_x * h_samp,
blocks_y: mcus_y * v_samp,
h_samp,
v_samp,
comp_w: mcus_x * h_samp * bs,
block_size: bs,
width_in_blocks: (img_w * h_samp).div_ceil(max_h * dct_size),
height_in_blocks: (img_h * v_samp).div_ceil(max_v * dct_size),
}
})
.collect();
// Allocate coefficient buffers (zero-initialized for progressive accumulation)
let mut coeff_bufs: Vec<Vec<[i16; 64]>> = comp_infos
.iter()
.map(|ci| vec![[0i16; 64]; ci.blocks_x * ci.blocks_y])
.collect();
// Process each scan, enforcing scan_limit if set
for (scan_idx, scan_info) in self.metadata.scans.iter().enumerate() {
if let Some(limit) = self.scan_limit {
if scan_idx as u32 >= limit {
return Err(JpegError::Unsupported(format!(
"progressive scan count {} exceeds limit of {}",
scan_idx + 1,
limit
)));
}
}
let scan_header = &scan_info.header;
let is_dc = scan_header.spec_start == 0 && scan_header.spec_end == 0;
let ah = scan_header.succ_high;
let al = scan_header.succ_low;
let ss = scan_header.spec_start;
let se = scan_header.spec_end;
let entropy_data = &self.raw_data[scan_info.data_offset..];
let mut arith = ArithDecoder::new(entropy_data, 0);
// Set conditioning parameters from DAC markers
for i in 0..4 {
let (l, u) = self.metadata.arith_dc_params[i];
arith.set_dc_conditioning(i, l, u);
arith.set_ac_conditioning(i, self.metadata.arith_ac_params[i]);
}
// Resolve component indices for this scan
let scan_comp_indices: Vec<usize> = scan_header
.components
.iter()
.map(|sc| {
frame
.components
.iter()
.position(|fc| fc.id == sc.component_id)
.ok_or_else(|| {
JpegError::CorruptData(format!(
"scan references unknown component {}",
sc.component_id
))
})
})
.collect::<Result<Vec<_>>>()?;
if scan_header.components.len() > 1 {
// Interleaved scan (DC only in progressive)
for mcu_y in 0..mcus_y {
for mcu_x in 0..mcus_x {
for (si, &comp_idx) in scan_comp_indices.iter().enumerate() {
let ci = &comp_infos[comp_idx];
let scan_comp = &scan_header.components[si];
let dc_tbl = scan_comp.dc_table_index as usize;
for v in 0..ci.v_samp {
for h in 0..ci.h_samp {
let bx = mcu_x * ci.h_samp + h;
let by = mcu_y * ci.v_samp + v;
let block_idx = by * ci.blocks_x + bx;
let coeffs = &mut coeff_bufs[comp_idx][block_idx];
if is_dc && ah == 0 {
arith.decode_dc_first_progressive(
coeffs, comp_idx, dc_tbl, al,
)?;
} else if is_dc {
arith.decode_dc_refine_progressive(coeffs, al)?;
}
}
}
}
}
}
} else {
// Non-interleaved scan (single component)
let comp_idx = scan_comp_indices[0];
let scan_comp = &scan_header.components[0];
let dc_tbl = scan_comp.dc_table_index as usize;
let ac_tbl = scan_comp.ac_table_index as usize;
let ci = &comp_infos[comp_idx];
let scan_bx = ci.width_in_blocks;
let scan_by = ci.height_in_blocks;
let stride = ci.blocks_x;
for by in 0..scan_by {
for bx in 0..scan_bx {
let block_idx = by * stride + bx;
let coeffs = &mut coeff_bufs[comp_idx][block_idx];
if is_dc && ah == 0 {
arith.decode_dc_first_progressive(coeffs, comp_idx, dc_tbl, al)?;
} else if is_dc {
arith.decode_dc_refine_progressive(coeffs, al)?;
} else if ah == 0 {
arith.decode_ac_first_progressive(coeffs, ac_tbl, ss, se, al)?;
} else {
arith.decode_ac_refine_progressive(coeffs, ac_tbl, ss, se, al)?;
}
}
}
}
}
// IDCT all blocks into component planes
#[allow(clippy::uninit_vec)]
let mut component_planes: Vec<Vec<u8>> = comp_infos
.iter()
.map(|ci| {
let size = ci.comp_w * ci.blocks_y * ci.block_size;
let mut v = Vec::with_capacity(size);
unsafe { v.set_len(size) };
v
})
.collect();
for (comp_idx, ci) in comp_infos.iter().enumerate() {
let qt_values = &quant_tables[comp_idx].values;
let bs: usize = ci.block_size;
for by in 0..ci.blocks_y {
for bx in 0..ci.blocks_x {
let block_idx = by * ci.blocks_x + bx;
let coeffs = &coeff_bufs[comp_idx][block_idx];
let px_x = bx * bs;
let px_y = by * bs;
let dst_offset = px_y * ci.comp_w + px_x;
unsafe {
let dst = component_planes[comp_idx].as_mut_ptr().add(dst_offset);
self.idct_scaled_strided(coeffs, qt_values, dst, ci.comp_w, bs);
}
}
}
}
Ok((component_planes, Vec::new()))
}
/// Decode progressive (multi-scan) into component planes.
/// Accumulates DCT coefficients across all scans, then runs IDCT.
#[allow(clippy::too_many_arguments)]
fn decode_progressive_planes(
&self,
frame: &FrameHeader,
quant_tables: &[&QuantTable],
_num_components: usize,
mcus_x: usize,
mcus_y: usize,
max_h: usize,
max_v: usize,
comp_block_sizes: &[usize],
) -> Result<(Vec<Vec<u8>>, Vec<DecodeWarning>)> {
let img_w = frame.width as usize;
let img_h = frame.height as usize;
// Per-component coefficient buffers: blocks_x * blocks_y blocks of 64 coefficients.
// width_in_blocks/height_in_blocks use DCT block size (8), not the scaled
// output block size, because coefficient buffers are indexed by 8x8 DCT blocks.
let dct_size: usize = 8;
let comp_infos: Vec<CompInfo> = frame
.components
.iter()
.enumerate()
.map(|(ci, comp)| {
let h_samp = comp.horizontal_sampling as usize;
let v_samp = comp.vertical_sampling as usize;
let bs = comp_block_sizes[ci];
CompInfo {
blocks_x: mcus_x * h_samp,
blocks_y: mcus_y * v_samp,
h_samp,
v_samp,
comp_w: mcus_x * h_samp * bs,
block_size: bs,
width_in_blocks: (img_w * h_samp).div_ceil(max_h * dct_size),
height_in_blocks: (img_h * v_samp).div_ceil(max_v * dct_size),
}
})
.collect();
// Allocate coefficient buffers (zero-initialized for progressive accumulation)
let mut coeff_bufs: Vec<Vec<[i16; 64]>> = comp_infos
.iter()
.map(|ci| vec![[0i16; 64]; ci.blocks_x * ci.blocks_y])
.collect();
// Process each scan, enforcing scan_limit if set
for (scan_idx, scan_info) in self.metadata.scans.iter().enumerate() {
if let Some(limit) = self.scan_limit {
if scan_idx as u32 >= limit {
return Err(JpegError::Unsupported(format!(
"progressive scan count {} exceeds limit of {}",
scan_idx + 1,
limit
)));
}
}
self.decode_progressive_scan(
frame,
scan_info,
&comp_infos,
&mut coeff_bufs,
mcus_x,
mcus_y,
max_h,
max_v,
)?;
}
// IDCT all blocks into component planes
#[allow(clippy::uninit_vec)]
let mut component_planes: Vec<Vec<u8>> = comp_infos
.iter()
.map(|ci| {
let size = ci.comp_w * ci.blocks_y * ci.block_size;
let mut v = Vec::with_capacity(size);
unsafe { v.set_len(size) };
v
})
.collect();
for (comp_idx, ci) in comp_infos.iter().enumerate() {
let qt_values = &quant_tables[comp_idx].values;
let bs: usize = ci.block_size;
for by in 0..ci.blocks_y {
for bx in 0..ci.blocks_x {
let block_idx = by * ci.blocks_x + bx;
let coeffs = &coeff_bufs[comp_idx][block_idx];
let px_x = bx * bs;
let px_y = by * bs;
let dst_offset = px_y * ci.comp_w + px_x;
unsafe {
let dst = component_planes[comp_idx].as_mut_ptr().add(dst_offset);
self.idct_scaled_strided(coeffs, qt_values, dst, ci.comp_w, bs);
}
}
}
}
// Progressive decoding doesn't have per-MCU error recovery yet;
// errors in scans propagate normally.
Ok((component_planes, Vec::new()))
}
/// Decode one progressive scan's entropy data into the coefficient buffers.
#[allow(clippy::too_many_arguments)]
fn decode_progressive_scan(
&self,
frame: &FrameHeader,
scan_info: &ScanInfo,
comp_infos: &[CompInfo],
coeff_bufs: &mut [Vec<[i16; 64]>],
mcus_x: usize,
mcus_y: usize,
max_h: usize,
max_v: usize,
) -> Result<()> {
let scan = &scan_info.header;
let ss = scan.spec_start;
let se = scan.spec_end;
let ah = scan.succ_high;
let al = scan.succ_low;
let is_dc = ss == 0 && se == 0;
let entropy_data = &self.raw_data[scan_info.data_offset..];
let mut bit_reader = BitReader::new(entropy_data);
// Resolve component indices for this scan
let scan_comp_indices: Vec<usize> = scan
.components
.iter()
.map(|sc| {
frame
.components
.iter()
.position(|fc| fc.id == sc.component_id)
.ok_or_else(|| {
JpegError::CorruptData(format!(
"scan references unknown component {}",
sc.component_id
))
})
})
.collect::<Result<Vec<_>>>()?;
if scan.components.len() > 1 {
// Interleaved scan (DC only in progressive)
self.decode_progressive_interleaved(
scan_info,
&scan_comp_indices,
comp_infos,
coeff_bufs,
&mut bit_reader,
mcus_x,
mcus_y,
is_dc,
ss,
se,
ah,
al,
)
} else {
// Non-interleaved scan (single component)
let comp_idx = scan_comp_indices[0];
let scan_comp = &scan.components[0];
self.decode_progressive_non_interleaved(
scan_info,
scan_comp,
comp_idx,
comp_infos,
coeff_bufs,
&mut bit_reader,
mcus_x,
mcus_y,
max_h,
max_v,
is_dc,
ss,
se,
ah,
al,
)
}
}
/// Decode an interleaved progressive scan (multiple components, DC only).
#[allow(clippy::too_many_arguments)]
fn decode_progressive_interleaved(
&self,
scan_info: &ScanInfo,
scan_comp_indices: &[usize],
comp_infos: &[CompInfo],
coeff_bufs: &mut [Vec<[i16; 64]>],
bit_reader: &mut BitReader,
mcus_x: usize,
mcus_y: usize,
is_dc: bool,
_ss: u8,
_se: u8,
ah: u8,
al: u8,
) -> Result<()> {
let scan = &scan_info.header;
let mut dc_preds = [0i16; 4];
// Pre-resolve Huffman tables outside the MCU loop
let dc_tables: Vec<&HuffmanTable> = scan
.components
.iter()
.map(|sc| Self::resolve_table(&scan_info.dc_huffman_tables, sc.dc_table_index, "DC"))
.collect::<Result<Vec<_>>>()?;
// Use countdown for restart interval to avoid modulo in hot loop
let restart_interval = scan_info.restart_interval as u32;
// Start at restart_interval so the first MCU doesn't trigger a reset.
// When restart_interval is 0, countdown is never checked.
let mut restart_countdown: u32 = restart_interval;
for mcu_y in 0..mcus_y {
for mcu_x in 0..mcus_x {
if restart_interval > 0 {
if restart_countdown == 0 {
bit_reader.reset();
dc_preds = [0i16; 4];
restart_countdown = restart_interval;
}
restart_countdown -= 1;
}
for (si, &comp_idx) in scan_comp_indices.iter().enumerate() {
let ci = &comp_infos[comp_idx];
let dc_table = dc_tables[si];
for v in 0..ci.v_samp {
for h in 0..ci.h_samp {
let bx = mcu_x * ci.h_samp + h;
let by = mcu_y * ci.v_samp + v;
let block_idx = by * ci.blocks_x + bx;
let coeffs = &mut coeff_bufs[comp_idx][block_idx];
if is_dc {
if ah == 0 {
progressive::decode_dc_first(
bit_reader,
dc_table,
&mut dc_preds[comp_idx],
coeffs,
al,
)?;
} else {
progressive::decode_dc_refine(bit_reader, coeffs, al)?;
}
}
}
}
}
}
}
Ok(())
}
/// Decode a non-interleaved progressive scan (single component).
#[allow(clippy::too_many_arguments)]
fn decode_progressive_non_interleaved(
&self,
scan_info: &ScanInfo,
scan_comp: &ScanComponentSelector,
comp_idx: usize,
comp_infos: &[CompInfo],
coeff_bufs: &mut [Vec<[i16; 64]>],
bit_reader: &mut BitReader,
_mcus_x: usize,
_mcus_y: usize,
_max_h: usize,
_max_v: usize,
is_dc: bool,
ss: u8,
se: u8,
ah: u8,
al: u8,
) -> Result<()> {
let ci = &comp_infos[comp_idx];
let mut dc_pred = 0i16;
let mut eob_run = 0u16;
let restart_interval = scan_info.restart_interval as u32;
let mut restart_countdown: u32 = restart_interval;
// Pre-resolve tables once before the block loop
let dc_table = if is_dc {
Some(Self::resolve_table(
&scan_info.dc_huffman_tables,
scan_comp.dc_table_index,
"DC",
)?)
} else {
None
};
let ac_table = if !is_dc || se > 0 {
Some(Self::resolve_table(
&scan_info.ac_huffman_tables,
scan_comp.ac_table_index,
"AC",
)?)
} else {
None
};
// Macro to handle restart interval countdown in each specialized loop.
macro_rules! restart_check_dc {
($bit_reader:expr, $dc_pred:expr, $countdown:expr, $interval:expr) => {
if $interval > 0 {
if $countdown == 0 {
$bit_reader.reset();
$dc_pred = 0;
$countdown = $interval;
}
$countdown -= 1;
}
};
}
macro_rules! restart_check_ac {
($bit_reader:expr, $eob_run:expr, $countdown:expr, $interval:expr) => {
if $interval > 0 {
if $countdown == 0 {
$bit_reader.reset();
$eob_run = 0;
$countdown = $interval;
}
$countdown -= 1;
}
};
}
// Non-interleaved scans use width_in_blocks/height_in_blocks for iteration,
// which may be smaller than blocks_x/blocks_y (the MCU-aligned buffer size).
// Dummy blocks at the right/bottom edges only receive DC from interleaved scans.
let coeff_slice = &mut coeff_bufs[comp_idx];
let scan_blocks_x = ci.width_in_blocks;
let scan_blocks_y = ci.height_in_blocks;
let stride = ci.blocks_x; // buffer stride (MCU-aligned)
if is_dc && ah == 0 {
let dc_table = dc_table.unwrap();
for by in 0..scan_blocks_y {
for bx in 0..scan_blocks_x {
restart_check_dc!(bit_reader, dc_pred, restart_countdown, restart_interval);
let coeffs = &mut coeff_slice[by * stride + bx];
progressive::decode_dc_first(bit_reader, dc_table, &mut dc_pred, coeffs, al)?;
}
}
} else if is_dc {
for by in 0..scan_blocks_y {
for bx in 0..scan_blocks_x {
if restart_interval > 0 {
if restart_countdown == 0 {
bit_reader.reset();
restart_countdown = restart_interval;
}
restart_countdown -= 1;
}
let coeffs = &mut coeff_slice[by * stride + bx];
progressive::decode_dc_refine(bit_reader, coeffs, al)?;
}
}
} else if ah == 0 {
let ac_table = ac_table.unwrap();
for by in 0..scan_blocks_y {
for bx in 0..scan_blocks_x {
restart_check_ac!(bit_reader, eob_run, restart_countdown, restart_interval);
let coeffs = &mut coeff_slice[by * stride + bx];
progressive::decode_ac_first(
bit_reader,
ac_table,
coeffs,
ss,
se,
al,
&mut eob_run,
)?;
}
}
} else {
let ac_table = ac_table.unwrap();
for by in 0..scan_blocks_y {
for bx in 0..scan_blocks_x {
restart_check_ac!(bit_reader, eob_run, restart_countdown, restart_interval);
let coeffs = &mut coeff_slice[by * stride + bx];
progressive::decode_ac_refine(
bit_reader,
ac_table,
coeffs,
ss,
se,
al,
&mut eob_run,
)?;
}
}
}
Ok(())
}
/// Resolve a Huffman table by index, returning an error if missing.
fn resolve_table<'t>(
tables: &'t [Option<HuffmanTable>; 4],
index: u8,
kind: &str,
) -> Result<&'t HuffmanTable> {
tables[index as usize].as_ref().ok_or_else(|| {
JpegError::CorruptData(format!("missing {} Huffman table {}", kind, index))
})
}
/// Compute the MCU row range [start, end) needed for the vertical crop region.
/// Returns (0, mcus_y) when no crop is set.
fn mcu_row_range(
&self,
mcus_y: usize,
block_size: usize,
frame: &FrameHeader,
) -> (usize, usize) {
let (crop_y, crop_h) = match (self.crop_y, self.crop_height) {
(Some(y), Some(h)) => (y, h),
_ => return (0, mcus_y),
};
let max_v = frame
.components
.iter()
.map(|c| c.vertical_sampling as usize)
.max()
.unwrap_or(1);
let mcu_pixel_h = max_v * block_size;
let mcu_start = crop_y / mcu_pixel_h;
let mcu_end = (crop_y + crop_h).div_ceil(mcu_pixel_h).min(mcus_y);
(mcu_start, mcu_end)
}
/// Reassemble ICC profile from parsed APP2 chunks.
fn icc_profile(&self) -> Option<Vec<u8>> {
icc::reassemble_icc_profile(&self.metadata.icc_chunks)
}
/// Decode a lossless JPEG (SOF3).
///
/// Lossless JPEG uses Huffman-coded differences + prediction instead of DCT.
/// No quantization or IDCT is involved.
fn decode_lossless_image(
&self,
frame: &FrameHeader,
width: usize,
height: usize,
icc_profile: Option<Vec<u8>>,
exif_data: Option<Vec<u8>>,
) -> Result<Image> {
if self.metadata.is_arithmetic {
self.decode_lossless_arithmetic(frame, width, height, icc_profile, exif_data)
} else {
self.decode_lossless_huffman(frame, width, height, icc_profile, exif_data)
}
}
/// Decode lossless JPEG with Huffman entropy coding (SOF3).
fn decode_lossless_huffman(
&self,
frame: &FrameHeader,
width: usize,
height: usize,
icc_profile: Option<Vec<u8>>,
exif_data: Option<Vec<u8>>,
) -> Result<Image> {
let scan = &self.metadata.scan;
let precision = frame.precision;
let psv = scan.spec_start; // Predictor selection value (Ss field)
let pt = scan.succ_low; // Point transform (Al field)
if !(1..=7).contains(&psv) {
return Err(JpegError::Unsupported(format!(
"lossless predictor {} (must be 1-7)",
psv
)));
}
let num_components = frame.components.len();
// Resolve DC Huffman tables for each scan component
let mut dc_tables: Vec<&HuffmanTable> = Vec::with_capacity(num_components);
for i in 0..scan.components.len().min(num_components) {
let dc_tbl_idx = scan.components[i].dc_table_index as usize;
let dc_table = self.metadata.dc_huffman_tables[dc_tbl_idx]
.as_ref()
.ok_or_else(|| {
JpegError::CorruptData(format!("missing DC Huffman table {}", dc_tbl_idx))
})?;
dc_tables.push(dc_table);
}
let entropy_data = &self.raw_data[self.metadata.entropy_data_offset..];
let mut reader = BitReader::new(entropy_data);
if num_components == 1 {
// Single-component (grayscale) lossless decode
let dc_table = dc_tables[0];
let mut output = vec![0u16; width * height];
let mut prev_row: Option<Vec<u16>> = None;
for y in 0..height {
let row_start = y * width;
let mut diffs = Vec::with_capacity(width);
for _ in 0..width {
let diff = huffman::decode_dc_coefficient(&mut reader, dc_table)?;
diffs.push(diff);
}
lossless::undifference_row(
&diffs,
prev_row.as_deref(),
&mut output[row_start..row_start + width],
psv,
precision,
pt,
y == 0,
);
prev_row = Some(output[row_start..row_start + width].to_vec());
}
self.lossless_output_grayscale(&output, width, height, pt, icc_profile, exif_data)
} else if num_components == 3 {
// Multi-component (color) lossless decode — interleaved scan
let mut comp_planes: Vec<Vec<u16>> =
(0..3).map(|_| vec![0u16; width * height]).collect();
let mut prev_rows: Vec<Option<Vec<u16>>> = vec![None; 3];
for y in 0..height {
let row_start = y * width;
let mut comp_diffs: Vec<Vec<i16>> =
(0..3).map(|_| Vec::with_capacity(width)).collect();
// Interleaved: for each pixel, decode diff for each component
for _ in 0..width {
for c in 0..3 {
let diff = huffman::decode_dc_coefficient(&mut reader, dc_tables[c])?;
comp_diffs[c].push(diff);
}
}
// Undifference each component
for c in 0..3 {
lossless::undifference_row(
&comp_diffs[c],
prev_rows[c].as_deref(),
&mut comp_planes[c][row_start..row_start + width],
psv,
precision,
pt,
y == 0,
);
prev_rows[c] = Some(comp_planes[c][row_start..row_start + width].to_vec());
}
}
self.lossless_output_color(&comp_planes, width, height, icc_profile, exif_data)
} else {
Err(JpegError::Unsupported(format!(
"{} components not yet supported for lossless",
num_components
)))
}
}
/// Decode lossless JPEG with arithmetic entropy coding (SOF11).
fn decode_lossless_arithmetic(
&self,
frame: &FrameHeader,
width: usize,
height: usize,
icc_profile: Option<Vec<u8>>,
exif_data: Option<Vec<u8>>,
) -> Result<Image> {
use crate::decode::arithmetic::ArithDecoder;
let scan = &self.metadata.scan;
let precision = frame.precision;
let psv = scan.spec_start;
let pt = scan.succ_low;
if !(1..=7).contains(&psv) {
return Err(JpegError::Unsupported(format!(
"lossless predictor {} (must be 1-7)",
psv
)));
}
let num_components = frame.components.len();
// Resolve DC table indices for each scan component
let dc_tbl_indices: Vec<usize> = scan
.components
.iter()
.take(num_components)
.map(|sc| sc.dc_table_index as usize)
.collect();
let entropy_data = &self.raw_data[self.metadata.entropy_data_offset..];
let mut arith = ArithDecoder::new(entropy_data, 0);
// Set conditioning parameters from DAC marker
for i in 0..4 {
let (l, u) = self.metadata.arith_dc_params[i];
arith.set_dc_conditioning(i, l, u);
arith.set_ac_conditioning(i, self.metadata.arith_ac_params[i]);
}
if num_components == 1 {
let dc_tbl = dc_tbl_indices[0];
let mut output = vec![0u16; width * height];
let mut prev_row: Option<Vec<u16>> = None;
for y in 0..height {
let row_start = y * width;
let mut diffs = Vec::with_capacity(width);
for _ in 0..width {
// Save previous accumulated DC to extract the raw difference
let prev_dc: i32 = arith.last_dc_val[0];
let mut block: [i16; 64] = [0i16; 64];
arith.decode_dc_sequential(&mut block, 0, dc_tbl)?;
let diff: i16 = (arith.last_dc_val[0] - prev_dc) as i16;
diffs.push(diff);
}
lossless::undifference_row(
&diffs,
prev_row.as_deref(),
&mut output[row_start..row_start + width],
psv,
precision,
pt,
y == 0,
);
prev_row = Some(output[row_start..row_start + width].to_vec());
}
self.lossless_output_grayscale(&output, width, height, pt, icc_profile, exif_data)
} else if num_components == 3 {
let mut comp_planes: Vec<Vec<u16>> =
(0..3).map(|_| vec![0u16; width * height]).collect();
let mut prev_rows: Vec<Option<Vec<u16>>> = vec![None; 3];
for y in 0..height {
let row_start = y * width;
let mut comp_diffs: Vec<Vec<i16>> =
(0..3).map(|_| Vec::with_capacity(width)).collect();
// Interleaved: for each pixel, decode diff for each component
for _ in 0..width {
for c in 0..3 {
let prev_dc: i32 = arith.last_dc_val[c];
let mut block: [i16; 64] = [0i16; 64];
arith.decode_dc_sequential(&mut block, c, dc_tbl_indices[c])?;
let diff: i16 = (arith.last_dc_val[c] - prev_dc) as i16;
comp_diffs[c].push(diff);
}
}
// Undifference each component
for c in 0..3 {
lossless::undifference_row(
&comp_diffs[c],
prev_rows[c].as_deref(),
&mut comp_planes[c][row_start..row_start + width],
psv,
precision,
pt,
y == 0,
);
prev_rows[c] = Some(comp_planes[c][row_start..row_start + width].to_vec());
}
}
self.lossless_output_color(&comp_planes, width, height, icc_profile, exif_data)
} else {
Err(JpegError::Unsupported(format!(
"{} components not yet supported for lossless",
num_components
)))
}
}
/// Convert decoded lossless grayscale samples to output Image.
fn lossless_output_grayscale(
&self,
output: &[u16],
width: usize,
height: usize,
pt: u8,
icc_profile: Option<Vec<u8>>,
exif_data: Option<Vec<u8>>,
) -> Result<Image> {
let out_format = self.output_format.unwrap_or(PixelFormat::Grayscale);
let bpp = out_format.bytes_per_pixel();
if out_format == PixelFormat::Grayscale {
let mut data = Vec::with_capacity(width * height);
for &sample in output {
let val = if pt > 0 {
((sample as u32) << pt) as u8
} else {
sample as u8
};
data.push(val);
}
Ok(Image {
width,
height,
pixel_format: PixelFormat::Grayscale,
precision: 8,
data,
icc_profile,
exif_data,
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: Vec::new(),
})
} else {
let mut data = Vec::with_capacity(width * height * bpp);
for &sample in output {
let val = if pt > 0 {
((sample as u32) << pt) as u8
} else {
sample as u8
};
match out_format {
PixelFormat::Rgb | PixelFormat::Bgr => {
data.push(val);
data.push(val);
data.push(val);
}
PixelFormat::Rgba
| PixelFormat::Bgra
| PixelFormat::Rgbx
| PixelFormat::Bgrx
| PixelFormat::Argb
| PixelFormat::Abgr => {
data.push(val);
data.push(val);
data.push(val);
data.push(255);
}
PixelFormat::Xrgb | PixelFormat::Xbgr => {
data.push(255);
data.push(val);
data.push(val);
data.push(val);
}
PixelFormat::Rgb565 => {
let packed: u16 = ((val as u16 >> 3) << 11)
| ((val as u16 >> 2) << 5)
| (val as u16 >> 3);
let bytes: [u8; 2] = packed.to_ne_bytes();
data.push(bytes[0]);
data.push(bytes[1]);
}
_ => unreachable!(),
}
}
Ok(Image {
width,
height,
pixel_format: out_format,
precision: 8,
data,
icc_profile,
exif_data,
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: Vec::new(),
})
}
}
/// Convert decoded lossless YCbCr component planes to output Image.
fn lossless_output_color(
&self,
comp_planes: &[Vec<u16>],
width: usize,
height: usize,
icc_profile: Option<Vec<u8>>,
exif_data: Option<Vec<u8>>,
) -> Result<Image> {
let out_format = self.output_format.unwrap_or(PixelFormat::Rgb);
let bpp = out_format.bytes_per_pixel();
let mut data = Vec::with_capacity(width * height * bpp);
for ((&y_pix, &cb_pix), &cr_pix) in comp_planes[0]
.iter()
.zip(comp_planes[1].iter())
.zip(comp_planes[2].iter())
{
let y_val = y_pix as i32;
let cb_val = cb_pix as i32;
let cr_val = cr_pix as i32;
// YCbCr to RGB (JFIF convention: Y,Cb,Cr centered at 128)
let r = (y_val + ((cr_val - 128) * 359 + 128) / 256).clamp(0, 255) as u8;
let g = (y_val - ((cb_val - 128) * 88 + (cr_val - 128) * 183 - 128) / 256).clamp(0, 255)
as u8;
let b = (y_val + ((cb_val - 128) * 454 + 128) / 256).clamp(0, 255) as u8;
match out_format {
PixelFormat::Rgb => {
data.push(r);
data.push(g);
data.push(b);
}
PixelFormat::Bgr => {
data.push(b);
data.push(g);
data.push(r);
}
PixelFormat::Rgba | PixelFormat::Rgbx => {
data.push(r);
data.push(g);
data.push(b);
data.push(255);
}
PixelFormat::Bgra | PixelFormat::Bgrx => {
data.push(b);
data.push(g);
data.push(r);
data.push(255);
}
PixelFormat::Xrgb | PixelFormat::Argb => {
data.push(255);
data.push(r);
data.push(g);
data.push(b);
}
PixelFormat::Xbgr | PixelFormat::Abgr => {
data.push(255);
data.push(b);
data.push(g);
data.push(r);
}
PixelFormat::Rgb565 => {
let packed: u16 =
((r as u16 >> 3) << 11) | ((g as u16 >> 2) << 5) | (b as u16 >> 3);
let bytes: [u8; 2] = packed.to_ne_bytes();
data.push(bytes[0]);
data.push(bytes[1]);
}
_ => {
return Err(JpegError::Unsupported(
"cannot convert lossless 3-component to requested format".to_string(),
));
}
}
}
Ok(Image {
width,
height,
pixel_format: out_format,
precision: 8,
data,
icc_profile,
exif_data,
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: Vec::new(),
})
}
pub fn decode_image(&self) -> Result<Image> {
let image: Image = self.decode_image_inner()?;
if !self.marker_processors.is_empty() {
for marker in &image.saved_markers {
if let Some(processor) = self.marker_processors.get(&marker.code) {
processor(&marker.data);
}
}
}
// When stop_on_warning is enabled, any accumulated warning becomes fatal.
if self.stop_on_warning && !image.warnings.is_empty() {
let first_warning = &image.warnings[0];
let detail = match first_warning {
DecodeWarning::HuffmanError {
mcu_x,
mcu_y,
message,
} => format!("Huffman error at MCU ({}, {}): {}", mcu_x, mcu_y, message),
DecodeWarning::TruncatedData {
decoded_mcus,
total_mcus,
} => format!("truncated: decoded {} of {} MCUs", decoded_mcus, total_mcus),
};
return Err(JpegError::CorruptData(format!(
"stop_on_warning: {}",
detail
)));
}
Ok(image)
}
/// Decode a 12-bit JPEG by delegating to `decompress_12bit`, then scaling
/// the 12-bit samples (0-4095) down to 8-bit (0-255). Converts to the
/// requested output pixel format if one was set.
fn decode_12bit_as_8bit(
&self,
icc_profile: Option<Vec<u8>>,
exif_data: Option<Vec<u8>>,
) -> Result<Image> {
let img12 = crate::api::precision::decompress_12bit(self.raw_data)?;
let num_components: usize = img12.num_components;
// Determine output format: default to Grayscale for 1-component,
// RGB for 3-component, same as the 8-bit path.
let default_format: PixelFormat = if num_components == 1 {
PixelFormat::Grayscale
} else {
PixelFormat::Rgb
};
let out_format: PixelFormat = self.output_format.unwrap_or(default_format);
// Scale 12-bit i16 samples to 8-bit u8: val * 255 / 4095.
// This matches C djpeg's 12-to-8 bit downscaling.
let width: usize = img12.width;
let height: usize = img12.height;
if num_components == 1 {
// Grayscale: scale directly, ignore output format conversion
// (only Grayscale makes sense for 1-component).
let mut data: Vec<u8> = Vec::with_capacity(width * height);
for &val in &img12.data {
let clamped: i16 = val.clamp(0, 4095);
data.push((clamped as u32 * 255 / 4095) as u8);
}
Ok(Image {
width,
height,
pixel_format: PixelFormat::Grayscale,
precision: 8,
data,
icc_profile,
exif_data,
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: Vec::new(),
})
} else {
// Color image: img12.data is interleaved RGB (3 values per pixel).
// Convert to the requested output format.
let bpp: usize = out_format.bytes_per_pixel();
let mut data: Vec<u8> = vec![0u8; width * height * bpp];
let r_off: Option<usize> = out_format.red_offset();
let g_off: Option<usize> = out_format.green_offset();
let b_off: Option<usize> = out_format.blue_offset();
for i in 0..(width * height) {
let src_idx: usize = i * 3;
let r: u8 = (img12.data[src_idx].clamp(0, 4095) as u32 * 255 / 4095) as u8;
let g: u8 = (img12.data[src_idx + 1].clamp(0, 4095) as u32 * 255 / 4095) as u8;
let b: u8 = (img12.data[src_idx + 2].clamp(0, 4095) as u32 * 255 / 4095) as u8;
let dst_idx: usize = i * bpp;
match out_format {
PixelFormat::Rgb => {
data[dst_idx] = r;
data[dst_idx + 1] = g;
data[dst_idx + 2] = b;
}
PixelFormat::Grayscale => {
// Approximate luminance from RGB.
data[dst_idx] =
((r as u32 * 77 + g as u32 * 150 + b as u32 * 29) >> 8) as u8;
}
_ => {
// Use offset-based mapping for all other RGB-derived formats.
if let (Some(ro), Some(go), Some(bo)) = (r_off, g_off, b_off) {
data[dst_idx + ro] = r;
data[dst_idx + go] = g;
data[dst_idx + bo] = b;
// Fill alpha/padding byte to 0xFF for 4-bpp formats.
if bpp == 4 {
let alpha_off: usize = 6 - ro - go - bo;
data[dst_idx + alpha_off] = 0xFF;
}
} else {
return Err(JpegError::Unsupported(format!(
"cannot convert 12-bit color JPEG to {:?}",
out_format
)));
}
}
}
}
Ok(Image {
width,
height,
pixel_format: out_format,
precision: 8,
data,
icc_profile,
exif_data,
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: Vec::new(),
})
}
}
fn decode_image_inner(&self) -> Result<Image> {
let frame = &self.metadata.frame;
let width = frame.width as usize;
let height = frame.height as usize;
// Check pixel limit
if let Some(max) = self.max_pixels {
let total = width * height;
if total > max {
return Err(JpegError::Unsupported(format!(
"image {}x{} ({} pixels) exceeds limit of {}",
width, height, total, max
)));
}
}
// Enforce max_memory: reject if estimated decode memory exceeds limit.
// Estimate = output_buffer + component_plane_buffers.
if let Some(max_mem) = self.max_memory {
let nc = frame.components.len();
let out_bpp = self
.output_format
.unwrap_or(if nc == 1 {
PixelFormat::Grayscale
} else {
PixelFormat::Rgb
})
.bytes_per_pixel();
let total_estimated = width * height * out_bpp + width * height * nc;
if total_estimated > max_mem {
return Err(JpegError::Unsupported(format!(
"estimated decode memory {} bytes exceeds limit of {} bytes",
total_estimated, max_mem
)));
}
}
let icc_profile = self.icc_profile();
let exif_data = self.metadata.exif_data.clone();
// Handle 12-bit JPEG transparently: decode via the 12-bit path, then
// scale samples from 0-4095 to 0-255 so callers get standard 8-bit output.
// This matches C djpeg behavior which handles 12-bit JPEGs automatically.
if frame.precision == 12 {
return self.decode_12bit_as_8bit(icc_profile, exif_data);
}
if frame.precision != 8 {
return Err(JpegError::Unsupported(format!(
"sample precision {} (only 8-bit supported)",
frame.precision
)));
}
let num_components = frame.components.len();
let max_h = frame
.components
.iter()
.map(|c| c.horizontal_sampling as usize)
.max()
.unwrap_or(1);
let max_v = frame
.components
.iter()
.map(|c| c.vertical_sampling as usize)
.max()
.unwrap_or(1);
let block_size = self.scale.block_size();
// Per-component IDCT block sizes: chroma components may use a larger
// IDCT to absorb subsampling factors (matches C libjpeg-turbo).
let comp_block_sizes: Vec<usize> =
Self::compute_all_comp_block_sizes(block_size, max_h, max_v, frame);
let mcu_width = max_h * 8;
let mcu_height = max_v * 8;
let mcus_x = width.div_ceil(mcu_width);
let mcus_y = height.div_ceil(mcu_height);
// Scaled output dimensions
let scaled_mcu_w = max_h * block_size;
let scaled_mcu_h = max_v * block_size;
let full_width = mcus_x * scaled_mcu_w;
let full_height = mcus_y * scaled_mcu_h;
// Final output dimensions (may be smaller than full due to MCU alignment)
let out_width = self.scale.scale_dim(width);
let out_height = self.scale.scale_dim(height);
// Lossless JPEG (SOF3/SOF11) — different pipeline, no IDCT/quant
if frame.is_lossless {
return self.decode_lossless_image(frame, width, height, icc_profile, exif_data);
}
// Pre-resolve quant tables per component (once, not per-block)
let quant_tables: Vec<&QuantTable> = frame
.components
.iter()
.map(|comp| {
self.metadata.quant_tables[comp.quant_table_index as usize]
.as_ref()
.ok_or_else(|| {
JpegError::CorruptData(format!(
"missing quant table {}",
comp.quant_table_index
))
})
})
.collect::<Result<Vec<_>>>()?;
// Decode component planes — different paths for baseline vs progressive vs arithmetic
let (component_planes, warnings) = if self.metadata.is_arithmetic && frame.is_progressive {
self.decode_arithmetic_progressive_planes(
frame,
&quant_tables,
num_components,
mcus_x,
mcus_y,
max_h,
max_v,
&comp_block_sizes,
)?
} else if self.metadata.is_arithmetic {
self.decode_arithmetic_planes(
frame,
&quant_tables,
num_components,
mcus_x,
mcus_y,
&comp_block_sizes,
)?
} else if frame.is_progressive {
self.decode_progressive_planes(
frame,
&quant_tables,
num_components,
mcus_x,
mcus_y,
max_h,
max_v,
&comp_block_sizes,
)?
} else {
self.decode_baseline_planes(
frame,
&quant_tables,
num_components,
mcus_x,
mcus_y,
&comp_block_sizes,
)?
};
// Apply block smoothing if requested
let (component_planes, warnings) = if self.block_smoothing {
let mut planes = component_planes;
for (ci, plane) in planes.iter_mut().enumerate() {
let comp_w: usize = mcus_x
* frame.components[ci].horizontal_sampling as usize
* comp_block_sizes[ci];
let comp_h: usize =
mcus_y * frame.components[ci].vertical_sampling as usize * comp_block_sizes[ci];
crate::decode::toggles::apply_block_smoothing(plane, comp_w, comp_h);
}
(planes, warnings)
} else {
(component_planes, warnings)
};
// Handle output colorspace override
if let Some(cs) = self.output_colorspace {
return crate::decode::toggles::decode_with_colorspace_override(
cs,
&component_planes,
frame,
out_width,
out_height,
mcus_x,
&comp_block_sizes,
icc_profile,
exif_data,
self.metadata.comment.clone(),
self.metadata.density,
self.metadata.saved_markers.clone(),
warnings,
);
}
// Upsample and color convert
if num_components == 1 {
let out_format = self.output_format.unwrap_or(PixelFormat::Grayscale);
let comp_w =
mcus_x * frame.components[0].horizontal_sampling as usize * comp_block_sizes[0];
if out_format == PixelFormat::Grayscale {
let mut data = Vec::with_capacity(out_width * out_height);
for y in 0..out_height {
data.extend_from_slice(
&component_planes[0][y * comp_w..y * comp_w + out_width],
);
}
Ok(Image {
width: out_width,
height: out_height,
pixel_format: PixelFormat::Grayscale,
precision: 8,
data,
icc_profile: icc_profile.clone(),
exif_data: exif_data.clone(),
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: warnings.clone(),
})
} else {
// Expand grayscale to requested color format
let bpp = out_format.bytes_per_pixel();
let data_size = out_width * out_height * bpp;
let mut data = Vec::with_capacity(data_size);
#[allow(clippy::uninit_vec)]
unsafe {
data.set_len(data_size)
};
for y in 0..out_height {
let row = &component_planes[0][y * comp_w..y * comp_w + out_width];
let out_row = &mut data[y * out_width * bpp..(y + 1) * out_width * bpp];
// For dithered RGB565, use the dedicated row-level function.
if out_format == PixelFormat::Rgb565 && self.dither_565 {
crate::decode::color::gray_to_rgb565_dithered_row(
row, out_row, out_width, y,
);
continue;
}
for x in 0..out_width {
let v = row[x];
match out_format {
PixelFormat::Rgb | PixelFormat::Bgr => {
out_row[x * 3] = v;
out_row[x * 3 + 1] = v;
out_row[x * 3 + 2] = v;
}
PixelFormat::Rgba
| PixelFormat::Bgra
| PixelFormat::Rgbx
| PixelFormat::Bgrx
| PixelFormat::Argb
| PixelFormat::Abgr => {
out_row[x * 4] = v;
out_row[x * 4 + 1] = v;
out_row[x * 4 + 2] = v;
out_row[x * 4 + 3] = 255;
}
PixelFormat::Xrgb | PixelFormat::Xbgr => {
out_row[x * 4] = 255;
out_row[x * 4 + 1] = v;
out_row[x * 4 + 2] = v;
out_row[x * 4 + 3] = v;
}
PixelFormat::Rgb565 => {
// Grayscale v → pack as R=G=B=v (no dither)
let packed: u16 = ((v as u16 >> 3) << 11)
| ((v as u16 >> 2) << 5)
| (v as u16 >> 3);
let bytes: [u8; 2] = packed.to_ne_bytes();
out_row[x * 2] = bytes[0];
out_row[x * 2 + 1] = bytes[1];
}
PixelFormat::Grayscale | PixelFormat::Cmyk => unreachable!(),
}
}
}
Ok(Image {
width: out_width,
height: out_height,
pixel_format: out_format,
precision: 8,
data,
icc_profile: icc_profile.clone(),
exif_data: exif_data.clone(),
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: warnings.clone(),
})
}
} else if num_components == 3 {
let out_format = self.output_format.unwrap_or(PixelFormat::Rgb);
if out_format == PixelFormat::Grayscale {
return Err(JpegError::Unsupported(
"cannot convert color JPEG to grayscale".to_string(),
));
}
let bpp = out_format.bytes_per_pixel();
let y_plane = &component_planes[0];
let y_width =
mcus_x * frame.components[0].horizontal_sampling as usize * comp_block_sizes[0];
let cb_comp = &frame.components[1];
let cr_comp = &frame.components[2];
let cb_w = mcus_x * cb_comp.horizontal_sampling as usize * comp_block_sizes[1];
let cb_h = mcus_y * cb_comp.vertical_sampling as usize * comp_block_sizes[1];
let cr_w = mcus_x * cr_comp.horizontal_sampling as usize * comp_block_sizes[2];
let cr_h = mcus_y * cr_comp.vertical_sampling as usize * comp_block_sizes[2];
let y_height =
mcus_y * frame.components[0].vertical_sampling as usize * comp_block_sizes[0];
// Per-component effective upsample factors.
// For scaled decode, chroma may use a larger IDCT that absorbs subsampling,
// making the effective factor 1 (no upsample needed).
let cb_h_factor: usize = y_width / cb_w;
let cb_v_factor: usize = y_height / cb_h;
let cr_h_factor: usize = y_width / cr_w;
let cr_v_factor: usize = y_height / cr_h;
// When both chroma components have the same factors, use the shared
// factor variables that the existing optimized paths expect.
let uniform_chroma: bool = cb_h_factor == cr_h_factor && cb_v_factor == cr_v_factor;
let h_factor: usize = cb_h_factor;
let v_factor: usize = cb_v_factor;
// Actual chroma dimensions (may be smaller than MCU-aligned cb_w/cb_h).
// C libjpeg-turbo uses downsampled_width/height for upsample, not
// MCU-padded dimensions. Using MCU-padded values causes the upsample
// to interpolate padding data, producing wrong edge pixels.
let actual_cb_w: usize = out_width.div_ceil(cb_h_factor);
let actual_cb_h: usize = out_height.div_ceil(cb_v_factor);
let actual_cr_w: usize = out_width.div_ceil(cr_h_factor);
let actual_cr_h: usize = out_height.div_ceil(cr_v_factor);
// For 4:4:4, use component planes directly without clone.
// For subsampled modes, upsample into separate buffers.
let (cb_data, cr_data, cb_stride, cr_stride): (&[u8], &[u8], usize, usize);
if cb_h_factor == 1 && cb_v_factor == 1 && cr_h_factor == 1 && cr_v_factor == 1 {
// 4:4:4: no upsampling needed — reference planes directly
cb_data = &component_planes[1];
cr_data = &component_planes[2];
cb_stride = cb_w;
cr_stride = cr_w;
} else {
// Merged upsample path: combine upsample + color convert in one pass
// for H2V1 (4:2:2) and H2V2 (4:2:0), avoiding intermediate chroma buffers.
// Only available when both chroma components have the same sampling factors.
if self.merged_upsample
&& uniform_chroma
&& out_format == PixelFormat::Rgb
&& h_factor == 2
&& (v_factor == 1 || v_factor == 2)
{
let data_size: usize = out_width * out_height * bpp;
let mut data: Vec<u8> = Vec::with_capacity(data_size);
#[allow(clippy::uninit_vec)]
unsafe {
data.set_len(data_size)
};
if v_factor == 1 {
// H2V1 (4:2:2): one chroma row per Y row
for y in 0..out_height {
Self::merged_h2v1(
&y_plane[y * y_width..],
&component_planes[1][y * cb_w..],
&component_planes[2][y * cb_w..],
&mut data[y * out_width * bpp..],
out_width,
);
}
} else {
// H2V2 (4:2:0): one chroma row per 2 Y rows
let row_pairs: usize = out_height / 2;
for pair in 0..row_pairs {
let y0: usize = pair * 2;
let y1: usize = pair * 2 + 1;
let chroma_row: usize = pair;
let out0_start: usize = y0 * out_width * bpp;
let out1_start: usize = y1 * out_width * bpp;
// Split data into two non-overlapping mutable slices
let (top, bottom) = data.split_at_mut(out1_start);
Self::merged_h2v2(
&y_plane[y0 * y_width..],
&y_plane[y1 * y_width..],
&component_planes[1][chroma_row * cb_w..],
&component_planes[2][chroma_row * cb_w..],
&mut top[out0_start..],
bottom,
out_width,
);
}
// Handle odd height: last row uses H2V1 with last chroma row
if out_height & 1 != 0 {
let last_y: usize = out_height - 1;
let chroma_row: usize = last_y / 2;
Self::merged_h2v1(
&y_plane[last_y * y_width..],
&component_planes[1][chroma_row * cb_w..],
&component_planes[2][chroma_row * cb_w..],
&mut data[last_y * out_width * bpp..],
out_width,
);
}
}
return Ok(Image {
width: out_width,
height: out_height,
pixel_format: out_format,
precision: 8,
data,
icc_profile: icc_profile.clone(),
exif_data: exif_data.clone(),
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: warnings.clone(),
});
}
// Row-streaming H2V2: skip full-plane allocation, process 2 rows at a time.
// When actual_cb_w <= 2, C's merged upsample uses box filter for the
// entire image (the NEON/SIMD fancy path doesn't kick in). Use box
// filter (fast_upsample equivalent) to match C exactly.
// Only available when both chroma components have the same sampling factors.
if !self.fast_upsample
&& uniform_chroma
&& h_factor == 2
&& v_factor == 2
&& actual_cb_w > 2
&& block_size == 8
{
// Row-streaming H2V2: fuse upsample + color convert to avoid
// allocating full-size cb_full/cr_full buffers (~4MB for 1080p).
// Process 2 output rows at a time, keeping data in L1/L2 cache.
let data_size = out_width * out_height * bpp;
let mut data = Vec::with_capacity(data_size);
#[allow(clippy::uninit_vec)]
unsafe {
data.set_len(data_size)
};
// Small per-row upsample buffers (2 rows × full_width per component)
let mut cb_row_top = vec![0u8; full_width];
let mut cb_row_bot = vec![0u8; full_width];
let mut cr_row_top = vec![0u8; full_width];
let mut cr_row_bot = vec![0u8; full_width];
// Use actual chroma dimensions for upsample (not MCU-padded).
for cy in 0..actual_cb_h {
let cb_cur = &component_planes[1][cy * cb_w..cy * cb_w + actual_cb_w];
let cr_cur = &component_planes[2][cy * cb_w..cy * cb_w + actual_cb_w];
let cb_above = if cy > 0 {
&component_planes[1][(cy - 1) * cb_w..(cy - 1) * cb_w + actual_cb_w]
} else {
cb_cur
};
let cb_below = if cy + 1 < actual_cb_h {
&component_planes[1][(cy + 1) * cb_w..(cy + 1) * cb_w + actual_cb_w]
} else {
cb_cur
};
let cr_above = if cy > 0 {
&component_planes[2][(cy - 1) * cb_w..(cy - 1) * cb_w + actual_cb_w]
} else {
cr_cur
};
let cr_below = if cy + 1 < actual_cb_h {
&component_planes[2][(cy + 1) * cb_w..(cy + 1) * cb_w + actual_cb_w]
} else {
cr_cur
};
// Fused vertical+horizontal upsample for top output row
crate::decode::upsample::fancy_h2v2_row(
cb_cur,
cb_above,
&mut cb_row_top,
actual_cb_w,
);
crate::decode::upsample::fancy_h2v2_row(
cr_cur,
cr_above,
&mut cr_row_top,
actual_cb_w,
);
// Fused vertical+horizontal upsample for bottom output row
crate::decode::upsample::fancy_h2v2_row(
cb_cur,
cb_below,
&mut cb_row_bot,
actual_cb_w,
);
crate::decode::upsample::fancy_h2v2_row(
cr_cur,
cr_below,
&mut cr_row_bot,
actual_cb_w,
);
// Color convert both output rows immediately
let out_y_top = cy * 2;
let out_y_bot = cy * 2 + 1;
if out_y_top < out_height {
self.color_convert_row(
out_format,
&y_plane[out_y_top * y_width..],
&cb_row_top,
&cr_row_top,
&mut data[out_y_top * out_width * bpp..],
out_width,
out_y_top,
);
}
if out_y_bot < out_height {
self.color_convert_row(
out_format,
&y_plane[out_y_bot * y_width..],
&cb_row_bot,
&cr_row_bot,
&mut data[out_y_bot * out_width * bpp..],
out_width,
out_y_bot,
);
}
}
return Ok(Image {
width: out_width,
height: out_height,
pixel_format: out_format,
precision: 8,
data,
icc_profile: icc_profile.clone(),
exif_data: exif_data.clone(),
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: warnings.clone(),
});
}
// All remaining paths need full-plane cb_full/cr_full buffers.
let alloc_size = full_width * full_height;
let mut cb_full = Vec::with_capacity(alloc_size);
let mut cr_full = Vec::with_capacity(alloc_size);
#[allow(clippy::uninit_vec)]
unsafe {
cb_full.set_len(alloc_size);
cr_full.set_len(alloc_size);
}
// Upsample each chroma component independently using its own factors.
// This handles non-uniform chroma sampling (e.g. Cb=2x1, Cr=1x1)
// where each component needs a different upsample strategy.
for (
comp_plane,
comp_full,
comp_w,
comp_h,
comp_hf,
comp_vf,
actual_w,
actual_h,
comp_bs,
) in [
(
&component_planes[1],
&mut cb_full,
cb_w,
cb_h,
cb_h_factor,
cb_v_factor,
actual_cb_w,
actual_cb_h,
comp_block_sizes[1],
),
(
&component_planes[2],
&mut cr_full,
cr_w,
cr_h,
cr_h_factor,
cr_v_factor,
actual_cr_w,
actual_cr_h,
comp_block_sizes[2],
),
] {
// C's merged upsample uses box filter when:
// - actual chroma width <= 2 (SIMD fancy requires >= 3 columns)
// - scaled decode with chroma still needing upsample (chroma IDCT < 8)
let use_box_filter: bool = self.fast_upsample || actual_w <= 2 || comp_bs < 8;
if comp_hf == 1 && comp_vf == 1 {
// No upsampling needed for this component — copy directly.
for row in 0..full_height.min(comp_h) {
let src_start: usize = row * comp_w;
let dst_start: usize = row * full_width;
let copy_len: usize = full_width.min(comp_w);
comp_full[dst_start..dst_start + copy_len]
.copy_from_slice(&comp_plane[src_start..src_start + copy_len]);
}
} else if use_box_filter {
crate::decode::toggles::upsample_nearest(
comp_plane, comp_w, comp_h, comp_full, full_width, comp_hf, comp_vf,
);
} else if comp_hf == 2 && comp_vf == 1 {
// H2V1: horizontal-only 2x fancy upsample.
for row in 0..actual_h {
self.fancy_upsample_h2v1(
&comp_plane[row * comp_w..],
actual_w,
&mut comp_full[row * full_width..],
);
}
} else if comp_hf == 2 && comp_vf == 2 {
// H2V2: fused 2D triangle filter fancy upsample.
crate::decode::upsample::fancy_h2v2_strided(
comp_plane, actual_w, comp_w, actual_h, comp_full, full_width,
);
} else if comp_hf == 1 && comp_vf == 2 {
// H1V2: vertical-only 2x fancy upsample.
self.fancy_h1v2(comp_plane, comp_w, actual_h, comp_full, full_width);
} else {
// Generic fallback: nearest-neighbor for any factor combination
// (4x1, 4x2, 1x4, 3x2, etc.).
upsample_generic_nearest(
comp_plane, comp_w, comp_h, comp_full, full_width, comp_hf, comp_vf,
);
}
}
// Rebind as immutable references for color conversion below.
// We use a trick: leak the Vecs temporarily, do the conversion,
// then reconstruct and drop them. But simpler: just use a nested scope.
// Actually, let's just do the color conversion here and return.
let data_size = out_width * out_height * bpp;
let mut data = Vec::with_capacity(data_size);
#[allow(clippy::uninit_vec)]
unsafe {
data.set_len(data_size)
};
for y in 0..out_height {
self.color_convert_row(
out_format,
&y_plane[y * y_width..],
&cb_full[y * full_width..],
&cr_full[y * full_width..],
&mut data[y * out_width * bpp..],
out_width,
y,
);
}
return Ok(Image {
width: out_width,
height: out_height,
pixel_format: out_format,
precision: 8,
data,
icc_profile: icc_profile.clone(),
exif_data: exif_data.clone(),
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: warnings.clone(),
});
}
// 4:4:4 path (no upsampling)
let data_size = out_width * out_height * bpp;
let mut data = Vec::with_capacity(data_size);
#[allow(clippy::uninit_vec)]
unsafe {
data.set_len(data_size)
};
for y in 0..out_height {
self.color_convert_row(
out_format,
&y_plane[y * y_width..],
&cb_data[y * cb_stride..],
&cr_data[y * cr_stride..],
&mut data[y * out_width * bpp..],
out_width,
y,
);
}
Ok(Image {
width: out_width,
height: out_height,
pixel_format: out_format,
precision: 8,
data,
icc_profile: icc_profile.clone(),
exif_data: exif_data.clone(),
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings: warnings.clone(),
})
} else if num_components == 4 {
self.decode_4_component(
&component_planes,
frame,
out_width,
out_height,
mcus_x,
mcus_y,
max_h,
max_v,
full_width,
full_height,
&comp_block_sizes,
icc_profile,
exif_data,
warnings,
)
} else {
Err(JpegError::Unsupported(format!(
"{} components not yet supported",
num_components
)))
}
}
/// Decode JPEG to raw downsampled component planes.
///
/// Returns component planes at their native (potentially subsampled)
/// resolution, without performing color conversion or upsampling.
/// This matches libjpeg-turbo's `jpeg_read_raw_data()` functionality.
pub fn decode_raw(self) -> Result<crate::api::raw_data::RawImage> {
let frame = &self.metadata.frame;
let width: usize = frame.width as usize;
let height: usize = frame.height as usize;
if frame.precision != 8 {
return Err(JpegError::Unsupported(format!(
"sample precision {} (only 8-bit supported)",
frame.precision
)));
}
let num_components: usize = frame.components.len();
let max_h: usize = frame
.components
.iter()
.map(|c| c.horizontal_sampling as usize)
.max()
.unwrap_or(1);
let max_v: usize = frame
.components
.iter()
.map(|c| c.vertical_sampling as usize)
.max()
.unwrap_or(1);
let block_size: usize = 8;
// Raw data decode always uses full-size (8x8) IDCT for all components
let comp_block_sizes: Vec<usize> = vec![block_size; num_components];
let mcu_width: usize = max_h * 8;
let mcu_height: usize = max_v * 8;
let mcus_x: usize = width.div_ceil(mcu_width);
let mcus_y: usize = height.div_ceil(mcu_height);
let quant_tables: Vec<&crate::common::quant_table::QuantTable> = frame
.components
.iter()
.map(|comp| {
self.metadata.quant_tables[comp.quant_table_index as usize]
.as_ref()
.ok_or_else(|| {
JpegError::CorruptData(format!(
"missing quant table {}",
comp.quant_table_index
))
})
})
.collect::<Result<Vec<_>>>()?;
let (component_planes, _warnings) = if self.metadata.is_arithmetic && frame.is_progressive {
self.decode_arithmetic_progressive_planes(
frame,
&quant_tables,
num_components,
mcus_x,
mcus_y,
max_h,
max_v,
&comp_block_sizes,
)?
} else if self.metadata.is_arithmetic {
self.decode_arithmetic_planes(
frame,
&quant_tables,
num_components,
mcus_x,
mcus_y,
&comp_block_sizes,
)?
} else if frame.is_progressive {
self.decode_progressive_planes(
frame,
&quant_tables,
num_components,
mcus_x,
mcus_y,
max_h,
max_v,
&comp_block_sizes,
)?
} else {
self.decode_baseline_planes(
frame,
&quant_tables,
num_components,
mcus_x,
mcus_y,
&comp_block_sizes,
)?
};
let mut plane_widths: Vec<usize> = Vec::with_capacity(num_components);
let mut plane_heights: Vec<usize> = Vec::with_capacity(num_components);
for (ci, comp) in frame.components.iter().enumerate() {
plane_widths.push(mcus_x * comp.horizontal_sampling as usize * comp_block_sizes[ci]);
plane_heights.push(mcus_y * comp.vertical_sampling as usize * comp_block_sizes[ci]);
}
Ok(crate::api::raw_data::RawImage {
planes: component_planes,
plane_widths,
plane_heights,
width,
height,
num_components,
})
}
/// Determine the JPEG color space from component count and Adobe marker.
/// Follows the same heuristic as libjpeg-turbo (jdapimin.c).
fn detect_color_space(&self) -> ColorSpace {
let num_components = self.metadata.frame.components.len();
match num_components {
1 => ColorSpace::Grayscale,
3 => {
if self.metadata.saw_adobe_marker && self.metadata.adobe_transform == 0 {
ColorSpace::Rgb
} else {
ColorSpace::YCbCr
}
}
4 => {
if self.metadata.saw_adobe_marker {
match self.metadata.adobe_transform {
0 => ColorSpace::Cmyk,
2 => ColorSpace::Ycck,
_ => ColorSpace::Ycck, // default for unknown Adobe transforms
}
} else {
ColorSpace::Cmyk // no Adobe marker → assume CMYK
}
}
_ => ColorSpace::YCbCr, // fallback
}
}
/// Decode a 4-component (CMYK/YCCK) image.
#[allow(clippy::too_many_arguments)]
fn decode_4_component(
&self,
component_planes: &[Vec<u8>],
frame: &FrameHeader,
width: usize,
height: usize,
mcus_x: usize,
mcus_y: usize,
_max_h: usize,
_max_v: usize,
full_width: usize,
full_height: usize,
comp_block_sizes: &[usize],
icc_profile: Option<Vec<u8>>,
exif_data: Option<Vec<u8>>,
warnings: Vec<DecodeWarning>,
) -> Result<Image> {
let color_space = self.detect_color_space();
let out_format = self.output_format.unwrap_or(PixelFormat::Cmyk);
if out_format == PixelFormat::Grayscale {
return Err(JpegError::Unsupported(
"cannot convert CMYK/YCCK to grayscale".to_string(),
));
}
// Component 0 is always full-resolution (Y or C).
let comp0_w =
mcus_x * frame.components[0].horizontal_sampling as usize * comp_block_sizes[0];
// For YCCK, components 1-2 may be subsampled (chroma), component 3 (K) is full.
// For CMYK, all components are typically the same resolution.
let comp1 = &frame.components[1];
let comp1_w = mcus_x * comp1.horizontal_sampling as usize * comp_block_sizes[1];
let comp1_h = mcus_y * comp1.vertical_sampling as usize * comp_block_sizes[1];
let comp3_w =
mcus_x * frame.components[3].horizontal_sampling as usize * comp_block_sizes[3];
let h_factor = comp0_w / comp1_w;
let v_factor =
(mcus_y * frame.components[0].vertical_sampling as usize * comp_block_sizes[0])
/ (mcus_y * comp1.vertical_sampling as usize * comp_block_sizes[1]);
// Upsample chroma if needed (for YCCK subsampled images)
let (plane1, plane2, p1_stride, p2_stride): (&[u8], &[u8], usize, usize);
if h_factor == 1 && v_factor == 1 {
plane1 = &component_planes[1];
plane2 = &component_planes[2];
p1_stride = comp1_w;
p2_stride = comp1_w;
} else {
let alloc_size = full_width * full_height;
let mut p1_full = Vec::with_capacity(alloc_size);
let mut p2_full = Vec::with_capacity(alloc_size);
unsafe {
p1_full.set_len(alloc_size);
p2_full.set_len(alloc_size);
}
if h_factor == 2 && v_factor == 1 {
for row in 0..comp1_h {
self.fancy_upsample_h2v1(
&component_planes[1][row * comp1_w..],
comp1_w,
&mut p1_full[row * full_width..],
);
self.fancy_upsample_h2v1(
&component_planes[2][row * comp1_w..],
comp1_w,
&mut p2_full[row * full_width..],
);
}
} else if h_factor == 2 && v_factor == 2 {
self.fancy_h2v2(
&component_planes[1],
comp1_w,
comp1_h,
&mut p1_full,
full_width,
);
self.fancy_h2v2(
&component_planes[2],
comp1_w,
comp1_h,
&mut p2_full,
full_width,
);
} else {
// Generic fallback for non-standard 4-component sampling factors.
upsample_generic_nearest(
&component_planes[1],
comp1_w,
comp1_h,
&mut p1_full,
full_width,
h_factor,
v_factor,
);
upsample_generic_nearest(
&component_planes[2],
comp1_w,
comp1_h,
&mut p2_full,
full_width,
h_factor,
v_factor,
);
}
return self.convert_4comp_output(
color_space,
out_format,
&component_planes[0],
comp0_w,
&p1_full,
full_width,
&p2_full,
full_width,
&component_planes[3],
comp3_w,
width,
height,
icc_profile,
exif_data,
warnings,
);
}
self.convert_4comp_output(
color_space,
out_format,
&component_planes[0],
comp0_w,
plane1,
p1_stride,
plane2,
p2_stride,
&component_planes[3],
comp3_w,
width,
height,
icc_profile,
exif_data,
warnings,
)
}
/// Color-convert 4 component planes to the output format.
#[allow(clippy::too_many_arguments)]
fn convert_4comp_output(
&self,
color_space: ColorSpace,
out_format: PixelFormat,
plane0: &[u8],
p0_stride: usize,
plane1: &[u8],
p1_stride: usize,
plane2: &[u8],
p2_stride: usize,
plane3: &[u8],
p3_stride: usize,
width: usize,
height: usize,
icc_profile: Option<Vec<u8>>,
exif_data: Option<Vec<u8>>,
warnings: Vec<DecodeWarning>,
) -> Result<Image> {
use crate::decode::color;
let bpp = out_format.bytes_per_pixel();
let data_size = width * height * bpp;
let mut data = Vec::with_capacity(data_size);
#[allow(clippy::uninit_vec)]
unsafe {
data.set_len(data_size)
};
for y in 0..height {
let p0 = &plane0[y * p0_stride..];
let p1 = &plane1[y * p1_stride..];
let p2 = &plane2[y * p2_stride..];
let p3 = &plane3[y * p3_stride..];
let out = &mut data[y * width * bpp..];
match (color_space, out_format) {
// CMYK → CMYK: passthrough
(ColorSpace::Cmyk, PixelFormat::Cmyk) => {
color::cmyk_passthrough_row(p0, p1, p2, p3, out, width);
}
// CMYK → RGB/RGBA/BGR/BGRA: direct conversion
(ColorSpace::Cmyk, PixelFormat::Rgb) => {
color::cmyk_to_rgb_row(p0, p1, p2, p3, out, width);
}
(ColorSpace::Cmyk, PixelFormat::Rgba) => {
color::cmyk_to_rgba_row(p0, p1, p2, p3, out, width);
}
(ColorSpace::Cmyk, PixelFormat::Bgr) => {
color::cmyk_to_bgr_row(p0, p1, p2, p3, out, width);
}
(ColorSpace::Cmyk, PixelFormat::Bgra) => {
color::cmyk_to_bgra_row(p0, p1, p2, p3, out, width);
}
// YCCK → CMYK: YCbCr→RGB→invert→CMYK, K passthrough
(ColorSpace::Ycck, PixelFormat::Cmyk) => {
color::ycck_to_cmyk_row(p0, p1, p2, p3, out, width);
}
// YCCK → RGB: convert YCCK→CMYK first (into temp), then CMYK→RGB
(ColorSpace::Ycck, PixelFormat::Rgb) => {
let mut cmyk_buf = vec![0u8; width * 4];
color::ycck_to_cmyk_row(p0, p1, p2, p3, &mut cmyk_buf, width);
for x in 0..width {
let kv = cmyk_buf[x * 4 + 3] as u16;
out[x * 3] = ((cmyk_buf[x * 4] as u16 * kv + 127) / 255) as u8;
out[x * 3 + 1] = ((cmyk_buf[x * 4 + 1] as u16 * kv + 127) / 255) as u8;
out[x * 3 + 2] = ((cmyk_buf[x * 4 + 2] as u16 * kv + 127) / 255) as u8;
}
}
(ColorSpace::Ycck, PixelFormat::Rgba) => {
let mut cmyk_buf = vec![0u8; width * 4];
color::ycck_to_cmyk_row(p0, p1, p2, p3, &mut cmyk_buf, width);
for x in 0..width {
let kv = cmyk_buf[x * 4 + 3] as u16;
out[x * 4] = ((cmyk_buf[x * 4] as u16 * kv + 127) / 255) as u8;
out[x * 4 + 1] = ((cmyk_buf[x * 4 + 1] as u16 * kv + 127) / 255) as u8;
out[x * 4 + 2] = ((cmyk_buf[x * 4 + 2] as u16 * kv + 127) / 255) as u8;
out[x * 4 + 3] = 255;
}
}
(ColorSpace::Ycck, PixelFormat::Bgr) => {
let mut cmyk_buf = vec![0u8; width * 4];
color::ycck_to_cmyk_row(p0, p1, p2, p3, &mut cmyk_buf, width);
for x in 0..width {
let kv = cmyk_buf[x * 4 + 3] as u16;
let r = ((cmyk_buf[x * 4] as u16 * kv + 127) / 255) as u8;
let g = ((cmyk_buf[x * 4 + 1] as u16 * kv + 127) / 255) as u8;
let b = ((cmyk_buf[x * 4 + 2] as u16 * kv + 127) / 255) as u8;
out[x * 3] = b;
out[x * 3 + 1] = g;
out[x * 3 + 2] = r;
}
}
(ColorSpace::Ycck, PixelFormat::Bgra) => {
let mut cmyk_buf = vec![0u8; width * 4];
color::ycck_to_cmyk_row(p0, p1, p2, p3, &mut cmyk_buf, width);
for x in 0..width {
let kv = cmyk_buf[x * 4 + 3] as u16;
let r = ((cmyk_buf[x * 4] as u16 * kv + 127) / 255) as u8;
let g = ((cmyk_buf[x * 4 + 1] as u16 * kv + 127) / 255) as u8;
let b = ((cmyk_buf[x * 4 + 2] as u16 * kv + 127) / 255) as u8;
out[x * 4] = b;
out[x * 4 + 1] = g;
out[x * 4 + 2] = r;
out[x * 4 + 3] = 255;
}
}
// CMYK → 4bpp offset-based formats
(
ColorSpace::Cmyk,
PixelFormat::Rgbx
| PixelFormat::Bgrx
| PixelFormat::Xrgb
| PixelFormat::Xbgr
| PixelFormat::Argb
| PixelFormat::Abgr,
) => {
let r_off: usize = out_format.red_offset().unwrap();
let g_off: usize = out_format.green_offset().unwrap();
let b_off: usize = out_format.blue_offset().unwrap();
// The remaining offset is 0+1+2+3=6 minus the other three
let pad_off: usize = 6 - r_off - g_off - b_off;
for x in 0..width {
let kv = p3[x] as u16;
let r = ((p0[x] as u16 * kv + 127) / 255) as u8;
let g = ((p1[x] as u16 * kv + 127) / 255) as u8;
let b = ((p2[x] as u16 * kv + 127) / 255) as u8;
out[x * 4 + r_off] = r;
out[x * 4 + g_off] = g;
out[x * 4 + b_off] = b;
out[x * 4 + pad_off] = 255;
}
}
// YCCK → 4bpp offset-based formats
(
ColorSpace::Ycck,
PixelFormat::Rgbx
| PixelFormat::Bgrx
| PixelFormat::Xrgb
| PixelFormat::Xbgr
| PixelFormat::Argb
| PixelFormat::Abgr,
) => {
let r_off: usize = out_format.red_offset().unwrap();
let g_off: usize = out_format.green_offset().unwrap();
let b_off: usize = out_format.blue_offset().unwrap();
let pad_off: usize = 6 - r_off - g_off - b_off;
let mut cmyk_buf = vec![0u8; width * 4];
color::ycck_to_cmyk_row(p0, p1, p2, p3, &mut cmyk_buf, width);
for x in 0..width {
let kv = cmyk_buf[x * 4 + 3] as u16;
let r = ((cmyk_buf[x * 4] as u16 * kv + 127) / 255) as u8;
let g = ((cmyk_buf[x * 4 + 1] as u16 * kv + 127) / 255) as u8;
let b = ((cmyk_buf[x * 4 + 2] as u16 * kv + 127) / 255) as u8;
out[x * 4 + r_off] = r;
out[x * 4 + g_off] = g;
out[x * 4 + b_off] = b;
out[x * 4 + pad_off] = 255;
}
}
_ => {
return Err(JpegError::Unsupported(format!(
"unsupported conversion: {:?} → {:?}",
color_space, out_format
)));
}
}
}
Ok(Image {
width,
height,
pixel_format: out_format,
precision: 8,
data,
icc_profile,
exif_data,
comment: self.metadata.comment.clone(),
density: self.metadata.density,
saved_markers: self.metadata.saved_markers.clone(),
warnings,
})
}
}