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
|
Unit JdMarker;
{ This file contains routines to decode JPEG datastream markers.
Most of the complexity arises from our desire to support input
suspension: if not all of the data for a marker is available;
we must exit back to the application. On resumption; we reprocess
the marker. }
{ Original: jdmarker.c; Copyright (C) 1991-1998; Thomas G. Lane. }
{ History
9.7.96 Conversion to pascal started jnn
22.3.98 updated to 6b jnn }
interface
{$I jconfig.inc}
uses
jmorecfg,
jinclude,
jdeferr,
jerror,
jcomapi,
jpeglib;
const { JPEG marker codes }
M_SOF0 = $c0;
M_SOF1 = $c1;
M_SOF2 = $c2;
M_SOF3 = $c3;
M_SOF5 = $c5;
M_SOF6 = $c6;
M_SOF7 = $c7;
M_JPG = $c8;
M_SOF9 = $c9;
M_SOF10 = $ca;
M_SOF11 = $cb;
M_SOF13 = $cd;
M_SOF14 = $ce;
M_SOF15 = $cf;
M_DHT = $c4;
M_DAC = $cc;
M_RST0 = $d0;
M_RST1 = $d1;
M_RST2 = $d2;
M_RST3 = $d3;
M_RST4 = $d4;
M_RST5 = $d5;
M_RST6 = $d6;
M_RST7 = $d7;
M_SOI = $d8;
M_EOI = $d9;
M_SOS = $da;
M_DQT = $db;
M_DNL = $dc;
M_DRI = $dd;
M_DHP = $de;
M_EXP = $df;
M_APP0 = $e0;
M_APP1 = $e1;
M_APP2 = $e2;
M_APP3 = $e3;
M_APP4 = $e4;
M_APP5 = $e5;
M_APP6 = $e6;
M_APP7 = $e7;
M_APP8 = $e8;
M_APP9 = $e9;
M_APP10 = $ea;
M_APP11 = $eb;
M_APP12 = $ec;
M_APP13 = $ed;
M_APP14 = $ee;
M_APP15 = $ef;
M_JPG0 = $f0;
M_JPG13 = $fd;
M_COM = $fe;
M_TEM = $01;
M_ERROR = $100;
type
JPEG_MARKER = uint; { JPEG marker codes }
{ Private state }
type
my_marker_ptr = ^my_marker_reader;
my_marker_reader = record
pub : jpeg_marker_reader; { public fields }
{ Application-overridable marker processing methods }
process_COM : jpeg_marker_parser_method;
process_APPn : array[0..16-1] of jpeg_marker_parser_method;
{ Limit on marker data length to save for each marker type }
length_limit_COM : uint;
length_limit_APPn : array[0..16-1] of uint;
{ Status of COM/APPn marker saving }
cur_marker : jpeg_saved_marker_ptr; { NIL if not processing a marker }
bytes_read : uint; { data bytes read so far in marker }
{ Note: cur_marker is not linked into marker_list until it's all read. }
end;
{GLOBAL}
function jpeg_resync_to_restart(cinfo : j_decompress_ptr;
desired : int) : boolean;
{GLOBAL}
procedure jinit_marker_reader (cinfo : j_decompress_ptr);
{$ifdef SAVE_MARKERS_SUPPORTED}
{GLOBAL}
procedure jpeg_save_markers (cinfo : j_decompress_ptr;
marker_code : int;
length_limit : uint);
{$ENDIF}
{GLOBAL}
procedure jpeg_set_marker_processor (cinfo : j_decompress_ptr;
marker_code : int;
routine : jpeg_marker_parser_method);
Var
on_unknown_marker : function (cinfo : j_decompress_ptr) : int; far;
implementation
uses
jutils;
{ At all times, cinfo1.src.next_input_byte and .bytes_in_buffer reflect
the current restart point; we update them only when we have reached a
suitable place to restart if a suspension occurs. }
{ Routines to process JPEG markers.
Entry condition: JPEG marker itself has been read and its code saved
in cinfo^.unread_marker; input restart point is just after the marker.
Exit: if return TRUE, have read and processed any parameters, and have
updated the restart point to point after the parameters.
If return FALSE, was forced to suspend before reaching end of
marker parameters; restart point has not been moved. Same routine
will be called again after application supplies more input data.
This approach to suspension assumes that all of a marker's parameters
can fit into a single input bufferload. This should hold for "normal"
markers. Some COM/APPn markers might have large parameter segments
that might not fit. If we are simply dropping such a marker, we use
skip_input_data to get past it, and thereby put the problem on the
source manager's shoulders. If we are saving the marker's contents
into memory, we use a slightly different convention: when forced to
suspend, the marker processor updates the restart point to the end of
what it's consumed (ie, the end of the buffer) before returning FALSE.
On resumption, cinfo->unread_marker still contains the marker code,
but the data source will point to the next chunk of marker data.
The marker processor must retain internal state to deal with this.
Note that we don't bother to avoid duplicate trace messages if a
suspension occurs within marker parameters. Other side effects
require more care. }
{LOCAL}
function get_soi (cinfo : j_decompress_ptr) : boolean;
{ Process an SOI marker }
var
i : int;
begin
{$IFDEF DEBUG}
TRACEMS(j_common_ptr(cinfo), 1, JTRC_SOI);
{$ENDIF}
if (cinfo^.marker^.saw_SOI) then
ERREXIT(j_common_ptr(cinfo), JERR_SOI_DUPLICATE);
{ Reset all parameters that are defined to be reset by SOI }
for i := 0 to Pred(NUM_ARITH_TBLS) do
with cinfo^ do
begin
arith_dc_L[i] := 0;
arith_dc_U[i] := 1;
arith_ac_K[i] := 5;
end;
cinfo^.restart_interval := 0;
{ Set initial assumptions for colorspace etc }
with cinfo^ do
begin
jpeg_color_space := JCS_UNKNOWN;
CCIR601_sampling := FALSE; { Assume non-CCIR sampling??? }
saw_JFIF_marker := FALSE;
JFIF_major_version := 1; { set default JFIF APP0 values }
JFIF_minor_version := 1;
density_unit := 0;
X_density := 1;
Y_density := 1;
saw_Adobe_marker := FALSE;
Adobe_transform := 0;
marker^.saw_SOI := TRUE;
end;
get_soi := TRUE;
end; { get_soi }
{LOCAL}
function get_sof(cinfo : j_decompress_ptr;
is_prog : boolean;
is_arith : boolean) : boolean;
{ Process a SOFn marker }
var
length : INT32;
c, ci : int;
compptr : jpeg_component_info_ptr;
{ Declare and initialize local copies of input pointer/count }
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{}
cinfo^.progressive_mode := is_prog;
cinfo^.arith_code := is_arith;
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
{ Read a byte into variable cinfo^.data_precision.
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
cinfo^.data_precision := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{ Read two bytes interpreted as an unsigned 16-bit integer.
cinfo^.image_height should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
cinfo^.image_height := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( cinfo^.image_height, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
{ Read two bytes interpreted as an unsigned 16-bit integer.
cinfo^.image_width should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
cinfo^.image_width := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( cinfo^.image_width, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
{ Read a byte into variable cinfo^.num_components.
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
cinfo^.num_components := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
Dec(length, 8);
{$IFDEF DEBUG}
TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOF, cinfo^.unread_marker,
int(cinfo^.image_width), int(cinfo^.image_height),
cinfo^.num_components);
{$ENDIF}
if (cinfo^.marker^.saw_SOF) then
ERREXIT(j_common_ptr(cinfo), JERR_SOF_DUPLICATE);
{ We don't support files in which the image height is initially specified }
{ as 0 and is later redefined by DNL. As long as we have to check that, }
{ might as well have a general sanity check. }
if (cinfo^.image_height <= 0) or (cinfo^.image_width <= 0)
or (cinfo^.num_components <= 0) then
ERREXIT(j_common_ptr(cinfo), JERR_EMPTY_IMAGE);
if (length <> (cinfo^.num_components * 3)) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
if (cinfo^.comp_info = NIL) then { do only once, even if suspend }
cinfo^.comp_info := jpeg_component_info_list_ptr(
cinfo^.mem^.alloc_small(j_common_ptr(cinfo), JPOOL_IMAGE,
cinfo^.num_components * SIZEOF(jpeg_component_info)));
compptr := jpeg_component_info_ptr(cinfo^.comp_info);
for ci := 0 to pred(cinfo^.num_components) do
begin
compptr^.component_index := ci;
{ Read a byte into variable compptr^.component_id.
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
compptr^.component_id := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
compptr^.h_samp_factor := (c shr 4) and 15;
compptr^.v_samp_factor := (c ) and 15;
{ Read a byte into variable compptr^.quant_tbl_no.
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sof := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
compptr^.quant_tbl_no := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{$IFDEF DEBUG}
TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOF_COMPONENT,
compptr^.component_id, compptr^.h_samp_factor,
compptr^.v_samp_factor, compptr^.quant_tbl_no);
{$ENDIF}
Inc(compptr);
end;
cinfo^.marker^.saw_SOF := TRUE;
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
get_sof := TRUE;
end; { get_sof }
{LOCAL}
function get_sos (cinfo : j_decompress_ptr) : boolean;
{ Process a SOS marker }
label
id_found;
var
length : INT32;
i, ci, n, c, cc : int;
compptr : jpeg_component_info_ptr;
{ Declare and initialize local copies of input pointer/count }
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr; { Array[] of JOCTET; }
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{}
if not cinfo^.marker^.saw_SOF then
ERREXIT(j_common_ptr(cinfo), JERR_SOS_NO_SOF);
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
{ Read a byte into variable n (Number of components).
If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
n := GETJOCTET(next_input_byte^); { Number of components }
Inc(next_input_byte);
{$IFDEF DEBUG}
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_SOS, n);
{$ENDIF}
if ((length <> (n * 2 + 6)) or (n < 1) or (n > MAX_COMPS_IN_SCAN)) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
cinfo^.comps_in_scan := n;
{ Collect the component-spec parameters }
for i := 0 to Pred(n) do
begin
{ Read a byte into variable cc. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
cc := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
compptr := jpeg_component_info_ptr(cinfo^.comp_info);
for ci := 0 to Pred(cinfo^.num_components) do
begin
if (cc = compptr^.component_id) then
goto id_found;
Inc(compptr);
end;
ERREXIT1(j_common_ptr(cinfo), JERR_BAD_COMPONENT_ID, cc);
id_found:
cinfo^.cur_comp_info[i] := compptr;
compptr^.dc_tbl_no := (c shr 4) and 15;
compptr^.ac_tbl_no := (c ) and 15;
{$IFDEF DEBUG}
TRACEMS3(j_common_ptr(cinfo), 1, JTRC_SOS_COMPONENT, cc,
compptr^.dc_tbl_no, compptr^.ac_tbl_no);
{$ENDIF}
end;
{ Collect the additional scan parameters Ss, Se, Ah/Al. }
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
cinfo^.Ss := c;
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
cinfo^.Se := c;
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_sos := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
cinfo^.Ah := (c shr 4) and 15;
cinfo^.Al := (c ) and 15;
{$IFDEF DEBUG}
TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOS_PARAMS, cinfo^.Ss, cinfo^.Se,
cinfo^.Ah, cinfo^.Al);
{$ENDIF}
{ Prepare to scan data & restart markers }
cinfo^.marker^.next_restart_num := 0;
{ Count another SOS marker }
Inc( cinfo^.input_scan_number );
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
get_sos := TRUE;
end; { get_sos }
{METHODDEF}
function skip_variable (cinfo : j_decompress_ptr) : boolean; far;
{ Skip over an unknown or uninteresting variable-length marker }
var
length : INT32;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr; { Array[] of JOCTET; }
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
skip_variable := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := uint(GETJOCTET(next_input_byte^)) shl 8;
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
skip_variable := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET(next_input_byte^));
Inc( next_input_byte );
Dec(length, 2);
{$IFDEF DEBUG}
TRACEMS2(j_common_ptr(cinfo), 1, JTRC_MISC_MARKER,
cinfo^.unread_marker, int(length));
{$ENDIF}
{ Unload the local copies --- do this only at a restart boundary }
{ do before skip_input_data }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
if (length > 0) then
cinfo^.src^.skip_input_data(cinfo, long(length));
skip_variable := TRUE;
end; { skip_variable }
{$IFDEF D_ARITH_CODING_SUPPORTED}
{LOCAL}
function get_dac (cinfo : j_decompress_ptr) : boolean;
{ Process a DAC marker }
var
length : INT32;
index, val : int;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dac := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dac := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
Dec(length, 2);
while (length > 0) do
begin
{ Read a byte into variable index. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dac := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
index := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{ Read a byte into variable val. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dac := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
val := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
Dec( length, 2);
{$IFDEF DEBUG}
TRACEMS2(j_common_ptr(cinfo), 1, JTRC_DAC, index, val);
{$ENDIF}
if (index < 0) or (index >= (2*NUM_ARITH_TBLS)) then
ERREXIT1(j_common_ptr(cinfo) , JERR_DAC_INDEX, index);
if (index >= NUM_ARITH_TBLS) then
begin { define AC table }
cinfo^.arith_ac_K[index-NUM_ARITH_TBLS] := UINT8(val);
end
else
begin { define DC table }
cinfo^.arith_dc_L[index] := UINT8(val and $0F);
cinfo^.arith_dc_U[index] := UINT8(val shr 4);
if (cinfo^.arith_dc_L[index] > cinfo^.arith_dc_U[index]) then
ERREXIT1(j_common_ptr(cinfo) , JERR_DAC_VALUE, val);
end;
end;
if (length <> 0) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
get_dac := TRUE;
end; { get_dac }
{$ELSE}
{LOCAL}
function get_dac (cinfo : j_decompress_ptr) : boolean;
begin
get_dac := skip_variable(cinfo);
end;
{$ENDIF}
{LOCAL}
function get_dht (cinfo : j_decompress_ptr) : boolean;
{ Process a DHT marker }
var
length : INT32;
bits : Array[0..17-1] of UINT8;
huffval : Array[0..256-1] of UINT8;
i, index, count : int;
htblptr : ^JHUFF_TBL_PTR;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dht := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dht := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
Dec(length, 2);
while (length > 16) do
begin
{ Read a byte into variable index. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dht := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
index := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{$IFDEF DEBUG}
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_DHT, index);
{$ENDIF}
bits[0] := 0;
count := 0;
for i := 1 to 16 do
begin
{ Read a byte into variable bits[i]. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dht := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
bits[i] := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
Inc( count, bits[i] );
end;
Dec( length, (1 + 16) );
{$IFDEF DEBUG}
TRACEMS8(j_common_ptr(cinfo), 2, JTRC_HUFFBITS,
bits[1], bits[2], bits[3], bits[4],
bits[5], bits[6], bits[7], bits[8]);
TRACEMS8(j_common_ptr(cinfo), 2, JTRC_HUFFBITS,
bits[9], bits[10], bits[11], bits[12],
bits[13], bits[14], bits[15], bits[16]);
{$ENDIF}
{ Here we just do minimal validation of the counts to avoid walking
off the end of our table space. jdhuff.c will check more carefully. }
if (count > 256) or (INT32(count) > length) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_HUFF_TABLE);
for i := 0 to Pred(count) do
begin
{ Read a byte into variable huffval[i]. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dht := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
huffval[i] := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
end;
Dec( length, count );
if (index and $10)<>0 then
begin { AC table definition }
Dec( index, $10 );
htblptr := @cinfo^.ac_huff_tbl_ptrs[index];
end
else
begin { DC table definition }
htblptr := @cinfo^.dc_huff_tbl_ptrs[index];
end;
if (index < 0) or (index >= NUM_HUFF_TBLS) then
ERREXIT1(j_common_ptr(cinfo), JERR_DHT_INDEX, index);
if (htblptr^ = NIL) then
htblptr^ := jpeg_alloc_huff_table(j_common_ptr(cinfo));
MEMCOPY(@(htblptr^)^.bits, @bits, SIZEOF((htblptr^)^.bits));
MEMCOPY(@(htblptr^)^.huffval, @huffval, SIZEOF((htblptr^)^.huffval));
end;
if (length <> 0) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
get_dht := TRUE;
end; { get_dht }
{LOCAL}
function get_dqt (cinfo : j_decompress_ptr) : boolean;
{ Process a DQT marker }
var
length : INT32;
n, i, prec : int;
tmp : uint;
quant_ptr : JQUANT_TBL_PTR;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dqt := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dqt := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
Dec( length, 2 );
while (length > 0) do
begin
{ Read a byte into variable n. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dqt := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
n := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
prec := n shr 4;
n := n and $0F;
{$IFDEF DEBUG}
TRACEMS2(j_common_ptr(cinfo), 1, JTRC_DQT, n, prec);
{$ENDIF}
if (n >= NUM_QUANT_TBLS) then
ERREXIT1(j_common_ptr(cinfo) , JERR_DQT_INDEX, n);
if (cinfo^.quant_tbl_ptrs[n] = NIL) then
cinfo^.quant_tbl_ptrs[n] := jpeg_alloc_quant_table(j_common_ptr(cinfo));
quant_ptr := cinfo^.quant_tbl_ptrs[n];
for i := 0 to Pred(DCTSIZE2) do
begin
if (prec <> 0) then
begin
{ Read two bytes interpreted as an unsigned 16-bit integer.
tmp should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dqt := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
tmp := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dqt := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( tmp, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
end
else
begin
{ Read a byte into variable tmp. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dqt := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
tmp := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
end;
{ We convert the zigzag-order table to natural array order. }
quant_ptr^.quantval[jpeg_natural_order[i]] := UINT16(tmp);
end;
if (cinfo^.err^.trace_level >= 2) then
begin
i := 0;
while i < Pred(DCTSIZE2) do
begin
{$IFDEF DEBUG}
TRACEMS8(j_common_ptr(cinfo), 2, JTRC_QUANTVALS,
quant_ptr^.quantval[i], quant_ptr^.quantval[i+1],
quant_ptr^.quantval[i+2], quant_ptr^.quantval[i+3],
quant_ptr^.quantval[i+4], quant_ptr^.quantval[i+5],
quant_ptr^.quantval[i+6], quant_ptr^.quantval[i+7]);
{$ENDIF}
Inc(i, 8);
end;
end;
Dec( length, DCTSIZE2+1 );
if (prec <> 0) then
Dec( length, DCTSIZE2 );
end;
if (length <> 0) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
get_dqt := TRUE;
end; { get_dqt }
{LOCAL}
function get_dri (cinfo : j_decompress_ptr) : boolean;
{ Process a DRI marker }
var
length : INT32;
tmp : uint;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dri := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dri := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
if (length <> 4) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
{ Read two bytes interpreted as an unsigned 16-bit integer.
tmp should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dri := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
tmp := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_dri := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( tmp, GETJOCTET( next_input_byte^));
Inc( next_input_byte );
{$IFDEF DEBUG}
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_DRI, tmp);
{$ENDIF}
cinfo^.restart_interval := tmp;
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
get_dri := TRUE;
end; { get_dri }
{ Routines for processing APPn and COM markers.
These are either saved in memory or discarded, per application request.
APP0 and APP14 are specially checked to see if they are
JFIF and Adobe markers, respectively. }
const
APP0_DATA_LEN = 14; { Length of interesting data in APP0 }
APP14_DATA_LEN = 12; { Length of interesting data in APP14 }
APPN_DATA_LEN = 14; { Must be the largest of the above!! }
{LOCAL}
procedure examine_app0 (cinfo : j_decompress_ptr;
var data : array of JOCTET;
datalen : uint;
remaining : INT32);
{ Examine first few bytes from an APP0.
Take appropriate action if it is a JFIF marker.
datalen is # of bytes at data[], remaining is length of rest of marker data.
}
{$IFDEF DEBUG}
var
totallen : INT32;
{$ENDIF}
begin
{$IFDEF DEBUG}
totallen := INT32(datalen) + remaining;
{$ENDIF}
if (datalen >= APP0_DATA_LEN) and
(GETJOCTET(data[0]) = $4A) and
(GETJOCTET(data[1]) = $46) and
(GETJOCTET(data[2]) = $49) and
(GETJOCTET(data[3]) = $46) and
(GETJOCTET(data[4]) = 0) then
begin
{ Found JFIF APP0 marker: save info }
cinfo^.saw_JFIF_marker := TRUE;
cinfo^.JFIF_major_version := GETJOCTET(data[5]);
cinfo^.JFIF_minor_version := GETJOCTET(data[6]);
cinfo^.density_unit := GETJOCTET(data[7]);
cinfo^.X_density := (GETJOCTET(data[8]) shl 8) + GETJOCTET(data[9]);
cinfo^.Y_density := (GETJOCTET(data[10]) shl 8) + GETJOCTET(data[11]);
{ Check version.
Major version must be 1, anything else signals an incompatible change.
(We used to treat this as an error, but now it's a nonfatal warning,
because some bozo at Hijaak couldn't read the spec.)
Minor version should be 0..2, but process anyway if newer. }
if (cinfo^.JFIF_major_version <> 1) then
WARNMS2(j_common_ptr(cinfo), JWRN_JFIF_MAJOR,
cinfo^.JFIF_major_version, cinfo^.JFIF_minor_version);
{ Generate trace messages }
{$IFDEF DEBUG}
TRACEMS5(j_common_ptr(cinfo), 1, JTRC_JFIF,
cinfo^.JFIF_major_version, cinfo^.JFIF_minor_version,
cinfo^.X_density, cinfo^.Y_density, cinfo^.density_unit);
{ Validate thumbnail dimensions and issue appropriate messages }
if (GETJOCTET(data[12]) or GETJOCTET(data[13])) <> 0 then
TRACEMS2(j_common_ptr(cinfo), 1, JTRC_JFIF_THUMBNAIL,
GETJOCTET(data[12]), GETJOCTET(data[13]));
Dec(totallen, APP0_DATA_LEN);
if (totallen <>
( INT32(GETJOCTET(data[12])) * INT32(GETJOCTET(data[13])) * INT32(3) )) then
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_JFIF_BADTHUMBNAILSIZE, int(totallen));
{$ENDIF}
end
else
if (datalen >= 6) and
(GETJOCTET(data[0]) = $4A) and
(GETJOCTET(data[1]) = $46) and
(GETJOCTET(data[2]) = $58) and
(GETJOCTET(data[3]) = $58) and
(GETJOCTET(data[4]) = 0) then
begin
{ Found JFIF "JFXX" extension APP0 marker }
{ The library doesn't actually do anything with these,
but we try to produce a helpful trace message. }
{$IFDEF DEBUG}
case (GETJOCTET(data[5])) of
$10:
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_THUMB_JPEG, int(totallen));
$11:
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_THUMB_PALETTE, int(totallen));
$13:
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_THUMB_RGB, int(totallen));
else
TRACEMS2(j_common_ptr(cinfo), 1, JTRC_JFIF_EXTENSION,
GETJOCTET(data[5]), int(totallen));
end;
{$ENDIF}
end
else
begin
{ Start of APP0 does not match "JFIF" or "JFXX", or too short }
{$IFDEF DEBUG}
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_APP0, int(totallen));
{$ENDIF}
end;
end;
{LOCAL}
procedure examine_app14 (cinfo : j_decompress_ptr;
var data : array of JOCTET;
datalen : uint;
remaining : INT32);
{ Examine first few bytes from an APP14.
Take appropriate action if it is an Adobe marker.
datalen is # of bytes at data[], remaining is length of rest of marker data.
}
var
{$IFDEF DEBUG}
version, flags0, flags1,
{$ENDIF}
transform : uint;
begin
if (datalen >= APP14_DATA_LEN) and
(GETJOCTET(data[0]) = $41) and
(GETJOCTET(data[1]) = $64) and
(GETJOCTET(data[2]) = $6F) and
(GETJOCTET(data[3]) = $62) and
(GETJOCTET(data[4]) = $65) then
begin
{ Found Adobe APP14 marker }
{$IFDEF DEBUG}
version := (GETJOCTET(data[5]) shl 8) + GETJOCTET(data[6]);
flags0 := (GETJOCTET(data[7]) shl 8) + GETJOCTET(data[8]);
flags1 := (GETJOCTET(data[9]) shl 8) + GETJOCTET(data[10]);
{$ENDIF}
transform := GETJOCTET(data[11]);
{$IFDEF DEBUG}
TRACEMS4(j_common_ptr(cinfo), 1, JTRC_ADOBE, version, flags0, flags1, transform);
{$ENDIF}
cinfo^.saw_Adobe_marker := TRUE;
cinfo^.Adobe_transform := UINT8 (transform);
end
else
begin
{ Start of APP14 does not match "Adobe", or too short }
{$IFDEF DEBUG}
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_APP14, int (datalen + remaining));
{$ENDIF}
end;
end;
{METHODDEF}
function get_interesting_appn (cinfo : j_decompress_ptr) : boolean; far;
{ Process an APP0 or APP14 marker without saving it }
var
length : INT32;
b : array[0..APPN_DATA_LEN-1] of JOCTET;
i, numtoread : uint;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{ Read two bytes interpreted as an unsigned 16-bit integer.
length should be declared unsigned int or perhaps INT32. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_interesting_appn := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_interesting_appn := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET(next_input_byte^));
Inc( next_input_byte );
Dec(length, 2);
{ get the interesting part of the marker data }
if (length >= APPN_DATA_LEN) then
numtoread := APPN_DATA_LEN
else
if (length > 0) then
numtoread := uint(length)
else
numtoread := 0;
if numtoread > 0 then
begin
for i := 0 to numtoread-1 do
begin
{ Read a byte into b[i]. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
get_interesting_appn := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
b[i] := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
end;
end;
Dec(length, numtoread);
{ process it }
case (cinfo^.unread_marker) of
M_APP0:
examine_app0(cinfo, b, numtoread, length);
M_APP14:
examine_app14(cinfo, b, numtoread, length);
else
{ can't get here unless jpeg_save_markers chooses wrong processor }
ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, cinfo^.unread_marker);
end;
{ skip any remaining data -- could be lots }
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
if (length > 0) then
cinfo^.src^.skip_input_data(cinfo, long(length));
get_interesting_appn := TRUE;
end;
{$ifdef SAVE_MARKERS_SUPPORTED}
{METHODDEF}
function save_marker (cinfo : j_decompress_ptr) : boolean; far;
{ Save an APPn or COM marker into the marker list }
var
marker : my_marker_ptr;
cur_marker : jpeg_saved_marker_ptr;
bytes_read, data_length : uint;
data : JOCTET_FIELD_PTR;
length : INT32;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
var
limit : uint;
var
prev : jpeg_saved_marker_ptr;
begin
{ local copies of input pointer/count }
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
marker := my_marker_ptr(cinfo^.marker);
cur_marker := marker^.cur_marker;
length := 0;
if (cur_marker = NIL) then
begin
{ begin reading a marker }
{ Read two bytes interpreted as an unsigned 16-bit integer. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
save_marker := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
length := (uint( GETJOCTET(next_input_byte^)) shl 8);
Inc( next_input_byte );
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
save_marker := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
Inc( length, GETJOCTET(next_input_byte^));
Inc( next_input_byte );
Dec(length, 2);
if (length >= 0) then
begin { watch out for bogus length word }
{ figure out how much we want to save }
if (cinfo^.unread_marker = int(M_COM)) then
limit := marker^.length_limit_COM
else
limit := marker^.length_limit_APPn[cinfo^.unread_marker - int(M_APP0)];
if (uint(length) < limit) then
limit := uint(length);
{ allocate and initialize the marker item }
cur_marker := jpeg_saved_marker_ptr(
cinfo^.mem^.alloc_large (j_common_ptr(cinfo), JPOOL_IMAGE,
SIZEOF(jpeg_marker_struct) + limit) );
cur_marker^.next := NIL;
cur_marker^.marker := UINT8 (cinfo^.unread_marker);
cur_marker^.original_length := uint(length);
cur_marker^.data_length := limit;
{ data area is just beyond the jpeg_marker_struct }
cur_marker^.data := JOCTET_FIELD_PTR(cur_marker);
Inc(jpeg_saved_marker_ptr(cur_marker^.data));
data := cur_marker^.data;
marker^.cur_marker := cur_marker;
marker^.bytes_read := 0;
bytes_read := 0;
data_length := limit;
end
else
begin
{ deal with bogus length word }
data_length := 0;
bytes_read := 0;
data := NIL;
end
end
else
begin
{ resume reading a marker }
bytes_read := marker^.bytes_read;
data_length := cur_marker^.data_length;
data := cur_marker^.data;
Inc(data, bytes_read);
end;
while (bytes_read < data_length) do
begin
{ move the restart point to here }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
marker^.bytes_read := bytes_read;
{ If there's not at least one byte in buffer, suspend }
if (bytes_in_buffer = 0) then
begin
if not datasrc^.fill_input_buffer (cinfo) then
begin
save_marker := FALSE;
exit;
end;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
{ Copy bytes with reasonable rapidity }
while (bytes_read < data_length) and (bytes_in_buffer > 0) do
begin
JOCTETPTR(data)^ := next_input_byte^;
Inc(JOCTETPTR(data));
Inc(next_input_byte);
Dec(bytes_in_buffer);
Inc(bytes_read);
end;
end;
{ Done reading what we want to read }
if (cur_marker <> NIL) then
begin { will be NIL if bogus length word }
{ Add new marker to end of list }
if (cinfo^.marker_list = NIL) then
begin
cinfo^.marker_list := cur_marker
end
else
begin
prev := cinfo^.marker_list;
while (prev^.next <> NIL) do
prev := prev^.next;
prev^.next := cur_marker;
end;
{ Reset pointer & calc remaining data length }
data := cur_marker^.data;
length := cur_marker^.original_length - data_length;
end;
{ Reset to initial state for next marker }
marker^.cur_marker := NIL;
{ Process the marker if interesting; else just make a generic trace msg }
case (cinfo^.unread_marker) of
M_APP0:
examine_app0(cinfo, data^, data_length, length);
M_APP14:
examine_app14(cinfo, data^, data_length, length);
else
{$IFDEF DEBUG}
TRACEMS2(j_common_ptr(cinfo), 1, JTRC_MISC_MARKER, cinfo^.unread_marker,
int(data_length + length));
{$ENDIF}
end;
{ skip any remaining data -- could be lots }
{ do before skip_input_data }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
if (length > 0) then
cinfo^.src^.skip_input_data (cinfo, long(length) );
save_marker := TRUE;
end;
{$endif} { SAVE_MARKERS_SUPPORTED }
{ Find the next JPEG marker, save it in cinfo^.unread_marker.
Returns FALSE if had to suspend before reaching a marker;
in that case cinfo^.unread_marker is unchanged.
Note that the result might not be a valid marker code,
but it will never be 0 or FF. }
{LOCAL}
function next_marker (cinfo : j_decompress_ptr) : boolean;
var
c : int;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{while TRUE do}
repeat
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
next_marker := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{ Skip any non-FF bytes.
This may look a bit inefficient, but it will not occur in a valid file.
We sync after each discarded byte so that a suspending data source
can discard the byte from its buffer. }
while (c <> $FF) do
begin
Inc(cinfo^.marker^.discarded_bytes);
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
next_marker := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
end;
{ This loop swallows any duplicate FF bytes. Extra FFs are legal as
pad bytes, so don't count them in discarded_bytes. We assume there
will not be so many consecutive FF bytes as to overflow a suspending
data source's input buffer. }
repeat
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
next_marker := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
Until (c <> $FF);
if (c <> 0) then
break; { found a valid marker, exit loop }
{ Reach here if we found a stuffed-zero data sequence (FF/00).
Discard it and loop back to try again. }
Inc(cinfo^.marker^.discarded_bytes, 2);
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
Until False;
if (cinfo^.marker^.discarded_bytes <> 0) then
begin
WARNMS2(j_common_ptr(cinfo), JWRN_EXTRANEOUS_DATA,
cinfo^.marker^.discarded_bytes, c);
cinfo^.marker^.discarded_bytes := 0;
end;
cinfo^.unread_marker := c;
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
next_marker := TRUE;
end; { next_marker }
{LOCAL}
function first_marker (cinfo : j_decompress_ptr) : boolean;
{ Like next_marker, but used to obtain the initial SOI marker. }
{ For this marker, we do not allow preceding garbage or fill; otherwise,
we might well scan an entire input file before realizing it ain't JPEG.
If an application wants to process non-JFIF files, it must seek to the
SOI before calling the JPEG library. }
var
c, c2 : int;
var
datasrc : jpeg_source_mgr_ptr;
next_input_byte : JOCTETptr;
bytes_in_buffer : size_t;
begin
datasrc := cinfo^.src;
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
{ Read a byte into variable c. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
first_marker := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
{ Read a byte into variable c2. If must suspend, return FALSE. }
{ make a byte available.
Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
but we must reload the local copies after a successful fill. }
if (bytes_in_buffer = 0) then
begin
if (not datasrc^.fill_input_buffer(cinfo)) then
begin
first_marker := FALSE;
exit;
end;
{ Reload the local copies }
next_input_byte := datasrc^.next_input_byte;
bytes_in_buffer := datasrc^.bytes_in_buffer;
end;
Dec( bytes_in_buffer );
c2 := GETJOCTET(next_input_byte^);
Inc(next_input_byte);
if (c <> $FF) or (c2 <> int(M_SOI)) then
ERREXIT2(j_common_ptr(cinfo), JERR_NO_SOI, c, c2);
cinfo^.unread_marker := c2;
{ Unload the local copies --- do this only at a restart boundary }
datasrc^.next_input_byte := next_input_byte;
datasrc^.bytes_in_buffer := bytes_in_buffer;
first_marker := TRUE;
end; { first_marker }
{ Read markers until SOS or EOI.
Returns same codes as are defined for jpeg_consume_input:
JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. }
{METHODDEF}
function read_markers (cinfo : j_decompress_ptr) : int; far;
begin
{ Outer loop repeats once for each marker. }
repeat
{ Collect the marker proper, unless we already did. }
{ NB: first_marker() enforces the requirement that SOI appear first. }
if (cinfo^.unread_marker = 0) then
begin
if not cinfo^.marker^.saw_SOI then
begin
if not first_marker(cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
end
else
begin
if not next_marker(cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
end;
end;
{ At this point cinfo^.unread_marker contains the marker code and the
input point is just past the marker proper, but before any parameters.
A suspension will cause us to return with this state still true. }
case (cinfo^.unread_marker) of
M_SOI:
if not get_soi(cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
M_SOF0, { Baseline }
M_SOF1: { Extended sequential, Huffman }
if not get_sof(cinfo, FALSE, FALSE) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
M_SOF2: { Progressive, Huffman }
if not get_sof(cinfo, TRUE, FALSE) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
M_SOF9: { Extended sequential, arithmetic }
if not get_sof(cinfo, FALSE, TRUE) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
M_SOF10: { Progressive, arithmetic }
if not get_sof(cinfo, TRUE, TRUE) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
{ Currently unsupported SOFn types }
M_SOF3, { Lossless, Huffman }
M_SOF5, { Differential sequential, Huffman }
M_SOF6, { Differential progressive, Huffman }
M_SOF7, { Differential lossless, Huffman }
M_JPG, { Reserved for JPEG extensions }
M_SOF11, { Lossless, arithmetic }
M_SOF13, { Differential sequential, arithmetic }
M_SOF14, { Differential progressive, arithmetic }
M_SOF15: { Differential lossless, arithmetic }
ERREXIT1(j_common_ptr(cinfo), JERR_SOF_UNSUPPORTED, cinfo^.unread_marker);
M_SOS:
begin
if not get_sos(cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
cinfo^.unread_marker := 0; { processed the marker }
read_markers := JPEG_REACHED_SOS;
exit;
end;
M_EOI:
begin
{$IFDEF DEBUG}
TRACEMS(j_common_ptr(cinfo), 1, JTRC_EOI);
{$ENDIF}
cinfo^.unread_marker := 0; { processed the marker }
read_markers := JPEG_REACHED_EOI;
exit;
end;
M_DAC:
if not get_dac(cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
M_DHT:
if not get_dht(cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
M_DQT:
if not get_dqt(cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
M_DRI:
if not get_dri(cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
M_APP0,
M_APP1,
M_APP2,
M_APP3,
M_APP4,
M_APP5,
M_APP6,
M_APP7,
M_APP8,
M_APP9,
M_APP10,
M_APP11,
M_APP12,
M_APP13,
M_APP14,
M_APP15:
if not my_marker_ptr(cinfo^.marker)^.
process_APPn[cinfo^.unread_marker - int(M_APP0)](cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
M_COM:
if not my_marker_ptr(cinfo^.marker)^.process_COM (cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
M_RST0, { these are all parameterless }
M_RST1,
M_RST2,
M_RST3,
M_RST4,
M_RST5,
M_RST6,
M_RST7,
M_TEM:
{$IFDEF DEBUG}
TRACEMS1(j_common_ptr(cinfo), 1, JTRC_PARMLESS_MARKER,
cinfo^.unread_marker)
{$ENDIF}
;
M_DNL: { Ignore DNL ... perhaps the wrong thing }
if not skip_variable(cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
else { must be DHP, EXP, JPGn, or RESn }
if (@on_unknown_marker<>nil) then
begin
read_markers:=on_unknown_marker(cinfo);
exit;
end
else if not skip_variable(cinfo) then
begin
read_markers := JPEG_SUSPENDED;
exit;
end;
{ // This is the previous code.
ERREXIT1(j_common_ptr(cinfo) , JERR_UNKNOWN_MARKER,cinfo^.unread_marker);
}
end; { end of case }
{ Successfully processed marker, so reset state variable }
cinfo^.unread_marker := 0;
Until false;
end; { read_markers }
{ Read a restart marker, which is expected to appear next in the datastream;
if the marker is not there, take appropriate recovery action.
Returns FALSE if suspension is required.
This is called by the entropy decoder after it has read an appropriate
number of MCUs. cinfo^.unread_marker may be nonzero if the entropy decoder
has already read a marker from the data source. Under normal conditions
cinfo^.unread_marker will be reset to 0 before returning; if not reset,
it holds a marker which the decoder will be unable to read past. }
{METHODDEF}
function read_restart_marker (cinfo : j_decompress_ptr) :boolean; far;
begin
{ Obtain a marker unless we already did. }
{ Note that next_marker will complain if it skips any data. }
if (cinfo^.unread_marker = 0) then
begin
if not next_marker(cinfo) then
begin
read_restart_marker := FALSE;
exit;
end;
end;
if (cinfo^.unread_marker = (int(M_RST0) + cinfo^.marker^.next_restart_num)) then
begin
{ Normal case --- swallow the marker and let entropy decoder continue }
{$IFDEF DEBUG}
TRACEMS1(j_common_ptr(cinfo), 3, JTRC_RST,
cinfo^.marker^.next_restart_num);
{$ENDIF}
cinfo^.unread_marker := 0;
end
else
begin
{ Uh-oh, the restart markers have been messed up. }
{ Let the data source manager determine how to resync. }
if not cinfo^.src^.resync_to_restart(cinfo,
cinfo^.marker^.next_restart_num) then
begin
read_restart_marker := FALSE;
exit;
end;
end;
{ Update next-restart state }
with cinfo^.marker^ do
next_restart_num := (next_restart_num + 1) and 7;
read_restart_marker := TRUE;
end; { read_restart_marker }
{ This is the default resync_to_restart method for data source managers
to use if they don't have any better approach. Some data source managers
may be able to back up, or may have additional knowledge about the data
which permits a more intelligent recovery strategy; such managers would
presumably supply their own resync method.
read_restart_marker calls resync_to_restart if it finds a marker other than
the restart marker it was expecting. (This code is *not* used unless
a nonzero restart interval has been declared.) cinfo^.unread_marker is
the marker code actually found (might be anything, except 0 or FF).
The desired restart marker number (0..7) is passed as a parameter.
This routine is supposed to apply whatever error recovery strategy seems
appropriate in order to position the input stream to the next data segment.
Note that cinfo^.unread_marker is treated as a marker appearing before
the current data-source input point; usually it should be reset to zero
before returning.
Returns FALSE if suspension is required.
This implementation is substantially constrained by wanting to treat the
input as a data stream; this means we can't back up. Therefore, we have
only the following actions to work with:
1. Simply discard the marker and let the entropy decoder resume at next
byte of file.
2. Read forward until we find another marker, discarding intervening
data. (In theory we could look ahead within the current bufferload,
without having to discard data if we don't find the desired marker.
This idea is not implemented here, in part because it makes behavior
dependent on buffer size and chance buffer-boundary positions.)
3. Leave the marker unread (by failing to zero cinfo^.unread_marker).
This will cause the entropy decoder to process an empty data segment,
inserting dummy zeroes, and then we will reprocess the marker.
#2 is appropriate if we think the desired marker lies ahead, while #3 is
appropriate if the found marker is a future restart marker (indicating
that we have missed the desired restart marker, probably because it got
corrupted).
We apply #2 or #3 if the found marker is a restart marker no more than
two counts behind or ahead of the expected one. We also apply #2 if the
found marker is not a legal JPEG marker code (it's certainly bogus data).
If the found marker is a restart marker more than 2 counts away, we do #1
(too much risk that the marker is erroneous; with luck we will be able to
resync at some future point).
For any valid non-restart JPEG marker, we apply #3. This keeps us from
overrunning the end of a scan. An implementation limited to single-scan
files might find it better to apply #2 for markers other than EOI, since
any other marker would have to be bogus data in that case. }
{GLOBAL}
function jpeg_resync_to_restart(cinfo : j_decompress_ptr;
desired : int) : boolean;
var
marker : int;
action : int;
begin
marker := cinfo^.unread_marker;
action := 1; { never used }
{ Always put up a warning. }
WARNMS2(j_common_ptr(cinfo), JWRN_MUST_RESYNC, marker, desired);
{ Outer loop handles repeated decision after scanning forward. }
repeat
if (marker < int(M_SOF0)) then
action := 2 { invalid marker }
else
if (marker < int(M_RST0)) or (marker > int(M_RST7)) then
action := 3 { valid non-restart marker }
else
begin
if (marker = (int(M_RST0) + ((desired+1) and 7))) or
(marker = (int(M_RST0) + ((desired+2) and 7))) then
action := 3 { one of the next two expected restarts }
else
if (marker = (int(M_RST0) + ((desired-1) and 7))) or
(marker = (int(M_RST0) + ((desired-2) and 7))) then
action := 2 { a prior restart, so advance }
else
action := 1; { desired restart or too far away }
end;
{$IFDEF DEBUG}
TRACEMS2(j_common_ptr(cinfo), 4, JTRC_RECOVERY_ACTION, marker, action);
{$ENDIF}
case action of
1:
{ Discard marker and let entropy decoder resume processing. }
begin
cinfo^.unread_marker := 0;
jpeg_resync_to_restart := TRUE;
exit;
end;
2:
{ Scan to the next marker, and repeat the decision loop. }
begin
if not next_marker(cinfo) then
begin
jpeg_resync_to_restart := FALSE;
exit;
end;
marker := cinfo^.unread_marker;
end;
3:
{ Return without advancing past this marker. }
{ Entropy decoder will be forced to process an empty segment. }
begin
jpeg_resync_to_restart := TRUE;
exit;
end;
end; { case }
Until false; { end loop }
end; { jpeg_resync_to_restart }
{ Reset marker processing state to begin a fresh datastream. }
{METHODDEF}
procedure reset_marker_reader (cinfo : j_decompress_ptr); far;
var
marker : my_marker_ptr;
begin
marker := my_marker_ptr (cinfo^.marker);
with cinfo^ do
begin
comp_info := NIL; { until allocated by get_sof }
input_scan_number := 0; { no SOS seen yet }
unread_marker := 0; { no pending marker }
end;
marker^.pub.saw_SOI := FALSE; { set internal state too }
marker^.pub.saw_SOF := FALSE;
marker^.pub.discarded_bytes := 0;
marker^.cur_marker := NIL;
end; { reset_marker_reader }
{ Initialize the marker reader module.
This is called only once, when the decompression object is created. }
{GLOBAL}
procedure jinit_marker_reader (cinfo : j_decompress_ptr);
var
marker : my_marker_ptr;
i : int;
begin
{ Create subobject in permanent pool }
marker := my_marker_ptr(
cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_PERMANENT,
SIZEOF(my_marker_reader))
);
cinfo^.marker := jpeg_marker_reader_ptr(marker);
{ Initialize method pointers }
marker^.pub.reset_marker_reader := reset_marker_reader;
marker^.pub.read_markers := read_markers;
marker^.pub.read_restart_marker := read_restart_marker;
{ Initialize COM/APPn processing.
By default, we examine and then discard APP0 and APP14,
but simply discard COM and all other APPn. }
marker^.process_COM := skip_variable;
marker^.length_limit_COM := 0;
for i := 0 to 16-1 do
begin
marker^.process_APPn[i] := skip_variable;
marker^.length_limit_APPn[i] := 0;
end;
marker^.process_APPn[0] := get_interesting_appn;
marker^.process_APPn[14] := get_interesting_appn;
{ Reset marker processing state }
reset_marker_reader(cinfo);
end; { jinit_marker_reader }
{ Control saving of COM and APPn markers into marker_list. }
{$ifdef SAVE_MARKERS_SUPPORTED}
{GLOBAL}
procedure jpeg_save_markers (cinfo : j_decompress_ptr;
marker_code : int;
length_limit : uint);
var
marker : my_marker_ptr;
maxlength : long;
processor : jpeg_marker_parser_method;
begin
marker := my_marker_ptr (cinfo^.marker);
{ Length limit mustn't be larger than what we can allocate
(should only be a concern in a 16-bit environment). }
maxlength := cinfo^.mem^.max_alloc_chunk - SIZEOF(jpeg_marker_struct);
if (long(length_limit) > maxlength) then
length_limit := uint(maxlength);
{ Choose processor routine to use.
APP0/APP14 have special requirements. }
if (length_limit <> 0) then
begin
processor := save_marker;
{ If saving APP0/APP14, save at least enough for our internal use. }
if (marker_code = int(M_APP0)) and (length_limit < APP0_DATA_LEN) then
length_limit := APP0_DATA_LEN
else
if (marker_code = int(M_APP14)) and (length_limit < APP14_DATA_LEN) then
length_limit := APP14_DATA_LEN;
end
else
begin
processor := skip_variable;
{ If discarding APP0/APP14, use our regular on-the-fly processor. }
if (marker_code = int(M_APP0)) or (marker_code = int(M_APP14)) then
processor := get_interesting_appn;
end;
if (marker_code = int(M_COM)) then
begin
marker^.process_COM := processor;
marker^.length_limit_COM := length_limit;
end
else
if (marker_code >= int(M_APP0)) and (marker_code <= int(M_APP15)) then
begin
marker^.process_APPn[marker_code - int(M_APP0)] := processor;
marker^.length_limit_APPn[marker_code - int(M_APP0)] := length_limit;
end
else
ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, marker_code);
end;
{$endif} { SAVE_MARKERS_SUPPORTED }
{ Install a special processing method for COM or APPn markers. }
{GLOBAL}
procedure jpeg_set_marker_processor (cinfo : j_decompress_ptr;
marker_code : int;
routine : jpeg_marker_parser_method);
var
marker : my_marker_ptr;
begin
marker := my_marker_ptr (cinfo^.marker);
if (marker_code = int(M_COM)) then
marker^.process_COM := routine
else
if (marker_code >= int(M_APP0)) and (marker_code <= int(M_APP15)) then
marker^.process_APPn[marker_code - int(M_APP0)] := routine
else
ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, marker_code);
end;
end.
|