Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
7.1 KiB

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