Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
9.2 KiB

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