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.

406 lines
14 KiB

3 years ago
  1. unit imjcprepct;
  2. { Original : jcprepct.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  3. { This file contains the compression preprocessing controller.
  4. This controller manages the color conversion, downsampling,
  5. and edge expansion steps.
  6. Most of the complexity here is associated with buffering input rows
  7. as required by the downsampler. See the comments at the head of
  8. jcsample.c for the downsampler's needs. }
  9. interface
  10. {$I imjconfig.inc}
  11. uses
  12. imjmorecfg,
  13. imjpeglib,
  14. imjdeferr,
  15. imjerror,
  16. imjinclude,
  17. imjutils;
  18. {GLOBAL}
  19. procedure jinit_c_prep_controller (cinfo : j_compress_ptr;
  20. need_full_buffer : boolean);
  21. implementation
  22. { At present, jcsample.c can request context rows only for smoothing.
  23. In the future, we might also need context rows for CCIR601 sampling
  24. or other more-complex downsampling procedures. The code to support
  25. context rows should be compiled only if needed. }
  26. {$ifdef INPUT_SMOOTHING_SUPPORTED}
  27. {$define CONTEXT_ROWS_SUPPORTED}
  28. {$endif}
  29. { For the simple (no-context-row) case, we just need to buffer one
  30. row group's worth of pixels for the downsampling step. At the bottom of
  31. the image, we pad to a full row group by replicating the last pixel row.
  32. The downsampler's last output row is then replicated if needed to pad
  33. out to a full iMCU row.
  34. When providing context rows, we must buffer three row groups' worth of
  35. pixels. Three row groups are physically allocated, but the row pointer
  36. arrays are made five row groups high, with the extra pointers above and
  37. below "wrapping around" to point to the last and first real row groups.
  38. This allows the downsampler to access the proper context rows.
  39. At the top and bottom of the image, we create dummy context rows by
  40. copying the first or last real pixel row. This copying could be avoided
  41. by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
  42. trouble on the compression side. }
  43. { Private buffer controller object }
  44. type
  45. my_prep_ptr = ^my_prep_controller;
  46. my_prep_controller = record
  47. pub : jpeg_c_prep_controller; { public fields }
  48. { Downsampling input buffer. This buffer holds color-converted data
  49. until we have enough to do a downsample step. }
  50. color_buf : array[0..MAX_COMPONENTS-1] of JSAMPARRAY;
  51. rows_to_go : JDIMENSION; { counts rows remaining in source image }
  52. next_buf_row : int; { index of next row to store in color_buf }
  53. {$ifdef CONTEXT_ROWS_SUPPORTED} { only needed for context case }
  54. this_row_group : int; { starting row index of group to process }
  55. next_buf_stop : int; { downsample when we reach this index }
  56. {$endif}
  57. end; {my_prep_controller;}
  58. { Initialize for a processing pass. }
  59. {METHODDEF}
  60. procedure start_pass_prep (cinfo : j_compress_ptr;
  61. pass_mode : J_BUF_MODE );
  62. var
  63. prep : my_prep_ptr;
  64. begin
  65. prep := my_prep_ptr (cinfo^.prep);
  66. if (pass_mode <> JBUF_PASS_THRU) then
  67. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  68. { Initialize total-height counter for detecting bottom of image }
  69. prep^.rows_to_go := cinfo^.image_height;
  70. { Mark the conversion buffer empty }
  71. prep^.next_buf_row := 0;
  72. {$ifdef CONTEXT_ROWS_SUPPORTED}
  73. { Preset additional state variables for context mode.
  74. These aren't used in non-context mode, so we needn't test which mode. }
  75. prep^.this_row_group := 0;
  76. { Set next_buf_stop to stop after two row groups have been read in. }
  77. prep^.next_buf_stop := 2 * cinfo^.max_v_samp_factor;
  78. {$endif}
  79. end;
  80. { Expand an image vertically from height input_rows to height output_rows,
  81. by duplicating the bottom row. }
  82. {LOCAL}
  83. procedure expand_bottom_edge (image_data : JSAMPARRAY;
  84. num_cols : JDIMENSION;
  85. input_rows : int;
  86. output_rows : int);
  87. var
  88. {register} row : int;
  89. begin
  90. for row := input_rows to pred(output_rows) do
  91. begin
  92. jcopy_sample_rows(image_data, input_rows-1, image_data, row,
  93. 1, num_cols);
  94. end;
  95. end;
  96. { Process some data in the simple no-context case.
  97. Preprocessor output data is counted in "row groups". A row group
  98. is defined to be v_samp_factor sample rows of each component.
  99. Downsampling will produce this much data from each max_v_samp_factor
  100. input rows. }
  101. {METHODDEF}
  102. procedure pre_process_data (cinfo : j_compress_ptr;
  103. input_buf : JSAMPARRAY;
  104. var in_row_ctr : JDIMENSION;
  105. in_rows_avail : JDIMENSION;
  106. output_buf : JSAMPIMAGE;
  107. var out_row_group_ctr : JDIMENSION;
  108. out_row_groups_avail : JDIMENSION);
  109. var
  110. prep : my_prep_ptr;
  111. numrows, ci : int;
  112. inrows : JDIMENSION;
  113. compptr : jpeg_component_info_ptr;
  114. var
  115. local_input_buf : JSAMPARRAY;
  116. begin
  117. prep := my_prep_ptr (cinfo^.prep);
  118. while (in_row_ctr < in_rows_avail) and
  119. (out_row_group_ctr < out_row_groups_avail) do
  120. begin
  121. { Do color conversion to fill the conversion buffer. }
  122. inrows := in_rows_avail - in_row_ctr;
  123. numrows := cinfo^.max_v_samp_factor - prep^.next_buf_row;
  124. {numrows := int( MIN(JDIMENSION(numrows), inrows) );}
  125. if inrows < JDIMENSION(numrows) then
  126. numrows := int(inrows);
  127. local_input_buf := JSAMPARRAY(@(input_buf^[in_row_ctr]));
  128. cinfo^.cconvert^.color_convert (cinfo, local_input_buf,
  129. JSAMPIMAGE(@prep^.color_buf),
  130. JDIMENSION(prep^.next_buf_row),
  131. numrows);
  132. Inc(in_row_ctr, numrows);
  133. Inc(prep^.next_buf_row, numrows);
  134. Dec(prep^.rows_to_go, numrows);
  135. { If at bottom of image, pad to fill the conversion buffer. }
  136. if (prep^.rows_to_go = 0) and
  137. (prep^.next_buf_row < cinfo^.max_v_samp_factor) then
  138. begin
  139. for ci := 0 to pred(cinfo^.num_components) do
  140. begin
  141. expand_bottom_edge(prep^.color_buf[ci], cinfo^.image_width,
  142. prep^.next_buf_row, cinfo^.max_v_samp_factor);
  143. end;
  144. prep^.next_buf_row := cinfo^.max_v_samp_factor;
  145. end;
  146. { If we've filled the conversion buffer, empty it. }
  147. if (prep^.next_buf_row = cinfo^.max_v_samp_factor) then
  148. begin
  149. cinfo^.downsample^.downsample (cinfo,
  150. JSAMPIMAGE(@prep^.color_buf),
  151. JDIMENSION (0),
  152. output_buf,
  153. out_row_group_ctr);
  154. prep^.next_buf_row := 0;
  155. Inc(out_row_group_ctr);;
  156. end;
  157. { If at bottom of image, pad the output to a full iMCU height.
  158. Note we assume the caller is providing a one-iMCU-height output buffer! }
  159. if (prep^.rows_to_go = 0) and
  160. (out_row_group_ctr < out_row_groups_avail) then
  161. begin
  162. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  163. for ci := 0 to pred(cinfo^.num_components) do
  164. begin
  165. expand_bottom_edge(output_buf^[ci],
  166. compptr^.width_in_blocks * DCTSIZE,
  167. int (out_row_group_ctr) * compptr^.v_samp_factor,
  168. int (out_row_groups_avail) * compptr^.v_samp_factor);
  169. Inc(compptr);
  170. end;
  171. out_row_group_ctr := out_row_groups_avail;
  172. break; { can exit outer loop without test }
  173. end;
  174. end;
  175. end;
  176. {$ifdef CONTEXT_ROWS_SUPPORTED}
  177. { Process some data in the context case. }
  178. {METHODDEF}
  179. procedure pre_process_context (cinfo : j_compress_ptr;
  180. input_buf : JSAMPARRAY;
  181. var in_row_ctr : JDIMENSION;
  182. in_rows_avail : JDIMENSION;
  183. output_buf : JSAMPIMAGE;
  184. var out_row_group_ctr : JDIMENSION;
  185. out_row_groups_avail : JDIMENSION);
  186. var
  187. prep : my_prep_ptr;
  188. numrows, ci : int;
  189. buf_height : int;
  190. inrows : JDIMENSION;
  191. var
  192. row : int;
  193. begin
  194. prep := my_prep_ptr (cinfo^.prep);
  195. buf_height := cinfo^.max_v_samp_factor * 3;
  196. while (out_row_group_ctr < out_row_groups_avail) do
  197. begin
  198. if (in_row_ctr < in_rows_avail) then
  199. begin
  200. { Do color conversion to fill the conversion buffer. }
  201. inrows := in_rows_avail - in_row_ctr;
  202. numrows := prep^.next_buf_stop - prep^.next_buf_row;
  203. {numrows := int ( MIN( JDIMENSION(numrows), inrows) );}
  204. if inrows < JDIMENSION(numrows) then
  205. numrows := int(inrows);
  206. cinfo^.cconvert^.color_convert (cinfo,
  207. JSAMPARRAY(@input_buf^[in_row_ctr]),
  208. JSAMPIMAGE(@prep^.color_buf),
  209. JDIMENSION (prep^.next_buf_row),
  210. numrows);
  211. { Pad at top of image, if first time through }
  212. if (prep^.rows_to_go = cinfo^.image_height) then
  213. begin
  214. for ci := 0 to pred(cinfo^.num_components) do
  215. begin
  216. for row := 1 to cinfo^.max_v_samp_factor do
  217. begin
  218. jcopy_sample_rows(prep^.color_buf[ci], 0,
  219. prep^.color_buf[ci], -row,
  220. 1, cinfo^.image_width);
  221. end;
  222. end;
  223. end;
  224. Inc(in_row_ctr, numrows);
  225. Inc(prep^.next_buf_row, numrows);
  226. Dec(prep^.rows_to_go, numrows);
  227. end
  228. else
  229. begin
  230. { Return for more data, unless we are at the bottom of the image. }
  231. if (prep^.rows_to_go <> 0) then
  232. break;
  233. { When at bottom of image, pad to fill the conversion buffer. }
  234. if (prep^.next_buf_row < prep^.next_buf_stop) then
  235. begin
  236. for ci := 0 to pred(cinfo^.num_components) do
  237. begin
  238. expand_bottom_edge(prep^.color_buf[ci], cinfo^.image_width,
  239. prep^.next_buf_row, prep^.next_buf_stop);
  240. end;
  241. prep^.next_buf_row := prep^.next_buf_stop;
  242. end;
  243. end;
  244. { If we've gotten enough data, downsample a row group. }
  245. if (prep^.next_buf_row = prep^.next_buf_stop) then
  246. begin
  247. cinfo^.downsample^.downsample (cinfo,
  248. JSAMPIMAGE(@prep^.color_buf),
  249. JDIMENSION(prep^.this_row_group),
  250. output_buf,
  251. out_row_group_ctr);
  252. Inc(out_row_group_ctr);
  253. { Advance pointers with wraparound as necessary. }
  254. Inc(prep^.this_row_group, cinfo^.max_v_samp_factor);
  255. if (prep^.this_row_group >= buf_height) then
  256. prep^.this_row_group := 0;
  257. if (prep^.next_buf_row >= buf_height) then
  258. prep^.next_buf_row := 0;
  259. prep^.next_buf_stop := prep^.next_buf_row + cinfo^.max_v_samp_factor;
  260. end;
  261. end;
  262. end;
  263. { Create the wrapped-around downsampling input buffer needed for context mode. }
  264. {LOCAL}
  265. procedure create_context_buffer (cinfo : j_compress_ptr);
  266. var
  267. prep : my_prep_ptr;
  268. rgroup_height : int;
  269. ci, i : int;
  270. compptr : jpeg_component_info_ptr;
  271. true_buffer, fake_buffer : JSAMPARRAY;
  272. begin
  273. prep := my_prep_ptr (cinfo^.prep);
  274. rgroup_height := cinfo^.max_v_samp_factor;
  275. { Grab enough space for fake row pointers for all the components;
  276. we need five row groups' worth of pointers for each component. }
  277. fake_buffer := JSAMPARRAY(
  278. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  279. (cinfo^.num_components * 5 * rgroup_height) *
  280. SIZEOF(JSAMPROW)) );
  281. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  282. for ci := 0 to pred(cinfo^.num_components) do
  283. begin
  284. { Allocate the actual buffer space (3 row groups) for this component.
  285. We make the buffer wide enough to allow the downsampler to edge-expand
  286. horizontally within the buffer, if it so chooses. }
  287. true_buffer := cinfo^.mem^.alloc_sarray
  288. (j_common_ptr(cinfo), JPOOL_IMAGE,
  289. JDIMENSION (( long(compptr^.width_in_blocks) * DCTSIZE *
  290. cinfo^.max_h_samp_factor) div compptr^.h_samp_factor),
  291. JDIMENSION (3 * rgroup_height));
  292. { Copy true buffer row pointers into the middle of the fake row array }
  293. MEMCOPY(JSAMPARRAY(@ fake_buffer^[rgroup_height]), true_buffer,
  294. 3 * rgroup_height * SIZEOF(JSAMPROW));
  295. { Fill in the above and below wraparound pointers }
  296. for i := 0 to pred(rgroup_height) do
  297. begin
  298. fake_buffer^[i] := true_buffer^[2 * rgroup_height + i];
  299. fake_buffer^[4 * rgroup_height + i] := true_buffer^[i];
  300. end;
  301. prep^.color_buf[ci] := JSAMPARRAY(@ fake_buffer^[rgroup_height]);
  302. Inc(JSAMPROW_PTR(fake_buffer), 5 * rgroup_height); { point to space for next component }
  303. Inc(compptr);
  304. end;
  305. end;
  306. {$endif} { CONTEXT_ROWS_SUPPORTED }
  307. { Initialize preprocessing controller. }
  308. {GLOBAL}
  309. procedure jinit_c_prep_controller (cinfo : j_compress_ptr;
  310. need_full_buffer : boolean);
  311. var
  312. prep : my_prep_ptr;
  313. ci : int;
  314. compptr : jpeg_component_info_ptr;
  315. begin
  316. if (need_full_buffer) then { safety check }
  317. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  318. prep := my_prep_ptr(
  319. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  320. SIZEOF(my_prep_controller)) );
  321. cinfo^.prep := jpeg_c_prep_controller_ptr(prep);
  322. prep^.pub.start_pass := start_pass_prep;
  323. { Allocate the color conversion buffer.
  324. We make the buffer wide enough to allow the downsampler to edge-expand
  325. horizontally within the buffer, if it so chooses. }
  326. if (cinfo^.downsample^.need_context_rows) then
  327. begin
  328. { Set up to provide context rows }
  329. {$ifdef CONTEXT_ROWS_SUPPORTED}
  330. prep^.pub.pre_process_data := pre_process_context;
  331. create_context_buffer(cinfo);
  332. {$else}
  333. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  334. {$endif}
  335. end
  336. else
  337. begin
  338. { No context, just make it tall enough for one row group }
  339. prep^.pub.pre_process_data := pre_process_data;
  340. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  341. for ci := 0 to pred(cinfo^.num_components) do
  342. begin
  343. prep^.color_buf[ci] := cinfo^.mem^.alloc_sarray
  344. (j_common_ptr(cinfo), JPOOL_IMAGE,
  345. JDIMENSION (( long(compptr^.width_in_blocks) * DCTSIZE *
  346. cinfo^.max_h_samp_factor) div compptr^.h_samp_factor),
  347. JDIMENSION(cinfo^.max_v_samp_factor) );
  348. Inc(compptr);
  349. end;
  350. end;
  351. end;
  352. end.