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.

1061 lines
32 KiB

3 years ago
  1. unit imjdphuff;
  2. { This file contains Huffman entropy decoding routines for progressive JPEG.
  3. Much of the complexity here has to do with supporting input suspension.
  4. If the data source module demands suspension, we want to be able to back
  5. up to the start of the current MCU. To do this, we copy state variables
  6. into local working storage, and update them back to the permanent
  7. storage only upon successful completion of an MCU. }
  8. { Original: jdphuff.c ; Copyright (C) 1995-1997, Thomas G. Lane. }
  9. interface
  10. {$I imjconfig.inc}
  11. uses
  12. imjmorecfg,
  13. imjinclude,
  14. imjpeglib,
  15. imjdeferr,
  16. imjerror,
  17. imjutils,
  18. imjdhuff; { Declarations shared with jdhuff.c }
  19. {GLOBAL}
  20. procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
  21. implementation
  22. { Expanded entropy decoder object for progressive Huffman decoding.
  23. The savable_state subrecord contains fields that change within an MCU,
  24. but must not be updated permanently until we complete the MCU. }
  25. type
  26. savable_state = record
  27. EOBRUN : uInt; { remaining EOBs in EOBRUN }
  28. last_dc_val : array[00..MAX_COMPS_IN_SCAN-1] of int;
  29. { last DC coef for each component }
  30. end;
  31. type
  32. phuff_entropy_ptr = ^phuff_entropy_decoder;
  33. phuff_entropy_decoder = record
  34. pub : jpeg_entropy_decoder; { public fields }
  35. { These fields are loaded into local variables at start of each MCU.
  36. In case of suspension, we exit WITHOUT updating them. }
  37. bitstate : bitread_perm_state; { Bit buffer at start of MCU }
  38. saved : savable_state; { Other state at start of MCU }
  39. { These fields are NOT loaded into local working state. }
  40. restarts_to_go : uInt; { MCUs left in this restart interval }
  41. { Pointers to derived tables (these workspaces have image lifespan) }
  42. derived_tbls : array[0..NUM_HUFF_TBLS-1] of d_derived_tbl_ptr;
  43. ac_derived_tbl : d_derived_tbl_ptr; { active table during an AC scan }
  44. end;
  45. { Forward declarations }
  46. {METHODDEF}
  47. function decode_mcu_DC_first (cinfo : j_decompress_ptr;
  48. var MCU_data : array of JBLOCKROW) : boolean;
  49. forward;
  50. {METHODDEF}
  51. function decode_mcu_AC_first (cinfo : j_decompress_ptr;
  52. var MCU_data : array of JBLOCKROW) : boolean;
  53. forward;
  54. {METHODDEF}
  55. function decode_mcu_DC_refine (cinfo : j_decompress_ptr;
  56. var MCU_data : array of JBLOCKROW) : boolean;
  57. forward;
  58. {METHODDEF}
  59. function decode_mcu_AC_refine (cinfo : j_decompress_ptr;
  60. var MCU_data : array of JBLOCKROW) : boolean;
  61. forward;
  62. { Initialize for a Huffman-compressed scan. }
  63. {METHODDEF}
  64. procedure start_pass_phuff_decoder (cinfo : j_decompress_ptr);
  65. var
  66. entropy : phuff_entropy_ptr;
  67. is_DC_band, bad : boolean;
  68. ci, coefi, tbl : int;
  69. coef_bit_ptr : coef_bits_ptr;
  70. compptr : jpeg_component_info_ptr;
  71. var
  72. cindex : int;
  73. expected : int;
  74. begin
  75. entropy := phuff_entropy_ptr (cinfo^.entropy);
  76. is_DC_band := (cinfo^.Ss = 0);
  77. { Validate scan parameters }
  78. bad := FALSE;
  79. if (is_DC_band) then
  80. begin
  81. if (cinfo^.Se <> 0) then
  82. bad := TRUE;
  83. end
  84. else
  85. begin
  86. { need not check Ss/Se < 0 since they came from unsigned bytes }
  87. if (cinfo^.Ss > cinfo^.Se) or (cinfo^.Se >= DCTSIZE2) then
  88. bad := TRUE;
  89. { AC scans may have only one component }
  90. if (cinfo^.comps_in_scan <> 1) then
  91. bad := TRUE;
  92. end;
  93. if (cinfo^.Ah <> 0) then
  94. begin
  95. { Successive approximation refinement scan: must have Al = Ah-1. }
  96. if (cinfo^.Al <> cinfo^.Ah-1) then
  97. bad := TRUE;
  98. end;
  99. if (cinfo^.Al > 13) then { need not check for < 0 }
  100. bad := TRUE;
  101. { Arguably the maximum Al value should be less than 13 for 8-bit precision,
  102. but the spec doesn't say so, and we try to be liberal about what we
  103. accept. Note: large Al values could result in out-of-range DC
  104. coefficients during early scans, leading to bizarre displays due to
  105. overflows in the IDCT math. But we won't crash. }
  106. if (bad) then
  107. ERREXIT4(j_common_ptr(cinfo), JERR_BAD_PROGRESSION,
  108. cinfo^.Ss, cinfo^.Se, cinfo^.Ah, cinfo^.Al);
  109. { Update progression status, and verify that scan order is legal.
  110. Note that inter-scan inconsistencies are treated as warnings
  111. not fatal errors ... not clear if this is right way to behave. }
  112. for ci := 0 to pred(cinfo^.comps_in_scan) do
  113. begin
  114. cindex := cinfo^.cur_comp_info[ci]^.component_index;
  115. coef_bit_ptr := coef_bits_ptr(@(cinfo^.coef_bits^[cindex])); {^[0] ???
  116. Nomssi }
  117. if (not is_DC_band) and (coef_bit_ptr^[0] < 0) then
  118. { AC without prior DC scan }
  119. WARNMS2(j_common_ptr(cinfo), JWRN_BOGUS_PROGRESSION, cindex, 0);
  120. for coefi := cinfo^.Ss to cinfo^.Se do
  121. begin
  122. if (coef_bit_ptr^[coefi] < 0) then
  123. expected := 0
  124. else
  125. expected := coef_bit_ptr^[coefi];
  126. if (cinfo^.Ah <> expected) then
  127. WARNMS2(j_common_ptr(cinfo), JWRN_BOGUS_PROGRESSION, cindex, coefi);
  128. coef_bit_ptr^[coefi] := cinfo^.Al;
  129. end;
  130. end;
  131. { Select MCU decoding routine }
  132. if (cinfo^.Ah = 0) then
  133. begin
  134. if (is_DC_band) then
  135. entropy^.pub.decode_mcu := decode_mcu_DC_first
  136. else
  137. entropy^.pub.decode_mcu := decode_mcu_AC_first;
  138. end
  139. else
  140. begin
  141. if (is_DC_band) then
  142. entropy^.pub.decode_mcu := decode_mcu_DC_refine
  143. else
  144. entropy^.pub.decode_mcu := decode_mcu_AC_refine;
  145. end;
  146. for ci := 0 to pred(cinfo^.comps_in_scan) do
  147. begin
  148. compptr := cinfo^.cur_comp_info[ci];
  149. { Make sure requested tables are present, and compute derived tables.
  150. We may build same derived table more than once, but it's not expensive. }
  151. if (is_DC_band) then
  152. begin
  153. if (cinfo^.Ah = 0) then
  154. begin { DC refinement needs no table }
  155. tbl := compptr^.dc_tbl_no;
  156. jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
  157. entropy^.derived_tbls[tbl]);
  158. end;
  159. end
  160. else
  161. begin
  162. tbl := compptr^.ac_tbl_no;
  163. jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
  164. entropy^.derived_tbls[tbl]);
  165. { remember the single active table }
  166. entropy^.ac_derived_tbl := entropy^.derived_tbls[tbl];
  167. end;
  168. { Initialize DC predictions to 0 }
  169. entropy^.saved.last_dc_val[ci] := 0;
  170. end;
  171. { Initialize bitread state variables }
  172. entropy^.bitstate.bits_left := 0;
  173. entropy^.bitstate.get_buffer := 0; { unnecessary, but keeps Purify quiet }
  174. entropy^.pub.insufficient_data := FALSE;
  175. { Initialize private state variables }
  176. entropy^.saved.EOBRUN := 0;
  177. { Initialize restart counter }
  178. entropy^.restarts_to_go := cinfo^.restart_interval;
  179. end;
  180. { Figure F.12: extend sign bit.
  181. On some machines, a shift and add will be faster than a table lookup. }
  182. {$ifdef AVOID_TABLES}
  183. #define HUFF_EXTEND(x,s)
  184. ((x) < (1shl((s)-1)) ? (x) + (((-1)shl(s)) + 1) : (x))
  185. {$else}
  186. { #define HUFF_EXTEND(x,s)
  187. if (x) < extend_test[s] then
  188. (x) + extend_offset[s]
  189. else
  190. (x)}
  191. const
  192. extend_test : Array[0..16-1] of int = { entry n is 2**(n-1) }
  193. ($0000, $0001, $0002, $0004, $0008, $0010, $0020, $0040,
  194. $0080, $0100, $0200, $0400, $0800, $1000, $2000, $4000);
  195. const
  196. extend_offset : array[0..16-1] of int = { entry n is (-1 shl n) + 1 }
  197. ( 0, ((-1) shl 1) + 1, ((-1) shl 2) + 1, ((-1) shl 3) + 1, ((-1) shl 4) + 1,
  198. ((-1) shl 5) + 1, ((-1) shl 6) + 1, ((-1) shl 7) + 1, ((-1) shl 8) + 1,
  199. ((-1) shl 9) + 1, ((-1) shl 10) + 1, ((-1) shl 11) + 1, ((-1) shl 12) + 1,
  200. ((-1) shl 13) + 1, ((-1) shl 14) + 1, ((-1) shl 15) + 1 );
  201. {$endif} { AVOID_TABLES }
  202. { Check for a restart marker & resynchronize decoder.
  203. return:=s FALSE if must suspend. }
  204. {LOCAL}
  205. function process_restart (cinfo : j_decompress_ptr) : boolean;
  206. var
  207. entropy : phuff_entropy_ptr;
  208. ci : int;
  209. begin
  210. entropy := phuff_entropy_ptr (cinfo^.entropy);
  211. { Throw away any unused bits remaining in bit buffer; }
  212. { include any full bytes in next_marker's count of discarded bytes }
  213. Inc(cinfo^.marker^.discarded_bytes, entropy^.bitstate.bits_left div 8);
  214. entropy^.bitstate.bits_left := 0;
  215. { Advance past the RSTn marker }
  216. if (not cinfo^.marker^.read_restart_marker (cinfo)) then
  217. begin
  218. process_restart := FALSE;
  219. exit;
  220. end;
  221. { Re-initialize DC predictions to 0 }
  222. for ci := 0 to pred(cinfo^.comps_in_scan) do
  223. entropy^.saved.last_dc_val[ci] := 0;
  224. { Re-init EOB run count, too }
  225. entropy^.saved.EOBRUN := 0;
  226. { Reset restart counter }
  227. entropy^.restarts_to_go := cinfo^.restart_interval;
  228. { Reset out-of-data flag, unless read_restart_marker left us smack up
  229. against a marker. In that case we will end up treating the next data
  230. segment as empty, and we can avoid producing bogus output pixels by
  231. leaving the flag set. }
  232. if (cinfo^.unread_marker = 0) then
  233. entropy^.pub.insufficient_data := FALSE;
  234. process_restart := TRUE;
  235. end;
  236. { Huffman MCU decoding.
  237. Each of these routines decodes and returns one MCU's worth of
  238. Huffman-compressed coefficients.
  239. The coefficients are reordered from zigzag order into natural array order,
  240. but are not dequantized.
  241. The i'th block of the MCU is stored into the block pointed to by
  242. MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
  243. We return FALSE if data source requested suspension. In that case no
  244. changes have been made to permanent state. (Exception: some output
  245. coefficients may already have been assigned. This is harmless for
  246. spectral selection, since we'll just re-assign them on the next call.
  247. Successive approximation AC refinement has to be more careful, however.) }
  248. { MCU decoding for DC initial scan (either spectral selection,
  249. or first pass of successive approximation). }
  250. {METHODDEF}
  251. function decode_mcu_DC_first (cinfo : j_decompress_ptr;
  252. var MCU_data : array of JBLOCKROW) : boolean;
  253. label
  254. label1;
  255. var
  256. entropy : phuff_entropy_ptr;
  257. Al : int;
  258. {register} s, r : int;
  259. blkn, ci : int;
  260. block : JBLOCK_PTR;
  261. {BITREAD_STATE_VARS;}
  262. get_buffer : bit_buf_type ; {register}
  263. bits_left : int; {register}
  264. br_state : bitread_working_state;
  265. state : savable_state;
  266. tbl : d_derived_tbl_ptr;
  267. compptr : jpeg_component_info_ptr;
  268. var
  269. nb, look : int; {register}
  270. begin
  271. entropy := phuff_entropy_ptr (cinfo^.entropy);
  272. Al := cinfo^.Al;
  273. { Process restart marker if needed; may have to suspend }
  274. if (cinfo^.restart_interval <> 0) then
  275. begin
  276. if (entropy^.restarts_to_go = 0) then
  277. if (not process_restart(cinfo)) then
  278. begin
  279. decode_mcu_DC_first := FALSE;
  280. exit;
  281. end;
  282. end;
  283. { If we've run out of data, just leave the MCU set to zeroes.
  284. This way, we return uniform gray for the remainder of the segment. }
  285. if not entropy^.pub.insufficient_data then
  286. begin
  287. { Load up working state }
  288. {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
  289. br_state.cinfo := cinfo;
  290. br_state.next_input_byte := cinfo^.src^.next_input_byte;
  291. br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  292. get_buffer := entropy^.bitstate.get_buffer;
  293. bits_left := entropy^.bitstate.bits_left;
  294. {ASSIGN_STATE(state, entropy^.saved);}
  295. state := entropy^.saved;
  296. { Outer loop handles each block in the MCU }
  297. for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  298. begin
  299. block := JBLOCK_PTR(MCU_data[blkn]);
  300. ci := cinfo^.MCU_membership[blkn];
  301. compptr := cinfo^.cur_comp_info[ci];
  302. tbl := entropy^.derived_tbls[compptr^.dc_tbl_no];
  303. { Decode a single block's worth of coefficients }
  304. { Section F.2.2.1: decode the DC coefficient difference }
  305. {HUFF_DECODE(s, br_state, tbl, return FALSE, label1);}
  306. if (bits_left < HUFF_LOOKAHEAD) then
  307. begin
  308. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
  309. begin
  310. decode_mcu_DC_first := FALSE;
  311. exit;
  312. end;
  313. get_buffer := br_state.get_buffer;
  314. bits_left := br_state.bits_left;
  315. if (bits_left < HUFF_LOOKAHEAD) then
  316. begin
  317. nb := 1;
  318. goto label1;
  319. end;
  320. end;
  321. {look := PEEK_BITS(HUFF_LOOKAHEAD);}
  322. look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
  323. pred(1 shl HUFF_LOOKAHEAD);
  324. nb := tbl^.look_nbits[look];
  325. if (nb <> 0) then
  326. begin
  327. {DROP_BITS(nb);}
  328. Dec(bits_left, nb);
  329. s := tbl^.look_sym[look];
  330. end
  331. else
  332. begin
  333. nb := HUFF_LOOKAHEAD+1;
  334. label1:
  335. s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
  336. if (s < 0) then
  337. begin
  338. decode_mcu_DC_first := FALSE;
  339. exit;
  340. end;
  341. get_buffer := br_state.get_buffer;
  342. bits_left := br_state.bits_left;
  343. end;
  344. if (s <> 0) then
  345. begin
  346. {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
  347. if (bits_left < s) then
  348. begin
  349. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
  350. begin
  351. decode_mcu_DC_first := FALSE;
  352. exit;
  353. end;
  354. get_buffer := br_state.get_buffer;
  355. bits_left := br_state.bits_left;
  356. end;
  357. {r := GET_BITS(s);}
  358. Dec(bits_left, s);
  359. r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
  360. {s := HUFF_EXTEND(r, s);}
  361. if (r < extend_test[s]) then
  362. s := r + extend_offset[s]
  363. else
  364. s := r;
  365. end;
  366. { Convert DC difference to actual value, update last_dc_val }
  367. Inc(s, state.last_dc_val[ci]);
  368. state.last_dc_val[ci] := s;
  369. { Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) }
  370. block^[0] := JCOEF (s shl Al);
  371. end;
  372. { Completed MCU, so update state }
  373. {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  374. cinfo^.src^.next_input_byte := br_state.next_input_byte;
  375. cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  376. entropy^.bitstate.get_buffer := get_buffer;
  377. entropy^.bitstate.bits_left := bits_left;
  378. {ASSIGN_STATE(entropy^.saved, state);}
  379. entropy^.saved := state;
  380. end;
  381. { Account for restart interval (no-op if not using restarts) }
  382. Dec(entropy^.restarts_to_go);
  383. decode_mcu_DC_first := TRUE;
  384. end;
  385. { MCU decoding for AC initial scan (either spectral selection,
  386. or first pass of successive approximation). }
  387. {METHODDEF}
  388. function decode_mcu_AC_first (cinfo : j_decompress_ptr;
  389. var MCU_data : array of JBLOCKROW) : boolean;
  390. label
  391. label2;
  392. var
  393. entropy : phuff_entropy_ptr;
  394. Se : int;
  395. Al : int;
  396. {register} s, k, r : int;
  397. EOBRUN : uInt;
  398. block : JBLOCK_PTR;
  399. {BITREAD_STATE_VARS;}
  400. get_buffer : bit_buf_type ; {register}
  401. bits_left : int; {register}
  402. br_state : bitread_working_state;
  403. tbl : d_derived_tbl_ptr;
  404. var
  405. nb, look : int; {register}
  406. begin
  407. entropy := phuff_entropy_ptr (cinfo^.entropy);
  408. Se := cinfo^.Se;
  409. Al := cinfo^.Al;
  410. { Process restart marker if needed; may have to suspend }
  411. if (cinfo^.restart_interval <> 0) then
  412. begin
  413. if (entropy^.restarts_to_go = 0) then
  414. if (not process_restart(cinfo)) then
  415. begin
  416. decode_mcu_AC_first := FALSE;
  417. exit;
  418. end;
  419. end;
  420. { If we've run out of data, just leave the MCU set to zeroes.
  421. This way, we return uniform gray for the remainder of the segment. }
  422. if not entropy^.pub.insufficient_data then
  423. begin
  424. { Load up working state.
  425. We can avoid loading/saving bitread state if in an EOB run. }
  426. EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }
  427. { There is always only one block per MCU }
  428. if (EOBRUN > 0) then { if it's a band of zeroes... }
  429. Dec(EOBRUN) { ...process it now (we do nothing) }
  430. else
  431. begin
  432. {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
  433. br_state.cinfo := cinfo;
  434. br_state.next_input_byte := cinfo^.src^.next_input_byte;
  435. br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  436. get_buffer := entropy^.bitstate.get_buffer;
  437. bits_left := entropy^.bitstate.bits_left;
  438. block := JBLOCK_PTR(MCU_data[0]);
  439. tbl := entropy^.ac_derived_tbl;
  440. k := cinfo^.Ss;
  441. while (k <= Se) do
  442. begin
  443. {HUFF_DECODE(s, br_state, tbl, return FALSE, label2);}
  444. if (bits_left < HUFF_LOOKAHEAD) then
  445. begin
  446. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
  447. begin
  448. decode_mcu_AC_first := FALSE;
  449. exit;
  450. end;
  451. get_buffer := br_state.get_buffer;
  452. bits_left := br_state.bits_left;
  453. if (bits_left < HUFF_LOOKAHEAD) then
  454. begin
  455. nb := 1;
  456. goto label2;
  457. end;
  458. end;
  459. {look := PEEK_BITS(HUFF_LOOKAHEAD);}
  460. look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
  461. pred(1 shl HUFF_LOOKAHEAD);
  462. nb := tbl^.look_nbits[look];
  463. if (nb <> 0) then
  464. begin
  465. {DROP_BITS(nb);}
  466. Dec(bits_left, nb);
  467. s := tbl^.look_sym[look];
  468. end
  469. else
  470. begin
  471. nb := HUFF_LOOKAHEAD+1;
  472. label2:
  473. s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
  474. if (s < 0) then
  475. begin
  476. decode_mcu_AC_first := FALSE;
  477. exit;
  478. end;
  479. get_buffer := br_state.get_buffer;
  480. bits_left := br_state.bits_left;
  481. end;
  482. r := s shr 4;
  483. s := s and 15;
  484. if (s <> 0) then
  485. begin
  486. Inc(k, r);
  487. {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
  488. if (bits_left < s) then
  489. begin
  490. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
  491. begin
  492. decode_mcu_AC_first := FALSE;
  493. exit;
  494. end;
  495. get_buffer := br_state.get_buffer;
  496. bits_left := br_state.bits_left;
  497. end;
  498. {r := GET_BITS(s);}
  499. Dec(bits_left, s);
  500. r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
  501. {s := HUFF_EXTEND(r, s);}
  502. if (r < extend_test[s]) then
  503. s := r + extend_offset[s]
  504. else
  505. s := r;
  506. { Scale and output coefficient in natural (dezigzagged) order }
  507. block^[jpeg_natural_order[k]] := JCOEF (s shl Al);
  508. end
  509. else
  510. begin
  511. if (r = 15) then
  512. begin { ZRL }
  513. Inc(k, 15); { skip 15 zeroes in band }
  514. end
  515. else
  516. begin { EOBr, run length is 2^r + appended bits }
  517. EOBRUN := 1 shl r;
  518. if (r <> 0) then
  519. begin { EOBr, r > 0 }
  520. {CHECK_BIT_BUFFER(br_state, r, return FALSE);}
  521. if (bits_left < r) then
  522. begin
  523. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
  524. begin
  525. decode_mcu_AC_first := FALSE;
  526. exit;
  527. end;
  528. get_buffer := br_state.get_buffer;
  529. bits_left := br_state.bits_left;
  530. end;
  531. {r := GET_BITS(r);}
  532. Dec(bits_left, r);
  533. r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );
  534. Inc(EOBRUN, r);
  535. end;
  536. Dec(EOBRUN); { this band is processed at this moment }
  537. break; { force end-of-band }
  538. end;
  539. end;
  540. Inc(k);
  541. end;
  542. {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  543. cinfo^.src^.next_input_byte := br_state.next_input_byte;
  544. cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  545. entropy^.bitstate.get_buffer := get_buffer;
  546. entropy^.bitstate.bits_left := bits_left;
  547. end;
  548. { Completed MCU, so update state }
  549. entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }
  550. end;
  551. { Account for restart interval (no-op if not using restarts) }
  552. Dec(entropy^.restarts_to_go);
  553. decode_mcu_AC_first := TRUE;
  554. end;
  555. { MCU decoding for DC successive approximation refinement scan.
  556. Note: we assume such scans can be multi-component, although the spec
  557. is not very clear on the point. }
  558. {METHODDEF}
  559. function decode_mcu_DC_refine (cinfo : j_decompress_ptr;
  560. var MCU_data : array of JBLOCKROW) : boolean;
  561. var
  562. entropy : phuff_entropy_ptr;
  563. p1 : int; { 1 in the bit position being coded }
  564. blkn : int;
  565. block : JBLOCK_PTR;
  566. {BITREAD_STATE_VARS;}
  567. get_buffer : bit_buf_type ; {register}
  568. bits_left : int; {register}
  569. br_state : bitread_working_state;
  570. begin
  571. entropy := phuff_entropy_ptr (cinfo^.entropy);
  572. p1 := 1 shl cinfo^.Al;
  573. { Process restart marker if needed; may have to suspend }
  574. if (cinfo^.restart_interval <> 0) then
  575. begin
  576. if (entropy^.restarts_to_go = 0) then
  577. if (not process_restart(cinfo)) then
  578. begin
  579. decode_mcu_DC_refine := FALSE;
  580. exit;
  581. end;
  582. end;
  583. { Not worth the cycles to check insufficient_data here,
  584. since we will not change the data anyway if we read zeroes. }
  585. { Load up working state }
  586. {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
  587. br_state.cinfo := cinfo;
  588. br_state.next_input_byte := cinfo^.src^.next_input_byte;
  589. br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  590. get_buffer := entropy^.bitstate.get_buffer;
  591. bits_left := entropy^.bitstate.bits_left;
  592. { Outer loop handles each block in the MCU }
  593. for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  594. begin
  595. block := JBLOCK_PTR(MCU_data[blkn]);
  596. { Encoded data is simply the next bit of the two's-complement DC value }
  597. {CHECK_BIT_BUFFER(br_state, 1, return FALSE);}
  598. if (bits_left < 1) then
  599. begin
  600. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
  601. begin
  602. decode_mcu_DC_refine := FALSE;
  603. exit;
  604. end;
  605. get_buffer := br_state.get_buffer;
  606. bits_left := br_state.bits_left;
  607. end;
  608. {if (GET_BITS(1)) then}
  609. Dec(bits_left);
  610. if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) ) <> 0 then
  611. block^[0] := block^[0] or p1;
  612. { Note: since we use OR, repeating the assignment later is safe }
  613. end;
  614. { Completed MCU, so update state }
  615. {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  616. cinfo^.src^.next_input_byte := br_state.next_input_byte;
  617. cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  618. entropy^.bitstate.get_buffer := get_buffer;
  619. entropy^.bitstate.bits_left := bits_left;
  620. { Account for restart interval (no-op if not using restarts) }
  621. Dec(entropy^.restarts_to_go);
  622. decode_mcu_DC_refine := TRUE;
  623. end;
  624. { MCU decoding for AC successive approximation refinement scan. }
  625. {METHODDEF}
  626. function decode_mcu_AC_refine (cinfo : j_decompress_ptr;
  627. var MCU_data : array of JBLOCKROW) : boolean;
  628. label
  629. undoit, label3;
  630. var
  631. entropy : phuff_entropy_ptr;
  632. Se : int;
  633. p1 : int; { 1 in the bit position being coded }
  634. m1 : int; { -1 in the bit position being coded }
  635. {register} s, k, r : int;
  636. EOBRUN : uInt;
  637. block : JBLOCK_PTR;
  638. thiscoef : JCOEF_PTR;
  639. {BITREAD_STATE_VARS;}
  640. get_buffer : bit_buf_type ; {register}
  641. bits_left : int; {register}
  642. br_state : bitread_working_state;
  643. tbl : d_derived_tbl_ptr;
  644. num_newnz : int;
  645. newnz_pos : array[0..DCTSIZE2-1] of int;
  646. var
  647. pos : int;
  648. var
  649. nb, look : int; {register}
  650. begin
  651. num_newnz := 0;
  652. block := nil;
  653. entropy := phuff_entropy_ptr (cinfo^.entropy);
  654. Se := cinfo^.Se;
  655. p1 := 1 shl cinfo^.Al; { 1 in the bit position being coded }
  656. m1 := (-1) shl cinfo^.Al; { -1 in the bit position being coded }
  657. { Process restart marker if needed; may have to suspend }
  658. if (cinfo^.restart_interval <> 0) then
  659. begin
  660. if (entropy^.restarts_to_go = 0) then
  661. if (not process_restart(cinfo)) then
  662. begin
  663. decode_mcu_AC_refine := FALSE;
  664. exit;
  665. end;
  666. end;
  667. { If we've run out of data, don't modify the MCU. }
  668. if not entropy^.pub.insufficient_data then
  669. begin
  670. { Load up working state }
  671. {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
  672. br_state.cinfo := cinfo;
  673. br_state.next_input_byte := cinfo^.src^.next_input_byte;
  674. br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  675. get_buffer := entropy^.bitstate.get_buffer;
  676. bits_left := entropy^.bitstate.bits_left;
  677. EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }
  678. { There is always only one block per MCU }
  679. block := JBLOCK_PTR(MCU_data[0]);
  680. tbl := entropy^.ac_derived_tbl;
  681. { If we are forced to suspend, we must undo the assignments to any newly
  682. nonzero coefficients in the block, because otherwise we'd get confused
  683. next time about which coefficients were already nonzero.
  684. But we need not undo addition of bits to already-nonzero coefficients;
  685. instead, we can test the current bit position to see if we already did it.}
  686. num_newnz := 0;
  687. { initialize coefficient loop counter to start of band }
  688. k := cinfo^.Ss;
  689. if (EOBRUN = 0) then
  690. begin
  691. while (k <= Se) do
  692. begin
  693. {HUFF_DECODE(s, br_state, tbl, goto undoit, label3);}
  694. if (bits_left < HUFF_LOOKAHEAD) then
  695. begin
  696. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
  697. goto undoit;
  698. get_buffer := br_state.get_buffer;
  699. bits_left := br_state.bits_left;
  700. if (bits_left < HUFF_LOOKAHEAD) then
  701. begin
  702. nb := 1;
  703. goto label3;
  704. end;
  705. end;
  706. {look := PEEK_BITS(HUFF_LOOKAHEAD);}
  707. look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
  708. pred(1 shl HUFF_LOOKAHEAD);
  709. nb := tbl^.look_nbits[look];
  710. if (nb <> 0) then
  711. begin
  712. {DROP_BITS(nb);}
  713. Dec(bits_left, nb);
  714. s := tbl^.look_sym[look];
  715. end
  716. else
  717. begin
  718. nb := HUFF_LOOKAHEAD+1;
  719. label3:
  720. s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
  721. if (s < 0) then
  722. goto undoit;
  723. get_buffer := br_state.get_buffer;
  724. bits_left := br_state.bits_left;
  725. end;
  726. r := s shr 4;
  727. s := s and 15;
  728. if (s <> 0) then
  729. begin
  730. if (s <> 1) then { size of new coef should always be 1 }
  731. WARNMS(j_common_ptr(cinfo), JWRN_HUFF_BAD_CODE);
  732. {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
  733. if (bits_left < 1) then
  734. begin
  735. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
  736. goto undoit;
  737. get_buffer := br_state.get_buffer;
  738. bits_left := br_state.bits_left;
  739. end;
  740. {if (GET_BITS(1)) then}
  741. Dec(bits_left);
  742. if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
  743. s := p1 { newly nonzero coef is positive }
  744. else
  745. s := m1; { newly nonzero coef is negative }
  746. end
  747. else
  748. begin
  749. if (r <> 15) then
  750. begin
  751. EOBRUN := 1 shl r; { EOBr, run length is 2^r + appended bits }
  752. if (r <> 0) then
  753. begin
  754. {CHECK_BIT_BUFFER(br_state, r, goto undoit);}
  755. if (bits_left < r) then
  756. begin
  757. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
  758. goto undoit;
  759. get_buffer := br_state.get_buffer;
  760. bits_left := br_state.bits_left;
  761. end;
  762. {r := GET_BITS(r);}
  763. Dec(bits_left, r);
  764. r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );
  765. Inc(EOBRUN, r);
  766. end;
  767. break; { rest of block is handled by EOB logic }
  768. end;
  769. { note s := 0 for processing ZRL }
  770. end;
  771. { Advance over already-nonzero coefs and r still-zero coefs,
  772. appending correction bits to the nonzeroes. A correction bit is 1
  773. if the absolute value of the coefficient must be increased. }
  774. repeat
  775. thiscoef :=@(block^[jpeg_natural_order[k]]);
  776. if (thiscoef^ <> 0) then
  777. begin
  778. {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
  779. if (bits_left < 1) then
  780. begin
  781. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
  782. goto undoit;
  783. get_buffer := br_state.get_buffer;
  784. bits_left := br_state.bits_left;
  785. end;
  786. {if (GET_BITS(1)) then}
  787. Dec(bits_left);
  788. if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
  789. begin
  790. if ((thiscoef^ and p1) = 0) then
  791. begin { do nothing if already set it }
  792. if (thiscoef^ >= 0) then
  793. Inc(thiscoef^, p1)
  794. else
  795. Inc(thiscoef^, m1);
  796. end;
  797. end;
  798. end
  799. else
  800. begin
  801. Dec(r);
  802. if (r < 0) then
  803. break; { reached target zero coefficient }
  804. end;
  805. Inc(k);
  806. until (k > Se);
  807. if (s <> 0) then
  808. begin
  809. pos := jpeg_natural_order[k];
  810. { Output newly nonzero coefficient }
  811. block^[pos] := JCOEF (s);
  812. { Remember its position in case we have to suspend }
  813. newnz_pos[num_newnz] := pos;
  814. Inc(num_newnz);
  815. end;
  816. Inc(k);
  817. end;
  818. end;
  819. if (EOBRUN > 0) then
  820. begin
  821. { Scan any remaining coefficient positions after the end-of-band
  822. (the last newly nonzero coefficient, if any). Append a correction
  823. bit to each already-nonzero coefficient. A correction bit is 1
  824. if the absolute value of the coefficient must be increased. }
  825. while (k <= Se) do
  826. begin
  827. thiscoef := @(block^[jpeg_natural_order[k]]);
  828. if (thiscoef^ <> 0) then
  829. begin
  830. {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
  831. if (bits_left < 1) then
  832. begin
  833. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
  834. goto undoit;
  835. get_buffer := br_state.get_buffer;
  836. bits_left := br_state.bits_left;
  837. end;
  838. {if (GET_BITS(1)) then}
  839. Dec(bits_left);
  840. if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
  841. begin
  842. if ((thiscoef^ and p1) = 0) then
  843. begin { do nothing if already changed it }
  844. if (thiscoef^ >= 0) then
  845. Inc(thiscoef^, p1)
  846. else
  847. Inc(thiscoef^, m1);
  848. end;
  849. end;
  850. end;
  851. Inc(k);
  852. end;
  853. { Count one block completed in EOB run }
  854. Dec(EOBRUN);
  855. end;
  856. { Completed MCU, so update state }
  857. {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  858. cinfo^.src^.next_input_byte := br_state.next_input_byte;
  859. cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  860. entropy^.bitstate.get_buffer := get_buffer;
  861. entropy^.bitstate.bits_left := bits_left;
  862. entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }
  863. end;
  864. { Account for restart interval (no-op if not using restarts) }
  865. Dec(entropy^.restarts_to_go);
  866. decode_mcu_AC_refine := TRUE;
  867. exit;
  868. undoit:
  869. { Re-zero any output coefficients that we made newly nonzero }
  870. while (num_newnz > 0) do
  871. begin
  872. Dec(num_newnz);
  873. block^[newnz_pos[num_newnz]] := 0;
  874. end;
  875. decode_mcu_AC_refine := FALSE;
  876. end;
  877. { Module initialization routine for progressive Huffman entropy decoding. }
  878. {GLOBAL}
  879. procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
  880. var
  881. entropy : phuff_entropy_ptr;
  882. coef_bit_ptr : int_ptr;
  883. ci, i : int;
  884. begin
  885. entropy := phuff_entropy_ptr(
  886. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  887. SIZEOF(phuff_entropy_decoder)) );
  888. cinfo^.entropy := jpeg_entropy_decoder_ptr (entropy);
  889. entropy^.pub.start_pass := start_pass_phuff_decoder;
  890. { Mark derived tables unallocated }
  891. for i := 0 to pred(NUM_HUFF_TBLS) do
  892. begin
  893. entropy^.derived_tbls[i] := NIL;
  894. end;
  895. { Create progression status table }
  896. cinfo^.coef_bits := coef_bits_ptrrow (
  897. cinfo^.mem^.alloc_small ( j_common_ptr (cinfo), JPOOL_IMAGE,
  898. cinfo^.num_components*DCTSIZE2*SIZEOF(int)) );
  899. coef_bit_ptr := @cinfo^.coef_bits^[0][0];
  900. for ci := 0 to pred(cinfo^.num_components) do
  901. for i := 0 to pred(DCTSIZE2) do
  902. begin
  903. coef_bit_ptr^ := -1;
  904. Inc(coef_bit_ptr);
  905. end;
  906. end;
  907. end.