xberg 1.1.1

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

use crate::Result;
use crate::core::config::ExtractionConfig;
use crate::extraction::image::extract_image_metadata_with_security_limits;
use crate::plugins::{InternalDocumentExtractor, Plugin};
use crate::types::internal::InternalDocument;
use crate::types::internal_builder::InternalDocumentBuilder;
use crate::types::metadata::Metadata;
use async_trait::async_trait;

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
const WHOLE_IMAGE_TESSERACT_PSM: i32 = 11;

// Tesseract's automatic layout modes can hang under the single-threaded WASM
// runtime, so retain the existing single-block default there. ~keep
#[cfg(all(
    target_arch = "wasm32",
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
const WHOLE_IMAGE_TESSERACT_PSM: i32 = 6;

#[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
const VERTICAL_BLOCK_TESSERACT_PSM: i32 = 5;

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
const SPARSE_IMAGE_OCR_WORD_LIMIT: usize = 20;

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
const SPARSE_IMAGE_OCR_FALLBACK_PSM: i32 = 3;

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
const SPARSE_IMAGE_OCR_MIN_WORD_CONFIDENCE: f64 = 0.30;

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
const SPARSE_IMAGE_OCR_MAX_LOW_CONFIDENCE_RATIO: f64 = 0.30;

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
const SPARSE_IMAGE_OCR_CONFIDENCE_PERCENTILE: f64 = 0.10;

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
const LAYOUT_REGION_TESSERACT_PSM: i32 = 6;

#[cfg(any(test, all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm"))))]
const MIN_LAYOUT_OCR_ALPHANUMERIC_TOKEN_RETENTION: f64 = 0.80;

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
const REQUIRED_CACHED_LAYOUT_TOKEN_RETENTION: f64 = 1.0;

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
const LAYOUT_READING_ORDER_ROW_HEIGHT_RATIO: f32 = 0.05;

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
const MIN_LAYOUT_CROP_DIMENSION: u32 = 4;

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
const MIN_LAYOUT_OCR_ELEMENT_INTERSECTION_OVER_WORD_AREA: f32 = 0.2;

#[cfg(feature = "ocr-pipeline")]
const NORMALIZED_PNG_ENCODE_BYTES_PER_PIXEL: u64 = 4;
#[cfg(feature = "ocr-pipeline")]
const NORMALIZED_PNG_ENCODE_FIXED_BYTES: u64 = 256 * 1024;

#[cfg(all(feature = "layout-detection", feature = "ocr"))]
const MAX_OCR_COORDINATE_SCALE_RELATIVE_DIFFERENCE: f64 = 0.01;

#[cfg(any(test, all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm"))))]
fn internal_document_text(doc: &InternalDocument) -> String {
    doc.elements
        .iter()
        .filter_map(|element| {
            let text = element.text.trim();
            (!text.is_empty()).then_some(text)
        })
        .collect::<Vec<_>>()
        .join("\n")
}

#[cfg(any(test, all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm"))))]
fn alphanumeric_tokens(text: &str) -> Vec<String> {
    text.split(|character: char| !character.is_alphanumeric())
        .filter(|token| !token.is_empty())
        .map(|token| token.to_lowercase())
        .collect()
}

#[cfg(any(test, all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm"))))]
fn alphanumeric_token_retention(layout_text: &str, whole_image_text: &str) -> f64 {
    let whole_image_tokens = alphanumeric_tokens(whole_image_text);
    if whole_image_tokens.is_empty() {
        return 1.0;
    }

    let mut layout_token_counts = std::collections::HashMap::<String, usize>::new();
    for token in alphanumeric_tokens(layout_text) {
        *layout_token_counts.entry(token).or_default() += 1;
    }

    let retained = whole_image_tokens
        .iter()
        .filter(|token| {
            let Some(count) = layout_token_counts.get_mut(*token) else {
                return false;
            };
            if *count == 0 {
                return false;
            }
            *count -= 1;
            true
        })
        .count();

    retained as f64 / whole_image_tokens.len() as f64
}

#[cfg(any(test, all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm"))))]
fn image_ocr_quality_score(text: &str) -> f64 {
    #[cfg(feature = "quality")]
    {
        crate::text::quality::calculate_quality_score(text, None)
    }

    #[cfg(not(feature = "quality"))]
    {
        let _ = text;
        0.0
    }
}

