Path: blob/master/thirdparty/libjpeg-turbo/src/jdapistd.c
21731 views
/*1* jdapistd.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1996, Thomas G. Lane.5* libjpeg-turbo Modifications:6* Copyright (C) 2010, 2015-2020, 2022-2025, D. R. Commander.7* Copyright (C) 2015, Google, Inc.8* For conditions of distribution and use, see the accompanying README.ijg9* file.10*11* This file contains application interface code for the decompression half12* of the JPEG library. These are the "standard" API routines that are13* used in the normal full-decompression case. They are not used by a14* transcoding-only application. Note that if an application links in15* jpeg_start_decompress, it will end up linking in the entire decompressor.16* We thus must separate this file from jdapimin.c to avoid linking the17* whole decompression library into a transcoder.18*/1920#include "jinclude.h"21#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)22#include "jdmainct.h"23#include "jdcoefct.h"24#else25#define JPEG_INTERNALS26#include "jpeglib.h"27#endif28#include "jdmaster.h"29#include "jdmerge.h"30#include "jdsample.h"31#include "jmemsys.h"3233#if BITS_IN_JSAMPLE == 83435/* Forward declarations */36LOCAL(boolean) output_pass_setup(j_decompress_ptr cinfo);373839/*40* Decompression initialization.41* jpeg_read_header must be completed before calling this.42*43* If a multipass operating mode was selected, this will do all but the44* last pass, and thus may take a great deal of time.45*46* Returns FALSE if suspended. The return value need be inspected only if47* a suspending data source is used.48*/4950GLOBAL(boolean)51jpeg_start_decompress(j_decompress_ptr cinfo)52{53if (cinfo->global_state == DSTATE_READY) {54/* First call: initialize master control, select active modules */55jinit_master_decompress(cinfo);56if (cinfo->buffered_image) {57/* No more work here; expecting jpeg_start_output next */58cinfo->global_state = DSTATE_BUFIMAGE;59return TRUE;60}61cinfo->global_state = DSTATE_PRELOAD;62}63if (cinfo->global_state == DSTATE_PRELOAD) {64/* If file has multiple scans, absorb them all into the coef buffer */65if (cinfo->inputctl->has_multiple_scans) {66#ifdef D_MULTISCAN_FILES_SUPPORTED67for (;;) {68int retcode;69/* Call progress monitor hook if present */70if (cinfo->progress != NULL)71(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);72/* Absorb some more input */73retcode = (*cinfo->inputctl->consume_input) (cinfo);74if (retcode == JPEG_SUSPENDED)75return FALSE;76if (retcode == JPEG_REACHED_EOI)77break;78/* Advance progress counter if appropriate */79if (cinfo->progress != NULL &&80(retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {81if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {82/* jdmaster underestimated number of scans; ratchet up one scan */83cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;84}85}86}87#else88ERREXIT(cinfo, JERR_NOT_COMPILED);89#endif /* D_MULTISCAN_FILES_SUPPORTED */90}91cinfo->output_scan_number = cinfo->input_scan_number;92} else if (cinfo->global_state != DSTATE_PRESCAN)93ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);94/* Perform any dummy output passes, and set up for the final pass */95return output_pass_setup(cinfo);96}979899/*100* Set up for an output pass, and perform any dummy pass(es) needed.101* Common subroutine for jpeg_start_decompress and jpeg_start_output.102* Entry: global_state = DSTATE_PRESCAN only if previously suspended.103* Exit: If done, returns TRUE and sets global_state for proper output mode.104* If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.105*/106107LOCAL(boolean)108output_pass_setup(j_decompress_ptr cinfo)109{110if (cinfo->global_state != DSTATE_PRESCAN) {111/* First call: do pass setup */112(*cinfo->master->prepare_for_output_pass) (cinfo);113cinfo->output_scanline = 0;114cinfo->global_state = DSTATE_PRESCAN;115}116/* Loop over any required dummy passes */117while (cinfo->master->is_dummy_pass) {118#ifdef QUANT_2PASS_SUPPORTED119/* Crank through the dummy pass */120while (cinfo->output_scanline < cinfo->output_height) {121JDIMENSION last_scanline;122/* Call progress monitor hook if present */123if (cinfo->progress != NULL) {124cinfo->progress->pass_counter = (long)cinfo->output_scanline;125cinfo->progress->pass_limit = (long)cinfo->output_height;126(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);127}128/* Process some data */129last_scanline = cinfo->output_scanline;130if (cinfo->data_precision <= 8) {131if (cinfo->main->process_data == NULL)132ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);133(*cinfo->main->process_data) (cinfo, (JSAMPARRAY)NULL,134&cinfo->output_scanline, (JDIMENSION)0);135} else if (cinfo->data_precision <= 12) {136if (cinfo->main->process_data_12 == NULL)137ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);138(*cinfo->main->process_data_12) (cinfo, (J12SAMPARRAY)NULL,139&cinfo->output_scanline,140(JDIMENSION)0);141} else {142#ifdef D_LOSSLESS_SUPPORTED143if (cinfo->main->process_data_16 == NULL)144ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);145(*cinfo->main->process_data_16) (cinfo, (J16SAMPARRAY)NULL,146&cinfo->output_scanline,147(JDIMENSION)0);148#else149ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);150#endif151}152if (cinfo->output_scanline == last_scanline)153return FALSE; /* No progress made, must suspend */154}155/* Finish up dummy pass, and set up for another one */156(*cinfo->master->finish_output_pass) (cinfo);157(*cinfo->master->prepare_for_output_pass) (cinfo);158cinfo->output_scanline = 0;159#else160ERREXIT(cinfo, JERR_NOT_COMPILED);161#endif /* QUANT_2PASS_SUPPORTED */162}163/* Ready for application to drive output pass through164* _jpeg_read_scanlines or _jpeg_read_raw_data.165*/166cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;167return TRUE;168}169170#endif /* BITS_IN_JSAMPLE == 8 */171172173#if BITS_IN_JSAMPLE != 16174175/*176* Enable partial scanline decompression177*178* Must be called after jpeg_start_decompress() and before any calls to179* _jpeg_read_scanlines() or _jpeg_skip_scanlines().180*181* Refer to libjpeg.txt for more information.182*/183184GLOBAL(void)185_jpeg_crop_scanline(j_decompress_ptr cinfo, JDIMENSION *xoffset,186JDIMENSION *width)187{188int ci, align, orig_downsampled_width;189JDIMENSION input_xoffset;190boolean reinit_upsampler = FALSE;191jpeg_component_info *compptr;192#ifdef UPSAMPLE_MERGING_SUPPORTED193my_master_ptr master = (my_master_ptr)cinfo->master;194#endif195196if (cinfo->data_precision != BITS_IN_JSAMPLE)197ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);198199if (cinfo->master->lossless)200ERREXIT(cinfo, JERR_NOTIMPL);201202if ((cinfo->global_state != DSTATE_SCANNING &&203cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)204ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);205206if (!xoffset || !width)207ERREXIT(cinfo, JERR_BAD_CROP_SPEC);208209/* xoffset and width must fall within the output image dimensions. */210if (*width == 0 ||211(unsigned long long)(*xoffset) + *width > cinfo->output_width)212ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);213214/* No need to do anything if the caller wants the entire width. */215if (*width == cinfo->output_width)216return;217218/* Ensuring the proper alignment of xoffset is tricky. At minimum, it219* must align with an MCU boundary, because:220*221* (1) The IDCT is performed in blocks, and it is not feasible to modify222* the algorithm so that it can transform partial blocks.223* (2) Because of the SIMD extensions, any input buffer passed to the224* upsampling and color conversion routines must be aligned to the225* SIMD word size (for instance, 128-bit in the case of SSE2.) The226* easiest way to accomplish this without copying data is to ensure227* that upsampling and color conversion begin at the start of the228* first MCU column that will be inverse transformed.229*230* In practice, we actually impose a stricter alignment requirement. We231* require that xoffset be a multiple of the maximum MCU column width of all232* of the components (the "iMCU column width.") This is to simplify the233* single-pass decompression case, allowing us to use the same MCU column234* width for all of the components.235*/236if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)237align = cinfo->_min_DCT_scaled_size;238else239align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;240241/* Adjust xoffset to the nearest iMCU boundary <= the requested value */242input_xoffset = *xoffset;243*xoffset = (input_xoffset / align) * align;244245/* Adjust the width so that the right edge of the output image is as246* requested (only the left edge is altered.) It is important that calling247* programs check this value after this function returns, so that they can248* allocate an output buffer with the appropriate size.249*/250*width = *width + input_xoffset - *xoffset;251cinfo->output_width = *width;252#ifdef UPSAMPLE_MERGING_SUPPORTED253if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {254my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;255upsample->out_row_width =256cinfo->output_width * cinfo->out_color_components;257}258#endif259260/* Set the first and last iMCU columns that we must decompress. These values261* will be used in single-scan decompressions.262*/263cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;264cinfo->master->last_iMCU_col =265(JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),266(long)align) - 1;267268for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;269ci++, compptr++) {270int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?2711 : compptr->h_samp_factor;272273/* Set downsampled_width to the new output width. */274orig_downsampled_width = compptr->downsampled_width;275compptr->downsampled_width =276(JDIMENSION)jdiv_round_up((long)cinfo->output_width *277(long)(compptr->h_samp_factor *278compptr->_DCT_scaled_size),279(long)(cinfo->max_h_samp_factor *280cinfo->_min_DCT_scaled_size));281if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)282reinit_upsampler = TRUE;283284/* Set the first and last iMCU columns that we must decompress. These285* values will be used in multi-scan decompressions.286*/287cinfo->master->first_MCU_col[ci] =288(JDIMENSION)(long)(*xoffset * hsf) / (long)align;289cinfo->master->last_MCU_col[ci] =290(JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),291(long)align) - 1;292}293294if (reinit_upsampler) {295cinfo->master->jinit_upsampler_no_alloc = TRUE;296_jinit_upsampler(cinfo);297cinfo->master->jinit_upsampler_no_alloc = FALSE;298}299}300301#endif /* BITS_IN_JSAMPLE != 16 */302303304/*305* Read some scanlines of data from the JPEG decompressor.306*307* The return value will be the number of lines actually read.308* This may be less than the number requested in several cases,309* including bottom of image, data source suspension, and operating310* modes that emit multiple scanlines at a time.311*312* Note: we warn about excess calls to _jpeg_read_scanlines() since313* this likely signals an application programmer error. However,314* an oversize buffer (max_lines > scanlines remaining) is not an error.315*/316317GLOBAL(JDIMENSION)318_jpeg_read_scanlines(j_decompress_ptr cinfo, _JSAMPARRAY scanlines,319JDIMENSION max_lines)320{321#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)322JDIMENSION row_ctr;323324#ifdef D_LOSSLESS_SUPPORTED325if (cinfo->master->lossless) {326#if BITS_IN_JSAMPLE == 8327if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)328#else329if (cinfo->data_precision > BITS_IN_JSAMPLE ||330cinfo->data_precision < BITS_IN_JSAMPLE - 3)331#endif332ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);333} else334#endif335{336if (cinfo->data_precision != BITS_IN_JSAMPLE)337ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);338}339340if (cinfo->global_state != DSTATE_SCANNING)341ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);342if (cinfo->output_scanline >= cinfo->output_height) {343WARNMS(cinfo, JWRN_TOO_MUCH_DATA);344return 0;345}346347/* Call progress monitor hook if present */348if (cinfo->progress != NULL) {349cinfo->progress->pass_counter = (long)cinfo->output_scanline;350cinfo->progress->pass_limit = (long)cinfo->output_height;351(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);352}353354/* Process some data */355row_ctr = 0;356if (cinfo->main->_process_data == NULL)357ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);358(*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);359cinfo->output_scanline += row_ctr;360return row_ctr;361#else362ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);363return 0;364#endif365}366367368#if BITS_IN_JSAMPLE != 16369370/* Dummy color convert function used by _jpeg_skip_scanlines() */371LOCAL(void)372noop_convert(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,373JDIMENSION input_row, _JSAMPARRAY output_buf, int num_rows)374{375}376377378/* Dummy quantize function used by _jpeg_skip_scanlines() */379LOCAL(void)380noop_quantize(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,381_JSAMPARRAY output_buf, int num_rows)382{383}384385386/*387* In some cases, it is best to call _jpeg_read_scanlines() and discard the388* output, rather than skipping the scanlines, because this allows us to389* maintain the internal state of the context-based upsampler. In these cases,390* we set up and tear down a dummy color converter in order to avoid valgrind391* errors and to achieve the best possible performance.392*/393394LOCAL(void)395read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)396{397JDIMENSION n;398#ifdef UPSAMPLE_MERGING_SUPPORTED399my_master_ptr master = (my_master_ptr)cinfo->master;400#endif401_JSAMPLE dummy_sample[1] = { 0 };402_JSAMPROW dummy_row = dummy_sample;403_JSAMPARRAY scanlines = NULL;404void (*color_convert) (j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,405JDIMENSION input_row, _JSAMPARRAY output_buf,406int num_rows) = NULL;407void (*color_quantize) (j_decompress_ptr cinfo, _JSAMPARRAY input_buf,408_JSAMPARRAY output_buf, int num_rows) = NULL;409410if (!master->using_merged_upsample && cinfo->cconvert &&411cinfo->cconvert->_color_convert) {412color_convert = cinfo->cconvert->_color_convert;413cinfo->cconvert->_color_convert = noop_convert;414/* This just prevents UBSan from complaining about adding 0 to a NULL415* pointer. The pointer isn't actually used.416*/417scanlines = &dummy_row;418}419420if (cinfo->quantize_colors && cinfo->cquantize &&421cinfo->cquantize->_color_quantize) {422color_quantize = cinfo->cquantize->_color_quantize;423cinfo->cquantize->_color_quantize = noop_quantize;424}425426#ifdef UPSAMPLE_MERGING_SUPPORTED427if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {428my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;429scanlines = &upsample->spare_row;430}431#endif432433for (n = 0; n < num_lines; n++)434_jpeg_read_scanlines(cinfo, scanlines, 1);435436if (color_convert)437cinfo->cconvert->_color_convert = color_convert;438439if (color_quantize)440cinfo->cquantize->_color_quantize = color_quantize;441}442443444/*445* Called by _jpeg_skip_scanlines(). This partially skips a decompress block446* by incrementing the rowgroup counter.447*/448449LOCAL(void)450increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)451{452JDIMENSION rows_left;453my_main_ptr main_ptr = (my_main_ptr)cinfo->main;454my_master_ptr master = (my_master_ptr)cinfo->master;455456if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {457read_and_discard_scanlines(cinfo, rows);458return;459}460461/* Increment the counter to the next row group after the skipped rows. */462main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;463464/* Partially skipping a row group would involve modifying the internal state465* of the upsampler, so read the remaining rows into a dummy buffer instead.466*/467rows_left = rows % cinfo->max_v_samp_factor;468cinfo->output_scanline += rows - rows_left;469470read_and_discard_scanlines(cinfo, rows_left);471}472473/*474* Skips some scanlines of data from the JPEG decompressor.475*476* The return value will be the number of lines actually skipped. If skipping477* num_lines would move beyond the end of the image, then the actual number of478* lines remaining in the image is returned. Otherwise, the return value will479* be equal to num_lines.480*481* Refer to libjpeg.txt for more information.482*/483484GLOBAL(JDIMENSION)485_jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)486{487my_main_ptr main_ptr = (my_main_ptr)cinfo->main;488my_coef_ptr coef = (my_coef_ptr)cinfo->coef;489my_master_ptr master = (my_master_ptr)cinfo->master;490my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;491JDIMENSION i, x;492int y;493JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;494JDIMENSION lines_to_skip, lines_to_read;495496if (cinfo->data_precision != BITS_IN_JSAMPLE)497ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);498499if (cinfo->master->lossless)500ERREXIT(cinfo, JERR_NOTIMPL);501502/* Two-pass color quantization is not supported. */503if (cinfo->quantize_colors && cinfo->two_pass_quantize)504ERREXIT(cinfo, JERR_NOTIMPL);505506if (cinfo->global_state != DSTATE_SCANNING)507ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);508509/* Do not skip past the bottom of the image. */510if ((unsigned long long)cinfo->output_scanline + num_lines >=511cinfo->output_height) {512num_lines = cinfo->output_height - cinfo->output_scanline;513cinfo->output_scanline = cinfo->output_height;514(*cinfo->inputctl->finish_input_pass) (cinfo);515cinfo->inputctl->eoi_reached = TRUE;516return num_lines;517}518519if (num_lines == 0)520return 0;521522lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;523lines_left_in_iMCU_row =524(lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %525lines_per_iMCU_row;526lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;527528/* Skip the lines remaining in the current iMCU row. When upsampling529* requires context rows, we need the previous and next rows in order to read530* the current row. This adds some complexity.531*/532if (cinfo->upsample->need_context_rows) {533/* If the skipped lines would not move us past the current iMCU row, we534* read the lines and ignore them. There might be a faster way of doing535* this, but we are facing increasing complexity for diminishing returns.536* The increasing complexity would be a by-product of meddling with the537* state machine used to skip context rows. Near the end of an iMCU row,538* the next iMCU row may have already been entropy-decoded. In this unique539* case, we will read the next iMCU row if we cannot skip past it as well.540*/541if ((num_lines < lines_left_in_iMCU_row + 1) ||542(lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&543lines_after_iMCU_row < lines_per_iMCU_row + 1)) {544read_and_discard_scanlines(cinfo, num_lines);545return num_lines;546}547548/* If the next iMCU row has already been entropy-decoded, make sure that549* we do not skip too far.550*/551if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {552cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;553lines_after_iMCU_row -= lines_per_iMCU_row;554} else {555cinfo->output_scanline += lines_left_in_iMCU_row;556}557558/* If we have just completed the first block, adjust the buffer pointers */559if (main_ptr->iMCU_row_ctr == 0 ||560(main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))561set_wraparound_pointers(cinfo);562main_ptr->buffer_full = FALSE;563main_ptr->rowgroup_ctr = 0;564main_ptr->context_state = CTX_PREPARE_FOR_IMCU;565if (!master->using_merged_upsample) {566upsample->next_row_out = cinfo->max_v_samp_factor;567upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;568}569}570571/* Skipping is much simpler when context rows are not required. */572else {573if (num_lines < lines_left_in_iMCU_row) {574increment_simple_rowgroup_ctr(cinfo, num_lines);575return num_lines;576} else {577cinfo->output_scanline += lines_left_in_iMCU_row;578main_ptr->buffer_full = FALSE;579main_ptr->rowgroup_ctr = 0;580if (!master->using_merged_upsample) {581upsample->next_row_out = cinfo->max_v_samp_factor;582upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;583}584}585}586587/* Calculate how many full iMCU rows we can skip. */588if (cinfo->upsample->need_context_rows)589lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *590lines_per_iMCU_row;591else592lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *593lines_per_iMCU_row;594/* Calculate the number of lines that remain to be skipped after skipping all595* of the full iMCU rows that we can. We will not read these lines unless we596* have to.597*/598lines_to_read = lines_after_iMCU_row - lines_to_skip;599600/* For images requiring multiple scans (progressive, non-interleaved, etc.),601* all of the entropy decoding occurs in jpeg_start_decompress(), assuming602* that the input data source is non-suspending. This makes skipping easy.603*/604if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {605if (cinfo->upsample->need_context_rows) {606cinfo->output_scanline += lines_to_skip;607cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;608main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;609/* It is complex to properly move to the middle of a context block, so610* read the remaining lines instead of skipping them.611*/612read_and_discard_scanlines(cinfo, lines_to_read);613} else {614cinfo->output_scanline += lines_to_skip;615cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;616increment_simple_rowgroup_ctr(cinfo, lines_to_read);617}618if (!master->using_merged_upsample)619upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;620return num_lines;621}622623/* Skip the iMCU rows that we can safely skip. */624for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {625for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {626for (x = 0; x < cinfo->MCUs_per_row; x++) {627/* Calling decode_mcu() with a NULL pointer causes it to discard the628* decoded coefficients. This is ~5% faster for large subsets, but629* it's tough to tell a difference for smaller images.630*/631if (!cinfo->entropy->insufficient_data)632cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;633(*cinfo->entropy->decode_mcu) (cinfo, NULL);634}635}636cinfo->input_iMCU_row++;637cinfo->output_iMCU_row++;638if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)639start_iMCU_row(cinfo);640else641(*cinfo->inputctl->finish_input_pass) (cinfo);642}643cinfo->output_scanline += lines_to_skip;644645if (cinfo->upsample->need_context_rows) {646/* Context-based upsampling keeps track of iMCU rows. */647main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;648649/* It is complex to properly move to the middle of a context block, so650* read the remaining lines instead of skipping them.651*/652read_and_discard_scanlines(cinfo, lines_to_read);653} else {654increment_simple_rowgroup_ctr(cinfo, lines_to_read);655}656657/* Since skipping lines involves skipping the upsampling step, the value of658* "rows_to_go" will become invalid unless we set it here. NOTE: This is a659* bit odd, since "rows_to_go" seems to be redundantly keeping track of660* output_scanline.661*/662if (!master->using_merged_upsample)663upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;664665/* Always skip the requested number of lines. */666return num_lines;667}668669/*670* Alternate entry point to read raw data.671* Processes exactly one iMCU row per call, unless suspended.672*/673674GLOBAL(JDIMENSION)675_jpeg_read_raw_data(j_decompress_ptr cinfo, _JSAMPIMAGE data,676JDIMENSION max_lines)677{678JDIMENSION lines_per_iMCU_row;679680if (cinfo->data_precision != BITS_IN_JSAMPLE)681ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);682683if (cinfo->master->lossless)684ERREXIT(cinfo, JERR_NOTIMPL);685686if (cinfo->global_state != DSTATE_RAW_OK)687ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);688if (cinfo->output_scanline >= cinfo->output_height) {689WARNMS(cinfo, JWRN_TOO_MUCH_DATA);690return 0;691}692693/* Call progress monitor hook if present */694if (cinfo->progress != NULL) {695cinfo->progress->pass_counter = (long)cinfo->output_scanline;696cinfo->progress->pass_limit = (long)cinfo->output_height;697(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);698}699700/* Verify that at least one iMCU row can be returned. */701lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;702if (max_lines < lines_per_iMCU_row)703ERREXIT(cinfo, JERR_BUFFER_SIZE);704705/* Decompress directly into user's buffer. */706if (cinfo->coef->_decompress_data == NULL)707ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);708if (!(*cinfo->coef->_decompress_data) (cinfo, data))709return 0; /* suspension forced, can do nothing more */710711/* OK, we processed one iMCU row. */712cinfo->output_scanline += lines_per_iMCU_row;713return lines_per_iMCU_row;714}715716#endif /* BITS_IN_JSAMPLE != 16 */717718719#if BITS_IN_JSAMPLE == 8720721/* Additional entry points for buffered-image mode. */722723#ifdef D_MULTISCAN_FILES_SUPPORTED724725/*726* Initialize for an output pass in buffered-image mode.727*/728729GLOBAL(boolean)730jpeg_start_output(j_decompress_ptr cinfo, int scan_number)731{732if (cinfo->global_state != DSTATE_BUFIMAGE &&733cinfo->global_state != DSTATE_PRESCAN)734ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);735/* Limit scan number to valid range */736if (scan_number <= 0)737scan_number = 1;738if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)739scan_number = cinfo->input_scan_number;740cinfo->output_scan_number = scan_number;741/* Perform any dummy output passes, and set up for the real pass */742return output_pass_setup(cinfo);743}744745746/*747* Finish up after an output pass in buffered-image mode.748*749* Returns FALSE if suspended. The return value need be inspected only if750* a suspending data source is used.751*/752753GLOBAL(boolean)754jpeg_finish_output(j_decompress_ptr cinfo)755{756if ((cinfo->global_state == DSTATE_SCANNING ||757cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {758/* Terminate this pass. */759/* We do not require the whole pass to have been completed. */760(*cinfo->master->finish_output_pass) (cinfo);761cinfo->global_state = DSTATE_BUFPOST;762} else if (cinfo->global_state != DSTATE_BUFPOST) {763/* BUFPOST = repeat call after a suspension, anything else is error */764ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);765}766/* Read markers looking for SOS or EOI */767while (cinfo->input_scan_number <= cinfo->output_scan_number &&768!cinfo->inputctl->eoi_reached) {769if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)770return FALSE; /* Suspend, come back later */771}772cinfo->global_state = DSTATE_BUFIMAGE;773return TRUE;774}775776#endif /* D_MULTISCAN_FILES_SUPPORTED */777778#endif /* BITS_IN_JSAMPLE == 8 */779780781