jcapimin.c 9.5 KB

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