jcapistd.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.ijg
  7. * file.
  8. *
  9. * This file contains application interface code for the compression half
  10. * of the JPEG library. These are the "standard" API routines that are
  11. * used in the normal full-compression case. They are not used by a
  12. * transcoding-only application. Note that if an application links in
  13. * jpeg_start_compress, it will end up linking in the entire compressor.
  14. * We thus must separate this file from jcapimin.c to avoid linking the
  15. * whole compression library into a transcoder.
  16. */
  17. #define JPEG_INTERNALS
  18. #include "jinclude.h"
  19. #include "jpeglib.h"
  20. /*
  21. * Compression initialization.
  22. * Before calling this, all parameters and a data destination must be set up.
  23. *
  24. * We require a write_all_tables parameter as a failsafe check when writing
  25. * multiple datastreams from the same compression object. Since prior runs
  26. * will have left all the tables marked sent_table=TRUE, a subsequent run
  27. * would emit an abbreviated stream (no tables) by default. This may be what
  28. * is wanted, but for safety's sake it should not be the default behavior:
  29. * programmers should have to make a deliberate choice to emit abbreviated
  30. * images. Therefore the documentation and examples should encourage people
  31. * to pass write_all_tables=TRUE; then it will take active thought to do the
  32. * wrong thing.
  33. */
  34. GLOBAL(void)
  35. jpeg_start_compress(j_compress_ptr cinfo, boolean write_all_tables)
  36. {
  37. if (cinfo->global_state != CSTATE_START)
  38. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  39. if (write_all_tables)
  40. jpeg_suppress_tables(cinfo, FALSE); /* 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. {
  72. JDIMENSION row_ctr, rows_left;
  73. if (cinfo->global_state != CSTATE_SCANNING)
  74. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  75. if (cinfo->next_scanline >= cinfo->image_height)
  76. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  77. /* Call progress monitor hook if present */
  78. if (cinfo->progress != NULL) {
  79. cinfo->progress->pass_counter = (long)cinfo->next_scanline;
  80. cinfo->progress->pass_limit = (long)cinfo->image_height;
  81. (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
  82. }
  83. /* Give master control module another chance if this is first call to
  84. * jpeg_write_scanlines. This lets output of the frame/scan headers be
  85. * delayed so that application can write COM, etc, markers between
  86. * jpeg_start_compress and jpeg_write_scanlines.
  87. */
  88. if (cinfo->master->call_pass_startup)
  89. (*cinfo->master->pass_startup) (cinfo);
  90. /* Ignore any extra scanlines at bottom of image. */
  91. rows_left = cinfo->image_height - cinfo->next_scanline;
  92. if (num_lines > rows_left)
  93. num_lines = rows_left;
  94. row_ctr = 0;
  95. (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  96. cinfo->next_scanline += row_ctr;
  97. return row_ctr;
  98. }
  99. /*
  100. * Alternate entry point to write raw data.
  101. * Processes exactly one iMCU row per call, unless suspended.
  102. */
  103. GLOBAL(JDIMENSION)
  104. jpeg_write_raw_data(j_compress_ptr cinfo, JSAMPIMAGE data,
  105. JDIMENSION num_lines)
  106. {
  107. JDIMENSION lines_per_iMCU_row;
  108. if (cinfo->global_state != CSTATE_RAW_OK)
  109. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  110. if (cinfo->next_scanline >= cinfo->image_height) {
  111. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  112. return 0;
  113. }
  114. /* Call progress monitor hook if present */
  115. if (cinfo->progress != NULL) {
  116. cinfo->progress->pass_counter = (long)cinfo->next_scanline;
  117. cinfo->progress->pass_limit = (long)cinfo->image_height;
  118. (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
  119. }
  120. /* Give master control module another chance if this is first call to
  121. * jpeg_write_raw_data. This lets output of the frame/scan headers be
  122. * delayed so that application can write COM, etc, markers between
  123. * jpeg_start_compress and jpeg_write_raw_data.
  124. */
  125. if (cinfo->master->call_pass_startup)
  126. (*cinfo->master->pass_startup) (cinfo);
  127. /* Verify that at least one iMCU row has been passed. */
  128. lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
  129. if (num_lines < lines_per_iMCU_row)
  130. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  131. /* Directly compress the row. */
  132. if (!(*cinfo->coef->compress_data) (cinfo, data)) {
  133. /* If compressor did not consume the whole row, suspend processing. */
  134. return 0;
  135. }
  136. /* OK, we processed one iMCU row. */
  137. cinfo->next_scanline += lines_per_iMCU_row;
  138. return lines_per_iMCU_row;
  139. }