jcapimin.cpp 7.1 KB

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