jcapimin.pas 13 KB

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