jcapimin.c 9.5 KB

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