jdapistd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * jdapistd.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1994-1996, Thomas G. Lane.
  6. * libjpeg-turbo Modifications:
  7. * Copyright (C) 2010, 2015-2016, D. R. Commander.
  8. * Copyright (C) 2015, Google, Inc.
  9. * For conditions of distribution and use, see the accompanying README.ijg
  10. * file.
  11. *
  12. * This file contains application interface code for the decompression half
  13. * of the JPEG library. These are the "standard" API routines that are
  14. * used in the normal full-decompression case. They are not used by a
  15. * transcoding-only application. Note that if an application links in
  16. * jpeg_start_decompress, it will end up linking in the entire decompressor.
  17. * We thus must separate this file from jdapimin.c to avoid linking the
  18. * whole decompression library into a transcoder.
  19. */
  20. #include "jinclude.h"
  21. #include "jdmainct.h"
  22. #include "jdcoefct.h"
  23. #include "jdsample.h"
  24. #include "jmemsys.h"
  25. /* Forward declarations */
  26. LOCAL(boolean) output_pass_setup (j_decompress_ptr cinfo);
  27. /*
  28. * Decompression initialization.
  29. * jpeg_read_header must be completed before calling this.
  30. *
  31. * If a multipass operating mode was selected, this will do all but the
  32. * last pass, and thus may take a great deal of time.
  33. *
  34. * Returns FALSE if suspended. The return value need be inspected only if
  35. * a suspending data source is used.
  36. */
  37. GLOBAL(boolean)
  38. jpeg_start_decompress (j_decompress_ptr cinfo)
  39. {
  40. if (cinfo->global_state == DSTATE_READY) {
  41. /* First call: initialize master control, select active modules */
  42. jinit_master_decompress(cinfo);
  43. if (cinfo->buffered_image) {
  44. /* No more work here; expecting jpeg_start_output next */
  45. cinfo->global_state = DSTATE_BUFIMAGE;
  46. return TRUE;
  47. }
  48. cinfo->global_state = DSTATE_PRELOAD;
  49. }
  50. if (cinfo->global_state == DSTATE_PRELOAD) {
  51. /* If file has multiple scans, absorb them all into the coef buffer */
  52. if (cinfo->inputctl->has_multiple_scans) {
  53. #ifdef D_MULTISCAN_FILES_SUPPORTED
  54. for (;;) {
  55. int retcode;
  56. /* Call progress monitor hook if present */
  57. if (cinfo->progress != NULL)
  58. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  59. /* Absorb some more input */
  60. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  61. if (retcode == JPEG_SUSPENDED)
  62. return FALSE;
  63. if (retcode == JPEG_REACHED_EOI)
  64. break;
  65. /* Advance progress counter if appropriate */
  66. if (cinfo->progress != NULL &&
  67. (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
  68. if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
  69. /* jdmaster underestimated number of scans; ratchet up one scan */
  70. cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
  71. }
  72. }
  73. }
  74. #else
  75. ERREXIT(cinfo, JERR_NOT_COMPILED);
  76. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  77. }
  78. cinfo->output_scan_number = cinfo->input_scan_number;
  79. } else if (cinfo->global_state != DSTATE_PRESCAN)
  80. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  81. /* Perform any dummy output passes, and set up for the final pass */
  82. return output_pass_setup(cinfo);
  83. }
  84. /*
  85. * Set up for an output pass, and perform any dummy pass(es) needed.
  86. * Common subroutine for jpeg_start_decompress and jpeg_start_output.
  87. * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
  88. * Exit: If done, returns TRUE and sets global_state for proper output mode.
  89. * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
  90. */
  91. LOCAL(boolean)
  92. output_pass_setup (j_decompress_ptr cinfo)
  93. {
  94. if (cinfo->global_state != DSTATE_PRESCAN) {
  95. /* First call: do pass setup */
  96. (*cinfo->master->prepare_for_output_pass) (cinfo);
  97. cinfo->output_scanline = 0;
  98. cinfo->global_state = DSTATE_PRESCAN;
  99. }
  100. /* Loop over any required dummy passes */
  101. while (cinfo->master->is_dummy_pass) {
  102. #ifdef QUANT_2PASS_SUPPORTED
  103. /* Crank through the dummy pass */
  104. while (cinfo->output_scanline < cinfo->output_height) {
  105. JDIMENSION last_scanline;
  106. /* Call progress monitor hook if present */
  107. if (cinfo->progress != NULL) {
  108. cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  109. cinfo->progress->pass_limit = (long) cinfo->output_height;
  110. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  111. }
  112. /* Process some data */
  113. last_scanline = cinfo->output_scanline;
  114. (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
  115. &cinfo->output_scanline, (JDIMENSION) 0);
  116. if (cinfo->output_scanline == last_scanline)
  117. return FALSE; /* No progress made, must suspend */
  118. }
  119. /* Finish up dummy pass, and set up for another one */
  120. (*cinfo->master->finish_output_pass) (cinfo);
  121. (*cinfo->master->prepare_for_output_pass) (cinfo);
  122. cinfo->output_scanline = 0;
  123. #else
  124. ERREXIT(cinfo, JERR_NOT_COMPILED);
  125. #endif /* QUANT_2PASS_SUPPORTED */
  126. }
  127. /* Ready for application to drive output pass through
  128. * jpeg_read_scanlines or jpeg_read_raw_data.
  129. */
  130. cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
  131. return TRUE;
  132. }
  133. /*
  134. * Enable partial scanline decompression
  135. *
  136. * Must be called after jpeg_start_decompress() and before any calls to
  137. * jpeg_read_scanlines() or jpeg_skip_scanlines().
  138. *
  139. * Refer to libjpeg.txt for more information.
  140. */
  141. GLOBAL(void)
  142. jpeg_crop_scanline (j_decompress_ptr cinfo, JDIMENSION *xoffset,
  143. JDIMENSION *width)
  144. {
  145. int ci, align, orig_downsampled_width;
  146. JDIMENSION input_xoffset;
  147. boolean reinit_upsampler = FALSE;
  148. jpeg_component_info *compptr;
  149. if (cinfo->global_state != DSTATE_SCANNING || cinfo->output_scanline != 0)
  150. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  151. if (!xoffset || !width)
  152. ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
  153. /* xoffset and width must fall within the output image dimensions. */
  154. if (*width == 0 || *xoffset + *width > cinfo->output_width)
  155. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  156. /* No need to do anything if the caller wants the entire width. */
  157. if (*width == cinfo->output_width)
  158. return;
  159. /* Ensuring the proper alignment of xoffset is tricky. At minimum, it
  160. * must align with an MCU boundary, because:
  161. *
  162. * (1) The IDCT is performed in blocks, and it is not feasible to modify
  163. * the algorithm so that it can transform partial blocks.
  164. * (2) Because of the SIMD extensions, any input buffer passed to the
  165. * upsampling and color conversion routines must be aligned to the
  166. * SIMD word size (for instance, 128-bit in the case of SSE2.) The
  167. * easiest way to accomplish this without copying data is to ensure
  168. * that upsampling and color conversion begin at the start of the
  169. * first MCU column that will be inverse transformed.
  170. *
  171. * In practice, we actually impose a stricter alignment requirement. We
  172. * require that xoffset be a multiple of the maximum MCU column width of all
  173. * of the components (the "iMCU column width.") This is to simplify the
  174. * single-pass decompression case, allowing us to use the same MCU column
  175. * width for all of the components.
  176. */
  177. align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
  178. /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
  179. input_xoffset = *xoffset;
  180. *xoffset = (input_xoffset / align) * align;
  181. /* Adjust the width so that the right edge of the output image is as
  182. * requested (only the left edge is altered.) It is important that calling
  183. * programs check this value after this function returns, so that they can
  184. * allocate an output buffer with the appropriate size.
  185. */
  186. *width = *width + input_xoffset - *xoffset;
  187. cinfo->output_width = *width;
  188. /* Set the first and last iMCU columns that we must decompress. These values
  189. * will be used in single-scan decompressions.
  190. */
  191. cinfo->master->first_iMCU_col =
  192. (JDIMENSION) (long) (*xoffset) / (long) align;
  193. cinfo->master->last_iMCU_col =
  194. (JDIMENSION) jdiv_round_up((long) (*xoffset + cinfo->output_width),
  195. (long) align) - 1;
  196. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  197. ci++, compptr++) {
  198. /* Set downsampled_width to the new output width. */
  199. orig_downsampled_width = compptr->downsampled_width;
  200. compptr->downsampled_width =
  201. (JDIMENSION) jdiv_round_up((long) (cinfo->output_width *
  202. compptr->h_samp_factor),
  203. (long) cinfo->max_h_samp_factor);
  204. if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)
  205. reinit_upsampler = TRUE;
  206. /* Set the first and last iMCU columns that we must decompress. These
  207. * values will be used in multi-scan decompressions.
  208. */
  209. cinfo->master->first_MCU_col[ci] =
  210. (JDIMENSION) (long) (*xoffset * compptr->h_samp_factor) /
  211. (long) align;
  212. cinfo->master->last_MCU_col[ci] =
  213. (JDIMENSION) jdiv_round_up((long) ((*xoffset + cinfo->output_width) *
  214. compptr->h_samp_factor),
  215. (long) align) - 1;
  216. }
  217. if (reinit_upsampler) {
  218. cinfo->master->jinit_upsampler_no_alloc = TRUE;
  219. jinit_upsampler(cinfo);
  220. cinfo->master->jinit_upsampler_no_alloc = FALSE;
  221. }
  222. }
  223. /*
  224. * Read some scanlines of data from the JPEG decompressor.
  225. *
  226. * The return value will be the number of lines actually read.
  227. * This may be less than the number requested in several cases,
  228. * including bottom of image, data source suspension, and operating
  229. * modes that emit multiple scanlines at a time.
  230. *
  231. * Note: we warn about excess calls to jpeg_read_scanlines() since
  232. * this likely signals an application programmer error. However,
  233. * an oversize buffer (max_lines > scanlines remaining) is not an error.
  234. */
  235. GLOBAL(JDIMENSION)
  236. jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
  237. JDIMENSION max_lines)
  238. {
  239. JDIMENSION row_ctr;
  240. if (cinfo->global_state != DSTATE_SCANNING)
  241. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  242. if (cinfo->output_scanline >= cinfo->output_height) {
  243. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  244. return 0;
  245. }
  246. /* Call progress monitor hook if present */
  247. if (cinfo->progress != NULL) {
  248. cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  249. cinfo->progress->pass_limit = (long) cinfo->output_height;
  250. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  251. }
  252. /* Process some data */
  253. row_ctr = 0;
  254. (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
  255. cinfo->output_scanline += row_ctr;
  256. return row_ctr;
  257. }
  258. /* Dummy color convert function used by jpeg_skip_scanlines() */
  259. LOCAL(void)
  260. noop_convert (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  261. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  262. {
  263. }
  264. /*
  265. * In some cases, it is best to call jpeg_read_scanlines() and discard the
  266. * output, rather than skipping the scanlines, because this allows us to
  267. * maintain the internal state of the context-based upsampler. In these cases,
  268. * we set up and tear down a dummy color converter in order to avoid valgrind
  269. * errors and to achieve the best possible performance.
  270. */
  271. LOCAL(void)
  272. read_and_discard_scanlines (j_decompress_ptr cinfo, JDIMENSION num_lines)
  273. {
  274. JDIMENSION n;
  275. void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  276. JDIMENSION input_row, JSAMPARRAY output_buf,
  277. int num_rows);
  278. color_convert = cinfo->cconvert->color_convert;
  279. cinfo->cconvert->color_convert = noop_convert;
  280. for (n = 0; n < num_lines; n++)
  281. jpeg_read_scanlines(cinfo, NULL, 1);
  282. cinfo->cconvert->color_convert = color_convert;
  283. }
  284. /*
  285. * Called by jpeg_skip_scanlines(). This partially skips a decompress block by
  286. * incrementing the rowgroup counter.
  287. */
  288. LOCAL(void)
  289. increment_simple_rowgroup_ctr (j_decompress_ptr cinfo, JDIMENSION rows)
  290. {
  291. JDIMENSION rows_left;
  292. my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
  293. /* Increment the counter to the next row group after the skipped rows. */
  294. main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
  295. /* Partially skipping a row group would involve modifying the internal state
  296. * of the upsampler, so read the remaining rows into a dummy buffer instead.
  297. */
  298. rows_left = rows % cinfo->max_v_samp_factor;
  299. cinfo->output_scanline += rows - rows_left;
  300. read_and_discard_scanlines(cinfo, rows_left);
  301. }
  302. /*
  303. * Skips some scanlines of data from the JPEG decompressor.
  304. *
  305. * The return value will be the number of lines actually skipped. If skipping
  306. * num_lines would move beyond the end of the image, then the actual number of
  307. * lines remaining in the image is returned. Otherwise, the return value will
  308. * be equal to num_lines.
  309. *
  310. * Refer to libjpeg.txt for more information.
  311. */
  312. GLOBAL(JDIMENSION)
  313. jpeg_skip_scanlines (j_decompress_ptr cinfo, JDIMENSION num_lines)
  314. {
  315. my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
  316. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  317. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  318. JDIMENSION i, x;
  319. int y;
  320. JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
  321. JDIMENSION lines_to_skip, lines_to_read;
  322. if (cinfo->global_state != DSTATE_SCANNING)
  323. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  324. /* Do not skip past the bottom of the image. */
  325. if (cinfo->output_scanline + num_lines >= cinfo->output_height) {
  326. cinfo->output_scanline = cinfo->output_height;
  327. return cinfo->output_height - cinfo->output_scanline;
  328. }
  329. if (num_lines == 0)
  330. return 0;
  331. lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
  332. lines_left_in_iMCU_row =
  333. (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
  334. lines_per_iMCU_row;
  335. lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
  336. /* Skip the lines remaining in the current iMCU row. When upsampling
  337. * requires context rows, we need the previous and next rows in order to read
  338. * the current row. This adds some complexity.
  339. */
  340. if (cinfo->upsample->need_context_rows) {
  341. /* If the skipped lines would not move us past the current iMCU row, we
  342. * read the lines and ignore them. There might be a faster way of doing
  343. * this, but we are facing increasing complexity for diminishing returns.
  344. * The increasing complexity would be a by-product of meddling with the
  345. * state machine used to skip context rows. Near the end of an iMCU row,
  346. * the next iMCU row may have already been entropy-decoded. In this unique
  347. * case, we will read the next iMCU row if we cannot skip past it as well.
  348. */
  349. if ((num_lines < lines_left_in_iMCU_row + 1) ||
  350. (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
  351. lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
  352. read_and_discard_scanlines(cinfo, num_lines);
  353. return num_lines;
  354. }
  355. /* If the next iMCU row has already been entropy-decoded, make sure that
  356. * we do not skip too far.
  357. */
  358. if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
  359. cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
  360. lines_after_iMCU_row -= lines_per_iMCU_row;
  361. } else {
  362. cinfo->output_scanline += lines_left_in_iMCU_row;
  363. }
  364. /* If we have just completed the first block, adjust the buffer pointers */
  365. if (main_ptr->iMCU_row_ctr == 0 ||
  366. (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
  367. set_wraparound_pointers(cinfo);
  368. main_ptr->buffer_full = FALSE;
  369. main_ptr->rowgroup_ctr = 0;
  370. main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
  371. upsample->next_row_out = cinfo->max_v_samp_factor;
  372. upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
  373. }
  374. /* Skipping is much simpler when context rows are not required. */
  375. else {
  376. if (num_lines < lines_left_in_iMCU_row) {
  377. increment_simple_rowgroup_ctr(cinfo, num_lines);
  378. return num_lines;
  379. } else {
  380. cinfo->output_scanline += lines_left_in_iMCU_row;
  381. main_ptr->buffer_full = FALSE;
  382. main_ptr->rowgroup_ctr = 0;
  383. upsample->next_row_out = cinfo->max_v_samp_factor;
  384. upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
  385. }
  386. }
  387. /* Calculate how many full iMCU rows we can skip. */
  388. if (cinfo->upsample->need_context_rows)
  389. lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
  390. lines_per_iMCU_row;
  391. else
  392. lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
  393. lines_per_iMCU_row;
  394. /* Calculate the number of lines that remain to be skipped after skipping all
  395. * of the full iMCU rows that we can. We will not read these lines unless we
  396. * have to.
  397. */
  398. lines_to_read = lines_after_iMCU_row - lines_to_skip;
  399. /* For images requiring multiple scans (progressive, non-interleaved, etc.),
  400. * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
  401. * that the input data source is non-suspending. This makes skipping easy.
  402. */
  403. if (cinfo->inputctl->has_multiple_scans) {
  404. if (cinfo->upsample->need_context_rows) {
  405. cinfo->output_scanline += lines_to_skip;
  406. cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
  407. main_ptr->iMCU_row_ctr += lines_after_iMCU_row / lines_per_iMCU_row;
  408. /* It is complex to properly move to the middle of a context block, so
  409. * read the remaining lines instead of skipping them.
  410. */
  411. read_and_discard_scanlines(cinfo, lines_to_read);
  412. } else {
  413. cinfo->output_scanline += lines_to_skip;
  414. cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
  415. increment_simple_rowgroup_ctr(cinfo, lines_to_read);
  416. }
  417. upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
  418. return num_lines;
  419. }
  420. /* Skip the iMCU rows that we can safely skip. */
  421. for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
  422. for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
  423. for (x = 0; x < cinfo->MCUs_per_row; x++) {
  424. /* Calling decode_mcu() with a NULL pointer causes it to discard the
  425. * decoded coefficients. This is ~5% faster for large subsets, but
  426. * it's tough to tell a difference for smaller images.
  427. */
  428. (*cinfo->entropy->decode_mcu) (cinfo, NULL);
  429. }
  430. }
  431. cinfo->input_iMCU_row++;
  432. cinfo->output_iMCU_row++;
  433. if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
  434. start_iMCU_row(cinfo);
  435. else
  436. (*cinfo->inputctl->finish_input_pass) (cinfo);
  437. }
  438. cinfo->output_scanline += lines_to_skip;
  439. if (cinfo->upsample->need_context_rows) {
  440. /* Context-based upsampling keeps track of iMCU rows. */
  441. main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
  442. /* It is complex to properly move to the middle of a context block, so
  443. * read the remaining lines instead of skipping them.
  444. */
  445. read_and_discard_scanlines(cinfo, lines_to_read);
  446. } else {
  447. increment_simple_rowgroup_ctr(cinfo, lines_to_read);
  448. }
  449. /* Since skipping lines involves skipping the upsampling step, the value of
  450. * "rows_to_go" will become invalid unless we set it here. NOTE: This is a
  451. * bit odd, since "rows_to_go" seems to be redundantly keeping track of
  452. * output_scanline.
  453. */
  454. upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
  455. /* Always skip the requested number of lines. */
  456. return num_lines;
  457. }
  458. /*
  459. * Alternate entry point to read raw data.
  460. * Processes exactly one iMCU row per call, unless suspended.
  461. */
  462. GLOBAL(JDIMENSION)
  463. jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
  464. JDIMENSION max_lines)
  465. {
  466. JDIMENSION lines_per_iMCU_row;
  467. if (cinfo->global_state != DSTATE_RAW_OK)
  468. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  469. if (cinfo->output_scanline >= cinfo->output_height) {
  470. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  471. return 0;
  472. }
  473. /* Call progress monitor hook if present */
  474. if (cinfo->progress != NULL) {
  475. cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  476. cinfo->progress->pass_limit = (long) cinfo->output_height;
  477. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  478. }
  479. /* Verify that at least one iMCU row can be returned. */
  480. lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
  481. if (max_lines < lines_per_iMCU_row)
  482. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  483. /* Decompress directly into user's buffer. */
  484. if (! (*cinfo->coef->decompress_data) (cinfo, data))
  485. return 0; /* suspension forced, can do nothing more */
  486. /* OK, we processed one iMCU row. */
  487. cinfo->output_scanline += lines_per_iMCU_row;
  488. return lines_per_iMCU_row;
  489. }
  490. /* Additional entry points for buffered-image mode. */
  491. #ifdef D_MULTISCAN_FILES_SUPPORTED
  492. /*
  493. * Initialize for an output pass in buffered-image mode.
  494. */
  495. GLOBAL(boolean)
  496. jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
  497. {
  498. if (cinfo->global_state != DSTATE_BUFIMAGE &&
  499. cinfo->global_state != DSTATE_PRESCAN)
  500. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  501. /* Limit scan number to valid range */
  502. if (scan_number <= 0)
  503. scan_number = 1;
  504. if (cinfo->inputctl->eoi_reached &&
  505. scan_number > cinfo->input_scan_number)
  506. scan_number = cinfo->input_scan_number;
  507. cinfo->output_scan_number = scan_number;
  508. /* Perform any dummy output passes, and set up for the real pass */
  509. return output_pass_setup(cinfo);
  510. }
  511. /*
  512. * Finish up after an output pass in buffered-image mode.
  513. *
  514. * Returns FALSE if suspended. The return value need be inspected only if
  515. * a suspending data source is used.
  516. */
  517. GLOBAL(boolean)
  518. jpeg_finish_output (j_decompress_ptr cinfo)
  519. {
  520. if ((cinfo->global_state == DSTATE_SCANNING ||
  521. cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
  522. /* Terminate this pass. */
  523. /* We do not require the whole pass to have been completed. */
  524. (*cinfo->master->finish_output_pass) (cinfo);
  525. cinfo->global_state = DSTATE_BUFPOST;
  526. } else if (cinfo->global_state != DSTATE_BUFPOST) {
  527. /* BUFPOST = repeat call after a suspension, anything else is error */
  528. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  529. }
  530. /* Read markers looking for SOS or EOI */
  531. while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  532. ! cinfo->inputctl->eoi_reached) {
  533. if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  534. return FALSE; /* Suspend, come back later */
  535. }
  536. cinfo->global_state = DSTATE_BUFIMAGE;
  537. return TRUE;
  538. }
  539. #endif /* D_MULTISCAN_FILES_SUPPORTED */