170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * jccoefct.c
370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Copyright (C) 1994-1997, Thomas G. Lane.
570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This file is part of the Independent JPEG Group's software.
670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * For conditions of distribution and use, see the accompanying README file.
770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This file contains the coefficient buffer controller for compression.
970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This controller is the top level of the JPEG compressor proper.
1070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The coefficient buffer lies between forward-DCT and entropy encoding steps.
1170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
1270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
1370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define JPEG_INTERNALS
1470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include "jinclude.h"
1570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include "jpeglib.h"
1670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
1770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
1870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* We use a full-image coefficient buffer when doing Huffman optimization,
1970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * and also for writing multiple-scan JPEG files.  In all cases, the DCT
2070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * step is run during the first pass, and subsequent passes need only read
2170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * the buffered coefficients.
2270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
2370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef ENTROPY_OPT_SUPPORTED
2470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define FULL_COEF_BUFFER_SUPPORTED
2570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#else
2670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef C_MULTISCAN_FILES_SUPPORTED
2770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define FULL_COEF_BUFFER_SUPPORTED
2870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
2970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
3070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Private buffer controller object */
3370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef struct {
3570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  struct jpeg_c_coef_controller pub; /* public fields */
3670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION iMCU_row_num;	/* iMCU row # within image */
3870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION mcu_ctr;		/* counts MCUs processed in current row */
3970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
4070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
4170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
4270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* For single-pass compression, it's sufficient to buffer just one MCU
4370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * (although this may prove a bit slow in practice).  We allocate a
4470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
4570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * MCU constructed and sent.  (On 80x86, the workspace is FAR even though
4670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * it's not really very big; this is to keep the module interfaces unchanged
4770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * when a large coefficient buffer is necessary.)
4870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * In multi-pass modes, this array points to the current MCU's blocks
4970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * within the virtual arrays.
5070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
5170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
5270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
5370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* In multi-pass modes, we need a virtual block array for each component. */
5470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
5570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} my_coef_controller;
5670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
5770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef my_coef_controller * my_coef_ptr;
5870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
5970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Forward declarations */
6170a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(boolean) compress_data
6270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
6370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef FULL_COEF_BUFFER_SUPPORTED
6470a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(boolean) compress_first_pass
6570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
6670a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(boolean) compress_output
6770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
6870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
6970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
7070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
7170a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(void)
7270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinestart_iMCU_row (j_compress_ptr cinfo)
7370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Reset within-iMCU-row counters for a new row */
7470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
7570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
7670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
7770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* In an interleaved scan, an MCU row is the same as an iMCU row.
7870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
7970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * But at the bottom of the image, process only what's left.
8070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
8170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (cinfo->comps_in_scan > 1) {
8270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    coef->MCU_rows_per_iMCU_row = 1;
8370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  } else {
8470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
8570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
8670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    else
8770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
8870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
8970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  coef->mcu_ctr = 0;
9170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  coef->MCU_vert_offset = 0;
9270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
9370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
9670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Initialize for a processing pass.
9770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
9870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9970a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void)
10070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinestart_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
10170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
10270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
10370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
10470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  coef->iMCU_row_num = 0;
10570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  start_iMCU_row(cinfo);
10670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
10770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  switch (pass_mode) {
10870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  case JBUF_PASS_THRU:
10970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (coef->whole_image[0] != NULL)
11070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
11170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    coef->pub.compress_data = compress_data;
11270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    break;
11370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef FULL_COEF_BUFFER_SUPPORTED
11470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  case JBUF_SAVE_AND_PASS:
11570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (coef->whole_image[0] == NULL)
11670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
11770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    coef->pub.compress_data = compress_first_pass;
11870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    break;
11970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  case JBUF_CRANK_DEST:
12070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (coef->whole_image[0] == NULL)
12170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
12270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    coef->pub.compress_data = compress_output;
12370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    break;
12470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
12570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  default:
12670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
12770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    break;
12870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
12970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
13070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
13170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
13270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
13370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Process some data in the single-pass case.
13470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
13570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * per call, ie, v_samp_factor block rows for each component in the image.
13670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Returns TRUE if the iMCU row is completed, FALSE if suspended.
13770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
13870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * NB: input_buf contains a plane for each component in image,
13970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * which we index according to the component's SOF position.
14070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
14170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
14270a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(boolean)
14370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinecompress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
14470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
14570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
14670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION MCU_col_num;	/* index of current MCU within row */
14770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
14870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
14970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int blkn, bi, ci, yindex, yoffset, blockcnt;
15070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION ypos, xpos;
15170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_component_info *compptr;
15270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
15370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Loop to write as much as one whole iMCU row */
15470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
15570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       yoffset++) {
15670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
15770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 MCU_col_num++) {
15870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Determine where data comes from in input_buf and do the DCT thing.
15970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       * Each call on forward_DCT processes a horizontal row of DCT blocks
16070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
16170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       * sequentially.  Dummy blocks at the right or bottom edge are filled in
16270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       * specially.  The data in them does not matter for image reconstruction,
16370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       * so we fill them with values that will encode to the smallest amount of
16470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       * data, viz: all zeroes in the AC entries, DC entries equal to previous
16570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
16670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       */
16770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      blkn = 0;
16870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
16970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	compptr = cinfo->cur_comp_info[ci];
17070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
17170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine						: compptr->last_col_width;
17270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	xpos = MCU_col_num * compptr->MCU_sample_width;
17370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
17470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
17570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  if (coef->iMCU_row_num < last_iMCU_row ||
17670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	      yoffset+yindex < compptr->last_row_height) {
17770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	    (*cinfo->fdct->forward_DCT) (cinfo, compptr,
17870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine					 input_buf[compptr->component_index],
17970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine					 coef->MCU_buffer[blkn],
18070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine					 ypos, xpos, (JDIMENSION) blockcnt);
18170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	    if (blockcnt < compptr->MCU_width) {
18270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	      /* Create some dummy blocks at the right edge of the image. */
18370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	      jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
18470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine			(compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
18570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	      for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
18670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine		coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
18770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	      }
18870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	    }
18970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  } else {
19070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	    /* Create a row of dummy blocks at the bottom of the image. */
19170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	    jzero_far((void FAR *) coef->MCU_buffer[blkn],
19270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine		      compptr->MCU_width * SIZEOF(JBLOCK));
19370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	    for (bi = 0; bi < compptr->MCU_width; bi++) {
19470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	      coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
19570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	    }
19670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  }
19770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  blkn += compptr->MCU_width;
19870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  ypos += DCTSIZE;
19970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	}
20070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
20170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Try to write the MCU.  In event of a suspension failure, we will
20270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
20370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       */
20470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
20570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	/* Suspension forced; update state counters and exit */
20670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	coef->MCU_vert_offset = yoffset;
20770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	coef->mcu_ctr = MCU_col_num;
20870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	return FALSE;
20970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
21070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
21170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Completed an MCU row, but perhaps not an iMCU row */
21270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    coef->mcu_ctr = 0;
21370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
21470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Completed the iMCU row, advance counters for next one */
21570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  coef->iMCU_row_num++;
21670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  start_iMCU_row(cinfo);
21770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return TRUE;
21870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
21970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
22070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
22170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef FULL_COEF_BUFFER_SUPPORTED
22270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
22370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
22470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Process some data in the first pass of a multi-pass case.
22570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
22670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * per call, ie, v_samp_factor block rows for each component in the image.
22770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This amount of data is read from the source buffer, DCT'd and quantized,
22870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * and saved into the virtual arrays.  We also generate suitable dummy blocks
22970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * as needed at the right and lower edges.  (The dummy blocks are constructed
23070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * in the virtual arrays, which have been padded appropriately.)  This makes
23170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * it possible for subsequent passes not to worry about real vs. dummy blocks.
23270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
23370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We must also emit the data to the entropy encoder.  This is conveniently
23470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * done by calling compress_output() after we've loaded the current strip
23570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * of the virtual arrays.
23670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
23770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * NB: input_buf contains a plane for each component in image.  All
23870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * components are DCT'd and loaded into the virtual arrays in this pass.
23970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * However, it may be that only a subset of the components are emitted to
24070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * the entropy encoder during this first pass; be careful about looking
24170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * at the scan-dependent variables (MCU dimensions, etc).
24270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
24370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
24470a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(boolean)
24570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinecompress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
24670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
24770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
24870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
24970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION blocks_across, MCUs_across, MCUindex;
25070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
25170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JCOEF lastDC;
25270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_component_info *compptr;
25370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JBLOCKARRAY buffer;
25470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JBLOCKROW thisblockrow, lastblockrow;
25570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
25670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
25770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       ci++, compptr++) {
25870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Align the virtual buffer for this component. */
25970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    buffer = (*cinfo->mem->access_virt_barray)
26070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ((j_common_ptr) cinfo, coef->whole_image[ci],
26170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       coef->iMCU_row_num * compptr->v_samp_factor,
26270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       (JDIMENSION) compptr->v_samp_factor, TRUE);
26370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Count non-dummy DCT block rows in this iMCU row. */
26470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (coef->iMCU_row_num < last_iMCU_row)
26570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      block_rows = compptr->v_samp_factor;
26670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    else {
26770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* NB: can't use last_row_height here, since may not be set! */
26870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
26970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (block_rows == 0) block_rows = compptr->v_samp_factor;
27070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
27170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    blocks_across = compptr->width_in_blocks;
27270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    h_samp_factor = compptr->h_samp_factor;
27370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Count number of dummy blocks to be added at the right margin. */
27470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ndummy = (int) (blocks_across % h_samp_factor);
27570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (ndummy > 0)
27670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ndummy = h_samp_factor - ndummy;
27770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
27870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     * on forward_DCT processes a complete horizontal row of DCT blocks.
27970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     */
28070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    for (block_row = 0; block_row < block_rows; block_row++) {
28170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      thisblockrow = buffer[block_row];
28270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      (*cinfo->fdct->forward_DCT) (cinfo, compptr,
28370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				   input_buf[ci], thisblockrow,
28470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				   (JDIMENSION) (block_row * DCTSIZE),
28570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				   (JDIMENSION) 0, blocks_across);
28670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (ndummy > 0) {
28770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	/* Create dummy blocks at the right edge of the image. */
28870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	thisblockrow += blocks_across; /* => first dummy block */
28970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
29070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	lastDC = thisblockrow[-1][0];
29170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	for (bi = 0; bi < ndummy; bi++) {
29270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  thisblockrow[bi][0] = lastDC;
29370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	}
29470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
29570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
29670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* If at end of image, create dummy block rows as needed.
29770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     * The tricky part here is that within each MCU, we want the DC values
29870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     * of the dummy blocks to match the last real block's DC value.
29970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     * This squeezes a few more bytes out of the resulting file...
30070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     */
30170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (coef->iMCU_row_num == last_iMCU_row) {
30270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      blocks_across += ndummy;	/* include lower right corner */
30370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      MCUs_across = blocks_across / h_samp_factor;
30470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      for (block_row = block_rows; block_row < compptr->v_samp_factor;
30570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	   block_row++) {
30670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	thisblockrow = buffer[block_row];
30770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	lastblockrow = buffer[block_row-1];
30870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	jzero_far((void FAR *) thisblockrow,
30970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine		  (size_t) (blocks_across * SIZEOF(JBLOCK)));
31070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
31170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  lastDC = lastblockrow[h_samp_factor-1][0];
31270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  for (bi = 0; bi < h_samp_factor; bi++) {
31370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	    thisblockrow[bi][0] = lastDC;
31470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  }
31570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  thisblockrow += h_samp_factor; /* advance to next MCU in row */
31670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  lastblockrow += h_samp_factor;
31770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	}
31870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
31970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
32070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
32170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* NB: compress_output will increment iMCU_row_num if successful.
32270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * A suspension return will result in redoing all the work above next time.
32370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
32470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
32570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Emit data to the entropy encoder, sharing code with subsequent passes */
32670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return compress_output(cinfo, input_buf);
32770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
32870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
32970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
33070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
33170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Process some data in subsequent passes of a multi-pass case.
33270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
33370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * per call, ie, v_samp_factor block rows for each component in the scan.
33470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The data is obtained from the virtual arrays and fed to the entropy coder.
33570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Returns TRUE if the iMCU row is completed, FALSE if suspended.
33670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
33770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * NB: input_buf is ignored; it is likely to be a NULL pointer.
33870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
33970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
34070a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(boolean)
34170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinecompress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
34270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
34370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
34470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION MCU_col_num;	/* index of current MCU within row */
34570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int blkn, ci, xindex, yindex, yoffset;
34670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION start_col;
34770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
34870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JBLOCKROW buffer_ptr;
34970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_component_info *compptr;
35070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
35170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Align the virtual buffers for the components used in this scan.
35270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * NB: during first pass, this is safe only because the buffers will
35370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
35470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
35570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
35670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    compptr = cinfo->cur_comp_info[ci];
35770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    buffer[ci] = (*cinfo->mem->access_virt_barray)
35870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
35970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       coef->iMCU_row_num * compptr->v_samp_factor,
36070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       (JDIMENSION) compptr->v_samp_factor, FALSE);
36170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
36270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
36370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Loop to process one whole iMCU row */
36470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
36570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       yoffset++) {
36670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
36770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 MCU_col_num++) {
36870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Construct list of pointers to DCT blocks belonging to this MCU */
36970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      blkn = 0;			/* index of current DCT block within MCU */
37070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
37170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	compptr = cinfo->cur_comp_info[ci];
37270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	start_col = MCU_col_num * compptr->MCU_width;
37370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
37470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
37570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
37670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	    coef->MCU_buffer[blkn++] = buffer_ptr++;
37770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  }
37870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	}
37970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
38070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Try to write the MCU. */
38170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
38270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	/* Suspension forced; update state counters and exit */
38370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	coef->MCU_vert_offset = yoffset;
38470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	coef->mcu_ctr = MCU_col_num;
38570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	return FALSE;
38670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
38770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
38870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Completed an MCU row, but perhaps not an iMCU row */
38970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    coef->mcu_ctr = 0;
39070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
39170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Completed the iMCU row, advance counters for next one */
39270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  coef->iMCU_row_num++;
39370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  start_iMCU_row(cinfo);
39470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return TRUE;
39570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
39670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
39770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif /* FULL_COEF_BUFFER_SUPPORTED */
39870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
39970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
40070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
40170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Initialize coefficient buffer controller.
40270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
40370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
40470a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
40570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
40670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
40770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  my_coef_ptr coef;
40870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
40970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  coef = (my_coef_ptr)
41070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
41170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				SIZEOF(my_coef_controller));
41270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
41370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  coef->pub.start_pass = start_pass_coef;
41470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
41570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Create the coefficient buffer. */
41670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (need_full_buffer) {
41770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef FULL_COEF_BUFFER_SUPPORTED
41870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Allocate a full-image virtual array for each component, */
41970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* padded to a multiple of samp_factor DCT blocks in each direction. */
42070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    int ci;
42170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    jpeg_component_info *compptr;
42270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
42370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
42470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 ci++, compptr++) {
42570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
42670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
42770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
42870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				(long) compptr->h_samp_factor),
42970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
43070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				(long) compptr->v_samp_factor),
43170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 (JDIMENSION) compptr->v_samp_factor);
43270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
43370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#else
43470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
43570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
43670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  } else {
43770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* We only need a single-MCU buffer. */
43870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    JBLOCKROW buffer;
43970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    int i;
44070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
44170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    buffer = (JBLOCKROW)
44270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
44370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				  C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
44470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
44570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      coef->MCU_buffer[i] = buffer + i;
44670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
44770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
44870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
44970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
450