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.

401 lines
13 KiB

3 years ago
  1. unit imjcapimin;
  2. { This file contains application interface code for the compression half
  3. of the JPEG library. These are the "minimum" API routines that may be
  4. needed in either the normal full-compression case or the transcoding-only
  5. case.
  6. Most of the routines intended to be called directly by an application
  7. are in this file or in jcapistd.c. But also see jcparam.c for
  8. parameter-setup helper routines, jcomapi.c for routines shared by
  9. compression and decompression, and jctrans.c for the transcoding case. }
  10. { jcapimin.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. imjcomapi,
  20. imjmemmgr,
  21. imjcmarker;
  22. { Initialization of JPEG compression objects.
  23. Nomssi: This is a macro in the original code.
  24. jpeg_create_compress() and jpeg_create_decompress() are the exported
  25. names that applications should call. These expand to calls on
  26. jpeg_CreateCompress and jpeg_CreateDecompress with additional information
  27. passed for version mismatch checking.
  28. NB: you must set up the error-manager BEFORE calling jpeg_create_xxx. }
  29. procedure jpeg_create_compress(cinfo : j_compress_ptr);
  30. { Initialization of a JPEG compression object.
  31. The error manager must already be set up (in case memory manager fails). }
  32. {GLOBAL}
  33. procedure jpeg_CreateCompress (cinfo : j_compress_ptr;
  34. version : int;
  35. structsize : size_t);
  36. { Destruction of a JPEG compression object }
  37. {GLOBAL}
  38. procedure jpeg_destroy_compress (cinfo : j_compress_ptr);
  39. { Abort processing of a JPEG compression operation,
  40. but don't destroy the object itself. }
  41. {GLOBAL}
  42. procedure jpeg_abort_compress (cinfo : j_compress_ptr);
  43. { Forcibly suppress or un-suppress all quantization and Huffman tables.
  44. Marks all currently defined tables as already written (if suppress)
  45. or not written (if !suppress). This will control whether they get emitted
  46. by a subsequent jpeg_start_compress call.
  47. This routine is exported for use by applications that want to produce
  48. abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
  49. since it is called by jpeg_start_compress, we put it here --- otherwise
  50. jcparam.o would be linked whether the application used it or not. }
  51. {GLOBAL}
  52. procedure jpeg_suppress_tables (cinfo : j_compress_ptr;
  53. suppress : boolean);
  54. { Finish JPEG compression.
  55. If a multipass operating mode was selected, this may do a great deal of
  56. work including most of the actual output. }
  57. {GLOBAL}
  58. procedure jpeg_finish_compress (cinfo : j_compress_ptr);
  59. { Write a special marker.
  60. This is only recommended for writing COM or APPn markers.
  61. Must be called after jpeg_start_compress() and before
  62. first call to jpeg_write_scanlines() or jpeg_write_raw_data(). }
  63. {GLOBAL}
  64. procedure jpeg_write_marker (cinfo : j_compress_ptr;
  65. marker : int;
  66. dataptr : JOCTETptr;
  67. datalen : uInt);
  68. {GLOBAL}
  69. procedure jpeg_write_m_header (cinfo : j_compress_ptr;
  70. marker : int;
  71. datalen : uint);
  72. {GLOBAL}
  73. procedure jpeg_write_m_byte (cinfo : j_compress_ptr; val : int);
  74. { Alternate compression function: just write an abbreviated table file.
  75. Before calling this, all parameters and a data destination must be set up.
  76. To produce a pair of files containing abbreviated tables and abbreviated
  77. image data, one would proceed as follows:
  78. initialize JPEG object
  79. set JPEG parameters
  80. set destination to table file
  81. jpeg_write_tables(cinfo);
  82. set destination to image file
  83. jpeg_start_compress(cinfo, FALSE);
  84. write data...
  85. jpeg_finish_compress(cinfo);
  86. jpeg_write_tables has the side effect of marking all tables written
  87. (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
  88. will not re-emit the tables unless it is passed write_all_tables=TRUE. }
  89. {GLOBAL}
  90. procedure jpeg_write_tables (cinfo : j_compress_ptr);
  91. implementation
  92. procedure jpeg_create_compress(cinfo : j_compress_ptr);
  93. begin
  94. jpeg_CreateCompress(cinfo, JPEG_LIB_VERSION,
  95. size_t(sizeof(jpeg_compress_struct)));
  96. end;
  97. { Initialization of a JPEG compression object.
  98. The error manager must already be set up (in case memory manager fails). }
  99. {GLOBAL}
  100. procedure jpeg_CreateCompress (cinfo : j_compress_ptr;
  101. version : int;
  102. structsize : size_t);
  103. var
  104. i : int;
  105. var
  106. err : jpeg_error_mgr_ptr;
  107. client_data : voidp;
  108. begin
  109. { Guard against version mismatches between library and caller. }
  110. cinfo^.mem := NIL; { so jpeg_destroy knows mem mgr not called }
  111. if (version <> JPEG_LIB_VERSION) then
  112. ERREXIT2(j_common_ptr(cinfo), JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  113. if (structsize <> SIZEOF(jpeg_compress_struct)) then
  114. ERREXIT2(j_common_ptr(cinfo), JERR_BAD_STRUCT_SIZE,
  115. int(SIZEOF(jpeg_compress_struct)), int(structsize));
  116. { For debugging purposes, we zero the whole master structure.
  117. But the application has already set the err pointer, and may have set
  118. client_data, so we have to save and restore those fields.
  119. Note: if application hasn't set client_data, tools like Purify may
  120. complain here. }
  121. err := cinfo^.err;
  122. client_data := cinfo^.client_data; { ignore Purify complaint here }
  123. MEMZERO(cinfo, SIZEOF(jpeg_compress_struct));
  124. cinfo^.err := err;
  125. cinfo^.is_decompressor := FALSE;
  126. { Initialize a memory manager instance for this object }
  127. jinit_memory_mgr(j_common_ptr(cinfo));
  128. { Zero out pointers to permanent structures. }
  129. cinfo^.progress := NIL;
  130. cinfo^.dest := NIL;
  131. cinfo^.comp_info := NIL;
  132. for i := 0 to pred(NUM_QUANT_TBLS) do
  133. cinfo^.quant_tbl_ptrs[i] := NIL;
  134. for i := 0 to pred(NUM_HUFF_TBLS) do
  135. begin
  136. cinfo^.dc_huff_tbl_ptrs[i] := NIL;
  137. cinfo^.ac_huff_tbl_ptrs[i] := NIL;
  138. end;
  139. cinfo^.script_space := NIL;
  140. cinfo^.input_gamma := 1.0; { in case application forgets }
  141. { OK, I'm ready }
  142. cinfo^.global_state := CSTATE_START;
  143. end;
  144. { Destruction of a JPEG compression object }
  145. {GLOBAL}
  146. procedure jpeg_destroy_compress (cinfo : j_compress_ptr);
  147. begin
  148. jpeg_destroy(j_common_ptr(cinfo)); { use common routine }
  149. end;
  150. { Abort processing of a JPEG compression operation,
  151. but don't destroy the object itself. }
  152. {GLOBAL}
  153. procedure jpeg_abort_compress (cinfo : j_compress_ptr);
  154. begin
  155. jpeg_abort(j_common_ptr(cinfo)); { use common routine }
  156. end;
  157. { Forcibly suppress or un-suppress all quantization and Huffman tables.
  158. Marks all currently defined tables as already written (if suppress)
  159. or not written (if !suppress). This will control whether they get emitted
  160. by a subsequent jpeg_start_compress call.
  161. This routine is exported for use by applications that want to produce
  162. abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
  163. since it is called by jpeg_start_compress, we put it here --- otherwise
  164. jcparam.o would be linked whether the application used it or not. }
  165. {GLOBAL}
  166. procedure jpeg_suppress_tables (cinfo : j_compress_ptr;
  167. suppress : boolean);
  168. var
  169. i : int;
  170. qtbl : JQUANT_TBL_PTR;
  171. htbl : JHUFF_TBL_PTR;
  172. begin
  173. for i := 0 to pred(NUM_QUANT_TBLS) do
  174. begin
  175. qtbl := cinfo^.quant_tbl_ptrs[i];
  176. if (qtbl <> NIL) then
  177. qtbl^.sent_table := suppress;
  178. end;
  179. for i := 0 to pred(NUM_HUFF_TBLS) do
  180. begin
  181. htbl := cinfo^.dc_huff_tbl_ptrs[i];
  182. if (htbl <> NIL) then
  183. htbl^.sent_table := suppress;
  184. htbl := cinfo^.ac_huff_tbl_ptrs[i];
  185. if (htbl <> NIL) then
  186. htbl^.sent_table := suppress;
  187. end;
  188. end;
  189. { Finish JPEG compression.
  190. If a multipass operating mode was selected, this may do a great deal of
  191. work including most of the actual output. }
  192. {GLOBAL}
  193. procedure jpeg_finish_compress (cinfo : j_compress_ptr);
  194. var
  195. iMCU_row : JDIMENSION;
  196. begin
  197. if (cinfo^.global_state = CSTATE_SCANNING) or
  198. (cinfo^.global_state = CSTATE_RAW_OK) then
  199. begin
  200. { Terminate first pass }
  201. if (cinfo^.next_scanline < cinfo^.image_height) then
  202. ERREXIT(j_common_ptr(cinfo), JERR_TOO_LITTLE_DATA);
  203. cinfo^.master^.finish_pass (cinfo);
  204. end
  205. else
  206. if (cinfo^.global_state <> CSTATE_WRCOEFS) then
  207. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  208. { Perform any remaining passes }
  209. while (not cinfo^.master^.is_last_pass) do
  210. begin
  211. cinfo^.master^.prepare_for_pass (cinfo);
  212. for iMCU_row := 0 to pred(cinfo^.total_iMCU_rows) do
  213. begin
  214. if (cinfo^.progress <> NIL) then
  215. begin
  216. cinfo^.progress^.pass_counter := long (iMCU_row);
  217. cinfo^.progress^.pass_limit := long (cinfo^.total_iMCU_rows);
  218. cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  219. end;
  220. { We bypass the main controller and invoke coef controller directly;
  221. all work is being done from the coefficient buffer. }
  222. if (not cinfo^.coef^.compress_data (cinfo, JSAMPIMAGE(NIL))) then
  223. ERREXIT(j_common_ptr(cinfo), JERR_CANT_SUSPEND);
  224. end;
  225. cinfo^.master^.finish_pass (cinfo);
  226. end;
  227. { Write EOI, do final cleanup }
  228. cinfo^.marker^.write_file_trailer (cinfo);
  229. cinfo^.dest^.term_destination (cinfo);
  230. { We can use jpeg_abort to release memory and reset global_state }
  231. jpeg_abort(j_common_ptr(cinfo));
  232. end;
  233. { Write a special marker.
  234. This is only recommended for writing COM or APPn markers.
  235. Must be called after jpeg_start_compress() and before
  236. first call to jpeg_write_scanlines() or jpeg_write_raw_data(). }
  237. {GLOBAL}
  238. procedure jpeg_write_marker (cinfo : j_compress_ptr;
  239. marker : int;
  240. dataptr : JOCTETptr;
  241. datalen : uInt);
  242. var
  243. write_marker_byte : procedure(info : j_compress_ptr; val : int);
  244. begin
  245. if (cinfo^.next_scanline <> 0) or
  246. ((cinfo^.global_state <> CSTATE_SCANNING) and
  247. (cinfo^.global_state <> CSTATE_RAW_OK) and
  248. (cinfo^.global_state <> CSTATE_WRCOEFS)) then
  249. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  250. cinfo^.marker^.write_marker_header (cinfo, marker, datalen);
  251. write_marker_byte := cinfo^.marker^.write_marker_byte; { copy for speed }
  252. while (datalen <> 0) do
  253. begin
  254. Dec(datalen);
  255. write_marker_byte (cinfo, dataptr^);
  256. Inc(dataptr);
  257. end;
  258. end;
  259. { Same, but piecemeal. }
  260. {GLOBAL}
  261. procedure jpeg_write_m_header (cinfo : j_compress_ptr;
  262. marker : int;
  263. datalen : uint);
  264. begin
  265. if (cinfo^.next_scanline <> 0) or
  266. ((cinfo^.global_state <> CSTATE_SCANNING) and
  267. (cinfo^.global_state <> CSTATE_RAW_OK) and
  268. (cinfo^.global_state <> CSTATE_WRCOEFS)) then
  269. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  270. cinfo^.marker^.write_marker_header (cinfo, marker, datalen);
  271. end;
  272. {GLOBAL}
  273. procedure jpeg_write_m_byte (cinfo : j_compress_ptr; val : int);
  274. begin
  275. cinfo^.marker^.write_marker_byte (cinfo, val);
  276. end;
  277. { Alternate compression function: just write an abbreviated table file.
  278. Before calling this, all parameters and a data destination must be set up.
  279. To produce a pair of files containing abbreviated tables and abbreviated
  280. image data, one would proceed as follows:
  281. initialize JPEG object
  282. set JPEG parameters
  283. set destination to table file
  284. jpeg_write_tables(cinfo);
  285. set destination to image file
  286. jpeg_start_compress(cinfo, FALSE);
  287. write data...
  288. jpeg_finish_compress(cinfo);
  289. jpeg_write_tables has the side effect of marking all tables written
  290. (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
  291. will not re-emit the tables unless it is passed write_all_tables=TRUE. }
  292. {GLOBAL}
  293. procedure jpeg_write_tables (cinfo : j_compress_ptr);
  294. begin
  295. if (cinfo^.global_state <> CSTATE_START) then
  296. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  297. { (Re)initialize error mgr and destination modules }
  298. cinfo^.err^.reset_error_mgr (j_common_ptr(cinfo));
  299. cinfo^.dest^.init_destination (cinfo);
  300. { Initialize the marker writer ... bit of a crock to do it here. }
  301. jinit_marker_writer(cinfo);
  302. { Write them tables! }
  303. cinfo^.marker^.write_tables_only (cinfo);
  304. { And clean up. }
  305. cinfo^.dest^.term_destination (cinfo);
  306. { In library releases up through v6a, we called jpeg_abort() here to free
  307. any working memory allocated by the destination manager and marker
  308. writer. Some applications had a problem with that: they allocated space
  309. of their own from the library memory manager, and didn't want it to go
  310. away during write_tables. So now we do nothing. This will cause a
  311. memory leak if an app calls write_tables repeatedly without doing a full
  312. compression cycle or otherwise resetting the JPEG object. However, that
  313. seems less bad than unexpectedly freeing memory in the normal case.
  314. An app that prefers the old behavior can call jpeg_abort for itself after
  315. each call to jpeg_write_tables(). }
  316. end;
  317. end.