jcapimin.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * jcapimin.c
  3. *
  4. * Copyright (C) 1994-1996, 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, zero the whole master structure.
  37. * But error manager pointer is already there, so save and restore it.
  38. */
  39. {
  40. struct jpeg_error_mgr * err = cinfo->err;
  41. MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
  42. cinfo->err = err;
  43. }
  44. cinfo->is_decompressor = FALSE;
  45. /* Initialize a memory manager instance for this object */
  46. jinit_memory_mgr((j_common_ptr) cinfo);
  47. /* Zero out pointers to permanent structures. */
  48. cinfo->progress = NULL;
  49. cinfo->dest = NULL;
  50. cinfo->comp_info = NULL;
  51. for (i = 0; i < NUM_QUANT_TBLS; i++)
  52. cinfo->quant_tbl_ptrs[i] = NULL;
  53. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  54. cinfo->dc_huff_tbl_ptrs[i] = NULL;
  55. cinfo->ac_huff_tbl_ptrs[i] = NULL;
  56. }
  57. cinfo->input_gamma = 1.0; /* in case application forgets */
  58. /* OK, I'm ready */
  59. cinfo->global_state = CSTATE_START;
  60. }
  61. /*
  62. * Destruction of a JPEG compression object
  63. */
  64. GLOBAL(void)
  65. jpeg_destroy_compress (j_compress_ptr cinfo)
  66. {
  67. jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  68. }
  69. /*
  70. * Abort processing of a JPEG compression operation,
  71. * but don't destroy the object itself.
  72. */
  73. GLOBAL(void)
  74. jpeg_abort_compress (j_compress_ptr cinfo)
  75. {
  76. jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  77. }
  78. /*
  79. * Forcibly suppress or un-suppress all quantization and Huffman tables.
  80. * Marks all currently defined tables as already written (if suppress)
  81. * or not written (if !suppress). This will control whether they get emitted
  82. * by a subsequent jpeg_start_compress call.
  83. *
  84. * This routine is exported for use by applications that want to produce
  85. * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
  86. * since it is called by jpeg_start_compress, we put it here --- otherwise
  87. * jcparam.o would be linked whether the application used it or not.
  88. */
  89. GLOBAL(void)
  90. jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
  91. {
  92. int i;
  93. JQUANT_TBL * qtbl;
  94. JHUFF_TBL * htbl;
  95. for (i = 0; i < NUM_QUANT_TBLS; i++) {
  96. if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
  97. qtbl->sent_table = suppress;
  98. }
  99. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  100. if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
  101. htbl->sent_table = suppress;
  102. if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
  103. htbl->sent_table = suppress;
  104. }
  105. }
  106. /*
  107. * Finish JPEG compression.
  108. *
  109. * If a multipass operating mode was selected, this may do a great deal of
  110. * work including most of the actual output.
  111. */
  112. GLOBAL(void)
  113. jpeg_finish_compress (j_compress_ptr cinfo)
  114. {
  115. JDIMENSION iMCU_row;
  116. if (cinfo->global_state == CSTATE_SCANNING ||
  117. cinfo->global_state == CSTATE_RAW_OK) {
  118. /* Terminate first pass */
  119. if (cinfo->next_scanline < cinfo->image_height)
  120. ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  121. (*cinfo->master->finish_pass) (cinfo);
  122. } else if (cinfo->global_state != CSTATE_WRCOEFS)
  123. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  124. /* Perform any remaining passes */
  125. while (! cinfo->master->is_last_pass) {
  126. (*cinfo->master->prepare_for_pass) (cinfo);
  127. for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
  128. if (cinfo->progress != NULL) {
  129. cinfo->progress->pass_counter = (long) iMCU_row;
  130. cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
  131. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  132. }
  133. /* We bypass the main controller and invoke coef controller directly;
  134. * all work is being done from the coefficient buffer.
  135. */
  136. if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
  137. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  138. }
  139. (*cinfo->master->finish_pass) (cinfo);
  140. }
  141. /* Write EOI, do final cleanup */
  142. (*cinfo->marker->write_file_trailer) (cinfo);
  143. (*cinfo->dest->term_destination) (cinfo);
  144. /* We can use jpeg_abort to release memory and reset global_state */
  145. jpeg_abort((j_common_ptr) cinfo);
  146. }
  147. /*
  148. * Write a special marker.
  149. * This is only recommended for writing COM or APPn markers.
  150. * Must be called after jpeg_start_compress() and before
  151. * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
  152. */
  153. GLOBAL(void)
  154. jpeg_write_marker (j_compress_ptr cinfo, int marker,
  155. const JOCTET *dataptr, unsigned int datalen)
  156. {
  157. if (cinfo->next_scanline != 0 ||
  158. (cinfo->global_state != CSTATE_SCANNING &&
  159. cinfo->global_state != CSTATE_RAW_OK &&
  160. cinfo->global_state != CSTATE_WRCOEFS))
  161. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  162. (*cinfo->marker->write_any_marker) (cinfo, marker, dataptr, datalen);
  163. }
  164. /*
  165. * Alternate compression function: just write an abbreviated table file.
  166. * Before calling this, all parameters and a data destination must be set up.
  167. *
  168. * To produce a pair of files containing abbreviated tables and abbreviated
  169. * image data, one would proceed as follows:
  170. *
  171. * initialize JPEG object
  172. * set JPEG parameters
  173. * set destination to table file
  174. * jpeg_write_tables(cinfo);
  175. * set destination to image file
  176. * jpeg_start_compress(cinfo, FALSE);
  177. * write data...
  178. * jpeg_finish_compress(cinfo);
  179. *
  180. * jpeg_write_tables has the side effect of marking all tables written
  181. * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
  182. * will not re-emit the tables unless it is passed write_all_tables=TRUE.
  183. */
  184. GLOBAL(void)
  185. jpeg_write_tables (j_compress_ptr cinfo)
  186. {
  187. if (cinfo->global_state != CSTATE_START)
  188. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  189. /* (Re)initialize error mgr and destination modules */
  190. (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  191. (*cinfo->dest->init_destination) (cinfo);
  192. /* Initialize the marker writer ... bit of a crock to do it here. */
  193. jinit_marker_writer(cinfo);
  194. /* Write them tables! */
  195. (*cinfo->marker->write_tables_only) (cinfo);
  196. /* And clean up. */
  197. (*cinfo->dest->term_destination) (cinfo);
  198. /* We can use jpeg_abort to release memory. */
  199. jpeg_abort((j_common_ptr) cinfo);
  200. }