Path: blob/master/thirdparty/libjpeg-turbo/src/jdapistd.c
9904 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-2024, 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)131(*cinfo->main->process_data) (cinfo, (JSAMPARRAY)NULL,132&cinfo->output_scanline, (JDIMENSION)0);133else if (cinfo->data_precision <= 12)134(*cinfo->main->process_data_12) (cinfo, (J12SAMPARRAY)NULL,135&cinfo->output_scanline,136(JDIMENSION)0);137#ifdef D_LOSSLESS_SUPPORTED138else139(*cinfo->main->process_data_16) (cinfo, (J16SAMPARRAY)NULL,140&cinfo->output_scanline,141(JDIMENSION)0);142#endif143if (cinfo->output_scanline == last_scanline)144return FALSE; /* No progress made, must suspend */145}146/* Finish up dummy pass, and set up for another one */147(*cinfo->master->finish_output_pass) (cinfo);148(*cinfo->master->prepare_for_output_pass) (cinfo);149cinfo->output_scanline = 0;150#else151ERREXIT(cinfo, JERR_NOT_COMPILED);152#endif /* QUANT_2PASS_SUPPORTED */153}154/* Ready for application to drive output pass through155* _jpeg_read_scanlines or _jpeg_read_raw_data.156*/157cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;158return TRUE;159}160161#endif /* BITS_IN_JSAMPLE == 8 */162163164#if BITS_IN_JSAMPLE != 16165166/*167* Enable partial scanline decompression168*169* Must be called after jpeg_start_decompress() and before any calls to170* _jpeg_read_scanlines() or _jpeg_skip_scanlines().171*172* Refer to libjpeg.txt for more information.173*/174175GLOBAL(void)176_jpeg_crop_scanline(j_decompress_ptr cinfo, JDIMENSION *xoffset,177JDIMENSION *width)178{179int ci, align, orig_downsampled_width;180JDIMENSION input_xoffset;181boolean reinit_upsampler = FALSE;182jpeg_component_info *compptr;183#ifdef UPSAMPLE_MERGING_SUPPORTED184my_master_ptr master = (my_master_ptr)cinfo->master;185#endif186187if (cinfo->data_precision != BITS_IN_JSAMPLE)188ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);189190if (cinfo->master->lossless)191ERREXIT(cinfo, JERR_NOTIMPL);192193if ((cinfo->global_state != DSTATE_SCANNING &&194cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)195ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);196197if (!xoffset || !width)198ERREXIT(cinfo, JERR_BAD_CROP_SPEC);199200/* xoffset and width must fall within the output image dimensions. */201if (*width == 0 ||202(unsigned long long)(*xoffset) + *width > cinfo->output_width)203ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);204205/* No need to do anything if the caller wants the entire width. */206if (*width == cinfo->output_width)207return;208209/* Ensuring the proper alignment of xoffset is tricky. At minimum, it210* must align with an MCU boundary, because:211*212* (1) The IDCT is performed in blocks, and it is not feasible to modify213* the algorithm so that it can transform partial blocks.214* (2) Because of the SIMD extensions, any input buffer passed to the215* upsampling and color conversion routines must be aligned to the216* SIMD word size (for instance, 128-bit in the case of SSE2.) The217* easiest way to accomplish this without copying data is to ensure218* that upsampling and color conversion begin at the start of the219* first MCU column that will be inverse transformed.220*221* In practice, we actually impose a stricter alignment requirement. We222* require that xoffset be a multiple of the maximum MCU column width of all223* of the components (the "iMCU column width.") This is to simplify the224* single-pass decompression case, allowing us to use the same MCU column225* width for all of the components.226*/227if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)228align = cinfo->_min_DCT_scaled_size;229else230align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;231232/* Adjust xoffset to the nearest iMCU boundary <= the requested value */233input_xoffset = *xoffset;234*xoffset = (input_xoffset / align) * align;235236/* Adjust the width so that the right edge of the output image is as237* requested (only the left edge is altered.) It is important that calling238* programs check this value after this function returns, so that they can239* allocate an output buffer with the appropriate size.240*/241*width = *width + input_xoffset - *xoffset;242cinfo->output_width = *width;243#ifdef UPSAMPLE_MERGING_SUPPORTED244if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {245my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;246upsample->out_row_width =247cinfo->output_width * cinfo->out_color_components;248}249#endif250251/* Set the first and last iMCU columns that we must decompress. These values252* will be used in single-scan decompressions.253*/254cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;255cinfo->master->last_iMCU_col =256(JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),257(long)align) - 1;258259for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;260ci++, compptr++) {261int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?2621 : compptr->h_samp_factor;263264/* Set downsampled_width to the new output width. */265orig_downsampled_width = compptr->downsampled_width;266compptr->downsampled_width =267(JDIMENSION)jdiv_round_up((long)cinfo->output_width *268(long)(compptr->h_samp_factor *269compptr->_DCT_scaled_size),270(long)(cinfo->max_h_samp_factor *271cinfo->_min_DCT_scaled_size));272if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)273reinit_upsampler = TRUE;274275/* Set the first and last iMCU columns that we must decompress. These276* values will be used in multi-scan decompressions.277*/278cinfo->master->first_MCU_col[ci] =279(JDIMENSION)(long)(*xoffset * hsf) / (long)align;280cinfo->master->last_MCU_col[ci] =281(JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),282(long)align) - 1;283}284285if (reinit_upsampler) {286cinfo->master->jinit_upsampler_no_alloc = TRUE;287_jinit_upsampler(cinfo);288cinfo->master->jinit_upsampler_no_alloc = FALSE;289}290}291292#endif /* BITS_IN_JSAMPLE != 16 */293294295/*296* Read some scanlines of data from the JPEG decompressor.297*298* The return value will be the number of lines actually read.299* This may be less than the number requested in several cases,300* including bottom of image, data source suspension, and operating301* modes that emit multiple scanlines at a time.302*303* Note: we warn about excess calls to _jpeg_read_scanlines() since304* this likely signals an application programmer error. However,305* an oversize buffer (max_lines > scanlines remaining) is not an error.306*/307308GLOBAL(JDIMENSION)309_jpeg_read_scanlines(j_decompress_ptr cinfo, _JSAMPARRAY scanlines,310JDIMENSION max_lines)311{312#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)313JDIMENSION row_ctr;314315#ifdef D_LOSSLESS_SUPPORTED316if (cinfo->master->lossless) {317#if BITS_IN_JSAMPLE == 8318if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)319#else320if (cinfo->data_precision > BITS_IN_JSAMPLE ||321cinfo->data_precision < BITS_IN_JSAMPLE - 3)322#endif323ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);324} else325#endif326{327if (cinfo->data_precision != BITS_IN_JSAMPLE)328ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);329}330331if (cinfo->global_state != DSTATE_SCANNING)332ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);333if (cinfo->output_scanline >= cinfo->output_height) {334WARNMS(cinfo, JWRN_TOO_MUCH_DATA);335return 0;336}337338/* Call progress monitor hook if present */339if (cinfo->progress != NULL) {340cinfo->progress->pass_counter = (long)cinfo->output_scanline;341cinfo->progress->pass_limit = (long)cinfo->output_height;342(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);343}344345/* Process some data */346row_ctr = 0;347(*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);348cinfo->output_scanline += row_ctr;349return row_ctr;350#else351ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);352return 0;353#endif354}355356357#if BITS_IN_JSAMPLE != 16358359/* Dummy color convert function used by _jpeg_skip_scanlines() */360LOCAL(void)361noop_convert(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,362JDIMENSION input_row, _JSAMPARRAY output_buf, int num_rows)363{364}365366367/* Dummy quantize function used by _jpeg_skip_scanlines() */368LOCAL(void)369noop_quantize(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,370_JSAMPARRAY output_buf, int num_rows)371{372}373374375/*376* In some cases, it is best to call _jpeg_read_scanlines() and discard the377* output, rather than skipping the scanlines, because this allows us to378* maintain the internal state of the context-based upsampler. In these cases,379* we set up and tear down a dummy color converter in order to avoid valgrind380* errors and to achieve the best possible performance.381*/382383LOCAL(void)384read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)385{386JDIMENSION n;387#ifdef UPSAMPLE_MERGING_SUPPORTED388my_master_ptr master = (my_master_ptr)cinfo->master;389#endif390_JSAMPLE dummy_sample[1] = { 0 };391_JSAMPROW dummy_row = dummy_sample;392_JSAMPARRAY scanlines = NULL;393void (*color_convert) (j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,394JDIMENSION input_row, _JSAMPARRAY output_buf,395int num_rows) = NULL;396void (*color_quantize) (j_decompress_ptr cinfo, _JSAMPARRAY input_buf,397_JSAMPARRAY output_buf, int num_rows) = NULL;398399if (cinfo->cconvert && cinfo->cconvert->_color_convert) {400color_convert = cinfo->cconvert->_color_convert;401cinfo->cconvert->_color_convert = noop_convert;402/* This just prevents UBSan from complaining about adding 0 to a NULL403* pointer. The pointer isn't actually used.404*/405scanlines = &dummy_row;406}407408if (cinfo->cquantize && cinfo->cquantize->_color_quantize) {409color_quantize = cinfo->cquantize->_color_quantize;410cinfo->cquantize->_color_quantize = noop_quantize;411}412413#ifdef UPSAMPLE_MERGING_SUPPORTED414if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {415my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;416scanlines = &upsample->spare_row;417}418#endif419420for (n = 0; n < num_lines; n++)421_jpeg_read_scanlines(cinfo, scanlines, 1);422423if (color_convert)424cinfo->cconvert->_color_convert = color_convert;425426if (color_quantize)427cinfo->cquantize->_color_quantize = color_quantize;428}429430431/*432* Called by _jpeg_skip_scanlines(). This partially skips a decompress block433* by incrementing the rowgroup counter.434*/435436LOCAL(void)437increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)438{439JDIMENSION rows_left;440my_main_ptr main_ptr = (my_main_ptr)cinfo->main;441my_master_ptr master = (my_master_ptr)cinfo->master;442443if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {444read_and_discard_scanlines(cinfo, rows);445return;446}447448/* Increment the counter to the next row group after the skipped rows. */449main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;450451/* Partially skipping a row group would involve modifying the internal state452* of the upsampler, so read the remaining rows into a dummy buffer instead.453*/454rows_left = rows % cinfo->max_v_samp_factor;455cinfo->output_scanline += rows - rows_left;456457read_and_discard_scanlines(cinfo, rows_left);458}459460/*461* Skips some scanlines of data from the JPEG decompressor.462*463* The return value will be the number of lines actually skipped. If skipping464* num_lines would move beyond the end of the image, then the actual number of465* lines remaining in the image is returned. Otherwise, the return value will466* be equal to num_lines.467*468* Refer to libjpeg.txt for more information.469*/470471GLOBAL(JDIMENSION)472_jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)473{474my_main_ptr main_ptr = (my_main_ptr)cinfo->main;475my_coef_ptr coef = (my_coef_ptr)cinfo->coef;476my_master_ptr master = (my_master_ptr)cinfo->master;477my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;478JDIMENSION i, x;479int y;480JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;481JDIMENSION lines_to_skip, lines_to_read;482483if (cinfo->data_precision != BITS_IN_JSAMPLE)484ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);485486if (cinfo->master->lossless)487ERREXIT(cinfo, JERR_NOTIMPL);488489/* Two-pass color quantization is not supported. */490if (cinfo->quantize_colors && cinfo->two_pass_quantize)491ERREXIT(cinfo, JERR_NOTIMPL);492493if (cinfo->global_state != DSTATE_SCANNING)494ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);495496/* Do not skip past the bottom of the image. */497if ((unsigned long long)cinfo->output_scanline + num_lines >=498cinfo->output_height) {499num_lines = cinfo->output_height - cinfo->output_scanline;500cinfo->output_scanline = cinfo->output_height;501(*cinfo->inputctl->finish_input_pass) (cinfo);502cinfo->inputctl->eoi_reached = TRUE;503return num_lines;504}505506if (num_lines == 0)507return 0;508509lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;510lines_left_in_iMCU_row =511(lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %512lines_per_iMCU_row;513lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;514515/* Skip the lines remaining in the current iMCU row. When upsampling516* requires context rows, we need the previous and next rows in order to read517* the current row. This adds some complexity.518*/519if (cinfo->upsample->need_context_rows) {520/* If the skipped lines would not move us past the current iMCU row, we521* read the lines and ignore them. There might be a faster way of doing522* this, but we are facing increasing complexity for diminishing returns.523* The increasing complexity would be a by-product of meddling with the524* state machine used to skip context rows. Near the end of an iMCU row,525* the next iMCU row may have already been entropy-decoded. In this unique526* case, we will read the next iMCU row if we cannot skip past it as well.527*/528if ((num_lines < lines_left_in_iMCU_row + 1) ||529(lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&530lines_after_iMCU_row < lines_per_iMCU_row + 1)) {531read_and_discard_scanlines(cinfo, num_lines);532return num_lines;533}534535/* If the next iMCU row has already been entropy-decoded, make sure that536* we do not skip too far.537*/538if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {539cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;540lines_after_iMCU_row -= lines_per_iMCU_row;541} else {542cinfo->output_scanline += lines_left_in_iMCU_row;543}544545/* If we have just completed the first block, adjust the buffer pointers */546if (main_ptr->iMCU_row_ctr == 0 ||547(main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))548set_wraparound_pointers(cinfo);549main_ptr->buffer_full = FALSE;550main_ptr->rowgroup_ctr = 0;551main_ptr->context_state = CTX_PREPARE_FOR_IMCU;552if (!master->using_merged_upsample) {553upsample->next_row_out = cinfo->max_v_samp_factor;554upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;555}556}557558/* Skipping is much simpler when context rows are not required. */559else {560if (num_lines < lines_left_in_iMCU_row) {561increment_simple_rowgroup_ctr(cinfo, num_lines);562return num_lines;563} else {564cinfo->output_scanline += lines_left_in_iMCU_row;565main_ptr->buffer_full = FALSE;566main_ptr->rowgroup_ctr = 0;567if (!master->using_merged_upsample) {568upsample->next_row_out = cinfo->max_v_samp_factor;569upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;570}571}572}573574/* Calculate how many full iMCU rows we can skip. */575if (cinfo->upsample->need_context_rows)576lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *577lines_per_iMCU_row;578else579lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *580lines_per_iMCU_row;581/* Calculate the number of lines that remain to be skipped after skipping all582* of the full iMCU rows that we can. We will not read these lines unless we583* have to.584*/585lines_to_read = lines_after_iMCU_row - lines_to_skip;586587/* For images requiring multiple scans (progressive, non-interleaved, etc.),588* all of the entropy decoding occurs in jpeg_start_decompress(), assuming589* that the input data source is non-suspending. This makes skipping easy.590*/591if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {592if (cinfo->upsample->need_context_rows) {593cinfo->output_scanline += lines_to_skip;594cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;595main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;596/* It is complex to properly move to the middle of a context block, so597* read the remaining lines instead of skipping them.598*/599read_and_discard_scanlines(cinfo, lines_to_read);600} else {601cinfo->output_scanline += lines_to_skip;602cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;603increment_simple_rowgroup_ctr(cinfo, lines_to_read);604}605if (!master->using_merged_upsample)606upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;607return num_lines;608}609610/* Skip the iMCU rows that we can safely skip. */611for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {612for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {613for (x = 0; x < cinfo->MCUs_per_row; x++) {614/* Calling decode_mcu() with a NULL pointer causes it to discard the615* decoded coefficients. This is ~5% faster for large subsets, but616* it's tough to tell a difference for smaller images.617*/618if (!cinfo->entropy->insufficient_data)619cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;620(*cinfo->entropy->decode_mcu) (cinfo, NULL);621}622}623cinfo->input_iMCU_row++;624cinfo->output_iMCU_row++;625if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)626start_iMCU_row(cinfo);627else628(*cinfo->inputctl->finish_input_pass) (cinfo);629}630cinfo->output_scanline += lines_to_skip;631632if (cinfo->upsample->need_context_rows) {633/* Context-based upsampling keeps track of iMCU rows. */634main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;635636/* It is complex to properly move to the middle of a context block, so637* read the remaining lines instead of skipping them.638*/639read_and_discard_scanlines(cinfo, lines_to_read);640} else {641increment_simple_rowgroup_ctr(cinfo, lines_to_read);642}643644/* Since skipping lines involves skipping the upsampling step, the value of645* "rows_to_go" will become invalid unless we set it here. NOTE: This is a646* bit odd, since "rows_to_go" seems to be redundantly keeping track of647* output_scanline.648*/649if (!master->using_merged_upsample)650upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;651652/* Always skip the requested number of lines. */653return num_lines;654}655656/*657* Alternate entry point to read raw data.658* Processes exactly one iMCU row per call, unless suspended.659*/660661GLOBAL(JDIMENSION)662_jpeg_read_raw_data(j_decompress_ptr cinfo, _JSAMPIMAGE data,663JDIMENSION max_lines)664{665JDIMENSION lines_per_iMCU_row;666667if (cinfo->data_precision != BITS_IN_JSAMPLE)668ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);669670if (cinfo->master->lossless)671ERREXIT(cinfo, JERR_NOTIMPL);672673if (cinfo->global_state != DSTATE_RAW_OK)674ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);675if (cinfo->output_scanline >= cinfo->output_height) {676WARNMS(cinfo, JWRN_TOO_MUCH_DATA);677return 0;678}679680/* Call progress monitor hook if present */681if (cinfo->progress != NULL) {682cinfo->progress->pass_counter = (long)cinfo->output_scanline;683cinfo->progress->pass_limit = (long)cinfo->output_height;684(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);685}686687/* Verify that at least one iMCU row can be returned. */688lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;689if (max_lines < lines_per_iMCU_row)690ERREXIT(cinfo, JERR_BUFFER_SIZE);691692/* Decompress directly into user's buffer. */693if (!(*cinfo->coef->_decompress_data) (cinfo, data))694return 0; /* suspension forced, can do nothing more */695696/* OK, we processed one iMCU row. */697cinfo->output_scanline += lines_per_iMCU_row;698return lines_per_iMCU_row;699}700701#endif /* BITS_IN_JSAMPLE != 16 */702703704#if BITS_IN_JSAMPLE == 8705706/* Additional entry points for buffered-image mode. */707708#ifdef D_MULTISCAN_FILES_SUPPORTED709710/*711* Initialize for an output pass in buffered-image mode.712*/713714GLOBAL(boolean)715jpeg_start_output(j_decompress_ptr cinfo, int scan_number)716{717if (cinfo->global_state != DSTATE_BUFIMAGE &&718cinfo->global_state != DSTATE_PRESCAN)719ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);720/* Limit scan number to valid range */721if (scan_number <= 0)722scan_number = 1;723if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)724scan_number = cinfo->input_scan_number;725cinfo->output_scan_number = scan_number;726/* Perform any dummy output passes, and set up for the real pass */727return output_pass_setup(cinfo);728}729730731/*732* Finish up after an output pass in buffered-image mode.733*734* Returns FALSE if suspended. The return value need be inspected only if735* a suspending data source is used.736*/737738GLOBAL(boolean)739jpeg_finish_output(j_decompress_ptr cinfo)740{741if ((cinfo->global_state == DSTATE_SCANNING ||742cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {743/* Terminate this pass. */744/* We do not require the whole pass to have been completed. */745(*cinfo->master->finish_output_pass) (cinfo);746cinfo->global_state = DSTATE_BUFPOST;747} else if (cinfo->global_state != DSTATE_BUFPOST) {748/* BUFPOST = repeat call after a suspension, anything else is error */749ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);750}751/* Read markers looking for SOS or EOI */752while (cinfo->input_scan_number <= cinfo->output_scan_number &&753!cinfo->inputctl->eoi_reached) {754if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)755return FALSE; /* Suspend, come back later */756}757cinfo->global_state = DSTATE_BUFIMAGE;758return TRUE;759}760761#endif /* D_MULTISCAN_FILES_SUPPORTED */762763#endif /* BITS_IN_JSAMPLE == 8 */764765766