jdapistd.c 24 KB

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