jdapistd.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. // leave this as first line for PCH reasons...
  17. //
  18. #include "../server/exe_headers.h"
  19. #define JPEG_INTERNALS
  20. #include "jinclude.h"
  21. #include "jpeglib.h"
  22. /* Forward declarations */
  23. LOCAL boolean output_pass_setup JPP((j_decompress_ptr cinfo));
  24. /*
  25. * Decompression initialization.
  26. * jpeg_read_header must be completed before calling this.
  27. *
  28. * If a multipass operating mode was selected, this will do all but the
  29. * last pass, and thus may take a great deal of time.
  30. *
  31. * Returns FALSE if suspended. The return value need be inspected only if
  32. * a suspending data source is used.
  33. */
  34. GLOBAL boolean
  35. jpeg_start_decompress (j_decompress_ptr cinfo)
  36. {
  37. if (cinfo->global_state == DSTATE_READY) {
  38. /* First call: initialize master control, select active modules */
  39. jinit_master_decompress(cinfo);
  40. if (cinfo->buffered_image) {
  41. /* No more work here; expecting jpeg_start_output next */
  42. cinfo->global_state = DSTATE_BUFIMAGE;
  43. return TRUE;
  44. }
  45. cinfo->global_state = DSTATE_PRELOAD;
  46. }
  47. if (cinfo->global_state == DSTATE_PRELOAD) {
  48. /* If file has multiple scans, absorb them all into the coef buffer */
  49. if (cinfo->inputctl->has_multiple_scans) {
  50. #ifdef D_MULTISCAN_FILES_SUPPORTED
  51. for (;;) {
  52. int retcode;
  53. /* Call progress monitor hook if present */
  54. if (cinfo->progress != NULL)
  55. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  56. /* Absorb some more input */
  57. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  58. if (retcode == JPEG_SUSPENDED)
  59. return FALSE;
  60. if (retcode == JPEG_REACHED_EOI)
  61. break;
  62. /* Advance progress counter if appropriate */
  63. if (cinfo->progress != NULL &&
  64. (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
  65. if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
  66. /* jdmaster underestimated number of scans; ratchet up one scan */
  67. cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
  68. }
  69. }
  70. }
  71. #else
  72. ERREXIT(cinfo, JERR_NOT_COMPILED);
  73. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  74. }
  75. cinfo->output_scan_number = cinfo->input_scan_number;
  76. } else if (cinfo->global_state != DSTATE_PRESCAN)
  77. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  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. {
  91. if (cinfo->global_state != DSTATE_PRESCAN) {
  92. /* First call: do pass setup */
  93. (*cinfo->master->prepare_for_output_pass) (cinfo);
  94. cinfo->output_scanline = 0;
  95. cinfo->global_state = DSTATE_PRESCAN;
  96. }
  97. /* Loop over any required dummy passes */
  98. while (cinfo->master->is_dummy_pass) {
  99. #ifdef QUANT_2PASS_SUPPORTED
  100. /* Crank through the dummy pass */
  101. while (cinfo->output_scanline < cinfo->output_height) {
  102. JDIMENSION last_scanline;
  103. /* Call progress monitor hook if present */
  104. if (cinfo->progress != NULL) {
  105. cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  106. cinfo->progress->pass_limit = (long) cinfo->output_height;
  107. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  108. }
  109. /* Process some data */
  110. last_scanline = cinfo->output_scanline;
  111. (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
  112. &cinfo->output_scanline, (JDIMENSION) 0);
  113. if (cinfo->output_scanline == last_scanline)
  114. return FALSE; /* 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. {
  146. JDIMENSION row_ctr;
  147. if (cinfo->global_state != DSTATE_SCANNING)
  148. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  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. {
  173. JDIMENSION lines_per_iMCU_row;
  174. if (cinfo->global_state != DSTATE_RAW_OK)
  175. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  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. /* Decompress directly into user's buffer. */
  191. if (! (*cinfo->coef->decompress_data) (cinfo, data))
  192. return 0; /* suspension forced, can do nothing more */
  193. /* OK, we processed one iMCU row. */
  194. cinfo->output_scanline += lines_per_iMCU_row;
  195. return lines_per_iMCU_row;
  196. }
  197. /* Additional entry points for buffered-image mode. */
  198. #ifdef D_MULTISCAN_FILES_SUPPORTED
  199. /*
  200. * Initialize for an output pass in buffered-image mode.
  201. */
  202. GLOBAL boolean
  203. jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
  204. {
  205. if (cinfo->global_state != DSTATE_BUFIMAGE &&
  206. cinfo->global_state != DSTATE_PRESCAN)
  207. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  208. /* Limit scan number to valid range */
  209. if (scan_number <= 0)
  210. scan_number = 1;
  211. if (cinfo->inputctl->eoi_reached &&
  212. scan_number > cinfo->input_scan_number)
  213. scan_number = cinfo->input_scan_number;
  214. cinfo->output_scan_number = scan_number;
  215. /* Perform any dummy output passes, and set up for the real pass */
  216. return output_pass_setup(cinfo);
  217. }
  218. /*
  219. * Finish up after an output pass in buffered-image mode.
  220. *
  221. * Returns FALSE if suspended. The return value need be inspected only if
  222. * a suspending data source is used.
  223. */
  224. GLOBAL boolean
  225. jpeg_finish_output (j_decompress_ptr cinfo)
  226. {
  227. if ((cinfo->global_state == DSTATE_SCANNING ||
  228. cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
  229. /* Terminate this pass. */
  230. /* We do not require the whole pass to have been completed. */
  231. (*cinfo->master->finish_output_pass) (cinfo);
  232. cinfo->global_state = DSTATE_BUFPOST;
  233. } else if (cinfo->global_state != DSTATE_BUFPOST) {
  234. /* BUFPOST = repeat call after a suspension, anything else is error */
  235. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  236. }
  237. /* Read markers looking for SOS or EOI */
  238. while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  239. ! cinfo->inputctl->eoi_reached) {
  240. if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  241. return FALSE; /* Suspend, come back later */
  242. }
  243. cinfo->global_state = DSTATE_BUFIMAGE;
  244. return TRUE;
  245. }
  246. #endif /* D_MULTISCAN_FILES_SUPPORTED */