jcapimin.pas 13 KB

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