jcapistd.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * jcapistd.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 "standard" API routines that are
  10. * used in the normal full-compression case. They are not used by a
  11. * transcoding-only application. Note that if an application links in
  12. * jpeg_start_compress, it will end up linking in the entire compressor.
  13. * We thus must separate this file from jcapimin.c to avoid linking the
  14. * whole compression library into a transcoder.
  15. */
  16. #define JPEG_INTERNALS
  17. #include "jinclude.h"
  18. #include "jpeglib.h"
  19. /*
  20. * Compression initialization.
  21. * Before calling this, all parameters and a data destination must be set up.
  22. *
  23. * We require a write_all_tables parameter as a failsafe check when writing
  24. * multiple datastreams from the same compression object. Since prior runs
  25. * will have left all the tables marked sent_table=TRUE, a subsequent run
  26. * would emit an abbreviated stream (no tables) by default. This may be what
  27. * is wanted, but for safety's sake it should not be the default behavior:
  28. * programmers should have to make a deliberate choice to emit abbreviated
  29. * images. Therefore the documentation and examples should encourage people
  30. * to pass write_all_tables=TRUE; then it will take active thought to do the
  31. * wrong thing.
  32. */
  33. GLOBAL void
  34. jpeg_start_compress( j_compress_ptr cinfo, boolean write_all_tables ) {
  35. if ( cinfo->global_state != CSTATE_START ) {
  36. ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state );
  37. }
  38. if ( write_all_tables ) {
  39. jpeg_suppress_tables( cinfo, FALSE );
  40. } /* mark all tables to be written */
  41. /* (Re)initialize error mgr and destination modules */
  42. ( *cinfo->err->reset_error_mgr )( (j_common_ptr) cinfo );
  43. ( *cinfo->dest->init_destination )( cinfo );
  44. /* Perform master selection of active modules */
  45. jinit_compress_master( cinfo );
  46. /* Set up for the first pass */
  47. ( *cinfo->master->prepare_for_pass )( cinfo );
  48. /* Ready for application to drive first pass through jpeg_write_scanlines
  49. * or jpeg_write_raw_data.
  50. */
  51. cinfo->next_scanline = 0;
  52. cinfo->global_state = ( cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING );
  53. }
  54. /*
  55. * Write some scanlines of data to the JPEG compressor.
  56. *
  57. * The return value will be the number of lines actually written.
  58. * This should be less than the supplied num_lines only in case that
  59. * the data destination module has requested suspension of the compressor,
  60. * or if more than image_height scanlines are passed in.
  61. *
  62. * Note: we warn about excess calls to jpeg_write_scanlines() since
  63. * this likely signals an application programmer error. However,
  64. * excess scanlines passed in the last valid call are *silently* ignored,
  65. * so that the application need not adjust num_lines for end-of-image
  66. * when using a multiple-scanline buffer.
  67. */
  68. GLOBAL JDIMENSION
  69. jpeg_write_scanlines( j_compress_ptr cinfo, JSAMPARRAY scanlines,
  70. JDIMENSION num_lines ) {
  71. JDIMENSION row_ctr, rows_left;
  72. if ( cinfo->global_state != CSTATE_SCANNING ) {
  73. ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state );
  74. }
  75. if ( cinfo->next_scanline >= cinfo->image_height ) {
  76. WARNMS( cinfo, JWRN_TOO_MUCH_DATA );
  77. }
  78. /* Call progress monitor hook if present */
  79. if ( cinfo->progress != NULL ) {
  80. cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  81. cinfo->progress->pass_limit = (long) cinfo->image_height;
  82. ( *cinfo->progress->progress_monitor )( (j_common_ptr) cinfo );
  83. }
  84. /* Give master control module another chance if this is first call to
  85. * jpeg_write_scanlines. This lets output of the frame/scan headers be
  86. * delayed so that application can write COM, etc, markers between
  87. * jpeg_start_compress and jpeg_write_scanlines.
  88. */
  89. if ( cinfo->master->call_pass_startup ) {
  90. ( *cinfo->master->pass_startup )( cinfo );
  91. }
  92. /* Ignore any extra scanlines at bottom of image. */
  93. rows_left = cinfo->image_height - cinfo->next_scanline;
  94. if ( num_lines > rows_left ) {
  95. num_lines = rows_left;
  96. }
  97. row_ctr = 0;
  98. ( *cinfo->main->process_data )( cinfo, scanlines, &row_ctr, num_lines );
  99. cinfo->next_scanline += row_ctr;
  100. return row_ctr;
  101. }
  102. /*
  103. * Alternate entry point to write raw data.
  104. * Processes exactly one iMCU row per call, unless suspended.
  105. */
  106. GLOBAL JDIMENSION
  107. jpeg_write_raw_data( j_compress_ptr cinfo, JSAMPIMAGE data,
  108. JDIMENSION num_lines ) {
  109. JDIMENSION lines_per_iMCU_row;
  110. if ( cinfo->global_state != CSTATE_RAW_OK ) {
  111. ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state );
  112. }
  113. if ( cinfo->next_scanline >= cinfo->image_height ) {
  114. WARNMS( cinfo, JWRN_TOO_MUCH_DATA );
  115. return 0;
  116. }
  117. /* Call progress monitor hook if present */
  118. if ( cinfo->progress != NULL ) {
  119. cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  120. cinfo->progress->pass_limit = (long) cinfo->image_height;
  121. ( *cinfo->progress->progress_monitor )( (j_common_ptr) cinfo );
  122. }
  123. /* Give master control module another chance if this is first call to
  124. * jpeg_write_raw_data. This lets output of the frame/scan headers be
  125. * delayed so that application can write COM, etc, markers between
  126. * jpeg_start_compress and jpeg_write_raw_data.
  127. */
  128. if ( cinfo->master->call_pass_startup ) {
  129. ( *cinfo->master->pass_startup )( cinfo );
  130. }
  131. /* Verify that at least one iMCU row has been passed. */
  132. lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
  133. if ( num_lines < lines_per_iMCU_row ) {
  134. ERREXIT( cinfo, JERR_BUFFER_SIZE );
  135. }
  136. /* Directly compress the row. */
  137. if ( !( *cinfo->coef->compress_data )( cinfo, data ) ) {
  138. /* If compressor did not consume the whole row, suspend processing. */
  139. return 0;
  140. }
  141. /* OK, we processed one iMCU row. */
  142. cinfo->next_scanline += lines_per_iMCU_row;
  143. return lines_per_iMCU_row;
  144. }