jcapimin.cpp 7.3 KB

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