c2202c2e0a44009a5bddea3e2d5d901cb38cac81
3 { This file contains routines to decode JPEG datastream markers.
4 Most of the complexity arises from our desire to support input
5 suspension: if not all of the data for a marker is available;
6 we must exit back to the application. On resumption; we reprocess
7 the marker. }
9 { Original: jdmarker.c; Copyright (C) 1991-1998; Thomas G. Lane. }
10 { History
11 9.7.96 Conversion to pascal started jnn
12 22.3.98 updated to 6b jnn }
15 interface
17 {$I imjconfig.inc}
19 uses
20 imjmorecfg,
21 imjinclude,
22 imjdeferr,
23 imjerror,
24 imjcomapi,
25 imjpeglib;
93 type
96 { Private state }
98 type
103 { Application-overridable marker processing methods }
107 { Limit on marker data length to save for each marker type }
111 { Status of COM/APPn marker saving }
114 { Note: cur_marker is not linked into marker_list until it's all read. }
117 {GLOBAL}
120 {GLOBAL}
123 {$ifdef SAVE_MARKERS_SUPPORTED}
125 {GLOBAL}
129 {$ENDIF}
131 {GLOBAL}
136 implementation
138 uses
139 imjutils;
141 { At all times, cinfo1.src.next_input_byte and .bytes_in_buffer reflect
142 the current restart point; we update them only when we have reached a
143 suitable place to restart if a suspension occurs. }
146 { Routines to process JPEG markers.
148 Entry condition: JPEG marker itself has been read and its code saved
149 in cinfo^.unread_marker; input restart point is just after the marker.
151 Exit: if return TRUE, have read and processed any parameters, and have
152 updated the restart point to point after the parameters.
153 If return FALSE, was forced to suspend before reaching end of
154 marker parameters; restart point has not been moved. Same routine
155 will be called again after application supplies more input data.
157 This approach to suspension assumes that all of a marker's parameters
158 can fit into a single input bufferload. This should hold for "normal"
159 markers. Some COM/APPn markers might have large parameter segments
160 that might not fit. If we are simply dropping such a marker, we use
161 skip_input_data to get past it, and thereby put the problem on the
162 source manager's shoulders. If we are saving the marker's contents
163 into memory, we use a slightly different convention: when forced to
164 suspend, the marker processor updates the restart point to the end of
165 what it's consumed (ie, the end of the buffer) before returning FALSE.
166 On resumption, cinfo->unread_marker still contains the marker code,
167 but the data source will point to the next chunk of marker data.
168 The marker processor must retain internal state to deal with this.
170 Note that we don't bother to avoid duplicate trace messages if a
171 suspension occurs within marker parameters. Other side effects
172 require more care. }
174 {LOCAL}
176 { Process an SOI marker }
177 var
179 begin
180 {$IFDEF DEBUG}
182 {$ENDIF}
187 { Reset all parameters that are defined to be reset by SOI }
191 begin
198 { Set initial assumptions for colorspace etc }
201 begin
220 {LOCAL}
224 { Process a SOFn marker }
225 var
229 { Declare and initialize local copies of input pointer/count }
230 var
234 begin
238 {}
242 { Read two bytes interpreted as an unsigned 16-bit integer.
243 length should be declared unsigned int or perhaps INT32. }
245 { make a byte available.
246 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
247 but we must reload the local copies after a successful fill. }
249 begin
251 begin
253 exit;
255 { Reload the local copies }
263 { make a byte available.
264 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
265 but we must reload the local copies after a successful fill. }
267 begin
269 begin
271 exit;
273 { Reload the local copies }
283 { Read a byte into variable cinfo^.data_precision.
284 If must suspend, return FALSE. }
285 { make a byte available.
286 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
287 but we must reload the local copies after a successful fill. }
289 begin
291 begin
293 exit;
295 { Reload the local copies }
304 { Read two bytes interpreted as an unsigned 16-bit integer.
305 cinfo^.image_height should be declared unsigned int or perhaps INT32. }
307 { make a byte available.
308 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
309 but we must reload the local copies after a successful fill. }
311 begin
313 begin
315 exit;
317 { Reload the local copies }
325 { make a byte available.
326 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
327 but we must reload the local copies after a successful fill. }
329 begin
331 begin
333 exit;
335 { Reload the local copies }
344 { Read two bytes interpreted as an unsigned 16-bit integer.
345 cinfo^.image_width should be declared unsigned int or perhaps INT32. }
347 { make a byte available.
348 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
349 but we must reload the local copies after a successful fill. }
351 begin
353 begin
355 exit;
357 { Reload the local copies }
365 { make a byte available.
366 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
367 but we must reload the local copies after a successful fill. }
369 begin
371 begin
373 exit;
375 { Reload the local copies }
384 { Read a byte into variable cinfo^.num_components.
385 If must suspend, return FALSE. }
386 { make a byte available.
387 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
388 but we must reload the local copies after a successful fill. }
390 begin
392 begin
394 exit;
396 { Reload the local copies }
407 {$IFDEF DEBUG}
411 {$ENDIF}
416 { We don't support files in which the image height is initially specified }
417 { as 0 and is later redefined by DNL. As long as we have to check that, }
418 { might as well have a general sanity check. }
433 begin
436 { Read a byte into variable compptr^.component_id.
437 If must suspend, return FALSE. }
438 { make a byte available.
439 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
440 but we must reload the local copies after a successful fill. }
442 begin
444 begin
446 exit;
448 { Reload the local copies }
457 { Read a byte into variable c. If must suspend, return FALSE. }
458 { make a byte available.
459 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
460 but we must reload the local copies after a successful fill. }
462 begin
464 begin
466 exit;
468 { Reload the local copies }
480 { Read a byte into variable compptr^.quant_tbl_no.
481 If must suspend, return FALSE. }
482 { make a byte available.
483 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
484 but we must reload the local copies after a successful fill. }
486 begin
488 begin
490 exit;
492 { Reload the local copies }
501 {$IFDEF DEBUG}
505 {$ENDIF}
512 { Unload the local copies --- do this only at a restart boundary }
520 {LOCAL}
522 { Process a SOS marker }
523 label
524 id_found;
525 var
529 { Declare and initialize local copies of input pointer/count }
530 var
534 begin
539 {}
544 { Read two bytes interpreted as an unsigned 16-bit integer.
545 length should be declared unsigned int or perhaps INT32. }
547 { make a byte available.
548 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
549 but we must reload the local copies after a successful fill. }
551 begin
553 begin
555 exit;
557 { Reload the local copies }
565 { make a byte available.
566 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
567 but we must reload the local copies after a successful fill. }
569 begin
571 begin
573 exit;
575 { Reload the local copies }
585 { Read a byte into variable n (Number of components).
586 If must suspend, return FALSE. }
587 { make a byte available.
588 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
589 but we must reload the local copies after a successful fill. }
591 begin
593 begin
595 exit;
597 { Reload the local copies }
606 {$IFDEF DEBUG}
608 {$ENDIF}
615 { Collect the component-spec parameters }
618 begin
619 { Read a byte into variable cc. If must suspend, return FALSE. }
620 { make a byte available.
621 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
622 but we must reload the local copies after a successful fill. }
624 begin
626 begin
628 exit;
630 { Reload the local copies }
639 { Read a byte into variable c. If must suspend, return FALSE. }
640 { make a byte available.
641 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
642 but we must reload the local copies after a successful fill. }
644 begin
646 begin
648 exit;
650 { Reload the local copies }
661 begin
669 id_found:
675 {$IFDEF DEBUG}
678 {$ENDIF}
681 { Collect the additional scan parameters Ss, Se, Ah/Al. }
682 { Read a byte into variable c. If must suspend, return FALSE. }
683 { make a byte available.
684 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
685 but we must reload the local copies after a successful fill. }
687 begin
689 begin
691 exit;
693 { Reload the local copies }
704 { Read a byte into variable c. If must suspend, return FALSE. }
705 { make a byte available.
706 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
707 but we must reload the local copies after a successful fill. }
709 begin
711 begin
713 exit;
715 { Reload the local copies }
726 { Read a byte into variable c. If must suspend, return FALSE. }
727 { make a byte available.
728 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
729 but we must reload the local copies after a successful fill. }
731 begin
733 begin
735 exit;
737 { Reload the local copies }
749 {$IFDEF DEBUG}
752 {$ENDIF}
754 { Prepare to scan data & restart markers }
757 { Count another SOS marker }
760 { Unload the local copies --- do this only at a restart boundary }
768 {METHODDEF}
770 { Skip over an unknown or uninteresting variable-length marker }
771 var
773 var
777 begin
782 { Read two bytes interpreted as an unsigned 16-bit integer.
783 length should be declared unsigned int or perhaps INT32. }
785 { make a byte available.
786 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
787 but we must reload the local copies after a successful fill. }
789 begin
791 begin
793 exit;
795 { Reload the local copies }
803 { make a byte available.
804 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
805 but we must reload the local copies after a successful fill. }
807 begin
809 begin
811 exit;
813 { Reload the local copies }
824 {$IFDEF DEBUG}
827 {$ENDIF}
829 { Unload the local copies --- do this only at a restart boundary }
830 { do before skip_input_data }
841 {$IFDEF D_ARITH_CODING_SUPPORTED}
843 {LOCAL}
845 { Process a DAC marker }
846 var
849 var
853 begin
858 { Read two bytes interpreted as an unsigned 16-bit integer.
859 length should be declared unsigned int or perhaps INT32. }
861 { make a byte available.
862 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
863 but we must reload the local copies after a successful fill. }
865 begin
867 begin
869 exit;
871 { Reload the local copies }
879 { make a byte available.
880 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
881 but we must reload the local copies after a successful fill. }
883 begin
885 begin
887 exit;
889 { Reload the local copies }
901 begin
902 { Read a byte into variable index. If must suspend, return FALSE. }
903 { make a byte available.
904 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
905 but we must reload the local copies after a successful fill. }
907 begin
909 begin
911 exit;
913 { Reload the local copies }
922 { Read a byte into variable val. If must suspend, return FALSE. }
923 { make a byte available.
924 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
925 but we must reload the local copies after a successful fill. }
927 begin
929 begin
931 exit;
933 { Reload the local copies }
944 {$IFDEF DEBUG}
946 {$ENDIF}
954 end
955 else
967 { Unload the local copies --- do this only at a restart boundary }
974 {$ELSE}
976 {LOCAL}
978 begin
982 {$ENDIF}
984 {LOCAL}
986 { Process a DHT marker }
987 var
993 var
997 begin
1002 { Read two bytes interpreted as an unsigned 16-bit integer.
1003 length should be declared unsigned int or perhaps INT32. }
1005 { make a byte available.
1006 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1007 but we must reload the local copies after a successful fill. }
1009 begin
1011 begin
1013 exit;
1015 { Reload the local copies }
1023 { make a byte available.
1024 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1025 but we must reload the local copies after a successful fill. }
1027 begin
1029 begin
1031 exit;
1033 { Reload the local copies }
1045 begin
1046 { Read a byte into variable index. If must suspend, return FALSE. }
1047 { make a byte available.
1048 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1049 but we must reload the local copies after a successful fill. }
1051 begin
1053 begin
1055 exit;
1057 { Reload the local copies }
1066 {$IFDEF DEBUG}
1068 {$ENDIF}
1073 begin
1074 { Read a byte into variable bits[i]. If must suspend, return FALSE. }
1075 { make a byte available.
1076 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1077 but we must reload the local copies after a successful fill. }
1079 begin
1081 begin
1083 exit;
1085 { Reload the local copies }
1099 {$IFDEF DEBUG}
1106 {$ENDIF}
1108 { Here we just do minimal validation of the counts to avoid walking
1109 off the end of our table space. jdhuff.c will check more carefully. }
1115 begin
1116 { Read a byte into variable huffval[i]. If must suspend, return FALSE. }
1117 { make a byte available.
1118 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1119 but we must reload the local copies after a successful fill. }
1121 begin
1123 begin
1125 exit;
1127 { Reload the local copies }
1143 end
1144 else
1162 { Unload the local copies --- do this only at a restart boundary }
1170 {LOCAL}
1172 { Process a DQT marker }
1173 var
1178 var
1182 begin
1187 { Read two bytes interpreted as an unsigned 16-bit integer.
1188 length should be declared unsigned int or perhaps INT32. }
1190 { make a byte available.
1191 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1192 but we must reload the local copies after a successful fill. }
1194 begin
1196 begin
1198 exit;
1200 { Reload the local copies }
1208 { make a byte available.
1209 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1210 but we must reload the local copies after a successful fill. }
1212 begin
1214 begin
1216 exit;
1218 { Reload the local copies }
1230 begin
1231 { Read a byte into variable n. If must suspend, return FALSE. }
1232 { make a byte available.
1233 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1234 but we must reload the local copies after a successful fill. }
1236 begin
1238 begin
1240 exit;
1242 { Reload the local copies }
1254 {$IFDEF DEBUG}
1256 {$ENDIF}
1266 begin
1268 begin
1269 { Read two bytes interpreted as an unsigned 16-bit integer.
1270 tmp should be declared unsigned int or perhaps INT32. }
1272 { make a byte available.
1273 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1274 but we must reload the local copies after a successful fill. }
1276 begin
1278 begin
1280 exit;
1282 { Reload the local copies }
1290 { make a byte available.
1291 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1292 but we must reload the local copies after a successful fill. }
1294 begin
1296 begin
1298 exit;
1300 { Reload the local copies }
1309 end
1310 else
1311 begin
1312 { Read a byte into variable tmp. If must suspend, return FALSE. }
1313 { make a byte available.
1314 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1315 but we must reload the local copies after a successful fill. }
1317 begin
1319 begin
1321 exit;
1323 { Reload the local copies }
1333 { We convert the zigzag-order table to natural array order. }
1338 begin
1341 begin
1342 {$IFDEF DEBUG}
1348 {$ENDIF}
1361 { Unload the local copies --- do this only at a restart boundary }
1369 {LOCAL}
1371 { Process a DRI marker }
1372 var
1375 var
1379 begin
1384 { Read two bytes interpreted as an unsigned 16-bit integer.
1385 length should be declared unsigned int or perhaps INT32. }
1387 { make a byte available.
1388 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1389 but we must reload the local copies after a successful fill. }
1391 begin
1393 begin
1395 exit;
1397 { Reload the local copies }
1405 { make a byte available.
1406 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1407 but we must reload the local copies after a successful fill. }
1409 begin
1411 begin
1413 exit;
1415 { Reload the local copies }
1427 { Read two bytes interpreted as an unsigned 16-bit integer.
1428 tmp should be declared unsigned int or perhaps INT32. }
1430 { make a byte available.
1431 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1432 but we must reload the local copies after a successful fill. }
1434 begin
1436 begin
1438 exit;
1440 { Reload the local copies }
1448 { make a byte available.
1449 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1450 but we must reload the local copies after a successful fill. }
1452 begin
1454 begin
1456 exit;
1458 { Reload the local copies }
1467 {$IFDEF DEBUG}
1469 {$ENDIF}
1473 { Unload the local copies --- do this only at a restart boundary }
1481 { Routines for processing APPn and COM markers.
1482 These are either saved in memory or discarded, per application request.
1483 APP0 and APP14 are specially checked to see if they are
1484 JFIF and Adobe markers, respectively. }
1486 const
1492 {LOCAL}
1498 { Examine first few bytes from an APP0.
1499 Take appropriate action if it is a JFIF marker.
1500 datalen is # of bytes at data[], remaining is length of rest of marker data.
1501 }
1502 {$IFDEF DEBUG}
1503 var
1505 {$ENDIF}
1506 begin
1507 {$IFDEF DEBUG}
1509 {$ENDIF}
1516 begin
1517 { Found JFIF APP0 marker: save info }
1524 { Check version.
1525 Major version must be 1, anything else signals an incompatible change.
1526 (We used to treat this as an error, but now it's a nonfatal warning,
1527 because some bozo at Hijaak couldn't read the spec.)
1528 Minor version should be 0..2, but process anyway if newer. }
1533 { Generate trace messages }
1534 {$IFDEF DEBUG}
1538 { Validate thumbnail dimensions and issue appropriate messages }
1546 {$ENDIF}
1547 end
1548 else
1555 begin
1556 { Found JFIF "JFXX" extension APP0 marker }
1557 { The library doesn't actually do anything with these,
1558 but we try to produce a helpful trace message. }
1559 {$IFDEF DEBUG}
1567 else
1571 {$ENDIF}
1572 end
1573 else
1574 begin
1575 { Start of APP0 does not match "JFIF" or "JFXX", or too short }
1576 {$IFDEF DEBUG}
1578 {$ENDIF}
1583 {LOCAL}
1588 { Examine first few bytes from an APP14.
1589 Take appropriate action if it is an Adobe marker.
1590 datalen is # of bytes at data[], remaining is length of rest of marker data.
1591 }
1592 var
1593 {$IFDEF DEBUG}
1595 {$ENDIF}
1597 begin
1604 begin
1605 { Found Adobe APP14 marker }
1606 {$IFDEF DEBUG}
1610 {$ENDIF}
1612 {$IFDEF DEBUG}
1614 {$ENDIF}
1617 end
1618 else
1619 begin
1620 { Start of APP14 does not match "Adobe", or too short }
1621 {$IFDEF DEBUG}
1623 {$ENDIF}
1628 {METHODDEF}
1630 { Process an APP0 or APP14 marker without saving it }
1631 var
1635 var
1639 begin
1644 { Read two bytes interpreted as an unsigned 16-bit integer.
1645 length should be declared unsigned int or perhaps INT32. }
1647 { make a byte available.
1648 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1649 but we must reload the local copies after a successful fill. }
1651 begin
1653 begin
1655 exit;
1657 { Reload the local copies }
1666 { make a byte available.
1667 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1668 but we must reload the local copies after a successful fill. }
1670 begin
1672 begin
1674 exit;
1676 { Reload the local copies }
1687 { get the interesting part of the marker data }
1689 numtoread := APPN_DATA_LEN
1690 else
1693 else
1696 begin
1697 { Read a byte into b[i]. If must suspend, return FALSE. }
1698 { make a byte available.
1699 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1700 but we must reload the local copies after a successful fill. }
1702 begin
1704 begin
1706 exit;
1708 { Reload the local copies }
1720 { process it }
1722 M_APP0:
1724 M_APP14:
1726 else
1727 { can't get here unless jpeg_save_markers chooses wrong processor }
1731 { skip any remaining data -- could be lots }
1733 { Unload the local copies --- do this only at a restart boundary }
1743 {$ifdef SAVE_MARKERS_SUPPORTED}
1745 {METHODDEF}
1747 { Save an APPn or COM marker into the marker list }
1748 var
1754 var
1758 var
1760 var
1762 begin
1763 { local copies of input pointer/count }
1773 begin
1774 { begin reading a marker }
1775 { Read two bytes interpreted as an unsigned 16-bit integer. }
1777 { make a byte available.
1778 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1779 but we must reload the local copies after a successful fill. }
1781 begin
1783 begin
1785 exit;
1787 { Reload the local copies }
1796 { make a byte available.
1797 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1798 but we must reload the local copies after a successful fill. }
1800 begin
1802 begin
1804 exit;
1806 { Reload the local copies }
1818 { figure out how much we want to save }
1822 else
1826 { allocate and initialize the marker item }
1834 { data area is just beyond the jpeg_marker_struct }
1843 end
1844 else
1845 begin
1846 { deal with bogus length word }
1850 end
1851 end
1852 else
1853 begin
1854 { resume reading a marker }
1862 begin
1863 { move the restart point to here }
1868 { If there's not at least one byte in buffer, suspend }
1870 begin
1872 begin
1874 exit;
1880 { Copy bytes with reasonable rapidity }
1882 begin
1891 { Done reading what we want to read }
1894 { Add new marker to end of list }
1896 begin
1898 end
1899 else
1900 begin
1906 { Reset pointer & calc remaining data length }
1910 { Reset to initial state for next marker }
1913 { Process the marker if interesting; else just make a generic trace msg }
1915 M_APP0:
1917 M_APP14:
1919 else
1920 {$IFDEF DEBUG}
1923 {$ENDIF}
1926 { skip any remaining data -- could be lots }
1927 { do before skip_input_data }
1940 { Find the next JPEG marker, save it in cinfo^.unread_marker.
1941 Returns FALSE if had to suspend before reaching a marker;
1942 in that case cinfo^.unread_marker is unchanged.
1944 Note that the result might not be a valid marker code,
1945 but it will never be 0 or FF. }
1947 {LOCAL}
1949 var
1951 var
1955 begin
1960 {while TRUE do}
1961 repeat
1962 { Read a byte into variable c. If must suspend, return FALSE. }
1963 { make a byte available.
1964 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1965 but we must reload the local copies after a successful fill. }
1967 begin
1969 begin
1971 exit;
1973 { Reload the local copies }
1982 { Skip any non-FF bytes.
1983 This may look a bit inefficient, but it will not occur in a valid file.
1984 We sync after each discarded byte so that a suspending data source
1985 can discard the byte from its buffer. }
1988 begin
1990 { Unload the local copies --- do this only at a restart boundary }
1994 { Read a byte into variable c. If must suspend, return FALSE. }
1995 { make a byte available.
1996 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1997 but we must reload the local copies after a successful fill. }
1999 begin
2001 begin
2003 exit;
2005 { Reload the local copies }
2015 { This loop swallows any duplicate FF bytes. Extra FFs are legal as
2016 pad bytes, so don't count them in discarded_bytes. We assume there
2017 will not be so many consecutive FF bytes as to overflow a suspending
2018 data source's input buffer. }
2020 repeat
2021 { Read a byte into variable c. If must suspend, return FALSE. }
2022 { make a byte available.
2023 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2024 but we must reload the local copies after a successful fill. }
2026 begin
2028 begin
2030 exit;
2032 { Reload the local copies }
2043 { Reach here if we found a stuffed-zero data sequence (FF/00).
2044 Discard it and loop back to try again. }
2047 { Unload the local copies --- do this only at a restart boundary }
2053 begin
2061 { Unload the local copies --- do this only at a restart boundary }
2069 {LOCAL}
2071 { Like next_marker, but used to obtain the initial SOI marker. }
2072 { For this marker, we do not allow preceding garbage or fill; otherwise,
2073 we might well scan an entire input file before realizing it ain't JPEG.
2074 If an application wants to process non-JFIF files, it must seek to the
2075 SOI before calling the JPEG library. }
2076 var
2078 var
2082 begin
2087 { Read a byte into variable c. If must suspend, return FALSE. }
2088 { make a byte available.
2089 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2090 but we must reload the local copies after a successful fill. }
2092 begin
2094 begin
2096 exit;
2098 { Reload the local copies }
2107 { Read a byte into variable c2. If must suspend, return FALSE. }
2108 { make a byte available.
2109 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2110 but we must reload the local copies after a successful fill. }
2112 begin
2114 begin
2116 exit;
2118 { Reload the local copies }
2132 { Unload the local copies --- do this only at a restart boundary }
2140 { Read markers until SOS or EOI.
2142 Returns same codes as are defined for jpeg_consume_input:
2143 JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. }
2145 {METHODDEF}
2147 begin
2148 { Outer loop repeats once for each marker. }
2149 repeat
2150 { Collect the marker proper, unless we already did. }
2151 { NB: first_marker() enforces the requirement that SOI appear first. }
2153 begin
2155 begin
2157 begin
2159 exit;
2161 end
2162 else
2163 begin
2165 begin
2167 exit;
2171 { At this point cinfo^.unread_marker contains the marker code and the
2172 input point is just past the marker proper, but before any parameters.
2173 A suspension will cause us to return with this state still true. }
2176 M_SOI:
2178 begin
2180 exit;
2186 begin
2188 exit;
2192 begin
2194 exit;
2199 begin
2201 exit;
2206 begin
2208 exit;
2211 { Currently unsupported SOFn types }
2223 M_SOS:
2224 begin
2226 begin
2228 exit;
2232 exit;
2235 M_EOI:
2236 begin
2237 {$IFDEF DEBUG}
2239 {$ENDIF}
2242 exit;
2245 M_DAC:
2247 begin
2249 exit;
2252 M_DHT:
2254 begin
2256 exit;
2259 M_DQT:
2261 begin
2263 exit;
2266 M_DRI:
2268 begin
2270 exit;
2273 M_APP0,
2274 M_APP1,
2275 M_APP2,
2276 M_APP3,
2277 M_APP4,
2278 M_APP5,
2279 M_APP6,
2280 M_APP7,
2281 M_APP8,
2282 M_APP9,
2283 M_APP10,
2284 M_APP11,
2285 M_APP12,
2286 M_APP13,
2287 M_APP14,
2288 M_APP15:
2291 begin
2293 exit;
2296 M_COM:
2298 begin
2300 exit;
2304 M_RST1,
2305 M_RST2,
2306 M_RST3,
2307 M_RST4,
2308 M_RST5,
2309 M_RST6,
2310 M_RST7,
2311 M_TEM:
2312 {$IFDEF DEBUG}
2315 {$ENDIF}
2316 ;
2320 begin
2322 exit;
2326 { For now, we treat the reserved markers as fatal errors since they are
2327 likely to be used to signal incompatible JPEG Part 3 extensions.
2328 Once the JPEG 3 version-number marker is well defined, this code
2329 ought to change! }
2333 { Successfully processed marker, so reset state variable }
2339 { Read a restart marker, which is expected to appear next in the datastream;
2340 if the marker is not there, take appropriate recovery action.
2341 Returns FALSE if suspension is required.
2343 This is called by the entropy decoder after it has read an appropriate
2344 number of MCUs. cinfo^.unread_marker may be nonzero if the entropy decoder
2345 has already read a marker from the data source. Under normal conditions
2346 cinfo^.unread_marker will be reset to 0 before returning; if not reset,
2347 it holds a marker which the decoder will be unable to read past. }
2349 {METHODDEF}
2351 begin
2352 { Obtain a marker unless we already did. }
2353 { Note that next_marker will complain if it skips any data. }
2355 begin
2357 begin
2359 exit;
2364 begin
2365 { Normal case --- swallow the marker and let entropy decoder continue }
2366 {$IFDEF DEBUG}
2369 {$ENDIF}
2371 end
2372 else
2373 begin
2374 { Uh-oh, the restart markers have been messed up. }
2375 { Let the data source manager determine how to resync. }
2378 begin
2380 exit;
2384 { Update next-restart state }
2392 { This is the default resync_to_restart method for data source managers
2393 to use if they don't have any better approach. Some data source managers
2394 may be able to back up, or may have additional knowledge about the data
2395 which permits a more intelligent recovery strategy; such managers would
2396 presumably supply their own resync method.
2398 read_restart_marker calls resync_to_restart if it finds a marker other than
2399 the restart marker it was expecting. (This code is *not* used unless
2400 a nonzero restart interval has been declared.) cinfo^.unread_marker is
2401 the marker code actually found (might be anything, except 0 or FF).
2402 The desired restart marker number (0..7) is passed as a parameter.
2403 This routine is supposed to apply whatever error recovery strategy seems
2404 appropriate in order to position the input stream to the next data segment.
2405 Note that cinfo^.unread_marker is treated as a marker appearing before
2406 the current data-source input point; usually it should be reset to zero
2407 before returning.
2408 Returns FALSE if suspension is required.
2410 This implementation is substantially constrained by wanting to treat the
2411 input as a data stream; this means we can't back up. Therefore, we have
2412 only the following actions to work with:
2413 1. Simply discard the marker and let the entropy decoder resume at next
2414 byte of file.
2415 2. Read forward until we find another marker, discarding intervening
2416 data. (In theory we could look ahead within the current bufferload,
2417 without having to discard data if we don't find the desired marker.
2418 This idea is not implemented here, in part because it makes behavior
2419 dependent on buffer size and chance buffer-boundary positions.)
2420 3. Leave the marker unread (by failing to zero cinfo^.unread_marker).
2421 This will cause the entropy decoder to process an empty data segment,
2422 inserting dummy zeroes, and then we will reprocess the marker.
2424 #2 is appropriate if we think the desired marker lies ahead, while #3 is
2425 appropriate if the found marker is a future restart marker (indicating
2426 that we have missed the desired restart marker, probably because it got
2427 corrupted).
2428 We apply #2 or #3 if the found marker is a restart marker no more than
2429 two counts behind or ahead of the expected one. We also apply #2 if the
2430 found marker is not a legal JPEG marker code (it's certainly bogus data).
2431 If the found marker is a restart marker more than 2 counts away, we do #1
2432 (too much risk that the marker is erroneous; with luck we will be able to
2433 resync at some future point).
2434 For any valid non-restart JPEG marker, we apply #3. This keeps us from
2435 overrunning the end of a scan. An implementation limited to single-scan
2436 files might find it better to apply #2 for markers other than EOI, since
2437 any other marker would have to be bogus data in that case. }
2440 {GLOBAL}
2443 var
2446 begin
2448 //action := 1; { never used }
2449 { Always put up a warning. }
2452 { Outer loop handles repeated decision after scanning forward. }
2453 repeat
2456 else
2459 else
2460 begin
2464 else
2468 else
2472 {$IFDEF DEBUG}
2474 {$ENDIF}
2477 { Discard marker and let entropy decoder resume processing. }
2478 begin
2481 exit;
2484 { Scan to the next marker, and repeat the decision loop. }
2485 begin
2487 begin
2489 exit;
2494 { Return without advancing past this marker. }
2495 { Entropy decoder will be forced to process an empty segment. }
2496 begin
2498 exit;
2505 { Reset marker processing state to begin a fresh datastream. }
2507 {METHODDEF}
2509 var
2511 begin
2514 begin
2526 { Initialize the marker reader module.
2527 This is called only once, when the decompression object is created. }
2529 {GLOBAL}
2531 var
2534 begin
2535 { Create subobject in permanent pool }
2539 );
2541 { Initialize method pointers }
2545 { Initialize COM/APPn processing.
2546 By default, we examine and then discard APP0 and APP14,
2547 but simply discard COM and all other APPn. }
2552 begin
2558 { Reset marker processing state }
2563 { Control saving of COM and APPn markers into marker_list. }
2566 {$ifdef SAVE_MARKERS_SUPPORTED}
2568 {GLOBAL}
2572 var
2576 begin
2579 { Length limit mustn't be larger than what we can allocate
2580 (should only be a concern in a 16-bit environment). }
2586 { Choose processor routine to use.
2587 APP0/APP14 have special requirements. }
2590 begin
2592 { If saving APP0/APP14, save at least enough for our internal use. }
2594 length_limit := APP0_DATA_LEN
2595 else
2598 end
2599 else
2600 begin
2602 { If discarding APP0/APP14, use our regular on-the-fly processor. }
2608 begin
2611 end
2612 else
2614 begin
2617 end
2618 else
2624 { Install a special processing method for COM or APPn markers. }
2626 {GLOBAL}
2631 var
2633 begin
2637 else
2640 else