jcapimin.c 9.9 KB

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