gecko-dev/media/libjpeg/jdapistd.c
Ryan VanderMeulen 4991ef2f9d Bug 1425835 - Update libjpeg-turbo to version 2.0.0. r=aosmond
Also includes the fix for upstream issue #288 to avoid crashes on some older Win7 systems.

Differential Revision: https://phabricator.services.mozilla.com/D7758

--HG--
rename : media/libjpeg/simd/jsimd_arm.c => media/libjpeg/simd/arm/jsimd.c
rename : media/libjpeg/simd/jsimd_arm_neon.S => media/libjpeg/simd/arm/jsimd_neon.S
rename : media/libjpeg/simd/jsimd_arm64.c => media/libjpeg/simd/arm64/jsimd.c
rename : media/libjpeg/simd/jsimd_arm64_neon.S => media/libjpeg/simd/arm64/jsimd_neon.S
rename : media/libjpeg/simd/jccolext-mmx.asm => media/libjpeg/simd/i386/jccolext-mmx.asm
rename : media/libjpeg/simd/jccolext-sse2.asm => media/libjpeg/simd/i386/jccolext-sse2.asm
rename : media/libjpeg/simd/jccolor-mmx.asm => media/libjpeg/simd/i386/jccolor-mmx.asm
rename : media/libjpeg/simd/jccolor-sse2.asm => media/libjpeg/simd/i386/jccolor-sse2.asm
rename : media/libjpeg/simd/jcgray-mmx.asm => media/libjpeg/simd/i386/jcgray-mmx.asm
rename : media/libjpeg/simd/jcgray-sse2.asm => media/libjpeg/simd/i386/jcgray-sse2.asm
rename : media/libjpeg/simd/jcgryext-mmx.asm => media/libjpeg/simd/i386/jcgryext-mmx.asm
rename : media/libjpeg/simd/jcgryext-sse2.asm => media/libjpeg/simd/i386/jcgryext-sse2.asm
rename : media/libjpeg/simd/jchuff-sse2.asm => media/libjpeg/simd/i386/jchuff-sse2.asm
rename : media/libjpeg/simd/jcsample-mmx.asm => media/libjpeg/simd/i386/jcsample-mmx.asm
rename : media/libjpeg/simd/jcsample-sse2.asm => media/libjpeg/simd/i386/jcsample-sse2.asm
rename : media/libjpeg/simd/jdcolext-mmx.asm => media/libjpeg/simd/i386/jdcolext-mmx.asm
rename : media/libjpeg/simd/jdcolext-sse2.asm => media/libjpeg/simd/i386/jdcolext-sse2.asm
rename : media/libjpeg/simd/jdcolor-mmx.asm => media/libjpeg/simd/i386/jdcolor-mmx.asm
rename : media/libjpeg/simd/jdcolor-sse2.asm => media/libjpeg/simd/i386/jdcolor-sse2.asm
rename : media/libjpeg/simd/jdmerge-mmx.asm => media/libjpeg/simd/i386/jdmerge-mmx.asm
rename : media/libjpeg/simd/jdmerge-sse2.asm => media/libjpeg/simd/i386/jdmerge-sse2.asm
rename : media/libjpeg/simd/jdmrgext-mmx.asm => media/libjpeg/simd/i386/jdmrgext-mmx.asm
rename : media/libjpeg/simd/jdmrgext-sse2.asm => media/libjpeg/simd/i386/jdmrgext-sse2.asm
rename : media/libjpeg/simd/jdsample-mmx.asm => media/libjpeg/simd/i386/jdsample-mmx.asm
rename : media/libjpeg/simd/jdsample-sse2.asm => media/libjpeg/simd/i386/jdsample-sse2.asm
rename : media/libjpeg/simd/jfdctflt-3dn.asm => media/libjpeg/simd/i386/jfdctflt-3dn.asm
rename : media/libjpeg/simd/jfdctflt-sse.asm => media/libjpeg/simd/i386/jfdctflt-sse.asm
rename : media/libjpeg/simd/jfdctfst-mmx.asm => media/libjpeg/simd/i386/jfdctfst-mmx.asm
rename : media/libjpeg/simd/jfdctfst-sse2.asm => media/libjpeg/simd/i386/jfdctfst-sse2.asm
rename : media/libjpeg/simd/jfdctint-mmx.asm => media/libjpeg/simd/i386/jfdctint-mmx.asm
rename : media/libjpeg/simd/jfdctint-sse2.asm => media/libjpeg/simd/i386/jfdctint-sse2.asm
rename : media/libjpeg/simd/jidctflt-3dn.asm => media/libjpeg/simd/i386/jidctflt-3dn.asm
rename : media/libjpeg/simd/jidctflt-sse.asm => media/libjpeg/simd/i386/jidctflt-sse.asm
rename : media/libjpeg/simd/jidctflt-sse2.asm => media/libjpeg/simd/i386/jidctflt-sse2.asm
rename : media/libjpeg/simd/jidctfst-mmx.asm => media/libjpeg/simd/i386/jidctfst-mmx.asm
rename : media/libjpeg/simd/jidctfst-sse2.asm => media/libjpeg/simd/i386/jidctfst-sse2.asm
rename : media/libjpeg/simd/jidctint-mmx.asm => media/libjpeg/simd/i386/jidctint-mmx.asm
rename : media/libjpeg/simd/jidctint-sse2.asm => media/libjpeg/simd/i386/jidctint-sse2.asm
rename : media/libjpeg/simd/jidctred-mmx.asm => media/libjpeg/simd/i386/jidctred-mmx.asm
rename : media/libjpeg/simd/jidctred-sse2.asm => media/libjpeg/simd/i386/jidctred-sse2.asm
rename : media/libjpeg/simd/jquant-3dn.asm => media/libjpeg/simd/i386/jquant-3dn.asm
rename : media/libjpeg/simd/jquant-mmx.asm => media/libjpeg/simd/i386/jquant-mmx.asm
rename : media/libjpeg/simd/jquant-sse.asm => media/libjpeg/simd/i386/jquant-sse.asm
rename : media/libjpeg/simd/jquantf-sse2.asm => media/libjpeg/simd/i386/jquantf-sse2.asm
rename : media/libjpeg/simd/jquanti-sse2.asm => media/libjpeg/simd/i386/jquanti-sse2.asm
rename : media/libjpeg/simd/jsimd_i386.c => media/libjpeg/simd/i386/jsimd.c
rename : media/libjpeg/simd/jsimdcpu.asm => media/libjpeg/simd/i386/jsimdcpu.asm
rename : media/libjpeg/simd/jsimd_mips.c => media/libjpeg/simd/mips/jsimd.c
rename : media/libjpeg/simd/jsimd_mips_dspr2.S => media/libjpeg/simd/mips/jsimd_dspr2.S
rename : media/libjpeg/simd/jsimd_mips_dspr2_asm.h => media/libjpeg/simd/mips/jsimd_dspr2_asm.h
rename : media/libjpeg/simd/jcolsamp.inc => media/libjpeg/simd/nasm/jcolsamp.inc
rename : media/libjpeg/simd/jdct.inc => media/libjpeg/simd/nasm/jdct.inc
rename : media/libjpeg/simd/jpeg_nbits_table.inc => media/libjpeg/simd/nasm/jpeg_nbits_table.inc
rename : media/libjpeg/simd/jsimdcfg.inc => media/libjpeg/simd/nasm/jsimdcfg.inc
rename : media/libjpeg/simd/jsimdext.inc => media/libjpeg/simd/nasm/jsimdext.inc
rename : media/libjpeg/simd/jccolext-altivec.c => media/libjpeg/simd/powerpc/jccolext-altivec.c
rename : media/libjpeg/simd/jccolor-altivec.c => media/libjpeg/simd/powerpc/jccolor-altivec.c
rename : media/libjpeg/simd/jcgray-altivec.c => media/libjpeg/simd/powerpc/jcgray-altivec.c
rename : media/libjpeg/simd/jcgryext-altivec.c => media/libjpeg/simd/powerpc/jcgryext-altivec.c
rename : media/libjpeg/simd/jcsample-altivec.c => media/libjpeg/simd/powerpc/jcsample-altivec.c
rename : media/libjpeg/simd/jcsample.h => media/libjpeg/simd/powerpc/jcsample.h
rename : media/libjpeg/simd/jdcolext-altivec.c => media/libjpeg/simd/powerpc/jdcolext-altivec.c
rename : media/libjpeg/simd/jdcolor-altivec.c => media/libjpeg/simd/powerpc/jdcolor-altivec.c
rename : media/libjpeg/simd/jdmerge-altivec.c => media/libjpeg/simd/powerpc/jdmerge-altivec.c
rename : media/libjpeg/simd/jdmrgext-altivec.c => media/libjpeg/simd/powerpc/jdmrgext-altivec.c
rename : media/libjpeg/simd/jdsample-altivec.c => media/libjpeg/simd/powerpc/jdsample-altivec.c
rename : media/libjpeg/simd/jfdctfst-altivec.c => media/libjpeg/simd/powerpc/jfdctfst-altivec.c
rename : media/libjpeg/simd/jfdctint-altivec.c => media/libjpeg/simd/powerpc/jfdctint-altivec.c
rename : media/libjpeg/simd/jidctfst-altivec.c => media/libjpeg/simd/powerpc/jidctfst-altivec.c
rename : media/libjpeg/simd/jidctint-altivec.c => media/libjpeg/simd/powerpc/jidctint-altivec.c
rename : media/libjpeg/simd/jquanti-altivec.c => media/libjpeg/simd/powerpc/jquanti-altivec.c
rename : media/libjpeg/simd/jsimd_powerpc.c => media/libjpeg/simd/powerpc/jsimd.c
rename : media/libjpeg/simd/jsimd_altivec.h => media/libjpeg/simd/powerpc/jsimd_altivec.h
rename : media/libjpeg/simd/jccolext-sse2-64.asm => media/libjpeg/simd/x86_64/jccolext-sse2.asm
rename : media/libjpeg/simd/jccolor-sse2-64.asm => media/libjpeg/simd/x86_64/jccolor-sse2.asm
rename : media/libjpeg/simd/jcgray-sse2-64.asm => media/libjpeg/simd/x86_64/jcgray-sse2.asm
rename : media/libjpeg/simd/jcgryext-sse2-64.asm => media/libjpeg/simd/x86_64/jcgryext-sse2.asm
rename : media/libjpeg/simd/jchuff-sse2-64.asm => media/libjpeg/simd/x86_64/jchuff-sse2.asm
rename : media/libjpeg/simd/jcsample-sse2-64.asm => media/libjpeg/simd/x86_64/jcsample-sse2.asm
rename : media/libjpeg/simd/jdcolext-sse2-64.asm => media/libjpeg/simd/x86_64/jdcolext-sse2.asm
rename : media/libjpeg/simd/jdcolor-sse2-64.asm => media/libjpeg/simd/x86_64/jdcolor-sse2.asm
rename : media/libjpeg/simd/jdmerge-sse2-64.asm => media/libjpeg/simd/x86_64/jdmerge-sse2.asm
rename : media/libjpeg/simd/jdmrgext-sse2-64.asm => media/libjpeg/simd/x86_64/jdmrgext-sse2.asm
rename : media/libjpeg/simd/jdsample-sse2-64.asm => media/libjpeg/simd/x86_64/jdsample-sse2.asm
rename : media/libjpeg/simd/jfdctflt-sse-64.asm => media/libjpeg/simd/x86_64/jfdctflt-sse.asm
rename : media/libjpeg/simd/jfdctfst-sse2-64.asm => media/libjpeg/simd/x86_64/jfdctfst-sse2.asm
rename : media/libjpeg/simd/jfdctint-sse2-64.asm => media/libjpeg/simd/x86_64/jfdctint-sse2.asm
rename : media/libjpeg/simd/jidctflt-sse2-64.asm => media/libjpeg/simd/x86_64/jidctflt-sse2.asm
rename : media/libjpeg/simd/jidctfst-sse2-64.asm => media/libjpeg/simd/x86_64/jidctfst-sse2.asm
rename : media/libjpeg/simd/jidctint-sse2-64.asm => media/libjpeg/simd/x86_64/jidctint-sse2.asm
rename : media/libjpeg/simd/jidctred-sse2-64.asm => media/libjpeg/simd/x86_64/jidctred-sse2.asm
rename : media/libjpeg/simd/jquantf-sse2-64.asm => media/libjpeg/simd/x86_64/jquantf-sse2.asm
rename : media/libjpeg/simd/jquanti-sse2-64.asm => media/libjpeg/simd/x86_64/jquanti-sse2.asm
rename : media/libjpeg/simd/jsimd_x86_64.c => media/libjpeg/simd/x86_64/jsimd.c
extra : moz-landing-system : lando
2018-10-09 15:23:57 +00:00

