Audio plugin host https://kx.studio/carla
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.

jcapimin.c 9.2KB

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