jcapimin.c 9.2 KB

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