Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2648 lines
78 KiB

3 years ago
  1. unit imjdmarker;
  2. { This file contains routines to decode JPEG datastream markers.
  3. Most of the complexity arises from our desire to support input
  4. suspension: if not all of the data for a marker is available;
  5. we must exit back to the application. On resumption; we reprocess
  6. the marker. }
  7. { Original: jdmarker.c; Copyright (C) 1991-1998; Thomas G. Lane. }
  8. { History
  9. 9.7.96 Conversion to pascal started jnn
  10. 22.3.98 updated to 6b jnn }
  11. interface
  12. {$I imjconfig.inc}
  13. uses
  14. imjmorecfg,
  15. imjinclude,
  16. imjdeferr,
  17. imjerror,
  18. imjcomapi,
  19. imjpeglib;
  20. const { JPEG marker codes }
  21. M_SOF0 = $c0;
  22. M_SOF1 = $c1;
  23. M_SOF2 = $c2;
  24. M_SOF3 = $c3;
  25. M_SOF5 = $c5;
  26. M_SOF6 = $c6;
  27. M_SOF7 = $c7;
  28. M_JPG = $c8;
  29. M_SOF9 = $c9;
  30. M_SOF10 = $ca;
  31. M_SOF11 = $cb;
  32. M_SOF13 = $cd;
  33. M_SOF14 = $ce;
  34. M_SOF15 = $cf;
  35. M_DHT = $c4;
  36. M_DAC = $cc;
  37. M_RST0 = $d0;
  38. M_RST1 = $d1;
  39. M_RST2 = $d2;
  40. M_RST3 = $d3;
  41. M_RST4 = $d4;
  42. M_RST5 = $d5;
  43. M_RST6 = $d6;
  44. M_RST7 = $d7;
  45. M_SOI = $d8;
  46. M_EOI = $d9;
  47. M_SOS = $da;
  48. M_DQT = $db;
  49. M_DNL = $dc;
  50. M_DRI = $dd;
  51. M_DHP = $de;
  52. M_EXP = $df;
  53. M_APP0 = $e0;
  54. M_APP1 = $e1;
  55. M_APP2 = $e2;
  56. M_APP3 = $e3;
  57. M_APP4 = $e4;
  58. M_APP5 = $e5;
  59. M_APP6 = $e6;
  60. M_APP7 = $e7;
  61. M_APP8 = $e8;
  62. M_APP9 = $e9;
  63. M_APP10 = $ea;
  64. M_APP11 = $eb;
  65. M_APP12 = $ec;
  66. M_APP13 = $ed;
  67. M_APP14 = $ee;
  68. M_APP15 = $ef;
  69. M_JPG0 = $f0;
  70. M_JPG13 = $fd;
  71. M_COM = $fe;
  72. M_TEM = $01;
  73. M_ERROR = $100;
  74. type
  75. JPEG_MARKER = uint; { JPEG marker codes }
  76. { Private state }
  77. type
  78. my_marker_ptr = ^my_marker_reader;
  79. my_marker_reader = record
  80. pub : jpeg_marker_reader; { public fields }
  81. { Application-overridable marker processing methods }
  82. process_COM : jpeg_marker_parser_method;
  83. process_APPn : array[0..16-1] of jpeg_marker_parser_method;
  84. { Limit on marker data length to save for each marker type }
  85. length_limit_COM : uint;
  86. length_limit_APPn : array[0..16-1] of uint;
  87. { Status of COM/APPn marker saving }
  88. cur_marker : jpeg_saved_marker_ptr; { NIL if not processing a marker }
  89. bytes_read : uint; { data bytes read so far in marker }
  90. { Note: cur_marker is not linked into marker_list until it's all read. }
  91. end;
  92. {GLOBAL}
  93. function jpeg_resync_to_restart(cinfo : j_decompress_ptr;
  94. desired : int) : boolean;
  95. {GLOBAL}
  96. procedure jinit_marker_reader (cinfo : j_decompress_ptr);
  97. {$ifdef SAVE_MARKERS_SUPPORTED}
  98. {GLOBAL}
  99. procedure jpeg_save_markers (cinfo : j_decompress_ptr;
  100. marker_code : int;
  101. length_limit : uint);
  102. {$ENDIF}
  103. {GLOBAL}
  104. procedure jpeg_set_marker_processor (cinfo : j_decompress_ptr;
  105. marker_code : int;
  106. routine : jpeg_marker_parser_method);
  107. implementation
  108. uses
  109. imjutils;
  110. { At all times, cinfo1.src.next_input_byte and .bytes_in_buffer reflect
  111. the current restart point; we update them only when we have reached a
  112. suitable place to restart if a suspension occurs. }
  113. { Routines to process JPEG markers.
  114. Entry condition: JPEG marker itself has been read and its code saved
  115. in cinfo^.unread_marker; input restart point is just after the marker.
  116. Exit: if return TRUE, have read and processed any parameters, and have
  117. updated the restart point to point after the parameters.
  118. If return FALSE, was forced to suspend before reaching end of
  119. marker parameters; restart point has not been moved. Same routine
  120. will be called again after application supplies more input data.
  121. This approach to suspension assumes that all of a marker's parameters
  122. can fit into a single input bufferload. This should hold for "normal"
  123. markers. Some COM/APPn markers might have large parameter segments
  124. that might not fit. If we are simply dropping such a marker, we use
  125. skip_input_data to get past it, and thereby put the problem on the
  126. source manager's shoulders. If we are saving the marker's contents
  127. into memory, we use a slightly different convention: when forced to
  128. suspend, the marker processor updates the restart point to the end of
  129. what it's consumed (ie, the end of the buffer) before returning FALSE.
  130. On resumption, cinfo->unread_marker still contains the marker code,
  131. but the data source will point to the next chunk of marker data.
  132. The marker processor must retain internal state to deal with this.
  133. Note that we don't bother to avoid duplicate trace messages if a
  134. suspension occurs within marker parameters. Other side effects
  135. require more care. }
  136. {LOCAL}
  137. function get_soi (cinfo : j_decompress_ptr) : boolean;
  138. { Process an SOI marker }
  139. var
  140. i : int;
  141. begin
  142. {$IFDEF DEBUG}
  143. TRACEMS(j_common_ptr(cinfo), 1, JTRC_SOI);
  144. {$ENDIF}
  145. if (cinfo^.marker^.saw_SOI) then
  146. ERREXIT(j_common_ptr(cinfo), JERR_SOI_DUPLICATE);
  147. { Reset all parameters that are defined to be reset by SOI }
  148. for i := 0 to Pred(NUM_ARITH_TBLS) do
  149. with cinfo^ do
  150. begin
  151. arith_dc_L[i] := 0;
  152. arith_dc_U[i] := 1;
  153. arith_ac_K[i] := 5;
  154. end;
  155. cinfo^.restart_interval := 0;
  156. { Set initial assumptions for colorspace etc }
  157. with cinfo^ do
  158. begin
  159. jpeg_color_space := JCS_UNKNOWN;
  160. CCIR601_sampling := FALSE; { Assume non-CCIR sampling??? }
  161. saw_JFIF_marker := FALSE;
  162. JFIF_major_version := 1; { set default JFIF APP0 values }
  163. JFIF_minor_version := 1;
  164. density_unit := 0;
  165. X_density := 1;
  166. Y_density := 1;
  167. saw_Adobe_marker := FALSE;
  168. Adobe_transform := 0;
  169. marker^.saw_SOI := TRUE;
  170. end;
  171. get_soi := TRUE;
  172. end; { get_soi }
  173. {LOCAL}
  174. function get_sof(cinfo : j_decompress_ptr;
  175. is_prog : boolean;
  176. is_arith : boolean) : boolean;
  177. { Process a SOFn marker }
  178. var
  179. length : INT32;
  180. c, ci : int;
  181. compptr : jpeg_component_info_ptr;
  182. { Declare and initialize local copies of input pointer/count }
  183. var
  184. datasrc : jpeg_source_mgr_ptr;
  185. next_input_byte : JOCTETptr;
  186. bytes_in_buffer : size_t;
  187. begin
  188. datasrc := cinfo^.src;
  189. next_input_byte := datasrc^.next_input_byte;
  190. bytes_in_buffer := datasrc^.bytes_in_buffer;
  191. {}
  192. cinfo^.progressive_mode := is_prog;
  193. cinfo^.arith_code := is_arith;
  194. { Read two bytes interpreted as an unsigned 16-bit integer.
  195. length should be declared unsigned int or perhaps INT32. }
  196. { make a byte available.
  197. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  198. but we must reload the local copies after a successful fill. }
  199. if (bytes_in_buffer = 0) then
  200. begin
  201. if (not datasrc^.fill_input_buffer(cinfo)) then
  202. begin
  203. get_sof := FALSE;
  204. exit;
  205. end;
  206. { Reload the local copies }
  207. next_input_byte := datasrc^.next_input_byte;
  208. bytes_in_buffer := datasrc^.bytes_in_buffer;
  209. end;
  210. Dec( bytes_in_buffer );
  211. length := (uint( GETJOCTET(next_input_byte^)) shl 8);
  212. Inc( next_input_byte );
  213. { make a byte available.
  214. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  215. but we must reload the local copies after a successful fill. }
  216. if (bytes_in_buffer = 0) then
  217. begin
  218. if (not datasrc^.fill_input_buffer(cinfo)) then
  219. begin
  220. get_sof := FALSE;
  221. exit;
  222. end;
  223. { Reload the local copies }
  224. next_input_byte := datasrc^.next_input_byte;
  225. bytes_in_buffer := datasrc^.bytes_in_buffer;
  226. end;
  227. Dec( bytes_in_buffer );
  228. Inc( length, GETJOCTET( next_input_byte^));
  229. Inc( next_input_byte );
  230. { Read a byte into variable cinfo^.data_precision.
  231. If must suspend, return FALSE. }
  232. { make a byte available.
  233. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  234. but we must reload the local copies after a successful fill. }
  235. if (bytes_in_buffer = 0) then
  236. begin
  237. if (not datasrc^.fill_input_buffer(cinfo)) then
  238. begin
  239. get_sof := FALSE;
  240. exit;
  241. end;
  242. { Reload the local copies }
  243. next_input_byte := datasrc^.next_input_byte;
  244. bytes_in_buffer := datasrc^.bytes_in_buffer;
  245. end;
  246. Dec( bytes_in_buffer );
  247. cinfo^.data_precision := GETJOCTET(next_input_byte^);
  248. Inc(next_input_byte);
  249. { Read two bytes interpreted as an unsigned 16-bit integer.
  250. cinfo^.image_height should be declared unsigned int or perhaps INT32. }
  251. { make a byte available.
  252. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  253. but we must reload the local copies after a successful fill. }
  254. if (bytes_in_buffer = 0) then
  255. begin
  256. if (not datasrc^.fill_input_buffer(cinfo)) then
  257. begin
  258. get_sof := FALSE;
  259. exit;
  260. end;
  261. { Reload the local copies }
  262. next_input_byte := datasrc^.next_input_byte;
  263. bytes_in_buffer := datasrc^.bytes_in_buffer;
  264. end;
  265. Dec( bytes_in_buffer );
  266. cinfo^.image_height := (uint( GETJOCTET(next_input_byte^)) shl 8);
  267. Inc( next_input_byte );
  268. { make a byte available.
  269. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  270. but we must reload the local copies after a successful fill. }
  271. if (bytes_in_buffer = 0) then
  272. begin
  273. if (not datasrc^.fill_input_buffer(cinfo)) then
  274. begin
  275. get_sof := FALSE;
  276. exit;
  277. end;
  278. { Reload the local copies }
  279. next_input_byte := datasrc^.next_input_byte;
  280. bytes_in_buffer := datasrc^.bytes_in_buffer;
  281. end;
  282. Dec( bytes_in_buffer );
  283. Inc( cinfo^.image_height, GETJOCTET( next_input_byte^));
  284. Inc( next_input_byte );
  285. { Read two bytes interpreted as an unsigned 16-bit integer.
  286. cinfo^.image_width should be declared unsigned int or perhaps INT32. }
  287. { make a byte available.
  288. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  289. but we must reload the local copies after a successful fill. }
  290. if (bytes_in_buffer = 0) then
  291. begin
  292. if (not datasrc^.fill_input_buffer(cinfo)) then
  293. begin
  294. get_sof := FALSE;
  295. exit;
  296. end;
  297. { Reload the local copies }
  298. next_input_byte := datasrc^.next_input_byte;
  299. bytes_in_buffer := datasrc^.bytes_in_buffer;
  300. end;
  301. Dec( bytes_in_buffer );
  302. cinfo^.image_width := (uint( GETJOCTET(next_input_byte^)) shl 8);
  303. Inc( next_input_byte );
  304. { make a byte available.
  305. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  306. but we must reload the local copies after a successful fill. }
  307. if (bytes_in_buffer = 0) then
  308. begin
  309. if (not datasrc^.fill_input_buffer(cinfo)) then
  310. begin
  311. get_sof := FALSE;
  312. exit;
  313. end;
  314. { Reload the local copies }
  315. next_input_byte := datasrc^.next_input_byte;
  316. bytes_in_buffer := datasrc^.bytes_in_buffer;
  317. end;
  318. Dec( bytes_in_buffer );
  319. Inc( cinfo^.image_width, GETJOCTET( next_input_byte^));
  320. Inc( next_input_byte );
  321. { Read a byte into variable cinfo^.num_components.
  322. If must suspend, return FALSE. }
  323. { make a byte available.
  324. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  325. but we must reload the local copies after a successful fill. }
  326. if (bytes_in_buffer = 0) then
  327. begin
  328. if (not datasrc^.fill_input_buffer(cinfo)) then
  329. begin
  330. get_sof := FALSE;
  331. exit;
  332. end;
  333. { Reload the local copies }
  334. next_input_byte := datasrc^.next_input_byte;
  335. bytes_in_buffer := datasrc^.bytes_in_buffer;
  336. end;
  337. Dec( bytes_in_buffer );
  338. cinfo^.num_components := GETJOCTET(next_input_byte^);
  339. Inc(next_input_byte);
  340. Dec(length, 8);
  341. {$IFDEF DEBUG}
  342. TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOF, cinfo^.unread_marker,
  343. int(cinfo^.image_width), int(cinfo^.image_height),
  344. cinfo^.num_components);
  345. {$ENDIF}
  346. if (cinfo^.marker^.saw_SOF) then
  347. ERREXIT(j_common_ptr(cinfo), JERR_SOF_DUPLICATE);
  348. { We don't support files in which the image height is initially specified }
  349. { as 0 and is later redefined by DNL. As long as we have to check that, }
  350. { might as well have a general sanity check. }
  351. if (cinfo^.image_height <= 0) or (cinfo^.image_width <= 0)
  352. or (cinfo^.num_components <= 0) then
  353. ERREXIT(j_common_ptr(cinfo), JERR_EMPTY_IMAGE);
  354. if (length <> (cinfo^.num_components * 3)) then
  355. ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
  356. if (cinfo^.comp_info = NIL) then { do only once, even if suspend }
  357. cinfo^.comp_info := jpeg_component_info_list_ptr(
  358. cinfo^.mem^.alloc_small(j_common_ptr(cinfo), JPOOL_IMAGE,
  359. cinfo^.num_components * SIZEOF(jpeg_component_info)));
  360. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  361. for ci := 0 to pred(cinfo^.num_components) do
  362. begin
  363. compptr^.component_index := ci;
  364. { Read a byte into variable compptr^.component_id.
  365. If must suspend, return FALSE. }
  366. { make a byte available.
  367. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  368. but we must reload the local copies after a successful fill. }
  369. if (bytes_in_buffer = 0) then
  370. begin
  371. if (not datasrc^.fill_input_buffer(cinfo)) then
  372. begin
  373. get_sof := FALSE;
  374. exit;
  375. end;
  376. { Reload the local copies }
  377. next_input_byte := datasrc^.next_input_byte;
  378. bytes_in_buffer := datasrc^.bytes_in_buffer;
  379. end;
  380. Dec( bytes_in_buffer );
  381. compptr^.component_id := GETJOCTET(next_input_byte^);
  382. Inc(next_input_byte);
  383. { Read a byte into variable c. If must suspend, return FALSE. }
  384. { make a byte available.
  385. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  386. but we must reload the local copies after a successful fill. }
  387. if (bytes_in_buffer = 0) then
  388. begin
  389. if (not datasrc^.fill_input_buffer(cinfo)) then
  390. begin
  391. get_sof := FALSE;
  392. exit;
  393. end;
  394. { Reload the local copies }
  395. next_input_byte := datasrc^.next_input_byte;
  396. bytes_in_buffer := datasrc^.bytes_in_buffer;
  397. end;
  398. Dec( bytes_in_buffer );
  399. c := GETJOCTET(next_input_byte^);
  400. Inc(next_input_byte);
  401. compptr^.h_samp_factor := (c shr 4) and 15;
  402. compptr^.v_samp_factor := (c ) and 15;
  403. { Read a byte into variable compptr^.quant_tbl_no.
  404. If must suspend, return FALSE. }
  405. { make a byte available.
  406. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  407. but we must reload the local copies after a successful fill. }
  408. if (bytes_in_buffer = 0) then
  409. begin
  410. if (not datasrc^.fill_input_buffer(cinfo)) then
  411. begin
  412. get_sof := FALSE;
  413. exit;
  414. end;
  415. { Reload the local copies }
  416. next_input_byte := datasrc^.next_input_byte;
  417. bytes_in_buffer := datasrc^.bytes_in_buffer;
  418. end;
  419. Dec( bytes_in_buffer );
  420. compptr^.quant_tbl_no := GETJOCTET(next_input_byte^);
  421. Inc(next_input_byte);
  422. {$IFDEF DEBUG}
  423. TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOF_COMPONENT,
  424. compptr^.component_id, compptr^.h_samp_factor,
  425. compptr^.v_samp_factor, compptr^.quant_tbl_no);
  426. {$ENDIF}
  427. Inc(compptr);
  428. end;
  429. cinfo^.marker^.saw_SOF := TRUE;
  430. { Unload the local copies --- do this only at a restart boundary }
  431. datasrc^.next_input_byte := next_input_byte;
  432. datasrc^.bytes_in_buffer := bytes_in_buffer;
  433. get_sof := TRUE;
  434. end; { get_sof }
  435. {LOCAL}
  436. function get_sos (cinfo : j_decompress_ptr) : boolean;
  437. { Process a SOS marker }
  438. label
  439. id_found;
  440. var
  441. length : INT32;
  442. i, ci, n, c, cc : int;
  443. compptr : jpeg_component_info_ptr;
  444. { Declare and initialize local copies of input pointer/count }
  445. var
  446. datasrc : jpeg_source_mgr_ptr;
  447. next_input_byte : JOCTETptr; { Array[] of JOCTET; }
  448. bytes_in_buffer : size_t;
  449. begin
  450. datasrc := cinfo^.src;
  451. next_input_byte := datasrc^.next_input_byte;
  452. bytes_in_buffer := datasrc^.bytes_in_buffer;
  453. {}
  454. if not cinfo^.marker^.saw_SOF then
  455. ERREXIT(j_common_ptr(cinfo), JERR_SOS_NO_SOF);
  456. { Read two bytes interpreted as an unsigned 16-bit integer.
  457. length should be declared unsigned int or perhaps INT32. }
  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. }
  461. if (bytes_in_buffer = 0) then
  462. begin
  463. if (not datasrc^.fill_input_buffer(cinfo)) then
  464. begin
  465. get_sos := FALSE;
  466. exit;
  467. end;
  468. { Reload the local copies }
  469. next_input_byte := datasrc^.next_input_byte;
  470. bytes_in_buffer := datasrc^.bytes_in_buffer;
  471. end;
  472. Dec( bytes_in_buffer );
  473. length := (uint( GETJOCTET(next_input_byte^)) shl 8);
  474. Inc( next_input_byte );
  475. { make a byte available.
  476. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  477. but we must reload the local copies after a successful fill. }
  478. if (bytes_in_buffer = 0) then
  479. begin
  480. if (not datasrc^.fill_input_buffer(cinfo)) then
  481. begin
  482. get_sos := FALSE;
  483. exit;
  484. end;
  485. { Reload the local copies }
  486. next_input_byte := datasrc^.next_input_byte;
  487. bytes_in_buffer := datasrc^.bytes_in_buffer;
  488. end;
  489. Dec( bytes_in_buffer );
  490. Inc( length, GETJOCTET( next_input_byte^));
  491. Inc( next_input_byte );
  492. { Read a byte into variable n (Number of components).
  493. If must suspend, return FALSE. }
  494. { make a byte available.
  495. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  496. but we must reload the local copies after a successful fill. }
  497. if (bytes_in_buffer = 0) then
  498. begin
  499. if (not datasrc^.fill_input_buffer(cinfo)) then
  500. begin
  501. get_sos := FALSE;
  502. exit;
  503. end;
  504. { Reload the local copies }
  505. next_input_byte := datasrc^.next_input_byte;
  506. bytes_in_buffer := datasrc^.bytes_in_buffer;
  507. end;
  508. Dec( bytes_in_buffer );
  509. n := GETJOCTET(next_input_byte^); { Number of components }
  510. Inc(next_input_byte);
  511. {$IFDEF DEBUG}
  512. TRACEMS1(j_common_ptr(cinfo), 1, JTRC_SOS, n);
  513. {$ENDIF}
  514. if ((length <> (n * 2 + 6)) or (n < 1) or (n > MAX_COMPS_IN_SCAN)) then
  515. ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
  516. cinfo^.comps_in_scan := n;
  517. { Collect the component-spec parameters }
  518. for i := 0 to Pred(n) do
  519. begin
  520. { Read a byte into variable cc. If must suspend, return FALSE. }
  521. { make a byte available.
  522. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  523. but we must reload the local copies after a successful fill. }
  524. if (bytes_in_buffer = 0) then
  525. begin
  526. if (not datasrc^.fill_input_buffer(cinfo)) then
  527. begin
  528. get_sos := FALSE;
  529. exit;
  530. end;
  531. { Reload the local copies }
  532. next_input_byte := datasrc^.next_input_byte;
  533. bytes_in_buffer := datasrc^.bytes_in_buffer;
  534. end;
  535. Dec( bytes_in_buffer );
  536. cc := GETJOCTET(next_input_byte^);
  537. Inc(next_input_byte);
  538. { Read a byte into variable c. If must suspend, return FALSE. }
  539. { make a byte available.
  540. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  541. but we must reload the local copies after a successful fill. }
  542. if (bytes_in_buffer = 0) then
  543. begin
  544. if (not datasrc^.fill_input_buffer(cinfo)) then
  545. begin
  546. get_sos := FALSE;
  547. exit;
  548. end;
  549. { Reload the local copies }
  550. next_input_byte := datasrc^.next_input_byte;
  551. bytes_in_buffer := datasrc^.bytes_in_buffer;
  552. end;
  553. Dec( bytes_in_buffer );
  554. c := GETJOCTET(next_input_byte^);
  555. Inc(next_input_byte);
  556. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  557. for ci := 0 to Pred(cinfo^.num_components) do
  558. begin
  559. if (cc = compptr^.component_id) then
  560. goto id_found;
  561. Inc(compptr);
  562. end;
  563. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_COMPONENT_ID, cc);
  564. id_found:
  565. cinfo^.cur_comp_info[i] := compptr;
  566. compptr^.dc_tbl_no := (c shr 4) and 15;
  567. compptr^.ac_tbl_no := (c ) and 15;
  568. {$IFDEF DEBUG}
  569. TRACEMS3(j_common_ptr(cinfo), 1, JTRC_SOS_COMPONENT, cc,
  570. compptr^.dc_tbl_no, compptr^.ac_tbl_no);
  571. {$ENDIF}
  572. end;
  573. { Collect the additional scan parameters Ss, Se, Ah/Al. }
  574. { Read a byte into variable c. If must suspend, return FALSE. }
  575. { make a byte available.
  576. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  577. but we must reload the local copies after a successful fill. }
  578. if (bytes_in_buffer = 0) then
  579. begin
  580. if (not datasrc^.fill_input_buffer(cinfo)) then
  581. begin
  582. get_sos := FALSE;
  583. exit;
  584. end;
  585. { Reload the local copies }
  586. next_input_byte := datasrc^.next_input_byte;
  587. bytes_in_buffer := datasrc^.bytes_in_buffer;
  588. end;
  589. Dec( bytes_in_buffer );
  590. c := GETJOCTET(next_input_byte^);
  591. Inc(next_input_byte);
  592. cinfo^.Ss := c;
  593. { Read a byte into variable c. If must suspend, return FALSE. }
  594. { make a byte available.
  595. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  596. but we must reload the local copies after a successful fill. }
  597. if (bytes_in_buffer = 0) then
  598. begin
  599. if (not datasrc^.fill_input_buffer(cinfo)) then
  600. begin
  601. get_sos := FALSE;
  602. exit;
  603. end;
  604. { Reload the local copies }
  605. next_input_byte := datasrc^.next_input_byte;
  606. bytes_in_buffer := datasrc^.bytes_in_buffer;
  607. end;
  608. Dec( bytes_in_buffer );
  609. c := GETJOCTET(next_input_byte^);
  610. Inc(next_input_byte);
  611. cinfo^.Se := c;
  612. { Read a byte into variable c. If must suspend, return FALSE. }
  613. { make a byte available.
  614. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  615. but we must reload the local copies after a successful fill. }
  616. if (bytes_in_buffer = 0) then
  617. begin
  618. if (not datasrc^.fill_input_buffer(cinfo)) then
  619. begin
  620. get_sos := FALSE;
  621. exit;
  622. end;
  623. { Reload the local copies }
  624. next_input_byte := datasrc^.next_input_byte;
  625. bytes_in_buffer := datasrc^.bytes_in_buffer;
  626. end;
  627. Dec( bytes_in_buffer );
  628. c := GETJOCTET(next_input_byte^);
  629. Inc(next_input_byte);
  630. cinfo^.Ah := (c shr 4) and 15;
  631. cinfo^.Al := (c ) and 15;
  632. {$IFDEF DEBUG}
  633. TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOS_PARAMS, cinfo^.Ss, cinfo^.Se,
  634. cinfo^.Ah, cinfo^.Al);
  635. {$ENDIF}
  636. { Prepare to scan data & restart markers }
  637. cinfo^.marker^.next_restart_num := 0;
  638. { Count another SOS marker }
  639. Inc( cinfo^.input_scan_number );
  640. { Unload the local copies --- do this only at a restart boundary }
  641. datasrc^.next_input_byte := next_input_byte;
  642. datasrc^.bytes_in_buffer := bytes_in_buffer;
  643. get_sos := TRUE;
  644. end; { get_sos }
  645. {METHODDEF}
  646. function skip_variable (cinfo : j_decompress_ptr) : boolean;
  647. { Skip over an unknown or uninteresting variable-length marker }
  648. var
  649. length : INT32;
  650. var
  651. datasrc : jpeg_source_mgr_ptr;
  652. next_input_byte : JOCTETptr; { Array[] of JOCTET; }
  653. bytes_in_buffer : size_t;
  654. begin
  655. datasrc := cinfo^.src;
  656. next_input_byte := datasrc^.next_input_byte;
  657. bytes_in_buffer := datasrc^.bytes_in_buffer;
  658. { Read two bytes interpreted as an unsigned 16-bit integer.
  659. length should be declared unsigned int or perhaps INT32. }
  660. { make a byte available.
  661. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  662. but we must reload the local copies after a successful fill. }
  663. if (bytes_in_buffer = 0) then
  664. begin
  665. if (not datasrc^.fill_input_buffer(cinfo)) then
  666. begin
  667. skip_variable := FALSE;
  668. exit;
  669. end;
  670. { Reload the local copies }
  671. next_input_byte := datasrc^.next_input_byte;
  672. bytes_in_buffer := datasrc^.bytes_in_buffer;
  673. end;
  674. Dec( bytes_in_buffer );
  675. length := uint(GETJOCTET(next_input_byte^)) shl 8;
  676. Inc( next_input_byte );
  677. { make a byte available.
  678. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  679. but we must reload the local copies after a successful fill. }
  680. if (bytes_in_buffer = 0) then
  681. begin
  682. if (not datasrc^.fill_input_buffer(cinfo)) then
  683. begin
  684. skip_variable := FALSE;
  685. exit;
  686. end;
  687. { Reload the local copies }
  688. next_input_byte := datasrc^.next_input_byte;
  689. bytes_in_buffer := datasrc^.bytes_in_buffer;
  690. end;
  691. Dec( bytes_in_buffer );
  692. Inc( length, GETJOCTET(next_input_byte^));
  693. Inc( next_input_byte );
  694. Dec(length, 2);
  695. {$IFDEF DEBUG}
  696. TRACEMS2(j_common_ptr(cinfo), 1, JTRC_MISC_MARKER,
  697. cinfo^.unread_marker, int(length));
  698. {$ENDIF}
  699. { Unload the local copies --- do this only at a restart boundary }
  700. { do before skip_input_data }
  701. datasrc^.next_input_byte := next_input_byte;
  702. datasrc^.bytes_in_buffer := bytes_in_buffer;
  703. if (length > 0) then
  704. cinfo^.src^.skip_input_data(cinfo, long(length));
  705. skip_variable := TRUE;
  706. end; { skip_variable }
  707. {$IFDEF D_ARITH_CODING_SUPPORTED}
  708. {LOCAL}
  709. function get_dac (cinfo : j_decompress_ptr) : boolean;
  710. { Process a DAC marker }
  711. var
  712. length : INT32;
  713. index, val : int;
  714. var
  715. datasrc : jpeg_source_mgr_ptr;
  716. next_input_byte : JOCTETptr;
  717. bytes_in_buffer : size_t;
  718. begin
  719. datasrc := cinfo^.src;
  720. next_input_byte := datasrc^.next_input_byte;
  721. bytes_in_buffer := datasrc^.bytes_in_buffer;
  722. { Read two bytes interpreted as an unsigned 16-bit integer.
  723. length should be declared unsigned int or perhaps INT32. }
  724. { make a byte available.
  725. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  726. but we must reload the local copies after a successful fill. }
  727. if (bytes_in_buffer = 0) then
  728. begin
  729. if (not datasrc^.fill_input_buffer(cinfo)) then
  730. begin
  731. get_dac := FALSE;
  732. exit;
  733. end;
  734. { Reload the local copies }
  735. next_input_byte := datasrc^.next_input_byte;
  736. bytes_in_buffer := datasrc^.bytes_in_buffer;
  737. end;
  738. Dec( bytes_in_buffer );
  739. length := (uint( GETJOCTET(next_input_byte^)) shl 8);
  740. Inc( next_input_byte );
  741. { make a byte available.
  742. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  743. but we must reload the local copies after a successful fill. }
  744. if (bytes_in_buffer = 0) then
  745. begin
  746. if (not datasrc^.fill_input_buffer(cinfo)) then
  747. begin
  748. get_dac := FALSE;
  749. exit;
  750. end;
  751. { Reload the local copies }
  752. next_input_byte := datasrc^.next_input_byte;
  753. bytes_in_buffer := datasrc^.bytes_in_buffer;
  754. end;
  755. Dec( bytes_in_buffer );
  756. Inc( length, GETJOCTET( next_input_byte^));
  757. Inc( next_input_byte );
  758. Dec(length, 2);
  759. while (length > 0) do
  760. begin
  761. { Read a byte into variable index. If must suspend, return FALSE. }
  762. { make a byte available.
  763. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  764. but we must reload the local copies after a successful fill. }
  765. if (bytes_in_buffer = 0) then
  766. begin
  767. if (not datasrc^.fill_input_buffer(cinfo)) then
  768. begin
  769. get_dac := FALSE;
  770. exit;
  771. end;
  772. { Reload the local copies }
  773. next_input_byte := datasrc^.next_input_byte;
  774. bytes_in_buffer := datasrc^.bytes_in_buffer;
  775. end;
  776. Dec( bytes_in_buffer );
  777. index := GETJOCTET(next_input_byte^);
  778. Inc(next_input_byte);
  779. { Read a byte into variable val. If must suspend, return FALSE. }
  780. { make a byte available.
  781. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  782. but we must reload the local copies after a successful fill. }
  783. if (bytes_in_buffer = 0) then
  784. begin
  785. if (not datasrc^.fill_input_buffer(cinfo)) then
  786. begin
  787. get_dac := FALSE;
  788. exit;
  789. end;
  790. { Reload the local copies }
  791. next_input_byte := datasrc^.next_input_byte;
  792. bytes_in_buffer := datasrc^.bytes_in_buffer;
  793. end;
  794. Dec( bytes_in_buffer );
  795. val := GETJOCTET(next_input_byte^);
  796. Inc(next_input_byte);
  797. Dec( length, 2);
  798. {$IFDEF DEBUG}
  799. TRACEMS2(j_common_ptr(cinfo), 1, JTRC_DAC, index, val);
  800. {$ENDIF}
  801. if (index < 0) or (index >= (2*NUM_ARITH_TBLS)) then
  802. ERREXIT1(j_common_ptr(cinfo) , JERR_DAC_INDEX, index);
  803. if (index >= NUM_ARITH_TBLS) then
  804. begin { define AC table }
  805. cinfo^.arith_ac_K[index-NUM_ARITH_TBLS] := UINT8(val);
  806. end
  807. else
  808. begin { define DC table }
  809. cinfo^.arith_dc_L[index] := UINT8(val and $0F);
  810. cinfo^.arith_dc_U[index] := UINT8(val shr 4);
  811. if (cinfo^.arith_dc_L[index] > cinfo^.arith_dc_U[index]) then
  812. ERREXIT1(j_common_ptr(cinfo) , JERR_DAC_VALUE, val);
  813. end;
  814. end;
  815. if (length <> 0) then
  816. ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
  817. { Unload the local copies --- do this only at a restart boundary }
  818. datasrc^.next_input_byte := next_input_byte;
  819. datasrc^.bytes_in_buffer := bytes_in_buffer;
  820. get_dac := TRUE;
  821. end; { get_dac }
  822. {$ELSE}
  823. {LOCAL}
  824. function get_dac (cinfo : j_decompress_ptr) : boolean;
  825. begin
  826. get_dac := skip_variable(cinfo);
  827. end;
  828. {$ENDIF}
  829. {LOCAL}
  830. function get_dht (cinfo : j_decompress_ptr) : boolean;
  831. { Process a DHT marker }
  832. var
  833. length : INT32;
  834. bits : Array[0..17-1] of UINT8;
  835. huffval : Array[0..256-1] of UINT8;
  836. i, index, count : int;
  837. htblptr : ^JHUFF_TBL_PTR;
  838. var
  839. datasrc : jpeg_source_mgr_ptr;
  840. next_input_byte : JOCTETptr;
  841. bytes_in_buffer : size_t;
  842. begin
  843. datasrc := cinfo^.src;
  844. next_input_byte := datasrc^.next_input_byte;
  845. bytes_in_buffer := datasrc^.bytes_in_buffer;
  846. { Read two bytes interpreted as an unsigned 16-bit integer.
  847. length should be declared unsigned int or perhaps INT32. }
  848. { make a byte available.
  849. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  850. but we must reload the local copies after a successful fill. }
  851. if (bytes_in_buffer = 0) then
  852. begin
  853. if (not datasrc^.fill_input_buffer(cinfo)) then
  854. begin
  855. get_dht := FALSE;
  856. exit;
  857. end;
  858. { Reload the local copies }
  859. next_input_byte := datasrc^.next_input_byte;
  860. bytes_in_buffer := datasrc^.bytes_in_buffer;
  861. end;
  862. Dec( bytes_in_buffer );
  863. length := (uint( GETJOCTET(next_input_byte^)) shl 8);
  864. Inc( next_input_byte );
  865. { make a byte available.
  866. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  867. but we must reload the local copies after a successful fill. }
  868. if (bytes_in_buffer = 0) then
  869. begin
  870. if (not datasrc^.fill_input_buffer(cinfo)) then
  871. begin
  872. get_dht := FALSE;
  873. exit;
  874. end;
  875. { Reload the local copies }
  876. next_input_byte := datasrc^.next_input_byte;
  877. bytes_in_buffer := datasrc^.bytes_in_buffer;
  878. end;
  879. Dec( bytes_in_buffer );
  880. Inc( length, GETJOCTET( next_input_byte^));
  881. Inc( next_input_byte );
  882. Dec(length, 2);
  883. while (length > 16) do
  884. begin
  885. { Read a byte into variable index. If must suspend, return FALSE. }
  886. { make a byte available.
  887. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  888. but we must reload the local copies after a successful fill. }
  889. if (bytes_in_buffer = 0) then
  890. begin
  891. if (not datasrc^.fill_input_buffer(cinfo)) then
  892. begin
  893. get_dht := FALSE;
  894. exit;
  895. end;
  896. { Reload the local copies }
  897. next_input_byte := datasrc^.next_input_byte;
  898. bytes_in_buffer := datasrc^.bytes_in_buffer;
  899. end;
  900. Dec( bytes_in_buffer );
  901. index := GETJOCTET(next_input_byte^);
  902. Inc(next_input_byte);
  903. {$IFDEF DEBUG}
  904. TRACEMS1(j_common_ptr(cinfo), 1, JTRC_DHT, index);
  905. {$ENDIF}
  906. bits[0] := 0;
  907. count := 0;
  908. for i := 1 to 16 do
  909. begin
  910. { Read a byte into variable bits[i]. If must suspend, return FALSE. }
  911. { make a byte available.
  912. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  913. but we must reload the local copies after a successful fill. }
  914. if (bytes_in_buffer = 0) then
  915. begin
  916. if (not datasrc^.fill_input_buffer(cinfo)) then
  917. begin
  918. get_dht := FALSE;
  919. exit;
  920. end;
  921. { Reload the local copies }
  922. next_input_byte := datasrc^.next_input_byte;
  923. bytes_in_buffer := datasrc^.bytes_in_buffer;
  924. end;
  925. Dec( bytes_in_buffer );
  926. bits[i] := GETJOCTET(next_input_byte^);
  927. Inc(next_input_byte);
  928. Inc( count, bits[i] );
  929. end;
  930. Dec( length, (1 + 16) );
  931. {$IFDEF DEBUG}
  932. TRACEMS8(j_common_ptr(cinfo), 2, JTRC_HUFFBITS,
  933. bits[1], bits[2], bits[3], bits[4],
  934. bits[5], bits[6], bits[7], bits[8]);
  935. TRACEMS8(j_common_ptr(cinfo), 2, JTRC_HUFFBITS,
  936. bits[9], bits[10], bits[11], bits[12],
  937. bits[13], bits[14], bits[15], bits[16]);
  938. {$ENDIF}
  939. { Here we just do minimal validation of the counts to avoid walking
  940. off the end of our table space. jdhuff.c will check more carefully. }
  941. if (count > 256) or (INT32(count) > length) then
  942. ERREXIT(j_common_ptr(cinfo), JERR_BAD_HUFF_TABLE);
  943. for i := 0 to Pred(count) do
  944. begin
  945. { Read a byte into variable huffval[i]. If must suspend, return FALSE. }
  946. { make a byte available.
  947. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  948. but we must reload the local copies after a successful fill. }
  949. if (bytes_in_buffer = 0) then
  950. begin
  951. if (not datasrc^.fill_input_buffer(cinfo)) then
  952. begin
  953. get_dht := FALSE;
  954. exit;
  955. end;
  956. { Reload the local copies }
  957. next_input_byte := datasrc^.next_input_byte;
  958. bytes_in_buffer := datasrc^.bytes_in_buffer;
  959. end;
  960. Dec( bytes_in_buffer );
  961. huffval[i] := GETJOCTET(next_input_byte^);
  962. Inc(next_input_byte);
  963. end;
  964. Dec( length, count );
  965. if (index and $10)<>0 then
  966. begin { AC table definition }
  967. Dec( index, $10 );
  968. htblptr := @cinfo^.ac_huff_tbl_ptrs[index];
  969. end
  970. else
  971. begin { DC table definition }
  972. htblptr := @cinfo^.dc_huff_tbl_ptrs[index];
  973. end;
  974. if (index < 0) or (index >= NUM_HUFF_TBLS) then
  975. ERREXIT1(j_common_ptr(cinfo), JERR_DHT_INDEX, index);
  976. if (htblptr^ = NIL) then
  977. htblptr^ := jpeg_alloc_huff_table(j_common_ptr(cinfo));
  978. MEMCOPY(@(htblptr^)^.bits, @bits, SIZEOF((htblptr^)^.bits));
  979. MEMCOPY(@(htblptr^)^.huffval, @huffval, SIZEOF((htblptr^)^.huffval));
  980. end;
  981. if (length <> 0) then
  982. ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
  983. { Unload the local copies --- do this only at a restart boundary }
  984. datasrc^.next_input_byte := next_input_byte;
  985. datasrc^.bytes_in_buffer := bytes_in_buffer;
  986. get_dht := TRUE;
  987. end; { get_dht }
  988. {LOCAL}
  989. function get_dqt (cinfo : j_decompress_ptr) : boolean;
  990. { Process a DQT marker }
  991. var
  992. length : INT32;
  993. n, i, prec : int;
  994. tmp : uint;
  995. quant_ptr : JQUANT_TBL_PTR;
  996. var
  997. datasrc : jpeg_source_mgr_ptr;
  998. next_input_byte : JOCTETptr;
  999. bytes_in_buffer : size_t;
  1000. begin
  1001. datasrc := cinfo^.src;
  1002. next_input_byte := datasrc^.next_input_byte;
  1003. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1004. { Read two bytes interpreted as an unsigned 16-bit integer.
  1005. length should be declared unsigned int or perhaps INT32. }
  1006. { make a byte available.
  1007. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1008. but we must reload the local copies after a successful fill. }
  1009. if (bytes_in_buffer = 0) then
  1010. begin
  1011. if (not datasrc^.fill_input_buffer(cinfo)) then
  1012. begin
  1013. get_dqt := FALSE;
  1014. exit;
  1015. end;
  1016. { Reload the local copies }
  1017. next_input_byte := datasrc^.next_input_byte;
  1018. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1019. end;
  1020. Dec( bytes_in_buffer );
  1021. length := (uint( GETJOCTET(next_input_byte^)) shl 8);
  1022. Inc( next_input_byte );
  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. }
  1026. if (bytes_in_buffer = 0) then
  1027. begin
  1028. if (not datasrc^.fill_input_buffer(cinfo)) then
  1029. begin
  1030. get_dqt := FALSE;
  1031. exit;
  1032. end;
  1033. { Reload the local copies }
  1034. next_input_byte := datasrc^.next_input_byte;
  1035. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1036. end;
  1037. Dec( bytes_in_buffer );
  1038. Inc( length, GETJOCTET( next_input_byte^));
  1039. Inc( next_input_byte );
  1040. Dec( length, 2 );
  1041. while (length > 0) do
  1042. begin
  1043. { Read a byte into variable n. If must suspend, return FALSE. }
  1044. { make a byte available.
  1045. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1046. but we must reload the local copies after a successful fill. }
  1047. if (bytes_in_buffer = 0) then
  1048. begin
  1049. if (not datasrc^.fill_input_buffer(cinfo)) then
  1050. begin
  1051. get_dqt := FALSE;
  1052. exit;
  1053. end;
  1054. { Reload the local copies }
  1055. next_input_byte := datasrc^.next_input_byte;
  1056. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1057. end;
  1058. Dec( bytes_in_buffer );
  1059. n := GETJOCTET(next_input_byte^);
  1060. Inc(next_input_byte);
  1061. prec := n shr 4;
  1062. n := n and $0F;
  1063. {$IFDEF DEBUG}
  1064. TRACEMS2(j_common_ptr(cinfo), 1, JTRC_DQT, n, prec);
  1065. {$ENDIF}
  1066. if (n >= NUM_QUANT_TBLS) then
  1067. ERREXIT1(j_common_ptr(cinfo) , JERR_DQT_INDEX, n);
  1068. if (cinfo^.quant_tbl_ptrs[n] = NIL) then
  1069. cinfo^.quant_tbl_ptrs[n] := jpeg_alloc_quant_table(j_common_ptr(cinfo));
  1070. quant_ptr := cinfo^.quant_tbl_ptrs[n];
  1071. for i := 0 to Pred(DCTSIZE2) do
  1072. begin
  1073. if (prec <> 0) then
  1074. begin
  1075. { Read two bytes interpreted as an unsigned 16-bit integer.
  1076. tmp should be declared unsigned int or perhaps INT32. }
  1077. { make a byte available.
  1078. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1079. but we must reload the local copies after a successful fill. }
  1080. if (bytes_in_buffer = 0) then
  1081. begin
  1082. if (not datasrc^.fill_input_buffer(cinfo)) then
  1083. begin
  1084. get_dqt := FALSE;
  1085. exit;
  1086. end;
  1087. { Reload the local copies }
  1088. next_input_byte := datasrc^.next_input_byte;
  1089. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1090. end;
  1091. Dec( bytes_in_buffer );
  1092. tmp := (uint( GETJOCTET(next_input_byte^)) shl 8);
  1093. Inc( next_input_byte );
  1094. { make a byte available.
  1095. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1096. but we must reload the local copies after a successful fill. }
  1097. if (bytes_in_buffer = 0) then
  1098. begin
  1099. if (not datasrc^.fill_input_buffer(cinfo)) then
  1100. begin
  1101. get_dqt := FALSE;
  1102. exit;
  1103. end;
  1104. { Reload the local copies }
  1105. next_input_byte := datasrc^.next_input_byte;
  1106. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1107. end;
  1108. Dec( bytes_in_buffer );
  1109. Inc( tmp, GETJOCTET( next_input_byte^));
  1110. Inc( next_input_byte );
  1111. end
  1112. else
  1113. begin
  1114. { Read a byte into variable tmp. If must suspend, return FALSE. }
  1115. { make a byte available.
  1116. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1117. but we must reload the local copies after a successful fill. }
  1118. if (bytes_in_buffer = 0) then
  1119. begin
  1120. if (not datasrc^.fill_input_buffer(cinfo)) then
  1121. begin
  1122. get_dqt := FALSE;
  1123. exit;
  1124. end;
  1125. { Reload the local copies }
  1126. next_input_byte := datasrc^.next_input_byte;
  1127. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1128. end;
  1129. Dec( bytes_in_buffer );
  1130. tmp := GETJOCTET(next_input_byte^);
  1131. Inc(next_input_byte);
  1132. end;
  1133. { We convert the zigzag-order table to natural array order. }
  1134. quant_ptr^.quantval[jpeg_natural_order[i]] := UINT16(tmp);
  1135. end;
  1136. if (cinfo^.err^.trace_level >= 2) then
  1137. begin
  1138. i := 0;
  1139. while i < Pred(DCTSIZE2) do
  1140. begin
  1141. {$IFDEF DEBUG}
  1142. TRACEMS8(j_common_ptr(cinfo), 2, JTRC_QUANTVALS,
  1143. quant_ptr^.quantval[i], quant_ptr^.quantval[i+1],
  1144. quant_ptr^.quantval[i+2], quant_ptr^.quantval[i+3],
  1145. quant_ptr^.quantval[i+4], quant_ptr^.quantval[i+5],
  1146. quant_ptr^.quantval[i+6], quant_ptr^.quantval[i+7]);
  1147. {$ENDIF}
  1148. Inc(i, 8);
  1149. end;
  1150. end;
  1151. Dec( length, DCTSIZE2+1 );
  1152. if (prec <> 0) then
  1153. Dec( length, DCTSIZE2 );
  1154. end;
  1155. if (length <> 0) then
  1156. ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
  1157. { Unload the local copies --- do this only at a restart boundary }
  1158. datasrc^.next_input_byte := next_input_byte;
  1159. datasrc^.bytes_in_buffer := bytes_in_buffer;
  1160. get_dqt := TRUE;
  1161. end; { get_dqt }
  1162. {LOCAL}
  1163. function get_dri (cinfo : j_decompress_ptr) : boolean;
  1164. { Process a DRI marker }
  1165. var
  1166. length : INT32;
  1167. tmp : uint;
  1168. var
  1169. datasrc : jpeg_source_mgr_ptr;
  1170. next_input_byte : JOCTETptr;
  1171. bytes_in_buffer : size_t;
  1172. begin
  1173. datasrc := cinfo^.src;
  1174. next_input_byte := datasrc^.next_input_byte;
  1175. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1176. { Read two bytes interpreted as an unsigned 16-bit integer.
  1177. length should be declared unsigned int or perhaps INT32. }
  1178. { make a byte available.
  1179. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1180. but we must reload the local copies after a successful fill. }
  1181. if (bytes_in_buffer = 0) then
  1182. begin
  1183. if (not datasrc^.fill_input_buffer(cinfo)) then
  1184. begin
  1185. get_dri := FALSE;
  1186. exit;
  1187. end;
  1188. { Reload the local copies }
  1189. next_input_byte := datasrc^.next_input_byte;
  1190. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1191. end;
  1192. Dec( bytes_in_buffer );
  1193. length := (uint( GETJOCTET(next_input_byte^)) shl 8);
  1194. Inc( next_input_byte );
  1195. { make a byte available.
  1196. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1197. but we must reload the local copies after a successful fill. }
  1198. if (bytes_in_buffer = 0) then
  1199. begin
  1200. if (not datasrc^.fill_input_buffer(cinfo)) then
  1201. begin
  1202. get_dri := FALSE;
  1203. exit;
  1204. end;
  1205. { Reload the local copies }
  1206. next_input_byte := datasrc^.next_input_byte;
  1207. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1208. end;
  1209. Dec( bytes_in_buffer );
  1210. Inc( length, GETJOCTET( next_input_byte^));
  1211. Inc( next_input_byte );
  1212. if (length <> 4) then
  1213. ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
  1214. { Read two bytes interpreted as an unsigned 16-bit integer.
  1215. tmp should be declared unsigned int or perhaps INT32. }
  1216. { make a byte available.
  1217. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1218. but we must reload the local copies after a successful fill. }
  1219. if (bytes_in_buffer = 0) then
  1220. begin
  1221. if (not datasrc^.fill_input_buffer(cinfo)) then
  1222. begin
  1223. get_dri := FALSE;
  1224. exit;
  1225. end;
  1226. { Reload the local copies }
  1227. next_input_byte := datasrc^.next_input_byte;
  1228. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1229. end;
  1230. Dec( bytes_in_buffer );
  1231. tmp := (uint( GETJOCTET(next_input_byte^)) shl 8);
  1232. Inc( next_input_byte );
  1233. { make a byte available.
  1234. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1235. but we must reload the local copies after a successful fill. }
  1236. if (bytes_in_buffer = 0) then
  1237. begin
  1238. if (not datasrc^.fill_input_buffer(cinfo)) then
  1239. begin
  1240. get_dri := FALSE;
  1241. exit;
  1242. end;
  1243. { Reload the local copies }
  1244. next_input_byte := datasrc^.next_input_byte;
  1245. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1246. end;
  1247. Dec( bytes_in_buffer );
  1248. Inc( tmp, GETJOCTET( next_input_byte^));
  1249. Inc( next_input_byte );
  1250. {$IFDEF DEBUG}
  1251. TRACEMS1(j_common_ptr(cinfo), 1, JTRC_DRI, tmp);
  1252. {$ENDIF}
  1253. cinfo^.restart_interval := tmp;
  1254. { Unload the local copies --- do this only at a restart boundary }
  1255. datasrc^.next_input_byte := next_input_byte;
  1256. datasrc^.bytes_in_buffer := bytes_in_buffer;
  1257. get_dri := TRUE;
  1258. end; { get_dri }
  1259. { Routines for processing APPn and COM markers.
  1260. These are either saved in memory or discarded, per application request.
  1261. APP0 and APP14 are specially checked to see if they are
  1262. JFIF and Adobe markers, respectively. }
  1263. const
  1264. APP0_DATA_LEN = 14; { Length of interesting data in APP0 }
  1265. APP14_DATA_LEN = 12; { Length of interesting data in APP14 }
  1266. APPN_DATA_LEN = 14; { Must be the largest of the above!! }
  1267. {LOCAL}
  1268. procedure examine_app0 (cinfo : j_decompress_ptr;
  1269. var data : array of JOCTET;
  1270. datalen : uint;
  1271. remaining : INT32);
  1272. { Examine first few bytes from an APP0.
  1273. Take appropriate action if it is a JFIF marker.
  1274. datalen is # of bytes at data[], remaining is length of rest of marker data.
  1275. }
  1276. {$IFDEF DEBUG}
  1277. var
  1278. totallen : INT32;
  1279. {$ENDIF}
  1280. begin
  1281. {$IFDEF DEBUG}
  1282. totallen := INT32(datalen) + remaining;
  1283. {$ENDIF}
  1284. if (datalen >= APP0_DATA_LEN) and
  1285. (GETJOCTET(data[0]) = $4A) and
  1286. (GETJOCTET(data[1]) = $46) and
  1287. (GETJOCTET(data[2]) = $49) and
  1288. (GETJOCTET(data[3]) = $46) and
  1289. (GETJOCTET(data[4]) = 0) then
  1290. begin
  1291. { Found JFIF APP0 marker: save info }
  1292. cinfo^.saw_JFIF_marker := TRUE;
  1293. cinfo^.JFIF_major_version := GETJOCTET(data[5]);
  1294. cinfo^.JFIF_minor_version := GETJOCTET(data[6]);
  1295. cinfo^.density_unit := GETJOCTET(data[7]);
  1296. cinfo^.X_density := (GETJOCTET(data[8]) shl 8) + GETJOCTET(data[9]);
  1297. cinfo^.Y_density := (GETJOCTET(data[10]) shl 8) + GETJOCTET(data[11]);
  1298. { Check version.
  1299. Major version must be 1, anything else signals an incompatible change.
  1300. (We used to treat this as an error, but now it's a nonfatal warning,
  1301. because some bozo at Hijaak couldn't read the spec.)
  1302. Minor version should be 0..2, but process anyway if newer. }
  1303. if (cinfo^.JFIF_major_version <> 1) then
  1304. WARNMS2(j_common_ptr(cinfo), JWRN_JFIF_MAJOR,
  1305. cinfo^.JFIF_major_version, cinfo^.JFIF_minor_version);
  1306. { Generate trace messages }
  1307. {$IFDEF DEBUG}
  1308. TRACEMS5(j_common_ptr(cinfo), 1, JTRC_JFIF,
  1309. cinfo^.JFIF_major_version, cinfo^.JFIF_minor_version,
  1310. cinfo^.X_density, cinfo^.Y_density, cinfo^.density_unit);
  1311. { Validate thumbnail dimensions and issue appropriate messages }
  1312. if (GETJOCTET(data[12]) or GETJOCTET(data[13])) <> 0 then
  1313. TRACEMS2(j_common_ptr(cinfo), 1, JTRC_JFIF_THUMBNAIL,
  1314. GETJOCTET(data[12]), GETJOCTET(data[13]));
  1315. Dec(totallen, APP0_DATA_LEN);
  1316. if (totallen <>
  1317. ( INT32(GETJOCTET(data[12])) * INT32(GETJOCTET(data[13])) * INT32(3) )) then
  1318. TRACEMS1(j_common_ptr(cinfo), 1, JTRC_JFIF_BADTHUMBNAILSIZE, int(totallen));
  1319. {$ENDIF}
  1320. end
  1321. else
  1322. if (datalen >= 6) and
  1323. (GETJOCTET(data[0]) = $4A) and
  1324. (GETJOCTET(data[1]) = $46) and
  1325. (GETJOCTET(data[2]) = $58) and
  1326. (GETJOCTET(data[3]) = $58) and
  1327. (GETJOCTET(data[4]) = 0) then
  1328. begin
  1329. { Found JFIF "JFXX" extension APP0 marker }
  1330. { The library doesn't actually do anything with these,
  1331. but we try to produce a helpful trace message. }
  1332. {$IFDEF DEBUG}
  1333. case (GETJOCTET(data[5])) of
  1334. $10:
  1335. TRACEMS1(j_common_ptr(cinfo), 1, JTRC_THUMB_JPEG, int(totallen));
  1336. $11:
  1337. TRACEMS1(j_common_ptr(cinfo), 1, JTRC_THUMB_PALETTE, int(totallen));
  1338. $13:
  1339. TRACEMS1(j_common_ptr(cinfo), 1, JTRC_THUMB_RGB, int(totallen));
  1340. else
  1341. TRACEMS2(j_common_ptr(cinfo), 1, JTRC_JFIF_EXTENSION,
  1342. GETJOCTET(data[5]), int(totallen));
  1343. end;
  1344. {$ENDIF}
  1345. end
  1346. else
  1347. begin
  1348. { Start of APP0 does not match "JFIF" or "JFXX", or too short }
  1349. {$IFDEF DEBUG}
  1350. TRACEMS1(j_common_ptr(cinfo), 1, JTRC_APP0, int(totallen));
  1351. {$ENDIF}
  1352. end;
  1353. end;
  1354. {LOCAL}
  1355. procedure examine_app14 (cinfo : j_decompress_ptr;
  1356. var data : array of JOCTET;
  1357. datalen : uint;
  1358. remaining : INT32);
  1359. { Examine first few bytes from an APP14.
  1360. Take appropriate action if it is an Adobe marker.
  1361. datalen is # of bytes at data[], remaining is length of rest of marker data.
  1362. }
  1363. var
  1364. {$IFDEF DEBUG}
  1365. version, flags0, flags1,
  1366. {$ENDIF}
  1367. transform : uint;
  1368. begin
  1369. if (datalen >= APP14_DATA_LEN) and
  1370. (GETJOCTET(data[0]) = $41) and
  1371. (GETJOCTET(data[1]) = $64) and
  1372. (GETJOCTET(data[2]) = $6F) and
  1373. (GETJOCTET(data[3]) = $62) and
  1374. (GETJOCTET(data[4]) = $65) then
  1375. begin
  1376. { Found Adobe APP14 marker }
  1377. {$IFDEF DEBUG}
  1378. version := (GETJOCTET(data[5]) shl 8) + GETJOCTET(data[6]);
  1379. flags0 := (GETJOCTET(data[7]) shl 8) + GETJOCTET(data[8]);
  1380. flags1 := (GETJOCTET(data[9]) shl 8) + GETJOCTET(data[10]);
  1381. {$ENDIF}
  1382. transform := GETJOCTET(data[11]);
  1383. {$IFDEF DEBUG}
  1384. TRACEMS4(j_common_ptr(cinfo), 1, JTRC_ADOBE, version, flags0, flags1, transform);
  1385. {$ENDIF}
  1386. cinfo^.saw_Adobe_marker := TRUE;
  1387. cinfo^.Adobe_transform := UINT8 (transform);
  1388. end
  1389. else
  1390. begin
  1391. { Start of APP14 does not match "Adobe", or too short }
  1392. {$IFDEF DEBUG}
  1393. TRACEMS1(j_common_ptr(cinfo), 1, JTRC_APP14, int (datalen + remaining));
  1394. {$ENDIF}
  1395. end;
  1396. end;
  1397. {METHODDEF}
  1398. function get_interesting_appn (cinfo : j_decompress_ptr) : boolean;
  1399. { Process an APP0 or APP14 marker without saving it }
  1400. var
  1401. length : INT32;
  1402. b : array[0..APPN_DATA_LEN-1] of JOCTET;
  1403. i, numtoread: uint;
  1404. var
  1405. datasrc : jpeg_source_mgr_ptr;
  1406. next_input_byte : JOCTETptr;
  1407. bytes_in_buffer : size_t;
  1408. begin
  1409. datasrc := cinfo^.src;
  1410. next_input_byte := datasrc^.next_input_byte;
  1411. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1412. { Read two bytes interpreted as an unsigned 16-bit integer.
  1413. length should be declared unsigned int or perhaps INT32. }
  1414. { make a byte available.
  1415. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1416. but we must reload the local copies after a successful fill. }
  1417. if (bytes_in_buffer = 0) then
  1418. begin
  1419. if (not datasrc^.fill_input_buffer(cinfo)) then
  1420. begin
  1421. get_interesting_appn := FALSE;
  1422. exit;
  1423. end;
  1424. { Reload the local copies }
  1425. next_input_byte := datasrc^.next_input_byte;
  1426. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1427. end;
  1428. Dec( bytes_in_buffer );
  1429. length := (uint( GETJOCTET(next_input_byte^)) shl 8);
  1430. Inc( next_input_byte );
  1431. { make a byte available.
  1432. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1433. but we must reload the local copies after a successful fill. }
  1434. if (bytes_in_buffer = 0) then
  1435. begin
  1436. if (not datasrc^.fill_input_buffer(cinfo)) then
  1437. begin
  1438. get_interesting_appn := FALSE;
  1439. exit;
  1440. end;
  1441. { Reload the local copies }
  1442. next_input_byte := datasrc^.next_input_byte;
  1443. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1444. end;
  1445. Dec( bytes_in_buffer );
  1446. Inc( length, GETJOCTET(next_input_byte^));
  1447. Inc( next_input_byte );
  1448. Dec(length, 2);
  1449. { get the interesting part of the marker data }
  1450. if (length >= APPN_DATA_LEN) then
  1451. numtoread := APPN_DATA_LEN
  1452. else
  1453. if (length > 0) then
  1454. numtoread := uint(length)
  1455. else
  1456. numtoread := 0;
  1457. if numtoread > 0 then
  1458. begin
  1459. for i := 0 to numtoread-1 do
  1460. begin
  1461. { Read a byte into b[i]. If must suspend, return FALSE. }
  1462. { make a byte available.
  1463. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1464. but we must reload the local copies after a successful fill. }
  1465. if (bytes_in_buffer = 0) then
  1466. begin
  1467. if (not datasrc^.fill_input_buffer(cinfo)) then
  1468. begin
  1469. get_interesting_appn := FALSE;
  1470. exit;
  1471. end;
  1472. { Reload the local copies }
  1473. next_input_byte := datasrc^.next_input_byte;
  1474. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1475. end;
  1476. Dec( bytes_in_buffer );
  1477. b[i] := GETJOCTET(next_input_byte^);
  1478. Inc(next_input_byte);
  1479. end;
  1480. end;
  1481. Dec(length, numtoread);
  1482. { process it }
  1483. case (cinfo^.unread_marker) of
  1484. M_APP0:
  1485. examine_app0(cinfo, b, numtoread, length);
  1486. M_APP14:
  1487. examine_app14(cinfo, b, numtoread, length);
  1488. else
  1489. { can't get here unless jpeg_save_markers chooses wrong processor }
  1490. ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, cinfo^.unread_marker);
  1491. end;
  1492. { skip any remaining data -- could be lots }
  1493. { Unload the local copies --- do this only at a restart boundary }
  1494. datasrc^.next_input_byte := next_input_byte;
  1495. datasrc^.bytes_in_buffer := bytes_in_buffer;
  1496. if (length > 0) then
  1497. cinfo^.src^.skip_input_data(cinfo, long(length));
  1498. get_interesting_appn := TRUE;
  1499. end;
  1500. {$ifdef SAVE_MARKERS_SUPPORTED}
  1501. {METHODDEF}
  1502. function save_marker (cinfo : j_decompress_ptr) : boolean;
  1503. { Save an APPn or COM marker into the marker list }
  1504. var
  1505. marker : my_marker_ptr;
  1506. cur_marker : jpeg_saved_marker_ptr;
  1507. bytes_read, data_length : uint;
  1508. data : JOCTET_FIELD_PTR;
  1509. length : INT32;
  1510. var
  1511. datasrc : jpeg_source_mgr_ptr;
  1512. next_input_byte : JOCTETptr;
  1513. bytes_in_buffer : size_t;
  1514. var
  1515. limit : uint;
  1516. var
  1517. prev : jpeg_saved_marker_ptr;
  1518. begin
  1519. { local copies of input pointer/count }
  1520. datasrc := cinfo^.src;
  1521. next_input_byte := datasrc^.next_input_byte;
  1522. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1523. marker := my_marker_ptr(cinfo^.marker);
  1524. cur_marker := marker^.cur_marker;
  1525. length := 0;
  1526. if (cur_marker = NIL) then
  1527. begin
  1528. { begin reading a marker }
  1529. { Read two bytes interpreted as an unsigned 16-bit integer. }
  1530. { make a byte available.
  1531. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1532. but we must reload the local copies after a successful fill. }
  1533. if (bytes_in_buffer = 0) then
  1534. begin
  1535. if (not datasrc^.fill_input_buffer(cinfo)) then
  1536. begin
  1537. save_marker := FALSE;
  1538. exit;
  1539. end;
  1540. { Reload the local copies }
  1541. next_input_byte := datasrc^.next_input_byte;
  1542. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1543. end;
  1544. Dec( bytes_in_buffer );
  1545. length := (uint( GETJOCTET(next_input_byte^)) shl 8);
  1546. Inc( next_input_byte );
  1547. { make a byte available.
  1548. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1549. but we must reload the local copies after a successful fill. }
  1550. if (bytes_in_buffer = 0) then
  1551. begin
  1552. if (not datasrc^.fill_input_buffer(cinfo)) then
  1553. begin
  1554. save_marker := FALSE;
  1555. exit;
  1556. end;
  1557. { Reload the local copies }
  1558. next_input_byte := datasrc^.next_input_byte;
  1559. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1560. end;
  1561. Dec( bytes_in_buffer );
  1562. Inc( length, GETJOCTET(next_input_byte^));
  1563. Inc( next_input_byte );
  1564. Dec(length, 2);
  1565. if (length >= 0) then
  1566. begin { watch out for bogus length word }
  1567. { figure out how much we want to save }
  1568. if (cinfo^.unread_marker = int(M_COM)) then
  1569. limit := marker^.length_limit_COM
  1570. else
  1571. limit := marker^.length_limit_APPn[cinfo^.unread_marker - int(M_APP0)];
  1572. if (uint(length) < limit) then
  1573. limit := uint(length);
  1574. { allocate and initialize the marker item }
  1575. cur_marker := jpeg_saved_marker_ptr(
  1576. cinfo^.mem^.alloc_large (j_common_ptr(cinfo), JPOOL_IMAGE,
  1577. SIZEOF(jpeg_marker_struct) + limit) );
  1578. cur_marker^.next := NIL;
  1579. cur_marker^.marker := UINT8 (cinfo^.unread_marker);
  1580. cur_marker^.original_length := uint(length);
  1581. cur_marker^.data_length := limit;
  1582. { data area is just beyond the jpeg_marker_struct }
  1583. cur_marker^.data := JOCTET_FIELD_PTR(cur_marker);
  1584. Inc(jpeg_saved_marker_ptr(cur_marker^.data));
  1585. data := cur_marker^.data;
  1586. marker^.cur_marker := cur_marker;
  1587. marker^.bytes_read := 0;
  1588. bytes_read := 0;
  1589. data_length := limit;
  1590. end
  1591. else
  1592. begin
  1593. { deal with bogus length word }
  1594. data_length := 0;
  1595. bytes_read := 0;
  1596. data := NIL;
  1597. end
  1598. end
  1599. else
  1600. begin
  1601. { resume reading a marker }
  1602. bytes_read := marker^.bytes_read;
  1603. data_length := cur_marker^.data_length;
  1604. data := cur_marker^.data;
  1605. Inc(data, bytes_read);
  1606. end;
  1607. while (bytes_read < data_length) do
  1608. begin
  1609. { move the restart point to here }
  1610. datasrc^.next_input_byte := next_input_byte;
  1611. datasrc^.bytes_in_buffer := bytes_in_buffer;
  1612. marker^.bytes_read := bytes_read;
  1613. { If there's not at least one byte in buffer, suspend }
  1614. if (bytes_in_buffer = 0) then
  1615. begin
  1616. if not datasrc^.fill_input_buffer (cinfo) then
  1617. begin
  1618. save_marker := FALSE;
  1619. exit;
  1620. end;
  1621. next_input_byte := datasrc^.next_input_byte;
  1622. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1623. end;
  1624. { Copy bytes with reasonable rapidity }
  1625. while (bytes_read < data_length) and (bytes_in_buffer > 0) do
  1626. begin
  1627. JOCTETPTR(data)^ := next_input_byte^;
  1628. Inc(JOCTETPTR(data));
  1629. Inc(next_input_byte);
  1630. Dec(bytes_in_buffer);
  1631. Inc(bytes_read);
  1632. end;
  1633. end;
  1634. { Done reading what we want to read }
  1635. if (cur_marker <> NIL) then
  1636. begin { will be NIL if bogus length word }
  1637. { Add new marker to end of list }
  1638. if (cinfo^.marker_list = NIL) then
  1639. begin
  1640. cinfo^.marker_list := cur_marker
  1641. end
  1642. else
  1643. begin
  1644. prev := cinfo^.marker_list;
  1645. while (prev^.next <> NIL) do
  1646. prev := prev^.next;
  1647. prev^.next := cur_marker;
  1648. end;
  1649. { Reset pointer & calc remaining data length }
  1650. data := cur_marker^.data;
  1651. length := cur_marker^.original_length - data_length;
  1652. end;
  1653. { Reset to initial state for next marker }
  1654. marker^.cur_marker := NIL;
  1655. { Process the marker if interesting; else just make a generic trace msg }
  1656. case (cinfo^.unread_marker) of
  1657. M_APP0:
  1658. examine_app0(cinfo, data^, data_length, length);
  1659. M_APP14:
  1660. examine_app14(cinfo, data^, data_length, length);
  1661. else
  1662. {$IFDEF DEBUG}
  1663. TRACEMS2(j_common_ptr(cinfo), 1, JTRC_MISC_MARKER, cinfo^.unread_marker,
  1664. int(data_length + length));
  1665. {$ENDIF}
  1666. end;
  1667. { skip any remaining data -- could be lots }
  1668. { do before skip_input_data }
  1669. datasrc^.next_input_byte := next_input_byte;
  1670. datasrc^.bytes_in_buffer := bytes_in_buffer;
  1671. if (length > 0) then
  1672. cinfo^.src^.skip_input_data (cinfo, long(length) );
  1673. save_marker := TRUE;
  1674. end;
  1675. {$endif} { SAVE_MARKERS_SUPPORTED }
  1676. { Find the next JPEG marker, save it in cinfo^.unread_marker.
  1677. Returns FALSE if had to suspend before reaching a marker;
  1678. in that case cinfo^.unread_marker is unchanged.
  1679. Note that the result might not be a valid marker code,
  1680. but it will never be 0 or FF. }
  1681. {LOCAL}
  1682. function next_marker (cinfo : j_decompress_ptr) : boolean;
  1683. var
  1684. c : int;
  1685. var
  1686. datasrc : jpeg_source_mgr_ptr;
  1687. next_input_byte : JOCTETptr;
  1688. bytes_in_buffer : size_t;
  1689. begin
  1690. datasrc := cinfo^.src;
  1691. next_input_byte := datasrc^.next_input_byte;
  1692. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1693. {while TRUE do}
  1694. repeat
  1695. { Read a byte into variable c. If must suspend, return FALSE. }
  1696. { make a byte available.
  1697. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1698. but we must reload the local copies after a successful fill. }
  1699. if (bytes_in_buffer = 0) then
  1700. begin
  1701. if (not datasrc^.fill_input_buffer(cinfo)) then
  1702. begin
  1703. next_marker := FALSE;
  1704. exit;
  1705. end;
  1706. { Reload the local copies }
  1707. next_input_byte := datasrc^.next_input_byte;
  1708. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1709. end;
  1710. Dec( bytes_in_buffer );
  1711. c := GETJOCTET(next_input_byte^);
  1712. Inc(next_input_byte);
  1713. { Skip any non-FF bytes.
  1714. This may look a bit inefficient, but it will not occur in a valid file.
  1715. We sync after each discarded byte so that a suspending data source
  1716. can discard the byte from its buffer. }
  1717. while (c <> $FF) do
  1718. begin
  1719. Inc(cinfo^.marker^.discarded_bytes);
  1720. { Unload the local copies --- do this only at a restart boundary }
  1721. datasrc^.next_input_byte := next_input_byte;
  1722. datasrc^.bytes_in_buffer := bytes_in_buffer;
  1723. { Read a byte into variable c. If must suspend, return FALSE. }
  1724. { make a byte available.
  1725. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1726. but we must reload the local copies after a successful fill. }
  1727. if (bytes_in_buffer = 0) then
  1728. begin
  1729. if (not datasrc^.fill_input_buffer(cinfo)) then
  1730. begin
  1731. next_marker := FALSE;
  1732. exit;
  1733. end;
  1734. { Reload the local copies }
  1735. next_input_byte := datasrc^.next_input_byte;
  1736. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1737. end;
  1738. Dec( bytes_in_buffer );
  1739. c := GETJOCTET(next_input_byte^);
  1740. Inc(next_input_byte);
  1741. end;
  1742. { This loop swallows any duplicate FF bytes. Extra FFs are legal as
  1743. pad bytes, so don't count them in discarded_bytes. We assume there
  1744. will not be so many consecutive FF bytes as to overflow a suspending
  1745. data source's input buffer. }
  1746. repeat
  1747. { Read a byte into variable c. If must suspend, return FALSE. }
  1748. { make a byte available.
  1749. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1750. but we must reload the local copies after a successful fill. }
  1751. if (bytes_in_buffer = 0) then
  1752. begin
  1753. if (not datasrc^.fill_input_buffer(cinfo)) then
  1754. begin
  1755. next_marker := FALSE;
  1756. exit;
  1757. end;
  1758. { Reload the local copies }
  1759. next_input_byte := datasrc^.next_input_byte;
  1760. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1761. end;
  1762. Dec( bytes_in_buffer );
  1763. c := GETJOCTET(next_input_byte^);
  1764. Inc(next_input_byte);
  1765. Until (c <> $FF);
  1766. if (c <> 0) then
  1767. break; { found a valid marker, exit loop }
  1768. { Reach here if we found a stuffed-zero data sequence (FF/00).
  1769. Discard it and loop back to try again. }
  1770. Inc(cinfo^.marker^.discarded_bytes, 2);
  1771. { Unload the local copies --- do this only at a restart boundary }
  1772. datasrc^.next_input_byte := next_input_byte;
  1773. datasrc^.bytes_in_buffer := bytes_in_buffer;
  1774. Until False;
  1775. if (cinfo^.marker^.discarded_bytes <> 0) then
  1776. begin
  1777. WARNMS2(j_common_ptr(cinfo), JWRN_EXTRANEOUS_DATA,
  1778. cinfo^.marker^.discarded_bytes, c);
  1779. cinfo^.marker^.discarded_bytes := 0;
  1780. end;
  1781. cinfo^.unread_marker := c;
  1782. { Unload the local copies --- do this only at a restart boundary }
  1783. datasrc^.next_input_byte := next_input_byte;
  1784. datasrc^.bytes_in_buffer := bytes_in_buffer;
  1785. next_marker := TRUE;
  1786. end; { next_marker }
  1787. {LOCAL}
  1788. function first_marker (cinfo : j_decompress_ptr) : boolean;
  1789. { Like next_marker, but used to obtain the initial SOI marker. }
  1790. { For this marker, we do not allow preceding garbage or fill; otherwise,
  1791. we might well scan an entire input file before realizing it ain't JPEG.
  1792. If an application wants to process non-JFIF files, it must seek to the
  1793. SOI before calling the JPEG library. }
  1794. var
  1795. c, c2 : int;
  1796. var
  1797. datasrc : jpeg_source_mgr_ptr;
  1798. next_input_byte : JOCTETptr;
  1799. bytes_in_buffer : size_t;
  1800. begin
  1801. datasrc := cinfo^.src;
  1802. next_input_byte := datasrc^.next_input_byte;
  1803. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1804. { Read a byte into variable c. If must suspend, return FALSE. }
  1805. { make a byte available.
  1806. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1807. but we must reload the local copies after a successful fill. }
  1808. if (bytes_in_buffer = 0) then
  1809. begin
  1810. if (not datasrc^.fill_input_buffer(cinfo)) then
  1811. begin
  1812. first_marker := FALSE;
  1813. exit;
  1814. end;
  1815. { Reload the local copies }
  1816. next_input_byte := datasrc^.next_input_byte;
  1817. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1818. end;
  1819. Dec( bytes_in_buffer );
  1820. c := GETJOCTET(next_input_byte^);
  1821. Inc(next_input_byte);
  1822. { Read a byte into variable c2. If must suspend, return FALSE. }
  1823. { make a byte available.
  1824. Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  1825. but we must reload the local copies after a successful fill. }
  1826. if (bytes_in_buffer = 0) then
  1827. begin
  1828. if (not datasrc^.fill_input_buffer(cinfo)) then
  1829. begin
  1830. first_marker := FALSE;
  1831. exit;
  1832. end;
  1833. { Reload the local copies }
  1834. next_input_byte := datasrc^.next_input_byte;
  1835. bytes_in_buffer := datasrc^.bytes_in_buffer;
  1836. end;
  1837. Dec( bytes_in_buffer );
  1838. c2 := GETJOCTET(next_input_byte^);
  1839. Inc(next_input_byte);
  1840. if (c <> $FF) or (c2 <> int(M_SOI)) then
  1841. ERREXIT2(j_common_ptr(cinfo), JERR_NO_SOI, c, c2);
  1842. cinfo^.unread_marker := c2;
  1843. { Unload the local copies --- do this only at a restart boundary }
  1844. datasrc^.next_input_byte := next_input_byte;
  1845. datasrc^.bytes_in_buffer := bytes_in_buffer;
  1846. first_marker := TRUE;
  1847. end; { first_marker }
  1848. { Read markers until SOS or EOI.
  1849. Returns same codes as are defined for jpeg_consume_input:
  1850. JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. }
  1851. {METHODDEF}
  1852. function read_markers (cinfo : j_decompress_ptr) : int;
  1853. begin
  1854. { Outer loop repeats once for each marker. }
  1855. repeat
  1856. { Collect the marker proper, unless we already did. }
  1857. { NB: first_marker() enforces the requirement that SOI appear first. }
  1858. if (cinfo^.unread_marker = 0) then
  1859. begin
  1860. if not cinfo^.marker^.saw_SOI then
  1861. begin
  1862. if not first_marker(cinfo) then
  1863. begin
  1864. read_markers := JPEG_SUSPENDED;
  1865. exit;
  1866. end;
  1867. end
  1868. else
  1869. begin
  1870. if not next_marker(cinfo) then
  1871. begin
  1872. read_markers := JPEG_SUSPENDED;
  1873. exit;
  1874. end;
  1875. end;
  1876. end;
  1877. { At this point cinfo^.unread_marker contains the marker code and the
  1878. input point is just past the marker proper, but before any parameters.
  1879. A suspension will cause us to return with this state still true. }
  1880. case (cinfo^.unread_marker) of
  1881. M_SOI:
  1882. if not get_soi(cinfo) then
  1883. begin
  1884. read_markers := JPEG_SUSPENDED;
  1885. exit;
  1886. end;
  1887. M_SOF0, { Baseline }
  1888. M_SOF1: { Extended sequential, Huffman }
  1889. if not get_sof(cinfo, FALSE, FALSE) then
  1890. begin
  1891. read_markers := JPEG_SUSPENDED;
  1892. exit;
  1893. end;
  1894. M_SOF2: { Progressive, Huffman }
  1895. if not get_sof(cinfo, TRUE, FALSE) then
  1896. begin
  1897. read_markers := JPEG_SUSPENDED;
  1898. exit;
  1899. end;
  1900. M_SOF9: { Extended sequential, arithmetic }
  1901. if not get_sof(cinfo, FALSE, TRUE) then
  1902. begin
  1903. read_markers := JPEG_SUSPENDED;
  1904. exit;
  1905. end;
  1906. M_SOF10: { Progressive, arithmetic }
  1907. if not get_sof(cinfo, TRUE, TRUE) then
  1908. begin
  1909. read_markers := JPEG_SUSPENDED;
  1910. exit;
  1911. end;
  1912. { Currently unsupported SOFn types }
  1913. M_SOF3, { Lossless, Huffman }
  1914. M_SOF5, { Differential sequential, Huffman }
  1915. M_SOF6, { Differential progressive, Huffman }
  1916. M_SOF7, { Differential lossless, Huffman }
  1917. M_JPG, { Reserved for JPEG extensions }
  1918. M_SOF11, { Lossless, arithmetic }
  1919. M_SOF13, { Differential sequential, arithmetic }
  1920. M_SOF14, { Differential progressive, arithmetic }
  1921. M_SOF15: { Differential lossless, arithmetic }
  1922. ERREXIT1(j_common_ptr(cinfo), JERR_SOF_UNSUPPORTED, cinfo^.unread_marker);
  1923. M_SOS:
  1924. begin
  1925. if not get_sos(cinfo) then
  1926. begin
  1927. read_markers := JPEG_SUSPENDED;
  1928. exit;
  1929. end;
  1930. cinfo^.unread_marker := 0; { processed the marker }
  1931. read_markers := JPEG_REACHED_SOS;
  1932. exit;
  1933. end;
  1934. M_EOI:
  1935. begin
  1936. {$IFDEF DEBUG}
  1937. TRACEMS(j_common_ptr(cinfo), 1, JTRC_EOI);
  1938. {$ENDIF}
  1939. cinfo^.unread_marker := 0; { processed the marker }
  1940. read_markers := JPEG_REACHED_EOI;
  1941. exit;
  1942. end;
  1943. M_DAC:
  1944. if not get_dac(cinfo) then
  1945. begin
  1946. read_markers := JPEG_SUSPENDED;
  1947. exit;
  1948. end;
  1949. M_DHT:
  1950. if not get_dht(cinfo) then
  1951. begin
  1952. read_markers := JPEG_SUSPENDED;
  1953. exit;
  1954. end;
  1955. M_DQT:
  1956. if not get_dqt(cinfo) then
  1957. begin
  1958. read_markers := JPEG_SUSPENDED;
  1959. exit;
  1960. end;
  1961. M_DRI:
  1962. if not get_dri(cinfo) then
  1963. begin
  1964. read_markers := JPEG_SUSPENDED;
  1965. exit;
  1966. end;
  1967. M_APP0,
  1968. M_APP1,
  1969. M_APP2,
  1970. M_APP3,
  1971. M_APP4,
  1972. M_APP5,
  1973. M_APP6,
  1974. M_APP7,
  1975. M_APP8,
  1976. M_APP9,
  1977. M_APP10,
  1978. M_APP11,
  1979. M_APP12,
  1980. M_APP13,
  1981. M_APP14,
  1982. M_APP15:
  1983. if not my_marker_ptr(cinfo^.marker)^.
  1984. process_APPn[cinfo^.unread_marker - int(M_APP0)](cinfo) then
  1985. begin
  1986. read_markers := JPEG_SUSPENDED;
  1987. exit;
  1988. end;
  1989. M_COM:
  1990. if not my_marker_ptr(cinfo^.marker)^.process_COM (cinfo) then
  1991. begin
  1992. read_markers := JPEG_SUSPENDED;
  1993. exit;
  1994. end;
  1995. M_RST0, { these are all parameterless }
  1996. M_RST1,
  1997. M_RST2,
  1998. M_RST3,
  1999. M_RST4,
  2000. M_RST5,
  2001. M_RST6,
  2002. M_RST7,
  2003. M_TEM:
  2004. {$IFDEF DEBUG}
  2005. TRACEMS1(j_common_ptr(cinfo), 1, JTRC_PARMLESS_MARKER,
  2006. cinfo^.unread_marker)
  2007. {$ENDIF}
  2008. ;
  2009. M_DNL: { Ignore DNL ... perhaps the wrong thing }
  2010. if not skip_variable(cinfo) then
  2011. begin
  2012. read_markers := JPEG_SUSPENDED;
  2013. exit;
  2014. end;
  2015. else { must be DHP, EXP, JPGn, or RESn }
  2016. { For now, we treat the reserved markers as fatal errors since they are
  2017. likely to be used to signal incompatible JPEG Part 3 extensions.
  2018. Once the JPEG 3 version-number marker is well defined, this code
  2019. ought to change! }
  2020. ERREXIT1(j_common_ptr(cinfo) , JERR_UNKNOWN_MARKER,
  2021. cinfo^.unread_marker);
  2022. end; { end of case }
  2023. { Successfully processed marker, so reset state variable }
  2024. cinfo^.unread_marker := 0;
  2025. Until false;
  2026. end; { read_markers }
  2027. { Read a restart marker, which is expected to appear next in the datastream;
  2028. if the marker is not there, take appropriate recovery action.
  2029. Returns FALSE if suspension is required.
  2030. This is called by the entropy decoder after it has read an appropriate
  2031. number of MCUs. cinfo^.unread_marker may be nonzero if the entropy decoder
  2032. has already read a marker from the data source. Under normal conditions
  2033. cinfo^.unread_marker will be reset to 0 before returning; if not reset,
  2034. it holds a marker which the decoder will be unable to read past. }
  2035. {METHODDEF}
  2036. function read_restart_marker (cinfo : j_decompress_ptr) :boolean;
  2037. begin
  2038. { Obtain a marker unless we already did. }
  2039. { Note that next_marker will complain if it skips any data. }
  2040. if (cinfo^.unread_marker = 0) then
  2041. begin
  2042. if not next_marker(cinfo) then
  2043. begin
  2044. read_restart_marker := FALSE;
  2045. exit;
  2046. end;
  2047. end;
  2048. if (cinfo^.unread_marker = (int(M_RST0) + cinfo^.marker^.next_restart_num)) then
  2049. begin
  2050. { Normal case --- swallow the marker and let entropy decoder continue }
  2051. {$IFDEF DEBUG}
  2052. TRACEMS1(j_common_ptr(cinfo), 3, JTRC_RST,
  2053. cinfo^.marker^.next_restart_num);
  2054. {$ENDIF}
  2055. cinfo^.unread_marker := 0;
  2056. end
  2057. else
  2058. begin
  2059. { Uh-oh, the restart markers have been messed up. }
  2060. { Let the data source manager determine how to resync. }
  2061. if not cinfo^.src^.resync_to_restart(cinfo,
  2062. cinfo^.marker^.next_restart_num) then
  2063. begin
  2064. read_restart_marker := FALSE;
  2065. exit;
  2066. end;
  2067. end;
  2068. { Update next-restart state }
  2069. with cinfo^.marker^ do
  2070. next_restart_num := (next_restart_num + 1) and 7;
  2071. read_restart_marker := TRUE;
  2072. end; { read_restart_marker }
  2073. { This is the default resync_to_restart method for data source managers
  2074. to use if they don't have any better approach. Some data source managers
  2075. may be able to back up, or may have additional knowledge about the data
  2076. which permits a more intelligent recovery strategy; such managers would
  2077. presumably supply their own resync method.
  2078. read_restart_marker calls resync_to_restart if it finds a marker other than
  2079. the restart marker it was expecting. (This code is *not* used unless
  2080. a nonzero restart interval has been declared.) cinfo^.unread_marker is
  2081. the marker code actually found (might be anything, except 0 or FF).
  2082. The desired restart marker number (0..7) is passed as a parameter.
  2083. This routine is supposed to apply whatever error recovery strategy seems
  2084. appropriate in order to position the input stream to the next data segment.
  2085. Note that cinfo^.unread_marker is treated as a marker appearing before
  2086. the current data-source input point; usually it should be reset to zero
  2087. before returning.
  2088. Returns FALSE if suspension is required.
  2089. This implementation is substantially constrained by wanting to treat the
  2090. input as a data stream; this means we can't back up. Therefore, we have
  2091. only the following actions to work with:
  2092. 1. Simply discard the marker and let the entropy decoder resume at next
  2093. byte of file.
  2094. 2. Read forward until we find another marker, discarding intervening
  2095. data. (In theory we could look ahead within the current bufferload,
  2096. without having to discard data if we don't find the desired marker.
  2097. This idea is not implemented here, in part because it makes behavior
  2098. dependent on buffer size and chance buffer-boundary positions.)
  2099. 3. Leave the marker unread (by failing to zero cinfo^.unread_marker).
  2100. This will cause the entropy decoder to process an empty data segment,
  2101. inserting dummy zeroes, and then we will reprocess the marker.
  2102. #2 is appropriate if we think the desired marker lies ahead, while #3 is
  2103. appropriate if the found marker is a future restart marker (indicating
  2104. that we have missed the desired restart marker, probably because it got
  2105. corrupted).
  2106. We apply #2 or #3 if the found marker is a restart marker no more than
  2107. two counts behind or ahead of the expected one. We also apply #2 if the
  2108. found marker is not a legal JPEG marker code (it's certainly bogus data).
  2109. If the found marker is a restart marker more than 2 counts away, we do #1
  2110. (too much risk that the marker is erroneous; with luck we will be able to
  2111. resync at some future point).
  2112. For any valid non-restart JPEG marker, we apply #3. This keeps us from
  2113. overrunning the end of a scan. An implementation limited to single-scan
  2114. files might find it better to apply #2 for markers other than EOI, since
  2115. any other marker would have to be bogus data in that case. }
  2116. {GLOBAL}
  2117. function jpeg_resync_to_restart(cinfo : j_decompress_ptr;
  2118. desired : int) : boolean;
  2119. var
  2120. marker : int;
  2121. action : int;
  2122. begin
  2123. marker := cinfo^.unread_marker;
  2124. //action := 1; { never used }
  2125. { Always put up a warning. }
  2126. WARNMS2(j_common_ptr(cinfo), JWRN_MUST_RESYNC, marker, desired);
  2127. { Outer loop handles repeated decision after scanning forward. }
  2128. repeat
  2129. if (marker < int(M_SOF0)) then
  2130. action := 2 { invalid marker }
  2131. else
  2132. if (marker < int(M_RST0)) or (marker > int(M_RST7)) then
  2133. action := 3 { valid non-restart marker }
  2134. else
  2135. begin
  2136. if (marker = (int(M_RST0) + ((desired+1) and 7))) or
  2137. (marker = (int(M_RST0) + ((desired+2) and 7))) then
  2138. action := 3 { one of the next two expected restarts }
  2139. else
  2140. if (marker = (int(M_RST0) + ((desired-1) and 7))) or
  2141. (marker = (int(M_RST0) + ((desired-2) and 7))) then
  2142. action := 2 { a prior restart, so advance }
  2143. else
  2144. action := 1; { desired restart or too far away }
  2145. end;
  2146. {$IFDEF DEBUG}
  2147. TRACEMS2(j_common_ptr(cinfo), 4, JTRC_RECOVERY_ACTION, marker, action);
  2148. {$ENDIF}
  2149. case action of
  2150. 1:
  2151. { Discard marker and let entropy decoder resume processing. }
  2152. begin
  2153. cinfo^.unread_marker := 0;
  2154. jpeg_resync_to_restart := TRUE;
  2155. exit;
  2156. end;
  2157. 2:
  2158. { Scan to the next marker, and repeat the decision loop. }
  2159. begin
  2160. if not next_marker(cinfo) then
  2161. begin
  2162. jpeg_resync_to_restart := FALSE;
  2163. exit;
  2164. end;
  2165. marker := cinfo^.unread_marker;
  2166. end;
  2167. 3:
  2168. { Return without advancing past this marker. }
  2169. { Entropy decoder will be forced to process an empty segment. }
  2170. begin
  2171. jpeg_resync_to_restart := TRUE;
  2172. exit;
  2173. end;
  2174. end; { case }
  2175. Until false; { end loop }
  2176. end; { jpeg_resync_to_restart }
  2177. { Reset marker processing state to begin a fresh datastream. }
  2178. {METHODDEF}
  2179. procedure reset_marker_reader (cinfo : j_decompress_ptr);
  2180. var
  2181. marker : my_marker_ptr;
  2182. begin
  2183. marker := my_marker_ptr (cinfo^.marker);
  2184. with cinfo^ do
  2185. begin
  2186. comp_info := NIL; { until allocated by get_sof }
  2187. input_scan_number := 0; { no SOS seen yet }
  2188. unread_marker := 0; { no pending marker }
  2189. end;
  2190. marker^.pub.saw_SOI := FALSE; { set internal state too }
  2191. marker^.pub.saw_SOF := FALSE;
  2192. marker^.pub.discarded_bytes := 0;
  2193. marker^.cur_marker := NIL;
  2194. end; { reset_marker_reader }
  2195. { Initialize the marker reader module.
  2196. This is called only once, when the decompression object is created. }
  2197. {GLOBAL}
  2198. procedure jinit_marker_reader (cinfo : j_decompress_ptr);
  2199. var
  2200. marker : my_marker_ptr;
  2201. i : int;
  2202. begin
  2203. { Create subobject in permanent pool }
  2204. marker := my_marker_ptr(
  2205. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_PERMANENT,
  2206. SIZEOF(my_marker_reader))
  2207. );
  2208. cinfo^.marker := jpeg_marker_reader_ptr(marker);
  2209. { Initialize method pointers }
  2210. marker^.pub.reset_marker_reader := reset_marker_reader;
  2211. marker^.pub.read_markers := read_markers;
  2212. marker^.pub.read_restart_marker := read_restart_marker;
  2213. { Initialize COM/APPn processing.
  2214. By default, we examine and then discard APP0 and APP14,
  2215. but simply discard COM and all other APPn. }
  2216. marker^.process_COM := skip_variable;
  2217. marker^.length_limit_COM := 0;
  2218. for i := 0 to 16-1 do
  2219. begin
  2220. marker^.process_APPn[i] := skip_variable;
  2221. marker^.length_limit_APPn[i] := 0;
  2222. end;
  2223. marker^.process_APPn[0] := get_interesting_appn;
  2224. marker^.process_APPn[14] := get_interesting_appn;
  2225. { Reset marker processing state }
  2226. reset_marker_reader(cinfo);
  2227. end; { jinit_marker_reader }
  2228. { Control saving of COM and APPn markers into marker_list. }
  2229. {$ifdef SAVE_MARKERS_SUPPORTED}
  2230. {GLOBAL}
  2231. procedure jpeg_save_markers (cinfo : j_decompress_ptr;
  2232. marker_code : int;
  2233. length_limit : uint);
  2234. var
  2235. marker : my_marker_ptr;
  2236. maxlength : long;
  2237. processor : jpeg_marker_parser_method;
  2238. begin
  2239. marker := my_marker_ptr (cinfo^.marker);
  2240. { Length limit mustn't be larger than what we can allocate
  2241. (should only be a concern in a 16-bit environment). }
  2242. maxlength := cinfo^.mem^.max_alloc_chunk - SIZEOF(jpeg_marker_struct);
  2243. if (long(length_limit) > maxlength) then
  2244. length_limit := uint(maxlength);
  2245. { Choose processor routine to use.
  2246. APP0/APP14 have special requirements. }
  2247. if (length_limit <> 0) then
  2248. begin
  2249. processor := save_marker;
  2250. { If saving APP0/APP14, save at least enough for our internal use. }
  2251. if (marker_code = int(M_APP0)) and (length_limit < APP0_DATA_LEN) then
  2252. length_limit := APP0_DATA_LEN
  2253. else
  2254. if (marker_code = int(M_APP14)) and (length_limit < APP14_DATA_LEN) then
  2255. length_limit := APP14_DATA_LEN;
  2256. end
  2257. else
  2258. begin
  2259. processor := skip_variable;
  2260. { If discarding APP0/APP14, use our regular on-the-fly processor. }
  2261. if (marker_code = int(M_APP0)) or (marker_code = int(M_APP14)) then
  2262. processor := get_interesting_appn;
  2263. end;
  2264. if (marker_code = int(M_COM)) then
  2265. begin
  2266. marker^.process_COM := processor;
  2267. marker^.length_limit_COM := length_limit;
  2268. end
  2269. else
  2270. if (marker_code >= int(M_APP0)) and (marker_code <= int(M_APP15)) then
  2271. begin
  2272. marker^.process_APPn[marker_code - int(M_APP0)] := processor;
  2273. marker^.length_limit_APPn[marker_code - int(M_APP0)] := length_limit;
  2274. end
  2275. else
  2276. ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, marker_code);
  2277. end;
  2278. {$endif} { SAVE_MARKERS_SUPPORTED }
  2279. { Install a special processing method for COM or APPn markers. }
  2280. {GLOBAL}
  2281. procedure jpeg_set_marker_processor (cinfo : j_decompress_ptr;
  2282. marker_code : int;
  2283. routine : jpeg_marker_parser_method);
  2284. var
  2285. marker : my_marker_ptr;
  2286. begin
  2287. marker := my_marker_ptr (cinfo^.marker);
  2288. if (marker_code = int(M_COM)) then
  2289. marker^.process_COM := routine
  2290. else
  2291. if (marker_code >= int(M_APP0)) and (marker_code <= int(M_APP15)) then
  2292. marker^.process_APPn[marker_code - int(M_APP0)] := routine
  2293. else
  2294. ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, marker_code);
  2295. end;
  2296. end.