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.

521 lines
18 KiB

3 years ago
  1. unit imjccoefct;
  2. { This file contains the coefficient buffer controller for compression.
  3. This controller is the top level of the JPEG compressor proper.
  4. The coefficient buffer lies between forward-DCT and entropy encoding steps.}
  5. { Original: jccoefct.c; Copyright (C) 1994-1997, Thomas G. Lane. }
  6. interface
  7. {$I imjconfig.inc}
  8. uses
  9. imjmorecfg,
  10. imjinclude,
  11. imjerror,
  12. imjdeferr,
  13. imjutils,
  14. imjpeglib;
  15. { We use a full-image coefficient buffer when doing Huffman optimization,
  16. and also for writing multiple-scan JPEG files. In all cases, the DCT
  17. step is run during the first pass, and subsequent passes need only read
  18. the buffered coefficients. }
  19. {$ifdef ENTROPY_OPT_SUPPORTED}
  20. {$define FULL_COEF_BUFFER_SUPPORTED}
  21. {$else}
  22. {$ifdef C_MULTISCAN_FILES_SUPPORTED}
  23. {$define FULL_COEF_BUFFER_SUPPORTED}
  24. {$endif}
  25. {$endif}
  26. { Initialize coefficient buffer controller. }
  27. {GLOBAL}
  28. procedure jinit_c_coef_controller (cinfo : j_compress_ptr;
  29. need_full_buffer : boolean);
  30. implementation
  31. { Private buffer controller object }
  32. type
  33. my_coef_ptr = ^my_coef_controller;
  34. my_coef_controller = record
  35. pub : jpeg_c_coef_controller; { public fields }
  36. iMCU_row_num : JDIMENSION; { iMCU row # within image }
  37. mcu_ctr : JDIMENSION; { counts MCUs processed in current row }
  38. MCU_vert_offset : int; { counts MCU rows within iMCU row }
  39. MCU_rows_per_iMCU_row : int; { number of such rows needed }
  40. { For single-pass compression, it's sufficient to buffer just one MCU
  41. (although this may prove a bit slow in practice). We allocate a
  42. workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
  43. MCU constructed and sent. (On 80x86, the workspace is FAR even though
  44. it's not really very big; this is to keep the module interfaces unchanged
  45. when a large coefficient buffer is necessary.)
  46. In multi-pass modes, this array points to the current MCU's blocks
  47. within the virtual arrays. }
  48. MCU_buffer : array[0..C_MAX_BLOCKS_IN_MCU-1] of JBLOCKROW;
  49. { In multi-pass modes, we need a virtual block array for each component. }
  50. whole_image : array[0..MAX_COMPONENTS-1] of jvirt_barray_ptr;
  51. end;
  52. { Forward declarations }
  53. {METHODDEF}
  54. function compress_data(cinfo : j_compress_ptr;
  55. input_buf : JSAMPIMAGE) : boolean; forward;
  56. {$ifdef FULL_COEF_BUFFER_SUPPORTED}
  57. {METHODDEF}
  58. function compress_first_pass(cinfo : j_compress_ptr;
  59. input_buf : JSAMPIMAGE) : boolean; forward;
  60. {METHODDEF}
  61. function compress_output(cinfo : j_compress_ptr;
  62. input_buf : JSAMPIMAGE) : boolean; forward;
  63. {$endif}
  64. {LOCAL}
  65. procedure start_iMCU_row (cinfo : j_compress_ptr);
  66. { Reset within-iMCU-row counters for a new row }
  67. var
  68. coef : my_coef_ptr;
  69. begin
  70. coef := my_coef_ptr (cinfo^.coef);
  71. { In an interleaved scan, an MCU row is the same as an iMCU row.
  72. In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  73. But at the bottom of the image, process only what's left. }
  74. if (cinfo^.comps_in_scan > 1) then
  75. begin
  76. coef^.MCU_rows_per_iMCU_row := 1;
  77. end
  78. else
  79. begin
  80. if (coef^.iMCU_row_num < (cinfo^.total_iMCU_rows-1)) then
  81. coef^.MCU_rows_per_iMCU_row := cinfo^.cur_comp_info[0]^.v_samp_factor
  82. else
  83. coef^.MCU_rows_per_iMCU_row := cinfo^.cur_comp_info[0]^.last_row_height;
  84. end;
  85. coef^.mcu_ctr := 0;
  86. coef^.MCU_vert_offset := 0;
  87. end;
  88. { Initialize for a processing pass. }
  89. {METHODDEF}
  90. procedure start_pass_coef (cinfo : j_compress_ptr;
  91. pass_mode : J_BUF_MODE);
  92. var
  93. coef : my_coef_ptr;
  94. begin
  95. coef := my_coef_ptr (cinfo^.coef);
  96. coef^.iMCU_row_num := 0;
  97. start_iMCU_row(cinfo);
  98. case (pass_mode) of
  99. JBUF_PASS_THRU:
  100. begin
  101. if (coef^.whole_image[0] <> NIL) then
  102. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  103. coef^.pub.compress_data := compress_data;
  104. end;
  105. {$ifdef FULL_COEF_BUFFER_SUPPORTED}
  106. JBUF_SAVE_AND_PASS:
  107. begin
  108. if (coef^.whole_image[0] = NIL) then
  109. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  110. coef^.pub.compress_data := compress_first_pass;
  111. end;
  112. JBUF_CRANK_DEST:
  113. begin
  114. if (coef^.whole_image[0] = NIL) then
  115. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  116. coef^.pub.compress_data := compress_output;
  117. end;
  118. {$endif}
  119. else
  120. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  121. end;
  122. end;
  123. { Process some data in the single-pass case.
  124. We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  125. per call, ie, v_samp_factor block rows for each component in the image.
  126. Returns TRUE if the iMCU row is completed, FALSE if suspended.
  127. NB: input_buf contains a plane for each component in image,
  128. which we index according to the component's SOF position. }
  129. {METHODDEF}
  130. function compress_data (cinfo : j_compress_ptr;
  131. input_buf : JSAMPIMAGE) : boolean;
  132. var
  133. coef : my_coef_ptr;
  134. MCU_col_num : JDIMENSION; { index of current MCU within row }
  135. last_MCU_col : JDIMENSION;
  136. last_iMCU_row : JDIMENSION;
  137. blkn, bi, ci, yindex, yoffset, blockcnt : int;
  138. ypos, xpos : JDIMENSION;
  139. compptr : jpeg_component_info_ptr;
  140. begin
  141. coef := my_coef_ptr (cinfo^.coef);
  142. last_MCU_col := cinfo^.MCUs_per_row - 1;
  143. last_iMCU_row := cinfo^.total_iMCU_rows - 1;
  144. { Loop to write as much as one whole iMCU row }
  145. for yoffset := coef^.MCU_vert_offset to pred(coef^.MCU_rows_per_iMCU_row) do
  146. begin
  147. for MCU_col_num := coef^.mcu_ctr to last_MCU_col do
  148. begin
  149. { Determine where data comes from in input_buf and do the DCT thing.
  150. Each call on forward_DCT processes a horizontal row of DCT blocks
  151. as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
  152. sequentially. Dummy blocks at the right or bottom edge are filled in
  153. specially. The data in them does not matter for image reconstruction,
  154. so we fill them with values that will encode to the smallest amount of
  155. data, viz: all zeroes in the AC entries, DC entries equal to previous
  156. block's DC value. (Thanks to Thomas Kinsman for this idea.) }
  157. blkn := 0;
  158. for ci := 0 to pred(cinfo^.comps_in_scan) do
  159. begin
  160. compptr := cinfo^.cur_comp_info[ci];
  161. if (MCU_col_num < last_MCU_col) then
  162. blockcnt := compptr^.MCU_width
  163. else
  164. blockcnt := compptr^.last_col_width;
  165. xpos := MCU_col_num * JDIMENSION(compptr^.MCU_sample_width);
  166. ypos := yoffset * DCTSIZE; { ypos = (yoffset+yindex) * DCTSIZE }
  167. for yindex := 0 to pred(compptr^.MCU_height) do
  168. begin
  169. if (coef^.iMCU_row_num < last_iMCU_row) or
  170. (yoffset+yindex < compptr^.last_row_height) then
  171. begin
  172. cinfo^.fdct^.forward_DCT (cinfo, compptr,
  173. input_buf^[compptr^.component_index],
  174. coef^.MCU_buffer[blkn],
  175. ypos, xpos, JDIMENSION (blockcnt));
  176. if (blockcnt < compptr^.MCU_width) then
  177. begin
  178. { Create some dummy blocks at the right edge of the image. }
  179. jzero_far({FAR}pointer(coef^.MCU_buffer[blkn + blockcnt]),
  180. (compptr^.MCU_width - blockcnt) * SIZEOF(JBLOCK));
  181. for bi := blockcnt to pred(compptr^.MCU_width) do
  182. begin
  183. coef^.MCU_buffer[blkn+bi]^[0][0] := coef^.MCU_buffer[blkn+bi-1]^[0][0];
  184. end;
  185. end;
  186. end
  187. else
  188. begin
  189. { Create a row of dummy blocks at the bottom of the image. }
  190. jzero_far({FAR}pointer(coef^.MCU_buffer[blkn]),
  191. compptr^.MCU_width * SIZEOF(JBLOCK));
  192. for bi := 0 to pred(compptr^.MCU_width) do
  193. begin
  194. coef^.MCU_buffer[blkn+bi]^[0][0] := coef^.MCU_buffer[blkn-1]^[0][0];
  195. end;
  196. end;
  197. Inc(blkn, compptr^.MCU_width);
  198. Inc(ypos, DCTSIZE);
  199. end;
  200. end;
  201. { Try to write the MCU. In event of a suspension failure, we will
  202. re-DCT the MCU on restart (a bit inefficient, could be fixed...) }
  203. if (not cinfo^.entropy^.encode_mcu (cinfo, JBLOCKARRAY(@coef^.MCU_buffer)^)) then
  204. begin
  205. { Suspension forced; update state counters and exit }
  206. coef^.MCU_vert_offset := yoffset;
  207. coef^.mcu_ctr := MCU_col_num;
  208. compress_data := FALSE;
  209. exit;
  210. end;
  211. end;
  212. { Completed an MCU row, but perhaps not an iMCU row }
  213. coef^.mcu_ctr := 0;
  214. end;
  215. { Completed the iMCU row, advance counters for next one }
  216. Inc(coef^.iMCU_row_num);
  217. start_iMCU_row(cinfo);
  218. compress_data := TRUE;
  219. end;
  220. {$ifdef FULL_COEF_BUFFER_SUPPORTED}
  221. { Process some data in the first pass of a multi-pass case.
  222. We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  223. per call, ie, v_samp_factor block rows for each component in the image.
  224. This amount of data is read from the source buffer, DCT'd and quantized,
  225. and saved into the virtual arrays. We also generate suitable dummy blocks
  226. as needed at the right and lower edges. (The dummy blocks are constructed
  227. in the virtual arrays, which have been padded appropriately.) This makes
  228. it possible for subsequent passes not to worry about real vs. dummy blocks.
  229. We must also emit the data to the entropy encoder. This is conveniently
  230. done by calling compress_output() after we've loaded the current strip
  231. of the virtual arrays.
  232. NB: input_buf contains a plane for each component in image. All
  233. components are DCT'd and loaded into the virtual arrays in this pass.
  234. However, it may be that only a subset of the components are emitted to
  235. the entropy encoder during this first pass; be careful about looking
  236. at the scan-dependent variables (MCU dimensions, etc). }
  237. {METHODDEF}
  238. function compress_first_pass (cinfo : j_compress_ptr;
  239. input_buf : JSAMPIMAGE) : boolean;
  240. var
  241. coef : my_coef_ptr;
  242. last_iMCU_row : JDIMENSION;
  243. blocks_across, MCUs_across, MCUindex : JDIMENSION;
  244. bi, ci, h_samp_factor, block_row, block_rows, ndummy : int;
  245. lastDC : JCOEF;
  246. compptr : jpeg_component_info_ptr;
  247. buffer : JBLOCKARRAY;
  248. thisblockrow, lastblockrow : JBLOCKROW;
  249. begin
  250. coef := my_coef_ptr (cinfo^.coef);
  251. last_iMCU_row := cinfo^.total_iMCU_rows - 1;
  252. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  253. for ci := 0 to pred(cinfo^.num_components) do
  254. begin
  255. { Align the virtual buffer for this component. }
  256. buffer := cinfo^.mem^.access_virt_barray
  257. (j_common_ptr(cinfo), coef^.whole_image[ci],
  258. coef^.iMCU_row_num * JDIMENSION(compptr^.v_samp_factor),
  259. JDIMENSION (compptr^.v_samp_factor), TRUE);
  260. { Count non-dummy DCT block rows in this iMCU row. }
  261. if (coef^.iMCU_row_num < last_iMCU_row) then
  262. block_rows := compptr^.v_samp_factor
  263. else
  264. begin
  265. { NB: can't use last_row_height here, since may not be set! }
  266. block_rows := int (compptr^.height_in_blocks) mod compptr^.v_samp_factor;
  267. if (block_rows = 0) then
  268. block_rows := compptr^.v_samp_factor;
  269. end;
  270. blocks_across := compptr^.width_in_blocks;
  271. h_samp_factor := compptr^.h_samp_factor;
  272. { Count number of dummy blocks to be added at the right margin. }
  273. ndummy := int (blocks_across) mod h_samp_factor;
  274. if (ndummy > 0) then
  275. ndummy := h_samp_factor - ndummy;
  276. { Perform DCT for all non-dummy blocks in this iMCU row. Each call
  277. on forward_DCT processes a complete horizontal row of DCT blocks. }
  278. for block_row := 0 to pred(block_rows) do
  279. begin
  280. thisblockrow := buffer^[block_row];
  281. cinfo^.fdct^.forward_DCT (cinfo, compptr,
  282. input_buf^[ci],
  283. thisblockrow,
  284. JDIMENSION (block_row * DCTSIZE),
  285. JDIMENSION (0),
  286. blocks_across);
  287. if (ndummy > 0) then
  288. begin
  289. { Create dummy blocks at the right edge of the image. }
  290. Inc(JBLOCK_PTR(thisblockrow), blocks_across); { => first dummy block }
  291. jzero_far({FAR}pointer(thisblockrow), ndummy * SIZEOF(JBLOCK));
  292. {lastDC := thisblockrow^[-1][0];}
  293. { work around Range Checking }
  294. Dec(JBLOCK_PTR(thisblockrow));
  295. lastDC := thisblockrow^[0][0];
  296. Inc(JBLOCK_PTR(thisblockrow));
  297. for bi := 0 to pred(ndummy) do
  298. begin
  299. thisblockrow^[bi][0] := lastDC;
  300. end;
  301. end;
  302. end;
  303. { If at end of image, create dummy block rows as needed.
  304. The tricky part here is that within each MCU, we want the DC values
  305. of the dummy blocks to match the last real block's DC value.
  306. This squeezes a few more bytes out of the resulting file... }
  307. if (coef^.iMCU_row_num = last_iMCU_row) then
  308. begin
  309. Inc(blocks_across, ndummy); { include lower right corner }
  310. MCUs_across := blocks_across div JDIMENSION(h_samp_factor);
  311. for block_row := block_rows to pred(compptr^.v_samp_factor) do
  312. begin
  313. thisblockrow := buffer^[block_row];
  314. lastblockrow := buffer^[block_row-1];
  315. jzero_far({FAR} pointer(thisblockrow),
  316. size_t(blocks_across * SIZEOF(JBLOCK)));
  317. for MCUindex := 0 to pred(MCUs_across) do
  318. begin
  319. lastDC := lastblockrow^[h_samp_factor-1][0];
  320. for bi := 0 to pred(h_samp_factor) do
  321. begin
  322. thisblockrow^[bi][0] := lastDC;
  323. end;
  324. Inc(JBLOCK_PTR(thisblockrow), h_samp_factor); { advance to next MCU in row }
  325. Inc(JBLOCK_PTR(lastblockrow), h_samp_factor);
  326. end;
  327. end;
  328. end;
  329. Inc(compptr);
  330. end;
  331. { NB: compress_output will increment iMCU_row_num if successful.
  332. A suspension return will result in redoing all the work above next time.}
  333. { Emit data to the entropy encoder, sharing code with subsequent passes }
  334. compress_first_pass := compress_output(cinfo, input_buf);
  335. end;
  336. { Process some data in subsequent passes of a multi-pass case.
  337. We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  338. per call, ie, v_samp_factor block rows for each component in the scan.
  339. The data is obtained from the virtual arrays and fed to the entropy coder.
  340. Returns TRUE if the iMCU row is completed, FALSE if suspended.
  341. NB: input_buf is ignored; it is likely to be a NIL pointer. }
  342. {METHODDEF}
  343. function compress_output (cinfo : j_compress_ptr;
  344. input_buf : JSAMPIMAGE) : boolean;
  345. var
  346. coef : my_coef_ptr;
  347. MCU_col_num : JDIMENSION; { index of current MCU within row }
  348. blkn, ci, xindex, yindex, yoffset : int;
  349. start_col : JDIMENSION;
  350. buffer : array[0..MAX_COMPS_IN_SCAN-1] of JBLOCKARRAY;
  351. buffer_ptr : JBLOCKROW;
  352. compptr : jpeg_component_info_ptr;
  353. begin
  354. coef := my_coef_ptr (cinfo^.coef);
  355. { Align the virtual buffers for the components used in this scan.
  356. NB: during first pass, this is safe only because the buffers will
  357. already be aligned properly, so jmemmgr.c won't need to do any I/O. }
  358. for ci := 0 to pred(cinfo^.comps_in_scan) do
  359. begin
  360. compptr := cinfo^.cur_comp_info[ci];
  361. buffer[ci] := cinfo^.mem^.access_virt_barray (
  362. j_common_ptr(cinfo), coef^.whole_image[compptr^.component_index],
  363. coef^.iMCU_row_num * JDIMENSION(compptr^.v_samp_factor),
  364. JDIMENSION (compptr^.v_samp_factor), FALSE);
  365. end;
  366. { Loop to process one whole iMCU row }
  367. for yoffset := coef^.MCU_vert_offset to pred(coef^.MCU_rows_per_iMCU_row) do
  368. begin
  369. for MCU_col_num := coef^.mcu_ctr to pred(cinfo^.MCUs_per_row) do
  370. begin
  371. { Construct list of pointers to DCT blocks belonging to this MCU }
  372. blkn := 0; { index of current DCT block within MCU }
  373. for ci := 0 to pred(cinfo^.comps_in_scan) do
  374. begin
  375. compptr := cinfo^.cur_comp_info[ci];
  376. start_col := MCU_col_num * JDIMENSION(compptr^.MCU_width);
  377. for yindex := 0 to pred(compptr^.MCU_height) do
  378. begin
  379. buffer_ptr := JBLOCKROW(@ buffer[ci]^[yindex+yoffset]^[start_col]);
  380. for xindex := 0 to pred(compptr^.MCU_width) do
  381. begin
  382. coef^.MCU_buffer[blkn] := buffer_ptr;
  383. Inc(blkn);
  384. Inc(JBLOCK_PTR(buffer_ptr));
  385. end;
  386. end;
  387. end;
  388. { Try to write the MCU. }
  389. if (not cinfo^.entropy^.encode_mcu (cinfo, coef^.MCU_buffer)) then
  390. begin
  391. { Suspension forced; update state counters and exit }
  392. coef^.MCU_vert_offset := yoffset;
  393. coef^.mcu_ctr := MCU_col_num;
  394. compress_output := FALSE;
  395. exit;
  396. end;
  397. end;
  398. { Completed an MCU row, but perhaps not an iMCU row }
  399. coef^.mcu_ctr := 0;
  400. end;
  401. { Completed the iMCU row, advance counters for next one }
  402. Inc(coef^.iMCU_row_num);
  403. start_iMCU_row(cinfo);
  404. compress_output := TRUE;
  405. end;
  406. {$endif} { FULL_COEF_BUFFER_SUPPORTED }
  407. { Initialize coefficient buffer controller. }
  408. {GLOBAL}
  409. procedure jinit_c_coef_controller (cinfo : j_compress_ptr;
  410. need_full_buffer : boolean);
  411. var
  412. coef : my_coef_ptr;
  413. var
  414. buffer : JBLOCKROW;
  415. i : int;
  416. var
  417. ci : int;
  418. compptr : jpeg_component_info_ptr;
  419. begin
  420. coef := my_coef_ptr (
  421. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  422. SIZEOF(my_coef_controller)) );
  423. cinfo^.coef := jpeg_c_coef_controller_ptr(coef);
  424. coef^.pub.start_pass := start_pass_coef;
  425. { Create the coefficient buffer. }
  426. if (need_full_buffer) then
  427. begin
  428. {$ifdef FULL_COEF_BUFFER_SUPPORTED}
  429. { Allocate a full-image virtual array for each component, }
  430. { padded to a multiple of samp_factor DCT blocks in each direction. }
  431. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  432. for ci := 0 to pred(cinfo^.num_components) do
  433. begin
  434. coef^.whole_image[ci] := cinfo^.mem^.request_virt_barray
  435. (j_common_ptr(cinfo), JPOOL_IMAGE, FALSE,
  436. JDIMENSION (jround_up( long (compptr^.width_in_blocks),
  437. long (compptr^.h_samp_factor) )),
  438. JDIMENSION (jround_up(long (compptr^.height_in_blocks),
  439. long (compptr^.v_samp_factor))),
  440. JDIMENSION (compptr^.v_samp_factor));
  441. Inc(compptr);
  442. end;
  443. {$else}
  444. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  445. {$endif}
  446. end
  447. else
  448. begin
  449. { We only need a single-MCU buffer. }
  450. buffer := JBLOCKROW (
  451. cinfo^.mem^.alloc_large (j_common_ptr(cinfo), JPOOL_IMAGE,
  452. C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)) );
  453. for i := 0 to pred(C_MAX_BLOCKS_IN_MCU) do
  454. begin
  455. coef^.MCU_buffer[i] := JBLOCKROW(@ buffer^[i]);
  456. end;
  457. coef^.whole_image[0] := NIL; { flag for no virtual arrays }
  458. end;
  459. end;
  460. end.