jdapistd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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-2018, 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. if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
  178. align = cinfo->_min_DCT_scaled_size;
  179. else
  180. align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
  181. /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
  182. input_xoffset = *xoffset;
  183. *xoffset = (input_xoffset / align) * align;
  184. /* Adjust the width so that the right edge of the output image is as
  185. * requested (only the left edge is altered.) It is important that calling
  186. * programs check this value after this function returns, so that they can
  187. * allocate an output buffer with the appropriate size.
  188. */
  189. *width = *width + input_xoffset - *xoffset;
  190. cinfo->output_width = *width;
  191. /* Set the first and last iMCU columns that we must decompress. These values
  192. * will be used in single-scan decompressions.
  193. */
  194. cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
  195. cinfo->master->last_iMCU_col =
  196. (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
  197. (long)align) - 1;
  198. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  199. ci++, compptr++) {
  200. int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
  201. 1 : compptr->h_samp_factor;
  202. /* Set downsampled_width to the new output width. */
  203. orig_downsampled_width = compptr->downsampled_width;
  204. compptr->downsampled_width =
  205. (JDIMENSION)jdiv_round_up((long)(cinfo->output_width *
  206. compptr->h_samp_factor),
  207. (long)cinfo->max_h_samp_factor);
  208. if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)
  209. reinit_upsampler = TRUE;
  210. /* Set the first and last iMCU columns that we must decompress. These
  211. * values will be used in multi-scan decompressions.
  212. */
  213. cinfo->master->first_MCU_col[ci] =
  214. (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
  215. cinfo->master->last_MCU_col[ci] =
  216. (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
  217. (long)align) - 1;
  218. }
  219. if (reinit_upsampler) {
  220. cinfo->master->jinit_upsampler_no_alloc = TRUE;
  221. jinit_upsampler(cinfo);
  222. cinfo->master->jinit_upsampler_no_alloc = FALSE;
  223. }
  224. }
  225. /*
  226. * Read some scanlines of data from the JPEG decompressor.
  227. *
  228. * The return value will be the number of lines actually read.
  229. * This may be less than the number requested in several cases,
  230. * including bottom of image, data source suspension, and operating
  231. * modes that emit multiple scanlines at a time.
  232. *
  233. * Note: we warn about excess calls to jpeg_read_scanlines() since
  234. * this likely signals an application programmer error. However,
  235. * an oversize buffer (max_lines > scanlines remaining) is not an error.
  236. */
  237. GLOBAL(JDIMENSION)
  238. jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines,
  239. JDIMENSION max_lines)
  240. {
  241. JDIMENSION row_ctr;
  242. if (cinfo->global_state != DSTATE_SCANNING)
  243. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  244. if (cinfo->output_scanline >= cinfo->output_height) {
  245. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  246. return 0;
  247. }
  248. /* Call progress monitor hook if present */
  249. if (cinfo->progress != NULL) {
  250. cinfo->progress->pass_counter = (long)cinfo->output_scanline;
  251. cinfo->progress->pass_limit = (long)cinfo->output_height;
  252. (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
  253. }
  254. /* Process some data */
  255. row_ctr = 0;
  256. (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
  257. cinfo->output_scanline += row_ctr;
  258. return row_ctr;
  259. }
  260. /* Dummy color convert function used by jpeg_skip_scanlines() */
  261. LOCAL(void)
  262. noop_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  263. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  264. {
  265. }
  266. /* Dummy quantize function used by jpeg_skip_scanlines() */
  267. LOCAL(void)
  268. noop_quantize(j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  269. JSAMPARRAY output_buf, int num_rows)
  270. {
  271. }
  272. /*
  273. * In some cases, it is best to call jpeg_read_scanlines() and discard the
  274. * output, rather than skipping the scanlines, because this allows us to
  275. * maintain the internal state of the context-based upsampler. In these cases,
  276. * we set up and tear down a dummy color converter in order to avoid valgrind
  277. * errors and to achieve the best possible performance.
  278. */
  279. LOCAL(void)
  280. read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
  281. {
  282. JDIMENSION n;
  283. void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  284. JDIMENSION input_row, JSAMPARRAY output_buf,
  285. int num_rows) = NULL;
  286. void (*color_quantize) (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  287. JSAMPARRAY output_buf, int num_rows) = NULL;
  288. if (cinfo->cconvert && cinfo->cconvert->color_convert) {
  289. color_convert = cinfo->cconvert->color_convert;
  290. cinfo->cconvert->color_convert = noop_convert;
  291. }
  292. if (cinfo->cquantize && cinfo->cquantize->color_quantize) {
  293. color_quantize = cinfo->cquantize->color_quantize;
  294. cinfo->cquantize->color_quantize = noop_quantize;
  295. }
  296. for (n = 0; n < num_lines; n++)
  297. jpeg_read_scanlines(cinfo, NULL, 1);
  298. if (color_convert)
  299. cinfo->cconvert->color_convert = color_convert;
  300. if (color_quantize)
  301. cinfo->cquantize->color_quantize = color_quantize;
  302. }
  303. /*
  304. * Called by jpeg_skip_scanlines(). This partially skips a decompress block by
  305. * incrementing the rowgroup counter.
  306. */
  307. LOCAL(void)
  308. increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)
  309. {
  310. JDIMENSION rows_left;
  311. my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
  312. /* Increment the counter to the next row group after the skipped rows. */
  313. main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
  314. /* Partially skipping a row group would involve modifying the internal state
  315. * of the upsampler, so read the remaining rows into a dummy buffer instead.
  316. */
  317. rows_left = rows % cinfo->max_v_samp_factor;
  318. cinfo->output_scanline += rows - rows_left;
  319. read_and_discard_scanlines(cinfo, rows_left);
  320. }
  321. /*
  322. * Skips some scanlines of data from the JPEG decompressor.
  323. *
  324. * The return value will be the number of lines actually skipped. If skipping
  325. * num_lines would move beyond the end of the image, then the actual number of
  326. * lines remaining in the image is returned. Otherwise, the return value will
  327. * be equal to num_lines.
  328. *
  329. * Refer to libjpeg.txt for more information.
  330. */
  331. GLOBAL(JDIMENSION)
  332. jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
  333. {
  334. my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
  335. my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
  336. my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
  337. JDIMENSION i, x;
  338. int y;
  339. JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
  340. JDIMENSION lines_to_skip, lines_to_read;
  341. if (cinfo->global_state != DSTATE_SCANNING)
  342. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  343. /* Do not skip past the bottom of the image. */
  344. if (cinfo->output_scanline + num_lines >= cinfo->output_height) {
  345. cinfo->output_scanline = cinfo->output_height;
  346. (*cinfo->inputctl->finish_input_pass) (cinfo);
  347. cinfo->inputctl->eoi_reached = TRUE;
  348. return cinfo->output_height - cinfo->output_scanline;
  349. }
  350. if (num_lines == 0)
  351. return 0;
  352. lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
  353. lines_left_in_iMCU_row =
  354. (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
  355. lines_per_iMCU_row;
  356. lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
  357. /* Skip the lines remaining in the current iMCU row. When upsampling
  358. * requires context rows, we need the previous and next rows in order to read
  359. * the current row. This adds some complexity.
  360. */
  361. if (cinfo->upsample->need_context_rows) {
  362. /* If the skipped lines would not move us past the current iMCU row, we
  363. * read the lines and ignore them. There might be a faster way of doing
  364. * this, but we are facing increasing complexity for diminishing returns.
  365. * The increasing complexity would be a by-product of meddling with the
  366. * state machine used to skip context rows. Near the end of an iMCU row,
  367. * the next iMCU row may have already been entropy-decoded. In this unique
  368. * case, we will read the next iMCU row if we cannot skip past it as well.
  369. */
  370. if ((num_lines < lines_left_in_iMCU_row + 1) ||
  371. (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
  372. lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
  373. read_and_discard_scanlines(cinfo, num_lines);
  374. return num_lines;
  375. }
  376. /* If the next iMCU row has already been entropy-decoded, make sure that
  377. * we do not skip too far.
  378. */
  379. if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
  380. cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
  381. lines_after_iMCU_row -= lines_per_iMCU_row;
  382. } else {
  383. cinfo->output_scanline += lines_left_in_iMCU_row;
  384. }
  385. /* If we have just completed the first block, adjust the buffer pointers */
  386. if (main_ptr->iMCU_row_ctr == 0 ||
  387. (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
  388. set_wraparound_pointers(cinfo);
  389. main_ptr->buffer_full = FALSE;
  390. main_ptr->rowgroup_ctr = 0;
  391. main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
  392. upsample->next_row_out = cinfo->max_v_samp_factor;
  393. upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
  394. }
  395. /* Skipping is much simpler when context rows are not required. */
  396. else {
  397. if (num_lines < lines_left_in_iMCU_row) {
  398. increment_simple_rowgroup_ctr(cinfo, num_lines);
  399. return num_lines;
  400. } else {
  401. cinfo->output_scanline += lines_left_in_iMCU_row;
  402. main_ptr->buffer_full = FALSE;
  403. main_ptr->rowgroup_ctr = 0;
  404. upsample->next_row_out = cinfo->max_v_samp_factor;
  405. upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
  406. }
  407. }
  408. /* Calculate how many full iMCU rows we can skip. */
  409. if (cinfo->upsample->need_context_rows)
  410. lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
  411. lines_per_iMCU_row;
  412. else
  413. lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
  414. lines_per_iMCU_row;
  415. /* Calculate the number of lines that remain to be skipped after skipping all
  416. * of the full iMCU rows that we can. We will not read these lines unless we
  417. * have to.
  418. */
  419. lines_to_read = lines_after_iMCU_row - lines_to_skip;
  420. /* For images requiring multiple scans (progressive, non-interleaved, etc.),
  421. * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
  422. * that the input data source is non-suspending. This makes skipping easy.
  423. */
  424. if (cinfo->inputctl->has_multiple_scans) {
  425. if (cinfo->upsample->need_context_rows) {
  426. cinfo->output_scanline += lines_to_skip;
  427. cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
  428. main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
  429. /* It is complex to properly move to the middle of a context block, so
  430. * read the remaining lines instead of skipping them.
  431. */
  432. read_and_discard_scanlines(cinfo, lines_to_read);
  433. } else {
  434. cinfo->output_scanline += lines_to_skip;
  435. cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
  436. increment_simple_rowgroup_ctr(cinfo, lines_to_read);
  437. }
  438. upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
  439. return num_lines;
  440. }
  441. /* Skip the iMCU rows that we can safely skip. */
  442. for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
  443. for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
  444. for (x = 0; x < cinfo->MCUs_per_row; x++) {
  445. /* Calling decode_mcu() with a NULL pointer causes it to discard the
  446. * decoded coefficients. This is ~5% faster for large subsets, but
  447. * it's tough to tell a difference for smaller images.
  448. */
  449. (*cinfo->entropy->decode_mcu) (cinfo, NULL);
  450. }
  451. }
  452. cinfo->input_iMCU_row++;
  453. cinfo->output_iMCU_row++;
  454. if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
  455. start_iMCU_row(cinfo);
  456. else
  457. (*cinfo->inputctl->finish_input_pass) (cinfo);
  458. }
  459. cinfo->output_scanline += lines_to_skip;
  460. if (cinfo->upsample->need_context_rows) {
  461. /* Context-based upsampling keeps track of iMCU rows. */
  462. main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
  463. /* It is complex to properly move to the middle of a context block, so
  464. * read the remaining lines instead of skipping them.
  465. */
  466. read_and_discard_scanlines(cinfo, lines_to_read);
  467. } else {
  468. increment_simple_rowgroup_ctr(cinfo, lines_to_read);
  469. }
  470. /* Since skipping lines involves skipping the upsampling step, the value of
  471. * "rows_to_go" will become invalid unless we set it here. NOTE: This is a
  472. * bit odd, since "rows_to_go" seems to be redundantly keeping track of
  473. * output_scanline.
  474. */
  475. upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
  476. /* Always skip the requested number of lines. */
  477. return num_lines;
  478. }
  479. /*
  480. * Alternate entry point to read raw data.
  481. * Processes exactly one iMCU row per call, unless suspended.
  482. */
  483. GLOBAL(JDIMENSION)
  484. jpeg_read_raw_data(j_decompress_ptr cinfo, JSAMPIMAGE data,
  485. JDIMENSION max_lines)
  486. {
  487. JDIMENSION lines_per_iMCU_row;
  488. if (cinfo->global_state != DSTATE_RAW_OK)
  489. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  490. if (cinfo->output_scanline >= cinfo->output_height) {
  491. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  492. return 0;
  493. }
  494. /* Call progress monitor hook if present */
  495. if (cinfo->progress != NULL) {
  496. cinfo->progress->pass_counter = (long)cinfo->output_scanline;
  497. cinfo->progress->pass_limit = (long)cinfo->output_height;
  498. (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
  499. }
  500. /* Verify that at least one iMCU row can be returned. */
  501. lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
  502. if (max_lines < lines_per_iMCU_row)
  503. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  504. /* Decompress directly into user's buffer. */
  505. if (!(*cinfo->coef->decompress_data) (cinfo, data))
  506. return 0; /* suspension forced, can do nothing more */
  507. /* OK, we processed one iMCU row. */
  508. cinfo->output_scanline += lines_per_iMCU_row;
  509. return lines_per_iMCU_row;
  510. }
  511. /* Additional entry points for buffered-image mode. */
  512. #ifdef D_MULTISCAN_FILES_SUPPORTED
  513. /*
  514. * Initialize for an output pass in buffered-image mode.
  515. */
  516. GLOBAL(boolean)
  517. jpeg_start_output(j_decompress_ptr cinfo, int scan_number)
  518. {
  519. if (cinfo->global_state != DSTATE_BUFIMAGE &&
  520. cinfo->global_state != DSTATE_PRESCAN)
  521. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  522. /* Limit scan number to valid range */
  523. if (scan_number <= 0)
  524. scan_number = 1;
  525. if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)
  526. scan_number = cinfo->input_scan_number;
  527. cinfo->output_scan_number = scan_number;
  528. /* Perform any dummy output passes, and set up for the real pass */
  529. return output_pass_setup(cinfo);
  530. }
  531. /*
  532. * Finish up after an output pass in buffered-image mode.
  533. *
  534. * Returns FALSE if suspended. The return value need be inspected only if
  535. * a suspending data source is used.
  536. */
  537. GLOBAL(boolean)
  538. jpeg_finish_output(j_decompress_ptr cinfo)
  539. {
  540. if ((cinfo->global_state == DSTATE_SCANNING ||
  541. cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
  542. /* Terminate this pass. */
  543. /* We do not require the whole pass to have been completed. */
  544. (*cinfo->master->finish_output_pass) (cinfo);
  545. cinfo->global_state = DSTATE_BUFPOST;
  546. } else if (cinfo->global_state != DSTATE_BUFPOST) {
  547. /* BUFPOST = repeat call after a suspension, anything else is error */
  548. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  549. }
  550. /* Read markers looking for SOS or EOI */
  551. while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  552. !cinfo->inputctl->eoi_reached) {
  553. if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  554. return FALSE; /* Suspend, come back later */
  555. }
  556. cinfo->global_state = DSTATE_BUFIMAGE;
  557. return TRUE;
  558. }
  559. #endif /* D_MULTISCAN_FILES_SUPPORTED */