0N/A * reserved comment block 0N/A * DO NOT REMOVE OR ALTER! 0N/A * Copyright (C) 1994-1997, Thomas G. Lane. 0N/A * This file is part of the Independent JPEG Group's software. 0N/A * For conditions of distribution and use, see the accompanying README file. 0N/A * This file contains the coefficient buffer controller for compression. 0N/A * This controller is the top level of the JPEG compressor proper. 0N/A * The coefficient buffer lies between forward-DCT and entropy encoding steps. 0N/A/* We use a full-image coefficient buffer when doing Huffman optimization, 0N/A * and also for writing multiple-scan JPEG files. In all cases, the DCT 0N/A * step is run during the first pass, and subsequent passes need only read 0N/A * the buffered coefficients. 0N/A/* Private buffer controller object */ 0N/A /* For single-pass compression, it's sufficient to buffer just one MCU 0N/A * (although this may prove a bit slow in practice). We allocate a 0N/A * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each 0N/A * MCU constructed and sent. (On 80x86, the workspace is FAR even though 0N/A * it's not really very big; this is to keep the module interfaces unchanged 0N/A * when a large coefficient buffer is necessary.) 0N/A * In multi-pass modes, this array points to the current MCU's blocks 0N/A * within the virtual arrays. 0N/A /* In multi-pass modes, we need a virtual block array for each component. */ 0N/A/* Forward declarations */ 0N/A/* Reset within-iMCU-row counters for a new row */ 0N/A /* In an interleaved scan, an MCU row is the same as an iMCU row. 0N/A * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. 0N/A * But at the bottom of the image, process only what's left. 0N/A * Initialize for a processing pass. 0N/A * Process some data in the single-pass case. 0N/A * We process the equivalent of one fully interleaved MCU row ("iMCU" row) 0N/A * per call, ie, v_samp_factor block rows for each component in the image. 0N/A * Returns TRUE if the iMCU row is completed, FALSE if suspended. 0N/A * NB: input_buf contains a plane for each component in image, 0N/A * which we index according to the component's SOF position. 0N/A /* Loop to write as much as one whole iMCU row */ 0N/A /* Determine where data comes from in input_buf and do the DCT thing. 0N/A * Each call on forward_DCT processes a horizontal row of DCT blocks 0N/A * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks 0N/A * sequentially. Dummy blocks at the right or bottom edge are filled in 0N/A * specially. The data in them does not matter for image reconstruction, 0N/A * so we fill them with values that will encode to the smallest amount of 0N/A * data, viz: all zeroes in the AC entries, DC entries equal to previous 0N/A * block's DC value. (Thanks to Thomas Kinsman for this idea.) 0N/A /* Create some dummy blocks at the right edge of the image. */ 0N/A /* Create a row of dummy blocks at the bottom of the image. */ 0N/A /* Try to write the MCU. In event of a suspension failure, we will 0N/A * re-DCT the MCU on restart (a bit inefficient, could be fixed...) 0N/A /* Suspension forced; update state counters and exit */ 0N/A /* Completed an MCU row, but perhaps not an iMCU row */ 0N/A /* Completed the iMCU row, advance counters for next one */ 0N/A * Process some data in the first pass of a multi-pass case. 0N/A * We process the equivalent of one fully interleaved MCU row ("iMCU" row) 0N/A * per call, ie, v_samp_factor block rows for each component in the image. 0N/A * This amount of data is read from the source buffer, DCT'd and quantized, 0N/A * and saved into the virtual arrays. We also generate suitable dummy blocks 0N/A * as needed at the right and lower edges. (The dummy blocks are constructed 0N/A * in the virtual arrays, which have been padded appropriately.) This makes 0N/A * it possible for subsequent passes not to worry about real vs. dummy blocks. 0N/A * We must also emit the data to the entropy encoder. This is conveniently 0N/A * done by calling compress_output() after we've loaded the current strip 0N/A * of the virtual arrays. 0N/A * NB: input_buf contains a plane for each component in image. All 0N/A * components are DCT'd and loaded into the virtual arrays in this pass. 0N/A * However, it may be that only a subset of the components are emitted to 0N/A * the entropy encoder during this first pass; be careful about looking 0N/A * at the scan-dependent variables (MCU dimensions, etc). 0N/A /* Align the virtual buffer for this component. */ 0N/A /* Count non-dummy DCT block rows in this iMCU row. */ 0N/A /* NB: can't use last_row_height here, since may not be set! */ 0N/A /* Count number of dummy blocks to be added at the right margin. */ 0N/A /* Perform DCT for all non-dummy blocks in this iMCU row. Each call 0N/A * on forward_DCT processes a complete horizontal row of DCT blocks. 0N/A /* Create dummy blocks at the right edge of the image. */ 0N/A /* If at end of image, create dummy block rows as needed. 0N/A * The tricky part here is that within each MCU, we want the DC values 0N/A * of the dummy blocks to match the last real block's DC value. 0N/A * This squeezes a few more bytes out of the resulting file... 0N/A /* NB: compress_output will increment iMCU_row_num if successful. 0N/A * A suspension return will result in redoing all the work above next time. 0N/A /* Emit data to the entropy encoder, sharing code with subsequent passes */ 0N/A * Process some data in subsequent passes of a multi-pass case. 0N/A * We process the equivalent of one fully interleaved MCU row ("iMCU" row) 0N/A * per call, ie, v_samp_factor block rows for each component in the scan. 0N/A * The data is obtained from the virtual arrays and fed to the entropy coder. 0N/A * Returns TRUE if the iMCU row is completed, FALSE if suspended. 0N/A * NB: input_buf is ignored; it is likely to be a NULL pointer. 0N/A /* Align the virtual buffers for the components used in this scan. 0N/A * NB: during first pass, this is safe only because the buffers will 0N/A * already be aligned properly, so jmemmgr.c won't need to do any I/O. 0N/A /* Loop to process one whole iMCU row */ 0N/A /* Construct list of pointers to DCT blocks belonging to this MCU */ 0N/A blkn = 0;
/* index of current DCT block within MCU */ 0N/A /* Try to write the MCU. */ 0N/A /* Suspension forced; update state counters and exit */ 0N/A /* Completed an MCU row, but perhaps not an iMCU row */ 0N/A /* Completed the iMCU row, advance counters for next one */ 0N/A#
endif /* FULL_COEF_BUFFER_SUPPORTED */ 0N/A * Initialize coefficient buffer controller. 0N/A /* Create the coefficient buffer. */ 0N/A /* Allocate a full-image virtual array for each component, */ 0N/A /* padded to a multiple of samp_factor DCT blocks in each direction. */ 0N/A /* We only need a single-MCU buffer. */