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
1697 begin
1699 begin
1700 { Read a byte into b[i]. If must suspend, return FALSE. }
1701 { make a byte available.
1702 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1703 but we must reload the local copies after a successful fill. }
1705 begin
1707 begin
1709 exit;
1711 { Reload the local copies }
1724 { process it }
1726 M_APP0:
1728 M_APP14:
1730 else
1731 { can't get here unless jpeg_save_markers chooses wrong processor }
1735 { skip any remaining data -- could be lots }
1737 { Unload the local copies --- do this only at a restart boundary }
1747 {$ifdef SAVE_MARKERS_SUPPORTED}
1749 {METHODDEF}
1751 { Save an APPn or COM marker into the marker list }
1752 var
1758 var
1762 var
1764 var
1766 begin
1767 { local copies of input pointer/count }
1777 begin
1778 { begin reading a marker }
1779 { Read two bytes interpreted as an unsigned 16-bit integer. }
1781 { make a byte available.
1782 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1783 but we must reload the local copies after a successful fill. }
1785 begin
1787 begin
1789 exit;
1791 { Reload the local copies }
1800 { make a byte available.
1801 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1802 but we must reload the local copies after a successful fill. }
1804 begin
1806 begin
1808 exit;
1810 { Reload the local copies }
1822 { figure out how much we want to save }
1826 else
1830 { allocate and initialize the marker item }
1838 { data area is just beyond the jpeg_marker_struct }
1847 end
1848 else
1849 begin
1850 { deal with bogus length word }
1854 end
1855 end
1856 else
1857 begin
1858 { resume reading a marker }
1866 begin
1867 { move the restart point to here }
1872 { If there's not at least one byte in buffer, suspend }
1874 begin
1876 begin
1878 exit;
1884 { Copy bytes with reasonable rapidity }
1886 begin
1895 { Done reading what we want to read }
1898 { Add new marker to end of list }
1900 begin
1902 end
1903 else
1904 begin
1910 { Reset pointer & calc remaining data length }
1914 { Reset to initial state for next marker }
1917 { Process the marker if interesting; else just make a generic trace msg }
1919 M_APP0:
1921 M_APP14:
1923 else
1924 {$IFDEF DEBUG}
1927 {$ENDIF}
1930 { skip any remaining data -- could be lots }
1931 { do before skip_input_data }
1944 { Find the next JPEG marker, save it in cinfo^.unread_marker.
1945 Returns FALSE if had to suspend before reaching a marker;
1946 in that case cinfo^.unread_marker is unchanged.
1948 Note that the result might not be a valid marker code,
1949 but it will never be 0 or FF. }
1951 {LOCAL}
1953 var
1955 var
1959 begin
1964 {while TRUE do}
1965 repeat
1966 { Read a byte into variable c. If must suspend, return FALSE. }
1967 { make a byte available.
1968 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1969 but we must reload the local copies after a successful fill. }
1971 begin
1973 begin
1975 exit;
1977 { Reload the local copies }
1986 { Skip any non-FF bytes.
1987 This may look a bit inefficient, but it will not occur in a valid file.
1988 We sync after each discarded byte so that a suspending data source
1989 can discard the byte from its buffer. }
1992 begin
1994 { Unload the local copies --- do this only at a restart boundary }
1998 { Read a byte into variable c. If must suspend, return FALSE. }
1999 { make a byte available.
2000 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2001 but we must reload the local copies after a successful fill. }
2003 begin
2005 begin
2007 exit;
2009 { Reload the local copies }
2019 { This loop swallows any duplicate FF bytes. Extra FFs are legal as
2020 pad bytes, so don't count them in discarded_bytes. We assume there
2021 will not be so many consecutive FF bytes as to overflow a suspending
2022 data source's input buffer. }
2024 repeat
2025 { Read a byte into variable c. If must suspend, return FALSE. }
2026 { make a byte available.
2027 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2028 but we must reload the local copies after a successful fill. }
2030 begin
2032 begin
2034 exit;
2036 { Reload the local copies }
2047 { Reach here if we found a stuffed-zero data sequence (FF/00).
2048 Discard it and loop back to try again. }
2051 { Unload the local copies --- do this only at a restart boundary }
2057 begin
2065 { Unload the local copies --- do this only at a restart boundary }
2073 {LOCAL}
2075 { Like next_marker, but used to obtain the initial SOI marker. }
2076 { For this marker, we do not allow preceding garbage or fill; otherwise,
2077 we might well scan an entire input file before realizing it ain't JPEG.
2078 If an application wants to process non-JFIF files, it must seek to the
2079 SOI before calling the JPEG library. }
2080 var
2082 var
2086 begin
2091 { Read a byte into variable c. If must suspend, return FALSE. }
2092 { make a byte available.
2093 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2094 but we must reload the local copies after a successful fill. }
2096 begin
2098 begin
2100 exit;
2102 { Reload the local copies }
2111 { Read a byte into variable c2. If must suspend, return FALSE. }
2112 { make a byte available.
2113 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2114 but we must reload the local copies after a successful fill. }
2116 begin
2118 begin
2120 exit;
2122 { Reload the local copies }
2136 { Unload the local copies --- do this only at a restart boundary }
2144 { Read markers until SOS or EOI.
2146 Returns same codes as are defined for jpeg_consume_input:
2147 JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. }
2149 {METHODDEF}
2151 begin
2152 { Outer loop repeats once for each marker. }
2153 repeat
2154 { Collect the marker proper, unless we already did. }
2155 { NB: first_marker() enforces the requirement that SOI appear first. }
2157 begin
2159 begin
2161 begin
2163 exit;
2165 end
2166 else
2167 begin
2169 begin
2171 exit;
2175 { At this point cinfo^.unread_marker contains the marker code and the
2176 input point is just past the marker proper, but before any parameters.
2177 A suspension will cause us to return with this state still true. }
2180 M_SOI:
2182 begin
2184 exit;
2190 begin
2192 exit;
2196 begin
2198 exit;
2203 begin
2205 exit;
2210 begin
2212 exit;
2215 { Currently unsupported SOFn types }
2227 M_SOS:
2228 begin
2230 begin
2232 exit;
2236 exit;
2239 M_EOI:
2240 begin
2241 {$IFDEF DEBUG}
2243 {$ENDIF}
2246 exit;
2249 M_DAC:
2251 begin
2253 exit;
2256 M_DHT:
2258 begin
2260 exit;
2263 M_DQT:
2265 begin
2267 exit;
2270 M_DRI:
2272 begin
2274 exit;
2277 M_APP0,
2278 M_APP1,
2279 M_APP2,
2280 M_APP3,
2281 M_APP4,
2282 M_APP5,
2283 M_APP6,
2284 M_APP7,
2285 M_APP8,
2286 M_APP9,
2287 M_APP10,
2288 M_APP11,
2289 M_APP12,
2290 M_APP13,
2291 M_APP14,
2292 M_APP15:
2295 begin
2297 exit;
2300 M_COM:
2302 begin
2304 exit;
2308 M_RST1,
2309 M_RST2,
2310 M_RST3,
2311 M_RST4,
2312 M_RST5,
2313 M_RST6,
2314 M_RST7,
2315 M_TEM:
2316 {$IFDEF DEBUG}
2319 {$ENDIF}
2320 ;
2324 begin
2326 exit;
2330 { For now, we treat the reserved markers as fatal errors since they are
2331 likely to be used to signal incompatible JPEG Part 3 extensions.
2332 Once the JPEG 3 version-number marker is well defined, this code
2333 ought to change! }
2337 { Successfully processed marker, so reset state variable }
2343 { Read a restart marker, which is expected to appear next in the datastream;
2344 if the marker is not there, take appropriate recovery action.
2345 Returns FALSE if suspension is required.
2347 This is called by the entropy decoder after it has read an appropriate
2348 number of MCUs. cinfo^.unread_marker may be nonzero if the entropy decoder
2349 has already read a marker from the data source. Under normal conditions
2350 cinfo^.unread_marker will be reset to 0 before returning; if not reset,
2351 it holds a marker which the decoder will be unable to read past. }
2353 {METHODDEF}
2355 begin
2356 { Obtain a marker unless we already did. }
2357 { Note that next_marker will complain if it skips any data. }
2359 begin
2361 begin
2363 exit;
2368 begin
2369 { Normal case --- swallow the marker and let entropy decoder continue }
2370 {$IFDEF DEBUG}
2373 {$ENDIF}
2375 end
2376 else
2377 begin
2378 { Uh-oh, the restart markers have been messed up. }
2379 { Let the data source manager determine how to resync. }
2382 begin
2384 exit;
2388 { Update next-restart state }
2396 { This is the default resync_to_restart method for data source managers
2397 to use if they don't have any better approach. Some data source managers
2398 may be able to back up, or may have additional knowledge about the data
2399 which permits a more intelligent recovery strategy; such managers would
2400 presumably supply their own resync method.
2402 read_restart_marker calls resync_to_restart if it finds a marker other than
2403 the restart marker it was expecting. (This code is *not* used unless
2404 a nonzero restart interval has been declared.) cinfo^.unread_marker is
2405 the marker code actually found (might be anything, except 0 or FF).
2406 The desired restart marker number (0..7) is passed as a parameter.
2407 This routine is supposed to apply whatever error recovery strategy seems
2408 appropriate in order to position the input stream to the next data segment.
2409 Note that cinfo^.unread_marker is treated as a marker appearing before
2410 the current data-source input point; usually it should be reset to zero
2411 before returning.
2412 Returns FALSE if suspension is required.
2414 This implementation is substantially constrained by wanting to treat the
2415 input as a data stream; this means we can't back up. Therefore, we have
2416 only the following actions to work with:
2417 1. Simply discard the marker and let the entropy decoder resume at next
2418 byte of file.
2419 2. Read forward until we find another marker, discarding intervening
2420 data. (In theory we could look ahead within the current bufferload,
2421 without having to discard data if we don't find the desired marker.
2422 This idea is not implemented here, in part because it makes behavior
2423 dependent on buffer size and chance buffer-boundary positions.)
2424 3. Leave the marker unread (by failing to zero cinfo^.unread_marker).
2425 This will cause the entropy decoder to process an empty data segment,
2426 inserting dummy zeroes, and then we will reprocess the marker.
2428 #2 is appropriate if we think the desired marker lies ahead, while #3 is
2429 appropriate if the found marker is a future restart marker (indicating
2430 that we have missed the desired restart marker, probably because it got
2431 corrupted).
2432 We apply #2 or #3 if the found marker is a restart marker no more than
2433 two counts behind or ahead of the expected one. We also apply #2 if the
2434 found marker is not a legal JPEG marker code (it's certainly bogus data).
2435 If the found marker is a restart marker more than 2 counts away, we do #1
2436 (too much risk that the marker is erroneous; with luck we will be able to
2437 resync at some future point).
2438 For any valid non-restart JPEG marker, we apply #3. This keeps us from
2439 overrunning the end of a scan. An implementation limited to single-scan
2440 files might find it better to apply #2 for markers other than EOI, since
2441 any other marker would have to be bogus data in that case. }
2444 {GLOBAL}
2447 var
2450 begin
2452 //action := 1; { never used }
2453 { Always put up a warning. }
2456 { Outer loop handles repeated decision after scanning forward. }
2457 repeat
2460 else
2463 else
2464 begin
2468 else
2472 else
2476 {$IFDEF DEBUG}
2478 {$ENDIF}
2481 { Discard marker and let entropy decoder resume processing. }
2482 begin
2485 exit;
2488 { Scan to the next marker, and repeat the decision loop. }
2489 begin
2491 begin
2493 exit;
2498 { Return without advancing past this marker. }
2499 { Entropy decoder will be forced to process an empty segment. }
2500 begin
2502 exit;
2509 { Reset marker processing state to begin a fresh datastream. }
2511 {METHODDEF}
2513 var
2515 begin
2518 begin
2530 { Initialize the marker reader module.
2531 This is called only once, when the decompression object is created. }
2533 {GLOBAL}
2535 var
2538 begin
2539 { Create subobject in permanent pool }
2543 );
2545 { Initialize method pointers }
2549 { Initialize COM/APPn processing.
2550 By default, we examine and then discard APP0 and APP14,
2551 but simply discard COM and all other APPn. }
2556 begin
2562 { Reset marker processing state }
2567 { Control saving of COM and APPn markers into marker_list. }
2570 {$ifdef SAVE_MARKERS_SUPPORTED}
2572 {GLOBAL}
2576 var
2580 begin
2583 { Length limit mustn't be larger than what we can allocate
2584 (should only be a concern in a 16-bit environment). }
2590 { Choose processor routine to use.
2591 APP0/APP14 have special requirements. }
2594 begin
2596 { If saving APP0/APP14, save at least enough for our internal use. }
2598 length_limit := APP0_DATA_LEN
2599 else
2602 end
2603 else
2604 begin
2606 { If discarding APP0/APP14, use our regular on-the-fly processor. }
2612 begin
2615 end
2616 else
2618 begin
2621 end
2622 else
2628 { Install a special processing method for COM or APPn markers. }
2630 {GLOBAL}
2635 var
2637 begin
2641 else
2644 else