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.

377 lines
12 KiB

3 years ago
  1. unit imjdapistd;
  2. { Original : jdapistd.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  3. { This file is part of the Independent JPEG Group's software.
  4. For conditions of distribution and use, see the accompanying README file.
  5. This file contains application interface code for the decompression half
  6. of the JPEG library. These are the "standard" API routines that are
  7. used in the normal full-decompression case. They are not used by a
  8. transcoding-only application. Note that if an application links in
  9. jpeg_start_decompress, it will end up linking in the entire decompressor.
  10. We thus must separate this file from jdapimin.c to avoid linking the
  11. whole decompression library into a transcoder. }
  12. interface
  13. {$I imjconfig.inc}
  14. uses
  15. imjmorecfg,
  16. imjinclude,
  17. imjdeferr,
  18. imjerror,
  19. imjpeglib,
  20. imjdmaster;
  21. { Read some scanlines of data from the JPEG decompressor.
  22. The return value will be the number of lines actually read.
  23. This may be less than the number requested in several cases,
  24. including bottom of image, data source suspension, and operating
  25. modes that emit multiple scanlines at a time.
  26. Note: we warn about excess calls to jpeg_read_scanlines() since
  27. this likely signals an application programmer error. However,
  28. an oversize buffer (max_lines > scanlines remaining) is not an error. }
  29. {GLOBAL}
  30. function jpeg_read_scanlines (cinfo : j_decompress_ptr;
  31. scanlines : JSAMPARRAY;
  32. max_lines : JDIMENSION) : JDIMENSION;
  33. { Alternate entry point to read raw data.
  34. Processes exactly one iMCU row per call, unless suspended. }
  35. {GLOBAL}
  36. function jpeg_read_raw_data (cinfo : j_decompress_ptr;
  37. data : JSAMPIMAGE;
  38. max_lines : JDIMENSION) : JDIMENSION;
  39. {$ifdef D_MULTISCAN_FILES_SUPPORTED}
  40. { Initialize for an output pass in buffered-image mode. }
  41. {GLOBAL}
  42. function jpeg_start_output (cinfo : j_decompress_ptr;
  43. scan_number : int) : boolean;
  44. { Finish up after an output pass in buffered-image mode.
  45. Returns FALSE if suspended. The return value need be inspected only if
  46. a suspending data source is used. }
  47. {GLOBAL}
  48. function jpeg_finish_output (cinfo : j_decompress_ptr) : boolean;
  49. {$endif} { D_MULTISCAN_FILES_SUPPORTED }
  50. { Decompression initialization.
  51. jpeg_read_header must be completed before calling this.
  52. If a multipass operating mode was selected, this will do all but the
  53. last pass, and thus may take a great deal of time.
  54. Returns FALSE if suspended. The return value need be inspected only if
  55. a suspending data source is used. }
  56. {GLOBAL}
  57. function jpeg_start_decompress (cinfo : j_decompress_ptr) : boolean;
  58. implementation
  59. { Forward declarations }
  60. {LOCAL}
  61. function output_pass_setup (cinfo : j_decompress_ptr) : boolean; forward;
  62. { Decompression initialization.
  63. jpeg_read_header must be completed before calling this.
  64. If a multipass operating mode was selected, this will do all but the
  65. last pass, and thus may take a great deal of time.
  66. Returns FALSE if suspended. The return value need be inspected only if
  67. a suspending data source is used. }
  68. {GLOBAL}
  69. function jpeg_start_decompress (cinfo : j_decompress_ptr) : boolean;
  70. var
  71. retcode : int;
  72. begin
  73. if (cinfo^.global_state = DSTATE_READY) then
  74. begin
  75. { First call: initialize master control, select active modules }
  76. jinit_master_decompress(cinfo);
  77. if (cinfo^.buffered_image) then
  78. begin
  79. { No more work here; expecting jpeg_start_output next }
  80. cinfo^.global_state := DSTATE_BUFIMAGE;
  81. jpeg_start_decompress := TRUE;
  82. exit;
  83. end;
  84. cinfo^.global_state := DSTATE_PRELOAD;
  85. end;
  86. if (cinfo^.global_state = DSTATE_PRELOAD) then
  87. begin
  88. { If file has multiple scans, absorb them all into the coef buffer }
  89. if (cinfo^.inputctl^.has_multiple_scans) then
  90. begin
  91. {$ifdef D_MULTISCAN_FILES_SUPPORTED}
  92. while TRUE do
  93. begin
  94. { Call progress monitor hook if present }
  95. if (cinfo^.progress <> NIL) then
  96. cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  97. { Absorb some more input }
  98. retcode := cinfo^.inputctl^.consume_input (cinfo);
  99. if (retcode = JPEG_SUSPENDED) then
  100. begin
  101. jpeg_start_decompress := FALSE;
  102. exit;
  103. end;
  104. if (retcode = JPEG_REACHED_EOI) then
  105. break;
  106. { Advance progress counter if appropriate }
  107. if (cinfo^.progress <> NIL) and
  108. ((retcode = JPEG_ROW_COMPLETED) or (retcode = JPEG_REACHED_SOS)) then
  109. begin
  110. Inc(cinfo^.progress^.pass_counter);
  111. if (cinfo^.progress^.pass_counter >= cinfo^.progress^.pass_limit) then
  112. begin
  113. { jdmaster underestimated number of scans; ratchet up one scan }
  114. Inc(cinfo^.progress^.pass_limit, long(cinfo^.total_iMCU_rows));
  115. end;
  116. end;
  117. end;
  118. {$else}
  119. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  120. {$endif} { D_MULTISCAN_FILES_SUPPORTED }
  121. end;
  122. cinfo^.output_scan_number := cinfo^.input_scan_number;
  123. end
  124. else
  125. if (cinfo^.global_state <> DSTATE_PRESCAN) then
  126. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  127. { Perform any dummy output passes, and set up for the final pass }
  128. jpeg_start_decompress := output_pass_setup(cinfo);
  129. end;
  130. { Set up for an output pass, and perform any dummy pass(es) needed.
  131. Common subroutine for jpeg_start_decompress and jpeg_start_output.
  132. Entry: global_state := DSTATE_PRESCAN only if previously suspended.
  133. Exit: If done, returns TRUE and sets global_state for proper output mode.
  134. If suspended, returns FALSE and sets global_state := DSTATE_PRESCAN. }
  135. {LOCAL}
  136. function output_pass_setup (cinfo : j_decompress_ptr) : boolean;
  137. var
  138. last_scanline : JDIMENSION;
  139. begin
  140. if (cinfo^.global_state <> DSTATE_PRESCAN) then
  141. begin
  142. { First call: do pass setup }
  143. cinfo^.master^.prepare_for_output_pass (cinfo);
  144. cinfo^.output_scanline := 0;
  145. cinfo^.global_state := DSTATE_PRESCAN;
  146. end;
  147. { Loop over any required dummy passes }
  148. while (cinfo^.master^.is_dummy_pass) do
  149. begin
  150. {$ifdef QUANT_2PASS_SUPPORTED}
  151. { Crank through the dummy pass }
  152. while (cinfo^.output_scanline < cinfo^.output_height) do
  153. begin
  154. { Call progress monitor hook if present }
  155. if (cinfo^.progress <> NIL) then
  156. begin
  157. cinfo^.progress^.pass_counter := long (cinfo^.output_scanline);
  158. cinfo^.progress^.pass_limit := long (cinfo^.output_height);
  159. cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  160. end;
  161. { Process some data }
  162. last_scanline := cinfo^.output_scanline;
  163. cinfo^.main^.process_data (cinfo, JSAMPARRAY(NIL),
  164. cinfo^.output_scanline, {var}
  165. JDIMENSION(0));
  166. if (cinfo^.output_scanline = last_scanline) then
  167. begin
  168. output_pass_setup := FALSE; { No progress made, must suspend }
  169. exit;
  170. end;
  171. end;
  172. { Finish up dummy pass, and set up for another one }
  173. cinfo^.master^.finish_output_pass (cinfo);
  174. cinfo^.master^.prepare_for_output_pass (cinfo);
  175. cinfo^.output_scanline := 0;
  176. {$else}
  177. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  178. {$endif} { QUANT_2PASS_SUPPORTED }
  179. end;
  180. { Ready for application to drive output pass through
  181. jpeg_read_scanlines or jpeg_read_raw_data. }
  182. if cinfo^.raw_data_out then
  183. cinfo^.global_state := DSTATE_RAW_OK
  184. else
  185. cinfo^.global_state := DSTATE_SCANNING;
  186. output_pass_setup := TRUE;
  187. end;
  188. { Read some scanlines of data from the JPEG decompressor.
  189. The return value will be the number of lines actually read.
  190. This may be less than the number requested in several cases,
  191. including bottom of image, data source suspension, and operating
  192. modes that emit multiple scanlines at a time.
  193. Note: we warn about excess calls to jpeg_read_scanlines() since
  194. this likely signals an application programmer error. However,
  195. an oversize buffer (max_lines > scanlines remaining) is not an error. }
  196. {GLOBAL}
  197. function jpeg_read_scanlines (cinfo : j_decompress_ptr;
  198. scanlines : JSAMPARRAY;
  199. max_lines : JDIMENSION) : JDIMENSION;
  200. var
  201. row_ctr : JDIMENSION;
  202. begin
  203. if (cinfo^.global_state <> DSTATE_SCANNING) then
  204. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  205. if (cinfo^.output_scanline >= cinfo^.output_height) then
  206. begin
  207. WARNMS(j_common_ptr(cinfo), JWRN_TOO_MUCH_DATA);
  208. jpeg_read_scanlines := 0;
  209. exit;
  210. end;
  211. { Call progress monitor hook if present }
  212. if (cinfo^.progress <> NIL) then
  213. begin
  214. cinfo^.progress^.pass_counter := long (cinfo^.output_scanline);
  215. cinfo^.progress^.pass_limit := long (cinfo^.output_height);
  216. cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  217. end;
  218. { Process some data }
  219. row_ctr := 0;
  220. cinfo^.main^.process_data (cinfo, scanlines, {var}row_ctr, max_lines);
  221. Inc(cinfo^.output_scanline, row_ctr);
  222. jpeg_read_scanlines := row_ctr;
  223. end;
  224. { Alternate entry point to read raw data.
  225. Processes exactly one iMCU row per call, unless suspended. }
  226. {GLOBAL}
  227. function jpeg_read_raw_data (cinfo : j_decompress_ptr;
  228. data : JSAMPIMAGE;
  229. max_lines : JDIMENSION) : JDIMENSION;
  230. var
  231. lines_per_iMCU_row : JDIMENSION;
  232. begin
  233. if (cinfo^.global_state <> DSTATE_RAW_OK) then
  234. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  235. if (cinfo^.output_scanline >= cinfo^.output_height) then
  236. begin
  237. WARNMS(j_common_ptr(cinfo), JWRN_TOO_MUCH_DATA);
  238. jpeg_read_raw_data := 0;
  239. exit;
  240. end;
  241. { Call progress monitor hook if present }
  242. if (cinfo^.progress <> NIL) then
  243. begin
  244. cinfo^.progress^.pass_counter := long (cinfo^.output_scanline);
  245. cinfo^.progress^.pass_limit := long (cinfo^.output_height);
  246. cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  247. end;
  248. { Verify that at least one iMCU row can be returned. }
  249. lines_per_iMCU_row := cinfo^.max_v_samp_factor * cinfo^.min_DCT_scaled_size;
  250. if (max_lines < lines_per_iMCU_row) then
  251. ERREXIT(j_common_ptr(cinfo), JERR_BUFFER_SIZE);
  252. { Decompress directly into user's buffer. }
  253. if (cinfo^.coef^.decompress_data (cinfo, data) = 0) then
  254. begin
  255. jpeg_read_raw_data := 0; { suspension forced, can do nothing more }
  256. exit;
  257. end;
  258. { OK, we processed one iMCU row. }
  259. Inc(cinfo^.output_scanline, lines_per_iMCU_row);
  260. jpeg_read_raw_data := lines_per_iMCU_row;
  261. end;
  262. { Additional entry points for buffered-image mode. }
  263. {$ifdef D_MULTISCAN_FILES_SUPPORTED}
  264. { Initialize for an output pass in buffered-image mode. }
  265. {GLOBAL}
  266. function jpeg_start_output (cinfo : j_decompress_ptr;
  267. scan_number : int) : boolean;
  268. begin
  269. if (cinfo^.global_state <> DSTATE_BUFIMAGE) and
  270. (cinfo^.global_state <> DSTATE_PRESCAN) then
  271. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  272. { Limit scan number to valid range }
  273. if (scan_number <= 0) then
  274. scan_number := 1;
  275. if (cinfo^.inputctl^.eoi_reached) and
  276. (scan_number > cinfo^.input_scan_number) then
  277. scan_number := cinfo^.input_scan_number;
  278. cinfo^.output_scan_number := scan_number;
  279. { Perform any dummy output passes, and set up for the real pass }
  280. jpeg_start_output := output_pass_setup(cinfo);
  281. end;
  282. { Finish up after an output pass in buffered-image mode.
  283. Returns FALSE if suspended. The return value need be inspected only if
  284. a suspending data source is used. }
  285. {GLOBAL}
  286. function jpeg_finish_output (cinfo : j_decompress_ptr) : boolean;
  287. begin
  288. if ((cinfo^.global_state = DSTATE_SCANNING) or
  289. (cinfo^.global_state = DSTATE_RAW_OK) and cinfo^.buffered_image) then
  290. begin
  291. { Terminate this pass. }
  292. { We do not require the whole pass to have been completed. }
  293. cinfo^.master^.finish_output_pass (cinfo);
  294. cinfo^.global_state := DSTATE_BUFPOST;
  295. end
  296. else
  297. if (cinfo^.global_state <> DSTATE_BUFPOST) then
  298. begin
  299. { BUFPOST := repeat call after a suspension, anything else is error }
  300. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  301. end;
  302. { Read markers looking for SOS or EOI }
  303. while (cinfo^.input_scan_number <= cinfo^.output_scan_number) and
  304. (not cinfo^.inputctl^.eoi_reached) do
  305. begin
  306. if (cinfo^.inputctl^.consume_input (cinfo) = JPEG_SUSPENDED) then
  307. begin
  308. jpeg_finish_output := FALSE; { Suspend, come back later }
  309. exit;
  310. end;
  311. end;
  312. cinfo^.global_state := DSTATE_BUFIMAGE;
  313. jpeg_finish_output := TRUE;
  314. end;
  315. {$endif} { D_MULTISCAN_FILES_SUPPORTED }
  316. end.