istereo2

diff libs/libjpeg/jdcoefct.c @ 2:81d35769f546

added the tunnel effect source
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 19 Sep 2015 05:51:51 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libjpeg/jdcoefct.c	Sat Sep 19 05:51:51 2015 +0300
     1.3 @@ -0,0 +1,736 @@
     1.4 +/*
     1.5 + * jdcoefct.c
     1.6 + *
     1.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
     1.8 + * This file is part of the Independent JPEG Group's software.
     1.9 + * For conditions of distribution and use, see the accompanying README file.
    1.10 + *
    1.11 + * This file contains the coefficient buffer controller for decompression.
    1.12 + * This controller is the top level of the JPEG decompressor proper.
    1.13 + * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
    1.14 + *
    1.15 + * In buffered-image mode, this controller is the interface between
    1.16 + * input-oriented processing and output-oriented processing.
    1.17 + * Also, the input side (only) is used when reading a file for transcoding.
    1.18 + */
    1.19 +
    1.20 +#define JPEG_INTERNALS
    1.21 +#include "jinclude.h"
    1.22 +#include "jpeglib.h"
    1.23 +
    1.24 +/* Block smoothing is only applicable for progressive JPEG, so: */
    1.25 +#ifndef D_PROGRESSIVE_SUPPORTED
    1.26 +#undef BLOCK_SMOOTHING_SUPPORTED
    1.27 +#endif
    1.28 +
    1.29 +/* Private buffer controller object */
    1.30 +
    1.31 +typedef struct {
    1.32 +  struct jpeg_d_coef_controller pub; /* public fields */
    1.33 +
    1.34 +  /* These variables keep track of the current location of the input side. */
    1.35 +  /* cinfo->input_iMCU_row is also used for this. */
    1.36 +  JDIMENSION MCU_ctr;		/* counts MCUs processed in current row */
    1.37 +  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
    1.38 +  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
    1.39 +
    1.40 +  /* The output side's location is represented by cinfo->output_iMCU_row. */
    1.41 +
    1.42 +  /* In single-pass modes, it's sufficient to buffer just one MCU.
    1.43 +   * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
    1.44 +   * and let the entropy decoder write into that workspace each time.
    1.45 +   * (On 80x86, the workspace is FAR even though it's not really very big;
    1.46 +   * this is to keep the module interfaces unchanged when a large coefficient
    1.47 +   * buffer is necessary.)
    1.48 +   * In multi-pass modes, this array points to the current MCU's blocks
    1.49 +   * within the virtual arrays; it is used only by the input side.
    1.50 +   */
    1.51 +  JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
    1.52 +
    1.53 +#ifdef D_MULTISCAN_FILES_SUPPORTED
    1.54 +  /* In multi-pass modes, we need a virtual block array for each component. */
    1.55 +  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
    1.56 +#endif
    1.57 +
    1.58 +#ifdef BLOCK_SMOOTHING_SUPPORTED
    1.59 +  /* When doing block smoothing, we latch coefficient Al values here */
    1.60 +  int * coef_bits_latch;
    1.61 +#define SAVED_COEFS  6		/* we save coef_bits[0..5] */
    1.62 +#endif
    1.63 +} my_coef_controller;
    1.64 +
    1.65 +typedef my_coef_controller * my_coef_ptr;
    1.66 +
    1.67 +/* Forward declarations */
    1.68 +METHODDEF(int) decompress_onepass
    1.69 +	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
    1.70 +#ifdef D_MULTISCAN_FILES_SUPPORTED
    1.71 +METHODDEF(int) decompress_data
    1.72 +	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
    1.73 +#endif
    1.74 +#ifdef BLOCK_SMOOTHING_SUPPORTED
    1.75 +LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
    1.76 +METHODDEF(int) decompress_smooth_data
    1.77 +	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
    1.78 +#endif
    1.79 +
    1.80 +
    1.81 +LOCAL(void)
    1.82 +start_iMCU_row (j_decompress_ptr cinfo)
    1.83 +/* Reset within-iMCU-row counters for a new row (input side) */
    1.84 +{
    1.85 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
    1.86 +
    1.87 +  /* In an interleaved scan, an MCU row is the same as an iMCU row.
    1.88 +   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
    1.89 +   * But at the bottom of the image, process only what's left.
    1.90 +   */
    1.91 +  if (cinfo->comps_in_scan > 1) {
    1.92 +    coef->MCU_rows_per_iMCU_row = 1;
    1.93 +  } else {
    1.94 +    if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
    1.95 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
    1.96 +    else
    1.97 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
    1.98 +  }
    1.99 +
   1.100 +  coef->MCU_ctr = 0;
   1.101 +  coef->MCU_vert_offset = 0;
   1.102 +}
   1.103 +
   1.104 +
   1.105 +/*
   1.106 + * Initialize for an input processing pass.
   1.107 + */
   1.108 +
   1.109 +METHODDEF(void)
   1.110 +start_input_pass (j_decompress_ptr cinfo)
   1.111 +{
   1.112 +  cinfo->input_iMCU_row = 0;
   1.113 +  start_iMCU_row(cinfo);
   1.114 +}
   1.115 +
   1.116 +
   1.117 +/*
   1.118 + * Initialize for an output processing pass.
   1.119 + */
   1.120 +
   1.121 +METHODDEF(void)
   1.122 +start_output_pass (j_decompress_ptr cinfo)
   1.123 +{
   1.124 +#ifdef BLOCK_SMOOTHING_SUPPORTED
   1.125 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   1.126 +
   1.127 +  /* If multipass, check to see whether to use block smoothing on this pass */
   1.128 +  if (coef->pub.coef_arrays != NULL) {
   1.129 +    if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
   1.130 +      coef->pub.decompress_data = decompress_smooth_data;
   1.131 +    else
   1.132 +      coef->pub.decompress_data = decompress_data;
   1.133 +  }
   1.134 +#endif
   1.135 +  cinfo->output_iMCU_row = 0;
   1.136 +}
   1.137 +
   1.138 +
   1.139 +/*
   1.140 + * Decompress and return some data in the single-pass case.
   1.141 + * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
   1.142 + * Input and output must run in lockstep since we have only a one-MCU buffer.
   1.143 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
   1.144 + *
   1.145 + * NB: output_buf contains a plane for each component in image,
   1.146 + * which we index according to the component's SOF position.
   1.147 + */
   1.148 +
   1.149 +METHODDEF(int)
   1.150 +decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
   1.151 +{
   1.152 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   1.153 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
   1.154 +  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
   1.155 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
   1.156 +  int blkn, ci, xindex, yindex, yoffset, useful_width;
   1.157 +  JSAMPARRAY output_ptr;
   1.158 +  JDIMENSION start_col, output_col;
   1.159 +  jpeg_component_info *compptr;
   1.160 +  inverse_DCT_method_ptr inverse_DCT;
   1.161 +
   1.162 +  /* Loop to process as much as one whole iMCU row */
   1.163 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
   1.164 +       yoffset++) {
   1.165 +    for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
   1.166 +	 MCU_col_num++) {
   1.167 +      /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */
   1.168 +      jzero_far((void FAR *) coef->MCU_buffer[0],
   1.169 +		(size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
   1.170 +      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
   1.171 +	/* Suspension forced; update state counters and exit */
   1.172 +	coef->MCU_vert_offset = yoffset;
   1.173 +	coef->MCU_ctr = MCU_col_num;
   1.174 +	return JPEG_SUSPENDED;
   1.175 +      }
   1.176 +      /* Determine where data should go in output_buf and do the IDCT thing.
   1.177 +       * We skip dummy blocks at the right and bottom edges (but blkn gets
   1.178 +       * incremented past them!).  Note the inner loop relies on having
   1.179 +       * allocated the MCU_buffer[] blocks sequentially.
   1.180 +       */
   1.181 +      blkn = 0;			/* index of current DCT block within MCU */
   1.182 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1.183 +	compptr = cinfo->cur_comp_info[ci];
   1.184 +	/* Don't bother to IDCT an uninteresting component. */
   1.185 +	if (! compptr->component_needed) {
   1.186 +	  blkn += compptr->MCU_blocks;
   1.187 +	  continue;
   1.188 +	}
   1.189 +	inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
   1.190 +	useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
   1.191 +						    : compptr->last_col_width;
   1.192 +	output_ptr = output_buf[compptr->component_index] +
   1.193 +	  yoffset * compptr->DCT_scaled_size;
   1.194 +	start_col = MCU_col_num * compptr->MCU_sample_width;
   1.195 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
   1.196 +	  if (cinfo->input_iMCU_row < last_iMCU_row ||
   1.197 +	      yoffset+yindex < compptr->last_row_height) {
   1.198 +	    output_col = start_col;
   1.199 +	    for (xindex = 0; xindex < useful_width; xindex++) {
   1.200 +	      (*inverse_DCT) (cinfo, compptr,
   1.201 +			      (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
   1.202 +			      output_ptr, output_col);
   1.203 +	      output_col += compptr->DCT_scaled_size;
   1.204 +	    }
   1.205 +	  }
   1.206 +	  blkn += compptr->MCU_width;
   1.207 +	  output_ptr += compptr->DCT_scaled_size;
   1.208 +	}
   1.209 +      }
   1.210 +    }
   1.211 +    /* Completed an MCU row, but perhaps not an iMCU row */
   1.212 +    coef->MCU_ctr = 0;
   1.213 +  }
   1.214 +  /* Completed the iMCU row, advance counters for next one */
   1.215 +  cinfo->output_iMCU_row++;
   1.216 +  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
   1.217 +    start_iMCU_row(cinfo);
   1.218 +    return JPEG_ROW_COMPLETED;
   1.219 +  }
   1.220 +  /* Completed the scan */
   1.221 +  (*cinfo->inputctl->finish_input_pass) (cinfo);
   1.222 +  return JPEG_SCAN_COMPLETED;
   1.223 +}
   1.224 +
   1.225 +
   1.226 +/*
   1.227 + * Dummy consume-input routine for single-pass operation.
   1.228 + */
   1.229 +
   1.230 +METHODDEF(int)
   1.231 +dummy_consume_data (j_decompress_ptr cinfo)
   1.232 +{
   1.233 +  return JPEG_SUSPENDED;	/* Always indicate nothing was done */
   1.234 +}
   1.235 +
   1.236 +
   1.237 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   1.238 +
   1.239 +/*
   1.240 + * Consume input data and store it in the full-image coefficient buffer.
   1.241 + * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
   1.242 + * ie, v_samp_factor block rows for each component in the scan.
   1.243 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
   1.244 + */
   1.245 +
   1.246 +METHODDEF(int)
   1.247 +consume_data (j_decompress_ptr cinfo)
   1.248 +{
   1.249 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   1.250 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
   1.251 +  int blkn, ci, xindex, yindex, yoffset;
   1.252 +  JDIMENSION start_col;
   1.253 +  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
   1.254 +  JBLOCKROW buffer_ptr;
   1.255 +  jpeg_component_info *compptr;
   1.256 +
   1.257 +  /* Align the virtual buffers for the components used in this scan. */
   1.258 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1.259 +    compptr = cinfo->cur_comp_info[ci];
   1.260 +    buffer[ci] = (*cinfo->mem->access_virt_barray)
   1.261 +      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
   1.262 +       cinfo->input_iMCU_row * compptr->v_samp_factor,
   1.263 +       (JDIMENSION) compptr->v_samp_factor, TRUE);
   1.264 +    /* Note: entropy decoder expects buffer to be zeroed,
   1.265 +     * but this is handled automatically by the memory manager
   1.266 +     * because we requested a pre-zeroed array.
   1.267 +     */
   1.268 +  }
   1.269 +
   1.270 +  /* Loop to process one whole iMCU row */
   1.271 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
   1.272 +       yoffset++) {
   1.273 +    for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
   1.274 +	 MCU_col_num++) {
   1.275 +      /* Construct list of pointers to DCT blocks belonging to this MCU */
   1.276 +      blkn = 0;			/* index of current DCT block within MCU */
   1.277 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1.278 +	compptr = cinfo->cur_comp_info[ci];
   1.279 +	start_col = MCU_col_num * compptr->MCU_width;
   1.280 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
   1.281 +	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
   1.282 +	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
   1.283 +	    coef->MCU_buffer[blkn++] = buffer_ptr++;
   1.284 +	  }
   1.285 +	}
   1.286 +      }
   1.287 +      /* Try to fetch the MCU. */
   1.288 +      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
   1.289 +	/* Suspension forced; update state counters and exit */
   1.290 +	coef->MCU_vert_offset = yoffset;
   1.291 +	coef->MCU_ctr = MCU_col_num;
   1.292 +	return JPEG_SUSPENDED;
   1.293 +      }
   1.294 +    }
   1.295 +    /* Completed an MCU row, but perhaps not an iMCU row */
   1.296 +    coef->MCU_ctr = 0;
   1.297 +  }
   1.298 +  /* Completed the iMCU row, advance counters for next one */
   1.299 +  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
   1.300 +    start_iMCU_row(cinfo);
   1.301 +    return JPEG_ROW_COMPLETED;
   1.302 +  }
   1.303 +  /* Completed the scan */
   1.304 +  (*cinfo->inputctl->finish_input_pass) (cinfo);
   1.305 +  return JPEG_SCAN_COMPLETED;
   1.306 +}
   1.307 +
   1.308 +
   1.309 +/*
   1.310 + * Decompress and return some data in the multi-pass case.
   1.311 + * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
   1.312 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
   1.313 + *
   1.314 + * NB: output_buf contains a plane for each component in image.
   1.315 + */
   1.316 +
   1.317 +METHODDEF(int)
   1.318 +decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
   1.319 +{
   1.320 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   1.321 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
   1.322 +  JDIMENSION block_num;
   1.323 +  int ci, block_row, block_rows;
   1.324 +  JBLOCKARRAY buffer;
   1.325 +  JBLOCKROW buffer_ptr;
   1.326 +  JSAMPARRAY output_ptr;
   1.327 +  JDIMENSION output_col;
   1.328 +  jpeg_component_info *compptr;
   1.329 +  inverse_DCT_method_ptr inverse_DCT;
   1.330 +
   1.331 +  /* Force some input to be done if we are getting ahead of the input. */
   1.332 +  while (cinfo->input_scan_number < cinfo->output_scan_number ||
   1.333 +	 (cinfo->input_scan_number == cinfo->output_scan_number &&
   1.334 +	  cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
   1.335 +    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
   1.336 +      return JPEG_SUSPENDED;
   1.337 +  }
   1.338 +
   1.339 +  /* OK, output from the virtual arrays. */
   1.340 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.341 +       ci++, compptr++) {
   1.342 +    /* Don't bother to IDCT an uninteresting component. */
   1.343 +    if (! compptr->component_needed)
   1.344 +      continue;
   1.345 +    /* Align the virtual buffer for this component. */
   1.346 +    buffer = (*cinfo->mem->access_virt_barray)
   1.347 +      ((j_common_ptr) cinfo, coef->whole_image[ci],
   1.348 +       cinfo->output_iMCU_row * compptr->v_samp_factor,
   1.349 +       (JDIMENSION) compptr->v_samp_factor, FALSE);
   1.350 +    /* Count non-dummy DCT block rows in this iMCU row. */
   1.351 +    if (cinfo->output_iMCU_row < last_iMCU_row)
   1.352 +      block_rows = compptr->v_samp_factor;
   1.353 +    else {
   1.354 +      /* NB: can't use last_row_height here; it is input-side-dependent! */
   1.355 +      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
   1.356 +      if (block_rows == 0) block_rows = compptr->v_samp_factor;
   1.357 +    }
   1.358 +    inverse_DCT = cinfo->idct->inverse_DCT[ci];
   1.359 +    output_ptr = output_buf[ci];
   1.360 +    /* Loop over all DCT blocks to be processed. */
   1.361 +    for (block_row = 0; block_row < block_rows; block_row++) {
   1.362 +      buffer_ptr = buffer[block_row];
   1.363 +      output_col = 0;
   1.364 +      for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
   1.365 +	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
   1.366 +			output_ptr, output_col);
   1.367 +	buffer_ptr++;
   1.368 +	output_col += compptr->DCT_scaled_size;
   1.369 +      }
   1.370 +      output_ptr += compptr->DCT_scaled_size;
   1.371 +    }
   1.372 +  }
   1.373 +
   1.374 +  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
   1.375 +    return JPEG_ROW_COMPLETED;
   1.376 +  return JPEG_SCAN_COMPLETED;
   1.377 +}
   1.378 +
   1.379 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
   1.380 +
   1.381 +
   1.382 +#ifdef BLOCK_SMOOTHING_SUPPORTED
   1.383 +
   1.384 +/*
   1.385 + * This code applies interblock smoothing as described by section K.8
   1.386 + * of the JPEG standard: the first 5 AC coefficients are estimated from
   1.387 + * the DC values of a DCT block and its 8 neighboring blocks.
   1.388 + * We apply smoothing only for progressive JPEG decoding, and only if
   1.389 + * the coefficients it can estimate are not yet known to full precision.
   1.390 + */
   1.391 +
   1.392 +/* Natural-order array positions of the first 5 zigzag-order coefficients */
   1.393 +#define Q01_POS  1
   1.394 +#define Q10_POS  8
   1.395 +#define Q20_POS  16
   1.396 +#define Q11_POS  9
   1.397 +#define Q02_POS  2
   1.398 +
   1.399 +/*
   1.400 + * Determine whether block smoothing is applicable and safe.
   1.401 + * We also latch the current states of the coef_bits[] entries for the
   1.402 + * AC coefficients; otherwise, if the input side of the decompressor
   1.403 + * advances into a new scan, we might think the coefficients are known
   1.404 + * more accurately than they really are.
   1.405 + */
   1.406 +
   1.407 +LOCAL(boolean)
   1.408 +smoothing_ok (j_decompress_ptr cinfo)
   1.409 +{
   1.410 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   1.411 +  boolean smoothing_useful = FALSE;
   1.412 +  int ci, coefi;
   1.413 +  jpeg_component_info *compptr;
   1.414 +  JQUANT_TBL * qtable;
   1.415 +  int * coef_bits;
   1.416 +  int * coef_bits_latch;
   1.417 +
   1.418 +  if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
   1.419 +    return FALSE;
   1.420 +
   1.421 +  /* Allocate latch area if not already done */
   1.422 +  if (coef->coef_bits_latch == NULL)
   1.423 +    coef->coef_bits_latch = (int *)
   1.424 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.425 +				  cinfo->num_components *
   1.426 +				  (SAVED_COEFS * SIZEOF(int)));
   1.427 +  coef_bits_latch = coef->coef_bits_latch;
   1.428 +
   1.429 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.430 +       ci++, compptr++) {
   1.431 +    /* All components' quantization values must already be latched. */
   1.432 +    if ((qtable = compptr->quant_table) == NULL)
   1.433 +      return FALSE;
   1.434 +    /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
   1.435 +    if (qtable->quantval[0] == 0 ||
   1.436 +	qtable->quantval[Q01_POS] == 0 ||
   1.437 +	qtable->quantval[Q10_POS] == 0 ||
   1.438 +	qtable->quantval[Q20_POS] == 0 ||
   1.439 +	qtable->quantval[Q11_POS] == 0 ||
   1.440 +	qtable->quantval[Q02_POS] == 0)
   1.441 +      return FALSE;
   1.442 +    /* DC values must be at least partly known for all components. */
   1.443 +    coef_bits = cinfo->coef_bits[ci];
   1.444 +    if (coef_bits[0] < 0)
   1.445 +      return FALSE;
   1.446 +    /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
   1.447 +    for (coefi = 1; coefi <= 5; coefi++) {
   1.448 +      coef_bits_latch[coefi] = coef_bits[coefi];
   1.449 +      if (coef_bits[coefi] != 0)
   1.450 +	smoothing_useful = TRUE;
   1.451 +    }
   1.452 +    coef_bits_latch += SAVED_COEFS;
   1.453 +  }
   1.454 +
   1.455 +  return smoothing_useful;
   1.456 +}
   1.457 +
   1.458 +
   1.459 +/*
   1.460 + * Variant of decompress_data for use when doing block smoothing.
   1.461 + */
   1.462 +
   1.463 +METHODDEF(int)
   1.464 +decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
   1.465 +{
   1.466 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   1.467 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
   1.468 +  JDIMENSION block_num, last_block_column;
   1.469 +  int ci, block_row, block_rows, access_rows;
   1.470 +  JBLOCKARRAY buffer;
   1.471 +  JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
   1.472 +  JSAMPARRAY output_ptr;
   1.473 +  JDIMENSION output_col;
   1.474 +  jpeg_component_info *compptr;
   1.475 +  inverse_DCT_method_ptr inverse_DCT;
   1.476 +  boolean first_row, last_row;
   1.477 +  JBLOCK workspace;
   1.478 +  int *coef_bits;
   1.479 +  JQUANT_TBL *quanttbl;
   1.480 +  INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
   1.481 +  int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
   1.482 +  int Al, pred;
   1.483 +
   1.484 +  /* Force some input to be done if we are getting ahead of the input. */
   1.485 +  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
   1.486 +	 ! cinfo->inputctl->eoi_reached) {
   1.487 +    if (cinfo->input_scan_number == cinfo->output_scan_number) {
   1.488 +      /* If input is working on current scan, we ordinarily want it to
   1.489 +       * have completed the current row.  But if input scan is DC,
   1.490 +       * we want it to keep one row ahead so that next block row's DC
   1.491 +       * values are up to date.
   1.492 +       */
   1.493 +      JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
   1.494 +      if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
   1.495 +	break;
   1.496 +    }
   1.497 +    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
   1.498 +      return JPEG_SUSPENDED;
   1.499 +  }
   1.500 +
   1.501 +  /* OK, output from the virtual arrays. */
   1.502 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.503 +       ci++, compptr++) {
   1.504 +    /* Don't bother to IDCT an uninteresting component. */
   1.505 +    if (! compptr->component_needed)
   1.506 +      continue;
   1.507 +    /* Count non-dummy DCT block rows in this iMCU row. */
   1.508 +    if (cinfo->output_iMCU_row < last_iMCU_row) {
   1.509 +      block_rows = compptr->v_samp_factor;
   1.510 +      access_rows = block_rows * 2; /* this and next iMCU row */
   1.511 +      last_row = FALSE;
   1.512 +    } else {
   1.513 +      /* NB: can't use last_row_height here; it is input-side-dependent! */
   1.514 +      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
   1.515 +      if (block_rows == 0) block_rows = compptr->v_samp_factor;
   1.516 +      access_rows = block_rows; /* this iMCU row only */
   1.517 +      last_row = TRUE;
   1.518 +    }
   1.519 +    /* Align the virtual buffer for this component. */
   1.520 +    if (cinfo->output_iMCU_row > 0) {
   1.521 +      access_rows += compptr->v_samp_factor; /* prior iMCU row too */
   1.522 +      buffer = (*cinfo->mem->access_virt_barray)
   1.523 +	((j_common_ptr) cinfo, coef->whole_image[ci],
   1.524 +	 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
   1.525 +	 (JDIMENSION) access_rows, FALSE);
   1.526 +      buffer += compptr->v_samp_factor;	/* point to current iMCU row */
   1.527 +      first_row = FALSE;
   1.528 +    } else {
   1.529 +      buffer = (*cinfo->mem->access_virt_barray)
   1.530 +	((j_common_ptr) cinfo, coef->whole_image[ci],
   1.531 +	 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
   1.532 +      first_row = TRUE;
   1.533 +    }
   1.534 +    /* Fetch component-dependent info */
   1.535 +    coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
   1.536 +    quanttbl = compptr->quant_table;
   1.537 +    Q00 = quanttbl->quantval[0];
   1.538 +    Q01 = quanttbl->quantval[Q01_POS];
   1.539 +    Q10 = quanttbl->quantval[Q10_POS];
   1.540 +    Q20 = quanttbl->quantval[Q20_POS];
   1.541 +    Q11 = quanttbl->quantval[Q11_POS];
   1.542 +    Q02 = quanttbl->quantval[Q02_POS];
   1.543 +    inverse_DCT = cinfo->idct->inverse_DCT[ci];
   1.544 +    output_ptr = output_buf[ci];
   1.545 +    /* Loop over all DCT blocks to be processed. */
   1.546 +    for (block_row = 0; block_row < block_rows; block_row++) {
   1.547 +      buffer_ptr = buffer[block_row];
   1.548 +      if (first_row && block_row == 0)
   1.549 +	prev_block_row = buffer_ptr;
   1.550 +      else
   1.551 +	prev_block_row = buffer[block_row-1];
   1.552 +      if (last_row && block_row == block_rows-1)
   1.553 +	next_block_row = buffer_ptr;
   1.554 +      else
   1.555 +	next_block_row = buffer[block_row+1];
   1.556 +      /* We fetch the surrounding DC values using a sliding-register approach.
   1.557 +       * Initialize all nine here so as to do the right thing on narrow pics.
   1.558 +       */
   1.559 +      DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
   1.560 +      DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
   1.561 +      DC7 = DC8 = DC9 = (int) next_block_row[0][0];
   1.562 +      output_col = 0;
   1.563 +      last_block_column = compptr->width_in_blocks - 1;
   1.564 +      for (block_num = 0; block_num <= last_block_column; block_num++) {
   1.565 +	/* Fetch current DCT block into workspace so we can modify it. */
   1.566 +	jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
   1.567 +	/* Update DC values */
   1.568 +	if (block_num < last_block_column) {
   1.569 +	  DC3 = (int) prev_block_row[1][0];
   1.570 +	  DC6 = (int) buffer_ptr[1][0];
   1.571 +	  DC9 = (int) next_block_row[1][0];
   1.572 +	}
   1.573 +	/* Compute coefficient estimates per K.8.
   1.574 +	 * An estimate is applied only if coefficient is still zero,
   1.575 +	 * and is not known to be fully accurate.
   1.576 +	 */
   1.577 +	/* AC01 */
   1.578 +	if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
   1.579 +	  num = 36 * Q00 * (DC4 - DC6);
   1.580 +	  if (num >= 0) {
   1.581 +	    pred = (int) (((Q01<<7) + num) / (Q01<<8));
   1.582 +	    if (Al > 0 && pred >= (1<<Al))
   1.583 +	      pred = (1<<Al)-1;
   1.584 +	  } else {
   1.585 +	    pred = (int) (((Q01<<7) - num) / (Q01<<8));
   1.586 +	    if (Al > 0 && pred >= (1<<Al))
   1.587 +	      pred = (1<<Al)-1;
   1.588 +	    pred = -pred;
   1.589 +	  }
   1.590 +	  workspace[1] = (JCOEF) pred;
   1.591 +	}
   1.592 +	/* AC10 */
   1.593 +	if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
   1.594 +	  num = 36 * Q00 * (DC2 - DC8);
   1.595 +	  if (num >= 0) {
   1.596 +	    pred = (int) (((Q10<<7) + num) / (Q10<<8));
   1.597 +	    if (Al > 0 && pred >= (1<<Al))
   1.598 +	      pred = (1<<Al)-1;
   1.599 +	  } else {
   1.600 +	    pred = (int) (((Q10<<7) - num) / (Q10<<8));
   1.601 +	    if (Al > 0 && pred >= (1<<Al))
   1.602 +	      pred = (1<<Al)-1;
   1.603 +	    pred = -pred;
   1.604 +	  }
   1.605 +	  workspace[8] = (JCOEF) pred;
   1.606 +	}
   1.607 +	/* AC20 */
   1.608 +	if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
   1.609 +	  num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
   1.610 +	  if (num >= 0) {
   1.611 +	    pred = (int) (((Q20<<7) + num) / (Q20<<8));
   1.612 +	    if (Al > 0 && pred >= (1<<Al))
   1.613 +	      pred = (1<<Al)-1;
   1.614 +	  } else {
   1.615 +	    pred = (int) (((Q20<<7) - num) / (Q20<<8));
   1.616 +	    if (Al > 0 && pred >= (1<<Al))
   1.617 +	      pred = (1<<Al)-1;
   1.618 +	    pred = -pred;
   1.619 +	  }
   1.620 +	  workspace[16] = (JCOEF) pred;
   1.621 +	}
   1.622 +	/* AC11 */
   1.623 +	if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
   1.624 +	  num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
   1.625 +	  if (num >= 0) {
   1.626 +	    pred = (int) (((Q11<<7) + num) / (Q11<<8));
   1.627 +	    if (Al > 0 && pred >= (1<<Al))
   1.628 +	      pred = (1<<Al)-1;
   1.629 +	  } else {
   1.630 +	    pred = (int) (((Q11<<7) - num) / (Q11<<8));
   1.631 +	    if (Al > 0 && pred >= (1<<Al))
   1.632 +	      pred = (1<<Al)-1;
   1.633 +	    pred = -pred;
   1.634 +	  }
   1.635 +	  workspace[9] = (JCOEF) pred;
   1.636 +	}
   1.637 +	/* AC02 */
   1.638 +	if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
   1.639 +	  num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
   1.640 +	  if (num >= 0) {
   1.641 +	    pred = (int) (((Q02<<7) + num) / (Q02<<8));
   1.642 +	    if (Al > 0 && pred >= (1<<Al))
   1.643 +	      pred = (1<<Al)-1;
   1.644 +	  } else {
   1.645 +	    pred = (int) (((Q02<<7) - num) / (Q02<<8));
   1.646 +	    if (Al > 0 && pred >= (1<<Al))
   1.647 +	      pred = (1<<Al)-1;
   1.648 +	    pred = -pred;
   1.649 +	  }
   1.650 +	  workspace[2] = (JCOEF) pred;
   1.651 +	}
   1.652 +	/* OK, do the IDCT */
   1.653 +	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
   1.654 +			output_ptr, output_col);
   1.655 +	/* Advance for next column */
   1.656 +	DC1 = DC2; DC2 = DC3;
   1.657 +	DC4 = DC5; DC5 = DC6;
   1.658 +	DC7 = DC8; DC8 = DC9;
   1.659 +	buffer_ptr++, prev_block_row++, next_block_row++;
   1.660 +	output_col += compptr->DCT_scaled_size;
   1.661 +      }
   1.662 +      output_ptr += compptr->DCT_scaled_size;
   1.663 +    }
   1.664 +  }
   1.665 +
   1.666 +  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
   1.667 +    return JPEG_ROW_COMPLETED;
   1.668 +  return JPEG_SCAN_COMPLETED;
   1.669 +}
   1.670 +
   1.671 +#endif /* BLOCK_SMOOTHING_SUPPORTED */
   1.672 +
   1.673 +
   1.674 +/*
   1.675 + * Initialize coefficient buffer controller.
   1.676 + */
   1.677 +
   1.678 +GLOBAL(void)
   1.679 +jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
   1.680 +{
   1.681 +  my_coef_ptr coef;
   1.682 +
   1.683 +  coef = (my_coef_ptr)
   1.684 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.685 +				SIZEOF(my_coef_controller));
   1.686 +  cinfo->coef = (struct jpeg_d_coef_controller *) coef;
   1.687 +  coef->pub.start_input_pass = start_input_pass;
   1.688 +  coef->pub.start_output_pass = start_output_pass;
   1.689 +#ifdef BLOCK_SMOOTHING_SUPPORTED
   1.690 +  coef->coef_bits_latch = NULL;
   1.691 +#endif
   1.692 +
   1.693 +  /* Create the coefficient buffer. */
   1.694 +  if (need_full_buffer) {
   1.695 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   1.696 +    /* Allocate a full-image virtual array for each component, */
   1.697 +    /* padded to a multiple of samp_factor DCT blocks in each direction. */
   1.698 +    /* Note we ask for a pre-zeroed array. */
   1.699 +    int ci, access_rows;
   1.700 +    jpeg_component_info *compptr;
   1.701 +
   1.702 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.703 +	 ci++, compptr++) {
   1.704 +      access_rows = compptr->v_samp_factor;
   1.705 +#ifdef BLOCK_SMOOTHING_SUPPORTED
   1.706 +      /* If block smoothing could be used, need a bigger window */
   1.707 +      if (cinfo->progressive_mode)
   1.708 +	access_rows *= 3;
   1.709 +#endif
   1.710 +      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
   1.711 +	((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
   1.712 +	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
   1.713 +				(long) compptr->h_samp_factor),
   1.714 +	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
   1.715 +				(long) compptr->v_samp_factor),
   1.716 +	 (JDIMENSION) access_rows);
   1.717 +    }
   1.718 +    coef->pub.consume_data = consume_data;
   1.719 +    coef->pub.decompress_data = decompress_data;
   1.720 +    coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
   1.721 +#else
   1.722 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.723 +#endif
   1.724 +  } else {
   1.725 +    /* We only need a single-MCU buffer. */
   1.726 +    JBLOCKROW buffer;
   1.727 +    int i;
   1.728 +
   1.729 +    buffer = (JBLOCKROW)
   1.730 +      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.731 +				  D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
   1.732 +    for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
   1.733 +      coef->MCU_buffer[i] = buffer + i;
   1.734 +    }
   1.735 +    coef->pub.consume_data = dummy_consume_data;
   1.736 +    coef->pub.decompress_data = decompress_onepass;
   1.737 +    coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
   1.738 +  }
   1.739 +}