jdapistd.cpp 10 KB

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