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.

962 lines
28 KiB

3 years ago
  1. unit imjcphuff;
  2. { This file contains Huffman entropy encoding routines for progressive JPEG.
  3. We do not support output suspension in this module, since the library
  4. currently does not allow multiple-scan files to be written with output
  5. suspension. }
  6. { Original: jcphuff.c; Copyright (C) 1995-1997, Thomas G. Lane. }
  7. interface
  8. {$I imjconfig.inc}
  9. uses
  10. imjmorecfg,
  11. imjinclude,
  12. imjpeglib,
  13. imjdeferr,
  14. imjerror,
  15. imjutils,
  16. imjcomapi,
  17. imjchuff; { Declarations shared with jchuff.c }
  18. { Module initialization routine for progressive Huffman entropy encoding. }
  19. {GLOBAL}
  20. procedure jinit_phuff_encoder (cinfo : j_compress_ptr);
  21. implementation
  22. { Expanded entropy encoder object for progressive Huffman encoding. }
  23. type
  24. phuff_entropy_ptr = ^phuff_entropy_encoder;
  25. phuff_entropy_encoder = record
  26. pub : jpeg_entropy_encoder; { public fields }
  27. { Mode flag: TRUE for optimization, FALSE for actual data output }
  28. gather_statistics : boolean;
  29. { Bit-level coding status.
  30. next_output_byte/free_in_buffer are local copies of cinfo^.dest fields.}
  31. next_output_byte : JOCTETptr; { => next byte to write in buffer }
  32. free_in_buffer : size_t; { # of byte spaces remaining in buffer }
  33. put_buffer : INT32; { current bit-accumulation buffer }
  34. put_bits : int; { # of bits now in it }
  35. cinfo : j_compress_ptr; { link to cinfo (needed for dump_buffer) }
  36. { Coding status for DC components }
  37. last_dc_val : array[0..MAX_COMPS_IN_SCAN-1] of int;
  38. { last DC coef for each component }
  39. { Coding status for AC components }
  40. ac_tbl_no : int; { the table number of the single component }
  41. EOBRUN : uInt; { run length of EOBs }
  42. BE : uInt; { # of buffered correction bits before MCU }
  43. bit_buffer : JBytePtr; { buffer for correction bits (1 per char) }
  44. { packing correction bits tightly would save some space but cost time... }
  45. restarts_to_go : uInt; { MCUs left in this restart interval }
  46. next_restart_num : int; { next restart number to write (0-7) }
  47. { Pointers to derived tables (these workspaces have image lifespan).
  48. Since any one scan codes only DC or only AC, we only need one set
  49. of tables, not one for DC and one for AC. }
  50. derived_tbls : array[0..NUM_HUFF_TBLS-1] of c_derived_tbl_ptr;
  51. { Statistics tables for optimization; again, one set is enough }
  52. count_ptrs : array[0..NUM_HUFF_TBLS-1] of TLongTablePtr;
  53. end;
  54. { MAX_CORR_BITS is the number of bits the AC refinement correction-bit
  55. buffer can hold. Larger sizes may slightly improve compression, but
  56. 1000 is already well into the realm of overkill.
  57. The minimum safe size is 64 bits. }
  58. const
  59. MAX_CORR_BITS = 1000; { Max # of correction bits I can buffer }
  60. { Forward declarations }
  61. {METHODDEF}
  62. function encode_mcu_DC_first (cinfo : j_compress_ptr;
  63. const MCU_data: array of JBLOCKROW) : boolean;
  64. forward;
  65. {METHODDEF}
  66. function encode_mcu_AC_first (cinfo : j_compress_ptr;
  67. const MCU_data: array of JBLOCKROW) : boolean;
  68. forward;
  69. {METHODDEF}
  70. function encode_mcu_DC_refine (cinfo : j_compress_ptr;
  71. const MCU_data: array of JBLOCKROW) : boolean;
  72. forward;
  73. {METHODDEF}
  74. function encode_mcu_AC_refine (cinfo : j_compress_ptr;
  75. const MCU_data: array of JBLOCKROW) : boolean;
  76. forward;
  77. {METHODDEF}
  78. procedure finish_pass_phuff (cinfo : j_compress_ptr); forward;
  79. {METHODDEF}
  80. procedure finish_pass_gather_phuff (cinfo : j_compress_ptr); forward;
  81. { Initialize for a Huffman-compressed scan using progressive JPEG. }
  82. {METHODDEF}
  83. procedure start_pass_phuff (cinfo : j_compress_ptr;
  84. gather_statistics : boolean);
  85. var
  86. entropy : phuff_entropy_ptr;
  87. is_DC_band : boolean;
  88. ci, tbl : int;
  89. compptr : jpeg_component_info_ptr;
  90. begin
  91. tbl := 0;
  92. entropy := phuff_entropy_ptr (cinfo^.entropy);
  93. entropy^.cinfo := cinfo;
  94. entropy^.gather_statistics := gather_statistics;
  95. is_DC_band := (cinfo^.Ss = 0);
  96. { We assume jcmaster.c already validated the scan parameters. }
  97. { Select execution routines }
  98. if (cinfo^.Ah = 0) then
  99. begin
  100. if (is_DC_band) then
  101. entropy^.pub.encode_mcu := encode_mcu_DC_first
  102. else
  103. entropy^.pub.encode_mcu := encode_mcu_AC_first;
  104. end
  105. else
  106. begin
  107. if (is_DC_band) then
  108. entropy^.pub.encode_mcu := encode_mcu_DC_refine
  109. else
  110. begin
  111. entropy^.pub.encode_mcu := encode_mcu_AC_refine;
  112. { AC refinement needs a correction bit buffer }
  113. if (entropy^.bit_buffer = NIL) then
  114. entropy^.bit_buffer := JBytePtr(
  115. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  116. MAX_CORR_BITS * SIZEOF(byte)) );
  117. end;
  118. end;
  119. if (gather_statistics) then
  120. entropy^.pub.finish_pass := finish_pass_gather_phuff
  121. else
  122. entropy^.pub.finish_pass := finish_pass_phuff;
  123. { Only DC coefficients may be interleaved, so cinfo^.comps_in_scan = 1
  124. for AC coefficients. }
  125. for ci := 0 to pred(cinfo^.comps_in_scan) do
  126. begin
  127. compptr := cinfo^.cur_comp_info[ci];
  128. { Initialize DC predictions to 0 }
  129. entropy^.last_dc_val[ci] := 0;
  130. { Get table index }
  131. if (is_DC_band) then
  132. begin
  133. if (cinfo^.Ah <> 0) then { DC refinement needs no table }
  134. continue;
  135. tbl := compptr^.dc_tbl_no;
  136. end
  137. else
  138. begin
  139. tbl := compptr^.ac_tbl_no;
  140. entropy^.ac_tbl_no := tbl;
  141. end;
  142. if (gather_statistics) then
  143. begin
  144. { Check for invalid table index }
  145. { (make_c_derived_tbl does this in the other path) }
  146. if (tbl < 0) or (tbl >= NUM_HUFF_TBLS) then
  147. ERREXIT1(j_common_ptr(cinfo), JERR_NO_HUFF_TABLE, tbl);
  148. { Allocate and zero the statistics tables }
  149. { Note that jpeg_gen_optimal_table expects 257 entries in each table! }
  150. if (entropy^.count_ptrs[tbl] = NIL) then
  151. entropy^.count_ptrs[tbl] := TLongTablePtr(
  152. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  153. 257 * SIZEOF(long)) );
  154. MEMZERO(entropy^.count_ptrs[tbl], 257 * SIZEOF(long));
  155. end else
  156. begin
  157. { Compute derived values for Huffman table }
  158. { We may do this more than once for a table, but it's not expensive }
  159. jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
  160. entropy^.derived_tbls[tbl]);
  161. end;
  162. end;
  163. { Initialize AC stuff }
  164. entropy^.EOBRUN := 0;
  165. entropy^.BE := 0;
  166. { Initialize bit buffer to empty }
  167. entropy^.put_buffer := 0;
  168. entropy^.put_bits := 0;
  169. { Initialize restart stuff }
  170. entropy^.restarts_to_go := cinfo^.restart_interval;
  171. entropy^.next_restart_num := 0;
  172. end;
  173. {LOCAL}
  174. procedure dump_buffer (entropy : phuff_entropy_ptr);
  175. { Empty the output buffer; we do not support suspension in this module. }
  176. var
  177. dest : jpeg_destination_mgr_ptr;
  178. begin
  179. dest := entropy^.cinfo^.dest;
  180. if (not dest^.empty_output_buffer (entropy^.cinfo)) then
  181. ERREXIT(j_common_ptr(entropy^.cinfo), JERR_CANT_SUSPEND);
  182. { After a successful buffer dump, must reset buffer pointers }
  183. entropy^.next_output_byte := dest^.next_output_byte;
  184. entropy^.free_in_buffer := dest^.free_in_buffer;
  185. end;
  186. { Outputting bits to the file }
  187. { Only the right 24 bits of put_buffer are used; the valid bits are
  188. left-justified in this part. At most 16 bits can be passed to emit_bits
  189. in one call, and we never retain more than 7 bits in put_buffer
  190. between calls, so 24 bits are sufficient. }
  191. {LOCAL}
  192. procedure emit_bits (entropy : phuff_entropy_ptr;
  193. code : uInt;
  194. size : int); {INLINE}
  195. { Emit some bits, unless we are in gather mode }
  196. var
  197. {register} put_buffer : INT32;
  198. {register} put_bits : int;
  199. var
  200. c : int;
  201. begin
  202. { This routine is heavily used, so it's worth coding tightly. }
  203. put_buffer := INT32 (code);
  204. put_bits := entropy^.put_bits;
  205. { if size is 0, caller used an invalid Huffman table entry }
  206. if (size = 0) then
  207. ERREXIT(j_common_ptr(entropy^.cinfo), JERR_HUFF_MISSING_CODE);
  208. if (entropy^.gather_statistics) then
  209. exit; { do nothing if we're only getting stats }
  210. put_buffer := put_buffer and ((INT32(1) shl size) - 1);
  211. { mask off any extra bits in code }
  212. Inc(put_bits, size); { new number of bits in buffer }
  213. put_buffer := put_buffer shl (24 - put_bits); { align incoming bits }
  214. put_buffer := put_buffer or entropy^.put_buffer;
  215. { and merge with old buffer contents }
  216. while (put_bits >= 8) do
  217. begin
  218. c := int ((put_buffer shr 16) and $FF);
  219. {emit_byte(entropy, c);}
  220. { Outputting bytes to the file.
  221. NB: these must be called only when actually outputting,
  222. that is, entropy^.gather_statistics = FALSE. }
  223. { Emit a byte }
  224. entropy^.next_output_byte^ := JOCTET(c);
  225. Inc(entropy^.next_output_byte);
  226. Dec(entropy^.free_in_buffer);
  227. if (entropy^.free_in_buffer = 0) then
  228. dump_buffer(entropy);
  229. if (c = $FF) then
  230. begin { need to stuff a zero byte? }
  231. {emit_byte(entropy, 0);}
  232. entropy^.next_output_byte^ := JOCTET(0);
  233. Inc(entropy^.next_output_byte);
  234. Dec(entropy^.free_in_buffer);
  235. if (entropy^.free_in_buffer = 0) then
  236. dump_buffer(entropy);
  237. end;
  238. put_buffer := put_buffer shl 8;
  239. Dec(put_bits, 8);
  240. end;
  241. entropy^.put_buffer := put_buffer; { update variables }
  242. entropy^.put_bits := put_bits;
  243. end;
  244. {LOCAL}
  245. procedure flush_bits (entropy : phuff_entropy_ptr);
  246. begin
  247. emit_bits(entropy, $7F, 7); { fill any partial byte with ones }
  248. entropy^.put_buffer := 0; { and reset bit-buffer to empty }
  249. entropy^.put_bits := 0;
  250. end;
  251. { Emit (or just count) a Huffman symbol. }
  252. {LOCAL}
  253. procedure emit_symbol (entropy : phuff_entropy_ptr;
  254. tbl_no : int;
  255. symbol : int); {INLINE}
  256. var
  257. tbl : c_derived_tbl_ptr;
  258. begin
  259. if (entropy^.gather_statistics) then
  260. Inc(entropy^.count_ptrs[tbl_no]^[symbol])
  261. else
  262. begin
  263. tbl := entropy^.derived_tbls[tbl_no];
  264. emit_bits(entropy, tbl^.ehufco[symbol], tbl^.ehufsi[symbol]);
  265. end;
  266. end;
  267. { Emit bits from a correction bit buffer. }
  268. {LOCAL}
  269. procedure emit_buffered_bits (entropy : phuff_entropy_ptr;
  270. bufstart : JBytePtr;
  271. nbits : uInt);
  272. var
  273. bufptr : byteptr;
  274. begin
  275. if (entropy^.gather_statistics) then
  276. exit; { no real work }
  277. bufptr := byteptr(bufstart);
  278. while (nbits > 0) do
  279. begin
  280. emit_bits(entropy, uInt(bufptr^), 1);
  281. Inc(bufptr);
  282. Dec(nbits);
  283. end;
  284. end;
  285. { Emit any pending EOBRUN symbol. }
  286. {LOCAL}
  287. procedure emit_eobrun (entropy : phuff_entropy_ptr);
  288. var
  289. {register} temp, nbits : int;
  290. begin
  291. if (entropy^.EOBRUN > 0) then
  292. begin { if there is any pending EOBRUN }
  293. temp := entropy^.EOBRUN;
  294. nbits := 0;
  295. temp := temp shr 1;
  296. while (temp <> 0) do
  297. begin
  298. Inc(nbits);
  299. temp := temp shr 1;
  300. end;
  301. { safety check: shouldn't happen given limited correction-bit buffer }
  302. if (nbits > 14) then
  303. ERREXIT(j_common_ptr(entropy^.cinfo), JERR_HUFF_MISSING_CODE);
  304. emit_symbol(entropy, entropy^.ac_tbl_no, nbits shl 4);
  305. if (nbits <> 0) then
  306. emit_bits(entropy, entropy^.EOBRUN, nbits);
  307. entropy^.EOBRUN := 0;
  308. { Emit any buffered correction bits }
  309. emit_buffered_bits(entropy, entropy^.bit_buffer, entropy^.BE);
  310. entropy^.BE := 0;
  311. end;
  312. end;
  313. { Emit a restart marker & resynchronize predictions. }
  314. {LOCAL}
  315. procedure emit_restart (entropy : phuff_entropy_ptr;
  316. restart_num : int);
  317. var
  318. ci : int;
  319. begin
  320. emit_eobrun(entropy);
  321. if (not entropy^.gather_statistics) then
  322. begin
  323. flush_bits(entropy);
  324. {emit_byte(entropy, $FF);}
  325. { Outputting bytes to the file.
  326. NB: these must be called only when actually outputting,
  327. that is, entropy^.gather_statistics = FALSE. }
  328. entropy^.next_output_byte^ := JOCTET($FF);
  329. Inc(entropy^.next_output_byte);
  330. Dec(entropy^.free_in_buffer);
  331. if (entropy^.free_in_buffer = 0) then
  332. dump_buffer(entropy);
  333. {emit_byte(entropy, JPEG_RST0 + restart_num);}
  334. entropy^.next_output_byte^ := JOCTET(JPEG_RST0 + restart_num);
  335. Inc(entropy^.next_output_byte);
  336. Dec(entropy^.free_in_buffer);
  337. if (entropy^.free_in_buffer = 0) then
  338. dump_buffer(entropy);
  339. end;
  340. if (entropy^.cinfo^.Ss = 0) then
  341. begin
  342. { Re-initialize DC predictions to 0 }
  343. for ci := 0 to pred(entropy^.cinfo^.comps_in_scan) do
  344. entropy^.last_dc_val[ci] := 0;
  345. end
  346. else
  347. begin
  348. { Re-initialize all AC-related fields to 0 }
  349. entropy^.EOBRUN := 0;
  350. entropy^.BE := 0;
  351. end;
  352. end;
  353. { MCU encoding for DC initial scan (either spectral selection,
  354. or first pass of successive approximation). }
  355. {METHODDEF}
  356. function encode_mcu_DC_first (cinfo : j_compress_ptr;
  357. const MCU_data: array of JBLOCKROW) : boolean;
  358. var
  359. entropy : phuff_entropy_ptr;
  360. {register} temp, temp2 : int;
  361. {register} nbits : int;
  362. blkn, ci : int;
  363. Al : int;
  364. block : JBLOCK_PTR;
  365. compptr : jpeg_component_info_ptr;
  366. ishift_temp : int;
  367. begin
  368. entropy := phuff_entropy_ptr (cinfo^.entropy);
  369. Al := cinfo^.Al;
  370. entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
  371. entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
  372. { Emit restart marker if needed }
  373. if (cinfo^.restart_interval <> 0) then
  374. if (entropy^.restarts_to_go = 0) then
  375. emit_restart(entropy, entropy^.next_restart_num);
  376. { Encode the MCU data blocks }
  377. for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  378. begin
  379. block := JBLOCK_PTR(MCU_data[blkn]);
  380. ci := cinfo^.MCU_membership[blkn];
  381. compptr := cinfo^.cur_comp_info[ci];
  382. { Compute the DC value after the required point transform by Al.
  383. This is simply an arithmetic right shift. }
  384. {temp2 := IRIGHT_SHIFT( int(block^[0]), Al);}
  385. {IRIGHT_SHIFT_IS_UNSIGNED}
  386. ishift_temp := int(block^[0]);
  387. if ishift_temp < 0 then
  388. temp2 := (ishift_temp shr Al) or ((not 0) shl (16-Al))
  389. else
  390. temp2 := ishift_temp shr Al;
  391. { DC differences are figured on the point-transformed values. }
  392. temp := temp2 - entropy^.last_dc_val[ci];
  393. entropy^.last_dc_val[ci] := temp2;
  394. { Encode the DC coefficient difference per section G.1.2.1 }
  395. temp2 := temp;
  396. if (temp < 0) then
  397. begin
  398. temp := -temp; { temp is abs value of input }
  399. { For a negative input, want temp2 := bitwise complement of abs(input) }
  400. { This code assumes we are on a two's complement machine }
  401. Dec(temp2);
  402. end;
  403. { Find the number of bits needed for the magnitude of the coefficient }
  404. nbits := 0;
  405. while (temp <> 0) do
  406. begin
  407. Inc(nbits);
  408. temp := temp shr 1;
  409. end;
  410. { Check for out-of-range coefficient values.
  411. Since we're encoding a difference, the range limit is twice as much. }
  412. if (nbits > MAX_COEF_BITS+1) then
  413. ERREXIT(j_common_ptr(cinfo), JERR_BAD_DCT_COEF);
  414. { Count/emit the Huffman-coded symbol for the number of bits }
  415. emit_symbol(entropy, compptr^.dc_tbl_no, nbits);
  416. { Emit that number of bits of the value, if positive, }
  417. { or the complement of its magnitude, if negative. }
  418. if (nbits <> 0) then { emit_bits rejects calls with size 0 }
  419. emit_bits(entropy, uInt(temp2), nbits);
  420. end;
  421. cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
  422. cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
  423. { Update restart-interval state too }
  424. if (cinfo^.restart_interval <> 0) then
  425. begin
  426. if (entropy^.restarts_to_go = 0) then
  427. begin
  428. entropy^.restarts_to_go := cinfo^.restart_interval;
  429. Inc(entropy^.next_restart_num);
  430. with entropy^ do
  431. next_restart_num := next_restart_num and 7;
  432. end;
  433. Dec(entropy^.restarts_to_go);
  434. end;
  435. encode_mcu_DC_first := TRUE;
  436. end;
  437. { MCU encoding for AC initial scan (either spectral selection,
  438. or first pass of successive approximation). }
  439. {METHODDEF}
  440. function encode_mcu_AC_first (cinfo : j_compress_ptr;
  441. const MCU_data: array of JBLOCKROW) : boolean;
  442. var
  443. entropy : phuff_entropy_ptr;
  444. {register} temp, temp2 : int;
  445. {register} nbits : int;
  446. {register} r, k : int;
  447. Se : int;
  448. Al : int;
  449. block : JBLOCK_PTR;
  450. begin
  451. entropy := phuff_entropy_ptr (cinfo^.entropy);
  452. Se := cinfo^.Se;
  453. Al := cinfo^.Al;
  454. entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
  455. entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
  456. { Emit restart marker if needed }
  457. if (cinfo^.restart_interval <> 0) then
  458. if (entropy^.restarts_to_go = 0) then
  459. emit_restart(entropy, entropy^.next_restart_num);
  460. { Encode the MCU data block }
  461. block := JBLOCK_PTR(MCU_data[0]);
  462. { Encode the AC coefficients per section G.1.2.2, fig. G.3 }
  463. r := 0; { r := run length of zeros }
  464. for k := cinfo^.Ss to Se do
  465. begin
  466. temp := (block^[jpeg_natural_order[k]]);
  467. if (temp = 0) then
  468. begin
  469. Inc(r);
  470. continue;
  471. end;
  472. { We must apply the point transform by Al. For AC coefficients this
  473. is an integer division with rounding towards 0. To do this portably
  474. in C, we shift after obtaining the absolute value; so the code is
  475. interwoven with finding the abs value (temp) and output bits (temp2). }
  476. if (temp < 0) then
  477. begin
  478. temp := -temp; { temp is abs value of input }
  479. temp := temp shr Al; { apply the point transform }
  480. { For a negative coef, want temp2 := bitwise complement of abs(coef) }
  481. temp2 := not temp;
  482. end
  483. else
  484. begin
  485. temp := temp shr Al; { apply the point transform }
  486. temp2 := temp;
  487. end;
  488. { Watch out for case that nonzero coef is zero after point transform }
  489. if (temp = 0) then
  490. begin
  491. Inc(r);
  492. continue;
  493. end;
  494. { Emit any pending EOBRUN }
  495. if (entropy^.EOBRUN > 0) then
  496. emit_eobrun(entropy);
  497. { if run length > 15, must emit special run-length-16 codes ($F0) }
  498. while (r > 15) do
  499. begin
  500. emit_symbol(entropy, entropy^.ac_tbl_no, $F0);
  501. Dec(r, 16);
  502. end;
  503. { Find the number of bits needed for the magnitude of the coefficient }
  504. nbits := 0; { there must be at least one 1 bit }
  505. repeat
  506. Inc(nbits);
  507. temp := temp shr 1;
  508. until (temp = 0);
  509. { Check for out-of-range coefficient values }
  510. if (nbits > MAX_COEF_BITS) then
  511. ERREXIT(j_common_ptr(cinfo), JERR_BAD_DCT_COEF);
  512. { Count/emit Huffman symbol for run length / number of bits }
  513. emit_symbol(entropy, entropy^.ac_tbl_no, (r shl 4) + nbits);
  514. { Emit that number of bits of the value, if positive, }
  515. { or the complement of its magnitude, if negative. }
  516. emit_bits(entropy, uInt(temp2), nbits);
  517. r := 0; { reset zero run length }
  518. end;
  519. if (r > 0) then
  520. begin { If there are trailing zeroes, }
  521. Inc(entropy^.EOBRUN); { count an EOB }
  522. if (entropy^.EOBRUN = $7FFF) then
  523. emit_eobrun(entropy); { force it out to avoid overflow }
  524. end;
  525. cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
  526. cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
  527. { Update restart-interval state too }
  528. if (cinfo^.restart_interval <> 0) then
  529. begin
  530. if (entropy^.restarts_to_go = 0) then
  531. begin
  532. entropy^.restarts_to_go := cinfo^.restart_interval;
  533. Inc(entropy^.next_restart_num);
  534. with entropy^ do
  535. next_restart_num := next_restart_num and 7;
  536. end;
  537. Dec(entropy^.restarts_to_go);
  538. end;
  539. encode_mcu_AC_first := TRUE;
  540. end;
  541. { MCU encoding for DC successive approximation refinement scan.
  542. Note: we assume such scans can be multi-component, although the spec
  543. is not very clear on the point. }
  544. {METHODDEF}
  545. function encode_mcu_DC_refine (cinfo : j_compress_ptr;
  546. const MCU_data: array of JBLOCKROW) : boolean;
  547. var
  548. entropy : phuff_entropy_ptr;
  549. {register} temp : int;
  550. blkn : int;
  551. Al : int;
  552. block : JBLOCK_PTR;
  553. begin
  554. entropy := phuff_entropy_ptr (cinfo^.entropy);
  555. Al := cinfo^.Al;
  556. entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
  557. entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
  558. { Emit restart marker if needed }
  559. if (cinfo^.restart_interval <> 0) then
  560. if (entropy^.restarts_to_go = 0) then
  561. emit_restart(entropy, entropy^.next_restart_num);
  562. { Encode the MCU data blocks }
  563. for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  564. begin
  565. block := JBLOCK_PTR(MCU_data[blkn]);
  566. { We simply emit the Al'th bit of the DC coefficient value. }
  567. temp := block^[0];
  568. emit_bits(entropy, uInt(temp shr Al), 1);
  569. end;
  570. cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
  571. cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
  572. { Update restart-interval state too }
  573. if (cinfo^.restart_interval <> 0) then
  574. begin
  575. if (entropy^.restarts_to_go = 0) then
  576. begin
  577. entropy^.restarts_to_go := cinfo^.restart_interval;
  578. Inc(entropy^.next_restart_num);
  579. with entropy^ do
  580. next_restart_num := next_restart_num and 7;
  581. end;
  582. Dec(entropy^.restarts_to_go);
  583. end;
  584. encode_mcu_DC_refine := TRUE;
  585. end;
  586. { MCU encoding for AC successive approximation refinement scan. }
  587. {METHODDEF}
  588. function encode_mcu_AC_refine (cinfo : j_compress_ptr;
  589. const MCU_data: array of JBLOCKROW) : boolean;
  590. var
  591. entropy : phuff_entropy_ptr;
  592. {register} temp : int;
  593. {register} r, k : int;
  594. EOB : int;
  595. BR_buffer : JBytePtr;
  596. BR : uInt;
  597. Se : int;
  598. Al : int;
  599. block : JBLOCK_PTR;
  600. absvalues : array[0..DCTSIZE2-1] of int;
  601. begin
  602. entropy := phuff_entropy_ptr(cinfo^.entropy);
  603. Se := cinfo^.Se;
  604. Al := cinfo^.Al;
  605. entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
  606. entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
  607. { Emit restart marker if needed }
  608. if (cinfo^.restart_interval <> 0) then
  609. if (entropy^.restarts_to_go = 0) then
  610. emit_restart(entropy, entropy^.next_restart_num);
  611. { Encode the MCU data block }
  612. block := JBLOCK_PTR(MCU_data[0]);
  613. { It is convenient to make a pre-pass to determine the transformed
  614. coefficients' absolute values and the EOB position. }
  615. EOB := 0;
  616. for k := cinfo^.Ss to Se do
  617. begin
  618. temp := block^[jpeg_natural_order[k]];
  619. { We must apply the point transform by Al. For AC coefficients this
  620. is an integer division with rounding towards 0. To do this portably
  621. in C, we shift after obtaining the absolute value. }
  622. if (temp < 0) then
  623. temp := -temp; { temp is abs value of input }
  624. temp := temp shr Al; { apply the point transform }
  625. absvalues[k] := temp; { save abs value for main pass }
  626. if (temp = 1) then
  627. EOB := k; { EOB := index of last newly-nonzero coef }
  628. end;
  629. { Encode the AC coefficients per section G.1.2.3, fig. G.7 }
  630. r := 0; { r := run length of zeros }
  631. BR := 0; { BR := count of buffered bits added now }
  632. BR_buffer := JBytePtr(@(entropy^.bit_buffer^[entropy^.BE]));
  633. { Append bits to buffer }
  634. for k := cinfo^.Ss to Se do
  635. begin
  636. temp := absvalues[k];
  637. if (temp = 0) then
  638. begin
  639. Inc(r);
  640. continue;
  641. end;
  642. { Emit any required ZRLs, but not if they can be folded into EOB }
  643. while (r > 15) and (k <= EOB) do
  644. begin
  645. { emit any pending EOBRUN and the BE correction bits }
  646. emit_eobrun(entropy);
  647. { Emit ZRL }
  648. emit_symbol(entropy, entropy^.ac_tbl_no, $F0);
  649. Dec(r, 16);
  650. { Emit buffered correction bits that must be associated with ZRL }
  651. emit_buffered_bits(entropy, BR_buffer, BR);
  652. BR_buffer := entropy^.bit_buffer; { BE bits are gone now }
  653. BR := 0;
  654. end;
  655. { If the coef was previously nonzero, it only needs a correction bit.
  656. NOTE: a straight translation of the spec's figure G.7 would suggest
  657. that we also need to test r > 15. But if r > 15, we can only get here
  658. if k > EOB, which implies that this coefficient is not 1. }
  659. if (temp > 1) then
  660. begin
  661. { The correction bit is the next bit of the absolute value. }
  662. BR_buffer^[BR] := byte (temp and 1);
  663. Inc(BR);
  664. continue;
  665. end;
  666. { Emit any pending EOBRUN and the BE correction bits }
  667. emit_eobrun(entropy);
  668. { Count/emit Huffman symbol for run length / number of bits }
  669. emit_symbol(entropy, entropy^.ac_tbl_no, (r shl 4) + 1);
  670. { Emit output bit for newly-nonzero coef }
  671. if (block^[jpeg_natural_order[k]] < 0) then
  672. temp := 0
  673. else
  674. temp := 1;
  675. emit_bits(entropy, uInt(temp), 1);
  676. { Emit buffered correction bits that must be associated with this code }
  677. emit_buffered_bits(entropy, BR_buffer, BR);
  678. BR_buffer := entropy^.bit_buffer; { BE bits are gone now }
  679. BR := 0;
  680. r := 0; { reset zero run length }
  681. end;
  682. if (r > 0) or (BR > 0) then
  683. begin { If there are trailing zeroes, }
  684. Inc(entropy^.EOBRUN); { count an EOB }
  685. Inc(entropy^.BE, BR); { concat my correction bits to older ones }
  686. { We force out the EOB if we risk either:
  687. 1. overflow of the EOB counter;
  688. 2. overflow of the correction bit buffer during the next MCU. }
  689. if (entropy^.EOBRUN = $7FFF) or
  690. (entropy^.BE > (MAX_CORR_BITS-DCTSIZE2+1)) then
  691. emit_eobrun(entropy);
  692. end;
  693. cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
  694. cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
  695. { Update restart-interval state too }
  696. if (cinfo^.restart_interval <> 0) then
  697. begin
  698. if (entropy^.restarts_to_go = 0) then
  699. begin
  700. entropy^.restarts_to_go := cinfo^.restart_interval;
  701. Inc(entropy^.next_restart_num);
  702. with entropy^ do
  703. next_restart_num := next_restart_num and 7;
  704. end;
  705. Dec(entropy^.restarts_to_go);
  706. end;
  707. encode_mcu_AC_refine := TRUE;
  708. end;
  709. { Finish up at the end of a Huffman-compressed progressive scan. }
  710. {METHODDEF}
  711. procedure finish_pass_phuff (cinfo : j_compress_ptr);
  712. var
  713. entropy : phuff_entropy_ptr;
  714. begin
  715. entropy := phuff_entropy_ptr (cinfo^.entropy);
  716. entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
  717. entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
  718. { Flush out any buffered data }
  719. emit_eobrun(entropy);
  720. flush_bits(entropy);
  721. cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
  722. cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
  723. end;
  724. { Finish up a statistics-gathering pass and create the new Huffman tables. }
  725. {METHODDEF}
  726. procedure finish_pass_gather_phuff (cinfo : j_compress_ptr);
  727. var
  728. entropy : phuff_entropy_ptr;
  729. is_DC_band : boolean;
  730. ci, tbl : int;
  731. compptr : jpeg_component_info_ptr;
  732. htblptr : ^JHUFF_TBL_PTR;
  733. did : array[0..NUM_HUFF_TBLS-1] of boolean;
  734. begin
  735. tbl := 0;
  736. entropy := phuff_entropy_ptr (cinfo^.entropy);
  737. { Flush out buffered data (all we care about is counting the EOB symbol) }
  738. emit_eobrun(entropy);
  739. is_DC_band := (cinfo^.Ss = 0);
  740. { It's important not to apply jpeg_gen_optimal_table more than once
  741. per table, because it clobbers the input frequency counts! }
  742. MEMZERO(@did, SIZEOF(did));
  743. for ci := 0 to pred(cinfo^.comps_in_scan) do
  744. begin
  745. compptr := cinfo^.cur_comp_info[ci];
  746. if (is_DC_band) then
  747. begin
  748. if (cinfo^.Ah <> 0) then { DC refinement needs no table }
  749. continue;
  750. tbl := compptr^.dc_tbl_no;
  751. end
  752. else
  753. begin
  754. tbl := compptr^.ac_tbl_no;
  755. end;
  756. if (not did[tbl]) then
  757. begin
  758. if (is_DC_band) then
  759. htblptr := @(cinfo^.dc_huff_tbl_ptrs[tbl])
  760. else
  761. htblptr := @(cinfo^.ac_huff_tbl_ptrs[tbl]);
  762. if (htblptr^ = NIL) then
  763. htblptr^ := jpeg_alloc_huff_table(j_common_ptr(cinfo));
  764. jpeg_gen_optimal_table(cinfo, htblptr^, entropy^.count_ptrs[tbl]^);
  765. did[tbl] := TRUE;
  766. end;
  767. end;
  768. end;
  769. { Module initialization routine for progressive Huffman entropy encoding. }
  770. {GLOBAL}
  771. procedure jinit_phuff_encoder (cinfo : j_compress_ptr);
  772. var
  773. entropy : phuff_entropy_ptr;
  774. i : int;
  775. begin
  776. entropy := phuff_entropy_ptr(
  777. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  778. SIZEOF(phuff_entropy_encoder)) );
  779. cinfo^.entropy := jpeg_entropy_encoder_ptr(entropy);
  780. entropy^.pub.start_pass := start_pass_phuff;
  781. { Mark tables unallocated }
  782. for i := 0 to pred(NUM_HUFF_TBLS) do
  783. begin
  784. entropy^.derived_tbls[i] := NIL;
  785. entropy^.count_ptrs[i] := NIL;
  786. end;
  787. entropy^.bit_buffer := NIL; { needed only in AC refinement scan }
  788. end;
  789. end.