Path: blob/master/src/java.desktop/share/native/libjavajpeg/jccoefct.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jccoefct.c6*7* Copyright (C) 1994-1997, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains the coefficient buffer controller for compression.12* This controller is the top level of the JPEG compressor proper.13* The coefficient buffer lies between forward-DCT and entropy encoding steps.14*/1516#define JPEG_INTERNALS17#include "jinclude.h"18#include "jpeglib.h"192021/* We use a full-image coefficient buffer when doing Huffman optimization,22* and also for writing multiple-scan JPEG files. In all cases, the DCT23* step is run during the first pass, and subsequent passes need only read24* the buffered coefficients.25*/26#ifdef ENTROPY_OPT_SUPPORTED27#define FULL_COEF_BUFFER_SUPPORTED28#else29#ifdef C_MULTISCAN_FILES_SUPPORTED30#define FULL_COEF_BUFFER_SUPPORTED31#endif32#endif333435/* Private buffer controller object */3637typedef struct {38struct jpeg_c_coef_controller pub; /* public fields */3940JDIMENSION iMCU_row_num; /* iMCU row # within image */41JDIMENSION mcu_ctr; /* counts MCUs processed in current row */42int MCU_vert_offset; /* counts MCU rows within iMCU row */43int MCU_rows_per_iMCU_row; /* number of such rows needed */4445/* For single-pass compression, it's sufficient to buffer just one MCU46* (although this may prove a bit slow in practice). We allocate a47* workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each48* MCU constructed and sent. (On 80x86, the workspace is FAR even though49* it's not really very big; this is to keep the module interfaces unchanged50* when a large coefficient buffer is necessary.)51* In multi-pass modes, this array points to the current MCU's blocks52* within the virtual arrays.53*/54JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];5556/* In multi-pass modes, we need a virtual block array for each component. */57jvirt_barray_ptr whole_image[MAX_COMPONENTS];58} my_coef_controller;5960typedef my_coef_controller * my_coef_ptr;616263/* Forward declarations */64METHODDEF(boolean) compress_data65JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));66#ifdef FULL_COEF_BUFFER_SUPPORTED67METHODDEF(boolean) compress_first_pass68JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));69METHODDEF(boolean) compress_output70JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));71#endif727374LOCAL(void)75start_iMCU_row (j_compress_ptr cinfo)76/* Reset within-iMCU-row counters for a new row */77{78my_coef_ptr coef = (my_coef_ptr) cinfo->coef;7980/* In an interleaved scan, an MCU row is the same as an iMCU row.81* In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.82* But at the bottom of the image, process only what's left.83*/84if (cinfo->comps_in_scan > 1) {85coef->MCU_rows_per_iMCU_row = 1;86} else {87if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))88coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;89else90coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;91}9293coef->mcu_ctr = 0;94coef->MCU_vert_offset = 0;95}969798/*99* Initialize for a processing pass.100*/101102METHODDEF(void)103start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)104{105my_coef_ptr coef = (my_coef_ptr) cinfo->coef;106107coef->iMCU_row_num = 0;108start_iMCU_row(cinfo);109110switch (pass_mode) {111case JBUF_PASS_THRU:112if (coef->whole_image[0] != NULL)113ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);114coef->pub.compress_data = compress_data;115break;116#ifdef FULL_COEF_BUFFER_SUPPORTED117case JBUF_SAVE_AND_PASS:118if (coef->whole_image[0] == NULL)119ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);120coef->pub.compress_data = compress_first_pass;121break;122case JBUF_CRANK_DEST:123if (coef->whole_image[0] == NULL)124ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);125coef->pub.compress_data = compress_output;126break;127#endif128default:129ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);130break;131}132}133134135/*136* Process some data in the single-pass case.137* We process the equivalent of one fully interleaved MCU row ("iMCU" row)138* per call, ie, v_samp_factor block rows for each component in the image.139* Returns TRUE if the iMCU row is completed, FALSE if suspended.140*141* NB: input_buf contains a plane for each component in image,142* which we index according to the component's SOF position.143*/144145METHODDEF(boolean)146compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)147{148my_coef_ptr coef = (my_coef_ptr) cinfo->coef;149JDIMENSION MCU_col_num; /* index of current MCU within row */150JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;151JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;152int blkn, bi, ci, yindex, yoffset, blockcnt;153JDIMENSION ypos, xpos;154jpeg_component_info *compptr;155156/* Loop to write as much as one whole iMCU row */157for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;158yoffset++) {159for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;160MCU_col_num++) {161/* Determine where data comes from in input_buf and do the DCT thing.162* Each call on forward_DCT processes a horizontal row of DCT blocks163* as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks164* sequentially. Dummy blocks at the right or bottom edge are filled in165* specially. The data in them does not matter for image reconstruction,166* so we fill them with values that will encode to the smallest amount of167* data, viz: all zeroes in the AC entries, DC entries equal to previous168* block's DC value. (Thanks to Thomas Kinsman for this idea.)169*/170blkn = 0;171for (ci = 0; ci < cinfo->comps_in_scan; ci++) {172compptr = cinfo->cur_comp_info[ci];173blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width174: compptr->last_col_width;175xpos = MCU_col_num * compptr->MCU_sample_width;176ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */177for (yindex = 0; yindex < compptr->MCU_height; yindex++) {178if (coef->iMCU_row_num < last_iMCU_row ||179yoffset+yindex < compptr->last_row_height) {180(*cinfo->fdct->forward_DCT) (cinfo, compptr,181input_buf[compptr->component_index],182coef->MCU_buffer[blkn],183ypos, xpos, (JDIMENSION) blockcnt);184if (blockcnt < compptr->MCU_width) {185/* Create some dummy blocks at the right edge of the image. */186jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],187(compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));188for (bi = blockcnt; bi < compptr->MCU_width; bi++) {189coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];190}191}192} else {193/* Create a row of dummy blocks at the bottom of the image. */194jzero_far((void FAR *) coef->MCU_buffer[blkn],195compptr->MCU_width * SIZEOF(JBLOCK));196for (bi = 0; bi < compptr->MCU_width; bi++) {197coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];198}199}200blkn += compptr->MCU_width;201ypos += DCTSIZE;202}203}204/* Try to write the MCU. In event of a suspension failure, we will205* re-DCT the MCU on restart (a bit inefficient, could be fixed...)206*/207if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {208/* Suspension forced; update state counters and exit */209coef->MCU_vert_offset = yoffset;210coef->mcu_ctr = MCU_col_num;211return FALSE;212}213}214/* Completed an MCU row, but perhaps not an iMCU row */215coef->mcu_ctr = 0;216}217/* Completed the iMCU row, advance counters for next one */218coef->iMCU_row_num++;219start_iMCU_row(cinfo);220return TRUE;221}222223224#ifdef FULL_COEF_BUFFER_SUPPORTED225226/*227* Process some data in the first pass of a multi-pass case.228* We process the equivalent of one fully interleaved MCU row ("iMCU" row)229* per call, ie, v_samp_factor block rows for each component in the image.230* This amount of data is read from the source buffer, DCT'd and quantized,231* and saved into the virtual arrays. We also generate suitable dummy blocks232* as needed at the right and lower edges. (The dummy blocks are constructed233* in the virtual arrays, which have been padded appropriately.) This makes234* it possible for subsequent passes not to worry about real vs. dummy blocks.235*236* We must also emit the data to the entropy encoder. This is conveniently237* done by calling compress_output() after we've loaded the current strip238* of the virtual arrays.239*240* NB: input_buf contains a plane for each component in image. All241* components are DCT'd and loaded into the virtual arrays in this pass.242* However, it may be that only a subset of the components are emitted to243* the entropy encoder during this first pass; be careful about looking244* at the scan-dependent variables (MCU dimensions, etc).245*/246247METHODDEF(boolean)248compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)249{250my_coef_ptr coef = (my_coef_ptr) cinfo->coef;251JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;252JDIMENSION blocks_across, MCUs_across, MCUindex;253int bi, ci, h_samp_factor, block_row, block_rows, ndummy;254JCOEF lastDC;255jpeg_component_info *compptr;256JBLOCKARRAY buffer;257JBLOCKROW thisblockrow, lastblockrow;258259for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;260ci++, compptr++) {261/* Align the virtual buffer for this component. */262buffer = (*cinfo->mem->access_virt_barray)263((j_common_ptr) cinfo, coef->whole_image[ci],264coef->iMCU_row_num * compptr->v_samp_factor,265(JDIMENSION) compptr->v_samp_factor, TRUE);266/* Count non-dummy DCT block rows in this iMCU row. */267if (coef->iMCU_row_num < last_iMCU_row)268block_rows = compptr->v_samp_factor;269else {270/* NB: can't use last_row_height here, since may not be set! */271block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);272if (block_rows == 0) block_rows = compptr->v_samp_factor;273}274blocks_across = compptr->width_in_blocks;275h_samp_factor = compptr->h_samp_factor;276/* Count number of dummy blocks to be added at the right margin. */277ndummy = (int) (blocks_across % h_samp_factor);278if (ndummy > 0)279ndummy = h_samp_factor - ndummy;280/* Perform DCT for all non-dummy blocks in this iMCU row. Each call281* on forward_DCT processes a complete horizontal row of DCT blocks.282*/283for (block_row = 0; block_row < block_rows; block_row++) {284thisblockrow = buffer[block_row];285(*cinfo->fdct->forward_DCT) (cinfo, compptr,286input_buf[ci], thisblockrow,287(JDIMENSION) (block_row * DCTSIZE),288(JDIMENSION) 0, blocks_across);289if (ndummy > 0) {290/* Create dummy blocks at the right edge of the image. */291thisblockrow += blocks_across; /* => first dummy block */292jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));293lastDC = thisblockrow[-1][0];294for (bi = 0; bi < ndummy; bi++) {295thisblockrow[bi][0] = lastDC;296}297}298}299/* If at end of image, create dummy block rows as needed.300* The tricky part here is that within each MCU, we want the DC values301* of the dummy blocks to match the last real block's DC value.302* This squeezes a few more bytes out of the resulting file...303*/304if (coef->iMCU_row_num == last_iMCU_row) {305blocks_across += ndummy; /* include lower right corner */306MCUs_across = blocks_across / h_samp_factor;307for (block_row = block_rows; block_row < compptr->v_samp_factor;308block_row++) {309thisblockrow = buffer[block_row];310lastblockrow = buffer[block_row-1];311jzero_far((void FAR *) thisblockrow,312(size_t) (blocks_across * SIZEOF(JBLOCK)));313for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {314lastDC = lastblockrow[h_samp_factor-1][0];315for (bi = 0; bi < h_samp_factor; bi++) {316thisblockrow[bi][0] = lastDC;317}318thisblockrow += h_samp_factor; /* advance to next MCU in row */319lastblockrow += h_samp_factor;320}321}322}323}324/* NB: compress_output will increment iMCU_row_num if successful.325* A suspension return will result in redoing all the work above next time.326*/327328/* Emit data to the entropy encoder, sharing code with subsequent passes */329return compress_output(cinfo, input_buf);330}331332333/*334* Process some data in subsequent passes of a multi-pass case.335* We process the equivalent of one fully interleaved MCU row ("iMCU" row)336* per call, ie, v_samp_factor block rows for each component in the scan.337* The data is obtained from the virtual arrays and fed to the entropy coder.338* Returns TRUE if the iMCU row is completed, FALSE if suspended.339*340* NB: input_buf is ignored; it is likely to be a NULL pointer.341*/342343METHODDEF(boolean)344compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)345{346my_coef_ptr coef = (my_coef_ptr) cinfo->coef;347JDIMENSION MCU_col_num; /* index of current MCU within row */348int blkn, ci, xindex, yindex, yoffset;349JDIMENSION start_col;350JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];351JBLOCKROW buffer_ptr;352jpeg_component_info *compptr;353354/* Align the virtual buffers for the components used in this scan.355* NB: during first pass, this is safe only because the buffers will356* already be aligned properly, so jmemmgr.c won't need to do any I/O.357*/358for (ci = 0; ci < cinfo->comps_in_scan; ci++) {359compptr = cinfo->cur_comp_info[ci];360buffer[ci] = (*cinfo->mem->access_virt_barray)361((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],362coef->iMCU_row_num * compptr->v_samp_factor,363(JDIMENSION) compptr->v_samp_factor, FALSE);364}365366/* Loop to process one whole iMCU row */367for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;368yoffset++) {369for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;370MCU_col_num++) {371/* Construct list of pointers to DCT blocks belonging to this MCU */372blkn = 0; /* index of current DCT block within MCU */373for (ci = 0; ci < cinfo->comps_in_scan; ci++) {374compptr = cinfo->cur_comp_info[ci];375start_col = MCU_col_num * compptr->MCU_width;376for (yindex = 0; yindex < compptr->MCU_height; yindex++) {377buffer_ptr = buffer[ci][yindex+yoffset] + start_col;378for (xindex = 0; xindex < compptr->MCU_width; xindex++) {379coef->MCU_buffer[blkn++] = buffer_ptr++;380}381}382}383/* Try to write the MCU. */384if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {385/* Suspension forced; update state counters and exit */386coef->MCU_vert_offset = yoffset;387coef->mcu_ctr = MCU_col_num;388return FALSE;389}390}391/* Completed an MCU row, but perhaps not an iMCU row */392coef->mcu_ctr = 0;393}394/* Completed the iMCU row, advance counters for next one */395coef->iMCU_row_num++;396start_iMCU_row(cinfo);397return TRUE;398}399400#endif /* FULL_COEF_BUFFER_SUPPORTED */401402403/*404* Initialize coefficient buffer controller.405*/406407GLOBAL(void)408jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)409{410my_coef_ptr coef;411412coef = (my_coef_ptr)413(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,414SIZEOF(my_coef_controller));415cinfo->coef = (struct jpeg_c_coef_controller *) coef;416coef->pub.start_pass = start_pass_coef;417418/* Create the coefficient buffer. */419if (need_full_buffer) {420#ifdef FULL_COEF_BUFFER_SUPPORTED421/* Allocate a full-image virtual array for each component, */422/* padded to a multiple of samp_factor DCT blocks in each direction. */423int ci;424jpeg_component_info *compptr;425426for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;427ci++, compptr++) {428coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)429((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,430(JDIMENSION) jround_up((long) compptr->width_in_blocks,431(long) compptr->h_samp_factor),432(JDIMENSION) jround_up((long) compptr->height_in_blocks,433(long) compptr->v_samp_factor),434(JDIMENSION) compptr->v_samp_factor);435}436#else437ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);438#endif439} else {440/* We only need a single-MCU buffer. */441JBLOCKROW buffer;442int i;443444buffer = (JBLOCKROW)445(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,446C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));447for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {448coef->MCU_buffer[i] = buffer + i;449}450coef->whole_image[0] = NULL; /* flag for no virtual arrays */451}452}453454455