gecko-dev/media/libjpeg/jccoefct.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

450 lines
17 KiB
C

/*
* jccoefct.c
*
* This file was part of the Independent JPEG Group's software:
* Copyright (C) 1994-1997, Thomas G. Lane.
* It was modified by The libjpeg-turbo Project to include only code and
* information relevant to libjpeg-turbo.
* For conditions of distribution and use, see the accompanying README.ijg
* file.
*
* This file contains the coefficient buffer controller for compression.
* This controller is the top level of the JPEG compressor proper.
* The coefficient buffer lies between forward-DCT and entropy encoding steps.
*/
#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"
/* We use a full-image coefficient buffer when doing Huffman optimization,
* and also for writing multiple-scan JPEG files. In all cases, the DCT
* step is run during the first pass, and subsequent passes need only read
* the buffered coefficients.
*/
#ifdef ENTROPY_OPT_SUPPORTED
#define FULL_COEF_BUFFER_SUPPORTED
#else
#ifdef C_MULTISCAN_FILES_SUPPORTED
#define FULL_COEF_BUFFER_SUPPORTED
#endif
#endif
/* Private buffer controller object */
typedef struct {
struct jpeg_c_coef_controller pub; /* public fields */
JDIMENSION iMCU_row_num; /* iMCU row # within image */
JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
int MCU_vert_offset; /* counts MCU rows within iMCU row */
int MCU_rows_per_iMCU_row; /* number of such rows needed */
/* For single-pass compression, it's sufficient to buffer just one MCU
* (although this may prove a bit slow in practice). We allocate a
* workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
* MCU constructed and sent. In multi-pass modes, this array points to the
* current MCU's blocks within the virtual arrays.
*/
JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
/* In multi-pass modes, we need a virtual block array for each component. */
jvirt_barray_ptr whole_image[MAX_COMPONENTS];
} my_coef_controller;
typedef my_coef_controller *my_coef_ptr;
/* Forward declarations */
METHODDEF(boolean) compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf);
#ifdef FULL_COEF_BUFFER_SUPPORTED
METHODDEF(boolean) compress_first_pass(j_compress_ptr cinfo,
JSAMPIMAGE input_buf);
METHODDEF(boolean) compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf);
#endif
LOCAL(void)
start_iMCU_row(j_compress_ptr cinfo)
/* Reset within-iMCU-row counters for a new row */
{
my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
/* In an interleaved scan, an MCU row is the same as an iMCU row.
* In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
* But at the bottom of the image, process only what's left.
*/
if (cinfo->comps_in_scan > 1) {
coef->MCU_rows_per_iMCU_row = 1;
} else {
if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
else
coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
}
coef->mcu_ctr = 0;
coef->MCU_vert_offset = 0;
}
/*
* Initialize for a processing pass.
*/
METHODDEF(void)
start_pass_coef(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
{
my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
coef->iMCU_row_num = 0;
start_iMCU_row(cinfo);
switch (pass_mode) {
case JBUF_PASS_THRU:
if (coef->whole_image[0] != NULL)
ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
coef->pub.compress_data = compress_data;
break;
#ifdef FULL_COEF_BUFFER_SUPPORTED
case JBUF_SAVE_AND_PASS:
if (coef->whole_image[0] == NULL)
ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
coef->pub.compress_data = compress_first_pass;
break;
case JBUF_CRANK_DEST:
if (coef->whole_image[0] == NULL)
ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
coef->pub.compress_data = compress_output;
break;
#endif
default:
ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
break;
}
}
/*
* Process some data in the single-pass case.
* We process the equivalent of one fully interleaved MCU row ("iMCU" row)
* per call, ie, v_samp_factor block rows for each component in the image.
* Returns TRUE if the iMCU row is completed, FALSE if suspended.
*
* NB: input_buf contains a plane for each component in image,
* which we index according to the component's SOF position.
*/
METHODDEF(boolean)
compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
{
my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
JDIMENSION MCU_col_num; /* index of current MCU within row */
JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
int blkn, bi, ci, yindex, yoffset, blockcnt;
JDIMENSION ypos, xpos;
jpeg_component_info *compptr;
/* Loop to write as much as one whole iMCU row */
for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
yoffset++) {
for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
MCU_col_num++) {
/* Determine where data comes from in input_buf and do the DCT thing.
* Each call on forward_DCT processes a horizontal row of DCT blocks
* as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
* sequentially. Dummy blocks at the right or bottom edge are filled in
* specially. The data in them does not matter for image reconstruction,
* so we fill them with values that will encode to the smallest amount of
* data, viz: all zeroes in the AC entries, DC entries equal to previous
* block's DC value. (Thanks to Thomas Kinsman for this idea.)
*/
blkn = 0;
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
compptr = cinfo->cur_comp_info[ci];
blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
compptr->last_col_width;
xpos = MCU_col_num * compptr->MCU_sample_width;
ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
if (coef->iMCU_row_num < last_iMCU_row ||
yoffset + yindex < compptr->last_row_height) {
(*cinfo->fdct->forward_DCT) (cinfo, compptr,
input_buf[compptr->component_index],
coef->MCU_buffer[blkn],
ypos, xpos, (JDIMENSION)blockcnt);
if (blockcnt < compptr->MCU_width) {
/* Create some dummy blocks at the right edge of the image. */
jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
(compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
coef->MCU_buffer[blkn + bi][0][0] =
coef->MCU_buffer[blkn + bi - 1][0][0];
}
}
} else {
/* Create a row of dummy blocks at the bottom of the image. */
jzero_far((void *)coef->MCU_buffer[blkn],
compptr->MCU_width * sizeof(JBLOCK));
for (bi = 0; bi < compptr->MCU_width; bi++) {
coef->MCU_buffer[blkn + bi][0][0] =
coef->MCU_buffer[blkn - 1][0][0];
}
}
blkn += compptr->MCU_width;
ypos += DCTSIZE;
}
}
/* Try to write the MCU. In event of a suspension failure, we will
* re-DCT the MCU on restart (a bit inefficient, could be fixed...)
*/
if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
/* Suspension forced; update state counters and exit */
coef->MCU_vert_offset = yoffset;
coef->mcu_ctr = MCU_col_num;
return FALSE;
}
}
/* Completed an MCU row, but perhaps not an iMCU row */
coef->mcu_ctr = 0;
}
/* Completed the iMCU row, advance counters for next one */
coef->iMCU_row_num++;
start_iMCU_row(cinfo);
return TRUE;
}
#ifdef FULL_COEF_BUFFER_SUPPORTED
/*
* Process some data in the first pass of a multi-pass case.
* We process the equivalent of one fully interleaved MCU row ("iMCU" row)
* per call, ie, v_samp_factor block rows for each component in the image.
* This amount of data is read from the source buffer, DCT'd and quantized,
* and saved into the virtual arrays. We also generate suitable dummy blocks
* as needed at the right and lower edges. (The dummy blocks are constructed
* in the virtual arrays, which have been padded appropriately.) This makes
* it possible for subsequent passes not to worry about real vs. dummy blocks.
*
* We must also emit the data to the entropy encoder. This is conveniently
* done by calling compress_output() after we've loaded the current strip
* of the virtual arrays.
*
* NB: input_buf contains a plane for each component in image. All
* components are DCT'd and loaded into the virtual arrays in this pass.
* However, it may be that only a subset of the components are emitted to
* the entropy encoder during this first pass; be careful about looking
* at the scan-dependent variables (MCU dimensions, etc).
*/
METHODDEF(boolean)
compress_first_pass(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
{
my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
JDIMENSION blocks_across, MCUs_across, MCUindex;
int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
JCOEF lastDC;
jpeg_component_info *compptr;
JBLOCKARRAY buffer;
JBLOCKROW thisblockrow, lastblockrow;
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
/* Align the virtual buffer for this component. */
buffer = (*cinfo->mem->access_virt_barray)
((j_common_ptr)cinfo, coef->whole_image[ci],
coef->iMCU_row_num * compptr->v_samp_factor,
(JDIMENSION)compptr->v_samp_factor, TRUE);
/* Count non-dummy DCT block rows in this iMCU row. */
if (coef->iMCU_row_num < last_iMCU_row)
block_rows = compptr->v_samp_factor;
else {
/* NB: can't use last_row_height here, since may not be set! */
block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
if (block_rows == 0) block_rows = compptr->v_samp_factor;
}
blocks_across = compptr->width_in_blocks;
h_samp_factor = compptr->h_samp_factor;
/* Count number of dummy blocks to be added at the right margin. */
ndummy = (int)(blocks_across % h_samp_factor);
if (ndummy > 0)
ndummy = h_samp_factor - ndummy;
/* Perform DCT for all non-dummy blocks in this iMCU row. Each call
* on forward_DCT processes a complete horizontal row of DCT blocks.
*/
for (block_row = 0; block_row < block_rows; block_row++) {
thisblockrow = buffer[block_row];
(*cinfo->fdct->forward_DCT) (cinfo, compptr,
input_buf[ci], thisblockrow,
(JDIMENSION)(block_row * DCTSIZE),
(JDIMENSION)0, blocks_across);
if (ndummy > 0) {
/* Create dummy blocks at the right edge of the image. */
thisblockrow += blocks_across; /* => first dummy block */
jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
lastDC = thisblockrow[-1][0];
for (bi = 0; bi < ndummy; bi++) {
thisblockrow[bi][0] = lastDC;
}
}
}
/* If at end of image, create dummy block rows as needed.
* The tricky part here is that within each MCU, we want the DC values
* of the dummy blocks to match the last real block's DC value.
* This squeezes a few more bytes out of the resulting file...
*/
if (coef->iMCU_row_num == last_iMCU_row) {
blocks_across += ndummy; /* include lower right corner */
MCUs_across = blocks_across / h_samp_factor;
for (block_row = block_rows; block_row < compptr->v_samp_factor;
block_row++) {
thisblockrow = buffer[block_row];
lastblockrow = buffer[block_row - 1];
jzero_far((void *)thisblockrow,
(size_t)(blocks_across * sizeof(JBLOCK)));
for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
lastDC = lastblockrow[h_samp_factor - 1][0];
for (bi = 0; bi < h_samp_factor; bi++) {
thisblockrow[bi][0] = lastDC;
}
thisblockrow += h_samp_factor; /* advance to next MCU in row */
lastblockrow += h_samp_factor;
}
}
}
}
/* NB: compress_output will increment iMCU_row_num if successful.
* A suspension return will result in redoing all the work above next time.
*/
/* Emit data to the entropy encoder, sharing code with subsequent passes */
return compress_output(cinfo, input_buf);
}
/*
* Process some data in subsequent passes of a multi-pass case.
* We process the equivalent of one fully interleaved MCU row ("iMCU" row)
* per call, ie, v_samp_factor block rows for each component in the scan.
* The data is obtained from the virtual arrays and fed to the entropy coder.
* Returns TRUE if the iMCU row is completed, FALSE if suspended.
*
* NB: input_buf is ignored; it is likely to be a NULL pointer.
*/
METHODDEF(boolean)
compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
{
my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
JDIMENSION MCU_col_num; /* index of current MCU within row */
int blkn, ci, xindex, yindex, yoffset;
JDIMENSION start_col;
JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
JBLOCKROW buffer_ptr;
jpeg_component_info *compptr;
/* Align the virtual buffers for the components used in this scan.
* NB: during first pass, this is safe only because the buffers will
* already be aligned properly, so jmemmgr.c won't need to do any I/O.
*/
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
compptr = cinfo->cur_comp_info[ci];
buffer[ci] = (*cinfo->mem->access_virt_barray)
((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
coef->iMCU_row_num * compptr->v_samp_factor,
(JDIMENSION)compptr->v_samp_factor, FALSE);
}
/* Loop to process one whole iMCU row */
for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
yoffset++) {
for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
MCU_col_num++) {
/* Construct list of pointers to DCT blocks belonging to this MCU */
blkn = 0; /* index of current DCT block within MCU */
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
compptr = cinfo->cur_comp_info[ci];
start_col = MCU_col_num * compptr->MCU_width;
for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
coef->MCU_buffer[blkn++] = buffer_ptr++;
}
}
}
/* Try to write the MCU. */
if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
/* Suspension forced; update state counters and exit */
coef->MCU_vert_offset = yoffset;
coef->mcu_ctr = MCU_col_num;
return FALSE;
}
}
/* Completed an MCU row, but perhaps not an iMCU row */
coef->mcu_ctr = 0;
}
/* Completed the iMCU row, advance counters for next one */
coef->iMCU_row_num++;
start_iMCU_row(cinfo);
return TRUE;
}
#endif /* FULL_COEF_BUFFER_SUPPORTED */
/*
* Initialize coefficient buffer controller.
*/
GLOBAL(void)
jinit_c_coef_controller(j_compress_ptr cinfo, boolean need_full_buffer)
{
my_coef_ptr coef;
coef = (my_coef_ptr)
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
sizeof(my_coef_controller));
cinfo->coef = (struct jpeg_c_coef_controller *)coef;
coef->pub.start_pass = start_pass_coef;
/* Create the coefficient buffer. */
if (need_full_buffer) {
#ifdef FULL_COEF_BUFFER_SUPPORTED
/* Allocate a full-image virtual array for each component, */
/* padded to a multiple of samp_factor DCT blocks in each direction. */
int ci;
jpeg_component_info *compptr;
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
(JDIMENSION)jround_up((long)compptr->width_in_blocks,
(long)compptr->h_samp_factor),
(JDIMENSION)jround_up((long)compptr->height_in_blocks,
(long)compptr->v_samp_factor),
(JDIMENSION)compptr->v_samp_factor);
}
#else
ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
#endif
} else {
/* We only need a single-MCU buffer. */
JBLOCKROW buffer;
int i;
buffer = (JBLOCKROW)
(*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
coef->MCU_buffer[i] = buffer + i;
}
coef->whole_image[0] = NULL; /* flag for no virtual arrays */
}
}