jcapimin.c 8.9 KB

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