jcapimin.c 9.2 KB

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