jdapistd.c 25 KB

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