jdapistd.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * jdapistd.c
  3. *
  4. * Copyright (C) 1994-1996, Thomas G. Lane.
  5. * Modified 2002-2013 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains application interface code for the decompression half
  10. * of the JPEG library. These are the "standard" API routines that are
  11. * used in the normal full-decompression case. They are not used by a
  12. * transcoding-only application. Note that if an application links in
  13. * jpeg_start_decompress, it will end up linking in the entire decompressor.
  14. * We thus must separate this file from jdapimin.c to avoid linking the
  15. * whole decompression library into a transcoder.
  16. */
  17. #define JPEG_INTERNALS
  18. #include "jinclude.h"
  19. #include "jpeglib.h"
  20. /* Forward declarations */
  21. LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
  22. /*
  23. * Decompression initialization.
  24. * jpeg_read_header must be completed before calling this.
  25. *
  26. * If a multipass operating mode was selected, this will do all but the
  27. * last pass, and thus may take a great deal of time.
  28. *
  29. * Returns FALSE if suspended. The return value need be inspected only if
  30. * a suspending data source is used.
  31. */
  32. GLOBAL(boolean)
  33. jpeg_start_decompress (j_decompress_ptr cinfo)
  34. {
  35. if (cinfo->global_state == DSTATE_READY) {
  36. /* First call: initialize master control, select active modules */
  37. jinit_master_decompress(cinfo);
  38. if (cinfo->buffered_image) {
  39. /* No more work here; expecting jpeg_start_output next */
  40. cinfo->global_state = DSTATE_BUFIMAGE;
  41. return TRUE;
  42. }
  43. cinfo->global_state = DSTATE_PRELOAD;
  44. }
  45. if (cinfo->global_state == DSTATE_PRELOAD) {
  46. /* If file has multiple scans, absorb them all into the coef buffer */
  47. if (cinfo->inputctl->has_multiple_scans) {
  48. #ifdef D_MULTISCAN_FILES_SUPPORTED
  49. for (;;) {
  50. int retcode;
  51. /* Call progress monitor hook if present */
  52. if (cinfo->progress != NULL)
  53. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  54. /* Absorb some more input */
  55. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  56. if (retcode == JPEG_SUSPENDED)
  57. return FALSE;
  58. if (retcode == JPEG_REACHED_EOI)
  59. break;
  60. /* Advance progress counter if appropriate */
  61. if (cinfo->progress != NULL &&
  62. (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
  63. if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
  64. /* jdmaster underestimated number of scans; ratchet up one scan */
  65. cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
  66. }
  67. }
  68. }
  69. #else
  70. ERREXIT(cinfo, JERR_NOT_COMPILED);
  71. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  72. }
  73. cinfo->output_scan_number = cinfo->input_scan_number;
  74. } else if (cinfo->global_state != DSTATE_PRESCAN)
  75. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  76. /* Perform any dummy output passes, and set up for the final pass */
  77. return output_pass_setup(cinfo);
  78. }
  79. /*
  80. * Set up for an output pass, and perform any dummy pass(es) needed.
  81. * Common subroutine for jpeg_start_decompress and jpeg_start_output.
  82. * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
  83. * Exit: If done, returns TRUE and sets global_state for proper output mode.
  84. * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
  85. */
  86. LOCAL(boolean)
  87. output_pass_setup (j_decompress_ptr cinfo)
  88. {
  89. if (cinfo->global_state != DSTATE_PRESCAN) {
  90. /* First call: do pass setup */
  91. (*cinfo->master->prepare_for_output_pass) (cinfo);
  92. cinfo->output_scanline = 0;
  93. cinfo->global_state = DSTATE_PRESCAN;
  94. }
  95. /* Loop over any required dummy passes */
  96. while (cinfo->master->is_dummy_pass) {
  97. #ifdef QUANT_2PASS_SUPPORTED
  98. /* Crank through the dummy pass */
  99. while (cinfo->output_scanline < cinfo->output_height) {
  100. JDIMENSION last_scanline;
  101. /* Call progress monitor hook if present */
  102. if (cinfo->progress != NULL) {
  103. cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  104. cinfo->progress->pass_limit = (long) cinfo->output_height;
  105. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  106. }
  107. /* Process some data */
  108. last_scanline = cinfo->output_scanline;
  109. (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
  110. &cinfo->output_scanline, (JDIMENSION) 0);
  111. if (cinfo->output_scanline == last_scanline)
  112. return FALSE; /* No progress made, must suspend */
  113. }
  114. /* Finish up dummy pass, and set up for another one */
  115. (*cinfo->master->finish_output_pass) (cinfo);
  116. (*cinfo->master->prepare_for_output_pass) (cinfo);
  117. cinfo->output_scanline = 0;
  118. #else
  119. ERREXIT(cinfo, JERR_NOT_COMPILED);
  120. #endif /* QUANT_2PASS_SUPPORTED */
  121. }
  122. /* Ready for application to drive output pass through
  123. * jpeg_read_scanlines or jpeg_read_raw_data.
  124. */
  125. cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
  126. return TRUE;
  127. }
  128. /*
  129. * Read some scanlines of data from the JPEG decompressor.
  130. *
  131. * The return value will be the number of lines actually read.
  132. * This may be less than the number requested in several cases,
  133. * including bottom of image, data source suspension, and operating
  134. * modes that emit multiple scanlines at a time.
  135. *
  136. * Note: we warn about excess calls to jpeg_read_scanlines() since
  137. * this likely signals an application programmer error. However,
  138. * an oversize buffer (max_lines > scanlines remaining) is not an error.
  139. */
  140. GLOBAL(JDIMENSION)
  141. jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
  142. JDIMENSION max_lines)
  143. {
  144. JDIMENSION row_ctr;
  145. if (cinfo->global_state != DSTATE_SCANNING)
  146. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  147. if (cinfo->output_scanline >= cinfo->output_height) {
  148. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  149. return 0;
  150. }
  151. /* Call progress monitor hook if present */
  152. if (cinfo->progress != NULL) {
  153. cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  154. cinfo->progress->pass_limit = (long) cinfo->output_height;
  155. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  156. }
  157. /* Process some data */
  158. row_ctr = 0;
  159. (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
  160. cinfo->output_scanline += row_ctr;
  161. return row_ctr;
  162. }
  163. /*
  164. * Alternate entry point to read raw data.
  165. * Processes exactly one iMCU row per call, unless suspended.
  166. */
  167. GLOBAL(JDIMENSION)
  168. jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
  169. JDIMENSION max_lines)
  170. {
  171. JDIMENSION lines_per_iMCU_row;
  172. if (cinfo->global_state != DSTATE_RAW_OK)
  173. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  174. if (cinfo->output_scanline >= cinfo->output_height) {
  175. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  176. return 0;
  177. }
  178. /* Call progress monitor hook if present */
  179. if (cinfo->progress != NULL) {
  180. cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  181. cinfo->progress->pass_limit = (long) cinfo->output_height;
  182. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  183. }
  184. /* Verify that at least one iMCU row can be returned. */
  185. lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size;
  186. if (max_lines < lines_per_iMCU_row)
  187. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  188. /* Decompress directly into user's buffer. */
  189. if (! (*cinfo->coef->decompress_data) (cinfo, data))
  190. return 0; /* suspension forced, can do nothing more */
  191. /* OK, we processed one iMCU row. */
  192. cinfo->output_scanline += lines_per_iMCU_row;
  193. return lines_per_iMCU_row;
  194. }
  195. /* Additional entry points for buffered-image mode. */
  196. #ifdef D_MULTISCAN_FILES_SUPPORTED
  197. /*
  198. * Initialize for an output pass in buffered-image mode.
  199. */
  200. GLOBAL(boolean)
  201. jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
  202. {
  203. if (cinfo->global_state != DSTATE_BUFIMAGE &&
  204. cinfo->global_state != DSTATE_PRESCAN)
  205. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  206. /* Limit scan number to valid range */
  207. if (scan_number <= 0)
  208. scan_number = 1;
  209. if (cinfo->inputctl->eoi_reached &&
  210. scan_number > cinfo->input_scan_number)
  211. scan_number = cinfo->input_scan_number;
  212. cinfo->output_scan_number = scan_number;
  213. /* Perform any dummy output passes, and set up for the real pass */
  214. return output_pass_setup(cinfo);
  215. }
  216. /*
  217. * Finish up after an output pass in buffered-image mode.
  218. *
  219. * Returns FALSE if suspended. The return value need be inspected only if
  220. * a suspending data source is used.
  221. */
  222. GLOBAL(boolean)
  223. jpeg_finish_output (j_decompress_ptr cinfo)
  224. {
  225. if ((cinfo->global_state == DSTATE_SCANNING ||
  226. cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
  227. /* Terminate this pass. */
  228. /* We do not require the whole pass to have been completed. */
  229. (*cinfo->master->finish_output_pass) (cinfo);
  230. cinfo->global_state = DSTATE_BUFPOST;
  231. } else if (cinfo->global_state != DSTATE_BUFPOST) {
  232. /* BUFPOST = repeat call after a suspension, anything else is error */
  233. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  234. }
  235. /* Read markers looking for SOS or EOI */
  236. while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  237. ! cinfo->inputctl->eoi_reached) {
  238. if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  239. return FALSE; /* Suspend, come back later */
  240. }
  241. cinfo->global_state = DSTATE_BUFIMAGE;
  242. return TRUE;
  243. }
  244. #endif /* D_MULTISCAN_FILES_SUPPORTED */