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