jcapistd.c 6.0 KB

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