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.

343 lines
10 KiB

3 years ago
  1. unit imjcmainct;
  2. { This file contains the main buffer controller for compression.
  3. The main buffer lies between the pre-processor and the JPEG
  4. compressor proper; it holds downsampled data in the JPEG colorspace. }
  5. { Original : jcmainct.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  6. interface
  7. {$I imjconfig.inc}
  8. { Note: currently, there is no operating mode in which a full-image buffer
  9. is needed at this step. If there were, that mode could not be used with
  10. "raw data" input, since this module is bypassed in that case. However,
  11. we've left the code here for possible use in special applications. }
  12. {$undef FULL_MAIN_BUFFER_SUPPORTED}
  13. uses
  14. imjmorecfg,
  15. imjinclude,
  16. imjdeferr,
  17. imjerror,
  18. {$ifdef FULL_MAIN_BUFFER_SUPPORTED}
  19. imjutils,
  20. {$endif}
  21. imjpeglib;
  22. { Initialize main buffer controller. }
  23. {GLOBAL}
  24. procedure jinit_c_main_controller (cinfo : j_compress_ptr;
  25. need_full_buffer : boolean);
  26. implementation
  27. { Private buffer controller object }
  28. type
  29. my_main_ptr = ^my_main_controller;
  30. my_main_controller = record
  31. pub : jpeg_c_main_controller; { public fields }
  32. cur_iMCU_row : JDIMENSION; { number of current iMCU row }
  33. rowgroup_ctr : JDIMENSION; { counts row groups received in iMCU row }
  34. suspended : boolean; { remember if we suspended output }
  35. pass_mode : J_BUF_MODE; { current operating mode }
  36. { If using just a strip buffer, this points to the entire set of buffers
  37. (we allocate one for each component). In the full-image case, this
  38. points to the currently accessible strips of the virtual arrays. }
  39. buffer : array[0..MAX_COMPONENTS-1] of JSAMPARRAY;
  40. {$ifdef FULL_MAIN_BUFFER_SUPPORTED}
  41. { If using full-image storage, this array holds pointers to virtual-array
  42. control blocks for each component. Unused if not full-image storage. }
  43. whole_image : array[0..MAX_COMPONENTS-1] of jvirt_sarray_ptr;
  44. {$endif}
  45. end; {my_main_controller}
  46. { Forward declarations }
  47. {METHODDEF}
  48. procedure process_data_simple_main(cinfo : j_compress_ptr;
  49. input_buf : JSAMPARRAY;
  50. var in_row_ctr: JDIMENSION;
  51. in_rows_avail : JDIMENSION); forward;
  52. {$ifdef FULL_MAIN_BUFFER_SUPPORTED}
  53. {METHODDEF}
  54. procedure process_data_buffer_main(cinfo : j_compress_ptr;
  55. input_buf : JSAMPARRAY;
  56. var in_row_ctr : JDIMENSION;
  57. in_rows_avail : JDIMENSION); forward;
  58. {$endif}
  59. { Initialize for a processing pass. }
  60. {METHODDEF}
  61. procedure start_pass_main (cinfo : j_compress_ptr;
  62. pass_mode : J_BUF_MODE);
  63. var
  64. main : my_main_ptr;
  65. begin
  66. main := my_main_ptr (cinfo^.main);
  67. { Do nothing in raw-data mode. }
  68. if (cinfo^.raw_data_in) then
  69. exit;
  70. main^.cur_iMCU_row := 0; { initialize counters }
  71. main^.rowgroup_ctr := 0;
  72. main^.suspended := FALSE;
  73. main^.pass_mode := pass_mode; { save mode for use by process_data }
  74. case (pass_mode) of
  75. JBUF_PASS_THRU:
  76. begin
  77. {$ifdef FULL_MAIN_BUFFER_SUPPORTED}
  78. if (main^.whole_image[0] <> NIL) then
  79. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  80. {$endif}
  81. main^.pub.process_data := process_data_simple_main;
  82. end;
  83. {$ifdef FULL_MAIN_BUFFER_SUPPORTED}
  84. JBUF_SAVE_SOURCE,
  85. JBUF_CRANK_DEST,
  86. JBUF_SAVE_AND_PASS:
  87. begin
  88. if (main^.whole_image[0] = NIL) then
  89. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  90. main^.pub.process_data := process_data_buffer_main;
  91. end;
  92. {$endif}
  93. else
  94. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  95. end;
  96. end;
  97. { Process some data.
  98. This routine handles the simple pass-through mode,
  99. where we have only a strip buffer. }
  100. {METHODDEF}
  101. procedure process_data_simple_main (cinfo : j_compress_ptr;
  102. input_buf : JSAMPARRAY;
  103. var in_row_ctr : JDIMENSION;
  104. in_rows_avail : JDIMENSION);
  105. var
  106. main : my_main_ptr;
  107. begin
  108. main := my_main_ptr (cinfo^.main);
  109. while (main^.cur_iMCU_row < cinfo^.total_iMCU_rows) do
  110. begin
  111. { Read input data if we haven't filled the main buffer yet }
  112. if (main^.rowgroup_ctr < DCTSIZE) then
  113. cinfo^.prep^.pre_process_data (cinfo,
  114. input_buf,
  115. in_row_ctr,
  116. in_rows_avail,
  117. JSAMPIMAGE(@main^.buffer),
  118. main^.rowgroup_ctr,
  119. JDIMENSION(DCTSIZE));
  120. { If we don't have a full iMCU row buffered, return to application for
  121. more data. Note that preprocessor will always pad to fill the iMCU row
  122. at the bottom of the image. }
  123. if (main^.rowgroup_ctr <> DCTSIZE) then
  124. exit;
  125. { Send the completed row to the compressor }
  126. if (not cinfo^.coef^.compress_data (cinfo, JSAMPIMAGE(@main^.buffer))) then
  127. begin
  128. { If compressor did not consume the whole row, then we must need to
  129. suspend processing and return to the application. In this situation
  130. we pretend we didn't yet consume the last input row; otherwise, if
  131. it happened to be the last row of the image, the application would
  132. think we were done. }
  133. if (not main^.suspended) then
  134. begin
  135. Dec(in_row_ctr);
  136. main^.suspended := TRUE;
  137. end;
  138. exit;
  139. end;
  140. { We did finish the row. Undo our little suspension hack if a previous
  141. call suspended; then mark the main buffer empty. }
  142. if (main^.suspended) then
  143. begin
  144. Inc(in_row_ctr);
  145. main^.suspended := FALSE;
  146. end;
  147. main^.rowgroup_ctr := 0;
  148. Inc(main^.cur_iMCU_row);
  149. end;
  150. end;
  151. {$ifdef FULL_MAIN_BUFFER_SUPPORTED}
  152. { Process some data.
  153. This routine handles all of the modes that use a full-size buffer. }
  154. {METHODDEF}
  155. procedure process_data_buffer_main (cinfo : j_compress_ptr;
  156. input_buf : JSAMPARRAY;
  157. var in_row_ctr : JDIMENSION;
  158. in_rows_avail : JDIMENSION);
  159. var
  160. main : my_main_ptr;
  161. ci : int;
  162. compptr : jpeg_component_info_ptr;
  163. writing : boolean;
  164. begin
  165. main := my_main_ptr (cinfo^.main);
  166. writing := (main^.pass_mode <> JBUF_CRANK_DEST);
  167. while (main^.cur_iMCU_row < cinfo^.total_iMCU_rows) do
  168. begin
  169. { Realign the virtual buffers if at the start of an iMCU row. }
  170. if (main^.rowgroup_ctr = 0) then
  171. begin
  172. compptr := cinfo^.comp_info;
  173. for ci := 0 to pred(cinfo^.num_components) do
  174. begin
  175. main^.buffer[ci] := cinfo^.mem^.access_virt_sarray
  176. (j_common_ptr (cinfo), main^.whole_image[ci],
  177. main^.cur_iMCU_row * (compptr^.v_samp_factor * DCTSIZE),
  178. JDIMENSION (compptr^.v_samp_factor * DCTSIZE), writing);
  179. Inc(compptr);
  180. end;
  181. { In a read pass, pretend we just read some source data. }
  182. if (not writing) then
  183. begin
  184. Inc(in_row_ctr, cinfo^.max_v_samp_factor * DCTSIZE);
  185. main^.rowgroup_ctr := DCTSIZE;
  186. end;
  187. end;
  188. { If a write pass, read input data until the current iMCU row is full. }
  189. { Note: preprocessor will pad if necessary to fill the last iMCU row. }
  190. if (writing) then
  191. begin
  192. cinfo^.prep^.pre_process_data (cinfo,
  193. input_buf, in_row_ctr, in_rows_avail,
  194. JSAMPIMAGE(@main^.buffer),
  195. main^.rowgroup_ctr,
  196. JDIMENSION (DCTSIZE));
  197. { Return to application if we need more data to fill the iMCU row. }
  198. if (main^.rowgroup_ctr < DCTSIZE) then
  199. exit;
  200. end;
  201. { Emit data, unless this is a sink-only pass. }
  202. if (main^.pass_mode <> JBUF_SAVE_SOURCE) then
  203. begin
  204. if (not cinfo^.coef^.compress_data (cinfo,
  205. JSAMPIMAGE(@main^.buffer))) then
  206. begin
  207. { If compressor did not consume the whole row, then we must need to
  208. suspend processing and return to the application. In this situation
  209. we pretend we didn't yet consume the last input row; otherwise, if
  210. it happened to be the last row of the image, the application would
  211. think we were done. }
  212. if (not main^.suspended) then
  213. begin
  214. Dec(in_row_ctr);
  215. main^.suspended := TRUE;
  216. end;
  217. exit;
  218. end;
  219. { We did finish the row. Undo our little suspension hack if a previous
  220. call suspended; then mark the main buffer empty. }
  221. if (main^.suspended) then
  222. begin
  223. Inc(in_row_ctr);
  224. main^.suspended := FALSE;
  225. end;
  226. end;
  227. { If get here, we are done with this iMCU row. Mark buffer empty. }
  228. main^.rowgroup_ctr := 0;
  229. Inc(main^.cur_iMCU_row);
  230. end;
  231. end;
  232. {$endif} { FULL_MAIN_BUFFER_SUPPORTED }
  233. { Initialize main buffer controller. }
  234. {GLOBAL}
  235. procedure jinit_c_main_controller (cinfo : j_compress_ptr;
  236. need_full_buffer : boolean);
  237. var
  238. main : my_main_ptr;
  239. ci : int;
  240. compptr : jpeg_component_info_ptr;
  241. begin
  242. main := my_main_ptr(
  243. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  244. SIZEOF(my_main_controller)) );
  245. cinfo^.main := jpeg_c_main_controller_ptr(main);
  246. main^.pub.start_pass := start_pass_main;
  247. { We don't need to create a buffer in raw-data mode. }
  248. if (cinfo^.raw_data_in) then
  249. exit;
  250. { Create the buffer. It holds downsampled data, so each component
  251. may be of a different size. }
  252. if (need_full_buffer) then
  253. begin
  254. {$ifdef FULL_MAIN_BUFFER_SUPPORTED}
  255. { Allocate a full-image virtual array for each component }
  256. { Note we pad the bottom to a multiple of the iMCU height }
  257. compptr := cinfo^.comp_info;
  258. for ci := 0 to pred(cinfo^.num_components) do
  259. begin
  260. main^.whole_image[ci] := cinfo^.mem^.request_virt_sarray
  261. (j_common_ptr(cinfo), JPOOL_IMAGE, FALSE,
  262. compptr^.width_in_blocks * DCTSIZE,
  263. JDIMENSION (jround_up( long (compptr^.height_in_blocks),
  264. long (compptr^.v_samp_factor)) * DCTSIZE),
  265. JDIMENSION (compptr^.v_samp_factor * DCTSIZE));
  266. Inc(compptr);
  267. end;
  268. {$else}
  269. ERREXIT(j_common_ptr(cinfo), JERR_BAD_BUFFER_MODE);
  270. {$endif}
  271. end
  272. else
  273. begin
  274. {$ifdef FULL_MAIN_BUFFER_SUPPORTED}
  275. main^.whole_image[0] := NIL; { flag for no virtual arrays }
  276. {$endif}
  277. { Allocate a strip buffer for each component }
  278. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  279. for ci := 0 to pred(cinfo^.num_components) do
  280. begin
  281. main^.buffer[ci] := cinfo^.mem^.alloc_sarray
  282. (j_common_ptr(cinfo), JPOOL_IMAGE,
  283. compptr^.width_in_blocks * DCTSIZE,
  284. JDIMENSION (compptr^.v_samp_factor * DCTSIZE));
  285. Inc(compptr);
  286. end;
  287. end;
  288. end;
  289. end.