640 lines
23 KiB
C

/*
* jdapistd.c
*
* This file was part of the Independent JPEG Group's software:
* Copyright (C) 1994-1996, Thomas G. Lane.
* libjpeg-turbo Modifications:
* Copyright (C) 2010, 2015-2018, D. R. Commander.
* Copyright (C) 2015, Google, Inc.
* For conditions of distribution and use, see the accompanying README.ijg
* file.
*
* This file contains application interface code for the decompression half
* of the JPEG library. These are the "standard" API routines that are
* used in the normal full-decompression case. They are not used by a
* transcoding-only application. Note that if an application links in
* jpeg_start_decompress, it will end up linking in the entire decompressor.
* We thus must separate this file from jdapimin.c to avoid linking the
* whole decompression library into a transcoder.
*/
#include "jinclude.h"
#include "jdmainct.h"
#include "jdcoefct.h"
#include "jdsample.h"
#include "jmemsys.h"
/* Forward declarations */
LOCAL(boolean) output_pass_setup(j_decompress_ptr cinfo);
/*
* Decompression initialization.
* jpeg_read_header must be completed before calling this.
*
* If a multipass operating mode was selected, this will do all but the
* last pass, and thus may take a great deal of time.
*
* Returns FALSE if suspended. The return value need be inspected only if
* a suspending data source is used.
*/
GLOBAL(boolean)
jpeg_start_decompress(j_decompress_ptr cinfo)
{
if (cinfo->global_state == DSTATE_READY) {
/* First call: initialize master control, select active modules */
jinit_master_decompress(cinfo);
if (cinfo->buffered_image) {
/* No more work here; expecting jpeg_start_output next */
cinfo->global_state = DSTATE_BUFIMAGE;
return TRUE;
}
cinfo->global_state = DSTATE_PRELOAD;
}
if (cinfo->global_state == DSTATE_PRELOAD) {
/* If file has multiple scans, absorb them all into the coef buffer */
if (cinfo->inputctl->has_multiple_scans) {
#ifdef D_MULTISCAN_FILES_SUPPORTED
for (;;) {
int retcode;
/* Call progress monitor hook if present */
if (cinfo->progress != NULL)
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
/* Absorb some more input */
retcode = (*cinfo->inputctl->consume_input) (cinfo);
if (retcode == JPEG_SUSPENDED)
return FALSE;
if (retcode == JPEG_REACHED_EOI)
break;
/* Advance progress counter if appropriate */
if (cinfo->progress != NULL &&
(retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
/* jdmaster underestimated number of scans; ratchet up one scan */
cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;
}
}
}
#else
ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif /* D_MULTISCAN_FILES_SUPPORTED */
}
cinfo->output_scan_number = cinfo->input_scan_number;
} else if (cinfo->global_state != DSTATE_PRESCAN)
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
/* Perform any dummy output passes, and set up for the final pass */
return output_pass_setup(cinfo);
}
/*
* Set up for an output pass, and perform any dummy pass(es) needed.
* Common subroutine for jpeg_start_decompress and jpeg_start_output.
* Entry: global_state = DSTATE_PRESCAN only if previously suspended.
* Exit: If done, returns TRUE and sets global_state for proper output mode.
* If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
*/
LOCAL(boolean)
output_pass_setup(j_decompress_ptr cinfo)
{
if (cinfo->global_state != DSTATE_PRESCAN) {
/* First call: do pass setup */
(*cinfo->master->prepare_for_output_pass) (cinfo);
cinfo->output_scanline = 0;
cinfo->global_state = DSTATE_PRESCAN;
}
/* Loop over any required dummy passes */
while (cinfo->master->is_dummy_pass) {
#ifdef QUANT_2PASS_SUPPORTED
/* Crank through the dummy pass */
while (cinfo->output_scanline < cinfo->output_height) {
JDIMENSION last_scanline;
/* Call progress monitor hook if present */
if (cinfo->progress != NULL) {
cinfo->progress->pass_counter = (long)cinfo->output_scanline;
cinfo->progress->pass_limit = (long)cinfo->output_height;
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
}
/* Process some data */
last_scanline = cinfo->output_scanline;
(*cinfo->main->process_data) (cinfo, (JSAMPARRAY)NULL,
&cinfo->output_scanline, (JDIMENSION)0);
if (cinfo->output_scanline == last_scanline)
return FALSE; /* No progress made, must suspend */
}
/* Finish up dummy pass, and set up for another one */
(*cinfo->master->finish_output_pass) (cinfo);
(*cinfo->master->prepare_for_output_pass) (cinfo);
cinfo->output_scanline = 0;
#else
ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif /* QUANT_2PASS_SUPPORTED */
}
/* Ready for application to drive output pass through
* jpeg_read_scanlines or jpeg_read_raw_data.
*/
cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
return TRUE;
}
/*
* Enable partial scanline decompression
*
* Must be called after jpeg_start_decompress() and before any calls to
* jpeg_read_scanlines() or jpeg_skip_scanlines().
*
* Refer to libjpeg.txt for more information.
*/
GLOBAL(void)
jpeg_crop_scanline(j_decompress_ptr cinfo, JDIMENSION *xoffset,
JDIMENSION *width)
{
int ci, align, orig_downsampled_width;
JDIMENSION input_xoffset;
boolean reinit_upsampler = FALSE;
jpeg_component_info *compptr;
if (cinfo->global_state != DSTATE_SCANNING || cinfo->output_scanline != 0)
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
if (!xoffset || !width)
ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
/* xoffset and width must fall within the output image dimensions. */
if (*width == 0 || *xoffset + *width > cinfo->output_width)
ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
/* No need to do anything if the caller wants the entire width. */
if (*width == cinfo->output_width)
return;
/* Ensuring the proper alignment of xoffset is tricky. At minimum, it
* must align with an MCU boundary, because:
*
* (1) The IDCT is performed in blocks, and it is not feasible to modify
* the algorithm so that it can transform partial blocks.
* (2) Because of the SIMD extensions, any input buffer passed to the
* upsampling and color conversion routines must be aligned to the
* SIMD word size (for instance, 128-bit in the case of SSE2.) The
* easiest way to accomplish this without copying data is to ensure
* that upsampling and color conversion begin at the start of the
* first MCU column that will be inverse transformed.
*
* In practice, we actually impose a stricter alignment requirement. We
* require that xoffset be a multiple of the maximum MCU column width of all
* of the components (the "iMCU column width.") This is to simplify the
* single-pass decompression case, allowing us to use the same MCU column
* width for all of the components.
*/
if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
align = cinfo->_min_DCT_scaled_size;
else
align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
/* Adjust xoffset to the nearest iMCU boundary <= the requested value */
input_xoffset = *xoffset;
*xoffset = (input_xoffset / align) * align;
/* Adjust the width so that the right edge of the output image is as
* requested (only the left edge is altered.) It is important that calling
* programs check this value after this function returns, so that they can
* allocate an output buffer with the appropriate size.
*/
*width = *width + input_xoffset - *xoffset;
cinfo->output_width = *width;
/* Set the first and last iMCU columns that we must decompress. These values
* will be used in single-scan decompressions.
*/
cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
cinfo->master->last_iMCU_col =
(JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
(long)align) - 1;
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
1 : compptr->h_samp_factor;
/* Set downsampled_width to the new output width. */
orig_downsampled_width = compptr->downsampled_width;
compptr->downsampled_width =
(JDIMENSION)jdiv_round_up((long)(cinfo->output_width *
compptr->h_samp_factor),
(long)cinfo->max_h_samp_factor);
if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)
reinit_upsampler = TRUE;
/* Set the first and last iMCU columns that we must decompress. These
* values will be used in multi-scan decompressions.
*/
cinfo->master->first_MCU_col[ci] =
(JDIMENSION)(long)(*xoffset * hsf) / (long)align;
cinfo->master->last_MCU_col[ci] =
(JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
(long)align) - 1;
}
if (reinit_upsampler) {
cinfo->master->jinit_upsampler_no_alloc = TRUE;
jinit_upsampler(cinfo);
cinfo->master->jinit_upsampler_no_alloc = FALSE;
}
}
/*
* Read some scanlines of data from the JPEG decompressor.
*
* The return value will be the number of lines actually read.
* This may be less than the number requested in several cases,
* including bottom of image, data source suspension, and operating
* modes that emit multiple scanlines at a time.
*
* Note: we warn about excess calls to jpeg_read_scanlines() since
* this likely signals an application programmer error. However,
* an oversize buffer (max_lines > scanlines remaining) is not an error.
*/
GLOBAL(JDIMENSION)
jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines,
JDIMENSION max_lines)
{
JDIMENSION row_ctr;
if (cinfo->global_state != DSTATE_SCANNING)
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
if (cinfo->output_scanline >= cinfo->output_height) {
WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
return 0;
}
/* Call progress monitor hook if present */
if (cinfo->progress != NULL) {
cinfo->progress->pass_counter = (long)cinfo->output_scanline;
cinfo->progress->pass_limit = (long)cinfo->output_height;
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
}
/* Process some data */
row_ctr = 0;
(*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
cinfo->output_scanline += row_ctr;
return row_ctr;
}
/* Dummy color convert function used by jpeg_skip_scanlines() */
LOCAL(void)
noop_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
{
}
/* Dummy quantize function used by jpeg_skip_scanlines() */
LOCAL(void)
noop_quantize(j_decompress_ptr cinfo, JSAMPARRAY input_buf,
JSAMPARRAY output_buf, int num_rows)
{
}
/*
* In some cases, it is best to call jpeg_read_scanlines() and discard the
* output, rather than skipping the scanlines, because this allows us to
* maintain the internal state of the context-based upsampler. In these cases,
* we set up and tear down a dummy color converter in order to avoid valgrind
* errors and to achieve the best possible performance.
*/
LOCAL(void)
read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
{
JDIMENSION n;
void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
JDIMENSION input_row, JSAMPARRAY output_buf,
int num_rows) = NULL;
void (*color_quantize) (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
JSAMPARRAY output_buf, int num_rows) = NULL;
if (cinfo->cconvert && cinfo->cconvert->color_convert) {
color_convert = cinfo->cconvert->color_convert;
cinfo->cconvert->color_convert = noop_convert;
}
if (cinfo->cquantize && cinfo->cquantize->color_quantize) {
color_quantize = cinfo->cquantize->color_quantize;
cinfo->cquantize->color_quantize = noop_quantize;
}
for (n = 0; n < num_lines; n++)
jpeg_read_scanlines(cinfo, NULL, 1);
if (color_convert)
cinfo->cconvert->color_convert = color_convert;
if (color_quantize)
cinfo->cquantize->color_quantize = color_quantize;
}
/*
* Called by jpeg_skip_scanlines(). This partially skips a decompress block by
* incrementing the rowgroup counter.
*/
LOCAL(void)
increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)
{
JDIMENSION rows_left;
my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
/* Increment the counter to the next row group after the skipped rows. */
main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
/* Partially skipping a row group would involve modifying the internal state
* of the upsampler, so read the remaining rows into a dummy buffer instead.
*/
rows_left = rows % cinfo->max_v_samp_factor;
cinfo->output_scanline += rows - rows_left;
read_and_discard_scanlines(cinfo, rows_left);
}
/*
* Skips some scanlines of data from the JPEG decompressor.
*
* The return value will be the number of lines actually skipped. If skipping
* num_lines would move beyond the end of the image, then the actual number of
* lines remaining in the image is returned. Otherwise, the return value will
* be equal to num_lines.
*
* Refer to libjpeg.txt for more information.
*/
GLOBAL(JDIMENSION)
jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
{
my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
JDIMENSION i, x;
int y;
JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
JDIMENSION lines_to_skip, lines_to_read;
if (cinfo->global_state != DSTATE_SCANNING)
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
/* Do not skip past the bottom of the image. */
if (cinfo->output_scanline + num_lines >= cinfo->output_height) {
cinfo->output_scanline = cinfo->output_height;
(*cinfo->inputctl->finish_input_pass) (cinfo);
cinfo->inputctl->eoi_reached = TRUE;
return cinfo->output_height - cinfo->output_scanline;
}
if (num_lines == 0)
return 0;
lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
lines_left_in_iMCU_row =
(lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
lines_per_iMCU_row;
lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
/* Skip the lines remaining in the current iMCU row. When upsampling
* requires context rows, we need the previous and next rows in order to read
* the current row. This adds some complexity.
*/
if (cinfo->upsample->need_context_rows) {
/* If the skipped lines would not move us past the current iMCU row, we
* read the lines and ignore them. There might be a faster way of doing
* this, but we are facing increasing complexity for diminishing returns.
* The increasing complexity would be a by-product of meddling with the
* state machine used to skip context rows. Near the end of an iMCU row,
* the next iMCU row may have already been entropy-decoded. In this unique
* case, we will read the next iMCU row if we cannot skip past it as well.
*/
if ((num_lines < lines_left_in_iMCU_row + 1) ||
(lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
read_and_discard_scanlines(cinfo, num_lines);
return num_lines;
}
/* If the next iMCU row has already been entropy-decoded, make sure that
* we do not skip too far.
*/
if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
lines_after_iMCU_row -= lines_per_iMCU_row;
} else {
cinfo->output_scanline += lines_left_in_iMCU_row;
}
/* If we have just completed the first block, adjust the buffer pointers */
if (main_ptr->iMCU_row_ctr == 0 ||
(main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
set_wraparound_pointers(cinfo);
main_ptr->buffer_full = FALSE;
main_ptr->rowgroup_ctr = 0;
main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
upsample->next_row_out = cinfo->max_v_samp_factor;
upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
}
/* Skipping is much simpler when context rows are not required. */
else {
if (num_lines < lines_left_in_iMCU_row) {
increment_simple_rowgroup_ctr(cinfo, num_lines);
return num_lines;
} else {
cinfo->output_scanline += lines_left_in_iMCU_row;
main_ptr->buffer_full = FALSE;
main_ptr->rowgroup_ctr = 0;
upsample->next_row_out = cinfo->max_v_samp_factor;
upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
}
}
/* Calculate how many full iMCU rows we can skip. */
if (cinfo->upsample->need_context_rows)
lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
lines_per_iMCU_row;
else
lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
lines_per_iMCU_row;
/* Calculate the number of lines that remain to be skipped after skipping all
* of the full iMCU rows that we can. We will not read these lines unless we
* have to.
*/
lines_to_read = lines_after_iMCU_row - lines_to_skip;
/* For images requiring multiple scans (progressive, non-interleaved, etc.),
* all of the entropy decoding occurs in jpeg_start_decompress(), assuming
* that the input data source is non-suspending. This makes skipping easy.
*/
if (cinfo->inputctl->has_multiple_scans) {
if (cinfo->upsample->need_context_rows) {
cinfo->output_scanline += lines_to_skip;
cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
/* It is complex to properly move to the middle of a context block, so
* read the remaining lines instead of skipping them.
*/
read_and_discard_scanlines(cinfo, lines_to_read);
} else {
cinfo->output_scanline += lines_to_skip;
cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
increment_simple_rowgroup_ctr(cinfo, lines_to_read);
}
upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
return num_lines;
}
/* Skip the iMCU rows that we can safely skip. */
for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
for (x = 0; x < cinfo->MCUs_per_row; x++) {
/* Calling decode_mcu() with a NULL pointer causes it to discard the
* decoded coefficients. This is ~5% faster for large subsets, but
* it's tough to tell a difference for smaller images.
*/
(*cinfo->entropy->decode_mcu) (cinfo, NULL);
}
}
cinfo->input_iMCU_row++;
cinfo->output_iMCU_row++;
if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
start_iMCU_row(cinfo);
else
(*cinfo->inputctl->finish_input_pass) (cinfo);
}
cinfo->output_scanline += lines_to_skip;
if (cinfo->upsample->need_context_rows) {
/* Context-based upsampling keeps track of iMCU rows. */
main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
/* It is complex to properly move to the middle of a context block, so
* read the remaining lines instead of skipping them.
*/
read_and_discard_scanlines(cinfo, lines_to_read);
} else {
increment_simple_rowgroup_ctr(cinfo, lines_to_read);
}
/* Since skipping lines involves skipping the upsampling step, the value of
* "rows_to_go" will become invalid unless we set it here. NOTE: This is a
* bit odd, since "rows_to_go" seems to be redundantly keeping track of
* output_scanline.
*/
upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
/* Always skip the requested number of lines. */
return num_lines;
}
/*
* Alternate entry point to read raw data.
* Processes exactly one iMCU row per call, unless suspended.
*/
GLOBAL(JDIMENSION)
jpeg_read_raw_data(j_decompress_ptr cinfo, JSAMPIMAGE data,
JDIMENSION max_lines)
{
JDIMENSION lines_per_iMCU_row;
if (cinfo->global_state != DSTATE_RAW_OK)
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
if (cinfo->output_scanline >= cinfo->output_height) {
WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
return 0;
}
/* Call progress monitor hook if present */
if (cinfo->progress != NULL) {
cinfo->progress->pass_counter = (long)cinfo->output_scanline;
cinfo->progress->pass_limit = (long)cinfo->output_height;
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
}
/* Verify that at least one iMCU row can be returned. */
lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
if (max_lines < lines_per_iMCU_row)
ERREXIT(cinfo, JERR_BUFFER_SIZE);
/* Decompress directly into user's buffer. */
if (!(*cinfo->coef->decompress_data) (cinfo, data))
return 0; /* suspension forced, can do nothing more */
/* OK, we processed one iMCU row. */
cinfo->output_scanline += lines_per_iMCU_row;
return lines_per_iMCU_row;
}
/* Additional entry points for buffered-image mode. */
#ifdef D_MULTISCAN_FILES_SUPPORTED
/*
* Initialize for an output pass in buffered-image mode.
*/
GLOBAL(boolean)
jpeg_start_output(j_decompress_ptr cinfo, int scan_number)
{
if (cinfo->global_state != DSTATE_BUFIMAGE &&
cinfo->global_state != DSTATE_PRESCAN)
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
/* Limit scan number to valid range */
if (scan_number <= 0)
scan_number = 1;
if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)
scan_number = cinfo->input_scan_number;
cinfo->output_scan_number = scan_number;
/* Perform any dummy output passes, and set up for the real pass */
return output_pass_setup(cinfo);
}
/*
* Finish up after an output pass in buffered-image mode.
*
* Returns FALSE if suspended. The return value need be inspected only if
* a suspending data source is used.
*/
GLOBAL(boolean)
jpeg_finish_output(j_decompress_ptr cinfo)
{
if ((cinfo->global_state == DSTATE_SCANNING ||
cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
/* Terminate this pass. */
/* We do not require the whole pass to have been completed. */
(*cinfo->master->finish_output_pass) (cinfo);
cinfo->global_state = DSTATE_BUFPOST;
} else if (cinfo->global_state != DSTATE_BUFPOST) {
/* BUFPOST = repeat call after a suspension, anything else is error */
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
}
/* Read markers looking for SOS or EOI */
while (cinfo->input_scan_number <= cinfo->output_scan_number &&
!cinfo->inputctl->eoi_reached) {
if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
return FALSE; /* Suspend, come back later */
}
cinfo->global_state = DSTATE_BUFIMAGE;
return TRUE;
}
#endif /* D_MULTISCAN_FILES_SUPPORTED */