jcapistd.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * 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. {
  36. if (cinfo->global_state != CSTATE_START)
  37. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  38. if (write_all_tables)
  39. jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
  40. /* (Re)initialize error mgr and destination modules */
  41. (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  42. (*cinfo->dest->init_destination) (cinfo);
  43. /* Perform master selection of active modules */
  44. jinit_compress_master(cinfo);
  45. /* Set up for the first pass */
  46. (*cinfo->master->prepare_for_pass) (cinfo);
  47. /* Ready for application to drive first pass through jpeg_write_scanlines
  48. * or jpeg_write_raw_data.
  49. */
  50. cinfo->next_scanline = 0;
  51. cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
  52. }
  53. /*
  54. * Write some scanlines of data to the JPEG compressor.
  55. *
  56. * The return value will be the number of lines actually written.
  57. * This should be less than the supplied num_lines only in case that
  58. * the data destination module has requested suspension of the compressor,
  59. * or if more than image_height scanlines are passed in.
  60. *
  61. * Note: we warn about excess calls to jpeg_write_scanlines() since
  62. * this likely signals an application programmer error. However,
  63. * excess scanlines passed in the last valid call are *silently* ignored,
  64. * so that the application need not adjust num_lines for end-of-image
  65. * when using a multiple-scanline buffer.
  66. */
  67. GLOBAL(JDIMENSION)
  68. jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
  69. JDIMENSION num_lines)
  70. {
  71. JDIMENSION row_ctr, rows_left;
  72. if (cinfo->global_state != CSTATE_SCANNING)
  73. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  74. if (cinfo->next_scanline >= cinfo->image_height)
  75. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  76. /* Call progress monitor hook if present */
  77. if (cinfo->progress != NULL) {
  78. cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  79. cinfo->progress->pass_limit = (long) cinfo->image_height;
  80. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  81. }
  82. /* Give master control module another chance if this is first call to
  83. * jpeg_write_scanlines. This lets output of the frame/scan headers be
  84. * delayed so that application can write COM, etc, markers between
  85. * jpeg_start_compress and jpeg_write_scanlines.
  86. */
  87. if (cinfo->master->call_pass_startup)
  88. (*cinfo->master->pass_startup) (cinfo);
  89. /* Ignore any extra scanlines at bottom of image. */
  90. rows_left = cinfo->image_height - cinfo->next_scanline;
  91. if (num_lines > rows_left)
  92. num_lines = rows_left;
  93. row_ctr = 0;
  94. (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  95. cinfo->next_scanline += row_ctr;
  96. return row_ctr;
  97. }
  98. /*
  99. * Alternate entry point to write raw data.
  100. * Processes exactly one iMCU row per call, unless suspended.
  101. */
  102. GLOBAL(JDIMENSION)
  103. jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
  104. JDIMENSION num_lines)
  105. {
  106. JDIMENSION lines_per_iMCU_row;
  107. if (cinfo->global_state != CSTATE_RAW_OK)
  108. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  109. if (cinfo->next_scanline >= cinfo->image_height) {
  110. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  111. return 0;
  112. }
  113. /* Call progress monitor hook if present */
  114. if (cinfo->progress != NULL) {
  115. cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  116. cinfo->progress->pass_limit = (long) cinfo->image_height;
  117. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  118. }
  119. /* Give master control module another chance if this is first call to
  120. * jpeg_write_raw_data. This lets output of the frame/scan headers be
  121. * delayed so that application can write COM, etc, markers between
  122. * jpeg_start_compress and jpeg_write_raw_data.
  123. */
  124. if (cinfo->master->call_pass_startup)
  125. (*cinfo->master->pass_startup) (cinfo);
  126. /* Verify that at least one iMCU row has been passed. */
  127. lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
  128. if (num_lines < lines_per_iMCU_row)
  129. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  130. /* Directly compress the row. */
  131. if (! (*cinfo->coef->compress_data) (cinfo, data)) {
  132. /* If compressor did not consume the whole row, suspend processing. */
  133. return 0;
  134. }
  135. /* OK, we processed one iMCU row. */
  136. cinfo->next_scanline += lines_per_iMCU_row;
  137. return lines_per_iMCU_row;
  138. }