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.

503 lines
17 KiB

3 years ago
  1. unit imjdapimin;
  2. { This file contains application interface code for the decompression half
  3. of the JPEG library. These are the "minimum" API routines that may be
  4. needed in either the normal full-decompression case or the
  5. transcoding-only case.
  6. Most of the routines intended to be called directly by an application
  7. are in this file or in jdapistd.c. But also see jcomapi.c for routines
  8. shared by compression and decompression, and jdtrans.c for the transcoding
  9. case. }
  10. { Original : jdapimin.c ; Copyright (C) 1994-1998, Thomas G. Lane. }
  11. interface
  12. {$I imjconfig.inc}
  13. uses
  14. imjmorecfg,
  15. imjinclude,
  16. imjdeferr,
  17. imjerror,
  18. imjpeglib,
  19. imjmemmgr, imjdmarker, imjdinput, imjcomapi;
  20. { Nomssi }
  21. procedure jpeg_create_decompress(cinfo : j_decompress_ptr);
  22. { Initialization of a JPEG decompression object.
  23. The error manager must already be set up (in case memory manager fails). }
  24. {GLOBAL}
  25. procedure jpeg_CreateDecompress (cinfo : j_decompress_ptr;
  26. version : int;
  27. structsize : size_t);
  28. { Destruction of a JPEG decompression object }
  29. {GLOBAL}
  30. procedure jpeg_destroy_decompress (cinfo : j_decompress_ptr);
  31. { Decompression startup: read start of JPEG datastream to see what's there.
  32. Need only initialize JPEG object and supply a data source before calling.
  33. This routine will read as far as the first SOS marker (ie, actual start of
  34. compressed data), and will save all tables and parameters in the JPEG
  35. object. It will also initialize the decompression parameters to default
  36. values, and finally return JPEG_HEADER_OK. On return, the application may
  37. adjust the decompression parameters and then call jpeg_start_decompress.
  38. (Or, if the application only wanted to determine the image parameters,
  39. the data need not be decompressed. In that case, call jpeg_abort or
  40. jpeg_destroy to release any temporary space.)
  41. If an abbreviated (tables only) datastream is presented, the routine will
  42. return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
  43. re-use the JPEG object to read the abbreviated image datastream(s).
  44. It is unnecessary (but OK) to call jpeg_abort in this case.
  45. The JPEG_SUSPENDED return code only occurs if the data source module
  46. requests suspension of the decompressor. In this case the application
  47. should load more source data and then re-call jpeg_read_header to resume
  48. processing.
  49. If a non-suspending data source is used and require_image is TRUE, then the
  50. return code need not be inspected since only JPEG_HEADER_OK is possible.
  51. This routine is now just a front end to jpeg_consume_input, with some
  52. extra error checking. }
  53. {GLOBAL}
  54. function jpeg_read_header (cinfo : j_decompress_ptr;
  55. require_image : boolean) : int;
  56. { Consume data in advance of what the decompressor requires.
  57. This can be called at any time once the decompressor object has
  58. been created and a data source has been set up.
  59. This routine is essentially a state machine that handles a couple
  60. of critical state-transition actions, namely initial setup and
  61. transition from header scanning to ready-for-start_decompress.
  62. All the actual input is done via the input controller's consume_input
  63. method. }
  64. {GLOBAL}
  65. function jpeg_consume_input (cinfo : j_decompress_ptr) : int;
  66. { Have we finished reading the input file? }
  67. {GLOBAL}
  68. function jpeg_input_complete (cinfo : j_decompress_ptr) : boolean;
  69. { Is there more than one scan? }
  70. {GLOBAL}
  71. function jpeg_has_multiple_scans (cinfo : j_decompress_ptr) : boolean;
  72. { Finish JPEG decompression.
  73. This will normally just verify the file trailer and release temp storage.
  74. Returns FALSE if suspended. The return value need be inspected only if
  75. a suspending data source is used. }
  76. {GLOBAL}
  77. function jpeg_finish_decompress (cinfo : j_decompress_ptr) : boolean;
  78. implementation
  79. procedure jpeg_create_decompress(cinfo : j_decompress_ptr);
  80. begin
  81. jpeg_CreateDecompress(cinfo, JPEG_LIB_VERSION,
  82. size_t(sizeof(jpeg_decompress_struct)));
  83. end;
  84. { Initialization of a JPEG decompression object.
  85. The error manager must already be set up (in case memory manager fails). }
  86. {GLOBAL}
  87. procedure jpeg_CreateDecompress (cinfo : j_decompress_ptr;
  88. version : int;
  89. structsize : size_t);
  90. var
  91. i : int;
  92. var
  93. err : jpeg_error_mgr_ptr;
  94. client_data : voidp;
  95. begin
  96. { Guard against version mismatches between library and caller. }
  97. cinfo^.mem := NIL; { so jpeg_destroy knows mem mgr not called }
  98. if (version <> JPEG_LIB_VERSION) then
  99. ERREXIT2(j_common_ptr(cinfo), JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  100. if (structsize <> SIZEOF(jpeg_decompress_struct)) then
  101. ERREXIT2(j_common_ptr(cinfo), JERR_BAD_STRUCT_SIZE,
  102. int(SIZEOF(jpeg_decompress_struct)), int(structsize));
  103. { For debugging purposes, we zero the whole master structure.
  104. But the application has already set the err pointer, and may have set
  105. client_data, so we have to save and restore those fields.
  106. Note: if application hasn't set client_data, tools like Purify may
  107. complain here. }
  108. begin
  109. err := cinfo^.err;
  110. client_data := cinfo^.client_data; { ignore Purify complaint here }
  111. MEMZERO(j_common_ptr(cinfo), SIZEOF(jpeg_decompress_struct));
  112. cinfo^.err := err;
  113. cinfo^.client_data := client_data;
  114. end;
  115. cinfo^.is_decompressor := TRUE;
  116. { Initialize a memory manager instance for this object }
  117. jinit_memory_mgr(j_common_ptr(cinfo));
  118. { Zero out pointers to permanent structures. }
  119. cinfo^.progress := NIL;
  120. cinfo^.src := NIL;
  121. for i := 0 to pred(NUM_QUANT_TBLS) do
  122. cinfo^.quant_tbl_ptrs[i] := NIL;
  123. for i := 0 to pred(NUM_HUFF_TBLS) do
  124. begin
  125. cinfo^.dc_huff_tbl_ptrs[i] := NIL;
  126. cinfo^.ac_huff_tbl_ptrs[i] := NIL;
  127. end;
  128. { Initialize marker processor so application can override methods
  129. for COM, APPn markers before calling jpeg_read_header. }
  130. cinfo^.marker_list := NIL;
  131. jinit_marker_reader(cinfo);
  132. { And initialize the overall input controller. }
  133. jinit_input_controller(cinfo);
  134. { OK, I'm ready }
  135. cinfo^.global_state := DSTATE_START;
  136. end;
  137. { Destruction of a JPEG decompression object }
  138. {GLOBAL}
  139. procedure jpeg_destroy_decompress (cinfo : j_decompress_ptr);
  140. begin
  141. jpeg_destroy(j_common_ptr(cinfo)); { use common routine }
  142. end;
  143. { Abort processing of a JPEG decompression operation,
  144. but don't destroy the object itself. }
  145. {GLOBAL}
  146. procedure jpeg_abort_decompress (cinfo : j_decompress_ptr);
  147. begin
  148. jpeg_abort(j_common_ptr(cinfo)); { use common routine }
  149. end;
  150. { Set default decompression parameters. }
  151. {LOCAL}
  152. procedure default_decompress_parms (cinfo : j_decompress_ptr);
  153. var
  154. cid0 : int;
  155. cid1 : int;
  156. cid2 : int;
  157. begin
  158. { Guess the input colorspace, and set output colorspace accordingly. }
  159. { (Wish JPEG committee had provided a real way to specify this...) }
  160. { Note application may override our guesses. }
  161. case (cinfo^.num_components) of
  162. 1: begin
  163. cinfo^.jpeg_color_space := JCS_GRAYSCALE;
  164. cinfo^.out_color_space := JCS_GRAYSCALE;
  165. end;
  166. 3: begin
  167. if (cinfo^.saw_JFIF_marker) then
  168. begin
  169. cinfo^.jpeg_color_space := JCS_YCbCr; { JFIF implies YCbCr }
  170. end
  171. else
  172. if (cinfo^.saw_Adobe_marker) then
  173. begin
  174. case (cinfo^.Adobe_transform) of
  175. 0: cinfo^.jpeg_color_space := JCS_RGB;
  176. 1: cinfo^.jpeg_color_space := JCS_YCbCr;
  177. else
  178. begin
  179. WARNMS1(j_common_ptr(cinfo), JWRN_ADOBE_XFORM, cinfo^.Adobe_transform);
  180. cinfo^.jpeg_color_space := JCS_YCbCr; { assume it's YCbCr }
  181. end;
  182. end;
  183. end
  184. else
  185. begin
  186. { Saw no special markers, try to guess from the component IDs }
  187. cid0 := cinfo^.comp_info^[0].component_id;
  188. cid1 := cinfo^.comp_info^[1].component_id;
  189. cid2 := cinfo^.comp_info^[2].component_id;
  190. if (cid0 = 1) and (cid1 = 2) and (cid2 = 3) then
  191. cinfo^.jpeg_color_space := JCS_YCbCr { assume JFIF w/out marker }
  192. else
  193. if (cid0 = 82) and (cid1 = 71) and (cid2 = 66) then
  194. cinfo^.jpeg_color_space := JCS_RGB { ASCII 'R', 'G', 'B' }
  195. else
  196. begin
  197. {$IFDEF DEBUG}
  198. TRACEMS3(j_common_ptr(cinfo), 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  199. {$ENDIF}
  200. cinfo^.jpeg_color_space := JCS_YCbCr; { assume it's YCbCr }
  201. end;
  202. end;
  203. { Always guess RGB is proper output colorspace. }
  204. cinfo^.out_color_space := JCS_RGB;
  205. end;
  206. 4: begin
  207. if (cinfo^.saw_Adobe_marker) then
  208. begin
  209. case (cinfo^.Adobe_transform) of
  210. 0: cinfo^.jpeg_color_space := JCS_CMYK;
  211. 2: cinfo^.jpeg_color_space := JCS_YCCK;
  212. else
  213. begin
  214. WARNMS1(j_common_ptr(cinfo), JWRN_ADOBE_XFORM, cinfo^.Adobe_transform);
  215. cinfo^.jpeg_color_space := JCS_YCCK; { assume it's YCCK }
  216. end;
  217. end;
  218. end
  219. else
  220. begin
  221. { No special markers, assume straight CMYK. }
  222. cinfo^.jpeg_color_space := JCS_CMYK;
  223. end;
  224. cinfo^.out_color_space := JCS_CMYK;
  225. end;
  226. else
  227. begin
  228. cinfo^.jpeg_color_space := JCS_UNKNOWN;
  229. cinfo^.out_color_space := JCS_UNKNOWN;
  230. end;
  231. end;
  232. { Set defaults for other decompression parameters. }
  233. cinfo^.scale_num := 1; { 1:1 scaling }
  234. cinfo^.scale_denom := 1;
  235. cinfo^.output_gamma := 1.0;
  236. cinfo^.buffered_image := FALSE;
  237. cinfo^.raw_data_out := FALSE;
  238. cinfo^.dct_method := JDCT_DEFAULT;
  239. cinfo^.do_fancy_upsampling := TRUE;
  240. cinfo^.do_block_smoothing := TRUE;
  241. cinfo^.quantize_colors := FALSE;
  242. { We set these in case application only sets quantize_colors. }
  243. cinfo^.dither_mode := JDITHER_FS;
  244. {$ifdef QUANT_2PASS_SUPPORTED}
  245. cinfo^.two_pass_quantize := TRUE;
  246. {$else}
  247. cinfo^.two_pass_quantize := FALSE;
  248. {$endif}
  249. cinfo^.desired_number_of_colors := 256;
  250. cinfo^.colormap := NIL;
  251. { Initialize for no mode change in buffered-image mode. }
  252. cinfo^.enable_1pass_quant := FALSE;
  253. cinfo^.enable_external_quant := FALSE;
  254. cinfo^.enable_2pass_quant := FALSE;
  255. end;
  256. { Decompression startup: read start of JPEG datastream to see what's there.
  257. Need only initialize JPEG object and supply a data source before calling.
  258. This routine will read as far as the first SOS marker (ie, actual start of
  259. compressed data), and will save all tables and parameters in the JPEG
  260. object. It will also initialize the decompression parameters to default
  261. values, and finally return JPEG_HEADER_OK. On return, the application may
  262. adjust the decompression parameters and then call jpeg_start_decompress.
  263. (Or, if the application only wanted to determine the image parameters,
  264. the data need not be decompressed. In that case, call jpeg_abort or
  265. jpeg_destroy to release any temporary space.)
  266. If an abbreviated (tables only) datastream is presented, the routine will
  267. return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
  268. re-use the JPEG object to read the abbreviated image datastream(s).
  269. It is unnecessary (but OK) to call jpeg_abort in this case.
  270. The JPEG_SUSPENDED return code only occurs if the data source module
  271. requests suspension of the decompressor. In this case the application
  272. should load more source data and then re-call jpeg_read_header to resume
  273. processing.
  274. If a non-suspending data source is used and require_image is TRUE, then the
  275. return code need not be inspected since only JPEG_HEADER_OK is possible.
  276. This routine is now just a front end to jpeg_consume_input, with some
  277. extra error checking. }
  278. {GLOBAL}
  279. function jpeg_read_header (cinfo : j_decompress_ptr;
  280. require_image : boolean) : int;
  281. var
  282. retcode : int;
  283. begin
  284. if (cinfo^.global_state <> DSTATE_START) and
  285. (cinfo^.global_state <> DSTATE_INHEADER) then
  286. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  287. retcode := jpeg_consume_input(cinfo);
  288. case (retcode) of
  289. JPEG_REACHED_SOS:
  290. retcode := JPEG_HEADER_OK;
  291. JPEG_REACHED_EOI:
  292. begin
  293. if (require_image) then { Complain if application wanted an image }
  294. ERREXIT(j_common_ptr(cinfo), JERR_NO_IMAGE);
  295. { Reset to start state; it would be safer to require the application to
  296. call jpeg_abort, but we can't change it now for compatibility reasons.
  297. A side effect is to free any temporary memory (there shouldn't be any). }
  298. jpeg_abort(j_common_ptr(cinfo)); { sets state := DSTATE_START }
  299. retcode := JPEG_HEADER_TABLES_ONLY;
  300. end;
  301. JPEG_SUSPENDED: ; { no work }
  302. end;
  303. jpeg_read_header := retcode;
  304. end;
  305. { Consume data in advance of what the decompressor requires.
  306. This can be called at any time once the decompressor object has
  307. been created and a data source has been set up.
  308. This routine is essentially a state machine that handles a couple
  309. of critical state-transition actions, namely initial setup and
  310. transition from header scanning to ready-for-start_decompress.
  311. All the actual input is done via the input controller's consume_input
  312. method. }
  313. {GLOBAL}
  314. function jpeg_consume_input (cinfo : j_decompress_ptr) : int;
  315. var
  316. retcode : int;
  317. begin
  318. retcode := JPEG_SUSPENDED;
  319. { NB: every possible DSTATE value should be listed in this switch }
  320. if (cinfo^.global_state) = DSTATE_START then
  321. begin {work around the FALLTHROUGH}
  322. { Start-of-datastream actions: reset appropriate modules }
  323. cinfo^.inputctl^.reset_input_controller (cinfo);
  324. { Initialize application's data source module }
  325. cinfo^.src^.init_source (cinfo);
  326. cinfo^.global_state := DSTATE_INHEADER;
  327. end;
  328. case (cinfo^.global_state) of
  329. DSTATE_START,
  330. DSTATE_INHEADER:
  331. begin
  332. retcode := cinfo^.inputctl^.consume_input (cinfo);
  333. if (retcode = JPEG_REACHED_SOS) then
  334. begin { Found SOS, prepare to decompress }
  335. { Set up default parameters based on header data }
  336. default_decompress_parms(cinfo);
  337. { Set global state: ready for start_decompress }
  338. cinfo^.global_state := DSTATE_READY;
  339. end;
  340. end;
  341. DSTATE_READY:
  342. { Can't advance past first SOS until start_decompress is called }
  343. retcode := JPEG_REACHED_SOS;
  344. DSTATE_PRELOAD,
  345. DSTATE_PRESCAN,
  346. DSTATE_SCANNING,
  347. DSTATE_RAW_OK,
  348. DSTATE_BUFIMAGE,
  349. DSTATE_BUFPOST,
  350. DSTATE_STOPPING:
  351. retcode := cinfo^.inputctl^.consume_input (cinfo);
  352. else
  353. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  354. end;
  355. jpeg_consume_input := retcode;
  356. end;
  357. { Have we finished reading the input file? }
  358. {GLOBAL}
  359. function jpeg_input_complete (cinfo : j_decompress_ptr) : boolean;
  360. begin
  361. { Check for valid jpeg object }
  362. if (cinfo^.global_state < DSTATE_START) or
  363. (cinfo^.global_state > DSTATE_STOPPING) then
  364. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  365. jpeg_input_complete := cinfo^.inputctl^.eoi_reached;
  366. end;
  367. { Is there more than one scan? }
  368. {GLOBAL}
  369. function jpeg_has_multiple_scans (cinfo : j_decompress_ptr) : boolean;
  370. begin
  371. { Only valid after jpeg_read_header completes }
  372. if (cinfo^.global_state < DSTATE_READY) or
  373. (cinfo^.global_state > DSTATE_STOPPING) then
  374. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  375. jpeg_has_multiple_scans := cinfo^.inputctl^.has_multiple_scans;
  376. end;
  377. { Finish JPEG decompression.
  378. This will normally just verify the file trailer and release temp storage.
  379. Returns FALSE if suspended. The return value need be inspected only if
  380. a suspending data source is used. }
  381. {GLOBAL}
  382. function jpeg_finish_decompress (cinfo : j_decompress_ptr) : boolean;
  383. begin
  384. if ((cinfo^.global_state = DSTATE_SCANNING) or
  385. (cinfo^.global_state = DSTATE_RAW_OK) and (not cinfo^.buffered_image)) then
  386. begin
  387. { Terminate final pass of non-buffered mode }
  388. if (cinfo^.output_scanline < cinfo^.output_height) then
  389. ERREXIT(j_common_ptr(cinfo), JERR_TOO_LITTLE_DATA);
  390. cinfo^.master^.finish_output_pass (cinfo);
  391. cinfo^.global_state := DSTATE_STOPPING;
  392. end
  393. else
  394. if (cinfo^.global_state = DSTATE_BUFIMAGE) then
  395. begin
  396. { Finishing after a buffered-image operation }
  397. cinfo^.global_state := DSTATE_STOPPING;
  398. end
  399. else
  400. if (cinfo^.global_state <> DSTATE_STOPPING) then
  401. begin
  402. { STOPPING := repeat call after a suspension, anything else is error }
  403. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  404. end;
  405. { Read until EOI }
  406. while (not cinfo^.inputctl^.eoi_reached) do
  407. begin
  408. if (cinfo^.inputctl^.consume_input (cinfo) = JPEG_SUSPENDED) then
  409. begin
  410. jpeg_finish_decompress := FALSE; { Suspend, come back later }
  411. exit;
  412. end;
  413. end;
  414. { Do final cleanup }
  415. cinfo^.src^.term_source (cinfo);
  416. { We can use jpeg_abort to release memory and reset global_state }
  417. jpeg_abort(j_common_ptr(cinfo));
  418. jpeg_finish_decompress := TRUE;
  419. end;
  420. end.