2
0

imjcapimin.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. err := cinfo^.err;
  120. client_data := cinfo^.client_data;
  121. MEMZERO(cinfo, SIZEOF(jpeg_compress_struct));
  122. cinfo^.err := err;
  123. cinfo^.is_decompressor := FALSE;
  124. cinfo^.client_data := client_data;
  125. { Initialize a memory manager instance for this object }
  126. jinit_memory_mgr(j_common_ptr(cinfo));
  127. { Zero out pointers to permanent structures. }
  128. cinfo^.progress := NIL;
  129. cinfo^.dest := NIL;
  130. cinfo^.comp_info := NIL;
  131. for i := 0 to pred(NUM_QUANT_TBLS) do
  132. cinfo^.quant_tbl_ptrs[i] := NIL;
  133. for i := 0 to pred(NUM_HUFF_TBLS) do
  134. begin
  135. cinfo^.dc_huff_tbl_ptrs[i] := NIL;
  136. cinfo^.ac_huff_tbl_ptrs[i] := NIL;
  137. end;
  138. cinfo^.script_space := NIL;
  139. cinfo^.input_gamma := 1.0; { in case application forgets }
  140. { OK, I'm ready }
  141. cinfo^.global_state := CSTATE_START;
  142. end;
  143. { Destruction of a JPEG compression object }
  144. {GLOBAL}
  145. procedure jpeg_destroy_compress (cinfo : j_compress_ptr);
  146. begin
  147. jpeg_destroy(j_common_ptr(cinfo)); { use common routine }
  148. end;
  149. { Abort processing of a JPEG compression operation,
  150. but don't destroy the object itself. }
  151. {GLOBAL}
  152. procedure jpeg_abort_compress (cinfo : j_compress_ptr);
  153. begin
  154. jpeg_abort(j_common_ptr(cinfo)); { use common routine }
  155. end;
  156. { Forcibly suppress or un-suppress all quantization and Huffman tables.
  157. Marks all currently defined tables as already written (if suppress)
  158. or not written (if !suppress). This will control whether they get emitted
  159. by a subsequent jpeg_start_compress call.
  160. This routine is exported for use by applications that want to produce
  161. abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
  162. since it is called by jpeg_start_compress, we put it here --- otherwise
  163. jcparam.o would be linked whether the application used it or not. }
  164. {GLOBAL}
  165. procedure jpeg_suppress_tables (cinfo : j_compress_ptr;
  166. suppress : boolean);
  167. var
  168. i : int;
  169. qtbl : JQUANT_TBL_PTR;
  170. htbl : JHUFF_TBL_PTR;
  171. begin
  172. for i := 0 to pred(NUM_QUANT_TBLS) do
  173. begin
  174. qtbl := cinfo^.quant_tbl_ptrs[i];
  175. if (qtbl <> NIL) then
  176. qtbl^.sent_table := suppress;
  177. end;
  178. for i := 0 to pred(NUM_HUFF_TBLS) do
  179. begin
  180. htbl := cinfo^.dc_huff_tbl_ptrs[i];
  181. if (htbl <> NIL) then
  182. htbl^.sent_table := suppress;
  183. htbl := cinfo^.ac_huff_tbl_ptrs[i];
  184. if (htbl <> NIL) then
  185. htbl^.sent_table := suppress;
  186. end;
  187. end;
  188. { Finish JPEG compression.
  189. If a multipass operating mode was selected, this may do a great deal of
  190. work including most of the actual output. }
  191. {GLOBAL}
  192. procedure jpeg_finish_compress (cinfo : j_compress_ptr);
  193. var
  194. iMCU_row : JDIMENSION;
  195. begin
  196. if (cinfo^.global_state = CSTATE_SCANNING) or
  197. (cinfo^.global_state = CSTATE_RAW_OK) then
  198. begin
  199. { Terminate first pass }
  200. if (cinfo^.next_scanline < cinfo^.image_height) then
  201. ERREXIT(j_common_ptr(cinfo), JERR_TOO_LITTLE_DATA);
  202. cinfo^.master^.finish_pass (cinfo);
  203. end
  204. else
  205. if (cinfo^.global_state <> CSTATE_WRCOEFS) then
  206. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  207. { Perform any remaining passes }
  208. while (not cinfo^.master^.is_last_pass) do
  209. begin
  210. cinfo^.master^.prepare_for_pass (cinfo);
  211. for iMCU_row := 0 to pred(cinfo^.total_iMCU_rows) do
  212. begin
  213. if (cinfo^.progress <> NIL) then
  214. begin
  215. cinfo^.progress^.pass_counter := long (iMCU_row);
  216. cinfo^.progress^.pass_limit := long (cinfo^.total_iMCU_rows);
  217. cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  218. end;
  219. { We bypass the main controller and invoke coef controller directly;
  220. all work is being done from the coefficient buffer. }
  221. if (not cinfo^.coef^.compress_data (cinfo, JSAMPIMAGE(NIL))) then
  222. ERREXIT(j_common_ptr(cinfo), JERR_CANT_SUSPEND);
  223. end;
  224. cinfo^.master^.finish_pass (cinfo);
  225. end;
  226. { Write EOI, do final cleanup }
  227. cinfo^.marker^.write_file_trailer (cinfo);
  228. cinfo^.dest^.term_destination (cinfo);
  229. { We can use jpeg_abort to release memory and reset global_state }
  230. jpeg_abort(j_common_ptr(cinfo));
  231. end;
  232. { Write a special marker.
  233. This is only recommended for writing COM or APPn markers.
  234. Must be called after jpeg_start_compress() and before
  235. first call to jpeg_write_scanlines() or jpeg_write_raw_data(). }
  236. {GLOBAL}
  237. procedure jpeg_write_marker (cinfo : j_compress_ptr;
  238. marker : int;
  239. dataptr : JOCTETptr;
  240. datalen : uInt);
  241. var
  242. write_marker_byte : procedure(info : j_compress_ptr; val : int);
  243. begin
  244. if (cinfo^.next_scanline <> 0) or
  245. ((cinfo^.global_state <> CSTATE_SCANNING) and
  246. (cinfo^.global_state <> CSTATE_RAW_OK) and
  247. (cinfo^.global_state <> CSTATE_WRCOEFS)) then
  248. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  249. cinfo^.marker^.write_marker_header (cinfo, marker, datalen);
  250. write_marker_byte := cinfo^.marker^.write_marker_byte; { copy for speed }
  251. while (datalen <> 0) do
  252. begin
  253. Dec(datalen);
  254. write_marker_byte (cinfo, dataptr^);
  255. Inc(dataptr);
  256. end;
  257. end;
  258. { Same, but piecemeal. }
  259. {GLOBAL}
  260. procedure jpeg_write_m_header (cinfo : j_compress_ptr;
  261. marker : int;
  262. datalen : uint);
  263. begin
  264. if (cinfo^.next_scanline <> 0) or
  265. ((cinfo^.global_state <> CSTATE_SCANNING) and
  266. (cinfo^.global_state <> CSTATE_RAW_OK) and
  267. (cinfo^.global_state <> CSTATE_WRCOEFS)) then
  268. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  269. cinfo^.marker^.write_marker_header (cinfo, marker, datalen);
  270. end;
  271. {GLOBAL}
  272. procedure jpeg_write_m_byte (cinfo : j_compress_ptr; val : int);
  273. begin
  274. cinfo^.marker^.write_marker_byte (cinfo, val);
  275. end;
  276. { Alternate compression function: just write an abbreviated table file.
  277. Before calling this, all parameters and a data destination must be set up.
  278. To produce a pair of files containing abbreviated tables and abbreviated
  279. image data, one would proceed as follows:
  280. initialize JPEG object
  281. set JPEG parameters
  282. set destination to table file
  283. jpeg_write_tables(cinfo);
  284. set destination to image file
  285. jpeg_start_compress(cinfo, FALSE);
  286. write data...
  287. jpeg_finish_compress(cinfo);
  288. jpeg_write_tables has the side effect of marking all tables written
  289. (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
  290. will not re-emit the tables unless it is passed write_all_tables=TRUE. }
  291. {GLOBAL}
  292. procedure jpeg_write_tables (cinfo : j_compress_ptr);
  293. begin
  294. if (cinfo^.global_state <> CSTATE_START) then
  295. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  296. { (Re)initialize error mgr and destination modules }
  297. cinfo^.err^.reset_error_mgr (j_common_ptr(cinfo));
  298. cinfo^.dest^.init_destination (cinfo);
  299. { Initialize the marker writer ... bit of a crock to do it here. }
  300. jinit_marker_writer(cinfo);
  301. { Write them tables! }
  302. cinfo^.marker^.write_tables_only (cinfo);
  303. { And clean up. }
  304. cinfo^.dest^.term_destination (cinfo);
  305. { In library releases up through v6a, we called jpeg_abort() here to free
  306. any working memory allocated by the destination manager and marker
  307. writer. Some applications had a problem with that: they allocated space
  308. of their own from the library memory manager, and didn't want it to go
  309. away during write_tables. So now we do nothing. This will cause a
  310. memory leak if an app calls write_tables repeatedly without doing a full
  311. compression cycle or otherwise resetting the JPEG object. However, that
  312. seems less bad than unexpectedly freeing memory in the normal case.
  313. An app that prefers the old behavior can call jpeg_abort for itself after
  314. each call to jpeg_write_tables(). }
  315. end;
  316. end.