#[cfg(any(test, all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm"))))]
fn select_image_ocr_result(
    layout_doc: InternalDocument,
    whole_image_result: Result<InternalDocument>,
) -> InternalDocument {
    let whole_image_doc = match whole_image_result {
        Ok(doc) => doc,
        Err(error) => {
            tracing::warn!(%error, "Whole-image OCR quality comparison failed; retaining layout-region OCR");
            return layout_doc;
        }
    };
    let layout_text = internal_document_text(&layout_doc);
    let whole_image_text = internal_document_text(&whole_image_doc);
    let layout_score = image_ocr_quality_score(&layout_text);
    let whole_image_score = image_ocr_quality_score(&whole_image_text);
    let token_retention = alphanumeric_token_retention(&layout_text, &whole_image_text);

    if layout_score < whole_image_score || token_retention < MIN_LAYOUT_OCR_ALPHANUMERIC_TOKEN_RETENTION {
        tracing::debug!(
            layout_score,
            whole_image_score,
            token_retention,
            "Whole-image OCR retained because layout-region OCR reduced text quality"
        );
        whole_image_doc
    } else {
        layout_doc
    }
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn cached_whole_image_after_layout_error(
    whole_image_result: &Result<InternalDocument>,
    error: crate::XbergError,
) -> Result<InternalDocument> {
    let whole_image_doc = match whole_image_result {
        Ok(doc) => doc,
        Err(whole_image_error) => {
            return Err(crate::XbergError::Other(format!(
                "Image OCR failed in both paths; whole-image OCR: {whole_image_error}; layout-region OCR: {error}"
            )));
        }
    };
    tracing::warn!(
        %error,
        "Layout-region OCR failed after whole-image OCR succeeded; retaining whole-image output"
    );
    let mut retained = whole_image_doc.clone();
    retained.processing_warnings.push(crate::types::ProcessingWarning {
        source: std::borrow::Cow::Borrowed("layout-ocr"),
        message: std::borrow::Cow::Borrowed(
            "Layout-region OCR failed after whole-image OCR succeeded; retained whole-image output",
        ),
    });
    Ok(retained)
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn ocr_geometry_bounds(geometry: &crate::types::OcrBoundingGeometry) -> (u32, u32, u32, u32) {
    match geometry {
        crate::types::OcrBoundingGeometry::Rectangle {
            left,
            top,
            width,
            height,
        } => (*left, *top, *width, *height),
        crate::types::OcrBoundingGeometry::Quadrilateral { points } => {
            let min_x = points.iter().map(|point| point.x).min().unwrap_or(0);
            let max_x = points.iter().map(|point| point.x).max().unwrap_or(0);
            let min_y = points.iter().map(|point| point.y).min().unwrap_or(0);
            let max_y = points.iter().map(|point| point.y).max().unwrap_or(0);
            (min_x, min_y, max_x.saturating_sub(min_x), max_y.saturating_sub(min_y))
        }
    }
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
#[derive(Clone, Copy)]
struct OcrCoordinateTransform {
    processed_width: u64,
    processed_height: u64,
    scale_x: f64,
    scale_y: f64,
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn whole_image_ocr_coordinate_transform(
    doc: &InternalDocument,
    image_width: u32,
    image_height: u32,
) -> Option<OcrCoordinateTransform> {
    #[cfg(feature = "ocr")]
    {
        let additional = &doc.metadata.additional;
        let processed_width = additional
            .get(crate::ocr_metadata_keys::OCR_PROCESSED_IMAGE_WIDTH_METADATA_KEY)
            .and_then(serde_json::Value::as_u64);
        let processed_height = additional
            .get(crate::ocr_metadata_keys::OCR_PROCESSED_IMAGE_HEIGHT_METADATA_KEY)
            .and_then(serde_json::Value::as_u64);
        let auto_rotated = additional
            .get(crate::ocr_metadata_keys::OCR_AUTO_ROTATED_METADATA_KEY)
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        let (processed_width, processed_height) = (processed_width?, processed_height?);
        if auto_rotated || processed_width == 0 || processed_height == 0 || image_width == 0 || image_height == 0 {
            return None;
        }
        let scale_x = f64::from(image_width) / processed_width as f64;
        let scale_y = f64::from(image_height) / processed_height as f64;
        let relative_difference = (scale_x - scale_y).abs() / scale_x.max(scale_y);
        (relative_difference <= MAX_OCR_COORDINATE_SCALE_RELATIVE_DIFFERENCE).then_some(OcrCoordinateTransform {
            processed_width,
            processed_height,
            scale_x,
            scale_y,
        })
    }

    #[cfg(not(feature = "ocr"))]
    {
        let _ = (doc, image_width, image_height);
        None
    }
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn transformed_ocr_bounds(
    geometry: &crate::types::OcrBoundingGeometry,
    transform: OcrCoordinateTransform,
) -> Option<(f32, f32, f32, f32)> {
    let (left, top, width, height) = ocr_geometry_bounds(geometry);
    let right = u64::from(left) + u64::from(width);
    let bottom = u64::from(top) + u64::from(height);
    if width == 0 || height == 0 || right > transform.processed_width || bottom > transform.processed_height {
        return None;
    }
    Some((
        (f64::from(left) * transform.scale_x) as f32,
        (f64::from(top) * transform.scale_y) as f32,
        (right as f64 * transform.scale_x) as f32,
        (bottom as f64 * transform.scale_y) as f32,
    ))
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn preferred_ocr_elements(
    elements: &[crate::types::OcrElement],
    preferred_level: crate::types::OcrElementLevel,
) -> Vec<&crate::types::OcrElement> {
    let fallback_level = match preferred_level {
        crate::types::OcrElementLevel::Line => crate::types::OcrElementLevel::Word,
        crate::types::OcrElementLevel::Word => crate::types::OcrElementLevel::Line,
        _ => preferred_level,
    };
    let meaningful = elements
        .iter()
        .filter(|element| !element.text.trim().is_empty())
        .collect::<Vec<_>>();
    let selected_level = if meaningful.iter().any(|element| element.level == preferred_level) {
        Some(preferred_level)
    } else if meaningful.iter().any(|element| element.level == fallback_level) {
        Some(fallback_level)
    } else {
        None
    };

    meaningful
        .into_iter()
        .filter(|element| selected_level.is_none_or(|level| element.level == level))
        .collect()
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn transformed_ocr_elements(
    elements: &[crate::types::OcrElement],
    transform: OcrCoordinateTransform,
    preferred_level: crate::types::OcrElementLevel,
) -> Option<Vec<crate::types::OcrElement>> {
    preferred_ocr_elements(elements, preferred_level)
        .into_iter()
        .map(|element| {
            let (left, top, right, bottom) = transformed_ocr_bounds(&element.geometry, transform)?;
            let mut transformed = element.clone();
            transformed.geometry = crate::types::OcrBoundingGeometry::Rectangle {
                left: left.round() as u32,
                top: top.round() as u32,
                width: (right - left).round() as u32,
                height: (bottom - top).round() as u32,
            };
            Some(transformed)
        })
        .collect()
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn ocr_element_has_unique_full_containment(
    element: &crate::types::OcrElement,
    detections: &[crate::layout::LayoutDetection],
    transform: OcrCoordinateTransform,
) -> bool {
    let Some((left, top, right, bottom)) = transformed_ocr_bounds(&element.geometry, transform) else {
        return false;
    };
    let mut matches = detections.iter().filter(|detection| {
        left >= detection.bbox.x1
            && right <= detection.bbox.x2
            && top >= detection.bbox.y1
            && bottom <= detection.bbox.y2
    });
    matches.next().is_some() && matches.next().is_none()
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn whole_image_layout_mapping_retention(
    doc: &InternalDocument,
    detections: &[crate::layout::LayoutDetection],
    image_width: u32,
    image_height: u32,
) -> Option<f64> {
    let pages = doc.prebuilt_pages.as_ref()?;
    if pages.len() != 1 || pages[0].page_number != 1 {
        return None;
    }
    let elements = doc.prebuilt_ocr_elements.as_ref()?;
    let meaningful = preferred_ocr_elements(elements, crate::types::OcrElementLevel::Line);
    if meaningful.is_empty() || meaningful.iter().any(|element| element.page_number != 1) {
        return None;
    }
    let transform = whole_image_ocr_coordinate_transform(doc, image_width, image_height)?;
    let mut total_tokens = 0;
    let mut mapped_tokens = 0;
    for element in meaningful {
        let token_count = alphanumeric_tokens(&element.text).len();
        total_tokens += token_count;
        if ocr_element_has_unique_full_containment(element, detections, transform) {
            mapped_tokens += token_count;
        }
    }
    (total_tokens > 0).then_some(mapped_tokens as f64 / total_tokens as f64)
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn source_image_is_proven_single_frame(content: &[u8], mime_type: &str) -> bool {
    match mime_type {
        "image/png" | "image/webp" => {
            crate::extraction::image_decode::standard_image_is_single_frame(content, mime_type)
        }
        "image/jpeg" | "image/jpg" | "image/pjpeg" => !content.windows(4).any(|window| window == b"MPF\0"),
        "image/bmp"
        | "image/x-bmp"
        | "image/x-ms-bmp"
        | "image/x-portable-anymap"
        | "image/x-portable-bitmap"
        | "image/x-portable-graymap"
        | "image/x-portable-pixmap" => true,
        #[cfg(feature = "ocr")]
        "image/tiff" | "image/x-tiff" => {
            crate::extraction::image_decode::standard_image_is_single_frame(content, mime_type)
        }
        _ => false,
    }
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn push_mapped_layout_text(
    builder: &mut InternalDocumentBuilder,
    formulas: &mut Vec<crate::types::Formula>,
    detection: &crate::layout::LayoutDetection,
    text: &str,
) -> bool {
    use crate::layout::LayoutClass;
    use crate::types::internal::{ElementKind, InternalElement};

    match detection.class_name {
        LayoutClass::Title => {
            builder.push_heading(1, text, None, None);
        }
        LayoutClass::SectionHeader => {
            builder.push_heading(2, text, None, None);
        }
        LayoutClass::Code => {
            builder.push_code(text, None, None, None);
        }
        LayoutClass::Formula => {
            formulas.push(crate::types::Formula {
                latex: text.to_string(),
                bbox: Some(crate::types::BoundingBox {
                    x0: detection.bbox.x1 as f64,
                    y0: detection.bbox.y1 as f64,
                    x1: detection.bbox.x2 as f64,
                    y1: detection.bbox.y2 as f64,
                }),
                page: Some(1),
            });
            builder.push_element(InternalElement::text(ElementKind::Formula, text, 0));
        }
        LayoutClass::ListItem | LayoutClass::CheckboxSelected | LayoutClass::CheckboxUnselected => {
            builder.push_list_item(text, false, vec![], None, None);
        }
        LayoutClass::PageHeader | LayoutClass::PageFooter | LayoutClass::Picture | LayoutClass::Chart => return false,
        _ => {
            builder.push_paragraph(text, vec![], None, None);
        }
    }
    true
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn bbox_contains_element(bbox: crate::layout::BBox, element: &crate::types::OcrElement) -> bool {
    let (left, top, width, height) = ocr_geometry_bounds(&element.geometry);
    let element_left = left as f32;
    let element_top = top as f32;
    let element_right = element_left + width as f32;
    let element_bottom = element_top + height as f32;
    let element_area = width as f32 * height as f32;
    if element_area <= 0.0 {
        let center_x = element_left + width as f32 / 2.0;
        let center_y = element_top + height as f32 / 2.0;
        return center_x >= bbox.x1 && center_x <= bbox.x2 && center_y >= bbox.y1 && center_y <= bbox.y2;
    }
    let intersection_width = (element_right.min(bbox.x2) - element_left.max(bbox.x1)).max(0.0);
    let intersection_height = (element_bottom.min(bbox.y2) - element_top.max(bbox.y1)).max(0.0);
    intersection_width * intersection_height / element_area >= MIN_LAYOUT_OCR_ELEMENT_INTERSECTION_OVER_WORD_AREA
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn text_from_positioned_elements(elements: &[&crate::types::OcrElement]) -> String {
    let mut positioned = elements
        .iter()
        .map(|element| {
            let (left, top, _, height) = ocr_geometry_bounds(&element.geometry);
            (*element, left, top, height)
        })
        .collect::<Vec<_>>();
    positioned.sort_by(|left, right| left.2.cmp(&right.2).then_with(|| left.1.cmp(&right.1)));

    let mut text = String::new();
    let mut previous_line: Option<(u32, u32)> = None;
    for (element, _, top, height) in positioned {
        if !text.is_empty() {
            let is_new_line = previous_line.is_some_and(|(previous_top, previous_height)| {
                top.abs_diff(previous_top) > previous_height.max(height) / 2
            });
            text.push(if is_new_line { '\n' } else { ' ' });
        }
        text.push_str(element.text.trim());
        previous_line = Some((top, height));
    }
    text
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn push_cached_layout_region(
    builder: &mut InternalDocumentBuilder,
    formulas: &mut Vec<crate::types::Formula>,
    detection: &crate::layout::LayoutDetection,
    recognized_tables: &[crate::RecognizedTable],
    elements: &[crate::types::OcrElement],
    assigned: &mut [bool],
) {
    if let Some(recognized) = recognized_tables
        .iter()
        .find(|table| table.detection_bbox == detection.bbox)
    {
        builder.push_table(
            crate::types::Table {
                cells: recognized.cells.clone(),
                markdown: recognized.markdown.clone(),
                page_number: 1,
                bounding_box: Some(crate::types::BoundingBox {
                    x0: recognized.detection_bbox.x1 as f64,
                    y0: recognized.detection_bbox.y1 as f64,
                    x1: recognized.detection_bbox.x2 as f64,
                    y1: recognized.detection_bbox.y2 as f64,
                }),
                // `table_id`/`columns` are assigned once, in document push order, by
                // `finish_cached_layout_document` after all regions for this image have
                // been pushed — see that function for the deterministic scheme.
                ..Default::default()
            },
            Some(1),
            None,
        );
        return;
    }

    let region_elements = elements
        .iter()
        .enumerate()
        .filter_map(|(index, element)| {
            (!assigned[index] && bbox_contains_element(detection.bbox, element)).then_some((index, element))
        })
        .collect::<Vec<_>>();
    let positioned = region_elements.iter().map(|(_, element)| *element).collect::<Vec<_>>();
    let text = text_from_positioned_elements(&positioned);
    if text.trim().is_empty() {
        return;
    }
    if push_mapped_layout_text(builder, formulas, detection, &text) {
        for (index, _) in region_elements {
            assigned[index] = true;
        }
    }
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn finish_cached_layout_document(
    builder: InternalDocumentBuilder,
    whole_image_doc: &InternalDocument,
    detections: &[crate::layout::LayoutDetection],
    formulas: Vec<crate::types::Formula>,
    image_width: u32,
    image_height: u32,
) -> InternalDocument {
    let mut assembled = builder.build();
    // Deterministic id: `"table-N"` where N is this table's 1-based position among
    // this image's tables, in document (push) order — never randomness/wall-clock.
    // See `crate::types::Table::table_id` for the shared scheme doc.
    for (index, table) in assembled.tables.iter_mut().enumerate() {
        table.table_id = Some(format!("table-{}", index + 1));
        if table.columns.is_none() {
            table.columns = table.cells.first().cloned();
        }
    }
    assembled.metadata = whole_image_doc.metadata.clone();
    assembled.processing_warnings = whole_image_doc.processing_warnings.clone();
    assembled.prebuilt_ocr_elements = whole_image_doc.prebuilt_ocr_elements.clone();
    assembled.formulas = if formulas.is_empty() {
        whole_image_doc.formulas.clone()
    } else {
        formulas
    };
    let page_content = crate::rendering::render_plain(&assembled);
    assembled.prebuilt_pages = Some(vec![crate::types::PageContent {
        page_number: 1,
        content: page_content,
        tables: assembled.tables.iter().cloned().map(std::sync::Arc::new).collect(),
        image_indices: vec![],
        hierarchy: None,
        is_blank: None,
        layout_regions: Some(layout_regions_from_detections(detections, image_width, image_height)),
        speaker_notes: None,
        section_name: None,
        sheet_name: None,
        ocr_confidence: None,
        image_preprocessing: whole_image_doc.metadata.image_preprocessing.clone(),
    }]);
    ImageExtractor::mark_ocr_extraction(&mut assembled);
    assembled
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn cached_layout_adds_structure(
    detections: &[crate::layout::LayoutDetection],
    recognized_tables: &[crate::RecognizedTable],
) -> bool {
    use crate::layout::LayoutClass;

    !recognized_tables.is_empty()
        || detections.iter().any(|detection| {
            matches!(
                detection.class_name,
                LayoutClass::Title
                    | LayoutClass::SectionHeader
                    | LayoutClass::Code
                    | LayoutClass::Formula
                    | LayoutClass::ListItem
                    | LayoutClass::CheckboxSelected
                    | LayoutClass::CheckboxUnselected
            )
        })
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn layout_detection_accepts_text(detection: &crate::layout::LayoutDetection) -> bool {
    !matches!(
        detection.class_name,
        crate::layout::LayoutClass::PageHeader
            | crate::layout::LayoutClass::PageFooter
            | crate::layout::LayoutClass::Picture
            | crate::layout::LayoutClass::Chart
    )
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn element_claimed_by_layout(
    element: &crate::types::OcrElement,
    detections: &[crate::layout::LayoutDetection],
    recognized_tables: &[crate::RecognizedTable],
) -> bool {
    recognized_tables
        .iter()
        .any(|table| bbox_contains_element(table.detection_bbox, element))
        || detections
            .iter()
            .any(|detection| layout_detection_accepts_text(detection) && bbox_contains_element(detection.bbox, element))
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn ocr_element_position(element: &crate::types::OcrElement) -> (u32, u32) {
    let (left, top, _, _) = ocr_geometry_bounds(&element.geometry);
    (top, left)
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn push_unmatched_ocr(builder: &mut InternalDocumentBuilder, elements: &[&crate::types::OcrElement]) {
    let text = text_from_positioned_elements(elements);
    if !text.trim().is_empty() {
        builder.push_paragraph(text.trim(), vec![], Some(1), None);
    }
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn cached_layout_elements(
    whole_image_doc: &InternalDocument,
    image_width: u32,
    image_height: u32,
) -> Option<Vec<crate::types::OcrElement>> {
    let pages = whole_image_doc.prebuilt_pages.as_ref()?;
    if pages.len() != 1 || pages[0].page_number != 1 {
        return None;
    }
    let source_elements = whole_image_doc.prebuilt_ocr_elements.as_ref()?;
    let transform = whole_image_ocr_coordinate_transform(whole_image_doc, image_width, image_height)?;
    let elements = transformed_ocr_elements(source_elements, transform, crate::types::OcrElementLevel::Line)?;
    if elements.iter().any(|element| element.page_number != 1) {
        return None;
    }
    Some(elements)
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn ordered_cached_layout_items<'a>(
    detections: &'a [crate::layout::LayoutDetection],
    elements: &'a [crate::types::OcrElement],
    recognized_tables: &[crate::RecognizedTable],
) -> (
    Vec<&'a crate::layout::LayoutDetection>,
    Vec<&'a crate::types::OcrElement>,
) {
    // `prepare_layout_ocr` already applies the page-aware row/column ordering. ~keep
    let ordered_detections = detections.iter().collect::<Vec<_>>();
    let mut unmatched = elements
        .iter()
        .filter(|element| !element_claimed_by_layout(element, detections, recognized_tables))
        .collect::<Vec<_>>();
    unmatched.sort_by_key(|element| ocr_element_position(element));
    (ordered_detections, unmatched)
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn assemble_cached_layout_elements(
    whole_image_doc: &InternalDocument,
    detections: &[crate::layout::LayoutDetection],
    recognized_tables: &[crate::RecognizedTable],
    elements: &[crate::types::OcrElement],
    image_width: u32,
    image_height: u32,
) -> InternalDocument {
    let mut assigned = elements
        .iter()
        .map(|element| {
            recognized_tables
                .iter()
                .any(|table| bbox_contains_element(table.detection_bbox, element))
        })
        .collect::<Vec<_>>();
    let mut builder = InternalDocumentBuilder::new("image");
    let mut formulas = Vec::new();
    let (ordered_detections, unmatched) = ordered_cached_layout_items(detections, elements, recognized_tables);
    let mut unmatched_index = 0;
    for detection in ordered_detections {
        let detection_position = (detection.bbox.y1.max(0.0) as u32, detection.bbox.x1.max(0.0) as u32);
        let next_index = unmatched[unmatched_index..]
            .partition_point(|element| ocr_element_position(element) < detection_position)
            + unmatched_index;
        push_unmatched_ocr(&mut builder, &unmatched[unmatched_index..next_index]);
        unmatched_index = next_index;
        push_cached_layout_region(
            &mut builder,
            &mut formulas,
            detection,
            recognized_tables,
            elements,
            &mut assigned,
        );
    }
    push_unmatched_ocr(&mut builder, &unmatched[unmatched_index..]);

    finish_cached_layout_document(
        builder,
        whole_image_doc,
        detections,
        formulas,
        image_width,
        image_height,
    )
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn cached_document_has_structure(document: &InternalDocument) -> bool {
    use crate::types::internal::ElementKind;

    document.tables.iter().any(|table| {
        !table.markdown.trim().is_empty() || table.cells.iter().flatten().any(|cell| !cell.trim().is_empty())
    }) || document.elements.iter().any(|element| {
        matches!(
            element.kind,
            ElementKind::Heading { .. } | ElementKind::Code | ElementKind::Formula | ElementKind::ListItem { .. }
        )
    })
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn try_assemble_cached_layout_document(
    whole_image_doc: &InternalDocument,
    detections: &[crate::layout::LayoutDetection],
    recognized_tables: &[crate::RecognizedTable],
    image_width: u32,
    image_height: u32,
) -> Option<InternalDocument> {
    if !cached_layout_adds_structure(detections, recognized_tables) {
        return None;
    }
    let elements = cached_layout_elements(whole_image_doc, image_width, image_height)?;
    let assembled = assemble_cached_layout_elements(
        whole_image_doc,
        detections,
        recognized_tables,
        &elements,
        image_width,
        image_height,
    );
    if !cached_document_has_structure(&assembled) {
        return None;
    }
    let retained_tokens = alphanumeric_token_retention(
        &crate::rendering::render_plain(&assembled),
        &internal_document_text(whole_image_doc),
    );
    (retained_tokens >= REQUIRED_CACHED_LAYOUT_TOKEN_RETENTION).then_some(assembled)
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn layout_regions_from_detections(
    detections: &[crate::layout::LayoutDetection],
    image_width: u32,
    image_height: u32,
) -> Vec<crate::types::LayoutRegion> {
    let page_area = f64::from(image_width) * f64::from(image_height);
    if page_area == 0.0 {
        return Vec::new();
    }
    detections
        .iter()
        .filter_map(|detection| {
            let bbox = clipped_layout_bbox(detection.bbox, image_width, image_height)?;
            Some(crate::types::LayoutRegion {
                class_name: detection.class_name.to_string(),
                confidence: f64::from(detection.confidence),
                bounding_box: crate::types::BoundingBox {
                    x0: f64::from(bbox.x1),
                    y0: f64::from(bbox.y1),
                    x1: f64::from(bbox.x2),
                    y1: f64::from(bbox.y2),
                },
                area_fraction: f64::from(bbox.area()) / page_area,
            })
        })
        .collect()
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn clipped_layout_bbox(bbox: crate::layout::BBox, image_width: u32, image_height: u32) -> Option<crate::layout::BBox> {
    if ![bbox.x1, bbox.y1, bbox.x2, bbox.y2]
        .iter()
        .all(|value| value.is_finite())
    {
        return None;
    }
    let max_x = image_width as f32;
    let max_y = image_height as f32;
    let clipped = crate::layout::BBox::new(
        bbox.x1.clamp(0.0, max_x),
        bbox.y1.clamp(0.0, max_y),
        bbox.x2.clamp(0.0, max_x),
        bbox.y2.clamp(0.0, max_y),
    );
    (clipped.x2 > clipped.x1 && clipped.y2 > clipped.y1).then_some(clipped)
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn sanitize_layout_detections(
    detections: &mut Vec<crate::layout::LayoutDetection>,
    image_width: u32,
    image_height: u32,
) {
    detections.retain_mut(|detection| {
        let Some(bbox) = clipped_layout_bbox(detection.bbox, image_width, image_height) else {
            return false;
        };
        detection.bbox = bbox;
        true
    });
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn try_retain_canonical_whole_image_ocr(
    whole_image_doc: &InternalDocument,
    detections: &[crate::layout::LayoutDetection],
    image_width: u32,
    image_height: u32,
    source_is_single_frame: bool,
) -> Option<InternalDocument> {
    if !source_is_single_frame {
        return None;
    }
    let retention = whole_image_layout_mapping_retention(whole_image_doc, detections, image_width, image_height);
    let mut retained = whole_image_doc.clone();
    let pages = retained.prebuilt_pages.as_mut()?;
    if pages.len() != 1 || pages[0].page_number != 1 {
        return None;
    }

    // A successful single-frame whole-image OCR is the lossless terminal fallback;
    // repeating OCR per region is expensive and can discard canonical tokens. ~keep
    pages[0].layout_regions = Some(layout_regions_from_detections(detections, image_width, image_height));
    tracing::debug!(
        ?retention,
        "Retained canonical whole-image OCR after cached layout assembly was unavailable"
    );
    Some(retained)
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
async fn detect_image_layout(
    content: &[u8],
    layout_config: crate::core::config::LayoutDetectionConfig,
    thread_budget: usize,
    security_limits: crate::extractors::security::SecurityLimits,
) -> Result<(image::RgbImage, crate::layout::DetectionResult)> {
    let layout_content = content.to_vec();
    tokio::task::spawn_blocking(move || -> Result<_> {
        let rgb =
            crate::extraction::image::decode_image_to_rgb8_with_security_limits(&layout_content, &security_limits)?;
        drop(layout_content);
        let mut engine = crate::layout::take_or_create_engine(&layout_config, thread_budget)
            .map_err(|error| crate::XbergError::Other(format!("Layout engine init failed: {error}")))?;
        let detection = engine.detect_with_security_limits(&rgb, &security_limits);
        crate::layout::return_engine(engine);
        let detection =
            detection.map_err(|error| crate::XbergError::Other(format!("Layout detection failed: {error}")))?;
        Ok((rgb, detection))
    })
    .await
    .map_err(|error| crate::XbergError::Other(format!("Image layout worker failed: {error}")))?
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn sort_layout_detections(detections: &mut [crate::layout::LayoutDetection], image_height: u32) {
    let row_threshold = (image_height as f32 * LAYOUT_READING_ORDER_ROW_HEIGHT_RATIO).max(1.0);
    detections.sort_by(|left, right| {
        let left_y = (left.bbox.y1 + left.bbox.y2) / 2.0;
        let right_y = (right.bbox.y1 + right.bbox.y2) / 2.0;
        let left_row = (left_y / row_threshold) as i64;
        let right_row = (right_y / row_threshold) as i64;
        left_row.cmp(&right_row).then_with(|| {
            let left_x = (left.bbox.x1 + left.bbox.x2) / 2.0;
            let right_x = (right.bbox.x1 + right.bbox.x2) / 2.0;
            left_x.total_cmp(&right_x)
        })
    });
}

/// Shared blocking-wrapper entry in the recognition module.
#[cfg(all(feature = "formula-recognition", any(feature = "ocr", feature = "ocr-wasm")))]
use crate::formula_recognition::recognize_crop_blocking as recognize_formula_crop_blocking;

/// Recognize the formula regions of an assembled cached-layout document,
/// replacing the plain OCR text in both the side channel and the matching
/// element. Failures keep the OCR text.
#[cfg(all(feature = "formula-recognition", any(feature = "ocr", feature = "ocr-wasm")))]
async fn recognize_assembled_formula_regions(
    doc: &mut InternalDocument,
    rgb: &image::RgbImage,
    detections: &[crate::layout::LayoutDetection],
    layout: &crate::core::config::LayoutDetectionConfig,
) {
    if layout.formula_model.is_none() {
        return;
    }
    for detection in detections
        .iter()
        .filter(|d| matches!(d.class_name, crate::layout::LayoutClass::Formula))
    {
        let Some(crop) = crop_layout_region(rgb, detection) else {
            continue;
        };
        match recognize_formula_crop_blocking(crop, layout.acceleration.clone()).await {
            Ok(Some(latex)) => {
                let target = crate::types::BoundingBox {
                    x0: detection.bbox.x1 as f64,
                    y0: detection.bbox.y1 as f64,
                    x1: detection.bbox.x2 as f64,
                    y1: detection.bbox.y2 as f64,
                };
                // The layout pass pushed the side-channel formula and its
                // element together from the same detection, so both hold the
                // detection's values; match the formula by its own bbox and
                // its element by the not-yet-replaced OCR text, front to back,
                // so repeated text on one page pairs positionally.
                for formula in doc.formulas.iter_mut() {
                    if formula.bbox == Some(target) && formula.latex != latex {
                        let ocr_text = formula.latex.clone();
                        if let Some(element) = doc.elements.iter_mut().find(|e| {
                            matches!(e.kind, crate::types::internal::ElementKind::Formula) && e.text == ocr_text
                        }) {
                            element.text = latex.clone();
                        }
                        formula.latex = latex.clone();
                        break;
                    }
                }
            }
            Ok(None) => {}
            Err(error) => {
                tracing::warn!(error = %error, "cached-path formula recognition failed; keeping OCR text");
            }
        }
    }
}

/// Crop the detection's region out of the page raster. `None` when the
/// clamped region is empty.
#[cfg(all(feature = "formula-recognition", any(feature = "ocr", feature = "ocr-wasm")))]
fn crop_layout_region(rgb: &image::RgbImage, detection: &crate::layout::LayoutDetection) -> Option<image::RgbImage> {
    let (x, y, w, h) = detection.bbox.clamp_to_image(rgb.width(), rgb.height())?;
    Some(image::imageops::crop_imm(rgb, x, y, w, h).to_image())
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn encode_layout_region(rgb: &image::RgbImage, detection: &crate::layout::LayoutDetection) -> Result<Option<Vec<u8>>> {
    use image::ImageEncoder;

    if matches!(
        detection.class_name,
        crate::layout::LayoutClass::Picture | crate::layout::LayoutClass::Chart
    ) {
        return Ok(None);
    }
    let Some((x1, y1, crop_width, crop_height)) = detection.bbox.clamp_to_image(rgb.width(), rgb.height()) else {
        return Ok(None);
    };
    if crop_width < MIN_LAYOUT_CROP_DIMENSION || crop_height < MIN_LAYOUT_CROP_DIMENSION {
        return Ok(None);
    }
    let crop = image::imageops::crop_imm(rgb, x1, y1, crop_width, crop_height).to_image();
    let mut png = std::io::Cursor::new(Vec::new());
    image::codecs::png::PngEncoder::new(&mut png)
        .write_image(
            crop.as_raw(),
            crop.width(),
            crop.height(),
            image::ExtendedColorType::Rgb8,
        )
        .map_err(|error| crate::XbergError::Other(format!("Failed to encode crop as PNG: {error}")))?;
    Ok(Some(png.into_inner()))
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn build_region_ocr_document(
    builder: InternalDocumentBuilder,
    formulas: Vec<crate::types::Formula>,
    processing_warnings: Vec<crate::types::ProcessingWarning>,
) -> InternalDocument {
    let mut doc = builder.build();
    doc.metadata = Metadata {
        output_format: Some("markdown".to_string()),
        ..Default::default()
    };
    doc.formulas = formulas;
    doc.processing_warnings = processing_warnings;
    ImageExtractor::mark_ocr_extraction(&mut doc);
    doc
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
async fn extract_layout_regions(
    backend: std::sync::Arc<dyn crate::plugins::OcrBackend>,
    rgb: &image::RgbImage,
    detections: &[crate::layout::LayoutDetection],
    ocr_config: &crate::core::config::OcrConfig,
    layout_config: Option<&crate::core::config::LayoutDetectionConfig>,
    cancel_token: Option<&crate::cancellation::CancellationToken>,
) -> Result<InternalDocument> {
    let mut builder = InternalDocumentBuilder::new("image");
    let mut formulas = Vec::new();
    let mut processing_warnings = Vec::new();
    #[cfg(not(feature = "formula-recognition"))]
    let _ = layout_config;
    for detection in detections {
        // A timed-out extraction cancels this token (see
        // `ExtractionConfig::ensure_cancel_token`); each region can involve a
        // formula-recognition or OCR call, so stop dispatching further regions
        // once cancelled rather than continuing until the last one finishes.
        if cancel_token.is_some_and(crate::cancellation::CancellationToken::is_cancelled) {
            processing_warnings.push(crate::core::diagnostics::warning(
                "layout-ocr",
                "extraction cancelled; remaining layout regions were not OCR'd".to_string(),
            ));
            break;
        }
        // A configured formula model takes the region crop directly; the
        // plain OCR text is the fallback when recognition yields nothing.
        #[cfg(feature = "formula-recognition")]
        if matches!(detection.class_name, crate::layout::LayoutClass::Formula)
            && let Some(layout) = layout_config
            && layout.formula_model.is_some()
            && let Some(crop) = crop_layout_region(rgb, detection)
        {
            match recognize_formula_crop_blocking(crop, layout.acceleration.clone()).await {
                Ok(Some(latex)) => {
                    push_mapped_layout_text(&mut builder, &mut formulas, detection, &latex);
                    continue;
                }
                Ok(None) => {}
                Err(error) => {
                    tracing::warn!(error = %error, "formula recognition failed; using OCR text for the region");
                    processing_warnings.push(crate::core::diagnostics::warning(
                        "formula-recognition",
                        format!("formula region fell back to OCR text: {error}"),
                    ));
                }
            }
        }
        let Some(crop_bytes) = encode_layout_region(rgb, detection)? else {
            continue;
        };
        let ocr_result = backend.process_image(&crop_bytes, ocr_config).await?;
        processing_warnings.extend(ocr_result.processing_warnings);
        let text = ocr_result.content.trim();
        if text.is_empty() {
            continue;
        }
        tracing::trace!(
            class = ?detection.class_name,
            confidence = detection.confidence,
            text_len = text.len(),
            "OCR result for layout region"
        );
        push_mapped_layout_text(&mut builder, &mut formulas, detection, text);
    }
    Ok(build_region_ocr_document(builder, formulas, processing_warnings))
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
async fn extract_selected_image_ocr_path<Layout, LayoutFuture, Whole, WholeFuture>(
    use_layout: bool,
    layout: Layout,
    whole: Whole,
) -> Result<InternalDocument>
where
    Layout: FnOnce() -> LayoutFuture,
    LayoutFuture: std::future::Future<Output = Result<InternalDocument>>,
    Whole: FnOnce() -> WholeFuture,
    WholeFuture: std::future::Future<Output = Result<InternalDocument>>,
{
    if use_layout { layout().await } else { whole().await }
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
enum LayoutOcrPreparation {
    Complete(InternalDocument),
    Detected {
        whole_image_result: Result<InternalDocument>,
        rgb: std::sync::Arc<image::RgbImage>,
        detections: Vec<crate::layout::LayoutDetection>,
    },
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn share_detected_image(rgb: image::RgbImage) -> std::sync::Arc<image::RgbImage> {
    std::sync::Arc::new(rgb)
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
async fn prepare_layout_ocr(
    extractor: &ImageExtractor,
    content: &[u8],
    mime_type: &str,
    config: &ExtractionConfig,
    layout_config: crate::core::config::LayoutDetectionConfig,
) -> Result<LayoutOcrPreparation> {
    let whole_image_result = extractor.extract_with_ocr(content, mime_type, config).await;
    let thread_budget = crate::core::config::concurrency::resolve_thread_budget(config.concurrency.as_ref());
    let security_limits = config.security_limits.clone().unwrap_or_default();
    let (rgb, detection) = match detect_image_layout(content, layout_config, thread_budget, security_limits).await {
        Ok(result) => result,
        Err(error) => {
            return cached_whole_image_after_layout_error(&whole_image_result, error)
                .map(LayoutOcrPreparation::Complete);
        }
    };
    let rgb = share_detected_image(rgb);
    tracing::info!(
        detections = detection.detections.len(),
        img_width = rgb.width(),
        img_height = rgb.height(),
        "Layout detection completed for image"
    );
    if detection.detections.is_empty() {
        tracing::debug!("No layout regions detected, retaining whole-image OCR");
        return whole_image_result.map(LayoutOcrPreparation::Complete);
    }
    let mut detections = detection.detections;
    sanitize_layout_detections(&mut detections, rgb.width(), rgb.height());
    if detections.is_empty() {
        tracing::debug!("No valid layout regions detected, retaining whole-image OCR");
        return whole_image_result.map(LayoutOcrPreparation::Complete);
    }
    sort_layout_detections(&mut detections, rgb.height());
    Ok(LayoutOcrPreparation::Detected {
        whole_image_result,
        rgb,
        detections,
    })
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn configured_region_ocr(
    config: &ExtractionConfig,
    ocr_config: &crate::core::config::OcrConfig,
) -> Result<(
    std::sync::Arc<dyn crate::plugins::OcrBackend>,
    crate::core::config::OcrConfig,
)> {
    crate::plugins::ensure_ocr_backends_initialized();
    let registry = crate::plugins::registry::get_ocr_backend_registry();
    let backend = registry.read().get(&ocr_config.backend)?;
    let mut region_config = ocr_config.clone();
    region_config.output_format = Some(crate::core::config::OutputFormat::Plain);
    if region_config.backend == "tesseract" {
        apply_default_tesseract_psm(&mut region_config, LAYOUT_REGION_TESSERACT_PSM);
        // Layout assembly consumes region text only; skip redundant Tesseract hOCR and
        // document-level table reconstruction when region OCR is unavoidable. ~keep
        let tesseract_config = region_config.tesseract_config.get_or_insert_default();
        tesseract_config.output_format = "text".to_string();
        tesseract_config.enable_table_detection = false;
    }
    if region_config.acceleration.is_none() {
        region_config.acceleration = config.acceleration.clone();
    }
    Ok((backend, region_config))
}

#[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
fn apply_default_tesseract_psm(config: &mut crate::core::config::OcrConfig, psm: i32) {
    if config.backend != "tesseract" || config.tesseract_config.is_some() {
        return;
    }

    let tesseract_config = crate::types::TesseractConfig {
        language: config.language.clone(),
        psm,
        ..Default::default()
    };
    config.tesseract_config = Some(tesseract_config);
}

#[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
fn apply_default_whole_image_tesseract_psm(config: &mut crate::core::config::OcrConfig) {
    let psm = if has_vertical_tesseract_language(config) {
        VERTICAL_BLOCK_TESSERACT_PSM
    } else {
        WHOLE_IMAGE_TESSERACT_PSM
    };
    apply_default_tesseract_psm(config, psm);
}

#[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
fn has_vertical_tesseract_language(config: &crate::core::config::OcrConfig) -> bool {
    config
        .language
        .iter()
        .flat_map(|language| language.split('+'))
        .any(|language| language.trim().to_ascii_lowercase().ends_with("_vert"))
}

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
fn usable_word_confidences(result: &crate::types::ExtractedDocument) -> Vec<f64> {
    result
        .ocr_elements
        .iter()
        .flatten()
        .filter(|element| {
            element.level == crate::types::OcrElementLevel::Word
                && !element.text.trim().is_empty()
                && element.confidence.recognition.is_finite()
        })
        .map(|element| element.confidence.recognition)
        .collect()
}

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
fn should_retry_sparse_image_ocr(
    config: &crate::core::config::OcrConfig,
    result: &crate::types::ExtractedDocument,
) -> bool {
    is_implicit_horizontal_tesseract(config)
        && usable_word_confidences(result).len() <= SPARSE_IMAGE_OCR_WORD_LIMIT
        && !has_robust_word_confidence_distribution(result)
}

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
fn is_implicit_horizontal_tesseract(config: &crate::core::config::OcrConfig) -> bool {
    config.backend == "tesseract" && config.tesseract_config.is_none() && !has_vertical_tesseract_language(config)
}

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
fn has_robust_word_confidence_distribution(result: &crate::types::ExtractedDocument) -> bool {
    let mut confidences = usable_word_confidences(result);
    if confidences.is_empty() {
        return false;
    }
    confidences.sort_by(f64::total_cmp);
    let percentile_index = ((confidences.len() as f64 - 1.0) * SPARSE_IMAGE_OCR_CONFIDENCE_PERCENTILE).floor() as usize;
    let low_confidence_count = confidences
        .iter()
        .filter(|confidence| **confidence < SPARSE_IMAGE_OCR_MIN_WORD_CONFIDENCE)
        .count();
    let low_confidence_ratio = low_confidence_count as f64 / confidences.len() as f64;

    confidences[percentile_index] >= SPARSE_IMAGE_OCR_MIN_WORD_CONFIDENCE
        && low_confidence_ratio <= SPARSE_IMAGE_OCR_MAX_LOW_CONFIDENCE_RATIO
}

#[cfg(all(
    not(target_arch = "wasm32"),
    any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
))]
fn sparse_image_ocr_fallback_config(
    whole_image_config: &crate::core::config::OcrConfig,
) -> crate::core::config::OcrConfig {
    let mut fallback_config = whole_image_config.clone();
    let tesseract_config = fallback_config.tesseract_config.get_or_insert_default();
    tesseract_config.psm = SPARSE_IMAGE_OCR_FALLBACK_PSM;
    let preprocessing = crate::types::ImagePreprocessingConfig {
        deskew: false,
        contrast_enhance: true,
        binarization_method: "none".to_string(),
        ..Default::default()
    };
    tesseract_config.preprocessing = Some(preprocessing);
    fallback_config
}

/// Resize/re-DPI raw image bytes for OCR using `ExtractionConfig::images`
/// (`ImageExtractionConfig`) before handing them to an OCR backend.
///
/// OCR backends only ever see `OcrConfig` (via the `OcrBackend` trait), and
/// `OcrConfig` has no field that traces back to `ExtractionConfig::images` — so
/// `target_dpi`, `max_image_dimension`, `auto_adjust_dpi`, `min_dpi`, and
/// `max_dpi` were parsed into config but silently dropped before reaching any
/// backend (issue #209). Normalizing the bytes once, here, at the extractor
/// boundary makes the setting effective for every backend without touching the
/// backend trait or its config types.
///
/// Falls back to the original bytes unchanged if decoding or normalization
/// fails; OCR should still be attempted on the original image rather than
/// aborting the extraction.
#[cfg(feature = "ocr-pipeline")]
struct NormalizedOcrImage {
    bytes: Vec<u8>,
    metadata: Option<crate::types::ImagePreprocessingMetadata>,
}

#[cfg(feature = "ocr-pipeline")]
fn unchanged_ocr_image(content: &[u8]) -> NormalizedOcrImage {
    NormalizedOcrImage {
        bytes: content.to_vec(),
        metadata: None,
    }
}

#[cfg(feature = "ocr-pipeline")]
fn normalize_image_bytes_for_ocr(
    content: &[u8],
    images_config: &crate::core::config::ImageExtractionConfig,
    security_limits: &crate::extractors::security::SecurityLimits,
) -> Result<NormalizedOcrImage> {
    let rgb = match crate::extraction::image::decode_image_to_rgb8_with_security_limits(content, security_limits) {
        Ok(decoded) => decoded,
        Err(error @ crate::XbergError::Validation { .. }) => return Err(error),
        Err(_) => return Ok(unchanged_ocr_image(content)),
    };
    let (width, height) = rgb.dimensions();
    let dpi_config = crate::types::ImageDpiConfig::from(images_config);
    let (planned_width, planned_height) =
        crate::image::preprocessing::normalized_image_dimensions(width, height, &dpi_config, None);
    let encoded_source_bytes = u64::try_from(content.len())
        .map_err(|_| crate::extraction::image_decode::image_dimension_error(width, height, u64::MAX, u64::MAX))?;
    let current_bytes = u64::try_from(rgb.as_raw().len())
        .ok()
        .and_then(|bytes| bytes.checked_add(encoded_source_bytes))
        .ok_or_else(|| crate::extraction::image_decode::image_dimension_error(width, height, u64::MAX, u64::MAX))?;
    let planned_bytes = crate::extraction::image_decode::decoded_byte_count(planned_width, planned_height, 3)?;
    if (planned_width, planned_height) != (width, height) {
        crate::extraction::image_decode::validate_image_live_bytes(
            width,
            height,
            current_bytes,
            planned_bytes,
            security_limits,
        )?;
    }
    let result = match crate::image::preprocessing::normalize_image_dpi_owned(
        rgb.into_raw(),
        width as usize,
        height as usize,
        &dpi_config,
        None,
    ) {
        Ok(result) => result,
        Err((error, _)) => {
            tracing::warn!(%error, "failed to normalize OCR image; using original image");
            return Ok(unchanged_ocr_image(content));
        }
    };
    let (new_width, new_height) = result.dimensions;
    let new_width = u32::try_from(new_width)
        .map_err(|_| crate::extraction::image_decode::image_dimension_error(u32::MAX, u32::MAX, u64::MAX, u64::MAX))?;
    let new_height = u32::try_from(new_height)
        .map_err(|_| crate::extraction::image_decode::image_dimension_error(new_width, u32::MAX, u64::MAX, u64::MAX))?;
    let normalized_bytes = u64::try_from(result.rgb_data.len())
        .ok()
        .and_then(|bytes| bytes.checked_add(encoded_source_bytes))
        .ok_or_else(|| {
            crate::extraction::image_decode::image_dimension_error(new_width, new_height, u64::MAX, u64::MAX)
        })?;
    let encode_live_bytes = crate::extraction::image_decode::decoded_byte_count(
        new_width,
        new_height,
        NORMALIZED_PNG_ENCODE_BYTES_PER_PIXEL,
    )?
    .checked_add(NORMALIZED_PNG_ENCODE_FIXED_BYTES)
    .ok_or_else(|| crate::extraction::image_decode::image_dimension_error(new_width, new_height, u64::MAX, u64::MAX))?;
    crate::extraction::image_decode::validate_image_live_bytes(
        new_width,
        new_height,
        normalized_bytes,
        encode_live_bytes,
        security_limits,
    )?;
    let bytes = match encode_rgb_as_png(&result.rgb_data, new_width, new_height) {
        Ok(bytes) => bytes,
        Err(error) => {
            tracing::warn!(%error, "failed to encode normalized OCR image; using original image");
            return Ok(unchanged_ocr_image(content));
        }
    };
    Ok(NormalizedOcrImage {
        bytes,
        metadata: Some(result.metadata),
    })
}

#[cfg(feature = "ocr-pipeline")]
fn encode_rgb_as_png(rgb_data: &[u8], width: u32, height: u32) -> Result<Vec<u8>> {
    use image::ImageEncoder;

    let mut png = std::io::Cursor::new(Vec::new());
    image::codecs::png::PngEncoder::new(&mut png)
        .write_image(rgb_data, width, height, image::ExtendedColorType::Rgb8)
        .map_err(|error| crate::XbergError::Other(format!("Failed to encode normalized image as PNG: {error}")))?;
    Ok(png.into_inner())
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn uses_tatr_image_table_recognition(table_model: crate::core::config::layout::TableModel) -> bool {
    use crate::core::config::layout::TableModel;

    match table_model {
        TableModel::Tatr => true,
        TableModel::Disabled => false,
        // TODO: SLANeXT image integration needs the same model routing and cell-assignment
        // contract as the PDF structure pipeline. Preserve the existing OCR fallback
        // until that can be shared without duplicating model orchestration. ~keep
        TableModel::SlanetWired | TableModel::SlanetWireless | TableModel::SlanetPlus | TableModel::SlanetAuto => false,
    }
}

#[cfg(all(
    feature = "layout-detection",
    feature = "pdf",
    any(feature = "ocr", feature = "ocr-wasm")
))]
async fn recognize_cached_image_tables(
    whole_image_doc: &InternalDocument,
    rgb: &std::sync::Arc<image::RgbImage>,
    detections: &[crate::layout::LayoutDetection],
    config: &ExtractionConfig,
) -> Vec<crate::RecognizedTable> {
    let Some(layout_config) = config.layout.as_ref() else {
        return Vec::new();
    };
    if !uses_tatr_image_table_recognition(layout_config.table_model) {
        return Vec::new();
    }
    let Some(source_elements) = whole_image_doc.prebuilt_ocr_elements.as_ref() else {
        return Vec::new();
    };
    let Some(transform) = whole_image_ocr_coordinate_transform(whole_image_doc, rgb.width(), rgb.height()) else {
        return Vec::new();
    };
    let Some(elements) = transformed_ocr_elements(source_elements, transform, crate::types::OcrElementLevel::Word)
    else {
        return Vec::new();
    };

    let page_image = std::sync::Arc::clone(rgb);
    let detection = crate::layout::DetectionResult {
        page_width: rgb.width(),
        page_height: rgb.height(),
        detections: detections.to_vec(),
    };
    let acceleration = config.resolved_layout_acceleration().cloned();
    let thread_budget = crate::core::config::concurrency::resolve_thread_budget(config.concurrency.as_ref());
    tokio::task::spawn_blocking(move || {
        let Some(mut model) = crate::layout::take_or_create_tatr(acceleration.as_ref(), thread_budget) else {
            return Vec::new();
        };
        crate::ocr::layout_assembly::recognize_page_tables(&page_image, &detection, &elements, &mut model)
    })
    .await
    .unwrap_or_else(|error| {
        tracing::warn!(%error, "Image table recognition worker failed; retaining OCR fallback");
        Vec::new()
    })
}

/// Returns `true` when the OCR backend configured in `config` self-declares that it
/// emits structured markdown directly. End-to-end VLM backends (PaddleOCR-VL,
/// future GOT-OCR / GLM-OCR) emit markdown in one forward pass and should
/// bypass the layout-detection + region-cropping + table-reconstruction stages.
///
/// Returns `false` when no OCR config is set, no backend is registered under the
/// configured name, or the backend uses the default classical contract.
#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn ocr_backend_emits_structured_markdown(config: &ExtractionConfig) -> bool {
    let Some(ocr) = config.ocr.as_ref() else {
        return false;
    };
    crate::plugins::ensure_ocr_backends_initialized();
    let registry = crate::plugins::registry::get_ocr_backend_registry();
    let registry = registry.read();
    registry
        .get(&ocr.backend)
        .map(|b| b.emits_structured_markdown())
        .unwrap_or(false)
}

#[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
fn should_use_layout_ocr(config: &ExtractionConfig) -> bool {
    config.layout.is_some() && config.ocr.is_some() && !ocr_backend_emits_structured_markdown(config)
}

#[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
fn enable_image_ocr_elements(config: &mut crate::core::config::OcrConfig, include_words: bool) {
    let element_config = config
        .element_config
        .get_or_insert_with(crate::types::OcrElementConfig::default);
    element_config.include_elements = true;
    if include_words {
        element_config.min_level = crate::types::OcrElementLevel::Word;
    }
}

#[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
fn apply_public_image_ocr_element_policy(document: &mut InternalDocument, config: &crate::core::config::OcrConfig) {
    document.prebuilt_ocr_elements = config.select_public_elements(document.prebuilt_ocr_elements.take());
}

#[cfg_attr(alef, alef(skip))]
/// Image extractor for various image formats.
///
/// Supports: PNG, JPEG, WebP, BMP, TIFF, GIF.
/// Extracts dimensions, format, and EXIF metadata.
/// Optionally runs OCR when configured.
/// When layout detection is also enabled, uses per-region OCR with
/// markdown formatting based on detected layout classes.
pub struct ImageExtractor;

impl ImageExtractor {
    /// Create a new image extractor.
    pub(crate) fn new() -> Self {
        Self
    }

    fn mark_ocr_extraction(doc: &mut InternalDocument) {
        doc.metadata.ocr_used = true;
        doc.metadata.additional.insert(
            std::borrow::Cow::Borrowed("extraction_method"),
            serde_json::Value::String(crate::types::ExtractionMethod::Ocr.as_str().to_string()),
        );
    }

    /// Extract text from image using OCR with optional page tracking for multi-frame TIFFs.
    #[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
    async fn extract_with_ocr(
        &self,
        content: &[u8],
        mime_type: &str,
        config: &ExtractionConfig,
    ) -> Result<InternalDocument> {
        use crate::plugins::registry::get_ocr_backend_registry;

        let default_ocr_config;
        let ocr_config = match config.ocr.as_ref() {
            Some(c) => c,
            None => {
                default_ocr_config = crate::core::config::OcrConfig::default();
                &default_ocr_config
            }
        };

        // `vlm_fallback` and explicit multi-stage pipelines were only honoured by the
        // PDF extractor, so a bare image upload always used a single backend and never
        // fell back to the VLM (issue #1339). When such a policy is configured, route
        // the image through the same shared pipeline runner. Gated on `pdf` because the
        // runner lives in that module; a build without `pdf` keeps the single-backend
        // path unchanged. The default (no fallback, no explicit pipeline) also keeps the
        // fast path, so ordinary image OCR is unaffected.
        #[cfg(all(feature = "pdf", any(feature = "ocr", feature = "ocr-pipeline")))]
        {
            let wants_pipeline = ocr_config.vlm_fallback != crate::core::config::VlmFallbackPolicy::Disabled
                || ocr_config.pipeline.is_some();
            if wants_pipeline && let Some(pipeline) = ocr_config.effective_pipeline() {
                return self.extract_with_ocr_pipeline(content, config, &pipeline).await;
            }
        }

        let backend = {
            crate::plugins::ensure_ocr_backends_initialized();
            let registry = get_ocr_backend_registry();
            let registry = registry.read();
            registry.get(&ocr_config.backend)?
        };

        let mut ocr_config_with_format = ocr_config.clone();
        apply_default_whole_image_tesseract_psm(&mut ocr_config_with_format);
        ocr_config_with_format.output_format = Some(config.output_format.clone());
        ocr_config_with_format.acceleration = config.acceleration.clone();
        #[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
        let include_words = should_use_layout_ocr(config);
        #[cfg(not(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm"))))]
        let include_words = false;
        enable_image_ocr_elements(&mut ocr_config_with_format, include_words);
        #[cfg(not(target_arch = "wasm32"))]
        if is_implicit_horizontal_tesseract(ocr_config) {
            enable_image_ocr_elements(&mut ocr_config_with_format, true);
        }

        // OCR backends only see `OcrConfig`, which has no route back to
        // `ExtractionConfig::images`, so DPI/dimension normalization from
        // `ImageExtractionConfig` has to happen here, once, before any backend
        // ever sees the bytes (issue #209). ~keep
        #[cfg(feature = "ocr-pipeline")]
        let default_security_limits = crate::extractors::security::SecurityLimits::default();
        #[cfg(feature = "ocr-pipeline")]
        let security_limits = config.security_limits.as_ref().unwrap_or(&default_security_limits);
        #[cfg(feature = "ocr-pipeline")]
        let normalized_ocr_bytes = config
            .images
            .as_ref()
            .map(|images_config| normalize_image_bytes_for_ocr(content, images_config, security_limits))
            .transpose()?;
        #[cfg(feature = "ocr-pipeline")]
        let ocr_input: &[u8] = normalized_ocr_bytes
            .as_ref()
            .map(|normalized| normalized.bytes.as_slice())
            .unwrap_or(content);
        #[cfg(not(feature = "ocr-pipeline"))]
        let ocr_input: &[u8] = content;

        let ocr_result = backend.process_image(ocr_input, &ocr_config_with_format).await?;
        #[cfg(not(target_arch = "wasm32"))]
        let ocr_result = {
            let mut ocr_result = ocr_result;
            if should_retry_sparse_image_ocr(ocr_config, &ocr_result) {
                let fallback_config = sparse_image_ocr_fallback_config(&ocr_config_with_format);
                match backend.process_image(ocr_input, &fallback_config).await {
                    Ok(mut fallback_result) if has_robust_word_confidence_distribution(&fallback_result) => {
                        let mut processing_warnings = ocr_result.processing_warnings.clone();
                        processing_warnings.append(&mut fallback_result.processing_warnings);
                        fallback_result.processing_warnings = processing_warnings;
                        ocr_result = fallback_result;
                    }
                    Ok(_) => {}
                    Err(error) => tracing::warn!(%error, "sparse standalone image OCR fallback failed"),
                }
            }
            ocr_result
        };
        #[cfg(feature = "ocr-pipeline")]
        let ocr_result = {
            let mut ocr_result = ocr_result;
            if let Some(metadata) = normalized_ocr_bytes.and_then(|normalized| normalized.metadata) {
                ocr_result.metadata.image_preprocessing = Some(metadata);
            }
            ocr_result
        };

        let ocr_content = ocr_result.content;
        let ocr_metadata = ocr_result.metadata;
        let ocr_elements = ocr_result.ocr_elements;
        let ocr_formulas = ocr_result.formulas;
        let processing_warnings = ocr_result.processing_warnings;
        // `ExtractedDocument::tables` (tables reconstructed by OCR table
        // detection) used to be dropped here entirely: nothing on the
        // standalone-image path ever read it, so a table found in a bare
        // image never reached the output.
        #[cfg(feature = "ocr")]
        let ocr_tables = ocr_result.tables;
        #[cfg(feature = "ocr")]
        let ocr_internal_document = ocr_result.ocr_internal_document;

        // ~keep The whole image is one OCR run, so its confidence describes page 1 and only
        // page 1. The multi-frame TIFF branch below splits that single run's text by byte
        // offset into several pages with no per-frame OCR of their own, so those pages keep
        // `ocr_confidence: None` rather than repeating a figure that was never measured for
        // them (#1568). Gated on `pdf` because the shared builder lives in that module.
        #[cfg(all(feature = "ocr", feature = "pdf"))]
        let whole_image_ocr_confidence = crate::extractors::pdf::ocr::page_ocr_confidence(
            backend.confidence_semantics(),
            crate::extractors::pdf::ocr::mean_text_conf_of(&ocr_metadata.additional),
            crate::extractors::pdf::ocr::word_count_of(&ocr_metadata.additional).unwrap_or(0),
            backend.name(),
        );
        #[cfg(all(feature = "ocr", not(feature = "pdf")))]
        let whole_image_ocr_confidence: Option<crate::types::page::PageOcrConfidence> = None;

        #[cfg(feature = "ocr")]
        {
            let ocr_extraction_result = crate::extraction::image::extract_text_from_image_with_ocr(
                content,
                mime_type,
                ocr_content,
                config.pages.as_ref(),
            )?;

            // The standalone-image path has no document-global structure pipeline
            // behind it (unlike the PDF mixed route), so it is the one place that
            // still needs to detect headings itself. Prefer the hOCR-derived
            // element tree — which still carries per-paragraph `x_fsize` — so
            // headings become real `Heading` elements the renderer understands,
            // rather than a pre-escaped `## ` string baked into plain text (which
            // produced `\#\#` under markdown escaping and leaked `## ` into
            // `Plain` output). Only usable when OCR ran on the whole image in one
            // pass: multi-frame TIFF page tracking slices `content` by byte
            // offset and has no per-frame correspondence to hOCR elements, so it
            // keeps the flat paragraph-split fallback.
            let use_hocr_headings = ocr_extraction_result.page_contents.is_none();
            let mut doc = match &ocr_internal_document {
                Some(internal_doc) if use_hocr_headings && !internal_doc.elements.is_empty() => {
                    build_image_internal_document_from_hocr_elements(&internal_doc.elements)
                }
                _ => build_image_internal_document(Some(&ocr_extraction_result.content), None),
            };
            doc.metadata = ocr_metadata;
            doc.formulas = ocr_formulas;
            doc.processing_warnings = processing_warnings;
            Self::mark_ocr_extraction(&mut doc);

            doc.prebuilt_ocr_elements = ocr_elements;

            let page_tables: Vec<std::sync::Arc<crate::types::Table>> = ocr_tables
                .into_iter()
                .map(|table| {
                    let bbox = table.bounding_box;
                    let table_index = doc.push_table(table.clone());
                    let mut element = crate::types::internal::InternalElement::text(
                        crate::types::internal::ElementKind::Table { table_index },
                        "",
                        0,
                    );
                    element.bbox = bbox;
                    doc.push_element(element);
                    std::sync::Arc::new(table)
                })
                .collect();

            if let Some(pages) = ocr_extraction_result.page_contents {
                doc.prebuilt_pages = Some(pages);
            } else {
                let text = ocr_extraction_result.content.trim().to_string();
                if !text.is_empty() {
                    doc.prebuilt_pages = Some(vec![crate::types::PageContent {
                        page_number: 1,
                        content: text,
                        tables: page_tables,
                        image_indices: vec![],
                        image_preprocessing: None,
                        hierarchy: None,
                        is_blank: None,
                        layout_regions: None,
                        speaker_notes: None,
                        section_name: None,
                        sheet_name: None,
                        ocr_confidence: whole_image_ocr_confidence,
                    }]);
                }
            }

            Ok(doc)
        }

        #[cfg(not(feature = "ocr"))]
        {
            let _ = mime_type;
            let mut doc = build_image_internal_document(Some(&ocr_content), None);
            doc.metadata = ocr_metadata;
            doc.formulas = ocr_formulas;
            doc.processing_warnings = processing_warnings;
            Self::mark_ocr_extraction(&mut doc);
            doc.prebuilt_ocr_elements = ocr_elements;
            let text = ocr_content.trim().to_string();
            if !text.is_empty() {
                doc.prebuilt_pages = Some(vec![crate::types::PageContent {
                    page_number: 1,
                    content: text,
                    tables: vec![],
                    image_indices: vec![],
                    image_preprocessing: None,
                    hierarchy: None,
                    is_blank: None,
                    layout_regions: None,
                    speaker_notes: None,
                    section_name: None,
                    sheet_name: None,
                    ocr_confidence: None,
                }]);
            }
            Ok(doc)
        }
    }

    /// Route a single image through the multi-stage OCR pipeline (issue #1339).
    ///
    /// Decodes the image and reuses the PDF extractor's pipeline runner so that
    /// `vlm_fallback` / explicit `pipeline` policies apply to bare images the same
    /// way they do to PDF pages. Gated on `pdf` because the runner lives there.
    #[cfg(all(feature = "pdf", any(feature = "ocr", feature = "ocr-pipeline")))]
    async fn extract_with_ocr_pipeline(
        &self,
        content: &[u8],
        config: &ExtractionConfig,
        pipeline: &crate::core::config::OcrPipelineConfig,
    ) -> Result<InternalDocument> {
        let default_security_limits = crate::extractors::security::SecurityLimits::default();
        let security_limits = config.security_limits.as_ref().unwrap_or(&default_security_limits);
        let image = crate::extraction::image::decode_image_to_rgb8_with_security_limits(content, security_limits)?;
        let rgb_bytes = u64::try_from(image.as_raw().len()).map_err(|_| {
            crate::extraction::image_decode::image_dimension_error(image.width(), image.height(), u64::MAX, u64::MAX)
        })?;
        crate::extraction::image_decode::validate_image_live_bytes(
            image.width(),
            image.height(),
            rgb_bytes,
            rgb_bytes,
            security_limits,
        )?;
        let image = image::DynamicImage::ImageRgb8(image);
        let images = [image];

        let (
            text,
            _tables,
            ocr_elements,
            pipeline_doc,
            llm_usage,
            _page_texts,
            _rasters,
            formulas,
            _,
            mut pipeline_ocr_confidence,
        ) = Box::pin(crate::extractors::pdf::ocr::run_ocr_pipeline(
            None,
            Some(&images),
            #[cfg(feature = "layout-detection")]
            None,
            config,
            pipeline,
            None,
        ))
        .await?;

        // Build a clean image document from the pipeline text (keeping the "image"
        // doc type and shape the rest of the image path produces), then carry over
        // the pipeline's elements, formulas, usage, and — crucially — any processing
        // warnings, so a fallback backend that failed is visible to the caller
        // rather than silently swapped for the classical result (issue #1339).
        let mut doc = build_image_internal_document(Some(&text), None);
        Self::mark_ocr_extraction(&mut doc);
        if !ocr_elements.is_empty() {
            doc.prebuilt_ocr_elements = Some(ocr_elements);
        }
        if !formulas.is_empty() {
            doc.formulas = formulas;
        }
        if !llm_usage.is_empty() {
            doc.llm_usage = Some(llm_usage);
        }
        if let Some(pipeline_doc) = pipeline_doc {
            doc.processing_warnings.extend(pipeline_doc.processing_warnings);
        }

        let trimmed = text.trim().to_string();
        if !trimmed.is_empty() {
            doc.prebuilt_pages = Some(vec![crate::types::PageContent {
                page_number: 1,
                content: trimmed,
                tables: vec![],
                image_indices: vec![],
                image_preprocessing: None,
                hierarchy: None,
                is_blank: None,
                layout_regions: None,
                speaker_notes: None,
                section_name: None,
                sheet_name: None,
                // ~keep The lone image was handed to the runner as page 0, so the winning
                // stage keys its summary at page 1 -- the same page this builds (#1568).
                ocr_confidence: pipeline_ocr_confidence.remove(&1),
            }]);
        }

        Ok(doc)
    }

    /// Extract text from image using layout detection + per-region OCR.
    ///
    /// Runs layout detection to identify document regions (headings, text,
    /// code, formulas, etc.), then OCRs each region individually and
    /// assembles the results into structured markdown.
    #[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
    async fn extract_with_layout_ocr(
        &self,
        content: &[u8],
        mime_type: &str,
        config: &ExtractionConfig,
    ) -> Result<InternalDocument> {
        let layout_config = config.layout.as_ref().ok_or_else(|| crate::XbergError::Parsing {
            message: "Layout config required for layout-enhanced OCR".to_string(),
            source: None,
        })?;

        let ocr_config = config.ocr.as_ref().ok_or_else(|| crate::XbergError::Parsing {
            message: "OCR config required for layout-enhanced OCR".to_string(),
            source: None,
        })?;

        let preparation = prepare_layout_ocr(self, content, mime_type, config, layout_config.clone()).await?;
        let (whole_image_result, rgb, detections) = match preparation {
            LayoutOcrPreparation::Complete(doc) => return Ok(doc),
            LayoutOcrPreparation::Detected {
                whole_image_result,
                rgb,
                detections,
            } => (whole_image_result, rgb, detections),
        };
        #[cfg(feature = "pdf")]
        let recognized_tables = match &whole_image_result {
            Ok(whole_image_doc) => recognize_cached_image_tables(whole_image_doc, &rgb, &detections, config).await,
            Err(_) => Vec::new(),
        };
        #[cfg(not(feature = "pdf"))]
        let recognized_tables = Vec::new();

        if let Ok(whole_image_doc) = &whole_image_result
            && source_image_is_proven_single_frame(content, mime_type)
            && let Some(structured) = try_assemble_cached_layout_document(
                whole_image_doc,
                &detections,
                &recognized_tables,
                rgb.width(),
                rgb.height(),
            )
        {
            #[cfg(feature = "formula-recognition")]
            let mut structured = structured;
            #[cfg(feature = "formula-recognition")]
            if let Some(layout) = config.layout.as_ref() {
                recognize_assembled_formula_regions(&mut structured, &rgb, &detections, layout).await;
            }
            tracing::debug!(
                tables = structured.tables.len(),
                "Assembled cached image OCR with layout structure"
            );
            return Ok(structured);
        }
        if let Ok(whole_image_doc) = &whole_image_result
            && let Some(structured) = try_retain_canonical_whole_image_ocr(
                whole_image_doc,
                &detections,
                rgb.width(),
                rgb.height(),
                source_image_is_proven_single_frame(content, mime_type),
            )
        {
            tracing::debug!(
                elements = whole_image_doc.prebuilt_ocr_elements.as_ref().map_or(0, Vec::len),
                "Retained canonical whole-image OCR without per-region OCR"
            );
            return Ok(structured);
        }

        let (backend, region_ocr_config) = match configured_region_ocr(config, ocr_config) {
            Ok(configured) => configured,
            Err(error) => return cached_whole_image_after_layout_error(&whole_image_result, error),
        };
        let region_doc = match extract_layout_regions(
            backend,
            &rgb,
            &detections,
            &region_ocr_config,
            config.layout.as_ref(),
            config.cancel_token.as_ref(),
        )
        .await
        {
            Ok(doc) => doc,
            Err(error) => return cached_whole_image_after_layout_error(&whole_image_result, error),
        };
        Ok(select_image_ocr_result(region_doc, whole_image_result))
    }
}

/// Minimum ratio of a paragraph's average hOCR font size (`x_fsize`) to the
/// image's median paragraph font size before the paragraph is promoted to a
/// heading (#185).
///
/// The standalone-image path is the last OCR route that still needs this
/// heuristic locally: it has no document-global structure pipeline behind it
/// (unlike the PDF mixed route, which classifies headings itself from the
/// same `x_fsize` attribute across the whole document). See
/// `ocr::processor::execution::flatten_hocr_elements_to_text` for why that
/// function no longer bakes headings into its output string.
#[cfg(feature = "ocr")]
const STANDALONE_IMAGE_HEADING_FONT_SIZE_RATIO: f64 = 1.3;

/// Maximum word count for a large-font paragraph to still be treated as a
/// heading; see [`STANDALONE_IMAGE_HEADING_FONT_SIZE_RATIO`].
#[cfg(feature = "ocr")]
const STANDALONE_IMAGE_HEADING_MAX_WORD_COUNT: usize = 12;

/// Read the paragraph-average hOCR font size stored by `hocr_parser`.
#[cfg(feature = "ocr")]
fn standalone_image_element_font_size(element: &crate::types::internal::InternalElement) -> Option<f64> {
    element
        .attributes
        .as_ref()?
        .get(crate::ocr::hocr_parser::HOCR_FONT_SIZE_ATTRIBUTE)?
        .parse::<f64>()
        .ok()
}

/// Build an `InternalDocument` from hOCR-derived paragraph elements.
///
/// Promotes large-font, short, single-line paragraphs to real `Heading`
/// elements rather than baking `## ` into a plain-text string: a pre-escaped
/// string is indistinguishable from literal user text once it reaches a
/// renderer, so it either leaks markdown syntax into `Plain` output or gets
/// escaped to `\#\#` by the markdown renderer. Always pushes the image
/// itself as an `Image` node, matching `build_image_internal_document`.
#[cfg(feature = "ocr")]
fn build_image_internal_document_from_hocr_elements(
    elements: &[crate::types::internal::InternalElement],
) -> InternalDocument {
    use crate::types::internal::ElementKind;

    let mut font_sizes: Vec<f64> = elements.iter().filter_map(standalone_image_element_font_size).collect();
    let median_font_size = if font_sizes.is_empty() {
        None
    } else {
        font_sizes.sort_by(f64::total_cmp);
        Some(font_sizes[font_sizes.len() / 2])
    };

    let mut builder = InternalDocumentBuilder::new("image");
    for element in elements {
        if matches!(element.kind, ElementKind::PageBreak) || element.text.is_empty() {
            continue;
        }
        let is_heading = median_font_size.is_some_and(|median| {
            standalone_image_element_font_size(element).is_some_and(|size| {
                size >= median * STANDALONE_IMAGE_HEADING_FONT_SIZE_RATIO
                    && !element.text.contains('\n')
                    && element.text.split_whitespace().count() <= STANDALONE_IMAGE_HEADING_MAX_WORD_COUNT
            })
        });
        if is_heading {
            builder.push_heading(1, &element.text, None, None);
        } else {
            builder.push_paragraph(&element.text, vec![], None, None);
        }
    }
    push_image_placeholder(&mut builder);
    builder.build()
}

/// Push a placeholder `Image` element carrying no binary data.
fn push_image_placeholder(builder: &mut InternalDocumentBuilder) {
    use crate::types::document_structure::ContentLayer;
    use crate::types::internal::{ElementKind, InternalElement, InternalElementId};

    let kind = ElementKind::Image { image_index: 0 };
    let id = InternalElementId::generate(kind.discriminant(), "", None, 0);
    builder.push_element(InternalElement {
        id,
        kind,
        text: String::new(),
        depth: 0,
        page: None,
        bbox: None,
        layer: ContentLayer::Body,
        annotations: Vec::new(),
        attributes: None,
        anchor: None,
        ocr_geometry: None,
        ocr_confidence: None,
        ocr_rotation: None,
    });
}

/// Build a simple `InternalDocument` for an image extraction result.
///
/// If OCR text is available, preserves its blank-line-delimited paragraphs. Always pushes
/// the image itself as an `Image` node. When `image_data` is provided,
/// the binary data is stored in `InternalDocument::images` and the
/// element references it by index.
fn build_image_internal_document(
    ocr_text: Option<&str>,
    image_data: Option<crate::types::ExtractedImage>,
) -> InternalDocument {
    let mut builder = InternalDocumentBuilder::new("image");
    if let Some(text) = ocr_text
        && !text.trim().is_empty()
    {
        for paragraph in split_ocr_paragraphs(text) {
            builder.push_paragraph(&paragraph, vec![], None, None);
        }
    }
    if let Some(img) = image_data {
        builder.push_image(None, img, None, None);
    } else {
        push_image_placeholder(&mut builder);
    }
    builder.build()
}

fn split_ocr_paragraphs(text: &str) -> Vec<String> {
    let mut paragraphs = Vec::new();
    let mut current = Vec::new();
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() {
            if !current.is_empty() {
                paragraphs.push(current.join("\n"));
                current.clear();
            }
        } else {
            current.push(line);
        }
    }
    if !current.is_empty() {
        paragraphs.push(current.join("\n"));
    }
    paragraphs
}

impl Default for ImageExtractor {
    fn default() -> Self {
        Self::new()
    }
}

impl Plugin for ImageExtractor {
    fn name(&self) -> &str {
        "image-extractor"
    }

    fn version(&self) -> String {
        env!("CARGO_PKG_VERSION").to_string()
    }

    fn initialize(&self) -> Result<()> {
        Ok(())
    }

    fn shutdown(&self) -> Result<()> {
        Ok(())
    }

    fn description(&self) -> &str {
        "Extracts dimensions, format, and EXIF data from images (PNG, JPEG, WebP, BMP, TIFF, GIF)"
    }

    fn author(&self) -> &str {
        "Xberg Team"
    }
}

/// Reject a multi-frame TIFF whose frame count exceeds `security_limits.max_pages`
/// before per-frame OCR begins (#1451).
///
/// TIFF is the only image container this crate treats as multi-page: frames are
/// OCR'd and their text tracked per page (see
/// `crate::extraction::image::extract_text_from_image_with_ocr`), while an
/// animated PNG/WebP/JPEG is always OCR'd as one whole image regardless of frame
/// count. Counting is only possible under the `ocr` feature, the sole feature
/// that pulls in the `tiff` crate `detect_tiff_frame_count` needs to walk IFDs
/// without decoding any raster data; other OCR feature combinations
/// (`ocr-wasm`, `ocr-pipeline` alone) have no way to count frames cheaply, so
/// this is a no-op there and `max_pages` goes unenforced on TIFF in those
/// builds.
#[cfg(feature = "ocr")]
fn enforce_image_page_limit(content: &[u8], mime_type: &str, config: &ExtractionConfig) -> Result<()> {
    let max_pages = config.security_limits.as_ref().and_then(|limits| limits.max_pages);
    let Some(max_pages) = max_pages else {
        return Ok(());
    };
    if !mime_type.to_lowercase().contains("tiff") {
        return Ok(());
    }
    let Ok(frame_count) = crate::extraction::image::detect_tiff_frame_count(content) else {
        return Ok(());
    };
    Ok(crate::extractors::security::enforce_page_count(
        frame_count,
        Some(max_pages),
    )?)
}

/// Guards the one-time "max_pages is unenforced for TIFF" warning so it fires
/// at most once per process, not once per extraction — same idiom as
/// `DEFAULT_CAP_WARNED` in `core::config::concurrency`.
#[cfg(not(feature = "ocr"))]
static UNENFORCED_TIFF_PAGE_LIMIT_WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Emit the "`max_pages` is unenforced for TIFF in this build" warning at most
/// once per `already_warned` guard.
///
/// The guard is a parameter rather than a direct read of
/// [`UNENFORCED_TIFF_PAGE_LIMIT_WARNED`] so the once-per-process property can be
/// tested against a guard the test owns, following
/// `warn_default_thread_cap_once` in `core::config::concurrency`: asserting on
/// the real global from more than one test would be unfixably racy.
#[cfg(not(feature = "ocr"))]
fn warn_unenforced_tiff_page_limit_once(already_warned: &std::sync::atomic::AtomicBool, max_pages: usize) {
    if already_warned
        .compare_exchange(
            false,
            true,
            std::sync::atomic::Ordering::SeqCst,
            std::sync::atomic::Ordering::SeqCst,
        )
        .is_ok()
    {
        tracing::warn!(
            max_pages,
            "security_limits.max_pages is set but cannot be enforced on TIFF images in this \
             build: counting TIFF frames requires the `ocr` feature, which this build does not \
             have enabled. TIFF page limits are silently unenforced until the binary is rebuilt \
             with `--features ocr`."
        );
    }
}

/// No-op stub for builds without the `ocr` feature: there is no `tiff` crate
/// dependency available to count frames, so `max_pages` is not enforced on TIFF
/// images at all in these builds (see `SecurityLimits::max_pages`'s doc
/// comment). A one-shot warning surfaces the gap instead of silently ignoring
/// a limit the caller explicitly set.
#[cfg(not(feature = "ocr"))]
fn enforce_image_page_limit(_content: &[u8], mime_type: &str, config: &ExtractionConfig) -> Result<()> {
    enforce_image_page_limit_stub_with_guard(mime_type, config, &UNENFORCED_TIFF_PAGE_LIMIT_WARNED)
}

/// Pure core of the `not(feature = "ocr")` [`enforce_image_page_limit`], parameterized on the
/// one-shot warning guard so tests can exercise it without touching process-global state.
#[cfg(not(feature = "ocr"))]
fn enforce_image_page_limit_stub_with_guard(
    mime_type: &str,
    config: &ExtractionConfig,
    already_warned: &std::sync::atomic::AtomicBool,
) -> Result<()> {
    let max_pages = config.security_limits.as_ref().and_then(|limits| limits.max_pages);
    let Some(max_pages) = max_pages else {
        return Ok(());
    };
    if !mime_type.to_lowercase().contains("tiff") {
        return Ok(());
    }
    warn_unenforced_tiff_page_limit_once(already_warned, max_pages);
    Ok(())
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl InternalDocumentExtractor for ImageExtractor {
    async fn extract_content(
        &self,
        content: &[u8],
        mime_type: &str,
        config: &ExtractionConfig,
    ) -> Result<InternalDocument> {
        tracing::debug!(format = "image", size_bytes = content.len(), "extraction starting");
        enforce_image_page_limit(content, mime_type, config)?;
        let default_security_limits = crate::extractors::security::SecurityLimits::default();
        let security_limits = config.security_limits.as_ref().unwrap_or(&default_security_limits);
        let extraction_metadata = extract_image_metadata_with_security_limits(content, security_limits)?;
        // Computed against the original bytes (before any HEIC->PNG rebinding
        // below) so it reflects the same input `extract_image_metadata` saw. ~keep
        let exif_warning = crate::extraction::exif::extract_exif_warning(content);

        #[cfg(feature = "heic")]
        let owned_png;
        #[cfg(feature = "heic")]
        let (content, mime_type): (&[u8], &str) = if crate::extraction::heif::is_heif_container(content) {
            owned_png = crate::extraction::heif::decode_heic_to_png(content, security_limits)?;
            (owned_png.as_slice(), "image/png")
        } else {
            (content, mime_type)
        };

        let format_str = extraction_metadata.format;
        let image_metadata = crate::types::ImageMetadata {
            width: extraction_metadata.width,
            height: extraction_metadata.height,
            format: format_str.clone(),
            exif: extraction_metadata.exif_data,
        };

        let (image_kind, kind_confidence) = crate::extraction::image_kind::classify(
            content,
            &format_str,
            Some(extraction_metadata.width),
            Some(extraction_metadata.height),
            None,
            None,
            false,
        );

        let extracted_image = crate::types::ExtractedImage {
            data: bytes::Bytes::copy_from_slice(content),
            format: std::borrow::Cow::Owned(format_str),
            image_index: 0,
            page_number: None,
            width: Some(extraction_metadata.width),
            height: Some(extraction_metadata.height),
            colorspace: None,
            bits_per_component: None,
            is_mask: false,
            description: None,
            ocr_result: None,
            bounding_box: None,
            source_path: None,
            image_kind: Some(image_kind),
            kind_confidence: Some(kind_confidence),
            cluster_id: None,
            caption: None,
            qr_codes: None,
            data_base64: None,
        };

        if config.effective_disable_ocr() {
            let attach_image = if config.needs_image_data() {
                Some(extracted_image)
            } else {
                None
            };
            let mut doc = build_image_internal_document(None, attach_image);
            doc.metadata = Metadata {
                format: Some(crate::types::FormatMetadata::Image(image_metadata)),
                ..Default::default()
            };
            doc.mime_type = mime_type.to_string();
            if let Some(warning) = exif_warning.clone() {
                doc.processing_warnings.push(warning);
            }
            tracing::debug!(
                format = "image",
                "OCR disabled via disable_ocr, returning metadata only"
            );
            return Ok(doc);
        }

        {
            #[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
            {
                let use_layout = should_use_layout_ocr(config);
                let mut doc = extract_selected_image_ocr_path(
                    use_layout,
                    || self.extract_with_layout_ocr(content, mime_type, config),
                    || self.extract_with_ocr(content, mime_type, config),
                )
                .await?;
                if let Some(ocr_config) = config.ocr.as_ref() {
                    apply_public_image_ocr_element_policy(&mut doc, ocr_config);
                }
                Self::mark_ocr_extraction(&mut doc);
                doc.metadata.format = Some(crate::types::FormatMetadata::Image(image_metadata));
                doc.mime_type = mime_type.to_string();
                if config.needs_image_data() {
                    doc.images.push(extracted_image);
                }
                if let Some(warning) = exif_warning.clone() {
                    doc.processing_warnings.push(warning);
                }
                return Ok(doc);
            }

            #[cfg(all(
                any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"),
                not(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))
            ))]
            {
                let mut doc = self.extract_with_ocr(content, mime_type, config).await?;
                if let Some(ocr_config) = config.ocr.as_ref() {
                    apply_public_image_ocr_element_policy(&mut doc, ocr_config);
                }
                Self::mark_ocr_extraction(&mut doc);
                doc.metadata.format = Some(crate::types::FormatMetadata::Image(image_metadata));
                doc.mime_type = mime_type.to_string();
                if config.needs_image_data() {
                    doc.images.push(extracted_image);
                }
                if let Some(warning) = exif_warning.clone() {
                    doc.processing_warnings.push(warning);
                }
                return Ok(doc);
            }
        }

        #[cfg(not(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")))]
        {
            let mut doc = build_image_internal_document(None, Some(extracted_image));
            doc.metadata = Metadata {
                format: Some(crate::types::FormatMetadata::Image(image_metadata)),
                ..Default::default()
            };
            doc.mime_type = mime_type.to_string();
            if let Some(warning) = exif_warning.clone() {
                doc.processing_warnings.push(warning);
            }

            tracing::debug!(
                element_count = doc.elements.len(),
                format = "image",
                "extraction complete"
            );
            Ok(doc)
        }
    }

    fn supported_mime_types(&self) -> &[&str] {
        &[
            "image/png",
            "image/jpeg",
            "image/jpg",
            "image/pjpeg",
            "image/webp",
            "image/bmp",
            "image/x-bmp",
            "image/x-ms-bmp",
            "image/tiff",
            "image/x-tiff",
            "image/gif",
            "image/jp2",
            "image/j2c",
            "image/x-jbig2",
            "image/x-portable-anymap",
            "image/x-portable-bitmap",
            "image/x-portable-graymap",
            "image/x-portable-pixmap",
            "image/heic",
            "image/heic-sequence",
            "image/heif",
            "image/heif-sequence",
            "image/avif",
            "image/avcs",
        ]
    }

    fn priority(&self) -> i32 {
        50
    }
}

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

    #[tokio::test]
    async fn rejects_declared_image_dimensions_over_configured_security_budget() {
        let content = crate::extraction::image_decode::bmp_with_declared_dimensions(100, 100);
        let config = ExtractionConfig {
            disable_ocr: true,
            security_limits: Some(crate::extractors::security::SecurityLimits {
                max_content_size: 1024,
                ..Default::default()
            }),
            ..Default::default()
        };

        let error = ImageExtractor::new()
            .extract_content(&content, "image/bmp", &config)
            .await
            .expect_err("the extractor must apply the configured budget before decoding pixels");

        assert!(matches!(error, crate::XbergError::Validation { .. }));
        assert!(error.to_string().contains("100x100"));
        assert!(error.to_string().contains("security_limits.max_content_size"));
    }

    /// #860: `normalize_image_bytes_for_ocr` applies `ImageExtractionConfig`'s
    /// `max_image_dimension` / DPI settings at the extractor boundary, because OCR
    /// backends only ever see `OcrConfig` and have no route back to them (#209).
    ///
    /// It used to be gated on `feature = "ocr"`, while `extract_with_ocr` is gated on
    /// `ocr` OR `ocr-wasm` OR `ocr-pipeline`. Every `candle-*` backend — and any
    /// `liter-llm` VLM-only build — implies `ocr-pipeline` but NOT `ocr`, so those
    /// builds took the `#[cfg(not(feature = "ocr"))]` arm and passed the raw bytes
    /// through with the settings silently dropped.
    ///
    /// Run this under a candle-only feature set to see the fix: before it, the
    /// function did not exist in such a build at all.
    #[cfg(feature = "ocr-pipeline")]
    #[test]
    fn normalizes_a_standalone_image_to_the_configured_maximum_dimension() {
        let oversized = image::RgbImage::from_pixel(1200, 600, image::Rgb([255, 255, 255]));
        let png = encode_rgb_as_png(oversized.as_raw(), 1200, 600).expect("encode the fixture");

        let images_config = crate::core::config::ImageExtractionConfig {
            max_image_dimension: 300,
            auto_adjust_dpi: false,
            ..Default::default()
        };
        let normalized = normalize_image_bytes_for_ocr(
            &png,
            &images_config,
            &crate::extractors::security::SecurityLimits::default(),
        )
        .expect("normal image should remain within the default decode budget");

        assert_ne!(
            normalized.bytes, png,
            "the oversized image should not pass through unchanged"
        );
        let decoded = image::load_from_memory(&normalized.bytes).expect("decode the normalized image");
        assert_eq!(
            (decoded.width(), decoded.height()),
            (300, 150),
            "the long edge should be clamped to max_image_dimension, preserving the 2:1 aspect ratio"
        );
        let metadata = normalized
            .metadata
            .expect("successful standalone-image normalization must retain metadata");
        assert_eq!(metadata.original_dimensions.width, 1200);
        assert_eq!(metadata.original_dimensions.height, 600);
        assert_eq!(
            metadata.new_dimensions.as_ref().map(|dimensions| dimensions.width),
            Some(300)
        );
        assert_eq!(
            metadata.new_dimensions.as_ref().map(|dimensions| dimensions.height),
            Some(150)
        );
        assert!(metadata.dimension_clamped);
    }

    #[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
    #[test]
    fn should_apply_vertical_block_psm_to_default_vertical_tesseract_config() {
        let mut ocr_config = crate::core::config::OcrConfig {
            language: vec!["jpn_vert".to_string()],
            ..Default::default()
        };

        apply_default_whole_image_tesseract_psm(&mut ocr_config);

        let tesseract_config = ocr_config
            .tesseract_config
            .expect("whole-image OCR must materialize Tesseract configuration");
        assert_eq!(tesseract_config.psm, VERTICAL_BLOCK_TESSERACT_PSM);
        assert_eq!(tesseract_config.language, vec!["jpn_vert"]);
    }

    #[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
    #[test]
    fn should_apply_default_whole_image_psm_to_horizontal_tesseract_config() {
        let mut ocr_config = crate::core::config::OcrConfig {
            language: vec!["eng".to_string()],
            ..Default::default()
        };

        apply_default_whole_image_tesseract_psm(&mut ocr_config);

        let tesseract_config = ocr_config
            .tesseract_config
            .expect("whole-image OCR must materialize Tesseract configuration");
        assert_eq!(tesseract_config.psm, WHOLE_IMAGE_TESSERACT_PSM);
        assert_eq!(tesseract_config.language, vec!["eng"]);
    }

    #[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
    #[test]
    fn should_preserve_explicit_whole_image_tesseract_psm() {
        let mut ocr_config = crate::core::config::OcrConfig {
            language: vec!["jpn_vert".to_string()],
            tesseract_config: Some(crate::types::TesseractConfig {
                language: vec!["jpn_vert".to_string()],
                psm: 4,
                ..Default::default()
            }),
            ..Default::default()
        };

        apply_default_whole_image_tesseract_psm(&mut ocr_config);

        let tesseract_config = ocr_config.tesseract_config.expect("explicit config must remain");
        assert_eq!(tesseract_config.psm, 4);
        assert_eq!(tesseract_config.language, vec!["jpn_vert"]);
    }

    #[cfg(feature = "ocr")]
    fn hocr_text_element_with_font_size(text: &str, font_size: Option<f64>) -> crate::types::internal::InternalElement {
        let mut element = crate::types::internal::InternalElement::text(
            crate::types::internal::ElementKind::OcrText {
                level: crate::types::OcrElementLevel::Block,
            },
            text,
            0,
        );
        if let Some(size) = font_size {
            element.attributes.get_or_insert_with(Default::default).insert(
                crate::ocr::hocr_parser::HOCR_FONT_SIZE_ATTRIBUTE.to_string(),
                size.to_string(),
            );
        }
        element
    }

    #[cfg(feature = "ocr")]
    #[test]
    fn standalone_image_plain_output_never_contains_markdown_heading_syntax() {
        // Regression test: `Plain` is the default output format and must never
        // contain markdown syntax. The standalone-image OCR path used to feed a
        // pre-baked `## Nasdaq & AMEX` string (produced by the now-removed
        // heading heuristic in `ocr::processor::execution`) straight into
        // `build_image_internal_document`, which stored it as literal paragraph
        // text. `render_plain` then emitted that text unchanged, leaking `## `
        // into `Plain` output. This must fail against that behavior.
        let elements = vec![
            hocr_text_element_with_font_size("Nasdaq & AMEX", Some(28.0)),
            hocr_text_element_with_font_size("Body text at normal size.", Some(12.0)),
        ];

        let doc = build_image_internal_document_from_hocr_elements(&elements);
        let plain = crate::rendering::render_plain(&doc);

        assert!(
            !plain.contains('#'),
            "Plain output must contain no markdown syntax: {plain:?}"
        );
        assert_eq!(plain, "Nasdaq & AMEX\n\nBody text at normal size.");
    }

    #[cfg(feature = "ocr")]
    #[test]
    fn standalone_image_markdown_output_emits_unescaped_heading() {
        // Regression test: feeding a pre-baked `## ` string into
        // `build_image_internal_document` (the old behavior) made the markdown
        // renderer treat it as literal paragraph text and escape it to
        // `\#\# Nasdaq & AMEX`, instead of a real level-1 `# Nasdaq & AMEX`
        // heading, because the renderer has no way to distinguish "this text
        // happens to start with #" from "this is a heading" once it has been
        // flattened to a string. Expressing the promotion as a real `Heading`
        // element (this function) avoids that ambiguity. This must fail
        // against the old flat-text path, which produces
        // "\\#\\# Nasdaq & AMEX" instead.
        // Three elements, not two: the promotion is median-based, so the body size must be the
        // median. With a single body element the median IS the heading size and nothing can exceed it.
        let elements = vec![
            hocr_text_element_with_font_size("Nasdaq & AMEX", Some(28.0)),
            hocr_text_element_with_font_size("Body text at normal size.", Some(12.0)),
            hocr_text_element_with_font_size("More body text at normal size.", Some(12.0)),
        ];

        let doc = build_image_internal_document_from_hocr_elements(&elements);
        let markdown = crate::rendering::render_markdown(&doc);

        assert!(
            markdown.contains("# Nasdaq & AMEX"),
            "markdown output must contain an unescaped level-1 heading: {markdown:?}"
        );
        assert!(
            !markdown.contains(r"\#"),
            "markdown output must not escape the heading marker: {markdown:?}"
        );
    }

    #[cfg(all(
        not(target_arch = "wasm32"),
        any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline")
    ))]
    mod sparse_image_ocr_fallback_tests {
        use super::*;

        fn result_with_word_confidences(confidences: &[f64]) -> crate::types::ExtractedDocument {
            let ocr_elements = confidences
                .iter()
                .enumerate()
                .map(|(index, confidence)| crate::types::OcrElement {
                    text: format!("word-{index}"),
                    confidence: crate::types::OcrConfidence {
                        recognition: *confidence,
                        ..Default::default()
                    },
                    level: crate::types::OcrElementLevel::Word,
                    ..Default::default()
                })
                .collect();
            crate::types::ExtractedDocument {
                ocr_elements: Some(ocr_elements),
                ..Default::default()
            }
        }

        #[test]
        fn should_retry_implicit_horizontal_tesseract_when_words_are_sparse() {
            let config = crate::core::config::OcrConfig::default();
            let confidences = vec![0.10; SPARSE_IMAGE_OCR_WORD_LIMIT];
            let result = result_with_word_confidences(&confidences);

            assert!(should_retry_sparse_image_ocr(&config, &result));
        }

        #[test]
        fn should_not_retry_implicit_horizontal_tesseract_when_words_are_dense() {
            let config = crate::core::config::OcrConfig::default();
            let confidences = vec![0.10; SPARSE_IMAGE_OCR_WORD_LIMIT + 1];
            let result = result_with_word_confidences(&confidences);

            assert!(!should_retry_sparse_image_ocr(&config, &result));
        }

        #[test]
        fn should_not_retry_sparse_primary_with_robust_word_confidences() {
            let config = crate::core::config::OcrConfig::default();
            let result = result_with_word_confidences(&[0.90, 0.95]);

            assert!(!should_retry_sparse_image_ocr(&config, &result));
        }

        #[test]
        fn should_exclude_explicit_and_vertical_tesseract_from_sparse_retry() {
            let result = result_with_word_confidences(&[0.10]);
            let explicit_config = crate::core::config::OcrConfig {
                tesseract_config: Some(crate::types::TesseractConfig::default()),
                ..Default::default()
            };
            let vertical_config = crate::core::config::OcrConfig {
                language: vec!["jpn_vert".to_string()],
                ..Default::default()
            };
            let other_backend_config = crate::core::config::OcrConfig {
                backend: "paddle-ocr".to_string(),
                ..Default::default()
            };

            assert!(!should_retry_sparse_image_ocr(&explicit_config, &result));
            assert!(!should_retry_sparse_image_ocr(&vertical_config, &result));
            assert!(!should_retry_sparse_image_ocr(&other_backend_config, &result));
        }

        #[test]
        fn should_reject_high_mean_fallback_with_bad_tenth_percentile() {
            let mut confidences = vec![0.95; 16];
            confidences.extend([0.14, 0.14]);
            let mean = confidences.iter().sum::<f64>() / confidences.len() as f64;
            let result = result_with_word_confidences(&confidences);

            assert!(mean > 0.80, "fixture must model misleadingly high mean confidence");
            assert!(!has_robust_word_confidence_distribution(&result));
        }

        #[test]
        fn should_select_fallback_with_robust_word_confidences() {
            let mut confidences = vec![0.95; 9];
            confidences.push(SPARSE_IMAGE_OCR_MIN_WORD_CONFIDENCE);
            let result = result_with_word_confidences(&confidences);

            assert!(has_robust_word_confidence_distribution(&result));
        }

        #[test]
        fn should_build_psm3_fallback_with_explicit_grayscale_enhancement() {
            let mut whole_image_config = crate::core::config::OcrConfig::default();
            apply_default_whole_image_tesseract_psm(&mut whole_image_config);

            let fallback_config = sparse_image_ocr_fallback_config(&whole_image_config);
            let tesseract_config = fallback_config
                .tesseract_config
                .expect("fallback must materialize Tesseract configuration");

            assert_eq!(tesseract_config.psm, SPARSE_IMAGE_OCR_FALLBACK_PSM);
            assert_eq!(
                tesseract_config
                    .preprocessing
                    .as_ref()
                    .expect("fallback must materialize preprocessing")
                    .binarization_method,
                "none"
            );
            let preprocessing = tesseract_config.preprocessing.unwrap();
            assert!(!preprocessing.deskew);
            assert!(preprocessing.contrast_enhance);
        }

        /// Regression test for the standalone-image-OCR variant of the `TesseractConfig`
        /// duplication hazard: `apply_default_whole_image_tesseract_psm` materializes
        /// `crate::types::TesseractConfig::default()` (the public struct) whenever the
        /// caller never set `tesseract_config` explicitly, which is the default path for
        /// every plain image (PNG/JPG/TIFF/...) OCR request. That materialized value is
        /// later converted into `crate::ocr::types::TesseractConfig` (the internal,
        /// engine-facing struct) via `From`, which carries the value across as-is — it does
        /// NOT fall back to the internal struct's own `Default`. So if the public struct's
        /// default ever drifts from the internal struct's default, standalone image OCR
        /// silently keeps using the stale public value while other callers that reach the
        /// internal `Default` directly (e.g. PDF-embedded OCR, which never calls this
        /// function) get the current one.
        ///
        /// Against the unfixed code, `crate::types::TesseractConfig::default()` still has
        /// `language_model_ngram_on: false`, so this assertion fails with `false` even
        /// though `crate::ocr::types::TesseractConfig::default()` already has `true`.
        #[test]
        fn should_apply_public_ngram_default_to_whole_image_tesseract_config() {
            let mut whole_image_config = crate::core::config::OcrConfig::default();
            apply_default_whole_image_tesseract_psm(&mut whole_image_config);

            let tesseract_config = whole_image_config
                .tesseract_config
                .expect("default whole-image OCR must materialize a Tesseract configuration");

            assert!(
                tesseract_config.language_model_ngram_on,
                "standalone image OCR must use the same language_model_ngram_on default as the \
                 internal TesseractConfig, not a stale value baked in from the public struct's \
                 own Default impl"
            );
        }
    }

    fn image_ocr_document(text: &str) -> InternalDocument {
        build_image_internal_document(Some(text), None)
    }

    #[test]
    fn should_mark_metadata_when_standalone_image_ocr_succeeds() {
        let mut doc = image_ocr_document("recognized text");

        ImageExtractor::mark_ocr_extraction(&mut doc);
        let result =
            crate::extraction::derive::derive_extraction_result(doc, false, crate::core::config::OutputFormat::Plain);

        assert!(result.metadata.ocr_used);
        assert_eq!(result.extraction_method, Some(crate::types::ExtractionMethod::Ocr));
    }

    #[tokio::test]
    async fn should_not_mark_metadata_when_standalone_image_ocr_is_disabled() {
        let mut png = std::io::Cursor::new(Vec::new());
        image::ImageBuffer::<image::Rgb<u8>, _>::from_pixel(1, 1, image::Rgb([255u8, 255, 255]))
            .write_to(&mut png, image::ImageFormat::Png)
            .expect("failed to encode test PNG");
        let config = ExtractionConfig {
            disable_ocr: true,
            ..Default::default()
        };

        let doc = ImageExtractor::new()
            .extract_content(&png.into_inner(), "image/png", &config)
            .await
            .expect("metadata-only image extraction must succeed");
        let result =
            crate::extraction::derive::derive_extraction_result(doc, false, crate::core::config::OutputFormat::Plain);

        assert!(!result.metadata.ocr_used);
        assert_eq!(result.extraction_method, None);
    }

    #[cfg(feature = "ocr")]
    #[tokio::test]
    async fn should_recover_receipt_header_with_implicit_sparse_image_fallback() {
        let Some(receipt) = crate::utils::read_test_fixture("images/cord_receipt_02.jpg") else {
            return;
        };
        let config = ExtractionConfig {
            use_cache: false,
            force_ocr: true,
            ocr: Some(crate::core::config::OcrConfig::default()),
            ..Default::default()
        };

        let doc = ImageExtractor::new()
            .extract_content(&receipt, "image/jpeg", &config)
            .await
            .expect("receipt OCR must succeed");
        let result =
            crate::extraction::derive::derive_extraction_result(doc, false, crate::core::config::OutputFormat::Plain);
        let normalized = result.content.to_ascii_lowercase();

        assert!(
            normalized.contains("j.stb promo"),
            "implicit sparse-image fallback must recover the grounded receipt header; got {normalized:?}"
        );
    }

    #[test]
    fn image_ocr_preserves_blank_line_paragraph_boundaries() {
        let doc = image_ocr_document("\nfirst line\nwrapped line\n\n\nsecond paragraph\n");
        let paragraphs = doc
            .elements
            .iter()
            .filter(|element| matches!(element.kind, crate::types::internal::ElementKind::Paragraph))
            .map(|element| element.text.as_str())
            .collect::<Vec<_>>();

        assert_eq!(paragraphs, ["first line\nwrapped line", "second paragraph"]);
    }

    #[test]
    fn image_ocr_keeps_single_newline_wrapping_in_one_paragraph() {
        let doc = image_ocr_document("first line\nwrapped line");
        let paragraphs = doc
            .elements
            .iter()
            .filter(|element| matches!(element.kind, crate::types::internal::ElementKind::Paragraph))
            .map(|element| element.text.as_str())
            .collect::<Vec<_>>();

        assert_eq!(paragraphs, ["first line\nwrapped line"]);
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    fn positioned_word(text: &str, left: u32, top: u32) -> crate::types::OcrElement {
        positioned_word_box(text, left, top, 40, 20)
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    fn positioned_word_box(text: &str, left: u32, top: u32, width: u32, height: u32) -> crate::types::OcrElement {
        crate::types::OcrElement::new(
            text,
            crate::types::OcrBoundingGeometry::Rectangle {
                left,
                top,
                width,
                height,
            },
            crate::types::OcrConfidence::from_tesseract(95.0),
        )
        .with_level(crate::types::OcrElementLevel::Word)
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    fn positioned_line_box(text: &str, left: u32, top: u32, width: u32, height: u32) -> crate::types::OcrElement {
        crate::types::OcrElement::new(
            text,
            crate::types::OcrBoundingGeometry::Rectangle {
                left,
                top,
                width,
                height,
            },
            crate::types::OcrConfidence::from_tesseract(95.0),
        )
        .with_level(crate::types::OcrElementLevel::Line)
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    fn whole_image_doc_with_elements(
        text: &str,
        elements: Vec<crate::types::OcrElement>,
        width: u32,
        height: u32,
    ) -> InternalDocument {
        let mut doc = image_ocr_document(text);
        doc.prebuilt_ocr_elements = Some(elements);
        doc.prebuilt_pages = Some(vec![crate::types::PageContent {
            page_number: 1,
            content: text.to_string(),
            tables: vec![],
            image_indices: vec![],
            image_preprocessing: None,
            hierarchy: None,
            is_blank: None,
            layout_regions: None,
            speaker_notes: None,
            section_name: None,
            sheet_name: None,
            ocr_confidence: None,
        }]);
        doc.metadata.additional.insert(
            std::borrow::Cow::Borrowed(crate::ocr_metadata_keys::OCR_PROCESSED_IMAGE_WIDTH_METADATA_KEY),
            serde_json::json!(width),
        );
        doc.metadata.additional.insert(
            std::borrow::Cow::Borrowed(crate::ocr_metadata_keys::OCR_PROCESSED_IMAGE_HEIGHT_METADATA_KEY),
            serde_json::json!(height),
        );
        doc
    }

    #[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
    #[test]
    fn shared_detected_image_preserves_content_dimensions_and_backing_buffer() {
        let rgb = image::RgbImage::from_raw(2, 1, vec![1, 2, 3, 4, 5, 6]).unwrap();
        let pixels = rgb.as_raw().as_ptr();

        let shared = share_detected_image(rgb);

        assert_eq!(shared.dimensions(), (2, 1));
        assert_eq!(shared.as_raw(), &[1, 2, 3, 4, 5, 6]);
        assert_eq!(shared.as_raw().as_ptr(), pixels);

        let task_image = std::sync::Arc::clone(&shared);
        assert!(std::sync::Arc::ptr_eq(&task_image, &shared));
    }

    #[cfg(feature = "ocr")]
    #[test]
    fn should_preserve_requested_element_level_without_layout() {
        let mut config = crate::core::config::OcrConfig {
            element_config: Some(crate::types::OcrElementConfig {
                min_level: crate::types::OcrElementLevel::Line,
                ..Default::default()
            }),
            ..Default::default()
        };

        enable_image_ocr_elements(&mut config, false);

        let element_config = config.element_config.unwrap();
        assert!(element_config.include_elements);
        assert_eq!(element_config.min_level, crate::types::OcrElementLevel::Line);
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_request_word_elements_for_layout_assembly() {
        let mut config = crate::core::config::OcrConfig::default();

        enable_image_ocr_elements(&mut config, true);

        let element_config = config.element_config.unwrap();
        assert!(element_config.include_elements);
        assert_eq!(element_config.min_level, crate::types::OcrElementLevel::Word);
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_select_line_text_for_semantics_and_word_boxes_for_tables() {
        let elements = vec![
            positioned_line_box("Hello, world!", 10, 10, 120, 20),
            positioned_word_box("Hello,", 10, 10, 50, 20),
            positioned_word_box("world!", 70, 10, 60, 20),
        ];
        let transform = OcrCoordinateTransform {
            processed_width: 200,
            processed_height: 100,
            scale_x: 1.0,
            scale_y: 1.0,
        };

        let semantic = transformed_ocr_elements(&elements, transform, crate::types::OcrElementLevel::Line).unwrap();
        let semantic_refs = semantic.iter().collect::<Vec<_>>();
        assert_eq!(semantic.len(), 1);
        assert_eq!(text_from_positioned_elements(&semantic_refs), "Hello, world!");

        let table = transformed_ocr_elements(&elements, transform, crate::types::OcrElementLevel::Word).unwrap();
        assert_eq!(
            table.iter().map(|element| element.text.as_str()).collect::<Vec<_>>(),
            ["Hello,", "world!"]
        );
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_fall_back_when_preferred_ocr_granularity_is_missing() {
        let lines = vec![positioned_line_box("Line only", 10, 10, 80, 20)];
        let words = vec![positioned_word("Word", 10, 10)];

        assert_eq!(
            preferred_ocr_elements(&lines, crate::types::OcrElementLevel::Word)[0].level,
            crate::types::OcrElementLevel::Line
        );
        assert_eq!(
            preferred_ocr_elements(&words, crate::types::OcrElementLevel::Line)[0].level,
            crate::types::OcrElementLevel::Word
        );
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_measure_mixed_level_layout_retention_once_at_line_granularity() {
        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Text,
            0.96,
            crate::layout::BBox::new(0.0, 0.0, 100.0, 50.0),
        )];
        let elements = vec![
            positioned_line_box("inside outside", 10, 10, 80, 20),
            positioned_word_box("inside", 10, 10, 35, 20),
            positioned_word_box("outside", 140, 10, 45, 20),
        ];
        let whole = whole_image_doc_with_elements("inside outside", elements, 200, 100);

        assert_eq!(
            whole_image_layout_mapping_retention(&whole, &detections, 200, 100),
            Some(1.0)
        );
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_retain_canonical_whole_image_when_layout_coverage_is_low() {
        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Title,
            0.96,
            crate::layout::BBox::new(0.0, 0.0, 60.0, 60.0),
        )];
        let elements = vec![positioned_word("inside", 10, 20), positioned_word("outside", 100, 20)];
        let mut whole = whole_image_doc_with_elements("Inside, outside!", elements, 200, 100);
        whole.relationships.push(crate::types::internal::Relationship {
            source: 0,
            target: crate::types::internal::RelationshipTarget::Index(0),
            kind: crate::types::document_structure::RelationshipKind::CrossReference,
        });
        let original = whole.clone();

        let retained = try_retain_canonical_whole_image_ocr(&whole, &detections, 200, 100, true)
            .expect("50% layout coverage must retain canonical whole-image OCR");

        assert_eq!(retained.elements, original.elements);
        assert_eq!(retained.relationships, original.relationships);
        assert_eq!(
            serde_json::to_value(&retained.prebuilt_ocr_elements).unwrap(),
            serde_json::to_value(&original.prebuilt_ocr_elements).unwrap()
        );
        assert_eq!(retained.prebuilt_pages.as_ref().unwrap()[0].content, "Inside, outside!");
        assert_eq!(
            retained.prebuilt_pages.as_ref().unwrap()[0]
                .layout_regions
                .as_ref()
                .map(Vec::len),
            Some(1)
        );
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_retain_canonical_whole_image_when_layout_coverage_is_sufficient() {
        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Text,
            0.96,
            crate::layout::BBox::new(0.0, 0.0, 200.0, 100.0),
        )];
        let elements = vec![positioned_word("inside", 10, 20)];
        let whole = whole_image_doc_with_elements("inside", elements, 200, 100);

        let retained = try_retain_canonical_whole_image_ocr(&whole, &detections, 200, 100, true)
            .expect("successful single-frame OCR must avoid repeated region OCR");

        assert_eq!(retained.elements, whole.elements);
        assert_eq!(retained.prebuilt_pages.as_ref().unwrap()[0].content, "inside");
        assert_eq!(
            retained.prebuilt_pages.as_ref().unwrap()[0]
                .layout_regions
                .as_ref()
                .map(Vec::len),
            Some(1)
        );
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_disable_redundant_tesseract_analysis_for_region_ocr() {
        let extraction_config = ExtractionConfig::default();
        let ocr_config = crate::core::config::OcrConfig::default();

        let (_, region_config) = configured_region_ocr(&extraction_config, &ocr_config).unwrap();
        let tesseract_config = region_config
            .tesseract_config
            .expect("region OCR must materialize Tesseract configuration");

        assert_eq!(
            region_config.output_format,
            Some(crate::core::config::OutputFormat::Plain)
        );
        assert_eq!(tesseract_config.output_format, "text");
        assert_eq!(tesseract_config.psm, 6);
        assert!(!tesseract_config.enable_table_detection);
        assert!(ocr_config.tesseract_config.is_none());
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_preserve_explicit_tesseract_psm_for_region_ocr() {
        let extraction_config = ExtractionConfig::default();
        let ocr_config = crate::core::config::OcrConfig {
            tesseract_config: Some(crate::types::TesseractConfig {
                psm: 4,
                ..Default::default()
            }),
            ..Default::default()
        };

        let (_, region_config) = configured_region_ocr(&extraction_config, &ocr_config).unwrap();

        assert_eq!(
            region_config.tesseract_config.expect("explicit config must remain").psm,
            4
        );
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_assemble_cached_headings_without_recognized_tables() {
        let detections = vec![
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::Title,
                0.98,
                crate::layout::BBox::new(0.0, 0.0, 200.0, 30.0),
            ),
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::SectionHeader,
                0.96,
                crate::layout::BBox::new(0.0, 30.0, 200.0, 60.0),
            ),
        ];
        let elements = vec![
            positioned_word("Annual Report", 10, 5),
            positioned_word("Summary", 10, 35),
        ];
        let whole = whole_image_doc_with_elements("Annual Report Summary", elements, 200, 100);

        let assembled = try_assemble_cached_layout_document(&whole, &detections, &[], 200, 100)
            .expect("semantic detections must structure cached whole-image OCR");
        let markdown = crate::rendering::render_markdown(&assembled);

        assert_eq!(
            assembled.elements[0].kind,
            crate::types::internal::ElementKind::Heading { level: 1 }
        );
        assert_eq!(
            assembled.elements[1].kind,
            crate::types::internal::ElementKind::Heading { level: 2 }
        );
        assert!(markdown.contains("# Annual Report"));
        assert!(markdown.contains("## Summary"));
        assert_eq!(
            assembled.prebuilt_pages.as_ref().unwrap()[0]
                .layout_regions
                .as_ref()
                .map(Vec::len),
            Some(2)
        );
        assert_eq!(alphanumeric_token_retention(&markdown, "Annual Report Summary"), 1.0);
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_keep_unmapped_invoice_tokens_when_assembling_cached_structure() {
        let detections = vec![
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::Title,
                0.98,
                crate::layout::BBox::new(0.0, 0.0, 100.0, 30.0),
            ),
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::SectionHeader,
                0.96,
                crate::layout::BBox::new(0.0, 30.0, 100.0, 60.0),
            ),
        ];
        let elements = vec![
            positioned_word("INVOICE", 10, 5),
            positioned_word("Bill To", 10, 35),
            positioned_word("John Doe", 10, 70),
            positioned_word("Invoice 123", 120, 35),
            positioned_word("Date 2025", 120, 70),
        ];
        let canonical = "INVOICE Bill To John Doe Invoice 123 Date 2025";
        let whole = whole_image_doc_with_elements(canonical, elements, 240, 100);

        let assembled = try_assemble_cached_layout_document(&whole, &detections, &[], 240, 100)
            .expect("partial invoice layout must preserve cached OCR");
        let markdown = crate::rendering::render_markdown(&assembled);

        assert_eq!(
            assembled.elements[0].kind,
            crate::types::internal::ElementKind::Heading { level: 1 }
        );
        assert_eq!(
            assembled.elements[1].kind,
            crate::types::internal::ElementKind::Heading { level: 2 }
        );
        assert!(markdown.contains("# INVOICE"));
        assert!(markdown.contains("## Bill To"));
        assert!(markdown.contains("John Doe"));
        assert!(markdown.contains("Invoice 123"));
        assert!(markdown.contains("Date 2025"));
        assert_eq!(
            assembled.prebuilt_pages.as_ref().unwrap()[0]
                .layout_regions
                .as_ref()
                .map(Vec::len),
            Some(2)
        );
        assert_eq!(alphanumeric_token_retention(&markdown, canonical), 1.0);
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_interleave_unmatched_ocr_with_structural_regions() {
        let detections = vec![
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::Title,
                0.98,
                crate::layout::BBox::new(0.0, 30.0, 100.0, 55.0),
            ),
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::SectionHeader,
                0.96,
                crate::layout::BBox::new(0.0, 70.0, 100.0, 95.0),
            ),
        ];
        let elements = vec![
            positioned_word("Before", 150, 5),
            positioned_word("Title", 10, 35),
            positioned_word("Between", 150, 60),
            positioned_word("Section", 10, 75),
            positioned_word("After", 150, 100),
        ];
        let canonical = "Before Title Between Section After";
        let whole = whole_image_doc_with_elements(canonical, elements, 240, 130);

        let assembled = try_assemble_cached_layout_document(&whole, &detections, &[], 240, 130)
            .expect("populated structure must retain spatially interleaved OCR");
        let texts = assembled
            .elements
            .iter()
            .map(|element| element.text.as_str())
            .collect::<Vec<_>>();

        assert_eq!(texts, vec!["Before", "Title", "Between", "Section", "After"]);
        assert_eq!(
            alphanumeric_token_retention(&crate::rendering::render_plain(&assembled), canonical),
            1.0
        );
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_retain_fallback_when_structural_detection_has_no_text() {
        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Title,
            0.98,
            crate::layout::BBox::new(0.0, 0.0, 50.0, 30.0),
        )];
        let whole = whole_image_doc_with_elements(
            "outside title",
            vec![positioned_word("outside title", 100, 40)],
            200,
            100,
        );

        assert!(try_assemble_cached_layout_document(&whole, &detections, &[], 200, 100).is_none());
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_reject_non_table_structure_with_partial_token_retention() {
        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Title,
            0.98,
            crate::layout::BBox::new(0.0, 0.0, 60.0, 30.0),
        )];
        let elements = vec![
            positioned_word("one", 10, 5),
            positioned_word("two", 100, 35),
            positioned_word("three", 100, 55),
            positioned_word("four", 100, 75),
        ];
        let whole = whole_image_doc_with_elements("one two three four five", elements, 200, 100);

        assert!(try_assemble_cached_layout_document(&whole, &detections, &[], 200, 100).is_none());
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_reject_table_structure_at_exact_four_of_five_token_retention() {
        let table_bbox = crate::layout::BBox::new(0.0, 0.0, 100.0, 120.0);
        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Table,
            0.98,
            table_bbox,
        )];
        let elements = vec![
            positioned_word("one", 10, 5),
            positioned_word("two", 10, 25),
            positioned_word("three", 10, 45),
            positioned_word("four", 10, 65),
            positioned_word("five", 10, 85),
        ];
        let whole = whole_image_doc_with_elements("one two three four five", elements, 100, 120);
        let recognized = vec![crate::RecognizedTable {
            detection_bbox: table_bbox,
            cells: vec![
                vec!["one".to_string(), "two".to_string()],
                vec!["three".to_string(), "four".to_string()],
            ],
            markdown: "| one | two |\n| --- | --- |\n| three | four |".to_string(),
        }];

        let cached_elements = cached_layout_elements(&whole, 100, 120).unwrap();
        let assembled = assemble_cached_layout_elements(&whole, &detections, &recognized, &cached_elements, 100, 120);
        let retention = alphanumeric_token_retention(
            &crate::rendering::render_plain(&assembled),
            &internal_document_text(&whole),
        );
        assert_eq!(retention, MIN_LAYOUT_OCR_ALPHANUMERIC_TOKEN_RETENTION);

        assert!(try_assemble_cached_layout_document(&whole, &detections, &recognized, 100, 120).is_none());
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_keep_line_elements_for_non_table_text_with_recognized_table() {
        let table_bbox = crate::layout::BBox::new(0.0, 0.0, 100.0, 60.0);
        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Table,
            0.98,
            table_bbox,
        )];
        let elements = vec![
            positioned_line_box("Header", 10, 10, 50, 20),
            positioned_line_box("Outside", 120, 10, 70, 20),
        ];
        let whole = whole_image_doc_with_elements("Header Outside", elements, 200, 100);
        let recognized = vec![crate::RecognizedTable {
            detection_bbox: table_bbox,
            cells: vec![vec!["Header".to_string()]],
            markdown: "| Header |\n| --- |".to_string(),
        }];

        let assembled = try_assemble_cached_layout_document(&whole, &detections, &recognized, 200, 100)
            .expect("line geometry must preserve canonical text outside the recognized table");

        assert_eq!(
            alphanumeric_token_retention(&crate::rendering::render_plain(&assembled), "Header Outside"),
            1.0
        );
        assert_eq!(
            crate::rendering::render_markdown(&assembled).matches("Header").count(),
            1
        );
        assert_eq!(
            crate::rendering::render_markdown(&assembled).matches("Outside").count(),
            1
        );
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_assemble_recognized_table_without_duplicate_fallback_text() {
        let table_bbox = crate::layout::BBox::new(0.0, 0.0, 100.0, 100.0);
        let detections = vec![
            crate::layout::LayoutDetection::new(crate::layout::LayoutClass::Table, 0.98, table_bbox),
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::Text,
                0.95,
                crate::layout::BBox::new(100.0, 0.0, 200.0, 100.0),
            ),
        ];
        let elements = vec![
            positioned_word_box("Header", 90, 10, 20, 20),
            positioned_word_box("Value", 50, 10, 30, 20),
            positioned_word_box("Total", 120, 10, 40, 20),
        ];
        let whole = whole_image_doc_with_elements("Header Value Total", elements, 200, 100);
        let recognized = vec![crate::RecognizedTable {
            detection_bbox: table_bbox,
            cells: vec![
                vec!["Header".to_string(), "Value".to_string()],
                vec!["A".to_string(), "1".to_string()],
            ],
            markdown: "| Header | Value |\n| --- | --- |\n| A | 1 |".to_string(),
        }];

        let assembled = try_assemble_cached_layout_document(&whole, &detections, &recognized, 200, 100)
            .expect("successful recognition must assemble a structured image table");
        let markdown = crate::rendering::render_markdown(&assembled);

        assert_eq!(assembled.tables.len(), 1);
        assert_eq!(assembled.tables[0].cells[1], vec!["A".to_string(), "1".to_string()]);
        assert_eq!(
            markdown.matches("Header").count(),
            1,
            "table OCR text must not be duplicated"
        );
        assert!(markdown.contains("Total"), "non-table OCR text must be retained");
        assert_eq!(
            serde_json::to_value(&assembled.prebuilt_ocr_elements).unwrap(),
            serde_json::to_value(&whole.prebuilt_ocr_elements).unwrap()
        );
        assert_eq!(assembled.prebuilt_pages.as_ref().unwrap()[0].tables.len(), 1);
    }

    /// Issue #181: a TATR-recognized table assembled from cached layout must carry
    /// a deterministic `table_id`, `columns`, and `bounding_box` derived from the
    /// detection's `detection_bbox` — not `..Default::default()` blanks.
    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn recognized_table_gets_table_id_columns_and_bounding_box() {
        let table_bbox = crate::layout::BBox::new(0.0, 0.0, 100.0, 100.0);
        let detections = vec![
            crate::layout::LayoutDetection::new(crate::layout::LayoutClass::Table, 0.98, table_bbox),
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::Text,
                0.95,
                crate::layout::BBox::new(100.0, 0.0, 200.0, 100.0),
            ),
        ];
        let elements = vec![
            positioned_word_box("Header", 90, 10, 20, 20),
            positioned_word_box("Value", 50, 10, 30, 20),
            positioned_word_box("Total", 120, 10, 40, 20),
        ];
        let whole = whole_image_doc_with_elements("Header Value Total", elements, 200, 100);
        let recognized = vec![crate::RecognizedTable {
            detection_bbox: table_bbox,
            cells: vec![
                vec!["Header".to_string(), "Value".to_string()],
                vec!["A".to_string(), "1".to_string()],
            ],
            markdown: "| Header | Value |\n| --- | --- |\n| A | 1 |".to_string(),
        }];

        let assembled = try_assemble_cached_layout_document(&whole, &detections, &recognized, 200, 100)
            .expect("successful recognition must assemble a structured image table");

        assert_eq!(assembled.tables.len(), 1);
        assert_eq!(assembled.tables[0].table_id.as_deref(), Some("table-1"));
        assert_eq!(
            assembled.tables[0].columns,
            Some(vec!["Header".to_string(), "Value".to_string()])
        );
        let bbox = assembled.tables[0]
            .bounding_box
            .expect("bounding box must be populated from detection_bbox");
        assert_eq!(bbox.x0, 0.0);
        assert_eq!(bbox.y0, 0.0);
        assert_eq!(bbox.x1, 100.0);
        assert_eq!(bbox.y1, 100.0);
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_reject_recognized_table_when_it_drops_cached_ocr_text() {
        let table_bbox = crate::layout::BBox::new(0.0, 0.0, 100.0, 100.0);
        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Table,
            0.98,
            table_bbox,
        )];
        let elements = vec![
            positioned_word("one", 10, 10),
            positioned_word("two", 10, 25),
            positioned_word("three", 10, 40),
            positioned_word("four", 10, 55),
            positioned_word("five", 10, 70),
        ];
        let whole = whole_image_doc_with_elements("one two three four five", elements, 100, 100);
        let recognized = vec![crate::RecognizedTable {
            detection_bbox: table_bbox,
            cells: vec![vec!["one".to_string()]],
            markdown: "| one |\n| --- |".to_string(),
        }];

        assert!(try_assemble_cached_layout_document(&whole, &detections, &recognized, 100, 100).is_none());
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_preserve_existing_fallback_when_no_table_was_recognized() {
        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Table,
            0.98,
            crate::layout::BBox::new(0.0, 0.0, 100.0, 100.0),
        )];
        let whole = whole_image_doc_with_elements(
            "unstructured fallback",
            vec![positioned_word("unstructured", 10, 10)],
            100,
            100,
        );

        assert!(try_assemble_cached_layout_document(&whole, &detections, &[], 100, 100).is_none());
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_respect_disabled_and_preserve_slanet_fallback_for_image_tables() {
        use crate::core::config::layout::TableModel;

        assert!(uses_tatr_image_table_recognition(TableModel::Tatr));
        assert!(!uses_tatr_image_table_recognition(TableModel::Disabled));
        assert!(!uses_tatr_image_table_recognition(TableModel::SlanetWired));
        assert!(!uses_tatr_image_table_recognition(TableModel::SlanetWireless));
        assert!(!uses_tatr_image_table_recognition(TableModel::SlanetPlus));
        assert!(!uses_tatr_image_table_recognition(TableModel::SlanetAuto));
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_reject_multiframe_whole_image_fast_path() {
        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Text,
            0.96,
            crate::layout::BBox::new(0.0, 0.0, 20.0, 20.0),
        )];
        let elements = vec![positioned_word("outside", 100, 20)];
        let mut whole = whole_image_doc_with_elements("outside", elements, 200, 100);
        let mut second_page = whole.prebuilt_pages.as_ref().unwrap()[0].clone();
        second_page.page_number = 2;
        whole.prebuilt_pages.as_mut().unwrap().push(second_page);

        assert!(try_retain_canonical_whole_image_ocr(&whole, &detections, 200, 100, true).is_none());
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_reject_real_multiframe_tiff_when_ocr_synthesizes_one_page() {
        let mut cursor = std::io::Cursor::new(Vec::new());
        {
            let mut encoder = tiff::encoder::TiffEncoder::new(&mut cursor).unwrap();
            encoder
                .write_image::<tiff::encoder::colortype::Gray8>(1, 1, &[0])
                .unwrap();
            encoder
                .write_image::<tiff::encoder::colortype::Gray8>(1, 1, &[255])
                .unwrap();
        }
        let source = cursor.into_inner();
        let decoder = tiff::decoder::Decoder::new(std::io::Cursor::new(&source)).unwrap();
        assert!(decoder.more_images(), "test input must contain multiple TIFF frames");

        let detections = vec![crate::layout::LayoutDetection::new(
            crate::layout::LayoutClass::Text,
            0.96,
            crate::layout::BBox::new(0.0, 0.0, 20.0, 20.0),
        )];
        let elements = vec![positioned_word("outside", 100, 20)];
        let whole = whole_image_doc_with_elements("outside", elements, 200, 100);
        let source_is_single_frame = source_image_is_proven_single_frame(&source, "image/tiff");

        assert!(!source_is_single_frame);
        assert!(try_retain_canonical_whole_image_ocr(&whole, &detections, 200, 100, source_is_single_frame).is_none());
    }

    /// Build an in-memory, single-pixel-per-frame TIFF with `frame_count` frames.
    #[cfg(feature = "ocr")]
    fn build_multiframe_tiff(frame_count: usize) -> Vec<u8> {
        let mut cursor = std::io::Cursor::new(Vec::new());
        {
            let mut encoder = tiff::encoder::TiffEncoder::new(&mut cursor).unwrap();
            for i in 0..frame_count {
                encoder
                    .write_image::<tiff::encoder::colortype::Gray8>(1, 1, &[i as u8])
                    .unwrap();
            }
        }
        cursor.into_inner()
    }

    /// #1451: `max_pages` must reject a multi-frame TIFF once its frame count is
    /// known, before per-frame OCR begins. Against unfixed code
    /// `enforce_image_page_limit` does not exist at all, so this fails to compile;
    /// once it exists but does not check frame count, it returns `Ok(())` for a
    /// 3-frame TIFF under a configured limit of 2 instead of the expected
    /// `SecurityError::TooManyPages`.
    #[cfg(feature = "ocr")]
    #[test]
    fn should_reject_tiff_exceeding_max_pages() {
        let tiff_bytes = build_multiframe_tiff(3);
        let config = ExtractionConfig {
            security_limits: Some(crate::extractors::security::SecurityLimits {
                max_pages: Some(2),
                ..Default::default()
            }),
            ..Default::default()
        };

        let error = enforce_image_page_limit(&tiff_bytes, "image/tiff", &config)
            .expect_err("a TIFF with more frames than max_pages must be rejected");
        let message = error.to_string();
        assert!(
            message.contains("too many pages") || message.contains("max_pages"),
            "error must name the limit that was hit: {message}"
        );
    }

    /// A TIFF exactly at the configured `max_pages` ceiling must not be rejected --
    /// the off-by-one boundary case #1451 asked to get right.
    #[cfg(feature = "ocr")]
    #[test]
    fn should_accept_tiff_at_max_pages_boundary() {
        let tiff_bytes = build_multiframe_tiff(3);
        let config = ExtractionConfig {
            security_limits: Some(crate::extractors::security::SecurityLimits {
                max_pages: Some(3),
                ..Default::default()
            }),
            ..Default::default()
        };

        assert!(
            enforce_image_page_limit(&tiff_bytes, "image/tiff", &config).is_ok(),
            "a TIFF exactly at max_pages must not be rejected"
        );
    }

    /// The default `SecurityLimits` (`max_pages: None`) must never reject a
    /// multi-frame TIFF: a real ceiling here is opt-in, not implied by #1451.
    #[cfg(feature = "ocr")]
    #[test]
    fn should_accept_tiff_under_default_max_pages() {
        let tiff_bytes = build_multiframe_tiff(3);
        let config = ExtractionConfig::default();

        assert!(
            enforce_image_page_limit(&tiff_bytes, "image/tiff", &config).is_ok(),
            "default security limits (max_pages: None) must not reject a multi-frame TIFF"
        );
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_clip_layout_regions_to_image_bounds_and_drop_invalid_boxes() {
        let detections = vec![
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::Text,
                0.96,
                crate::layout::BBox::new(-20.0, -10.0, 220.0, 110.0),
            ),
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::Text,
                0.80,
                crate::layout::BBox::new(f32::NAN, 0.0, 10.0, 10.0),
            ),
            crate::layout::LayoutDetection::new(
                crate::layout::LayoutClass::Text,
                0.70,
                crate::layout::BBox::new(30.0, 30.0, 20.0, 20.0),
            ),
        ];

        let regions = layout_regions_from_detections(&detections, 200, 100);

        assert_eq!(regions.len(), 1);
        assert_eq!(regions[0].bounding_box.x0, 0.0);
        assert_eq!(regions[0].bounding_box.y0, 0.0);
        assert_eq!(regions[0].bounding_box.x1, 200.0);
        assert_eq!(regions[0].bounding_box.y1, 100.0);
        assert_eq!(regions[0].area_fraction, 1.0);
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_retain_cached_whole_image_when_region_ocr_fails() {
        let mut whole = image_ocr_document("cached whole-image text");
        whole.metadata.additional.insert(
            std::borrow::Cow::Borrowed("ocr_candidate"),
            serde_json::json!("whole-image"),
        );

        let retained = cached_whole_image_after_layout_error(
            &Ok(whole),
            crate::XbergError::Other("region backend failed".to_string()),
        )
        .expect("cached whole-image OCR must remain usable");

        assert_eq!(internal_document_text(&retained), "cached whole-image text");
        assert_eq!(
            retained.metadata.additional.get("ocr_candidate"),
            Some(&serde_json::json!("whole-image"))
        );
        assert_eq!(retained.processing_warnings.len(), 1);
        assert_eq!(
            retained.processing_warnings[0].message,
            "Layout-region OCR failed after whole-image OCR succeeded; retained whole-image output"
        );
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[test]
    fn should_report_both_ocr_failures() {
        let result = cached_whole_image_after_layout_error(
            &Err(crate::XbergError::Other("whole backend failed".to_string())),
            crate::XbergError::Other("region backend failed".to_string()),
        );

        let message = result
            .expect_err("both failed OCR paths must return an error")
            .to_string();
        assert!(message.contains("whole-image OCR: whole backend failed"));
        assert!(message.contains("layout-region OCR: region backend failed"));
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[tokio::test]
    async fn should_not_retry_whole_image_ocr_after_layout_path_failure() {
        let layout_calls = std::cell::Cell::new(0);
        let whole_calls = std::cell::Cell::new(0);

        let result = extract_selected_image_ocr_path(
            true,
            || {
                layout_calls.set(layout_calls.get() + 1);
                std::future::ready(Err(crate::XbergError::Other("layout path failed".to_string())))
            },
            || {
                whole_calls.set(whole_calls.get() + 1);
                std::future::ready(Ok(image_ocr_document("unexpected retry")))
            },
        )
        .await;

        assert!(result.is_err());
        assert_eq!(layout_calls.get(), 1);
        assert_eq!(whole_calls.get(), 0);
    }

    #[cfg(all(feature = "layout-detection", feature = "ocr"))]
    #[tokio::test]
    async fn should_use_default_whole_image_ocr_when_layout_has_no_ocr_config() {
        let config = ExtractionConfig {
            layout: Some(crate::core::config::LayoutDetectionConfig::default()),
            ocr: None,
            ..Default::default()
        };
        let layout_calls = std::cell::Cell::new(0);
        let whole_calls = std::cell::Cell::new(0);

        let result = extract_selected_image_ocr_path(
            should_use_layout_ocr(&config),
            || {
                layout_calls.set(layout_calls.get() + 1);
                std::future::ready(Err(crate::XbergError::Other("unexpected layout path".to_string())))
            },
            || {
                whole_calls.set(whole_calls.get() + 1);
                std::future::ready(Ok(image_ocr_document("default whole-image OCR")))
            },
        )
        .await
        .expect("missing explicit OCR config must retain the default whole-image path");

        assert_eq!(internal_document_text(&result), "default whole-image OCR");
        assert_eq!(layout_calls.get(), 0);
        assert_eq!(whole_calls.get(), 1);
    }

    #[test]
    fn should_use_whole_image_ocr_when_invoice_regions_drop_fields() {
        let layout = image_ocr_document("Invoice 1042 Acme Total 1250 USD");
        let mut whole = image_ocr_document("Invoice 1042 Acme Corporation Date July 31 Total 1250 USD");
        whole.metadata.additional.insert(
            std::borrow::Cow::Borrowed("ocr_candidate"),
            serde_json::Value::String("whole-image".to_string()),
        );
        whole.processing_warnings.push(crate::types::ProcessingWarning {
            source: std::borrow::Cow::Borrowed("ocr"),
            message: std::borrow::Cow::Borrowed("whole-image warning"),
        });

        let selected = select_image_ocr_result(layout, Ok(whole));

        assert_eq!(
            internal_document_text(&selected),
            "Invoice 1042 Acme Corporation Date July 31 Total 1250 USD"
        );
        assert_eq!(
            selected.metadata.additional.get("ocr_candidate"),
            Some(&serde_json::Value::String("whole-image".to_string()))
        );
        assert_eq!(selected.processing_warnings.len(), 1);
        assert_eq!(selected.processing_warnings[0].message, "whole-image warning");
    }

    #[test]
    fn should_keep_layout_ocr_for_complete_simple_line() {
        let mut layout = image_ocr_document("The quick brown fox jumps over the lazy dog");
        layout.metadata.additional.insert(
            std::borrow::Cow::Borrowed("ocr_candidate"),
            serde_json::Value::String("layout".to_string()),
        );
        let whole = image_ocr_document("The quick brown fox jumps over the lazy dog");

        let selected = select_image_ocr_result(layout, Ok(whole));

        assert_eq!(
            selected.metadata.additional.get("ocr_candidate"),
            Some(&serde_json::Value::String("layout".to_string()))
        );
    }

    #[cfg(feature = "quality")]
    #[test]
    fn should_use_whole_image_ocr_when_complex_layout_text_scores_lower() {
        let layout =
            image_ocr_document("Quarterly   revenue   increased   while   operating   expenses   remained   stable.");
        let whole = image_ocr_document("Quarterly revenue increased while operating expenses remained stable.");

        assert_eq!(
            alphanumeric_token_retention(&internal_document_text(&layout), &internal_document_text(&whole)),
            1.0
        );
        let selected = select_image_ocr_result(layout, Ok(whole));

        assert_eq!(
            internal_document_text(&selected),
            "Quarterly revenue increased while operating expenses remained stable."
        );
    }

    #[test]
    fn should_count_duplicate_alphanumeric_tokens_individually() {
        let retention = alphanumeric_token_retention("invoice total", "invoice invoice total");

        assert!((retention - (2.0 / 3.0)).abs() < f64::EPSILON);
    }

    #[test]
    fn should_accept_exact_minimum_alphanumeric_token_retention() {
        let retention = alphanumeric_token_retention("one two three four", "one two three four five");

        assert!((retention - MIN_LAYOUT_OCR_ALPHANUMERIC_TOKEN_RETENTION).abs() < f64::EPSILON);
    }

    #[test]
    fn should_keep_layout_ocr_when_whole_image_comparison_fails() {
        let mut layout = image_ocr_document("Invoice 1042 Total 1250 USD");
        layout.metadata.additional.insert(
            std::borrow::Cow::Borrowed("ocr_candidate"),
            serde_json::Value::String("layout".to_string()),
        );
        layout.processing_warnings.push(crate::types::ProcessingWarning {
            source: std::borrow::Cow::Borrowed("layout-ocr"),
            message: std::borrow::Cow::Borrowed("layout warning"),
        });

        let selected = select_image_ocr_result(
            layout,
            Err(crate::XbergError::Other("comparison OCR failed".to_string())),
        );

        assert_eq!(
            selected.metadata.additional.get("ocr_candidate"),
            Some(&serde_json::Value::String("layout".to_string()))
        );
        assert_eq!(selected.processing_warnings.len(), 1);
        assert_eq!(selected.processing_warnings[0].message, "layout warning");
    }

    /// Regression test for #705: a backend that gates ocr_elements on include_elements
    /// (e.g. paddle-ocr) must still produce non-empty pages[].
    ///
    /// extract_with_ocr forces include_elements=true before calling the backend so that
    /// elements are available; prebuilt_pages is then set from the HOCR content string,
    /// ensuring pages[] is populated regardless of the original config.
    #[cfg(feature = "ocr")]
    #[tokio::test]
    async fn test_extract_with_ocr_populates_pages_for_elements_gated_backend() {
        use crate::core::config::OcrConfig;
        use crate::plugins::{OcrBackend, OcrBackendType, Plugin, register_ocr_backend, unregister_ocr_backend};
        use crate::types::{
            ExtractedDocument, OcrBoundingGeometry, OcrConfidence, OcrElement, OcrElementConfig, OcrElementLevel,
        };

        let mut png_buf = std::io::Cursor::new(Vec::new());
        image::ImageBuffer::<image::Rgb<u8>, _>::from_pixel(1, 1, image::Rgb([255u8, 255, 255]))
            .write_to(&mut png_buf, image::ImageFormat::Png)
            .expect("failed to encode test PNG");
        let png_1x1 = png_buf.into_inner();

        /// A mock backend that behaves like paddle-ocr: returns ocr_elements only when
        /// include_elements is true. This is the exact contract that caused issue #705.
        struct GatedElementsBackend;

        #[async_trait::async_trait]
        impl OcrBackend for GatedElementsBackend {
            fn backend_type(&self) -> OcrBackendType {
                OcrBackendType::Custom
            }
            fn supports_language(&self, _: &str) -> bool {
                true
            }
            async fn process_image(&self, _: &[u8], config: &OcrConfig) -> crate::Result<ExtractedDocument> {
                let include_elements = config.element_config.as_ref().is_some_and(|ec| ec.include_elements);

                let elements = if include_elements {
                    let element = |text: &str, level: OcrElementLevel, confidence: f64, left: u32, width: u32| {
                        OcrElement::new(
                            text.to_string(),
                            OcrBoundingGeometry::Rectangle {
                                left,
                                top: 0,
                                width,
                                height: 20,
                            },
                            OcrConfidence::from_tesseract(confidence),
                        )
                        .with_level(level)
                        .with_page_number(1)
                    };
                    Some(vec![
                        element("hello world", OcrElementLevel::Line, 99.0, 0, 100),
                        element("hello", OcrElementLevel::Word, 95.0, 5, 30),
                        element("noise", OcrElementLevel::Word, 20.0, 60, 30),
                    ])
                } else {
                    None
                };

                Ok(ExtractedDocument {
                    content: "hello world".to_string(),
                    ocr_elements: elements,
                    ..Default::default()
                })
            }
        }

        impl Plugin for GatedElementsBackend {
            fn name(&self) -> &str {
                "gated-elements-test"
            }
            fn version(&self) -> String {
                "0.0.0".to_string()
            }
            fn initialize(&self) -> crate::Result<()> {
                Ok(())
            }
            fn shutdown(&self) -> crate::Result<()> {
                Ok(())
            }
        }

        register_ocr_backend(std::sync::Arc::new(GatedElementsBackend)).unwrap();

        let config = ExtractionConfig {
            ocr: Some(OcrConfig {
                backend: "gated-elements-test".to_string(),
                ..Default::default()
            }),
            ..Default::default()
        };

        let extractor = ImageExtractor::new();
        let internal_doc = extractor.extract_content(&png_1x1, "image/png", &config).await.unwrap();

        let result = crate::extraction::derive::derive_extraction_result(
            internal_doc,
            false,
            crate::core::config::OutputFormat::Plain,
        );

        assert!(
            result.metadata.ocr_used,
            "successful image OCR must be reflected in metadata"
        );
        assert_eq!(result.extraction_method, Some(crate::types::ExtractionMethod::Ocr));
        assert!(
            result.ocr_elements.is_none(),
            "elements forced for internal page assembly must remain private unless requested"
        );
        let pages = result
            .pages
            .as_ref()
            .expect("pages must be populated (regression of #705)");
        assert!(!pages.is_empty(), "pages[] must not be empty (regression of #705)");
        assert_eq!(pages[0].content.trim(), "hello world");

        let explicitly_disabled = ExtractionConfig {
            ocr: Some(OcrConfig {
                backend: "gated-elements-test".to_string(),
                element_config: Some(OcrElementConfig::default()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let disabled_doc = extractor
            .extract_content(&png_1x1, "image/png", &explicitly_disabled)
            .await
            .unwrap();
        assert!(disabled_doc.prebuilt_ocr_elements.is_none());

        let filtered_config = ExtractionConfig {
            ocr: Some(OcrConfig {
                backend: "gated-elements-test".to_string(),
                element_config: Some(OcrElementConfig {
                    include_elements: true,
                    min_level: OcrElementLevel::Word,
                    min_confidence: 0.8,
                    build_hierarchy: true,
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let filtered_doc = extractor
            .extract_content(&png_1x1, "image/png", &filtered_config)
            .await
            .unwrap();
        let filtered = filtered_doc
            .prebuilt_ocr_elements
            .expect("requested custom-backend elements must be published");
        assert_eq!(
            filtered.iter().map(|element| element.text.as_str()).collect::<Vec<_>>(),
            ["hello world", "hello"]
        );
        let line = filtered.iter().find(|element| element.text == "hello world").unwrap();
        let word = filtered.iter().find(|element| element.text == "hello").unwrap();
        assert!(line.parent_id.is_none());
        assert!(word.parent_id.is_some());

        unregister_ocr_backend("gated-elements-test").unwrap();
    }

    /// Regression test for #706: pages[0].content must be the coherent HOCR-rendered
    /// text, not a word-by-word dump assembled from raw OcrText elements.
    #[cfg(feature = "ocr")]
    #[tokio::test]
    async fn test_extract_with_ocr_page_content_matches_top_level_content() {
        use crate::core::config::OcrConfig;
        use crate::plugins::{OcrBackend, OcrBackendType, Plugin, register_ocr_backend, unregister_ocr_backend};
        use crate::types::{ExtractedDocument, OcrBoundingGeometry, OcrConfidence, OcrElement, OcrElementLevel};

        let mut png_buf = std::io::Cursor::new(Vec::new());
        image::ImageBuffer::<image::Rgb<u8>, _>::from_pixel(1, 1, image::Rgb([255u8, 255, 255]))
            .write_to(&mut png_buf, image::ImageFormat::Png)
            .expect("failed to encode test PNG");
        let png_1x1 = png_buf.into_inner();

        const COHERENT: &str = "Sales Report 2024\n\nThis report contains quarterly sales data.";

        struct TesseractLikeBackend;

        #[async_trait::async_trait]
        impl OcrBackend for TesseractLikeBackend {
            fn backend_type(&self) -> OcrBackendType {
                OcrBackendType::Custom
            }
            fn supports_language(&self, _: &str) -> bool {
                true
            }
            async fn process_image(&self, _: &[u8], _: &OcrConfig) -> crate::Result<ExtractedDocument> {
                let content = COHERENT.to_string();
                let words = [
                    "Sales",
                    "Report",
                    "2024",
                    "This",
                    "report",
                    "contains",
                    "quarterly",
                    "sales",
                    "data.",
                ];
                let mut elements = Vec::new();
                for (i, word) in words.iter().enumerate() {
                    let geo = OcrBoundingGeometry::Rectangle {
                        left: i as u32 * 60,
                        top: 0,
                        width: 50,
                        height: 20,
                    };
                    let elem = OcrElement::new(word.to_string(), geo, OcrConfidence::from_tesseract(99.0))
                        .with_level(OcrElementLevel::Word)
                        .with_page_number(1);
                    elements.push(elem);
                }
                Ok(ExtractedDocument {
                    content,
                    ocr_elements: Some(elements),
                    ..Default::default()
                })
            }
        }

        impl Plugin for TesseractLikeBackend {
            fn name(&self) -> &str {
                "tesseract-like-706"
            }
            fn version(&self) -> String {
                "0.0.0".to_string()
            }
            fn initialize(&self) -> crate::Result<()> {
                Ok(())
            }
            fn shutdown(&self) -> crate::Result<()> {
                Ok(())
            }
        }

        register_ocr_backend(std::sync::Arc::new(TesseractLikeBackend)).unwrap();

        let config = ExtractionConfig {
            ocr: Some(OcrConfig {
                backend: "tesseract-like-706".to_string(),
                ..Default::default()
            }),
            ..Default::default()
        };

        let extractor = ImageExtractor::new();
        let internal_doc = extractor.extract_content(&png_1x1, "image/png", &config).await.unwrap();

        let result = crate::extraction::derive::derive_extraction_result(
            internal_doc,
            false,
            crate::core::config::OutputFormat::Plain,
        );

        assert_eq!(result.content.trim(), COHERENT, "top-level content mismatch");

        let pages = result
            .pages
            .as_ref()
            .expect("pages must be populated (regression of #706)");
        assert!(!pages.is_empty(), "pages must not be empty");
        assert_eq!(
            pages[0].content.trim(),
            COHERENT,
            "pages[0].content is a word-by-word dump instead of coherent text (regression of #706)"
        );

        unregister_ocr_backend("tesseract-like-706").unwrap();
    }

    #[tokio::test]
    async fn test_image_extractor_invalid_image() {
        let extractor = ImageExtractor::new();
        let invalid_bytes = vec![0, 1, 2, 3, 4, 5];
        let config = ExtractionConfig::default();

        let result = extractor.extract_content(&invalid_bytes, "image/png", &config).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_image_plugin_interface() {
        let extractor = ImageExtractor::new();
        assert_eq!(extractor.name(), "image-extractor");
        assert_eq!(extractor.version(), env!("CARGO_PKG_VERSION"));
        assert!(extractor.supported_mime_types().contains(&"image/png"));
        assert!(extractor.supported_mime_types().contains(&"image/jpeg"));
        assert!(extractor.supported_mime_types().contains(&"image/webp"));
        assert_eq!(extractor.priority(), 50);
    }

    #[test]
    fn test_image_extractor_default() {
        let extractor = ImageExtractor;
        assert_eq!(extractor.name(), "image-extractor");
    }

    #[test]
    fn test_image_extractor_supports_alias_mime_types() {
        let extractor = ImageExtractor::new();
        let supported = extractor.supported_mime_types();
        assert!(supported.contains(&"image/pjpeg"));
        assert!(supported.contains(&"image/x-bmp"));
        assert!(supported.contains(&"image/x-ms-bmp"));
        assert!(supported.contains(&"image/x-tiff"));
        assert!(supported.contains(&"image/x-portable-anymap"));
    }

    /// Regression test for #732: when captioning is configured and OCR runs,
    /// doc.images must contain the raw image bytes so CaptioningProcessor can act.
    #[cfg(feature = "ocr")]
    #[tokio::test]
    async fn test_extract_with_ocr_populates_images_for_captioning() {
        use crate::core::config::{CaptioningConfig, LlmConfig, OcrConfig};
        use crate::plugins::{OcrBackend, OcrBackendType, Plugin, register_ocr_backend, unregister_ocr_backend};
        use crate::types::ExtractedDocument;

        let mut png_buf = std::io::Cursor::new(Vec::new());
        image::ImageBuffer::<image::Rgb<u8>, _>::from_pixel(1, 1, image::Rgb([255u8, 255, 255]))
            .write_to(&mut png_buf, image::ImageFormat::Png)
            .expect("failed to encode test PNG");
        let png_1x1 = png_buf.into_inner();

        /// A minimal mock OCR backend that returns empty text.
        struct EmptyOcrBackend;

        #[async_trait::async_trait]
        impl OcrBackend for EmptyOcrBackend {
            fn backend_type(&self) -> OcrBackendType {
                OcrBackendType::Custom
            }
            fn supports_language(&self, _: &str) -> bool {
                true
            }
            async fn process_image(&self, _: &[u8], _config: &OcrConfig) -> crate::Result<ExtractedDocument> {
                Ok(ExtractedDocument {
                    content: String::new(),
                    ..Default::default()
                })
            }
        }

        impl Plugin for EmptyOcrBackend {
            fn name(&self) -> &str {
                "empty-ocr-732"
            }
            fn version(&self) -> String {
                "0.0.0".to_string()
            }
            fn initialize(&self) -> crate::Result<()> {
                Ok(())
            }
            fn shutdown(&self) -> crate::Result<()> {
                Ok(())
            }
        }

        register_ocr_backend(std::sync::Arc::new(EmptyOcrBackend)).unwrap();
        struct BackendGuard(&'static str);
        impl Drop for BackendGuard {
            fn drop(&mut self) {
                let _ = unregister_ocr_backend(self.0);
            }
        }
        let _guard = BackendGuard("empty-ocr-732");

        let config = ExtractionConfig {
            ocr: Some(OcrConfig {
                backend: "empty-ocr-732".to_string(),
                ..Default::default()
            }),
            captioning: Some(CaptioningConfig {
                llm: LlmConfig {
                    model: "openai/gpt-4o-mini".to_string(),
                    ..Default::default()
                },
                prompt: None,
                min_image_area: 0,
            }),
            ..Default::default()
        };

        let extractor = ImageExtractor::new();
        let doc = extractor.extract_content(&png_1x1, "image/png", &config).await.unwrap();

        assert_eq!(
            doc.images.len(),
            1,
            "doc.images must contain the raw image (regression of #732)"
        );
        assert!(!doc.images[0].data.is_empty(), "image data must be non-empty");
    }

    /// Full-pipeline regression for #732: InternalDocument.images must survive the
    /// derive.rs conversion so ExtractedDocument.images is Some after run_pipeline.
    // Holds a `ProcessorSnapshotLease` for the whole `run_pipeline` call. Without `#[serial]`
    // this raced the `#[serial]`-guarded lifecycle tests in `core::pipeline::tests`, whose
    // mutations are refused while any snapshot lease is live. ~keep
    #[cfg(feature = "ocr")]
    #[tokio::test]
    #[serial_test::serial]
    async fn test_pipeline_images_some_after_ocr_with_captioning() {
        use crate::core::config::{CaptioningConfig, LlmConfig, OcrConfig};
        use crate::core::pipeline::run_pipeline;
        use crate::plugins::{OcrBackend, OcrBackendType, Plugin, register_ocr_backend, unregister_ocr_backend};
        use crate::types::ExtractedDocument;

        let mut png_buf = std::io::Cursor::new(Vec::new());
        image::ImageBuffer::<image::Rgb<u8>, _>::from_pixel(1, 1, image::Rgb([255u8, 255, 255]))
            .write_to(&mut png_buf, image::ImageFormat::Png)
            .expect("failed to encode test PNG");
        let png_1x1 = png_buf.into_inner();

        struct EmptyOcrBackend732b;

        #[async_trait::async_trait]
        impl OcrBackend for EmptyOcrBackend732b {
            fn backend_type(&self) -> OcrBackendType {
                OcrBackendType::Custom
            }
            fn supports_language(&self, _: &str) -> bool {
                true
            }
            async fn process_image(&self, _: &[u8], _config: &OcrConfig) -> crate::Result<ExtractedDocument> {
                Ok(ExtractedDocument {
                    content: String::new(),
                    ..Default::default()
                })
            }
        }
        impl Plugin for EmptyOcrBackend732b {
            fn name(&self) -> &str {
                "empty-ocr-732b"
            }
            fn version(&self) -> String {
                "0.0.0".to_string()
            }
            fn initialize(&self) -> crate::Result<()> {
                Ok(())
            }
            fn shutdown(&self) -> crate::Result<()> {
                Ok(())
            }
        }

        register_ocr_backend(std::sync::Arc::new(EmptyOcrBackend732b)).unwrap();
        struct BackendGuard(&'static str);
        impl Drop for BackendGuard {
            fn drop(&mut self) {
                let _ = unregister_ocr_backend(self.0);
            }
        }
        let _guard = BackendGuard("empty-ocr-732b");

        let config = ExtractionConfig {
            ocr: Some(OcrConfig {
                backend: "empty-ocr-732b".to_string(),
                ..Default::default()
            }),
            captioning: Some(CaptioningConfig {
                llm: LlmConfig {
                    model: "openai/gpt-4o-mini".to_string(),
                    ..Default::default()
                },
                prompt: None,
                min_image_area: u32::MAX,
            }),
            ..Default::default()
        };

        let extractor = ImageExtractor::new();
        let doc = extractor.extract_content(&png_1x1, "image/png", &config).await.unwrap();
        assert_eq!(doc.images.len(), 1, "InternalDocument must have image before pipeline");

        let result = run_pipeline(doc, &config).await.unwrap();
        assert!(
            result.images.is_some(),
            "ExtractedDocument.images must be Some after pipeline — regression of #732"
        );
        assert_eq!(
            result.images.unwrap().len(),
            1,
            "image count must survive the derive.rs conversion"
        );
    }

    /// Regression test for task #709: `extract_layout_regions` must stop dispatching
    /// OCR calls for further layout regions once cancellation is signalled, instead of
    /// working through every remaining region regardless.
    ///
    /// Proves the checkpoint actually stops work (not merely that an error comes back):
    /// a counting backend records how many regions it was actually asked to OCR. With a
    /// token cancelled *before* the call, the loop's first-iteration check must break
    /// before the backend is invoked for *any* region, so the counter stays at 0 out of
    /// 3 regions — against code with the checkpoint removed, this backend is invoked for
    /// all 3 regions regardless of cancellation, so the counter would be 3, not 0.
    #[cfg(all(feature = "layout-detection", any(feature = "ocr", feature = "ocr-wasm")))]
    #[tokio::test]
    async fn extract_layout_regions_stops_dispatching_once_cancelled() {
        use crate::cancellation::CancellationToken;
        use crate::core::config::OcrConfig;
        use crate::layout::{BBox, LayoutClass, LayoutDetection};
        use crate::plugins::{OcrBackend, OcrBackendType, Plugin};
        use crate::types::ExtractedDocument;
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        /// A backend that counts how many regions it was actually asked to OCR,
        /// standing in for a real (slow) OCR call so the test stays fast and
        /// deterministic while still proving the loop-level checkpoint's effect.
        struct CountingOcrBackend(Arc<AtomicUsize>);

        #[async_trait::async_trait]
        impl OcrBackend for CountingOcrBackend {
            fn backend_type(&self) -> OcrBackendType {
                OcrBackendType::Custom
            }
            fn supports_language(&self, _: &str) -> bool {
                true
            }
            async fn process_image(&self, _: &[u8], _: &OcrConfig) -> crate::Result<ExtractedDocument> {
                self.0.fetch_add(1, Ordering::SeqCst);
                Ok(ExtractedDocument {
                    content: "region text".to_string(),
                    ..Default::default()
                })
            }
        }

        impl Plugin for CountingOcrBackend {
            fn name(&self) -> &str {
                "counting-ocr-test-709"
            }
            fn version(&self) -> String {
                "0.0.0".to_string()
            }
            fn initialize(&self) -> crate::Result<()> {
                Ok(())
            }
            fn shutdown(&self) -> crate::Result<()> {
                Ok(())
            }
        }

        let rgb = image::RgbImage::from_pixel(100, 100, image::Rgb([255u8, 255, 255]));
        let detections = vec![
            LayoutDetection::new(LayoutClass::Text, 0.9, BBox::new(0.0, 0.0, 20.0, 20.0)),
            LayoutDetection::new(LayoutClass::Text, 0.9, BBox::new(30.0, 0.0, 50.0, 20.0)),
            LayoutDetection::new(LayoutClass::Text, 0.9, BBox::new(60.0, 0.0, 80.0, 20.0)),
        ];
        let ocr_config = OcrConfig::default();

        let calls = Arc::new(AtomicUsize::new(0));
        let backend: Arc<dyn OcrBackend> = Arc::new(CountingOcrBackend(Arc::clone(&calls)));
        let cancelled_token = CancellationToken::new();
        cancelled_token.cancel();

        let doc = extract_layout_regions(
            Arc::clone(&backend),
            &rgb,
            &detections,
            &ocr_config,
            None,
            Some(&cancelled_token),
        )
        .await
        .expect("extract_layout_regions must not error when cancelled, only skip remaining work");

        assert_eq!(
            calls.load(Ordering::SeqCst),
            0,
            "no region should be OCR'd once the token is already cancelled"
        );
        assert!(
            doc.processing_warnings
                .iter()
                .any(|warning| warning.message.contains("cancelled")),
            "a cancelled run should explain why regions were skipped, got {:?}",
            doc.processing_warnings
        );

        // Sanity check: the same 3 regions, uncancelled, all reach the backend — this
        // rules out the zero count above being an unrelated bug (e.g. a bbox rejected
        // by `encode_layout_region`) rather than the cancellation checkpoint.
        calls.store(0, Ordering::SeqCst);
        let _ = extract_layout_regions(Arc::clone(&backend), &rgb, &detections, &ocr_config, None, None)
            .await
            .expect("uncancelled extract_layout_regions must succeed");
        assert_eq!(
            calls.load(Ordering::SeqCst),
            3,
            "all 3 regions should be OCR'd when nothing is cancelled"
        );
    }

    // -- enforce_image_page_limit stub (not(feature = "ocr")) -------------------
    //
    // The `ocr`-enabled sibling counts real TIFF frames and is exercised
    // elsewhere; these tests cover the no-op stub compiled when the `tiff`
    // crate isn't available, which instead emits a one-shot warning so a
    // caller-set `max_pages` going unenforced is discoverable (#1451-adjacent).
    //
    // `warn_unenforced_tiff_page_limit_once` takes its "already warned" guard
    // as a parameter rather than reading `UNENFORCED_TIFF_PAGE_LIMIT_WARNED`
    // directly, mirroring `warn_default_thread_cap_once` in
    // `core::config::concurrency`: a shared process-global guard can only be
    // asserted from a single test without the assertion racing every other
    // test in the binary that reaches the same code path. Three of the four
    // tests below use a guard they own, so they are independent of each other
    // and of test execution order; only the "fires" test below exercises the
    // real global-backed entry point (`enforce_image_page_limit`), and it is
    // the only test in this binary that does so.
    #[cfg(not(feature = "ocr"))]
    mod enforce_image_page_limit_stub {
        use std::sync::atomic::AtomicBool;
        use std::sync::{Arc, Mutex};

        use tracing_subscriber::layer::SubscriberExt as _;
        use tracing_subscriber::{EnvFilter, Layer};

        use super::super::{ExtractionConfig, enforce_image_page_limit, enforce_image_page_limit_stub_with_guard};
        use crate::extractors::security::SecurityLimits;

        /// A tracing `Layer` that records the level of every emitted event.
        #[derive(Clone, Default)]
        struct EventCapture {
            levels: Arc<Mutex<Vec<tracing::Level>>>,
        }

        impl<S> Layer<S> for EventCapture
        where
            S: tracing::Subscriber,
        {
            fn on_event(&self, event: &tracing::Event<'_>, _ctx: tracing_subscriber::layer::Context<'_, S>) {
                self.levels.lock().unwrap().push(*event.metadata().level());
            }
        }

        fn warn_event_count(capture: &EventCapture) -> usize {
            capture
                .levels
                .lock()
                .unwrap()
                .iter()
                .filter(|level| **level == tracing::Level::WARN)
                .count()
        }

        fn config_with_max_pages(max_pages: usize) -> ExtractionConfig {
            ExtractionConfig {
                security_limits: Some(SecurityLimits {
                    max_pages: Some(max_pages),
                    ..Default::default()
                }),
                ..Default::default()
            }
        }

        /// Changing this to `_content.is_empty()` (as opposed to inspecting
        /// `mime_type`) would make this test pass for the wrong reason; the
        /// content bytes are irrelevant to the stub, which is the point.
        const TIFF_MIME: &str = "image/tiff";

        /// The real, process-global-backed entry point warns exactly once
        /// across repeated calls when `max_pages` is set on a TIFF. Bundled
        /// into a single test (rather than a separate "fires" and "fires
        /// once" test) because both assertions read the same global guard;
        /// splitting them would make the pass/fail outcome depend on which
        /// test the harness happens to run first.
        ///
        /// Fails if: the `if !mime_type...contains("tiff")` check in
        /// `enforce_image_page_limit_stub_with_guard` is removed or inverted;
        /// the `let Some(max_pages) = max_pages else { return Ok(()) };` guard
        /// is removed; `warn_unenforced_tiff_page_limit_once` is changed to
        /// unconditionally warn (no longer once); or the call to
        /// `warn_unenforced_tiff_page_limit_once` is deleted altogether.
        #[test]
        fn fires_exactly_once_for_tiff_with_max_pages_set_across_repeated_calls() {
            let capture = EventCapture::default();
            let subscriber = tracing_subscriber::registry()
                .with(EnvFilter::new("warn"))
                .with(capture.clone());
            let config = config_with_max_pages(3);

            tracing::subscriber::with_default(subscriber, || {
                for _ in 0..5 {
                    let result = enforce_image_page_limit(b"unused", TIFF_MIME, &config);
                    assert!(result.is_ok(), "the stub warns but never rejects extraction");
                }
            });

            assert_eq!(
                warn_event_count(&capture),
                1,
                "expected exactly one WARN across repeated calls, got {:?}",
                capture.levels.lock().unwrap()
            );
        }

        /// Fails if: the `let Some(max_pages) = max_pages else { return
        /// Ok(()) };` early return in `enforce_image_page_limit_stub_with_guard`
        /// is removed (or its condition inverted), causing an unset
        /// `max_pages` to warn anyway.
        #[test]
        fn does_not_fire_when_max_pages_is_unset() {
            let already_warned = AtomicBool::new(false);
            let capture = EventCapture::default();
            let subscriber = tracing_subscriber::registry()
                .with(EnvFilter::new("warn"))
                .with(capture.clone());
            let config = ExtractionConfig::default();
            assert!(config.security_limits.is_none());

            let result = tracing::subscriber::with_default(subscriber, || {
                enforce_image_page_limit_stub_with_guard(TIFF_MIME, &config, &already_warned)
            });

            assert!(result.is_ok());
            assert_eq!(warn_event_count(&capture), 0);
        }

        /// Fails if: the `if !mime_type.to_lowercase().contains("tiff") {
        /// return Ok(()); }` guard in `enforce_image_page_limit_stub_with_guard`
        /// is removed (or its condition inverted), causing a non-TIFF mime type
        /// to warn.
        #[test]
        fn does_not_fire_for_non_tiff_mime() {
            let already_warned = AtomicBool::new(false);
            let capture = EventCapture::default();
            let subscriber = tracing_subscriber::registry()
                .with(EnvFilter::new("warn"))
                .with(capture.clone());
            let config = config_with_max_pages(3);

            let result = tracing::subscriber::with_default(subscriber, || {
                enforce_image_page_limit_stub_with_guard("image/png", &config, &already_warned)
            });

            assert!(result.is_ok());
            assert_eq!(warn_event_count(&capture), 0);
        }

        /// A second, independently-guarded call for a TIFF with `max_pages`
        /// set still succeeds and still warns — proving the "does not fire"
        /// tests above are silent because their inputs don't qualify, not
        /// because the warning path is broken. Fails under the same mutations
        /// as `fires_exactly_once_for_tiff_with_max_pages_set_across_repeated_calls`.
        #[test]
        fn fires_when_max_pages_is_set_and_mime_is_tiff_with_a_fresh_guard() {
            let already_warned = AtomicBool::new(false);
            let capture = EventCapture::default();
            let subscriber = tracing_subscriber::registry()
                .with(EnvFilter::new("warn"))
                .with(capture.clone());
            let config = config_with_max_pages(3);

            let result = tracing::subscriber::with_default(subscriber, || {
                enforce_image_page_limit_stub_with_guard(TIFF_MIME, &config, &already_warned)
            });

            assert!(result.is_ok());
            assert_eq!(warn_event_count(&capture), 1);
        }
    }
}